aac.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * AAC definitions and structures
  3. * Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
  4. * Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * AAC definitions and structures
  25. * @author Oded Shimon ( ods15 ods15 dyndns org )
  26. * @author Maxim Gavrilov ( maxim.gavrilov gmail com )
  27. */
  28. #ifndef AVCODEC_AAC_H
  29. #define AVCODEC_AAC_H
  30. #include "aac_defines.h"
  31. #include "libavutil/float_dsp.h"
  32. #include "libavutil/fixed_dsp.h"
  33. #include "avcodec.h"
  34. #if !USE_FIXED
  35. #include "mdct15.h"
  36. #endif
  37. #include "fft.h"
  38. #include "mpeg4audio.h"
  39. #include "sbr.h"
  40. #include <stdint.h>
  41. #define MAX_CHANNELS 64
  42. #define MAX_ELEM_ID 16
  43. #define TNS_MAX_ORDER 20
  44. #define MAX_LTP_LONG_SFB 40
  45. #define CLIP_AVOIDANCE_FACTOR 0.95f
  46. enum RawDataBlockType {
  47. TYPE_SCE,
  48. TYPE_CPE,
  49. TYPE_CCE,
  50. TYPE_LFE,
  51. TYPE_DSE,
  52. TYPE_PCE,
  53. TYPE_FIL,
  54. TYPE_END,
  55. };
  56. enum ExtensionPayloadID {
  57. EXT_FILL,
  58. EXT_FILL_DATA,
  59. EXT_DATA_ELEMENT,
  60. EXT_DYNAMIC_RANGE = 0xb,
  61. EXT_SBR_DATA = 0xd,
  62. EXT_SBR_DATA_CRC = 0xe,
  63. };
  64. enum WindowSequence {
  65. ONLY_LONG_SEQUENCE,
  66. LONG_START_SEQUENCE,
  67. EIGHT_SHORT_SEQUENCE,
  68. LONG_STOP_SEQUENCE,
  69. };
  70. enum BandType {
  71. ZERO_BT = 0, ///< Scalefactors and spectral data are all zero.
  72. FIRST_PAIR_BT = 5, ///< This and later band types encode two values (rather than four) with one code word.
  73. ESC_BT = 11, ///< Spectral data are coded with an escape sequence.
  74. RESERVED_BT = 12, ///< Band types following are encoded differently from others.
  75. NOISE_BT = 13, ///< Spectral data are scaled white noise not coded in the bitstream.
  76. INTENSITY_BT2 = 14, ///< Scalefactor data are intensity stereo positions (out of phase).
  77. INTENSITY_BT = 15, ///< Scalefactor data are intensity stereo positions (in phase).
  78. };
  79. #define IS_CODEBOOK_UNSIGNED(x) (((x) - 1) & 10)
  80. enum ChannelPosition {
  81. AAC_CHANNEL_OFF = 0,
  82. AAC_CHANNEL_FRONT = 1,
  83. AAC_CHANNEL_SIDE = 2,
  84. AAC_CHANNEL_BACK = 3,
  85. AAC_CHANNEL_LFE = 4,
  86. AAC_CHANNEL_CC = 5,
  87. };
  88. /**
  89. * The point during decoding at which channel coupling is applied.
  90. */
  91. enum CouplingPoint {
  92. BEFORE_TNS,
  93. BETWEEN_TNS_AND_IMDCT,
  94. AFTER_IMDCT = 3,
  95. };
  96. /**
  97. * Output configuration status
  98. */
  99. enum OCStatus {
  100. OC_NONE, ///< Output unconfigured
  101. OC_TRIAL_PCE, ///< Output configuration under trial specified by an inband PCE
  102. OC_TRIAL_FRAME, ///< Output configuration under trial specified by a frame header
  103. OC_GLOBAL_HDR, ///< Output configuration set in a global header but not yet locked
  104. OC_LOCKED, ///< Output configuration locked in place
  105. };
  106. typedef struct OutputConfiguration {
  107. MPEG4AudioConfig m4ac;
  108. uint8_t layout_map[MAX_ELEM_ID*4][3];
  109. int layout_map_tags;
  110. int channels;
  111. uint64_t channel_layout;
  112. enum OCStatus status;
  113. } OutputConfiguration;
  114. /**
  115. * Predictor State
  116. */
  117. typedef struct PredictorState {
  118. AAC_FLOAT cor0;
  119. AAC_FLOAT cor1;
  120. AAC_FLOAT var0;
  121. AAC_FLOAT var1;
  122. AAC_FLOAT r0;
  123. AAC_FLOAT r1;
  124. AAC_FLOAT k1;
  125. AAC_FLOAT x_est;
  126. } PredictorState;
  127. #define MAX_PREDICTORS 672
  128. #define SCALE_DIV_512 36 ///< scalefactor difference that corresponds to scale difference in 512 times
  129. #define SCALE_ONE_POS 140 ///< scalefactor index that corresponds to scale=1.0
  130. #define SCALE_MAX_POS 255 ///< scalefactor index maximum value
  131. #define SCALE_MAX_DIFF 60 ///< maximum scalefactor difference allowed by standard
  132. #define SCALE_DIFF_ZERO 60 ///< codebook index corresponding to zero scalefactor indices difference
  133. #define POW_SF2_ZERO 200 ///< ff_aac_pow2sf_tab index corresponding to pow(2, 0);
  134. #define NOISE_PRE 256 ///< preamble for NOISE_BT, put in bitstream with the first noise band
  135. #define NOISE_PRE_BITS 9 ///< length of preamble
  136. #define NOISE_OFFSET 90 ///< subtracted from global gain, used as offset for the preamble
  137. /**
  138. * Long Term Prediction
  139. */
  140. typedef struct LongTermPrediction {
  141. int8_t present;
  142. int16_t lag;
  143. int coef_idx;
  144. INTFLOAT coef;
  145. int8_t used[MAX_LTP_LONG_SFB];
  146. } LongTermPrediction;
  147. /**
  148. * Individual Channel Stream
  149. */
  150. typedef struct IndividualChannelStream {
  151. uint8_t max_sfb; ///< number of scalefactor bands per group
  152. enum WindowSequence window_sequence[2];
  153. uint8_t use_kb_window[2]; ///< If set, use Kaiser-Bessel window, otherwise use a sine window.
  154. int num_window_groups;
  155. uint8_t group_len[8];
  156. LongTermPrediction ltp;
  157. const uint16_t *swb_offset; ///< table of offsets to the lowest spectral coefficient of a scalefactor band, sfb, for a particular window
  158. const uint8_t *swb_sizes; ///< table of scalefactor band sizes for a particular window
  159. int num_swb; ///< number of scalefactor window bands
  160. int num_windows;
  161. int tns_max_bands;
  162. int predictor_present;
  163. int predictor_initialized;
  164. int predictor_reset_group;
  165. int predictor_reset_count[31]; ///< used by encoder to count prediction resets
  166. uint8_t prediction_used[41];
  167. uint8_t window_clipping[8]; ///< set if a certain window is near clipping
  168. float clip_avoidance_factor; ///< set if any window is near clipping to the necessary atennuation factor to avoid it
  169. } IndividualChannelStream;
  170. /**
  171. * Temporal Noise Shaping
  172. */
  173. typedef struct TemporalNoiseShaping {
  174. int present;
  175. int n_filt[8];
  176. int length[8][4];
  177. int direction[8][4];
  178. int order[8][4];
  179. int coef_idx[8][4][TNS_MAX_ORDER];
  180. INTFLOAT coef[8][4][TNS_MAX_ORDER];
  181. } TemporalNoiseShaping;
  182. /**
  183. * Dynamic Range Control - decoded from the bitstream but not processed further.
  184. */
  185. typedef struct DynamicRangeControl {
  186. int pce_instance_tag; ///< Indicates with which program the DRC info is associated.
  187. int dyn_rng_sgn[17]; ///< DRC sign information; 0 - positive, 1 - negative
  188. int dyn_rng_ctl[17]; ///< DRC magnitude information
  189. int exclude_mask[MAX_CHANNELS]; ///< Channels to be excluded from DRC processing.
  190. int band_incr; ///< Number of DRC bands greater than 1 having DRC info.
  191. int interpolation_scheme; ///< Indicates the interpolation scheme used in the SBR QMF domain.
  192. int band_top[17]; ///< Indicates the top of the i-th DRC band in units of 4 spectral lines.
  193. int prog_ref_level; /**< A reference level for the long-term program audio level for all
  194. * channels combined.
  195. */
  196. } DynamicRangeControl;
  197. typedef struct Pulse {
  198. int num_pulse;
  199. int start;
  200. int pos[4];
  201. int amp[4];
  202. } Pulse;
  203. /**
  204. * coupling parameters
  205. */
  206. typedef struct ChannelCoupling {
  207. enum CouplingPoint coupling_point; ///< The point during decoding at which coupling is applied.
  208. int num_coupled; ///< number of target elements
  209. enum RawDataBlockType type[8]; ///< Type of channel element to be coupled - SCE or CPE.
  210. int id_select[8]; ///< element id
  211. int ch_select[8]; /**< [0] shared list of gains; [1] list of gains for right channel;
  212. * [2] list of gains for left channel; [3] lists of gains for both channels
  213. */
  214. INTFLOAT gain[16][120];
  215. } ChannelCoupling;
  216. /**
  217. * Single Channel Element - used for both SCE and LFE elements.
  218. */
  219. typedef struct SingleChannelElement {
  220. IndividualChannelStream ics;
  221. TemporalNoiseShaping tns;
  222. Pulse pulse;
  223. enum BandType band_type[128]; ///< band types
  224. enum BandType band_alt[128]; ///< alternative band type (used by encoder)
  225. int band_type_run_end[120]; ///< band type run end points
  226. INTFLOAT sf[120]; ///< scalefactors
  227. int sf_idx[128]; ///< scalefactor indices (used by encoder)
  228. uint8_t zeroes[128]; ///< band is not coded (used by encoder)
  229. uint8_t can_pns[128]; ///< band is allowed to PNS (informative)
  230. float is_ener[128]; ///< Intensity stereo pos (used by encoder)
  231. float pns_ener[128]; ///< Noise energy values (used by encoder)
  232. DECLARE_ALIGNED(32, INTFLOAT, pcoeffs)[1024]; ///< coefficients for IMDCT, pristine
  233. DECLARE_ALIGNED(32, INTFLOAT, coeffs)[1024]; ///< coefficients for IMDCT, maybe processed
  234. DECLARE_ALIGNED(32, INTFLOAT, saved)[1536]; ///< overlap
  235. DECLARE_ALIGNED(32, INTFLOAT, ret_buf)[2048]; ///< PCM output buffer
  236. DECLARE_ALIGNED(16, INTFLOAT, ltp_state)[3072]; ///< time signal for LTP
  237. DECLARE_ALIGNED(32, AAC_FLOAT, lcoeffs)[1024]; ///< MDCT of LTP coefficients (used by encoder)
  238. DECLARE_ALIGNED(32, AAC_FLOAT, prcoeffs)[1024]; ///< Main prediction coefs (used by encoder)
  239. PredictorState predictor_state[MAX_PREDICTORS];
  240. INTFLOAT *ret; ///< PCM output
  241. } SingleChannelElement;
  242. /**
  243. * channel element - generic struct for SCE/CPE/CCE/LFE
  244. */
  245. typedef struct ChannelElement {
  246. int present;
  247. // CPE specific
  248. int common_window; ///< Set if channels share a common 'IndividualChannelStream' in bitstream.
  249. int ms_mode; ///< Signals mid/side stereo flags coding mode (used by encoder)
  250. uint8_t is_mode; ///< Set if any bands have been encoded using intensity stereo (used by encoder)
  251. uint8_t ms_mask[128]; ///< Set if mid/side stereo is used for each scalefactor window band
  252. uint8_t is_mask[128]; ///< Set if intensity stereo is used (used by encoder)
  253. // shared
  254. SingleChannelElement ch[2];
  255. // CCE specific
  256. ChannelCoupling coup;
  257. SpectralBandReplication sbr;
  258. } ChannelElement;
  259. /**
  260. * main AAC context
  261. */
  262. struct AACContext {
  263. AVClass *class;
  264. AVCodecContext *avctx;
  265. AVFrame *frame;
  266. int is_saved; ///< Set if elements have stored overlap from previous frame.
  267. DynamicRangeControl che_drc;
  268. /**
  269. * @name Channel element related data
  270. * @{
  271. */
  272. ChannelElement *che[4][MAX_ELEM_ID];
  273. ChannelElement *tag_che_map[4][MAX_ELEM_ID];
  274. int tags_mapped;
  275. int warned_remapping_once;
  276. /** @} */
  277. /**
  278. * @name temporary aligned temporary buffers
  279. * (We do not want to have these on the stack.)
  280. * @{
  281. */
  282. DECLARE_ALIGNED(32, INTFLOAT, buf_mdct)[1024];
  283. /** @} */
  284. /**
  285. * @name Computed / set up during initialization
  286. * @{
  287. */
  288. FFTContext mdct;
  289. FFTContext mdct_small;
  290. FFTContext mdct_ld;
  291. FFTContext mdct_ltp;
  292. #if USE_FIXED
  293. AVFixedDSPContext *fdsp;
  294. #else
  295. MDCT15Context *mdct120;
  296. MDCT15Context *mdct480;
  297. MDCT15Context *mdct960;
  298. AVFloatDSPContext *fdsp;
  299. #endif /* USE_FIXED */
  300. int random_state;
  301. /** @} */
  302. /**
  303. * @name Members used for output
  304. * @{
  305. */
  306. SingleChannelElement *output_element[MAX_CHANNELS]; ///< Points to each SingleChannelElement
  307. /** @} */
  308. /**
  309. * @name Japanese DTV specific extension
  310. * @{
  311. */
  312. int force_dmono_mode;///< 0->not dmono, 1->use first channel, 2->use second channel
  313. int dmono_mode; ///< 0->not dmono, 1->use first channel, 2->use second channel
  314. /** @} */
  315. DECLARE_ALIGNED(32, INTFLOAT, temp)[128];
  316. OutputConfiguration oc[2];
  317. int warned_num_aac_frames;
  318. int warned_960_sbr;
  319. unsigned warned_71_wide;
  320. int warned_gain_control;
  321. /* aacdec functions pointers */
  322. void (*imdct_and_windowing)(AACContext *ac, SingleChannelElement *sce);
  323. void (*apply_ltp)(AACContext *ac, SingleChannelElement *sce);
  324. void (*apply_tns)(INTFLOAT coef[1024], TemporalNoiseShaping *tns,
  325. IndividualChannelStream *ics, int decode);
  326. void (*windowing_and_mdct_ltp)(AACContext *ac, INTFLOAT *out,
  327. INTFLOAT *in, IndividualChannelStream *ics);
  328. void (*update_ltp)(AACContext *ac, SingleChannelElement *sce);
  329. void (*vector_pow43)(int *coefs, int len);
  330. void (*subband_scale)(int *dst, int *src, int scale, int offset, int len, void *log_context);
  331. };
  332. void ff_aacdec_init_mips(AACContext *c);
  333. #endif /* AVCODEC_AAC_H */