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.

2099 lines
78KB

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