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.

2102 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 "fft.h"
  37. #include "get_bits.h"
  38. #include "put_bits.h"
  39. #include "dcadata.h"
  40. #include "dcahuff.h"
  41. #include "dca.h"
  42. #include "mathops.h"
  43. #include "synth_filter.h"
  44. #include "dcadsp.h"
  45. #include "fmtconvert.h"
  46. #include "internal.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. /* static float pcm_to_double[8] = { 32768.0, 32768.0, 524288.0, 524288.0,
  1090. 0, 8388608.0, 8388608.0 };*/
  1091. if (s->channel_order_tab[k] >= 0)
  1092. qmf_32_subbands(s, k, subband_samples[k],
  1093. s->samples_chanptr[s->channel_order_tab[k]],
  1094. M_SQRT1_2 / 32768.0 /* pcm_to_double[s->source_pcm_res] */);
  1095. }
  1096. /* Generate LFE samples for this subsubframe FIXME!!! */
  1097. if (s->lfe) {
  1098. lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
  1099. s->lfe_data + 2 * s->lfe * (block_index + 4),
  1100. s->samples_chanptr[dca_lfe_index[s->amode]]);
  1101. /* Outputs 20bits pcm samples */
  1102. }
  1103. /* Downmixing to Stereo */
  1104. if (s->prim_channels + !!s->lfe > 2 &&
  1105. s->avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
  1106. dca_downmix(s->samples_chanptr, s->amode, !!s->lfe, s->downmix_coef,
  1107. s->channel_order_tab);
  1108. }
  1109. return 0;
  1110. }
  1111. static int dca_subframe_footer(DCAContext *s, int base_channel)
  1112. {
  1113. int in, out, aux_data_count, aux_data_end, reserved;
  1114. uint32_t nsyncaux;
  1115. /*
  1116. * Unpack optional information
  1117. */
  1118. /* presumably optional information only appears in the core? */
  1119. if (!base_channel) {
  1120. if (s->timestamp)
  1121. skip_bits_long(&s->gb, 32);
  1122. if (s->aux_data) {
  1123. aux_data_count = get_bits(&s->gb, 6);
  1124. // align (32-bit)
  1125. skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
  1126. aux_data_end = 8 * aux_data_count + get_bits_count(&s->gb);
  1127. if ((nsyncaux = get_bits_long(&s->gb, 32)) != DCA_NSYNCAUX) {
  1128. av_log(s->avctx, AV_LOG_ERROR, "nSYNCAUX mismatch %#"PRIx32"\n",
  1129. nsyncaux);
  1130. return AVERROR_INVALIDDATA;
  1131. }
  1132. if (get_bits1(&s->gb)) { // bAUXTimeStampFlag
  1133. avpriv_request_sample(s->avctx,
  1134. "Auxiliary Decode Time Stamp Flag");
  1135. // align (4-bit)
  1136. skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 4);
  1137. // 44 bits: nMSByte (8), nMarker (4), nLSByte (28), nMarker (4)
  1138. skip_bits_long(&s->gb, 44);
  1139. }
  1140. if ((s->core_downmix = get_bits1(&s->gb))) {
  1141. int am = get_bits(&s->gb, 3);
  1142. switch (am) {
  1143. case 0:
  1144. s->core_downmix_amode = DCA_MONO;
  1145. break;
  1146. case 1:
  1147. s->core_downmix_amode = DCA_STEREO;
  1148. break;
  1149. case 2:
  1150. s->core_downmix_amode = DCA_STEREO_TOTAL;
  1151. break;
  1152. case 3:
  1153. s->core_downmix_amode = DCA_3F;
  1154. break;
  1155. case 4:
  1156. s->core_downmix_amode = DCA_2F1R;
  1157. break;
  1158. case 5:
  1159. s->core_downmix_amode = DCA_2F2R;
  1160. break;
  1161. case 6:
  1162. s->core_downmix_amode = DCA_3F1R;
  1163. break;
  1164. default:
  1165. av_log(s->avctx, AV_LOG_ERROR,
  1166. "Invalid mode %d for embedded downmix coefficients\n",
  1167. am);
  1168. return AVERROR_INVALIDDATA;
  1169. }
  1170. for (out = 0; out < dca_channels[s->core_downmix_amode]; out++) {
  1171. for (in = 0; in < s->prim_channels + !!s->lfe; in++) {
  1172. uint16_t tmp = get_bits(&s->gb, 9);
  1173. if ((tmp & 0xFF) > 241) {
  1174. av_log(s->avctx, AV_LOG_ERROR,
  1175. "Invalid downmix coefficient code %"PRIu16"\n",
  1176. tmp);
  1177. return AVERROR_INVALIDDATA;
  1178. }
  1179. s->core_downmix_codes[in][out] = tmp;
  1180. }
  1181. }
  1182. }
  1183. align_get_bits(&s->gb); // byte align
  1184. skip_bits(&s->gb, 16); // nAUXCRC16
  1185. // additional data (reserved, cf. ETSI TS 102 114 V1.4.1)
  1186. if ((reserved = (aux_data_end - get_bits_count(&s->gb))) < 0) {
  1187. av_log(s->avctx, AV_LOG_ERROR,
  1188. "Overread auxiliary data by %d bits\n", -reserved);
  1189. return AVERROR_INVALIDDATA;
  1190. } else if (reserved) {
  1191. avpriv_request_sample(s->avctx,
  1192. "Core auxiliary data reserved content");
  1193. skip_bits_long(&s->gb, reserved);
  1194. }
  1195. }
  1196. if (s->crc_present && s->dynrange)
  1197. get_bits(&s->gb, 16);
  1198. }
  1199. return 0;
  1200. }
  1201. /**
  1202. * Decode a dca frame block
  1203. *
  1204. * @param s pointer to the DCAContext
  1205. */
  1206. static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
  1207. {
  1208. int ret;
  1209. /* Sanity check */
  1210. if (s->current_subframe >= s->subframes) {
  1211. av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
  1212. s->current_subframe, s->subframes);
  1213. return AVERROR_INVALIDDATA;
  1214. }
  1215. if (!s->current_subsubframe) {
  1216. #ifdef TRACE
  1217. av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
  1218. #endif
  1219. /* Read subframe header */
  1220. if ((ret = dca_subframe_header(s, base_channel, block_index)))
  1221. return ret;
  1222. }
  1223. /* Read subsubframe */
  1224. #ifdef TRACE
  1225. av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
  1226. #endif
  1227. if ((ret = dca_subsubframe(s, base_channel, block_index)))
  1228. return ret;
  1229. /* Update state */
  1230. s->current_subsubframe++;
  1231. if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
  1232. s->current_subsubframe = 0;
  1233. s->current_subframe++;
  1234. }
  1235. if (s->current_subframe >= s->subframes) {
  1236. #ifdef TRACE
  1237. av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
  1238. #endif
  1239. /* Read subframe footer */
  1240. if ((ret = dca_subframe_footer(s, base_channel)))
  1241. return ret;
  1242. }
  1243. return 0;
  1244. }
  1245. /**
  1246. * Return the number of channels in an ExSS speaker mask (HD)
  1247. */
  1248. static int dca_exss_mask2count(int mask)
  1249. {
  1250. /* count bits that mean speaker pairs twice */
  1251. return av_popcount(mask) +
  1252. av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
  1253. DCA_EXSS_FRONT_LEFT_RIGHT |
  1254. DCA_EXSS_FRONT_HIGH_LEFT_RIGHT |
  1255. DCA_EXSS_WIDE_LEFT_RIGHT |
  1256. DCA_EXSS_SIDE_LEFT_RIGHT |
  1257. DCA_EXSS_SIDE_HIGH_LEFT_RIGHT |
  1258. DCA_EXSS_SIDE_REAR_LEFT_RIGHT |
  1259. DCA_EXSS_REAR_LEFT_RIGHT |
  1260. DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
  1261. }
  1262. /**
  1263. * Skip mixing coefficients of a single mix out configuration (HD)
  1264. */
  1265. static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
  1266. {
  1267. int i;
  1268. for (i = 0; i < channels; i++) {
  1269. int mix_map_mask = get_bits(gb, out_ch);
  1270. int num_coeffs = av_popcount(mix_map_mask);
  1271. skip_bits_long(gb, num_coeffs * 6);
  1272. }
  1273. }
  1274. /**
  1275. * Parse extension substream asset header (HD)
  1276. */
  1277. static int dca_exss_parse_asset_header(DCAContext *s)
  1278. {
  1279. int header_pos = get_bits_count(&s->gb);
  1280. int header_size;
  1281. int channels;
  1282. int embedded_stereo = 0;
  1283. int embedded_6ch = 0;
  1284. int drc_code_present;
  1285. int extensions_mask;
  1286. int i, j;
  1287. if (get_bits_left(&s->gb) < 16)
  1288. return -1;
  1289. /* We will parse just enough to get to the extensions bitmask with which
  1290. * we can set the profile value. */
  1291. header_size = get_bits(&s->gb, 9) + 1;
  1292. skip_bits(&s->gb, 3); // asset index
  1293. if (s->static_fields) {
  1294. if (get_bits1(&s->gb))
  1295. skip_bits(&s->gb, 4); // asset type descriptor
  1296. if (get_bits1(&s->gb))
  1297. skip_bits_long(&s->gb, 24); // language descriptor
  1298. if (get_bits1(&s->gb)) {
  1299. /* How can one fit 1024 bytes of text here if the maximum value
  1300. * for the asset header size field above was 512 bytes? */
  1301. int text_length = get_bits(&s->gb, 10) + 1;
  1302. if (get_bits_left(&s->gb) < text_length * 8)
  1303. return -1;
  1304. skip_bits_long(&s->gb, text_length * 8); // info text
  1305. }
  1306. skip_bits(&s->gb, 5); // bit resolution - 1
  1307. skip_bits(&s->gb, 4); // max sample rate code
  1308. channels = get_bits(&s->gb, 8) + 1;
  1309. if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
  1310. int spkr_remap_sets;
  1311. int spkr_mask_size = 16;
  1312. int num_spkrs[7];
  1313. if (channels > 2)
  1314. embedded_stereo = get_bits1(&s->gb);
  1315. if (channels > 6)
  1316. embedded_6ch = get_bits1(&s->gb);
  1317. if (get_bits1(&s->gb)) {
  1318. spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
  1319. skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
  1320. }
  1321. spkr_remap_sets = get_bits(&s->gb, 3);
  1322. for (i = 0; i < spkr_remap_sets; i++) {
  1323. /* std layout mask for each remap set */
  1324. num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
  1325. }
  1326. for (i = 0; i < spkr_remap_sets; i++) {
  1327. int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
  1328. if (get_bits_left(&s->gb) < 0)
  1329. return -1;
  1330. for (j = 0; j < num_spkrs[i]; j++) {
  1331. int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
  1332. int num_dec_ch = av_popcount(remap_dec_ch_mask);
  1333. skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
  1334. }
  1335. }
  1336. } else {
  1337. skip_bits(&s->gb, 3); // representation type
  1338. }
  1339. }
  1340. drc_code_present = get_bits1(&s->gb);
  1341. if (drc_code_present)
  1342. get_bits(&s->gb, 8); // drc code
  1343. if (get_bits1(&s->gb))
  1344. skip_bits(&s->gb, 5); // dialog normalization code
  1345. if (drc_code_present && embedded_stereo)
  1346. get_bits(&s->gb, 8); // drc stereo code
  1347. if (s->mix_metadata && get_bits1(&s->gb)) {
  1348. skip_bits(&s->gb, 1); // external mix
  1349. skip_bits(&s->gb, 6); // post mix gain code
  1350. if (get_bits(&s->gb, 2) != 3) // mixer drc code
  1351. skip_bits(&s->gb, 3); // drc limit
  1352. else
  1353. skip_bits(&s->gb, 8); // custom drc code
  1354. if (get_bits1(&s->gb)) // channel specific scaling
  1355. for (i = 0; i < s->num_mix_configs; i++)
  1356. skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
  1357. else
  1358. skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
  1359. for (i = 0; i < s->num_mix_configs; i++) {
  1360. if (get_bits_left(&s->gb) < 0)
  1361. return -1;
  1362. dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
  1363. if (embedded_6ch)
  1364. dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
  1365. if (embedded_stereo)
  1366. dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
  1367. }
  1368. }
  1369. switch (get_bits(&s->gb, 2)) {
  1370. case 0: extensions_mask = get_bits(&s->gb, 12); break;
  1371. case 1: extensions_mask = DCA_EXT_EXSS_XLL; break;
  1372. case 2: extensions_mask = DCA_EXT_EXSS_LBR; break;
  1373. case 3: extensions_mask = 0; /* aux coding */ break;
  1374. }
  1375. /* not parsed further, we were only interested in the extensions mask */
  1376. if (get_bits_left(&s->gb) < 0)
  1377. return -1;
  1378. if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
  1379. av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
  1380. return -1;
  1381. }
  1382. skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
  1383. if (extensions_mask & DCA_EXT_EXSS_XLL)
  1384. s->profile = FF_PROFILE_DTS_HD_MA;
  1385. else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
  1386. DCA_EXT_EXSS_XXCH))
  1387. s->profile = FF_PROFILE_DTS_HD_HRA;
  1388. if (!(extensions_mask & DCA_EXT_CORE))
  1389. av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
  1390. if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
  1391. av_log(s->avctx, AV_LOG_WARNING,
  1392. "DTS extensions detection mismatch (%d, %d)\n",
  1393. extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
  1394. return 0;
  1395. }
  1396. /**
  1397. * Parse extension substream header (HD)
  1398. */
  1399. static void dca_exss_parse_header(DCAContext *s)
  1400. {
  1401. int ss_index;
  1402. int blownup;
  1403. int num_audiop = 1;
  1404. int num_assets = 1;
  1405. int active_ss_mask[8];
  1406. int i, j;
  1407. if (get_bits_left(&s->gb) < 52)
  1408. return;
  1409. skip_bits(&s->gb, 8); // user data
  1410. ss_index = get_bits(&s->gb, 2);
  1411. blownup = get_bits1(&s->gb);
  1412. skip_bits(&s->gb, 8 + 4 * blownup); // header_size
  1413. skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
  1414. s->static_fields = get_bits1(&s->gb);
  1415. if (s->static_fields) {
  1416. skip_bits(&s->gb, 2); // reference clock code
  1417. skip_bits(&s->gb, 3); // frame duration code
  1418. if (get_bits1(&s->gb))
  1419. skip_bits_long(&s->gb, 36); // timestamp
  1420. /* a single stream can contain multiple audio assets that can be
  1421. * combined to form multiple audio presentations */
  1422. num_audiop = get_bits(&s->gb, 3) + 1;
  1423. if (num_audiop > 1) {
  1424. avpriv_request_sample(s->avctx,
  1425. "Multiple DTS-HD audio presentations");
  1426. /* ignore such streams for now */
  1427. return;
  1428. }
  1429. num_assets = get_bits(&s->gb, 3) + 1;
  1430. if (num_assets > 1) {
  1431. avpriv_request_sample(s->avctx, "Multiple DTS-HD audio assets");
  1432. /* ignore such streams for now */
  1433. return;
  1434. }
  1435. for (i = 0; i < num_audiop; i++)
  1436. active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
  1437. for (i = 0; i < num_audiop; i++)
  1438. for (j = 0; j <= ss_index; j++)
  1439. if (active_ss_mask[i] & (1 << j))
  1440. skip_bits(&s->gb, 8); // active asset mask
  1441. s->mix_metadata = get_bits1(&s->gb);
  1442. if (s->mix_metadata) {
  1443. int mix_out_mask_size;
  1444. skip_bits(&s->gb, 2); // adjustment level
  1445. mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
  1446. s->num_mix_configs = get_bits(&s->gb, 2) + 1;
  1447. for (i = 0; i < s->num_mix_configs; i++) {
  1448. int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
  1449. s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
  1450. }
  1451. }
  1452. }
  1453. for (i = 0; i < num_assets; i++)
  1454. skip_bits_long(&s->gb, 16 + 4 * blownup); // asset size
  1455. for (i = 0; i < num_assets; i++) {
  1456. if (dca_exss_parse_asset_header(s))
  1457. return;
  1458. }
  1459. /* not parsed further, we were only interested in the extensions mask
  1460. * from the asset header */
  1461. }
  1462. static float dca_dmix_code(unsigned code)
  1463. {
  1464. int sign = (code >> 8) - 1;
  1465. code &= 0xff;
  1466. return ldexpf((dca_dmixtable[code] ^ sign) - sign, -15);
  1467. }
  1468. /**
  1469. * Main frame decoding function
  1470. * FIXME add arguments
  1471. */
  1472. static int dca_decode_frame(AVCodecContext *avctx, void *data,
  1473. int *got_frame_ptr, AVPacket *avpkt)
  1474. {
  1475. AVFrame *frame = data;
  1476. const uint8_t *buf = avpkt->data;
  1477. int buf_size = avpkt->size;
  1478. int lfe_samples;
  1479. int num_core_channels = 0;
  1480. int i, ret;
  1481. float **samples_flt;
  1482. DCAContext *s = avctx->priv_data;
  1483. int channels, full_channels;
  1484. int core_ss_end;
  1485. s->xch_present = 0;
  1486. s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
  1487. DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
  1488. if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
  1489. av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
  1490. return AVERROR_INVALIDDATA;
  1491. }
  1492. if ((ret = dca_parse_frame_header(s)) < 0) {
  1493. //seems like the frame is corrupt, try with the next one
  1494. return ret;
  1495. }
  1496. //set AVCodec values with parsed data
  1497. avctx->sample_rate = s->sample_rate;
  1498. avctx->bit_rate = s->bit_rate;
  1499. s->profile = FF_PROFILE_DTS;
  1500. for (i = 0; i < (s->sample_blocks / 8); i++) {
  1501. if ((ret = dca_decode_block(s, 0, i))) {
  1502. av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
  1503. return ret;
  1504. }
  1505. }
  1506. /* record number of core channels incase less than max channels are requested */
  1507. num_core_channels = s->prim_channels;
  1508. if (s->ext_coding)
  1509. s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
  1510. else
  1511. s->core_ext_mask = 0;
  1512. core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
  1513. /* only scan for extensions if ext_descr was unknown or indicated a
  1514. * supported XCh extension */
  1515. if (s->core_ext_mask < 0 || s->core_ext_mask & DCA_EXT_XCH) {
  1516. /* if ext_descr was unknown, clear s->core_ext_mask so that the
  1517. * extensions scan can fill it up */
  1518. s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
  1519. /* extensions start at 32-bit boundaries into bitstream */
  1520. skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
  1521. while (core_ss_end - get_bits_count(&s->gb) >= 32) {
  1522. uint32_t bits = get_bits_long(&s->gb, 32);
  1523. switch (bits) {
  1524. case 0x5a5a5a5a: {
  1525. int ext_amode, xch_fsize;
  1526. s->xch_base_channel = s->prim_channels;
  1527. /* validate sync word using XCHFSIZE field */
  1528. xch_fsize = show_bits(&s->gb, 10);
  1529. if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
  1530. (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
  1531. continue;
  1532. /* skip length-to-end-of-frame field for the moment */
  1533. skip_bits(&s->gb, 10);
  1534. s->core_ext_mask |= DCA_EXT_XCH;
  1535. /* extension amode(number of channels in extension) should be 1 */
  1536. /* AFAIK XCh is not used for more channels */
  1537. if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
  1538. av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
  1539. " supported!\n", ext_amode);
  1540. continue;
  1541. }
  1542. /* much like core primary audio coding header */
  1543. dca_parse_audio_coding_header(s, s->xch_base_channel);
  1544. for (i = 0; i < (s->sample_blocks / 8); i++)
  1545. if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
  1546. av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
  1547. continue;
  1548. }
  1549. s->xch_present = 1;
  1550. break;
  1551. }
  1552. case 0x47004a03:
  1553. /* XXCh: extended channels */
  1554. /* usually found either in core or HD part in DTS-HD HRA streams,
  1555. * but not in DTS-ES which contains XCh extensions instead */
  1556. s->core_ext_mask |= DCA_EXT_XXCH;
  1557. break;
  1558. case 0x1d95f262: {
  1559. int fsize96 = show_bits(&s->gb, 12) + 1;
  1560. if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
  1561. continue;
  1562. av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
  1563. get_bits_count(&s->gb));
  1564. skip_bits(&s->gb, 12);
  1565. av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
  1566. av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
  1567. s->core_ext_mask |= DCA_EXT_X96;
  1568. break;
  1569. }
  1570. }
  1571. skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
  1572. }
  1573. } else {
  1574. /* no supported extensions, skip the rest of the core substream */
  1575. skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
  1576. }
  1577. if (s->core_ext_mask & DCA_EXT_X96)
  1578. s->profile = FF_PROFILE_DTS_96_24;
  1579. else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
  1580. s->profile = FF_PROFILE_DTS_ES;
  1581. /* check for ExSS (HD part) */
  1582. if (s->dca_buffer_size - s->frame_size > 32 &&
  1583. get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
  1584. dca_exss_parse_header(s);
  1585. avctx->profile = s->profile;
  1586. full_channels = channels = s->prim_channels + !!s->lfe;
  1587. if (s->amode < 16) {
  1588. avctx->channel_layout = dca_core_channel_layout[s->amode];
  1589. if (s->prim_channels + !!s->lfe > 2 &&
  1590. avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
  1591. /*
  1592. * Neither the core's auxiliary data nor our default tables contain
  1593. * downmix coefficients for the additional channel coded in the XCh
  1594. * extension, so when we're doing a Stereo downmix, don't decode it.
  1595. */
  1596. s->xch_disable = 1;
  1597. }
  1598. #if FF_API_REQUEST_CHANNELS
  1599. FF_DISABLE_DEPRECATION_WARNINGS
  1600. if (s->xch_present && !s->xch_disable &&
  1601. (!avctx->request_channels ||
  1602. avctx->request_channels > num_core_channels + !!s->lfe)) {
  1603. FF_ENABLE_DEPRECATION_WARNINGS
  1604. #else
  1605. if (s->xch_present && !s->xch_disable) {
  1606. #endif
  1607. avctx->channel_layout |= AV_CH_BACK_CENTER;
  1608. if (s->lfe) {
  1609. avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
  1610. s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
  1611. } else {
  1612. s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
  1613. }
  1614. } else {
  1615. channels = num_core_channels + !!s->lfe;
  1616. s->xch_present = 0; /* disable further xch processing */
  1617. if (s->lfe) {
  1618. avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
  1619. s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
  1620. } else
  1621. s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
  1622. }
  1623. if (channels > !!s->lfe &&
  1624. s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
  1625. return AVERROR_INVALIDDATA;
  1626. if (num_core_channels + !!s->lfe > 2 &&
  1627. avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
  1628. channels = 2;
  1629. s->output = s->prim_channels == 2 ? s->amode : DCA_STEREO;
  1630. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  1631. /* Stereo downmix coefficients
  1632. *
  1633. * The decoder can only downmix to 2-channel, so we need to ensure
  1634. * embedded downmix coefficients are actually targeting 2-channel.
  1635. */
  1636. if (s->core_downmix && (s->core_downmix_amode == DCA_STEREO ||
  1637. s->core_downmix_amode == DCA_STEREO_TOTAL)) {
  1638. for (i = 0; i < num_core_channels + !!s->lfe; i++) {
  1639. /* Range checked earlier */
  1640. s->downmix_coef[i][0] = dca_dmix_code(s->core_downmix_codes[i][0]);
  1641. s->downmix_coef[i][1] = dca_dmix_code(s->core_downmix_codes[i][1]);
  1642. }
  1643. s->output = s->core_downmix_amode;
  1644. } else {
  1645. int am = s->amode & DCA_CHANNEL_MASK;
  1646. if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
  1647. av_log(s->avctx, AV_LOG_ERROR,
  1648. "Invalid channel mode %d\n", am);
  1649. return AVERROR_INVALIDDATA;
  1650. }
  1651. if (num_core_channels + !!s->lfe >
  1652. FF_ARRAY_ELEMS(dca_default_coeffs[0])) {
  1653. avpriv_request_sample(s->avctx, "Downmixing %d channels",
  1654. s->prim_channels + !!s->lfe);
  1655. return AVERROR_PATCHWELCOME;
  1656. }
  1657. for (i = 0; i < num_core_channels + !!s->lfe; i++) {
  1658. s->downmix_coef[i][0] = dca_default_coeffs[am][i][0];
  1659. s->downmix_coef[i][1] = dca_default_coeffs[am][i][1];
  1660. }
  1661. }
  1662. av_dlog(s->avctx, "Stereo downmix coeffs:\n");
  1663. for (i = 0; i < num_core_channels + !!s->lfe; i++) {
  1664. av_dlog(s->avctx, "L, input channel %d = %f\n", i,
  1665. s->downmix_coef[i][0]);
  1666. av_dlog(s->avctx, "R, input channel %d = %f\n", i,
  1667. s->downmix_coef[i][1]);
  1668. }
  1669. av_dlog(s->avctx, "\n");
  1670. }
  1671. } else {
  1672. av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n", s->amode);
  1673. return AVERROR_INVALIDDATA;
  1674. }
  1675. avctx->channels = channels;
  1676. /* get output buffer */
  1677. frame->nb_samples = 256 * (s->sample_blocks / 8);
  1678. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  1679. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1680. return ret;
  1681. }
  1682. samples_flt = (float **)frame->extended_data;
  1683. /* allocate buffer for extra channels if downmixing */
  1684. if (avctx->channels < full_channels) {
  1685. ret = av_samples_get_buffer_size(NULL, full_channels - channels,
  1686. frame->nb_samples,
  1687. avctx->sample_fmt, 0);
  1688. if (ret < 0)
  1689. return ret;
  1690. av_fast_malloc(&s->extra_channels_buffer,
  1691. &s->extra_channels_buffer_size, ret);
  1692. if (!s->extra_channels_buffer)
  1693. return AVERROR(ENOMEM);
  1694. ret = av_samples_fill_arrays((uint8_t **)s->extra_channels, NULL,
  1695. s->extra_channels_buffer,
  1696. full_channels - channels,
  1697. frame->nb_samples, avctx->sample_fmt, 0);
  1698. if (ret < 0)
  1699. return ret;
  1700. }
  1701. /* filter to get final output */
  1702. for (i = 0; i < (s->sample_blocks / 8); i++) {
  1703. int ch;
  1704. for (ch = 0; ch < channels; ch++)
  1705. s->samples_chanptr[ch] = samples_flt[ch] + i * 256;
  1706. for (; ch < full_channels; ch++)
  1707. s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * 256;
  1708. dca_filter_channels(s, i);
  1709. /* If this was marked as a DTS-ES stream we need to subtract back- */
  1710. /* channel from SL & SR to remove matrixed back-channel signal */
  1711. if ((s->source_pcm_res & 1) && s->xch_present) {
  1712. float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
  1713. float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
  1714. float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
  1715. s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
  1716. s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
  1717. }
  1718. }
  1719. /* update lfe history */
  1720. lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
  1721. for (i = 0; i < 2 * s->lfe * 4; i++)
  1722. s->lfe_data[i] = s->lfe_data[i + lfe_samples];
  1723. /* AVMatrixEncoding
  1724. *
  1725. * DCA_STEREO_TOTAL (Lt/Rt) is equivalent to Dolby Surround */
  1726. ret = ff_side_data_update_matrix_encoding(frame,
  1727. (s->output & ~DCA_LFE) == DCA_STEREO_TOTAL ?
  1728. AV_MATRIX_ENCODING_DOLBY : AV_MATRIX_ENCODING_NONE);
  1729. if (ret < 0)
  1730. return ret;
  1731. *got_frame_ptr = 1;
  1732. return buf_size;
  1733. }
  1734. /**
  1735. * DCA initialization
  1736. *
  1737. * @param avctx pointer to the AVCodecContext
  1738. */
  1739. static av_cold int dca_decode_init(AVCodecContext *avctx)
  1740. {
  1741. DCAContext *s = avctx->priv_data;
  1742. s->avctx = avctx;
  1743. dca_init_vlcs();
  1744. avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
  1745. ff_mdct_init(&s->imdct, 6, 1, 1.0);
  1746. ff_synth_filter_init(&s->synth);
  1747. ff_dcadsp_init(&s->dcadsp);
  1748. ff_fmt_convert_init(&s->fmt_conv, avctx);
  1749. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  1750. /* allow downmixing to stereo */
  1751. #if FF_API_REQUEST_CHANNELS
  1752. FF_DISABLE_DEPRECATION_WARNINGS
  1753. if (avctx->request_channels == 2)
  1754. avctx->request_channel_layout = AV_CH_LAYOUT_STEREO;
  1755. FF_ENABLE_DEPRECATION_WARNINGS
  1756. #endif
  1757. if (avctx->channels > 2 &&
  1758. avctx->request_channel_layout == AV_CH_LAYOUT_STEREO)
  1759. avctx->channels = 2;
  1760. return 0;
  1761. }
  1762. static av_cold int dca_decode_end(AVCodecContext *avctx)
  1763. {
  1764. DCAContext *s = avctx->priv_data;
  1765. ff_mdct_end(&s->imdct);
  1766. av_freep(&s->extra_channels_buffer);
  1767. return 0;
  1768. }
  1769. static const AVProfile profiles[] = {
  1770. { FF_PROFILE_DTS, "DTS" },
  1771. { FF_PROFILE_DTS_ES, "DTS-ES" },
  1772. { FF_PROFILE_DTS_96_24, "DTS 96/24" },
  1773. { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
  1774. { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
  1775. { FF_PROFILE_UNKNOWN },
  1776. };
  1777. static const AVOption options[] = {
  1778. { "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 },
  1779. { NULL },
  1780. };
  1781. static const AVClass dca_decoder_class = {
  1782. .class_name = "DCA decoder",
  1783. .item_name = av_default_item_name,
  1784. .option = options,
  1785. .version = LIBAVUTIL_VERSION_INT,
  1786. };
  1787. AVCodec ff_dca_decoder = {
  1788. .name = "dca",
  1789. .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
  1790. .type = AVMEDIA_TYPE_AUDIO,
  1791. .id = AV_CODEC_ID_DTS,
  1792. .priv_data_size = sizeof(DCAContext),
  1793. .init = dca_decode_init,
  1794. .decode = dca_decode_frame,
  1795. .close = dca_decode_end,
  1796. .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
  1797. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
  1798. AV_SAMPLE_FMT_NONE },
  1799. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  1800. .priv_class = &dca_decoder_class,
  1801. };