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