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.

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