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.

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