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.

2100 lines
79KB

  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 Libav.
  9. *
  10. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include <math.h>
  25. #include <stddef.h>
  26. #include <stdio.h>
  27. #include "libavutil/channel_layout.h"
  28. #include "libavutil/common.h"
  29. #include "libavutil/float_dsp.h"
  30. #include "libavutil/internal.h"
  31. #include "libavutil/intreadwrite.h"
  32. #include "libavutil/mathematics.h"
  33. #include "libavutil/opt.h"
  34. #include "libavutil/samplefmt.h"
  35. #include "avcodec.h"
  36. #include "dca.h"
  37. #include "dcadata.h"
  38. #include "dcadsp.h"
  39. #include "dcahuff.h"
  40. #include "fft.h"
  41. #include "fmtconvert.h"
  42. #include "get_bits.h"
  43. #include "internal.h"
  44. #include "mathops.h"
  45. #include "put_bits.h"
  46. #include "synth_filter.h"
  47. #if ARCH_ARM
  48. # include "arm/dca.h"
  49. #endif
  50. //#define TRACE
  51. #define DCA_PRIM_CHANNELS_MAX (7)
  52. #define DCA_ABITS_MAX (32) /* Should be 28 */
  53. #define DCA_SUBSUBFRAMES_MAX (4)
  54. #define DCA_SUBFRAMES_MAX (16)
  55. #define DCA_BLOCKS_MAX (16)
  56. #define DCA_LFE_MAX (3)
  57. enum DCAMode {
  58. DCA_MONO = 0,
  59. DCA_CHANNEL,
  60. DCA_STEREO,
  61. DCA_STEREO_SUMDIFF,
  62. DCA_STEREO_TOTAL,
  63. DCA_3F,
  64. DCA_2F1R,
  65. DCA_3F1R,
  66. DCA_2F2R,
  67. DCA_3F2R,
  68. DCA_4F2R
  69. };
  70. /* these are unconfirmed but should be mostly correct */
  71. enum DCAExSSSpeakerMask {
  72. DCA_EXSS_FRONT_CENTER = 0x0001,
  73. DCA_EXSS_FRONT_LEFT_RIGHT = 0x0002,
  74. DCA_EXSS_SIDE_REAR_LEFT_RIGHT = 0x0004,
  75. DCA_EXSS_LFE = 0x0008,
  76. DCA_EXSS_REAR_CENTER = 0x0010,
  77. DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
  78. DCA_EXSS_REAR_LEFT_RIGHT = 0x0040,
  79. DCA_EXSS_FRONT_HIGH_CENTER = 0x0080,
  80. DCA_EXSS_OVERHEAD = 0x0100,
  81. DCA_EXSS_CENTER_LEFT_RIGHT = 0x0200,
  82. DCA_EXSS_WIDE_LEFT_RIGHT = 0x0400,
  83. DCA_EXSS_SIDE_LEFT_RIGHT = 0x0800,
  84. DCA_EXSS_LFE2 = 0x1000,
  85. DCA_EXSS_SIDE_HIGH_LEFT_RIGHT = 0x2000,
  86. DCA_EXSS_REAR_HIGH_CENTER = 0x4000,
  87. DCA_EXSS_REAR_HIGH_LEFT_RIGHT = 0x8000,
  88. };
  89. enum DCAExtensionMask {
  90. DCA_EXT_CORE = 0x001, ///< core in core substream
  91. DCA_EXT_XXCH = 0x002, ///< XXCh channels extension in core substream
  92. DCA_EXT_X96 = 0x004, ///< 96/24 extension in core substream
  93. DCA_EXT_XCH = 0x008, ///< XCh channel extension in core substream
  94. DCA_EXT_EXSS_CORE = 0x010, ///< core in ExSS (extension substream)
  95. DCA_EXT_EXSS_XBR = 0x020, ///< extended bitrate extension in ExSS
  96. DCA_EXT_EXSS_XXCH = 0x040, ///< XXCh channels extension in ExSS
  97. DCA_EXT_EXSS_X96 = 0x080, ///< 96/24 extension in ExSS
  98. DCA_EXT_EXSS_LBR = 0x100, ///< low bitrate component in ExSS
  99. DCA_EXT_EXSS_XLL = 0x200, ///< lossless extension in ExSS
  100. };
  101. /* -1 are reserved or unknown */
  102. static const int dca_ext_audio_descr_mask[] = {
  103. DCA_EXT_XCH,
  104. -1,
  105. DCA_EXT_X96,
  106. DCA_EXT_XCH | DCA_EXT_X96,
  107. -1,
  108. -1,
  109. DCA_EXT_XXCH,
  110. -1,
  111. };
  112. /* extensions that reside in core substream */
  113. #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
  114. /* Tables for mapping dts channel configurations to libavcodec multichannel api.
  115. * Some compromises have been made for special configurations. Most configurations
  116. * are never used so complete accuracy is not needed.
  117. *
  118. * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
  119. * S -> side, when both rear and back are configured move one of them to the side channel
  120. * OV -> center back
  121. * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
  122. */
  123. static const uint64_t dca_core_channel_layout[] = {
  124. AV_CH_FRONT_CENTER, ///< 1, A
  125. AV_CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
  126. AV_CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
  127. AV_CH_LAYOUT_STEREO, ///< 2, (L + R) + (L - R) (sum-difference)
  128. AV_CH_LAYOUT_STEREO, ///< 2, LT + RT (left and right total)
  129. AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER, ///< 3, C + L + R
  130. AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER, ///< 3, L + R + S
  131. AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 4, C + L + R + S
  132. AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 4, L + R + SL + SR
  133. AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
  134. AV_CH_SIDE_RIGHT, ///< 5, C + L + R + SL + SR
  135. AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
  136. AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR
  137. AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
  138. AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 6, C + L + R + LR + RR + OV
  139. AV_CH_FRONT_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
  140. AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_BACK_CENTER |
  141. AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 6, CF + CR + LF + RF + LR + RR
  142. AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
  143. AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
  144. AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
  145. AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
  146. AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
  147. AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2 + SR1 + SR2
  148. AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
  149. AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
  150. AV_CH_SIDE_LEFT | AV_CH_BACK_CENTER | AV_CH_SIDE_RIGHT, ///< 8, CL + C + CR + L + R + SL + S + SR
  151. };
  152. static const int8_t dca_lfe_index[] = {
  153. 1, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 1, 3, 2, 3
  154. };
  155. static const int8_t dca_channel_reorder_lfe[][9] = {
  156. { 0, -1, -1, -1, -1, -1, -1, -1, -1 },
  157. { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
  158. { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
  159. { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
  160. { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
  161. { 2, 0, 1, -1, -1, -1, -1, -1, -1 },
  162. { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
  163. { 2, 0, 1, 4, -1, -1, -1, -1, -1 },
  164. { 0, 1, 3, 4, -1, -1, -1, -1, -1 },
  165. { 2, 0, 1, 4, 5, -1, -1, -1, -1 },
  166. { 3, 4, 0, 1, 5, 6, -1, -1, -1 },
  167. { 2, 0, 1, 4, 5, 6, -1, -1, -1 },
  168. { 0, 6, 4, 5, 2, 3, -1, -1, -1 },
  169. { 4, 2, 5, 0, 1, 6, 7, -1, -1 },
  170. { 5, 6, 0, 1, 7, 3, 8, 4, -1 },
  171. { 4, 2, 5, 0, 1, 6, 8, 7, -1 },
  172. };
  173. static const int8_t dca_channel_reorder_lfe_xch[][9] = {
  174. { 0, 2, -1, -1, -1, -1, -1, -1, -1 },
  175. { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
  176. { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
  177. { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
  178. { 0, 1, 3, -1, -1, -1, -1, -1, -1 },
  179. { 2, 0, 1, 4, -1, -1, -1, -1, -1 },
  180. { 0, 1, 3, 4, -1, -1, -1, -1, -1 },
  181. { 2, 0, 1, 4, 5, -1, -1, -1, -1 },
  182. { 0, 1, 4, 5, 3, -1, -1, -1, -1 },
  183. { 2, 0, 1, 5, 6, 4, -1, -1, -1 },
  184. { 3, 4, 0, 1, 6, 7, 5, -1, -1 },
  185. { 2, 0, 1, 4, 5, 6, 7, -1, -1 },
  186. { 0, 6, 4, 5, 2, 3, 7, -1, -1 },
  187. { 4, 2, 5, 0, 1, 7, 8, 6, -1 },
  188. { 5, 6, 0, 1, 8, 3, 9, 4, 7 },
  189. { 4, 2, 5, 0, 1, 6, 9, 8, 7 },
  190. };
  191. static const int8_t dca_channel_reorder_nolfe[][9] = {
  192. { 0, -1, -1, -1, -1, -1, -1, -1, -1 },
  193. { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
  194. { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
  195. { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
  196. { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
  197. { 2, 0, 1, -1, -1, -1, -1, -1, -1 },
  198. { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
  199. { 2, 0, 1, 3, -1, -1, -1, -1, -1 },
  200. { 0, 1, 2, 3, -1, -1, -1, -1, -1 },
  201. { 2, 0, 1, 3, 4, -1, -1, -1, -1 },
  202. { 2, 3, 0, 1, 4, 5, -1, -1, -1 },
  203. { 2, 0, 1, 3, 4, 5, -1, -1, -1 },
  204. { 0, 5, 3, 4, 1, 2, -1, -1, -1 },
  205. { 3, 2, 4, 0, 1, 5, 6, -1, -1 },
  206. { 4, 5, 0, 1, 6, 2, 7, 3, -1 },
  207. { 3, 2, 4, 0, 1, 5, 7, 6, -1 },
  208. };
  209. static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
  210. { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
  211. { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
  212. { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
  213. { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
  214. { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
  215. { 2, 0, 1, 3, -1, -1, -1, -1, -1 },
  216. { 0, 1, 2, 3, -1, -1, -1, -1, -1 },
  217. { 2, 0, 1, 3, 4, -1, -1, -1, -1 },
  218. { 0, 1, 3, 4, 2, -1, -1, -1, -1 },
  219. { 2, 0, 1, 4, 5, 3, -1, -1, -1 },
  220. { 2, 3, 0, 1, 5, 6, 4, -1, -1 },
  221. { 2, 0, 1, 3, 4, 5, 6, -1, -1 },
  222. { 0, 5, 3, 4, 1, 2, 6, -1, -1 },
  223. { 3, 2, 4, 0, 1, 6, 7, 5, -1 },
  224. { 4, 5, 0, 1, 7, 2, 8, 3, 6 },
  225. { 3, 2, 4, 0, 1, 5, 8, 7, 6 },
  226. };
  227. #define DCA_DOLBY 101 /* FIXME */
  228. #define DCA_CHANNEL_BITS 6
  229. #define DCA_CHANNEL_MASK 0x3F
  230. #define DCA_LFE 0x80
  231. #define HEADER_SIZE 14
  232. #define DCA_MAX_FRAME_SIZE 16384
  233. #define DCA_MAX_EXSS_HEADER_SIZE 4096
  234. #define DCA_BUFFER_PADDING_SIZE 1024
  235. #define DCA_NSYNCAUX 0x9A1105A0
  236. /** Bit allocation */
  237. typedef struct {
  238. int offset; ///< code values offset
  239. int maxbits[8]; ///< max bits in VLC
  240. int wrap; ///< wrap for get_vlc2()
  241. VLC vlc[8]; ///< actual codes
  242. } BitAlloc;
  243. static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
  244. static BitAlloc dca_tmode; ///< transition mode VLCs
  245. static BitAlloc dca_scalefactor; ///< scalefactor VLCs
  246. static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
  247. static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba,
  248. int idx)
  249. {
  250. return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
  251. ba->offset;
  252. }
  253. typedef struct {
  254. AVClass *class; ///< class for AVOptions
  255. AVCodecContext *avctx;
  256. /* Frame header */
  257. int frame_type; ///< type of the current frame
  258. int samples_deficit; ///< deficit sample count
  259. int crc_present; ///< crc is present in the bitstream
  260. int sample_blocks; ///< number of PCM sample blocks
  261. int frame_size; ///< primary frame byte size
  262. int amode; ///< audio channels arrangement
  263. int sample_rate; ///< audio sampling rate
  264. int bit_rate; ///< transmission bit rate
  265. int bit_rate_index; ///< transmission bit rate index
  266. int dynrange; ///< embedded dynamic range flag
  267. int timestamp; ///< embedded time stamp flag
  268. int aux_data; ///< auxiliary data flag
  269. int hdcd; ///< source material is mastered in HDCD
  270. int ext_descr; ///< extension audio descriptor flag
  271. int ext_coding; ///< extended coding flag
  272. int aspf; ///< audio sync word insertion flag
  273. int lfe; ///< low frequency effects flag
  274. int predictor_history; ///< predictor history flag
  275. int header_crc; ///< header crc check bytes
  276. int multirate_inter; ///< multirate interpolator switch
  277. int version; ///< encoder software revision
  278. int copy_history; ///< copy history
  279. int source_pcm_res; ///< source pcm resolution
  280. int front_sum; ///< front sum/difference flag
  281. int surround_sum; ///< surround sum/difference flag
  282. int dialog_norm; ///< dialog normalisation parameter
  283. /* Primary audio coding header */
  284. int subframes; ///< number of subframes
  285. int total_channels; ///< number of channels including extensions
  286. int prim_channels; ///< number of primary audio channels
  287. int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
  288. int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
  289. int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
  290. int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
  291. int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
  292. int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
  293. int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
  294. float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
  295. /* Primary audio coding side information */
  296. int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes
  297. int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count
  298. int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
  299. int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
  300. int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
  301. int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
  302. int32_t scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2];///< scale factors (2 if transient)
  303. int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
  304. int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
  305. float downmix_coef[DCA_PRIM_CHANNELS_MAX + 1][2]; ///< stereo downmix coefficients
  306. int dynrange_coef; ///< dynamic range coefficient
  307. /* Core substream's embedded downmix coefficients (cf. ETSI TS 102 114 V1.4.1)
  308. * Input: primary audio channels (incl. LFE if present)
  309. * Output: downmix audio channels (up to 4, no LFE) */
  310. uint8_t core_downmix; ///< embedded downmix coefficients available
  311. uint8_t core_downmix_amode; ///< audio channel arrangement of embedded downmix
  312. uint16_t core_downmix_codes[DCA_PRIM_CHANNELS_MAX + 1][4]; ///< embedded downmix coefficients (9-bit codes)
  313. int32_t high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
  314. float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
  315. int lfe_scale_factor;
  316. /* Subband samples history (for ADPCM) */
  317. DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
  318. DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
  319. DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
  320. int hist_index[DCA_PRIM_CHANNELS_MAX];
  321. DECLARE_ALIGNED(32, float, raXin)[32];
  322. int output; ///< type of output
  323. DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
  324. float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
  325. float *extra_channels[DCA_PRIM_CHANNELS_MAX + 1];
  326. uint8_t *extra_channels_buffer;
  327. unsigned int extra_channels_buffer_size;
  328. uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
  329. int dca_buffer_size; ///< how much data is in the dca_buffer
  330. const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
  331. GetBitContext gb;
  332. /* Current position in DCA frame */
  333. int current_subframe;
  334. int current_subsubframe;
  335. int core_ext_mask; ///< present extensions in the core substream
  336. /* XCh extension information */
  337. int xch_present; ///< XCh extension present and valid
  338. int xch_base_channel; ///< index of first (only) channel containing XCH data
  339. int xch_disable; ///< whether the XCh extension should be decoded or not
  340. /* ExSS header parser */
  341. int static_fields; ///< static fields present
  342. int mix_metadata; ///< mixing metadata present
  343. int num_mix_configs; ///< number of mix out configurations
  344. int mix_config_num_ch[4]; ///< number of channels in each mix out configuration
  345. int profile;
  346. int debug_flag; ///< used for suppressing repeated error messages output
  347. AVFloatDSPContext fdsp;
  348. FFTContext imdct;
  349. SynthFilterContext synth;
  350. DCADSPContext dcadsp;
  351. FmtConvertContext fmt_conv;
  352. } DCAContext;
  353. static const uint16_t dca_vlc_offs[] = {
  354. 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
  355. 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
  356. 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
  357. 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
  358. 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
  359. 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
  360. };
  361. static av_cold void dca_init_vlcs(void)
  362. {
  363. static int vlcs_initialized = 0;
  364. int i, j, c = 14;
  365. static VLC_TYPE dca_table[23622][2];
  366. if (vlcs_initialized)
  367. return;
  368. dca_bitalloc_index.offset = 1;
  369. dca_bitalloc_index.wrap = 2;
  370. for (i = 0; i < 5; i++) {
  371. dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
  372. dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
  373. init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
  374. bitalloc_12_bits[i], 1, 1,
  375. bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
  376. }
  377. dca_scalefactor.offset = -64;
  378. dca_scalefactor.wrap = 2;
  379. for (i = 0; i < 5; i++) {
  380. dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
  381. dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
  382. init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
  383. scales_bits[i], 1, 1,
  384. scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
  385. }
  386. dca_tmode.offset = 0;
  387. dca_tmode.wrap = 1;
  388. for (i = 0; i < 4; i++) {
  389. dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
  390. dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
  391. init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
  392. tmode_bits[i], 1, 1,
  393. tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
  394. }
  395. for (i = 0; i < 10; i++)
  396. for (j = 0; j < 7; j++) {
  397. if (!bitalloc_codes[i][j])
  398. break;
  399. dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
  400. dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
  401. dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
  402. dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
  403. init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
  404. bitalloc_sizes[i],
  405. bitalloc_bits[i][j], 1, 1,
  406. bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
  407. c++;
  408. }
  409. vlcs_initialized = 1;
  410. }
  411. static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
  412. {
  413. while (len--)
  414. *dst++ = get_bits(gb, bits);
  415. }
  416. static int dca_parse_audio_coding_header(DCAContext *s, int base_channel)
  417. {
  418. int i, j;
  419. static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
  420. static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
  421. static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
  422. s->total_channels = get_bits(&s->gb, 3) + 1 + base_channel;
  423. s->prim_channels = s->total_channels;
  424. if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
  425. s->prim_channels = DCA_PRIM_CHANNELS_MAX;
  426. for (i = base_channel; i < s->prim_channels; i++) {
  427. s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
  428. if (s->subband_activity[i] > DCA_SUBBANDS)
  429. s->subband_activity[i] = DCA_SUBBANDS;
  430. }
  431. for (i = base_channel; i < s->prim_channels; i++) {
  432. s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
  433. if (s->vq_start_subband[i] > DCA_SUBBANDS)
  434. s->vq_start_subband[i] = DCA_SUBBANDS;
  435. }
  436. get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
  437. get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
  438. get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
  439. get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
  440. /* Get codebooks quantization indexes */
  441. if (!base_channel)
  442. memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
  443. for (j = 1; j < 11; j++)
  444. for (i = base_channel; i < s->prim_channels; i++)
  445. s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
  446. /* Get scale factor adjustment */
  447. for (j = 0; j < 11; j++)
  448. for (i = base_channel; i < s->prim_channels; i++)
  449. s->scalefactor_adj[i][j] = 1;
  450. for (j = 1; j < 11; j++)
  451. for (i = base_channel; i < s->prim_channels; i++)
  452. if (s->quant_index_huffman[i][j] < thr[j])
  453. s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
  454. if (s->crc_present) {
  455. /* Audio header CRC check */
  456. get_bits(&s->gb, 16);
  457. }
  458. s->current_subframe = 0;
  459. s->current_subsubframe = 0;
  460. #ifdef TRACE
  461. av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
  462. av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
  463. for (i = base_channel; i < s->prim_channels; i++) {
  464. av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n",
  465. s->subband_activity[i]);
  466. av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n",
  467. s->vq_start_subband[i]);
  468. av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n",
  469. s->joint_intensity[i]);
  470. av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n",
  471. s->transient_huffman[i]);
  472. av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n",
  473. s->scalefactor_huffman[i]);
  474. av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n",
  475. s->bitalloc_huffman[i]);
  476. av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
  477. for (j = 0; j < 11; j++)
  478. av_log(s->avctx, AV_LOG_DEBUG, " %i", s->quant_index_huffman[i][j]);
  479. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  480. av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
  481. for (j = 0; j < 11; j++)
  482. av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
  483. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  484. }
  485. #endif
  486. return 0;
  487. }
  488. static int dca_parse_frame_header(DCAContext *s)
  489. {
  490. init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
  491. /* Sync code */
  492. skip_bits_long(&s->gb, 32);
  493. /* Frame header */
  494. s->frame_type = get_bits(&s->gb, 1);
  495. s->samples_deficit = get_bits(&s->gb, 5) + 1;
  496. s->crc_present = get_bits(&s->gb, 1);
  497. s->sample_blocks = get_bits(&s->gb, 7) + 1;
  498. s->frame_size = get_bits(&s->gb, 14) + 1;
  499. if (s->frame_size < 95)
  500. return AVERROR_INVALIDDATA;
  501. s->amode = get_bits(&s->gb, 6);
  502. s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
  503. if (!s->sample_rate)
  504. return AVERROR_INVALIDDATA;
  505. s->bit_rate_index = get_bits(&s->gb, 5);
  506. s->bit_rate = dca_bit_rates[s->bit_rate_index];
  507. if (!s->bit_rate)
  508. return AVERROR_INVALIDDATA;
  509. skip_bits1(&s->gb); // always 0 (reserved, cf. ETSI TS 102 114 V1.4.1)
  510. s->dynrange = get_bits(&s->gb, 1);
  511. s->timestamp = get_bits(&s->gb, 1);
  512. s->aux_data = get_bits(&s->gb, 1);
  513. s->hdcd = get_bits(&s->gb, 1);
  514. s->ext_descr = get_bits(&s->gb, 3);
  515. s->ext_coding = get_bits(&s->gb, 1);
  516. s->aspf = get_bits(&s->gb, 1);
  517. s->lfe = get_bits(&s->gb, 2);
  518. s->predictor_history = get_bits(&s->gb, 1);
  519. if (s->lfe > 2) {
  520. av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE value: %d\n", s->lfe);
  521. return AVERROR_INVALIDDATA;
  522. }
  523. /* TODO: check CRC */
  524. if (s->crc_present)
  525. s->header_crc = get_bits(&s->gb, 16);
  526. s->multirate_inter = get_bits(&s->gb, 1);
  527. s->version = get_bits(&s->gb, 4);
  528. s->copy_history = get_bits(&s->gb, 2);
  529. s->source_pcm_res = get_bits(&s->gb, 3);
  530. s->front_sum = get_bits(&s->gb, 1);
  531. s->surround_sum = get_bits(&s->gb, 1);
  532. s->dialog_norm = get_bits(&s->gb, 4);
  533. /* FIXME: channels mixing levels */
  534. s->output = s->amode;
  535. if (s->lfe)
  536. s->output |= DCA_LFE;
  537. #ifdef TRACE
  538. av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
  539. av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
  540. av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
  541. av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
  542. s->sample_blocks, s->sample_blocks * 32);
  543. av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
  544. av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
  545. s->amode, dca_channels[s->amode]);
  546. av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
  547. s->sample_rate);
  548. av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
  549. s->bit_rate);
  550. av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
  551. av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
  552. av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
  553. av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
  554. av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
  555. av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
  556. av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
  557. av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
  558. av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
  559. s->predictor_history);
  560. av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
  561. av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
  562. s->multirate_inter);
  563. av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
  564. av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
  565. av_log(s->avctx, AV_LOG_DEBUG,
  566. "source pcm resolution: %i (%i bits/sample)\n",
  567. s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
  568. av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
  569. av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
  570. av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
  571. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  572. #endif
  573. /* Primary audio coding header */
  574. s->subframes = get_bits(&s->gb, 4) + 1;
  575. return dca_parse_audio_coding_header(s, 0);
  576. }
  577. static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
  578. {
  579. if (level < 5) {
  580. /* huffman encoded */
  581. value += get_bitalloc(gb, &dca_scalefactor, level);
  582. value = av_clip(value, 0, (1 << log2range) - 1);
  583. } else if (level < 8) {
  584. if (level + 1 > log2range) {
  585. skip_bits(gb, level + 1 - log2range);
  586. value = get_bits(gb, log2range);
  587. } else {
  588. value = get_bits(gb, level + 1);
  589. }
  590. }
  591. return value;
  592. }
  593. static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
  594. {
  595. /* Primary audio coding side information */
  596. int j, k;
  597. if (get_bits_left(&s->gb) < 0)
  598. return AVERROR_INVALIDDATA;
  599. if (!base_channel) {
  600. s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
  601. s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
  602. }
  603. for (j = base_channel; j < s->prim_channels; j++) {
  604. for (k = 0; k < s->subband_activity[j]; k++)
  605. s->prediction_mode[j][k] = get_bits(&s->gb, 1);
  606. }
  607. /* Get prediction codebook */
  608. for (j = base_channel; j < s->prim_channels; j++) {
  609. for (k = 0; k < s->subband_activity[j]; k++) {
  610. if (s->prediction_mode[j][k] > 0) {
  611. /* (Prediction coefficient VQ address) */
  612. s->prediction_vq[j][k] = get_bits(&s->gb, 12);
  613. }
  614. }
  615. }
  616. /* Bit allocation index */
  617. for (j = base_channel; j < s->prim_channels; j++) {
  618. for (k = 0; k < s->vq_start_subband[j]; k++) {
  619. if (s->bitalloc_huffman[j] == 6)
  620. s->bitalloc[j][k] = get_bits(&s->gb, 5);
  621. else if (s->bitalloc_huffman[j] == 5)
  622. s->bitalloc[j][k] = get_bits(&s->gb, 4);
  623. else if (s->bitalloc_huffman[j] == 7) {
  624. av_log(s->avctx, AV_LOG_ERROR,
  625. "Invalid bit allocation index\n");
  626. return AVERROR_INVALIDDATA;
  627. } else {
  628. s->bitalloc[j][k] =
  629. get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
  630. }
  631. if (s->bitalloc[j][k] > 26) {
  632. av_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n",
  633. j, k, s->bitalloc[j][k]);
  634. return AVERROR_INVALIDDATA;
  635. }
  636. }
  637. }
  638. /* Transition mode */
  639. for (j = base_channel; j < s->prim_channels; j++) {
  640. for (k = 0; k < s->subband_activity[j]; k++) {
  641. s->transition_mode[j][k] = 0;
  642. if (s->subsubframes[s->current_subframe] > 1 &&
  643. k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
  644. s->transition_mode[j][k] =
  645. get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
  646. }
  647. }
  648. }
  649. if (get_bits_left(&s->gb) < 0)
  650. return AVERROR_INVALIDDATA;
  651. for (j = base_channel; j < s->prim_channels; j++) {
  652. const uint32_t *scale_table;
  653. int scale_sum, log_size;
  654. memset(s->scale_factor[j], 0,
  655. s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
  656. if (s->scalefactor_huffman[j] == 6) {
  657. scale_table = scale_factor_quant7;
  658. log_size = 7;
  659. } else {
  660. scale_table = scale_factor_quant6;
  661. log_size = 6;
  662. }
  663. /* When huffman coded, only the difference is encoded */
  664. scale_sum = 0;
  665. for (k = 0; k < s->subband_activity[j]; k++) {
  666. if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
  667. scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
  668. s->scale_factor[j][k][0] = scale_table[scale_sum];
  669. }
  670. if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
  671. /* Get second scale factor */
  672. scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
  673. s->scale_factor[j][k][1] = scale_table[scale_sum];
  674. }
  675. }
  676. }
  677. /* Joint subband scale factor codebook select */
  678. for (j = base_channel; j < s->prim_channels; j++) {
  679. /* Transmitted only if joint subband coding enabled */
  680. if (s->joint_intensity[j] > 0)
  681. s->joint_huff[j] = get_bits(&s->gb, 3);
  682. }
  683. if (get_bits_left(&s->gb) < 0)
  684. return AVERROR_INVALIDDATA;
  685. /* Scale factors for joint subband coding */
  686. for (j = base_channel; j < s->prim_channels; j++) {
  687. int source_channel;
  688. /* Transmitted only if joint subband coding enabled */
  689. if (s->joint_intensity[j] > 0) {
  690. int scale = 0;
  691. source_channel = s->joint_intensity[j] - 1;
  692. /* When huffman coded, only the difference is encoded
  693. * (is this valid as well for joint scales ???) */
  694. for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
  695. scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
  696. s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
  697. }
  698. if (!(s->debug_flag & 0x02)) {
  699. av_log(s->avctx, AV_LOG_DEBUG,
  700. "Joint stereo coding not supported\n");
  701. s->debug_flag |= 0x02;
  702. }
  703. }
  704. }
  705. /* Dynamic range coefficient */
  706. if (!base_channel && s->dynrange)
  707. s->dynrange_coef = get_bits(&s->gb, 8);
  708. /* Side information CRC check word */
  709. if (s->crc_present) {
  710. get_bits(&s->gb, 16);
  711. }
  712. /*
  713. * Primary audio data arrays
  714. */
  715. /* VQ encoded high frequency subbands */
  716. for (j = base_channel; j < s->prim_channels; j++)
  717. for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
  718. /* 1 vector -> 32 samples */
  719. s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
  720. /* Low frequency effect data */
  721. if (!base_channel && s->lfe) {
  722. /* LFE samples */
  723. int lfe_samples = 2 * s->lfe * (4 + block_index);
  724. int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
  725. float lfe_scale;
  726. for (j = lfe_samples; j < lfe_end_sample; j++) {
  727. /* Signed 8 bits int */
  728. s->lfe_data[j] = get_sbits(&s->gb, 8);
  729. }
  730. /* Scale factor index */
  731. skip_bits(&s->gb, 1);
  732. s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 7)];
  733. /* Quantization step size * scale factor */
  734. lfe_scale = 0.035 * s->lfe_scale_factor;
  735. for (j = lfe_samples; j < lfe_end_sample; j++)
  736. s->lfe_data[j] *= lfe_scale;
  737. }
  738. #ifdef TRACE
  739. av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
  740. s->subsubframes[s->current_subframe]);
  741. av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
  742. s->partial_samples[s->current_subframe]);
  743. for (j = base_channel; j < s->prim_channels; j++) {
  744. av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
  745. for (k = 0; k < s->subband_activity[j]; k++)
  746. av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
  747. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  748. }
  749. for (j = base_channel; j < s->prim_channels; j++) {
  750. for (k = 0; k < s->subband_activity[j]; k++)
  751. av_log(s->avctx, AV_LOG_DEBUG,
  752. "prediction coefs: %f, %f, %f, %f\n",
  753. (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
  754. (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
  755. (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
  756. (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
  757. }
  758. for (j = base_channel; j < s->prim_channels; j++) {
  759. av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
  760. for (k = 0; k < s->vq_start_subband[j]; k++)
  761. av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
  762. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  763. }
  764. for (j = base_channel; j < s->prim_channels; j++) {
  765. av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
  766. for (k = 0; k < s->subband_activity[j]; k++)
  767. av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
  768. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  769. }
  770. for (j = base_channel; j < s->prim_channels; j++) {
  771. av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
  772. for (k = 0; k < s->subband_activity[j]; k++) {
  773. if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
  774. av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
  775. if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
  776. av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
  777. }
  778. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  779. }
  780. for (j = base_channel; j < s->prim_channels; j++) {
  781. if (s->joint_intensity[j] > 0) {
  782. int source_channel = s->joint_intensity[j] - 1;
  783. av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
  784. for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
  785. av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
  786. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  787. }
  788. }
  789. for (j = base_channel; j < s->prim_channels; j++)
  790. for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
  791. av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
  792. if (!base_channel && s->lfe) {
  793. int lfe_samples = 2 * s->lfe * (4 + block_index);
  794. int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
  795. av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
  796. for (j = lfe_samples; j < lfe_end_sample; j++)
  797. av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
  798. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  799. }
  800. #endif
  801. return 0;
  802. }
  803. static void qmf_32_subbands(DCAContext *s, int chans,
  804. float samples_in[32][8], float *samples_out,
  805. float scale)
  806. {
  807. const float *prCoeff;
  808. int sb_act = s->subband_activity[chans];
  809. scale *= sqrt(1 / 8.0);
  810. /* Select filter */
  811. if (!s->multirate_inter) /* Non-perfect reconstruction */
  812. prCoeff = fir_32bands_nonperfect;
  813. else /* Perfect reconstruction */
  814. prCoeff = fir_32bands_perfect;
  815. s->dcadsp.qmf_32_subbands(samples_in, sb_act, &s->synth, &s->imdct,
  816. s->subband_fir_hist[chans],
  817. &s->hist_index[chans],
  818. s->subband_fir_noidea[chans], prCoeff,
  819. samples_out, s->raXin, scale);
  820. }
  821. static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
  822. int num_deci_sample, float *samples_in,
  823. float *samples_out)
  824. {
  825. /* samples_in: An array holding decimated samples.
  826. * Samples in current subframe starts from samples_in[0],
  827. * while samples_in[-1], samples_in[-2], ..., stores samples
  828. * from last subframe as history.
  829. *
  830. * samples_out: An array holding interpolated samples
  831. */
  832. int idx;
  833. const float *prCoeff;
  834. int deciindex;
  835. /* Select decimation filter */
  836. if (decimation_select == 1) {
  837. idx = 1;
  838. prCoeff = lfe_fir_128;
  839. } else {
  840. idx = 0;
  841. prCoeff = lfe_fir_64;
  842. }
  843. /* Interpolation */
  844. for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
  845. s->dcadsp.lfe_fir[idx](samples_out, samples_in, prCoeff);
  846. samples_in++;
  847. samples_out += 2 * 32 * (1 + idx);
  848. }
  849. }
  850. /* downmixing routines */
  851. #define MIX_REAR1(samples, s1, rs, coef) \
  852. samples[0][i] += samples[s1][i] * coef[rs][0]; \
  853. samples[1][i] += samples[s1][i] * coef[rs][1];
  854. #define MIX_REAR2(samples, s1, s2, rs, coef) \
  855. samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
  856. samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
  857. #define MIX_FRONT3(samples, coef) \
  858. t = samples[c][i]; \
  859. u = samples[l][i]; \
  860. v = samples[r][i]; \
  861. samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
  862. samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
  863. #define DOWNMIX_TO_STEREO(op1, op2) \
  864. for (i = 0; i < 256; i++) { \
  865. op1 \
  866. op2 \
  867. }
  868. static void dca_downmix(float **samples, int srcfmt, int lfe_present,
  869. float coef[DCA_PRIM_CHANNELS_MAX + 1][2],
  870. const int8_t *channel_mapping)
  871. {
  872. int c, l, r, sl, sr, s;
  873. int i;
  874. float t, u, v;
  875. switch (srcfmt) {
  876. case DCA_MONO:
  877. case DCA_4F2R:
  878. av_log(NULL, 0, "Not implemented!\n");
  879. break;
  880. case DCA_CHANNEL:
  881. case DCA_STEREO:
  882. case DCA_STEREO_TOTAL:
  883. case DCA_STEREO_SUMDIFF:
  884. break;
  885. case DCA_3F:
  886. c = channel_mapping[0];
  887. l = channel_mapping[1];
  888. r = channel_mapping[2];
  889. DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
  890. break;
  891. case DCA_2F1R:
  892. s = channel_mapping[2];
  893. DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
  894. break;
  895. case DCA_3F1R:
  896. c = channel_mapping[0];
  897. l = channel_mapping[1];
  898. r = channel_mapping[2];
  899. s = channel_mapping[3];
  900. DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
  901. MIX_REAR1(samples, s, 3, coef));
  902. break;
  903. case DCA_2F2R:
  904. sl = channel_mapping[2];
  905. sr = channel_mapping[3];
  906. DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
  907. break;
  908. case DCA_3F2R:
  909. c = channel_mapping[0];
  910. l = channel_mapping[1];
  911. r = channel_mapping[2];
  912. sl = channel_mapping[3];
  913. sr = channel_mapping[4];
  914. DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
  915. MIX_REAR2(samples, sl, sr, 3, coef));
  916. break;
  917. }
  918. if (lfe_present) {
  919. int lf_buf = dca_lfe_index[srcfmt];
  920. int lf_idx = dca_channels[srcfmt];
  921. for (i = 0; i < 256; i++) {
  922. samples[0][i] += samples[lf_buf][i] * coef[lf_idx][0];
  923. samples[1][i] += samples[lf_buf][i] * coef[lf_idx][1];
  924. }
  925. }
  926. }
  927. #ifndef decode_blockcodes
  928. /* Very compact version of the block code decoder that does not use table
  929. * look-up but is slightly slower */
  930. static int decode_blockcode(int code, int levels, int32_t *values)
  931. {
  932. int i;
  933. int offset = (levels - 1) >> 1;
  934. for (i = 0; i < 4; i++) {
  935. int div = FASTDIV(code, levels);
  936. values[i] = code - offset - div * levels;
  937. code = div;
  938. }
  939. return code;
  940. }
  941. static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
  942. {
  943. return decode_blockcode(code1, levels, values) |
  944. decode_blockcode(code2, levels, values + 4);
  945. }
  946. #endif
  947. static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
  948. static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
  949. static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
  950. {
  951. int k, l;
  952. int subsubframe = s->current_subsubframe;
  953. const float *quant_step_table;
  954. /* FIXME */
  955. float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
  956. LOCAL_ALIGNED_16(int32_t, block, [8 * DCA_SUBBANDS]);
  957. /*
  958. * Audio data
  959. */
  960. /* Select quantization step size table */
  961. if (s->bit_rate_index == 0x1f)
  962. quant_step_table = lossless_quant_d;
  963. else
  964. quant_step_table = lossy_quant_d;
  965. for (k = base_channel; k < s->prim_channels; k++) {
  966. float rscale[DCA_SUBBANDS];
  967. if (get_bits_left(&s->gb) < 0)
  968. return AVERROR_INVALIDDATA;
  969. for (l = 0; l < s->vq_start_subband[k]; l++) {
  970. int m;
  971. /* Select the mid-tread linear quantizer */
  972. int abits = s->bitalloc[k][l];
  973. float quant_step_size = quant_step_table[abits];
  974. /*
  975. * Determine quantization index code book and its type
  976. */
  977. /* Select quantization index code book */
  978. int sel = s->quant_index_huffman[k][abits];
  979. /*
  980. * Extract bits from the bit stream
  981. */
  982. if (!abits) {
  983. rscale[l] = 0;
  984. memset(block + 8 * l, 0, 8 * sizeof(block[0]));
  985. } else {
  986. /* Deal with transients */
  987. int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
  988. rscale[l] = quant_step_size * s->scale_factor[k][l][sfi] *
  989. s->scalefactor_adj[k][sel];
  990. if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
  991. if (abits <= 7) {
  992. /* Block code */
  993. int block_code1, block_code2, size, levels, err;
  994. size = abits_sizes[abits - 1];
  995. levels = abits_levels[abits - 1];
  996. block_code1 = get_bits(&s->gb, size);
  997. block_code2 = get_bits(&s->gb, size);
  998. err = decode_blockcodes(block_code1, block_code2,
  999. levels, block + 8 * l);
  1000. if (err) {
  1001. av_log(s->avctx, AV_LOG_ERROR,
  1002. "ERROR: block code look-up failed\n");
  1003. return AVERROR_INVALIDDATA;
  1004. }
  1005. } else {
  1006. /* no coding */
  1007. for (m = 0; m < 8; m++)
  1008. block[8 * l + m] = get_sbits(&s->gb, abits - 3);
  1009. }
  1010. } else {
  1011. /* Huffman coded */
  1012. for (m = 0; m < 8; m++)
  1013. block[8 * l + m] = get_bitalloc(&s->gb,
  1014. &dca_smpl_bitalloc[abits], sel);
  1015. }
  1016. }
  1017. }
  1018. s->fmt_conv.int32_to_float_fmul_array8(&s->fmt_conv, subband_samples[k][0],
  1019. block, rscale, 8 * s->vq_start_subband[k]);
  1020. for (l = 0; l < s->vq_start_subband[k]; l++) {
  1021. int m;
  1022. /*
  1023. * Inverse ADPCM if in prediction mode
  1024. */
  1025. if (s->prediction_mode[k][l]) {
  1026. int n;
  1027. if (s->predictor_history)
  1028. subband_samples[k][l][0] += (adpcm_vb[s->prediction_vq[k][l]][0] *
  1029. s->subband_samples_hist[k][l][3] +
  1030. adpcm_vb[s->prediction_vq[k][l]][1] *
  1031. s->subband_samples_hist[k][l][2] +
  1032. adpcm_vb[s->prediction_vq[k][l]][2] *
  1033. s->subband_samples_hist[k][l][1] +
  1034. adpcm_vb[s->prediction_vq[k][l]][3] *
  1035. s->subband_samples_hist[k][l][0]) *
  1036. (1.0f / 8192);
  1037. for (m = 1; m < 8; m++) {
  1038. float sum = adpcm_vb[s->prediction_vq[k][l]][0] *
  1039. subband_samples[k][l][m - 1];
  1040. for (n = 2; n <= 4; n++)
  1041. if (m >= n)
  1042. sum += adpcm_vb[s->prediction_vq[k][l]][n - 1] *
  1043. subband_samples[k][l][m - n];
  1044. else if (s->predictor_history)
  1045. sum += adpcm_vb[s->prediction_vq[k][l]][n - 1] *
  1046. s->subband_samples_hist[k][l][m - n + 4];
  1047. subband_samples[k][l][m] += sum * 1.0f / 8192;
  1048. }
  1049. }
  1050. }
  1051. /*
  1052. * Decode VQ encoded high frequencies
  1053. */
  1054. if (s->subband_activity[k] > s->vq_start_subband[k]) {
  1055. if (!s->debug_flag & 0x01) {
  1056. av_log(s->avctx, AV_LOG_DEBUG,
  1057. "Stream with high frequencies VQ coding\n");
  1058. s->debug_flag |= 0x01;
  1059. }
  1060. s->dcadsp.decode_hf(subband_samples[k], s->high_freq_vq[k],
  1061. high_freq_vq, subsubframe * 8,
  1062. s->scale_factor[k], s->vq_start_subband[k],
  1063. s->subband_activity[k]);
  1064. }
  1065. }
  1066. /* Check for DSYNC after subsubframe */
  1067. if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
  1068. if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
  1069. #ifdef TRACE
  1070. av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
  1071. #endif
  1072. } else {
  1073. av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
  1074. return AVERROR_INVALIDDATA;
  1075. }
  1076. }
  1077. /* Backup predictor history for adpcm */
  1078. for (k = base_channel; k < s->prim_channels; k++)
  1079. for (l = 0; l < s->vq_start_subband[k]; l++)
  1080. AV_COPY128(s->subband_samples_hist[k][l], &subband_samples[k][l][4]);
  1081. return 0;
  1082. }
  1083. static int dca_filter_channels(DCAContext *s, int block_index)
  1084. {
  1085. float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
  1086. int k;
  1087. /* 32 subbands QMF */
  1088. for (k = 0; k < s->prim_channels; k++) {
  1089. if (s->channel_order_tab[k] >= 0)
  1090. qmf_32_subbands(s, k, subband_samples[k],
  1091. s->samples_chanptr[s->channel_order_tab[k]],
  1092. M_SQRT1_2 / 32768.0);
  1093. }
  1094. /* Generate LFE samples for this subsubframe FIXME!!! */
  1095. if (s->lfe) {
  1096. lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
  1097. s->lfe_data + 2 * s->lfe * (block_index + 4),
  1098. s->samples_chanptr[dca_lfe_index[s->amode]]);
  1099. /* Outputs 20bits pcm samples */
  1100. }
  1101. /* Downmixing to Stereo */
  1102. if (s->prim_channels + !!s->lfe > 2 &&
  1103. s->avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
  1104. dca_downmix(s->samples_chanptr, s->amode, !!s->lfe, s->downmix_coef,
  1105. s->channel_order_tab);
  1106. }
  1107. return 0;
  1108. }
  1109. static int dca_subframe_footer(DCAContext *s, int base_channel)
  1110. {
  1111. int in, out, aux_data_count, aux_data_end, reserved;
  1112. uint32_t nsyncaux;
  1113. /*
  1114. * Unpack optional information
  1115. */
  1116. /* presumably optional information only appears in the core? */
  1117. if (!base_channel) {
  1118. if (s->timestamp)
  1119. skip_bits_long(&s->gb, 32);
  1120. if (s->aux_data) {
  1121. aux_data_count = get_bits(&s->gb, 6);
  1122. // align (32-bit)
  1123. skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
  1124. aux_data_end = 8 * aux_data_count + get_bits_count(&s->gb);
  1125. if ((nsyncaux = get_bits_long(&s->gb, 32)) != DCA_NSYNCAUX) {
  1126. av_log(s->avctx, AV_LOG_ERROR, "nSYNCAUX mismatch %#"PRIx32"\n",
  1127. nsyncaux);
  1128. return AVERROR_INVALIDDATA;
  1129. }
  1130. if (get_bits1(&s->gb)) { // bAUXTimeStampFlag
  1131. avpriv_request_sample(s->avctx,
  1132. "Auxiliary Decode Time Stamp Flag");
  1133. // align (4-bit)
  1134. skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 4);
  1135. // 44 bits: nMSByte (8), nMarker (4), nLSByte (28), nMarker (4)
  1136. skip_bits_long(&s->gb, 44);
  1137. }
  1138. if ((s->core_downmix = get_bits1(&s->gb))) {
  1139. int am = get_bits(&s->gb, 3);
  1140. switch (am) {
  1141. case 0:
  1142. s->core_downmix_amode = DCA_MONO;
  1143. break;
  1144. case 1:
  1145. s->core_downmix_amode = DCA_STEREO;
  1146. break;
  1147. case 2:
  1148. s->core_downmix_amode = DCA_STEREO_TOTAL;
  1149. break;
  1150. case 3:
  1151. s->core_downmix_amode = DCA_3F;
  1152. break;
  1153. case 4:
  1154. s->core_downmix_amode = DCA_2F1R;
  1155. break;
  1156. case 5:
  1157. s->core_downmix_amode = DCA_2F2R;
  1158. break;
  1159. case 6:
  1160. s->core_downmix_amode = DCA_3F1R;
  1161. break;
  1162. default:
  1163. av_log(s->avctx, AV_LOG_ERROR,
  1164. "Invalid mode %d for embedded downmix coefficients\n",
  1165. am);
  1166. return AVERROR_INVALIDDATA;
  1167. }
  1168. for (out = 0; out < dca_channels[s->core_downmix_amode]; out++) {
  1169. for (in = 0; in < s->prim_channels + !!s->lfe; in++) {
  1170. uint16_t tmp = get_bits(&s->gb, 9);
  1171. if ((tmp & 0xFF) > 241) {
  1172. av_log(s->avctx, AV_LOG_ERROR,
  1173. "Invalid downmix coefficient code %"PRIu16"\n",
  1174. tmp);
  1175. return AVERROR_INVALIDDATA;
  1176. }
  1177. s->core_downmix_codes[in][out] = tmp;
  1178. }
  1179. }
  1180. }
  1181. align_get_bits(&s->gb); // byte align
  1182. skip_bits(&s->gb, 16); // nAUXCRC16
  1183. // additional data (reserved, cf. ETSI TS 102 114 V1.4.1)
  1184. if ((reserved = (aux_data_end - get_bits_count(&s->gb))) < 0) {
  1185. av_log(s->avctx, AV_LOG_ERROR,
  1186. "Overread auxiliary data by %d bits\n", -reserved);
  1187. return AVERROR_INVALIDDATA;
  1188. } else if (reserved) {
  1189. avpriv_request_sample(s->avctx,
  1190. "Core auxiliary data reserved content");
  1191. skip_bits_long(&s->gb, reserved);
  1192. }
  1193. }
  1194. if (s->crc_present && s->dynrange)
  1195. get_bits(&s->gb, 16);
  1196. }
  1197. return 0;
  1198. }
  1199. /**
  1200. * Decode a dca frame block
  1201. *
  1202. * @param s pointer to the DCAContext
  1203. */
  1204. static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
  1205. {
  1206. int ret;
  1207. /* Sanity check */
  1208. if (s->current_subframe >= s->subframes) {
  1209. av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
  1210. s->current_subframe, s->subframes);
  1211. return AVERROR_INVALIDDATA;
  1212. }
  1213. if (!s->current_subsubframe) {
  1214. #ifdef TRACE
  1215. av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
  1216. #endif
  1217. /* Read subframe header */
  1218. if ((ret = dca_subframe_header(s, base_channel, block_index)))
  1219. return ret;
  1220. }
  1221. /* Read subsubframe */
  1222. #ifdef TRACE
  1223. av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
  1224. #endif
  1225. if ((ret = dca_subsubframe(s, base_channel, block_index)))
  1226. return ret;
  1227. /* Update state */
  1228. s->current_subsubframe++;
  1229. if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
  1230. s->current_subsubframe = 0;
  1231. s->current_subframe++;
  1232. }
  1233. if (s->current_subframe >= s->subframes) {
  1234. #ifdef TRACE
  1235. av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
  1236. #endif
  1237. /* Read subframe footer */
  1238. if ((ret = dca_subframe_footer(s, base_channel)))
  1239. return ret;
  1240. }
  1241. return 0;
  1242. }
  1243. /**
  1244. * Return the number of channels in an ExSS speaker mask (HD)
  1245. */
  1246. static int dca_exss_mask2count(int mask)
  1247. {
  1248. /* count bits that mean speaker pairs twice */
  1249. return av_popcount(mask) +
  1250. av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
  1251. DCA_EXSS_FRONT_LEFT_RIGHT |
  1252. DCA_EXSS_FRONT_HIGH_LEFT_RIGHT |
  1253. DCA_EXSS_WIDE_LEFT_RIGHT |
  1254. DCA_EXSS_SIDE_LEFT_RIGHT |
  1255. DCA_EXSS_SIDE_HIGH_LEFT_RIGHT |
  1256. DCA_EXSS_SIDE_REAR_LEFT_RIGHT |
  1257. DCA_EXSS_REAR_LEFT_RIGHT |
  1258. DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
  1259. }
  1260. /**
  1261. * Skip mixing coefficients of a single mix out configuration (HD)
  1262. */
  1263. static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
  1264. {
  1265. int i;
  1266. for (i = 0; i < channels; i++) {
  1267. int mix_map_mask = get_bits(gb, out_ch);
  1268. int num_coeffs = av_popcount(mix_map_mask);
  1269. skip_bits_long(gb, num_coeffs * 6);
  1270. }
  1271. }
  1272. /**
  1273. * Parse extension substream asset header (HD)
  1274. */
  1275. static int dca_exss_parse_asset_header(DCAContext *s)
  1276. {
  1277. int header_pos = get_bits_count(&s->gb);
  1278. int header_size;
  1279. int channels = 0;
  1280. int embedded_stereo = 0;
  1281. int embedded_6ch = 0;
  1282. int drc_code_present;
  1283. int extensions_mask = 0;
  1284. int i, j;
  1285. if (get_bits_left(&s->gb) < 16)
  1286. return -1;
  1287. /* We will parse just enough to get to the extensions bitmask with which
  1288. * we can set the profile value. */
  1289. header_size = get_bits(&s->gb, 9) + 1;
  1290. skip_bits(&s->gb, 3); // asset index
  1291. if (s->static_fields) {
  1292. if (get_bits1(&s->gb))
  1293. skip_bits(&s->gb, 4); // asset type descriptor
  1294. if (get_bits1(&s->gb))
  1295. skip_bits_long(&s->gb, 24); // language descriptor
  1296. if (get_bits1(&s->gb)) {
  1297. /* How can one fit 1024 bytes of text here if the maximum value
  1298. * for the asset header size field above was 512 bytes? */
  1299. int text_length = get_bits(&s->gb, 10) + 1;
  1300. if (get_bits_left(&s->gb) < text_length * 8)
  1301. return -1;
  1302. skip_bits_long(&s->gb, text_length * 8); // info text
  1303. }
  1304. skip_bits(&s->gb, 5); // bit resolution - 1
  1305. skip_bits(&s->gb, 4); // max sample rate code
  1306. channels = get_bits(&s->gb, 8) + 1;
  1307. if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
  1308. int spkr_remap_sets;
  1309. int spkr_mask_size = 16;
  1310. int num_spkrs[7];
  1311. if (channels > 2)
  1312. embedded_stereo = get_bits1(&s->gb);
  1313. if (channels > 6)
  1314. embedded_6ch = get_bits1(&s->gb);
  1315. if (get_bits1(&s->gb)) {
  1316. spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
  1317. skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
  1318. }
  1319. spkr_remap_sets = get_bits(&s->gb, 3);
  1320. for (i = 0; i < spkr_remap_sets; i++) {
  1321. /* std layout mask for each remap set */
  1322. num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
  1323. }
  1324. for (i = 0; i < spkr_remap_sets; i++) {
  1325. int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
  1326. if (get_bits_left(&s->gb) < 0)
  1327. return -1;
  1328. for (j = 0; j < num_spkrs[i]; j++) {
  1329. int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
  1330. int num_dec_ch = av_popcount(remap_dec_ch_mask);
  1331. skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
  1332. }
  1333. }
  1334. } else {
  1335. skip_bits(&s->gb, 3); // representation type
  1336. }
  1337. }
  1338. drc_code_present = get_bits1(&s->gb);
  1339. if (drc_code_present)
  1340. get_bits(&s->gb, 8); // drc code
  1341. if (get_bits1(&s->gb))
  1342. skip_bits(&s->gb, 5); // dialog normalization code
  1343. if (drc_code_present && embedded_stereo)
  1344. get_bits(&s->gb, 8); // drc stereo code
  1345. if (s->mix_metadata && get_bits1(&s->gb)) {
  1346. skip_bits(&s->gb, 1); // external mix
  1347. skip_bits(&s->gb, 6); // post mix gain code
  1348. if (get_bits(&s->gb, 2) != 3) // mixer drc code
  1349. skip_bits(&s->gb, 3); // drc limit
  1350. else
  1351. skip_bits(&s->gb, 8); // custom drc code
  1352. if (get_bits1(&s->gb)) // channel specific scaling
  1353. for (i = 0; i < s->num_mix_configs; i++)
  1354. skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
  1355. else
  1356. skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
  1357. for (i = 0; i < s->num_mix_configs; i++) {
  1358. if (get_bits_left(&s->gb) < 0)
  1359. return -1;
  1360. dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
  1361. if (embedded_6ch)
  1362. dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
  1363. if (embedded_stereo)
  1364. dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
  1365. }
  1366. }
  1367. switch (get_bits(&s->gb, 2)) {
  1368. case 0:
  1369. extensions_mask = get_bits(&s->gb, 12);
  1370. break;
  1371. case 1:
  1372. extensions_mask = DCA_EXT_EXSS_XLL;
  1373. break;
  1374. case 2:
  1375. extensions_mask = DCA_EXT_EXSS_LBR;
  1376. break;
  1377. case 3:
  1378. extensions_mask = 0; /* aux coding */
  1379. break;
  1380. }
  1381. /* not parsed further, we were only interested in the extensions mask */
  1382. if (get_bits_left(&s->gb) < 0)
  1383. return -1;
  1384. if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
  1385. av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
  1386. return -1;
  1387. }
  1388. skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
  1389. if (extensions_mask & DCA_EXT_EXSS_XLL)
  1390. s->profile = FF_PROFILE_DTS_HD_MA;
  1391. else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
  1392. DCA_EXT_EXSS_XXCH))
  1393. s->profile = FF_PROFILE_DTS_HD_HRA;
  1394. if (!(extensions_mask & DCA_EXT_CORE))
  1395. av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
  1396. if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
  1397. av_log(s->avctx, AV_LOG_WARNING,
  1398. "DTS extensions detection mismatch (%d, %d)\n",
  1399. extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
  1400. return 0;
  1401. }
  1402. /**
  1403. * Parse extension substream header (HD)
  1404. */
  1405. static void dca_exss_parse_header(DCAContext *s)
  1406. {
  1407. int ss_index;
  1408. int blownup;
  1409. int num_audiop = 1;
  1410. int num_assets = 1;
  1411. int active_ss_mask[8];
  1412. int i, j;
  1413. if (get_bits_left(&s->gb) < 52)
  1414. return;
  1415. skip_bits(&s->gb, 8); // user data
  1416. ss_index = get_bits(&s->gb, 2);
  1417. blownup = get_bits1(&s->gb);
  1418. skip_bits(&s->gb, 8 + 4 * blownup); // header_size
  1419. skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
  1420. s->static_fields = get_bits1(&s->gb);
  1421. if (s->static_fields) {
  1422. skip_bits(&s->gb, 2); // reference clock code
  1423. skip_bits(&s->gb, 3); // frame duration code
  1424. if (get_bits1(&s->gb))
  1425. skip_bits_long(&s->gb, 36); // timestamp
  1426. /* a single stream can contain multiple audio assets that can be
  1427. * combined to form multiple audio presentations */
  1428. num_audiop = get_bits(&s->gb, 3) + 1;
  1429. if (num_audiop > 1) {
  1430. avpriv_request_sample(s->avctx,
  1431. "Multiple DTS-HD audio presentations");
  1432. /* ignore such streams for now */
  1433. return;
  1434. }
  1435. num_assets = get_bits(&s->gb, 3) + 1;
  1436. if (num_assets > 1) {
  1437. avpriv_request_sample(s->avctx, "Multiple DTS-HD audio assets");
  1438. /* ignore such streams for now */
  1439. return;
  1440. }
  1441. for (i = 0; i < num_audiop; i++)
  1442. active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
  1443. for (i = 0; i < num_audiop; i++)
  1444. for (j = 0; j <= ss_index; j++)
  1445. if (active_ss_mask[i] & (1 << j))
  1446. skip_bits(&s->gb, 8); // active asset mask
  1447. s->mix_metadata = get_bits1(&s->gb);
  1448. if (s->mix_metadata) {
  1449. int mix_out_mask_size;
  1450. skip_bits(&s->gb, 2); // adjustment level
  1451. mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
  1452. s->num_mix_configs = get_bits(&s->gb, 2) + 1;
  1453. for (i = 0; i < s->num_mix_configs; i++) {
  1454. int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
  1455. s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
  1456. }
  1457. }
  1458. }
  1459. for (i = 0; i < num_assets; i++)
  1460. skip_bits_long(&s->gb, 16 + 4 * blownup); // asset size
  1461. for (i = 0; i < num_assets; i++) {
  1462. if (dca_exss_parse_asset_header(s))
  1463. return;
  1464. }
  1465. /* not parsed further, we were only interested in the extensions mask
  1466. * from the asset header */
  1467. }
  1468. static float dca_dmix_code(unsigned code)
  1469. {
  1470. int sign = (code >> 8) - 1;
  1471. code &= 0xff;
  1472. return ((dca_dmixtable[code] ^ sign) - sign) * (1.0 / (1U << 15));
  1473. }
  1474. /**
  1475. * Main frame decoding function
  1476. * FIXME add arguments
  1477. */
  1478. static int dca_decode_frame(AVCodecContext *avctx, void *data,
  1479. int *got_frame_ptr, AVPacket *avpkt)
  1480. {
  1481. AVFrame *frame = data;
  1482. const uint8_t *buf = avpkt->data;
  1483. int buf_size = avpkt->size;
  1484. int lfe_samples;
  1485. int num_core_channels = 0;
  1486. int i, ret;
  1487. float **samples_flt;
  1488. DCAContext *s = avctx->priv_data;
  1489. int channels, full_channels;
  1490. int core_ss_end;
  1491. s->xch_present = 0;
  1492. s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
  1493. DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
  1494. if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
  1495. av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
  1496. return AVERROR_INVALIDDATA;
  1497. }
  1498. if ((ret = dca_parse_frame_header(s)) < 0) {
  1499. // seems like the frame is corrupt, try with the next one
  1500. return ret;
  1501. }
  1502. // set AVCodec values with parsed data
  1503. avctx->sample_rate = s->sample_rate;
  1504. avctx->bit_rate = s->bit_rate;
  1505. s->profile = FF_PROFILE_DTS;
  1506. for (i = 0; i < (s->sample_blocks / 8); i++) {
  1507. if ((ret = dca_decode_block(s, 0, i))) {
  1508. av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
  1509. return ret;
  1510. }
  1511. }
  1512. /* record number of core channels incase less than max channels are requested */
  1513. num_core_channels = s->prim_channels;
  1514. if (s->ext_coding)
  1515. s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
  1516. else
  1517. s->core_ext_mask = 0;
  1518. core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
  1519. /* only scan for extensions if ext_descr was unknown or indicated a
  1520. * supported XCh extension */
  1521. if (s->core_ext_mask < 0 || s->core_ext_mask & DCA_EXT_XCH) {
  1522. /* if ext_descr was unknown, clear s->core_ext_mask so that the
  1523. * extensions scan can fill it up */
  1524. s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
  1525. /* extensions start at 32-bit boundaries into bitstream */
  1526. skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
  1527. while (core_ss_end - get_bits_count(&s->gb) >= 32) {
  1528. uint32_t bits = get_bits_long(&s->gb, 32);
  1529. switch (bits) {
  1530. case 0x5a5a5a5a: {
  1531. int ext_amode, xch_fsize;
  1532. s->xch_base_channel = s->prim_channels;
  1533. /* validate sync word using XCHFSIZE field */
  1534. xch_fsize = show_bits(&s->gb, 10);
  1535. if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
  1536. (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
  1537. continue;
  1538. /* skip length-to-end-of-frame field for the moment */
  1539. skip_bits(&s->gb, 10);
  1540. s->core_ext_mask |= DCA_EXT_XCH;
  1541. /* extension amode(number of channels in extension) should be 1 */
  1542. /* AFAIK XCh is not used for more channels */
  1543. if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
  1544. av_log(avctx, AV_LOG_ERROR,
  1545. "XCh extension amode %d not supported!\n",
  1546. ext_amode);
  1547. continue;
  1548. }
  1549. /* much like core primary audio coding header */
  1550. dca_parse_audio_coding_header(s, s->xch_base_channel);
  1551. for (i = 0; i < (s->sample_blocks / 8); i++)
  1552. if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
  1553. av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
  1554. continue;
  1555. }
  1556. s->xch_present = 1;
  1557. break;
  1558. }
  1559. case 0x47004a03:
  1560. /* XXCh: extended channels */
  1561. /* usually found either in core or HD part in DTS-HD HRA streams,
  1562. * but not in DTS-ES which contains XCh extensions instead */
  1563. s->core_ext_mask |= DCA_EXT_XXCH;
  1564. break;
  1565. case 0x1d95f262: {
  1566. int fsize96 = show_bits(&s->gb, 12) + 1;
  1567. if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
  1568. continue;
  1569. av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
  1570. get_bits_count(&s->gb));
  1571. skip_bits(&s->gb, 12);
  1572. av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
  1573. av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
  1574. s->core_ext_mask |= DCA_EXT_X96;
  1575. break;
  1576. }
  1577. }
  1578. skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
  1579. }
  1580. } else {
  1581. /* no supported extensions, skip the rest of the core substream */
  1582. skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
  1583. }
  1584. if (s->core_ext_mask & DCA_EXT_X96)
  1585. s->profile = FF_PROFILE_DTS_96_24;
  1586. else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
  1587. s->profile = FF_PROFILE_DTS_ES;
  1588. /* check for ExSS (HD part) */
  1589. if (s->dca_buffer_size - s->frame_size > 32 &&
  1590. get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
  1591. dca_exss_parse_header(s);
  1592. avctx->profile = s->profile;
  1593. full_channels = channels = s->prim_channels + !!s->lfe;
  1594. if (s->amode < 16) {
  1595. avctx->channel_layout = dca_core_channel_layout[s->amode];
  1596. if (s->prim_channels + !!s->lfe > 2 &&
  1597. avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
  1598. /*
  1599. * Neither the core's auxiliary data nor our default tables contain
  1600. * downmix coefficients for the additional channel coded in the XCh
  1601. * extension, so when we're doing a Stereo downmix, don't decode it.
  1602. */
  1603. s->xch_disable = 1;
  1604. }
  1605. #if FF_API_REQUEST_CHANNELS
  1606. FF_DISABLE_DEPRECATION_WARNINGS
  1607. if (s->xch_present && !s->xch_disable &&
  1608. (!avctx->request_channels ||
  1609. avctx->request_channels > num_core_channels + !!s->lfe)) {
  1610. FF_ENABLE_DEPRECATION_WARNINGS
  1611. #else
  1612. if (s->xch_present && !s->xch_disable) {
  1613. #endif
  1614. avctx->channel_layout |= AV_CH_BACK_CENTER;
  1615. if (s->lfe) {
  1616. avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
  1617. s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
  1618. } else {
  1619. s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
  1620. }
  1621. } else {
  1622. channels = num_core_channels + !!s->lfe;
  1623. s->xch_present = 0; /* disable further xch processing */
  1624. if (s->lfe) {
  1625. avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
  1626. s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
  1627. } else
  1628. s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
  1629. }
  1630. if (channels > !!s->lfe &&
  1631. s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
  1632. return AVERROR_INVALIDDATA;
  1633. if (num_core_channels + !!s->lfe > 2 &&
  1634. avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
  1635. channels = 2;
  1636. s->output = s->prim_channels == 2 ? s->amode : DCA_STEREO;
  1637. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  1638. /* Stereo downmix coefficients
  1639. *
  1640. * The decoder can only downmix to 2-channel, so we need to ensure
  1641. * embedded downmix coefficients are actually targeting 2-channel.
  1642. */
  1643. if (s->core_downmix && (s->core_downmix_amode == DCA_STEREO ||
  1644. s->core_downmix_amode == DCA_STEREO_TOTAL)) {
  1645. for (i = 0; i < num_core_channels + !!s->lfe; i++) {
  1646. /* Range checked earlier */
  1647. s->downmix_coef[i][0] = dca_dmix_code(s->core_downmix_codes[i][0]);
  1648. s->downmix_coef[i][1] = dca_dmix_code(s->core_downmix_codes[i][1]);
  1649. }
  1650. s->output = s->core_downmix_amode;
  1651. } else {
  1652. int am = s->amode & DCA_CHANNEL_MASK;
  1653. if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
  1654. av_log(s->avctx, AV_LOG_ERROR,
  1655. "Invalid channel mode %d\n", am);
  1656. return AVERROR_INVALIDDATA;
  1657. }
  1658. if (num_core_channels + !!s->lfe >
  1659. FF_ARRAY_ELEMS(dca_default_coeffs[0])) {
  1660. avpriv_request_sample(s->avctx, "Downmixing %d channels",
  1661. s->prim_channels + !!s->lfe);
  1662. return AVERROR_PATCHWELCOME;
  1663. }
  1664. for (i = 0; i < num_core_channels + !!s->lfe; i++) {
  1665. s->downmix_coef[i][0] = dca_default_coeffs[am][i][0];
  1666. s->downmix_coef[i][1] = dca_default_coeffs[am][i][1];
  1667. }
  1668. }
  1669. av_dlog(s->avctx, "Stereo downmix coeffs:\n");
  1670. for (i = 0; i < num_core_channels + !!s->lfe; i++) {
  1671. av_dlog(s->avctx, "L, input channel %d = %f\n", i,
  1672. s->downmix_coef[i][0]);
  1673. av_dlog(s->avctx, "R, input channel %d = %f\n", i,
  1674. s->downmix_coef[i][1]);
  1675. }
  1676. av_dlog(s->avctx, "\n");
  1677. }
  1678. } else {
  1679. av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n", s->amode);
  1680. return AVERROR_INVALIDDATA;
  1681. }
  1682. avctx->channels = channels;
  1683. /* get output buffer */
  1684. frame->nb_samples = 256 * (s->sample_blocks / 8);
  1685. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  1686. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1687. return ret;
  1688. }
  1689. samples_flt = (float **) frame->extended_data;
  1690. /* allocate buffer for extra channels if downmixing */
  1691. if (avctx->channels < full_channels) {
  1692. ret = av_samples_get_buffer_size(NULL, full_channels - channels,
  1693. frame->nb_samples,
  1694. avctx->sample_fmt, 0);
  1695. if (ret < 0)
  1696. return ret;
  1697. av_fast_malloc(&s->extra_channels_buffer,
  1698. &s->extra_channels_buffer_size, ret);
  1699. if (!s->extra_channels_buffer)
  1700. return AVERROR(ENOMEM);
  1701. ret = av_samples_fill_arrays((uint8_t **) s->extra_channels, NULL,
  1702. s->extra_channels_buffer,
  1703. full_channels - channels,
  1704. frame->nb_samples, avctx->sample_fmt, 0);
  1705. if (ret < 0)
  1706. return ret;
  1707. }
  1708. /* filter to get final output */
  1709. for (i = 0; i < (s->sample_blocks / 8); i++) {
  1710. int ch;
  1711. for (ch = 0; ch < channels; ch++)
  1712. s->samples_chanptr[ch] = samples_flt[ch] + i * 256;
  1713. for (; ch < full_channels; ch++)
  1714. s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * 256;
  1715. dca_filter_channels(s, i);
  1716. /* If this was marked as a DTS-ES stream we need to subtract back- */
  1717. /* channel from SL & SR to remove matrixed back-channel signal */
  1718. if ((s->source_pcm_res & 1) && s->xch_present) {
  1719. float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
  1720. float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
  1721. float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
  1722. s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
  1723. s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
  1724. }
  1725. }
  1726. /* update lfe history */
  1727. lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
  1728. for (i = 0; i < 2 * s->lfe * 4; i++)
  1729. s->lfe_data[i] = s->lfe_data[i + lfe_samples];
  1730. /* AVMatrixEncoding
  1731. *
  1732. * DCA_STEREO_TOTAL (Lt/Rt) is equivalent to Dolby Surround */
  1733. ret = ff_side_data_update_matrix_encoding(frame,
  1734. (s->output & ~DCA_LFE) == DCA_STEREO_TOTAL ?
  1735. AV_MATRIX_ENCODING_DOLBY : AV_MATRIX_ENCODING_NONE);
  1736. if (ret < 0)
  1737. return ret;
  1738. *got_frame_ptr = 1;
  1739. return buf_size;
  1740. }
  1741. /**
  1742. * DCA initialization
  1743. *
  1744. * @param avctx pointer to the AVCodecContext
  1745. */
  1746. static av_cold int dca_decode_init(AVCodecContext *avctx)
  1747. {
  1748. DCAContext *s = avctx->priv_data;
  1749. s->avctx = avctx;
  1750. dca_init_vlcs();
  1751. avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
  1752. ff_mdct_init(&s->imdct, 6, 1, 1.0);
  1753. ff_synth_filter_init(&s->synth);
  1754. ff_dcadsp_init(&s->dcadsp);
  1755. ff_fmt_convert_init(&s->fmt_conv, avctx);
  1756. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  1757. /* allow downmixing to stereo */
  1758. #if FF_API_REQUEST_CHANNELS
  1759. FF_DISABLE_DEPRECATION_WARNINGS
  1760. if (avctx->request_channels == 2)
  1761. avctx->request_channel_layout = AV_CH_LAYOUT_STEREO;
  1762. FF_ENABLE_DEPRECATION_WARNINGS
  1763. #endif
  1764. if (avctx->channels > 2 &&
  1765. avctx->request_channel_layout == AV_CH_LAYOUT_STEREO)
  1766. avctx->channels = 2;
  1767. return 0;
  1768. }
  1769. static av_cold int dca_decode_end(AVCodecContext *avctx)
  1770. {
  1771. DCAContext *s = avctx->priv_data;
  1772. ff_mdct_end(&s->imdct);
  1773. av_freep(&s->extra_channels_buffer);
  1774. return 0;
  1775. }
  1776. static const AVProfile profiles[] = {
  1777. { FF_PROFILE_DTS, "DTS" },
  1778. { FF_PROFILE_DTS_ES, "DTS-ES" },
  1779. { FF_PROFILE_DTS_96_24, "DTS 96/24" },
  1780. { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
  1781. { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
  1782. { FF_PROFILE_UNKNOWN },
  1783. };
  1784. static const AVOption options[] = {
  1785. { "disable_xch", "disable decoding of the XCh extension", offsetof(DCAContext, xch_disable), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM },
  1786. { NULL },
  1787. };
  1788. static const AVClass dca_decoder_class = {
  1789. .class_name = "DCA decoder",
  1790. .item_name = av_default_item_name,
  1791. .option = options,
  1792. .version = LIBAVUTIL_VERSION_INT,
  1793. };
  1794. AVCodec ff_dca_decoder = {
  1795. .name = "dca",
  1796. .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
  1797. .type = AVMEDIA_TYPE_AUDIO,
  1798. .id = AV_CODEC_ID_DTS,
  1799. .priv_data_size = sizeof(DCAContext),
  1800. .init = dca_decode_init,
  1801. .decode = dca_decode_frame,
  1802. .close = dca_decode_end,
  1803. .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
  1804. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
  1805. AV_SAMPLE_FMT_NONE },
  1806. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  1807. .priv_class = &dca_decoder_class,
  1808. };