From patchwork Tue Jan 17 13:39:20 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tobias Rapp X-Patchwork-Id: 2234 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp524073vsb; Tue, 17 Jan 2017 05:40:11 -0800 (PST) X-Received: by 10.28.94.2 with SMTP id s2mr11130996wmb.127.1484660411115; Tue, 17 Jan 2017 05:40:11 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id v188si15691266wmf.44.2017.01.17.05.40.10; Tue, 17 Jan 2017 05:40:11 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A325968A1B1; Tue, 17 Jan 2017 15:39:20 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from p1002.netstorage.at (p1002.netstorage.at [89.207.146.186]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4F64F68A13F for ; Tue, 17 Jan 2017 15:39:12 +0200 (EET) Received: from mailix (noaport.de [46.237.252.213]) by p1002.netstorage.at (Postfix) with ESMTPA id 7EA9481BD4 for ; Tue, 17 Jan 2017 14:39:21 +0100 (CET) Received: from frogstar-a.kuhnle.local (frogstar-a.kuhnle.local [192.168.0.26]) by mailix with ESMTPA ; Tue, 17 Jan 2017 14:39:21 +0100 From: Tobias Rapp To: ffmpeg-devel@ffmpeg.org Date: Tue, 17 Jan 2017 14:39:20 +0100 Message-Id: <1484660361-21502-4-git-send-email-t.rapp@noa-archive.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1484660361-21502-1-git-send-email-t.rapp@noa-archive.com> References: <1484660361-21502-1-git-send-email-t.rapp@noa-archive.com> X-PPP-Message-ID: <20170117133921.32493.65896@p1002.netstorage.at> X-PPP-Vhost: noa-archive.com Subject: [FFmpeg-devel] [PATCH 3/4] avformat/avienc: add reserve_index_space option X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Allows the user to reserve space for the ODML master index. A sufficient sized master index in the AVI header avoids storing follow-up master indexes within the 'movi' data later. If the option is omitted or zero the index size is estimated from output duration and bitrate. A worst-case bitrate for video streams is assumed in case it is not available. Note: fate reference files changed because the video stream had zero bitrate before and is guessed now. Signed-off-by: Tobias Rapp --- libavformat/avi.h | 1 - libavformat/avienc.c | 84 +++++++++++++++++++++++++++++---- libavformat/version.h | 2 +- tests/ref/fate/mpeg4-bsf-unpack-bframes | 2 +- tests/ref/lavf-fate/avi_cram | 2 +- 5 files changed, 78 insertions(+), 13 deletions(-) diff --git a/libavformat/avi.h b/libavformat/avi.h index af21f2c..b1711f0 100644 --- a/libavformat/avi.h +++ b/libavformat/avi.h @@ -29,7 +29,6 @@ #define AVIF_COPYRIGHTED 0x00020000 #define AVI_MAX_RIFF_SIZE 0x40000000LL -#define AVI_MASTER_INDEX_SIZE 256 #define AVI_MAX_STREAM_COUNT 100 /* stream header flags */ diff --git a/libavformat/avienc.c b/libavformat/avienc.c index 5d5c02a..d1feedc 100644 --- a/libavformat/avienc.c +++ b/libavformat/avienc.c @@ -21,6 +21,8 @@ //#define DEBUG +#include + #include "avformat.h" #include "internal.h" #include "avi.h" @@ -29,6 +31,7 @@ #include "mpegts.h" #include "libavformat/avlanguage.h" #include "libavutil/avstring.h" +#include "libavutil/avutil.h" #include "libavutil/internal.h" #include "libavutil/intreadwrite.h" #include "libavutil/dict.h" @@ -51,6 +54,9 @@ typedef struct AVIIentry { } AVIIentry; #define AVI_INDEX_CLUSTER_SIZE 16384 +#define AVI_MASTER_INDEX_PREFIX_SIZE (8+2+1+1+4+8+4+4) +#define AVI_MASTER_INDEX_ENTRY_SIZE 16 /* bytes per entry */ +#define AVI_MASTER_INDEX_SIZE_DEFAULT 256 /* number of entries */ typedef struct AVIIndex { int64_t indx_start; @@ -66,6 +72,8 @@ typedef struct AVIContext { int64_t riff_start, movi_list, odml_list; int64_t frames_hdr_all; int riff_id; + int reserve_index_space; + int master_index_max_size; int write_channel_mask; } AVIContext; @@ -134,6 +142,21 @@ static int avi_add_ientry(AVFormatContext *s, int stream_index, char *tag, return 0; } +static av_cold int avi_init(struct AVFormatContext *s) +{ + AVIContext *avi = s->priv_data; + + if (avi->reserve_index_space > 0) { + avi->master_index_max_size = (avi->reserve_index_space - AVI_MASTER_INDEX_PREFIX_SIZE) / AVI_MASTER_INDEX_ENTRY_SIZE; + avi->master_index_max_size = FFMAX(avi->master_index_max_size, 16); + } else + avi->master_index_max_size = AVI_MASTER_INDEX_SIZE_DEFAULT; + av_log(s, AV_LOG_DEBUG, "reserve_index_space:%d master_index_max_size:%d\n", + avi->reserve_index_space, avi->master_index_max_size); + + return 0; +} + static int64_t avi_start_new_riff(AVFormatContext *s, AVIOContext *pb, const char *riff_tag, const char *list_tag) { @@ -210,6 +233,7 @@ static int avi_write_counters(AVFormatContext *s, int riff_id) static void write_odml_master(AVFormatContext *s, int stream_index) { AVIOContext *pb = s->pb; + AVIContext *avi = s->priv_data; AVStream *st = s->streams[stream_index]; AVCodecParameters *par = st->codecpar; AVIStream *avist = st->priv_data; @@ -229,7 +253,7 @@ static void write_odml_master(AVFormatContext *s, int stream_index) /* dwChunkId */ avio_wl64(pb, 0); /* dwReserved[3] */ avio_wl32(pb, 0); /* Must be 0. */ - for (j = 0; j < AVI_MASTER_INDEX_SIZE * 2; j++) + for (j = 0; j < avi->master_index_max_size * 2; j++) avio_wl64(pb, 0); ff_end_tag(pb, avist->indexes.indx_start); } @@ -239,6 +263,7 @@ static int avi_write_header(AVFormatContext *s) AVIContext *avi = s->priv_data; AVIOContext *pb = s->pb; int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale; + int64_t max_stream_duration = 0; AVCodecParameters *video_par; AVStream *video_st = NULL; int64_t list1, list2, strh, strf; @@ -269,13 +294,43 @@ static int avi_write_header(AVFormatContext *s) video_par = NULL; for (n = 0; n < s->nb_streams; n++) { AVCodecParameters *par = s->streams[n]->codecpar; - bitrate += par->bit_rate; + AVStream *st = s->streams[n]; + int64_t stream_bitrate = par->bit_rate; + /* when bitrate is unset, assume uncompressed as a worst-case value */ + if (!stream_bitrate && par->codec_type == AVMEDIA_TYPE_VIDEO && + (st->avg_frame_rate.num > 0) && (st->avg_frame_rate.den > 0)) { + stream_bitrate = (int64_t)par->bits_per_coded_sample * par->width * par->height * + st->avg_frame_rate.num / st->avg_frame_rate.den; + av_log(s, AV_LOG_DEBUG, "Bitrate not available for stream #%d (%s), estimating %"PRId64"kbit/s\n", + n, avcodec_get_name(par->codec_id), stream_bitrate / 1000); + } + bitrate = FFMIN(bitrate + stream_bitrate, INT32_MAX); + if (st->duration > 0) { + int64_t stream_duration = av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q); + max_stream_duration = FFMAX(stream_duration, max_stream_duration); + } if (par->codec_type == AVMEDIA_TYPE_VIDEO) { video_par = par; - video_st = s->streams[n]; + video_st = st; } } + /* guess master index size based on bitrate and duration */ + if (!avi->reserve_index_space) { + double duration_est, filesize_est; + if (s->duration > 0) + duration_est = (double)s->duration / AV_TIME_BASE; + else if (max_stream_duration > 0) + duration_est = (double)max_stream_duration / AV_TIME_BASE; + else + duration_est = 10 * 60 * 60; /* default to 10 hours */ + filesize_est = duration_est * (bitrate / 8) * 1.10; /* add 10% safety margin for muxer+bitrate */ + avi->master_index_max_size = FFMAX((int)ceil(filesize_est / AVI_MAX_RIFF_SIZE) + 1, + avi->master_index_max_size); + av_log(s, AV_LOG_DEBUG, "duration_est:%0.3f, filesize_est:%0.1fGiB, master_index_max_size:%d\n", + duration_est, filesize_est / (1024*1024*1024), avi->master_index_max_size); + } + nb_frames = 0; // TODO: should be avg_frame_rate @@ -576,17 +631,18 @@ static int avi_write_ix(AVFormatContext *s) av_assert0(pb->seekable); - if (avi->riff_id >= AVI_MASTER_INDEX_SIZE && s->strict_std_compliance > FF_COMPLIANCE_NORMAL) { - av_log(s, AV_LOG_ERROR, "Invalid riff index %d >= %d\n", - avi->riff_id, AVI_MASTER_INDEX_SIZE); + if (avi->riff_id >= avi->master_index_max_size && s->strict_std_compliance > FF_COMPLIANCE_NORMAL) { + av_log(s, AV_LOG_ERROR, "Invalid riff index %d >= %d, " + "consider increasing the 'reserve_index_space' option value\n", + avi->riff_id, avi->master_index_max_size); return AVERROR(EINVAL); } for (i = 0; i < s->nb_streams; i++) { AVIStream *avist = s->streams[i]->priv_data; - if (avi->riff_id - avist->indexes.master_odml_riff_id_base == AVI_MASTER_INDEX_SIZE) { + if (avi->riff_id - avist->indexes.master_odml_riff_id_base == avi->master_index_max_size) { int64_t pos; - int size = 8+2+1+1+4+8+4+4+16*AVI_MASTER_INDEX_SIZE; + int size = AVI_MASTER_INDEX_PREFIX_SIZE + AVI_MASTER_INDEX_ENTRY_SIZE * avi->master_index_max_size; pos = avio_tell(pb); update_odml_entry(s, i, pos, size, AVI_UPDATE_ODML_ENTRY_MASTER); @@ -594,7 +650,7 @@ static int avi_write_ix(AVFormatContext *s) av_assert1(avio_tell(pb) - pos == size); avist->indexes.master_odml_riff_id_base = avi->riff_id - 1; } - av_assert0(avi->riff_id - avist->indexes.master_odml_riff_id_base < AVI_MASTER_INDEX_SIZE); + av_assert0(avi->riff_id - avist->indexes.master_odml_riff_id_base < avi->master_index_max_size); } for (i = 0; i < s->nb_streams; i++) { @@ -906,6 +962,14 @@ static int avi_write_trailer(AVFormatContext *s) } } + if (avi->riff_id >= avi->master_index_max_size && s->strict_std_compliance <= FF_COMPLIANCE_NORMAL) { + int index_space = AVI_MASTER_INDEX_PREFIX_SIZE + + AVI_MASTER_INDEX_ENTRY_SIZE * avi->riff_id; + av_log(s, AV_LOG_WARNING, "Output file not strictly OpenDML compliant, " + "consider re-muxing with 'reserve_index_space' option value >= %d\n", + index_space); + } + for (i = 0; i < s->nb_streams; i++) { AVIStream *avist = s->streams[i]->priv_data; for (j = 0; j < avist->indexes.ents_allocated / AVI_INDEX_CLUSTER_SIZE; j++) @@ -924,6 +988,7 @@ static int avi_write_trailer(AVFormatContext *s) #define OFFSET(x) offsetof(AVIContext, x) #define ENC AV_OPT_FLAG_ENCODING_PARAM static const AVOption options[] = { + { "reserve_index_space", "reserve space (in bytes) at the beginning of the file for each stream index", OFFSET(reserve_index_space), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, ENC }, { "write_channel_mask", "write channel mask into wave format header", OFFSET(write_channel_mask), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, ENC }, { NULL }, }; @@ -943,6 +1008,7 @@ AVOutputFormat ff_avi_muxer = { .priv_data_size = sizeof(AVIContext), .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_AC3, .video_codec = AV_CODEC_ID_MPEG4, + .init = avi_init, .write_header = avi_write_header, .write_packet = avi_write_packet, .write_trailer = avi_write_trailer, diff --git a/libavformat/version.h b/libavformat/version.h index 21cc8a9..70c8467 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -33,7 +33,7 @@ // Also please add any ticket numbers that you believe might be affected here #define LIBAVFORMAT_VERSION_MAJOR 57 #define LIBAVFORMAT_VERSION_MINOR 62 -#define LIBAVFORMAT_VERSION_MICRO 100 +#define LIBAVFORMAT_VERSION_MICRO 101 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ LIBAVFORMAT_VERSION_MINOR, \ diff --git a/tests/ref/fate/mpeg4-bsf-unpack-bframes b/tests/ref/fate/mpeg4-bsf-unpack-bframes index 162d436..499f825 100644 --- a/tests/ref/fate/mpeg4-bsf-unpack-bframes +++ b/tests/ref/fate/mpeg4-bsf-unpack-bframes @@ -1 +1 @@ -c9535e459c2ee4ead6d84b93bc7e9f46 +bcb771f8e756290d63d69b6268346ab2 diff --git a/tests/ref/lavf-fate/avi_cram b/tests/ref/lavf-fate/avi_cram index 82882fb..1217bcb 100644 --- a/tests/ref/lavf-fate/avi_cram +++ b/tests/ref/lavf-fate/avi_cram @@ -1,3 +1,3 @@ -6fc88702c23b895c305c5e1f51a0904e *./tests/data/lavf-fate/lavf.avi +87acefc3f163bb2b89de6b055eb721e8 *./tests/data/lavf-fate/lavf.avi 928260 ./tests/data/lavf-fate/lavf.avi ./tests/data/lavf-fate/lavf.avi CRC=0xa4770de2