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.

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