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.

1961 lines
72KB

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