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