From patchwork Fri Sep 2 23:49:54 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Campbell X-Patchwork-Id: 403 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.140.134 with SMTP id o128csp1710689vsd; Fri, 2 Sep 2016 16:51:11 -0700 (PDT) X-Received: by 10.28.141.76 with SMTP id p73mr5200402wmd.118.1472860271849; Fri, 02 Sep 2016 16:51:11 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id l2si6826381wmi.63.2016.09.02.16.51.10; Fri, 02 Sep 2016 16:51:11 -0700 (PDT) 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 41FCE689D8F; Sat, 3 Sep 2016 02:51:00 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from p3plsmtpout003.prod.phx3.secureserver.net (p3plsmtpout003.prod.phx3.secureserver.net [208.109.80.53]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0477A689B59 for ; Sat, 3 Sep 2016 02:50:53 +0300 (EEST) Received: from ip-192-169-235-64.secureserver.net ([192.169.235.64]) by : HOSTING RELAY : with SMTP id fyDbbEab4M8BZfyDbbkk2g; Fri, 02 Sep 2016 16:49:56 -0700 x-originating-ip: 192.169.235.64 Received: from 50-245-141-73-static.hfc.comcastbusiness.net ([50.245.141.73]:33129 helo=[172.19.234.190]) by ip-192-169-235-64.secureserver.net with esmtpsa (TLSv1:DHE-RSA-CAMELLIA256-SHA:256) (Exim 4.82) (envelope-from ) id 1bfyDb-0005tf-Ft for ffmpeg-devel@ffmpeg.org; Fri, 02 Sep 2016 16:49:55 -0700 Message-ID: <57CA1022.9050304@impactstudiopro.com> Date: Fri, 02 Sep 2016 16:49:54 -0700 From: Jonathan Campbell User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: FFmpeg development discussions and patches References: <57C8DD12.7030104@impactstudiopro.com> <20160902115034.GV4692@nb4> <57C9B49B.9020804@impactstudiopro.com> <20160902200148.GW4692@nb4> <57C9DD6D.8060406@impactstudiopro.com> <20160902205623.GY4692@nb4> <57CA05C8.30605@impactstudiopro.com> <20160902233718.GC4692@nb4> In-Reply-To: <20160902233718.GC4692@nb4> X-CMAE-Envelope: MS4wfLtVbYkgJ8+QcE9HZUGhmkfStiDBwBRSrn9lzX7Vk/Owf2Pgnvh0mjrjLvEyW6p7uzdWLBMululA3wTHrThzBAvqIKGidMBhlIV2NXA7uNKAGBGGq7Xm 5FIX/GGNlvvIAPWEC1jjXqdDQcOo4iEbn144sbXIBJ1+3roaZY+/xfa/szHpiS6qaoPSO8WWArlybjJEFuMmv47Wzf0XcO6fHynFyGAO5uRpNbAHrdjn78zA Subject: Re: [FFmpeg-devel] CNG (consistent noise generation) patch for AC-3 decoder 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" On 09/02/2016 04:37 PM, Michael Niedermayer wrote: > On Fri, Sep 02, 2016 at 04:05:44PM -0700, Jonathan Campbell wrote: >> On 09/02/2016 01:56 PM, Michael Niedermayer wrote: >>> On Fri, Sep 02, 2016 at 01:13:33PM -0700, Jonathan Campbell wrote: >>>> On 09/02/2016 01:01 PM, Michael Niedermayer wrote: >>>>> On Fri, Sep 02, 2016 at 10:19:23AM -0700, Jonathan Campbell wrote: >>>>> [...] >>>>>> CRC computation isn't fast enough? What should I use then? A sum of >>>>>> byte values? >>>>> av_lfg_init() calls av_md5_sum() >>>>> av_md5_sum() is too slow to be called per ac3 frame >>>>> >>>>> [...] >>>>> >>>>> >>>>> _______________________________________________ >>>>> ffmpeg-devel mailing list >>>>> ffmpeg-devel@ffmpeg.org >>>>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel >>>> Then for this to work at better performance, I'm going to have to >>>> either directly modify the AVLFG struct state (probably not a good >>>> idea), or add a function to libavutil/lfg.c that allows "fast >>>> seeding" without the use of MD5 which could probably be something as >>>> simple as copy the 32-bit seed 16 times through c->state[] with or >>>> without modification during the loop. Sound good? >>> yes, something like this >>> probably spliting the input into 16 parts get 16 CRCs and use them >>> instead of replication is probably better >>> >>> >>> [...] >>> >>> >>> _______________________________________________ >>> ffmpeg-devel mailing list >>> ffmpeg-devel@ffmpeg.org >>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel >> How's this: >> >> void av_lfg_init_from_data(AVLFG *c, const unsigned char *data, >> unsigned int length) { >> unsigned int beg, end, segm; >> const AVCRC *avcrc; >> uint32_t crc = 0; > 1 might be better > > should be ok otherwise didnt review it deeply though > > [...] > > > _______________________________________________ > ffmpeg-devel mailing list > ffmpeg-devel@ffmpeg.org > http://ffmpeg.org/mailman/listinfo/ffmpeg-devel Finished patch. Hopefully it won't get garbled as an attachment. Jonathan Campbell diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c index fac189b..18a674b 100644 --- a/libavcodec/ac3dec.c +++ b/libavcodec/ac3dec.c @@ -1419,6 +1419,13 @@ static int ac3_decode_frame(AVCodecContext * avctx, void *data, (const uint16_t *) buf, cnt); } else memcpy(s->input_buffer, buf, FFMIN(buf_size, AC3_FRAME_BUFFER_SIZE)); + + /* if consistent noise generation is enabled, seed the linear feedback generator + * with the contents of the AC-3 frame so that the noise is identical across + * decodes given the same AC-3 frame data, for use with non-linear edititing software. */ + if (s->consistent_noise_generation) + av_lfg_init_from_data(&s->dith_state, s->input_buffer, FFMIN(buf_size, AC3_FRAME_BUFFER_SIZE)); + buf = s->input_buffer; /* initialize the GetBitContext with the start of valid AC-3 Frame */ if ((ret = init_get_bits8(&s->gbc, buf, buf_size)) < 0) diff --git a/libavcodec/ac3dec.h b/libavcodec/ac3dec.h index c2b867e..98184e9 100644 --- a/libavcodec/ac3dec.h +++ b/libavcodec/ac3dec.h @@ -177,6 +177,10 @@ typedef struct AC3DecodeContext { int end_freq[AC3_MAX_CHANNELS]; ///< end frequency bin (endmant) ///@} +///@name Consistent noise generation + int consistent_noise_generation; ///< seed noise generation with AC-3 frame on decode +///@} + ///@name Rematrixing int num_rematrixing_bands; ///< number of rematrixing bands (nrematbnd) int rematrixing_flags[4]; ///< rematrixing flags (rematflg) diff --git a/libavcodec/ac3dec_fixed.c b/libavcodec/ac3dec_fixed.c index 6416da4..1f79ade 100644 --- a/libavcodec/ac3dec_fixed.c +++ b/libavcodec/ac3dec_fixed.c @@ -168,6 +168,7 @@ static void ac3_downmix_c_fixed16(int16_t **samples, int16_t (*matrix)[2], #include "ac3dec.c" static const AVOption options[] = { + { "cons_noisegen", "enable consistent noise generation", OFFSET(consistent_noise_generation), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, PAR }, { "drc_scale", "percentage of dynamic range compression to apply", OFFSET(drc_scale), AV_OPT_TYPE_FLOAT, {.dbl = 1.0}, 0.0, 6.0, PAR }, { "heavy_compr", "enable heavy dynamic range compression", OFFSET(heavy_compression), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, PAR }, { NULL}, diff --git a/libavcodec/ac3dec_float.c b/libavcodec/ac3dec_float.c index 0a5319a..b85a4ce 100644 --- a/libavcodec/ac3dec_float.c +++ b/libavcodec/ac3dec_float.c @@ -32,6 +32,7 @@ #include "ac3dec.c" static const AVOption options[] = { + { "cons_noisegen", "enable consistent noise generation", OFFSET(consistent_noise_generation), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, PAR }, { "drc_scale", "percentage of dynamic range compression to apply", OFFSET(drc_scale), AV_OPT_TYPE_FLOAT, {.dbl = 1.0}, 0.0, 6.0, PAR }, { "heavy_compr", "enable heavy dynamic range compression", OFFSET(heavy_compression), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, PAR }, { "target_level", "target level in -dBFS (0 not applied)", OFFSET(target_level), AV_OPT_TYPE_INT, {.i64 = 0 }, -31, 0, PAR }, diff --git a/libavutil/lfg.c b/libavutil/lfg.c index 08a4f67..178d27e 100644 --- a/libavutil/lfg.c +++ b/libavutil/lfg.c @@ -23,6 +23,7 @@ #include #include #include "lfg.h" +#include "crc.h" #include "md5.h" #include "intreadwrite.h" #include "attributes.h" @@ -58,3 +59,29 @@ void av_bmg_get(AVLFG *lfg, double out[2]) out[0] = x1 * w; out[1] = x2 * w; } + +void av_lfg_init_from_data(AVLFG *c, const unsigned char *data, unsigned int length) { + unsigned int beg, end, segm; + const AVCRC *avcrc; + uint32_t crc = 1; + + c->index = 0; + + avcrc = av_crc_get_table(AV_CRC_32_IEEE); + if (avcrc == NULL) return; + + /* try to avoid integer overflow during the segmented crc loop below. + * the code below would break if "end" went backwards before "beg". */ + if (length > (UINT_MAX / 128U)) return; + + /* across 64 pieces of the incoming data, + * do a running crc of each segment and store the crc as the state for that slot. + * this works even if the length of the piece is 0 bytes. */ + beg = 0; + for (segm = 0;segm < 64;segm++) { + end = (((segm + 1) * length) / 64); + crc = av_crc(avcrc, crc, data + beg, end - beg); + c->state[segm] = (unsigned int)crc; + beg = end; + } +} diff --git a/libavutil/lfg.h b/libavutil/lfg.h index ec90562..f3c08ec 100644 --- a/libavutil/lfg.h +++ b/libavutil/lfg.h @@ -29,6 +29,11 @@ typedef struct AVLFG { void av_lfg_init(AVLFG *c, unsigned int seed); +/* + * Seed the state of the ALFG using binary data + */ +void av_lfg_init_from_data(AVLFG *c, const unsigned char *data, unsigned int length); + /** * Get the next random unsigned 32-bit number using an ALFG. *