You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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