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 "libavcodec/opt.h"
00023 #include "libavutil/avstring.h"
00024 #include "riff.h"
00025 #include <sys/time.h>
00026 #include <time.h>
00027
00028 #undef NDEBUG
00029 #include <assert.h>
00030
00031
00032
00033
00034
00035
00036 static void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den);
00037 static void av_frac_add(AVFrac *f, int64_t incr);
00038
00039
00040 AVInputFormat *first_iformat = NULL;
00041
00042 AVOutputFormat *first_oformat = NULL;
00043
00044 AVInputFormat *av_iformat_next(AVInputFormat *f)
00045 {
00046 if(f) return f->next;
00047 else return first_iformat;
00048 }
00049
00050 AVOutputFormat *av_oformat_next(AVOutputFormat *f)
00051 {
00052 if(f) return f->next;
00053 else return first_oformat;
00054 }
00055
00056 void av_register_input_format(AVInputFormat *format)
00057 {
00058 AVInputFormat **p;
00059 p = &first_iformat;
00060 while (*p != NULL) p = &(*p)->next;
00061 *p = format;
00062 format->next = NULL;
00063 }
00064
00065 void av_register_output_format(AVOutputFormat *format)
00066 {
00067 AVOutputFormat **p;
00068 p = &first_oformat;
00069 while (*p != NULL) p = &(*p)->next;
00070 *p = format;
00071 format->next = NULL;
00072 }
00073
00074 int match_ext(const char *filename, const char *extensions)
00075 {
00076 const char *ext, *p;
00077 char ext1[32], *q;
00078
00079 if(!filename)
00080 return 0;
00081
00082 ext = strrchr(filename, '.');
00083 if (ext) {
00084 ext++;
00085 p = extensions;
00086 for(;;) {
00087 q = ext1;
00088 while (*p != '\0' && *p != ',' && q-ext1<sizeof(ext1)-1)
00089 *q++ = *p++;
00090 *q = '\0';
00091 if (!strcasecmp(ext1, ext))
00092 return 1;
00093 if (*p == '\0')
00094 break;
00095 p++;
00096 }
00097 }
00098 return 0;
00099 }
00100
00101 AVOutputFormat *guess_format(const char *short_name, const char *filename,
00102 const char *mime_type)
00103 {
00104 AVOutputFormat *fmt, *fmt_found;
00105 int score_max, score;
00106
00107
00108 #ifdef CONFIG_IMAGE2_MUXER
00109 if (!short_name && filename &&
00110 av_filename_number_test(filename) &&
00111 av_guess_image2_codec(filename) != CODEC_ID_NONE) {
00112 return guess_format("image2", NULL, NULL);
00113 }
00114 #endif
00115
00116 fmt_found = NULL;
00117 score_max = 0;
00118 fmt = first_oformat;
00119 while (fmt != NULL) {
00120 score = 0;
00121 if (fmt->name && short_name && !strcmp(fmt->name, short_name))
00122 score += 100;
00123 if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
00124 score += 10;
00125 if (filename && fmt->extensions &&
00126 match_ext(filename, fmt->extensions)) {
00127 score += 5;
00128 }
00129 if (score > score_max) {
00130 score_max = score;
00131 fmt_found = fmt;
00132 }
00133 fmt = fmt->next;
00134 }
00135 return fmt_found;
00136 }
00137
00138 AVOutputFormat *guess_stream_format(const char *short_name, const char *filename,
00139 const char *mime_type)
00140 {
00141 AVOutputFormat *fmt = guess_format(short_name, filename, mime_type);
00142
00143 if (fmt) {
00144 AVOutputFormat *stream_fmt;
00145 char stream_format_name[64];
00146
00147 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream", fmt->name);
00148 stream_fmt = guess_format(stream_format_name, NULL, NULL);
00149
00150 if (stream_fmt)
00151 fmt = stream_fmt;
00152 }
00153
00154 return fmt;
00155 }
00156
00157 enum CodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
00158 const char *filename, const char *mime_type, enum CodecType type){
00159 if(type == CODEC_TYPE_VIDEO){
00160 enum CodecID codec_id= CODEC_ID_NONE;
00161
00162 #ifdef CONFIG_IMAGE2_MUXER
00163 if(!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")){
00164 codec_id= av_guess_image2_codec(filename);
00165 }
00166 #endif
00167 if(codec_id == CODEC_ID_NONE)
00168 codec_id= fmt->video_codec;
00169 return codec_id;
00170 }else if(type == CODEC_TYPE_AUDIO)
00171 return fmt->audio_codec;
00172 else
00173 return CODEC_ID_NONE;
00174 }
00175
00176 AVInputFormat *av_find_input_format(const char *short_name)
00177 {
00178 AVInputFormat *fmt;
00179 for(fmt = first_iformat; fmt != NULL; fmt = fmt->next) {
00180 if (!strcmp(fmt->name, short_name))
00181 return fmt;
00182 }
00183 return NULL;
00184 }
00185
00186
00187
00188 void av_destruct_packet(AVPacket *pkt)
00189 {
00190 av_free(pkt->data);
00191 pkt->data = NULL; pkt->size = 0;
00192 }
00193
00194 void av_init_packet(AVPacket *pkt)
00195 {
00196 pkt->pts = AV_NOPTS_VALUE;
00197 pkt->dts = AV_NOPTS_VALUE;
00198 pkt->pos = -1;
00199 pkt->duration = 0;
00200 pkt->flags = 0;
00201 pkt->stream_index = 0;
00202 pkt->destruct= av_destruct_packet_nofree;
00203 }
00204
00205 int av_new_packet(AVPacket *pkt, int size)
00206 {
00207 uint8_t *data;
00208 if((unsigned)size > (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
00209 return AVERROR(ENOMEM);
00210 data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
00211 if (!data)
00212 return AVERROR(ENOMEM);
00213 memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00214
00215 av_init_packet(pkt);
00216 pkt->data = data;
00217 pkt->size = size;
00218 pkt->destruct = av_destruct_packet;
00219 return 0;
00220 }
00221
00222 int av_get_packet(ByteIOContext *s, AVPacket *pkt, int size)
00223 {
00224 int ret= av_new_packet(pkt, size);
00225
00226 if(ret<0)
00227 return ret;
00228
00229 pkt->pos= url_ftell(s);
00230
00231 ret= get_buffer(s, pkt->data, size);
00232 if(ret<=0)
00233 av_free_packet(pkt);
00234 else
00235 pkt->size= ret;
00236
00237 return ret;
00238 }
00239
00240 int av_dup_packet(AVPacket *pkt)
00241 {
00242 if (pkt->destruct != av_destruct_packet) {
00243 uint8_t *data;
00244
00245 if((unsigned)pkt->size > (unsigned)pkt->size + FF_INPUT_BUFFER_PADDING_SIZE)
00246 return AVERROR(ENOMEM);
00247 data = av_malloc(pkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
00248 if (!data) {
00249 return AVERROR(ENOMEM);
00250 }
00251 memcpy(data, pkt->data, pkt->size);
00252 memset(data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00253 pkt->data = data;
00254 pkt->destruct = av_destruct_packet;
00255 }
00256 return 0;
00257 }
00258
00259 int av_filename_number_test(const char *filename)
00260 {
00261 char buf[1024];
00262 return filename && (av_get_frame_filename(buf, sizeof(buf), filename, 1)>=0);
00263 }
00264
00265 static AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened, int *score_max)
00266 {
00267 AVInputFormat *fmt1, *fmt;
00268 int score;
00269
00270 fmt = NULL;
00271 for(fmt1 = first_iformat; fmt1 != NULL; fmt1 = fmt1->next) {
00272 if (!is_opened == !(fmt1->flags & AVFMT_NOFILE))
00273 continue;
00274 score = 0;
00275 if (fmt1->read_probe) {
00276 score = fmt1->read_probe(pd);
00277 } else if (fmt1->extensions) {
00278 if (match_ext(pd->filename, fmt1->extensions)) {
00279 score = 50;
00280 }
00281 }
00282 if (score > *score_max) {
00283 *score_max = score;
00284 fmt = fmt1;
00285 }else if (score == *score_max)
00286 fmt = NULL;
00287 }
00288 return fmt;
00289 }
00290
00291 AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened){
00292 int score=0;
00293 return av_probe_input_format2(pd, is_opened, &score);
00294 }
00295
00296 static int set_codec_from_probe_data(AVStream *st, AVProbeData *pd, int score)
00297 {
00298 AVInputFormat *fmt;
00299 fmt = av_probe_input_format2(pd, 1, &score);
00300
00301 if (fmt) {
00302 if (!strcmp(fmt->name, "mp3"))
00303 st->codec->codec_id = CODEC_ID_MP3;
00304 else if (!strcmp(fmt->name, "ac3"))
00305 st->codec->codec_id = CODEC_ID_AC3;
00306 else if (!strcmp(fmt->name, "mpegvideo"))
00307 st->codec->codec_id = CODEC_ID_MPEG2VIDEO;
00308 else if (!strcmp(fmt->name, "h264"))
00309 st->codec->codec_id = CODEC_ID_H264;
00310 }
00311 return !!fmt;
00312 }
00313
00314
00315
00316
00317
00318
00319
00320 static const char* format_to_name(void* ptr)
00321 {
00322 AVFormatContext* fc = (AVFormatContext*) ptr;
00323 if(fc->iformat) return fc->iformat->name;
00324 else if(fc->oformat) return fc->oformat->name;
00325 else return "NULL";
00326 }
00327
00328 #define OFFSET(x) offsetof(AVFormatContext,x)
00329 #define DEFAULT 0 //should be NAN but it does not work as it is not a constant in glibc as required by ANSI/ISO C
00330
00331 #define E AV_OPT_FLAG_ENCODING_PARAM
00332 #define D AV_OPT_FLAG_DECODING_PARAM
00333
00334 static const AVOption options[]={
00335 {"probesize", NULL, OFFSET(probesize), FF_OPT_TYPE_INT, 32000, 32, INT_MAX, D},
00336 {"muxrate", "set mux rate", OFFSET(mux_rate), FF_OPT_TYPE_INT, DEFAULT, 0, INT_MAX, E},
00337 {"packetsize", "set packet size", OFFSET(packet_size), FF_OPT_TYPE_INT, DEFAULT, 0, INT_MAX, E},
00338 {"fflags", NULL, OFFSET(flags), FF_OPT_TYPE_FLAGS, DEFAULT, INT_MIN, INT_MAX, D|E, "fflags"},
00339 {"ignidx", "ignore index", 0, FF_OPT_TYPE_CONST, AVFMT_FLAG_IGNIDX, INT_MIN, INT_MAX, D, "fflags"},
00340 {"genpts", "generate pts", 0, FF_OPT_TYPE_CONST, AVFMT_FLAG_GENPTS, INT_MIN, INT_MAX, D, "fflags"},
00341 {"track", " set the track number", OFFSET(track), FF_OPT_TYPE_INT, DEFAULT, 0, INT_MAX, E},
00342 {"year", "set the year", OFFSET(year), FF_OPT_TYPE_INT, DEFAULT, INT_MIN, INT_MAX, E},
00343 {"analyzeduration", "how many microseconds are analyzed to estimate duration", OFFSET(max_analyze_duration), FF_OPT_TYPE_INT, 3*AV_TIME_BASE, 0, INT_MAX, D},
00344 {"cryptokey", "decryption key", OFFSET(key), FF_OPT_TYPE_BINARY, 0, 0, 0, D},
00345 {"indexmem", "max memory used for timestamp index (per stream)", OFFSET(max_index_size), FF_OPT_TYPE_INT, 1<<20, 0, INT_MAX, D},
00346 {"rtbufsize", "max memory used for buffering real-time frames", OFFSET(max_picture_buffer), FF_OPT_TYPE_INT, 3041280, 0, INT_MAX, D},
00347 {"fdebug", "print specific debug info", OFFSET(debug), FF_OPT_TYPE_FLAGS, DEFAULT, 0, INT_MAX, E|D, "fdebug"},
00348 {"ts", NULL, 0, FF_OPT_TYPE_CONST, FF_FDEBUG_TS, INT_MIN, INT_MAX, E|D, "fdebug"},
00349 {NULL},
00350 };
00351
00352 #undef E
00353 #undef D
00354 #undef DEFAULT
00355
00356 static const AVClass av_format_context_class = { "AVFormatContext", format_to_name, options };
00357
00358 static void avformat_get_context_defaults(AVFormatContext *s)
00359 {
00360 memset(s, 0, sizeof(AVFormatContext));
00361
00362 s->av_class = &av_format_context_class;
00363
00364 av_opt_set_defaults(s);
00365 }
00366
00367 AVFormatContext *av_alloc_format_context(void)
00368 {
00369 AVFormatContext *ic;
00370 ic = av_malloc(sizeof(AVFormatContext));
00371 if (!ic) return ic;
00372 avformat_get_context_defaults(ic);
00373 ic->av_class = &av_format_context_class;
00374 return ic;
00375 }
00376
00377 int av_open_input_stream(AVFormatContext **ic_ptr,
00378 ByteIOContext *pb, const char *filename,
00379 AVInputFormat *fmt, AVFormatParameters *ap)
00380 {
00381 int err;
00382 AVFormatContext *ic;
00383 AVFormatParameters default_ap;
00384
00385 if(!ap){
00386 ap=&default_ap;
00387 memset(ap, 0, sizeof(default_ap));
00388 }
00389
00390 if(!ap->prealloced_context)
00391 ic = av_alloc_format_context();
00392 else
00393 ic = *ic_ptr;
00394 if (!ic) {
00395 err = AVERROR(ENOMEM);
00396 goto fail;
00397 }
00398 ic->iformat = fmt;
00399 ic->pb = pb;
00400 ic->duration = AV_NOPTS_VALUE;
00401 ic->start_time = AV_NOPTS_VALUE;
00402 av_strlcpy(ic->filename, filename, sizeof(ic->filename));
00403
00404
00405 if (fmt->priv_data_size > 0) {
00406 ic->priv_data = av_mallocz(fmt->priv_data_size);
00407 if (!ic->priv_data) {
00408 err = AVERROR(ENOMEM);
00409 goto fail;
00410 }
00411 } else {
00412 ic->priv_data = NULL;
00413 }
00414
00415 if (ic->iformat->read_header) {
00416 err = ic->iformat->read_header(ic, ap);
00417 if (err < 0)
00418 goto fail;
00419 }
00420
00421 if (pb && !ic->data_offset)
00422 ic->data_offset = url_ftell(ic->pb);
00423
00424 *ic_ptr = ic;
00425 return 0;
00426 fail:
00427 if (ic) {
00428 int i;
00429 av_freep(&ic->priv_data);
00430 for(i=0;i<ic->nb_streams;i++) {
00431 AVStream *st = ic->streams[i];
00432 if (st) {
00433 av_free(st->priv_data);
00434 av_free(st->codec->extradata);
00435 }
00436 av_free(st);
00437 }
00438 }
00439 av_free(ic);
00440 *ic_ptr = NULL;
00441 return err;
00442 }
00443
00444
00445 #define PROBE_BUF_MIN 2048
00446 #define PROBE_BUF_MAX (1<<20)
00447
00448 int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
00449 AVInputFormat *fmt,
00450 int buf_size,
00451 AVFormatParameters *ap)
00452 {
00453 int err, probe_size;
00454 AVProbeData probe_data, *pd = &probe_data;
00455 ByteIOContext *pb = NULL;
00456
00457 pd->filename = "";
00458 if (filename)
00459 pd->filename = filename;
00460 pd->buf = NULL;
00461 pd->buf_size = 0;
00462
00463 if (!fmt) {
00464
00465 fmt = av_probe_input_format(pd, 0);
00466 }
00467
00468
00469
00470 if (!fmt || !(fmt->flags & AVFMT_NOFILE)) {
00471
00472 if ((err=url_fopen(&pb, filename, URL_RDONLY)) < 0) {
00473 goto fail;
00474 }
00475 if (buf_size > 0) {
00476 url_setbufsize(pb, buf_size);
00477 }
00478
00479 for(probe_size= PROBE_BUF_MIN; probe_size<=PROBE_BUF_MAX && !fmt; probe_size<<=1){
00480 int score= probe_size < PROBE_BUF_MAX ? AVPROBE_SCORE_MAX/4 : 0;
00481
00482 pd->buf= av_realloc(pd->buf, probe_size + AVPROBE_PADDING_SIZE);
00483 pd->buf_size = get_buffer(pb, pd->buf, probe_size);
00484 memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE);
00485 if (url_fseek(pb, 0, SEEK_SET) < 0) {
00486 url_fclose(pb);
00487 if (url_fopen(&pb, filename, URL_RDONLY) < 0) {
00488 pb = NULL;
00489 err = AVERROR(EIO);
00490 goto fail;
00491 }
00492 }
00493
00494 fmt = av_probe_input_format2(pd, 1, &score);
00495 }
00496 av_freep(&pd->buf);
00497 }
00498
00499
00500 if (!fmt) {
00501 err = AVERROR_NOFMT;
00502 goto fail;
00503 }
00504
00505
00506 if (fmt->flags & AVFMT_NEEDNUMBER) {
00507 if (!av_filename_number_test(filename)) {
00508 err = AVERROR_NUMEXPECTED;
00509 goto fail;
00510 }
00511 }
00512 err = av_open_input_stream(ic_ptr, pb, filename, fmt, ap);
00513 if (err)
00514 goto fail;
00515 return 0;
00516 fail:
00517 av_freep(&pd->buf);
00518 if (pb)
00519 url_fclose(pb);
00520 *ic_ptr = NULL;
00521 return err;
00522
00523 }
00524
00525
00526
00527 static AVPacket *add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt){
00528 AVPacketList *pktl;
00529 AVPacketList **plast_pktl= packet_buffer;
00530
00531 while(*plast_pktl) plast_pktl= &(*plast_pktl)->next;
00532
00533 pktl = av_mallocz(sizeof(AVPacketList));
00534 if (!pktl)
00535 return NULL;
00536
00537
00538 *plast_pktl = pktl;
00539 pktl->pkt= *pkt;
00540 return &pktl->pkt;
00541 }
00542
00543 int av_read_packet(AVFormatContext *s, AVPacket *pkt)
00544 {
00545 int ret;
00546 AVStream *st;
00547
00548 for(;;){
00549 AVPacketList *pktl = s->raw_packet_buffer;
00550
00551 if (pktl) {
00552 *pkt = pktl->pkt;
00553 if(s->streams[pkt->stream_index]->codec->codec_id != CODEC_ID_PROBE){
00554 s->raw_packet_buffer = pktl->next;
00555 av_free(pktl);
00556 return 0;
00557 }
00558 }
00559
00560 av_init_packet(pkt);
00561 ret= s->iformat->read_packet(s, pkt);
00562 if (ret < 0)
00563 return ret;
00564 st= s->streams[pkt->stream_index];
00565
00566 switch(st->codec->codec_type){
00567 case CODEC_TYPE_VIDEO:
00568 if(s->video_codec_id) st->codec->codec_id= s->video_codec_id;
00569 break;
00570 case CODEC_TYPE_AUDIO:
00571 if(s->audio_codec_id) st->codec->codec_id= s->audio_codec_id;
00572 break;
00573 case CODEC_TYPE_SUBTITLE:
00574 if(s->subtitle_codec_id)st->codec->codec_id= s->subtitle_codec_id;
00575 break;
00576 }
00577
00578 if(!pktl && st->codec->codec_id!=CODEC_ID_PROBE)
00579 return ret;
00580
00581 add_to_pktbuf(&s->raw_packet_buffer, pkt);
00582
00583 if(st->codec->codec_id == CODEC_ID_PROBE){
00584 AVProbeData *pd = &st->probe_data;
00585
00586 pd->buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
00587 memcpy(pd->buf+pd->buf_size, pkt->data, pkt->size);
00588 pd->buf_size += pkt->size;
00589 memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE);
00590
00591 if(av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)){
00592 set_codec_from_probe_data(st, pd, 1);
00593 if(st->codec->codec_id != CODEC_ID_PROBE){
00594 pd->buf_size=0;
00595 av_freep(&pd->buf);
00596 }
00597 }
00598 }
00599 }
00600 }
00601
00602
00603
00604
00605
00606
00607 static int get_audio_frame_size(AVCodecContext *enc, int size)
00608 {
00609 int frame_size;
00610
00611 if(enc->codec_id == CODEC_ID_VORBIS)
00612 return -1;
00613
00614 if (enc->frame_size <= 1) {
00615 int bits_per_sample = av_get_bits_per_sample(enc->codec_id);
00616
00617 if (bits_per_sample) {
00618 if (enc->channels == 0)
00619 return -1;
00620 frame_size = (size << 3) / (bits_per_sample * enc->channels);
00621 } else {
00622
00623 if (enc->bit_rate == 0)
00624 return -1;
00625 frame_size = (size * 8 * enc->sample_rate) / enc->bit_rate;
00626 }
00627 } else {
00628 frame_size = enc->frame_size;
00629 }
00630 return frame_size;
00631 }
00632
00633
00634
00635
00636
00637 static void compute_frame_duration(int *pnum, int *pden, AVStream *st,
00638 AVCodecParserContext *pc, AVPacket *pkt)
00639 {
00640 int frame_size;
00641
00642 *pnum = 0;
00643 *pden = 0;
00644 switch(st->codec->codec_type) {
00645 case CODEC_TYPE_VIDEO:
00646 if(st->time_base.num*1000LL > st->time_base.den){
00647 *pnum = st->time_base.num;
00648 *pden = st->time_base.den;
00649 }else if(st->codec->time_base.num*1000LL > st->codec->time_base.den){
00650 *pnum = st->codec->time_base.num;
00651 *pden = st->codec->time_base.den;
00652 if (pc && pc->repeat_pict) {
00653 *pden *= 2;
00654 *pnum = (*pnum) * (2 + pc->repeat_pict);
00655 }
00656 }
00657 break;
00658 case CODEC_TYPE_AUDIO:
00659 frame_size = get_audio_frame_size(st->codec, pkt->size);
00660 if (frame_size < 0)
00661 break;
00662 *pnum = frame_size;
00663 *pden = st->codec->sample_rate;
00664 break;
00665 default:
00666 break;
00667 }
00668 }
00669
00670 static int is_intra_only(AVCodecContext *enc){
00671 if(enc->codec_type == CODEC_TYPE_AUDIO){
00672 return 1;
00673 }else if(enc->codec_type == CODEC_TYPE_VIDEO){
00674 switch(enc->codec_id){
00675 case CODEC_ID_MJPEG:
00676 case CODEC_ID_MJPEGB:
00677 case CODEC_ID_LJPEG:
00678 case CODEC_ID_RAWVIDEO:
00679 case CODEC_ID_DVVIDEO:
00680 case CODEC_ID_HUFFYUV:
00681 case CODEC_ID_FFVHUFF:
00682 case CODEC_ID_ASV1:
00683 case CODEC_ID_ASV2:
00684 case CODEC_ID_VCR1:
00685 return 1;
00686 default: break;
00687 }
00688 }
00689 return 0;
00690 }
00691
00692 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
00693 int64_t dts, int64_t pts)
00694 {
00695 AVStream *st= s->streams[stream_index];
00696 AVPacketList *pktl= s->packet_buffer;
00697
00698 if(st->first_dts != AV_NOPTS_VALUE || dts == AV_NOPTS_VALUE || st->cur_dts == AV_NOPTS_VALUE)
00699 return;
00700
00701 st->first_dts= dts - st->cur_dts;
00702 st->cur_dts= dts;
00703
00704 for(; pktl; pktl= pktl->next){
00705 if(pktl->pkt.stream_index != stream_index)
00706 continue;
00707
00708 if(pktl->pkt.pts != AV_NOPTS_VALUE && pktl->pkt.pts == pktl->pkt.dts)
00709 pktl->pkt.pts += st->first_dts;
00710
00711 if(pktl->pkt.dts != AV_NOPTS_VALUE)
00712 pktl->pkt.dts += st->first_dts;
00713
00714 if(st->start_time == AV_NOPTS_VALUE && pktl->pkt.pts != AV_NOPTS_VALUE)
00715 st->start_time= pktl->pkt.pts;
00716 }
00717 if (st->start_time == AV_NOPTS_VALUE)
00718 st->start_time = pts;
00719 }
00720
00721 static void update_initial_durations(AVFormatContext *s, AVStream *st, AVPacket *pkt)
00722 {
00723 AVPacketList *pktl= s->packet_buffer;
00724 int64_t cur_dts= 0;
00725
00726 if(st->first_dts != AV_NOPTS_VALUE){
00727 cur_dts= st->first_dts;
00728 for(; pktl; pktl= pktl->next){
00729 if(pktl->pkt.stream_index == pkt->stream_index){
00730 if(pktl->pkt.pts != pktl->pkt.dts || pktl->pkt.dts != AV_NOPTS_VALUE || pktl->pkt.duration)
00731 break;
00732 cur_dts -= pkt->duration;
00733 }
00734 }
00735 pktl= s->packet_buffer;
00736 st->first_dts = cur_dts;
00737 }else if(st->cur_dts)
00738 return;
00739
00740 for(; pktl; pktl= pktl->next){
00741 if(pktl->pkt.stream_index != pkt->stream_index)
00742 continue;
00743 if(pktl->pkt.pts == pktl->pkt.dts && pktl->pkt.dts == AV_NOPTS_VALUE
00744 && !pktl->pkt.duration){
00745 pktl->pkt.dts= cur_dts;
00746 if(!st->codec->has_b_frames)
00747 pktl->pkt.pts= cur_dts;
00748 cur_dts += pkt->duration;
00749 pktl->pkt.duration= pkt->duration;
00750 }else
00751 break;
00752 }
00753 if(st->first_dts == AV_NOPTS_VALUE)
00754 st->cur_dts= cur_dts;
00755 }
00756
00757 static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
00758 AVCodecParserContext *pc, AVPacket *pkt)
00759 {
00760 int num, den, presentation_delayed, delay, i;
00761 int64_t offset;
00762
00763 if(pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && pkt->dts > pkt->pts && st->pts_wrap_bits<63
00764 ){
00765 pkt->dts -= 1LL<<st->pts_wrap_bits;
00766 }
00767
00768 if (pkt->duration == 0) {
00769 compute_frame_duration(&num, &den, st, pc, pkt);
00770 if (den && num) {
00771 pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den, den * (int64_t)st->time_base.num);
00772
00773 if(pkt->duration != 0 && s->packet_buffer)
00774 update_initial_durations(s, st, pkt);
00775 }
00776 }
00777
00778
00779
00780 if(pc && st->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size){
00781
00782 offset = av_rescale(pc->offset, pkt->duration, pkt->size);
00783 if(pkt->pts != AV_NOPTS_VALUE)
00784 pkt->pts += offset;
00785 if(pkt->dts != AV_NOPTS_VALUE)
00786 pkt->dts += offset;
00787 }
00788
00789
00790 delay= st->codec->has_b_frames;
00791 presentation_delayed = 0;
00792
00793
00794 if (delay &&
00795 pc && pc->pict_type != FF_B_TYPE)
00796 presentation_delayed = 1;
00797
00798 if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts > pkt->dts)
00799 presentation_delayed = 1;
00800
00801
00802
00803 if(delay==0 || (delay==1 && pc)){
00804 if (presentation_delayed) {
00805
00806
00807 if (pkt->dts == AV_NOPTS_VALUE)
00808 pkt->dts = st->last_IP_pts;
00809 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts);
00810 if (pkt->dts == AV_NOPTS_VALUE)
00811 pkt->dts = st->cur_dts;
00812
00813
00814
00815 if (st->last_IP_duration == 0)
00816 st->last_IP_duration = pkt->duration;
00817 if(pkt->dts != AV_NOPTS_VALUE)
00818 st->cur_dts = pkt->dts + st->last_IP_duration;
00819 st->last_IP_duration = pkt->duration;
00820 st->last_IP_pts= pkt->pts;
00821
00822
00823 } else if(pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE || pkt->duration){
00824 if(pkt->pts != AV_NOPTS_VALUE && pkt->duration){
00825 int64_t old_diff= FFABS(st->cur_dts - pkt->duration - pkt->pts);
00826 int64_t new_diff= FFABS(st->cur_dts - pkt->pts);
00827 if(old_diff < new_diff && old_diff < (pkt->duration>>3)){
00828 pkt->pts += pkt->duration;
00829
00830 }
00831 }
00832
00833
00834 if(pkt->pts == AV_NOPTS_VALUE)
00835 pkt->pts = pkt->dts;
00836 update_initial_timestamps(s, pkt->stream_index, pkt->pts, pkt->pts);
00837 if(pkt->pts == AV_NOPTS_VALUE)
00838 pkt->pts = st->cur_dts;
00839 pkt->dts = pkt->pts;
00840 if(pkt->pts != AV_NOPTS_VALUE)
00841 st->cur_dts = pkt->pts + pkt->duration;
00842 }
00843 }
00844
00845 if(pkt->pts != AV_NOPTS_VALUE){
00846 st->pts_buffer[0]= pkt->pts;
00847 for(i=1; i<delay+1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
00848 st->pts_buffer[i]= (i-delay-1) * pkt->duration;
00849 for(i=0; i<delay && st->pts_buffer[i] > st->pts_buffer[i+1]; i++)
00850 FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i+1]);
00851 if(pkt->dts == AV_NOPTS_VALUE)
00852 pkt->dts= st->pts_buffer[0];
00853 if(delay>1){
00854 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts);