Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 foo86
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
24 #include "libavutil/common.h"
25 #include "libavutil/float_dsp.h"
26 #include "libavutil/fixed_dsp.h"
27 #include "libavutil/mem_internal.h"
29 #include "avcodec.h"
30 #include "internal.h"
31 #include "get_bits.h"
32 #include "dca.h"
33 #include "dca_exss.h"
34 #include "dcadsp.h"
35 #include "dcadct.h"
36 #include "dcamath.h"
37 #include "dcahuff.h"
38 #include "fft.h"
39 #include "synth_filter.h"
41 #define DCA_CHANNELS 7
42 #define DCA_SUBBANDS 32
43 #define DCA_SUBBANDS_X96 64
44 #define DCA_SUBFRAMES 16
47 #define DCA_LFE_HISTORY 8
48 #define DCA_ABITS_MAX 26
54 #define DCA_EXSS_CHSETS_MAX 4
56 #define DCA_FILTER_MODE_X96 0x01
57 #define DCA_FILTER_MODE_FIXED 0x02
60  DCA_AMODE_MONO, // Mode 0: A (mono)
61  DCA_AMODE_MONO_DUAL, // Mode 1: A + B (dual mono)
62  DCA_AMODE_STEREO, // Mode 2: L + R (stereo)
63  DCA_AMODE_STEREO_SUMDIFF, // Mode 3: (L+R) + (L-R) (sum-diff)
64  DCA_AMODE_STEREO_TOTAL, // Mode 4: LT + RT (left and right total)
65  DCA_AMODE_3F, // Mode 5: C + L + R
66  DCA_AMODE_2F1R, // Mode 6: L + R + S
67  DCA_AMODE_3F1R, // Mode 7: C + L + R + S
68  DCA_AMODE_2F2R, // Mode 8: L + R + SL + SR
69  DCA_AMODE_3F2R, // Mode 9: C + L + R + SL + SR
72 };
78 };
85 };
87 typedef struct DCADSPData {
88  union {
89  struct {
90  DECLARE_ALIGNED(32, float, hist1)[1024];
91  DECLARE_ALIGNED(32, float, hist2)[64];
92  } flt;
93  struct {
96  } fix;
97  } u;
98  int offset;
99 } DCADSPData;
101 typedef struct DCACoreDecoder {
106  // Bit stream header
107  int crc_present; ///< CRC present flag
108  int npcmblocks; ///< Number of PCM sample blocks
109  int frame_size; ///< Primary frame byte size
110  int audio_mode; ///< Audio channel arrangement
111  int sample_rate; ///< Core audio sampling frequency
112  int bit_rate; ///< Transmission bit rate
113  int drc_present; ///< Embedded dynamic range flag
114  int ts_present; ///< Embedded time stamp flag
115  int aux_present; ///< Auxiliary data flag
116  int ext_audio_type; ///< Extension audio descriptor flag
117  int ext_audio_present; ///< Extended coding flag
118  int sync_ssf; ///< Audio sync word insertion flag
119  int lfe_present; ///< Low frequency effects flag
120  int predictor_history; ///< Predictor history flag switch
121  int filter_perfect; ///< Multirate interpolator switch
122  int source_pcm_res; ///< Source PCM resolution
123  int es_format; ///< Extended surround (ES) mastering flag
124  int sumdiff_front; ///< Front sum/difference flag
125  int sumdiff_surround; ///< Surround sum/difference flag
127  // Primary audio coding header
128  int nsubframes; ///< Number of subframes
129  int nchannels; ///< Number of primary audio channels (incl. extension channels)
130  int ch_mask; ///< Speaker layout mask (incl. LFE and extension channels)
131  int8_t nsubbands[DCA_CHANNELS]; ///< Subband activity count
132  int8_t subband_vq_start[DCA_CHANNELS]; ///< High frequency VQ start subband
133  int8_t joint_intensity_index[DCA_CHANNELS]; ///< Joint intensity coding index
134  int8_t transition_mode_sel[DCA_CHANNELS]; ///< Transient mode code book
135  int8_t scale_factor_sel[DCA_CHANNELS]; ///< Scale factor code book
136  int8_t bit_allocation_sel[DCA_CHANNELS]; ///< Bit allocation quantizer select
137  int8_t quant_index_sel[DCA_CHANNELS][DCA_CODE_BOOKS]; ///< Quantization index codebook select
138  int32_t scale_factor_adj[DCA_CHANNELS][DCA_CODE_BOOKS]; ///< Scale factor adjustment
140  // Primary audio coding side information
141  int8_t nsubsubframes[DCA_SUBFRAMES]; ///< Subsubframe count for each subframe
142  int8_t prediction_mode[DCA_CHANNELS][DCA_SUBBANDS_X96]; ///< Prediction mode
143  int16_t prediction_vq_index[DCA_CHANNELS][DCA_SUBBANDS_X96]; ///< Prediction coefficients VQ address
144  int8_t bit_allocation[DCA_CHANNELS][DCA_SUBBANDS_X96]; ///< Bit allocation index
145  int8_t transition_mode[DCA_SUBFRAMES][DCA_CHANNELS][DCA_SUBBANDS]; ///< Transition mode
146  int32_t scale_factors[DCA_CHANNELS][DCA_SUBBANDS][2]; ///< Scale factors (2x for transients and X96)
147  int8_t joint_scale_sel[DCA_CHANNELS]; ///< Joint subband codebook select
148  int32_t joint_scale_factors[DCA_CHANNELS][DCA_SUBBANDS_X96]; ///< Scale factors for joint subband coding
150  // Auxiliary data
151  int prim_dmix_embedded; ///< Auxiliary dynamic downmix flag
152  int prim_dmix_type; ///< Auxiliary primary channel downmix type
153  int prim_dmix_coeff[DCA_DMIX_CHANNELS_MAX * DCA_CORE_CHANNELS_MAX]; ///< Dynamic downmix code coefficients
155  // Core extensions
156  int ext_audio_mask; ///< Bit mask of fully decoded core extensions
158  // XCH extension data
159  int xch_pos; ///< Bit position of XCH frame in core substream
161  // XXCH extension data
162  int xxch_crc_present; ///< CRC presence flag for XXCH channel set header
163  int xxch_mask_nbits; ///< Number of bits for loudspeaker mask
164  int xxch_core_mask; ///< Core loudspeaker activity mask
165  int xxch_spkr_mask; ///< Loudspeaker layout mask
166  int xxch_dmix_embedded; ///< Downmix already performed by encoder
167  int xxch_dmix_scale_inv; ///< Downmix scale factor
168  int xxch_dmix_mask[DCA_XXCH_CHANNELS_MAX]; ///< Downmix channel mapping mask
169  int xxch_dmix_coeff[DCA_XXCH_CHANNELS_MAX * DCA_CORE_CHANNELS_MAX]; ///< Downmix coefficients
170  int xxch_pos; ///< Bit position of XXCH frame in core substream
172  // X96 extension data
173  int x96_rev_no; ///< X96 revision number
174  int x96_crc_present; ///< CRC presence flag for X96 channel set header
175  int x96_nchannels; ///< Number of primary channels in X96 extension
176  int x96_high_res; ///< X96 high resolution flag
177  int x96_subband_start; ///< First encoded subband in X96 extension
178  int x96_rand; ///< Random seed for generating samples for unallocated X96 subbands
179  int x96_pos; ///< Bit position of X96 frame in core substream
181  // Sample buffers
182  unsigned int x96_subband_size;
183  int32_t *x96_subband_buffer; ///< X96 subband sample buffer base
184  int32_t *x96_subband_samples[DCA_CHANNELS][DCA_SUBBANDS_X96]; ///< X96 subband samples
186  unsigned int subband_size;
187  int32_t *subband_buffer; ///< Subband sample buffer base
188  int32_t *subband_samples[DCA_CHANNELS][DCA_SUBBANDS]; ///< Subband samples
189  int32_t *lfe_samples; ///< Decimated LFE samples
191  // DSP contexts
192  DCADSPData dcadsp_data[DCA_CHANNELS]; ///< FIR history buffers
195  FFTContext imdct[2];
200  // PCM output data
201  unsigned int output_size;
202  void *output_buffer; ///< PCM output buffer base
203  int32_t *output_samples[DCA_SPEAKER_COUNT]; ///< PCM output for fixed point mode
204  int32_t output_history_lfe_fixed; ///< LFE PCM history for X96 filter
205  float output_history_lfe_float; ///< LFE PCM history for X96 filter
207  int ch_remap[DCA_SPEAKER_COUNT]; ///< Channel to speaker map
208  int request_mask; ///< Requested channel layout (for stereo downmix)
210  int npcmsamples; ///< Number of PCM samples per channel
211  int output_rate; ///< Output sample rate (1x or 2x header rate)
213  int filter_mode; ///< Previous filtering mode for detecting changes
216 static inline int ff_dca_core_map_spkr(DCACoreDecoder *core, int spkr)
217 {
218  if (core->ch_mask & (1U << spkr))
219  return spkr;
220  if (spkr == DCA_SPEAKER_Lss && (core->ch_mask & DCA_SPEAKER_MASK_Ls))
221  return DCA_SPEAKER_Ls;
222  if (spkr == DCA_SPEAKER_Rss && (core->ch_mask & DCA_SPEAKER_MASK_Rs))
223  return DCA_SPEAKER_Rs;
224  return -1;
225 }
227 static inline void ff_dca_core_dequantize(int32_t *output, const int32_t *input,
228  int32_t step_size, int32_t scale, int residual, int len)
229 {
230  // Account for quantizer step size
231  int64_t step_scale = (int64_t)step_size * scale;
232  int n, shift = 0;
234  // Limit scale factor resolution to 22 bits
235  if (step_scale > (1 << 23)) {
236  shift = av_log2(step_scale >> 23) + 1;
237  step_scale >>= shift;
238  }
240  // Scale the samples
241  if (residual) {
242  for (n = 0; n < len; n++)
243  output[n] += clip23(norm__(input[n] * step_scale, 22 - shift));
244  } else {
245  for (n = 0; n < len; n++)
246  output[n] = clip23(norm__(input[n] * step_scale, 22 - shift));
247  }
248 }
252 int ff_dca_core_filter_fixed(DCACoreDecoder *s, int x96_synth);
258 #endif
int audio_mode
Audio channel arrangement.
Definition: dca_core.h:110
union DCADSPData::@47 u
int xxch_mask_nbits
Number of bits for loudspeaker mask.
Definition: dca_core.h:163
int offset
Definition: dca_core.h:98
static int shift(int a, int b)
Definition: sonic.c:82
This structure describes decoded (raw) audio or video data.
Definition: frame.h:314
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
int xxch_dmix_embedded
Downmix already performed by encoder.
Definition: dca_core.h:166
int ext_audio_type
Extension audio descriptor flag.
Definition: dca_core.h:116
int frame_size
Primary frame byte size.
Definition: dca_core.h:109
unsigned int output_size
Definition: dca_core.h:201
int av_log2(unsigned v)
Definition: intmath.c:26
int filter_perfect
Multirate interpolator switch.
Definition: dca_core.h:121
float output_history_lfe_float
LFE PCM history for X96 filter.
Definition: dca_core.h:205
Definition: dca_core.h:50
struct DCADSPData::@47::@49 fix
Definition: dca_core.h:44
unsigned int x96_subband_size
Definition: dca_core.h:182
int nsubframes
Number of subframes.
Definition: dca_core.h:128
static int32_t clip23(int32_t a)
Definition: dcamath.h:54
int x96_rand
Random seed for generating samples for unallocated X96 subbands.
Definition: dca_core.h:178
Definition: dca_core.h:74
#define av_cold
Definition: attributes.h:88
int source_pcm_res
Source PCM resolution.
Definition: dca_core.h:122
int xxch_dmix_scale_inv
Downmix scale factor.
Definition: dca_core.h:167
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: dca_core.h:51
int filter_mode
Previous filtering mode for detecting changes.
Definition: dca_core.h:213
void * output_buffer
PCM output buffer base.
Definition: dca_core.h:202
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
Definition: mem.h:117
#define DCA_SUBBANDS_X96
Definition: dca_core.h:43
int prim_dmix_type
Auxiliary primary channel downmix type.
Definition: dca_core.h:152
bitstream reader API header.
int32_t * x96_subband_buffer
X96 subband sample buffer base.
Definition: dca_core.h:183
static void ff_dca_core_dequantize(int32_t *output, const int32_t *input, int32_t step_size, int32_t scale, int residual, int len)
Definition: dca_core.h:227
ptrdiff_t size
Definition: opengl_enc.c:100
int ff_dca_core_parse_exss(DCACoreDecoder *s, uint8_t *data, DCAExssAsset *asset)
Definition: dca_core.c:1831
av_cold void ff_dca_core_flush(DCACoreDecoder *s)
Definition: dca_core.c:2399
int sync_ssf
Audio sync word insertion flag.
Definition: dca_core.h:118
int32_t * subband_buffer
Subband sample buffer base.
Definition: dca_core.h:187
int npcmsamples
Number of PCM samples per channel.
Definition: dca_core.h:210
#define U(x)
Definition: vp56_arith.h:37
int sumdiff_front
Front sum/difference flag.
Definition: dca_core.h:124
SynthFilterContext synth
Definition: dca_core.h:196
int bit_rate
Transmission bit rate.
Definition: dca_core.h:112
int ff_dca_core_parse(DCACoreDecoder *s, uint8_t *data, int size)
Definition: dca_core.c:1798
int xxch_crc_present
CRC presence flag for XXCH channel set header.
Definition: dca_core.h:162
DCADCTContext dcadct
Definition: dca_core.h:194
AVFloatDSPContext * float_dsp
Definition: dca_core.h:197
int ff_dca_core_filter_fixed(DCACoreDecoder *s, int x96_synth)
Definition: dca_core.c:1959
int aux_present
Auxiliary data flag.
Definition: dca_core.h:115
int32_t * lfe_samples
Decimated LFE samples.
Definition: dca_core.h:189
int prim_dmix_embedded
Auxiliary dynamic downmix flag.
Definition: dca_core.h:151
int sample_rate
Core audio sampling frequency.
Definition: dca_core.h:111
int request_mask
Requested channel layout (for stereo downmix)
Definition: dca_core.h:208
Definition: fft.h:83
int output_rate
Output sample rate (1x or 2x header rate)
Definition: dca_core.h:211
int nchannels
Number of primary audio channels (incl. extension channels)
Definition: dca_core.h:129
static int ff_dca_core_map_spkr(DCACoreDecoder *core, int spkr)
Definition: dca_core.h:216
int xxch_pos
Bit position of XXCH frame in core substream.
Definition: dca_core.h:170
int sumdiff_surround
Surround sum/difference flag.
Definition: dca_core.h:125
AVCodecContext * avctx
Definition: dca_core.h:102
int x96_crc_present
CRC presence flag for X96 channel set header.
Definition: dca_core.h:174
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#define s(width, name)
Definition: cbs_vp9.c:257
float hist2[64]
Definition: dca_core.h:91
av_cold int ff_dca_core_init(DCACoreDecoder *s)
Definition: dca_core.c:2412
int xxch_core_mask
Core loudspeaker activity mask.
Definition: dca_core.h:164
Definition: dca_core.h:42
Libavcodec external API header.
int ts_present
Embedded time stamp flag.
Definition: dca_core.h:114
int xxch_spkr_mask
Loudspeaker layout mask.
Definition: dca_core.h:165
int crc_present
CRC present flag.
Definition: dca_core.h:107
Definition: dcahuff.h:32
main external API structure.
Definition: avcodec.h:531
int ext_audio_present
Extended coding flag.
Definition: dca_core.h:117
int es_format
Extended surround (ES) mastering flag.
Definition: dca_core.h:123
int x96_subband_start
First encoded subband in X96 extension.
Definition: dca_core.h:177
int x96_pos
Bit position of X96 frame in core substream.
Definition: dca_core.h:179
int xch_pos
Bit position of XCH frame in core substream.
Definition: dca_core.h:159
DCADSPContext * dcadsp
Definition: dca_core.h:193
int x96_rev_no
X96 revision number.
Definition: dca_core.h:173
struct DCADSPData::@47::@48 flt
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
Definition: dca_core.h:80
int x96_nchannels
Number of primary channels in X96 extension.
Definition: dca_core.h:175
float hist1[1024]
Definition: dca_core.h:90
Definition: dca_core.h:41
unsigned int subband_size
Definition: dca_core.h:186
int lfe_present
Low frequency effects flag.
Definition: dca_core.h:119
Definition: dca_core.h:59
common internal api header.
Definition: dca_core.h:52
common internal and external API header
static int bit_allocation(IMCContext *q, IMCChannel *chctx, int stream_format_code, int freebits, int flag)
Perform bit allocation depending on bits available.
Definition: imc.c:454
GetBitContext gb_in
Definition: dca_core.h:104
AVFixedDSPContext * fixed_dsp
Definition: dca_core.h:198
int len
GetBitContext gb
Definition: dca_core.h:103
static int32_t norm__(int64_t a, int bits)
Definition: dcamath.h:27
int ch_mask
Speaker layout mask (incl. LFE and extension channels)
Definition: dca_core.h:130
av_cold void ff_dca_core_close(DCACoreDecoder *s)
Definition: dca_core.c:2430
int drc_present
Embedded dynamic range flag.
Definition: dca_core.h:113
int ff_dca_core_filter_frame(DCACoreDecoder *s, AVFrame *frame)
Definition: dca_core.c:2345
int x96_high_res
X96 high resolution flag.
Definition: dca_core.h:176
int ext_audio_mask
Bit mask of fully decoded core extensions.
Definition: dca_core.h:156
int predictor_history
Predictor history flag switch.
Definition: dca_core.h:120
int32_t output_history_lfe_fixed
LFE PCM history for X96 filter.
Definition: dca_core.h:204
int npcmblocks
Number of PCM sample blocks.
Definition: dca_core.h:108