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