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.

1969 lines
73KB

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