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