atrac3plus.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * ATRAC3+ compatible decoder
  3. *
  4. * Copyright (c) 2010-2013 Maxim Poliakovski
  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. * Global structures, constants and data for ATRAC3+ decoder.
  25. */
  26. #ifndef AVCODEC_ATRAC3PLUS_H
  27. #define AVCODEC_ATRAC3PLUS_H
  28. #include <stdint.h>
  29. #include "libavutil/float_dsp.h"
  30. #include "atrac.h"
  31. #include "avcodec.h"
  32. #include "fft.h"
  33. #include "get_bits.h"
  34. /** Global unit sizes */
  35. #define ATRAC3P_SUBBANDS 16 ///< number of PQF subbands
  36. #define ATRAC3P_SUBBAND_SAMPLES 128 ///< number of samples per subband
  37. #define ATRAC3P_FRAME_SAMPLES (ATRAC3P_SUBBAND_SAMPLES * ATRAC3P_SUBBANDS)
  38. #define ATRAC3P_PQF_FIR_LEN 12 ///< length of the prototype FIR of the PQF
  39. /** Global constants */
  40. #define ATRAC3P_POWER_COMP_OFF 15 ///< disable power compensation
  41. /** ATRAC3+ channel unit types */
  42. enum Atrac3pChannelUnitTypes {
  43. CH_UNIT_MONO = 0, ///< unit containing one coded channel
  44. CH_UNIT_STEREO = 1, ///< unit containing two jointly-coded channels
  45. CH_UNIT_EXTENSION = 2, ///< unit containing extension information
  46. CH_UNIT_TERMINATOR = 3 ///< unit sequence terminator
  47. };
  48. /** Per-channel IPQF history */
  49. typedef struct Atrac3pIPQFChannelCtx {
  50. DECLARE_ALIGNED(32, float, buf1)[ATRAC3P_PQF_FIR_LEN * 2][8];
  51. DECLARE_ALIGNED(32, float, buf2)[ATRAC3P_PQF_FIR_LEN * 2][8];
  52. int pos;
  53. } Atrac3pIPQFChannelCtx;
  54. /** Amplitude envelope of a group of sine waves */
  55. typedef struct Atrac3pWaveEnvelope {
  56. int has_start_point; ///< indicates start point within the GHA window
  57. int has_stop_point; ///< indicates stop point within the GHA window
  58. int start_pos; ///< start position expressed in n*4 samples
  59. int stop_pos; ///< stop position expressed in n*4 samples
  60. } Atrac3pWaveEnvelope;
  61. /** Parameters of a group of sine waves */
  62. typedef struct Atrac3pWavesData {
  63. Atrac3pWaveEnvelope pend_env; ///< pending envelope from the previous frame
  64. Atrac3pWaveEnvelope curr_env; ///< group envelope from the current frame
  65. int num_wavs; ///< number of sine waves in the group
  66. int start_index; ///< start index into global tones table for that subband
  67. } Atrac3pWavesData;
  68. /** Parameters of a single sine wave */
  69. typedef struct Atrac3pWaveParam {
  70. int freq_index; ///< wave frequency index
  71. int amp_sf; ///< quantized amplitude scale factor
  72. int amp_index; ///< quantized amplitude index
  73. int phase_index; ///< quantized phase index
  74. } Atrac3pWaveParam;
  75. /** Sound channel parameters */
  76. typedef struct Atrac3pChanParams {
  77. int ch_num;
  78. int num_coded_vals; ///< number of transmitted quant unit values
  79. int fill_mode;
  80. int split_point;
  81. int table_type; ///< table type: 0 - tone?, 1- noise?
  82. int qu_wordlen[32]; ///< array of word lengths for each quant unit
  83. int qu_sf_idx[32]; ///< array of scale factor indexes for each quant unit
  84. int qu_tab_idx[32]; ///< array of code table indexes for each quant unit
  85. int16_t spectrum[2048]; ///< decoded IMDCT spectrum
  86. uint8_t power_levs[5]; ///< power compensation levels
  87. /* imdct window shape history (2 frames) for overlapping. */
  88. uint8_t wnd_shape_hist[2][ATRAC3P_SUBBANDS]; ///< IMDCT window shape, 0=sine/1=steep
  89. uint8_t *wnd_shape; ///< IMDCT window shape for current frame
  90. uint8_t *wnd_shape_prev; ///< IMDCT window shape for previous frame
  91. /* gain control data history (2 frames) for overlapping. */
  92. AtracGainInfo gain_data_hist[2][ATRAC3P_SUBBANDS]; ///< gain control data for all subbands
  93. AtracGainInfo *gain_data; ///< gain control data for next frame
  94. AtracGainInfo *gain_data_prev; ///< gain control data for previous frame
  95. int num_gain_subbands; ///< number of subbands with gain control data
  96. /* tones data history (2 frames) for overlapping. */
  97. Atrac3pWavesData tones_info_hist[2][ATRAC3P_SUBBANDS];
  98. Atrac3pWavesData *tones_info;
  99. Atrac3pWavesData *tones_info_prev;
  100. } Atrac3pChanParams;
  101. /* Per-unit sine wave parameters */
  102. typedef struct Atrac3pWaveSynthParams {
  103. int tones_present; ///< 1 - tones info present
  104. int amplitude_mode; ///< 1 - low range, 0 - high range
  105. int num_tone_bands; ///< number of PQF bands with tones
  106. uint8_t tone_sharing[ATRAC3P_SUBBANDS]; ///< 1 - subband-wise tone sharing flags
  107. uint8_t tone_master[ATRAC3P_SUBBANDS]; ///< 1 - subband-wise tone channel swapping
  108. uint8_t invert_phase[ATRAC3P_SUBBANDS]; ///< 1 - subband-wise phase inversion
  109. int tones_index; ///< total sum of tones in this unit
  110. Atrac3pWaveParam waves[48];
  111. } Atrac3pWaveSynthParams;
  112. /** Channel unit parameters */
  113. typedef struct Atrac3pChanUnitCtx {
  114. /* channel unit variables */
  115. int unit_type; ///< unit type (mono/stereo)
  116. int num_quant_units;
  117. int num_subbands;
  118. int used_quant_units; ///< number of quant units with coded spectrum
  119. int num_coded_subbands; ///< number of subbands with coded spectrum
  120. int mute_flag; ///< mute flag
  121. int use_full_table; ///< 1 - full table list, 0 - restricted one
  122. int noise_present; ///< 1 - global noise info present
  123. int noise_level_index; ///< global noise level index
  124. int noise_table_index; ///< global noise RNG table index
  125. uint8_t swap_channels[ATRAC3P_SUBBANDS]; ///< 1 - perform subband-wise channel swapping
  126. uint8_t negate_coeffs[ATRAC3P_SUBBANDS]; ///< 1 - subband-wise IMDCT coefficients negation
  127. Atrac3pChanParams channels[2];
  128. /* Variables related to GHA tones */
  129. Atrac3pWaveSynthParams wave_synth_hist[2]; ///< waves synth history for two frames
  130. Atrac3pWaveSynthParams *waves_info;
  131. Atrac3pWaveSynthParams *waves_info_prev;
  132. Atrac3pIPQFChannelCtx ipqf_ctx[2];
  133. DECLARE_ALIGNED(32, float, prev_buf)[2][ATRAC3P_FRAME_SAMPLES]; ///< overlapping buffer
  134. } Atrac3pChanUnitCtx;
  135. /**
  136. * Initialize VLC tables for bitstream parsing.
  137. */
  138. void ff_atrac3p_init_vlcs(void);
  139. /**
  140. * Decode bitstream data of a channel unit.
  141. *
  142. * @param[in] gb the GetBit context
  143. * @param[in,out] ctx ptr to the channel unit context
  144. * @param[in] num_channels number of channels to process
  145. * @param[in] avctx ptr to the AVCodecContext
  146. * @return result code: 0 = OK, otherwise - error code
  147. */
  148. int ff_atrac3p_decode_channel_unit(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  149. int num_channels, AVCodecContext *avctx);
  150. /**
  151. * Initialize IMDCT transform.
  152. *
  153. * @param[in] avctx ptr to the AVCodecContext
  154. * @param[in] mdct_ctx pointer to MDCT transform context
  155. */
  156. void ff_atrac3p_init_imdct(AVCodecContext *avctx, FFTContext *mdct_ctx);
  157. /**
  158. * Initialize sine waves synthesizer.
  159. */
  160. void ff_atrac3p_init_wave_synth(void);
  161. /**
  162. * Synthesize sine waves for a particular subband.
  163. *
  164. * @param[in] ch_unit pointer to the channel unit context
  165. * @param[in] fdsp pointer to float DSP context
  166. * @param[in] ch_num which channel to process
  167. * @param[in] sb which subband to process
  168. * @param[out] out receives processed data
  169. */
  170. void ff_atrac3p_generate_tones(Atrac3pChanUnitCtx *ch_unit, AVFloatDSPContext *fdsp,
  171. int ch_num, int sb, float *out);
  172. /**
  173. * Perform power compensation aka noise dithering.
  174. *
  175. * @param[in] ctx ptr to the channel context
  176. * @param[in] fdsp pointer to float DSP context
  177. * @param[in] ch_index which channel to process
  178. * @param[in,out] sp ptr to channel spectrum to process
  179. * @param[in] rng_index indicates which RNG table to use
  180. * @param[in] sb_num which subband to process
  181. */
  182. void ff_atrac3p_power_compensation(Atrac3pChanUnitCtx *ctx, AVFloatDSPContext *fdsp,
  183. int ch_index, float *sp, int rng_index, int sb_num);
  184. /**
  185. * Regular IMDCT and windowing without overlapping,
  186. * with spectrum reversal in the odd subbands.
  187. *
  188. * @param[in] fdsp pointer to float DSP context
  189. * @param[in] mdct_ctx pointer to MDCT transform context
  190. * @param[in] pIn float input
  191. * @param[out] pOut float output
  192. * @param[in] wind_id which MDCT window to apply
  193. * @param[in] sb subband number
  194. */
  195. void ff_atrac3p_imdct(AVFloatDSPContext *fdsp, FFTContext *mdct_ctx, float *pIn,
  196. float *pOut, int wind_id, int sb);
  197. /**
  198. * Subband synthesis filter based on the polyphase quadrature (pseudo-QMF)
  199. * filter bank.
  200. *
  201. * @param[in] dct_ctx ptr to the pre-initialized IDCT context
  202. * @param[in,out] hist ptr to the filter history
  203. * @param[in] in input data to process
  204. * @param[out] out receives processed data
  205. */
  206. void ff_atrac3p_ipqf(FFTContext *dct_ctx, Atrac3pIPQFChannelCtx *hist,
  207. const float *in, float *out);
  208. extern const uint16_t ff_atrac3p_qu_to_spec_pos[33];
  209. extern const float ff_atrac3p_sf_tab[64];
  210. extern const float ff_atrac3p_mant_tab[8];
  211. #endif /* AVCODEC_ATRAC3PLUS_H */