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.

1965 lines
72KB

  1. /*
  2. * DCA compatible decoder
  3. * Copyright (C) 2004 Gildas Bazin
  4. * Copyright (C) 2004 Benjamin Zores
  5. * Copyright (C) 2006 Benjamin Larsson
  6. * Copyright (C) 2007 Konstantin Shishkov
  7. *
  8. * This file is part of Libav.
  9. *
  10. * Libav is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * Libav is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with Libav; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include <math.h>
  25. #include <stddef.h>
  26. #include <stdio.h>
  27. #include "libavutil/channel_layout.h"
  28. #include "libavutil/common.h"
  29. #include "libavutil/float_dsp.h"
  30. #include "libavutil/intmath.h"
  31. #include "libavutil/intreadwrite.h"
  32. #include "libavutil/mathematics.h"
  33. #include "libavutil/samplefmt.h"
  34. #include "avcodec.h"
  35. #include "dsputil.h"
  36. #include "fft.h"
  37. #include "get_bits.h"
  38. #include "put_bits.h"
  39. #include "dcadata.h"
  40. #include "dcahuff.h"
  41. #include "dca.h"
  42. #include "dca_parser.h"
  43. #include "synth_filter.h"
  44. #include "dcadsp.h"
  45. #include "fmtconvert.h"
  46. #include "internal.h"
  47. #if ARCH_ARM
  48. # include "arm/dca.h"
  49. #endif
  50. //#define TRACE
  51. #define DCA_PRIM_CHANNELS_MAX (7)
  52. #define DCA_SUBBANDS (32)
  53. #define DCA_ABITS_MAX (32) /* Should be 28 */
  54. #define DCA_SUBSUBFRAMES_MAX (4)
  55. #define DCA_SUBFRAMES_MAX (16)
  56. #define DCA_BLOCKS_MAX (16)
  57. #define DCA_LFE_MAX (3)
  58. enum DCAMode {
  59. DCA_MONO = 0,
  60. DCA_CHANNEL,
  61. DCA_STEREO,
  62. DCA_STEREO_SUMDIFF,
  63. DCA_STEREO_TOTAL,
  64. DCA_3F,
  65. DCA_2F1R,
  66. DCA_3F1R,
  67. DCA_2F2R,
  68. DCA_3F2R,
  69. DCA_4F2R
  70. };
  71. /* these are unconfirmed but should be mostly correct */
  72. enum DCAExSSSpeakerMask {
  73. DCA_EXSS_FRONT_CENTER = 0x0001,
  74. DCA_EXSS_FRONT_LEFT_RIGHT = 0x0002,
  75. DCA_EXSS_SIDE_REAR_LEFT_RIGHT = 0x0004,
  76. DCA_EXSS_LFE = 0x0008,
  77. DCA_EXSS_REAR_CENTER = 0x0010,
  78. DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
  79. DCA_EXSS_REAR_LEFT_RIGHT = 0x0040,
  80. DCA_EXSS_FRONT_HIGH_CENTER = 0x0080,
  81. DCA_EXSS_OVERHEAD = 0x0100,
  82. DCA_EXSS_CENTER_LEFT_RIGHT = 0x0200,
  83. DCA_EXSS_WIDE_LEFT_RIGHT = 0x0400,
  84. DCA_EXSS_SIDE_LEFT_RIGHT = 0x0800,
  85. DCA_EXSS_LFE2 = 0x1000,
  86. DCA_EXSS_SIDE_HIGH_LEFT_RIGHT = 0x2000,
  87. DCA_EXSS_REAR_HIGH_CENTER = 0x4000,
  88. DCA_EXSS_REAR_HIGH_LEFT_RIGHT = 0x8000,
  89. };
  90. enum DCAExtensionMask {
  91. DCA_EXT_CORE = 0x001, ///< core in core substream
  92. DCA_EXT_XXCH = 0x002, ///< XXCh channels extension in core substream
  93. DCA_EXT_X96 = 0x004, ///< 96/24 extension in core substream
  94. DCA_EXT_XCH = 0x008, ///< XCh channel extension in core substream
  95. DCA_EXT_EXSS_CORE = 0x010, ///< core in ExSS (extension substream)
  96. DCA_EXT_EXSS_XBR = 0x020, ///< extended bitrate extension in ExSS
  97. DCA_EXT_EXSS_XXCH = 0x040, ///< XXCh channels extension in ExSS
  98. DCA_EXT_EXSS_X96 = 0x080, ///< 96/24 extension in ExSS
  99. DCA_EXT_EXSS_LBR = 0x100, ///< low bitrate component in ExSS
  100. DCA_EXT_EXSS_XLL = 0x200, ///< lossless extension in ExSS
  101. };
  102. /* -1 are reserved or unknown */
  103. static const int dca_ext_audio_descr_mask[] = {
  104. DCA_EXT_XCH,
  105. -1,
  106. DCA_EXT_X96,
  107. DCA_EXT_XCH | DCA_EXT_X96,
  108. -1,
  109. -1,
  110. DCA_EXT_XXCH,
  111. -1,
  112. };
  113. /* extensions that reside in core substream */
  114. #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
  115. /* Tables for mapping dts channel configurations to libavcodec multichannel api.
  116. * Some compromises have been made for special configurations. Most configurations
  117. * are never used so complete accuracy is not needed.
  118. *
  119. * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
  120. * S -> side, when both rear and back are configured move one of them to the side channel
  121. * OV -> center back
  122. * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
  123. */
  124. static const uint64_t dca_core_channel_layout[] = {
  125. AV_CH_FRONT_CENTER, ///< 1, A
  126. AV_CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
  127. AV_CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
  128. AV_CH_LAYOUT_STEREO, ///< 2, (L + R) + (L - R) (sum-difference)
  129. AV_CH_LAYOUT_STEREO, ///< 2, LT + RT (left and right total)
  130. AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER, ///< 3, C + L + R
  131. AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER, ///< 3, L + R + S
  132. AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 4, C + L + R + S
  133. AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 4, L + R + SL + SR
  134. AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
  135. AV_CH_SIDE_RIGHT, ///< 5, C + L + R + SL + SR
  136. AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
  137. AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR
  138. AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
  139. AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 6, C + L + R + LR + RR + OV
  140. AV_CH_FRONT_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
  141. AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_BACK_CENTER |
  142. AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 6, CF + CR + LF + RF + LR + RR
  143. AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
  144. AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
  145. AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
  146. AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
  147. AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
  148. AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2 + SR1 + SR2
  149. AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
  150. AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
  151. AV_CH_SIDE_LEFT | AV_CH_BACK_CENTER | AV_CH_SIDE_RIGHT, ///< 8, CL + C + CR + L + R + SL + S + SR
  152. };
  153. static const int8_t dca_lfe_index[] = {
  154. 1, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 1, 3, 2, 3
  155. };
  156. static const int8_t dca_channel_reorder_lfe[][9] = {
  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. { 0, 1, -1, -1, -1, -1, -1, -1, -1},
  162. { 2, 0, 1, -1, -1, -1, -1, -1, -1},
  163. { 0, 1, 3, -1, -1, -1, -1, -1, -1},
  164. { 2, 0, 1, 4, -1, -1, -1, -1, -1},
  165. { 0, 1, 3, 4, -1, -1, -1, -1, -1},
  166. { 2, 0, 1, 4, 5, -1, -1, -1, -1},
  167. { 3, 4, 0, 1, 5, 6, -1, -1, -1},
  168. { 2, 0, 1, 4, 5, 6, -1, -1, -1},
  169. { 0, 6, 4, 5, 2, 3, -1, -1, -1},
  170. { 4, 2, 5, 0, 1, 6, 7, -1, -1},
  171. { 5, 6, 0, 1, 7, 3, 8, 4, -1},
  172. { 4, 2, 5, 0, 1, 6, 8, 7, -1},
  173. };
  174. static const int8_t dca_channel_reorder_lfe_xch[][9] = {
  175. { 0, 2, -1, -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. { 0, 1, 3, -1, -1, -1, -1, -1, -1},
  180. { 2, 0, 1, 4, -1, -1, -1, -1, -1},
  181. { 0, 1, 3, 4, -1, -1, -1, -1, -1},
  182. { 2, 0, 1, 4, 5, -1, -1, -1, -1},
  183. { 0, 1, 4, 5, 3, -1, -1, -1, -1},
  184. { 2, 0, 1, 5, 6, 4, -1, -1, -1},
  185. { 3, 4, 0, 1, 6, 7, 5, -1, -1},
  186. { 2, 0, 1, 4, 5, 6, 7, -1, -1},
  187. { 0, 6, 4, 5, 2, 3, 7, -1, -1},
  188. { 4, 2, 5, 0, 1, 7, 8, 6, -1},
  189. { 5, 6, 0, 1, 8, 3, 9, 4, 7},
  190. { 4, 2, 5, 0, 1, 6, 9, 8, 7},
  191. };
  192. static const int8_t dca_channel_reorder_nolfe[][9] = {
  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. { 0, 1, -1, -1, -1, -1, -1, -1, -1},
  198. { 2, 0, 1, -1, -1, -1, -1, -1, -1},
  199. { 0, 1, 2, -1, -1, -1, -1, -1, -1},
  200. { 2, 0, 1, 3, -1, -1, -1, -1, -1},
  201. { 0, 1, 2, 3, -1, -1, -1, -1, -1},
  202. { 2, 0, 1, 3, 4, -1, -1, -1, -1},
  203. { 2, 3, 0, 1, 4, 5, -1, -1, -1},
  204. { 2, 0, 1, 3, 4, 5, -1, -1, -1},
  205. { 0, 5, 3, 4, 1, 2, -1, -1, -1},
  206. { 3, 2, 4, 0, 1, 5, 6, -1, -1},
  207. { 4, 5, 0, 1, 6, 2, 7, 3, -1},
  208. { 3, 2, 4, 0, 1, 5, 7, 6, -1},
  209. };
  210. static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
  211. { 0, 1, -1, -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. { 0, 1, 2, -1, -1, -1, -1, -1, -1},
  216. { 2, 0, 1, 3, -1, -1, -1, -1, -1},
  217. { 0, 1, 2, 3, -1, -1, -1, -1, -1},
  218. { 2, 0, 1, 3, 4, -1, -1, -1, -1},
  219. { 0, 1, 3, 4, 2, -1, -1, -1, -1},
  220. { 2, 0, 1, 4, 5, 3, -1, -1, -1},
  221. { 2, 3, 0, 1, 5, 6, 4, -1, -1},
  222. { 2, 0, 1, 3, 4, 5, 6, -1, -1},
  223. { 0, 5, 3, 4, 1, 2, 6, -1, -1},
  224. { 3, 2, 4, 0, 1, 6, 7, 5, -1},
  225. { 4, 5, 0, 1, 7, 2, 8, 3, 6},
  226. { 3, 2, 4, 0, 1, 5, 8, 7, 6},
  227. };
  228. #define DCA_DOLBY 101 /* FIXME */
  229. #define DCA_CHANNEL_BITS 6
  230. #define DCA_CHANNEL_MASK 0x3F
  231. #define DCA_LFE 0x80
  232. #define HEADER_SIZE 14
  233. #define DCA_MAX_FRAME_SIZE 16384
  234. #define DCA_MAX_EXSS_HEADER_SIZE 4096
  235. #define DCA_BUFFER_PADDING_SIZE 1024
  236. /** Bit allocation */
  237. typedef struct {
  238. int offset; ///< code values offset
  239. int maxbits[8]; ///< max bits in VLC
  240. int wrap; ///< wrap for get_vlc2()
  241. VLC vlc[8]; ///< actual codes
  242. } BitAlloc;
  243. static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
  244. static BitAlloc dca_tmode; ///< transition mode VLCs
  245. static BitAlloc dca_scalefactor; ///< scalefactor VLCs
  246. static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
  247. static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba,
  248. int idx)
  249. {
  250. return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
  251. ba->offset;
  252. }
  253. typedef struct {
  254. AVCodecContext *avctx;
  255. AVFrame frame;
  256. /* Frame header */
  257. int frame_type; ///< type of the current frame
  258. int samples_deficit; ///< deficit sample count
  259. int crc_present; ///< crc is present in the bitstream
  260. int sample_blocks; ///< number of PCM sample blocks
  261. int frame_size; ///< primary frame byte size
  262. int amode; ///< audio channels arrangement
  263. int sample_rate; ///< audio sampling rate
  264. int bit_rate; ///< transmission bit rate
  265. int bit_rate_index; ///< transmission bit rate index
  266. int downmix; ///< embedded downmix enabled
  267. int dynrange; ///< embedded dynamic range flag
  268. int timestamp; ///< embedded time stamp flag
  269. int aux_data; ///< auxiliary data flag
  270. int hdcd; ///< source material is mastered in HDCD
  271. int ext_descr; ///< extension audio descriptor flag
  272. int ext_coding; ///< extended coding flag
  273. int aspf; ///< audio sync word insertion flag
  274. int lfe; ///< low frequency effects flag
  275. int predictor_history; ///< predictor history flag
  276. int header_crc; ///< header crc check bytes
  277. int multirate_inter; ///< multirate interpolator switch
  278. int version; ///< encoder software revision
  279. int copy_history; ///< copy history
  280. int source_pcm_res; ///< source pcm resolution
  281. int front_sum; ///< front sum/difference flag
  282. int surround_sum; ///< surround sum/difference flag
  283. int dialog_norm; ///< dialog normalisation parameter
  284. /* Primary audio coding header */
  285. int subframes; ///< number of subframes
  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. if (s->channel_order_tab[k] >= 0)
  1120. qmf_32_subbands(s, k, subband_samples[k],
  1121. s->samples_chanptr[s->channel_order_tab[k]],
  1122. M_SQRT1_2 / 32768.0 /* 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_chanptr, 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_chanptr[dca_lfe_index[s->amode]],
  1133. 1.0 / (256.0 * 32768.0));
  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;
  1238. int embedded_stereo = 0;
  1239. int embedded_6ch = 0;
  1240. int drc_code_present;
  1241. int 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. DCAContext *s = avctx->priv_data;
  1431. int channels, full_channels;
  1432. int core_ss_end;
  1433. s->xch_present = 0;
  1434. s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
  1435. DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
  1436. if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
  1437. av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
  1438. return AVERROR_INVALIDDATA;
  1439. }
  1440. init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
  1441. if ((ret = dca_parse_frame_header(s)) < 0) {
  1442. //seems like the frame is corrupt, try with the next one
  1443. return ret;
  1444. }
  1445. //set AVCodec values with parsed data
  1446. avctx->sample_rate = s->sample_rate;
  1447. avctx->bit_rate = s->bit_rate;
  1448. s->profile = FF_PROFILE_DTS;
  1449. for (i = 0; i < (s->sample_blocks / 8); i++) {
  1450. if ((ret = dca_decode_block(s, 0, i))) {
  1451. av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
  1452. return ret;
  1453. }
  1454. }
  1455. /* record number of core channels incase less than max channels are requested */
  1456. num_core_channels = s->prim_channels;
  1457. if (s->ext_coding)
  1458. s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
  1459. else
  1460. s->core_ext_mask = 0;
  1461. core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
  1462. /* only scan for extensions if ext_descr was unknown or indicated a
  1463. * supported XCh extension */
  1464. if (s->core_ext_mask < 0 || s->core_ext_mask & DCA_EXT_XCH) {
  1465. /* if ext_descr was unknown, clear s->core_ext_mask so that the
  1466. * extensions scan can fill it up */
  1467. s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
  1468. /* extensions start at 32-bit boundaries into bitstream */
  1469. skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
  1470. while (core_ss_end - get_bits_count(&s->gb) >= 32) {
  1471. uint32_t bits = get_bits_long(&s->gb, 32);
  1472. switch (bits) {
  1473. case 0x5a5a5a5a: {
  1474. int ext_amode, xch_fsize;
  1475. s->xch_base_channel = s->prim_channels;
  1476. /* validate sync word using XCHFSIZE field */
  1477. xch_fsize = show_bits(&s->gb, 10);
  1478. if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
  1479. (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
  1480. continue;
  1481. /* skip length-to-end-of-frame field for the moment */
  1482. skip_bits(&s->gb, 10);
  1483. s->core_ext_mask |= DCA_EXT_XCH;
  1484. /* extension amode(number of channels in extension) should be 1 */
  1485. /* AFAIK XCh is not used for more channels */
  1486. if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
  1487. av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
  1488. " supported!\n", ext_amode);
  1489. continue;
  1490. }
  1491. /* much like core primary audio coding header */
  1492. dca_parse_audio_coding_header(s, s->xch_base_channel);
  1493. for (i = 0; i < (s->sample_blocks / 8); i++)
  1494. if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
  1495. av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
  1496. continue;
  1497. }
  1498. s->xch_present = 1;
  1499. break;
  1500. }
  1501. case 0x47004a03:
  1502. /* XXCh: extended channels */
  1503. /* usually found either in core or HD part in DTS-HD HRA streams,
  1504. * but not in DTS-ES which contains XCh extensions instead */
  1505. s->core_ext_mask |= DCA_EXT_XXCH;
  1506. break;
  1507. case 0x1d95f262: {
  1508. int fsize96 = show_bits(&s->gb, 12) + 1;
  1509. if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
  1510. continue;
  1511. av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
  1512. get_bits_count(&s->gb));
  1513. skip_bits(&s->gb, 12);
  1514. av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
  1515. av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
  1516. s->core_ext_mask |= DCA_EXT_X96;
  1517. break;
  1518. }
  1519. }
  1520. skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
  1521. }
  1522. } else {
  1523. /* no supported extensions, skip the rest of the core substream */
  1524. skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
  1525. }
  1526. if (s->core_ext_mask & DCA_EXT_X96)
  1527. s->profile = FF_PROFILE_DTS_96_24;
  1528. else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
  1529. s->profile = FF_PROFILE_DTS_ES;
  1530. /* check for ExSS (HD part) */
  1531. if (s->dca_buffer_size - s->frame_size > 32 &&
  1532. get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
  1533. dca_exss_parse_header(s);
  1534. avctx->profile = s->profile;
  1535. full_channels = channels = s->prim_channels + !!s->lfe;
  1536. if (s->amode < 16) {
  1537. avctx->channel_layout = dca_core_channel_layout[s->amode];
  1538. if (s->xch_present && (!avctx->request_channels ||
  1539. avctx->request_channels > num_core_channels + !!s->lfe)) {
  1540. avctx->channel_layout |= AV_CH_BACK_CENTER;
  1541. if (s->lfe) {
  1542. avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
  1543. s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
  1544. } else {
  1545. s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
  1546. }
  1547. } else {
  1548. channels = num_core_channels + !!s->lfe;
  1549. s->xch_present = 0; /* disable further xch processing */
  1550. if (s->lfe) {
  1551. avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
  1552. s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
  1553. } else
  1554. s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
  1555. }
  1556. if (channels > !!s->lfe &&
  1557. s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
  1558. return AVERROR_INVALIDDATA;
  1559. if (avctx->request_channels == 2 && s->prim_channels > 2) {
  1560. channels = 2;
  1561. s->output = DCA_STEREO;
  1562. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  1563. }
  1564. } else {
  1565. av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n", s->amode);
  1566. return AVERROR_INVALIDDATA;
  1567. }
  1568. avctx->channels = channels;
  1569. /* get output buffer */
  1570. s->frame.nb_samples = 256 * (s->sample_blocks / 8);
  1571. if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
  1572. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1573. return ret;
  1574. }
  1575. samples_flt = (float **) s->frame.extended_data;
  1576. /* allocate buffer for extra channels if downmixing */
  1577. if (avctx->channels < full_channels) {
  1578. ret = av_samples_get_buffer_size(NULL, full_channels - channels,
  1579. s->frame.nb_samples,
  1580. avctx->sample_fmt, 0);
  1581. if (ret < 0)
  1582. return ret;
  1583. av_fast_malloc(&s->extra_channels_buffer,
  1584. &s->extra_channels_buffer_size, ret);
  1585. if (!s->extra_channels_buffer)
  1586. return AVERROR(ENOMEM);
  1587. ret = av_samples_fill_arrays((uint8_t **)s->extra_channels, NULL,
  1588. s->extra_channels_buffer,
  1589. full_channels - channels,
  1590. s->frame.nb_samples, avctx->sample_fmt, 0);
  1591. if (ret < 0)
  1592. return ret;
  1593. }
  1594. /* filter to get final output */
  1595. for (i = 0; i < (s->sample_blocks / 8); i++) {
  1596. int ch;
  1597. for (ch = 0; ch < channels; ch++)
  1598. s->samples_chanptr[ch] = samples_flt[ch] + i * 256;
  1599. for (; ch < full_channels; ch++)
  1600. s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * 256;
  1601. dca_filter_channels(s, i);
  1602. /* If this was marked as a DTS-ES stream we need to subtract back- */
  1603. /* channel from SL & SR to remove matrixed back-channel signal */
  1604. if ((s->source_pcm_res & 1) && s->xch_present) {
  1605. float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
  1606. float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
  1607. float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
  1608. s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
  1609. s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
  1610. }
  1611. }
  1612. /* update lfe history */
  1613. lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
  1614. for (i = 0; i < 2 * s->lfe * 4; i++)
  1615. s->lfe_data[i] = s->lfe_data[i + lfe_samples];
  1616. *got_frame_ptr = 1;
  1617. *(AVFrame *) data = s->frame;
  1618. return buf_size;
  1619. }
  1620. /**
  1621. * DCA initialization
  1622. *
  1623. * @param avctx pointer to the AVCodecContext
  1624. */
  1625. static av_cold int dca_decode_init(AVCodecContext *avctx)
  1626. {
  1627. DCAContext *s = avctx->priv_data;
  1628. s->avctx = avctx;
  1629. dca_init_vlcs();
  1630. avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
  1631. ff_mdct_init(&s->imdct, 6, 1, 1.0);
  1632. ff_synth_filter_init(&s->synth);
  1633. ff_dcadsp_init(&s->dcadsp);
  1634. ff_fmt_convert_init(&s->fmt_conv, avctx);
  1635. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  1636. /* allow downmixing to stereo */
  1637. if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
  1638. avctx->request_channels == 2) {
  1639. avctx->channels = avctx->request_channels;
  1640. }
  1641. avcodec_get_frame_defaults(&s->frame);
  1642. avctx->coded_frame = &s->frame;
  1643. return 0;
  1644. }
  1645. static av_cold int dca_decode_end(AVCodecContext *avctx)
  1646. {
  1647. DCAContext *s = avctx->priv_data;
  1648. ff_mdct_end(&s->imdct);
  1649. av_freep(&s->extra_channels_buffer);
  1650. return 0;
  1651. }
  1652. static const AVProfile profiles[] = {
  1653. { FF_PROFILE_DTS, "DTS" },
  1654. { FF_PROFILE_DTS_ES, "DTS-ES" },
  1655. { FF_PROFILE_DTS_96_24, "DTS 96/24" },
  1656. { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
  1657. { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
  1658. { FF_PROFILE_UNKNOWN },
  1659. };
  1660. AVCodec ff_dca_decoder = {
  1661. .name = "dca",
  1662. .type = AVMEDIA_TYPE_AUDIO,
  1663. .id = AV_CODEC_ID_DTS,
  1664. .priv_data_size = sizeof(DCAContext),
  1665. .init = dca_decode_init,
  1666. .decode = dca_decode_frame,
  1667. .close = dca_decode_end,
  1668. .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
  1669. .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
  1670. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
  1671. AV_SAMPLE_FMT_NONE },
  1672. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  1673. };