]> git.sesse.net Git - ffmpeg/blob - libavcodec/ac3dec.h
add support for spectral extension
[ffmpeg] / libavcodec / ac3dec.h
1 /*
2  * Common code between the AC-3 and E-AC-3 decoders
3  * Copyright (c) 2007 Bartlomiej Wolowiec <bartek.wolowiec@gmail.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file ac3.h
24  * Common code between the AC-3 and E-AC-3 decoders.
25  */
26
27 #ifndef AVCODEC_AC3DEC_H
28 #define AVCODEC_AC3DEC_H
29
30 #include "libavutil/lfg.h"
31 #include "ac3.h"
32 #include "bitstream.h"
33 #include "dsputil.h"
34
35 /* override ac3.h to include coupling channel */
36 #undef AC3_MAX_CHANNELS
37 #define AC3_MAX_CHANNELS 7
38 #define CPL_CH 0
39
40 #define AC3_OUTPUT_LFEON  8
41
42 #define AC3_MAX_COEFS   256
43 #define AC3_BLOCK_SIZE  256
44 #define MAX_BLOCKS        6
45 #define SPX_MAX_BANDS    17
46
47 #define INT24_MIN -8388608
48 #define INT24_MAX  8388607
49 #define M_SQRT_POW2_15 181
50
51 typedef struct {
52     AVCodecContext *avctx;                  ///< parent context
53     GetBitContext gbc;                      ///< bitstream reader
54     uint8_t *input_buffer;                  ///< temp buffer to prevent overread
55
56 ///@defgroup bsi bit stream information
57 ///@{
58     int frame_type;                         ///< frame type                             (strmtyp)
59     int substreamid;                        ///< substream identification
60     int frame_size;                         ///< current frame size, in bytes
61     int bit_rate;                           ///< stream bit rate, in bits-per-second
62     int sample_rate;                        ///< sample frequency, in Hz
63     int num_blocks;                         ///< number of audio blocks
64     int channel_mode;                       ///< channel mode                           (acmod)
65     int lfe_on;                             ///< lfe channel in use
66     int channel_map;                        ///< custom channel map
67     int center_mix_level;                   ///< Center mix level index
68     int surround_mix_level;                 ///< Surround mix level index
69     int eac3;                               ///< indicates if current frame is E-AC-3
70 ///@}
71
72 ///@defgroup audfrm frame syntax parameters
73     int snr_offset_strategy;                ///< SNR offset strategy                    (snroffststr)
74     int block_switch_syntax;                ///< block switch syntax enabled            (blkswe)
75     int dither_flag_syntax;                 ///< dither flag syntax enabled             (dithflage)
76     int bit_allocation_syntax;              ///< bit allocation model syntax enabled    (bamode)
77     int fast_gain_syntax;                   ///< fast gain codes enabled                (frmfgaincode)
78     int dba_syntax;                         ///< delta bit allocation syntax enabled    (dbaflde)
79     int skip_syntax;                        ///< skip field syntax enabled              (skipflde)
80  ///@}
81
82 ///@defgroup cpl standard coupling
83     int cpl_in_use[MAX_BLOCKS];             ///< coupling in use                        (cplinu)
84     int cpl_strategy_exists[MAX_BLOCKS];    ///< coupling strategy exists               (cplstre)
85     int channel_in_cpl[AC3_MAX_CHANNELS];   ///< channel in coupling                    (chincpl)
86     int phase_flags_in_use;                 ///< phase flags in use                     (phsflginu)
87     int phase_flags[18];                    ///< phase flags                            (phsflg)
88     int num_cpl_subbands;                   ///< number of coupling sub bands           (ncplsubnd)
89     int num_cpl_bands;                      ///< number of coupling bands               (ncplbnd)
90     uint8_t cpl_band_struct[18];            ///< coupling band structure                (cplbndstrc)
91     int firstchincpl;                       ///< first channel in coupling
92     int first_cpl_coords[AC3_MAX_CHANNELS]; ///< first coupling coordinates states      (firstcplcos)
93     int cpl_coords[AC3_MAX_CHANNELS][18];   ///< coupling coordinates                   (cplco)
94 ///@}
95
96 ///@defgroup spx spectral extension
97 ///@{
98     int spx_in_use[MAX_BLOCKS];             ///< spectral extension in use              (spxinu)
99     int channel_in_spx[AC3_MAX_CHANNELS];   ///< channel in spectral extension          (chinspx)
100     int spx_atten_code[AC3_MAX_CHANNELS];   ///< spx attenuation code                   (spxattencod)
101     int spx_coords_exist[AC3_MAX_CHANNELS]; ///< indicates if a channel has spx coords  (spxcoe)
102     int spx_start_subband;                  ///< spx beginning frequency band           (spxbegf)
103     int spx_start_freq;                     ///< spx start frequency bin
104     int spx_end_freq;                       ///< spx end frequency bin
105     int spx_copy_start_freq;                ///< spx starting frequency for copying     (copystartmant)
106     int num_spx_subbands;                   ///< number of spectral extension subbands
107     int num_spx_bands;                      ///< number of spectral extension bands     (nspxbnds)
108     uint8_t spx_band_struct[SPX_MAX_BANDS]; ///< spectral extension band structure      (spxbndstrc)
109     int spx_band_sizes[SPX_MAX_BANDS];      ///< number of bins in each band            (spxbndsztab)
110     int first_spx_coords[AC3_MAX_CHANNELS]; ///< first spx coordinates states           (firstspxcos)
111     int spx_noise_blend[AC3_MAX_CHANNELS][SPX_MAX_BANDS];   ///< spx noise blending factor  (nblendfact)
112     int spx_signal_blend[AC3_MAX_CHANNELS][SPX_MAX_BANDS];  ///< spx signal blending factor (sblendfact)
113     int spx_coords[AC3_MAX_CHANNELS][SPX_MAX_BANDS];    ///< spectral extension coordinates (spxco)
114 ///@}
115
116 ///@defgroup aht adaptive hybrid transform
117     int channel_uses_aht[AC3_MAX_CHANNELS];                         ///< channel AHT in use (chahtinu)
118     int pre_mantissa[AC3_MAX_CHANNELS][AC3_MAX_COEFS][MAX_BLOCKS];  ///< pre-IDCT mantissas
119 ///@}
120
121 ///@defgroup channel channel
122     int fbw_channels;                           ///< number of full-bandwidth channels
123     int channels;                               ///< number of total channels
124     int lfe_ch;                                 ///< index of LFE channel
125     float downmix_coeffs[AC3_MAX_CHANNELS][2];  ///< stereo downmix coefficients
126     int downmixed;                              ///< indicates if coeffs are currently downmixed
127     int output_mode;                            ///< output channel configuration
128     int out_channels;                           ///< number of output channels
129 ///@}
130
131 ///@defgroup dynrng dynamic range
132     float dynamic_range[2];                 ///< dynamic range
133 ///@}
134
135 ///@defgroup bandwidth bandwidth
136     int start_freq[AC3_MAX_CHANNELS];       ///< start frequency bin                    (strtmant)
137     int end_freq[AC3_MAX_CHANNELS];         ///< end frequency bin                      (endmant)
138 ///@}
139
140 ///@defgroup rematrixing rematrixing
141     int num_rematrixing_bands;              ///< number of rematrixing bands            (nrematbnd)
142     int rematrixing_flags[4];               ///< rematrixing flags                      (rematflg)
143 ///@}
144
145 ///@defgroup exponents exponents
146     int num_exp_groups[AC3_MAX_CHANNELS];           ///< Number of exponent groups      (nexpgrp)
147     int8_t dexps[AC3_MAX_CHANNELS][AC3_MAX_COEFS];  ///< decoded exponents
148     int exp_strategy[MAX_BLOCKS][AC3_MAX_CHANNELS]; ///< exponent strategies            (expstr)
149 ///@}
150
151 ///@defgroup bitalloc bit allocation
152     AC3BitAllocParameters bit_alloc_params;         ///< bit allocation parameters
153     int first_cpl_leak;                             ///< first coupling leak state      (firstcplleak)
154     int snr_offset[AC3_MAX_CHANNELS];               ///< signal-to-noise ratio offsets  (snroffst)
155     int fast_gain[AC3_MAX_CHANNELS];                ///< fast gain values/SMR's         (fgain)
156     uint8_t bap[AC3_MAX_CHANNELS][AC3_MAX_COEFS];   ///< bit allocation pointers
157     int16_t psd[AC3_MAX_CHANNELS][AC3_MAX_COEFS];   ///< scaled exponents
158     int16_t band_psd[AC3_MAX_CHANNELS][50];         ///< interpolated exponents
159     int16_t mask[AC3_MAX_CHANNELS][50];             ///< masking curve values
160     int dba_mode[AC3_MAX_CHANNELS];                 ///< delta bit allocation mode
161     int dba_nsegs[AC3_MAX_CHANNELS];                ///< number of delta segments
162     uint8_t dba_offsets[AC3_MAX_CHANNELS][8];       ///< delta segment offsets
163     uint8_t dba_lengths[AC3_MAX_CHANNELS][8];       ///< delta segment lengths
164     uint8_t dba_values[AC3_MAX_CHANNELS][8];        ///< delta values for each segment
165 ///@}
166
167 ///@defgroup dithering zero-mantissa dithering
168     int dither_flag[AC3_MAX_CHANNELS];      ///< dither flags                           (dithflg)
169     AVLFG dith_state;                       ///< for dither generation
170 ///@}
171
172 ///@defgroup imdct IMDCT
173     int block_switch[AC3_MAX_CHANNELS];     ///< block switch flags                     (blksw)
174     MDCTContext imdct_512;                  ///< for 512 sample IMDCT
175     MDCTContext imdct_256;                  ///< for 256 sample IMDCT
176 ///@}
177
178 ///@defgroup opt optimization
179     DSPContext dsp;                         ///< for optimization
180     float add_bias;                         ///< offset for float_to_int16 conversion
181     float mul_bias;                         ///< scaling for float_to_int16 conversion
182 ///@}
183
184     DECLARE_ALIGNED_16(int, fixed_coeffs[AC3_MAX_CHANNELS][AC3_MAX_COEFS]);  ///> fixed-point transform coefficients
185
186 ///@defgroup arrays aligned arrays
187     DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][AC3_MAX_COEFS]);   ///< transform coefficients
188     DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE]);             ///< delay - added to the next block
189     DECLARE_ALIGNED_16(float, window[AC3_BLOCK_SIZE]);                              ///< window coefficients
190     DECLARE_ALIGNED_16(float, tmp_output[AC3_BLOCK_SIZE]);                          ///< temporary storage for output before windowing
191     DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE]);            ///< output after imdct transform and windowing
192 ///@}
193 } AC3DecodeContext;
194
195 /**
196  * Parse the E-AC-3 frame header.
197  * This parses both the bit stream info and audio frame header.
198  */
199 int ff_eac3_parse_header(AC3DecodeContext *s);
200
201 /**
202  * Decode mantissas in a single channel for the entire frame.
203  * This is used when AHT mode is enabled.
204  */
205 void ff_eac3_decode_transform_coeffs_aht_ch(AC3DecodeContext *s, int ch);
206
207 void ff_eac3_apply_spectral_extension(AC3DecodeContext *s);
208
209 #endif /* AVCODEC_AC3DEC_H */