CNG (consistent noise generation) patch for AC-3 decoder
I finished the consistent noise generation patch for AC-3 decoding. Set AVOption "cons_noisegen" to 1 (true) to enable it. Git repository: https://github.com/joncampbell123/FFmpeg.git commit dbd086586f0ad1591ea2013293bbb6e4dbfd0455 Author: Jonathan Campbell <jonathan@castus.tv> Date: Thu Sep 1 18:46:16 2016 -0700 AC-3 consistent noise generation: avopt -cons_noisegen <number> When -cons_noisegen 1, the linear feedback generator used for AC-3 dithering is seeded with the contents of the AC-3 frame. Seeding from the AC-3 frame ensures the dithering noise comes out exactly the same when given the same AC-3 frame data, which can then be used by non-linear editing software to reliably decode discontinuous segments of an AC-3 bitstream without gaps or discontinuities. Jonathan Campbell Patch follows, hope Thunderbird doesn't garble it: diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c index fac189b..28048d7 100644 --- a/libavcodec/ac3dec.c +++ b/libavcodec/ac3dec.c @@ -1419,6 +1419,19 @@ 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) { + const AVCRC *avcrc = av_crc_get_table(AV_CRC_32_IEEE); + + if (avcrc != NULL) + av_lfg_init(&s->dith_state, av_crc(avcrc, 0, s->input_buffer, FFMIN(buf_size, AC3_FRAME_BUFFER_SIZE))); + else + av_log(avctx, AV_LOG_ERROR, "CNG unable to seed from frame"); + } + 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 },
On Thu, Sep 01, 2016 at 06:59:46PM -0700, Jonathan Campbell wrote:
I finished the consistent noise generation patch for AC-3 decoding.
Set AVOption "cons_noisegen" to 1 (true) to enable it.
Git repository: https://github.com/joncampbell123/FFmpeg.git
commit dbd086586f0ad1591ea2013293bbb6e4dbfd0455 Author: Jonathan Campbell <jonathan@castus.tv> Date: Thu Sep 1 18:46:16 2016 -0700
AC-3 consistent noise generation: avopt -cons_noisegen <number>
When -cons_noisegen 1, the linear feedback generator used for AC-3 dithering is seeded with the contents of the AC-3 frame. Seeding from the AC-3 frame ensures the dithering noise comes out exactly the same when given the same AC-3 frame data, which can then be used by non-linear editing software to reliably decode discontinuous segments of an AC-3 bitstream without gaps or discontinuities.
Jonathan Campbell
Patch follows, hope Thunderbird doesn't garble it:
diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c index fac189b..28048d7 100644 --- a/libavcodec/ac3dec.c +++ b/libavcodec/ac3dec.c @@ -1419,6 +1419,19 @@ 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));
patch looks newline garbled [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB The misfortune of the wise is better than the prosperity of the fool. -- Epicurus
On 09/01/2016 07:13 PM, Michael Niedermayer wrote:
I finished the consistent noise generation patch for AC-3 decoding.
Set AVOption "cons_noisegen" to 1 (true) to enable it.
Git repository: https://github.com/joncampbell123/FFmpeg.git
commit dbd086586f0ad1591ea2013293bbb6e4dbfd0455 Author: Jonathan Campbell <jonathan@castus.tv> Date: Thu Sep 1 18:46:16 2016 -0700
AC-3 consistent noise generation: avopt -cons_noisegen <number>
When -cons_noisegen 1, the linear feedback generator used for AC-3 dithering is seeded with the contents of the AC-3 frame. Seeding from the AC-3 frame ensures the dithering noise comes out exactly the same when given the same AC-3 frame data, which can then be used by non-linear editing software to reliably decode discontinuous segments of an AC-3 bitstream without gaps or discontinuities.
Jonathan Campbell
Patch follows, hope Thunderbird doesn't garble it:
diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c index fac189b..28048d7 100644 --- a/libavcodec/ac3dec.c +++ b/libavcodec/ac3dec.c @@ -1419,6 +1419,19 @@ 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));
On Thu, Sep 01, 2016 at 06:59:46PM -0700, Jonathan Campbell wrote: patch looks newline garbled
[...]
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel Yeah, figured that would happen.
Can you accept patches directly from Github? https://github.com/joncampbell123/FFmpeg/commit/dbd086586f0ad1591ea2013293bb... Jonathan Campbell
On 09/01/2016 07:13 PM, Michael Niedermayer wrote:
I finished the consistent noise generation patch for AC-3 decoding.
Set AVOption "cons_noisegen" to 1 (true) to enable it.
Git repository: https://github.com/joncampbell123/FFmpeg.git
commit dbd086586f0ad1591ea2013293bbb6e4dbfd0455 Author: Jonathan Campbell <jonathan@castus.tv> Date: Thu Sep 1 18:46:16 2016 -0700
AC-3 consistent noise generation: avopt -cons_noisegen <number>
When -cons_noisegen 1, the linear feedback generator used for AC-3 dithering is seeded with the contents of the AC-3 frame. Seeding from the AC-3 frame ensures the dithering noise comes out exactly the same when given the same AC-3 frame data, which can then be used by non-linear editing software to reliably decode discontinuous segments of an AC-3 bitstream without gaps or discontinuities.
Jonathan Campbell
Patch follows, hope Thunderbird doesn't garble it:
diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c index fac189b..28048d7 100644 --- a/libavcodec/ac3dec.c +++ b/libavcodec/ac3dec.c @@ -1419,6 +1419,19 @@ 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));
On Thu, Sep 01, 2016 at 06:59:46PM -0700, Jonathan Campbell wrote: patch looks newline garbled
[...]
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel Figured that would happen.
Can you accept patches directly from Github? https://github.com/joncampbell123/FFmpeg/commit/dbd086586f0ad1591ea2013293bb... Jonathan Campbell
On Thu, Sep 01, 2016 at 06:59:46PM -0700, Jonathan Campbell wrote:
I finished the consistent noise generation patch for AC-3 decoding.
Set AVOption "cons_noisegen" to 1 (true) to enable it.
Git repository: https://github.com/joncampbell123/FFmpeg.git
commit dbd086586f0ad1591ea2013293bbb6e4dbfd0455 Author: Jonathan Campbell <jonathan@castus.tv> Date: Thu Sep 1 18:46:16 2016 -0700
AC-3 consistent noise generation: avopt -cons_noisegen <number>
When -cons_noisegen 1, the linear feedback generator used for AC-3 dithering is seeded with the contents of the AC-3 frame. Seeding from the AC-3 frame ensures the dithering noise comes out exactly the same when given the same AC-3 frame data, which can then be used by non-linear editing software to reliably decode discontinuous segments of an AC-3 bitstream without gaps or discontinuities.
Jonathan Campbell
Patch follows, hope Thunderbird doesn't garble it:
diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c index fac189b..28048d7 100644 --- a/libavcodec/ac3dec.c +++ b/libavcodec/ac3dec.c @@ -1419,6 +1419,19 @@ 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) { + const AVCRC *avcrc = av_crc_get_table(AV_CRC_32_IEEE); + + if (avcrc != NULL) + av_lfg_init(&s->dith_state, av_crc(avcrc, 0, s->input_buffer, FFMIN(buf_size, AC3_FRAME_BUFFER_SIZE)));
av_lfg_init involves md5 which is slow and not apropriate to be executed per frame, this would double the time to decode a frame 965641 decicycles in rndint, 64 runs, 0 skips 2375023 decicycles in FRAME, 64 runs, 0 skips write/use a faster way to init the LFG please [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB If you fake or manipulate statistics in a paper in physics you will never get a job again. If you fake or manipulate statistics in a paper in medicin you will get a job for life at the pharma industry.
CRC computation isn't fast enough? What should I use then? A sum of byte values? Jonathan Campbell CASTUS On 09/02/2016 04:50 AM, Michael Niedermayer wrote:
On Thu, Sep 01, 2016 at 06:59:46PM -0700, Jonathan Campbell wrote:
I finished the consistent noise generation patch for AC-3 decoding.
Set AVOption "cons_noisegen" to 1 (true) to enable it.
Git repository: https://github.com/joncampbell123/FFmpeg.git
commit dbd086586f0ad1591ea2013293bbb6e4dbfd0455 Author: Jonathan Campbell <jonathan@castus.tv> Date: Thu Sep 1 18:46:16 2016 -0700
AC-3 consistent noise generation: avopt -cons_noisegen <number>
When -cons_noisegen 1, the linear feedback generator used for AC-3 dithering is seeded with the contents of the AC-3 frame. Seeding from the AC-3 frame ensures the dithering noise comes out exactly the same when given the same AC-3 frame data, which can then be used by non-linear editing software to reliably decode discontinuous segments of an AC-3 bitstream without gaps or discontinuities.
Jonathan Campbell
Patch follows, hope Thunderbird doesn't garble it:
diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c index fac189b..28048d7 100644 --- a/libavcodec/ac3dec.c +++ b/libavcodec/ac3dec.c @@ -1419,6 +1419,19 @@ 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) { + const AVCRC *avcrc = av_crc_get_table(AV_CRC_32_IEEE); + + if (avcrc != NULL) + av_lfg_init(&s->dith_state, av_crc(avcrc, 0, s->input_buffer, FFMIN(buf_size, AC3_FRAME_BUFFER_SIZE))); av_lfg_init involves md5 which is slow and not apropriate to be executed per frame, this would double the time to decode a frame
965641 decicycles in rndint, 64 runs, 0 skips 2375023 decicycles in FRAME, 64 runs, 0 skips
write/use a faster way to init the LFG please
[...]
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
On 09/02/2016 04:50 AM, Michael Niedermayer wrote:
On Thu, Sep 01, 2016 at 06:59:46PM -0700, Jonathan Campbell wrote:
I finished the consistent noise generation patch for AC-3 decoding.
Set AVOption "cons_noisegen" to 1 (true) to enable it.
Git repository: https://github.com/joncampbell123/FFmpeg.git
commit dbd086586f0ad1591ea2013293bbb6e4dbfd0455 Author: Jonathan Campbell <jonathan@castus.tv> Date: Thu Sep 1 18:46:16 2016 -0700
AC-3 consistent noise generation: avopt -cons_noisegen <number>
When -cons_noisegen 1, the linear feedback generator used for AC-3 dithering is seeded with the contents of the AC-3 frame. Seeding from the AC-3 frame ensures the dithering noise comes out exactly the same when given the same AC-3 frame data, which can then be used by non-linear editing software to reliably decode discontinuous segments of an AC-3 bitstream without gaps or discontinuities.
Jonathan Campbell
Patch follows, hope Thunderbird doesn't garble it:
diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c index fac189b..28048d7 100644 --- a/libavcodec/ac3dec.c +++ b/libavcodec/ac3dec.c @@ -1419,6 +1419,19 @@ 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) { + const AVCRC *avcrc = av_crc_get_table(AV_CRC_32_IEEE); + + if (avcrc != NULL) + av_lfg_init(&s->dith_state, av_crc(avcrc, 0, s->input_buffer, FFMIN(buf_size, AC3_FRAME_BUFFER_SIZE))); av_lfg_init involves md5 which is slow and not apropriate to be executed per frame, this would double the time to decode a frame
965641 decicycles in rndint, 64 runs, 0 skips 2375023 decicycles in FRAME, 64 runs, 0 skips
write/use a faster way to init the LFG please
[...]
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel (sorry..)
CRC computation isn't fast enough? What should I use then? A sum of byte values? Jonathan Campbell CASTUS
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 [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB it is not once nor twice but times without number that the same ideas make their appearance in the world. -- Aristotle
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
On 09/02/2016 01:01 PM, Michael Niedermayer wrote: 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? Jonathan Campbell CASTUS
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 [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB You can kill me, but you cannot change the truth.
On 09/02/2016 01:56 PM, Michael Niedermayer 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
On Fri, Sep 02, 2016 at 01:13:33PM -0700, Jonathan Campbell wrote: 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; 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; } } I also realize that c->state[] is an array of 64 unsigned ints, not 64 bytes. A running CRC over the data is used to fill in all 64 slots of the LFG. This code should work correctly even if any one of the 64 "slots" is computed from 0 bytes of the source (when length < 64). Jonathan Campbell
On Fri, Sep 02, 2016 at 04:05:44PM -0700, Jonathan Campbell wrote:
On 09/02/2016 01:56 PM, Michael Niedermayer 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
On Fri, Sep 02, 2016 at 01:13:33PM -0700, Jonathan Campbell wrote: 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 [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB I do not agree with what you have to say, but I'll defend to the death your right to say it. -- Voltaire
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 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
On Fri, Sep 02, 2016 at 01:13:33PM -0700, Jonathan Campbell wrote: 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
On Fri, Sep 02, 2016 at 04:49:54PM -0700, Jonathan Campbell wrote:
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 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
On Fri, Sep 02, 2016 at 01:13:33PM -0700, Jonathan Campbell wrote: 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
libavcodec/ac3dec.c | 7 +++++++ libavcodec/ac3dec.h | 4 ++++ libavcodec/ac3dec_fixed.c | 1 + libavcodec/ac3dec_float.c | 1 +
libavutil/lfg.c | 27 +++++++++++++++++++++++++++ libavutil/lfg.h | 5 +++++
should be in a different patch also needs minor version bump and APIChanges update also please generate patches with git format-patch or git send-email see the respective man pages (otherwise they woul be lacking commit messages) thx [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Why not whip the teacher when the pupil misbehaves? -- Diogenes of Sinope
should be in a different patch also needs minor version bump and APIChanges update
also please generate patches with git format-patch or git send-email see the respective man pages (otherwise they woul be lacking commit messages)
thx
[...]
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel One patch for the new function in lfg.c, and another patch to use the addition in the ac3 decoder, right? I can do that.
The version minor bump would bring libavutil 55.30.100 (or should it be 55.30.0?). I'll format the patch correctly. Jonathan Campbell
On Fri, Sep 02, 2016 at 05:31:35PM -0700, Jonathan Campbell wrote:
should be in a different patch also needs minor version bump and APIChanges update
also please generate patches with git format-patch or git send-email see the respective man pages (otherwise they woul be lacking commit messages)
thx
[...]
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
One patch for the new function in lfg.c, and another patch to use the addition in the ac3 decoder, right? I can do that.
yes
The version minor bump would bring libavutil 55.30.100 (or should it be 55.30.0?). I'll format the patch correctly.
55.30.100 thx [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Observe your enemies, for they first find out your faults. -- Antisthenes
On 09/02/2016 05:35 PM, Michael Niedermayer wrote:
On Fri, Sep 02, 2016 at 05:31:35PM -0700, Jonathan Campbell wrote:
should be in a different patch also needs minor version bump and APIChanges update
also please generate patches with git format-patch or git send-email see the respective man pages (otherwise they woul be lacking commit messages)
thx
[...]
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
One patch for the new function in lfg.c, and another patch to use the addition in the ac3 decoder, right? I can do that.
yes
The version minor bump would bring libavutil 55.30.100 (or should it be 55.30.0?). I'll format the patch correctly.
55.30.100
thx
[...]
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Here you go (as attachments). Jonathan Campbell
Hi! 2016-09-03 12:50 GMT+02:00 Jonathan Campbell <jonathan@impactstudiopro.com>:
Here you go (as attachments).
The changes to lfg and the version bump must be one patch.
+ { "cons_noisegen", "enable consistent noise generation", OFFSET(consistent_noise_generation), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, PAR },
If this change makes sense why is it not the default? Carl Eugen
On 9/3/2016 8:09 AM, Carl Eugen Hoyos wrote:
The changes to lfg and the version bump must be one patch.
They will be squashed. See https://ffmpeg.org/pipermail/ffmpeg-devel/2016-August/198446.html https://ffmpeg.org/pipermail/ffmpeg-devel/2016-August/198448.html https://ffmpeg.org/pipermail/ffmpeg-devel/2016-August/198449.html
On 09/03/2016 04:09 AM, Carl Eugen Hoyos wrote:
Hi!
2016-09-03 12:50 GMT+02:00 Jonathan Campbell <jonathan@impactstudiopro.com>:
Here you go (as attachments).
The changes to lfg and the version bump must be one patch.
+ { "cons_noisegen", "enable consistent noise generation", OFFSET(consistent_noise_generation), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, PAR },
If this change makes sense why is it not the default?
Carl Eugen _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
The option is intended for non-linear editing software that needs to be able to decode AC-3 from any arbitrary point in the stream. Making the dithering noise consistent means the AC-3 decoder will produce the same audio samples on output no matter where you begin decoding or how many frames you've decoded. If the dithering noise is allowed to vary, then decoding from point A to B, freeing the decoder context, then later allocating a context and resuming decode from point B to C, will produce a slight discontinuity at point B where the decoded audio is put together because the dithering noise applied to some frequency bands came out differently between the two decodes. If the noise is made consistent, then the decoded audio at point B will come out the exact same as if you had decoded continuously from point A to C. But, if you just want to play a media file and you don't care about that consistency, then the CPU work to ensure a consistent decode like that is a waste of effort. Playing, streaming and transcoding sequentially is probably 99 percent of FFMPEG's general use, right? I'm well aware the AC-3 decoder has a window delay in the frequency domain. Decoding from point B to C actually means going to point B, stepping back 1 to 2 AC-3 frames, decoding up to B and discarding the audio, then taking decoded audio from point B on. Do you understand now why this is useful for NLE software, but should not be enabled by default? Jonathan Campbell
On 9/3/2016 7:50 AM, Jonathan Campbell wrote:
+int av_lfg_init_from_data(AVLFG *c, const uint8_t *data, unsigned int length) { + unsigned int beg, end, segm; + const AVCRC *avcrc; + uint32_t crc = 1; + + c->index = 0;
The AVLFG struct should IMO remain untouched if init is guaranteed to fail, as it would be the case with an out or range length value, so move this after that check.
+ avcrc = av_crc_get_table(AV_CRC_32_IEEE); /* This can't fail. It's a well-defined table in crc.c */
You could also move this one below.
+ + /* avoid integer overflow in the loop below. */ + if (length > (UINT_MAX / 128U)) return AVERROR(EINVAL); + + /* across 64 segments 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 segment 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);
The thing about speed made me wonder, wouldn't using adler32 be faster? If so, this feature could maybe even be enabled by default with it.
+ c->state[segm] = (unsigned int)crc; + beg = end; + } + + return 0; +} diff --git a/libavutil/lfg.h b/libavutil/lfg.h index ec90562..72eb673 100644 --- a/libavutil/lfg.h +++ b/libavutil/lfg.h @@ -22,6 +22,8 @@ #ifndef AVUTIL_LFG_H #define AVUTIL_LFG_H
+#include <stdint.h> /* uint8_t type */ + typedef struct AVLFG { unsigned int state[64]; int index; @@ -29,6 +31,13 @@ typedef struct AVLFG {
void av_lfg_init(AVLFG *c, unsigned int seed);
+/*
This is meant for doxygen, so /** like in every other function.
+ * Seed the state of the ALFG using binary data. + * + * Return value: 0 on success, negative value (AVERROR) on failure. + */ +int av_lfg_init_from_data(AVLFG *c, const uint8_t *data, unsigned int length); + /** * Get the next random unsigned 32-bit number using an ALFG. *
No more comments for me.
On 9/3/2016 11:07 AM, James Almer wrote:
+ + /* avoid integer overflow in the loop below. */ + if (length > (UINT_MAX / 128U)) return AVERROR(EINVAL); + + /* across 64 segments 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 segment 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); The thing about speed made me wonder, wouldn't using adler32 be faster?
Nevermind, probably not a good idea. adler32 wouldn't be a good seed for lfg.
On 09/03/2016 07:07 AM, James Almer wrote:
On 9/3/2016 7:50 AM, Jonathan Campbell wrote:
+int av_lfg_init_from_data(AVLFG *c, const uint8_t *data, unsigned int length) { + unsigned int beg, end, segm; + const AVCRC *avcrc; + uint32_t crc = 1; + + c->index = 0;
The AVLFG struct should IMO remain untouched if init is guaranteed to fail, as it would be the case with an out or range length value, so move this after that check.
+ avcrc = av_crc_get_table(AV_CRC_32_IEEE); /* This can't fail. It's a well-defined table in crc.c */
You could also move this one below.
+ + /* avoid integer overflow in the loop below. */ + if (length > (UINT_MAX / 128U)) return AVERROR(EINVAL); + + /* across 64 segments 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 segment 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);
The thing about speed made me wonder, wouldn't using adler32 be faster? If so, this feature could maybe even be enabled by default with it.
+ c->state[segm] = (unsigned int)crc; + beg = end; + } + + return 0; +} diff --git a/libavutil/lfg.h b/libavutil/lfg.h index ec90562..72eb673 100644 --- a/libavutil/lfg.h +++ b/libavutil/lfg.h @@ -22,6 +22,8 @@ #ifndef AVUTIL_LFG_H #define AVUTIL_LFG_H
+#include <stdint.h> /* uint8_t type */ + typedef struct AVLFG { unsigned int state[64]; int index; @@ -29,6 +31,13 @@ typedef struct AVLFG {
void av_lfg_init(AVLFG *c, unsigned int seed);
+/*
This is meant for doxygen, so /** like in every other function.
+ * Seed the state of the ALFG using binary data. + * + * Return value: 0 on success, negative value (AVERROR) on failure. + */ +int av_lfg_init_from_data(AVLFG *c, const uint8_t *data, unsigned int length); + /** * Get the next random unsigned 32-bit number using an ALFG. *
No more comments for me.
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Here you go. Jonathan Campbell
On Sat, Sep 03, 2016 at 03:45:17PM -0700, Jonathan Campbell wrote:
On 09/03/2016 07:07 AM, James Almer wrote:
On 9/3/2016 7:50 AM, Jonathan Campbell wrote:
+int av_lfg_init_from_data(AVLFG *c, const uint8_t *data, unsigned int length) { + unsigned int beg, end, segm; + const AVCRC *avcrc; + uint32_t crc = 1; + + c->index = 0;
The AVLFG struct should IMO remain untouched if init is guaranteed to fail, as it would be the case with an out or range length value, so move this after that check.
+ avcrc = av_crc_get_table(AV_CRC_32_IEEE); /* This can't fail. It's a well-defined table in crc.c */
You could also move this one below.
+ + /* avoid integer overflow in the loop below. */ + if (length > (UINT_MAX / 128U)) return AVERROR(EINVAL); + + /* across 64 segments 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 segment 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);
The thing about speed made me wonder, wouldn't using adler32 be faster? If so, this feature could maybe even be enabled by default with it.
+ c->state[segm] = (unsigned int)crc; + beg = end; + } + + return 0; +} diff --git a/libavutil/lfg.h b/libavutil/lfg.h index ec90562..72eb673 100644 --- a/libavutil/lfg.h +++ b/libavutil/lfg.h @@ -22,6 +22,8 @@ #ifndef AVUTIL_LFG_H #define AVUTIL_LFG_H
+#include <stdint.h> /* uint8_t type */ + typedef struct AVLFG { unsigned int state[64]; int index; @@ -29,6 +31,13 @@ typedef struct AVLFG {
void av_lfg_init(AVLFG *c, unsigned int seed);
+/*
This is meant for doxygen, so /** like in every other function.
+ * Seed the state of the ALFG using binary data. + * + * Return value: 0 on success, negative value (AVERROR) on failure. + */ +int av_lfg_init_from_data(AVLFG *c, const uint8_t *data, unsigned int length); + /** * Get the next random unsigned 32-bit number using an ALFG. *
No more comments for me.
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Here you go.
combined the related patches, added APIdocs update and pushed thanks [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB I am the wisest man alive, for I know one thing, and that is that I know nothing. -- Socrates
On 9/2/2016 9:31 PM, Jonathan Campbell wrote:
should be in a different patch also needs minor version bump and APIChanges update
also please generate patches with git format-patch or git send-email see the respective man pages (otherwise they woul be lacking commit messages)
thx
[...]
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel One patch for the new function in lfg.c, and another patch to use the addition in the ac3 decoder, right? I can do that.
The version minor bump would bring libavutil 55.30.100 (or should it be 55.30.0?). I'll format the patch correctly.
The former. Micro version always starts at 100 for all the libraries. See the doxy in libavutil/version.h for a full explanation of how it works.
Jonathan Campbell
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
On 9/2/2016 8:49 PM, Jonathan Campbell wrote:
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 },
Bump libavcodec micro version by 1 in its own patch as Michael requested.
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 <limits.h> #include <math.h> #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) {
Don't return void if one of the arguments can be invalid. The user needs to know if init failed in some way. Return int. Also, crc (and every other hash function) expect const uint8_t*, so use that for data.
+ unsigned int beg, end, segm; + const AVCRC *avcrc; + uint32_t crc = 1; + + c->index = 0; + + avcrc = av_crc_get_table(AV_CRC_32_IEEE);
This can't fail. It's a well defined table in crc.c, so just remove the check below.
+ 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;
return AVERROR(EINVAL).
+ + /* 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; + }
return 0 on success, of course.
+} 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 + */
Make sure to mention possible return values are 0 on success, negative value on failure after the above changes.
+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. *
You should add a line in doc/APIChanges about the new function, and bump minor version for libavutil as Michael requested.
On 2 September 2016 at 02:59, Jonathan Campbell < jonathan@impactstudiopro.com> wrote:
I finished the consistent noise generation patch for AC-3 decoding.
Set AVOption "cons_noisegen" to 1 (true) to enable it.
Git repository: https://github.com/joncampbell123/FFmpeg.git
commit dbd086586f0ad1591ea2013293bbb6e4dbfd0455 Author: Jonathan Campbell <jonathan@castus.tv> Date: Thu Sep 1 18:46:16 2016 -0700
AC-3 consistent noise generation: avopt -cons_noisegen <number>
When -cons_noisegen 1, the linear feedback generator used for AC-3 dithering is seeded with the contents of the AC-3 frame. Seeding from the AC-3 frame ensures the dithering noise comes out exactly the same when given the same AC-3 frame data, which can then be used by non-linear editing software to reliably decode discontinuous segments of an AC-3 bitstream without gaps or discontinuities.
Jonathan Campbell
Patch follows, hope Thunderbird doesn't garble it:
diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c index fac189b..28048d7 100644 --- a/libavcodec/ac3dec.c +++ b/libavcodec/ac3dec.c @@ -1419,6 +1419,19 @@ 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) { + const AVCRC *avcrc = av_crc_get_table(AV_CRC_32_IEEE); + + if (avcrc != NULL) + av_lfg_init(&s->dith_state, av_crc(avcrc, 0, s->input_buffer, FFMIN(buf_size, AC3_FRAME_BUFFER_SIZE))); + else + av_log(avctx, AV_LOG_ERROR, "CNG unable to seed from frame"); + } + 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 },
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Any random noise, hardcoded constant or not sill sounds like white noise. What is the point of this patch?
On 09/03/2016 01:32 PM, Rostislav Pehlivanov wrote:
On 2 September 2016 at 02:59, Jonathan Campbell < jonathan@impactstudiopro.com> wrote:
I finished the consistent noise generation patch for AC-3 decoding.
Set AVOption "cons_noisegen" to 1 (true) to enable it.
Git repository: https://github.com/joncampbell123/FFmpeg.git
commit dbd086586f0ad1591ea2013293bbb6e4dbfd0455 Author: Jonathan Campbell <jonathan@castus.tv> Date: Thu Sep 1 18:46:16 2016 -0700
AC-3 consistent noise generation: avopt -cons_noisegen <number>
When -cons_noisegen 1, the linear feedback generator used for AC-3 dithering is seeded with the contents of the AC-3 frame. Seeding from the AC-3 frame ensures the dithering noise comes out exactly the same when given the same AC-3 frame data, which can then be used by non-linear editing software to reliably decode discontinuous segments of an AC-3 bitstream without gaps or discontinuities.
Jonathan Campbell
Patch follows, hope Thunderbird doesn't garble it:
diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c index fac189b..28048d7 100644 --- a/libavcodec/ac3dec.c +++ b/libavcodec/ac3dec.c @@ -1419,6 +1419,19 @@ 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) { + const AVCRC *avcrc = av_crc_get_table(AV_CRC_32_IEEE); + + if (avcrc != NULL) + av_lfg_init(&s->dith_state, av_crc(avcrc, 0, s->input_buffer, FFMIN(buf_size, AC3_FRAME_BUFFER_SIZE))); + else + av_log(avctx, AV_LOG_ERROR, "CNG unable to seed from frame"); + } + 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 },
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Any random noise, hardcoded constant or not sill sounds like white noise. Yes.
What is the point of this patch? To make the random noise consistent with the AC-3 frame data given to the decoder, so that decoding the same data gives the exact same audio samples. It doesn't matter to normal file playback but it does matter to non-linear editing software that would want the decoded audio to be consistent for editing purposes no matter where or how much it decodes.
Without CNG, the dithering would change the audio slightly every time the same audio is decoded and that can cause audible discontinuities between cuts on the NLE's timeline even if the audio across the cuts is continuous from one to the other. The discontinuities are most noticeable if the AC-3 audio is slowed down or encoded at a lower sample rate like 16KHz.
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Jonathan Campbell
participants (6)
-
Carl Eugen Hoyos -
James Almer -
Jonathan Campbell -
Michael Niedermayer -
Michael Niedermayer -
Rostislav Pehlivanov