2011-06-07 16:47:09 +00:00
|
|
|
/*
|
|
|
|
* AC-3 encoder & E-AC-3 encoder common header
|
|
|
|
* Copyright (c) 2000 Fabrice Bellard
|
|
|
|
* Copyright (c) 2006-2010 Justin Ruggles <justin.ruggles@gmail.com>
|
|
|
|
*
|
2013-08-15 21:12:51 +00:00
|
|
|
* This file is part of FFmpeg.
|
2011-06-07 16:47:09 +00:00
|
|
|
*
|
2013-08-15 21:12:51 +00:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2011-06-07 16:47:09 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2013-08-15 21:12:51 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2011-06-07 16:47:09 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2013-08-15 21:12:51 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2011-06-07 16:47:09 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* AC-3 encoder & E-AC-3 encoder common header
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef AVCODEC_AC3ENC_H
|
|
|
|
#define AVCODEC_AC3ENC_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2012-05-21 16:58:41 +00:00
|
|
|
|
2011-06-07 16:47:09 +00:00
|
|
|
#include "ac3.h"
|
|
|
|
#include "ac3dsp.h"
|
|
|
|
#include "avcodec.h"
|
|
|
|
#include "fft.h"
|
2014-02-26 01:26:29 +00:00
|
|
|
#include "mathops.h"
|
2014-02-08 01:59:58 +00:00
|
|
|
#include "me_cmp.h"
|
2014-02-26 01:26:29 +00:00
|
|
|
#include "put_bits.h"
|
2014-01-16 16:30:19 +00:00
|
|
|
#include "audiodsp.h"
|
2011-06-07 16:47:09 +00:00
|
|
|
|
2020-12-03 03:13:28 +00:00
|
|
|
#ifndef AC3ENC_FLOAT
|
|
|
|
#define AC3ENC_FLOAT 0
|
2011-06-07 16:47:09 +00:00
|
|
|
#endif
|
|
|
|
|
2020-12-03 03:13:28 +00:00
|
|
|
#if AC3ENC_FLOAT
|
ac3enc_fixed: convert to 32-bit sample format
The AC3 encoder used to be a separate library called "Aften", which
got merged into libavcodec (literally, SVN commits and all).
The merge preserved as much features from the library as possible.
The code had two versions - a fixed point version and a floating
point version. FFmpeg had floating point DSP code used by other
codecs, the AC3 decoder including, so the floating-point DSP was
simply replaced with FFmpeg's own functions.
However, FFmpeg had no fixed-point audio code at that point. So
the encoder brought along its own fixed-point DSP functions,
including a fixed-point MDCT.
The fixed-point MDCT itself is trivially just a float MDCT with a
different type and each multiply being a fixed-point multiply.
So over time, it got refactored, and the FFT used for all other codecs
was templated.
Due to design decisions at the time, the fixed-point version of the
encoder operates at 16-bits of precision. Although convenient, this,
even at the time, was inadequate and inefficient. The encoder is noisy,
does not produce output comparable to the float encoder, and even
rings at higher frequencies due to the badly approximated winow function.
Enter MIPS (owned by Imagination Technologies at the time). They wanted
quick fixed-point decoding on their FPUless cores. So they contributed
patches to template the AC3 decoder so it had both a fixed-point
and a floating-point version. They also did the same for the AAC decoder.
They however, used 32-bit samples. Not 16-bits. And we did not have
32-bit fixed-point DSP functions, including an MDCT. But instead of
templating our MDCT to output 3 versions (float, 32-bit fixed and 16-bit fixed),
they simply copy-pasted their own MDCT into ours, and completely
ifdeffed our own MDCT code out if a 32-bit fixed point MDCT was selected.
This is also the status quo nowadays - 2 separate MDCTs, one which
produces floating point and 16-bit fixed point versions, and one
sort-of integrated which produces 32-bit MDCT.
MIPS weren't all that interested in encoding, so they left the encoder
as-is, and they didn't care much about the ifdeffery, mess or quality - it's
not their problem.
So the MDCT/FFT code has always been a thorn in anyone looking to clean up
code's eye.
Backstory over. Internally AC3 operates on 25-bit fixed-point coefficients.
So for the floating point version, the encoder simply runs the float MDCT,
and converts the resulting coefficients to 25-bit fixed-point, as AC3 is inherently
a fixed-point codec. For the fixed-point version, the input is 16-bit samples,
so to maximize precision the frame samples are analyzed and the highest set
bit is detected via ac3_max_msb_abs_int16(), and the coefficients are then
scaled up via ac3_lshift_int16(), so the input for the FFT is always at least 14 bits,
computed in normalize_samples(). After FFT, the coefficients are scaled up to 25 bits.
This patch simply changes the encoder to accept 32-bit samples, reusing
the already well-optimized 32-bit MDCT code, allowing us to clean up and drop
a large part of a very messy code of ours, as well as prepare for the future lavu/tx
conversion. The coefficients are simply scaled down to 25 bits during windowing,
skipping 2 separate scalings, as the hacks to extend precision are simply no longer
necessary. There's no point in running the MDCT always at 32 bits when you're
going to drop 6 bits off anyway, the headroom is plenty, and the MDCT rounds
properly.
This also makes the encoder even slightly more accurate over the float version,
as there's no coefficient conversion step necessary.
SIZE SAVINGS:
ARM32:
HARDCODED TABLES:
BASE - 10709590
DROP DSP - 10702872 - diff: -6.56KiB
DROP MDCT - 10667932 - diff: -34.12KiB - both: -40.68KiB
DROP FFT - 10336652 - diff: -323.52KiB - all: -364.20KiB
SOFTCODED TABLES:
BASE - 9685096
DROP DSP - 9678378 - diff: -6.56KiB
DROP MDCT - 9643466 - diff: -34.09KiB - both: -40.65KiB
DROP FFT - 9573918 - diff: -67.92KiB - all: -108.57KiB
ARM64:
HARDCODED TABLES:
BASE - 14641112
DROP DSP - 14633806 - diff: -7.13KiB
DROP MDCT - 14604812 - diff: -28.31KiB - both: -35.45KiB
DROP FFT - 14286826 - diff: -310.53KiB - all: -345.98KiB
SOFTCODED TABLES:
BASE - 13636238
DROP DSP - 13628932 - diff: -7.13KiB
DROP MDCT - 13599866 - diff: -28.38KiB - both: -35.52KiB
DROP FFT - 13542080 - diff: -56.43KiB - all: -91.95KiB
x86:
HARDCODED TABLES:
BASE - 12367336
DROP DSP - 12354698 - diff: -12.34KiB
DROP MDCT - 12331024 - diff: -23.12KiB - both: -35.46KiB
DROP FFT - 12029788 - diff: -294.18KiB - all: -329.64KiB
SOFTCODED TABLES:
BASE - 11358094
DROP DSP - 11345456 - diff: -12.34KiB
DROP MDCT - 11321742 - diff: -23.16KiB - both: -35.50KiB
DROP FFT - 11276946 - diff: -43.75KiB - all: -79.25KiB
PERFORMANCE (10min random s32le):
ARM32 - before - 39.9x - 0m15.046s
ARM32 - after - 28.2x - 0m21.525s
Speed: -30%
ARM64 - before - 36.1x - 0m16.637s
ARM64 - after - 36.0x - 0m16.727s
Speed: -0.5%
x86 - before - 184x - 0m3.277s
x86 - after - 190x - 0m3.187s
Speed: +3%
2021-01-09 00:51:52 +00:00
|
|
|
#include "libavutil/float_dsp.h"
|
2011-06-10 18:57:19 +00:00
|
|
|
#define AC3_NAME(x) ff_ac3_float_ ## x
|
2011-06-07 16:47:09 +00:00
|
|
|
#define MAC_COEF(d,a,b) ((d)+=(a)*(b))
|
2011-06-27 18:29:33 +00:00
|
|
|
#define COEF_MIN (-16777215.0/16777216.0)
|
|
|
|
#define COEF_MAX ( 16777215.0/16777216.0)
|
2011-08-07 21:47:42 +00:00
|
|
|
#define NEW_CPL_COORD_THRESHOLD 0.03
|
2011-06-07 16:47:09 +00:00
|
|
|
typedef float SampleType;
|
|
|
|
typedef float CoefType;
|
|
|
|
typedef float CoefSumType;
|
|
|
|
#else
|
ac3enc_fixed: convert to 32-bit sample format
The AC3 encoder used to be a separate library called "Aften", which
got merged into libavcodec (literally, SVN commits and all).
The merge preserved as much features from the library as possible.
The code had two versions - a fixed point version and a floating
point version. FFmpeg had floating point DSP code used by other
codecs, the AC3 decoder including, so the floating-point DSP was
simply replaced with FFmpeg's own functions.
However, FFmpeg had no fixed-point audio code at that point. So
the encoder brought along its own fixed-point DSP functions,
including a fixed-point MDCT.
The fixed-point MDCT itself is trivially just a float MDCT with a
different type and each multiply being a fixed-point multiply.
So over time, it got refactored, and the FFT used for all other codecs
was templated.
Due to design decisions at the time, the fixed-point version of the
encoder operates at 16-bits of precision. Although convenient, this,
even at the time, was inadequate and inefficient. The encoder is noisy,
does not produce output comparable to the float encoder, and even
rings at higher frequencies due to the badly approximated winow function.
Enter MIPS (owned by Imagination Technologies at the time). They wanted
quick fixed-point decoding on their FPUless cores. So they contributed
patches to template the AC3 decoder so it had both a fixed-point
and a floating-point version. They also did the same for the AAC decoder.
They however, used 32-bit samples. Not 16-bits. And we did not have
32-bit fixed-point DSP functions, including an MDCT. But instead of
templating our MDCT to output 3 versions (float, 32-bit fixed and 16-bit fixed),
they simply copy-pasted their own MDCT into ours, and completely
ifdeffed our own MDCT code out if a 32-bit fixed point MDCT was selected.
This is also the status quo nowadays - 2 separate MDCTs, one which
produces floating point and 16-bit fixed point versions, and one
sort-of integrated which produces 32-bit MDCT.
MIPS weren't all that interested in encoding, so they left the encoder
as-is, and they didn't care much about the ifdeffery, mess or quality - it's
not their problem.
So the MDCT/FFT code has always been a thorn in anyone looking to clean up
code's eye.
Backstory over. Internally AC3 operates on 25-bit fixed-point coefficients.
So for the floating point version, the encoder simply runs the float MDCT,
and converts the resulting coefficients to 25-bit fixed-point, as AC3 is inherently
a fixed-point codec. For the fixed-point version, the input is 16-bit samples,
so to maximize precision the frame samples are analyzed and the highest set
bit is detected via ac3_max_msb_abs_int16(), and the coefficients are then
scaled up via ac3_lshift_int16(), so the input for the FFT is always at least 14 bits,
computed in normalize_samples(). After FFT, the coefficients are scaled up to 25 bits.
This patch simply changes the encoder to accept 32-bit samples, reusing
the already well-optimized 32-bit MDCT code, allowing us to clean up and drop
a large part of a very messy code of ours, as well as prepare for the future lavu/tx
conversion. The coefficients are simply scaled down to 25 bits during windowing,
skipping 2 separate scalings, as the hacks to extend precision are simply no longer
necessary. There's no point in running the MDCT always at 32 bits when you're
going to drop 6 bits off anyway, the headroom is plenty, and the MDCT rounds
properly.
This also makes the encoder even slightly more accurate over the float version,
as there's no coefficient conversion step necessary.
SIZE SAVINGS:
ARM32:
HARDCODED TABLES:
BASE - 10709590
DROP DSP - 10702872 - diff: -6.56KiB
DROP MDCT - 10667932 - diff: -34.12KiB - both: -40.68KiB
DROP FFT - 10336652 - diff: -323.52KiB - all: -364.20KiB
SOFTCODED TABLES:
BASE - 9685096
DROP DSP - 9678378 - diff: -6.56KiB
DROP MDCT - 9643466 - diff: -34.09KiB - both: -40.65KiB
DROP FFT - 9573918 - diff: -67.92KiB - all: -108.57KiB
ARM64:
HARDCODED TABLES:
BASE - 14641112
DROP DSP - 14633806 - diff: -7.13KiB
DROP MDCT - 14604812 - diff: -28.31KiB - both: -35.45KiB
DROP FFT - 14286826 - diff: -310.53KiB - all: -345.98KiB
SOFTCODED TABLES:
BASE - 13636238
DROP DSP - 13628932 - diff: -7.13KiB
DROP MDCT - 13599866 - diff: -28.38KiB - both: -35.52KiB
DROP FFT - 13542080 - diff: -56.43KiB - all: -91.95KiB
x86:
HARDCODED TABLES:
BASE - 12367336
DROP DSP - 12354698 - diff: -12.34KiB
DROP MDCT - 12331024 - diff: -23.12KiB - both: -35.46KiB
DROP FFT - 12029788 - diff: -294.18KiB - all: -329.64KiB
SOFTCODED TABLES:
BASE - 11358094
DROP DSP - 11345456 - diff: -12.34KiB
DROP MDCT - 11321742 - diff: -23.16KiB - both: -35.50KiB
DROP FFT - 11276946 - diff: -43.75KiB - all: -79.25KiB
PERFORMANCE (10min random s32le):
ARM32 - before - 39.9x - 0m15.046s
ARM32 - after - 28.2x - 0m21.525s
Speed: -30%
ARM64 - before - 36.1x - 0m16.637s
ARM64 - after - 36.0x - 0m16.727s
Speed: -0.5%
x86 - before - 184x - 0m3.277s
x86 - after - 190x - 0m3.187s
Speed: +3%
2021-01-09 00:51:52 +00:00
|
|
|
#include "libavutil/fixed_dsp.h"
|
2011-06-10 18:57:19 +00:00
|
|
|
#define AC3_NAME(x) ff_ac3_fixed_ ## x
|
2011-06-07 16:47:09 +00:00
|
|
|
#define MAC_COEF(d,a,b) MAC64(d,a,b)
|
2011-06-27 18:29:33 +00:00
|
|
|
#define COEF_MIN -16777215
|
|
|
|
#define COEF_MAX 16777215
|
2011-08-07 21:47:42 +00:00
|
|
|
#define NEW_CPL_COORD_THRESHOLD 503317
|
ac3enc_fixed: convert to 32-bit sample format
The AC3 encoder used to be a separate library called "Aften", which
got merged into libavcodec (literally, SVN commits and all).
The merge preserved as much features from the library as possible.
The code had two versions - a fixed point version and a floating
point version. FFmpeg had floating point DSP code used by other
codecs, the AC3 decoder including, so the floating-point DSP was
simply replaced with FFmpeg's own functions.
However, FFmpeg had no fixed-point audio code at that point. So
the encoder brought along its own fixed-point DSP functions,
including a fixed-point MDCT.
The fixed-point MDCT itself is trivially just a float MDCT with a
different type and each multiply being a fixed-point multiply.
So over time, it got refactored, and the FFT used for all other codecs
was templated.
Due to design decisions at the time, the fixed-point version of the
encoder operates at 16-bits of precision. Although convenient, this,
even at the time, was inadequate and inefficient. The encoder is noisy,
does not produce output comparable to the float encoder, and even
rings at higher frequencies due to the badly approximated winow function.
Enter MIPS (owned by Imagination Technologies at the time). They wanted
quick fixed-point decoding on their FPUless cores. So they contributed
patches to template the AC3 decoder so it had both a fixed-point
and a floating-point version. They also did the same for the AAC decoder.
They however, used 32-bit samples. Not 16-bits. And we did not have
32-bit fixed-point DSP functions, including an MDCT. But instead of
templating our MDCT to output 3 versions (float, 32-bit fixed and 16-bit fixed),
they simply copy-pasted their own MDCT into ours, and completely
ifdeffed our own MDCT code out if a 32-bit fixed point MDCT was selected.
This is also the status quo nowadays - 2 separate MDCTs, one which
produces floating point and 16-bit fixed point versions, and one
sort-of integrated which produces 32-bit MDCT.
MIPS weren't all that interested in encoding, so they left the encoder
as-is, and they didn't care much about the ifdeffery, mess or quality - it's
not their problem.
So the MDCT/FFT code has always been a thorn in anyone looking to clean up
code's eye.
Backstory over. Internally AC3 operates on 25-bit fixed-point coefficients.
So for the floating point version, the encoder simply runs the float MDCT,
and converts the resulting coefficients to 25-bit fixed-point, as AC3 is inherently
a fixed-point codec. For the fixed-point version, the input is 16-bit samples,
so to maximize precision the frame samples are analyzed and the highest set
bit is detected via ac3_max_msb_abs_int16(), and the coefficients are then
scaled up via ac3_lshift_int16(), so the input for the FFT is always at least 14 bits,
computed in normalize_samples(). After FFT, the coefficients are scaled up to 25 bits.
This patch simply changes the encoder to accept 32-bit samples, reusing
the already well-optimized 32-bit MDCT code, allowing us to clean up and drop
a large part of a very messy code of ours, as well as prepare for the future lavu/tx
conversion. The coefficients are simply scaled down to 25 bits during windowing,
skipping 2 separate scalings, as the hacks to extend precision are simply no longer
necessary. There's no point in running the MDCT always at 32 bits when you're
going to drop 6 bits off anyway, the headroom is plenty, and the MDCT rounds
properly.
This also makes the encoder even slightly more accurate over the float version,
as there's no coefficient conversion step necessary.
SIZE SAVINGS:
ARM32:
HARDCODED TABLES:
BASE - 10709590
DROP DSP - 10702872 - diff: -6.56KiB
DROP MDCT - 10667932 - diff: -34.12KiB - both: -40.68KiB
DROP FFT - 10336652 - diff: -323.52KiB - all: -364.20KiB
SOFTCODED TABLES:
BASE - 9685096
DROP DSP - 9678378 - diff: -6.56KiB
DROP MDCT - 9643466 - diff: -34.09KiB - both: -40.65KiB
DROP FFT - 9573918 - diff: -67.92KiB - all: -108.57KiB
ARM64:
HARDCODED TABLES:
BASE - 14641112
DROP DSP - 14633806 - diff: -7.13KiB
DROP MDCT - 14604812 - diff: -28.31KiB - both: -35.45KiB
DROP FFT - 14286826 - diff: -310.53KiB - all: -345.98KiB
SOFTCODED TABLES:
BASE - 13636238
DROP DSP - 13628932 - diff: -7.13KiB
DROP MDCT - 13599866 - diff: -28.38KiB - both: -35.52KiB
DROP FFT - 13542080 - diff: -56.43KiB - all: -91.95KiB
x86:
HARDCODED TABLES:
BASE - 12367336
DROP DSP - 12354698 - diff: -12.34KiB
DROP MDCT - 12331024 - diff: -23.12KiB - both: -35.46KiB
DROP FFT - 12029788 - diff: -294.18KiB - all: -329.64KiB
SOFTCODED TABLES:
BASE - 11358094
DROP DSP - 11345456 - diff: -12.34KiB
DROP MDCT - 11321742 - diff: -23.16KiB - both: -35.50KiB
DROP FFT - 11276946 - diff: -43.75KiB - all: -79.25KiB
PERFORMANCE (10min random s32le):
ARM32 - before - 39.9x - 0m15.046s
ARM32 - after - 28.2x - 0m21.525s
Speed: -30%
ARM64 - before - 36.1x - 0m16.637s
ARM64 - after - 36.0x - 0m16.727s
Speed: -0.5%
x86 - before - 184x - 0m3.277s
x86 - after - 190x - 0m3.187s
Speed: +3%
2021-01-09 00:51:52 +00:00
|
|
|
typedef int32_t SampleType;
|
2011-06-07 16:47:09 +00:00
|
|
|
typedef int32_t CoefType;
|
|
|
|
typedef int64_t CoefSumType;
|
|
|
|
#endif
|
|
|
|
|
2011-08-09 22:11:08 +00:00
|
|
|
/* common option values */
|
|
|
|
#define AC3ENC_OPT_NONE -1
|
|
|
|
#define AC3ENC_OPT_AUTO -1
|
|
|
|
#define AC3ENC_OPT_OFF 0
|
|
|
|
#define AC3ENC_OPT_ON 1
|
|
|
|
#define AC3ENC_OPT_NOT_INDICATED 0
|
2011-10-11 18:57:58 +00:00
|
|
|
#define AC3ENC_OPT_MODE_ON 2
|
|
|
|
#define AC3ENC_OPT_MODE_OFF 1
|
2014-02-18 02:39:52 +00:00
|
|
|
#define AC3ENC_OPT_DSUREX_DPLIIZ 3
|
2011-08-09 22:11:08 +00:00
|
|
|
|
|
|
|
/* specific option values */
|
|
|
|
#define AC3ENC_OPT_LARGE_ROOM 1
|
|
|
|
#define AC3ENC_OPT_SMALL_ROOM 2
|
|
|
|
#define AC3ENC_OPT_DOWNMIX_LTRT 1
|
|
|
|
#define AC3ENC_OPT_DOWNMIX_LORO 2
|
2014-02-18 02:39:51 +00:00
|
|
|
#define AC3ENC_OPT_DOWNMIX_DPLII 3 // reserved value in A/52, but used by encoders to indicate DPL2
|
2011-08-09 22:11:08 +00:00
|
|
|
#define AC3ENC_OPT_ADCONV_STANDARD 0
|
|
|
|
#define AC3ENC_OPT_ADCONV_HDCD 1
|
|
|
|
|
2011-06-10 18:57:19 +00:00
|
|
|
|
2011-06-07 16:47:09 +00:00
|
|
|
/**
|
|
|
|
* Encoding Options used by AVOption.
|
|
|
|
*/
|
|
|
|
typedef struct AC3EncOptions {
|
|
|
|
/* AC-3 metadata options*/
|
|
|
|
int dialogue_level;
|
|
|
|
int bitstream_mode;
|
|
|
|
float center_mix_level;
|
|
|
|
float surround_mix_level;
|
|
|
|
int dolby_surround_mode;
|
|
|
|
int audio_production_info;
|
|
|
|
int mixing_level;
|
|
|
|
int room_type;
|
|
|
|
int copyright;
|
|
|
|
int original;
|
|
|
|
int extended_bsi_1;
|
|
|
|
int preferred_stereo_downmix;
|
|
|
|
float ltrt_center_mix_level;
|
|
|
|
float ltrt_surround_mix_level;
|
|
|
|
float loro_center_mix_level;
|
|
|
|
float loro_surround_mix_level;
|
|
|
|
int extended_bsi_2;
|
|
|
|
int dolby_surround_ex_mode;
|
|
|
|
int dolby_headphone_mode;
|
|
|
|
int ad_converter_type;
|
2011-05-29 23:16:46 +00:00
|
|
|
int eac3_mixing_metadata;
|
|
|
|
int eac3_info_metadata;
|
2011-06-07 16:47:09 +00:00
|
|
|
|
|
|
|
/* other encoding options */
|
|
|
|
int allow_per_frame_metadata;
|
|
|
|
int stereo_rematrixing;
|
|
|
|
int channel_coupling;
|
|
|
|
int cpl_start;
|
|
|
|
} AC3EncOptions;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data for a single audio block.
|
|
|
|
*/
|
|
|
|
typedef struct AC3Block {
|
|
|
|
CoefType **mdct_coef; ///< MDCT coefficients
|
|
|
|
int32_t **fixed_coef; ///< fixed-point MDCT coefficients
|
|
|
|
uint8_t **exp; ///< original exponents
|
|
|
|
uint8_t **grouped_exp; ///< grouped exponents
|
|
|
|
int16_t **psd; ///< psd per frequency bin
|
|
|
|
int16_t **band_psd; ///< psd per critical band
|
|
|
|
int16_t **mask; ///< masking curve
|
|
|
|
uint16_t **qmant; ///< quantized mantissas
|
|
|
|
uint8_t **cpl_coord_exp; ///< coupling coord exponents (cplcoexp)
|
|
|
|
uint8_t **cpl_coord_mant; ///< coupling coord mantissas (cplcomant)
|
|
|
|
uint8_t new_rematrixing_strategy; ///< send new rematrixing flags in this block
|
|
|
|
int num_rematrixing_bands; ///< number of rematrixing bands
|
|
|
|
uint8_t rematrixing_flags[4]; ///< rematrixing flags
|
|
|
|
int new_cpl_strategy; ///< send new coupling strategy
|
|
|
|
int cpl_in_use; ///< coupling in use for this block (cplinu)
|
|
|
|
uint8_t channel_in_cpl[AC3_MAX_CHANNELS]; ///< channel in coupling (chincpl)
|
|
|
|
int num_cpl_channels; ///< number of channels in coupling
|
2011-08-05 20:28:39 +00:00
|
|
|
uint8_t new_cpl_coords[AC3_MAX_CHANNELS]; ///< send new coupling coordinates (cplcoe)
|
2011-06-07 16:47:09 +00:00
|
|
|
uint8_t cpl_master_exp[AC3_MAX_CHANNELS]; ///< coupling coord master exponents (mstrcplco)
|
|
|
|
int new_snr_offsets; ///< send new SNR offsets
|
|
|
|
int new_cpl_leak; ///< send new coupling leak info
|
|
|
|
int end_freq[AC3_MAX_CHANNELS]; ///< end frequency bin (endmant)
|
|
|
|
} AC3Block;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AC-3 encoder private context.
|
|
|
|
*/
|
|
|
|
typedef struct AC3EncodeContext {
|
|
|
|
AVClass *av_class; ///< AVClass used for AVOption
|
|
|
|
AC3EncOptions options; ///< encoding options
|
2011-06-13 21:44:50 +00:00
|
|
|
AVCodecContext *avctx; ///< parent AVCodecContext
|
2011-06-07 16:47:09 +00:00
|
|
|
PutBitContext pb; ///< bitstream writer context
|
2014-01-16 16:30:19 +00:00
|
|
|
AudioDSPContext adsp;
|
ac3enc_fixed: convert to 32-bit sample format
The AC3 encoder used to be a separate library called "Aften", which
got merged into libavcodec (literally, SVN commits and all).
The merge preserved as much features from the library as possible.
The code had two versions - a fixed point version and a floating
point version. FFmpeg had floating point DSP code used by other
codecs, the AC3 decoder including, so the floating-point DSP was
simply replaced with FFmpeg's own functions.
However, FFmpeg had no fixed-point audio code at that point. So
the encoder brought along its own fixed-point DSP functions,
including a fixed-point MDCT.
The fixed-point MDCT itself is trivially just a float MDCT with a
different type and each multiply being a fixed-point multiply.
So over time, it got refactored, and the FFT used for all other codecs
was templated.
Due to design decisions at the time, the fixed-point version of the
encoder operates at 16-bits of precision. Although convenient, this,
even at the time, was inadequate and inefficient. The encoder is noisy,
does not produce output comparable to the float encoder, and even
rings at higher frequencies due to the badly approximated winow function.
Enter MIPS (owned by Imagination Technologies at the time). They wanted
quick fixed-point decoding on their FPUless cores. So they contributed
patches to template the AC3 decoder so it had both a fixed-point
and a floating-point version. They also did the same for the AAC decoder.
They however, used 32-bit samples. Not 16-bits. And we did not have
32-bit fixed-point DSP functions, including an MDCT. But instead of
templating our MDCT to output 3 versions (float, 32-bit fixed and 16-bit fixed),
they simply copy-pasted their own MDCT into ours, and completely
ifdeffed our own MDCT code out if a 32-bit fixed point MDCT was selected.
This is also the status quo nowadays - 2 separate MDCTs, one which
produces floating point and 16-bit fixed point versions, and one
sort-of integrated which produces 32-bit MDCT.
MIPS weren't all that interested in encoding, so they left the encoder
as-is, and they didn't care much about the ifdeffery, mess or quality - it's
not their problem.
So the MDCT/FFT code has always been a thorn in anyone looking to clean up
code's eye.
Backstory over. Internally AC3 operates on 25-bit fixed-point coefficients.
So for the floating point version, the encoder simply runs the float MDCT,
and converts the resulting coefficients to 25-bit fixed-point, as AC3 is inherently
a fixed-point codec. For the fixed-point version, the input is 16-bit samples,
so to maximize precision the frame samples are analyzed and the highest set
bit is detected via ac3_max_msb_abs_int16(), and the coefficients are then
scaled up via ac3_lshift_int16(), so the input for the FFT is always at least 14 bits,
computed in normalize_samples(). After FFT, the coefficients are scaled up to 25 bits.
This patch simply changes the encoder to accept 32-bit samples, reusing
the already well-optimized 32-bit MDCT code, allowing us to clean up and drop
a large part of a very messy code of ours, as well as prepare for the future lavu/tx
conversion. The coefficients are simply scaled down to 25 bits during windowing,
skipping 2 separate scalings, as the hacks to extend precision are simply no longer
necessary. There's no point in running the MDCT always at 32 bits when you're
going to drop 6 bits off anyway, the headroom is plenty, and the MDCT rounds
properly.
This also makes the encoder even slightly more accurate over the float version,
as there's no coefficient conversion step necessary.
SIZE SAVINGS:
ARM32:
HARDCODED TABLES:
BASE - 10709590
DROP DSP - 10702872 - diff: -6.56KiB
DROP MDCT - 10667932 - diff: -34.12KiB - both: -40.68KiB
DROP FFT - 10336652 - diff: -323.52KiB - all: -364.20KiB
SOFTCODED TABLES:
BASE - 9685096
DROP DSP - 9678378 - diff: -6.56KiB
DROP MDCT - 9643466 - diff: -34.09KiB - both: -40.65KiB
DROP FFT - 9573918 - diff: -67.92KiB - all: -108.57KiB
ARM64:
HARDCODED TABLES:
BASE - 14641112
DROP DSP - 14633806 - diff: -7.13KiB
DROP MDCT - 14604812 - diff: -28.31KiB - both: -35.45KiB
DROP FFT - 14286826 - diff: -310.53KiB - all: -345.98KiB
SOFTCODED TABLES:
BASE - 13636238
DROP DSP - 13628932 - diff: -7.13KiB
DROP MDCT - 13599866 - diff: -28.38KiB - both: -35.52KiB
DROP FFT - 13542080 - diff: -56.43KiB - all: -91.95KiB
x86:
HARDCODED TABLES:
BASE - 12367336
DROP DSP - 12354698 - diff: -12.34KiB
DROP MDCT - 12331024 - diff: -23.12KiB - both: -35.46KiB
DROP FFT - 12029788 - diff: -294.18KiB - all: -329.64KiB
SOFTCODED TABLES:
BASE - 11358094
DROP DSP - 11345456 - diff: -12.34KiB
DROP MDCT - 11321742 - diff: -23.16KiB - both: -35.50KiB
DROP FFT - 11276946 - diff: -43.75KiB - all: -79.25KiB
PERFORMANCE (10min random s32le):
ARM32 - before - 39.9x - 0m15.046s
ARM32 - after - 28.2x - 0m21.525s
Speed: -30%
ARM64 - before - 36.1x - 0m16.637s
ARM64 - after - 36.0x - 0m16.727s
Speed: -0.5%
x86 - before - 184x - 0m3.277s
x86 - after - 190x - 0m3.187s
Speed: +3%
2021-01-09 00:51:52 +00:00
|
|
|
#if AC3ENC_FLOAT
|
2014-11-29 17:54:27 +00:00
|
|
|
AVFloatDSPContext *fdsp;
|
ac3enc_fixed: convert to 32-bit sample format
The AC3 encoder used to be a separate library called "Aften", which
got merged into libavcodec (literally, SVN commits and all).
The merge preserved as much features from the library as possible.
The code had two versions - a fixed point version and a floating
point version. FFmpeg had floating point DSP code used by other
codecs, the AC3 decoder including, so the floating-point DSP was
simply replaced with FFmpeg's own functions.
However, FFmpeg had no fixed-point audio code at that point. So
the encoder brought along its own fixed-point DSP functions,
including a fixed-point MDCT.
The fixed-point MDCT itself is trivially just a float MDCT with a
different type and each multiply being a fixed-point multiply.
So over time, it got refactored, and the FFT used for all other codecs
was templated.
Due to design decisions at the time, the fixed-point version of the
encoder operates at 16-bits of precision. Although convenient, this,
even at the time, was inadequate and inefficient. The encoder is noisy,
does not produce output comparable to the float encoder, and even
rings at higher frequencies due to the badly approximated winow function.
Enter MIPS (owned by Imagination Technologies at the time). They wanted
quick fixed-point decoding on their FPUless cores. So they contributed
patches to template the AC3 decoder so it had both a fixed-point
and a floating-point version. They also did the same for the AAC decoder.
They however, used 32-bit samples. Not 16-bits. And we did not have
32-bit fixed-point DSP functions, including an MDCT. But instead of
templating our MDCT to output 3 versions (float, 32-bit fixed and 16-bit fixed),
they simply copy-pasted their own MDCT into ours, and completely
ifdeffed our own MDCT code out if a 32-bit fixed point MDCT was selected.
This is also the status quo nowadays - 2 separate MDCTs, one which
produces floating point and 16-bit fixed point versions, and one
sort-of integrated which produces 32-bit MDCT.
MIPS weren't all that interested in encoding, so they left the encoder
as-is, and they didn't care much about the ifdeffery, mess or quality - it's
not their problem.
So the MDCT/FFT code has always been a thorn in anyone looking to clean up
code's eye.
Backstory over. Internally AC3 operates on 25-bit fixed-point coefficients.
So for the floating point version, the encoder simply runs the float MDCT,
and converts the resulting coefficients to 25-bit fixed-point, as AC3 is inherently
a fixed-point codec. For the fixed-point version, the input is 16-bit samples,
so to maximize precision the frame samples are analyzed and the highest set
bit is detected via ac3_max_msb_abs_int16(), and the coefficients are then
scaled up via ac3_lshift_int16(), so the input for the FFT is always at least 14 bits,
computed in normalize_samples(). After FFT, the coefficients are scaled up to 25 bits.
This patch simply changes the encoder to accept 32-bit samples, reusing
the already well-optimized 32-bit MDCT code, allowing us to clean up and drop
a large part of a very messy code of ours, as well as prepare for the future lavu/tx
conversion. The coefficients are simply scaled down to 25 bits during windowing,
skipping 2 separate scalings, as the hacks to extend precision are simply no longer
necessary. There's no point in running the MDCT always at 32 bits when you're
going to drop 6 bits off anyway, the headroom is plenty, and the MDCT rounds
properly.
This also makes the encoder even slightly more accurate over the float version,
as there's no coefficient conversion step necessary.
SIZE SAVINGS:
ARM32:
HARDCODED TABLES:
BASE - 10709590
DROP DSP - 10702872 - diff: -6.56KiB
DROP MDCT - 10667932 - diff: -34.12KiB - both: -40.68KiB
DROP FFT - 10336652 - diff: -323.52KiB - all: -364.20KiB
SOFTCODED TABLES:
BASE - 9685096
DROP DSP - 9678378 - diff: -6.56KiB
DROP MDCT - 9643466 - diff: -34.09KiB - both: -40.65KiB
DROP FFT - 9573918 - diff: -67.92KiB - all: -108.57KiB
ARM64:
HARDCODED TABLES:
BASE - 14641112
DROP DSP - 14633806 - diff: -7.13KiB
DROP MDCT - 14604812 - diff: -28.31KiB - both: -35.45KiB
DROP FFT - 14286826 - diff: -310.53KiB - all: -345.98KiB
SOFTCODED TABLES:
BASE - 13636238
DROP DSP - 13628932 - diff: -7.13KiB
DROP MDCT - 13599866 - diff: -28.38KiB - both: -35.52KiB
DROP FFT - 13542080 - diff: -56.43KiB - all: -91.95KiB
x86:
HARDCODED TABLES:
BASE - 12367336
DROP DSP - 12354698 - diff: -12.34KiB
DROP MDCT - 12331024 - diff: -23.12KiB - both: -35.46KiB
DROP FFT - 12029788 - diff: -294.18KiB - all: -329.64KiB
SOFTCODED TABLES:
BASE - 11358094
DROP DSP - 11345456 - diff: -12.34KiB
DROP MDCT - 11321742 - diff: -23.16KiB - both: -35.50KiB
DROP FFT - 11276946 - diff: -43.75KiB - all: -79.25KiB
PERFORMANCE (10min random s32le):
ARM32 - before - 39.9x - 0m15.046s
ARM32 - after - 28.2x - 0m21.525s
Speed: -30%
ARM64 - before - 36.1x - 0m16.637s
ARM64 - after - 36.0x - 0m16.727s
Speed: -0.5%
x86 - before - 184x - 0m3.277s
x86 - after - 190x - 0m3.187s
Speed: +3%
2021-01-09 00:51:52 +00:00
|
|
|
#else
|
|
|
|
AVFixedDSPContext *fdsp;
|
|
|
|
#endif
|
2014-02-08 01:59:58 +00:00
|
|
|
MECmpContext mecc;
|
2011-06-07 16:47:09 +00:00
|
|
|
AC3DSPContext ac3dsp; ///< AC-3 optimized functions
|
2011-07-13 19:12:11 +00:00
|
|
|
FFTContext mdct; ///< FFT context for MDCT calculation
|
|
|
|
const SampleType *mdct_window; ///< MDCT window function array
|
2011-06-07 16:47:09 +00:00
|
|
|
|
|
|
|
AC3Block blocks[AC3_MAX_BLOCKS]; ///< per-block info
|
|
|
|
|
2011-06-10 18:57:19 +00:00
|
|
|
int fixed_point; ///< indicates if fixed-point encoder is being used
|
2011-06-07 16:47:09 +00:00
|
|
|
int eac3; ///< indicates if this is E-AC-3 vs. AC-3
|
|
|
|
int bitstream_id; ///< bitstream id (bsid)
|
|
|
|
int bitstream_mode; ///< bitstream mode (bsmod)
|
|
|
|
|
|
|
|
int bit_rate; ///< target bit rate, in bits-per-second
|
|
|
|
int sample_rate; ///< sampling frequency, in Hz
|
|
|
|
|
2011-07-14 17:02:45 +00:00
|
|
|
int num_blks_code; ///< number of blocks code (numblkscod)
|
|
|
|
int num_blocks; ///< number of blocks per frame
|
2011-06-07 16:47:09 +00:00
|
|
|
int frame_size_min; ///< minimum frame size in case rounding is necessary
|
|
|
|
int frame_size; ///< current frame size in bytes
|
|
|
|
int frame_size_code; ///< frame size code (frmsizecod)
|
|
|
|
uint16_t crc_inv[2];
|
|
|
|
int64_t bits_written; ///< bit count (used to avg. bitrate)
|
|
|
|
int64_t samples_written; ///< sample count (used to avg. bitrate)
|
|
|
|
|
|
|
|
int fbw_channels; ///< number of full-bandwidth channels (nfchans)
|
|
|
|
int channels; ///< total number of channels (nchans)
|
|
|
|
int lfe_on; ///< indicates if there is an LFE channel (lfeon)
|
|
|
|
int lfe_channel; ///< channel index of the LFE channel
|
|
|
|
int has_center; ///< indicates if there is a center channel
|
|
|
|
int has_surround; ///< indicates if there are one or more surround channels
|
|
|
|
int channel_mode; ///< channel mode (acmod)
|
|
|
|
const uint8_t *channel_map; ///< channel map used to reorder channels
|
|
|
|
|
|
|
|
int center_mix_level; ///< center mix level code
|
|
|
|
int surround_mix_level; ///< surround mix level code
|
|
|
|
int ltrt_center_mix_level; ///< Lt/Rt center mix level code
|
|
|
|
int ltrt_surround_mix_level; ///< Lt/Rt surround mix level code
|
|
|
|
int loro_center_mix_level; ///< Lo/Ro center mix level code
|
|
|
|
int loro_surround_mix_level; ///< Lo/Ro surround mix level code
|
|
|
|
|
|
|
|
int cutoff; ///< user-specified cutoff frequency, in Hz
|
|
|
|
int bandwidth_code; ///< bandwidth code (0 to 60) (chbwcod)
|
|
|
|
int start_freq[AC3_MAX_CHANNELS]; ///< start frequency bin (strtmant)
|
|
|
|
int cpl_end_freq; ///< coupling channel end frequency bin
|
|
|
|
|
|
|
|
int cpl_on; ///< coupling turned on for this frame
|
|
|
|
int cpl_enabled; ///< coupling enabled for all frames
|
|
|
|
int num_cpl_subbands; ///< number of coupling subbands (ncplsubnd)
|
|
|
|
int num_cpl_bands; ///< number of coupling bands (ncplbnd)
|
|
|
|
uint8_t cpl_band_sizes[AC3_MAX_CPL_BANDS]; ///< number of coeffs in each coupling band
|
|
|
|
|
|
|
|
int rematrixing_enabled; ///< stereo rematrixing enabled
|
|
|
|
|
|
|
|
/* bitrate allocation control */
|
|
|
|
int slow_gain_code; ///< slow gain code (sgaincod)
|
|
|
|
int slow_decay_code; ///< slow decay code (sdcycod)
|
|
|
|
int fast_decay_code; ///< fast decay code (fdcycod)
|
|
|
|
int db_per_bit_code; ///< dB/bit code (dbpbcod)
|
|
|
|
int floor_code; ///< floor code (floorcod)
|
|
|
|
AC3BitAllocParameters bit_alloc; ///< bit allocation parameters
|
|
|
|
int coarse_snr_offset; ///< coarse SNR offsets (csnroffst)
|
|
|
|
int fast_gain_code[AC3_MAX_CHANNELS]; ///< fast gain codes (signal-to-mask ratio) (fgaincod)
|
|
|
|
int fine_snr_offset[AC3_MAX_CHANNELS]; ///< fine SNR offsets (fsnroffst)
|
|
|
|
int frame_bits_fixed; ///< number of non-coefficient bits for fixed parameters
|
|
|
|
int frame_bits; ///< all frame bits except exponents and mantissas
|
|
|
|
int exponent_bits; ///< number of bits used for exponents
|
|
|
|
|
2011-06-10 16:42:36 +00:00
|
|
|
SampleType *windowed_samples;
|
2011-06-07 16:47:09 +00:00
|
|
|
SampleType **planar_samples;
|
|
|
|
uint8_t *bap_buffer;
|
|
|
|
uint8_t *bap1_buffer;
|
|
|
|
CoefType *mdct_coef_buffer;
|
|
|
|
int32_t *fixed_coef_buffer;
|
|
|
|
uint8_t *exp_buffer;
|
|
|
|
uint8_t *grouped_exp_buffer;
|
|
|
|
int16_t *psd_buffer;
|
|
|
|
int16_t *band_psd_buffer;
|
|
|
|
int16_t *mask_buffer;
|
2011-06-21 17:39:02 +00:00
|
|
|
int16_t *qmant_buffer;
|
2011-06-07 16:47:09 +00:00
|
|
|
uint8_t *cpl_coord_exp_buffer;
|
|
|
|
uint8_t *cpl_coord_mant_buffer;
|
|
|
|
|
|
|
|
uint8_t exp_strategy[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< exponent strategies
|
2011-07-13 16:20:29 +00:00
|
|
|
uint8_t frame_exp_strategy[AC3_MAX_CHANNELS]; ///< frame exp strategy index
|
|
|
|
int use_frame_exp_strategy; ///< indicates use of frame exp strategy
|
2011-06-07 16:47:09 +00:00
|
|
|
uint8_t exp_ref_block[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< reference blocks for EXP_REUSE
|
|
|
|
uint8_t *ref_bap [AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< bit allocation pointers (bap)
|
|
|
|
int ref_bap_set; ///< indicates if ref_bap pointers have been set
|
|
|
|
|
2020-01-29 15:34:48 +00:00
|
|
|
int warned_alternate_bitstream;
|
|
|
|
|
2011-06-10 18:57:19 +00:00
|
|
|
/* fixed vs. float function pointers */
|
2011-07-13 19:12:11 +00:00
|
|
|
void (*mdct_end)(struct AC3EncodeContext *s);
|
|
|
|
int (*mdct_init)(struct AC3EncodeContext *s);
|
2011-06-10 18:57:19 +00:00
|
|
|
|
|
|
|
/* fixed vs. float templated function pointers */
|
2011-06-13 21:44:50 +00:00
|
|
|
int (*allocate_sample_buffers)(struct AC3EncodeContext *s);
|
2011-06-10 18:57:19 +00:00
|
|
|
|
|
|
|
/* AC-3 vs. E-AC-3 function pointers */
|
2011-06-10 15:45:03 +00:00
|
|
|
void (*output_frame_header)(struct AC3EncodeContext *s);
|
2011-06-07 16:47:09 +00:00
|
|
|
} AC3EncodeContext;
|
|
|
|
|
2011-06-10 18:57:19 +00:00
|
|
|
|
2011-11-25 12:51:57 +00:00
|
|
|
extern const uint64_t ff_ac3_channel_layouts[19];
|
2020-12-03 04:32:18 +00:00
|
|
|
extern const AVOption ff_ac3_enc_options[];
|
|
|
|
extern const AVCodecDefault ff_ac3_enc_defaults[];
|
2011-06-10 18:57:19 +00:00
|
|
|
|
|
|
|
int ff_ac3_encode_init(AVCodecContext *avctx);
|
2014-06-11 12:32:01 +00:00
|
|
|
int ff_ac3_float_encode_init(AVCodecContext *avctx);
|
2011-06-10 18:57:19 +00:00
|
|
|
|
|
|
|
int ff_ac3_encode_close(AVCodecContext *avctx);
|
|
|
|
|
2011-07-13 17:53:58 +00:00
|
|
|
int ff_ac3_validate_metadata(AC3EncodeContext *s);
|
2011-06-27 03:58:19 +00:00
|
|
|
|
|
|
|
void ff_ac3_adjust_frame_size(AC3EncodeContext *s);
|
|
|
|
|
|
|
|
void ff_ac3_compute_coupling_strategy(AC3EncodeContext *s);
|
|
|
|
|
2020-12-03 14:28:35 +00:00
|
|
|
int ff_ac3_encode_frame_common_end(AVCodecContext *avctx, AVPacket *avpkt,
|
|
|
|
const AVFrame *frame, int *got_packet_ptr);
|
2011-06-10 18:57:19 +00:00
|
|
|
|
|
|
|
/* prototypes for functions in ac3enc_template.c */
|
|
|
|
|
2012-01-30 00:01:10 +00:00
|
|
|
int ff_ac3_fixed_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
|
|
|
|
const AVFrame *frame, int *got_packet_ptr);
|
|
|
|
int ff_ac3_float_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
|
|
|
|
const AVFrame *frame, int *got_packet_ptr);
|
2011-06-10 18:57:19 +00:00
|
|
|
|
2011-06-07 16:47:09 +00:00
|
|
|
#endif /* AVCODEC_AC3ENC_H */
|