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