X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Futils.c;h=35d85cbf3decdd0dc853c3482e2ac8fd6521d50e;hb=c12e1bd1bc7f0dd0dae2937d7f8c564e887c84ff;hp=333baa02903117719036ad7841dc2c4328d94550;hpb=fae714a9fbff876c4cacfe7d437c940d557b44ce;p=ffmpeg diff --git a/libavformat/utils.c b/libavformat/utils.c index 333baa02903..35d85cbf3de 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -33,12 +33,12 @@ #include "id3v2.h" #include "libavutil/avstring.h" #include "libavutil/mathematics.h" +#include "libavutil/parseutils.h" #include "riff.h" #include "audiointerleave.h" #include "url.h" #include #include -#include #include #if CONFIG_NETWORK #include "network.h" @@ -170,7 +170,7 @@ int av_match_ext(const char *filename, const char *extensions) while (*p != '\0' && *p != ',' && q-ext1iformat && !strlen(filename)) + return 0; + if (s->pb) { s->flags |= AVFMT_FLAG_CUSTOM_IO; if (!s->iformat) @@ -621,7 +625,8 @@ static int init_input(AVFormatContext *s, const char *filename) (!s->iformat && (s->iformat = av_probe_input_format(&pd, 0)))) return 0; - if ((ret = avio_open(&s->pb, filename, AVIO_FLAG_READ)) < 0) + if ((ret = avio_open2(&s->pb, filename, AVIO_FLAG_READ, + &s->interrupt_callback, options)) < 0) return ret; if (s->iformat) return 0; @@ -646,7 +651,7 @@ int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputForma if ((ret = av_opt_set_dict(s, &tmp)) < 0) goto fail; - if ((ret = init_input(s, filename)) < 0) + if ((ret = init_input(s, filename, &tmp)) < 0) goto fail; /* check filename in case an image number is expected */ @@ -756,6 +761,7 @@ int av_read_packet(AVFormatContext *s, AVPacket *pkt) av_log(s, AV_LOG_WARNING, "Dropped corrupted packet (stream = %d)\n", pkt->stream_index); + av_free_packet(pkt); continue; } @@ -1000,11 +1006,6 @@ static void compute_pkt_fields(AVFormatContext *s, AVStream *st, delay= st->codec->has_b_frames; presentation_delayed = 0; - // ignore delay caused by frame threading so that the mpeg2-without-dts - // warning will not trigger - if (delay && st->codec->active_thread_type&FF_THREAD_FRAME) - delay -= st->codec->thread_count-1; - /* XXX: need has_b_frame, but cannot get it if the codec is not initialized */ if (delay && @@ -1020,7 +1021,7 @@ static void compute_pkt_fields(AVFormatContext *s, AVStream *st, // Note, if this is misbehaving for a H.264 file then possibly presentation_delayed is not set correctly. if(delay==1 && pkt->dts == pkt->pts && pkt->dts != AV_NOPTS_VALUE && presentation_delayed){ av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination %"PRIi64"\n", pkt->dts); - pkt->dts= pkt->pts= AV_NOPTS_VALUE; + pkt->dts= AV_NOPTS_VALUE; } if (pkt->duration == 0) { @@ -1413,7 +1414,8 @@ void ff_read_frame_flush(AVFormatContext *s) av_free_packet(&st->cur_pkt); } st->last_IP_pts = AV_NOPTS_VALUE; - st->cur_dts = AV_NOPTS_VALUE; /* we set the current DTS to an unspecified origin */ + if(st->first_dts == AV_NOPTS_VALUE) st->cur_dts = 0; + else st->cur_dts = AV_NOPTS_VALUE; /* we set the current DTS to an unspecified origin */ st->reference_dts = AV_NOPTS_VALUE; /* fail safe */ st->cur_ptr = NULL; @@ -1426,7 +1428,15 @@ void ff_read_frame_flush(AVFormatContext *s) } } -void av_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp){ +#if FF_API_SEEK_PUBLIC +void av_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp) +{ + ff_update_cur_dts(s, ref_st, timestamp); +} +#endif + +void ff_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp) +{ int i; for(i = 0; i < s->nb_streams; i++) { @@ -1546,7 +1556,14 @@ int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp, wanted_timestamp, flags); } +#if FF_API_SEEK_PUBLIC int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){ + return ff_seek_frame_binary(s, stream_index, target_ts, flags); +} +#endif + +int ff_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags) +{ AVInputFormat *avif= s->iformat; int64_t av_uninit(pos_min), av_uninit(pos_max), pos, pos_limit; int64_t ts_min, ts_max, ts; @@ -1593,7 +1610,7 @@ int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts } } - pos= av_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit, ts_min, ts_max, flags, &ts, avif->read_timestamp); + pos= ff_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit, ts_min, ts_max, flags, &ts, avif->read_timestamp); if(pos<0) return -1; @@ -1601,12 +1618,29 @@ int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts if ((ret = avio_seek(s->pb, pos, SEEK_SET)) < 0) return ret; - av_update_cur_dts(s, st, ts); + ff_read_frame_flush(s); + ff_update_cur_dts(s, st, ts); return 0; } -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 )){ +#if FF_API_SEEK_PUBLIC +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 )) +{ + return ff_gen_search(s, stream_index, target_ts, pos_min, pos_max, + pos_limit, ts_min, ts_max, flags, ts_ret, + read_timestamp); +} +#endif + +int64_t ff_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 )) +{ int64_t pos, ts; int64_t start_pos, filesize; int no_change; @@ -1620,6 +1654,11 @@ int64_t av_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts, i return -1; } + if(ts_min >= target_ts){ + *ts_ret= ts_min; + return pos_min; + } + if(ts_max == AV_NOPTS_VALUE){ int step= 1024; filesize = avio_size(s->pb); @@ -1645,6 +1684,11 @@ int64_t av_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts, i pos_limit= pos_max; } + if(ts_max <= target_ts){ + *ts_ret= ts_max; + return pos_max; + } + if(ts_min > ts_max){ return -1; }else if(ts_min == ts_max){ @@ -1702,12 +1746,14 @@ int64_t av_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts, i pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max; ts = (flags & AVSEEK_FLAG_BACKWARD) ? ts_min : ts_max; +#if 0 pos_min = pos; ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX); pos_min++; ts_max = read_timestamp(s, stream_index, &pos_min, INT64_MAX); av_dlog(s, "pos=0x%"PRIx64" %"PRId64"<=%"PRId64"<=%"PRId64"\n", pos, ts_min, target_ts, ts_max); +#endif *ts_ret= ts; return pos; } @@ -1761,7 +1807,7 @@ static int seek_frame_generic(AVFormatContext *s, ie= &st->index_entries[st->nb_index_entries-1]; if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0) return ret; - av_update_cur_dts(s, st, ie->timestamp); + ff_update_cur_dts(s, st, ie->timestamp); }else{ if ((ret = avio_seek(s->pb, s->data_offset, SEEK_SET)) < 0) return ret; @@ -1777,7 +1823,7 @@ static int seek_frame_generic(AVFormatContext *s, if(stream_index == pkt.stream_index && pkt.dts > timestamp){ if(pkt.flags & AV_PKT_FLAG_KEY) break; - if(nonkey++ > 1000){ + if(nonkey++ > 1000 && st->codec->codec_id != CODEC_ID_CDGRAPHICS){ av_log(s, AV_LOG_ERROR,"seek_frame_generic failed as this stream seems to contain no keyframes after the target timestamp, %d non keyframes found\n", nonkey); break; } @@ -1798,7 +1844,7 @@ static int seek_frame_generic(AVFormatContext *s, ie = &st->index_entries[index]; if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0) return ret; - av_update_cur_dts(s, st, ie->timestamp); + ff_update_cur_dts(s, st, ie->timestamp); return 0; } @@ -1839,7 +1885,7 @@ int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int f if (s->iformat->read_timestamp && !(s->iformat->flags & AVFMT_NOBINSEARCH)) { ff_read_frame_flush(s); - return av_seek_frame_binary(s, stream_index, timestamp, flags); + return ff_seek_frame_binary(s, stream_index, timestamp, flags); } else if (!(s->iformat->flags & AVFMT_NOGENSEARCH)) { ff_read_frame_flush(s); return seek_frame_generic(s, stream_index, timestamp, flags); @@ -1918,29 +1964,24 @@ static void update_stream_timings(AVFormatContext *ic) if (start_time1 < start_time_text) start_time_text = start_time1; } else - if (start_time1 < start_time) - start_time = start_time1; + start_time = FFMIN(start_time, start_time1); if (st->duration != AV_NOPTS_VALUE) { end_time1 = start_time1 + av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q); - if (end_time1 > end_time) - end_time = end_time1; + end_time = FFMAX(end_time, end_time1); } } if (st->duration != AV_NOPTS_VALUE) { duration1 = av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q); - if (duration1 > duration) - duration = duration1; + duration = FFMAX(duration, duration1); } } if (start_time == INT64_MAX || (start_time > start_time_text && start_time - start_time_text < AV_TIME_BASE)) start_time = start_time_text; if (start_time != INT64_MAX) { ic->start_time = start_time; - if (end_time != INT64_MIN) { - if (end_time - start_time > duration) - duration = end_time - start_time; - } + if (end_time != INT64_MIN) + duration = FFMAX(duration, end_time - start_time); } if (duration != INT64_MIN && ic->duration == AV_NOPTS_VALUE) { ic->duration = duration; @@ -2094,8 +2135,7 @@ static void estimate_timings(AVFormatContext *ic, int64_t old_offset) file_size = 0; } else { file_size = avio_size(ic->pb); - if (file_size < 0) - file_size = 0; + file_size = FFMAX(0, file_size); } if ((!strcmp(ic->iformat->name, "mpeg") || @@ -2142,7 +2182,6 @@ static int has_codec_parameters(AVCodecContext *avctx) avctx->codec_id == CODEC_ID_MP1 || avctx->codec_id == CODEC_ID_MP2 || avctx->codec_id == CODEC_ID_MP3 || - avctx->codec_id == CODEC_ID_SPEEX || avctx->codec_id == CODEC_ID_CELT)) return 0; break; @@ -2161,7 +2200,7 @@ static int has_codec_parameters(AVCodecContext *avctx) static int has_decode_delay_been_guessed(AVStream *st) { return st->codec->codec_id != CODEC_ID_H264 || - st->codec_info_nb_frames >= 6 + st->codec->has_b_frames; + st->info->nb_decoded_frames >= 6; } static int try_decode_frame(AVStream *st, AVPacket *avpkt, AVDictionary **options) @@ -2187,6 +2226,8 @@ static int try_decode_frame(AVStream *st, AVPacket *avpkt, AVDictionary **option avcodec_get_frame_defaults(&picture); ret = avcodec_decode_video2(st->codec, &picture, &got_picture, avpkt); + if (got_picture) + st->info->nb_decoded_frames++; break; case AVMEDIA_TYPE_AUDIO: data_size = FFMAX(avpkt->size, AVCODEC_MAX_AUDIO_FRAME_SIZE); @@ -2223,7 +2264,7 @@ enum CodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag) return tags[i].id; } for(i=0; tags[i].id != CODEC_ID_NONE; i++) { - if (ff_toupper4(tag) == ff_toupper4(tags[i].tag)) + if (avpriv_toupper4(tag) == avpriv_toupper4(tags[i].tag)) return tags[i].id; } return CODEC_ID_NONE; @@ -2357,7 +2398,7 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options) count = 0; read_size = 0; for(;;) { - if(url_interrupt_cb()){ + if (ff_check_interrupt(&ic->interrupt_callback)){ ret= AVERROR_EXIT; av_log(ic, AV_LOG_DEBUG, "interrupted\n"); break; @@ -2761,7 +2802,17 @@ void av_close_input_file(AVFormatContext *s) avio_close(pb); } +#if FF_API_NEW_STREAM AVStream *av_new_stream(AVFormatContext *s, int id) +{ + AVStream *st = avformat_new_stream(s, NULL); + if (st) + st->id = id; + return st; +} +#endif + +AVStream *avformat_new_stream(AVFormatContext *s, AVCodec *c) { AVStream *st; int i; @@ -2782,13 +2833,12 @@ AVStream *av_new_stream(AVFormatContext *s, int id) return NULL; } - st->codec = avcodec_alloc_context3(NULL); + st->codec = avcodec_alloc_context3(c); if (s->iformat) { /* no default bitrate if decoding */ st->codec->bit_rate = 0; } st->index = s->nb_streams; - st->id = id; st->start_time = AV_NOPTS_VALUE; st->duration = AV_NOPTS_VALUE; /* we set the current DTS to 0 so that formats without any timestamps @@ -2835,7 +2885,7 @@ AVProgram *av_new_program(AVFormatContext *ac, int id) return program; } -AVChapter *ff_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title) +AVChapter *avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title) { AVChapter *chapter = NULL; int i; @@ -2959,7 +3009,7 @@ static int validate_codec_tag(AVFormatContext *s, AVStream *st) for (n = 0; s->oformat->codec_tag[n]; n++) { avctag = s->oformat->codec_tag[n]; while (avctag->id != CODEC_ID_NONE) { - if (ff_toupper4(avctag->tag) == ff_toupper4(st->codec->codec_tag)) { + if (avpriv_toupper4(avctag->tag) == avpriv_toupper4(st->codec->codec_tag)) { id = avctag->id; if (id == st->codec->codec_id) return 1; @@ -3130,9 +3180,6 @@ static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt){ av_dlog(s, "compute_pkt_fields2: pts:%"PRId64" dts:%"PRId64" cur_dts:%"PRId64" b:%d size:%d st:%d\n", pkt->pts, pkt->dts, st->cur_dts, delay, pkt->size, pkt->stream_index); -/* if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE) - return AVERROR(EINVAL);*/ - /* duration field */ if (pkt->duration == 0) { compute_frame_duration(&num, &den, st, NULL, pkt); @@ -3212,12 +3259,14 @@ int av_write_frame(AVFormatContext *s, AVPacket *pkt) return ret; } -void ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt, +int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt, int (*compare)(AVFormatContext *, AVPacket *, AVPacket *)) { AVPacketList **next_point, *this_pktl; this_pktl = av_mallocz(sizeof(AVPacketList)); + if (!this_pktl) + return AVERROR(ENOMEM); this_pktl->pkt= *pkt; pkt->destruct= NULL; // do not free original but only the copy av_dup_packet(&this_pktl->pkt); // duplicate the packet if it uses non-alloced memory @@ -3246,6 +3295,7 @@ next_non_null: s->streams[pkt->stream_index]->last_in_packet_buffer= *next_point= this_pktl; + return 0; } static int ff_interleave_compare_dts(AVFormatContext *s, AVPacket *next, AVPacket *pkt) @@ -3264,10 +3314,12 @@ int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out, AVPacket *pk AVPacketList *pktl; int stream_count=0, noninterleaved_count=0; int64_t delta_dts_max = 0; - int i; + int i, ret; if(pkt){ - ff_interleave_add_packet(s, pkt, ff_interleave_compare_dts); + ret = ff_interleave_add_packet(s, pkt, ff_interleave_compare_dts); + if (ret < 0) + return ret; } for(i=0; i < s->nb_streams; i++) { @@ -4110,9 +4162,14 @@ void ff_make_absolute_url(char *buf, int size, const char *base, int64_t ff_iso8601_to_unix_time(const char *datestr) { #if HAVE_STRPTIME - struct tm time = {0}; - strptime(datestr, "%Y - %m - %dT%T", &time); - return mktime(&time); + struct tm time1 = {0}, time2 = {0}; + char *ret1, *ret2; + ret1 = strptime(datestr, "%Y - %m - %d %T", &time1); + ret2 = strptime(datestr, "%Y - %m - %dT%T", &time2); + if (ret2 && !ret1) + return av_timegm(&time2); + else + return av_timegm(&time1); #else av_log(NULL, AV_LOG_WARNING, "strptime() unavailable on this system, cannot convert " "the date string.\n"); @@ -4133,3 +4190,24 @@ int avformat_query_codec(AVOutputFormat *ofmt, enum CodecID codec_id, int std_co } return AVERROR_PATCHWELCOME; } + +int avformat_network_init(void) +{ +#if CONFIG_NETWORK + int ret; + ff_network_inited_globally = 1; + if ((ret = ff_network_init()) < 0) + return ret; + ff_tls_init(); +#endif + return 0; +} + +int avformat_network_deinit(void) +{ +#if CONFIG_NETWORK + ff_network_close(); + ff_tls_deinit(); +#endif + return 0; +}