00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avformat.h"
00022 #include "avio_internal.h"
00023 #include "internal.h"
00024 #include "libavcodec/internal.h"
00025 #include "libavutil/opt.h"
00026 #include "metadata.h"
00027 #include "id3v2.h"
00028 #include "libavutil/avstring.h"
00029 #include "riff.h"
00030 #include "audiointerleave.h"
00031 #include <sys/time.h>
00032 #include <time.h>
00033 #include <strings.h>
00034 #include <stdarg.h>
00035 #if CONFIG_NETWORK
00036 #include "network.h"
00037 #endif
00038
00039 #undef NDEBUG
00040 #include <assert.h>
00041
00047 unsigned avformat_version(void)
00048 {
00049 return LIBAVFORMAT_VERSION_INT;
00050 }
00051
00052 const char *avformat_configuration(void)
00053 {
00054 return FFMPEG_CONFIGURATION;
00055 }
00056
00057 const char *avformat_license(void)
00058 {
00059 #define LICENSE_PREFIX "libavformat license: "
00060 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
00061 }
00062
00063
00064
00075 static void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
00076 {
00077 num += (den >> 1);
00078 if (num >= den) {
00079 val += num / den;
00080 num = num % den;
00081 }
00082 f->val = val;
00083 f->num = num;
00084 f->den = den;
00085 }
00086
00093 static void av_frac_add(AVFrac *f, int64_t incr)
00094 {
00095 int64_t num, den;
00096
00097 num = f->num + incr;
00098 den = f->den;
00099 if (num < 0) {
00100 f->val += num / den;
00101 num = num % den;
00102 if (num < 0) {
00103 num += den;
00104 f->val--;
00105 }
00106 } else if (num >= den) {
00107 f->val += num / den;
00108 num = num % den;
00109 }
00110 f->num = num;
00111 }
00112
00114 #if !FF_API_FIRST_FORMAT
00115 static
00116 #endif
00117 AVInputFormat *first_iformat = NULL;
00119 #if !FF_API_FIRST_FORMAT
00120 static
00121 #endif
00122 AVOutputFormat *first_oformat = NULL;
00123
00124 AVInputFormat *av_iformat_next(AVInputFormat *f)
00125 {
00126 if(f) return f->next;
00127 else return first_iformat;
00128 }
00129
00130 AVOutputFormat *av_oformat_next(AVOutputFormat *f)
00131 {
00132 if(f) return f->next;
00133 else return first_oformat;
00134 }
00135
00136 void av_register_input_format(AVInputFormat *format)
00137 {
00138 AVInputFormat **p;
00139 p = &first_iformat;
00140 while (*p != NULL) p = &(*p)->next;
00141 *p = format;
00142 format->next = NULL;
00143 }
00144
00145 void av_register_output_format(AVOutputFormat *format)
00146 {
00147 AVOutputFormat **p;
00148 p = &first_oformat;
00149 while (*p != NULL) p = &(*p)->next;
00150 *p = format;
00151 format->next = NULL;
00152 }
00153
00154 int av_match_ext(const char *filename, const char *extensions)
00155 {
00156 const char *ext, *p;
00157 char ext1[32], *q;
00158
00159 if(!filename)
00160 return 0;
00161
00162 ext = strrchr(filename, '.');
00163 if (ext) {
00164 ext++;
00165 p = extensions;
00166 for(;;) {
00167 q = ext1;
00168 while (*p != '\0' && *p != ',' && q-ext1<sizeof(ext1)-1)
00169 *q++ = *p++;
00170 *q = '\0';
00171 if (!strcasecmp(ext1, ext))
00172 return 1;
00173 if (*p == '\0')
00174 break;
00175 p++;
00176 }
00177 }
00178 return 0;
00179 }
00180
00181 static int match_format(const char *name, const char *names)
00182 {
00183 const char *p;
00184 int len, namelen;
00185
00186 if (!name || !names)
00187 return 0;
00188
00189 namelen = strlen(name);
00190 while ((p = strchr(names, ','))) {
00191 len = FFMAX(p - names, namelen);
00192 if (!strncasecmp(name, names, len))
00193 return 1;
00194 names = p+1;
00195 }
00196 return !strcasecmp(name, names);
00197 }
00198
00199 #if FF_API_GUESS_FORMAT
00200 AVOutputFormat *guess_format(const char *short_name, const char *filename,
00201 const char *mime_type)
00202 {
00203 return av_guess_format(short_name, filename, mime_type);
00204 }
00205 #endif
00206
00207 AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
00208 const char *mime_type)
00209 {
00210 AVOutputFormat *fmt = NULL, *fmt_found;
00211 int score_max, score;
00212
00213
00214 #if CONFIG_IMAGE2_MUXER
00215 if (!short_name && filename &&
00216 av_filename_number_test(filename) &&
00217 av_guess_image2_codec(filename) != CODEC_ID_NONE) {
00218 return av_guess_format("image2", NULL, NULL);
00219 }
00220 #endif
00221
00222 fmt_found = NULL;
00223 score_max = 0;
00224 while ((fmt = av_oformat_next(fmt))) {
00225 score = 0;
00226 if (fmt->name && short_name && !strcmp(fmt->name, short_name))
00227 score += 100;
00228 if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
00229 score += 10;
00230 if (filename && fmt->extensions &&
00231 av_match_ext(filename, fmt->extensions)) {
00232 score += 5;
00233 }
00234 if (score > score_max) {
00235 score_max = score;
00236 fmt_found = fmt;
00237 }
00238 }
00239 return fmt_found;
00240 }
00241
00242 #if FF_API_GUESS_FORMAT
00243 AVOutputFormat *guess_stream_format(const char *short_name, const char *filename,
00244 const char *mime_type)
00245 {
00246 AVOutputFormat *fmt = av_guess_format(short_name, filename, mime_type);
00247
00248 if (fmt) {
00249 AVOutputFormat *stream_fmt;
00250 char stream_format_name[64];
00251
00252 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream", fmt->name);
00253 stream_fmt = av_guess_format(stream_format_name, NULL, NULL);
00254
00255 if (stream_fmt)
00256 fmt = stream_fmt;
00257 }
00258
00259 return fmt;
00260 }
00261 #endif
00262
00263 enum CodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
00264 const char *filename, const char *mime_type, enum AVMediaType type){
00265 if(type == AVMEDIA_TYPE_VIDEO){
00266 enum CodecID codec_id= CODEC_ID_NONE;
00267
00268 #if CONFIG_IMAGE2_MUXER
00269 if(!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")){
00270 codec_id= av_guess_image2_codec(filename);
00271 }
00272 #endif
00273 if(codec_id == CODEC_ID_NONE)
00274 codec_id= fmt->video_codec;
00275 return codec_id;
00276 }else if(type == AVMEDIA_TYPE_AUDIO)
00277 return fmt->audio_codec;
00278 else if (type == AVMEDIA_TYPE_SUBTITLE)
00279 return fmt->subtitle_codec;
00280 else
00281 return CODEC_ID_NONE;
00282 }
00283
00284 AVInputFormat *av_find_input_format(const char *short_name)
00285 {
00286 AVInputFormat *fmt = NULL;
00287 while ((fmt = av_iformat_next(fmt))) {
00288 if (match_format(short_name, fmt->name))
00289 return fmt;
00290 }
00291 return NULL;
00292 }
00293
00294 #if FF_API_SYMVER && CONFIG_SHARED && HAVE_SYMVER
00295 FF_SYMVER(void, av_destruct_packet_nofree, (AVPacket *pkt), "LIBAVFORMAT_52")
00296 {
00297 av_destruct_packet_nofree(pkt);
00298 }
00299
00300 FF_SYMVER(void, av_destruct_packet, (AVPacket *pkt), "LIBAVFORMAT_52")
00301 {
00302 av_destruct_packet(pkt);
00303 }
00304
00305 FF_SYMVER(int, av_new_packet, (AVPacket *pkt, int size), "LIBAVFORMAT_52")
00306 {
00307 return av_new_packet(pkt, size);
00308 }
00309
00310 FF_SYMVER(int, av_dup_packet, (AVPacket *pkt), "LIBAVFORMAT_52")
00311 {
00312 return av_dup_packet(pkt);
00313 }
00314
00315 FF_SYMVER(void, av_free_packet, (AVPacket *pkt), "LIBAVFORMAT_52")
00316 {
00317 av_free_packet(pkt);
00318 }
00319
00320 FF_SYMVER(void, av_init_packet, (AVPacket *pkt), "LIBAVFORMAT_52")
00321 {
00322 av_log(NULL, AV_LOG_WARNING, "Diverting av_*_packet function calls to libavcodec. Recompile to improve performance\n");
00323 av_init_packet(pkt);
00324 }
00325 #endif
00326
00327 int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
00328 {
00329 int ret= av_new_packet(pkt, size);
00330
00331 if(ret<0)
00332 return ret;
00333
00334 pkt->pos= avio_tell(s);
00335
00336 ret= avio_read(s, pkt->data, size);
00337 if(ret<=0)
00338 av_free_packet(pkt);
00339 else
00340 av_shrink_packet(pkt, ret);
00341
00342 return ret;
00343 }
00344
00345 int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
00346 {
00347 int ret;
00348 int old_size;
00349 if (!pkt->size)
00350 return av_get_packet(s, pkt, size);
00351 old_size = pkt->size;
00352 ret = av_grow_packet(pkt, size);
00353 if (ret < 0)
00354 return ret;
00355 ret = avio_read(s, pkt->data + old_size, size);
00356 av_shrink_packet(pkt, old_size + FFMAX(ret, 0));
00357 return ret;
00358 }
00359
00360
00361 int av_filename_number_test(const char *filename)
00362 {
00363 char buf[1024];
00364 return filename && (av_get_frame_filename(buf, sizeof(buf), filename, 1)>=0);
00365 }
00366
00367 AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened, int *score_max)
00368 {
00369 AVProbeData lpd = *pd;
00370 AVInputFormat *fmt1 = NULL, *fmt;
00371 int score;
00372
00373 if (lpd.buf_size > 10 && ff_id3v2_match(lpd.buf, ID3v2_DEFAULT_MAGIC)) {
00374 int id3len = ff_id3v2_tag_len(lpd.buf);
00375 if (lpd.buf_size > id3len + 16) {
00376 lpd.buf += id3len;
00377 lpd.buf_size -= id3len;
00378 }
00379 }
00380
00381 fmt = NULL;
00382 while ((fmt1 = av_iformat_next(fmt1))) {
00383 if (!is_opened == !(fmt1->flags & AVFMT_NOFILE))
00384 continue;
00385 score = 0;
00386 if (fmt1->read_probe) {
00387 score = fmt1->read_probe(&lpd);
00388 } else if (fmt1->extensions) {
00389 if (av_match_ext(lpd.filename, fmt1->extensions)) {
00390 score = 50;
00391 }
00392 }
00393 if (score > *score_max) {
00394 *score_max = score;
00395 fmt = fmt1;
00396 }else if (score == *score_max)
00397 fmt = NULL;
00398 }
00399 return fmt;
00400 }
00401
00402 AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened){
00403 int score=0;
00404 return av_probe_input_format2(pd, is_opened, &score);
00405 }
00406
00407 static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st, AVProbeData *pd, int score)
00408 {
00409 static const struct {
00410 const char *name; enum CodecID id; enum AVMediaType type;
00411 } fmt_id_type[] = {
00412 { "aac" , CODEC_ID_AAC , AVMEDIA_TYPE_AUDIO },
00413 { "ac3" , CODEC_ID_AC3 , AVMEDIA_TYPE_AUDIO },
00414 { "dts" , CODEC_ID_DTS , AVMEDIA_TYPE_AUDIO },
00415 { "eac3" , CODEC_ID_EAC3 , AVMEDIA_TYPE_AUDIO },
00416 { "h264" , CODEC_ID_H264 , AVMEDIA_TYPE_VIDEO },
00417 { "m4v" , CODEC_ID_MPEG4 , AVMEDIA_TYPE_VIDEO },
00418 { "mp3" , CODEC_ID_MP3 , AVMEDIA_TYPE_AUDIO },
00419 { "mpegvideo", CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
00420 { 0 }
00421 };
00422 AVInputFormat *fmt = av_probe_input_format2(pd, 1, &score);
00423
00424 if (fmt) {
00425 int i;
00426 av_log(s, AV_LOG_DEBUG, "Probe with size=%d, packets=%d detected %s with score=%d\n",
00427 pd->buf_size, MAX_PROBE_PACKETS - st->probe_packets, fmt->name, score);
00428 for (i = 0; fmt_id_type[i].name; i++) {
00429 if (!strcmp(fmt->name, fmt_id_type[i].name)) {
00430 st->codec->codec_id = fmt_id_type[i].id;
00431 st->codec->codec_type = fmt_id_type[i].type;
00432 break;
00433 }
00434 }
00435 }
00436 return !!fmt;
00437 }
00438
00439
00440
00441
00445 int av_open_input_stream(AVFormatContext **ic_ptr,
00446 AVIOContext *pb, const char *filename,
00447 AVInputFormat *fmt, AVFormatParameters *ap)
00448 {
00449 int err;
00450 AVFormatContext *ic;
00451 AVFormatParameters default_ap;
00452
00453 if(!ap){
00454 ap=&default_ap;
00455 memset(ap, 0, sizeof(default_ap));
00456 }
00457
00458 if(!ap->prealloced_context)
00459 ic = avformat_alloc_context();
00460 else
00461 ic = *ic_ptr;
00462 if (!ic) {
00463 err = AVERROR(ENOMEM);
00464 goto fail;
00465 }
00466 ic->iformat = fmt;
00467 ic->pb = pb;
00468 ic->duration = AV_NOPTS_VALUE;
00469 ic->start_time = AV_NOPTS_VALUE;
00470 av_strlcpy(ic->filename, filename, sizeof(ic->filename));
00471
00472
00473 if (fmt->priv_data_size > 0) {
00474 ic->priv_data = av_mallocz(fmt->priv_data_size);
00475 if (!ic->priv_data) {
00476 err = AVERROR(ENOMEM);
00477 goto fail;
00478 }
00479 } else {
00480 ic->priv_data = NULL;
00481 }
00482
00483
00484 if (ic->pb)
00485 ff_id3v2_read(ic, ID3v2_DEFAULT_MAGIC);
00486
00487 if (ic->iformat->read_header) {
00488 err = ic->iformat->read_header(ic, ap);
00489 if (err < 0)
00490 goto fail;
00491 }
00492
00493 if (pb && !ic->data_offset)
00494 ic->data_offset = avio_tell(ic->pb);
00495
00496 #if FF_API_OLD_METADATA
00497 ff_metadata_demux_compat(ic);
00498 #endif
00499
00500 ic->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
00501
00502 *ic_ptr = ic;
00503 return 0;
00504 fail:
00505 if (ic) {
00506 int i;
00507 av_freep(&ic->priv_data);
00508 for(i=0;i<ic->nb_streams;i++) {
00509 AVStream *st = ic->streams[i];
00510 if (st) {
00511 av_free(st->priv_data);
00512 av_free(st->codec->extradata);
00513 av_free(st->codec);
00514 av_free(st->info);
00515 }
00516 av_free(st);
00517 }
00518 }
00519 av_free(ic);
00520 *ic_ptr = NULL;
00521 return err;
00522 }
00523
00525 #define PROBE_BUF_MIN 2048
00526 #define PROBE_BUF_MAX (1<<20)
00527
00528 int av_probe_input_buffer(AVIOContext *pb, AVInputFormat **fmt,
00529 const char *filename, void *logctx,
00530 unsigned int offset, unsigned int max_probe_size)
00531 {
00532 AVProbeData pd = { filename ? filename : "", NULL, -offset };
00533 unsigned char *buf = NULL;
00534 int ret = 0, probe_size;
00535
00536 if (!max_probe_size) {
00537 max_probe_size = PROBE_BUF_MAX;
00538 } else if (max_probe_size > PROBE_BUF_MAX) {
00539 max_probe_size = PROBE_BUF_MAX;
00540 } else if (max_probe_size < PROBE_BUF_MIN) {
00541 return AVERROR(EINVAL);
00542 }
00543
00544 if (offset >= max_probe_size) {
00545 return AVERROR(EINVAL);
00546 }
00547
00548 for(probe_size= PROBE_BUF_MIN; probe_size<=max_probe_size && !*fmt && ret >= 0;
00549 probe_size = FFMIN(probe_size<<1, FFMAX(max_probe_size, probe_size+1))) {
00550 int ret, score = probe_size < max_probe_size ? AVPROBE_SCORE_MAX/4 : 0;
00551 int buf_offset = (probe_size == PROBE_BUF_MIN) ? 0 : probe_size>>1;
00552
00553 if (probe_size < offset) {
00554 continue;
00555 }
00556
00557
00558 buf = av_realloc(buf, probe_size + AVPROBE_PADDING_SIZE);
00559 if ((ret = avio_read(pb, buf + buf_offset, probe_size - buf_offset)) < 0) {
00560
00561 if (ret != AVERROR_EOF) {
00562 av_free(buf);
00563 return ret;
00564 }
00565 score = 0;
00566 ret = 0;
00567 }
00568 pd.buf_size += ret;
00569 pd.buf = &buf[offset];
00570
00571 memset(pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE);
00572
00573
00574 *fmt = av_probe_input_format2(&pd, 1, &score);
00575 if(*fmt){
00576 if(score <= AVPROBE_SCORE_MAX/4){
00577 av_log(logctx, AV_LOG_WARNING, "Format detected only with low score of %d, misdetection possible!\n", score);
00578 }else
00579 av_log(logctx, AV_LOG_DEBUG, "Probed with size=%d and score=%d\n", probe_size, score);
00580 }
00581 }
00582
00583 if (!*fmt) {
00584 av_free(buf);
00585 return AVERROR_INVALIDDATA;
00586 }
00587
00588
00589 if ((ret = ffio_rewind_with_probe_data(pb, buf, pd.buf_size)) < 0)
00590 av_free(buf);
00591
00592 return ret;
00593 }
00594
00595 int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
00596 AVInputFormat *fmt,
00597 int buf_size,
00598 AVFormatParameters *ap)
00599 {
00600 int err;
00601 AVProbeData probe_data, *pd = &probe_data;
00602 AVIOContext *pb = NULL;
00603 void *logctx= ap && ap->prealloced_context ? *ic_ptr : NULL;
00604
00605 pd->filename = "";
00606 if (filename)
00607 pd->filename = filename;
00608 pd->buf = NULL;
00609 pd->buf_size = 0;
00610
00611 if (!fmt) {
00612
00613 fmt = av_probe_input_format(pd, 0);
00614 }
00615
00616
00617
00618 if (!fmt || !(fmt->flags & AVFMT_NOFILE)) {
00619
00620 if ((err=avio_open(&pb, filename, URL_RDONLY)) < 0) {
00621 goto fail;
00622 }
00623 if (buf_size > 0) {
00624 url_setbufsize(pb, buf_size);
00625 }
00626 if (!fmt && (err = av_probe_input_buffer(pb, &fmt, filename, logctx, 0, logctx ? (*ic_ptr)->probesize : 0)) < 0) {
00627 goto fail;
00628 }
00629 }
00630
00631
00632 if (!fmt) {
00633 err = AVERROR_INVALIDDATA;
00634 goto fail;
00635 }
00636
00637
00638 if (fmt->flags & AVFMT_NEEDNUMBER) {
00639 if (!av_filename_number_test(filename)) {
00640 err = AVERROR_NUMEXPECTED;
00641 goto fail;
00642 }
00643 }
00644 err = av_open_input_stream(ic_ptr, pb, filename, fmt, ap);
00645 if (err)
00646 goto fail;
00647 return 0;
00648 fail:
00649 av_freep(&pd->buf);
00650 if (pb)
00651 avio_close(pb);
00652 if (ap && ap->prealloced_context)
00653 av_free(*ic_ptr);
00654 *ic_ptr = NULL;
00655 return err;
00656
00657 }
00658
00659
00660
00661 static AVPacket *add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt,
00662 AVPacketList **plast_pktl){
00663 AVPacketList *pktl = av_mallocz(sizeof(AVPacketList));
00664 if (!pktl)
00665 return NULL;
00666
00667 if (*packet_buffer)
00668 (*plast_pktl)->next = pktl;
00669 else
00670 *packet_buffer = pktl;
00671
00672
00673 *plast_pktl = pktl;
00674 pktl->pkt= *pkt;
00675 return &pktl->pkt;
00676 }
00677
00678 int av_read_packet(AVFormatContext *s, AVPacket *pkt)
00679 {
00680 int ret, i;
00681 AVStream *st;
00682
00683 for(;;){
00684 AVPacketList *pktl = s->raw_packet_buffer;
00685
00686 if (pktl) {
00687 *pkt = pktl->pkt;
00688 if(s->streams[pkt->stream_index]->codec->codec_id != CODEC_ID_PROBE ||
00689 !s->streams[pkt->stream_index]->probe_packets ||
00690 s->raw_packet_buffer_remaining_size < pkt->size){
00691 AVProbeData *pd = &s->streams[pkt->stream_index]->probe_data;
00692 av_freep(&pd->buf);
00693 pd->buf_size = 0;
00694 s->raw_packet_buffer = pktl->next;
00695 s->raw_packet_buffer_remaining_size += pkt->size;
00696 av_free(pktl);
00697 return 0;
00698 }
00699 }
00700
00701 av_init_packet(pkt);
00702 ret= s->iformat->read_packet(s, pkt);
00703 if (ret < 0) {
00704 if (!pktl || ret == AVERROR(EAGAIN))
00705 return ret;
00706 for (i = 0; i < s->nb_streams; i++)
00707 s->streams[i]->probe_packets = 0;
00708 continue;
00709 }
00710 st= s->streams[pkt->stream_index];
00711
00712 switch(st->codec->codec_type){
00713 case AVMEDIA_TYPE_VIDEO:
00714 if(s->video_codec_id) st->codec->codec_id= s->video_codec_id;
00715 break;
00716 case AVMEDIA_TYPE_AUDIO:
00717 if(s->audio_codec_id) st->codec->codec_id= s->audio_codec_id;
00718 break;
00719 case AVMEDIA_TYPE_SUBTITLE:
00720 if(s->subtitle_codec_id)st->codec->codec_id= s->subtitle_codec_id;
00721 break;
00722 }
00723
00724 if(!pktl && (st->codec->codec_id != CODEC_ID_PROBE ||
00725 !st->probe_packets))
00726 return ret;
00727
00728 add_to_pktbuf(&s->raw_packet_buffer, pkt, &s->raw_packet_buffer_end);
00729 s->raw_packet_buffer_remaining_size -= pkt->size;
00730
00731 if(st->codec->codec_id == CODEC_ID_PROBE){
00732 AVProbeData *pd = &st->probe_data;
00733 av_log(s, AV_LOG_DEBUG, "probing stream %d\n", st->index);
00734 --st->probe_packets;
00735
00736 pd->buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
00737 memcpy(pd->buf+pd->buf_size, pkt->data, pkt->size);
00738 pd->buf_size += pkt->size;
00739 memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE);
00740
00741 if(av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)){
00742
00743 set_codec_from_probe_data(s, st, pd, st->probe_packets > 0 ? AVPROBE_SCORE_MAX/4 : 0);
00744 if(st->codec->codec_id != CODEC_ID_PROBE){
00745 pd->buf_size=0;
00746 av_freep(&pd->buf);
00747 av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index);
00748 }
00749 }
00750 }
00751 }
00752 }
00753
00754
00755
00759 static int get_audio_frame_size(AVCodecContext *enc, int size)
00760 {
00761 int frame_size;
00762
00763 if(enc->codec_id == CODEC_ID_VORBIS)
00764 return -1;
00765
00766 if (enc->frame_size <= 1) {
00767 int bits_per_sample = av_get_bits_per_sample(enc->codec_id);
00768
00769 if (bits_per_sample) {
00770 if (enc->channels == 0)
00771 return -1;
00772 frame_size = (size << 3) / (bits_per_sample * enc->channels);
00773 } else {
00774
00775 if (enc->bit_rate == 0)
00776 return -1;
00777 frame_size = ((int64_t)size * 8 * enc->sample_rate) / enc->bit_rate;
00778 }
00779 } else {
00780 frame_size = enc->frame_size;
00781 }
00782 return frame_size;
00783 }
00784
00785
00789 static void compute_frame_duration(int *pnum, int *pden, AVStream *st,
00790 AVCodecParserContext *pc, AVPacket *pkt)
00791 {
00792 int frame_size;
00793
00794 *pnum = 0;
00795 *pden = 0;
00796 switch(st->codec->codec_type) {
00797 case AVMEDIA_TYPE_VIDEO:
00798 if(st->time_base.num*1000LL > st->time_base.den){
00799 *pnum = st->time_base.num;
00800 *pden = st->time_base.den;
00801 }else if(st->codec->time_base.num*1000LL > st->codec->time_base.den){
00802 *pnum = st->codec->time_base.num;
00803 *pden = st->codec->time_base.den;
00804 if (pc && pc->repeat_pict) {
00805 *pnum = (*pnum) * (1 + pc->repeat_pict);
00806 }
00807
00808
00809 if(st->codec->ticks_per_frame>1 && !pc){
00810 *pnum = *pden = 0;
00811 }
00812 }
00813 break;
00814 case AVMEDIA_TYPE_AUDIO:
00815 frame_size = get_audio_frame_size(st->codec, pkt->size);
00816 if (frame_size <= 0 || st->codec->sample_rate <= 0)
00817 break;
00818 *pnum = frame_size;
00819 *pden = st->codec->sample_rate;
00820 break;
00821 default:
00822 break;
00823 }
00824 }
00825
00826 static int is_intra_only(AVCodecContext *enc){
00827 if(enc->codec_type == AVMEDIA_TYPE_AUDIO){
00828 return 1;
00829 }else if(enc->codec_type == AVMEDIA_TYPE_VIDEO){
00830 switch(enc->codec_id){
00831 case CODEC_ID_MJPEG:
00832 case CODEC_ID_MJPEGB:
00833 case CODEC_ID_LJPEG:
00834 case CODEC_ID_RAWVIDEO:
00835 case CODEC_ID_DVVIDEO:
00836 case CODEC_ID_HUFFYUV:
00837 case CODEC_ID_FFVHUFF:
00838 case CODEC_ID_ASV1:
00839 case CODEC_ID_ASV2:
00840 case CODEC_ID_VCR1:
00841 case CODEC_ID_DNXHD:
00842 case CODEC_ID_JPEG2000:
00843 return 1;
00844 default: break;
00845 }
00846 }
00847 return 0;
00848 }
00849
00850 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
00851 int64_t dts, int64_t pts)
00852 {
00853 AVStream *st= s->streams[stream_index];
00854 AVPacketList *pktl= s->packet_buffer;
00855
00856 if(st->first_dts != AV_NOPTS_VALUE || dts == AV_NOPTS_VALUE || st->cur_dts == AV_NOPTS_VALUE)
00857 return;
00858
00859 st->first_dts= dts - st->cur_dts;
00860 st->cur_dts= dts;
00861
00862 for(; pktl; pktl= pktl->next){
00863 if(pktl->pkt.stream_index != stream_index)
00864 continue;
00865
00866 if(pktl->pkt.pts != AV_NOPTS_VALUE && pktl->pkt.pts == pktl->pkt.dts)
00867 pktl->pkt.pts += st->first_dts;
00868
00869 if(pktl->pkt.dts != AV_NOPTS_VALUE)
00870 pktl->pkt.dts += st->first_dts;
00871
00872 if(st->start_time == AV_NOPTS_VALUE && pktl->pkt.pts != AV_NOPTS_VALUE)
00873 st->start_time= pktl->pkt.pts;
00874 }
00875 if (st->start_time == AV_NOPTS_VALUE)
00876 st->start_time = pts;
00877 }
00878
00879 static void update_initial_durations(AVFormatContext *s, AVStream *st, AVPacket *pkt)
00880 {
00881 AVPacketList *pktl= s->packet_buffer;
00882 int64_t cur_dts= 0;
00883
00884 if(st->first_dts != AV_NOPTS_VALUE){
00885 cur_dts= st->first_dts;
00886 for(; pktl; pktl= pktl->next){
00887 if(pktl->pkt.stream_index == pkt->stream_index){
00888 if(pktl->pkt.pts != pktl->pkt.dts || pktl->pkt.dts != AV_NOPTS_VALUE || pktl->pkt.duration)
00889 break;
00890 cur_dts -= pkt->duration;
00891 }
00892 }
00893 pktl= s->packet_buffer;
00894 st->first_dts = cur_dts;
00895 }else if(st->cur_dts)
00896 return;
00897
00898 for(; pktl; pktl= pktl->next){
00899 if(pktl->pkt.stream_index != pkt->stream_index)
00900 continue;
00901 if(pktl->pkt.pts == pktl->pkt.dts && pktl->pkt.dts == AV_NOPTS_VALUE
00902 && !pktl->pkt.duration){
00903 pktl->pkt.dts= cur_dts;
00904 if(!st->codec->has_b_frames)
00905 pktl->pkt.pts= cur_dts;
00906 cur_dts += pkt->duration;
00907 pktl->pkt.duration= pkt->duration;
00908 }else
00909 break;
00910 }
00911 if(st->first_dts == AV_NOPTS_VALUE)
00912 st->cur_dts= cur_dts;
00913 }
00914
00915 static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
00916 AVCodecParserContext *pc, AVPacket *pkt)
00917 {
00918 int num, den, presentation_delayed, delay, i;
00919 int64_t offset;
00920
00921 if (s->flags & AVFMT_FLAG_NOFILLIN)
00922 return;
00923
00924 if((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
00925 pkt->dts= AV_NOPTS_VALUE;
00926
00927 if (st->codec->codec_id != CODEC_ID_H264 && pc && pc->pict_type == FF_B_TYPE)
00928
00929 st->codec->has_b_frames = 1;
00930
00931
00932 delay= st->codec->has_b_frames;
00933 presentation_delayed = 0;
00934
00935
00936
00937 if (delay && st->codec->active_thread_type&FF_THREAD_FRAME)
00938 delay -= st->codec->thread_count-1;
00939
00940
00941
00942 if (delay &&
00943 pc && pc->pict_type != FF_B_TYPE)
00944 presentation_delayed = 1;
00945
00946 if(pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && pkt->dts > pkt->pts && st->pts_wrap_bits<63
00947 ){
00948 pkt->dts -= 1LL<<st->pts_wrap_bits;
00949 }
00950
00951
00952
00953
00954 if(delay==1 && pkt->dts == pkt->pts && pkt->dts != AV_NOPTS_VALUE && presentation_delayed){
00955 av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination\n");
00956 pkt->dts= pkt->pts= AV_NOPTS_VALUE;
00957 }
00958
00959 if (pkt->duration == 0) {
00960 compute_frame_duration(&num, &den, st, pc, pkt);
00961 if (den && num) {
00962 pkt->duration = av_rescale_rnd(1, num * (int64_t)st->time_base.den, den * (int64_t)st->time_base.num, AV_ROUND_DOWN);
00963
00964 if(pkt->duration != 0 && s->packet_buffer)
00965 update_initial_durations(s, st, pkt);
00966 }
00967 }
00968
00969
00970
00971 if(pc && st->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size){
00972
00973 offset = av_rescale(pc->offset, pkt->duration, pkt->size);
00974 if(pkt->pts != AV_NOPTS_VALUE)
00975 pkt->pts += offset;
00976 if(pkt->dts != AV_NOPTS_VALUE)
00977 pkt->dts += offset;
00978 }
00979
00980 if (pc && pc->dts_sync_point >= 0) {
00981
00982 int64_t den = st->codec->time_base.den * (int64_t) st->time_base.num;
00983 if (den > 0) {
00984 int64_t num = st->codec->time_base.num * (int64_t) st->time_base.den;
00985 if (pkt->dts != AV_NOPTS_VALUE) {
00986
00987 st->reference_dts = pkt->dts - pc->dts_ref_dts_delta * num / den;
00988 pkt->pts = pkt->dts + pc->pts_dts_delta * num / den;
00989 } else if (st->reference_dts != AV_NOPTS_VALUE) {
00990
00991 pkt->dts = st->reference_dts + pc->dts_ref_dts_delta * num / den;
00992 pkt->pts = pkt->dts + pc->pts_dts_delta * num / den;
00993 }
00994 if (pc->dts_sync_point > 0)
00995 st->reference_dts = pkt->dts;
00996 }
00997 }
00998
00999
01000 if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts > pkt->dts)
01001 presentation_delayed = 1;
01002
01003
01004
01005
01006 if((delay==0 || (delay==1 && pc)) && st->codec->codec_id != CODEC_ID_H264){
01007 if (presentation_delayed) {
01008
01009
01010 if (pkt->dts == AV_NOPTS_VALUE)
01011 pkt->dts = st->last_IP_pts;
01012 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts);
01013 if (pkt->dts == AV_NOPTS_VALUE)
01014 pkt->dts = st->cur_dts;
01015
01016
01017
01018 if (st->last_IP_duration == 0)
01019 st->last_IP_duration = pkt->duration;
01020 if(pkt->dts != AV_NOPTS_VALUE)
01021 st->cur_dts = pkt->dts + st->last_IP_duration;
01022 st->last_IP_duration = pkt->duration;
01023 st->last_IP_pts= pkt->pts;
01024
01025
01026 } else if(pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE || pkt->duration){
01027 if(pkt->pts != AV_NOPTS_VALUE && pkt->duration){
01028 int64_t old_diff= FFABS(st->cur_dts - pkt->duration - pkt->pts);
01029 int64_t new_diff= FFABS(st->cur_dts - pkt->pts);
01030 if(old_diff < new_diff && old_diff < (pkt->duration>>3)){
01031 pkt->pts += pkt->duration;
01032
01033 }
01034 }
01035
01036
01037 if(pkt->pts == AV_NOPTS_VALUE)
01038 pkt->pts = pkt->dts;
01039 update_initial_timestamps(s, pkt->stream_index, pkt->pts, pkt->pts);
01040 if(pkt->pts == AV_NOPTS_VALUE)
01041 pkt->pts = st->cur_dts;
01042 pkt->dts = pkt->pts;
01043 if(pkt->pts != AV_NOPTS_VALUE)
01044 st->cur_dts = pkt->pts + pkt->duration;
01045 }
01046 }
01047
01048 if(pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY){
01049 st->pts_buffer[0]= pkt->pts;
01050 for(i=0; i<delay && st->pts_buffer[i] > st->pts_buffer[i+1]; i++)
01051 FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i+1]);
01052 if(pkt->dts == AV_NOPTS_VALUE)
01053 pkt->dts= st->pts_buffer[0];
01054 if(st->codec->codec_id == CODEC_ID_H264){
01055 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts);
01056 }
01057 if(pkt->dts > st->cur_dts)
01058 st->cur_dts = pkt->dts;
01059 }
01060
01061
01062
01063
01064 if(is_intra_only(st->codec))
01065 pkt->flags |= AV_PKT_FLAG_KEY;
01066 else if (pc) {
01067 pkt->flags = 0;
01068
01069 if (pc->key_frame == 1)
01070 pkt->flags |= AV_PKT_FLAG_KEY;
01071 else if (pc->key_frame == -1 && pc->pict_type == FF_I_TYPE)
01072 pkt->flags |= AV_PKT_FLAG_KEY;
01073 }
01074 if (pc)
01075 pkt->convergence_duration = pc->convergence_duration;
01076 }
01077
01078
01079 static int av_read_frame_internal(AVFormatContext *s, AVPacket *pkt)
01080 {
01081 AVStream *st;
01082 int len, ret, i;
01083
01084 av_init_packet(pkt);
01085
01086 for(;;) {
01087
01088 st = s->cur_st;
01089 if (st) {
01090 if (!st->need_parsing || !st->parser) {
01091
01092
01093 *pkt = st->cur_pkt; st->cur_pkt.data= NULL;
01094 compute_pkt_fields(s, st, NULL, pkt);
01095 s->cur_st = NULL;
01096 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
01097 (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
01098 ff_reduce_index(s, st->index);
01099 av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
01100 }
01101 break;
01102 } else if (st->cur_len > 0 && st->discard < AVDISCARD_ALL) {
01103 len = av_parser_parse2(st->parser, st->codec, &pkt->data, &pkt->size,
01104 st->cur_ptr, st->cur_len,
01105 st->cur_pkt.pts, st->cur_pkt.dts,
01106 st->cur_pkt.pos);
01107 st->cur_pkt.pts = AV_NOPTS_VALUE;
01108 st->cur_pkt.dts = AV_NOPTS_VALUE;
01109
01110 st->cur_ptr += len;
01111 st->cur_len -= len;
01112
01113
01114 if (pkt->size) {
01115 got_packet:
01116 pkt->duration = 0;
01117 pkt->stream_index = st->index;
01118 pkt->pts = st->parser->pts;
01119 pkt->dts = st->parser->dts;
01120 pkt->pos = st->parser->pos;
01121 if(pkt->data == st->cur_pkt.data && pkt->size == st->cur_pkt.size){
01122 s->cur_st = NULL;
01123 pkt->destruct= st->cur_pkt.destruct;
01124 st->cur_pkt.destruct= NULL;
01125 st->cur_pkt.data = NULL;
01126 assert(st->cur_len == 0);
01127 }else{
01128 pkt->destruct = NULL;
01129 }
01130 compute_pkt_fields(s, st, st->parser, pkt);
01131
01132 if((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY){
01133 ff_reduce_index(s, st->index);
01134 av_add_index_entry(st, st->parser->frame_offset, pkt->dts,
01135 0, 0, AVINDEX_KEYFRAME);
01136 }
01137
01138 break;
01139 }
01140 } else {
01141
01142 av_free_packet(&st->cur_pkt);
01143 s->cur_st = NULL;
01144 }
01145 } else {
01146 AVPacket cur_pkt;
01147
01148 ret = av_read_packet(s, &cur_pkt);
01149 if (ret < 0) {
01150 if (ret == AVERROR(EAGAIN))
01151 return ret;
01152
01153 for(i = 0; i < s->nb_streams; i++) {
01154 st = s->streams[i];
01155 if (st->parser && st->need_parsing) {
01156 av_parser_parse2(st->parser, st->codec,
01157 &pkt->data, &pkt->size,
01158 NULL, 0,
01159 AV_NOPTS_VALUE, AV_NOPTS_VALUE,
01160 AV_NOPTS_VALUE);
01161 if (pkt->size)
01162 goto got_packet;
01163 }
01164 }
01165
01166 return ret;
01167 }
01168 st = s->streams[cur_pkt.stream_index];
01169 st->cur_pkt= cur_pkt;
01170
01171 if(st->cur_pkt.pts != AV_NOPTS_VALUE &&
01172 st->cur_pkt.dts != AV_NOPTS_VALUE &&
01173 st->cur_pkt.pts < st->cur_pkt.dts){
01174 av_log(s, AV_LOG_WARNING, "Invalid timestamps stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d\n",
01175 st->cur_pkt.stream_index,
01176 st->cur_pkt.pts,
01177 st->cur_pkt.dts,
01178 st->cur_pkt.size);
01179
01180
01181 }
01182
01183 if(s->debug & FF_FDEBUG_TS)
01184 av_log(s, AV_LOG_DEBUG, "av_read_packet stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d, duration=%d, flags=%d\n",
01185 st->cur_pkt.stream_index,
01186 st->cur_pkt.pts,
01187 st->cur_pkt.dts,
01188 st->cur_pkt.size,
01189 st->cur_pkt.duration,
01190 st->cur_pkt.flags);
01191
01192 s->cur_st = st;
01193 st->cur_ptr = st->cur_pkt.data;
01194 st->cur_len = st->cur_pkt.size;
01195 if (st->need_parsing && !st->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
01196 st->parser = av_parser_init(st->codec->codec_id);
01197 if (!st->parser) {
01198
01199 st->need_parsing = AVSTREAM_PARSE_NONE;
01200 }else if(st->need_parsing == AVSTREAM_PARSE_HEADERS){
01201 st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
01202 }else if(st->need_parsing == AVSTREAM_PARSE_FULL_ONCE){
01203 st->parser->flags |= PARSER_FLAG_ONCE;
01204 }
01205 }
01206 }
01207 }
01208 if(s->debug & FF_FDEBUG_TS)
01209 av_log(s, AV_LOG_DEBUG, "av_read_frame_internal stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d, duration=%d, flags=%d\n",
01210 pkt->stream_index,
01211 pkt->pts,
01212 pkt->dts,
01213 pkt->size,
01214 pkt->duration,
01215 pkt->flags);
01216
01217 return 0;
01218 }
01219
01220 int av_read_frame(AVFormatContext *s, AVPacket *pkt)
01221 {
01222 AVPacketList *pktl;
01223 int eof=0;
01224 const int genpts= s->flags & AVFMT_FLAG_GENPTS;
01225
01226 for(;;){
01227 pktl = s->packet_buffer;
01228 if (pktl) {
01229 AVPacket *next_pkt= &pktl->pkt;
01230
01231 if(genpts && next_pkt->dts != AV_NOPTS_VALUE){
01232 int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
01233 while(pktl && next_pkt->pts == AV_NOPTS_VALUE){
01234 if( pktl->pkt.stream_index == next_pkt->stream_index
01235 && (0 > av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2LL << (wrap_bits - 1)))
01236 && av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2LL << (wrap_bits - 1))) {
01237 next_pkt->pts= pktl->pkt.dts;
01238 }
01239 pktl= pktl->next;
01240 }
01241 pktl = s->packet_buffer;
01242 }
01243
01244 if( next_pkt->pts != AV_NOPTS_VALUE
01245 || next_pkt->dts == AV_NOPTS_VALUE
01246 || !genpts || eof){
01247
01248 *pkt = *next_pkt;
01249 s->packet_buffer = pktl->next;
01250 av_free(pktl);
01251 return 0;
01252 }
01253 }
01254 if(genpts){
01255 int ret= av_read_frame_internal(s, pkt);
01256 if(ret<0){
01257 if(pktl && ret != AVERROR(EAGAIN)){
01258 eof=1;
01259 continue;
01260 }else
01261 return ret;
01262 }
01263
01264 if(av_dup_packet(add_to_pktbuf(&s->packet_buffer, pkt,
01265 &s->packet_buffer_end)) < 0)
01266 return AVERROR(ENOMEM);
01267 }else{
01268 assert(!s->packet_buffer);
01269 return av_read_frame_internal(s, pkt);
01270 }
01271 }
01272 }
01273
01274
01275 static void flush_packet_queue(AVFormatContext *s)
01276 {
01277 AVPacketList *pktl;
01278
01279 for(;;) {
01280 pktl = s->packet_buffer;
01281 if (!pktl)
01282 break;
01283 s->packet_buffer = pktl->next;
01284 av_free_packet(&pktl->pkt);
01285 av_free(pktl);
01286 }
01287 while(s->raw_packet_buffer){
01288 pktl = s->raw_packet_buffer;
01289 s->raw_packet_buffer = pktl->next;
01290 av_free_packet(&pktl->pkt);
01291 av_free(pktl);
01292 }
01293 s->packet_buffer_end=
01294 s->raw_packet_buffer_end= NULL;
01295 s->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
01296 }
01297
01298
01299
01300
01301 int av_find_default_stream_index(AVFormatContext *s)
01302 {
01303 int first_audio_index = -1;
01304 int i;
01305 AVStream *st;
01306
01307 if (s->nb_streams <= 0)
01308 return -1;
01309 for(i = 0; i < s->nb_streams; i++) {
01310 st = s->streams[i];
01311 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
01312 return i;
01313 }
01314 if (first_audio_index < 0 && st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
01315 first_audio_index = i;
01316 }
01317 return first_audio_index >= 0 ? first_audio_index : 0;
01318 }
01319
01323 void ff_read_frame_flush(AVFormatContext *s)
01324 {
01325 AVStream *st;
01326 int i, j;
01327
01328 flush_packet_queue(s);
01329
01330 s->cur_st = NULL;
01331
01332
01333 for(i = 0; i < s->nb_streams; i++) {
01334 st = s->streams[i];
01335
01336 if (st->parser) {
01337 av_parser_close(st->parser);
01338 st->parser = NULL;
01339 av_free_packet(&st->cur_pkt);
01340 }
01341 st->last_IP_pts = AV_NOPTS_VALUE;
01342 st->cur_dts = AV_NOPTS_VALUE;
01343 st->reference_dts = AV_NOPTS_VALUE;
01344
01345 st->cur_ptr = NULL;
01346 st->cur_len = 0;
01347
01348 st->probe_packets = MAX_PROBE_PACKETS;
01349
01350 for(j=0; j<MAX_REORDER_DELAY+1; j++)
01351 st->pts_buffer[j]= AV_NOPTS_VALUE;
01352 }
01353 }
01354
01355 void av_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp){
01356 int i;
01357
01358 for(i = 0; i < s->nb_streams; i++) {
01359 AVStream *st = s->streams[i];
01360
01361 st->cur_dts = av_rescale(timestamp,
01362 st->time_base.den * (int64_t)ref_st->time_base.num,
01363 st->time_base.num * (int64_t)ref_st->time_base.den);
01364 }
01365 }
01366
01367 void ff_reduce_index(AVFormatContext *s, int stream_index)
01368 {
01369 AVStream *st= s->streams[stream_index];
01370 unsigned int max_entries= s->max_index_size / sizeof(AVIndexEntry);
01371
01372 if((unsigned)st->nb_index_entries >= max_entries){
01373 int i;
01374 for(i=0; 2*i<st->nb_index_entries; i++)
01375 st->index_entries[i]= st->index_entries[2*i];
01376 st->nb_index_entries= i;
01377 }
01378 }
01379
01380 int ff_add_index_entry(AVIndexEntry **index_entries,
01381 int *nb_index_entries,
01382 unsigned int *index_entries_allocated_size,
01383 int64_t pos, int64_t timestamp, int size, int distance, int flags)
01384 {
01385 AVIndexEntry *entries, *ie;
01386 int index;
01387
01388 if((unsigned)*nb_index_entries + 1 >= UINT_MAX / sizeof(AVIndexEntry))
01389 return -1;
01390
01391 entries = av_fast_realloc(*index_entries,
01392 index_entries_allocated_size,
01393 (*nb_index_entries + 1) *
01394 sizeof(AVIndexEntry));
01395 if(!entries)
01396 return -1;
01397
01398 *index_entries= entries;
01399
01400 index= ff_index_search_timestamp(*index_entries, *nb_index_entries, timestamp, AVSEEK_FLAG_ANY);
01401
01402 if(index<0){
01403 index= (*nb_index_entries)++;
01404 ie= &entries[index];
01405 assert(index==0 || ie[-1].timestamp < timestamp);
01406 }else{
01407 ie= &entries[index];
01408 if(ie->timestamp != timestamp){
01409 if(ie->timestamp <= timestamp)
01410 return -1;
01411 memmove(entries + index + 1, entries + index, sizeof(AVIndexEntry)*(*nb_index_entries - index));
01412 (*nb_index_entries)++;
01413 }else if(ie->pos == pos && distance < ie->min_distance)
01414 distance= ie->min_distance;
01415 }
01416
01417 ie->pos = pos;
01418 ie->timestamp = timestamp;
01419 ie->min_distance= distance;
01420 ie->size= size;
01421 ie->flags = flags;
01422
01423 return index;
01424 }
01425
01426 int av_add_index_entry(AVStream *st,
01427 int64_t pos, int64_t timestamp, int size, int distance, int flags)
01428 {
01429 return ff_add_index_entry(&st->index_entries, &st->nb_index_entries,
01430 &st->index_entries_allocated_size, pos,
01431 timestamp, size, distance, flags);
01432 }
01433
01434 int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries,
01435 int64_t wanted_timestamp, int flags)
01436 {
01437 int a, b, m;
01438 int64_t timestamp;
01439
01440 a = - 1;
01441 b = nb_entries;
01442
01443
01444 if(b && entries[b-1].timestamp < wanted_timestamp)
01445 a= b-1;
01446
01447 while (b - a > 1) {
01448 m = (a + b) >> 1;
01449 timestamp = entries[m].timestamp;
01450 if(timestamp >= wanted_timestamp)
01451 b = m;
01452 if(timestamp <= wanted_timestamp)
01453 a = m;
01454 }
01455 m= (flags & AVSEEK_FLAG_BACKWARD) ? a : b;
01456
01457 if(!(flags & AVSEEK_FLAG_ANY)){
01458 while(m>=0 && m<nb_entries && !(entries[m].flags & AVINDEX_KEYFRAME)){
01459 m += (flags & AVSEEK_FLAG_BACKWARD) ? -1 : 1;
01460 }
01461 }
01462
01463 if(m == nb_entries)
01464 return -1;
01465 return m;
01466 }
01467
01468 int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp,
01469 int flags)
01470 {
01471 return ff_index_search_timestamp(st->index_entries, st->nb_index_entries,
01472 wanted_timestamp, flags);
01473 }
01474
01475 #define DEBUG_SEEK
01476
01477 int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
01478 AVInputFormat *avif= s->iformat;
01479 int64_t av_uninit(pos_min), av_uninit(pos_max), pos, pos_limit;
01480 int64_t ts_min, ts_max, ts;
01481 int index;
01482 int64_t ret;
01483 AVStream *st;
01484
01485 if (stream_index < 0)
01486 return -1;
01487
01488 #ifdef DEBUG_SEEK
01489 av_log(s, AV_LOG_DEBUG, "read_seek: %d %"PRId64"\n", stream_index, target_ts);
01490 #endif
01491
01492 ts_max=
01493 ts_min= AV_NOPTS_VALUE;
01494 pos_limit= -1;
01495
01496 st= s->streams[stream_index];
01497 if(st->index_entries){
01498 AVIndexEntry *e;
01499
01500 index= av_index_search_timestamp(st, target_ts, flags | AVSEEK_FLAG_BACKWARD);
01501 index= FFMAX(index, 0);
01502 e= &st->index_entries[index];
01503
01504 if(e->timestamp <= target_ts || e->pos == e->min_distance){
01505 pos_min= e->pos;
01506 ts_min= e->timestamp;
01507 #ifdef DEBUG_SEEK
01508 av_log(s, AV_LOG_DEBUG, "using cached pos_min=0x%"PRIx64" dts_min=%"PRId64"\n",
01509 pos_min,ts_min);
01510 #endif
01511 }else{
01512 assert(index==0);
01513 }
01514
01515 index= av_index_search_timestamp(st, target_ts, flags & ~AVSEEK_FLAG_BACKWARD);
01516 assert(index < st->nb_index_entries);
01517 if(index >= 0){
01518 e= &st->index_entries[index];
01519 assert(e->timestamp >= target_ts);
01520 pos_max= e->pos;
01521 ts_max= e->timestamp;
01522 pos_limit= pos_max - e->min_distance;
01523 #ifdef DEBUG_SEEK
01524 av_log(s, AV_LOG_DEBUG, "using cached pos_max=0x%"PRIx64" pos_limit=0x%"PRIx64" dts_max=%"PRId64"\n",
01525 pos_max,pos_limit, ts_max);
01526 #endif
01527 }
01528 }
01529
01530 pos= av_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit, ts_min, ts_max, flags, &ts, avif->read_timestamp);
01531 if(pos<0)
01532 return -1;
01533
01534
01535 if ((ret = avio_seek(s->pb, pos, SEEK_SET)) < 0)
01536 return ret;
01537
01538 av_update_cur_dts(s, st, ts);
01539
01540 return 0;
01541 }
01542
01543 int64_t av_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts, int64_t pos_min, int64_t pos_max, int64_t pos_limit, int64_t ts_min, int64_t ts_max, int flags, int64_t *ts_ret, int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t )){
01544 int64_t pos, ts;
01545 int64_t start_pos, filesize;
01546 int no_change;
01547
01548 #ifdef DEBUG_SEEK
01549 av_log(s, AV_LOG_DEBUG, "gen_seek: %d %"PRId64"\n", stream_index, target_ts);
01550 #endif
01551
01552 if(ts_min == AV_NOPTS_VALUE){
01553 pos_min = s->data_offset;
01554 ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
01555 if (ts_min == AV_NOPTS_VALUE)
01556 return -1;
01557 }
01558
01559 if(ts_max == AV_NOPTS_VALUE){
01560 int step= 1024;
01561 filesize = avio_size(s->pb);
01562 pos_max = filesize - 1;
01563 do{
01564 pos_max -= step;
01565 ts_max = read_timestamp(s, stream_index, &pos_max, pos_max + step);
01566 step += step;
01567 }while(ts_max == AV_NOPTS_VALUE && pos_max >= step);
01568 if (ts_max == AV_NOPTS_VALUE)
01569 return -1;
01570
01571 for(;;){
01572 int64_t tmp_pos= pos_max + 1;
01573 int64_t tmp_ts= read_timestamp(s, stream_index, &tmp_pos, INT64_MAX);
01574 if(tmp_ts == AV_NOPTS_VALUE)
01575 break;
01576 ts_max= tmp_ts;
01577 pos_max= tmp_pos;
01578 if(tmp_pos >= filesize)
01579 break;
01580 }
01581 pos_limit= pos_max;
01582 }
01583
01584 if(ts_min > ts_max){
01585 return -1;
01586 }else if(ts_min == ts_max){
01587 pos_limit= pos_min;
01588 }
01589
01590 no_change=0;
01591 while (pos_min < pos_limit) {
01592 #ifdef DEBUG_SEEK
01593 av_log(s, AV_LOG_DEBUG, "pos_min=0x%"PRIx64" pos_max=0x%"PRIx64" dts_min=%"PRId64" dts_max=%"PRId64"\n",
01594 pos_min, pos_max,
01595 ts_min, ts_max);
01596 #endif
01597 assert(pos_limit <= pos_max);
01598
01599 if(no_change==0){
01600 int64_t approximate_keyframe_distance= pos_max - pos_limit;
01601
01602 pos = av_rescale(target_ts - ts_min, pos_max - pos_min, ts_max - ts_min)
01603 + pos_min - approximate_keyframe_distance;
01604 }else if(no_change==1){
01605
01606 pos = (pos_min + pos_limit)>>1;
01607 }else{
01608
01609
01610 pos=pos_min;
01611 }
01612 if(pos <= pos_min)
01613 pos= pos_min + 1;
01614 else if(pos > pos_limit)
01615 pos= pos_limit;
01616 start_pos= pos;
01617
01618 ts = read_timestamp(s, stream_index, &pos, INT64_MAX);
01619 if(pos == pos_max)
01620 no_change++;
01621 else
01622 no_change=0;
01623 #ifdef DEBUG_SEEK
01624 av_log(s, AV_LOG_DEBUG, "%"PRId64" %"PRId64" %"PRId64" / %"PRId64" %"PRId64" %"PRId64" target:%"PRId64" limit:%"PRId64" start:%"PRId64" noc:%d\n",
01625 pos_min, pos, pos_max, ts_min, ts, ts_max, target_ts, pos_limit,
01626 start_pos, no_change);
01627 #endif
01628 if(ts == AV_NOPTS_VALUE){
01629 av_log(s, AV_LOG_ERROR, "read_timestamp() failed in the middle\n");
01630 return -1;
01631 }
01632 assert(ts != AV_NOPTS_VALUE);
01633 if (target_ts <= ts) {
01634 pos_limit = start_pos - 1;
01635 pos_max = pos;
01636 ts_max = ts;
01637 }
01638 if (target_ts >= ts) {
01639 pos_min = pos;
01640 ts_min = ts;
01641 }
01642 }
01643
01644 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
01645 ts = (flags & AVSEEK_FLAG_BACKWARD) ? ts_min : ts_max;
01646 #ifdef DEBUG_SEEK
01647 pos_min = pos;
01648 ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
01649 pos_min++;
01650 ts_max = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
01651 av_log(s, AV_LOG_DEBUG, "pos=0x%"PRIx64" %"PRId64"<=%"PRId64"<=%"PRId64"\n",
01652 pos, ts_min, target_ts, ts_max);
01653 #endif
01654 *ts_ret= ts;
01655 return pos;
01656 }
01657
01658 static int av_seek_frame_byte(AVFormatContext *s, int stream_index, int64_t pos, int flags){
01659 int64_t pos_min, pos_max;
01660 #if 0
01661 AVStream *st;
01662
01663 if (stream_index < 0)
01664 return -1;
01665
01666 st= s->streams[stream_index];
01667 #endif
01668
01669 pos_min = s->data_offset;
01670 pos_max = avio_size(s->pb) - 1;
01671
01672 if (pos < pos_min) pos= pos_min;
01673 else if(pos > pos_max) pos= pos_max;
01674
01675 avio_seek(s->pb, pos, SEEK_SET);
01676
01677 #if 0
01678 av_update_cur_dts(s, st, ts);
01679 #endif
01680 return 0;
01681 }
01682
01683 static int av_seek_frame_generic(AVFormatContext *s,
01684 int stream_index, int64_t timestamp, int flags)
01685 {
01686 int index;
01687 int64_t ret;
01688 AVStream *st;
01689 AVIndexEntry *ie;
01690
01691 st = s->streams[stream_index];
01692
01693 index = av_index_search_timestamp(st, timestamp, flags);
01694
01695 if(index < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
01696 return -1;
01697
01698 if(index < 0 || index==st->nb_index_entries-1){
01699 int i;
01700 AVPacket pkt;
01701
01702 if(st->nb_index_entries){
01703 assert(st->index_entries);
01704 ie= &st->index_entries[st->nb_index_entries-1];
01705 if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
01706 return ret;
01707 av_update_cur_dts(s, st, ie->timestamp);
01708 }else{
01709 if ((ret = avio_seek(s->pb, s->data_offset, SEEK_SET)) < 0)
01710 return ret;
01711 }
01712 for(i=0;; i++) {
01713 int ret;
01714 do{
01715 ret = av_read_frame(s, &pkt);
01716 }while(ret == AVERROR(EAGAIN));
01717 if(ret<0)
01718 break;
01719 av_free_packet(&pkt);
01720 if(stream_index == pkt.stream_index){
01721 if((pkt.flags & AV_PKT_FLAG_KEY) && pkt.dts > timestamp)
01722 break;
01723 }
01724 }
01725 index = av_index_search_timestamp(st, timestamp, flags);
01726 }
01727 if (index < 0)
01728 return -1;
01729
01730 ff_read_frame_flush(s);
01731 if (s->iformat->read_seek){
01732 if(s->iformat->read_seek(s, stream_index, timestamp, flags) >= 0)
01733 return 0;
01734 }
01735 ie = &st->index_entries[index];
01736 if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
01737 return ret;
01738 av_update_cur_dts(s, st, ie->timestamp);
01739
01740 return 0;
01741 }
01742
01743 int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
01744 {
01745 int ret;
01746 AVStream *st;
01747
01748 ff_read_frame_flush(s);
01749
01750 if(flags & AVSEEK_FLAG_BYTE)
01751 return av_seek_frame_byte(s, stream_index, timestamp, flags);
01752
01753 if(stream_index < 0){
01754 stream_index= av_find_default_stream_index(s);
01755 if(stream_index < 0)
01756 return -1;
01757
01758 st= s->streams[stream_index];
01759
01760 timestamp = av_rescale(timestamp, st->time_base.den, AV_TIME_BASE * (int64_t)st->time_base.num);
01761 }
01762
01763
01764 if (s->iformat->read_seek)
01765 ret = s->iformat->read_seek(s, stream_index, timestamp, flags);
01766 else
01767 ret = -1;
01768 if (ret >= 0) {
01769 return 0;
01770 }
01771
01772 if(s->iformat->read_timestamp)
01773 return av_seek_frame_binary(s, stream_index, timestamp, flags);
01774 else
01775 return av_seek_frame_generic(s, stream_index, timestamp, flags);
01776 }
01777
01778 int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
01779 {
01780 if(min_ts > ts || max_ts < ts)
01781 return -1;
01782
01783 ff_read_frame_flush(s);
01784
01785 if (s->iformat->read_seek2)
01786 return s->iformat->read_seek2(s, stream_index, min_ts, ts, max_ts, flags);
01787
01788 if(s->iformat->read_timestamp){
01789
01790 }
01791
01792
01793
01794 if(s->iformat->read_seek || 1)
01795 return av_seek_frame(s, stream_index, ts, flags | (ts - min_ts > (uint64_t)(max_ts - ts) ? AVSEEK_FLAG_BACKWARD : 0));
01796
01797
01798 }
01799
01800
01801
01807 static int av_has_duration(AVFormatContext *ic)
01808 {
01809 int i;
01810 AVStream *st;
01811
01812 for(i = 0;i < ic->nb_streams; i++) {
01813 st = ic->streams[i];
01814 if (st->duration != AV_NOPTS_VALUE)
01815 return 1;
01816 }
01817 return 0;
01818 }
01819
01825 static void av_update_stream_timings(AVFormatContext *ic)
01826 {
01827 int64_t start_time, start_time1, end_time, end_time1;
01828 int64_t duration, duration1;
01829 int i;
01830 AVStream *st;
01831
01832 start_time = INT64_MAX;
01833 end_time = INT64_MIN;
01834 duration = INT64_MIN;
01835 for(i = 0;i < ic->nb_streams; i++) {
01836 st = ic->streams[i];
01837 if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
01838 start_time1= av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q);
01839 if (start_time1 < start_time)
01840 start_time = start_time1;
01841 if (st->duration != AV_NOPTS_VALUE) {
01842 end_time1 = start_time1
01843 + av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
01844 if (end_time1 > end_time)
01845 end_time = end_time1;
01846 }
01847 }
01848 if (st->duration != AV_NOPTS_VALUE) {
01849 duration1 = av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
01850 if (duration1 > duration)
01851 duration = duration1;
01852 }
01853 }
01854 if (start_time != INT64_MAX) {
01855 ic->start_time = start_time;
01856 if (end_time != INT64_MIN) {
01857 if (end_time - start_time > duration)
01858 duration = end_time - start_time;
01859 }
01860 }
01861 if (duration != INT64_MIN) {
01862 ic->duration = duration;
01863 if (ic->file_size > 0) {
01864
01865 ic->bit_rate = (double)ic->file_size * 8.0 * AV_TIME_BASE /
01866 (double)ic->duration;
01867 }
01868 }
01869 }
01870
01871 static void fill_all_stream_timings(AVFormatContext *ic)
01872 {
01873 int i;
01874 AVStream *st;
01875
01876 av_update_stream_timings(ic);
01877 for(i = 0;i < ic->nb_streams; i++) {
01878 st = ic->streams[i];
01879 if (st->start_time == AV_NOPTS_VALUE) {
01880 if(ic->start_time != AV_NOPTS_VALUE)
01881 st->start_time = av_rescale_q(ic->start_time, AV_TIME_BASE_Q, st->time_base);
01882 if(ic->duration != AV_NOPTS_VALUE)
01883 st->duration = av_rescale_q(ic->duration, AV_TIME_BASE_Q, st->time_base);
01884 }
01885 }
01886 }
01887
01888 static void av_estimate_timings_from_bit_rate(AVFormatContext *ic)
01889 {
01890 int64_t filesize, duration;
01891 int bit_rate, i;
01892 AVStream *st;
01893
01894
01895 if (ic->bit_rate <= 0) {
01896 bit_rate = 0;
01897 for(i=0;i<ic->nb_streams;i++) {
01898 st = ic->streams[i];
01899 if (st->codec->bit_rate > 0)
01900 bit_rate += st->codec->bit_rate;
01901 }
01902 ic->bit_rate = bit_rate;
01903 }
01904
01905
01906 if (ic->duration == AV_NOPTS_VALUE &&
01907 ic->bit_rate != 0 &&
01908 ic->file_size != 0) {
01909 filesize = ic->file_size;
01910 if (filesize > 0) {
01911 for(i = 0; i < ic->nb_streams; i++) {
01912 st = ic->streams[i];
01913 duration= av_rescale(8*filesize, st->time_base.den, ic->bit_rate*(int64_t)st->time_base.num);
01914 if (st->duration == AV_NOPTS_VALUE)
01915 st->duration = duration;
01916 }
01917 }
01918 }
01919 }
01920
01921 #define DURATION_MAX_READ_SIZE 250000
01922 #define DURATION_MAX_RETRY 3
01923
01924
01925 static void av_estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
01926 {
01927 AVPacket pkt1, *pkt = &pkt1;
01928 AVStream *st;
01929 int read_size, i, ret;
01930 int64_t end_time;
01931 int64_t filesize, offset, duration;
01932 int retry=0;
01933
01934 ic->cur_st = NULL;
01935
01936
01937 flush_packet_queue(ic);
01938
01939 for (i=0; i<ic->nb_streams; i++) {
01940 st = ic->streams[i];
01941 if (st->start_time == AV_NOPTS_VALUE && st->first_dts == AV_NOPTS_VALUE)
01942 av_log(st->codec, AV_LOG_WARNING, "start time is not set in av_estimate_timings_from_pts\n");
01943
01944 if (st->parser) {
01945 av_parser_close(st->parser);
01946 st->parser= NULL;
01947 av_free_packet(&st->cur_pkt);
01948 }
01949 }
01950
01951
01952
01953 filesize = ic->file_size;
01954 end_time = AV_NOPTS_VALUE;
01955 do{
01956 offset = filesize - (DURATION_MAX_READ_SIZE<<retry);
01957 if (offset < 0)
01958 offset = 0;
01959
01960 avio_seek(ic->pb, offset, SEEK_SET);
01961 read_size = 0;
01962 for(;;) {
01963 if (read_size >= DURATION_MAX_READ_SIZE<<(FFMAX(retry-1,0)))
01964 break;
01965
01966 do{
01967 ret = av_read_packet(ic, pkt);
01968 }while(ret == AVERROR(EAGAIN));
01969 if (ret != 0)
01970 break;
01971 read_size += pkt->size;
01972 st = ic->streams[pkt->stream_index];
01973 if (pkt->pts != AV_NOPTS_VALUE &&
01974 (st->start_time != AV_NOPTS_VALUE ||
01975 st->first_dts != AV_NOPTS_VALUE)) {
01976 duration = end_time = pkt->pts;
01977 if (st->start_time != AV_NOPTS_VALUE) duration -= st->start_time;
01978 else duration -= st->first_dts;
01979 if (duration < 0)
01980 duration += 1LL<<st->pts_wrap_bits;
01981 if (duration > 0) {
01982 if (st->duration == AV_NOPTS_VALUE ||
01983 st->duration < duration)
01984 st->duration = duration;
01985 }
01986 }
01987 av_free_packet(pkt);
01988 }
01989 }while( end_time==AV_NOPTS_VALUE
01990 && filesize > (DURATION_MAX_READ_SIZE<<retry)
01991 && ++retry <= DURATION_MAX_RETRY);
01992
01993 fill_all_stream_timings(ic);
01994
01995 avio_seek(ic->pb, old_offset, SEEK_SET);
01996 for (i=0; i<ic->nb_streams; i++) {
01997 st= ic->streams[i];
01998 st->cur_dts= st->first_dts;
01999 st->last_IP_pts = AV_NOPTS_VALUE;
02000 }
02001 }
02002
02003 static void av_estimate_timings(AVFormatContext *ic, int64_t old_offset)
02004 {
02005 int64_t file_size;
02006
02007
02008 if (ic->iformat->flags & AVFMT_NOFILE) {
02009 file_size = 0;
02010 } else {
02011 file_size = avio_size(ic->pb);
02012 if (file_size < 0)
02013 file_size = 0;
02014 }
02015 ic->file_size = file_size;
02016
02017 if ((!strcmp(ic->iformat->name, "mpeg") ||
02018 !strcmp(ic->iformat->name, "mpegts")) &&
02019 file_size && !url_is_streamed(ic->pb)) {
02020
02021 av_estimate_timings_from_pts(ic, old_offset);
02022 } else if (av_has_duration(ic)) {
02023
02024
02025 fill_all_stream_timings(ic);
02026 } else {
02027 av_log(ic, AV_LOG_WARNING, "Estimating duration from bitrate, this may be inaccurate\n");
02028
02029 av_estimate_timings_from_bit_rate(ic);
02030 }
02031 av_update_stream_timings(ic);
02032
02033 #if 0
02034 {
02035 int i;
02036 AVStream *st;
02037 for(i = 0;i < ic->nb_streams; i++) {
02038 st = ic->streams[i];
02039 printf("%d: start_time: %0.3f duration: %0.3f\n",
02040 i, (double)st->start_time / AV_TIME_BASE,
02041 (double)st->duration / AV_TIME_BASE);
02042 }
02043 printf("stream: start_time: %0.3f duration: %0.3f bitrate=%d kb/s\n",
02044 (double)ic->start_time / AV_TIME_BASE,
02045 (double)ic->duration / AV_TIME_BASE,
02046 ic->bit_rate / 1000);
02047 }
02048 #endif
02049 }
02050
02051 static int has_codec_parameters(AVCodecContext *enc)
02052 {
02053 int val;
02054 switch(enc->codec_type) {
02055 case AVMEDIA_TYPE_AUDIO:
02056 val = enc->sample_rate && enc->channels && enc->sample_fmt != AV_SAMPLE_FMT_NONE;
02057 if(!enc->frame_size &&
02058 (enc->codec_id == CODEC_ID_VORBIS ||
02059 enc->codec_id == CODEC_ID_AAC ||
02060 enc->codec_id == CODEC_ID_MP1 ||
02061 enc->codec_id == CODEC_ID_MP2 ||
02062 enc->codec_id == CODEC_ID_MP3 ||
02063 enc->codec_id == CODEC_ID_SPEEX))
02064 return 0;
02065 break;
02066 case AVMEDIA_TYPE_VIDEO:
02067 val = enc->width && enc->pix_fmt != PIX_FMT_NONE;
02068 break;
02069 default:
02070 val = 1;
02071 break;
02072 }
02073 return enc->codec_id != CODEC_ID_NONE && val != 0;
02074 }
02075
02076 static int has_decode_delay_been_guessed(AVStream *st)
02077 {
02078 return st->codec->codec_id != CODEC_ID_H264 ||
02079 st->codec_info_nb_frames >= 6 + st->codec->has_b_frames;
02080 }
02081
02082 static int try_decode_frame(AVStream *st, AVPacket *avpkt)
02083 {
02084 int16_t *samples;
02085 AVCodec *codec;
02086 int got_picture, data_size, ret=0;
02087 AVFrame picture;
02088
02089 if(!st->codec->codec){
02090 codec = avcodec_find_decoder(st->codec->codec_id);
02091 if (!codec)
02092 return -1;
02093 ret = avcodec_open(st->codec, codec);
02094 if (ret < 0)
02095 return ret;
02096 }
02097
02098 if(!has_codec_parameters(st->codec) || !has_decode_delay_been_guessed(st)){
02099 switch(st->codec->codec_type) {
02100 case AVMEDIA_TYPE_VIDEO:
02101 avcodec_get_frame_defaults(&picture);
02102 ret = avcodec_decode_video2(st->codec, &picture,
02103 &got_picture, avpkt);
02104 break;
02105 case AVMEDIA_TYPE_AUDIO:
02106 data_size = FFMAX(avpkt->size, AVCODEC_MAX_AUDIO_FRAME_SIZE);
02107 samples = av_malloc(data_size);
02108 if (!samples)
02109 goto fail;
02110 ret = avcodec_decode_audio3(st->codec, samples,
02111 &data_size, avpkt);
02112 av_free(samples);
02113 break;
02114 default:
02115 break;
02116 }
02117 }
02118 fail:
02119 return ret;
02120 }
02121
02122 unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum CodecID id)
02123 {
02124 while (tags->id != CODEC_ID_NONE) {
02125 if (tags->id == id)
02126 return tags->tag;
02127 tags++;
02128 }
02129 return 0;
02130 }
02131
02132 enum CodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
02133 {
02134 int i;
02135 for(i=0; tags[i].id != CODEC_ID_NONE;i++) {
02136 if(tag == tags[i].tag)
02137 return tags[i].id;
02138 }
02139 for(i=0; tags[i].id != CODEC_ID_NONE; i++) {
02140 if (ff_toupper4(tag) == ff_toupper4(tags[i].tag))
02141 return tags[i].id;
02142 }
02143 return CODEC_ID_NONE;
02144 }
02145
02146 unsigned int av_codec_get_tag(const AVCodecTag * const *tags, enum CodecID id)
02147 {
02148 int i;
02149 for(i=0; tags && tags[i]; i++){
02150 int tag= ff_codec_get_tag(tags[i], id);
02151 if(tag) return tag;
02152 }
02153 return 0;
02154 }
02155
02156 enum CodecID av_codec_get_id(const AVCodecTag * const *tags, unsigned int tag)
02157 {
02158 int i;
02159 for(i=0; tags && tags[i]; i++){
02160 enum CodecID id= ff_codec_get_id(tags[i], tag);
02161 if(id!=CODEC_ID_NONE) return id;
02162 }
02163 return CODEC_ID_NONE;
02164 }
02165
02166 static void compute_chapters_end(AVFormatContext *s)
02167 {
02168 unsigned int i;
02169
02170 for (i=0; i+1<s->nb_chapters; i++)
02171 if (s->chapters[i]->end == AV_NOPTS_VALUE) {
02172 assert(s->chapters[i]->start <= s->chapters[i+1]->start);
02173 assert(!av_cmp_q(s->chapters[i]->time_base, s->chapters[i+1]->time_base));
02174 s->chapters[i]->end = s->chapters[i+1]->start;
02175 }
02176
02177 if (s->nb_chapters && s->chapters[i]->end == AV_NOPTS_VALUE) {
02178 assert(s->start_time != AV_NOPTS_VALUE);
02179 assert(s->duration > 0);
02180 s->chapters[i]->end = av_rescale_q(s->start_time + s->duration,
02181 AV_TIME_BASE_Q,
02182 s->chapters[i]->time_base);
02183 }
02184 }
02185
02186 static int get_std_framerate(int i){
02187 if(i<60*12) return i*1001;
02188 else return ((const int[]){24,30,60,12,15})[i-60*12]*1000*12;
02189 }
02190
02191
02192
02193
02194
02195
02196
02197
02198
02199 static int tb_unreliable(AVCodecContext *c){
02200 if( c->time_base.den >= 101L*c->time_base.num
02201 || c->time_base.den < 5L*c->time_base.num
02202
02203
02204 || c->codec_id == CODEC_ID_MPEG2VIDEO
02205 || c->codec_id == CODEC_ID_H264
02206 )
02207 return 1;
02208 return 0;
02209 }
02210
02211 int av_find_stream_info(AVFormatContext *ic)
02212 {
02213 int i, count, ret, read_size, j;
02214 AVStream *st;
02215 AVPacket pkt1, *pkt;
02216 int64_t old_offset = avio_tell(ic->pb);
02217
02218 for(i=0;i<ic->nb_streams;i++) {
02219 AVCodec *codec;
02220 st = ic->streams[i];
02221 if (st->codec->codec_id == CODEC_ID_AAC) {
02222 st->codec->sample_rate = 0;
02223 st->codec->frame_size = 0;
02224 st->codec->channels = 0;
02225 }
02226 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
02227 st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
02228
02229
02230 if(!st->codec->time_base.num)
02231 st->codec->time_base= st->time_base;
02232 }
02233
02234 if (!st->parser && !(ic->flags & AVFMT_FLAG_NOPARSE)) {
02235 st->parser = av_parser_init(st->codec->codec_id);
02236 if(st->need_parsing == AVSTREAM_PARSE_HEADERS && st->parser){
02237 st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
02238 }
02239 }
02240 assert(!st->codec->codec);
02241 codec = avcodec_find_decoder(st->codec->codec_id);
02242
02243
02244
02245
02246
02247 if (codec && codec->capabilities & CODEC_CAP_CHANNEL_CONF)
02248 st->codec->channels = 0;
02249
02250
02251 if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE
02252 && codec && !st->codec->codec)
02253 avcodec_open(st->codec, codec);
02254
02255
02256 if(!has_codec_parameters(st->codec)){
02257 if (codec && !st->codec->codec)
02258 avcodec_open(st->codec, codec);
02259 }
02260 }
02261
02262 for (i=0; i<ic->nb_streams; i++) {
02263 ic->streams[i]->info->last_dts = AV_NOPTS_VALUE;
02264 }
02265
02266 count = 0;
02267 read_size = 0;
02268 for(;;) {
02269 if(url_interrupt_cb()){
02270 ret= AVERROR(EINTR);
02271 av_log(ic, AV_LOG_DEBUG, "interrupted\n");
02272 break;
02273 }
02274
02275
02276 for(i=0;i<ic->nb_streams;i++) {
02277 st = ic->streams[i];
02278 if (!has_codec_parameters(st->codec))
02279 break;
02280
02281 if( tb_unreliable(st->codec) && !(st->r_frame_rate.num && st->avg_frame_rate.num)
02282 && st->info->duration_count<20 && st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
02283 break;
02284 if(st->parser && st->parser->parser->split && !st->codec->extradata)
02285 break;
02286 if(st->first_dts == AV_NOPTS_VALUE)
02287 break;
02288 }
02289 if (i == ic->nb_streams) {
02290
02291
02292
02293 if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
02294
02295 ret = count;
02296 av_log(ic, AV_LOG_DEBUG, "All info found\n");
02297 break;
02298 }
02299 }
02300
02301 if (read_size >= ic->probesize) {
02302 ret = count;
02303 av_log(ic, AV_LOG_DEBUG, "Probe buffer size limit %d reached\n", ic->probesize);
02304 break;
02305 }
02306
02307
02308
02309 ret = av_read_frame_internal(ic, &pkt1);
02310 if (ret < 0 && ret != AVERROR(EAGAIN)) {
02311
02312 ret = -1;
02313 for(i=0;i<ic->nb_streams;i++) {
02314 st = ic->streams[i];
02315 if (!has_codec_parameters(st->codec)){
02316 char buf[256];
02317 avcodec_string(buf, sizeof(buf), st->codec, 0);
02318 av_log(ic, AV_LOG_WARNING, "Could not find codec parameters (%s)\n", buf);
02319 } else {
02320 ret = 0;
02321 }
02322 }
02323 break;
02324 }
02325
02326 if (ret == AVERROR(EAGAIN))
02327 continue;
02328
02329 pkt= add_to_pktbuf(&ic->packet_buffer, &pkt1, &ic->packet_buffer_end);
02330 if ((ret = av_dup_packet(pkt)) < 0)
02331 goto find_stream_info_err;
02332
02333 read_size += pkt->size;
02334
02335 st = ic->streams[pkt->stream_index];
02336 if (st->codec_info_nb_frames>1) {
02337 if (st->time_base.den > 0 && av_rescale_q(st->info->codec_info_duration, st->time_base, AV_TIME_BASE_Q) >= ic->max_analyze_duration) {
02338 av_log(ic, AV_LOG_WARNING, "max_analyze_duration reached\n");
02339 break;
02340 }
02341 st->info->codec_info_duration += pkt->duration;
02342 }
02343 {
02344 int64_t last = st->info->last_dts;
02345 int64_t duration= pkt->dts - last;
02346
02347 if(pkt->dts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && duration>0){
02348 double dur= duration * av_q2d(st->time_base);
02349
02350
02351
02352 if (st->info->duration_count < 2)
02353 memset(st->info->duration_error, 0, sizeof(st->info->duration_error));
02354 for (i=1; i<FF_ARRAY_ELEMS(st->info->duration_error); i++) {
02355 int framerate= get_std_framerate(i);
02356 int ticks= lrintf(dur*framerate/(1001*12));
02357 double error= dur - ticks*1001*12/(double)framerate;
02358 st->info->duration_error[i] += error*error;
02359 }
02360 st->info->duration_count++;
02361
02362 if (st->info->duration_count > 3)
02363 st->info->duration_gcd = av_gcd(st->info->duration_gcd, duration);
02364 }
02365 if (last == AV_NOPTS_VALUE || st->info->duration_count <= 1)
02366 st->info->last_dts = pkt->dts;
02367 }
02368 if(st->parser && st->parser->parser->split && !st->codec->extradata){
02369 int i= st->parser->parser->split(st->codec, pkt->data, pkt->size);
02370 if(i){
02371 st->codec->extradata_size= i;
02372 st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
02373 memcpy(st->codec->extradata, pkt->data, st->codec->extradata_size);
02374 memset(st->codec->extradata + i, 0, FF_INPUT_BUFFER_PADDING_SIZE);
02375 }
02376 }
02377
02378
02379
02380
02381
02382 if (!has_codec_parameters(st->codec) || !has_decode_delay_been_guessed(st))
02383 try_decode_frame(st, pkt);
02384
02385 st->codec_info_nb_frames++;
02386 count++;
02387 }
02388
02389
02390 for(i=0;i<ic->nb_streams;i++) {
02391 st = ic->streams[i];
02392 if(st->codec->codec)
02393 avcodec_close(st->codec);
02394 }
02395 for(i=0;i<ic->nb_streams;i++) {
02396 st = ic->streams[i];
02397 if (st->codec_info_nb_frames>2 && !st->avg_frame_rate.num && st->info->codec_info_duration)
02398 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
02399 (st->codec_info_nb_frames-2)*(int64_t)st->time_base.den,
02400 st->info->codec_info_duration*(int64_t)st->time_base.num, 60000);
02401 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
02402 if(st->codec->codec_id == CODEC_ID_RAWVIDEO && !st->codec->codec_tag && !st->codec->bits_per_coded_sample)
02403 st->codec->codec_tag= avcodec_pix_fmt_to_codec_tag(st->codec->pix_fmt);
02404
02405
02406
02407
02408 if (tb_unreliable(st->codec) && st->info->duration_count > 15 && st->info->duration_gcd > 1 && !st->r_frame_rate.num)
02409 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, st->time_base.den, st->time_base.num * st->info->duration_gcd, INT_MAX);
02410 if (st->info->duration_count && !st->r_frame_rate.num
02411 && tb_unreliable(st->codec)
02412
02413 ){
02414 int num = 0;
02415 double best_error= 2*av_q2d(st->time_base);
02416 best_error = best_error*best_error*st->info->duration_count*1000*12*30;
02417
02418 for (j=1; j<FF_ARRAY_ELEMS(st->info->duration_error); j++) {
02419 double error = st->info->duration_error[j] * get_std_framerate(j);
02420
02421
02422 if(error < best_error){
02423 best_error= error;
02424 num = get_std_framerate(j);
02425 }
02426 }
02427
02428 if (num && (!st->r_frame_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(st->r_frame_rate)))
02429 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
02430 }
02431
02432 if (!st->r_frame_rate.num){
02433 if( st->codec->time_base.den * (int64_t)st->time_base.num
02434 <= st->codec->time_base.num * st->codec->ticks_per_frame * (int64_t)st->time_base.den){
02435 st->r_frame_rate.num = st->codec->time_base.den;
02436 st->r_frame_rate.den = st->codec->time_base.num * st->codec->ticks_per_frame;
02437 }else{
02438 st->r_frame_rate.num = st->time_base.den;
02439 st->r_frame_rate.den = st->time_base.num;
02440 }
02441 }
02442 }else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
02443 if(!st->codec->bits_per_coded_sample)
02444 st->codec->bits_per_coded_sample= av_get_bits_per_sample(st->codec->codec_id);
02445 }
02446 }
02447
02448 av_estimate_timings(ic, old_offset);
02449
02450 compute_chapters_end(ic);
02451
02452 #if 0
02453
02454 for(i=0;i<ic->nb_streams;i++) {
02455 st = ic->streams[i];
02456 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
02457 if(b-frames){
02458 ppktl = &ic->packet_buffer;
02459 while(ppkt1){
02460 if(ppkt1->stream_index != i)
02461 continue;
02462 if(ppkt1->pkt->dts < 0)
02463 break;
02464 if(ppkt1->pkt->pts != AV_NOPTS_VALUE)
02465 break;
02466 ppkt1->pkt->dts -= delta;
02467 ppkt1= ppkt1->next;
02468 }
02469 if(ppkt1)
02470 continue;
02471 st->cur_dts -= delta;
02472 }
02473 }
02474 }
02475 #endif
02476
02477 find_stream_info_err:
02478 for (i=0; i < ic->nb_streams; i++)
02479 av_freep(&ic->streams[i]->info);
02480 return ret;
02481 }
02482
02483 static AVProgram *find_program_from_stream(AVFormatContext *ic, int s)
02484 {
02485 int i, j;
02486
02487 for (i = 0; i < ic->nb_programs; i++)
02488 for (j = 0; j < ic->programs[i]->nb_stream_indexes; j++)
02489 if (ic->programs[i]->stream_index[j] == s)
02490 return ic->programs[i];
02491 return NULL;
02492 }
02493
02494 int av_find_best_stream(AVFormatContext *ic,
02495 enum AVMediaType type,
02496 int wanted_stream_nb,
02497 int related_stream,
02498 AVCodec **decoder_ret,
02499 int flags)
02500 {
02501 int i, nb_streams = ic->nb_streams, stream_number = 0;
02502 int ret = AVERROR_STREAM_NOT_FOUND, best_count = -1;
02503 unsigned *program = NULL;
02504 AVCodec *decoder = NULL, *best_decoder = NULL;
02505
02506 if (related_stream >= 0 && wanted_stream_nb < 0) {
02507 AVProgram *p = find_program_from_stream(ic, related_stream);
02508 if (p) {
02509 program = p->stream_index;
02510 nb_streams = p->nb_stream_indexes;
02511 }
02512 }
02513 for (i = 0; i < nb_streams; i++) {
02514 AVStream *st = ic->streams[program ? program[i] : i];
02515 AVCodecContext *avctx = st->codec;
02516 if (avctx->codec_type != type)
02517 continue;
02518 if (wanted_stream_nb >= 0 && stream_number++ != wanted_stream_nb)
02519 continue;
02520 if (st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED|AV_DISPOSITION_VISUAL_IMPAIRED))
02521 continue;
02522 if (decoder_ret) {
02523 decoder = avcodec_find_decoder(ic->streams[i]->codec->codec_id);
02524 if (!decoder) {
02525 if (ret < 0)
02526 ret = AVERROR_DECODER_NOT_FOUND;
02527 continue;
02528 }
02529 }
02530 if (best_count >= st->codec_info_nb_frames)
02531 continue;
02532 best_count = st->codec_info_nb_frames;
02533 ret = program ? program[i] : i;
02534 best_decoder = decoder;
02535 if (program && i == nb_streams - 1 && ret < 0) {
02536 program = NULL;
02537 nb_streams = ic->nb_streams;
02538 i = 0;
02539 }
02540 }
02541 if (decoder_ret)
02542 *decoder_ret = best_decoder;
02543 return ret;
02544 }
02545
02546
02547
02548 int av_read_play(AVFormatContext *s)
02549 {
02550 if (s->iformat->read_play)
02551 return s->iformat->read_play(s);
02552 if (s->pb)
02553 return av_url_read_fpause(s->pb, 0);
02554 return AVERROR(ENOSYS);
02555 }
02556
02557 int av_read_pause(AVFormatContext *s)
02558 {
02559 if (s->iformat->read_pause)
02560 return s->iformat->read_pause(s);
02561 if (s->pb)
02562 return av_url_read_fpause(s->pb, 1);
02563 return AVERROR(ENOSYS);
02564 }
02565
02566 void av_close_input_stream(AVFormatContext *s)
02567 {
02568 flush_packet_queue(s);
02569 if (s->iformat->read_close)
02570 s->iformat->read_close(s);
02571 avformat_free_context(s);
02572 }
02573
02574 void avformat_free_context(AVFormatContext *s)
02575 {
02576 int i;
02577 AVStream *st;
02578
02579 for(i=0;i<s->nb_streams;i++) {
02580
02581 st = s->streams[i];
02582 if (st->parser) {
02583 av_parser_close(st->parser);
02584 av_free_packet(&st->cur_pkt);
02585 }
02586 av_metadata_free(&st->metadata);
02587 av_free(st->index_entries);
02588 av_free(st->codec->extradata);
02589 av_free(st->codec->subtitle_header);
02590 av_free(st->codec);
02591 #if FF_API_OLD_METADATA
02592 av_free(st->filename);
02593 #endif
02594 av_free(st->priv_data);
02595 av_free(st->info);
02596 av_free(st);
02597 }
02598 for(i=s->nb_programs-1; i>=0; i--) {
02599 #if FF_API_OLD_METADATA
02600 av_freep(&s->programs[i]->provider_name);
02601 av_freep(&s->programs[i]->name);
02602 #endif
02603 av_metadata_free(&s->programs[i]->metadata);
02604 av_freep(&s->programs[i]->stream_index);
02605 av_freep(&s->programs[i]);
02606 }
02607 av_freep(&s->programs);
02608 av_freep(&s->priv_data);
02609 while(s->nb_chapters--) {
02610 #if FF_API_OLD_METADATA
02611 av_free(s->chapters[s->nb_chapters]->title);
02612 #endif
02613 av_metadata_free(&s->chapters[s->nb_chapters]->metadata);
02614 av_free(s->chapters[s->nb_chapters]);
02615 }
02616 av_freep(&s->chapters);
02617 av_metadata_free(&s->metadata);
02618 av_freep(&s->key);
02619 av_free(s);
02620 }
02621
02622 void av_close_input_file(AVFormatContext *s)
02623 {
02624 AVIOContext *pb = s->iformat->flags & AVFMT_NOFILE ? NULL : s->pb;
02625 av_close_input_stream(s);
02626 if (pb)
02627 avio_close(pb);
02628 }
02629
02630 AVStream *av_new_stream(AVFormatContext *s, int id)
02631 {
02632 AVStream *st;
02633 int i;
02634
02635 #if FF_API_MAX_STREAMS
02636 if (s->nb_streams >= MAX_STREAMS){
02637 av_log(s, AV_LOG_ERROR, "Too many streams\n");
02638 return NULL;
02639 }
02640 #else
02641 AVStream **streams;
02642
02643 if (s->nb_streams >= INT_MAX/sizeof(*streams))
02644 return NULL;
02645 streams = av_realloc(s->streams, (s->nb_streams + 1) * sizeof(*streams));
02646 if (!streams)
02647 return NULL;
02648 s->streams = streams;
02649 #endif
02650
02651 st = av_mallocz(sizeof(AVStream));
02652 if (!st)
02653 return NULL;
02654 if (!(st->info = av_mallocz(sizeof(*st->info)))) {
02655 av_free(st);
02656 return NULL;
02657 }
02658
02659 st->codec= avcodec_alloc_context();
02660 if (s->iformat) {
02661
02662 st->codec->bit_rate = 0;
02663 }
02664 st->index = s->nb_streams;
02665 st->id = id;
02666 st->start_time = AV_NOPTS_VALUE;
02667 st->duration = AV_NOPTS_VALUE;
02668
02669
02670
02671
02672 st->cur_dts = 0;
02673 st->first_dts = AV_NOPTS_VALUE;
02674 st->probe_packets = MAX_PROBE_PACKETS;
02675
02676
02677 av_set_pts_info(st, 33, 1, 90000);
02678 st->last_IP_pts = AV_NOPTS_VALUE;
02679 for(i=0; i<MAX_REORDER_DELAY+1; i++)
02680 st->pts_buffer[i]= AV_NOPTS_VALUE;
02681 st->reference_dts = AV_NOPTS_VALUE;
02682
02683 st->sample_aspect_ratio = (AVRational){0,1};
02684
02685 s->streams[s->nb_streams++] = st;
02686 return st;
02687 }
02688
02689 AVProgram *av_new_program(AVFormatContext *ac, int id)
02690 {
02691 AVProgram *program=NULL;
02692 int i;
02693
02694 #ifdef DEBUG_SI
02695 av_log(ac, AV_LOG_DEBUG, "new_program: id=0x%04x\n", id);
02696 #endif
02697
02698 for(i=0; i<ac->nb_programs; i++)
02699 if(ac->programs[i]->id == id)
02700 program = ac->programs[i];
02701
02702 if(!program){
02703 program = av_mallocz(sizeof(AVProgram));
02704 if (!program)
02705 return NULL;
02706 dynarray_add(&ac->programs, &ac->nb_programs, program);
02707 program->discard = AVDISCARD_NONE;
02708 }
02709 program->id = id;
02710
02711 return program;
02712 }
02713
02714 AVChapter *ff_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
02715 {
02716 AVChapter *chapter = NULL;
02717 int i;
02718
02719 for(i=0; i<s->nb_chapters; i++)
02720 if(s->chapters[i]->id == id)
02721 chapter = s->chapters[i];
02722
02723 if(!chapter){
02724 chapter= av_mallocz(sizeof(AVChapter));
02725 if(!chapter)
02726 return NULL;
02727 dynarray_add(&s->chapters, &s->nb_chapters, chapter);
02728 }
02729 #if FF_API_OLD_METADATA
02730 av_free(chapter->title);
02731 #endif
02732 av_metadata_set2(&chapter->metadata, "title", title, 0);
02733 chapter->id = id;
02734 chapter->time_base= time_base;
02735 chapter->start = start;
02736 chapter->end = end;
02737
02738 return chapter;
02739 }
02740
02741
02742
02743
02744 int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
02745 {
02746 int ret;
02747
02748 if (s->oformat->priv_data_size > 0) {
02749 s->priv_data = av_mallocz(s->oformat->priv_data_size);
02750 if (!s->priv_data)
02751 return AVERROR(ENOMEM);
02752 if (s->oformat->priv_class) {
02753 *(const AVClass**)s->priv_data= s->oformat->priv_class;
02754 av_opt_set_defaults(s->priv_data);
02755 }
02756 } else
02757 s->priv_data = NULL;
02758
02759 if (s->oformat->set_parameters) {
02760 ret = s->oformat->set_parameters(s, ap);
02761 if (ret < 0)
02762 return ret;
02763 }
02764 return 0;
02765 }
02766
02767 static int validate_codec_tag(AVFormatContext *s, AVStream *st)
02768 {
02769 const AVCodecTag *avctag;
02770 int n;
02771 enum CodecID id = CODEC_ID_NONE;
02772 unsigned int tag = 0;
02773
02780 for (n = 0; s->oformat->codec_tag[n]; n++) {
02781 avctag = s->oformat->codec_tag[n];
02782 while (avctag->id != CODEC_ID_NONE) {
02783 if (ff_toupper4(avctag->tag) == ff_toupper4(st->codec->codec_tag)) {
02784 id = avctag->id;
02785 if (id == st->codec->codec_id)
02786 return 1;
02787 }
02788 if (avctag->id == st->codec->codec_id)
02789 tag = avctag->tag;
02790 avctag++;
02791 }
02792 }
02793 if (id != CODEC_ID_NONE)
02794 return 0;
02795 if (tag && (st->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
02796 return 0;
02797 return 1;
02798 }
02799
02800 int av_write_header(AVFormatContext *s)
02801 {
02802 int ret, i;
02803 AVStream *st;
02804
02805
02806 if (s->nb_streams == 0 && !(s->oformat->flags & AVFMT_NOSTREAMS)) {
02807 av_log(s, AV_LOG_ERROR, "no streams\n");
02808 return AVERROR(EINVAL);
02809 }
02810
02811 for(i=0;i<s->nb_streams;i++) {
02812 st = s->streams[i];
02813
02814 switch (st->codec->codec_type) {
02815 case AVMEDIA_TYPE_AUDIO:
02816 if(st->codec->sample_rate<=0){
02817 av_log(s, AV_LOG_ERROR, "sample rate not set\n");
02818 return AVERROR(EINVAL);
02819 }
02820 if(!st->codec->block_align)
02821 st->codec->block_align = st->codec->channels *
02822 av_get_bits_per_sample(st->codec->codec_id) >> 3;
02823 break;
02824 case AVMEDIA_TYPE_VIDEO:
02825 if(st->codec->time_base.num<=0 || st->codec->time_base.den<=0){
02826 av_log(s, AV_LOG_ERROR, "time base not set\n");
02827 return AVERROR(EINVAL);
02828 }
02829 if((st->codec->width<=0 || st->codec->height<=0) && !(s->oformat->flags & AVFMT_NODIMENSIONS)){
02830 av_log(s, AV_LOG_ERROR, "dimensions not set\n");
02831 return AVERROR(EINVAL);
02832 }
02833 if(av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)){
02834 av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between encoder and muxer layer\n");
02835 return AVERROR(EINVAL);
02836 }
02837 break;
02838 }
02839
02840 if(s->oformat->codec_tag){
02841 if(st->codec->codec_tag && st->codec->codec_id == CODEC_ID_RAWVIDEO && av_codec_get_tag(s->oformat->codec_tag, st->codec->codec_id) == 0 && !validate_codec_tag(s, st)){
02842
02843 st->codec->codec_tag= 0;
02844 }
02845 if(st->codec->codec_tag){
02846 if (!validate_codec_tag(s, st)) {
02847 char tagbuf[32];
02848 av_get_codec_tag_string(tagbuf, sizeof(tagbuf), st->codec->codec_tag);
02849 av_log(s, AV_LOG_ERROR,
02850 "Tag %s/0x%08x incompatible with output codec id '%d'\n",
02851 tagbuf, st->codec->codec_tag, st->codec->codec_id);
02852 return AVERROR_INVALIDDATA;
02853 }
02854 }else
02855 st->codec->codec_tag= av_codec_get_tag(s->oformat->codec_tag, st->codec->codec_id);
02856 }
02857
02858 if(s->oformat->flags & AVFMT_GLOBALHEADER &&
02859 !(st->codec->flags & CODEC_FLAG_GLOBAL_HEADER))
02860 av_log(s, AV_LOG_WARNING, "Codec for stream %d does not use global headers but container format requires global headers\n", i);
02861 }
02862
02863 if (!s->priv_data && s->oformat->priv_data_size > 0) {
02864 s->priv_data = av_mallocz(s->oformat->priv_data_size);
02865 if (!s->priv_data)
02866 return AVERROR(ENOMEM);
02867 }
02868
02869 #if FF_API_OLD_METADATA
02870 ff_metadata_mux_compat(s);
02871 #endif
02872
02873
02874 if (s->nb_streams && !(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
02875 av_metadata_set2(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
02876 }
02877
02878 if(s->oformat->write_header){
02879 ret = s->oformat->write_header(s);
02880 if (ret < 0)
02881 return ret;
02882 }
02883
02884
02885 for(i=0;i<s->nb_streams;i++) {
02886 int64_t den = AV_NOPTS_VALUE;
02887 st = s->streams[i];
02888
02889 switch (st->codec->codec_type) {
02890 case AVMEDIA_TYPE_AUDIO:
02891 den = (int64_t)st->time_base.num * st->codec->sample_rate;
02892 break;
02893 case AVMEDIA_TYPE_VIDEO:
02894 den = (int64_t)st->time_base.num * st->codec->time_base.den;
02895 break;
02896 default:
02897 break;
02898 }
02899 if (den != AV_NOPTS_VALUE) {
02900 if (den <= 0)
02901 return AVERROR_INVALIDDATA;
02902 av_frac_init(&st->pts, 0, 0, den);
02903 }
02904 }
02905 return 0;
02906 }
02907
02908
02909 static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt){
02910 int delay = FFMAX(st->codec->has_b_frames, !!st->codec->max_b_frames);
02911 int num, den, frame_size, i;
02912
02913
02914
02915
02916
02917
02918
02919 if (pkt->duration == 0) {
02920 compute_frame_duration(&num, &den, st, NULL, pkt);
02921 if (den && num) {
02922 pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
02923 }
02924 }
02925
02926 if(pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay==0)
02927 pkt->pts= pkt->dts;
02928
02929
02930 if((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay){
02931 pkt->dts=
02932
02933 pkt->pts= st->pts.val;
02934 }
02935
02936
02937 if(pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY){
02938 st->pts_buffer[0]= pkt->pts;
02939 for(i=1; i<delay+1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
02940 st->pts_buffer[i]= pkt->pts + (i-delay-1) * pkt->duration;
02941 for(i=0; i<delay && st->pts_buffer[i] > st->pts_buffer[i+1]; i++)
02942 FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i+1]);
02943
02944 pkt->dts= st->pts_buffer[0];
02945 }
02946
02947 if(st->cur_dts && st->cur_dts != AV_NOPTS_VALUE && st->cur_dts >= pkt->dts){
02948 av_log(s, AV_LOG_ERROR,
02949 "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %"PRId64" >= %"PRId64"\n",
02950 st->index, st->cur_dts, pkt->dts);
02951 return -1;
02952 }
02953 if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts){
02954 av_log(s, AV_LOG_ERROR, "pts < dts in stream %d\n", st->index);
02955 return -1;
02956 }
02957
02958
02959 st->cur_dts= pkt->dts;
02960 st->pts.val= pkt->dts;
02961
02962
02963 switch (st->codec->codec_type) {
02964 case AVMEDIA_TYPE_AUDIO:
02965 frame_size = get_audio_frame_size(st->codec, pkt->size);
02966
02967
02968
02969
02970 if (frame_size >= 0 && (pkt->size || st->pts.num!=st->pts.den>>1 || st->pts.val)) {
02971 av_frac_add(&st->pts, (int64_t)st->time_base.den * frame_size);
02972 }
02973 break;
02974 case AVMEDIA_TYPE_VIDEO:
02975 av_frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num);
02976 break;
02977 default:
02978 break;
02979 }
02980 return 0;
02981 }
02982
02983 int av_write_frame(AVFormatContext *s, AVPacket *pkt)
02984 {
02985 int ret = compute_pkt_fields2(s, s->streams[pkt->stream_index], pkt);
02986
02987 if(ret<0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
02988 return ret;
02989
02990 ret= s->oformat->write_packet(s, pkt);
02991 if(!ret)
02992 ret= url_ferror(s->pb);
02993 return ret;
02994 }
02995
02996 void ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
02997 int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
02998 {
02999 AVPacketList **next_point, *this_pktl;
03000
03001 this_pktl = av_mallocz(sizeof(AVPacketList));
03002 this_pktl->pkt= *pkt;
03003 pkt->destruct= NULL;
03004 av_dup_packet(&this_pktl->pkt);
03005
03006 if(s->streams[pkt->stream_index]->last_in_packet_buffer){
03007 next_point = &(s->streams[pkt->stream_index]->last_in_packet_buffer->next);
03008 }else
03009 next_point = &s->packet_buffer;
03010
03011 if(*next_point){
03012 if(compare(s, &s->packet_buffer_end->pkt, pkt)){
03013 while(!compare(s, &(*next_point)->pkt, pkt)){
03014 next_point= &(*next_point)->next;
03015 }
03016 goto next_non_null;
03017 }else{
03018 next_point = &(s->packet_buffer_end->next);
03019 }
03020 }
03021 assert(!*next_point);
03022
03023 s->packet_buffer_end= this_pktl;
03024 next_non_null:
03025
03026 this_pktl->next= *next_point;
03027
03028 s->streams[pkt->stream_index]->last_in_packet_buffer=
03029 *next_point= this_pktl;
03030 }
03031
03032 static int ff_interleave_compare_dts(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
03033 {
03034 AVStream *st = s->streams[ pkt ->stream_index];
03035 AVStream *st2= s->streams[ next->stream_index];
03036 int64_t a= st2->time_base.num * (int64_t)st ->time_base.den;
03037 int64_t b= st ->time_base.num * (int64_t)st2->time_base.den;
03038 return av_rescale_rnd(pkt->dts, b, a, AV_ROUND_DOWN) < next->dts;
03039 }
03040
03041 int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush){
03042 AVPacketList *pktl;
03043 int stream_count=0;
03044 int i;
03045
03046 if(pkt){
03047 ff_interleave_add_packet(s, pkt, ff_interleave_compare_dts);
03048 }
03049
03050 for(i=0; i < s->nb_streams; i++)
03051 stream_count+= !!s->streams[i]->last_in_packet_buffer;
03052
03053 if(stream_count && (s->nb_streams == stream_count || flush)){
03054 pktl= s->packet_buffer;
03055 *out= pktl->pkt;
03056
03057 s->packet_buffer= pktl->next;
03058 if(!s->packet_buffer)
03059 s->packet_buffer_end= NULL;
03060
03061 if(s->streams[out->stream_index]->last_in_packet_buffer == pktl)
03062 s->streams[out->stream_index]->last_in_packet_buffer= NULL;
03063 av_freep(&pktl);
03064 return 1;
03065 }else{
03066 av_init_packet(out);
03067 return 0;
03068 }
03069 }
03070
03080 static int av_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, int flush){
03081 if(s->oformat->interleave_packet)
03082 return s->oformat->interleave_packet(s, out, in, flush);
03083 else
03084 return av_interleave_packet_per_dts(s, out, in, flush);
03085 }
03086
03087 int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt){
03088 AVStream *st= s->streams[ pkt->stream_index];
03089
03090
03091 if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO && pkt->size==0)
03092 return 0;
03093
03094
03095 if(compute_pkt_fields2(s, st, pkt) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
03096 return -1;
03097
03098 if(pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
03099 return -1;
03100
03101 for(;;){
03102 AVPacket opkt;
03103 int ret= av_interleave_packet(s, &opkt, pkt, 0);
03104 if(ret<=0)
03105 return ret;
03106
03107 ret= s->oformat->write_packet(s, &opkt);
03108
03109 av_free_packet(&opkt);
03110 pkt= NULL;
03111
03112 if(ret<0)
03113 return ret;
03114 if(url_ferror(s->pb))
03115 return url_ferror(s->pb);
03116 }
03117 }
03118
03119 int av_write_trailer(AVFormatContext *s)
03120 {
03121 int ret, i;
03122
03123 for(;;){
03124 AVPacket pkt;
03125 ret= av_interleave_packet(s, &pkt, NULL, 1);
03126 if(ret<0)
03127 goto fail;
03128 if(!ret)
03129 break;
03130
03131 ret= s->oformat->write_packet(s, &pkt);
03132
03133 av_free_packet(&pkt);
03134
03135 if(ret<0)
03136 goto fail;
03137 if(url_ferror(s->pb))
03138 goto fail;
03139 }
03140
03141 if(s->oformat->write_trailer)
03142 ret = s->oformat->write_trailer(s);
03143 fail:
03144 if(ret == 0)
03145 ret=url_ferror(s->pb);
03146 for(i=0;i<s->nb_streams;i++) {
03147 av_freep(&s->streams[i]->priv_data);
03148 av_freep(&s->streams[i]->index_entries);
03149 }
03150 av_freep(&s->priv_data);
03151 return ret;
03152 }
03153
03154 void ff_program_add_stream_index(AVFormatContext *ac, int progid, unsigned int idx)
03155 {
03156 int i, j;
03157 AVProgram *program=NULL;
03158 void *tmp;
03159
03160 if (idx >= ac->nb_streams) {
03161 av_log(ac, AV_LOG_ERROR, "stream index %d is not valid\n", idx);
03162 return;
03163 }
03164
03165 for(i=0; i<ac->nb_programs; i++){
03166 if(ac->programs[i]->id != progid)
03167 continue;
03168 program = ac->programs[i];
03169 for(j=0; j<program->nb_stream_indexes; j++)
03170 if(program->stream_index[j] == idx)
03171 return;
03172
03173 tmp = av_realloc(program->stream_index, sizeof(unsigned int)*(program->nb_stream_indexes+1));
03174 if(!tmp)
03175 return;
03176 program->stream_index = tmp;
03177 program->stream_index[program->nb_stream_indexes++] = idx;
03178 return;
03179 }
03180 }
03181
03182 static void print_fps(double d, const char *postfix){
03183 uint64_t v= lrintf(d*100);
03184 if (v% 100 ) av_log(NULL, AV_LOG_INFO, ", %3.2f %s", d, postfix);
03185 else if(v%(100*1000)) av_log(NULL, AV_LOG_INFO, ", %1.0f %s", d, postfix);
03186 else av_log(NULL, AV_LOG_INFO, ", %1.0fk %s", d/1000, postfix);
03187 }
03188
03189 static void dump_metadata(void *ctx, AVMetadata *m, const char *indent)
03190 {
03191 if(m && !(m->count == 1 && av_metadata_get(m, "language", NULL, 0))){
03192 AVMetadataTag *tag=NULL;
03193
03194 av_log(ctx, AV_LOG_INFO, "%sMetadata:\n", indent);
03195 while((tag=av_metadata_get(m, "", tag, AV_METADATA_IGNORE_SUFFIX))) {
03196 if(strcmp("language", tag->key))
03197 av_log(ctx, AV_LOG_INFO, "%s %-16s: %s\n", indent, tag->key, tag->value);
03198 }
03199 }
03200 }
03201
03202
03203 static void dump_stream_format(AVFormatContext *ic, int i, int index, int is_output)
03204 {
03205 char buf[256];
03206 int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
03207 AVStream *st = ic->streams[i];
03208 int g = av_gcd(st->time_base.num, st->time_base.den);
03209 AVMetadataTag *lang = av_metadata_get(st->metadata, "language", NULL, 0);
03210 avcodec_string(buf, sizeof(buf), st->codec, is_output);
03211 av_log(NULL, AV_LOG_INFO, " Stream #%d.%d", index, i);
03212
03213
03214 if (flags & AVFMT_SHOW_IDS)
03215 av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
03216 if (lang)
03217 av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
03218 av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames, st->time_base.num/g, st->time_base.den/g);
03219 av_log(NULL, AV_LOG_INFO, ": %s", buf);
03220 if (st->sample_aspect_ratio.num &&
03221 av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)) {
03222 AVRational display_aspect_ratio;
03223 av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
03224 st->codec->width*st->sample_aspect_ratio.num,
03225 st->codec->height*st->sample_aspect_ratio.den,
03226 1024*1024);
03227 av_log(NULL, AV_LOG_INFO, ", PAR %d:%d DAR %d:%d",
03228 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
03229 display_aspect_ratio.num, display_aspect_ratio.den);
03230 }
03231 if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
03232 if(st->avg_frame_rate.den && st->avg_frame_rate.num)
03233 print_fps(av_q2d(st->avg_frame_rate), "fps");
03234 if(st->r_frame_rate.den && st->r_frame_rate.num)
03235 print_fps(av_q2d(st->r_frame_rate), "tbr");
03236 if(st->time_base.den && st->time_base.num)
03237 print_fps(1/av_q2d(st->time_base), "tbn");
03238 if(st->codec->time_base.den && st->codec->time_base.num)
03239 print_fps(1/av_q2d(st->codec->time_base), "tbc");
03240 }
03241 if (st->disposition & AV_DISPOSITION_DEFAULT)
03242 av_log(NULL, AV_LOG_INFO, " (default)");
03243 if (st->disposition & AV_DISPOSITION_DUB)
03244 av_log(NULL, AV_LOG_INFO, " (dub)");
03245 if (st->disposition & AV_DISPOSITION_ORIGINAL)
03246 av_log(NULL, AV_LOG_INFO, " (original)");
03247 if (st->disposition & AV_DISPOSITION_COMMENT)
03248 av_log(NULL, AV_LOG_INFO, " (comment)");
03249 if (st->disposition & AV_DISPOSITION_LYRICS)
03250 av_log(NULL, AV_LOG_INFO, " (lyrics)");
03251 if (st->disposition & AV_DISPOSITION_KARAOKE)
03252 av_log(NULL, AV_LOG_INFO, " (karaoke)");
03253 if (st->disposition & AV_DISPOSITION_FORCED)
03254 av_log(NULL, AV_LOG_INFO, " (forced)");
03255 if (st->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
03256 av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
03257 if (st->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
03258 av_log(NULL, AV_LOG_INFO, " (visual impaired)");
03259 if (st->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
03260 av_log(NULL, AV_LOG_INFO, " (clean effects)");
03261 av_log(NULL, AV_LOG_INFO, "\n");
03262 dump_metadata(NULL, st->metadata, " ");
03263 }
03264
03265 #if FF_API_DUMP_FORMAT
03266 void dump_format(AVFormatContext *ic,
03267 int index,
03268 const char *url,
03269 int is_output)
03270 {
03271 av_dump_format(ic, index, url, is_output);
03272 }
03273 #endif
03274
03275 void av_dump_format(AVFormatContext *ic,
03276 int index,
03277 const char *url,
03278 int is_output)
03279 {
03280 int i;
03281 uint8_t *printed = av_mallocz(ic->nb_streams);
03282 if (ic->nb_streams && !printed)
03283 return;
03284
03285 av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
03286 is_output ? "Output" : "Input",
03287 index,
03288 is_output ? ic->oformat->name : ic->iformat->name,
03289 is_output ? "to" : "from", url);
03290 dump_metadata(NULL, ic->metadata, " ");
03291 if (!is_output) {
03292 av_log(NULL, AV_LOG_INFO, " Duration: ");
03293 if (ic->duration != AV_NOPTS_VALUE) {
03294 int hours, mins, secs, us;
03295 secs = ic->duration / AV_TIME_BASE;
03296 us = ic->duration % AV_TIME_BASE;
03297 mins = secs / 60;
03298 secs %= 60;
03299 hours = mins / 60;
03300 mins %= 60;
03301 av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
03302 (100 * us) / AV_TIME_BASE);
03303 } else {
03304 av_log(NULL, AV_LOG_INFO, "N/A");
03305 }
03306 if (ic->start_time != AV_NOPTS_VALUE) {
03307 int secs, us;
03308 av_log(NULL, AV_LOG_INFO, ", start: ");
03309 secs = ic->start_time / AV_TIME_BASE;
03310 us = abs(ic->start_time % AV_TIME_BASE);
03311 av_log(NULL, AV_LOG_INFO, "%d.%06d",
03312 secs, (int)av_rescale(us, 1000000, AV_TIME_BASE));
03313 }
03314 av_log(NULL, AV_LOG_INFO, ", bitrate: ");
03315 if (ic->bit_rate) {
03316 av_log(NULL, AV_LOG_INFO,"%d kb/s", ic->bit_rate / 1000);
03317 } else {
03318 av_log(NULL, AV_LOG_INFO, "N/A");
03319 }
03320 av_log(NULL, AV_LOG_INFO, "\n");
03321 }
03322 for (i = 0; i < ic->nb_chapters; i++) {
03323 AVChapter *ch = ic->chapters[i];
03324 av_log(NULL, AV_LOG_INFO, " Chapter #%d.%d: ", index, i);
03325 av_log(NULL, AV_LOG_INFO, "start %f, ", ch->start * av_q2d(ch->time_base));
03326 av_log(NULL, AV_LOG_INFO, "end %f\n", ch->end * av_q2d(ch->time_base));
03327
03328 dump_metadata(NULL, ch->metadata, " ");
03329 }
03330 if(ic->nb_programs) {
03331 int j, k, total = 0;
03332 for(j=0; j<ic->nb_programs; j++) {
03333 AVMetadataTag *name = av_metadata_get(ic->programs[j]->metadata,
03334 "name", NULL, 0);
03335 av_log(NULL, AV_LOG_INFO, " Program %d %s\n", ic->programs[j]->id,
03336 name ? name->value : "");
03337 dump_metadata(NULL, ic->programs[j]->metadata, " ");
03338 for(k=0; k<ic->programs[j]->nb_stream_indexes; k++) {
03339 dump_stream_format(ic, ic->programs[j]->stream_index[k], index, is_output);
03340 printed[ic->programs[j]->stream_index[k]] = 1;
03341 }
03342 total += ic->programs[j]->nb_stream_indexes;
03343 }
03344 if (total < ic->nb_streams)
03345 av_log(NULL, AV_LOG_INFO, " No Program\n");
03346 }
03347 for(i=0;i<ic->nb_streams;i++)
03348 if (!printed[i])
03349 dump_stream_format(ic, i, index, is_output);
03350
03351 av_free(printed);
03352 }
03353
03354 #if FF_API_PARSE_FRAME_PARAM
03355 #include "libavutil/parseutils.h"
03356
03357 int parse_image_size(int *width_ptr, int *height_ptr, const char *str)
03358 {
03359 return av_parse_video_size(width_ptr, height_ptr, str);
03360 }
03361
03362 int parse_frame_rate(int *frame_rate_num, int *frame_rate_den, const char *arg)
03363 {
03364 AVRational frame_rate;
03365 int ret = av_parse_video_rate(&frame_rate, arg);
03366 *frame_rate_num= frame_rate.num;
03367 *frame_rate_den= frame_rate.den;
03368 return ret;
03369 }
03370 #endif
03371
03372 int64_t av_gettime(void)
03373 {
03374 struct timeval tv;
03375 gettimeofday(&tv,NULL);
03376 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
03377 }
03378
03379 uint64_t ff_ntp_time(void)
03380 {
03381 return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
03382 }
03383
03384 #if FF_API_PARSE_DATE
03385 #include "libavutil/parseutils.h"
03386
03387 int64_t parse_date(const char *timestr, int duration)
03388 {
03389 int64_t timeval;
03390 av_parse_time(&timeval, timestr, duration);
03391 return timeval;
03392 }
03393 #endif
03394
03395 #if FF_API_FIND_INFO_TAG
03396 #include "libavutil/parseutils.h"
03397
03398 int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
03399 {
03400 return av_find_info_tag(arg, arg_size, tag1, info);
03401 }
03402 #endif
03403
03404 int av_get_frame_filename(char *buf, int buf_size,
03405 const char *path, int number)
03406 {
03407 const char *p;
03408 char *q, buf1[20], c;
03409 int nd, len, percentd_found;
03410
03411 q = buf;
03412 p = path;
03413 percentd_found = 0;
03414 for(;;) {
03415 c = *p++;
03416 if (c == '\0')
03417 break;
03418 if (c == '%') {
03419 do {
03420 nd = 0;
03421 while (isdigit(*p)) {
03422 nd = nd * 10 + *p++ - '0';
03423 }
03424 c = *p++;
03425 } while (isdigit(c));
03426
03427 switch(c) {
03428 case '%':
03429 goto addchar;
03430 case 'd':
03431 if (percentd_found)
03432 goto fail;
03433 percentd_found = 1;
03434 snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
03435 len = strlen(buf1);
03436 if ((q - buf + len) > buf_size - 1)
03437 goto fail;
03438 memcpy(q, buf1, len);
03439 q += len;
03440 break;
03441 default:
03442 goto fail;
03443 }
03444 } else {
03445 addchar:
03446 if ((q - buf) < buf_size - 1)
03447 *q++ = c;
03448 }
03449 }
03450 if (!percentd_found)
03451 goto fail;
03452 *q = '\0';
03453 return 0;
03454 fail:
03455 *q = '\0';
03456 return -1;
03457 }
03458
03459 static void hex_dump_internal(void *avcl, FILE *f, int level, uint8_t *buf, int size)
03460 {
03461 int len, i, j, c;
03462 #undef fprintf
03463 #define PRINT(...) do { if (!f) av_log(avcl, level, __VA_ARGS__); else fprintf(f, __VA_ARGS__); } while(0)
03464
03465 for(i=0;i<size;i+=16) {
03466 len = size - i;
03467 if (len > 16)
03468 len = 16;
03469 PRINT("%08x ", i);
03470 for(j=0;j<16;j++) {
03471 if (j < len)
03472 PRINT(" %02x", buf[i+j]);
03473 else
03474 PRINT(" ");
03475 }
03476 PRINT(" ");
03477 for(j=0;j<len;j++) {
03478 c = buf[i+j];
03479 if (c < ' ' || c > '~')
03480 c = '.';
03481 PRINT("%c", c);
03482 }
03483 PRINT("\n");
03484 }
03485 #undef PRINT
03486 }
03487
03488 void av_hex_dump(FILE *f, uint8_t *buf, int size)
03489 {
03490 hex_dump_internal(NULL, f, 0, buf, size);
03491 }
03492
03493 void av_hex_dump_log(void *avcl, int level, uint8_t *buf, int size)
03494 {
03495 hex_dump_internal(avcl, NULL, level, buf, size);
03496 }
03497
03498 static void pkt_dump_internal(void *avcl, FILE *f, int level, AVPacket *pkt, int dump_payload, AVRational time_base)
03499 {
03500 #undef fprintf
03501 #define PRINT(...) do { if (!f) av_log(avcl, level, __VA_ARGS__); else fprintf(f, __VA_ARGS__); } while(0)
03502 PRINT("stream #%d:\n", pkt->stream_index);
03503 PRINT(" keyframe=%d\n", ((pkt->flags & AV_PKT_FLAG_KEY) != 0));
03504 PRINT(" duration=%0.3f\n", pkt->duration * av_q2d(time_base));
03505
03506 PRINT(" dts=");
03507 if (pkt->dts == AV_NOPTS_VALUE)
03508 PRINT("N/A");
03509 else
03510 PRINT("%0.3f", pkt->dts * av_q2d(time_base));
03511
03512 PRINT(" pts=");
03513 if (pkt->pts == AV_NOPTS_VALUE)
03514 PRINT("N/A");
03515 else
03516 PRINT("%0.3f", pkt->pts * av_q2d(time_base));
03517 PRINT("\n");
03518 PRINT(" size=%d\n", pkt->size);
03519 #undef PRINT
03520 if (dump_payload)
03521 av_hex_dump(f, pkt->data, pkt->size);
03522 }
03523
03524 void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload)
03525 {
03526 AVRational tb = { 1, AV_TIME_BASE };
03527 pkt_dump_internal(NULL, f, 0, pkt, dump_payload, tb);
03528 }
03529
03530 void av_pkt_dump2(FILE *f, AVPacket *pkt, int dump_payload, AVStream *st)
03531 {
03532 pkt_dump_internal(NULL, f, 0, pkt, dump_payload, st->time_base);
03533 }
03534
03535 void av_pkt_dump_log(void *avcl, int level, AVPacket *pkt, int dump_payload)
03536 {
03537 AVRational tb = { 1, AV_TIME_BASE };
03538 pkt_dump_internal(avcl, NULL, level, pkt, dump_payload, tb);
03539 }
03540
03541 void av_pkt_dump_log2(void *avcl, int level, AVPacket *pkt, int dump_payload,
03542 AVStream *st)
03543 {
03544 pkt_dump_internal(avcl, NULL, level, pkt, dump_payload, st->time_base);
03545 }
03546
03547 #if FF_API_URL_SPLIT
03548 attribute_deprecated
03549 void ff_url_split(char *proto, int proto_size,
03550 char *authorization, int authorization_size,
03551 char *hostname, int hostname_size,
03552 int *port_ptr,
03553 char *path, int path_size,
03554 const char *url)
03555 {
03556 av_url_split(proto, proto_size,
03557 authorization, authorization_size,
03558 hostname, hostname_size,
03559 port_ptr,
03560 path, path_size,
03561 url);
03562 }
03563 #endif
03564
03565 void av_url_split(char *proto, int proto_size,
03566 char *authorization, int authorization_size,
03567 char *hostname, int hostname_size,
03568 int *port_ptr,
03569 char *path, int path_size,
03570 const char *url)
03571 {
03572 const char *p, *ls, *at, *col, *brk;
03573
03574 if (port_ptr) *port_ptr = -1;
03575 if (proto_size > 0) proto[0] = 0;
03576 if (authorization_size > 0) authorization[0] = 0;
03577 if (hostname_size > 0) hostname[0] = 0;
03578 if (path_size > 0) path[0] = 0;
03579
03580
03581 if ((p = strchr(url, ':'))) {
03582 av_strlcpy(proto, url, FFMIN(proto_size, p + 1 - url));
03583 p++;
03584 if (*p == '/') p++;
03585 if (*p == '/') p++;
03586 } else {
03587
03588 av_strlcpy(path, url, path_size);
03589 return;
03590 }
03591
03592
03593 ls = strchr(p, '/');
03594 if(!ls)
03595 ls = strchr(p, '?');
03596 if(ls)
03597 av_strlcpy(path, ls, path_size);
03598 else
03599 ls = &p[strlen(p)];
03600
03601
03602 if (ls != p) {
03603
03604 if ((at = strchr(p, '@')) && at < ls) {
03605 av_strlcpy(authorization, p,
03606 FFMIN(authorization_size, at + 1 - p));
03607 p = at + 1;
03608 }
03609
03610 if (*p == '[' && (brk = strchr(p, ']')) && brk < ls) {
03611
03612 av_strlcpy(hostname, p + 1,
03613 FFMIN(hostname_size, brk - p));
03614 if (brk[1] == ':' && port_ptr)
03615 *port_ptr = atoi(brk + 2);
03616 } else if ((col = strchr(p, ':')) && col < ls) {
03617 av_strlcpy(hostname, p,
03618 FFMIN(col + 1 - p, hostname_size));
03619 if (port_ptr) *port_ptr = atoi(col + 1);
03620 } else
03621 av_strlcpy(hostname, p,
03622 FFMIN(ls + 1 - p, hostname_size));
03623 }
03624 }
03625
03626 char *ff_data_to_hex(char *buff, const uint8_t *src, int s, int lowercase)
03627 {
03628 int i;
03629 static const char hex_table_uc[16] = { '0', '1', '2', '3',
03630 '4', '5', '6', '7',
03631 '8', '9', 'A', 'B',
03632 'C', 'D', 'E', 'F' };
03633 static const char hex_table_lc[16] = { '0', '1', '2', '3',
03634 '4', '5', '6', '7',
03635 '8', '9', 'a', 'b',
03636 'c', 'd', 'e', 'f' };
03637 const char *hex_table = lowercase ? hex_table_lc : hex_table_uc;
03638
03639 for(i = 0; i < s; i++) {
03640 buff[i * 2] = hex_table[src[i] >> 4];
03641 buff[i * 2 + 1] = hex_table[src[i] & 0xF];
03642 }
03643
03644 return buff;
03645 }
03646
03647 int ff_hex_to_data(uint8_t *data, const char *p)
03648 {
03649 int c, len, v;
03650
03651 len = 0;
03652 v = 1;
03653 for (;;) {
03654 p += strspn(p, SPACE_CHARS);
03655 if (*p == '\0')
03656 break;
03657 c = toupper((unsigned char) *p++);
03658 if (c >= '0' && c <= '9')
03659 c = c - '0';
03660 else if (c >= 'A' && c <= 'F')
03661 c = c - 'A' + 10;
03662 else
03663 break;
03664 v = (v << 4) | c;
03665 if (v & 0x100) {
03666 if (data)
03667 data[len] = v;
03668 len++;
03669 v = 1;
03670 }
03671 }
03672 return len;
03673 }
03674
03675 void av_set_pts_info(AVStream *s, int pts_wrap_bits,
03676 unsigned int pts_num, unsigned int pts_den)
03677 {
03678 AVRational new_tb;
03679 if(av_reduce(&new_tb.num, &new_tb.den, pts_num, pts_den, INT_MAX)){
03680 if(new_tb.num != pts_num)
03681 av_log(NULL, AV_LOG_DEBUG, "st:%d removing common factor %d from timebase\n", s->index, pts_num/new_tb.num);
03682 }else
03683 av_log(NULL, AV_LOG_WARNING, "st:%d has too large timebase, reducing\n", s->index);
03684
03685 if(new_tb.num <= 0 || new_tb.den <= 0) {
03686 av_log(NULL, AV_LOG_ERROR, "Ignoring attempt to set invalid timebase for st:%d\n", s->index);
03687 return;
03688 }
03689 s->time_base = new_tb;
03690 s->pts_wrap_bits = pts_wrap_bits;
03691 }
03692
03693 int ff_url_join(char *str, int size, const char *proto,
03694 const char *authorization, const char *hostname,
03695 int port, const char *fmt, ...)
03696 {
03697 #if CONFIG_NETWORK
03698 struct addrinfo hints, *ai;
03699 #endif
03700
03701 str[0] = '\0';
03702 if (proto)
03703 av_strlcatf(str, size, "%s://", proto);
03704 if (authorization && authorization[0])
03705 av_strlcatf(str, size, "%s@", authorization);
03706 #if CONFIG_NETWORK && defined(AF_INET6)
03707
03708
03709 memset(&hints, 0, sizeof(hints));
03710 hints.ai_flags = AI_NUMERICHOST;
03711 if (!getaddrinfo(hostname, NULL, &hints, &ai)) {
03712 if (ai->ai_family == AF_INET6) {
03713 av_strlcat(str, "[", size);
03714 av_strlcat(str, hostname, size);
03715 av_strlcat(str, "]", size);
03716 } else {
03717 av_strlcat(str, hostname, size);
03718 }
03719 freeaddrinfo(ai);
03720 } else
03721 #endif
03722
03723 av_strlcat(str, hostname, size);
03724
03725 if (port >= 0)
03726 av_strlcatf(str, size, ":%d", port);
03727 if (fmt) {
03728 va_list vl;
03729 int len = strlen(str);
03730
03731 va_start(vl, fmt);
03732 vsnprintf(str + len, size > len ? size - len : 0, fmt, vl);
03733 va_end(vl);
03734 }
03735 return strlen(str);
03736 }
03737
03738 int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
03739 AVFormatContext *src)
03740 {
03741 AVPacket local_pkt;
03742
03743 local_pkt = *pkt;
03744 local_pkt.stream_index = dst_stream;
03745 if (pkt->pts != AV_NOPTS_VALUE)
03746 local_pkt.pts = av_rescale_q(pkt->pts,
03747 src->streams[pkt->stream_index]->time_base,
03748 dst->streams[dst_stream]->time_base);
03749 if (pkt->dts != AV_NOPTS_VALUE)
03750 local_pkt.dts = av_rescale_q(pkt->dts,
03751 src->streams[pkt->stream_index]->time_base,
03752 dst->streams[dst_stream]->time_base);
03753 return av_write_frame(dst, &local_pkt);
03754 }
03755
03756 void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf,
03757 void *context)
03758 {
03759 const char *ptr = str;
03760
03761
03762 for (;;) {
03763 const char *key;
03764 char *dest = NULL, *dest_end;
03765 int key_len, dest_len = 0;
03766
03767
03768 while (*ptr && (isspace(*ptr) || *ptr == ','))
03769 ptr++;
03770 if (!*ptr)
03771 break;
03772
03773 key = ptr;
03774
03775 if (!(ptr = strchr(key, '=')))
03776 break;
03777 ptr++;
03778 key_len = ptr - key;
03779
03780 callback_get_buf(context, key, key_len, &dest, &dest_len);
03781 dest_end = dest + dest_len - 1;
03782
03783 if (*ptr == '\"') {
03784 ptr++;
03785 while (*ptr && *ptr != '\"') {
03786 if (*ptr == '\\') {
03787 if (!ptr[1])
03788 break;
03789 if (dest && dest < dest_end)
03790 *dest++ = ptr[1];
03791 ptr += 2;
03792 } else {
03793 if (dest && dest < dest_end)
03794 *dest++ = *ptr;
03795 ptr++;
03796 }
03797 }
03798 if (*ptr == '\"')
03799 ptr++;
03800 } else {
03801 for (; *ptr && !(isspace(*ptr) || *ptr == ','); ptr++)
03802 if (dest && dest < dest_end)
03803 *dest++ = *ptr;
03804 }
03805 if (dest)
03806 *dest = 0;
03807 }
03808 }
03809
03810 int ff_find_stream_index(AVFormatContext *s, int id)
03811 {
03812 int i;
03813 for (i = 0; i < s->nb_streams; i++) {
03814 if (s->streams[i]->id == id)
03815 return i;
03816 }
03817 return -1;
03818 }