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.

219 lines
9.9KB

  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. /** DCA syncwords, also used for bitstream type detection */
  34. #define DCA_MARKER_RAW_BE 0x7FFE8001
  35. #define DCA_MARKER_RAW_LE 0xFE7F0180
  36. #define DCA_MARKER_14B_BE 0x1FFFE800
  37. #define DCA_MARKER_14B_LE 0xFF1F00E8
  38. /** DCA-HD specific block starts with this marker. */
  39. #define DCA_HD_MARKER 0x64582025
  40. #define DCA_PRIM_CHANNELS_MAX (7)
  41. #define DCA_ABITS_MAX (32) /* Should be 28 */
  42. #define DCA_SUBSUBFRAMES_MAX (4)
  43. #define DCA_SUBFRAMES_MAX (16)
  44. #define DCA_BLOCKS_MAX (16)
  45. #define DCA_LFE_MAX (3)
  46. #define DCA_CHSETS_MAX (4)
  47. #define DCA_CHSET_CHANS_MAX (8)
  48. #define DCA_MAX_FRAME_SIZE 16384
  49. #define DCA_MAX_EXSS_HEADER_SIZE 4096
  50. #define DCA_BUFFER_PADDING_SIZE 1024
  51. enum DCAExtensionMask {
  52. DCA_EXT_CORE = 0x001, ///< core in core substream
  53. DCA_EXT_XXCH = 0x002, ///< XXCh channels extension in core substream
  54. DCA_EXT_X96 = 0x004, ///< 96/24 extension in core substream
  55. DCA_EXT_XCH = 0x008, ///< XCh channel extension in core substream
  56. DCA_EXT_EXSS_CORE = 0x010, ///< core in ExSS (extension substream)
  57. DCA_EXT_EXSS_XBR = 0x020, ///< extended bitrate extension in ExSS
  58. DCA_EXT_EXSS_XXCH = 0x040, ///< XXCh channels extension in ExSS
  59. DCA_EXT_EXSS_X96 = 0x080, ///< 96/24 extension in ExSS
  60. DCA_EXT_EXSS_LBR = 0x100, ///< low bitrate component in ExSS
  61. DCA_EXT_EXSS_XLL = 0x200, ///< lossless extension in ExSS
  62. };
  63. typedef struct DCAContext {
  64. const AVClass *class; ///< class for AVOptions
  65. AVCodecContext *avctx;
  66. /* Frame header */
  67. int frame_type; ///< type of the current frame
  68. int samples_deficit; ///< deficit sample count
  69. int crc_present; ///< crc is present in the bitstream
  70. int sample_blocks; ///< number of PCM sample blocks
  71. int frame_size; ///< primary frame byte size
  72. int amode; ///< audio channels arrangement
  73. int sample_rate; ///< audio sampling rate
  74. int bit_rate; ///< transmission bit rate
  75. int bit_rate_index; ///< transmission bit rate index
  76. int dynrange; ///< embedded dynamic range flag
  77. int timestamp; ///< embedded time stamp flag
  78. int aux_data; ///< auxiliary data flag
  79. int hdcd; ///< source material is mastered in HDCD
  80. int ext_descr; ///< extension audio descriptor flag
  81. int ext_coding; ///< extended coding flag
  82. int aspf; ///< audio sync word insertion flag
  83. int lfe; ///< low frequency effects flag
  84. int predictor_history; ///< predictor history flag
  85. int header_crc; ///< header crc check bytes
  86. int multirate_inter; ///< multirate interpolator switch
  87. int version; ///< encoder software revision
  88. int copy_history; ///< copy history
  89. int source_pcm_res; ///< source pcm resolution
  90. int front_sum; ///< front sum/difference flag
  91. int surround_sum; ///< surround sum/difference flag
  92. int dialog_norm; ///< dialog normalisation parameter
  93. /* Primary audio coding header */
  94. int subframes; ///< number of subframes
  95. int total_channels; ///< number of channels including extensions
  96. int prim_channels; ///< number of primary audio channels
  97. int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
  98. int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
  99. int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
  100. int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
  101. int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
  102. int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
  103. int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
  104. float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
  105. /* Primary audio coding side information */
  106. int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes
  107. int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count
  108. int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
  109. int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
  110. int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
  111. int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
  112. int32_t scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2];///< scale factors (2 if transient)
  113. int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
  114. int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
  115. float downmix_coef[DCA_PRIM_CHANNELS_MAX + 1][2]; ///< stereo downmix coefficients
  116. int dynrange_coef; ///< dynamic range coefficient
  117. /* Core substream's embedded downmix coefficients (cf. ETSI TS 102 114 V1.4.1)
  118. * Input: primary audio channels (incl. LFE if present)
  119. * Output: downmix audio channels (up to 4, no LFE) */
  120. uint8_t core_downmix; ///< embedded downmix coefficients available
  121. uint8_t core_downmix_amode; ///< audio channel arrangement of embedded downmix
  122. uint16_t core_downmix_codes[DCA_PRIM_CHANNELS_MAX + 1][4]; ///< embedded downmix coefficients (9-bit codes)
  123. int32_t high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
  124. float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
  125. int lfe_scale_factor;
  126. /* Subband samples history (for ADPCM) */
  127. DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
  128. DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
  129. DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
  130. int hist_index[DCA_PRIM_CHANNELS_MAX];
  131. DECLARE_ALIGNED(32, float, raXin)[32];
  132. int output; ///< type of output
  133. DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
  134. float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
  135. float *extra_channels[DCA_PRIM_CHANNELS_MAX + 1];
  136. uint8_t *extra_channels_buffer;
  137. unsigned int extra_channels_buffer_size;
  138. uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
  139. int dca_buffer_size; ///< how much data is in the dca_buffer
  140. const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
  141. GetBitContext gb;
  142. /* Current position in DCA frame */
  143. int current_subframe;
  144. int current_subsubframe;
  145. int core_ext_mask; ///< present extensions in the core substream
  146. /* XCh extension information */
  147. int xch_present; ///< XCh extension present and valid
  148. int xch_base_channel; ///< index of first (only) channel containing XCH data
  149. int xch_disable; ///< whether the XCh extension should be decoded or not
  150. /* XXCH extension information */
  151. int xxch_chset;
  152. int xxch_nbits_spk_mask;
  153. uint32_t xxch_core_spkmask;
  154. uint32_t xxch_spk_masks[4]; /* speaker masks, last element is core mask */
  155. int xxch_chset_nch[4];
  156. float xxch_dmix_sf[DCA_CHSETS_MAX];
  157. uint32_t xxch_dmix_embedded; /* lower layer has mix pre-embedded, per chset */
  158. float xxch_dmix_coeff[DCA_PRIM_CHANNELS_MAX][32]; /* worst case sizing */
  159. int8_t xxch_order_tab[32];
  160. int8_t lfe_index;
  161. /* ExSS header parser */
  162. int static_fields; ///< static fields present
  163. int mix_metadata; ///< mixing metadata present
  164. int num_mix_configs; ///< number of mix out configurations
  165. int mix_config_num_ch[4]; ///< number of channels in each mix out configuration
  166. int profile;
  167. int debug_flag; ///< used for suppressing repeated error messages output
  168. AVFloatDSPContext *fdsp;
  169. FFTContext imdct;
  170. SynthFilterContext synth;
  171. DCADSPContext dcadsp;
  172. FmtConvertContext fmt_conv;
  173. } DCAContext;
  174. extern av_export const uint32_t avpriv_dca_sample_rates[16];
  175. /**
  176. * Convert bitstream to one representation based on sync marker
  177. */
  178. int avpriv_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
  179. int max_size);
  180. int ff_dca_xbr_parse_frame(DCAContext *s);
  181. int ff_dca_xxch_decode_frame(DCAContext *s);
  182. void ff_dca_exss_parse_header(DCAContext *s);
  183. #endif /* AVCODEC_DCA_H */