Mercurial > libavformat.hg
annotate utils.c @ 1139:ff241582890f libavformat
do not fail av_find_stream_info() if params for all streams could not be found
author | mru |
---|---|
date | Thu, 29 Jun 2006 19:02:15 +0000 |
parents | 107f90e1551b |
children | ef9bf68071d3 |
rev | line source |
---|---|
0 | 1 /* |
2 * Various utilities for ffmpeg system | |
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
887
diff
changeset
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 */ |
19 #include "avformat.h" | |
20 | |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
21 #undef NDEBUG |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
22 #include <assert.h> |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
23 |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
24 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
25 * @file libavformat/utils.c |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
26 * Various utility functions for using ffmpeg library. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
27 */ |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
28 |
1135
107f90e1551b
cleanup patch from Diego Petten <flameeyes@gentoo.org>
lu_zero
parents:
1127
diff
changeset
|
29 static void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den); |
107f90e1551b
cleanup patch from Diego Petten <flameeyes@gentoo.org>
lu_zero
parents:
1127
diff
changeset
|
30 static void av_frac_add(AVFrac *f, int64_t incr); |
107f90e1551b
cleanup patch from Diego Petten <flameeyes@gentoo.org>
lu_zero
parents:
1127
diff
changeset
|
31 static void av_frac_set(AVFrac *f, int64_t val); |
107f90e1551b
cleanup patch from Diego Petten <flameeyes@gentoo.org>
lu_zero
parents:
1127
diff
changeset
|
32 |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
33 /** head of registered input format linked list. */ |
512
8dfd00fb6a6d
Minor Patch for shared libs on Mac OSX by (Bill May <wmay at cisco dot com>)
michael
parents:
511
diff
changeset
|
34 AVInputFormat *first_iformat = NULL; |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
35 /** head of registered output format linked list. */ |
512
8dfd00fb6a6d
Minor Patch for shared libs on Mac OSX by (Bill May <wmay at cisco dot com>)
michael
parents:
511
diff
changeset
|
36 AVOutputFormat *first_oformat = NULL; |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
37 /** head of registered image format linked list. */ |
512
8dfd00fb6a6d
Minor Patch for shared libs on Mac OSX by (Bill May <wmay at cisco dot com>)
michael
parents:
511
diff
changeset
|
38 AVImageFormat *first_image_format = NULL; |
0 | 39 |
40 void av_register_input_format(AVInputFormat *format) | |
41 { | |
42 AVInputFormat **p; | |
43 p = &first_iformat; | |
44 while (*p != NULL) p = &(*p)->next; | |
45 *p = format; | |
46 format->next = NULL; | |
47 } | |
48 | |
49 void av_register_output_format(AVOutputFormat *format) | |
50 { | |
51 AVOutputFormat **p; | |
52 p = &first_oformat; | |
53 while (*p != NULL) p = &(*p)->next; | |
54 *p = format; | |
55 format->next = NULL; | |
56 } | |
57 | |
58 int match_ext(const char *filename, const char *extensions) | |
59 { | |
60 const char *ext, *p; | |
61 char ext1[32], *q; | |
62 | |
453 | 63 if(!filename) |
64 return 0; | |
885 | 65 |
0 | 66 ext = strrchr(filename, '.'); |
67 if (ext) { | |
68 ext++; | |
69 p = extensions; | |
70 for(;;) { | |
71 q = ext1; | |
885 | 72 while (*p != '\0' && *p != ',' && q-ext1<sizeof(ext1)-1) |
0 | 73 *q++ = *p++; |
74 *q = '\0'; | |
885 | 75 if (!strcasecmp(ext1, ext)) |
0 | 76 return 1; |
885 | 77 if (*p == '\0') |
0 | 78 break; |
79 p++; | |
80 } | |
81 } | |
82 return 0; | |
83 } | |
84 | |
885 | 85 AVOutputFormat *guess_format(const char *short_name, const char *filename, |
0 | 86 const char *mime_type) |
87 { | |
88 AVOutputFormat *fmt, *fmt_found; | |
89 int score_max, score; | |
90 | |
20 | 91 /* specific test for image sequences */ |
885 | 92 if (!short_name && filename && |
21 | 93 filename_number_test(filename) >= 0 && |
583 | 94 av_guess_image2_codec(filename) != CODEC_ID_NONE) { |
95 return guess_format("image2", NULL, NULL); | |
96 } | |
885 | 97 if (!short_name && filename && |
583 | 98 filename_number_test(filename) >= 0 && |
21 | 99 guess_image_format(filename)) { |
20 | 100 return guess_format("image", NULL, NULL); |
101 } | |
102 | |
0 | 103 /* find the proper file type */ |
104 fmt_found = NULL; | |
105 score_max = 0; | |
106 fmt = first_oformat; | |
107 while (fmt != NULL) { | |
108 score = 0; | |
109 if (fmt->name && short_name && !strcmp(fmt->name, short_name)) | |
110 score += 100; | |
111 if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type)) | |
112 score += 10; | |
885 | 113 if (filename && fmt->extensions && |
0 | 114 match_ext(filename, fmt->extensions)) { |
115 score += 5; | |
116 } | |
117 if (score > score_max) { | |
118 score_max = score; | |
119 fmt_found = fmt; | |
120 } | |
121 fmt = fmt->next; | |
122 } | |
123 return fmt_found; | |
885 | 124 } |
125 | |
126 AVOutputFormat *guess_stream_format(const char *short_name, const char *filename, | |
0 | 127 const char *mime_type) |
128 { | |
129 AVOutputFormat *fmt = guess_format(short_name, filename, mime_type); | |
130 | |
131 if (fmt) { | |
132 AVOutputFormat *stream_fmt; | |
133 char stream_format_name[64]; | |
134 | |
135 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream", fmt->name); | |
136 stream_fmt = guess_format(stream_format_name, NULL, NULL); | |
137 | |
138 if (stream_fmt) | |
139 fmt = stream_fmt; | |
140 } | |
141 | |
142 return fmt; | |
143 } | |
144 | |
583 | 145 /** |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
146 * Guesses the codec id based upon muxer and filename. |
583 | 147 */ |
885 | 148 enum CodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name, |
583 | 149 const char *filename, const char *mime_type, enum CodecType type){ |
150 if(type == CODEC_TYPE_VIDEO){ | |
151 enum CodecID codec_id= CODEC_ID_NONE; | |
152 | |
586 | 153 if(!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")){ |
583 | 154 codec_id= av_guess_image2_codec(filename); |
155 } | |
156 if(codec_id == CODEC_ID_NONE) | |
157 codec_id= fmt->video_codec; | |
158 return codec_id; | |
159 }else if(type == CODEC_TYPE_AUDIO) | |
160 return fmt->audio_codec; | |
161 else | |
162 return CODEC_ID_NONE; | |
163 } | |
164 | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
165 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
166 * finds AVInputFormat based on input format's short name. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
167 */ |
0 | 168 AVInputFormat *av_find_input_format(const char *short_name) |
169 { | |
170 AVInputFormat *fmt; | |
171 for(fmt = first_iformat; fmt != NULL; fmt = fmt->next) { | |
172 if (!strcmp(fmt->name, short_name)) | |
173 return fmt; | |
174 } | |
175 return NULL; | |
176 } | |
177 | |
178 /* memory handling */ | |
179 | |
180 /** | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
181 * Default packet destructor. |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
182 */ |
797
f5a231a9a2f1
support changing in bitstream global headers into extradata style and back
michael
parents:
786
diff
changeset
|
183 void av_destruct_packet(AVPacket *pkt) |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
184 { |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
185 av_free(pkt->data); |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
186 pkt->data = NULL; pkt->size = 0; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
187 } |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
188 |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
189 /** |
0 | 190 * Allocate the payload of a packet and intialized its fields to default values. |
191 * | |
192 * @param pkt packet | |
193 * @param size wanted payload size | |
194 * @return 0 if OK. AVERROR_xxx otherwise. | |
195 */ | |
196 int av_new_packet(AVPacket *pkt, int size) | |
197 { | |
639 | 198 void *data; |
199 if((unsigned)size > (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE) | |
885 | 200 return AVERROR_NOMEM; |
639 | 201 data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
202 if (!data) |
0 | 203 return AVERROR_NOMEM; |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
204 memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
0 | 205 |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
206 av_init_packet(pkt); |
885 | 207 pkt->data = data; |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
208 pkt->size = size; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
209 pkt->destruct = av_destruct_packet; |
0 | 210 return 0; |
211 } | |
212 | |
775 | 213 /** |
214 * Allocate and read the payload of a packet and intialized its fields to default values. | |
215 * | |
216 * @param pkt packet | |
217 * @param size wanted payload size | |
218 * @return >0 (read size) if OK. AVERROR_xxx otherwise. | |
219 */ | |
220 int av_get_packet(ByteIOContext *s, AVPacket *pkt, int size) | |
221 { | |
222 int ret= av_new_packet(pkt, size); | |
223 | |
224 if(ret<0) | |
225 return ret; | |
226 | |
227 pkt->pos= url_ftell(s); | |
228 | |
229 ret= get_buffer(s, pkt->data, size); | |
230 if(ret<=0) | |
231 av_free_packet(pkt); | |
232 else | |
233 pkt->size= ret; | |
234 | |
235 return ret; | |
236 } | |
237 | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
238 /* This is a hack - the packet memory allocation stuff is broken. The |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
239 packet is allocated if it was not really allocated */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
240 int av_dup_packet(AVPacket *pkt) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
241 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
242 if (pkt->destruct != av_destruct_packet) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
243 uint8_t *data; |
330 | 244 /* we duplicate the packet and don't forget to put the padding |
245 again */ | |
639 | 246 if((unsigned)pkt->size > (unsigned)pkt->size + FF_INPUT_BUFFER_PADDING_SIZE) |
885 | 247 return AVERROR_NOMEM; |
330 | 248 data = av_malloc(pkt->size + FF_INPUT_BUFFER_PADDING_SIZE); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
249 if (!data) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
250 return AVERROR_NOMEM; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
251 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
252 memcpy(data, pkt->data, pkt->size); |
330 | 253 memset(data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
254 pkt->data = data; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
255 pkt->destruct = av_destruct_packet; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
256 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
257 return 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
258 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
259 |
0 | 260 /* fifo handling */ |
261 | |
262 int fifo_init(FifoBuffer *f, int size) | |
263 { | |
264 f->buffer = av_malloc(size); | |
265 if (!f->buffer) | |
266 return -1; | |
267 f->end = f->buffer + size; | |
268 f->wptr = f->rptr = f->buffer; | |
269 return 0; | |
270 } | |
271 | |
272 void fifo_free(FifoBuffer *f) | |
273 { | |
274 av_free(f->buffer); | |
275 } | |
276 | |
65 | 277 int fifo_size(FifoBuffer *f, uint8_t *rptr) |
0 | 278 { |
279 int size; | |
885 | 280 |
602 | 281 if(!rptr) |
282 rptr= f->rptr; | |
0 | 283 |
284 if (f->wptr >= rptr) { | |
285 size = f->wptr - rptr; | |
286 } else { | |
287 size = (f->end - rptr) + (f->wptr - f->buffer); | |
288 } | |
289 return size; | |
290 } | |
291 | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
292 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
293 * Get data from the fifo (returns -1 if not enough data). |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
294 */ |
65 | 295 int fifo_read(FifoBuffer *f, uint8_t *buf, int buf_size, uint8_t **rptr_ptr) |
0 | 296 { |
602 | 297 uint8_t *rptr; |
0 | 298 int size, len; |
299 | |
602 | 300 if(!rptr_ptr) |
301 rptr_ptr= &f->rptr; | |
302 rptr = *rptr_ptr; | |
303 | |
0 | 304 if (f->wptr >= rptr) { |
305 size = f->wptr - rptr; | |
306 } else { | |
307 size = (f->end - rptr) + (f->wptr - f->buffer); | |
308 } | |
885 | 309 |
0 | 310 if (size < buf_size) |
311 return -1; | |
312 while (buf_size > 0) { | |
313 len = f->end - rptr; | |
314 if (len > buf_size) | |
315 len = buf_size; | |
316 memcpy(buf, rptr, len); | |
317 buf += len; | |
318 rptr += len; | |
319 if (rptr >= f->end) | |
320 rptr = f->buffer; | |
321 buf_size -= len; | |
322 } | |
323 *rptr_ptr = rptr; | |
324 return 0; | |
325 } | |
326 | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
327 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
328 * Resizes a FIFO. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
329 */ |
639 | 330 void fifo_realloc(FifoBuffer *f, unsigned int new_size){ |
331 unsigned int old_size= f->end - f->buffer; | |
885 | 332 |
602 | 333 if(old_size < new_size){ |
334 uint8_t *old= f->buffer; | |
335 | |
336 f->buffer= av_realloc(f->buffer, new_size); | |
337 | |
338 f->rptr += f->buffer - old; | |
339 f->wptr += f->buffer - old; | |
340 | |
341 if(f->wptr < f->rptr){ | |
342 memmove(f->rptr + new_size - old_size, f->rptr, f->buffer + old_size - f->rptr); | |
343 f->rptr += new_size - old_size; | |
344 } | |
345 f->end= f->buffer + new_size; | |
346 } | |
347 } | |
348 | |
1123
6992dd78ff68
Add (mostly) const to variable and parameter declaration, where a char* was
diego
parents:
1120
diff
changeset
|
349 void fifo_write(FifoBuffer *f, const uint8_t *buf, int size, uint8_t **wptr_ptr) |
0 | 350 { |
351 int len; | |
65 | 352 uint8_t *wptr; |
602 | 353 |
354 if(!wptr_ptr) | |
355 wptr_ptr= &f->wptr; | |
0 | 356 wptr = *wptr_ptr; |
602 | 357 |
0 | 358 while (size > 0) { |
359 len = f->end - wptr; | |
360 if (len > size) | |
361 len = size; | |
362 memcpy(wptr, buf, len); | |
363 wptr += len; | |
364 if (wptr >= f->end) | |
365 wptr = f->buffer; | |
366 buf += len; | |
367 size -= len; | |
368 } | |
369 *wptr_ptr = wptr; | |
370 } | |
371 | |
542 | 372 /* get data from the fifo (return -1 if not enough data) */ |
373 int put_fifo(ByteIOContext *pb, FifoBuffer *f, int buf_size, uint8_t **rptr_ptr) | |
374 { | |
375 uint8_t *rptr = *rptr_ptr; | |
376 int size, len; | |
377 | |
378 if (f->wptr >= rptr) { | |
379 size = f->wptr - rptr; | |
380 } else { | |
381 size = (f->end - rptr) + (f->wptr - f->buffer); | |
382 } | |
885 | 383 |
542 | 384 if (size < buf_size) |
385 return -1; | |
386 while (buf_size > 0) { | |
387 len = f->end - rptr; | |
388 if (len > buf_size) | |
389 len = buf_size; | |
390 put_buffer(pb, rptr, len); | |
391 rptr += len; | |
392 if (rptr >= f->end) | |
393 rptr = f->buffer; | |
394 buf_size -= len; | |
395 } | |
396 *rptr_ptr = rptr; | |
397 return 0; | |
398 } | |
399 | |
0 | 400 int filename_number_test(const char *filename) |
401 { | |
402 char buf[1024]; | |
453 | 403 if(!filename) |
404 return -1; | |
0 | 405 return get_frame_filename(buf, sizeof(buf), filename, 1); |
406 } | |
407 | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
408 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
409 * Guess file format. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
410 */ |
0 | 411 AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened) |
412 { | |
413 AVInputFormat *fmt1, *fmt; | |
414 int score, score_max; | |
415 | |
416 fmt = NULL; | |
417 score_max = 0; | |
418 for(fmt1 = first_iformat; fmt1 != NULL; fmt1 = fmt1->next) { | |
419 if (!is_opened && !(fmt1->flags & AVFMT_NOFILE)) | |
420 continue; | |
421 score = 0; | |
422 if (fmt1->read_probe) { | |
423 score = fmt1->read_probe(pd); | |
424 } else if (fmt1->extensions) { | |
425 if (match_ext(pd->filename, fmt1->extensions)) { | |
426 score = 50; | |
427 } | |
885 | 428 } |
0 | 429 if (score > score_max) { |
430 score_max = score; | |
431 fmt = fmt1; | |
432 } | |
433 } | |
434 return fmt; | |
435 } | |
436 | |
437 /************************************************************/ | |
438 /* input media file */ | |
439 | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
440 /** |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
441 * Open a media file from an IO stream. 'fmt' must be specified. |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
442 */ |
371 | 443 static const char* format_to_name(void* ptr) |
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
444 { |
371 | 445 AVFormatContext* fc = (AVFormatContext*) ptr; |
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
446 if(fc->iformat) return fc->iformat->name; |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
447 else if(fc->oformat) return fc->oformat->name; |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
448 else return "NULL"; |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
449 } |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
450 |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
451 static const AVClass av_format_context_class = { "AVFormatContext", format_to_name }; |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
452 |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
453 AVFormatContext *av_alloc_format_context(void) |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
454 { |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
455 AVFormatContext *ic; |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
456 ic = av_mallocz(sizeof(AVFormatContext)); |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
457 if (!ic) return ic; |
371 | 458 ic->av_class = &av_format_context_class; |
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
459 return ic; |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
460 } |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
461 |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
462 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
463 * Allocates all the structures needed to read an input stream. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
464 * This does not open the needed codecs for decoding the stream[s]. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
465 */ |
885 | 466 int av_open_input_stream(AVFormatContext **ic_ptr, |
467 ByteIOContext *pb, const char *filename, | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
468 AVInputFormat *fmt, AVFormatParameters *ap) |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
469 { |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
470 int err; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
471 AVFormatContext *ic; |
1003 | 472 AVFormatParameters default_ap; |
473 | |
474 if(!ap){ | |
475 ap=&default_ap; | |
476 memset(ap, 0, sizeof(default_ap)); | |
477 } | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
478 |
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
479 ic = av_alloc_format_context(); |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
480 if (!ic) { |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
481 err = AVERROR_NOMEM; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
482 goto fail; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
483 } |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
484 ic->iformat = fmt; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
485 if (pb) |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
486 ic->pb = *pb; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
487 ic->duration = AV_NOPTS_VALUE; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
488 ic->start_time = AV_NOPTS_VALUE; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
489 pstrcpy(ic->filename, sizeof(ic->filename), filename); |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
490 |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
491 /* allocate private data */ |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
492 if (fmt->priv_data_size > 0) { |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
493 ic->priv_data = av_mallocz(fmt->priv_data_size); |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
494 if (!ic->priv_data) { |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
495 err = AVERROR_NOMEM; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
496 goto fail; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
497 } |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
498 } else { |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
499 ic->priv_data = NULL; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
500 } |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
501 |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
502 err = ic->iformat->read_header(ic, ap); |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
503 if (err < 0) |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
504 goto fail; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
505 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
506 if (pb) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
507 ic->data_offset = url_ftell(&ic->pb); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
508 |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
509 *ic_ptr = ic; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
510 return 0; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
511 fail: |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
512 if (ic) { |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
513 av_freep(&ic->priv_data); |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
514 } |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
515 av_free(ic); |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
516 *ic_ptr = NULL; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
517 return err; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
518 } |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
519 |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
520 /** Size of probe buffer, for guessing file type from file contents. */ |
922
7f20b3ce1100
dynamically increase probe buffer until format is detected
michael
parents:
915
diff
changeset
|
521 #define PROBE_BUF_MIN 2048 |
1111 | 522 #define PROBE_BUF_MAX (1<<20) |
0 | 523 |
524 /** | |
525 * Open a media file as input. The codec are not opened. Only the file | |
526 * header (if present) is read. | |
527 * | |
528 * @param ic_ptr the opened media file handle is put here | |
529 * @param filename filename to open. | |
530 * @param fmt if non NULL, force the file format to use | |
531 * @param buf_size optional buffer size (zero if default is OK) | |
532 * @param ap additionnal parameters needed when opening the file (NULL if default) | |
533 * @return 0 if OK. AVERROR_xxx otherwise. | |
534 */ | |
885 | 535 int av_open_input_file(AVFormatContext **ic_ptr, const char *filename, |
0 | 536 AVInputFormat *fmt, |
537 int buf_size, | |
538 AVFormatParameters *ap) | |
539 { | |
922
7f20b3ce1100
dynamically increase probe buffer until format is detected
michael
parents:
915
diff
changeset
|
540 int err, must_open_file, file_opened, probe_size; |
0 | 541 AVProbeData probe_data, *pd = &probe_data; |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
542 ByteIOContext pb1, *pb = &pb1; |
885 | 543 |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
544 file_opened = 0; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
545 pd->filename = ""; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
546 if (filename) |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
547 pd->filename = filename; |
922
7f20b3ce1100
dynamically increase probe buffer until format is detected
michael
parents:
915
diff
changeset
|
548 pd->buf = NULL; |
0 | 549 pd->buf_size = 0; |
550 | |
551 if (!fmt) { | |
552 /* guess format if no file can be opened */ | |
553 fmt = av_probe_input_format(pd, 0); | |
554 } | |
555 | |
172 | 556 /* do not open file if the format does not need it. XXX: specific |
557 hack needed to handle RTSP/TCP */ | |
558 must_open_file = 1; | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
559 if (fmt && (fmt->flags & AVFMT_NOFILE)) { |
172 | 560 must_open_file = 0; |
535 | 561 pb= NULL; //FIXME this or memset(pb, 0, sizeof(ByteIOContext)); otherwise its uninitalized |
172 | 562 } |
563 | |
564 if (!fmt || must_open_file) { | |
20 | 565 /* if no file needed do not try to open one */ |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
566 if (url_fopen(pb, filename, URL_RDONLY) < 0) { |
0 | 567 err = AVERROR_IO; |
568 goto fail; | |
569 } | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
570 file_opened = 1; |
0 | 571 if (buf_size > 0) { |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
572 url_setbufsize(pb, buf_size); |
0 | 573 } |
922
7f20b3ce1100
dynamically increase probe buffer until format is detected
michael
parents:
915
diff
changeset
|
574 |
7f20b3ce1100
dynamically increase probe buffer until format is detected
michael
parents:
915
diff
changeset
|
575 for(probe_size= PROBE_BUF_MIN; probe_size<=PROBE_BUF_MAX && !fmt; probe_size<<=1){ |
0 | 576 /* read probe data */ |
922
7f20b3ce1100
dynamically increase probe buffer until format is detected
michael
parents:
915
diff
changeset
|
577 pd->buf= av_realloc(pd->buf, probe_size); |
7f20b3ce1100
dynamically increase probe buffer until format is detected
michael
parents:
915
diff
changeset
|
578 pd->buf_size = get_buffer(pb, pd->buf, probe_size); |
502
813b0119a98e
ffserver fixes by (Koos Vriezen <koos.vriezen at xs4all dot nl>)
michael
parents:
497
diff
changeset
|
579 if (url_fseek(pb, 0, SEEK_SET) == (offset_t)-EPIPE) { |
813b0119a98e
ffserver fixes by (Koos Vriezen <koos.vriezen at xs4all dot nl>)
michael
parents:
497
diff
changeset
|
580 url_fclose(pb); |
813b0119a98e
ffserver fixes by (Koos Vriezen <koos.vriezen at xs4all dot nl>)
michael
parents:
497
diff
changeset
|
581 if (url_fopen(pb, filename, URL_RDONLY) < 0) { |
941
5e15da09cd6b
Fix for url_fclose() being called on an already closed file based on a patch by (<Colin Ward> hitman codehq org)
michael
parents:
927
diff
changeset
|
582 file_opened = 0; |
502
813b0119a98e
ffserver fixes by (Koos Vriezen <koos.vriezen at xs4all dot nl>)
michael
parents:
497
diff
changeset
|
583 err = AVERROR_IO; |
813b0119a98e
ffserver fixes by (Koos Vriezen <koos.vriezen at xs4all dot nl>)
michael
parents:
497
diff
changeset
|
584 goto fail; |
813b0119a98e
ffserver fixes by (Koos Vriezen <koos.vriezen at xs4all dot nl>)
michael
parents:
497
diff
changeset
|
585 } |
813b0119a98e
ffserver fixes by (Koos Vriezen <koos.vriezen at xs4all dot nl>)
michael
parents:
497
diff
changeset
|
586 } |
922
7f20b3ce1100
dynamically increase probe buffer until format is detected
michael
parents:
915
diff
changeset
|
587 /* guess file format */ |
7f20b3ce1100
dynamically increase probe buffer until format is detected
michael
parents:
915
diff
changeset
|
588 fmt = av_probe_input_format(pd, 1); |
0 | 589 } |
922
7f20b3ce1100
dynamically increase probe buffer until format is detected
michael
parents:
915
diff
changeset
|
590 av_freep(&pd->buf); |
0 | 591 } |
592 | |
593 /* if still no format found, error */ | |
594 if (!fmt) { | |
595 err = AVERROR_NOFMT; | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
596 goto fail; |
0 | 597 } |
885 | 598 |
0 | 599 /* XXX: suppress this hack for redirectors */ |
22
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
600 #ifdef CONFIG_NETWORK |
0 | 601 if (fmt == &redir_demux) { |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
602 err = redir_open(ic_ptr, pb); |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
603 url_fclose(pb); |
0 | 604 return err; |
605 } | |
11
932b59c66c60
mingw patch by (Bill Eldridge <bill at rfa dot org>)
michaelni
parents:
9
diff
changeset
|
606 #endif |
0 | 607 |
20 | 608 /* check filename in case of an image number is expected */ |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
609 if (fmt->flags & AVFMT_NEEDNUMBER) { |
885 | 610 if (filename_number_test(filename) < 0) { |
20 | 611 err = AVERROR_NUMEXPECTED; |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
612 goto fail; |
20 | 613 } |
614 } | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
615 err = av_open_input_stream(ic_ptr, pb, filename, fmt, ap); |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
616 if (err) |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
617 goto fail; |
0 | 618 return 0; |
619 fail: | |
922
7f20b3ce1100
dynamically increase probe buffer until format is detected
michael
parents:
915
diff
changeset
|
620 av_freep(&pd->buf); |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
621 if (file_opened) |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
622 url_fclose(pb); |
0 | 623 *ic_ptr = NULL; |
624 return err; | |
885 | 625 |
0 | 626 } |
627 | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
628 /*******************************************************/ |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
629 |
0 | 630 /** |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
631 * Read a transport packet from a media file. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
632 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
633 * This function is absolete and should never be used. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
634 * Use av_read_frame() instead. |
885 | 635 * |
0 | 636 * @param s media file handle |
885 | 637 * @param pkt is filled |
638 * @return 0 if OK. AVERROR_xxx if error. | |
0 | 639 */ |
640 int av_read_packet(AVFormatContext *s, AVPacket *pkt) | |
641 { | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
642 return s->iformat->read_packet(s, pkt); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
643 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
644 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
645 /**********************************************************/ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
646 |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
647 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
648 * Get the number of samples of an audio frame. Return (-1) if error. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
649 */ |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
650 static int get_audio_frame_size(AVCodecContext *enc, int size) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
651 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
652 int frame_size; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
653 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
654 if (enc->frame_size <= 1) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
655 /* specific hack for pcm codecs because no frame size is |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
656 provided */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
657 switch(enc->codec_id) { |
846
fd1c2109505e
Support de-/encoding of 24 and 32 bit PCM (from and to internal 16 bit).
reimar
parents:
842
diff
changeset
|
658 case CODEC_ID_PCM_S32LE: |
fd1c2109505e
Support de-/encoding of 24 and 32 bit PCM (from and to internal 16 bit).
reimar
parents:
842
diff
changeset
|
659 case CODEC_ID_PCM_S32BE: |
fd1c2109505e
Support de-/encoding of 24 and 32 bit PCM (from and to internal 16 bit).
reimar
parents:
842
diff
changeset
|
660 case CODEC_ID_PCM_U32LE: |
fd1c2109505e
Support de-/encoding of 24 and 32 bit PCM (from and to internal 16 bit).
reimar
parents:
842
diff
changeset
|
661 case CODEC_ID_PCM_U32BE: |
fd1c2109505e
Support de-/encoding of 24 and 32 bit PCM (from and to internal 16 bit).
reimar
parents:
842
diff
changeset
|
662 if (enc->channels == 0) |
fd1c2109505e
Support de-/encoding of 24 and 32 bit PCM (from and to internal 16 bit).
reimar
parents:
842
diff
changeset
|
663 return -1; |
fd1c2109505e
Support de-/encoding of 24 and 32 bit PCM (from and to internal 16 bit).
reimar
parents:
842
diff
changeset
|
664 frame_size = size / (4 * enc->channels); |
fd1c2109505e
Support de-/encoding of 24 and 32 bit PCM (from and to internal 16 bit).
reimar
parents:
842
diff
changeset
|
665 break; |
fd1c2109505e
Support de-/encoding of 24 and 32 bit PCM (from and to internal 16 bit).
reimar
parents:
842
diff
changeset
|
666 case CODEC_ID_PCM_S24LE: |
fd1c2109505e
Support de-/encoding of 24 and 32 bit PCM (from and to internal 16 bit).
reimar
parents:
842
diff
changeset
|
667 case CODEC_ID_PCM_S24BE: |
fd1c2109505e
Support de-/encoding of 24 and 32 bit PCM (from and to internal 16 bit).
reimar
parents:
842
diff
changeset
|
668 case CODEC_ID_PCM_U24LE: |
fd1c2109505e
Support de-/encoding of 24 and 32 bit PCM (from and to internal 16 bit).
reimar
parents:
842
diff
changeset
|
669 case CODEC_ID_PCM_U24BE: |
fd1c2109505e
Support de-/encoding of 24 and 32 bit PCM (from and to internal 16 bit).
reimar
parents:
842
diff
changeset
|
670 case CODEC_ID_PCM_S24DAUD: |
fd1c2109505e
Support de-/encoding of 24 and 32 bit PCM (from and to internal 16 bit).
reimar
parents:
842
diff
changeset
|
671 if (enc->channels == 0) |
fd1c2109505e
Support de-/encoding of 24 and 32 bit PCM (from and to internal 16 bit).
reimar
parents:
842
diff
changeset
|
672 return -1; |
fd1c2109505e
Support de-/encoding of 24 and 32 bit PCM (from and to internal 16 bit).
reimar
parents:
842
diff
changeset
|
673 frame_size = size / (3 * enc->channels); |
fd1c2109505e
Support de-/encoding of 24 and 32 bit PCM (from and to internal 16 bit).
reimar
parents:
842
diff
changeset
|
674 break; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
675 case CODEC_ID_PCM_S16LE: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
676 case CODEC_ID_PCM_S16BE: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
677 case CODEC_ID_PCM_U16LE: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
678 case CODEC_ID_PCM_U16BE: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
679 if (enc->channels == 0) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
680 return -1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
681 frame_size = size / (2 * enc->channels); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
682 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
683 case CODEC_ID_PCM_S8: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
684 case CODEC_ID_PCM_U8: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
685 case CODEC_ID_PCM_MULAW: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
686 case CODEC_ID_PCM_ALAW: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
687 if (enc->channels == 0) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
688 return -1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
689 frame_size = size / (enc->channels); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
690 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
691 default: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
692 /* used for example by ADPCM codecs */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
693 if (enc->bit_rate == 0) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
694 return -1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
695 frame_size = (size * 8 * enc->sample_rate) / enc->bit_rate; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
696 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
697 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
698 } else { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
699 frame_size = enc->frame_size; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
700 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
701 return frame_size; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
702 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
703 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
704 |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
705 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
706 * Return the frame duration in seconds, return 0 if not available. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
707 */ |
885 | 708 static void compute_frame_duration(int *pnum, int *pden, AVStream *st, |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
709 AVCodecParserContext *pc, AVPacket *pkt) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
710 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
711 int frame_size; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
712 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
713 *pnum = 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
714 *pden = 0; |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
715 switch(st->codec->codec_type) { |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
716 case CODEC_TYPE_VIDEO: |
757 | 717 if(st->time_base.num*1000LL > st->time_base.den){ |
743 | 718 *pnum = st->time_base.num; |
719 *pden = st->time_base.den; | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
720 }else if(st->codec->time_base.num*1000LL > st->codec->time_base.den){ |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
721 *pnum = st->codec->time_base.num; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
722 *pden = st->codec->time_base.den; |
747
95c9cef3c3db
prefer container time_base for frame duration guess
michael
parents:
743
diff
changeset
|
723 if (pc && pc->repeat_pict) { |
95c9cef3c3db
prefer container time_base for frame duration guess
michael
parents:
743
diff
changeset
|
724 *pden *= 2; |
95c9cef3c3db
prefer container time_base for frame duration guess
michael
parents:
743
diff
changeset
|
725 *pnum = (*pnum) * (2 + pc->repeat_pict); |
95c9cef3c3db
prefer container time_base for frame duration guess
michael
parents:
743
diff
changeset
|
726 } |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
727 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
728 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
729 case CODEC_TYPE_AUDIO: |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
730 frame_size = get_audio_frame_size(st->codec, pkt->size); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
731 if (frame_size < 0) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
732 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
733 *pnum = frame_size; |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
734 *pden = st->codec->sample_rate; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
735 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
736 default: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
737 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
738 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
739 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
740 |
570
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
741 static int is_intra_only(AVCodecContext *enc){ |
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
742 if(enc->codec_type == CODEC_TYPE_AUDIO){ |
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
743 return 1; |
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
744 }else if(enc->codec_type == CODEC_TYPE_VIDEO){ |
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
745 switch(enc->codec_id){ |
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
746 case CODEC_ID_MJPEG: |
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
747 case CODEC_ID_MJPEGB: |
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
748 case CODEC_ID_LJPEG: |
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
749 case CODEC_ID_RAWVIDEO: |
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
750 case CODEC_ID_DVVIDEO: |
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
751 case CODEC_ID_HUFFYUV: |
599 | 752 case CODEC_ID_FFVHUFF: |
570
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
753 case CODEC_ID_ASV1: |
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
754 case CODEC_ID_ASV2: |
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
755 case CODEC_ID_VCR1: |
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
756 return 1; |
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
757 default: break; |
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
758 } |
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
759 } |
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
760 return 0; |
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
761 } |
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
762 |
464 | 763 static int64_t lsb2full(int64_t lsb, int64_t last_ts, int lsb_bits){ |
466 | 764 int64_t mask = lsb_bits < 64 ? (1LL<<lsb_bits)-1 : -1LL; |
464 | 765 int64_t delta= last_ts - mask/2; |
766 return ((lsb - delta)&mask) + delta; | |
767 } | |
768 | |
885 | 769 static void compute_pkt_fields(AVFormatContext *s, AVStream *st, |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
770 AVCodecParserContext *pc, AVPacket *pkt) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
771 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
772 int num, den, presentation_delayed; |
464 | 773 /* handle wrapping */ |
468 | 774 if(st->cur_dts != AV_NOPTS_VALUE){ |
775 if(pkt->pts != AV_NOPTS_VALUE) | |
776 pkt->pts= lsb2full(pkt->pts, st->cur_dts, st->pts_wrap_bits); | |
777 if(pkt->dts != AV_NOPTS_VALUE) | |
778 pkt->dts= lsb2full(pkt->dts, st->cur_dts, st->pts_wrap_bits); | |
779 } | |
885 | 780 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
781 if (pkt->duration == 0) { |
470 | 782 compute_frame_duration(&num, &den, st, pc, pkt); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
783 if (den && num) { |
464 | 784 pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den, den * (int64_t)st->time_base.num); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
785 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
786 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
787 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
788 if(is_intra_only(st->codec)) |
570
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
789 pkt->flags |= PKT_FLAG_KEY; |
d82ccc7cff1c
set keyframe flag at a more central place instead of in every demuxer for containers which only store intra only streams
michael
parents:
563
diff
changeset
|
790 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
791 /* do we have a video B frame ? */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
792 presentation_delayed = 0; |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
793 if (st->codec->codec_type == CODEC_TYPE_VIDEO) { |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
794 /* XXX: need has_b_frame, but cannot get it if the codec is |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
795 not initialized */ |
885 | 796 if (( st->codec->codec_id == CODEC_ID_H264 |
797 || st->codec->has_b_frames) && | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
798 pc && pc->pict_type != FF_B_TYPE) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
799 presentation_delayed = 1; |
464 | 800 /* this may be redundant, but it shouldnt hurt */ |
801 if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts > pkt->dts) | |
802 presentation_delayed = 1; | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
803 } |
885 | 804 |
468 | 805 if(st->cur_dts == AV_NOPTS_VALUE){ |
806 if(presentation_delayed) st->cur_dts = -pkt->duration; | |
807 else st->cur_dts = 0; | |
808 } | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
809 |
470 | 810 // av_log(NULL, AV_LOG_DEBUG, "IN delayed:%d pts:%lld, dts:%lld cur_dts:%lld st:%d pc:%p\n", presentation_delayed, pkt->pts, pkt->dts, st->cur_dts, pkt->stream_index, pc); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
811 /* interpolate PTS and DTS if they are not present */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
812 if (presentation_delayed) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
813 /* DTS = decompression time stamp */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
814 /* PTS = presentation time stamp */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
815 if (pkt->dts == AV_NOPTS_VALUE) { |
464 | 816 /* if we know the last pts, use it */ |
817 if(st->last_IP_pts != AV_NOPTS_VALUE) | |
818 st->cur_dts = pkt->dts = st->last_IP_pts; | |
819 else | |
820 pkt->dts = st->cur_dts; | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
821 } else { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
822 st->cur_dts = pkt->dts; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
823 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
824 /* this is tricky: the dts must be incremented by the duration |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
825 of the frame we are displaying, i.e. the last I or P frame */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
826 if (st->last_IP_duration == 0) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
827 st->cur_dts += pkt->duration; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
828 else |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
829 st->cur_dts += st->last_IP_duration; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
830 st->last_IP_duration = pkt->duration; |
464 | 831 st->last_IP_pts= pkt->pts; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
832 /* cannot compute PTS if not present (we can compute it only |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
833 by knowing the futur */ |
666
ffad4fdbd3d1
dont predict missing timestamps if we lack the required information to do so
michael
parents:
662
diff
changeset
|
834 } else if(pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE || pkt->duration){ |
618 | 835 if(pkt->pts != AV_NOPTS_VALUE && pkt->duration){ |
836 int64_t old_diff= ABS(st->cur_dts - pkt->duration - pkt->pts); | |
837 int64_t new_diff= ABS(st->cur_dts - pkt->pts); | |
838 if(old_diff < new_diff && old_diff < (pkt->duration>>3)){ | |
839 pkt->pts += pkt->duration; | |
624 | 840 // av_log(NULL, AV_LOG_DEBUG, "id:%d old:%Ld new:%Ld dur:%d cur:%Ld size:%d\n", pkt->stream_index, old_diff, new_diff, pkt->duration, st->cur_dts, pkt->size); |
618 | 841 } |
842 } | |
885 | 843 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
844 /* presentation is not delayed : PTS and DTS are the same */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
845 if (pkt->pts == AV_NOPTS_VALUE) { |
367
3fca8e9142a4
avsync patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
354
diff
changeset
|
846 if (pkt->dts == AV_NOPTS_VALUE) { |
3fca8e9142a4
avsync patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
354
diff
changeset
|
847 pkt->pts = st->cur_dts; |
3fca8e9142a4
avsync patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
354
diff
changeset
|
848 pkt->dts = st->cur_dts; |
3fca8e9142a4
avsync patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
354
diff
changeset
|
849 } |
3fca8e9142a4
avsync patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
354
diff
changeset
|
850 else { |
3fca8e9142a4
avsync patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
354
diff
changeset
|
851 st->cur_dts = pkt->dts; |
3fca8e9142a4
avsync patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
354
diff
changeset
|
852 pkt->pts = pkt->dts; |
3fca8e9142a4
avsync patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
354
diff
changeset
|
853 } |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
854 } else { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
855 st->cur_dts = pkt->pts; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
856 pkt->dts = pkt->pts; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
857 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
858 st->cur_dts += pkt->duration; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
859 } |
468 | 860 // av_log(NULL, AV_LOG_DEBUG, "OUTdelayed:%d pts:%lld, dts:%lld cur_dts:%lld\n", presentation_delayed, pkt->pts, pkt->dts, st->cur_dts); |
885 | 861 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
862 /* update flags */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
863 if (pc) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
864 pkt->flags = 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
865 /* key frame computation */ |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
866 switch(st->codec->codec_type) { |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
867 case CODEC_TYPE_VIDEO: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
868 if (pc->pict_type == FF_I_TYPE) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
869 pkt->flags |= PKT_FLAG_KEY; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
870 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
871 case CODEC_TYPE_AUDIO: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
872 pkt->flags |= PKT_FLAG_KEY; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
873 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
874 default: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
875 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
876 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
877 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
878 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
879 |
535 | 880 void av_destruct_packet_nofree(AVPacket *pkt) |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
881 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
882 pkt->data = NULL; pkt->size = 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
883 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
884 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
885 static int av_read_frame_internal(AVFormatContext *s, AVPacket *pkt) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
886 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
887 AVStream *st; |
333 | 888 int len, ret, i; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
889 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
890 for(;;) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
891 /* select current input stream component */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
892 st = s->cur_st; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
893 if (st) { |
797
f5a231a9a2f1
support changing in bitstream global headers into extradata style and back
michael
parents:
786
diff
changeset
|
894 if (!st->need_parsing || !st->parser) { |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
895 /* no parsing needed: we just output the packet as is */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
896 /* raw data support */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
897 *pkt = s->cur_pkt; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
898 compute_pkt_fields(s, st, NULL, pkt); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
899 s->cur_st = NULL; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
900 return 0; |
708 | 901 } else if (s->cur_len > 0 && st->discard < AVDISCARD_ALL) { |
885 | 902 len = av_parser_parse(st->parser, st->codec, &pkt->data, &pkt->size, |
334
7f089db11f9a
fixed incorrect PTS/DTS logic in MPEG video case (caused rare PTS glitches if start codes were between two PES packets)
bellard
parents:
333
diff
changeset
|
903 s->cur_ptr, s->cur_len, |
7f089db11f9a
fixed incorrect PTS/DTS logic in MPEG video case (caused rare PTS glitches if start codes were between two PES packets)
bellard
parents:
333
diff
changeset
|
904 s->cur_pkt.pts, s->cur_pkt.dts); |
7f089db11f9a
fixed incorrect PTS/DTS logic in MPEG video case (caused rare PTS glitches if start codes were between two PES packets)
bellard
parents:
333
diff
changeset
|
905 s->cur_pkt.pts = AV_NOPTS_VALUE; |
7f089db11f9a
fixed incorrect PTS/DTS logic in MPEG video case (caused rare PTS glitches if start codes were between two PES packets)
bellard
parents:
333
diff
changeset
|
906 s->cur_pkt.dts = AV_NOPTS_VALUE; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
907 /* increment read pointer */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
908 s->cur_ptr += len; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
909 s->cur_len -= len; |
885 | 910 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
911 /* return packet if any */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
912 if (pkt->size) { |
333 | 913 got_packet: |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
914 pkt->duration = 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
915 pkt->stream_index = st->index; |
334
7f089db11f9a
fixed incorrect PTS/DTS logic in MPEG video case (caused rare PTS glitches if start codes were between two PES packets)
bellard
parents:
333
diff
changeset
|
916 pkt->pts = st->parser->pts; |
7f089db11f9a
fixed incorrect PTS/DTS logic in MPEG video case (caused rare PTS glitches if start codes were between two PES packets)
bellard
parents:
333
diff
changeset
|
917 pkt->dts = st->parser->dts; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
918 pkt->destruct = av_destruct_packet_nofree; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
919 compute_pkt_fields(s, st, st->parser, pkt); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
920 return 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
921 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
922 } else { |
319 | 923 /* free packet */ |
885 | 924 av_free_packet(&s->cur_pkt); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
925 s->cur_st = NULL; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
926 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
927 } else { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
928 /* read next packet */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
929 ret = av_read_packet(s, &s->cur_pkt); |
333 | 930 if (ret < 0) { |
931 if (ret == -EAGAIN) | |
932 return ret; | |
933 /* return the last frames, if any */ | |
934 for(i = 0; i < s->nb_streams; i++) { | |
935 st = s->streams[i]; | |
797
f5a231a9a2f1
support changing in bitstream global headers into extradata style and back
michael
parents:
786
diff
changeset
|
936 if (st->parser && st->need_parsing) { |
885 | 937 av_parser_parse(st->parser, st->codec, |
938 &pkt->data, &pkt->size, | |
939 NULL, 0, | |
334
7f089db11f9a
fixed incorrect PTS/DTS logic in MPEG video case (caused rare PTS glitches if start codes were between two PES packets)
bellard
parents:
333
diff
changeset
|
940 AV_NOPTS_VALUE, AV_NOPTS_VALUE); |
333 | 941 if (pkt->size) |
942 goto got_packet; | |
943 } | |
944 } | |
945 /* no more packets: really terminates parsing */ | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
946 return ret; |
333 | 947 } |
885 | 948 |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
453
diff
changeset
|
949 st = s->streams[s->cur_pkt.stream_index]; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
950 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
951 s->cur_st = st; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
952 s->cur_ptr = s->cur_pkt.data; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
953 s->cur_len = s->cur_pkt.size; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
954 if (st->need_parsing && !st->parser) { |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
955 st->parser = av_parser_init(st->codec->codec_id); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
956 if (!st->parser) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
957 /* no parser available : just output the raw packets */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
958 st->need_parsing = 0; |
842 | 959 }else if(st->need_parsing == 2){ |
960 st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES; | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
961 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
962 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
963 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
964 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
965 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
966 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
967 /** |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
968 * Return the next frame of a stream. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
969 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
970 * The returned packet is valid |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
971 * until the next av_read_frame() or until av_close_input_file() and |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
972 * must be freed with av_free_packet. For video, the packet contains |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
973 * exactly one frame. For audio, it contains an integer number of |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
974 * frames if each frame has a known fixed size (e.g. PCM or ADPCM |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
975 * data). If the audio frames have a variable size (e.g. MPEG audio), |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
976 * then it contains one frame. |
885 | 977 * |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
978 * pkt->pts, pkt->dts and pkt->duration are always set to correct |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
979 * values in AV_TIME_BASE unit (and guessed if the format cannot |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
980 * provided them). pkt->pts can be AV_NOPTS_VALUE if the video format |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
981 * has B frames, so it is better to rely on pkt->dts if you do not |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
982 * decompress the payload. |
885 | 983 * |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
984 * @return 0 if OK, < 0 if error or end of file. |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
985 */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
986 int av_read_frame(AVFormatContext *s, AVPacket *pkt) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
987 { |
0 | 988 AVPacketList *pktl; |
841
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
989 int eof=0; |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
990 const int genpts= s->flags & AVFMT_FLAG_GENPTS; |
0 | 991 |
841
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
992 for(;;){ |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
993 pktl = s->packet_buffer; |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
994 if (pktl) { |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
995 AVPacket *next_pkt= &pktl->pkt; |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
996 |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
997 if(genpts && next_pkt->dts != AV_NOPTS_VALUE){ |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
998 while(pktl && next_pkt->pts == AV_NOPTS_VALUE){ |
885 | 999 if( pktl->pkt.stream_index == next_pkt->stream_index |
841
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1000 && next_pkt->dts < pktl->pkt.dts |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1001 && pktl->pkt.pts != pktl->pkt.dts //not b frame |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1002 /*&& pktl->pkt.dts != AV_NOPTS_VALUE*/){ |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1003 next_pkt->pts= pktl->pkt.dts; |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1004 } |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1005 pktl= pktl->next; |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1006 } |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1007 pktl = s->packet_buffer; |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1008 } |
885 | 1009 |
1010 if( next_pkt->pts != AV_NOPTS_VALUE | |
1011 || next_pkt->dts == AV_NOPTS_VALUE | |
841
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1012 || !genpts || eof){ |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1013 /* read packet from packet buffer, if there is data */ |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1014 *pkt = *next_pkt; |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1015 s->packet_buffer = pktl->next; |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1016 av_free(pktl); |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1017 return 0; |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1018 } |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1019 } |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1020 if(genpts){ |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1021 AVPacketList **plast_pktl= &s->packet_buffer; |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1022 int ret= av_read_frame_internal(s, pkt); |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1023 if(ret<0){ |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1024 if(pktl && ret != -EAGAIN){ |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1025 eof=1; |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1026 continue; |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1027 }else |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1028 return ret; |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1029 } |
885 | 1030 |
841
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1031 /* duplicate the packet */ |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1032 if (av_dup_packet(pkt) < 0) |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1033 return AVERROR_NOMEM; |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1034 |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1035 while(*plast_pktl) plast_pktl= &(*plast_pktl)->next; //FIXME maybe maintain pointer to the last? |
885 | 1036 |
841
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1037 pktl = av_mallocz(sizeof(AVPacketList)); |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1038 if (!pktl) |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1039 return AVERROR_NOMEM; |
885 | 1040 |
841
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1041 /* add the packet in the buffered packet list */ |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1042 *plast_pktl = pktl; |
885 | 1043 pktl->pkt= *pkt; |
841
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1044 }else{ |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1045 assert(!s->packet_buffer); |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1046 return av_read_frame_internal(s, pkt); |
ba7631ba33a7
support fixing missing pts by parsing future frames
michael
parents:
840
diff
changeset
|
1047 } |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1048 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1049 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1050 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1051 /* XXX: suppress the packet queue */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1052 static void flush_packet_queue(AVFormatContext *s) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1053 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1054 AVPacketList *pktl; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1055 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1056 for(;;) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1057 pktl = s->packet_buffer; |
885 | 1058 if (!pktl) |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1059 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1060 s->packet_buffer = pktl->next; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1061 av_free_packet(&pktl->pkt); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1062 av_free(pktl); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1063 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1064 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1065 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1066 /*******************************************************/ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1067 /* seek support */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1068 |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1069 int av_find_default_stream_index(AVFormatContext *s) |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1070 { |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1071 int i; |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1072 AVStream *st; |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1073 |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1074 if (s->nb_streams <= 0) |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1075 return -1; |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1076 for(i = 0; i < s->nb_streams; i++) { |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1077 st = s->streams[i]; |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
1078 if (st->codec->codec_type == CODEC_TYPE_VIDEO) { |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1079 return i; |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1080 } |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1081 } |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1082 return 0; |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1083 } |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1084 |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1085 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1086 * Flush the frame reader. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1087 */ |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1088 static void av_read_frame_flush(AVFormatContext *s) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1089 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1090 AVStream *st; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1091 int i; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1092 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1093 flush_packet_queue(s); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1094 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1095 /* free previous packet */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1096 if (s->cur_st) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1097 if (s->cur_st->parser) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1098 av_free_packet(&s->cur_pkt); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1099 s->cur_st = NULL; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1100 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1101 /* fail safe */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1102 s->cur_ptr = NULL; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1103 s->cur_len = 0; |
885 | 1104 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1105 /* for each stream, reset read state */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1106 for(i = 0; i < s->nb_streams; i++) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1107 st = s->streams[i]; |
885 | 1108 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1109 if (st->parser) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1110 av_parser_close(st->parser); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1111 st->parser = NULL; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1112 } |
464 | 1113 st->last_IP_pts = AV_NOPTS_VALUE; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1114 st->cur_dts = 0; /* we set the current DTS to an unspecified origin */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1115 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1116 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1117 |
463
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1118 /** |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1119 * Updates cur_dts of all streams based on given timestamp and AVStream. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1120 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1121 * Stream ref_st unchanged, others set cur_dts in their native timebase |
572
640706a29efb
bug in libavformat av_update_cur_dts(), patch by (Nathan Kurz <nate at verse dot com>)
michael
parents:
570
diff
changeset
|
1122 * only needed for timestamp wrapping or if (dts not set and pts!=dts) |
640706a29efb
bug in libavformat av_update_cur_dts(), patch by (Nathan Kurz <nate at verse dot com>)
michael
parents:
570
diff
changeset
|
1123 * @param timestamp new dts expressed in time_base of param ref_st |
640706a29efb
bug in libavformat av_update_cur_dts(), patch by (Nathan Kurz <nate at verse dot com>)
michael
parents:
570
diff
changeset
|
1124 * @param ref_st reference stream giving time_base of param timestamp |
560 | 1125 */ |
572
640706a29efb
bug in libavformat av_update_cur_dts(), patch by (Nathan Kurz <nate at verse dot com>)
michael
parents:
570
diff
changeset
|
1126 static void av_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp){ |
560 | 1127 int i; |
1128 | |
1129 for(i = 0; i < s->nb_streams; i++) { | |
572
640706a29efb
bug in libavformat av_update_cur_dts(), patch by (Nathan Kurz <nate at verse dot com>)
michael
parents:
570
diff
changeset
|
1130 AVStream *st = s->streams[i]; |
560 | 1131 |
885 | 1132 st->cur_dts = av_rescale(timestamp, |
572
640706a29efb
bug in libavformat av_update_cur_dts(), patch by (Nathan Kurz <nate at verse dot com>)
michael
parents:
570
diff
changeset
|
1133 st->time_base.den * (int64_t)ref_st->time_base.num, |
640706a29efb
bug in libavformat av_update_cur_dts(), patch by (Nathan Kurz <nate at verse dot com>)
michael
parents:
570
diff
changeset
|
1134 st->time_base.num * (int64_t)ref_st->time_base.den); |
560 | 1135 } |
1136 } | |
1137 | |
1138 /** | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1139 * Add a index entry into a sorted list updateing if it is already there. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1140 * |
463
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1141 * @param timestamp timestamp in the timebase of the given stream |
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1142 */ |
354
6770ca07abe2
store searched distance in index, so we dont waste time searching for keyframes where we already searched
michael
parents:
346
diff
changeset
|
1143 int av_add_index_entry(AVStream *st, |
979 | 1144 int64_t pos, int64_t timestamp, int size, int distance, int flags) |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1145 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1146 AVIndexEntry *entries, *ie; |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1147 int index; |
885 | 1148 |
639 | 1149 if((unsigned)st->nb_index_entries + 1 >= UINT_MAX / sizeof(AVIndexEntry)) |
1150 return -1; | |
885 | 1151 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1152 entries = av_fast_realloc(st->index_entries, |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1153 &st->index_entries_allocated_size, |
885 | 1154 (st->nb_index_entries + 1) * |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1155 sizeof(AVIndexEntry)); |
639 | 1156 if(!entries) |
1157 return -1; | |
1158 | |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1159 st->index_entries= entries; |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1160 |
698 | 1161 index= av_index_search_timestamp(st, timestamp, AVSEEK_FLAG_ANY); |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1162 |
555 | 1163 if(index<0){ |
354
6770ca07abe2
store searched distance in index, so we dont waste time searching for keyframes where we already searched
michael
parents:
346
diff
changeset
|
1164 index= st->nb_index_entries++; |
6770ca07abe2
store searched distance in index, so we dont waste time searching for keyframes where we already searched
michael
parents:
346
diff
changeset
|
1165 ie= &entries[index]; |
555 | 1166 assert(index==0 || ie[-1].timestamp < timestamp); |
1167 }else{ | |
1168 ie= &entries[index]; | |
1169 if(ie->timestamp != timestamp){ | |
563
d141aa45ca86
fix assertion failure in case of timestamp discontinuities
michael
parents:
560
diff
changeset
|
1170 if(ie->timestamp <= timestamp) |
d141aa45ca86
fix assertion failure in case of timestamp discontinuities
michael
parents:
560
diff
changeset
|
1171 return -1; |
555 | 1172 memmove(entries + index + 1, entries + index, sizeof(AVIndexEntry)*(st->nb_index_entries - index)); |
1173 st->nb_index_entries++; | |
1174 }else if(ie->pos == pos && distance < ie->min_distance) //dont reduce the distance | |
1175 distance= ie->min_distance; | |
354
6770ca07abe2
store searched distance in index, so we dont waste time searching for keyframes where we already searched
michael
parents:
346
diff
changeset
|
1176 } |
555 | 1177 |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1178 ie->pos = pos; |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1179 ie->timestamp = timestamp; |
354
6770ca07abe2
store searched distance in index, so we dont waste time searching for keyframes where we already searched
michael
parents:
346
diff
changeset
|
1180 ie->min_distance= distance; |
979 | 1181 ie->size= size; |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1182 ie->flags = flags; |
885 | 1183 |
354
6770ca07abe2
store searched distance in index, so we dont waste time searching for keyframes where we already searched
michael
parents:
346
diff
changeset
|
1184 return index; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1185 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1186 |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1187 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1188 * build an index for raw streams using a parser. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1189 */ |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1190 static void av_build_index_raw(AVFormatContext *s) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1191 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1192 AVPacket pkt1, *pkt = &pkt1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1193 int ret; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1194 AVStream *st; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1195 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1196 st = s->streams[0]; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1197 av_read_frame_flush(s); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1198 url_fseek(&s->pb, s->data_offset, SEEK_SET); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1199 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1200 for(;;) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1201 ret = av_read_frame(s, pkt); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1202 if (ret < 0) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1203 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1204 if (pkt->stream_index == 0 && st->parser && |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1205 (pkt->flags & PKT_FLAG_KEY)) { |
885 | 1206 av_add_index_entry(st, st->parser->frame_offset, pkt->dts, |
979 | 1207 0, 0, AVINDEX_KEYFRAME); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1208 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1209 av_free_packet(pkt); |
0 | 1210 } |
1211 } | |
1212 | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1213 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1214 * Returns TRUE if we deal with a raw stream. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1215 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1216 * Raw codec data and parsing needed. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1217 */ |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1218 static int is_raw_stream(AVFormatContext *s) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1219 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1220 AVStream *st; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1221 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1222 if (s->nb_streams != 1) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1223 return 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1224 st = s->streams[0]; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1225 if (!st->need_parsing) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1226 return 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1227 return 1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1228 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1229 |
555 | 1230 /** |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1231 * Gets the index for a specific timestamp. |
885 | 1232 * @param flags if AVSEEK_FLAG_BACKWARD then the returned index will correspond to |
1233 * the timestamp which is <= the requested one, if backward is 0 | |
555 | 1234 * then it will be >= |
698 | 1235 * if AVSEEK_FLAG_ANY seek to any frame, only keyframes otherwise |
555 | 1236 * @return < 0 if no such timestamp could be found |
1237 */ | |
597
d814669d2c13
int / int64 fix by (Wolfram Gloger <wmglo @@@ dent:med:uni-muenchen:de>)
michael
parents:
588
diff
changeset
|
1238 int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp, |
698 | 1239 int flags) |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1240 { |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1241 AVIndexEntry *entries= st->index_entries; |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1242 int nb_entries= st->nb_index_entries; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1243 int a, b, m; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1244 int64_t timestamp; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1245 |
555 | 1246 a = - 1; |
1247 b = nb_entries; | |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1248 |
555 | 1249 while (b - a > 1) { |
1250 m = (a + b) >> 1; | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1251 timestamp = entries[m].timestamp; |
555 | 1252 if(timestamp >= wanted_timestamp) |
1253 b = m; | |
1254 if(timestamp <= wanted_timestamp) | |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1255 a = m; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1256 } |
698 | 1257 m= (flags & AVSEEK_FLAG_BACKWARD) ? a : b; |
885 | 1258 |
698 | 1259 if(!(flags & AVSEEK_FLAG_ANY)){ |
1260 while(m>=0 && m<nb_entries && !(entries[m].flags & AVINDEX_KEYFRAME)){ | |
1261 m += (flags & AVSEEK_FLAG_BACKWARD) ? -1 : 1; | |
1262 } | |
1263 } | |
555 | 1264 |
885 | 1265 if(m == nb_entries) |
555 | 1266 return -1; |
1267 return m; | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1268 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1269 |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1270 #define DEBUG_SEEK |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1271 |
463
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1272 /** |
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1273 * Does a binary search using av_index_search_timestamp() and AVCodec.read_timestamp(). |
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1274 * this isnt supposed to be called directly by a user application, but by demuxers |
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1275 * @param target_ts target timestamp in the time base of the given stream |
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1276 * @param stream_index stream number |
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1277 */ |
555 | 1278 int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){ |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1279 AVInputFormat *avif= s->iformat; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1280 int64_t pos_min, pos_max, pos, pos_limit; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1281 int64_t ts_min, ts_max, ts; |
810
2a84386ed2a8
avformat/av_seek_frame_binary with growing file patch by (Kenneth Aafly: kenneth, aafloy net)
michael
parents:
809
diff
changeset
|
1282 int64_t start_pos, filesize; |
560 | 1283 int index, no_change; |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1284 AVStream *st; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1285 |
463
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1286 if (stream_index < 0) |
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1287 return -1; |
885 | 1288 |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1289 #ifdef DEBUG_SEEK |
881 | 1290 av_log(s, AV_LOG_DEBUG, "read_seek: %d %"PRId64"\n", stream_index, target_ts); |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1291 #endif |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1292 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1293 ts_max= |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1294 ts_min= AV_NOPTS_VALUE; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1295 pos_limit= -1; //gcc falsely says it may be uninitalized |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1296 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1297 st= s->streams[stream_index]; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1298 if(st->index_entries){ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1299 AVIndexEntry *e; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1300 |
698 | 1301 index= av_index_search_timestamp(st, target_ts, flags | AVSEEK_FLAG_BACKWARD); //FIXME whole func must be checked for non keyframe entries in index case, especially read_timestamp() |
555 | 1302 index= FFMAX(index, 0); |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1303 e= &st->index_entries[index]; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1304 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1305 if(e->timestamp <= target_ts || e->pos == e->min_distance){ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1306 pos_min= e->pos; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1307 ts_min= e->timestamp; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1308 #ifdef DEBUG_SEEK |
885 | 1309 av_log(s, AV_LOG_DEBUG, "using cached pos_min=0x%"PRIx64" dts_min=%"PRId64"\n", |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1310 pos_min,ts_min); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1311 #endif |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1312 }else{ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1313 assert(index==0); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1314 } |
885 | 1315 |
1316 index= av_index_search_timestamp(st, target_ts, flags & ~AVSEEK_FLAG_BACKWARD); | |
698 | 1317 assert(index < st->nb_index_entries); |
1318 if(index >= 0){ | |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1319 e= &st->index_entries[index]; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1320 assert(e->timestamp >= target_ts); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1321 pos_max= e->pos; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1322 ts_max= e->timestamp; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1323 pos_limit= pos_max - e->min_distance; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1324 #ifdef DEBUG_SEEK |
885 | 1325 av_log(s, AV_LOG_DEBUG, "using cached pos_max=0x%"PRIx64" pos_limit=0x%"PRIx64" dts_max=%"PRId64"\n", |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1326 pos_max,pos_limit, ts_max); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1327 #endif |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1328 } |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1329 } |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1330 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1331 if(ts_min == AV_NOPTS_VALUE){ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1332 pos_min = s->data_offset; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1333 ts_min = avif->read_timestamp(s, stream_index, &pos_min, INT64_MAX); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1334 if (ts_min == AV_NOPTS_VALUE) |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1335 return -1; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1336 } |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1337 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1338 if(ts_max == AV_NOPTS_VALUE){ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1339 int step= 1024; |
810
2a84386ed2a8
avformat/av_seek_frame_binary with growing file patch by (Kenneth Aafly: kenneth, aafloy net)
michael
parents:
809
diff
changeset
|
1340 filesize = url_fsize(&s->pb); |
2a84386ed2a8
avformat/av_seek_frame_binary with growing file patch by (Kenneth Aafly: kenneth, aafloy net)
michael
parents:
809
diff
changeset
|
1341 pos_max = filesize - 1; |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1342 do{ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1343 pos_max -= step; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1344 ts_max = avif->read_timestamp(s, stream_index, &pos_max, pos_max + step); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1345 step += step; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1346 }while(ts_max == AV_NOPTS_VALUE && pos_max >= step); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1347 if (ts_max == AV_NOPTS_VALUE) |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1348 return -1; |
885 | 1349 |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1350 for(;;){ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1351 int64_t tmp_pos= pos_max + 1; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1352 int64_t tmp_ts= avif->read_timestamp(s, stream_index, &tmp_pos, INT64_MAX); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1353 if(tmp_ts == AV_NOPTS_VALUE) |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1354 break; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1355 ts_max= tmp_ts; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1356 pos_max= tmp_pos; |
810
2a84386ed2a8
avformat/av_seek_frame_binary with growing file patch by (Kenneth Aafly: kenneth, aafloy net)
michael
parents:
809
diff
changeset
|
1357 if(tmp_pos >= filesize) |
2a84386ed2a8
avformat/av_seek_frame_binary with growing file patch by (Kenneth Aafly: kenneth, aafloy net)
michael
parents:
809
diff
changeset
|
1358 break; |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1359 } |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1360 pos_limit= pos_max; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1361 } |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1362 |
926 | 1363 if(ts_min > ts_max){ |
1364 return -1; | |
1365 }else if(ts_min == ts_max){ | |
1366 pos_limit= pos_min; | |
1367 } | |
1368 | |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1369 no_change=0; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1370 while (pos_min < pos_limit) { |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1371 #ifdef DEBUG_SEEK |
885 | 1372 av_log(s, AV_LOG_DEBUG, "pos_min=0x%"PRIx64" pos_max=0x%"PRIx64" dts_min=%"PRId64" dts_max=%"PRId64"\n", |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1373 pos_min, pos_max, |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1374 ts_min, ts_max); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1375 #endif |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1376 assert(pos_limit <= pos_max); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1377 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1378 if(no_change==0){ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1379 int64_t approximate_keyframe_distance= pos_max - pos_limit; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1380 // interpolate position (better than dichotomy) |
555 | 1381 pos = av_rescale(target_ts - ts_min, pos_max - pos_min, ts_max - ts_min) |
1382 + pos_min - approximate_keyframe_distance; | |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1383 }else if(no_change==1){ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1384 // bisection, if interpolation failed to change min or max pos last time |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1385 pos = (pos_min + pos_limit)>>1; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1386 }else{ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1387 // linear search if bisection failed, can only happen if there are very few or no keframes between min/max |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1388 pos=pos_min; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1389 } |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1390 if(pos <= pos_min) |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1391 pos= pos_min + 1; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1392 else if(pos > pos_limit) |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1393 pos= pos_limit; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1394 start_pos= pos; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1395 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1396 ts = avif->read_timestamp(s, stream_index, &pos, INT64_MAX); //may pass pos_limit instead of -1 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1397 if(pos == pos_max) |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1398 no_change++; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1399 else |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1400 no_change=0; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1401 #ifdef DEBUG_SEEK |
881 | 1402 av_log(s, AV_LOG_DEBUG, "%"PRId64" %"PRId64" %"PRId64" / %"PRId64" %"PRId64" %"PRId64" target:%"PRId64" limit:%"PRId64" start:%"PRId64" noc:%d\n", pos_min, pos, pos_max, ts_min, ts, ts_max, target_ts, pos_limit, start_pos, no_change); |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1403 #endif |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1404 assert(ts != AV_NOPTS_VALUE); |
555 | 1405 if (target_ts <= ts) { |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1406 pos_limit = start_pos - 1; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1407 pos_max = pos; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1408 ts_max = ts; |
555 | 1409 } |
1410 if (target_ts >= ts) { | |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1411 pos_min = pos; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1412 ts_min = ts; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1413 } |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1414 } |
885 | 1415 |
555 | 1416 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max; |
1417 ts = (flags & AVSEEK_FLAG_BACKWARD) ? ts_min : ts_max; | |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1418 #ifdef DEBUG_SEEK |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1419 pos_min = pos; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1420 ts_min = avif->read_timestamp(s, stream_index, &pos_min, INT64_MAX); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1421 pos_min++; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1422 ts_max = avif->read_timestamp(s, stream_index, &pos_min, INT64_MAX); |
885 | 1423 av_log(s, AV_LOG_DEBUG, "pos=0x%"PRIx64" %"PRId64"<=%"PRId64"<=%"PRId64"\n", |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1424 pos, ts_min, target_ts, ts_max); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1425 #endif |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1426 /* do the seek */ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1427 url_fseek(&s->pb, pos, SEEK_SET); |
463
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1428 |
560 | 1429 av_update_cur_dts(s, st, ts); |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1430 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1431 return 0; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1432 } |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1433 |
555 | 1434 static int av_seek_frame_byte(AVFormatContext *s, int stream_index, int64_t pos, int flags){ |
1435 int64_t pos_min, pos_max; | |
1436 #if 0 | |
1437 AVStream *st; | |
1438 | |
1439 if (stream_index < 0) | |
1440 return -1; | |
1441 | |
1442 st= s->streams[stream_index]; | |
1443 #endif | |
1444 | |
1445 pos_min = s->data_offset; | |
764
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
757
diff
changeset
|
1446 pos_max = url_fsize(&s->pb) - 1; |
555 | 1447 |
1448 if (pos < pos_min) pos= pos_min; | |
1449 else if(pos > pos_max) pos= pos_max; | |
1450 | |
1451 url_fseek(&s->pb, pos, SEEK_SET); | |
1452 | |
1453 #if 0 | |
560 | 1454 av_update_cur_dts(s, st, ts); |
555 | 1455 #endif |
1456 return 0; | |
1457 } | |
1458 | |
885 | 1459 static int av_seek_frame_generic(AVFormatContext *s, |
555 | 1460 int stream_index, int64_t timestamp, int flags) |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1461 { |
560 | 1462 int index; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1463 AVStream *st; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1464 AVIndexEntry *ie; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1465 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1466 if (!s->index_built) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1467 if (is_raw_stream(s)) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1468 av_build_index_raw(s); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1469 } else { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1470 return -1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1471 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1472 s->index_built = 1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1473 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1474 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1475 st = s->streams[stream_index]; |
698 | 1476 index = av_index_search_timestamp(st, timestamp, flags); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1477 if (index < 0) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1478 return -1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1479 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1480 /* now we have found the index, we can seek */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1481 ie = &st->index_entries[index]; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1482 av_read_frame_flush(s); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1483 url_fseek(&s->pb, ie->pos, SEEK_SET); |
555 | 1484 |
560 | 1485 av_update_cur_dts(s, st, ie->timestamp); |
463
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1486 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1487 return 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1488 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1489 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1490 /** |
555 | 1491 * Seek to the key frame at timestamp. |
463
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1492 * 'timestamp' in 'stream_index'. |
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1493 * @param stream_index If stream_index is (-1), a default |
885 | 1494 * stream is selected, and timestamp is automatically converted |
557
084de726b4d0
default stream timebase docs patch by (Nathan Kurz <nate at verse dot com>)
michael
parents:
556
diff
changeset
|
1495 * from AV_TIME_BASE units to the stream specific time_base. |
555 | 1496 * @param timestamp timestamp in AVStream.time_base units |
809 | 1497 * or if there is no stream specified then in AV_TIME_BASE units |
555 | 1498 * @param flags flags which select direction and seeking mode |
463
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1499 * @return >= 0 on success |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1500 */ |
555 | 1501 int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1502 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1503 int ret; |
463
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1504 AVStream *st; |
885 | 1505 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1506 av_read_frame_flush(s); |
885 | 1507 |
555 | 1508 if(flags & AVSEEK_FLAG_BYTE) |
1509 return av_seek_frame_byte(s, stream_index, timestamp, flags); | |
885 | 1510 |
463
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1511 if(stream_index < 0){ |
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1512 stream_index= av_find_default_stream_index(s); |
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1513 if(stream_index < 0) |
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1514 return -1; |
885 | 1515 |
555 | 1516 st= s->streams[stream_index]; |
557
084de726b4d0
default stream timebase docs patch by (Nathan Kurz <nate at verse dot com>)
michael
parents:
556
diff
changeset
|
1517 /* timestamp for default must be expressed in AV_TIME_BASE units */ |
555 | 1518 timestamp = av_rescale(timestamp, st->time_base.den, AV_TIME_BASE * (int64_t)st->time_base.num); |
463
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1519 } |
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1520 st= s->streams[stream_index]; |
696f41bc8784
store index for seeking in the native timebase of each stream
michael
parents:
462
diff
changeset
|
1521 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1522 /* first, we try the format specific seek */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1523 if (s->iformat->read_seek) |
555 | 1524 ret = s->iformat->read_seek(s, stream_index, timestamp, flags); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1525 else |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1526 ret = -1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1527 if (ret >= 0) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1528 return 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1529 } |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1530 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1531 if(s->iformat->read_timestamp) |
555 | 1532 return av_seek_frame_binary(s, stream_index, timestamp, flags); |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1533 else |
555 | 1534 return av_seek_frame_generic(s, stream_index, timestamp, flags); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1535 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1536 |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
1537 /*******************************************************/ |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1538 |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1539 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1540 * Returns TRUE if the stream has accurate timings in any stream. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1541 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1542 * @return TRUE if the stream has accurate timings for at least one component. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1543 */ |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1544 static int av_has_timings(AVFormatContext *ic) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1545 { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1546 int i; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1547 AVStream *st; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1548 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1549 for(i = 0;i < ic->nb_streams; i++) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1550 st = ic->streams[i]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1551 if (st->start_time != AV_NOPTS_VALUE && |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1552 st->duration != AV_NOPTS_VALUE) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1553 return 1; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1554 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1555 return 0; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1556 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1557 |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1558 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1559 * Estimate the stream timings from the one of each components. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1560 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1561 * Also computes the global bitrate if possible. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
1562 */ |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1563 static void av_update_stream_timings(AVFormatContext *ic) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1564 { |
743 | 1565 int64_t start_time, start_time1, end_time, end_time1; |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1566 int i; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1567 AVStream *st; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1568 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1569 start_time = MAXINT64; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1570 end_time = MININT64; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1571 for(i = 0;i < ic->nb_streams; i++) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1572 st = ic->streams[i]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1573 if (st->start_time != AV_NOPTS_VALUE) { |
743 | 1574 start_time1= av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q); |
1575 if (start_time1 < start_time) | |
1576 start_time = start_time1; | |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1577 if (st->duration != AV_NOPTS_VALUE) { |
743 | 1578 end_time1 = start_time1 |
1579 + av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q); | |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1580 if (end_time1 > end_time) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1581 end_time = end_time1; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1582 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1583 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1584 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1585 if (start_time != MAXINT64) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1586 ic->start_time = start_time; |
786 | 1587 if (end_time != MININT64) { |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1588 ic->duration = end_time - start_time; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1589 if (ic->file_size > 0) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1590 /* compute the bit rate */ |
885 | 1591 ic->bit_rate = (double)ic->file_size * 8.0 * AV_TIME_BASE / |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1592 (double)ic->duration; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1593 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1594 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1595 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1596 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1597 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1598 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1599 static void fill_all_stream_timings(AVFormatContext *ic) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1600 { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1601 int i; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1602 AVStream *st; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1603 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1604 av_update_stream_timings(ic); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1605 for(i = 0;i < ic->nb_streams; i++) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1606 st = ic->streams[i]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1607 if (st->start_time == AV_NOPTS_VALUE) { |
743 | 1608 if(ic->start_time != AV_NOPTS_VALUE) |
1609 st->start_time = av_rescale_q(ic->start_time, AV_TIME_BASE_Q, st->time_base); | |
1610 if(ic->duration != AV_NOPTS_VALUE) | |
1611 st->duration = av_rescale_q(ic->duration, AV_TIME_BASE_Q, st->time_base); | |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1612 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1613 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1614 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1615 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1616 static void av_estimate_timings_from_bit_rate(AVFormatContext *ic) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1617 { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1618 int64_t filesize, duration; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1619 int bit_rate, i; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1620 AVStream *st; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1621 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1622 /* if bit_rate is already set, we believe it */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1623 if (ic->bit_rate == 0) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1624 bit_rate = 0; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1625 for(i=0;i<ic->nb_streams;i++) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1626 st = ic->streams[i]; |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
1627 bit_rate += st->codec->bit_rate; |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1628 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1629 ic->bit_rate = bit_rate; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1630 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1631 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1632 /* if duration is already set, we believe it */ |
885 | 1633 if (ic->duration == AV_NOPTS_VALUE && |
1634 ic->bit_rate != 0 && | |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1635 ic->file_size != 0) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1636 filesize = ic->file_size; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1637 if (filesize > 0) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1638 for(i = 0; i < ic->nb_streams; i++) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1639 st = ic->streams[i]; |
743 | 1640 duration= av_rescale(8*filesize, st->time_base.den, ic->bit_rate*(int64_t)st->time_base.num); |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1641 if (st->start_time == AV_NOPTS_VALUE || |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1642 st->duration == AV_NOPTS_VALUE) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1643 st->start_time = 0; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1644 st->duration = duration; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1645 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1646 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1647 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1648 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1649 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1650 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1651 #define DURATION_MAX_READ_SIZE 250000 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1652 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1653 /* only usable for MPEG-PS streams */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1654 static void av_estimate_timings_from_pts(AVFormatContext *ic) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1655 { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1656 AVPacket pkt1, *pkt = &pkt1; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1657 AVStream *st; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1658 int read_size, i, ret; |
838 | 1659 int64_t end_time; |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1660 int64_t filesize, offset, duration; |
885 | 1661 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1662 /* free previous packet */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1663 if (ic->cur_st && ic->cur_st->parser) |
885 | 1664 av_free_packet(&ic->cur_pkt); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1665 ic->cur_st = NULL; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1666 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1667 /* flush packet queue */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1668 flush_packet_queue(ic); |
488 | 1669 |
1670 for(i=0;i<ic->nb_streams;i++) { | |
1671 st = ic->streams[i]; | |
1672 if (st->parser) { | |
1673 av_parser_close(st->parser); | |
1674 st->parser= NULL; | |
1675 } | |
1676 } | |
885 | 1677 |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1678 /* we read the first packets to get the first PTS (not fully |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1679 accurate, but it is enough now) */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1680 url_fseek(&ic->pb, 0, SEEK_SET); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1681 read_size = 0; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1682 for(;;) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1683 if (read_size >= DURATION_MAX_READ_SIZE) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1684 break; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1685 /* if all info is available, we can stop */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1686 for(i = 0;i < ic->nb_streams; i++) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1687 st = ic->streams[i]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1688 if (st->start_time == AV_NOPTS_VALUE) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1689 break; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1690 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1691 if (i == ic->nb_streams) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1692 break; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1693 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1694 ret = av_read_packet(ic, pkt); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1695 if (ret != 0) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1696 break; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1697 read_size += pkt->size; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1698 st = ic->streams[pkt->stream_index]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1699 if (pkt->pts != AV_NOPTS_VALUE) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1700 if (st->start_time == AV_NOPTS_VALUE) |
743 | 1701 st->start_time = pkt->pts; |
224
8d1569be0ee1
memory leak fix by (Tom Dexter <devel at www dot digitalaudiorock dot com>)
michaelni
parents:
205
diff
changeset
|
1702 } |
8d1569be0ee1
memory leak fix by (Tom Dexter <devel at www dot digitalaudiorock dot com>)
michaelni
parents:
205
diff
changeset
|
1703 av_free_packet(pkt); |
8d1569be0ee1
memory leak fix by (Tom Dexter <devel at www dot digitalaudiorock dot com>)
michaelni
parents:
205
diff
changeset
|
1704 } |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1705 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1706 /* estimate the end time (duration) */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1707 /* XXX: may need to support wrapping */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1708 filesize = ic->file_size; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1709 offset = filesize - DURATION_MAX_READ_SIZE; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1710 if (offset < 0) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1711 offset = 0; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1712 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1713 url_fseek(&ic->pb, offset, SEEK_SET); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1714 read_size = 0; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1715 for(;;) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1716 if (read_size >= DURATION_MAX_READ_SIZE) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1717 break; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1718 /* if all info is available, we can stop */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1719 for(i = 0;i < ic->nb_streams; i++) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1720 st = ic->streams[i]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1721 if (st->duration == AV_NOPTS_VALUE) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1722 break; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1723 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1724 if (i == ic->nb_streams) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1725 break; |
885 | 1726 |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1727 ret = av_read_packet(ic, pkt); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1728 if (ret != 0) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1729 break; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1730 read_size += pkt->size; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1731 st = ic->streams[pkt->stream_index]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1732 if (pkt->pts != AV_NOPTS_VALUE) { |
743 | 1733 end_time = pkt->pts; |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1734 duration = end_time - st->start_time; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1735 if (duration > 0) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1736 if (st->duration == AV_NOPTS_VALUE || |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1737 st->duration < duration) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1738 st->duration = duration; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1739 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1740 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1741 av_free_packet(pkt); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1742 } |
885 | 1743 |
743 | 1744 fill_all_stream_timings(ic); |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1745 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1746 url_fseek(&ic->pb, 0, SEEK_SET); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1747 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1748 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1749 static void av_estimate_timings(AVFormatContext *ic) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1750 { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1751 int64_t file_size; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1752 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1753 /* get the file size, if possible */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1754 if (ic->iformat->flags & AVFMT_NOFILE) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1755 file_size = 0; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1756 } else { |
764
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
757
diff
changeset
|
1757 file_size = url_fsize(&ic->pb); |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1758 if (file_size < 0) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1759 file_size = 0; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1760 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1761 ic->file_size = file_size; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1762 |
606
ef6d04c1dd9a
use pts based duration/bitrate guessing code for mpeg-ts
michael
parents:
605
diff
changeset
|
1763 if ((ic->iformat == &mpegps_demux || ic->iformat == &mpegts_demux) && file_size && !ic->pb.is_streamed) { |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1764 /* get accurate estimate from the PTSes */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1765 av_estimate_timings_from_pts(ic); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1766 } else if (av_has_timings(ic)) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1767 /* at least one components has timings - we use them for all |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1768 the components */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1769 fill_all_stream_timings(ic); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1770 } else { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1771 /* less precise: use bit rate info */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1772 av_estimate_timings_from_bit_rate(ic); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1773 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1774 av_update_stream_timings(ic); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1775 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1776 #if 0 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1777 { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1778 int i; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1779 AVStream *st; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1780 for(i = 0;i < ic->nb_streams; i++) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1781 st = ic->streams[i]; |
885 | 1782 printf("%d: start_time: %0.3f duration: %0.3f\n", |
1783 i, (double)st->start_time / AV_TIME_BASE, | |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1784 (double)st->duration / AV_TIME_BASE); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1785 } |
885 | 1786 printf("stream: start_time: %0.3f duration: %0.3f bitrate=%d kb/s\n", |
1787 (double)ic->start_time / AV_TIME_BASE, | |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1788 (double)ic->duration / AV_TIME_BASE, |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1789 ic->bit_rate / 1000); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1790 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1791 #endif |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1792 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1793 |
0 | 1794 static int has_codec_parameters(AVCodecContext *enc) |
1795 { | |
1796 int val; | |
1797 switch(enc->codec_type) { | |
1798 case CODEC_TYPE_AUDIO: | |
1799 val = enc->sample_rate; | |
1800 break; | |
1801 case CODEC_TYPE_VIDEO: | |
737 | 1802 val = enc->width && enc->pix_fmt != PIX_FMT_NONE; |
0 | 1803 break; |
1804 default: | |
1805 val = 1; | |
1806 break; | |
1807 } | |
1808 return (val != 0); | |
1809 } | |
1810 | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1811 static int try_decode_frame(AVStream *st, const uint8_t *data, int size) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1812 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1813 int16_t *samples; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1814 AVCodec *codec; |
801
bca2334ff177
dont open and close codec at every call of try_decode_frame() as this is not only slow but also fails if the previous frame is needed for setting some parameters correctly
michael
parents:
797
diff
changeset
|
1815 int got_picture, ret=0; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1816 AVFrame picture; |
885 | 1817 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
1818 if(!st->codec->codec){ |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
1819 codec = avcodec_find_decoder(st->codec->codec_id); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1820 if (!codec) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1821 return -1; |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
1822 ret = avcodec_open(st->codec, codec); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1823 if (ret < 0) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1824 return ret; |
801
bca2334ff177
dont open and close codec at every call of try_decode_frame() as this is not only slow but also fails if the previous frame is needed for setting some parameters correctly
michael
parents:
797
diff
changeset
|
1825 } |
737 | 1826 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
1827 if(!has_codec_parameters(st->codec)){ |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
1828 switch(st->codec->codec_type) { |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1829 case CODEC_TYPE_VIDEO: |
885 | 1830 ret = avcodec_decode_video(st->codec, &picture, |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1831 &got_picture, (uint8_t *)data, size); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1832 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1833 case CODEC_TYPE_AUDIO: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1834 samples = av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1835 if (!samples) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1836 goto fail; |
885 | 1837 ret = avcodec_decode_audio(st->codec, samples, |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1838 &got_picture, (uint8_t *)data, size); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1839 av_free(samples); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1840 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1841 default: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1842 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1843 } |
737 | 1844 } |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1845 fail: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1846 return ret; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1847 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1848 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1849 /* absolute maximum size we read until we abort */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1850 #define MAX_READ_SIZE 5000000 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1851 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1852 /* maximum duration until we stop analysing the stream */ |
1127 | 1853 #define MAX_STREAM_DURATION ((int)(AV_TIME_BASE * 3.0)) |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1854 |
0 | 1855 /** |
1856 * Read the beginning of a media file to get stream information. This | |
1857 * is useful for file formats with no headers such as MPEG. This | |
1858 * function also compute the real frame rate in case of mpeg2 repeat | |
1859 * frame mode. | |
1860 * | |
1861 * @param ic media file handle | |
885 | 1862 * @return >=0 if OK. AVERROR_xxx if error. |
737 | 1863 * @todo let user decide somehow what information is needed so we dont waste time geting stuff the user doesnt need |
0 | 1864 */ |
1865 int av_find_stream_info(AVFormatContext *ic) | |
1866 { | |
927 | 1867 int i, count, ret, read_size, j; |
0 | 1868 AVStream *st; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1869 AVPacket pkt1, *pkt; |
0 | 1870 AVPacketList *pktl=NULL, **ppktl; |
620 | 1871 int64_t last_dts[MAX_STREAMS]; |
750 | 1872 int64_t duration_sum[MAX_STREAMS]; |
1873 int duration_count[MAX_STREAMS]={0}; | |
0 | 1874 |
743 | 1875 for(i=0;i<ic->nb_streams;i++) { |
1876 st = ic->streams[i]; | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
1877 if(st->codec->codec_type == CODEC_TYPE_VIDEO){ |
743 | 1878 /* if(!st->time_base.num) |
1879 st->time_base= */ | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
1880 if(!st->codec->time_base.num) |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
1881 st->codec->time_base= st->time_base; |
743 | 1882 } |
797
f5a231a9a2f1
support changing in bitstream global headers into extradata style and back
michael
parents:
786
diff
changeset
|
1883 //only for the split stuff |
f5a231a9a2f1
support changing in bitstream global headers into extradata style and back
michael
parents:
786
diff
changeset
|
1884 if (!st->parser) { |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
1885 st->parser = av_parser_init(st->codec->codec_id); |
842 | 1886 if(st->need_parsing == 2 && st->parser){ |
1887 st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES; | |
1888 } | |
797
f5a231a9a2f1
support changing in bitstream global headers into extradata style and back
michael
parents:
786
diff
changeset
|
1889 } |
743 | 1890 } |
1891 | |
620 | 1892 for(i=0;i<MAX_STREAMS;i++){ |
1893 last_dts[i]= AV_NOPTS_VALUE; | |
750 | 1894 duration_sum[i]= INT64_MAX; |
620 | 1895 } |
885 | 1896 |
0 | 1897 count = 0; |
1898 read_size = 0; | |
1899 ppktl = &ic->packet_buffer; | |
1900 for(;;) { | |
1901 /* check if one codec still needs to be handled */ | |
1902 for(i=0;i<ic->nb_streams;i++) { | |
1903 st = ic->streams[i]; | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
1904 if (!has_codec_parameters(st->codec)) |
0 | 1905 break; |
624 | 1906 /* variable fps and no guess at the real fps */ |
927 | 1907 if( st->codec->time_base.den >= 101LL*st->codec->time_base.num |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
1908 && duration_count[i]<20 && st->codec->codec_type == CODEC_TYPE_VIDEO) |
624 | 1909 break; |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
1910 if(st->parser && st->parser->parser->split && !st->codec->extradata) |
797
f5a231a9a2f1
support changing in bitstream global headers into extradata style and back
michael
parents:
786
diff
changeset
|
1911 break; |
0 | 1912 } |
1913 if (i == ic->nb_streams) { | |
1914 /* NOTE: if the format has no header, then we need to read | |
1915 some packets to get most of the streams, so we cannot | |
1916 stop here */ | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1917 if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) { |
0 | 1918 /* if we found the info for all the codecs, we can stop */ |
1919 ret = count; | |
1920 break; | |
1921 } | |
1922 } else { | |
1923 /* we did not get all the codec info, but we read too much data */ | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1924 if (read_size >= MAX_READ_SIZE) { |
0 | 1925 ret = count; |
1926 break; | |
1927 } | |
1928 } | |
1929 | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1930 /* NOTE: a new stream can be added there if no header in file |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1931 (AVFMTCTX_NOHEADER) */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1932 ret = av_read_frame_internal(ic, &pkt1); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1933 if (ret < 0) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1934 /* EOF or error */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1935 ret = -1; /* we could not have all the codec parameters before EOF */ |
651
6a5ba24b2c6b
fixing demuxing for short files where the framerate detection failed
michael
parents:
643
diff
changeset
|
1936 for(i=0;i<ic->nb_streams;i++) { |
6a5ba24b2c6b
fixing demuxing for short files where the framerate detection failed
michael
parents:
643
diff
changeset
|
1937 st = ic->streams[i]; |
1018
bddcb32bde33
provide more info on which streams lack codec parameters
michael
parents:
1003
diff
changeset
|
1938 if (!has_codec_parameters(st->codec)){ |
bddcb32bde33
provide more info on which streams lack codec parameters
michael
parents:
1003
diff
changeset
|
1939 char buf[256]; |
bddcb32bde33
provide more info on which streams lack codec parameters
michael
parents:
1003
diff
changeset
|
1940 avcodec_string(buf, sizeof(buf), st->codec, 0); |
bddcb32bde33
provide more info on which streams lack codec parameters
michael
parents:
1003
diff
changeset
|
1941 av_log(ic, AV_LOG_INFO, "Could not find codec parameters (%s)\n", buf); |
1139
ff241582890f
do not fail av_find_stream_info() if params for all streams could not be found
mru
parents:
1135
diff
changeset
|
1942 } else { |
ff241582890f
do not fail av_find_stream_info() if params for all streams could not be found
mru
parents:
1135
diff
changeset
|
1943 ret = 0; |
1018
bddcb32bde33
provide more info on which streams lack codec parameters
michael
parents:
1003
diff
changeset
|
1944 } |
651
6a5ba24b2c6b
fixing demuxing for short files where the framerate detection failed
michael
parents:
643
diff
changeset
|
1945 } |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1946 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1947 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1948 |
0 | 1949 pktl = av_mallocz(sizeof(AVPacketList)); |
1950 if (!pktl) { | |
1951 ret = AVERROR_NOMEM; | |
1952 break; | |
1953 } | |
1954 | |
1955 /* add the packet in the buffered packet list */ | |
1956 *ppktl = pktl; | |
1957 ppktl = &pktl->next; | |
1958 | |
1959 pkt = &pktl->pkt; | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1960 *pkt = pkt1; |
885 | 1961 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1962 /* duplicate the packet */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1963 if (av_dup_packet(pkt) < 0) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1964 ret = AVERROR_NOMEM; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1965 break; |
0 | 1966 } |
1967 | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1968 read_size += pkt->size; |
0 | 1969 |
1970 st = ic->streams[pkt->stream_index]; | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1971 st->codec_info_duration += pkt->duration; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1972 if (pkt->duration != 0) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1973 st->codec_info_nb_frames++; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1974 |
772 | 1975 { |
750 | 1976 int index= pkt->stream_index; |
1977 int64_t last= last_dts[index]; | |
1978 int64_t duration= pkt->dts - last; | |
1979 | |
1980 if(pkt->dts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && duration>0){ | |
1981 if(duration*duration_count[index]*10/9 < duration_sum[index]){ | |
1982 duration_sum[index]= duration; | |
1983 duration_count[index]=1; | |
1984 }else{ | |
1985 int factor= av_rescale(duration, duration_count[index], duration_sum[index]); | |
1986 duration_sum[index] += duration; | |
1987 duration_count[index]+= factor; | |
1988 } | |
801
bca2334ff177
dont open and close codec at every call of try_decode_frame() as this is not only slow but also fails if the previous frame is needed for setting some parameters correctly
michael
parents:
797
diff
changeset
|
1989 if(st->codec_info_nb_frames == 0 && 0) |
772 | 1990 st->codec_info_duration += duration; |
620 | 1991 } |
1992 last_dts[pkt->stream_index]= pkt->dts; | |
1993 } | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
1994 if(st->parser && st->parser->parser->split && !st->codec->extradata){ |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
1995 int i= st->parser->parser->split(st->codec, pkt->data, pkt->size); |
797
f5a231a9a2f1
support changing in bitstream global headers into extradata style and back
michael
parents:
786
diff
changeset
|
1996 if(i){ |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
1997 st->codec->extradata_size= i; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
1998 st->codec->extradata= av_malloc(st->codec->extradata_size); |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
1999 memcpy(st->codec->extradata, pkt->data, st->codec->extradata_size); |
797
f5a231a9a2f1
support changing in bitstream global headers into extradata style and back
michael
parents:
786
diff
changeset
|
2000 } |
f5a231a9a2f1
support changing in bitstream global headers into extradata style and back
michael
parents:
786
diff
changeset
|
2001 } |
885 | 2002 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2003 /* if still no information, we try to open the codec and to |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2004 decompress the frame. We try to avoid that in most cases as |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2005 it takes longer and uses more memory. For MPEG4, we need to |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2006 decompress for Quicktime. */ |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2007 if (!has_codec_parameters(st->codec) /*&& |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2008 (st->codec->codec_id == CODEC_ID_FLV1 || |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2009 st->codec->codec_id == CODEC_ID_H264 || |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2010 st->codec->codec_id == CODEC_ID_H263 || |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2011 st->codec->codec_id == CODEC_ID_H261 || |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2012 st->codec->codec_id == CODEC_ID_VORBIS || |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2013 st->codec->codec_id == CODEC_ID_MJPEG || |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2014 st->codec->codec_id == CODEC_ID_PNG || |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2015 st->codec->codec_id == CODEC_ID_PAM || |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2016 st->codec->codec_id == CODEC_ID_PGM || |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2017 st->codec->codec_id == CODEC_ID_PGMYUV || |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2018 st->codec->codec_id == CODEC_ID_PBM || |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2019 st->codec->codec_id == CODEC_ID_PPM || |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2020 st->codec->codec_id == CODEC_ID_SHORTEN || |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2021 (st->codec->codec_id == CODEC_ID_MPEG4 && !st->need_parsing))*/) |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2022 try_decode_frame(st, pkt->data, pkt->size); |
885 | 2023 |
772 | 2024 if (av_rescale_q(st->codec_info_duration, st->time_base, AV_TIME_BASE_Q) >= MAX_STREAM_DURATION) { |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2025 break; |
0 | 2026 } |
2027 count++; | |
2028 } | |
2029 | |
801
bca2334ff177
dont open and close codec at every call of try_decode_frame() as this is not only slow but also fails if the previous frame is needed for setting some parameters correctly
michael
parents:
797
diff
changeset
|
2030 // close codecs which where opened in try_decode_frame() |
bca2334ff177
dont open and close codec at every call of try_decode_frame() as this is not only slow but also fails if the previous frame is needed for setting some parameters correctly
michael
parents:
797
diff
changeset
|
2031 for(i=0;i<ic->nb_streams;i++) { |
bca2334ff177
dont open and close codec at every call of try_decode_frame() as this is not only slow but also fails if the previous frame is needed for setting some parameters correctly
michael
parents:
797
diff
changeset
|
2032 st = ic->streams[i]; |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2033 if(st->codec->codec) |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2034 avcodec_close(st->codec); |
801
bca2334ff177
dont open and close codec at every call of try_decode_frame() as this is not only slow but also fails if the previous frame is needed for setting some parameters correctly
michael
parents:
797
diff
changeset
|
2035 } |
0 | 2036 for(i=0;i<ic->nb_streams;i++) { |
2037 st = ic->streams[i]; | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2038 if (st->codec->codec_type == CODEC_TYPE_VIDEO) { |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2039 if(st->codec->codec_id == CODEC_ID_RAWVIDEO && !st->codec->codec_tag && !st->codec->bits_per_sample) |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2040 st->codec->codec_tag= avcodec_pix_fmt_to_codec_tag(st->codec->pix_fmt); |
620 | 2041 |
927 | 2042 if(duration_count[i] && st->codec->time_base.num*101LL <= st->codec->time_base.den && |
2043 st->time_base.num*duration_sum[i]/duration_count[i]*101LL > st->time_base.den){ | |
2044 int64_t num, den, error, best_error; | |
625 | 2045 |
750 | 2046 num= st->time_base.den*duration_count[i]; |
2047 den= st->time_base.num*duration_sum[i]; | |
885 | 2048 |
927 | 2049 best_error= INT64_MAX; |
2050 for(j=1; j<60*12; j++){ | |
2051 error= ABS(1001*12*num - 1001*j*den); | |
2052 if(error < best_error){ | |
2053 best_error= error; | |
2054 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, j, 12, INT_MAX); | |
2055 } | |
2056 } | |
2057 for(j=24; j<=30; j+=6){ | |
2058 error= ABS(1001*12*num - 1000*12*j*den); | |
2059 if(error < best_error){ | |
2060 best_error= error; | |
2061 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, j*1000, 1001, INT_MAX); | |
2062 } | |
750 | 2063 } |
620 | 2064 } |
2065 | |
578 | 2066 /* set real frame rate info */ |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2067 /* compute the real frame rate for telecine */ |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2068 if ((st->codec->codec_id == CODEC_ID_MPEG1VIDEO || |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2069 st->codec->codec_id == CODEC_ID_MPEG2VIDEO) && |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2070 st->codec->sub_id == 2) { |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2071 if (st->codec_info_nb_frames >= 20) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2072 float coded_frame_rate, est_frame_rate; |
885 | 2073 est_frame_rate = ((double)st->codec_info_nb_frames * AV_TIME_BASE) / |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2074 (double)st->codec_info_duration ; |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2075 coded_frame_rate = 1.0/av_q2d(st->codec->time_base); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2076 #if 0 |
885 | 2077 printf("telecine: coded_frame_rate=%0.3f est_frame_rate=%0.3f\n", |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2078 coded_frame_rate, est_frame_rate); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2079 #endif |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2080 /* if we detect that it could be a telecine, we |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2081 signal it. It would be better to do it at a |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2082 higher level as it can change in a film */ |
885 | 2083 if (coded_frame_rate >= 24.97 && |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2084 (est_frame_rate >= 23.5 && est_frame_rate < 24.5)) { |
743 | 2085 st->r_frame_rate = (AVRational){24000, 1001}; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2086 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2087 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2088 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2089 /* if no real frame rate, use the codec one */ |
743 | 2090 if (!st->r_frame_rate.num){ |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2091 st->r_frame_rate.num = st->codec->time_base.den; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2092 st->r_frame_rate.den = st->codec->time_base.num; |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
2093 } |
0 | 2094 } |
2095 } | |
2096 | |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
2097 av_estimate_timings(ic); |
468 | 2098 #if 0 |
2099 /* correct DTS for b frame streams with no timestamps */ | |
2100 for(i=0;i<ic->nb_streams;i++) { | |
2101 st = ic->streams[i]; | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2102 if (st->codec->codec_type == CODEC_TYPE_VIDEO) { |
468 | 2103 if(b-frames){ |
2104 ppktl = &ic->packet_buffer; | |
2105 while(ppkt1){ | |
2106 if(ppkt1->stream_index != i) | |
2107 continue; | |
2108 if(ppkt1->pkt->dts < 0) | |
2109 break; | |
2110 if(ppkt1->pkt->pts != AV_NOPTS_VALUE) | |
2111 break; | |
2112 ppkt1->pkt->dts -= delta; | |
2113 ppkt1= ppkt1->next; | |
2114 } | |
2115 if(ppkt1) | |
2116 continue; | |
2117 st->cur_dts -= delta; | |
2118 } | |
2119 } | |
2120 } | |
2121 #endif | |
0 | 2122 return ret; |
2123 } | |
2124 | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2125 /*******************************************************/ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2126 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2127 /** |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2128 * start playing a network based stream (e.g. RTSP stream) at the |
885 | 2129 * current position |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2130 */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2131 int av_read_play(AVFormatContext *s) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2132 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2133 if (!s->iformat->read_play) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2134 return AVERROR_NOTSUPP; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2135 return s->iformat->read_play(s); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2136 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2137 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2138 /** |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2139 * Pause a network based stream (e.g. RTSP stream). |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2140 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2141 * Use av_read_play() to resume it. |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2142 */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2143 int av_read_pause(AVFormatContext *s) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2144 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2145 if (!s->iformat->read_pause) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2146 return AVERROR_NOTSUPP; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2147 return s->iformat->read_pause(s); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2148 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2149 |
0 | 2150 /** |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2151 * Close a media file (but not its codecs). |
0 | 2152 * |
2153 * @param s media file handle | |
2154 */ | |
2155 void av_close_input_file(AVFormatContext *s) | |
2156 { | |
172 | 2157 int i, must_open_file; |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
2158 AVStream *st; |
0 | 2159 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2160 /* free previous packet */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2161 if (s->cur_st && s->cur_st->parser) |
885 | 2162 av_free_packet(&s->cur_pkt); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2163 |
0 | 2164 if (s->iformat->read_close) |
2165 s->iformat->read_close(s); | |
2166 for(i=0;i<s->nb_streams;i++) { | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
2167 /* free all data in a stream component */ |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
2168 st = s->streams[i]; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2169 if (st->parser) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2170 av_parser_close(st->parser); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2171 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2172 av_free(st->index_entries); |
1120 | 2173 av_free(st->codec->extradata); |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2174 av_free(st->codec); |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
2175 av_free(st); |
0 | 2176 } |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2177 flush_packet_queue(s); |
172 | 2178 must_open_file = 1; |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
2179 if (s->iformat->flags & AVFMT_NOFILE) { |
172 | 2180 must_open_file = 0; |
2181 } | |
2182 if (must_open_file) { | |
0 | 2183 url_fclose(&s->pb); |
2184 } | |
2185 av_freep(&s->priv_data); | |
2186 av_free(s); | |
2187 } | |
2188 | |
2189 /** | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2190 * Add a new stream to a media file. |
0 | 2191 * |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2192 * Can only be called in the read_header() function. If the flag |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2193 * AVFMTCTX_NOHEADER is in the format context, then new streams |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2194 * can be added in read_packet too. |
0 | 2195 * |
2196 * @param s media file handle | |
885 | 2197 * @param id file format dependent stream id |
0 | 2198 */ |
2199 AVStream *av_new_stream(AVFormatContext *s, int id) | |
2200 { | |
2201 AVStream *st; | |
2202 | |
2203 if (s->nb_streams >= MAX_STREAMS) | |
2204 return NULL; | |
2205 | |
2206 st = av_mallocz(sizeof(AVStream)); | |
2207 if (!st) | |
2208 return NULL; | |
885 | 2209 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2210 st->codec= avcodec_alloc_context(); |
193 | 2211 if (s->iformat) { |
2212 /* no default bitrate if decoding */ | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2213 st->codec->bit_rate = 0; |
193 | 2214 } |
0 | 2215 st->index = s->nb_streams; |
2216 st->id = id; | |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
2217 st->start_time = AV_NOPTS_VALUE; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
2218 st->duration = AV_NOPTS_VALUE; |
468 | 2219 st->cur_dts = AV_NOPTS_VALUE; |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
453
diff
changeset
|
2220 |
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
453
diff
changeset
|
2221 /* default pts settings is MPEG like */ |
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
453
diff
changeset
|
2222 av_set_pts_info(st, 33, 1, 90000); |
464 | 2223 st->last_IP_pts = AV_NOPTS_VALUE; |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
453
diff
changeset
|
2224 |
0 | 2225 s->streams[s->nb_streams++] = st; |
2226 return st; | |
2227 } | |
2228 | |
2229 /************************************************************/ | |
2230 /* output media file */ | |
2231 | |
20 | 2232 int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap) |
2233 { | |
2234 int ret; | |
885 | 2235 |
32 | 2236 if (s->oformat->priv_data_size > 0) { |
2237 s->priv_data = av_mallocz(s->oformat->priv_data_size); | |
2238 if (!s->priv_data) | |
2239 return AVERROR_NOMEM; | |
2240 } else | |
2241 s->priv_data = NULL; | |
885 | 2242 |
20 | 2243 if (s->oformat->set_parameters) { |
2244 ret = s->oformat->set_parameters(s, ap); | |
2245 if (ret < 0) | |
2246 return ret; | |
2247 } | |
2248 return 0; | |
2249 } | |
2250 | |
0 | 2251 /** |
2252 * allocate the stream private data and write the stream header to an | |
2253 * output media file | |
2254 * | |
2255 * @param s media file handle | |
885 | 2256 * @return 0 if OK. AVERROR_xxx if error. |
0 | 2257 */ |
2258 int av_write_header(AVFormatContext *s) | |
2259 { | |
2260 int ret, i; | |
2261 AVStream *st; | |
2262 | |
839 | 2263 // some sanity checks |
2264 for(i=0;i<s->nb_streams;i++) { | |
2265 st = s->streams[i]; | |
2266 | |
2267 switch (st->codec->codec_type) { | |
2268 case CODEC_TYPE_AUDIO: | |
2269 if(st->codec->sample_rate<=0){ | |
2270 av_log(s, AV_LOG_ERROR, "sample rate not set\n"); | |
2271 return -1; | |
2272 } | |
2273 break; | |
2274 case CODEC_TYPE_VIDEO: | |
2275 if(st->codec->time_base.num<=0 || st->codec->time_base.den<=0){ //FIXME audio too? | |
2276 av_log(s, AV_LOG_ERROR, "time base not set\n"); | |
2277 return -1; | |
2278 } | |
2279 if(st->codec->width<=0 || st->codec->height<=0){ | |
2280 av_log(s, AV_LOG_ERROR, "dimensions not set\n"); | |
2281 return -1; | |
2282 } | |
2283 break; | |
2284 } | |
2285 } | |
2286 | |
869 | 2287 if(s->oformat->write_header){ |
2288 ret = s->oformat->write_header(s); | |
2289 if (ret < 0) | |
2290 return ret; | |
2291 } | |
0 | 2292 |
2293 /* init PTS generation */ | |
2294 for(i=0;i<s->nb_streams;i++) { | |
840
8834d33b7ca1
minor fixes for invalid audio data patch by (Wolfram Gloger: wmglo, dent med uni-muenchen de)
michael
parents:
839
diff
changeset
|
2295 int64_t den = AV_NOPTS_VALUE; |
0 | 2296 st = s->streams[i]; |
2297 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2298 switch (st->codec->codec_type) { |
0 | 2299 case CODEC_TYPE_AUDIO: |
840
8834d33b7ca1
minor fixes for invalid audio data patch by (Wolfram Gloger: wmglo, dent med uni-muenchen de)
michael
parents:
839
diff
changeset
|
2300 den = (int64_t)st->time_base.num * st->codec->sample_rate; |
0 | 2301 break; |
2302 case CODEC_TYPE_VIDEO: | |
840
8834d33b7ca1
minor fixes for invalid audio data patch by (Wolfram Gloger: wmglo, dent med uni-muenchen de)
michael
parents:
839
diff
changeset
|
2303 den = (int64_t)st->time_base.num * st->codec->time_base.den; |
0 | 2304 break; |
2305 default: | |
2306 break; | |
2307 } | |
840
8834d33b7ca1
minor fixes for invalid audio data patch by (Wolfram Gloger: wmglo, dent med uni-muenchen de)
michael
parents:
839
diff
changeset
|
2308 if (den != AV_NOPTS_VALUE) { |
8834d33b7ca1
minor fixes for invalid audio data patch by (Wolfram Gloger: wmglo, dent med uni-muenchen de)
michael
parents:
839
diff
changeset
|
2309 if (den <= 0) |
8834d33b7ca1
minor fixes for invalid audio data patch by (Wolfram Gloger: wmglo, dent med uni-muenchen de)
michael
parents:
839
diff
changeset
|
2310 return AVERROR_INVALIDDATA; |
8834d33b7ca1
minor fixes for invalid audio data patch by (Wolfram Gloger: wmglo, dent med uni-muenchen de)
michael
parents:
839
diff
changeset
|
2311 av_frac_init(&st->pts, 0, 0, den); |
8834d33b7ca1
minor fixes for invalid audio data patch by (Wolfram Gloger: wmglo, dent med uni-muenchen de)
michael
parents:
839
diff
changeset
|
2312 } |
0 | 2313 } |
2314 return 0; | |
2315 } | |
2316 | |
470 | 2317 //FIXME merge with compute_pkt_fields |
617
1ca4877e42f3
some sanity checks on what is muxed, invalid timestamps in mpeg are very common and lead to strange errors in the mpeg muxer otherwise
michael
parents:
616
diff
changeset
|
2318 static int compute_pkt_fields2(AVStream *st, AVPacket *pkt){ |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2319 int b_frames = FFMAX(st->codec->has_b_frames, st->codec->max_b_frames); |
470 | 2320 int num, den, frame_size; |
0 | 2321 |
470 | 2322 // av_log(NULL, AV_LOG_DEBUG, "av_write_frame: pts:%lld dts:%lld cur_dts:%lld b:%d size:%d st:%d\n", pkt->pts, pkt->dts, st->cur_dts, b_frames, pkt->size, pkt->stream_index); |
885 | 2323 |
468 | 2324 /* if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE) |
2325 return -1;*/ | |
885 | 2326 |
468 | 2327 /* duration field */ |
470 | 2328 if (pkt->duration == 0) { |
2329 compute_frame_duration(&num, &den, st, NULL, pkt); | |
2330 if (den && num) { | |
2331 pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den, den * (int64_t)st->time_base.num); | |
2332 } | |
2333 } | |
409 | 2334 |
468 | 2335 //XXX/FIXME this is a temporary hack until all encoders output pts |
2336 if((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !b_frames){ | |
2337 pkt->dts= | |
2338 // pkt->pts= st->cur_dts; | |
2339 pkt->pts= st->pts.val; | |
2340 } | |
2341 | |
885 | 2342 //calculate dts from pts |
468 | 2343 if(pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE){ |
2344 if(b_frames){ | |
2345 if(st->last_IP_pts == AV_NOPTS_VALUE){ | |
470 | 2346 st->last_IP_pts= -pkt->duration; |
468 | 2347 } |
2348 if(st->last_IP_pts < pkt->pts){ | |
2349 pkt->dts= st->last_IP_pts; | |
2350 st->last_IP_pts= pkt->pts; | |
2351 }else | |
2352 pkt->dts= pkt->pts; | |
2353 }else | |
2354 pkt->dts= pkt->pts; | |
2355 } | |
885 | 2356 |
617
1ca4877e42f3
some sanity checks on what is muxed, invalid timestamps in mpeg are very common and lead to strange errors in the mpeg muxer otherwise
michael
parents:
616
diff
changeset
|
2357 if(st->cur_dts && st->cur_dts != AV_NOPTS_VALUE && st->cur_dts >= pkt->dts){ |
881 | 2358 av_log(NULL, AV_LOG_ERROR, "error, non monotone timestamps %"PRId64" >= %"PRId64"\n", st->cur_dts, pkt->dts); |
617
1ca4877e42f3
some sanity checks on what is muxed, invalid timestamps in mpeg are very common and lead to strange errors in the mpeg muxer otherwise
michael
parents:
616
diff
changeset
|
2359 return -1; |
1ca4877e42f3
some sanity checks on what is muxed, invalid timestamps in mpeg are very common and lead to strange errors in the mpeg muxer otherwise
michael
parents:
616
diff
changeset
|
2360 } |
1ca4877e42f3
some sanity checks on what is muxed, invalid timestamps in mpeg are very common and lead to strange errors in the mpeg muxer otherwise
michael
parents:
616
diff
changeset
|
2361 if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts){ |
1ca4877e42f3
some sanity checks on what is muxed, invalid timestamps in mpeg are very common and lead to strange errors in the mpeg muxer otherwise
michael
parents:
616
diff
changeset
|
2362 av_log(NULL, AV_LOG_ERROR, "error, pts < dts\n"); |
1ca4877e42f3
some sanity checks on what is muxed, invalid timestamps in mpeg are very common and lead to strange errors in the mpeg muxer otherwise
michael
parents:
616
diff
changeset
|
2363 return -1; |
1ca4877e42f3
some sanity checks on what is muxed, invalid timestamps in mpeg are very common and lead to strange errors in the mpeg muxer otherwise
michael
parents:
616
diff
changeset
|
2364 } |
1ca4877e42f3
some sanity checks on what is muxed, invalid timestamps in mpeg are very common and lead to strange errors in the mpeg muxer otherwise
michael
parents:
616
diff
changeset
|
2365 |
470 | 2366 // av_log(NULL, AV_LOG_DEBUG, "av_write_frame: pts2:%lld dts2:%lld\n", pkt->pts, pkt->dts); |
468 | 2367 st->cur_dts= pkt->dts; |
2368 st->pts.val= pkt->dts; | |
2369 | |
0 | 2370 /* update pts */ |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2371 switch (st->codec->codec_type) { |
0 | 2372 case CODEC_TYPE_AUDIO: |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2373 frame_size = get_audio_frame_size(st->codec, pkt->size); |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
404
diff
changeset
|
2374 |
409 | 2375 /* HACK/FIXME, we skip the initial 0-size packets as they are most likely equal to the encoder delay, |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
404
diff
changeset
|
2376 but it would be better if we had the real timestamps from the encoder */ |
468 | 2377 if (frame_size >= 0 && (pkt->size || st->pts.num!=st->pts.den>>1 || st->pts.val)) { |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
453
diff
changeset
|
2378 av_frac_add(&st->pts, (int64_t)st->time_base.den * frame_size); |
0 | 2379 } |
2380 break; | |
2381 case CODEC_TYPE_VIDEO: | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2382 av_frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num); |
0 | 2383 break; |
2384 default: | |
2385 break; | |
2386 } | |
617
1ca4877e42f3
some sanity checks on what is muxed, invalid timestamps in mpeg are very common and lead to strange errors in the mpeg muxer otherwise
michael
parents:
616
diff
changeset
|
2387 return 0; |
470 | 2388 } |
2389 | |
2390 static void truncate_ts(AVStream *st, AVPacket *pkt){ | |
2391 int64_t pts_mask = (2LL << (st->pts_wrap_bits-1)) - 1; | |
885 | 2392 |
547 | 2393 // if(pkt->dts < 0) |
2394 // pkt->dts= 0; //this happens for low_delay=0 and b frames, FIXME, needs further invstigation about what we should do here | |
885 | 2395 |
470 | 2396 pkt->pts &= pts_mask; |
2397 pkt->dts &= pts_mask; | |
2398 } | |
2399 | |
2400 /** | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2401 * Write a packet to an output media file. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2402 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2403 * The packet shall contain one audio or video frame. |
470 | 2404 * |
2405 * @param s media file handle | |
2406 * @param pkt the packet, which contains the stream_index, buf/buf_size, dts/pts, ... | |
2407 * @return < 0 if error, = 0 if OK, 1 if end of stream wanted. | |
2408 */ | |
2409 int av_write_frame(AVFormatContext *s, AVPacket *pkt) | |
2410 { | |
554 | 2411 int ret; |
2412 | |
617
1ca4877e42f3
some sanity checks on what is muxed, invalid timestamps in mpeg are very common and lead to strange errors in the mpeg muxer otherwise
michael
parents:
616
diff
changeset
|
2413 ret=compute_pkt_fields2(s->streams[pkt->stream_index], pkt); |
1ca4877e42f3
some sanity checks on what is muxed, invalid timestamps in mpeg are very common and lead to strange errors in the mpeg muxer otherwise
michael
parents:
616
diff
changeset
|
2414 if(ret<0) |
1ca4877e42f3
some sanity checks on what is muxed, invalid timestamps in mpeg are very common and lead to strange errors in the mpeg muxer otherwise
michael
parents:
616
diff
changeset
|
2415 return ret; |
885 | 2416 |
470 | 2417 truncate_ts(s->streams[pkt->stream_index], pkt); |
2418 | |
554 | 2419 ret= s->oformat->write_packet(s, pkt); |
2420 if(!ret) | |
2421 ret= url_ferror(&s->pb); | |
2422 return ret; | |
470 | 2423 } |
2424 | |
2425 /** | |
536
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2426 * interleave_packet implementation which will interleave per DTS. |
885 | 2427 * packets with pkt->destruct == av_destruct_packet will be freed inside this function. |
804 | 2428 * so they cannot be used after it, note calling av_free_packet() on them is still safe |
470 | 2429 */ |
536
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2430 static int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush){ |
470 | 2431 AVPacketList *pktl, **next_point, *this_pktl; |
2432 int stream_count=0; | |
2433 int streams[MAX_STREAMS]; | |
536
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2434 |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2435 if(pkt){ |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2436 AVStream *st= s->streams[ pkt->stream_index]; |
470 | 2437 |
804 | 2438 // assert(pkt->destruct != av_destruct_packet); //FIXME |
470 | 2439 |
536
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2440 this_pktl = av_mallocz(sizeof(AVPacketList)); |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2441 this_pktl->pkt= *pkt; |
804 | 2442 if(pkt->destruct == av_destruct_packet) |
2443 pkt->destruct= NULL; // non shared -> must keep original from being freed | |
2444 else | |
2445 av_dup_packet(&this_pktl->pkt); //shared -> must dup | |
470 | 2446 |
536
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2447 next_point = &s->packet_buffer; |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2448 while(*next_point){ |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2449 AVStream *st2= s->streams[ (*next_point)->pkt.stream_index]; |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2450 int64_t left= st2->time_base.num * (int64_t)st ->time_base.den; |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2451 int64_t right= st ->time_base.num * (int64_t)st2->time_base.den; |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2452 if((*next_point)->pkt.dts * left > pkt->dts * right) //FIXME this can overflow |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2453 break; |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2454 next_point= &(*next_point)->next; |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2455 } |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2456 this_pktl->next= *next_point; |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2457 *next_point= this_pktl; |
470 | 2458 } |
885 | 2459 |
470 | 2460 memset(streams, 0, sizeof(streams)); |
2461 pktl= s->packet_buffer; | |
2462 while(pktl){ | |
2463 //av_log(s, AV_LOG_DEBUG, "show st:%d dts:%lld\n", pktl->pkt.stream_index, pktl->pkt.dts); | |
2464 if(streams[ pktl->pkt.stream_index ] == 0) | |
2465 stream_count++; | |
2466 streams[ pktl->pkt.stream_index ]++; | |
2467 pktl= pktl->next; | |
2468 } | |
885 | 2469 |
536
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2470 if(s->nb_streams == stream_count || (flush && stream_count)){ |
470 | 2471 pktl= s->packet_buffer; |
536
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2472 *out= pktl->pkt; |
885 | 2473 |
2474 s->packet_buffer= pktl->next; | |
536
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2475 av_freep(&pktl); |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2476 return 1; |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2477 }else{ |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2478 av_init_packet(out); |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2479 return 0; |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2480 } |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2481 } |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2482 |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2483 /** |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2484 * Interleaves a AVPacket correctly so it can be muxed. |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2485 * @param out the interleaved packet will be output here |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2486 * @param in the input packet |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2487 * @param flush 1 if no further packets are available as input and all |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2488 * remaining packets should be output |
885 | 2489 * @return 1 if a packet was output, 0 if no packet could be output, |
536
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2490 * < 0 if an error occured |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2491 */ |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2492 static int av_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush){ |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2493 if(s->oformat->interleave_packet) |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2494 return s->oformat->interleave_packet(s, out, in, flush); |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2495 else |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2496 return av_interleave_packet_per_dts(s, out, in, flush); |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2497 } |
470 | 2498 |
536
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2499 /** |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2500 * Writes a packet to an output media file ensuring correct interleaving. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2501 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2502 * The packet must contain one audio or video frame. |
536
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2503 * If the packets are already correctly interleaved the application should |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2504 * call av_write_frame() instead as its slightly faster, its also important |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2505 * to keep in mind that completly non interleaved input will need huge amounts |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2506 * of memory to interleave with this, so its prefereable to interleave at the |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2507 * demuxer level |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2508 * |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2509 * @param s media file handle |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2510 * @param pkt the packet, which contains the stream_index, buf/buf_size, dts/pts, ... |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2511 * @return < 0 if error, = 0 if OK, 1 if end of stream wanted. |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2512 */ |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2513 int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt){ |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2514 AVStream *st= s->streams[ pkt->stream_index]; |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2515 |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2516 //FIXME/XXX/HACK drop zero sized packets |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2517 if(st->codec->codec_type == CODEC_TYPE_AUDIO && pkt->size==0) |
536
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2518 return 0; |
717
a79f89d39c3c
discard dummy packets before doing inapropriate checks on them and failing as a result
michael
parents:
708
diff
changeset
|
2519 |
a79f89d39c3c
discard dummy packets before doing inapropriate checks on them and failing as a result
michael
parents:
708
diff
changeset
|
2520 //av_log(NULL, AV_LOG_DEBUG, "av_interleaved_write_frame %d %Ld %Ld\n", pkt->size, pkt->dts, pkt->pts); |
a79f89d39c3c
discard dummy packets before doing inapropriate checks on them and failing as a result
michael
parents:
708
diff
changeset
|
2521 if(compute_pkt_fields2(st, pkt) < 0) |
a79f89d39c3c
discard dummy packets before doing inapropriate checks on them and failing as a result
michael
parents:
708
diff
changeset
|
2522 return -1; |
885 | 2523 |
536
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2524 if(pkt->dts == AV_NOPTS_VALUE) |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2525 return -1; |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2526 |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2527 for(;;){ |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2528 AVPacket opkt; |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2529 int ret= av_interleave_packet(s, &opkt, pkt, 0); |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2530 if(ret<=0) //FIXME cleanup needed for ret<0 ? |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2531 return ret; |
885 | 2532 |
536
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2533 truncate_ts(s->streams[opkt.stream_index], &opkt); |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2534 ret= s->oformat->write_packet(s, &opkt); |
885 | 2535 |
536
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2536 av_free_packet(&opkt); |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2537 pkt= NULL; |
885 | 2538 |
470 | 2539 if(ret<0) |
2540 return ret; | |
554 | 2541 if(url_ferror(&s->pb)) |
2542 return url_ferror(&s->pb); | |
470 | 2543 } |
0 | 2544 } |
2545 | |
2546 /** | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2547 * @brief Write the stream trailer to an output media file and |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2548 * free the file private data. |
0 | 2549 * |
2550 * @param s media file handle | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2551 * @return 0 if OK. AVERROR_xxx if error. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2552 */ |
0 | 2553 int av_write_trailer(AVFormatContext *s) |
2554 { | |
540
26a477a5ebda
move free() of AVStream priv data to av_write_trailer()
michael
parents:
536
diff
changeset
|
2555 int ret, i; |
885 | 2556 |
536
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2557 for(;;){ |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2558 AVPacket pkt; |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2559 ret= av_interleave_packet(s, &pkt, NULL, 1); |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2560 if(ret<0) //FIXME cleanup needed for ret<0 ? |
540
26a477a5ebda
move free() of AVStream priv data to av_write_trailer()
michael
parents:
536
diff
changeset
|
2561 goto fail; |
536
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2562 if(!ret) |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2563 break; |
885 | 2564 |
536
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2565 truncate_ts(s->streams[pkt.stream_index], &pkt); |
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2566 ret= s->oformat->write_packet(s, &pkt); |
885 | 2567 |
536
76c47c58064f
move packet interleaving function into AVOutputFormat, so it can be overriden easily instead of doing reordering twice if the muxer needs some other interleaving then dts based
michael
parents:
535
diff
changeset
|
2568 av_free_packet(&pkt); |
885 | 2569 |
470 | 2570 if(ret<0) |
540
26a477a5ebda
move free() of AVStream priv data to av_write_trailer()
michael
parents:
536
diff
changeset
|
2571 goto fail; |
554 | 2572 if(url_ferror(&s->pb)) |
2573 goto fail; | |
470 | 2574 } |
2575 | |
869 | 2576 if(s->oformat->write_trailer) |
2577 ret = s->oformat->write_trailer(s); | |
540
26a477a5ebda
move free() of AVStream priv data to av_write_trailer()
michael
parents:
536
diff
changeset
|
2578 fail: |
554 | 2579 if(ret == 0) |
2580 ret=url_ferror(&s->pb); | |
540
26a477a5ebda
move free() of AVStream priv data to av_write_trailer()
michael
parents:
536
diff
changeset
|
2581 for(i=0;i<s->nb_streams;i++) |
26a477a5ebda
move free() of AVStream priv data to av_write_trailer()
michael
parents:
536
diff
changeset
|
2582 av_freep(&s->streams[i]->priv_data); |
0 | 2583 av_freep(&s->priv_data); |
2584 return ret; | |
2585 } | |
2586 | |
2587 /* "user interface" functions */ | |
2588 | |
2589 void dump_format(AVFormatContext *ic, | |
885 | 2590 int index, |
0 | 2591 const char *url, |
2592 int is_output) | |
2593 { | |
2594 int i, flags; | |
2595 char buf[256]; | |
2596 | |
885 | 2597 av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n", |
0 | 2598 is_output ? "Output" : "Input", |
885 | 2599 index, |
2600 is_output ? ic->oformat->name : ic->iformat->name, | |
0 | 2601 is_output ? "to" : "from", url); |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
2602 if (!is_output) { |
776 | 2603 av_log(NULL, AV_LOG_INFO, " Duration: "); |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
2604 if (ic->duration != AV_NOPTS_VALUE) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
2605 int hours, mins, secs, us; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
2606 secs = ic->duration / AV_TIME_BASE; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
2607 us = ic->duration % AV_TIME_BASE; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
2608 mins = secs / 60; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
2609 secs %= 60; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
2610 hours = mins / 60; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
2611 mins %= 60; |
885 | 2612 av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%01d", hours, mins, secs, |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
2613 (10 * us) / AV_TIME_BASE); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
2614 } else { |
776 | 2615 av_log(NULL, AV_LOG_INFO, "N/A"); |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
2616 } |
556
cb5f220888c0
print start_time patch by (Wolfram Gloger <wmglo at dent dot med dot uni-muenchen dot de>)
michael
parents:
555
diff
changeset
|
2617 if (ic->start_time != AV_NOPTS_VALUE) { |
cb5f220888c0
print start_time patch by (Wolfram Gloger <wmglo at dent dot med dot uni-muenchen dot de>)
michael
parents:
555
diff
changeset
|
2618 int secs, us; |
776 | 2619 av_log(NULL, AV_LOG_INFO, ", start: "); |
556
cb5f220888c0
print start_time patch by (Wolfram Gloger <wmglo at dent dot med dot uni-muenchen dot de>)
michael
parents:
555
diff
changeset
|
2620 secs = ic->start_time / AV_TIME_BASE; |
cb5f220888c0
print start_time patch by (Wolfram Gloger <wmglo at dent dot med dot uni-muenchen dot de>)
michael
parents:
555
diff
changeset
|
2621 us = ic->start_time % AV_TIME_BASE; |
776 | 2622 av_log(NULL, AV_LOG_INFO, "%d.%06d", |
556
cb5f220888c0
print start_time patch by (Wolfram Gloger <wmglo at dent dot med dot uni-muenchen dot de>)
michael
parents:
555
diff
changeset
|
2623 secs, (int)av_rescale(us, 1000000, AV_TIME_BASE)); |
cb5f220888c0
print start_time patch by (Wolfram Gloger <wmglo at dent dot med dot uni-muenchen dot de>)
michael
parents:
555
diff
changeset
|
2624 } |
776 | 2625 av_log(NULL, AV_LOG_INFO, ", bitrate: "); |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
2626 if (ic->bit_rate) { |
776 | 2627 av_log(NULL, AV_LOG_INFO,"%d kb/s", ic->bit_rate / 1000); |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
2628 } else { |
776 | 2629 av_log(NULL, AV_LOG_INFO, "N/A"); |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
2630 } |
776 | 2631 av_log(NULL, AV_LOG_INFO, "\n"); |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
2632 } |
0 | 2633 for(i=0;i<ic->nb_streams;i++) { |
2634 AVStream *st = ic->streams[i]; | |
855
379c4e948363
print more time_base fps stuff if av_log level is at debug or above
michael
parents:
851
diff
changeset
|
2635 int g= ff_gcd(st->time_base.num, st->time_base.den); |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2636 avcodec_string(buf, sizeof(buf), st->codec, is_output); |
776 | 2637 av_log(NULL, AV_LOG_INFO, " Stream #%d.%d", index, i); |
0 | 2638 /* the pid is an important information, so we display it */ |
2639 /* XXX: add a generic system */ | |
2640 if (is_output) | |
2641 flags = ic->oformat->flags; | |
2642 else | |
2643 flags = ic->iformat->flags; | |
2644 if (flags & AVFMT_SHOW_IDS) { | |
776 | 2645 av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id); |
0 | 2646 } |
819
a6c035e7f429
DVB subtitle decoder by (Ian Caulfield: imc25, cam ac uk)
michael
parents:
810
diff
changeset
|
2647 if (strlen(st->language) > 0) { |
a6c035e7f429
DVB subtitle decoder by (Ian Caulfield: imc25, cam ac uk)
michael
parents:
810
diff
changeset
|
2648 av_log(NULL, AV_LOG_INFO, "(%s)", st->language); |
a6c035e7f429
DVB subtitle decoder by (Ian Caulfield: imc25, cam ac uk)
michael
parents:
810
diff
changeset
|
2649 } |
855
379c4e948363
print more time_base fps stuff if av_log level is at debug or above
michael
parents:
851
diff
changeset
|
2650 av_log(NULL, AV_LOG_DEBUG, ", %d/%d", st->time_base.num/g, st->time_base.den/g); |
1119 | 2651 av_log(NULL, AV_LOG_INFO, ": %s", buf); |
855
379c4e948363
print more time_base fps stuff if av_log level is at debug or above
michael
parents:
851
diff
changeset
|
2652 if(st->codec->codec_type == CODEC_TYPE_VIDEO){ |
882 | 2653 if(st->r_frame_rate.den && st->r_frame_rate.num) |
2654 av_log(NULL, AV_LOG_INFO, ", %5.2f fps(r)", av_q2d(st->r_frame_rate)); | |
2655 /* else if(st->time_base.den && st->time_base.num) | |
2656 av_log(NULL, AV_LOG_INFO, ", %5.2f fps(m)", 1/av_q2d(st->time_base));*/ | |
2657 else | |
2658 av_log(NULL, AV_LOG_INFO, ", %5.2f fps(c)", 1/av_q2d(st->codec->time_base)); | |
855
379c4e948363
print more time_base fps stuff if av_log level is at debug or above
michael
parents:
851
diff
changeset
|
2659 } |
1119 | 2660 av_log(NULL, AV_LOG_INFO, "\n"); |
0 | 2661 } |
2662 } | |
2663 | |
2664 typedef struct { | |
168 | 2665 const char *abv; |
0 | 2666 int width, height; |
168 | 2667 int frame_rate, frame_rate_base; |
2668 } AbvEntry; | |
0 | 2669 |
168 | 2670 static AbvEntry frame_abvs[] = { |
204 | 2671 { "ntsc", 720, 480, 30000, 1001 }, |
2672 { "pal", 720, 576, 25, 1 }, | |
2673 { "qntsc", 352, 240, 30000, 1001 }, /* VCD compliant ntsc */ | |
2674 { "qpal", 352, 288, 25, 1 }, /* VCD compliant pal */ | |
205 | 2675 { "sntsc", 640, 480, 30000, 1001 }, /* square pixel ntsc */ |
2676 { "spal", 768, 576, 25, 1 }, /* square pixel pal */ | |
168 | 2677 { "film", 352, 240, 24, 1 }, |
2678 { "ntsc-film", 352, 240, 24000, 1001 }, | |
2679 { "sqcif", 128, 96, 0, 0 }, | |
2680 { "qcif", 176, 144, 0, 0 }, | |
2681 { "cif", 352, 288, 0, 0 }, | |
2682 { "4cif", 704, 576, 0, 0 }, | |
0 | 2683 }; |
168 | 2684 |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2685 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2686 * parses width and height out of string str. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2687 */ |
0 | 2688 int parse_image_size(int *width_ptr, int *height_ptr, const char *str) |
2689 { | |
2690 int i; | |
168 | 2691 int n = sizeof(frame_abvs) / sizeof(AbvEntry); |
0 | 2692 const char *p; |
2693 int frame_width = 0, frame_height = 0; | |
2694 | |
2695 for(i=0;i<n;i++) { | |
168 | 2696 if (!strcmp(frame_abvs[i].abv, str)) { |
2697 frame_width = frame_abvs[i].width; | |
2698 frame_height = frame_abvs[i].height; | |
0 | 2699 break; |
2700 } | |
2701 } | |
2702 if (i == n) { | |
2703 p = str; | |
2704 frame_width = strtol(p, (char **)&p, 10); | |
2705 if (*p) | |
2706 p++; | |
2707 frame_height = strtol(p, (char **)&p, 10); | |
2708 } | |
2709 if (frame_width <= 0 || frame_height <= 0) | |
2710 return -1; | |
2711 *width_ptr = frame_width; | |
2712 *height_ptr = frame_height; | |
2713 return 0; | |
2714 } | |
2715 | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2716 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2717 * Converts frame rate from string to a fraction. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2718 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2719 * First we try to get an exact integer or fractional frame rate. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2720 * If this fails we convert the frame rate to a double and return |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2721 * an approximate fraction using the DEFAULT_FRAME_RATE_BASE. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2722 */ |
168 | 2723 int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg) |
2724 { | |
2725 int i; | |
2726 char* cp; | |
885 | 2727 |
168 | 2728 /* First, we check our abbreviation table */ |
2729 for (i = 0; i < sizeof(frame_abvs)/sizeof(*frame_abvs); ++i) | |
2730 if (!strcmp(frame_abvs[i].abv, arg)) { | |
887 | 2731 *frame_rate = frame_abvs[i].frame_rate; |
2732 *frame_rate_base = frame_abvs[i].frame_rate_base; | |
2733 return 0; | |
2734 } | |
168 | 2735 |
2736 /* Then, we try to parse it as fraction */ | |
2737 cp = strchr(arg, '/'); | |
662
549e594a13c2
support colon-separated rates patch by Roine Gustafsson <roine AT users DOT sourceforge DOT net>
mmu_man
parents:
652
diff
changeset
|
2738 if (!cp) |
549e594a13c2
support colon-separated rates patch by Roine Gustafsson <roine AT users DOT sourceforge DOT net>
mmu_man
parents:
652
diff
changeset
|
2739 cp = strchr(arg, ':'); |
168 | 2740 if (cp) { |
2741 char* cpp; | |
887 | 2742 *frame_rate = strtol(arg, &cpp, 10); |
2743 if (cpp != arg || cpp == cp) | |
2744 *frame_rate_base = strtol(cp+1, &cpp, 10); | |
2745 else | |
2746 *frame_rate = 0; | |
885 | 2747 } |
168 | 2748 else { |
2749 /* Finally we give up and parse it as double */ | |
1030 | 2750 AVRational time_base = av_d2q(strtod(arg, 0), DEFAULT_FRAME_RATE_BASE); |
2751 *frame_rate_base = time_base.den; | |
2752 *frame_rate = time_base.num; | |
168 | 2753 } |
2754 if (!*frame_rate || !*frame_rate_base) | |
2755 return -1; | |
2756 else | |
2757 return 0; | |
2758 } | |
2759 | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2760 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2761 * Converts date string to number of seconds since Jan 1st, 1970. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2762 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2763 * @code |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2764 * Syntax: |
0 | 2765 * - If not a duration: |
2766 * [{YYYY-MM-DD|YYYYMMDD}]{T| }{HH[:MM[:SS[.m...]]][Z]|HH[MM[SS[.m...]]][Z]} | |
2767 * Time is localtime unless Z is suffixed to the end. In this case GMT | |
885 | 2768 * Return the date in micro seconds since 1970 |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2769 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2770 * - If a duration: |
0 | 2771 * HH[:MM[:SS[.m...]]] |
2772 * S+[.m...] | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2773 * @endcode |
0 | 2774 */ |
907 | 2775 #ifndef CONFIG_WINCE |
65 | 2776 int64_t parse_date(const char *datestr, int duration) |
0 | 2777 { |
2778 const char *p; | |
65 | 2779 int64_t t; |
0 | 2780 struct tm dt; |
2781 int i; | |
2782 static const char *date_fmt[] = { | |
2783 "%Y-%m-%d", | |
2784 "%Y%m%d", | |
2785 }; | |
2786 static const char *time_fmt[] = { | |
2787 "%H:%M:%S", | |
2788 "%H%M%S", | |
2789 }; | |
2790 const char *q; | |
2791 int is_utc, len; | |
2792 char lastch; | |
477 | 2793 int negative = 0; |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
404
diff
changeset
|
2794 |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
404
diff
changeset
|
2795 #undef time |
0 | 2796 time_t now = time(0); |
2797 | |
2798 len = strlen(datestr); | |
2799 if (len > 0) | |
2800 lastch = datestr[len - 1]; | |
2801 else | |
2802 lastch = '\0'; | |
2803 is_utc = (lastch == 'z' || lastch == 'Z'); | |
2804 | |
2805 memset(&dt, 0, sizeof(dt)); | |
2806 | |
2807 p = datestr; | |
2808 q = NULL; | |
2809 if (!duration) { | |
2810 for (i = 0; i < sizeof(date_fmt) / sizeof(date_fmt[0]); i++) { | |
230
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
229
diff
changeset
|
2811 q = small_strptime(p, date_fmt[i], &dt); |
0 | 2812 if (q) { |
2813 break; | |
2814 } | |
2815 } | |
2816 | |
2817 if (!q) { | |
2818 if (is_utc) { | |
2819 dt = *gmtime(&now); | |
2820 } else { | |
2821 dt = *localtime(&now); | |
2822 } | |
2823 dt.tm_hour = dt.tm_min = dt.tm_sec = 0; | |
2824 } else { | |
2825 p = q; | |
2826 } | |
2827 | |
2828 if (*p == 'T' || *p == 't' || *p == ' ') | |
2829 p++; | |
2830 | |
2831 for (i = 0; i < sizeof(time_fmt) / sizeof(time_fmt[0]); i++) { | |
230
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
229
diff
changeset
|
2832 q = small_strptime(p, time_fmt[i], &dt); |
0 | 2833 if (q) { |
2834 break; | |
2835 } | |
2836 } | |
2837 } else { | |
887 | 2838 if (p[0] == '-') { |
2839 negative = 1; | |
2840 ++p; | |
2841 } | |
230
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
229
diff
changeset
|
2842 q = small_strptime(p, time_fmt[0], &dt); |
0 | 2843 if (!q) { |
2844 dt.tm_sec = strtol(p, (char **)&q, 10); | |
2845 dt.tm_min = 0; | |
2846 dt.tm_hour = 0; | |
2847 } | |
2848 } | |
2849 | |
2850 /* Now we have all the fields that we can get */ | |
2851 if (!q) { | |
2852 if (duration) | |
2853 return 0; | |
2854 else | |
65 | 2855 return now * int64_t_C(1000000); |
0 | 2856 } |
2857 | |
2858 if (duration) { | |
2859 t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec; | |
2860 } else { | |
2861 dt.tm_isdst = -1; /* unknown */ | |
2862 if (is_utc) { | |
2863 t = mktimegm(&dt); | |
2864 } else { | |
2865 t = mktime(&dt); | |
2866 } | |
2867 } | |
2868 | |
2869 t *= 1000000; | |
2870 | |
2871 if (*q == '.') { | |
2872 int val, n; | |
2873 q++; | |
2874 for (val = 0, n = 100000; n >= 1; n /= 10, q++) { | |
885 | 2875 if (!isdigit(*q)) |
0 | 2876 break; |
2877 val += n * (*q - '0'); | |
2878 } | |
2879 t += val; | |
2880 } | |
477 | 2881 return negative ? -t : t; |
0 | 2882 } |
907 | 2883 #endif /* CONFIG_WINCE */ |
0 | 2884 |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2885 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2886 * Attempts to find a specific tag in a URL. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2887 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2888 * syntax: '?tag1=val1&tag2=val2...'. Little URL decoding is done. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2889 * Return 1 if found. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2890 */ |
0 | 2891 int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info) |
2892 { | |
2893 const char *p; | |
2894 char tag[128], *q; | |
2895 | |
2896 p = info; | |
2897 if (*p == '?') | |
2898 p++; | |
2899 for(;;) { | |
2900 q = tag; | |
2901 while (*p != '\0' && *p != '=' && *p != '&') { | |
2902 if ((q - tag) < sizeof(tag) - 1) | |
2903 *q++ = *p; | |
2904 p++; | |
2905 } | |
2906 *q = '\0'; | |
2907 q = arg; | |
2908 if (*p == '=') { | |
2909 p++; | |
2910 while (*p != '&' && *p != '\0') { | |
2911 if ((q - arg) < arg_size - 1) { | |
2912 if (*p == '+') | |
2913 *q++ = ' '; | |
2914 else | |
2915 *q++ = *p; | |
2916 } | |
2917 p++; | |
2918 } | |
2919 *q = '\0'; | |
2920 } | |
885 | 2921 if (!strcmp(tag, tag1)) |
0 | 2922 return 1; |
2923 if (*p != '&') | |
2924 break; | |
2925 p++; | |
2926 } | |
2927 return 0; | |
2928 } | |
2929 | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2930 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2931 * Returns in 'buf' the path with '%d' replaced by number. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2932 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2933 * Also handles the '%0nd' format where 'n' is the total number |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2934 * of digits and '%%'. Return 0 if OK, and -1 if format error. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2935 */ |
0 | 2936 int get_frame_filename(char *buf, int buf_size, |
2937 const char *path, int number) | |
2938 { | |
2939 const char *p; | |
290
7a3ed84008ec
GCC 3.3.2 warnings patch by (Panagiotis Issaris <takis at lumumba dot luc dot ac dot be>)
michael
parents:
230
diff
changeset
|
2940 char *q, buf1[20], c; |
7a3ed84008ec
GCC 3.3.2 warnings patch by (Panagiotis Issaris <takis at lumumba dot luc dot ac dot be>)
michael
parents:
230
diff
changeset
|
2941 int nd, len, percentd_found; |
0 | 2942 |
2943 q = buf; | |
2944 p = path; | |
2945 percentd_found = 0; | |
2946 for(;;) { | |
2947 c = *p++; | |
2948 if (c == '\0') | |
2949 break; | |
2950 if (c == '%') { | |
9
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2951 do { |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2952 nd = 0; |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2953 while (isdigit(*p)) { |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2954 nd = nd * 10 + *p++ - '0'; |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2955 } |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2956 c = *p++; |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2957 } while (isdigit(c)); |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2958 |
0 | 2959 switch(c) { |
2960 case '%': | |
2961 goto addchar; | |
2962 case 'd': | |
2963 if (percentd_found) | |
2964 goto fail; | |
2965 percentd_found = 1; | |
2966 snprintf(buf1, sizeof(buf1), "%0*d", nd, number); | |
2967 len = strlen(buf1); | |
2968 if ((q - buf + len) > buf_size - 1) | |
2969 goto fail; | |
2970 memcpy(q, buf1, len); | |
2971 q += len; | |
2972 break; | |
2973 default: | |
2974 goto fail; | |
2975 } | |
2976 } else { | |
2977 addchar: | |
2978 if ((q - buf) < buf_size - 1) | |
2979 *q++ = c; | |
2980 } | |
2981 } | |
2982 if (!percentd_found) | |
2983 goto fail; | |
2984 *q = '\0'; | |
2985 return 0; | |
2986 fail: | |
2987 *q = '\0'; | |
2988 return -1; | |
2989 } | |
2990 | |
2991 /** | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2992 * Print nice hexa dump of a buffer |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2993 * @param f stream for output |
0 | 2994 * @param buf buffer |
2995 * @param size buffer size | |
2996 */ | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2997 void av_hex_dump(FILE *f, uint8_t *buf, int size) |
0 | 2998 { |
2999 int len, i, j, c; | |
3000 | |
3001 for(i=0;i<size;i+=16) { | |
3002 len = size - i; | |
3003 if (len > 16) | |
3004 len = 16; | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3005 fprintf(f, "%08x ", i); |
0 | 3006 for(j=0;j<16;j++) { |
3007 if (j < len) | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3008 fprintf(f, " %02x", buf[i+j]); |
0 | 3009 else |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3010 fprintf(f, " "); |
0 | 3011 } |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3012 fprintf(f, " "); |
0 | 3013 for(j=0;j<len;j++) { |
3014 c = buf[i+j]; | |
3015 if (c < ' ' || c > '~') | |
3016 c = '.'; | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3017 fprintf(f, "%c", c); |
0 | 3018 } |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3019 fprintf(f, "\n"); |
0 | 3020 } |
3021 } | |
3022 | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3023 /** |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3024 * Print on 'f' a nice dump of a packet |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3025 * @param f stream for output |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3026 * @param pkt packet to dump |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3027 * @param dump_payload true if the payload must be displayed too |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3028 */ |
743 | 3029 //FIXME needs to know the time_base |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3030 void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3031 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3032 fprintf(f, "stream #%d:\n", pkt->stream_index); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3033 fprintf(f, " keyframe=%d\n", ((pkt->flags & PKT_FLAG_KEY) != 0)); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3034 fprintf(f, " duration=%0.3f\n", (double)pkt->duration / AV_TIME_BASE); |
333 | 3035 /* DTS is _always_ valid after av_read_frame() */ |
3036 fprintf(f, " dts="); | |
3037 if (pkt->dts == AV_NOPTS_VALUE) | |
3038 fprintf(f, "N/A"); | |
3039 else | |
3040 fprintf(f, "%0.3f", (double)pkt->dts / AV_TIME_BASE); | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3041 /* PTS may be not known if B frames are present */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3042 fprintf(f, " pts="); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3043 if (pkt->pts == AV_NOPTS_VALUE) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3044 fprintf(f, "N/A"); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3045 else |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3046 fprintf(f, "%0.3f", (double)pkt->pts / AV_TIME_BASE); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3047 fprintf(f, "\n"); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3048 fprintf(f, " size=%d\n", pkt->size); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3049 if (dump_payload) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3050 av_hex_dump(f, pkt->data, pkt->size); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3051 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3052 |
0 | 3053 void url_split(char *proto, int proto_size, |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3054 char *authorization, int authorization_size, |
0 | 3055 char *hostname, int hostname_size, |
3056 int *port_ptr, | |
3057 char *path, int path_size, | |
3058 const char *url) | |
3059 { | |
3060 const char *p; | |
3061 char *q; | |
3062 int port; | |
3063 | |
3064 port = -1; | |
3065 | |
3066 p = url; | |
3067 q = proto; | |
3068 while (*p != ':' && *p != '\0') { | |
3069 if ((q - proto) < proto_size - 1) | |
3070 *q++ = *p; | |
3071 p++; | |
3072 } | |
3073 if (proto_size > 0) | |
3074 *q = '\0'; | |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3075 if (authorization_size > 0) |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3076 authorization[0] = '\0'; |
0 | 3077 if (*p == '\0') { |
3078 if (proto_size > 0) | |
3079 proto[0] = '\0'; | |
3080 if (hostname_size > 0) | |
3081 hostname[0] = '\0'; | |
3082 p = url; | |
3083 } else { | |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3084 char *at,*slash; // PETR: position of '@' character and '/' character |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3085 |
0 | 3086 p++; |
3087 if (*p == '/') | |
3088 p++; | |
3089 if (*p == '/') | |
3090 p++; | |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3091 at = strchr(p,'@'); // PETR: get the position of '@' |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3092 slash = strchr(p,'/'); // PETR: get position of '/' - end of hostname |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3093 if (at && slash && at > slash) at = NULL; // PETR: not interested in '@' behind '/' |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3094 |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3095 q = at ? authorization : hostname; // PETR: if '@' exists starting with auth. |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3096 |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3097 while ((at || *p != ':') && *p != '/' && *p != '?' && *p != '\0') { // PETR: |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3098 if (*p == '@') { // PETR: passed '@' |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3099 if (authorization_size > 0) |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3100 *q = '\0'; |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3101 q = hostname; |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3102 at = NULL; |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3103 } else if (!at) { // PETR: hostname |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3104 if ((q - hostname) < hostname_size - 1) |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3105 *q++ = *p; |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3106 } else { |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3107 if ((q - authorization) < authorization_size - 1) |
0 | 3108 *q++ = *p; |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3109 } |
0 | 3110 p++; |
3111 } | |
3112 if (hostname_size > 0) | |
3113 *q = '\0'; | |
3114 if (*p == ':') { | |
3115 p++; | |
3116 port = strtoul(p, (char **)&p, 10); | |
3117 } | |
3118 } | |
3119 if (port_ptr) | |
3120 *port_ptr = port; | |
3121 pstrcpy(path, path_size, p); | |
3122 } | |
3123 | |
3124 /** | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3125 * Set the pts for a given stream. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3126 * |
885 | 3127 * @param s stream |
0 | 3128 * @param pts_wrap_bits number of bits effectively used by the pts |
885 | 3129 * (used for wrap control, 33 is the value for MPEG) |
3130 * @param pts_num numerator to convert to seconds (MPEG: 1) | |
0 | 3131 * @param pts_den denominator to convert to seconds (MPEG: 90000) |
3132 */ | |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
453
diff
changeset
|
3133 void av_set_pts_info(AVStream *s, int pts_wrap_bits, |
0 | 3134 int pts_num, int pts_den) |
3135 { | |
3136 s->pts_wrap_bits = pts_wrap_bits; | |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
453
diff
changeset
|
3137 s->time_base.num = pts_num; |
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
453
diff
changeset
|
3138 s->time_base.den = pts_den; |
0 | 3139 } |
3140 | |
3141 /* fraction handling */ | |
3142 | |
3143 /** | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3144 * f = val + (num / den) + 0.5. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3145 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3146 * 'num' is normalized so that it is such as 0 <= num < den. |
0 | 3147 * |
3148 * @param f fractional number | |
3149 * @param val integer value | |
3150 * @param num must be >= 0 | |
885 | 3151 * @param den must be >= 1 |
0 | 3152 */ |
1135
107f90e1551b
cleanup patch from Diego Petten <flameeyes@gentoo.org>
lu_zero
parents:
1127
diff
changeset
|
3153 static void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den) |
0 | 3154 { |
3155 num += (den >> 1); | |
3156 if (num >= den) { | |
3157 val += num / den; | |
3158 num = num % den; | |
3159 } | |
3160 f->val = val; | |
3161 f->num = num; | |
3162 f->den = den; | |
3163 } | |
3164 | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3165 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3166 * Set f to (val + 0.5). |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3167 */ |
1135
107f90e1551b
cleanup patch from Diego Petten <flameeyes@gentoo.org>
lu_zero
parents:
1127
diff
changeset
|
3168 static void av_frac_set(AVFrac *f, int64_t val) |
0 | 3169 { |
3170 f->val = val; | |
3171 f->num = f->den >> 1; | |
3172 } | |
3173 | |
3174 /** | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3175 * Fractionnal addition to f: f = f + (incr / f->den). |
0 | 3176 * |
3177 * @param f fractional number | |
3178 * @param incr increment, can be positive or negative | |
3179 */ | |
1135
107f90e1551b
cleanup patch from Diego Petten <flameeyes@gentoo.org>
lu_zero
parents:
1127
diff
changeset
|
3180 static void av_frac_add(AVFrac *f, int64_t incr) |
0 | 3181 { |
65 | 3182 int64_t num, den; |
0 | 3183 |
3184 num = f->num + incr; | |
3185 den = f->den; | |
3186 if (num < 0) { | |
3187 f->val += num / den; | |
3188 num = num % den; | |
3189 if (num < 0) { | |
3190 num += den; | |
3191 f->val--; | |
3192 } | |
3193 } else if (num >= den) { | |
3194 f->val += num / den; | |
3195 num = num % den; | |
3196 } | |
3197 f->num = num; | |
3198 } | |
20 | 3199 |
3200 /** | |
3201 * register a new image format | |
3202 * @param img_fmt Image format descriptor | |
3203 */ | |
3204 void av_register_image_format(AVImageFormat *img_fmt) | |
3205 { | |
3206 AVImageFormat **p; | |
3207 | |
3208 p = &first_image_format; | |
3209 while (*p != NULL) p = &(*p)->next; | |
3210 *p = img_fmt; | |
3211 img_fmt->next = NULL; | |
3212 } | |
3213 | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3214 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3215 * Guesses image format based on data in the image. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3216 */ |
20 | 3217 AVImageFormat *av_probe_image_format(AVProbeData *pd) |
3218 { | |
3219 AVImageFormat *fmt1, *fmt; | |
3220 int score, score_max; | |
3221 | |
3222 fmt = NULL; | |
3223 score_max = 0; | |
3224 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) { | |
3225 if (fmt1->img_probe) { | |
3226 score = fmt1->img_probe(pd); | |
3227 if (score > score_max) { | |
3228 score_max = score; | |
3229 fmt = fmt1; | |
3230 } | |
3231 } | |
3232 } | |
3233 return fmt; | |
3234 } | |
3235 | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3236 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3237 * Guesses image format based on file name extensions. |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3238 */ |
20 | 3239 AVImageFormat *guess_image_format(const char *filename) |
3240 { | |
3241 AVImageFormat *fmt1; | |
3242 | |
3243 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) { | |
3244 if (fmt1->extensions && match_ext(filename, fmt1->extensions)) | |
3245 return fmt1; | |
3246 } | |
3247 return NULL; | |
3248 } | |
3249 | |
3250 /** | |
885 | 3251 * Read an image from a stream. |
20 | 3252 * @param gb byte stream containing the image |
3253 * @param fmt image format, NULL if probing is required | |
3254 */ | |
3255 int av_read_image(ByteIOContext *pb, const char *filename, | |
3256 AVImageFormat *fmt, | |
3257 int (*alloc_cb)(void *, AVImageInfo *info), void *opaque) | |
3258 { | |
922
7f20b3ce1100
dynamically increase probe buffer until format is detected
michael
parents:
915
diff
changeset
|
3259 uint8_t buf[PROBE_BUF_MIN]; |
20 | 3260 AVProbeData probe_data, *pd = &probe_data; |
3261 offset_t pos; | |
3262 int ret; | |
3263 | |
3264 if (!fmt) { | |
64 | 3265 pd->filename = filename; |
20 | 3266 pd->buf = buf; |
3267 pos = url_ftell(pb); | |
922
7f20b3ce1100
dynamically increase probe buffer until format is detected
michael
parents:
915
diff
changeset
|
3268 pd->buf_size = get_buffer(pb, buf, PROBE_BUF_MIN); |
20 | 3269 url_fseek(pb, pos, SEEK_SET); |
3270 fmt = av_probe_image_format(pd); | |
3271 } | |
3272 if (!fmt) | |
3273 return AVERROR_NOFMT; | |
3274 ret = fmt->img_read(pb, alloc_cb, opaque); | |
3275 return ret; | |
3276 } | |
3277 | |
3278 /** | |
3279 * Write an image to a stream. | |
3280 * @param pb byte stream for the image output | |
3281 * @param fmt image format | |
3282 * @param img image data and informations | |
3283 */ | |
3284 int av_write_image(ByteIOContext *pb, AVImageFormat *fmt, AVImageInfo *img) | |
3285 { | |
3286 return fmt->img_write(pb, img); | |
3287 } | |
3288 |