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.

258 lines
11KB

  1. /*
  2. * Copyright (C) 2016 foo86
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef AVCODEC_DCA_CORE_H
  21. #define AVCODEC_DCA_CORE_H
  22. #include "libavutil/common.h"
  23. #include "libavutil/float_dsp.h"
  24. #include "libavutil/fixed_dsp.h"
  25. #include "libavutil/mem.h"
  26. #include "avcodec.h"
  27. #include "internal.h"
  28. #include "get_bits.h"
  29. #include "dca.h"
  30. #include "dca_exss.h"
  31. #include "dcadsp.h"
  32. #include "dcadct.h"
  33. #include "dcamath.h"
  34. #include "dcahuff.h"
  35. #include "fft.h"
  36. #include "synth_filter.h"
  37. #define DCA_CHANNELS 7
  38. #define DCA_SUBBANDS 32
  39. #define DCA_SUBBANDS_X96 64
  40. #define DCA_SUBFRAMES 16
  41. #define DCA_SUBBAND_SAMPLES 8
  42. #define DCA_PCMBLOCK_SAMPLES 32
  43. #define DCA_LFE_HISTORY 8
  44. #define DCA_ABITS_MAX 26
  45. #define DCA_CORE_CHANNELS_MAX 6
  46. #define DCA_DMIX_CHANNELS_MAX 4
  47. #define DCA_XXCH_CHANNELS_MAX 2
  48. #define DCA_EXSS_CHANNELS_MAX 8
  49. #define DCA_EXSS_CHSETS_MAX 4
  50. #define DCA_FILTER_MODE_X96 0x01
  51. #define DCA_FILTER_MODE_FIXED 0x02
  52. enum DCACoreAudioMode {
  53. DCA_AMODE_MONO, // Mode 0: A (mono)
  54. DCA_AMODE_MONO_DUAL, // Mode 1: A + B (dual mono)
  55. DCA_AMODE_STEREO, // Mode 2: L + R (stereo)
  56. DCA_AMODE_STEREO_SUMDIFF, // Mode 3: (L+R) + (L-R) (sum-diff)
  57. DCA_AMODE_STEREO_TOTAL, // Mode 4: LT + RT (left and right total)
  58. DCA_AMODE_3F, // Mode 5: C + L + R
  59. DCA_AMODE_2F1R, // Mode 6: L + R + S
  60. DCA_AMODE_3F1R, // Mode 7: C + L + R + S
  61. DCA_AMODE_2F2R, // Mode 8: L + R + SL + SR
  62. DCA_AMODE_3F2R, // Mode 9: C + L + R + SL + SR
  63. DCA_AMODE_COUNT
  64. };
  65. enum DCACoreExtAudioType {
  66. DCA_EXT_AUDIO_XCH = 0,
  67. DCA_EXT_AUDIO_X96 = 2,
  68. DCA_EXT_AUDIO_XXCH = 6
  69. };
  70. enum DCACoreLFEFlag {
  71. DCA_LFE_FLAG_NONE,
  72. DCA_LFE_FLAG_128,
  73. DCA_LFE_FLAG_64,
  74. DCA_LFE_FLAG_INVALID
  75. };
  76. typedef struct DCADSPData {
  77. union {
  78. struct {
  79. DECLARE_ALIGNED(32, float, hist1)[1024];
  80. DECLARE_ALIGNED(32, float, hist2)[64];
  81. } flt;
  82. struct {
  83. DECLARE_ALIGNED(32, int32_t, hist1)[1024];
  84. DECLARE_ALIGNED(32, int32_t, hist2)[64];
  85. } fix;
  86. } u;
  87. int offset;
  88. } DCADSPData;
  89. typedef struct DCACoreDecoder {
  90. AVCodecContext *avctx;
  91. GetBitContext gb;
  92. // Bit stream header
  93. int crc_present; ///< CRC present flag
  94. int npcmblocks; ///< Number of PCM sample blocks
  95. int frame_size; ///< Primary frame byte size
  96. int audio_mode; ///< Audio channel arrangement
  97. int sample_rate; ///< Core audio sampling frequency
  98. int bit_rate; ///< Transmission bit rate
  99. int drc_present; ///< Embedded dynamic range flag
  100. int ts_present; ///< Embedded time stamp flag
  101. int aux_present; ///< Auxiliary data flag
  102. int ext_audio_type; ///< Extension audio descriptor flag
  103. int ext_audio_present; ///< Extended coding flag
  104. int sync_ssf; ///< Audio sync word insertion flag
  105. int lfe_present; ///< Low frequency effects flag
  106. int predictor_history; ///< Predictor history flag switch
  107. int filter_perfect; ///< Multirate interpolator switch
  108. int source_pcm_res; ///< Source PCM resolution
  109. int es_format; ///< Extended surround (ES) mastering flag
  110. int sumdiff_front; ///< Front sum/difference flag
  111. int sumdiff_surround; ///< Surround sum/difference flag
  112. // Primary audio coding header
  113. int nsubframes; ///< Number of subframes
  114. int nchannels; ///< Number of primary audio channels (incl. extension channels)
  115. int ch_mask; ///< Speaker layout mask (incl. LFE and extension channels)
  116. int8_t nsubbands[DCA_CHANNELS]; ///< Subband activity count
  117. int8_t subband_vq_start[DCA_CHANNELS]; ///< High frequency VQ start subband
  118. int8_t joint_intensity_index[DCA_CHANNELS]; ///< Joint intensity coding index
  119. int8_t transition_mode_sel[DCA_CHANNELS]; ///< Transient mode code book
  120. int8_t scale_factor_sel[DCA_CHANNELS]; ///< Scale factor code book
  121. int8_t bit_allocation_sel[DCA_CHANNELS]; ///< Bit allocation quantizer select
  122. int8_t quant_index_sel[DCA_CHANNELS][DCA_CODE_BOOKS]; ///< Quantization index codebook select
  123. int32_t scale_factor_adj[DCA_CHANNELS][DCA_CODE_BOOKS]; ///< Scale factor adjustment
  124. // Primary audio coding side information
  125. int8_t nsubsubframes[DCA_SUBFRAMES]; ///< Subsubframe count for each subframe
  126. int8_t prediction_mode[DCA_CHANNELS][DCA_SUBBANDS_X96]; ///< Prediction mode
  127. int16_t prediction_vq_index[DCA_CHANNELS][DCA_SUBBANDS_X96]; ///< Prediction coefficients VQ address
  128. int8_t bit_allocation[DCA_CHANNELS][DCA_SUBBANDS_X96]; ///< Bit allocation index
  129. int8_t transition_mode[DCA_SUBFRAMES][DCA_CHANNELS][DCA_SUBBANDS]; ///< Transition mode
  130. int32_t scale_factors[DCA_CHANNELS][DCA_SUBBANDS][2]; ///< Scale factors (2x for transients and X96)
  131. int8_t joint_scale_sel[DCA_CHANNELS]; ///< Joint subband codebook select
  132. int32_t joint_scale_factors[DCA_CHANNELS][DCA_SUBBANDS_X96]; ///< Scale factors for joint subband coding
  133. // Auxiliary data
  134. int prim_dmix_embedded; ///< Auxiliary dynamic downmix flag
  135. int prim_dmix_type; ///< Auxiliary primary channel downmix type
  136. int prim_dmix_coeff[DCA_DMIX_CHANNELS_MAX * DCA_CORE_CHANNELS_MAX]; ///< Dynamic downmix code coefficients
  137. // Core extensions
  138. int ext_audio_mask; ///< Bit mask of fully decoded core extensions
  139. // XCH extension data
  140. int xch_pos; ///< Bit position of XCH frame in core substream
  141. // XXCH extension data
  142. int xxch_crc_present; ///< CRC presence flag for XXCH channel set header
  143. int xxch_mask_nbits; ///< Number of bits for loudspeaker mask
  144. int xxch_core_mask; ///< Core loudspeaker activity mask
  145. int xxch_spkr_mask; ///< Loudspeaker layout mask
  146. int xxch_dmix_embedded; ///< Downmix already performed by encoder
  147. int xxch_dmix_scale_inv; ///< Downmix scale factor
  148. int xxch_dmix_mask[DCA_XXCH_CHANNELS_MAX]; ///< Downmix channel mapping mask
  149. int xxch_dmix_coeff[DCA_XXCH_CHANNELS_MAX * DCA_CORE_CHANNELS_MAX]; ///< Downmix coefficients
  150. int xxch_pos; ///< Bit position of XXCH frame in core substream
  151. // X96 extension data
  152. int x96_rev_no; ///< X96 revision number
  153. int x96_crc_present; ///< CRC presence flag for X96 channel set header
  154. int x96_nchannels; ///< Number of primary channels in X96 extension
  155. int x96_high_res; ///< X96 high resolution flag
  156. int x96_subband_start; ///< First encoded subband in X96 extension
  157. int x96_rand; ///< Random seed for generating samples for unallocated X96 subbands
  158. int x96_pos; ///< Bit position of X96 frame in core substream
  159. // Sample buffers
  160. unsigned int x96_subband_size;
  161. int32_t *x96_subband_buffer; ///< X96 subband sample buffer base
  162. int32_t *x96_subband_samples[DCA_CHANNELS][DCA_SUBBANDS_X96]; ///< X96 subband samples
  163. unsigned int subband_size;
  164. int32_t *subband_buffer; ///< Subband sample buffer base
  165. int32_t *subband_samples[DCA_CHANNELS][DCA_SUBBANDS]; ///< Subband samples
  166. int32_t *lfe_samples; ///< Decimated LFE samples
  167. // DSP contexts
  168. DCADSPData dcadsp_data[DCA_CHANNELS]; ///< FIR history buffers
  169. DCADSPContext *dcadsp;
  170. DCADCTContext dcadct;
  171. FFTContext imdct[2];
  172. SynthFilterContext synth;
  173. AVFloatDSPContext *float_dsp;
  174. AVFixedDSPContext *fixed_dsp;
  175. // PCM output data
  176. unsigned int output_size;
  177. void *output_buffer; ///< PCM output buffer base
  178. int32_t *output_samples[DCA_SPEAKER_COUNT]; ///< PCM output for fixed point mode
  179. int32_t output_history_lfe_fixed; ///< LFE PCM history for X96 filter
  180. float output_history_lfe_float; ///< LFE PCM history for X96 filter
  181. int ch_remap[DCA_SPEAKER_COUNT]; ///< Channel to speaker map
  182. int request_mask; ///< Requested channel layout (for stereo downmix)
  183. int npcmsamples; ///< Number of PCM samples per channel
  184. int output_rate; ///< Output sample rate (1x or 2x header rate)
  185. int filter_mode; ///< Previous filtering mode for detecting changes
  186. } DCACoreDecoder;
  187. static inline int ff_dca_core_map_spkr(DCACoreDecoder *core, int spkr)
  188. {
  189. if (core->ch_mask & (1U << spkr))
  190. return spkr;
  191. if (spkr == DCA_SPEAKER_Lss && (core->ch_mask & DCA_SPEAKER_MASK_Ls))
  192. return DCA_SPEAKER_Ls;
  193. if (spkr == DCA_SPEAKER_Rss && (core->ch_mask & DCA_SPEAKER_MASK_Rs))
  194. return DCA_SPEAKER_Rs;
  195. return -1;
  196. }
  197. static inline void ff_dca_core_dequantize(int32_t *output, const int32_t *input,
  198. int32_t step_size, int32_t scale, int residual, int len)
  199. {
  200. // Account for quantizer step size
  201. int64_t step_scale = (int64_t)step_size * scale;
  202. int n, shift = 0;
  203. // Limit scale factor resolution to 22 bits
  204. if (step_scale > (1 << 23)) {
  205. shift = av_log2(step_scale >> 23) + 1;
  206. step_scale >>= shift;
  207. }
  208. // Scale the samples
  209. if (residual) {
  210. for (n = 0; n < len; n++)
  211. output[n] += clip23(norm__(input[n] * step_scale, 22 - shift));
  212. } else {
  213. for (n = 0; n < len; n++)
  214. output[n] = clip23(norm__(input[n] * step_scale, 22 - shift));
  215. }
  216. }
  217. int ff_dca_core_parse(DCACoreDecoder *s, uint8_t *data, int size);
  218. int ff_dca_core_parse_exss(DCACoreDecoder *s, uint8_t *data, DCAExssAsset *asset);
  219. int ff_dca_core_filter_fixed(DCACoreDecoder *s, int x96_synth);
  220. int ff_dca_core_filter_frame(DCACoreDecoder *s, AVFrame *frame);
  221. av_cold void ff_dca_core_flush(DCACoreDecoder *s);
  222. av_cold int ff_dca_core_init(DCACoreDecoder *s);
  223. av_cold void ff_dca_core_close(DCACoreDecoder *s);
  224. #endif