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.

307 lines
14KB

  1. /*
  2. * DCA compatible decoder
  3. * Copyright (C) 2004 Gildas Bazin
  4. * Copyright (C) 2004 Benjamin Zores
  5. * Copyright (C) 2006 Benjamin Larsson
  6. * Copyright (C) 2007 Konstantin Shishkov
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #ifndef AVCODEC_DCA_H
  25. #define AVCODEC_DCA_H
  26. #include <stdint.h>
  27. #include "libavutil/float_dsp.h"
  28. #include "libavutil/internal.h"
  29. #include "avcodec.h"
  30. #include "dcadsp.h"
  31. #include "fmtconvert.h"
  32. #include "get_bits.h"
  33. #define DCA_PRIM_CHANNELS_MAX (7)
  34. #define DCA_ABITS_MAX (32) /* Should be 28 */
  35. #define DCA_SUBSUBFRAMES_MAX (4)
  36. #define DCA_SUBFRAMES_MAX (16)
  37. #define DCA_BLOCKS_MAX (16)
  38. #define DCA_LFE_MAX (3)
  39. #define DCA_CHSETS_MAX (4)
  40. #define DCA_CHSET_CHANS_MAX (8)
  41. #define DCA_PRIM_CHANNELS_MAX (7)
  42. #define DCA_ABITS_MAX (32) /* Should be 28 */
  43. #define DCA_SUBSUBFRAMES_MAX (4)
  44. #define DCA_SUBFRAMES_MAX (16)
  45. #define DCA_BLOCKS_MAX (16)
  46. #define DCA_LFE_MAX (3)
  47. #define DCA_XLL_FBANDS_MAX (4)
  48. #define DCA_XLL_SEGMENTS_MAX (16)
  49. #define DCA_XLL_CHSETS_MAX (16)
  50. #define DCA_XLL_CHANNELS_MAX (16)
  51. #define DCA_XLL_AORDER_MAX (15)
  52. /* Arbitrary limit; not sure what the maximum really is, but much larger. */
  53. #define DCA_XLL_DMIX_NCOEFFS_MAX (18)
  54. #define DCA_MAX_FRAME_SIZE 16384
  55. #define DCA_MAX_EXSS_HEADER_SIZE 4096
  56. #define DCA_BUFFER_PADDING_SIZE 1024
  57. enum DCAExtensionMask {
  58. DCA_EXT_CORE = 0x001, ///< core in core substream
  59. DCA_EXT_XXCH = 0x002, ///< XXCh channels extension in core substream
  60. DCA_EXT_X96 = 0x004, ///< 96/24 extension in core substream
  61. DCA_EXT_XCH = 0x008, ///< XCh channel extension in core substream
  62. DCA_EXT_EXSS_CORE = 0x010, ///< core in ExSS (extension substream)
  63. DCA_EXT_EXSS_XBR = 0x020, ///< extended bitrate extension in ExSS
  64. DCA_EXT_EXSS_XXCH = 0x040, ///< XXCh channels extension in ExSS
  65. DCA_EXT_EXSS_X96 = 0x080, ///< 96/24 extension in ExSS
  66. DCA_EXT_EXSS_LBR = 0x100, ///< low bitrate component in ExSS
  67. DCA_EXT_EXSS_XLL = 0x200, ///< lossless extension in ExSS
  68. };
  69. typedef struct XllChSetSubHeader {
  70. int channels; ///< number of channels in channel set, at most 16
  71. int residual_encode; ///< residual channel encoding
  72. int bit_resolution; ///< input sample bit-width
  73. int bit_width; ///< original input sample bit-width
  74. int sampling_frequency; ///< sampling frequency
  75. int samp_freq_interp; ///< sampling frequency interpolation multiplier
  76. int replacement_set; ///< replacement channel set group
  77. int active_replace_set; ///< current channel set is active channel set
  78. int primary_ch_set;
  79. int downmix_coeff_code_embedded;
  80. int downmix_embedded;
  81. int downmix_type;
  82. int hier_chset; ///< hierarchical channel set
  83. int downmix_ncoeffs;
  84. int downmix_coeffs[DCA_XLL_DMIX_NCOEFFS_MAX];
  85. int ch_mask_enabled;
  86. int ch_mask;
  87. int mapping_coeffs_present;
  88. int num_freq_bands;
  89. /* m_nOrigChanOrder */
  90. uint8_t orig_chan_order[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX];
  91. uint8_t orig_chan_order_inv[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX];
  92. /* Coefficients for channel pairs (at most 8), m_anPWChPairsCoeffs */
  93. int8_t pw_ch_pairs_coeffs[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX/2];
  94. /* m_nCurrHighestLPCOrder */
  95. uint8_t adapt_order_max[DCA_XLL_FBANDS_MAX];
  96. /* m_pnAdaptPredOrder */
  97. uint8_t adapt_order[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX];
  98. /* m_pnFixedPredOrder */
  99. uint8_t fixed_order[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX];
  100. /* m_pnLPCReflCoeffsQInd, unsigned version */
  101. uint8_t lpc_refl_coeffs_q_ind[DCA_XLL_FBANDS_MAX]
  102. [DCA_XLL_CHANNELS_MAX][DCA_XLL_AORDER_MAX];
  103. int lsb_fsize[DCA_XLL_FBANDS_MAX];
  104. int8_t scalable_lsbs[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX];
  105. int8_t bit_width_adj_per_ch[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX];
  106. } XllChSetSubHeader;
  107. typedef struct XllNavi {
  108. GetBitContext gb; // Context for parsing the data segments
  109. unsigned band_size[DCA_XLL_FBANDS_MAX];
  110. unsigned segment_size[DCA_XLL_FBANDS_MAX][DCA_XLL_SEGMENTS_MAX];
  111. unsigned chset_size[DCA_XLL_FBANDS_MAX][DCA_XLL_SEGMENTS_MAX][DCA_XLL_CHSETS_MAX];
  112. } XllNavi;
  113. typedef struct QMF64_table {
  114. float dct4_coeff[32][32];
  115. float dct2_coeff[32][32];
  116. float rcos[32];
  117. float rsin[32];
  118. } QMF64_table;
  119. typedef struct DCAContext {
  120. const AVClass *class; ///< class for AVOptions
  121. AVCodecContext *avctx;
  122. /* Frame header */
  123. int frame_type; ///< type of the current frame
  124. int samples_deficit; ///< deficit sample count
  125. int crc_present; ///< crc is present in the bitstream
  126. int sample_blocks; ///< number of PCM sample blocks
  127. int frame_size; ///< primary frame byte size
  128. int amode; ///< audio channels arrangement
  129. int sample_rate; ///< audio sampling rate
  130. int bit_rate; ///< transmission bit rate
  131. int bit_rate_index; ///< transmission bit rate index
  132. int dynrange; ///< embedded dynamic range flag
  133. int timestamp; ///< embedded time stamp flag
  134. int aux_data; ///< auxiliary data flag
  135. int hdcd; ///< source material is mastered in HDCD
  136. int ext_descr; ///< extension audio descriptor flag
  137. int ext_coding; ///< extended coding flag
  138. int aspf; ///< audio sync word insertion flag
  139. int lfe; ///< low frequency effects flag
  140. int predictor_history; ///< predictor history flag
  141. int header_crc; ///< header crc check bytes
  142. int multirate_inter; ///< multirate interpolator switch
  143. int version; ///< encoder software revision
  144. int copy_history; ///< copy history
  145. int source_pcm_res; ///< source pcm resolution
  146. int front_sum; ///< front sum/difference flag
  147. int surround_sum; ///< surround sum/difference flag
  148. int dialog_norm; ///< dialog normalisation parameter
  149. /* Primary audio coding header */
  150. int subframes; ///< number of subframes
  151. int total_channels; ///< number of channels including extensions
  152. int prim_channels; ///< number of primary audio channels
  153. int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
  154. int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
  155. int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
  156. int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
  157. int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
  158. int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
  159. int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
  160. float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
  161. /* Primary audio coding side information */
  162. int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes
  163. int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count
  164. int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
  165. int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
  166. int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
  167. int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
  168. int32_t scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2];///< scale factors (2 if transient)
  169. int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
  170. int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
  171. float downmix_coef[DCA_PRIM_CHANNELS_MAX + 1][2]; ///< stereo downmix coefficients
  172. int dynrange_coef; ///< dynamic range coefficient
  173. /* Core substream's embedded downmix coefficients (cf. ETSI TS 102 114 V1.4.1)
  174. * Input: primary audio channels (incl. LFE if present)
  175. * Output: downmix audio channels (up to 4, no LFE) */
  176. uint8_t core_downmix; ///< embedded downmix coefficients available
  177. uint8_t core_downmix_amode; ///< audio channel arrangement of embedded downmix
  178. uint16_t core_downmix_codes[DCA_PRIM_CHANNELS_MAX + 1][4]; ///< embedded downmix coefficients (9-bit codes)
  179. int32_t high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
  180. float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
  181. int lfe_scale_factor;
  182. /* Subband samples history (for ADPCM) */
  183. DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
  184. /* Half size is sufficient for core decoding, but for 96 kHz data
  185. * we need QMF with 64 subbands and 1024 samples. */
  186. DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][1024];
  187. DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][64];
  188. int hist_index[DCA_PRIM_CHANNELS_MAX];
  189. DECLARE_ALIGNED(32, float, raXin)[32];
  190. int output; ///< type of output
  191. DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
  192. float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
  193. float *extra_channels[DCA_PRIM_CHANNELS_MAX + 1];
  194. uint8_t *extra_channels_buffer;
  195. unsigned int extra_channels_buffer_size;
  196. uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
  197. int dca_buffer_size; ///< how much data is in the dca_buffer
  198. const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
  199. GetBitContext gb;
  200. /* Current position in DCA frame */
  201. int current_subframe;
  202. int current_subsubframe;
  203. int core_ext_mask; ///< present extensions in the core substream
  204. int exss_ext_mask; ///< Non-core extensions
  205. /* XCh extension information */
  206. int xch_present; ///< XCh extension present and valid
  207. int xch_base_channel; ///< index of first (only) channel containing XCH data
  208. int xch_disable; ///< whether the XCh extension should be decoded or not
  209. /* XXCH extension information */
  210. int xxch_chset;
  211. int xxch_nbits_spk_mask;
  212. uint32_t xxch_core_spkmask;
  213. uint32_t xxch_spk_masks[4]; /* speaker masks, last element is core mask */
  214. int xxch_chset_nch[4];
  215. float xxch_dmix_sf[DCA_CHSETS_MAX];
  216. uint32_t xxch_dmix_embedded; /* lower layer has mix pre-embedded, per chset */
  217. float xxch_dmix_coeff[DCA_PRIM_CHANNELS_MAX][32]; /* worst case sizing */
  218. int8_t xxch_order_tab[32];
  219. int8_t lfe_index;
  220. /* XLL extension information */
  221. int xll_disable;
  222. int xll_nch_sets; ///< number of channel sets per frame
  223. int xll_channels; ///< total number of channels (in all channel sets)
  224. int xll_residual_channels; ///< number of residual channels
  225. int xll_segments; ///< number of segments per frame
  226. int xll_log_smpl_in_seg; ///< supposedly this is "nBits4SamplLoci"
  227. int xll_smpl_in_seg; ///< samples in segment per one frequency band for the first channel set
  228. int xll_bits4seg_size; ///< number of bits used to read segment size
  229. int xll_banddata_crc; ///< presence of CRC16 within each frequency band
  230. int xll_scalable_lsb;
  231. int xll_bits4ch_mask; ///< channel position mask
  232. int xll_fixed_lsb_width;
  233. XllChSetSubHeader xll_chsets[DCA_XLL_CHSETS_MAX];
  234. XllNavi xll_navi;
  235. int *xll_sample_buf;
  236. unsigned int xll_sample_buf_size;
  237. /* ExSS header parser */
  238. int static_fields; ///< static fields present
  239. int mix_metadata; ///< mixing metadata present
  240. int num_mix_configs; ///< number of mix out configurations
  241. int mix_config_num_ch[4]; ///< number of channels in each mix out configuration
  242. int profile;
  243. int one2one_map_chtospkr;
  244. int debug_flag; ///< used for suppressing repeated error messages output
  245. AVFloatDSPContext *fdsp;
  246. FFTContext imdct;
  247. SynthFilterContext synth;
  248. DCADSPContext dcadsp;
  249. QMF64_table *qmf64_table;
  250. FmtConvertContext fmt_conv;
  251. } DCAContext;
  252. extern av_export const uint32_t avpriv_dca_sample_rates[16];
  253. /**
  254. * Convert bitstream to one representation based on sync marker
  255. */
  256. int avpriv_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
  257. int max_size);
  258. int ff_dca_xbr_parse_frame(DCAContext *s);
  259. int ff_dca_xxch_decode_frame(DCAContext *s);
  260. void ff_dca_exss_parse_header(DCAContext *s);
  261. int ff_dca_xll_decode_header(DCAContext *s);
  262. int ff_dca_xll_decode_navi(DCAContext *s, int asset_end);
  263. int ff_dca_xll_decode_audio(DCAContext *s, AVFrame *frame);
  264. #endif /* AVCODEC_DCA_H */