00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "avformat.h"
00025 #include "avio_internal.h"
00026 #include "internal.h"
00027 #include "libavcodec/internal.h"
00028 #include "libavutil/opt.h"
00029 #include "metadata.h"
00030 #include "id3v2.h"
00031 #include "libavutil/avstring.h"
00032 #include "riff.h"
00033 #include "audiointerleave.h"
00034 #include <sys/time.h>
00035 #include <time.h>
00036 #include <strings.h>
00037 #include <stdarg.h>
00038 #if CONFIG_NETWORK
00039 #include "network.h"
00040 #endif
00041
00042 #undef NDEBUG
00043 #include <assert.h>
00044
00050 unsigned avformat_version(void)
00051 {
00052 return LIBAVFORMAT_VERSION_INT;
00053 }
00054
00055 const char *avformat_configuration(void)
00056 {
00057 return FFMPEG_CONFIGURATION;
00058 }
00059
00060 const char *avformat_license(void)
00061 {
00062 #define LICENSE_PREFIX "libavformat license: "
00063 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
00064 }
00065
00066
00067
00078 static void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
00079 {
00080 num += (den >> 1);
00081 if (num >= den) {
00082 val += num / den;
00083 num = num % den;
00084 }
00085 f->val = val;
00086 f->num = num;
00087 f->den = den;
00088 }
00089
00096 static void av_frac_add(AVFrac *f, int64_t incr)
00097 {
00098 int64_t num, den;
00099
00100 num = f->num + incr;
00101 den = f->den;
00102 if (num < 0) {
00103 f->val += num / den;
00104 num = num % den;
00105 if (num < 0) {
00106 num += den;
00107 f->val--;
00108 }
00109 } else if (num >= den) {
00110 f->val += num / den;
00111 num = num % den;
00112 }
00113 f->num = num;
00114 }
00115
00117 #if !FF_API_FIRST_FORMAT
00118 static
00119 #endif
00120 AVInputFormat *first_iformat = NULL;
00122 #if !FF_API_FIRST_FORMAT
00123 static
00124 #endif
00125 AVOutputFormat *first_oformat = NULL;
00126
00127 AVInputFormat *av_iformat_next(AVInputFormat *f)
00128 {
00129 if(f) return f->next;
00130 else return first_iformat;
00131 }
00132
00133 AVOutputFormat *av_oformat_next(AVOutputFormat *f)
00134 {
00135 if(f) return f->next;
00136 else return first_oformat;
00137 }
00138
00139 void av_register_input_format(AVInputFormat *format)
00140 {
00141 AVInputFormat **p;
00142 p = &first_iformat;
00143 while (*p != NULL) p = &(*p)->next;
00144 *p = format;
00145 format->next = NULL;
00146 }
00147
00148 void av_register_output_format(AVOutputFormat *format)
00149 {
00150 AVOutputFormat **p;
00151 p = &first_oformat;
00152 while (*p != NULL) p = &(*p)->next;
00153 *p = format;
00154 format->next = NULL;
00155 }
00156
00157 int av_match_ext(const char *filename, const char *extensions)
00158 {
00159 const char *ext, *p;
00160 char ext1[32], *q;
00161
00162 if(!filename)
00163 return 0;
00164
00165 ext = strrchr(filename, '.');
00166 if (ext) {
00167 ext++;
00168 p = extensions;
00169 for(;;) {
00170 q = ext1;
00171 while (*p != '\0' && *p != ',' && q-ext1<sizeof(ext1)-1)
00172 *q++ = *p++;
00173 *q = '\0';
00174 if (!strcasecmp(ext1, ext))
00175 return 1;
00176 if (*p == '\0')
00177 break;
00178 p++;
00179 }
00180 }
00181 return 0;
00182 }
00183
00184 static int match_format(const char *name, const char *names)
00185 {
00186 const char *p;
00187 int len, namelen;
00188
00189 if (!name || !names)
00190 return 0;
00191
00192 namelen = strlen(name);
00193 while ((p = strchr(names, ','))) {
00194 len = FFMAX(p - names, namelen);
00195 if (!strncasecmp(name, names, len))
00196 return 1;
00197 names = p+1;
00198 }
00199 return !strcasecmp(name, names);
00200 }
00201
00202 #if FF_API_GUESS_FORMAT
00203 AVOutputFormat *guess_format(const char *short_name, const char *filename,
00204 const char *mime_type)
00205 {
00206 return av_guess_format(short_name, filename, mime_type);
00207 }
00208 #endif
00209
00210 AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
00211 const char *mime_type)
00212 {
00213 AVOutputFormat *fmt = NULL, *fmt_found;
00214 int score_max, score;
00215
00216
00217 #if CONFIG_IMAGE2_MUXER
00218 if (!short_name && filename &&
00219 av_filename_number_test(filename) &&
00220 av_guess_image2_codec(filename) != CODEC_ID_NONE) {
00221 return av_guess_format("image2", NULL, NULL);
00222 }
00223 #endif
00224
00225 fmt_found = NULL;
00226 score_max = 0;
00227 while ((fmt = av_oformat_next(fmt))) {
00228 score = 0;
00229 if (fmt->name && short_name && !strcmp(fmt->name, short_name))
00230 score += 100;
00231 if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
00232 score += 10;
00233 if (filename && fmt->extensions &&
00234 av_match_ext(filename, fmt->extensions)) {
00235 score += 5;
00236 }
00237 if (score > score_max) {
00238 score_max = score;
00239 fmt_found = fmt;
00240 }
00241 }
00242 return fmt_found;
00243 }
00244
00245 #if FF_API_GUESS_FORMAT
00246 AVOutputFormat *guess_stream_format(const char *short_name, const char *filename,
00247 const char *mime_type)
00248 {
00249 AVOutputFormat *fmt = av_guess_format(short_name, filename, mime_type);
00250
00251 if (fmt) {
00252 AVOutputFormat *stream_fmt;
00253 char stream_format_name[64];
00254
00255 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream", fmt->name);
00256 stream_fmt = av_guess_format(stream_format_name, NULL, NULL);
00257
00258 if (stream_fmt)
00259 fmt = stream_fmt;
00260 }
00261
00262 return fmt;
00263 }
00264 #endif
00265
00266 enum CodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
00267 const char *filename, const char *mime_type, enum AVMediaType type){
00268 if(type == AVMEDIA_TYPE_VIDEO){
00269 enum CodecID codec_id= CODEC_ID_NONE;
00270
00271 #if CONFIG_IMAGE2_MUXER
00272 if(!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")){
00273 codec_id= av_guess_image2_codec(filename);
00274 }
00275 #endif
00276 if(codec_id == CODEC_ID_NONE)
00277 codec_id= fmt->video_codec;
00278 return codec_id;
00279 }else if(type == AVMEDIA_TYPE_AUDIO)
00280 return fmt->audio_codec;
00281 else if (type == AVMEDIA_TYPE_SUBTITLE)
00282 return fmt->subtitle_codec;
00283 else
00284 return CODEC_ID_NONE;
00285 }
00286
00287 AVInputFormat *av_find_input_format(const char *short_name)
00288 {
00289 AVInputFormat *fmt = NULL;
00290 while ((fmt = av_iformat_next(fmt))) {
00291 if (match_format(short_name, fmt->name))
00292 return fmt;
00293 }
00294 return NULL;
00295 }
00296
00297 #if FF_API_SYMVER && CONFIG_SHARED && HAVE_SYMVER
00298 FF_SYMVER(void, av_destruct_packet_nofree, (AVPacket *pkt), "LIBAVFORMAT_52")
00299 {
00300 av_destruct_packet_nofree(pkt);
00301 }
00302
00303 FF_SYMVER(void, av_destruct_packet, (AVPacket *pkt), "LIBAVFORMAT_52")
00304 {
00305 av_destruct_packet(pkt);
00306 }
00307
00308 FF_SYMVER(int, av_new_packet, (AVPacket *pkt, int size), "LIBAVFORMAT_52")
00309 {
00310 return av_new_packet(pkt, size);
00311 }
00312
00313 FF_SYMVER(int, av_dup_packet, (AVPacket *pkt), "LIBAVFORMAT_52")
00314 {
00315 return av_dup_packet(pkt);
00316 }
00317
00318 FF_SYMVER(void, av_free_packet, (AVPacket *pkt), "LIBAVFORMAT_52")
00319 {
00320 av_free_packet(pkt);
00321 }
00322
00323 FF_SYMVER(void, av_init_packet, (AVPacket *pkt), "LIBAVFORMAT_52")
00324 {
00325 av_log(NULL, AV_LOG_WARNING, "Diverting av_*_packet function calls to libavcodec. Recompile to improve performance\n");
00326 av_init_packet(pkt);
00327 }
00328 #endif
00329
00330 int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
00331 {
00332 int ret= av_new_packet(pkt, size);
00333
00334 if(ret<0)
00335 return ret;
00336
00337 pkt->pos= avio_tell(s);
00338
00339 ret= avio_read(s, pkt->data, size);
00340 if(ret<=0)
00341 av_free_packet(pkt);
00342 else
00343 av_shrink_packet(pkt, ret);
00344
00345 return ret;
00346 }
00347
00348 int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
00349 {
00350 int ret;
00351 int old_size;
00352 if (!pkt->size)
00353 return av_get_packet(s, pkt, size);
00354 old_size = pkt->size;
00355 ret = av_grow_packet(pkt, size);
00356 if (ret < 0)
00357 return ret;
00358 ret = avio_read(s, pkt->data + old_size, size);
00359 av_shrink_packet(pkt, old_size + FFMAX(ret, 0));
00360 return ret;
00361 }
00362
00363
00364 int av_filename_number_test(const char *filename)
00365 {
00366 char buf[1024];
00367 return filename && (av_get_frame_filename(buf, sizeof(buf), filename, 1)>=0);
00368 }
00369
00370 AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened, int *score_max)
00371 {
00372 AVProbeData lpd = *pd;
00373 AVInputFormat *fmt1 = NULL, *fmt;
00374 int score;
00375
00376 if (lpd.buf_size > 10 && ff_id3v2_match(lpd.buf, ID3v2_DEFAULT_MAGIC)) {
00377 int id3len = ff_id3v2_tag_len(lpd.buf);
00378 if (lpd.buf_size > id3len + 16) {
00379 lpd.buf += id3len;
00380 lpd.buf_size -= id3len;
00381 }
00382 }
00383
00384 fmt = NULL;
00385 while ((fmt1 = av_iformat_next(fmt1))) {
00386 if (!is_opened == !(fmt1->flags & AVFMT_NOFILE))
00387 continue;
00388 score = 0;
00389 if (fmt1->read_probe) {
00390 score = fmt1->read_probe(&lpd);
00391 } else if (fmt1->extensions) {
00392 if (av_match_ext(lpd.filename, fmt1->extensions)) {
00393 score = 50;
00394 }
00395 }
00396 if (score > *score_max) {
00397 *score_max = score;
00398 fmt = fmt1;
00399 }else if (score == *score_max)
00400 fmt = NULL;
00401 }
00402 return fmt;
00403 }
00404
00405 AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened){
00406 int score=0;
00407 return av_probe_input_format2(pd, is_opened, &score);
00408 }
00409
00410 static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st, AVProbeData *pd, int score)
00411 {
00412 static const struct {
00413 const char *name; enum CodecID id; enum AVMediaType type;
00414 } fmt_id_type[] = {
00415 { "aac" , CODEC_ID_AAC , AVMEDIA_TYPE_AUDIO },
00416 { "ac3" , CODEC_ID_AC3 , AVMEDIA_TYPE_AUDIO },
00417 { "dts" , CODEC_ID_DTS , AVMEDIA_TYPE_AUDIO },
00418 { "eac3" , CODEC_ID_EAC3 , AVMEDIA_TYPE_AUDIO },
00419 { "h264" , CODEC_ID_H264 , AVMEDIA_TYPE_VIDEO },
00420 { "m4v" , CODEC_ID_MPEG4 , AVMEDIA_TYPE_VIDEO },
00421 { "mp3" , CODEC_ID_MP3 , AVMEDIA_TYPE_AUDIO },
00422 { "mpegvideo", CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
00423 { 0 }
00424 };
00425 AVInputFormat *fmt = av_probe_input_format2(pd, 1, &score);
00426
00427 if (fmt) {
00428 int i;
00429 av_log(s, AV_LOG_DEBUG, "Probe with size=%d, packets=%d detected %s with score=%d\n",
00430 pd->buf_size, MAX_PROBE_PACKETS - st->probe_packets, fmt->name, score);
00431 for (i = 0; fmt_id_type[i].name; i++) {
00432 if (!strcmp(fmt->name, fmt_id_type[i].name)) {
00433 st->codec->codec_id = fmt_id_type[i].id;
00434 st->codec->codec_type = fmt_id_type[i].type;
00435 break;
00436 }
00437 }
00438 }
00439 return !!fmt;
00440 }
00441
00442
00443
00444
00448 int av_open_input_stream(AVFormatContext **ic_ptr,
00449 AVIOContext *pb, const char *filename,
00450 AVInputFormat *fmt, AVFormatParameters *ap)
00451 {
00452 int err;
00453 AVFormatContext *ic;
00454 AVFormatParameters default_ap;
00455
00456 if(!ap){
00457 ap=&default_ap;
00458 memset(ap, 0, sizeof(default_ap));
00459 }
00460
00461 if(!ap->prealloced_context)
00462 ic = avformat_alloc_context();
00463 else
00464 ic = *ic_ptr;
00465 if (!ic) {
00466 err = AVERROR(ENOMEM);
00467 goto fail;
00468 }
00469 ic->iformat = fmt;
00470 ic->pb = pb;
00471 ic->duration = AV_NOPTS_VALUE;
00472 ic->start_time = AV_NOPTS_VALUE;
00473 av_strlcpy(ic->filename, filename, sizeof(ic->filename));
00474
00475
00476 if (fmt->priv_data_size > 0) {
00477 ic->priv_data = av_mallocz(fmt->priv_data_size);
00478 if (!ic->priv_data) {
00479 err = AVERROR(ENOMEM);
00480 goto fail;
00481 }
00482 } else {
00483 ic->priv_data = NULL;
00484 }
00485
00486
00487 if (ic->pb)
00488 ff_id3v2_read(ic, ID3v2_DEFAULT_MAGIC);
00489
00490 if (ic->iformat->read_header) {
00491 err = ic->iformat->read_header(ic, ap);
00492 if (err < 0)
00493 goto fail;
00494 }
00495
00496 if (pb && !ic->data_offset)
00497 ic->data_offset = avio_tell(ic->pb);
00498
00499 #if FF_API_OLD_METADATA
00500 ff_metadata_demux_compat(ic);
00501 #endif
00502
00503 ic->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
00504
00505 *ic_ptr = ic;
00506 return 0;
00507 fail:
00508 if (ic) {
00509 int i;
00510 av_freep(&ic->priv_data);
00511 for(i=0;i<ic->nb_streams;i++) {
00512 AVStream *st = ic->streams[i];
00513 if (st) {
00514 av_free(st->priv_data);
00515 av_free(st->codec->extradata);
00516 av_free(st->codec);
00517 av_free(st->info);
00518 }
00519 av_free(st);
00520 }
00521 }
00522 av_free(ic);
00523 *ic_ptr = NULL;
00524 return err;
00525 }
00526
00528 #define PROBE_BUF_MIN 2048
00529 #define PROBE_BUF_MAX (1<<20)
00530
00531 int av_probe_input_buffer(AVIOContext *pb, AVInputFormat **fmt,
00532 const char *filename, void *logctx,
00533 unsigned int offset, unsigned int max_probe_size)
00534 {
00535 AVProbeData pd = { filename ? filename : "", NULL, -offset };
00536 unsigned char *buf = NULL;
00537 int ret = 0, probe_size;
00538
00539 if (!max_probe_size) {
00540 max_probe_size = PROBE_BUF_MAX;
00541 } else if (max_probe_size > PROBE_BUF_MAX) {
00542 max_probe_size = PROBE_BUF_MAX;
00543 } else if (max_probe_size < PROBE_BUF_MIN) {
00544 return AVERROR(EINVAL);
00545 }
00546
00547 if (offset >= max_probe_size) {
00548 return AVERROR(EINVAL);
00549 }
00550
00551 for(probe_size= PROBE_BUF_MIN; probe_size<=max_probe_size && !*fmt && ret >= 0;
00552 probe_size = FFMIN(probe_size<<1, FFMAX(max_probe_size, probe_size+1))) {
00553 int ret, score = probe_size < max_probe_size ? AVPROBE_SCORE_MAX/4 : 0;
00554 int buf_offset = (probe_size == PROBE_BUF_MIN) ? 0 : probe_size>>1;
00555
00556 if (probe_size < offset) {
00557 continue;
00558 }
00559
00560
00561 buf = av_realloc(buf, probe_size + AVPROBE_PADDING_SIZE);
00562 if ((ret = avio_read(pb, buf + buf_offset, probe_size - buf_offset)) < 0) {
00563
00564 if (ret != AVERROR_EOF) {
00565 av_free(buf);
00566 return ret;
00567 }
00568 score = 0;
00569 ret = 0;
00570 }
00571 pd.buf_size += ret;
00572 pd.buf = &buf[offset];
00573
00574 memset(pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE);
00575
00576
00577 *fmt = av_probe_input_format2(&pd, 1, &score);
00578 if(*fmt){
00579 if(score <= AVPROBE_SCORE_MAX/4){
00580 av_log(logctx, AV_LOG_WARNING, "Format detected only with low score of %d, misdetection possible!\n", score);
00581 }else
00582 av_log(logctx, AV_LOG_DEBUG, "Probed with size=%d and score=%d\n", probe_size, score);
00583 }
00584 }
00585
00586 if (!*fmt) {
00587 av_free(buf);
00588 return AVERROR_INVALIDDATA;
00589 }
00590
00591
00592 if ((ret = ffio_rewind_with_probe_data(pb, buf, pd.buf_size)) < 0)
00593 av_free(buf);
00594
00595 return ret;
00596 }
00597
00598 int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
00599 AVInputFormat *fmt,
00600 int buf_size,
00601 AVFormatParameters *ap)
00602 {
00603 int err;
00604 AVProbeData probe_data, *pd = &probe_data;
00605 AVIOContext *pb = NULL;
00606 void *logctx= ap && ap->prealloced_context ? *ic_ptr : NULL;
00607
00608 pd->filename = "";
00609 if (filename)
00610 pd->filename = filename;
00611 pd->buf = NULL;
00612 pd->buf_size = 0;
00613
00614 if (!fmt) {
00615
00616 fmt = av_probe_input_format(pd, 0);
00617 }
00618
00619
00620
00621 if (!fmt || !(fmt->flags & AVFMT_NOFILE)) {
00622
00623 if ((err=avio_open(&pb, filename, URL_RDONLY)) < 0) {
00624 goto fail;
00625 }
00626 if (buf_size > 0) {
00627 ffio_set_buf_size(pb, buf_size);
00628 }
00629 if (!fmt && (err = av_probe_input_buffer(pb, &fmt, filename, logctx, 0, logctx ? (*ic_ptr)->probesize : 0)) < 0) {
00630 goto fail;
00631 }
00632 }
00633
00634
00635 if (!fmt) {
00636 err = AVERROR_INVALIDDATA;
00637 goto fail;
00638 }
00639
00640
00641 if (fmt->flags & AVFMT_NEEDNUMBER) {
00642 if (!av_filename_number_test(filename)) {
00643 err = AVERROR_NUMEXPECTED;
00644 goto fail;
00645 }
00646 }
00647 err = av_open_input_stream(ic_ptr, pb, filename, fmt, ap);
00648 if (err)
00649 goto fail;
00650 return 0;
00651 fail:
00652 av_freep(&pd->buf);
00653 if (pb)
00654 avio_close(pb);
00655 if (ap && ap->prealloced_context)
00656 av_free(*ic_ptr);
00657 *ic_ptr = NULL;
00658 return err;
00659
00660 }
00661
00662
00663
00664 static AVPacket *add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt,
00665 AVPacketList **plast_pktl){
00666 AVPacketList *pktl = av_mallocz(sizeof(AVPacketList));
00667 if (!pktl)
00668 return NULL;
00669
00670 if (*packet_buffer)
00671 (*plast_pktl)->next = pktl;
00672 else
00673 *packet_buffer = pktl;
00674
00675
00676 *plast_pktl = pktl;
00677 pktl->pkt= *pkt;
00678 return &pktl->pkt;
00679 }
00680
00681 int av_read_packet(AVFormatContext *s, AVPacket *pkt)
00682 {
00683 int ret, i;
00684 AVStream *st;
00685
00686 for(;;){
00687 AVPacketList *pktl = s->raw_packet_buffer;
00688
00689 if (pktl) {
00690 *pkt = pktl->pkt;
00691 if(s->streams[pkt->stream_index]->codec->codec_id != CODEC_ID_PROBE ||
00692 !s->streams[pkt->stream_index]->probe_packets ||
00693 s->raw_packet_buffer_remaining_size < pkt->size){
00694 AVProbeData *pd = &s->streams[pkt->stream_index]->probe_data;
00695 av_freep(&pd->buf);
00696 pd->buf_size = 0;
00697 s->raw_packet_buffer = pktl->next;
00698 s->raw_packet_buffer_remaining_size += pkt->size;
00699 av_free(pktl);
00700 return 0;
00701 }
00702 }
00703
00704 av_init_packet(pkt);
00705 ret= s->iformat->read_packet(s, pkt);
00706 if (ret < 0) {
00707 if (!pktl || ret == AVERROR(EAGAIN))
00708 return ret;
00709 for (i = 0; i < s->nb_streams; i++)
00710 s->streams[i]->probe_packets = 0;
00711 continue;
00712 }
00713 st= s->streams[pkt->stream_index];
00714
00715 switch(st->codec->codec_type){
00716 case AVMEDIA_TYPE_VIDEO:
00717 if(s->video_codec_id) st->codec->codec_id= s->video_codec_id;
00718 break;
00719 case AVMEDIA_TYPE_AUDIO:
00720 if(s->audio_codec_id) st->codec->codec_id= s->audio_codec_id;
00721 break;
00722 case AVMEDIA_TYPE_SUBTITLE:
00723 if(s->subtitle_codec_id)st->codec->codec_id= s->subtitle_codec_id;
00724 break;
00725 }
00726
00727 if(!pktl && (st->codec->codec_id != CODEC_ID_PROBE ||
00728 !st->probe_packets))
00729 return ret;
00730
00731 add_to_pktbuf(&s->raw_packet_buffer, pkt, &s->raw_packet_buffer_end);
00732 s->raw_packet_buffer_remaining_size -= pkt->size;
00733
00734 if(st->codec->codec_id == CODEC_ID_PROBE){
00735 AVProbeData *pd = &st->probe_data;
00736 av_log(s, AV_LOG_DEBUG, "probing stream %d\n", st->index);
00737 --st->probe_packets;
00738
00739 pd->buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
00740 memcpy(pd->buf+pd->buf_size, pkt->data, pkt->size);
00741 pd->buf_size += pkt->size;
00742 memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE);
00743
00744 if(av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)){
00745
00746 set_codec_from_probe_data(s, st, pd, st->probe_packets > 0 ? AVPROBE_SCORE_MAX/4 : 0);
00747 if(st->codec->codec_id != CODEC_ID_PROBE){
00748 pd->buf_size=0;
00749 av_freep(&pd->buf);
00750 av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index);
00751 }
00752 }
00753 }
00754 }
00755 }
00756
00757
00758
00762 static int get_audio_frame_size(AVCodecContext *enc, int size)
00763 {
00764 int frame_size;
00765
00766 if(enc->codec_id == CODEC_ID_VORBIS)
00767 return -1;
00768
00769 if (enc->frame_size <= 1) {
00770 int bits_per_sample = av_get_bits_per_sample(enc->codec_id);
00771
00772 if (bits_per_sample) {
00773 if (enc->channels == 0)
00774 return -1;
00775 frame_size = (size << 3) / (bits_per_sample * enc->channels);
00776 } else {
00777
00778 if (enc->bit_rate == 0)
00779 return -1;
00780 frame_size = ((int64_t)size * 8 * enc->sample_rate) / enc->bit_rate;
00781 }
00782 } else {
00783 frame_size = enc->frame_size;
00784 }
00785 return frame_size;
00786 }
00787
00788
00792 static void compute_frame_duration(int *pnum, int *pden, AVStream *st,
00793 AVCodecParserContext *pc, AVPacket *pkt)
00794 {
00795 int frame_size;
00796
00797 *pnum = 0;
00798 *pden = 0;
00799 switch(st->codec->codec_type) {
00800 case AVMEDIA_TYPE_VIDEO:
00801 if(st->time_base.num*1000LL > st->time_base.den){
00802 *pnum = st->time_base.num;
00803 *pden = st->time_base.den;
00804 }else if(st->codec->time_base.num*1000LL > st->codec->time_base.den){
00805 *pnum = st->codec->time_base.num;
00806 *pden = st->codec->time_base.den;
00807 if (pc && pc->repeat_pict) {
00808 *pnum = (*pnum) * (1 + pc->repeat_pict);
00809 }
00810
00811
00812 if(st->codec->ticks_per_frame>1 && !pc){
00813 *pnum = *pden = 0;
00814 }
00815 }
00816 break;
00817 case AVMEDIA_TYPE_AUDIO:
00818 frame_size = get_audio_frame_size(st->codec, pkt->size);
00819 if (frame_size <= 0 || st->codec->sample_rate <= 0)
00820 break;
00821 *pnum = frame_size;
00822 *pden = st->codec->sample_rate;
00823 break;
00824 default:
00825 break;
00826 }
00827 }
00828
00829 static int is_intra_only(AVCodecContext *enc){
00830 if(enc->codec_type == AVMEDIA_TYPE_AUDIO){
00831 return 1;
00832 }else if(enc->codec_type == AVMEDIA_TYPE_VIDEO){
00833 switch(enc->codec_id){
00834 case CODEC_ID_MJPEG:
00835 case CODEC_ID_MJPEGB:
00836 case CODEC_ID_LJPEG:
00837 case CODEC_ID_RAWVIDEO:
00838 case CODEC_ID_DVVIDEO:
00839 case CODEC_ID_HUFFYUV:
00840 case CODEC_ID_FFVHUFF:
00841 case CODEC_ID_ASV1:
00842 case CODEC_ID_ASV2:
00843 case CODEC_ID_VCR1:
00844 case CODEC_ID_DNXHD:
00845 case CODEC_ID_JPEG2000:
00846 return 1;
00847 default: break;
00848 }
00849 }
00850 return 0;
00851 }
00852
00853 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
00854 int64_t dts, int64_t pts)
00855 {
00856 AVStream *st= s->streams[stream_index];
00857 AVPacketList *pktl= s->packet_buffer;
00858
00859 if(st->first_dts != AV_NOPTS_VALUE || dts == AV_NOPTS_VALUE || st->cur_dts == AV_NOPTS_VALUE)
00860 return;
00861
00862 st->first_dts= dts - st->cur_dts;
00863 st->cur_dts= dts;
00864
00865 for(; pktl; pktl= pktl->next){
00866 if(pktl->pkt.stream_index != stream_index)
00867 continue;
00868
00869 if(pktl->pkt.pts != AV_NOPTS_VALUE && pktl->pkt.pts == pktl->pkt.dts)
00870 pktl->pkt.pts += st->first_dts;
00871
00872 if(pktl->pkt.dts != AV_NOPTS_VALUE)
00873 pktl->pkt.dts += st->first_dts;
00874
00875 if(st->start_time == AV_NOPTS_VALUE && pktl->pkt.pts != AV_NOPTS_VALUE)
00876 st->start_time= pktl->pkt.pts;
00877 }
00878 if (st->start_time == AV_NOPTS_VALUE)
00879 st->start_time = pts;
00880 }
00881
00882 static void update_initial_durations(AVFormatContext *s, AVStream *st, AVPacket *pkt)
00883 {
00884 AVPacketList *pktl= s->packet_buffer;
00885 int64_t cur_dts= 0;
00886
00887 if(st->first_dts != AV_NOPTS_VALUE){
00888 cur_dts= st->first_dts;
00889 for(; pktl; pktl= pktl->next){
00890 if(pktl->pkt.stream_index == pkt->stream_index){
00891 if(pktl->pkt.pts != pktl->pkt.dts || pktl->pkt.dts != AV_NOPTS_VALUE || pktl->pkt.duration)
00892 break;
00893 cur_dts -= pkt->duration;
00894 }
00895 }
00896 pktl= s->packet_buffer;
00897 st->first_dts = cur_dts;
00898 }else if(st->cur_dts)
00899 return;
00900
00901 for(; pktl; pktl= pktl->next){
00902 if(pktl->pkt.stream_index != pkt->stream_index)
00903 continue;
00904 if(pktl->pkt.pts == pktl->pkt.dts && pktl->pkt.dts == AV_NOPTS_VALUE
00905 && !pktl->pkt.duration){
00906 pktl->pkt.dts= cur_dts;
00907 if(!st->codec->has_b_frames)
00908 pktl->pkt.pts= cur_dts;
00909 cur_dts += pkt->duration;
00910 pktl->pkt.duration= pkt->duration;
00911 }else
00912 break;
00913 }
00914 if(st->first_dts == AV_NOPTS_VALUE)
00915 st->cur_dts= cur_dts;
00916 }
00917
00918 static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
00919 AVCodecParserContext *pc, AVPacket *pkt)
00920 {
00921 int num, den, presentation_delayed, delay, i;
00922 int64_t offset;
00923
00924 if (s->flags & AVFMT_FLAG_NOFILLIN)
00925 return;
00926
00927 if((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
00928 pkt->dts= AV_NOPTS_VALUE;
00929
00930 if (st->codec->codec_id != CODEC_ID_H264 && pc && pc->pict_type == FF_B_TYPE)
00931
00932 st->codec->has_b_frames = 1;
00933
00934
00935 delay= st->codec->has_b_frames;
00936 presentation_delayed = 0;
00937
00938
00939
00940 if (delay && st->codec->active_thread_type&FF_THREAD_FRAME)
00941 delay -= st->codec->thread_count-1;
00942
00943
00944
00945 if (delay &&
00946 pc && pc->pict_type != FF_B_TYPE)
00947 presentation_delayed = 1;
00948
00949 if(pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && pkt->dts > pkt->pts && st->pts_wrap_bits<63
00950 ){
00951 pkt->dts -= 1LL<<st->pts_wrap_bits;
00952 }
00953
00954
00955
00956
00957 if(delay==1 && pkt->dts == pkt->pts && pkt->dts != AV_NOPTS_VALUE && presentation_delayed){
00958 av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination\n");
00959 pkt->dts= pkt->pts= AV_NOPTS_VALUE;
00960 }
00961
00962 if (pkt->duration == 0) {
00963 compute_frame_duration(&num, &den, st, pc, pkt);
00964 if (den && num) {
00965 pkt->duration = av_rescale_rnd(1, num * (int64_t)st->time_base.den, den * (int64_t)st->time_base.num, AV_ROUND_DOWN);
00966
00967 if(pkt->duration != 0 && s->packet_buffer)
00968 update_initial_durations(s, st, pkt);
00969 }
00970 }
00971
00972
00973
00974 if(pc && st->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size){
00975
00976 offset = av_rescale(pc->offset, pkt->duration, pkt->size);
00977 if(pkt->pts != AV_NOPTS_VALUE)
00978 pkt->pts += offset;
00979 if(pkt->dts != AV_NOPTS_VALUE)
00980 pkt->dts += offset;
00981 }
00982
00983 if (pc && pc->dts_sync_point >= 0) {
00984
00985 int64_t den = st->codec->time_base.den * (int64_t) st->time_base.num;
00986 if (den > 0) {
00987 int64_t num = st->codec->time_base.num * (int64_t) st->time_base.den;
00988 if (pkt->dts != AV_NOPTS_VALUE) {
00989
00990 st->reference_dts = pkt->dts - pc->dts_ref_dts_delta * num / den;
00991 pkt->pts = pkt->dts + pc->pts_dts_delta * num / den;
00992 } else if (st->reference_dts != AV_NOPTS_VALUE) {
00993
00994 pkt->dts = st->reference_dts + pc->dts_ref_dts_delta * num / den;
00995 pkt->pts = pkt->dts + pc->pts_dts_delta * num / den;
00996 }
00997 if (pc->dts_sync_point > 0)
00998 st->reference_dts = pkt->dts;
00999 }
01000 }
01001
01002
01003 if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts > pkt->dts)
01004 presentation_delayed = 1;
01005
01006
01007
01008
01009 if((delay==0 || (delay==1 && pc)) && st->codec->codec_id != CODEC_ID_H264){
01010 if (presentation_delayed) {
01011
01012
01013 if (pkt->dts == AV_NOPTS_VALUE)
01014 pkt->dts = st->last_IP_pts;
01015 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts);
01016 if (pkt->dts == AV_NOPTS_VALUE)
01017 pkt->dts = st->cur_dts;
01018
01019
01020
01021 if (st->last_IP_duration == 0)
01022 st->last_IP_duration = pkt->duration;
01023 if(pkt->dts != AV_NOPTS_VALUE)
01024 st->cur_dts = pkt->dts + st->last_IP_duration;
01025 st->last_IP_duration = pkt->duration;
01026 st->last_IP_pts= pkt->pts;
01027
01028
01029 } else if(pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE || pkt->duration){
01030 if(pkt->pts != AV_NOPTS_VALUE && pkt->duration){
01031 int64_t old_diff= FFABS(st->cur_dts - pkt->duration - pkt->pts);
01032 int64_t new_diff= FFABS(st->cur_dts - pkt->pts);
01033 if(old_diff < new_diff && old_diff < (pkt->duration>>3)){
01034 pkt->pts += pkt->duration;
01035
01036 }
01037 }
01038
01039
01040 if(pkt->pts == AV_NOPTS_VALUE)
01041 pkt->pts = pkt->dts;
01042 update_initial_timestamps(s, pkt->stream_index, pkt->pts, pkt->pts);
01043 if(pkt->pts == AV_NOPTS_VALUE)
01044 pkt->pts = st->cur_dts;
01045 pkt->dts = pkt->pts;
01046 if(pkt->pts != AV_NOPTS_VALUE)
01047 st->cur_dts = pkt->pts + pkt->duration;
01048 }
01049 }
01050
01051 if(pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY){
01052 st->pts_buffer[0]= pkt->pts;
01053 for(i=0; i<delay && st->pts_buffer[i] > st->pts_buffer[i+1]; i++)
01054 FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i+1]);
01055 if(pkt->dts == AV_NOPTS_VALUE)
01056 pkt->dts= st->pts_buffer[0];
01057 if(st->codec->codec_id == CODEC_ID_H264){
01058 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts);
01059 }
01060 if(pkt->dts > st->cur_dts)
01061 st->cur_dts = pkt->dts;
01062 }
01063
01064
01065
01066
01067 if(is_intra_only(st->codec))
01068 pkt->flags |= AV_PKT_FLAG_KEY;
01069 else if (pc) {
01070 pkt->flags = 0;
01071
01072 if (pc->key_frame == 1)
01073 pkt->flags |= AV_PKT_FLAG_KEY;
01074 else if (pc->key_frame == -1 && pc->pict_type == FF_I_TYPE)
01075 pkt->flags |= AV_PKT_FLAG_KEY;
01076 }
01077 if (pc)
01078 pkt->convergence_duration = pc->convergence_duration;
01079 }
01080
01081
01082 static int av_read_frame_internal(AVFormatContext *s, AVPacket *pkt)
01083 {
01084 AVStream *st;
01085 int len, ret, i;
01086
01087 av_init_packet(pkt);
01088
01089 for(;;) {
01090
01091 st = s->cur_st;
01092 if (st) {
01093 if (!st->need_parsing || !st->parser) {
01094
01095
01096 *pkt = st->cur_pkt; st->cur_pkt.data= NULL;
01097 compute_pkt_fields(s, st, NULL, pkt);
01098 s->cur_st = NULL;
01099 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
01100 (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
01101 ff_reduce_index(s, st->index);
01102 av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
01103 }
01104 break;
01105 } else if (st->cur_len > 0 && st->discard < AVDISCARD_ALL) {
01106 len = av_parser_parse2(st->parser, st->codec, &pkt->data, &pkt->size,
01107 st->cur_ptr, st->cur_len,
01108 st->cur_pkt.pts, st->cur_pkt.dts,
01109 st->cur_pkt.pos);
01110 st->cur_pkt.pts = AV_NOPTS_VALUE;
01111 st->cur_pkt.dts = AV_NOPTS_VALUE;
01112
01113 st->cur_ptr += len;
01114 st->cur_len -= len;
01115
01116
01117 if (pkt->size) {
01118 got_packet:
01119 pkt->duration = 0;
01120 pkt->stream_index = st->index;
01121 pkt->pts = st->parser->pts;
01122 pkt->dts = st->parser->dts;
01123 pkt->pos = st->parser->pos;
01124 if(pkt->data == st->cur_pkt.data && pkt->size == st->cur_pkt.size){
01125 s->cur_st = NULL;
01126 pkt->destruct= st->cur_pkt.destruct;
01127 st->cur_pkt.destruct= NULL;
01128 st->cur_pkt.data = NULL;
01129 assert(st->cur_len == 0);
01130 }else{
01131 pkt->destruct = NULL;
01132 }
01133 compute_pkt_fields(s, st, st->parser, pkt);
01134
01135 if((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY){
01136 ff_reduce_index(s, st->index);
01137 av_add_index_entry(st, st->parser->frame_offset, pkt->dts,
01138 0, 0, AVINDEX_KEYFRAME);
01139 }
01140
01141 break;
01142 }
01143 } else {
01144
01145 av_free_packet(&st->cur_pkt);
01146 s->cur_st = NULL;
01147 }
01148 } else {
01149 AVPacket cur_pkt;
01150
01151 ret = av_read_packet(s, &cur_pkt);
01152 if (ret < 0) {
01153 if (ret == AVERROR(EAGAIN))
01154 return ret;
01155
01156 for(i = 0; i < s->nb_streams; i++) {
01157 st = s->streams[i];
01158 if (st->parser && st->need_parsing) {
01159 av_parser_parse2(st->parser, st->codec,
01160 &pkt->data, &pkt->size,
01161 NULL, 0,
01162 AV_NOPTS_VALUE, AV_NOPTS_VALUE,
01163 AV_NOPTS_VALUE);
01164 if (pkt->size)
01165 goto got_packet;
01166 }
01167 }
01168
01169 return ret;
01170 }
01171 st = s->streams[cur_pkt.stream_index];
01172 st->cur_pkt= cur_pkt;
01173
01174 if(st->cur_pkt.pts != AV_NOPTS_VALUE &&
01175 st->cur_pkt.dts != AV_NOPTS_VALUE &&
01176 st->cur_pkt.pts < st->cur_pkt.dts){
01177 av_log(s, AV_LOG_WARNING, "Invalid timestamps stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d\n",
01178 st->cur_pkt.stream_index,
01179 st->cur_pkt.pts,
01180 st->cur_pkt.dts,
01181 st->cur_pkt.size);
01182
01183
01184 }
01185
01186 if(s->debug & FF_FDEBUG_TS)
01187 av_log(s, AV_LOG_DEBUG, "av_read_packet stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d, duration=%d, flags=%d\n",
01188 st->cur_pkt.stream_index,
01189 st->cur_pkt.pts,
01190 st->cur_pkt.dts,
01191 st->cur_pkt.size,
01192 st->cur_pkt.duration,
01193 st->cur_pkt.flags);
01194
01195 s->cur_st = st;
01196 st->cur_ptr = st->cur_pkt.data;
01197 st->cur_len = st->cur_pkt.size;
01198 if (st->need_parsing && !st->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
01199 st->parser = av_parser_init(st->codec->codec_id);
01200 if (!st->parser) {
01201
01202 st->need_parsing = AVSTREAM_PARSE_NONE;
01203 }else if(st->need_parsing == AVSTREAM_PARSE_HEADERS){
01204 st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
01205 }else if(st->need_parsing == AVSTREAM_PARSE_FULL_ONCE){
01206 st->parser->flags |= PARSER_FLAG_ONCE;
01207 }
01208 }
01209 }
01210 }
01211 if(s->debug & FF_FDEBUG_TS)
01212 av_log(s, AV_LOG_DEBUG, "av_read_frame_internal stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d, duration=%d, flags=%d\n",
01213 pkt->stream_index,
01214 pkt->pts,
01215 pkt->dts,
01216 pkt->size,
01217 pkt->duration,
01218 pkt->flags);
01219
01220 return 0;
01221 }
01222
01223 int av_read_frame(AVFormatContext *s, AVPacket *pkt)
01224 {
01225 AVPacketList *pktl;
01226 int eof=0;
01227 const int genpts= s->flags & AVFMT_FLAG_GENPTS;
01228
01229 for(;;){
01230 pktl = s->packet_buffer;
01231 if (pktl) {
01232 AVPacket *next_pkt= &pktl->pkt;
01233
01234 if(genpts && next_pkt->dts != AV_NOPTS_VALUE){
01235 int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
01236 while(pktl && next_pkt->pts == AV_NOPTS_VALUE){
01237 if( pktl->pkt.stream_index == next_pkt->stream_index
01238 && (0 > av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2LL << (wrap_bits - 1)))
01239 && av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2LL << (wrap_bits - 1))) {
01240 next_pkt->pts= pktl->pkt.dts;
01241 }
01242 pktl= pktl->next;
01243 }
01244 pktl = s->packet_buffer;
01245 }
01246
01247 if( next_pkt->pts != AV_NOPTS_VALUE
01248 || next_pkt->dts == AV_NOPTS_VALUE
01249 || !genpts || eof){
01250
01251 *pkt = *next_pkt;
01252 s->packet_buffer = pktl->next;
01253 av_free(pktl);
01254 return 0;
01255 }
01256 }
01257 if(genpts){
01258 int ret= av_read_frame_internal(s, pkt);
01259 if(ret<0){
01260 if(pktl && ret != AVERROR(EAGAIN)){
01261 eof=1;
01262 continue;
01263 }else
01264 return ret;
01265 }
01266
01267 if(av_dup_packet(add_to_pktbuf(&s->packet_buffer, pkt,
01268 &s->packet_buffer_end)) < 0)
01269 return AVERROR(ENOMEM);
01270 }else{
01271 assert(!s->packet_buffer);
01272 return av_read_frame_internal(s, pkt);
01273 }
01274 }
01275 }
01276
01277
01278 static void flush_packet_queue(AVFormatContext *s)
01279 {
01280 AVPacketList *pktl;
01281
01282 for(;;) {
01283 pktl = s->packet_buffer;
01284 if (!pktl)
01285 break;
01286 s->packet_buffer = pktl->next;
01287 av_free_packet(&pktl->pkt);
01288 av_free(pktl);
01289 }
01290 while(s->raw_packet_buffer){
01291 pktl = s->raw_packet_buffer;
01292 s->raw_packet_buffer = pktl->next;
01293 av_free_packet(&pktl->pkt);
01294 av_free(pktl);
01295 }
01296 s->packet_buffer_end=
01297 s->raw_packet_buffer_end= NULL;
01298 s->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
01299 }
01300
01301
01302
01303
01304 int av_find_default_stream_index(AVFormatContext *s)
01305 {
01306 int first_audio_index = -1;
01307 int i;
01308 AVStream *st;
01309
01310 if (s->nb_streams <= 0)
01311 return -1;
01312 for(i = 0; i < s->nb_streams; i++) {
01313 st = s->streams[i];
01314 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
01315 return i;
01316 }
01317 if (first_audio_index < 0 && st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
01318 first_audio_index = i;
01319 }
01320 return first_audio_index >= 0 ? first_audio_index : 0;
01321 }
01322
01326 void ff_read_frame_flush(AVFormatContext *s)
01327 {
01328 AVStream *st;
01329 int i, j;
01330
01331 flush_packet_queue(s);
01332
01333 s->cur_st = NULL;
01334
01335
01336 for(i = 0; i < s->nb_streams; i++) {
01337 st = s->streams[i];
01338
01339 if (st->parser) {
01340 av_parser_close(st->parser);
01341 st->parser = NULL;
01342 av_free_packet(&st->cur_pkt);
01343 }
01344 st->last_IP_pts = AV_NOPTS_VALUE;
01345 st->cur_dts = AV_NOPTS_VALUE;
01346 st->reference_dts = AV_NOPTS_VALUE;
01347
01348 st->cur_ptr = NULL;
01349 st->cur_len = 0;
01350
01351 st->probe_packets = MAX_PROBE_PACKETS;
01352
01353 for(j=0; j<MAX_REORDER_DELAY+1; j++)
01354 st->pts_buffer[j]= AV_NOPTS_VALUE;
01355 }
01356 }
01357
01358 void av_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp){
01359 int i;
01360
01361 for(i = 0; i < s->nb_streams; i++) {
01362 AVStream *st = s->streams[i];
01363
01364 st->cur_dts = av_rescale(timestamp,
01365 st->time_base.den * (int64_t)ref_st->time_base.num,
01366 st->time_base.num * (int64_t)ref_st->time_base.den);
01367 }
01368 }
01369
01370 void ff_reduce_index(AVFormatContext *s, int stream_index)
01371 {
01372 AVStream *st= s->streams[stream_index];
01373 unsigned int max_entries= s->max_index_size / sizeof(AVIndexEntry);
01374
01375 if((unsigned)st->nb_index_entries >= max_entries){
01376 int i;
01377 for(i=0; 2*i<st->nb_index_entries; i++)
01378 st->index_entries[i]= st->index_entries[2*i];
01379 st->nb_index_entries= i;
01380 }
01381 }
01382
01383 int ff_add_index_entry(AVIndexEntry **index_entries,
01384 int *nb_index_entries,
01385 unsigned int *index_entries_allocated_size,
01386 int64_t pos, int64_t timestamp, int size, int distance, int flags)
01387 {
01388 AVIndexEntry *entries, *ie;
01389 int index;
01390
01391 if((unsigned)*nb_index_entries + 1 >= UINT_MAX / sizeof(AVIndexEntry))
01392 return -1;
01393
01394 entries = av_fast_realloc(*index_entries,
01395 index_entries_allocated_size,
01396 (*nb_index_entries + 1) *
01397 sizeof(AVIndexEntry));
01398 if(!entries)
01399 return -1;
01400
01401 *index_entries= entries;
01402
01403 index= ff_index_search_timestamp(*index_entries, *nb_index_entries, timestamp, AVSEEK_FLAG_ANY);
01404
01405 if(index<0){
01406 index= (*nb_index_entries)++;
01407 ie= &entries[index];
01408 assert(index==0 || ie[-1].timestamp < timestamp);
01409 }else{
01410 ie= &entries[index];
01411 if(ie->timestamp != timestamp){
01412 if(ie->timestamp <= timestamp)
01413 return -1;
01414 memmove(entries + index + 1, entries + index, sizeof(AVIndexEntry)*(*nb_index_entries - index));
01415 (*nb_index_entries)++;
01416 }else if(ie->pos == pos && distance < ie->min_distance)
01417 distance= ie->min_distance;
01418 }
01419
01420 ie->pos = pos;
01421 ie->timestamp = timestamp;
01422 ie->min_distance= distance;
01423 ie->size= size;
01424 ie->flags = flags;
01425
01426 return index;
01427 }
01428
01429 int av_add_index_entry(AVStream *st,
01430 int64_t pos, int64_t timestamp, int size, int distance, int flags)
01431 {
01432 return ff_add_index_entry(&st->index_entries, &st->nb_index_entries,
01433 &st->index_entries_allocated_size, pos,
01434 timestamp, size, distance, flags);
01435 }
01436
01437 int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries,
01438 int64_t wanted_timestamp, int flags)
01439 {
01440 int a, b, m;
01441 int64_t timestamp;
01442
01443 a = - 1;
01444 b = nb_entries;
01445
01446
01447 if(b && entries[b-1].timestamp < wanted_timestamp)
01448 a= b-1;
01449
01450 while (b - a > 1) {
01451 m = (a + b) >> 1;
01452 timestamp = entries[m].timestamp;
01453 if(timestamp >= wanted_timestamp)
01454 b = m;
01455 if(timestamp <= wanted_timestamp)
01456 a = m;
01457 }
01458 m= (flags & AVSEEK_FLAG_BACKWARD) ? a : b;
01459
01460 if(!(flags & AVSEEK_FLAG_ANY)){
01461 while(m>=0 && m<nb_entries && !(entries[m].flags & AVINDEX_KEYFRAME)){
01462 m += (flags & AVSEEK_FLAG_BACKWARD) ? -1 : 1;
01463 }
01464 }
01465
01466 if(m == nb_entries)
01467 return -1;
01468 return m;
01469 }
01470
01471 int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp,
01472 int flags)
01473 {
01474 return ff_index_search_timestamp(st->index_entries, st->nb_index_entries,
01475 wanted_timestamp, flags);
01476 }
01477
01478 #define DEBUG_SEEK
01479
01480 int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
01481 AVInputFormat *avif= s->iformat;
01482 int64_t av_uninit(pos_min), av_uninit(pos_max), pos, pos_limit;
01483 int64_t ts_min, ts_max, ts;
01484 int index;
01485 int64_t ret;
01486 AVStream *st;
01487
01488 if (stream_index < 0)
01489 return -1;
01490
01491 #ifdef DEBUG_SEEK
01492 av_log(s, AV_LOG_DEBUG, "read_seek: %d %"PRId64"\n", stream_index, target_ts);
01493 #endif
01494
01495 ts_max=
01496 ts_min= AV_NOPTS_VALUE;
01497 pos_limit= -1;
01498
01499 st= s->streams[stream_index];
01500 if(st->index_entries){
01501 AVIndexEntry *e;
01502
01503 index= av_index_search_timestamp(st, target_ts, flags | AVSEEK_FLAG_BACKWARD);
01504 index= FFMAX(index, 0);
01505 e= &st->index_entries[index];
01506
01507 if(e->timestamp <= target_ts || e->pos == e->min_distance){
01508 pos_min= e->pos;
01509 ts_min= e->timestamp;
01510 #ifdef DEBUG_SEEK
01511 av_log(s, AV_LOG_DEBUG, "using cached pos_min=0x%"PRIx64" dts_min=%"PRId64"\n",
01512 pos_min,ts_min);
01513 #endif
01514 }else{
01515 assert(index==0);
01516 }
01517
01518 index= av_index_search_timestamp(st, target_ts, flags & ~AVSEEK_FLAG_BACKWARD);
01519 assert(index < st->nb_index_entries);
01520 if(index >= 0){
01521 e= &st->index_entries[index];
01522 assert(e->timestamp >= target_ts);
01523 pos_max= e->pos;
01524 ts_max= e->timestamp;
01525 pos_limit= pos_max - e->min_distance;
01526 #ifdef DEBUG_SEEK
01527 av_log(s, AV_LOG_DEBUG, "using cached pos_max=0x%"PRIx64" pos_limit=0x%"PRIx64" dts_max=%"PRId64"\n",
01528 pos_max,pos_limit, ts_max);
01529 #endif
01530 }
01531 }
01532
01533 pos= av_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit, ts_min, ts_max, flags, &ts, avif->read_timestamp);
01534 if(pos<0)
01535 return -1;
01536
01537
01538 if ((ret = avio_seek(s->pb, pos, SEEK_SET)) < 0)
01539 return ret;
01540
01541 av_update_cur_dts(s, st, ts);
01542
01543 return 0;
01544 }
01545
01546 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 )){
01547 int64_t pos, ts;
01548 int64_t start_pos, filesize;
01549 int no_change;
01550
01551 #ifdef DEBUG_SEEK
01552 av_log(s, AV_LOG_DEBUG, "gen_seek: %d %"PRId64"\n", stream_index, target_ts);
01553 #endif
01554
01555 if(ts_min == AV_NOPTS_VALUE){
01556 pos_min = s->data_offset;
01557 ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
01558 if (ts_min == AV_NOPTS_VALUE)
01559 return -1;
01560 }
01561
01562 if(ts_max == AV_NOPTS_VALUE){
01563 int step= 1024;
01564 filesize = avio_size(s->pb);
01565 pos_max = filesize - 1;
01566 do{
01567 pos_max -= step;
01568 ts_max = read_timestamp(s, stream_index, &pos_max, pos_max + step);
01569 step += step;
01570 }while(ts_max == AV_NOPTS_VALUE && pos_max >= step);
01571 if (ts_max == AV_NOPTS_VALUE)
01572 return -1;
01573
01574 for(;;){
01575 int64_t tmp_pos= pos_max + 1;
01576 int64_t tmp_ts= read_timestamp(s, stream_index, &tmp_pos, INT64_MAX);
01577 if(tmp_ts == AV_NOPTS_VALUE)
01578 break;
01579 ts_max= tmp_ts;
01580 pos_max= tmp_pos;
01581 if(tmp_pos >= filesize)
01582 break;
01583 }
01584 pos_limit= pos_max;
01585 }
01586
01587 if(ts_min > ts_max){
01588 return -1;
01589 }else if(ts_min == ts_max){
01590 pos_limit= pos_min;
01591 }
01592
01593 no_change=0;
01594 while (pos_min < pos_limit) {
01595 #ifdef DEBUG_SEEK
01596 av_log(s, AV_LOG_DEBUG, "pos_min=0x%"PRIx64" pos_max=0x%"PRIx64" dts_min=%"PRId64" dts_max=%"PRId64"\n",
01597 pos_min, pos_max,
01598 ts_min, ts_max);
01599 #endif
01600 assert(pos_limit <= pos_max);
01601
01602 if(no_change==0){
01603 int64_t approximate_keyframe_distance= pos_max - pos_limit;
01604
01605 pos = av_rescale(target_ts - ts_min, pos_max - pos_min, ts_max - ts_min)
01606 + pos_min - approximate_keyframe_distance;
01607 }else if(no_change==1){
01608
01609 pos = (pos_min + pos_limit)>>1;
01610 }else{
01611
01612
01613 pos=pos_min;
01614 }
01615 if(pos <= pos_min)
01616 pos= pos_min + 1;
01617 else if(pos > pos_limit)
01618 pos= pos_limit;
01619 start_pos= pos;
01620
01621 ts = read_timestamp(s, stream_index, &pos, INT64_MAX);
01622 if(pos == pos_max)
01623 no_change++;
01624 else
01625 no_change=0;
01626 #ifdef DEBUG_SEEK
01627 av_log(s, AV_LOG_DEBUG, "%"PRId64" %"PRId64" %"PRId64" / %"PRId64" %"PRId64" %"PRId64" target:%"PRId64" limit:%"PRId64" start:%"PRId64" noc:%d\n",
01628 pos_min, pos, pos_max, ts_min, ts, ts_max, target_ts, pos_limit,
01629 start_pos, no_change);
01630 #endif
01631 if(ts == AV_NOPTS_VALUE){
01632 av_log(s, AV_LOG_ERROR, "read_timestamp() failed in the middle\n");
01633 return -1;
01634 }
01635 assert(ts != AV_NOPTS_VALUE);
01636 if (target_ts <= ts) {
01637 pos_limit = start_pos - 1;
01638 pos_max = pos;
01639 ts_max = ts;
01640 }
01641 if (target_ts >= ts) {
01642 pos_min = pos;
01643 ts_min = ts;
01644 }
01645 }
01646
01647 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
01648 ts = (flags & AVSEEK_FLAG_BACKWARD) ? ts_min : ts_max;
01649 #ifdef DEBUG_SEEK
01650 pos_min = pos;
01651 ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
01652 pos_min++;
01653 ts_max = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
01654 av_log(s, AV_LOG_DEBUG, "pos=0x%"PRIx64" %"PRId64"<=%"PRId64"<=%"PRId64"\n",
01655 pos, ts_min, target_ts, ts_max);
01656 #endif
01657 *ts_ret= ts;
01658 return pos;
01659 }
01660
01661 static int av_seek_frame_byte(AVFormatContext *s, int stream_index, int64_t pos, int flags){
01662 int64_t pos_min, pos_max;
01663 #if 0
01664 AVStream *st;
01665
01666 if (stream_index < 0)
01667 return -1;
01668
01669 st= s->streams[stream_index];
01670 #endif
01671
01672 pos_min = s->data_offset;
01673 pos_max = avio_size(s->pb) - 1;
01674
01675 if (pos < pos_min) pos= pos_min;
01676 else if(pos > pos_max) pos= pos_max;
01677
01678 avio_seek(s->pb, pos, SEEK_SET);
01679
01680 #if 0
01681 av_update_cur_dts(s, st, ts);
01682 #endif
01683 return 0;
01684 }
01685
01686 static int av_seek_frame_generic(AVFormatContext *s,
01687 int stream_index, int64_t timestamp, int flags)
01688 {
01689 int index;
01690 int64_t ret;
01691 AVStream *st;
01692 AVIndexEntry *ie;
01693
01694 st = s->streams[stream_index];
01695
01696 index = av_index_search_timestamp(st, timestamp, flags);
01697
01698 if(index < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
01699 return -1;
01700
01701 if(index < 0 || index==st->nb_index_entries-1){
01702 int i;
01703 AVPacket pkt;
01704
01705 if(st->nb_index_entries){
01706 assert(st->index_entries);
01707 ie= &st->index_entries[st->nb_index_entries-1];
01708 if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
01709 return ret;
01710 av_update_cur_dts(s, st, ie->timestamp);
01711 }else{
01712 if ((ret = avio_seek(s->pb, s->data_offset, SEEK_SET)) < 0)
01713 return ret;
01714 }
01715 for(i=0;; i++) {
01716 int ret;
01717 do{
01718 ret = av_read_frame(s, &pkt);
01719 }while(ret == AVERROR(EAGAIN));
01720 if(ret<0)
01721 break;
01722 av_free_packet(&pkt);
01723 if(stream_index == pkt.stream_index){
01724 if((pkt.flags & AV_PKT_FLAG_KEY) && pkt.dts > timestamp)
01725 break;
01726 }
01727 }
01728 index = av_index_search_timestamp(st, timestamp, flags);
01729 }
01730 if (index < 0)
01731 return -1;
01732
01733 ff_read_frame_flush(s);
01734 if (s->iformat->read_seek){
01735 if(s->iformat->read_seek(s, stream_index, timestamp, flags) >= 0)
01736 return 0;
01737 }
01738 ie = &st->index_entries[index];
01739 if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
01740 return ret;
01741 av_update_cur_dts(s, st, ie->timestamp);
01742
01743 return 0;
01744 }
01745
01746 int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
01747 {
01748 int ret;
01749 AVStream *st;
01750
01751 ff_read_frame_flush(s);
01752
01753 if(flags & AVSEEK_FLAG_BYTE)
01754 return av_seek_frame_byte(s, stream_index, timestamp, flags);
01755
01756 if(stream_index < 0){
01757 stream_index= av_find_default_stream_index(s);
01758 if(stream_index < 0)
01759 return -1;
01760
01761 st= s->streams[stream_index];
01762
01763 timestamp = av_rescale(timestamp, st->time_base.den, AV_TIME_BASE * (int64_t)st->time_base.num);
01764 }
01765
01766
01767 if (s->iformat->read_seek)
01768 ret = s->iformat->read_seek(s, stream_index, timestamp, flags);
01769 else
01770 ret = -1;
01771 if (ret >= 0) {
01772 return 0;
01773 }
01774
01775 if(s->iformat->read_timestamp)
01776 return av_seek_frame_binary(s, stream_index, timestamp, flags);
01777 else
01778 return av_seek_frame_generic(s, stream_index, timestamp, flags);
01779 }
01780
01781 int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
01782 {
01783 if(min_ts > ts || max_ts < ts)
01784 return -1;
01785
01786 ff_read_frame_flush(s);
01787
01788 if (s->iformat->read_seek2)
01789 return s->iformat->read_seek2(s, stream_index, min_ts, ts, max_ts, flags);
01790
01791 if(s->iformat->read_timestamp){
01792
01793 }
01794
01795
01796
01797 if(s->iformat->read_seek || 1)
01798 return av_seek_frame(s, stream_index, ts, flags | (ts - min_ts > (uint64_t)(max_ts - ts) ? AVSEEK_FLAG_BACKWARD : 0));
01799
01800
01801 }
01802
01803
01804
01810 static int av_has_duration(AVFormatContext *ic)
01811 {
01812 int i;
01813 AVStream *st;
01814
01815 for(i = 0;i < ic->nb_streams; i++) {
01816 st = ic->streams[i];
01817 if (st->duration != AV_NOPTS_VALUE)
01818 return 1;
01819 }
01820 return 0;
01821 }
01822
01828 static void av_update_stream_timings(AVFormatContext *ic)
01829 {
01830 int64_t start_time, start_time1, end_time, end_time1;
01831 int64_t duration, duration1;
01832 int i;
01833 AVStream *st;
01834
01835 start_time = INT64_MAX;
01836 end_time = INT64_MIN;
01837 duration = INT64_MIN;
01838 for(i = 0;i < ic->nb_streams; i++) {
01839 st = ic->streams[i];
01840 if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
01841 start_time1= av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q);
01842 if (start_time1 < start_time)
01843 start_time = start_time1;
01844 if (st->duration != AV_NOPTS_VALUE) {
01845 end_time1 = start_time1
01846 + av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
01847 if (end_time1 > end_time)
01848 end_time = end_time1;
01849 }
01850 }
01851 if (st->duration != AV_NOPTS_VALUE) {
01852 duration1 = av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
01853 if (duration1 > duration)
01854 duration = duration1;
01855 }
01856 }
01857 if (start_time != INT64_MAX) {
01858 ic->start_time = start_time;
01859 if (end_time != INT64_MIN) {
01860 if (end_time - start_time > duration)
01861 duration = end_time - start_time;
01862 }
01863 }
01864 if (duration != INT64_MIN) {
01865 ic->duration = duration;
01866 if (ic->file_size > 0) {
01867
01868 ic->bit_rate = (double)ic->file_size * 8.0 * AV_TIME_BASE /
01869 (double)ic->duration;
01870 }
01871 }
01872 }
01873
01874 static void fill_all_stream_timings(AVFormatContext *ic)
01875 {
01876 int i;
01877 AVStream *st;
01878
01879 av_update_stream_timings(ic);
01880 for(i = 0;i < ic->nb_streams; i++) {
01881 st = ic->streams[i];
01882 if (st->start_time == AV_NOPTS_VALUE) {
01883 if(ic->start_time != AV_NOPTS_VALUE)
01884 st->start_time = av_rescale_q(ic->start_time, AV_TIME_BASE_Q, st->time_base);
01885 if(ic->duration != AV_NOPTS_VALUE)
01886 st->duration = av_rescale_q(ic->duration, AV_TIME_BASE_Q, st->time_base);
01887 }
01888 }
01889 }
01890
01891 static void av_estimate_timings_from_bit_rate(AVFormatContext *ic)
01892 {
01893 int64_t filesize, duration;
01894 int bit_rate, i;
01895 AVStream *st;
01896
01897
01898 if (ic->bit_rate <= 0) {
01899 bit_rate = 0;
01900 for(i=0;i<ic->nb_streams;i++) {
01901 st = ic->streams[i];
01902 if (st->codec->bit_rate > 0)
01903 bit_rate += st->codec->bit_rate;
01904 }
01905 ic->bit_rate = bit_rate;
01906 }
01907
01908
01909 if (ic->duration == AV_NOPTS_VALUE &&
01910 ic->bit_rate != 0 &&
01911 ic->file_size != 0) {
01912 filesize = ic->file_size;
01913 if (filesize > 0) {
01914 for(i = 0; i < ic->nb_streams; i++) {
01915 st = ic->streams[i];
01916 duration= av_rescale(8*filesize, st->time_base.den, ic->bit_rate*(int64_t)st->time_base.num);
01917 if (st->duration == AV_NOPTS_VALUE)
01918 st->duration = duration;
01919 }
01920 }
01921 }
01922 }
01923
01924 #define DURATION_MAX_READ_SIZE 250000
01925 #define DURATION_MAX_RETRY 3
01926
01927
01928 static void av_estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
01929 {
01930 AVPacket pkt1, *pkt = &pkt1;
01931 AVStream *st;
01932 int read_size, i, ret;
01933 int64_t end_time;
01934 int64_t filesize, offset, duration;
01935 int retry=0;
01936
01937 ic->cur_st = NULL;
01938
01939
01940 flush_packet_queue(ic);
01941
01942 for (i=0; i<ic->nb_streams; i++) {
01943 st = ic->streams[i];
01944 if (st->start_time == AV_NOPTS_VALUE && st->first_dts == AV_NOPTS_VALUE)
01945 av_log(st->codec, AV_LOG_WARNING, "start time is not set in av_estimate_timings_from_pts\n");
01946
01947 if (st->parser) {
01948 av_parser_close(st->parser);
01949 st->parser= NULL;
01950 av_free_packet(&st->cur_pkt);
01951 }
01952 }
01953
01954
01955
01956 filesize = ic->file_size;
01957 end_time = AV_NOPTS_VALUE;
01958 do{
01959 offset = filesize - (DURATION_MAX_READ_SIZE<<retry);
01960 if (offset < 0)
01961 offset = 0;
01962
01963 avio_seek(ic->pb, offset, SEEK_SET);
01964 read_size = 0;
01965 for(;;) {
01966 if (read_size >= DURATION_MAX_READ_SIZE<<(FFMAX(retry-1,0)))
01967 break;
01968
01969 do{
01970 ret = av_read_packet(ic, pkt);
01971 }while(ret == AVERROR(EAGAIN));
01972 if (ret != 0)
01973 break;
01974 read_size += pkt->size;
01975 st = ic->streams[pkt->stream_index];
01976 if (pkt->pts != AV_NOPTS_VALUE &&
01977 (st->start_time != AV_NOPTS_VALUE ||
01978 st->first_dts != AV_NOPTS_VALUE)) {
01979 duration = end_time = pkt->pts;
01980 if (st->start_time != AV_NOPTS_VALUE) duration -= st->start_time;
01981 else duration -= st->first_dts;
01982 if (duration < 0)
01983 duration += 1LL<<st->pts_wrap_bits;
01984 if (duration > 0) {
01985 if (st->duration == AV_NOPTS_VALUE ||
01986 st->duration < duration)
01987 st->duration = duration;
01988 }
01989 }
01990 av_free_packet(pkt);
01991 }
01992 }while( end_time==AV_NOPTS_VALUE
01993 && filesize > (DURATION_MAX_READ_SIZE<<retry)
01994 && ++retry <= DURATION_MAX_RETRY);
01995
01996 fill_all_stream_timings(ic);
01997
01998 avio_seek(ic->pb, old_offset, SEEK_SET);
01999 for (i=0; i<ic->nb_streams; i++) {
02000 st= ic->streams[i];
02001 st->cur_dts= st->first_dts;
02002 st->last_IP_pts = AV_NOPTS_VALUE;
02003 }
02004 }
02005
02006 static void av_estimate_timings(AVFormatContext *ic, int64_t old_offset)
02007 {
02008 int64_t file_size;
02009
02010
02011 if (ic->iformat->flags & AVFMT_NOFILE) {
02012 file_size = 0;
02013 } else {
02014 file_size = avio_size(ic->pb);
02015 if (file_size < 0)
02016 file_size = 0;
02017 }
02018 ic->file_size = file_size;
02019
02020 if ((!strcmp(ic->iformat->name, "mpeg") ||
02021 !strcmp(ic->iformat->name, "mpegts")) &&
02022 file_size && !url_is_streamed(ic->pb)) {
02023
02024 av_estimate_timings_from_pts(ic, old_offset);
02025 } else if (av_has_duration(ic)) {
02026
02027
02028 fill_all_stream_timings(ic);
02029 } else {
02030 av_log(ic, AV_LOG_WARNING, "Estimating duration from bitrate, this may be inaccurate\n");
02031
02032 av_estimate_timings_from_bit_rate(ic);
02033 }
02034 av_update_stream_timings(ic);
02035
02036 #if 0
02037 {
02038 int i;
02039 AVStream *st;
02040 for(i = 0;i < ic->nb_streams; i++) {
02041 st = ic->streams[i];
02042 printf("%d: start_time: %0.3f duration: %0.3f\n",
02043 i, (double)st->start_time / AV_TIME_BASE,
02044 (double)st->duration / AV_TIME_BASE);
02045 }
02046 printf("stream: start_time: %0.3f duration: %0.3f bitrate=%d kb/s\n",
02047 (double)ic->start_time / AV_TIME_BASE,
02048 (double)ic->duration / AV_TIME_BASE,
02049 ic->bit_rate / 1000);
02050 }
02051 #endif
02052 }
02053
02054 static int has_codec_parameters(AVCodecContext *enc)
02055 {
02056 int val;
02057 switch(enc->codec_type) {
02058 case AVMEDIA_TYPE_AUDIO:
02059 val = enc->sample_rate && enc->channels && enc->sample_fmt != AV_SAMPLE_FMT_NONE;
02060 if(!enc->frame_size &&
02061 (enc->codec_id == CODEC_ID_VORBIS ||
02062 enc->codec_id == CODEC_ID_AAC ||
02063 enc->codec_id == CODEC_ID_MP1 ||
02064 enc->codec_id == CODEC_ID_MP2 ||
02065 enc->codec_id == CODEC_ID_MP3 ||
02066 enc->codec_id == CODEC_ID_SPEEX))
02067 return 0;
02068 break;
02069 case AVMEDIA_TYPE_VIDEO:
02070 val = enc->width && enc->pix_fmt != PIX_FMT_NONE;
02071 break;
02072 default:
02073 val = 1;
02074 break;
02075 }
02076 return enc->codec_id != CODEC_ID_NONE && val != 0;
02077 }
02078
02079 static int has_decode_delay_been_guessed(AVStream *st)
02080 {
02081 return st->codec->codec_id != CODEC_ID_H264 ||
02082 st->codec_info_nb_frames >= 6 + st->codec->has_b_frames;
02083 }
02084
02085 static int try_decode_frame(AVStream *st, AVPacket *avpkt)
02086 {
02087 int16_t *samples;
02088 AVCodec *codec;
02089 int got_picture, data_size, ret=0;
02090 AVFrame picture;
02091
02092 if(!st->codec->codec){
02093 codec = avcodec_find_decoder(st->codec->codec_id);
02094 if (!codec)
02095 return -1;
02096 ret = avcodec_open(st->codec, codec);
02097 if (ret < 0)
02098 return ret;
02099 }
02100
02101 if(!has_codec_parameters(st->codec) || !has_decode_delay_been_guessed(st)){
02102 switch(st->codec->codec_type) {
02103 case AVMEDIA_TYPE_VIDEO:
02104 avcodec_get_frame_defaults(&picture);
02105 ret = avcodec_decode_video2(st->codec, &picture,
02106 &got_picture, avpkt);
02107 break;
02108 case AVMEDIA_TYPE_AUDIO:
02109 data_size = FFMAX(avpkt->size, AVCODEC_MAX_AUDIO_FRAME_SIZE);
02110 samples = av_malloc(data_size);
02111 if (!samples)
02112 goto fail;
02113 ret = avcodec_decode_audio3(st->codec, samples,
02114 &data_size, avpkt);
02115 av_free(samples);
02116 break;
02117 default:
02118 break;
02119 }
02120 }
02121 fail:
02122 return ret;
02123 }
02124
02125 unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum CodecID id)
02126 {
02127 while (tags->id != CODEC_ID_NONE) {
02128 if (tags->id == id)
02129 return tags->tag;
02130 tags++;
02131 }
02132 return 0;
02133 }
02134
02135 enum CodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
02136 {
02137 int i;
02138 for(i=0; tags[i].id != CODEC_ID_NONE;i++) {
02139 if(tag == tags[i].tag)
02140 return tags[i].id;
02141 }
02142 for(i=0; tags[i].id != CODEC_ID_NONE; i++) {
02143 if (ff_toupper4(tag) == ff_toupper4(tags[i].tag))
02144 return tags[i].id;
02145 }
02146 return CODEC_ID_NONE;
02147 }
02148
02149 unsigned int av_codec_get_tag(const AVCodecTag * const *tags, enum CodecID id)
02150 {
02151 int i;
02152 for(i=0; tags && tags[i]; i++){
02153 int tag= ff_codec_get_tag(tags[i], id);
02154 if(tag) return tag;
02155 }
02156 return 0;
02157 }
02158
02159 enum CodecID av_codec_get_id(const AVCodecTag * const *tags, unsigned int tag)
02160 {
02161 int i;
02162 for(i=0; tags && tags[i]; i++){
02163 enum CodecID id= ff_codec_get_id(tags[i], tag);
02164 if(id!=CODEC_ID_NONE) return id;
02165 }
02166 return CODEC_ID_NONE;
02167 }
02168
02169 static void compute_chapters_end(AVFormatContext *s)
02170 {
02171 unsigned int i;
02172
02173 for (i=0; i+1<s->nb_chapters; i++)
02174 if (s->chapters[i]->end == AV_NOPTS_VALUE) {
02175 assert(s->chapters[i]->start <= s->chapters[i+1]->start);
02176 assert(!av_cmp_q(s->chapters[i]->time_base, s->chapters[i+1]->time_base));
02177 s->chapters[i]->end = s->chapters[i+1]->start;
02178 }
02179
02180 if (s->nb_chapters && s->chapters[i]->end == AV_NOPTS_VALUE) {
02181 assert(s->start_time != AV_NOPTS_VALUE);
02182 assert(s->duration > 0);
02183 s->chapters[i]->end = av_rescale_q(s->start_time + s->duration,
02184 AV_TIME_BASE_Q,
02185 s->chapters[i]->time_base);
02186 }
02187 }
02188
02189 static int get_std_framerate(int i){
02190 if(i<60*12) return i*1001;
02191 else return ((const int[]){24,30,60,12,15})[i-60*12]*1000*12;
02192 }
02193
02194
02195
02196
02197
02198
02199
02200
02201
02202 static int tb_unreliable(AVCodecContext *c){
02203 if( c->time_base.den >= 101L*c->time_base.num
02204 || c->time_base.den < 5L*c->time_base.num
02205
02206
02207 || c->codec_id == CODEC_ID_MPEG2VIDEO
02208 || c->codec_id == CODEC_ID_H264
02209 )
02210 return 1;
02211 return 0;
02212 }
02213
02214 int av_find_stream_info(AVFormatContext *ic)
02215 {
02216 int i, count, ret, read_size, j;
02217 AVStream *st;
02218 AVPacket pkt1, *pkt;
02219 int64_t old_offset = avio_tell(ic->pb);
02220
02221 for(i=0;i<ic->nb_streams;i++) {
02222 AVCodec *codec;
02223 st = ic->streams[i];
02224 if (st->codec->codec_id == CODEC_ID_AAC) {
02225 st->codec->sample_rate = 0;
02226 st->codec->frame_size = 0;
02227 st->codec->channels = 0;
02228 }
02229 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
02230 st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
02231
02232
02233 if(!st->codec->time_base.num)
02234 st->codec->time_base= st->time_base;
02235 }
02236
02237 if (!st->parser && !(ic->flags & AVFMT_FLAG_NOPARSE)) {
02238 st->parser = av_parser_init(st->codec->codec_id);
02239 if(st->need_parsing == AVSTREAM_PARSE_HEADERS && st->parser){
02240 st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
02241 }
02242 }
02243 assert(!st->codec->codec);
02244 codec = avcodec_find_decoder(st->codec->codec_id);
02245
02246
02247
02248
02249
02250 if (codec && codec->capabilities & CODEC_CAP_CHANNEL_CONF)
02251 st->codec->channels = 0;
02252
02253
02254 if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE
02255 && codec && !st->codec->codec)
02256 avcodec_open(st->codec, codec);
02257
02258
02259 if(!has_codec_parameters(st->codec)){
02260 if (codec && !st->codec->codec)
02261 avcodec_open(st->codec, codec);
02262 }
02263 }
02264
02265 for (i=0; i<ic->nb_streams; i++) {
02266 ic->streams[i]->info->last_dts = AV_NOPTS_VALUE;
02267 }
02268
02269 count = 0;
02270 read_size = 0;
02271 for(;;) {
02272 if(url_interrupt_cb()){
02273 ret= AVERROR_EXIT;
02274 av_log(ic, AV_LOG_DEBUG, "interrupted\n");
02275 break;
02276 }
02277
02278
02279 for(i=0;i<ic->nb_streams;i++) {
02280 st = ic->streams[i];
02281 if (!has_codec_parameters(st->codec))
02282 break;
02283
02284 if( tb_unreliable(st->codec) && !(st->r_frame_rate.num && st->avg_frame_rate.num)
02285 && st->info->duration_count<20 && st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
02286 break;
02287 if(st->parser && st->parser->parser->split && !st->codec->extradata)
02288 break;
02289 if(st->first_dts == AV_NOPTS_VALUE)
02290 break;
02291 }
02292 if (i == ic->nb_streams) {
02293
02294
02295
02296 if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
02297
02298 ret = count;
02299 av_log(ic, AV_LOG_DEBUG, "All info found\n");
02300 break;
02301 }
02302 }
02303
02304 if (read_size >= ic->probesize) {
02305 ret = count;
02306 av_log(ic, AV_LOG_DEBUG, "Probe buffer size limit %d reached\n", ic->probesize);
02307 break;
02308 }
02309
02310
02311
02312 ret = av_read_frame_internal(ic, &pkt1);
02313 if (ret < 0 && ret != AVERROR(EAGAIN)) {
02314
02315 ret = -1;
02316 for(i=0;i<ic->nb_streams;i++) {
02317 st = ic->streams[i];
02318 if (!has_codec_parameters(st->codec)){
02319 char buf[256];
02320 avcodec_string(buf, sizeof(buf), st->codec, 0);
02321 av_log(ic, AV_LOG_WARNING, "Could not find codec parameters (%s)\n", buf);
02322 } else {
02323 ret = 0;
02324 }
02325 }
02326 break;
02327 }
02328
02329 if (ret == AVERROR(EAGAIN))
02330 continue;
02331
02332 pkt= add_to_pktbuf(&ic->packet_buffer, &pkt1, &ic->packet_buffer_end);
02333 if ((ret = av_dup_packet(pkt)) < 0)
02334 goto find_stream_info_err;
02335
02336 read_size += pkt->size;
02337
02338 st = ic->streams[pkt->stream_index];
02339 if (st->codec_info_nb_frames>1) {
02340 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) {
02341 av_log(ic, AV_LOG_WARNING, "max_analyze_duration reached\n");
02342 break;
02343 }
02344 st->info->codec_info_duration += pkt->duration;
02345 }
02346 {
02347 int64_t last = st->info->last_dts;
02348 int64_t duration= pkt->dts - last;
02349
02350 if(pkt->dts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && duration>0){
02351 double dur= duration * av_q2d(st->time_base);
02352
02353
02354
02355 if (st->info->duration_count < 2)
02356 memset(st->info->duration_error, 0, sizeof(st->info->duration_error));
02357 for (i=1; i<FF_ARRAY_ELEMS(st->info->duration_error); i++) {
02358 int framerate= get_std_framerate(i);
02359 int ticks= lrintf(dur*framerate/(1001*12));
02360 double error= dur - ticks*1001*12/(double)framerate;
02361 st->info->duration_error[i] += error*error;
02362 }
02363 st->info->duration_count++;
02364
02365 if (st->info->duration_count > 3)
02366 st->info->duration_gcd = av_gcd(st->info->duration_gcd, duration);
02367 }
02368 if (last == AV_NOPTS_VALUE || st->info->duration_count <= 1)
02369 st->info->last_dts = pkt->dts;
02370 }
02371 if(st->parser && st->parser->parser->split && !st->codec->extradata){
02372 int i= st->parser->parser->split(st->codec, pkt->data, pkt->size);
02373 if(i){
02374 st->codec->extradata_size= i;
02375 st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
02376 memcpy(st->codec->extradata, pkt->data, st->codec->extradata_size);
02377 memset(st->codec->extradata + i, 0, FF_INPUT_BUFFER_PADDING_SIZE);
02378 }
02379 }
02380
02381
02382
02383
02384
02385 if (!has_codec_parameters(st->codec) || !has_decode_delay_been_guessed(st))
02386 try_decode_frame(st, pkt);
02387
02388 st->codec_info_nb_frames++;
02389 count++;
02390 }
02391
02392
02393 for(i=0;i<ic->nb_streams;i++) {
02394 st = ic->streams[i];
02395 if(st->codec->codec)
02396 avcodec_close(st->codec);
02397 }
02398 for(i=0;i<ic->nb_streams;i++) {
02399 st = ic->streams[i];
02400 if (st->codec_info_nb_frames>2 && !st->avg_frame_rate.num && st->info->codec_info_duration)
02401 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
02402 (st->codec_info_nb_frames-2)*(int64_t)st->time_base.den,
02403 st->info->codec_info_duration*(int64_t)st->time_base.num, 60000);
02404 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
02405 if(st->codec->codec_id == CODEC_ID_RAWVIDEO && !st->codec->codec_tag && !st->codec->bits_per_coded_sample)
02406 st->codec->codec_tag= avcodec_pix_fmt_to_codec_tag(st->codec->pix_fmt);
02407
02408
02409
02410
02411 if (tb_unreliable(st->codec) && st->info->duration_count > 15 && st->info->duration_gcd > FFMAX(1, st->time_base.den/(500LL*st->time_base.num)) && !st->r_frame_rate.num)
02412 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);
02413 if (st->info->duration_count && !st->r_frame_rate.num
02414 && tb_unreliable(st->codec)
02415
02416 ){
02417 int num = 0;
02418 double best_error= 2*av_q2d(st->time_base);
02419 best_error = best_error*best_error*st->info->duration_count*1000*12*30;
02420
02421 for (j=1; j<FF_ARRAY_ELEMS(st->info->duration_error); j++) {
02422 double error = st->info->duration_error[j] * get_std_framerate(j);
02423
02424
02425 if(error < best_error){
02426 best_error= error;
02427 num = get_std_framerate(j);
02428 }
02429 }
02430
02431 if (num && (!st->r_frame_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(st->r_frame_rate)))
02432 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
02433 }
02434
02435 if (!st->r_frame_rate.num){
02436 if( st->codec->time_base.den * (int64_t)st->time_base.num
02437 <= st->codec->time_base.num * st->codec->ticks_per_frame * (int64_t)st->time_base.den){
02438 st->r_frame_rate.num = st->codec->time_base.den;
02439 st->r_frame_rate.den = st->codec->time_base.num * st->codec->ticks_per_frame;
02440 }else{
02441 st->r_frame_rate.num = st->time_base.den;
02442 st->r_frame_rate.den = st->time_base.num;
02443 }
02444 }
02445 }else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
02446 if(!st->codec->bits_per_coded_sample)
02447 st->codec->bits_per_coded_sample= av_get_bits_per_sample(st->codec->codec_id);
02448 }
02449 }
02450
02451 av_estimate_timings(ic, old_offset);
02452
02453 compute_chapters_end(ic);
02454
02455 #if 0
02456
02457 for(i=0;i<ic->nb_streams;i++) {
02458 st = ic->streams[i];
02459 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
02460 if(b-frames){
02461 ppktl = &ic->packet_buffer;
02462 while(ppkt1){
02463 if(ppkt1->stream_index != i)
02464 continue;
02465 if(ppkt1->pkt->dts < 0)
02466 break;
02467 if(ppkt1->pkt->pts != AV_NOPTS_VALUE)
02468 break;
02469 ppkt1->pkt->dts -= delta;
02470 ppkt1= ppkt1->next;
02471 }
02472 if(ppkt1)
02473 continue;
02474 st->cur_dts -= delta;
02475 }
02476 }
02477 }
02478 #endif
02479
02480 find_stream_info_err:
02481 for (i=0; i < ic->nb_streams; i++)
02482 av_freep(&ic->streams[i]->info);
02483 return ret;
02484 }
02485
02486 static AVProgram *find_program_from_stream(AVFormatContext *ic, int s)
02487 {
02488 int i, j;
02489
02490 for (i = 0; i < ic->nb_programs; i++)
02491 for (j = 0; j < ic->programs[i]->nb_stream_indexes; j++)
02492 if (ic->programs[i]->stream_index[j] == s)
02493 return ic->programs[i];
02494 return NULL;
02495 }
02496
02497 int av_find_best_stream(AVFormatContext *ic,
02498 enum AVMediaType type,
02499 int wanted_stream_nb,
02500 int related_stream,
02501 AVCodec **decoder_ret,
02502 int flags)
02503 {
02504 int i, nb_streams = ic->nb_streams;
02505 int ret = AVERROR_STREAM_NOT_FOUND, best_count = -1;
02506 unsigned *program = NULL;
02507 AVCodec *decoder = NULL, *best_decoder = NULL;
02508
02509 if (related_stream >= 0 && wanted_stream_nb < 0) {
02510 AVProgram *p = find_program_from_stream(ic, related_stream);
02511 if (p) {
02512 program = p->stream_index;
02513 nb_streams = p->nb_stream_indexes;
02514 }
02515 }
02516 for (i = 0; i < nb_streams; i++) {
02517 int real_stream_index = program ? program[i] : i;
02518 AVStream *st = ic->streams[real_stream_index];
02519 AVCodecContext *avctx = st->codec;
02520 if (avctx->codec_type != type)
02521 continue;
02522 if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
02523 continue;
02524 if (st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED|AV_DISPOSITION_VISUAL_IMPAIRED))
02525 continue;
02526 if (decoder_ret) {
02527 decoder = avcodec_find_decoder(st->codec->codec_id);
02528 if (!decoder) {
02529 if (ret < 0)
02530 ret = AVERROR_DECODER_NOT_FOUND;
02531 continue;
02532 }
02533 }
02534 if (best_count >= st->codec_info_nb_frames)
02535 continue;
02536 best_count = st->codec_info_nb_frames;
02537 ret = real_stream_index;
02538 best_decoder = decoder;
02539 if (program && i == nb_streams - 1 && ret < 0) {
02540 program = NULL;
02541 nb_streams = ic->nb_streams;
02542 i = 0;
02543 }
02544 }
02545 if (decoder_ret)
02546 *decoder_ret = best_decoder;
02547 return ret;
02548 }
02549
02550
02551
02552 int av_read_play(AVFormatContext *s)
02553 {
02554 if (s->iformat->read_play)
02555 return s->iformat->read_play(s);
02556 if (s->pb)
02557 return av_url_read_fpause(s->pb, 0);
02558 return AVERROR(ENOSYS);
02559 }
02560
02561 int av_read_pause(AVFormatContext *s)
02562 {
02563 if (s->iformat->read_pause)
02564 return s->iformat->read_pause(s);
02565 if (s->pb)
02566 return av_url_read_fpause(s->pb, 1);
02567 return AVERROR(ENOSYS);
02568 }
02569
02570 void av_close_input_stream(AVFormatContext *s)
02571 {
02572 flush_packet_queue(s);
02573 if (s->iformat->read_close)
02574 s->iformat->read_close(s);
02575 avformat_free_context(s);
02576 }
02577
02578 void avformat_free_context(AVFormatContext *s)
02579 {
02580 int i;
02581 AVStream *st;
02582
02583 for(i=0;i<s->nb_streams;i++) {
02584
02585 st = s->streams[i];
02586 if (st->parser) {
02587 av_parser_close(st->parser);
02588 av_free_packet(&st->cur_pkt);
02589 }
02590 av_metadata_free(&st->metadata);
02591 av_free(st->index_entries);
02592 av_free(st->codec->extradata);
02593 av_free(st->codec->subtitle_header);
02594 av_free(st->codec);
02595 #if FF_API_OLD_METADATA
02596 av_free(st->filename);
02597 #endif
02598 av_free(st->priv_data);
02599 av_free(st->info);
02600 av_free(st);
02601 }
02602 for(i=s->nb_programs-1; i>=0; i--) {
02603 #if FF_API_OLD_METADATA
02604 av_freep(&s->programs[i]->provider_name);
02605 av_freep(&s->programs[i]->name);
02606 #endif
02607 av_metadata_free(&s->programs[i]->metadata);
02608 av_freep(&s->programs[i]->stream_index);
02609 av_freep(&s->programs[i]);
02610 }
02611 av_freep(&s->programs);
02612 av_freep(&s->priv_data);
02613 while(s->nb_chapters--) {
02614 #if FF_API_OLD_METADATA
02615 av_free(s->chapters[s->nb_chapters]->title);
02616 #endif
02617 av_metadata_free(&s->chapters[s->nb_chapters]->metadata);
02618 av_free(s->chapters[s->nb_chapters]);
02619 }
02620 av_freep(&s->chapters);
02621 av_metadata_free(&s->metadata);
02622 av_freep(&s->key);
02623 av_free(s);
02624 }
02625
02626 void av_close_input_file(AVFormatContext *s)
02627 {
02628 AVIOContext *pb = s->iformat->flags & AVFMT_NOFILE ? NULL : s->pb;
02629 av_close_input_stream(s);
02630 if (pb)
02631 avio_close(pb);
02632 }
02633
02634 AVStream *av_new_stream(AVFormatContext *s, int id)
02635 {
02636 AVStream *st;
02637 int i;
02638
02639 #if FF_API_MAX_STREAMS
02640 if (s->nb_streams >= MAX_STREAMS){
02641 av_log(s, AV_LOG_ERROR, "Too many streams\n");
02642 return NULL;
02643 }
02644 #else
02645 AVStream **streams;
02646
02647 if (s->nb_streams >= INT_MAX/sizeof(*streams))
02648 return NULL;
02649 streams = av_realloc(s->streams, (s->nb_streams + 1) * sizeof(*streams));
02650 if (!streams)
02651 return NULL;
02652 s->streams = streams;
02653 #endif
02654
02655 st = av_mallocz(sizeof(AVStream));
02656 if (!st)
02657 return NULL;
02658 if (!(st->info = av_mallocz(sizeof(*st->info)))) {
02659 av_free(st);
02660 return NULL;
02661 }
02662
02663 st->codec= avcodec_alloc_context();
02664 if (s->iformat) {
02665
02666 st->codec->bit_rate = 0;
02667 }
02668 st->index = s->nb_streams;
02669 st->id = id;
02670 st->start_time = AV_NOPTS_VALUE;
02671 st->duration = AV_NOPTS_VALUE;
02672
02673
02674
02675
02676 st->cur_dts = 0;
02677 st->first_dts = AV_NOPTS_VALUE;
02678 st->probe_packets = MAX_PROBE_PACKETS;
02679
02680
02681 av_set_pts_info(st, 33, 1, 90000);
02682 st->last_IP_pts = AV_NOPTS_VALUE;
02683 for(i=0; i<MAX_REORDER_DELAY+1; i++)
02684 st->pts_buffer[i]= AV_NOPTS_VALUE;
02685 st->reference_dts = AV_NOPTS_VALUE;
02686
02687 st->sample_aspect_ratio = (AVRational){0,1};
02688
02689 s->streams[s->nb_streams++] = st;
02690 return st;
02691 }
02692
02693 AVProgram *av_new_program(AVFormatContext *ac, int id)
02694 {
02695 AVProgram *program=NULL;
02696 int i;
02697
02698 #ifdef DEBUG_SI
02699 av_log(ac, AV_LOG_DEBUG, "new_program: id=0x%04x\n", id);
02700 #endif
02701
02702 for(i=0; i<ac->nb_programs; i++)
02703 if(ac->programs[i]->id == id)
02704 program = ac->programs[i];
02705
02706 if(!program){
02707 program = av_mallocz(sizeof(AVProgram));
02708 if (!program)
02709 return NULL;
02710 dynarray_add(&ac->programs, &ac->nb_programs, program);
02711 program->discard = AVDISCARD_NONE;
02712 }
02713 program->id = id;
02714
02715 return program;
02716 }
02717
02718 AVChapter *ff_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
02719 {
02720 AVChapter *chapter = NULL;
02721 int i;
02722
02723 for(i=0; i<s->nb_chapters; i++)
02724 if(s->chapters[i]->id == id)
02725 chapter = s->chapters[i];
02726
02727 if(!chapter){
02728 chapter= av_mallocz(sizeof(AVChapter));
02729 if(!chapter)
02730 return NULL;
02731 dynarray_add(&s->chapters, &s->nb_chapters, chapter);
02732 }
02733 #if FF_API_OLD_METADATA
02734 av_free(chapter->title);
02735 #endif
02736 av_metadata_set2(&chapter->metadata, "title", title, 0);
02737 chapter->id = id;
02738 chapter->time_base= time_base;
02739 chapter->start = start;
02740 chapter->end = end;
02741
02742 return chapter;
02743 }
02744
02745
02746
02747
02748 int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
02749 {
02750 int ret;
02751
02752 if (s->oformat->priv_data_size > 0) {
02753 s->priv_data = av_mallocz(s->oformat->priv_data_size);
02754 if (!s->priv_data)
02755 return AVERROR(ENOMEM);
02756 if (s->oformat->priv_class) {
02757 *(const AVClass**)s->priv_data= s->oformat->priv_class;
02758 av_opt_set_defaults(s->priv_data);
02759 }
02760 } else
02761 s->priv_data = NULL;
02762
02763 if (s->oformat->set_parameters) {
02764 ret = s->oformat->set_parameters(s, ap);
02765 if (ret < 0)
02766 return ret;
02767 }
02768 return 0;
02769 }
02770
02771 static int validate_codec_tag(AVFormatContext *s, AVStream *st)
02772 {
02773 const AVCodecTag *avctag;
02774 int n;
02775 enum CodecID id = CODEC_ID_NONE;
02776 unsigned int tag = 0;
02777
02784 for (n = 0; s->oformat->codec_tag[n]; n++) {
02785 avctag = s->oformat->codec_tag[n];
02786 while (avctag->id != CODEC_ID_NONE) {
02787 if (ff_toupper4(avctag->tag) == ff_toupper4(st->codec->codec_tag)) {
02788 id = avctag->id;
02789 if (id == st->codec->codec_id)
02790 return 1;
02791 }
02792 if (avctag->id == st->codec->codec_id)
02793 tag = avctag->tag;
02794 avctag++;
02795 }
02796 }
02797 if (id != CODEC_ID_NONE)
02798 return 0;
02799 if (tag && (st->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
02800 return 0;
02801 return 1;
02802 }
02803
02804 int av_write_header(AVFormatContext *s)
02805 {
02806 int ret, i;
02807 AVStream *st;
02808
02809
02810 if (s->nb_streams == 0 && !(s->oformat->flags & AVFMT_NOSTREAMS)) {
02811 av_log(s, AV_LOG_ERROR, "no streams\n");
02812 return AVERROR(EINVAL);
02813 }
02814
02815 for(i=0;i<s->nb_streams;i++) {
02816 st = s->streams[i];
02817
02818 switch (st->codec->codec_type) {
02819 case AVMEDIA_TYPE_AUDIO:
02820 if(st->codec->sample_rate<=0){
02821 av_log(s, AV_LOG_ERROR, "sample rate not set\n");
02822 return AVERROR(EINVAL);
02823 }
02824 if(!st->codec->block_align)
02825 st->codec->block_align = st->codec->channels *
02826 av_get_bits_per_sample(st->codec->codec_id) >> 3;
02827 break;
02828 case AVMEDIA_TYPE_VIDEO:
02829 if(st->codec->time_base.num<=0 || st->codec->time_base.den<=0){
02830 av_log(s, AV_LOG_ERROR, "time base not set\n");
02831 return AVERROR(EINVAL);
02832 }
02833 if((st->codec->width<=0 || st->codec->height<=0) && !(s->oformat->flags & AVFMT_NODIMENSIONS)){
02834 av_log(s, AV_LOG_ERROR, "dimensions not set\n");
02835 return AVERROR(EINVAL);
02836 }
02837 if(av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)){
02838 av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between encoder and muxer layer\n");
02839 return AVERROR(EINVAL);
02840 }
02841 break;
02842 }
02843
02844 if(s->oformat->codec_tag){
02845 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)){
02846
02847 st->codec->codec_tag= 0;
02848 }
02849 if(st->codec->codec_tag){
02850 if (!validate_codec_tag(s, st)) {
02851 char tagbuf[32];
02852 av_get_codec_tag_string(tagbuf, sizeof(tagbuf), st->codec->codec_tag);
02853 av_log(s, AV_LOG_ERROR,
02854 "Tag %s/0x%08x incompatible with output codec id '%d'\n",
02855 tagbuf, st->codec->codec_tag, st->codec->codec_id);
02856 return AVERROR_INVALIDDATA;
02857 }
02858 }else
02859 st->codec->codec_tag= av_codec_get_tag(s->oformat->codec_tag, st->codec->codec_id);
02860 }
02861
02862 if(s->oformat->flags & AVFMT_GLOBALHEADER &&
02863 !(st->codec->flags & CODEC_FLAG_GLOBAL_HEADER))
02864 av_log(s, AV_LOG_WARNING, "Codec for stream %d does not use global headers but container format requires global headers\n", i);
02865 }
02866
02867 if (!s->priv_data && s->oformat->priv_data_size > 0) {
02868 s->priv_data = av_mallocz(s->oformat->priv_data_size);
02869 if (!s->priv_data)
02870 return AVERROR(ENOMEM);
02871 }
02872
02873 #if FF_API_OLD_METADATA
02874 ff_metadata_mux_compat(s);
02875 #endif
02876
02877
02878 if (s->nb_streams && !(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
02879 av_metadata_set2(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
02880 }
02881
02882 if(s->oformat->write_header){
02883 ret = s->oformat->write_header(s);
02884 if (ret < 0)
02885 return ret;
02886 }
02887
02888
02889 for(i=0;i<s->nb_streams;i++) {
02890 int64_t den = AV_NOPTS_VALUE;
02891 st = s->streams[i];
02892
02893 switch (st->codec->codec_type) {
02894 case AVMEDIA_TYPE_AUDIO:
02895 den = (int64_t)st->time_base.num * st->codec->sample_rate;
02896 break;
02897 case AVMEDIA_TYPE_VIDEO:
02898 den = (int64_t)st->time_base.num * st->codec->time_base.den;
02899 break;
02900 default:
02901 break;
02902 }
02903 if (den != AV_NOPTS_VALUE) {
02904 if (den <= 0)
02905 return AVERROR_INVALIDDATA;
02906 av_frac_init(&st->pts, 0, 0, den);
02907 }
02908 }
02909 return 0;
02910 }
02911
02912
02913 static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt){
02914 int delay = FFMAX(st->codec->has_b_frames, !!st->codec->max_b_frames);
02915 int num, den, frame_size, i;
02916
02917 av_dlog(s, "compute_pkt_fields2: pts:%"PRId64" dts:%"PRId64" cur_dts:%"PRId64" b:%d size:%d st:%d\n",
02918 pkt->pts, pkt->dts, st->cur_dts, delay, pkt->size, pkt->stream_index);
02919
02920
02921
02922
02923
02924 if (pkt->duration == 0) {
02925 compute_frame_duration(&num, &den, st, NULL, pkt);
02926 if (den && num) {
02927 pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
02928 }
02929 }
02930
02931 if(pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay==0)
02932 pkt->pts= pkt->dts;
02933
02934
02935 if((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay){
02936 pkt->