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