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.

230 lines
10KB

  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. typedef struct DCADSPData {
  53. union {
  54. struct {
  55. DECLARE_ALIGNED(32, float, hist1)[1024];
  56. DECLARE_ALIGNED(32, float, hist2)[64];
  57. } flt;
  58. struct {
  59. DECLARE_ALIGNED(32, int32_t, hist1)[1024];
  60. DECLARE_ALIGNED(32, int32_t, hist2)[64];
  61. } fix;
  62. } u;
  63. int offset;
  64. } DCADSPData;
  65. typedef struct DCACoreDecoder {
  66. AVCodecContext *avctx;
  67. GetBitContext gb;
  68. // Bit stream header
  69. int crc_present; ///< CRC present flag
  70. int npcmblocks; ///< Number of PCM sample blocks
  71. int frame_size; ///< Primary frame byte size
  72. int audio_mode; ///< Audio channel arrangement
  73. int sample_rate; ///< Core audio sampling frequency
  74. int bit_rate; ///< Transmission bit rate
  75. int drc_present; ///< Embedded dynamic range flag
  76. int ts_present; ///< Embedded time stamp flag
  77. int aux_present; ///< Auxiliary data flag
  78. int ext_audio_type; ///< Extension audio descriptor flag
  79. int ext_audio_present; ///< Extended coding flag
  80. int sync_ssf; ///< Audio sync word insertion flag
  81. int lfe_present; ///< Low frequency effects flag
  82. int predictor_history; ///< Predictor history flag switch
  83. int filter_perfect; ///< Multirate interpolator switch
  84. int source_pcm_res; ///< Source PCM resolution
  85. int es_format; ///< Extended surround (ES) mastering flag
  86. int sumdiff_front; ///< Front sum/difference flag
  87. int sumdiff_surround; ///< Surround sum/difference flag
  88. // Primary audio coding header
  89. int nsubframes; ///< Number of subframes
  90. int nchannels; ///< Number of primary audio channels (incl. extension channels)
  91. int ch_mask; ///< Speaker layout mask (incl. LFE and extension channels)
  92. int8_t nsubbands[DCA_CHANNELS]; ///< Subband activity count
  93. int8_t subband_vq_start[DCA_CHANNELS]; ///< High frequency VQ start subband
  94. int8_t joint_intensity_index[DCA_CHANNELS]; ///< Joint intensity coding index
  95. int8_t transition_mode_sel[DCA_CHANNELS]; ///< Transient mode code book
  96. int8_t scale_factor_sel[DCA_CHANNELS]; ///< Scale factor code book
  97. int8_t bit_allocation_sel[DCA_CHANNELS]; ///< Bit allocation quantizer select
  98. int8_t quant_index_sel[DCA_CHANNELS][DCA_CODE_BOOKS]; ///< Quantization index codebook select
  99. int32_t scale_factor_adj[DCA_CHANNELS][DCA_CODE_BOOKS]; ///< Scale factor adjustment
  100. // Primary audio coding side information
  101. int8_t nsubsubframes[DCA_SUBFRAMES]; ///< Subsubframe count for each subframe
  102. int8_t prediction_mode[DCA_CHANNELS][DCA_SUBBANDS_X96]; ///< Prediction mode
  103. int16_t prediction_vq_index[DCA_CHANNELS][DCA_SUBBANDS_X96]; ///< Prediction coefficients VQ address
  104. int8_t bit_allocation[DCA_CHANNELS][DCA_SUBBANDS_X96]; ///< Bit allocation index
  105. int8_t transition_mode[DCA_SUBFRAMES][DCA_CHANNELS][DCA_SUBBANDS]; ///< Transition mode
  106. int32_t scale_factors[DCA_CHANNELS][DCA_SUBBANDS][2]; ///< Scale factors (2x for transients and X96)
  107. int8_t joint_scale_sel[DCA_CHANNELS]; ///< Joint subband codebook select
  108. int32_t joint_scale_factors[DCA_CHANNELS][DCA_SUBBANDS_X96]; ///< Scale factors for joint subband coding
  109. // Auxiliary data
  110. int prim_dmix_embedded; ///< Auxiliary dynamic downmix flag
  111. int prim_dmix_type; ///< Auxiliary primary channel downmix type
  112. int prim_dmix_coeff[DCA_DMIX_CHANNELS_MAX * DCA_CORE_CHANNELS_MAX]; ///< Dynamic downmix code coefficients
  113. // Core extensions
  114. int ext_audio_mask; ///< Bit mask of fully decoded core extensions
  115. // XCH extension data
  116. int xch_pos; ///< Bit position of XCH frame in core substream
  117. // XXCH extension data
  118. int xxch_crc_present; ///< CRC presence flag for XXCH channel set header
  119. int xxch_mask_nbits; ///< Number of bits for loudspeaker mask
  120. int xxch_core_mask; ///< Core loudspeaker activity mask
  121. int xxch_spkr_mask; ///< Loudspeaker layout mask
  122. int xxch_dmix_embedded; ///< Downmix already performed by encoder
  123. int xxch_dmix_scale_inv; ///< Downmix scale factor
  124. int xxch_dmix_mask[DCA_XXCH_CHANNELS_MAX]; ///< Downmix channel mapping mask
  125. int xxch_dmix_coeff[DCA_XXCH_CHANNELS_MAX * DCA_CORE_CHANNELS_MAX]; ///< Downmix coefficients
  126. int xxch_pos; ///< Bit position of XXCH frame in core substream
  127. // X96 extension data
  128. int x96_rev_no; ///< X96 revision number
  129. int x96_crc_present; ///< CRC presence flag for X96 channel set header
  130. int x96_nchannels; ///< Number of primary channels in X96 extension
  131. int x96_high_res; ///< X96 high resolution flag
  132. int x96_subband_start; ///< First encoded subband in X96 extension
  133. int x96_rand; ///< Random seed for generating samples for unallocated X96 subbands
  134. int x96_pos; ///< Bit position of X96 frame in core substream
  135. // Sample buffers
  136. unsigned int x96_subband_size;
  137. int32_t *x96_subband_buffer; ///< X96 subband sample buffer base
  138. int32_t *x96_subband_samples[DCA_CHANNELS][DCA_SUBBANDS_X96]; ///< X96 subband samples
  139. unsigned int subband_size;
  140. int32_t *subband_buffer; ///< Subband sample buffer base
  141. int32_t *subband_samples[DCA_CHANNELS][DCA_SUBBANDS]; ///< Subband samples
  142. int32_t *lfe_samples; ///< Decimated LFE samples
  143. // DSP contexts
  144. DCADSPData dcadsp_data[DCA_CHANNELS]; ///< FIR history buffers
  145. DCADSPContext *dcadsp;
  146. DCADCTContext dcadct;
  147. FFTContext imdct[2];
  148. SynthFilterContext synth;
  149. AVFloatDSPContext *float_dsp;
  150. AVFixedDSPContext *fixed_dsp;
  151. // PCM output data
  152. unsigned int output_size;
  153. void *output_buffer; ///< PCM output buffer base
  154. int32_t *output_samples[DCA_SPEAKER_COUNT]; ///< PCM output for fixed point mode
  155. int32_t output_history_lfe_fixed; ///< LFE PCM history for X96 filter
  156. float output_history_lfe_float; ///< LFE PCM history for X96 filter
  157. int ch_remap[DCA_SPEAKER_COUNT]; ///< Channel to speaker map
  158. int request_mask; ///< Requested channel layout (for stereo downmix)
  159. int npcmsamples; ///< Number of PCM samples per channel
  160. int output_rate; ///< Output sample rate (1x or 2x header rate)
  161. int filter_mode; ///< Previous filtering mode for detecting changes
  162. } DCACoreDecoder;
  163. static inline int ff_dca_core_map_spkr(DCACoreDecoder *core, int spkr)
  164. {
  165. if (core->ch_mask & (1U << spkr))
  166. return spkr;
  167. if (spkr == DCA_SPEAKER_Lss && (core->ch_mask & DCA_SPEAKER_MASK_Ls))
  168. return DCA_SPEAKER_Ls;
  169. if (spkr == DCA_SPEAKER_Rss && (core->ch_mask & DCA_SPEAKER_MASK_Rs))
  170. return DCA_SPEAKER_Rs;
  171. return -1;
  172. }
  173. static inline void ff_dca_core_dequantize(int32_t *output, const int32_t *input,
  174. int32_t step_size, int32_t scale, int residual, int len)
  175. {
  176. // Account for quantizer step size
  177. int64_t step_scale = (int64_t)step_size * scale;
  178. int n, shift = 0;
  179. // Limit scale factor resolution to 22 bits
  180. if (step_scale > (1 << 23)) {
  181. shift = av_log2(step_scale >> 23) + 1;
  182. step_scale >>= shift;
  183. }
  184. // Scale the samples
  185. if (residual) {
  186. for (n = 0; n < len; n++)
  187. output[n] += clip23(norm__(input[n] * step_scale, 22 - shift));
  188. } else {
  189. for (n = 0; n < len; n++)
  190. output[n] = clip23(norm__(input[n] * step_scale, 22 - shift));
  191. }
  192. }
  193. int ff_dca_core_parse(DCACoreDecoder *s, uint8_t *data, int size);
  194. int ff_dca_core_parse_exss(DCACoreDecoder *s, uint8_t *data, DCAExssAsset *asset);
  195. int ff_dca_core_filter_fixed(DCACoreDecoder *s, int x96_synth);
  196. int ff_dca_core_filter_frame(DCACoreDecoder *s, AVFrame *frame);
  197. av_cold void ff_dca_core_flush(DCACoreDecoder *s);
  198. av_cold int ff_dca_core_init(DCACoreDecoder *s);
  199. av_cold void ff_dca_core_close(DCACoreDecoder *s);
  200. #endif