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