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.

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