Mercurial > libavformat.hg
annotate utils.c @ 1141:ef9bf68071d3 libavformat
extradata may be used as bitstream reader input so allocate with padding
author | mru |
---|---|
date | Thu, 29 Jun 2006 19:42:34 +0000 |
parents | ff241582890f |
children | e3a585883bbd |
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; |
1141
ef9bf68071d3
extradata may be used as bitstream reader input so allocate with padding
mru
parents:
1139
diff
changeset
|
1998 st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_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
|
1999 memcpy(st->codec->extradata, pkt->data, st->codec->extradata_size); |
1141
ef9bf68071d3
extradata may be used as bitstream reader input so allocate with padding
mru
parents:
1139
diff
changeset
|
2000 memset(st->codec->extradata + i, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
797
f5a231a9a2f1
support changing in bitstream global headers into extradata style and back
michael
parents:
786
diff
changeset
|
2001 } |
f5a231a9a2f1
support changing in bitstream global headers into extradata style and back
michael
parents:
786
diff
changeset
|
2002 } |
885 | 2003 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2004 /* 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
|
2005 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
|
2006 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
|
2007 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
|
2008 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
|
2009 (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
|
2010 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
|
2011 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
|
2012 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
|
2013 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
|
2014 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
|
2015 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
|
2016 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
|
2017 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
|
2018 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
|
2019 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
|
2020 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
|
2021 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
|
2022 (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
|
2023 try_decode_frame(st, pkt->data, pkt->size); |
885 | 2024 |
772 | 2025 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
|
2026 break; |
0 | 2027 } |
2028 count++; | |
2029 } | |
2030 | |
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
|
2031 // 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
|
2032 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
|
2033 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
|
2034 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
|
2035 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
|
2036 } |
0 | 2037 for(i=0;i<ic->nb_streams;i++) { |
2038 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
|
2039 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
|
2040 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
|
2041 st->codec->codec_tag= avcodec_pix_fmt_to_codec_tag(st->codec->pix_fmt); |
620 | 2042 |
927 | 2043 if(duration_count[i] && st->codec->time_base.num*101LL <= st->codec->time_base.den && |
2044 st->time_base.num*duration_sum[i]/duration_count[i]*101LL > st->time_base.den){ | |
2045 int64_t num, den, error, best_error; | |
625 | 2046 |
750 | 2047 num= st->time_base.den*duration_count[i]; |
2048 den= st->time_base.num*duration_sum[i]; | |
885 | 2049 |
927 | 2050 best_error= INT64_MAX; |
2051 for(j=1; j<60*12; j++){ | |
2052 error= ABS(1001*12*num - 1001*j*den); | |
2053 if(error < best_error){ | |
2054 best_error= error; | |
2055 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, j, 12, INT_MAX); | |
2056 } | |
2057 } | |
2058 for(j=24; j<=30; j+=6){ | |
2059 error= ABS(1001*12*num - 1000*12*j*den); | |
2060 if(error < best_error){ | |
2061 best_error= error; | |
2062 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, j*1000, 1001, INT_MAX); | |
2063 } | |
750 | 2064 } |
620 | 2065 } |
2066 | |
578 | 2067 /* set real frame rate info */ |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2068 /* 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
|
2069 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
|
2070 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
|
2071 st->codec->sub_id == 2) { |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2072 if (st->codec_info_nb_frames >= 20) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2073 float coded_frame_rate, est_frame_rate; |
885 | 2074 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
|
2075 (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
|
2076 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
|
2077 #if 0 |
885 | 2078 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
|
2079 coded_frame_rate, est_frame_rate); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2080 #endif |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2081 /* 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
|
2082 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
|
2083 higher level as it can change in a film */ |
885 | 2084 if (coded_frame_rate >= 24.97 && |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2085 (est_frame_rate >= 23.5 && est_frame_rate < 24.5)) { |
743 | 2086 st->r_frame_rate = (AVRational){24000, 1001}; |
303
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 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2090 /* if no real frame rate, use the codec one */ |
743 | 2091 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
|
2092 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
|
2093 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
|
2094 } |
0 | 2095 } |
2096 } | |
2097 | |
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
|
2098 av_estimate_timings(ic); |
468 | 2099 #if 0 |
2100 /* correct DTS for b frame streams with no timestamps */ | |
2101 for(i=0;i<ic->nb_streams;i++) { | |
2102 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
|
2103 if (st->codec->codec_type == CODEC_TYPE_VIDEO) { |
468 | 2104 if(b-frames){ |
2105 ppktl = &ic->packet_buffer; | |
2106 while(ppkt1){ | |
2107 if(ppkt1->stream_index != i) | |
2108 continue; | |
2109 if(ppkt1->pkt->dts < 0) | |
2110 break; | |
2111 if(ppkt1->pkt->pts != AV_NOPTS_VALUE) | |
2112 break; | |
2113 ppkt1->pkt->dts -= delta; | |
2114 ppkt1= ppkt1->next; | |
2115 } | |
2116 if(ppkt1) | |
2117 continue; | |
2118 st->cur_dts -= delta; | |
2119 } | |
2120 } | |
2121 } | |
2122 #endif | |
0 | 2123 return ret; |
2124 } | |
2125 | |
303
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 /** |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2129 * start playing a network based stream (e.g. RTSP stream) at the |
885 | 2130 * current position |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2131 */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2132 int av_read_play(AVFormatContext *s) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2133 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2134 if (!s->iformat->read_play) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2135 return AVERROR_NOTSUPP; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2136 return s->iformat->read_play(s); |
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 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2139 /** |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2140 * 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
|
2141 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2142 * Use av_read_play() to resume it. |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2143 */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2144 int av_read_pause(AVFormatContext *s) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2145 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2146 if (!s->iformat->read_pause) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2147 return AVERROR_NOTSUPP; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2148 return s->iformat->read_pause(s); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2149 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2150 |
0 | 2151 /** |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2152 * Close a media file (but not its codecs). |
0 | 2153 * |
2154 * @param s media file handle | |
2155 */ | |
2156 void av_close_input_file(AVFormatContext *s) | |
2157 { | |
172 | 2158 int i, must_open_file; |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
2159 AVStream *st; |
0 | 2160 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2161 /* free previous packet */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2162 if (s->cur_st && s->cur_st->parser) |
885 | 2163 av_free_packet(&s->cur_pkt); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2164 |
0 | 2165 if (s->iformat->read_close) |
2166 s->iformat->read_close(s); | |
2167 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
|
2168 /* free all data in a stream component */ |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
2169 st = s->streams[i]; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2170 if (st->parser) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2171 av_parser_close(st->parser); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2172 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2173 av_free(st->index_entries); |
1120 | 2174 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
|
2175 av_free(st->codec); |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
2176 av_free(st); |
0 | 2177 } |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2178 flush_packet_queue(s); |
172 | 2179 must_open_file = 1; |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
2180 if (s->iformat->flags & AVFMT_NOFILE) { |
172 | 2181 must_open_file = 0; |
2182 } | |
2183 if (must_open_file) { | |
0 | 2184 url_fclose(&s->pb); |
2185 } | |
2186 av_freep(&s->priv_data); | |
2187 av_free(s); | |
2188 } | |
2189 | |
2190 /** | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2191 * Add a new stream to a media file. |
0 | 2192 * |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2193 * 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
|
2194 * 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
|
2195 * can be added in read_packet too. |
0 | 2196 * |
2197 * @param s media file handle | |
885 | 2198 * @param id file format dependent stream id |
0 | 2199 */ |
2200 AVStream *av_new_stream(AVFormatContext *s, int id) | |
2201 { | |
2202 AVStream *st; | |
2203 | |
2204 if (s->nb_streams >= MAX_STREAMS) | |
2205 return NULL; | |
2206 | |
2207 st = av_mallocz(sizeof(AVStream)); | |
2208 if (!st) | |
2209 return NULL; | |
885 | 2210 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2211 st->codec= avcodec_alloc_context(); |
193 | 2212 if (s->iformat) { |
2213 /* 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
|
2214 st->codec->bit_rate = 0; |
193 | 2215 } |
0 | 2216 st->index = s->nb_streams; |
2217 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
|
2218 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
|
2219 st->duration = AV_NOPTS_VALUE; |
468 | 2220 st->cur_dts = AV_NOPTS_VALUE; |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
453
diff
changeset
|
2221 |
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
453
diff
changeset
|
2222 /* default pts settings is MPEG like */ |
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
453
diff
changeset
|
2223 av_set_pts_info(st, 33, 1, 90000); |
464 | 2224 st->last_IP_pts = AV_NOPTS_VALUE; |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
453
diff
changeset
|
2225 |
0 | 2226 s->streams[s->nb_streams++] = st; |
2227 return st; | |
2228 } | |
2229 | |
2230 /************************************************************/ | |
2231 /* output media file */ | |
2232 | |
20 | 2233 int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap) |
2234 { | |
2235 int ret; | |
885 | 2236 |
32 | 2237 if (s->oformat->priv_data_size > 0) { |
2238 s->priv_data = av_mallocz(s->oformat->priv_data_size); | |
2239 if (!s->priv_data) | |
2240 return AVERROR_NOMEM; | |
2241 } else | |
2242 s->priv_data = NULL; | |
885 | 2243 |
20 | 2244 if (s->oformat->set_parameters) { |
2245 ret = s->oformat->set_parameters(s, ap); | |
2246 if (ret < 0) | |
2247 return ret; | |
2248 } | |
2249 return 0; | |
2250 } | |
2251 | |
0 | 2252 /** |
2253 * allocate the stream private data and write the stream header to an | |
2254 * output media file | |
2255 * | |
2256 * @param s media file handle | |
885 | 2257 * @return 0 if OK. AVERROR_xxx if error. |
0 | 2258 */ |
2259 int av_write_header(AVFormatContext *s) | |
2260 { | |
2261 int ret, i; | |
2262 AVStream *st; | |
2263 | |
839 | 2264 // some sanity checks |
2265 for(i=0;i<s->nb_streams;i++) { | |
2266 st = s->streams[i]; | |
2267 | |
2268 switch (st->codec->codec_type) { | |
2269 case CODEC_TYPE_AUDIO: | |
2270 if(st->codec->sample_rate<=0){ | |
2271 av_log(s, AV_LOG_ERROR, "sample rate not set\n"); | |
2272 return -1; | |
2273 } | |
2274 break; | |
2275 case CODEC_TYPE_VIDEO: | |
2276 if(st->codec->time_base.num<=0 || st->codec->time_base.den<=0){ //FIXME audio too? | |
2277 av_log(s, AV_LOG_ERROR, "time base not set\n"); | |
2278 return -1; | |
2279 } | |
2280 if(st->codec->width<=0 || st->codec->height<=0){ | |
2281 av_log(s, AV_LOG_ERROR, "dimensions not set\n"); | |
2282 return -1; | |
2283 } | |
2284 break; | |
2285 } | |
2286 } | |
2287 | |
869 | 2288 if(s->oformat->write_header){ |
2289 ret = s->oformat->write_header(s); | |
2290 if (ret < 0) | |
2291 return ret; | |
2292 } | |
0 | 2293 |
2294 /* init PTS generation */ | |
2295 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
|
2296 int64_t den = AV_NOPTS_VALUE; |
0 | 2297 st = s->streams[i]; |
2298 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
819
diff
changeset
|
2299 switch (st->codec->codec_type) { |
0 | 2300 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
|
2301 den = (int64_t)st->time_base.num * st->codec->sample_rate; |
0 | 2302 break; |
2303 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
|
2304 den = (int64_t)st->time_base.num * st->codec->time_base.den; |
0 | 2305 break; |
2306 default: | |
2307 break; | |
2308 } | |
840
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 != 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
|
2310 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
|
2311 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
|
2312 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
|
2313 } |
0 | 2314 } |
2315 return 0; | |
2316 } | |
2317 | |
470 | 2318 //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
|
2319 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
|
2320 int b_frames = FFMAX(st->codec->has_b_frames, st->codec->max_b_frames); |
470 | 2321 int num, den, frame_size; |
0 | 2322 |
470 | 2323 // 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 | 2324 |
468 | 2325 /* if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE) |
2326 return -1;*/ | |
885 | 2327 |
468 | 2328 /* duration field */ |
470 | 2329 if (pkt->duration == 0) { |
2330 compute_frame_duration(&num, &den, st, NULL, pkt); | |
2331 if (den && num) { | |
2332 pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den, den * (int64_t)st->time_base.num); | |
2333 } | |
2334 } | |
409 | 2335 |
468 | 2336 //XXX/FIXME this is a temporary hack until all encoders output pts |
2337 if((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !b_frames){ | |
2338 pkt->dts= | |
2339 // pkt->pts= st->cur_dts; | |
2340 pkt->pts= st->pts.val; | |
2341 } | |
2342 | |
885 | 2343 //calculate dts from pts |
468 | 2344 if(pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE){ |
2345 if(b_frames){ | |
2346 if(st->last_IP_pts == AV_NOPTS_VALUE){ | |
470 | 2347 st->last_IP_pts= -pkt->duration; |
468 | 2348 } |
2349 if(st->last_IP_pts < pkt->pts){ | |
2350 pkt->dts= st->last_IP_pts; | |
2351 st->last_IP_pts= pkt->pts; | |
2352 }else | |
2353 pkt->dts= pkt->pts; | |
2354 }else | |
2355 pkt->dts= pkt->pts; | |
2356 } | |
885 | 2357 |
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
|
2358 if(st->cur_dts && st->cur_dts != AV_NOPTS_VALUE && st->cur_dts >= pkt->dts){ |
881 | 2359 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
|
2360 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
|
2361 } |
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 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
|
2363 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
|
2364 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
|
2365 } |
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
|
2366 |
470 | 2367 // av_log(NULL, AV_LOG_DEBUG, "av_write_frame: pts2:%lld dts2:%lld\n", pkt->pts, pkt->dts); |
468 | 2368 st->cur_dts= pkt->dts; |
2369 st->pts.val= pkt->dts; | |
2370 | |
0 | 2371 /* 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
|
2372 switch (st->codec->codec_type) { |
0 | 2373 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
|
2374 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
|
2375 |
409 | 2376 /* 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
|
2377 but it would be better if we had the real timestamps from the encoder */ |
468 | 2378 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
|
2379 av_frac_add(&st->pts, (int64_t)st->time_base.den * frame_size); |
0 | 2380 } |
2381 break; | |
2382 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
|
2383 av_frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num); |
0 | 2384 break; |
2385 default: | |
2386 break; | |
2387 } | |
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
|
2388 return 0; |
470 | 2389 } |
2390 | |
2391 static void truncate_ts(AVStream *st, AVPacket *pkt){ | |
2392 int64_t pts_mask = (2LL << (st->pts_wrap_bits-1)) - 1; | |
885 | 2393 |
547 | 2394 // if(pkt->dts < 0) |
2395 // pkt->dts= 0; //this happens for low_delay=0 and b frames, FIXME, needs further invstigation about what we should do here | |
885 | 2396 |
470 | 2397 pkt->pts &= pts_mask; |
2398 pkt->dts &= pts_mask; | |
2399 } | |
2400 | |
2401 /** | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2402 * 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
|
2403 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2404 * The packet shall contain one audio or video frame. |
470 | 2405 * |
2406 * @param s media file handle | |
2407 * @param pkt the packet, which contains the stream_index, buf/buf_size, dts/pts, ... | |
2408 * @return < 0 if error, = 0 if OK, 1 if end of stream wanted. | |
2409 */ | |
2410 int av_write_frame(AVFormatContext *s, AVPacket *pkt) | |
2411 { | |
554 | 2412 int ret; |
2413 | |
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
|
2414 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
|
2415 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
|
2416 return ret; |
885 | 2417 |
470 | 2418 truncate_ts(s->streams[pkt->stream_index], pkt); |
2419 | |
554 | 2420 ret= s->oformat->write_packet(s, pkt); |
2421 if(!ret) | |
2422 ret= url_ferror(&s->pb); | |
2423 return ret; | |
470 | 2424 } |
2425 | |
2426 /** | |
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
|
2427 * interleave_packet implementation which will interleave per DTS. |
885 | 2428 * packets with pkt->destruct == av_destruct_packet will be freed inside this function. |
804 | 2429 * so they cannot be used after it, note calling av_free_packet() on them is still safe |
470 | 2430 */ |
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
|
2431 static int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush){ |
470 | 2432 AVPacketList *pktl, **next_point, *this_pktl; |
2433 int stream_count=0; | |
2434 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
|
2435 |
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 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
|
2437 AVStream *st= s->streams[ pkt->stream_index]; |
470 | 2438 |
804 | 2439 // assert(pkt->destruct != av_destruct_packet); //FIXME |
470 | 2440 |
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
|
2441 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
|
2442 this_pktl->pkt= *pkt; |
804 | 2443 if(pkt->destruct == av_destruct_packet) |
2444 pkt->destruct= NULL; // non shared -> must keep original from being freed | |
2445 else | |
2446 av_dup_packet(&this_pktl->pkt); //shared -> must dup | |
470 | 2447 |
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
|
2448 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
|
2449 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
|
2450 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
|
2451 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
|
2452 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
|
2453 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
|
2454 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
|
2455 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
|
2456 } |
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 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
|
2458 *next_point= this_pktl; |
470 | 2459 } |
885 | 2460 |
470 | 2461 memset(streams, 0, sizeof(streams)); |
2462 pktl= s->packet_buffer; | |
2463 while(pktl){ | |
2464 //av_log(s, AV_LOG_DEBUG, "show st:%d dts:%lld\n", pktl->pkt.stream_index, pktl->pkt.dts); | |
2465 if(streams[ pktl->pkt.stream_index ] == 0) | |
2466 stream_count++; | |
2467 streams[ pktl->pkt.stream_index ]++; | |
2468 pktl= pktl->next; | |
2469 } | |
885 | 2470 |
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
|
2471 if(s->nb_streams == stream_count || (flush && stream_count)){ |
470 | 2472 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
|
2473 *out= pktl->pkt; |
885 | 2474 |
2475 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
|
2476 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
|
2477 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
|
2478 }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
|
2479 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
|
2480 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
|
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 /** |
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 * 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
|
2486 * @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
|
2487 * @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
|
2488 * @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
|
2489 * remaining packets should be output |
885 | 2490 * @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
|
2491 * < 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
|
2492 */ |
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 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
|
2494 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
|
2495 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
|
2496 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
|
2497 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
|
2498 } |
470 | 2499 |
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
|
2500 /** |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2501 * 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
|
2502 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2503 * 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
|
2504 * 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
|
2505 * 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
|
2506 * 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
|
2507 * 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
|
2508 * 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
|
2509 * |
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 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
|
2511 * @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
|
2512 * @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
|
2513 */ |
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 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
|
2515 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
|
2516 |
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
|
2517 //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
|
2518 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
|
2519 return 0; |
717
a79f89d39c3c
discard dummy packets before doing inapropriate checks on them and failing as a result
michael
parents:
708
diff
changeset
|
2520 |
a79f89d39c3c
discard dummy packets before doing inapropriate checks on them and failing as a result
michael
parents:
708
diff
changeset
|
2521 //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
|
2522 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
|
2523 return -1; |
885 | 2524 |
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
|
2525 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
|
2526 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
|
2527 |
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 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
|
2529 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
|
2530 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
|
2531 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
|
2532 return ret; |
885 | 2533 |
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
|
2534 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
|
2535 ret= s->oformat->write_packet(s, &opkt); |
885 | 2536 |
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
|
2537 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
|
2538 pkt= NULL; |
885 | 2539 |
470 | 2540 if(ret<0) |
2541 return ret; | |
554 | 2542 if(url_ferror(&s->pb)) |
2543 return url_ferror(&s->pb); | |
470 | 2544 } |
0 | 2545 } |
2546 | |
2547 /** | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2548 * @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
|
2549 * free the file private data. |
0 | 2550 * |
2551 * @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
|
2552 * @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
|
2553 */ |
0 | 2554 int av_write_trailer(AVFormatContext *s) |
2555 { | |
540
26a477a5ebda
move free() of AVStream priv data to av_write_trailer()
michael
parents:
536
diff
changeset
|
2556 int ret, i; |
885 | 2557 |
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
|
2558 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
|
2559 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
|
2560 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
|
2561 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
|
2562 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
|
2563 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
|
2564 break; |
885 | 2565 |
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
|
2566 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
|
2567 ret= s->oformat->write_packet(s, &pkt); |
885 | 2568 |
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
|
2569 av_free_packet(&pkt); |
885 | 2570 |
470 | 2571 if(ret<0) |
540
26a477a5ebda
move free() of AVStream priv data to av_write_trailer()
michael
parents:
536
diff
changeset
|
2572 goto fail; |
554 | 2573 if(url_ferror(&s->pb)) |
2574 goto fail; | |
470 | 2575 } |
2576 | |
869 | 2577 if(s->oformat->write_trailer) |
2578 ret = s->oformat->write_trailer(s); | |
540
26a477a5ebda
move free() of AVStream priv data to av_write_trailer()
michael
parents:
536
diff
changeset
|
2579 fail: |
554 | 2580 if(ret == 0) |
2581 ret=url_ferror(&s->pb); | |
540
26a477a5ebda
move free() of AVStream priv data to av_write_trailer()
michael
parents:
536
diff
changeset
|
2582 for(i=0;i<s->nb_streams;i++) |
26a477a5ebda
move free() of AVStream priv data to av_write_trailer()
michael
parents:
536
diff
changeset
|
2583 av_freep(&s->streams[i]->priv_data); |
0 | 2584 av_freep(&s->priv_data); |
2585 return ret; | |
2586 } | |
2587 | |
2588 /* "user interface" functions */ | |
2589 | |
2590 void dump_format(AVFormatContext *ic, | |
885 | 2591 int index, |
0 | 2592 const char *url, |
2593 int is_output) | |
2594 { | |
2595 int i, flags; | |
2596 char buf[256]; | |
2597 | |
885 | 2598 av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n", |
0 | 2599 is_output ? "Output" : "Input", |
885 | 2600 index, |
2601 is_output ? ic->oformat->name : ic->iformat->name, | |
0 | 2602 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
|
2603 if (!is_output) { |
776 | 2604 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
|
2605 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
|
2606 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
|
2607 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
|
2608 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
|
2609 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
|
2610 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
|
2611 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
|
2612 mins %= 60; |
885 | 2613 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
|
2614 (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
|
2615 } else { |
776 | 2616 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
|
2617 } |
556
cb5f220888c0
print start_time patch by (Wolfram Gloger <wmglo at dent dot med dot uni-muenchen dot de>)
michael
parents:
555
diff
changeset
|
2618 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
|
2619 int secs, us; |
776 | 2620 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
|
2621 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
|
2622 us = ic->start_time % AV_TIME_BASE; |
776 | 2623 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
|
2624 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
|
2625 } |
776 | 2626 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
|
2627 if (ic->bit_rate) { |
776 | 2628 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
|
2629 } else { |
776 | 2630 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
|
2631 } |
776 | 2632 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
|
2633 } |
0 | 2634 for(i=0;i<ic->nb_streams;i++) { |
2635 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
|
2636 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
|
2637 avcodec_string(buf, sizeof(buf), st->codec, is_output); |
776 | 2638 av_log(NULL, AV_LOG_INFO, " Stream #%d.%d", index, i); |
0 | 2639 /* the pid is an important information, so we display it */ |
2640 /* XXX: add a generic system */ | |
2641 if (is_output) | |
2642 flags = ic->oformat->flags; | |
2643 else | |
2644 flags = ic->iformat->flags; | |
2645 if (flags & AVFMT_SHOW_IDS) { | |
776 | 2646 av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id); |
0 | 2647 } |
819
a6c035e7f429
DVB subtitle decoder by (Ian Caulfield: imc25, cam ac uk)
michael
parents:
810
diff
changeset
|
2648 if (strlen(st->language) > 0) { |
a6c035e7f429
DVB subtitle decoder by (Ian Caulfield: imc25, cam ac uk)
michael
parents:
810
diff
changeset
|
2649 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
|
2650 } |
855
379c4e948363
print more time_base fps stuff if av_log level is at debug or above
michael
parents:
851
diff
changeset
|
2651 av_log(NULL, AV_LOG_DEBUG, ", %d/%d", st->time_base.num/g, st->time_base.den/g); |
1119 | 2652 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
|
2653 if(st->codec->codec_type == CODEC_TYPE_VIDEO){ |
882 | 2654 if(st->r_frame_rate.den && st->r_frame_rate.num) |
2655 av_log(NULL, AV_LOG_INFO, ", %5.2f fps(r)", av_q2d(st->r_frame_rate)); | |
2656 /* else if(st->time_base.den && st->time_base.num) | |
2657 av_log(NULL, AV_LOG_INFO, ", %5.2f fps(m)", 1/av_q2d(st->time_base));*/ | |
2658 else | |
2659 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
|
2660 } |
1119 | 2661 av_log(NULL, AV_LOG_INFO, "\n"); |
0 | 2662 } |
2663 } | |
2664 | |
2665 typedef struct { | |
168 | 2666 const char *abv; |
0 | 2667 int width, height; |
168 | 2668 int frame_rate, frame_rate_base; |
2669 } AbvEntry; | |
0 | 2670 |
168 | 2671 static AbvEntry frame_abvs[] = { |
204 | 2672 { "ntsc", 720, 480, 30000, 1001 }, |
2673 { "pal", 720, 576, 25, 1 }, | |
2674 { "qntsc", 352, 240, 30000, 1001 }, /* VCD compliant ntsc */ | |
2675 { "qpal", 352, 288, 25, 1 }, /* VCD compliant pal */ | |
205 | 2676 { "sntsc", 640, 480, 30000, 1001 }, /* square pixel ntsc */ |
2677 { "spal", 768, 576, 25, 1 }, /* square pixel pal */ | |
168 | 2678 { "film", 352, 240, 24, 1 }, |
2679 { "ntsc-film", 352, 240, 24000, 1001 }, | |
2680 { "sqcif", 128, 96, 0, 0 }, | |
2681 { "qcif", 176, 144, 0, 0 }, | |
2682 { "cif", 352, 288, 0, 0 }, | |
2683 { "4cif", 704, 576, 0, 0 }, | |
0 | 2684 }; |
168 | 2685 |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2686 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2687 * 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
|
2688 */ |
0 | 2689 int parse_image_size(int *width_ptr, int *height_ptr, const char *str) |
2690 { | |
2691 int i; | |
168 | 2692 int n = sizeof(frame_abvs) / sizeof(AbvEntry); |
0 | 2693 const char *p; |
2694 int frame_width = 0, frame_height = 0; | |
2695 | |
2696 for(i=0;i<n;i++) { | |
168 | 2697 if (!strcmp(frame_abvs[i].abv, str)) { |
2698 frame_width = frame_abvs[i].width; | |
2699 frame_height = frame_abvs[i].height; | |
0 | 2700 break; |
2701 } | |
2702 } | |
2703 if (i == n) { | |
2704 p = str; | |
2705 frame_width = strtol(p, (char **)&p, 10); | |
2706 if (*p) | |
2707 p++; | |
2708 frame_height = strtol(p, (char **)&p, 10); | |
2709 } | |
2710 if (frame_width <= 0 || frame_height <= 0) | |
2711 return -1; | |
2712 *width_ptr = frame_width; | |
2713 *height_ptr = frame_height; | |
2714 return 0; | |
2715 } | |
2716 | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2717 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2718 * 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
|
2719 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2720 * 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
|
2721 * 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
|
2722 * 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
|
2723 */ |
168 | 2724 int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg) |
2725 { | |
2726 int i; | |
2727 char* cp; | |
885 | 2728 |
168 | 2729 /* First, we check our abbreviation table */ |
2730 for (i = 0; i < sizeof(frame_abvs)/sizeof(*frame_abvs); ++i) | |
2731 if (!strcmp(frame_abvs[i].abv, arg)) { | |
887 | 2732 *frame_rate = frame_abvs[i].frame_rate; |
2733 *frame_rate_base = frame_abvs[i].frame_rate_base; | |
2734 return 0; | |
2735 } | |
168 | 2736 |
2737 /* Then, we try to parse it as fraction */ | |
2738 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
|
2739 if (!cp) |
549e594a13c2
support colon-separated rates patch by Roine Gustafsson <roine AT users DOT sourceforge DOT net>
mmu_man
parents:
652
diff
changeset
|
2740 cp = strchr(arg, ':'); |
168 | 2741 if (cp) { |
2742 char* cpp; | |
887 | 2743 *frame_rate = strtol(arg, &cpp, 10); |
2744 if (cpp != arg || cpp == cp) | |
2745 *frame_rate_base = strtol(cp+1, &cpp, 10); | |
2746 else | |
2747 *frame_rate = 0; | |
885 | 2748 } |
168 | 2749 else { |
2750 /* Finally we give up and parse it as double */ | |
1030 | 2751 AVRational time_base = av_d2q(strtod(arg, 0), DEFAULT_FRAME_RATE_BASE); |
2752 *frame_rate_base = time_base.den; | |
2753 *frame_rate = time_base.num; | |
168 | 2754 } |
2755 if (!*frame_rate || !*frame_rate_base) | |
2756 return -1; | |
2757 else | |
2758 return 0; | |
2759 } | |
2760 | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2761 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2762 * 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
|
2763 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2764 * @code |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2765 * Syntax: |
0 | 2766 * - If not a duration: |
2767 * [{YYYY-MM-DD|YYYYMMDD}]{T| }{HH[:MM[:SS[.m...]]][Z]|HH[MM[SS[.m...]]][Z]} | |
2768 * Time is localtime unless Z is suffixed to the end. In this case GMT | |
885 | 2769 * 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
|
2770 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2771 * - If a duration: |
0 | 2772 * HH[:MM[:SS[.m...]]] |
2773 * 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
|
2774 * @endcode |
0 | 2775 */ |
907 | 2776 #ifndef CONFIG_WINCE |
65 | 2777 int64_t parse_date(const char *datestr, int duration) |
0 | 2778 { |
2779 const char *p; | |
65 | 2780 int64_t t; |
0 | 2781 struct tm dt; |
2782 int i; | |
2783 static const char *date_fmt[] = { | |
2784 "%Y-%m-%d", | |
2785 "%Y%m%d", | |
2786 }; | |
2787 static const char *time_fmt[] = { | |
2788 "%H:%M:%S", | |
2789 "%H%M%S", | |
2790 }; | |
2791 const char *q; | |
2792 int is_utc, len; | |
2793 char lastch; | |
477 | 2794 int negative = 0; |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
404
diff
changeset
|
2795 |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
404
diff
changeset
|
2796 #undef time |
0 | 2797 time_t now = time(0); |
2798 | |
2799 len = strlen(datestr); | |
2800 if (len > 0) | |
2801 lastch = datestr[len - 1]; | |
2802 else | |
2803 lastch = '\0'; | |
2804 is_utc = (lastch == 'z' || lastch == 'Z'); | |
2805 | |
2806 memset(&dt, 0, sizeof(dt)); | |
2807 | |
2808 p = datestr; | |
2809 q = NULL; | |
2810 if (!duration) { | |
2811 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
|
2812 q = small_strptime(p, date_fmt[i], &dt); |
0 | 2813 if (q) { |
2814 break; | |
2815 } | |
2816 } | |
2817 | |
2818 if (!q) { | |
2819 if (is_utc) { | |
2820 dt = *gmtime(&now); | |
2821 } else { | |
2822 dt = *localtime(&now); | |
2823 } | |
2824 dt.tm_hour = dt.tm_min = dt.tm_sec = 0; | |
2825 } else { | |
2826 p = q; | |
2827 } | |
2828 | |
2829 if (*p == 'T' || *p == 't' || *p == ' ') | |
2830 p++; | |
2831 | |
2832 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
|
2833 q = small_strptime(p, time_fmt[i], &dt); |
0 | 2834 if (q) { |
2835 break; | |
2836 } | |
2837 } | |
2838 } else { | |
887 | 2839 if (p[0] == '-') { |
2840 negative = 1; | |
2841 ++p; | |
2842 } | |
230
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
229
diff
changeset
|
2843 q = small_strptime(p, time_fmt[0], &dt); |
0 | 2844 if (!q) { |
2845 dt.tm_sec = strtol(p, (char **)&q, 10); | |
2846 dt.tm_min = 0; | |
2847 dt.tm_hour = 0; | |
2848 } | |
2849 } | |
2850 | |
2851 /* Now we have all the fields that we can get */ | |
2852 if (!q) { | |
2853 if (duration) | |
2854 return 0; | |
2855 else | |
65 | 2856 return now * int64_t_C(1000000); |
0 | 2857 } |
2858 | |
2859 if (duration) { | |
2860 t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec; | |
2861 } else { | |
2862 dt.tm_isdst = -1; /* unknown */ | |
2863 if (is_utc) { | |
2864 t = mktimegm(&dt); | |
2865 } else { | |
2866 t = mktime(&dt); | |
2867 } | |
2868 } | |
2869 | |
2870 t *= 1000000; | |
2871 | |
2872 if (*q == '.') { | |
2873 int val, n; | |
2874 q++; | |
2875 for (val = 0, n = 100000; n >= 1; n /= 10, q++) { | |
885 | 2876 if (!isdigit(*q)) |
0 | 2877 break; |
2878 val += n * (*q - '0'); | |
2879 } | |
2880 t += val; | |
2881 } | |
477 | 2882 return negative ? -t : t; |
0 | 2883 } |
907 | 2884 #endif /* CONFIG_WINCE */ |
0 | 2885 |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2886 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2887 * 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
|
2888 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2889 * 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
|
2890 * 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
|
2891 */ |
0 | 2892 int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info) |
2893 { | |
2894 const char *p; | |
2895 char tag[128], *q; | |
2896 | |
2897 p = info; | |
2898 if (*p == '?') | |
2899 p++; | |
2900 for(;;) { | |
2901 q = tag; | |
2902 while (*p != '\0' && *p != '=' && *p != '&') { | |
2903 if ((q - tag) < sizeof(tag) - 1) | |
2904 *q++ = *p; | |
2905 p++; | |
2906 } | |
2907 *q = '\0'; | |
2908 q = arg; | |
2909 if (*p == '=') { | |
2910 p++; | |
2911 while (*p != '&' && *p != '\0') { | |
2912 if ((q - arg) < arg_size - 1) { | |
2913 if (*p == '+') | |
2914 *q++ = ' '; | |
2915 else | |
2916 *q++ = *p; | |
2917 } | |
2918 p++; | |
2919 } | |
2920 *q = '\0'; | |
2921 } | |
885 | 2922 if (!strcmp(tag, tag1)) |
0 | 2923 return 1; |
2924 if (*p != '&') | |
2925 break; | |
2926 p++; | |
2927 } | |
2928 return 0; | |
2929 } | |
2930 | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2931 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2932 * 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
|
2933 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
2934 * 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
|
2935 * 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
|
2936 */ |
0 | 2937 int get_frame_filename(char *buf, int buf_size, |
2938 const char *path, int number) | |
2939 { | |
2940 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
|
2941 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
|
2942 int nd, len, percentd_found; |
0 | 2943 |
2944 q = buf; | |
2945 p = path; | |
2946 percentd_found = 0; | |
2947 for(;;) { | |
2948 c = *p++; | |
2949 if (c == '\0') | |
2950 break; | |
2951 if (c == '%') { | |
9
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2952 do { |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2953 nd = 0; |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2954 while (isdigit(*p)) { |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2955 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
|
2956 } |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2957 c = *p++; |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2958 } while (isdigit(c)); |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2959 |
0 | 2960 switch(c) { |
2961 case '%': | |
2962 goto addchar; | |
2963 case 'd': | |
2964 if (percentd_found) | |
2965 goto fail; | |
2966 percentd_found = 1; | |
2967 snprintf(buf1, sizeof(buf1), "%0*d", nd, number); | |
2968 len = strlen(buf1); | |
2969 if ((q - buf + len) > buf_size - 1) | |
2970 goto fail; | |
2971 memcpy(q, buf1, len); | |
2972 q += len; | |
2973 break; | |
2974 default: | |
2975 goto fail; | |
2976 } | |
2977 } else { | |
2978 addchar: | |
2979 if ((q - buf) < buf_size - 1) | |
2980 *q++ = c; | |
2981 } | |
2982 } | |
2983 if (!percentd_found) | |
2984 goto fail; | |
2985 *q = '\0'; | |
2986 return 0; | |
2987 fail: | |
2988 *q = '\0'; | |
2989 return -1; | |
2990 } | |
2991 | |
2992 /** | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2993 * Print nice hexa dump of a buffer |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2994 * @param f stream for output |
0 | 2995 * @param buf buffer |
2996 * @param size buffer size | |
2997 */ | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2998 void av_hex_dump(FILE *f, uint8_t *buf, int size) |
0 | 2999 { |
3000 int len, i, j, c; | |
3001 | |
3002 for(i=0;i<size;i+=16) { | |
3003 len = size - i; | |
3004 if (len > 16) | |
3005 len = 16; | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3006 fprintf(f, "%08x ", i); |
0 | 3007 for(j=0;j<16;j++) { |
3008 if (j < len) | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3009 fprintf(f, " %02x", buf[i+j]); |
0 | 3010 else |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3011 fprintf(f, " "); |
0 | 3012 } |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3013 fprintf(f, " "); |
0 | 3014 for(j=0;j<len;j++) { |
3015 c = buf[i+j]; | |
3016 if (c < ' ' || c > '~') | |
3017 c = '.'; | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3018 fprintf(f, "%c", c); |
0 | 3019 } |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3020 fprintf(f, "\n"); |
0 | 3021 } |
3022 } | |
3023 | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3024 /** |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3025 * Print on 'f' a nice dump of a packet |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3026 * @param f stream for output |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3027 * @param pkt packet to dump |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3028 * @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
|
3029 */ |
743 | 3030 //FIXME needs to know the time_base |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3031 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
|
3032 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3033 fprintf(f, "stream #%d:\n", pkt->stream_index); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3034 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
|
3035 fprintf(f, " duration=%0.3f\n", (double)pkt->duration / AV_TIME_BASE); |
333 | 3036 /* DTS is _always_ valid after av_read_frame() */ |
3037 fprintf(f, " dts="); | |
3038 if (pkt->dts == AV_NOPTS_VALUE) | |
3039 fprintf(f, "N/A"); | |
3040 else | |
3041 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
|
3042 /* 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
|
3043 fprintf(f, " pts="); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3044 if (pkt->pts == AV_NOPTS_VALUE) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3045 fprintf(f, "N/A"); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3046 else |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3047 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
|
3048 fprintf(f, "\n"); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3049 fprintf(f, " size=%d\n", pkt->size); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3050 if (dump_payload) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3051 av_hex_dump(f, pkt->data, pkt->size); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3052 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
3053 |
0 | 3054 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
|
3055 char *authorization, int authorization_size, |
0 | 3056 char *hostname, int hostname_size, |
3057 int *port_ptr, | |
3058 char *path, int path_size, | |
3059 const char *url) | |
3060 { | |
3061 const char *p; | |
3062 char *q; | |
3063 int port; | |
3064 | |
3065 port = -1; | |
3066 | |
3067 p = url; | |
3068 q = proto; | |
3069 while (*p != ':' && *p != '\0') { | |
3070 if ((q - proto) < proto_size - 1) | |
3071 *q++ = *p; | |
3072 p++; | |
3073 } | |
3074 if (proto_size > 0) | |
3075 *q = '\0'; | |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3076 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
|
3077 authorization[0] = '\0'; |
0 | 3078 if (*p == '\0') { |
3079 if (proto_size > 0) | |
3080 proto[0] = '\0'; | |
3081 if (hostname_size > 0) | |
3082 hostname[0] = '\0'; | |
3083 p = url; | |
3084 } else { | |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3085 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
|
3086 |
0 | 3087 p++; |
3088 if (*p == '/') | |
3089 p++; | |
3090 if (*p == '/') | |
3091 p++; | |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3092 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
|
3093 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
|
3094 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
|
3095 |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3096 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
|
3097 |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3098 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
|
3099 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
|
3100 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
|
3101 *q = '\0'; |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3102 q = hostname; |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3103 at = NULL; |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3104 } 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
|
3105 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
|
3106 *q++ = *p; |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3107 } else { |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3108 if ((q - authorization) < authorization_size - 1) |
0 | 3109 *q++ = *p; |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
502
diff
changeset
|
3110 } |
0 | 3111 p++; |
3112 } | |
3113 if (hostname_size > 0) | |
3114 *q = '\0'; | |
3115 if (*p == ':') { | |
3116 p++; | |
3117 port = strtoul(p, (char **)&p, 10); | |
3118 } | |
3119 } | |
3120 if (port_ptr) | |
3121 *port_ptr = port; | |
3122 pstrcpy(path, path_size, p); | |
3123 } | |
3124 | |
3125 /** | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3126 * 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
|
3127 * |
885 | 3128 * @param s stream |
0 | 3129 * @param pts_wrap_bits number of bits effectively used by the pts |
885 | 3130 * (used for wrap control, 33 is the value for MPEG) |
3131 * @param pts_num numerator to convert to seconds (MPEG: 1) | |
0 | 3132 * @param pts_den denominator to convert to seconds (MPEG: 90000) |
3133 */ | |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
453
diff
changeset
|
3134 void av_set_pts_info(AVStream *s, int pts_wrap_bits, |
0 | 3135 int pts_num, int pts_den) |
3136 { | |
3137 s->pts_wrap_bits = pts_wrap_bits; | |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
453
diff
changeset
|
3138 s->time_base.num = pts_num; |
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
453
diff
changeset
|
3139 s->time_base.den = pts_den; |
0 | 3140 } |
3141 | |
3142 /* fraction handling */ | |
3143 | |
3144 /** | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3145 * 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
|
3146 * |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3147 * 'num' is normalized so that it is such as 0 <= num < den. |
0 | 3148 * |
3149 * @param f fractional number | |
3150 * @param val integer value | |
3151 * @param num must be >= 0 | |
885 | 3152 * @param den must be >= 1 |
0 | 3153 */ |
1135
107f90e1551b
cleanup patch from Diego Petten <flameeyes@gentoo.org>
lu_zero
parents:
1127
diff
changeset
|
3154 static void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den) |
0 | 3155 { |
3156 num += (den >> 1); | |
3157 if (num >= den) { | |
3158 val += num / den; | |
3159 num = num % den; | |
3160 } | |
3161 f->val = val; | |
3162 f->num = num; | |
3163 f->den = den; | |
3164 } | |
3165 | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3166 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3167 * 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
|
3168 */ |
1135
107f90e1551b
cleanup patch from Diego Petten <flameeyes@gentoo.org>
lu_zero
parents:
1127
diff
changeset
|
3169 static void av_frac_set(AVFrac *f, int64_t val) |
0 | 3170 { |
3171 f->val = val; | |
3172 f->num = f->den >> 1; | |
3173 } | |
3174 | |
3175 /** | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3176 * Fractionnal addition to f: f = f + (incr / f->den). |
0 | 3177 * |
3178 * @param f fractional number | |
3179 * @param incr increment, can be positive or negative | |
3180 */ | |
1135
107f90e1551b
cleanup patch from Diego Petten <flameeyes@gentoo.org>
lu_zero
parents:
1127
diff
changeset
|
3181 static void av_frac_add(AVFrac *f, int64_t incr) |
0 | 3182 { |
65 | 3183 int64_t num, den; |
0 | 3184 |
3185 num = f->num + incr; | |
3186 den = f->den; | |
3187 if (num < 0) { | |
3188 f->val += num / den; | |
3189 num = num % den; | |
3190 if (num < 0) { | |
3191 num += den; | |
3192 f->val--; | |
3193 } | |
3194 } else if (num >= den) { | |
3195 f->val += num / den; | |
3196 num = num % den; | |
3197 } | |
3198 f->num = num; | |
3199 } | |
20 | 3200 |
3201 /** | |
3202 * register a new image format | |
3203 * @param img_fmt Image format descriptor | |
3204 */ | |
3205 void av_register_image_format(AVImageFormat *img_fmt) | |
3206 { | |
3207 AVImageFormat **p; | |
3208 | |
3209 p = &first_image_format; | |
3210 while (*p != NULL) p = &(*p)->next; | |
3211 *p = img_fmt; | |
3212 img_fmt->next = NULL; | |
3213 } | |
3214 | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3215 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3216 * 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
|
3217 */ |
20 | 3218 AVImageFormat *av_probe_image_format(AVProbeData *pd) |
3219 { | |
3220 AVImageFormat *fmt1, *fmt; | |
3221 int score, score_max; | |
3222 | |
3223 fmt = NULL; | |
3224 score_max = 0; | |
3225 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) { | |
3226 if (fmt1->img_probe) { | |
3227 score = fmt1->img_probe(pd); | |
3228 if (score > score_max) { | |
3229 score_max = score; | |
3230 fmt = fmt1; | |
3231 } | |
3232 } | |
3233 } | |
3234 return fmt; | |
3235 } | |
3236 | |
802
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3237 /** |
ea0e995ac4a8
part of the 'libavformat/utils.c doxygen documentation' patch by (Daniel Kristjansson: danielk, cat nyu edu)
michael
parents:
801
diff
changeset
|
3238 * 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
|
3239 */ |
20 | 3240 AVImageFormat *guess_image_format(const char *filename) |
3241 { | |
3242 AVImageFormat *fmt1; | |
3243 | |
3244 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) { | |
3245 if (fmt1->extensions && match_ext(filename, fmt1->extensions)) | |
3246 return fmt1; | |
3247 } | |
3248 return NULL; | |
3249 } | |
3250 | |
3251 /** | |
885 | 3252 * Read an image from a stream. |
20 | 3253 * @param gb byte stream containing the image |
3254 * @param fmt image format, NULL if probing is required | |
3255 */ | |
3256 int av_read_image(ByteIOContext *pb, const char *filename, | |
3257 AVImageFormat *fmt, | |
3258 int (*alloc_cb)(void *, AVImageInfo *info), void *opaque) | |
3259 { | |
922
7f20b3ce1100
dynamically increase probe buffer until format is detected
michael
parents:
915
diff
changeset
|
3260 uint8_t buf[PROBE_BUF_MIN]; |
20 | 3261 AVProbeData probe_data, *pd = &probe_data; |
3262 offset_t pos; | |
3263 int ret; | |
3264 | |
3265 if (!fmt) { | |
64 | 3266 pd->filename = filename; |
20 | 3267 pd->buf = buf; |
3268 pos = url_ftell(pb); | |
922
7f20b3ce1100
dynamically increase probe buffer until format is detected
michael
parents:
915
diff
changeset
|
3269 pd->buf_size = get_buffer(pb, buf, PROBE_BUF_MIN); |
20 | 3270 url_fseek(pb, pos, SEEK_SET); |
3271 fmt = av_probe_image_format(pd); | |
3272 } | |
3273 if (!fmt) | |
3274 return AVERROR_NOFMT; | |
3275 ret = fmt->img_read(pb, alloc_cb, opaque); | |
3276 return ret; | |
3277 } | |
3278 | |
3279 /** | |
3280 * Write an image to a stream. | |
3281 * @param pb byte stream for the image output | |
3282 * @param fmt image format | |
3283 * @param img image data and informations | |
3284 */ | |
3285 int av_write_image(ByteIOContext *pb, AVImageFormat *fmt, AVImageInfo *img) | |
3286 { | |
3287 return fmt->img_write(pb, img); | |
3288 } | |
3289 |