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