You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

358 lines
12KB

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