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.

362 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. #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. } PredictorState;
  125. #define MAX_PREDICTORS 672
  126. #define SCALE_DIV_512 36 ///< scalefactor difference that corresponds to scale difference in 512 times
  127. #define SCALE_ONE_POS 140 ///< scalefactor index that corresponds to scale=1.0
  128. #define SCALE_MAX_POS 255 ///< scalefactor index maximum value
  129. #define SCALE_MAX_DIFF 60 ///< maximum scalefactor difference allowed by standard
  130. #define SCALE_DIFF_ZERO 60 ///< codebook index corresponding to zero scalefactor indices difference
  131. #define NOISE_PRE 256 ///< preamble for NOISE_BT, put in bitstream with the first noise band
  132. #define NOISE_PRE_BITS 9 ///< length of preamble
  133. #define NOISE_OFFSET 90 ///< subtracted from global gain, used as offset for the preamble
  134. /**
  135. * Long Term Prediction
  136. */
  137. typedef struct LongTermPrediction {
  138. int8_t present;
  139. int16_t lag;
  140. INTFLOAT coef;
  141. int8_t used[MAX_LTP_LONG_SFB];
  142. } LongTermPrediction;
  143. /**
  144. * Individual Channel Stream
  145. */
  146. typedef struct IndividualChannelStream {
  147. uint8_t max_sfb; ///< number of scalefactor bands per group
  148. enum WindowSequence window_sequence[2];
  149. uint8_t use_kb_window[2]; ///< If set, use Kaiser-Bessel window, otherwise use a sine window.
  150. int num_window_groups;
  151. uint8_t group_len[8];
  152. LongTermPrediction ltp;
  153. const uint16_t *swb_offset; ///< table of offsets to the lowest spectral coefficient of a scalefactor band, sfb, for a particular window
  154. const uint8_t *swb_sizes; ///< table of scalefactor band sizes for a particular window
  155. int num_swb; ///< number of scalefactor window bands
  156. int num_windows;
  157. int tns_max_bands;
  158. int predictor_present;
  159. int predictor_initialized;
  160. int predictor_reset_group;
  161. uint8_t prediction_used[41];
  162. uint8_t window_clipping[8]; ///< set if a certain window is near clipping
  163. float clip_avoidance_factor; ///< set if any window is near clipping to the necessary atennuation factor to avoid it
  164. } IndividualChannelStream;
  165. /**
  166. * Temporal Noise Shaping
  167. */
  168. typedef struct TemporalNoiseShaping {
  169. int present;
  170. int n_filt[8];
  171. int length[8][4];
  172. int direction[8][4];
  173. int order[8][4];
  174. INTFLOAT coef[8][4][TNS_MAX_ORDER];
  175. } TemporalNoiseShaping;
  176. /**
  177. * Dynamic Range Control - decoded from the bitstream but not processed further.
  178. */
  179. typedef struct DynamicRangeControl {
  180. int pce_instance_tag; ///< Indicates with which program the DRC info is associated.
  181. int dyn_rng_sgn[17]; ///< DRC sign information; 0 - positive, 1 - negative
  182. int dyn_rng_ctl[17]; ///< DRC magnitude information
  183. int exclude_mask[MAX_CHANNELS]; ///< Channels to be excluded from DRC processing.
  184. int band_incr; ///< Number of DRC bands greater than 1 having DRC info.
  185. int interpolation_scheme; ///< Indicates the interpolation scheme used in the SBR QMF domain.
  186. int band_top[17]; ///< Indicates the top of the i-th DRC band in units of 4 spectral lines.
  187. int prog_ref_level; /**< A reference level for the long-term program audio level for all
  188. * channels combined.
  189. */
  190. } DynamicRangeControl;
  191. typedef struct Pulse {
  192. int num_pulse;
  193. int start;
  194. int pos[4];
  195. int amp[4];
  196. } Pulse;
  197. /**
  198. * coupling parameters
  199. */
  200. typedef struct ChannelCoupling {
  201. enum CouplingPoint coupling_point; ///< The point during decoding at which coupling is applied.
  202. int num_coupled; ///< number of target elements
  203. enum RawDataBlockType type[8]; ///< Type of channel element to be coupled - SCE or CPE.
  204. int id_select[8]; ///< element id
  205. int ch_select[8]; /**< [0] shared list of gains; [1] list of gains for right channel;
  206. * [2] list of gains for left channel; [3] lists of gains for both channels
  207. */
  208. INTFLOAT gain[16][120];
  209. } ChannelCoupling;
  210. /**
  211. * Single Channel Element - used for both SCE and LFE elements.
  212. */
  213. typedef struct SingleChannelElement {
  214. IndividualChannelStream ics;
  215. TemporalNoiseShaping tns;
  216. Pulse pulse;
  217. enum BandType band_type[128]; ///< band types
  218. int band_type_run_end[120]; ///< band type run end points
  219. INTFLOAT sf[120]; ///< scalefactors
  220. int sf_idx[128]; ///< scalefactor indices (used by encoder)
  221. uint8_t zeroes[128]; ///< band is not coded (used by encoder)
  222. float is_ener[128]; ///< Intensity stereo pos (used by encoder)
  223. float pns_ener[128]; ///< Noise energy values (used by encoder)
  224. DECLARE_ALIGNED(32, INTFLOAT, pcoeffs)[1024]; ///< coefficients for IMDCT, pristine
  225. DECLARE_ALIGNED(32, INTFLOAT, coeffs)[1024]; ///< coefficients for IMDCT, maybe processed
  226. DECLARE_ALIGNED(32, INTFLOAT, saved)[1536]; ///< overlap
  227. DECLARE_ALIGNED(32, INTFLOAT, ret_buf)[2048]; ///< PCM output buffer
  228. DECLARE_ALIGNED(16, INTFLOAT, ltp_state)[3072]; ///< time signal for LTP
  229. PredictorState predictor_state[MAX_PREDICTORS];
  230. INTFLOAT *ret; ///< PCM output
  231. } SingleChannelElement;
  232. /**
  233. * channel element - generic struct for SCE/CPE/CCE/LFE
  234. */
  235. typedef struct ChannelElement {
  236. int present;
  237. // CPE specific
  238. int common_window; ///< Set if channels share a common 'IndividualChannelStream' in bitstream.
  239. int ms_mode; ///< Signals mid/side stereo flags coding mode (used by encoder)
  240. uint8_t is_mode; ///< Set if any bands have been encoded using intensity stereo (used by encoder)
  241. uint8_t ms_mask[128]; ///< Set if mid/side stereo is used for each scalefactor window band
  242. uint8_t is_mask[128]; ///< Set if intensity stereo is used (used by encoder)
  243. // shared
  244. SingleChannelElement ch[2];
  245. // CCE specific
  246. ChannelCoupling coup;
  247. SpectralBandReplication sbr;
  248. } ChannelElement;
  249. /**
  250. * main AAC context
  251. */
  252. struct AACContext {
  253. AVClass *class;
  254. AVCodecContext *avctx;
  255. AVFrame *frame;
  256. int is_saved; ///< Set if elements have stored overlap from previous frame.
  257. DynamicRangeControl che_drc;
  258. /**
  259. * @name Channel element related data
  260. * @{
  261. */
  262. ChannelElement *che[4][MAX_ELEM_ID];
  263. ChannelElement *tag_che_map[4][MAX_ELEM_ID];
  264. int tags_mapped;
  265. int warned_remapping_once;
  266. /** @} */
  267. /**
  268. * @name temporary aligned temporary buffers
  269. * (We do not want to have these on the stack.)
  270. * @{
  271. */
  272. DECLARE_ALIGNED(32, INTFLOAT, buf_mdct)[1024];
  273. /** @} */
  274. /**
  275. * @name Computed / set up during initialization
  276. * @{
  277. */
  278. FFTContext mdct;
  279. FFTContext mdct_small;
  280. FFTContext mdct_ld;
  281. FFTContext mdct_ltp;
  282. #if USE_FIXED
  283. AVFixedDSPContext *fdsp;
  284. #else
  285. IMDCT15Context *mdct480;
  286. AVFloatDSPContext *fdsp;
  287. #endif /* USE_FIXED */
  288. int random_state;
  289. /** @} */
  290. /**
  291. * @name Members used for output
  292. * @{
  293. */
  294. SingleChannelElement *output_element[MAX_CHANNELS]; ///< Points to each SingleChannelElement
  295. /** @} */
  296. /**
  297. * @name Japanese DTV specific extension
  298. * @{
  299. */
  300. int force_dmono_mode;///< 0->not dmono, 1->use first channel, 2->use second channel
  301. int dmono_mode; ///< 0->not dmono, 1->use first channel, 2->use second channel
  302. /** @} */
  303. DECLARE_ALIGNED(32, INTFLOAT, temp)[128];
  304. OutputConfiguration oc[2];
  305. int warned_num_aac_frames;
  306. /* aacdec functions pointers */
  307. void (*imdct_and_windowing)(AACContext *ac, SingleChannelElement *sce);
  308. void (*apply_ltp)(AACContext *ac, SingleChannelElement *sce);
  309. void (*apply_tns)(INTFLOAT coef[1024], TemporalNoiseShaping *tns,
  310. IndividualChannelStream *ics, int decode);
  311. void (*windowing_and_mdct_ltp)(AACContext *ac, INTFLOAT *out,
  312. INTFLOAT *in, IndividualChannelStream *ics);
  313. void (*update_ltp)(AACContext *ac, SingleChannelElement *sce);
  314. void (*vector_pow43)(int *coefs, int len);
  315. void (*subband_scale)(int *dst, int *src, int scale, int offset, int len);
  316. };
  317. void ff_aacdec_init_mips(AACContext *c);
  318. #endif /* AVCODEC_AAC_H */