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