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.

1964 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/internal.h"
  31. #include "libavutil/intreadwrite.h"
  32. #include "libavutil/mathematics.h"
  33. #include "libavutil/samplefmt.h"
  34. #include "avcodec.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 "mathops.h"
  42. #include "synth_filter.h"
  43. #include "dcadsp.h"
  44. #include "fmtconvert.h"
  45. #include "internal.h"
  46. #if ARCH_ARM
  47. # include "arm/dca.h"
  48. #endif
  49. //#define TRACE
  50. #define DCA_PRIM_CHANNELS_MAX (7)
  51. #define DCA_SUBBANDS (32)
  52. #define DCA_ABITS_MAX (32) /* Should be 28 */
  53. #define DCA_SUBSUBFRAMES_MAX (4)
  54. #define DCA_SUBFRAMES_MAX (16)
  55. #define DCA_BLOCKS_MAX (16)
  56. #define DCA_LFE_MAX (3)
  57. enum DCAMode {
  58. DCA_MONO = 0,
  59. DCA_CHANNEL,
  60. DCA_STEREO,
  61. DCA_STEREO_SUMDIFF,
  62. DCA_STEREO_TOTAL,
  63. DCA_3F,
  64. DCA_2F1R,
  65. DCA_3F1R,
  66. DCA_2F2R,
  67. DCA_3F2R,
  68. DCA_4F2R
  69. };
  70. /* these are unconfirmed but should be mostly correct */
  71. enum DCAExSSSpeakerMask {
  72. DCA_EXSS_FRONT_CENTER = 0x0001,
  73. DCA_EXSS_FRONT_LEFT_RIGHT = 0x0002,
  74. DCA_EXSS_SIDE_REAR_LEFT_RIGHT = 0x0004,
  75. DCA_EXSS_LFE = 0x0008,
  76. DCA_EXSS_REAR_CENTER = 0x0010,
  77. DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
  78. DCA_EXSS_REAR_LEFT_RIGHT = 0x0040,
  79. DCA_EXSS_FRONT_HIGH_CENTER = 0x0080,
  80. DCA_EXSS_OVERHEAD = 0x0100,
  81. DCA_EXSS_CENTER_LEFT_RIGHT = 0x0200,
  82. DCA_EXSS_WIDE_LEFT_RIGHT = 0x0400,
  83. DCA_EXSS_SIDE_LEFT_RIGHT = 0x0800,
  84. DCA_EXSS_LFE2 = 0x1000,
  85. DCA_EXSS_SIDE_HIGH_LEFT_RIGHT = 0x2000,
  86. DCA_EXSS_REAR_HIGH_CENTER = 0x4000,
  87. DCA_EXSS_REAR_HIGH_LEFT_RIGHT = 0x8000,
  88. };
  89. enum DCAExtensionMask {
  90. DCA_EXT_CORE = 0x001, ///< core in core substream
  91. DCA_EXT_XXCH = 0x002, ///< XXCh channels extension in core substream
  92. DCA_EXT_X96 = 0x004, ///< 96/24 extension in core substream
  93. DCA_EXT_XCH = 0x008, ///< XCh channel extension in core substream
  94. DCA_EXT_EXSS_CORE = 0x010, ///< core in ExSS (extension substream)
  95. DCA_EXT_EXSS_XBR = 0x020, ///< extended bitrate extension in ExSS
  96. DCA_EXT_EXSS_XXCH = 0x040, ///< XXCh channels extension in ExSS
  97. DCA_EXT_EXSS_X96 = 0x080, ///< 96/24 extension in ExSS
  98. DCA_EXT_EXSS_LBR = 0x100, ///< low bitrate component in ExSS
  99. DCA_EXT_EXSS_XLL = 0x200, ///< lossless extension in ExSS
  100. };
  101. /* -1 are reserved or unknown */
  102. static const int dca_ext_audio_descr_mask[] = {
  103. DCA_EXT_XCH,
  104. -1,
  105. DCA_EXT_X96,
  106. DCA_EXT_XCH | DCA_EXT_X96,
  107. -1,
  108. -1,
  109. DCA_EXT_XXCH,
  110. -1,
  111. };
  112. /* extensions that reside in core substream */
  113. #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
  114. /* Tables for mapping dts channel configurations to libavcodec multichannel api.
  115. * Some compromises have been made for special configurations. Most configurations
  116. * are never used so complete accuracy is not needed.
  117. *
  118. * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
  119. * S -> side, when both rear and back are configured move one of them to the side channel
  120. * OV -> center back
  121. * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
  122. */
  123. static const uint64_t dca_core_channel_layout[] = {
  124. AV_CH_FRONT_CENTER, ///< 1, A
  125. AV_CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
  126. AV_CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
  127. AV_CH_LAYOUT_STEREO, ///< 2, (L + R) + (L - R) (sum-difference)
  128. AV_CH_LAYOUT_STEREO, ///< 2, LT + RT (left and right total)
  129. AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER, ///< 3, C + L + R
  130. AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER, ///< 3, L + R + S
  131. AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 4, C + L + R + S
  132. AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 4, L + R + SL + SR
  133. AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
  134. AV_CH_SIDE_RIGHT, ///< 5, C + L + R + SL + SR
  135. AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
  136. AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR
  137. AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
  138. AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 6, C + L + R + LR + RR + OV
  139. AV_CH_FRONT_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
  140. AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_BACK_CENTER |
  141. AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 6, CF + CR + LF + RF + LR + RR
  142. AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
  143. AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
  144. AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
  145. AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
  146. AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
  147. AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2 + SR1 + SR2
  148. AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
  149. AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
  150. AV_CH_SIDE_LEFT | AV_CH_BACK_CENTER | AV_CH_SIDE_RIGHT, ///< 8, CL + C + CR + L + R + SL + S + SR
  151. };
  152. static const int8_t dca_lfe_index[] = {
  153. 1, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 1, 3, 2, 3
  154. };
  155. static const int8_t dca_channel_reorder_lfe[][9] = {
  156. { 0, -1, -1, -1, -1, -1, -1, -1, -1},
  157. { 0, 1, -1, -1, -1, -1, -1, -1, -1},
  158. { 0, 1, -1, -1, -1, -1, -1, -1, -1},
  159. { 0, 1, -1, -1, -1, -1, -1, -1, -1},
  160. { 0, 1, -1, -1, -1, -1, -1, -1, -1},
  161. { 2, 0, 1, -1, -1, -1, -1, -1, -1},
  162. { 0, 1, 3, -1, -1, -1, -1, -1, -1},
  163. { 2, 0, 1, 4, -1, -1, -1, -1, -1},
  164. { 0, 1, 3, 4, -1, -1, -1, -1, -1},
  165. { 2, 0, 1, 4, 5, -1, -1, -1, -1},
  166. { 3, 4, 0, 1, 5, 6, -1, -1, -1},
  167. { 2, 0, 1, 4, 5, 6, -1, -1, -1},
  168. { 0, 6, 4, 5, 2, 3, -1, -1, -1},
  169. { 4, 2, 5, 0, 1, 6, 7, -1, -1},
  170. { 5, 6, 0, 1, 7, 3, 8, 4, -1},
  171. { 4, 2, 5, 0, 1, 6, 8, 7, -1},
  172. };
  173. static const int8_t dca_channel_reorder_lfe_xch[][9] = {
  174. { 0, 2, -1, -1, -1, -1, -1, -1, -1},
  175. { 0, 1, 3, -1, -1, -1, -1, -1, -1},
  176. { 0, 1, 3, -1, -1, -1, -1, -1, -1},
  177. { 0, 1, 3, -1, -1, -1, -1, -1, -1},
  178. { 0, 1, 3, -1, -1, -1, -1, -1, -1},
  179. { 2, 0, 1, 4, -1, -1, -1, -1, -1},
  180. { 0, 1, 3, 4, -1, -1, -1, -1, -1},
  181. { 2, 0, 1, 4, 5, -1, -1, -1, -1},
  182. { 0, 1, 4, 5, 3, -1, -1, -1, -1},
  183. { 2, 0, 1, 5, 6, 4, -1, -1, -1},
  184. { 3, 4, 0, 1, 6, 7, 5, -1, -1},
  185. { 2, 0, 1, 4, 5, 6, 7, -1, -1},
  186. { 0, 6, 4, 5, 2, 3, 7, -1, -1},
  187. { 4, 2, 5, 0, 1, 7, 8, 6, -1},
  188. { 5, 6, 0, 1, 8, 3, 9, 4, 7},
  189. { 4, 2, 5, 0, 1, 6, 9, 8, 7},
  190. };
  191. static const int8_t dca_channel_reorder_nolfe[][9] = {
  192. { 0, -1, -1, -1, -1, -1, -1, -1, -1},
  193. { 0, 1, -1, -1, -1, -1, -1, -1, -1},
  194. { 0, 1, -1, -1, -1, -1, -1, -1, -1},
  195. { 0, 1, -1, -1, -1, -1, -1, -1, -1},
  196. { 0, 1, -1, -1, -1, -1, -1, -1, -1},
  197. { 2, 0, 1, -1, -1, -1, -1, -1, -1},
  198. { 0, 1, 2, -1, -1, -1, -1, -1, -1},
  199. { 2, 0, 1, 3, -1, -1, -1, -1, -1},
  200. { 0, 1, 2, 3, -1, -1, -1, -1, -1},
  201. { 2, 0, 1, 3, 4, -1, -1, -1, -1},
  202. { 2, 3, 0, 1, 4, 5, -1, -1, -1},
  203. { 2, 0, 1, 3, 4, 5, -1, -1, -1},
  204. { 0, 5, 3, 4, 1, 2, -1, -1, -1},
  205. { 3, 2, 4, 0, 1, 5, 6, -1, -1},
  206. { 4, 5, 0, 1, 6, 2, 7, 3, -1},
  207. { 3, 2, 4, 0, 1, 5, 7, 6, -1},
  208. };
  209. static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
  210. { 0, 1, -1, -1, -1, -1, -1, -1, -1},
  211. { 0, 1, 2, -1, -1, -1, -1, -1, -1},
  212. { 0, 1, 2, -1, -1, -1, -1, -1, -1},
  213. { 0, 1, 2, -1, -1, -1, -1, -1, -1},
  214. { 0, 1, 2, -1, -1, -1, -1, -1, -1},
  215. { 2, 0, 1, 3, -1, -1, -1, -1, -1},
  216. { 0, 1, 2, 3, -1, -1, -1, -1, -1},
  217. { 2, 0, 1, 3, 4, -1, -1, -1, -1},
  218. { 0, 1, 3, 4, 2, -1, -1, -1, -1},
  219. { 2, 0, 1, 4, 5, 3, -1, -1, -1},
  220. { 2, 3, 0, 1, 5, 6, 4, -1, -1},
  221. { 2, 0, 1, 3, 4, 5, 6, -1, -1},
  222. { 0, 5, 3, 4, 1, 2, 6, -1, -1},
  223. { 3, 2, 4, 0, 1, 6, 7, 5, -1},
  224. { 4, 5, 0, 1, 7, 2, 8, 3, 6},
  225. { 3, 2, 4, 0, 1, 5, 8, 7, 6},
  226. };
  227. #define DCA_DOLBY 101 /* FIXME */
  228. #define DCA_CHANNEL_BITS 6
  229. #define DCA_CHANNEL_MASK 0x3F
  230. #define DCA_LFE 0x80
  231. #define HEADER_SIZE 14
  232. #define DCA_MAX_FRAME_SIZE 16384
  233. #define DCA_MAX_EXSS_HEADER_SIZE 4096
  234. #define DCA_BUFFER_PADDING_SIZE 1024
  235. /** Bit allocation */
  236. typedef struct {
  237. int offset; ///< code values offset
  238. int maxbits[8]; ///< max bits in VLC
  239. int wrap; ///< wrap for get_vlc2()
  240. VLC vlc[8]; ///< actual codes
  241. } BitAlloc;
  242. static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
  243. static BitAlloc dca_tmode; ///< transition mode VLCs
  244. static BitAlloc dca_scalefactor; ///< scalefactor VLCs
  245. static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
  246. static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba,
  247. int idx)
  248. {
  249. return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
  250. ba->offset;
  251. }
  252. typedef struct {
  253. AVCodecContext *avctx;
  254. /* 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 total_channels; ///< number of channels including extensions
  285. int prim_channels; ///< number of primary audio channels
  286. int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
  287. int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
  288. int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
  289. int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
  290. int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
  291. int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
  292. int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
  293. float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
  294. /* Primary audio coding side information */
  295. int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes
  296. int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count
  297. int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
  298. int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
  299. int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
  300. int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
  301. int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient)
  302. int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
  303. int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
  304. int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]; ///< stereo downmix coefficients
  305. int dynrange_coef; ///< dynamic range coefficient
  306. int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
  307. float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
  308. int lfe_scale_factor;
  309. /* Subband samples history (for ADPCM) */
  310. DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
  311. DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
  312. DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
  313. int hist_index[DCA_PRIM_CHANNELS_MAX];
  314. DECLARE_ALIGNED(32, float, raXin)[32];
  315. int output; ///< type of output
  316. DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
  317. float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
  318. float *extra_channels[DCA_PRIM_CHANNELS_MAX + 1];
  319. uint8_t *extra_channels_buffer;
  320. unsigned int extra_channels_buffer_size;
  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 = avpriv_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. if (s->lfe > 2) {
  512. av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE value: %d\n", s->lfe);
  513. return AVERROR_INVALIDDATA;
  514. }
  515. /* TODO: check CRC */
  516. if (s->crc_present)
  517. s->header_crc = get_bits(&s->gb, 16);
  518. s->multirate_inter = get_bits(&s->gb, 1);
  519. s->version = get_bits(&s->gb, 4);
  520. s->copy_history = get_bits(&s->gb, 2);
  521. s->source_pcm_res = get_bits(&s->gb, 3);
  522. s->front_sum = get_bits(&s->gb, 1);
  523. s->surround_sum = get_bits(&s->gb, 1);
  524. s->dialog_norm = get_bits(&s->gb, 4);
  525. /* FIXME: channels mixing levels */
  526. s->output = s->amode;
  527. if (s->lfe)
  528. s->output |= DCA_LFE;
  529. #ifdef TRACE
  530. av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
  531. av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
  532. av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
  533. av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
  534. s->sample_blocks, s->sample_blocks * 32);
  535. av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
  536. av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
  537. s->amode, dca_channels[s->amode]);
  538. av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
  539. s->sample_rate);
  540. av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
  541. s->bit_rate);
  542. av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
  543. av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
  544. av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
  545. av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
  546. av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
  547. av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
  548. av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
  549. av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
  550. av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
  551. av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
  552. s->predictor_history);
  553. av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
  554. av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
  555. s->multirate_inter);
  556. av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
  557. av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
  558. av_log(s->avctx, AV_LOG_DEBUG,
  559. "source pcm resolution: %i (%i bits/sample)\n",
  560. s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
  561. av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
  562. av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
  563. av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
  564. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  565. #endif
  566. /* Primary audio coding header */
  567. s->subframes = get_bits(&s->gb, 4) + 1;
  568. return dca_parse_audio_coding_header(s, 0);
  569. }
  570. static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
  571. {
  572. if (level < 5) {
  573. /* huffman encoded */
  574. value += get_bitalloc(gb, &dca_scalefactor, level);
  575. value = av_clip(value, 0, (1 << log2range) - 1);
  576. } else if (level < 8) {
  577. if (level + 1 > log2range) {
  578. skip_bits(gb, level + 1 - log2range);
  579. value = get_bits(gb, log2range);
  580. } else {
  581. value = get_bits(gb, level + 1);
  582. }
  583. }
  584. return value;
  585. }
  586. static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
  587. {
  588. /* Primary audio coding side information */
  589. int j, k;
  590. if (get_bits_left(&s->gb) < 0)
  591. return AVERROR_INVALIDDATA;
  592. if (!base_channel) {
  593. s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
  594. s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
  595. }
  596. for (j = base_channel; j < s->prim_channels; j++) {
  597. for (k = 0; k < s->subband_activity[j]; k++)
  598. s->prediction_mode[j][k] = get_bits(&s->gb, 1);
  599. }
  600. /* Get prediction codebook */
  601. for (j = base_channel; j < s->prim_channels; j++) {
  602. for (k = 0; k < s->subband_activity[j]; k++) {
  603. if (s->prediction_mode[j][k] > 0) {
  604. /* (Prediction coefficient VQ address) */
  605. s->prediction_vq[j][k] = get_bits(&s->gb, 12);
  606. }
  607. }
  608. }
  609. /* Bit allocation index */
  610. for (j = base_channel; j < s->prim_channels; j++) {
  611. for (k = 0; k < s->vq_start_subband[j]; k++) {
  612. if (s->bitalloc_huffman[j] == 6)
  613. s->bitalloc[j][k] = get_bits(&s->gb, 5);
  614. else if (s->bitalloc_huffman[j] == 5)
  615. s->bitalloc[j][k] = get_bits(&s->gb, 4);
  616. else if (s->bitalloc_huffman[j] == 7) {
  617. av_log(s->avctx, AV_LOG_ERROR,
  618. "Invalid bit allocation index\n");
  619. return AVERROR_INVALIDDATA;
  620. } else {
  621. s->bitalloc[j][k] =
  622. get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
  623. }
  624. if (s->bitalloc[j][k] > 26) {
  625. av_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n",
  626. j, k, s->bitalloc[j][k]);
  627. return AVERROR_INVALIDDATA;
  628. }
  629. }
  630. }
  631. /* Transition mode */
  632. for (j = base_channel; j < s->prim_channels; j++) {
  633. for (k = 0; k < s->subband_activity[j]; k++) {
  634. s->transition_mode[j][k] = 0;
  635. if (s->subsubframes[s->current_subframe] > 1 &&
  636. k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
  637. s->transition_mode[j][k] =
  638. get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
  639. }
  640. }
  641. }
  642. if (get_bits_left(&s->gb) < 0)
  643. return AVERROR_INVALIDDATA;
  644. for (j = base_channel; j < s->prim_channels; j++) {
  645. const uint32_t *scale_table;
  646. int scale_sum, log_size;
  647. memset(s->scale_factor[j], 0,
  648. s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
  649. if (s->scalefactor_huffman[j] == 6) {
  650. scale_table = scale_factor_quant7;
  651. log_size = 7;
  652. } else {
  653. scale_table = scale_factor_quant6;
  654. log_size = 6;
  655. }
  656. /* When huffman coded, only the difference is encoded */
  657. scale_sum = 0;
  658. for (k = 0; k < s->subband_activity[j]; k++) {
  659. if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
  660. scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
  661. s->scale_factor[j][k][0] = scale_table[scale_sum];
  662. }
  663. if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
  664. /* Get second scale factor */
  665. scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
  666. s->scale_factor[j][k][1] = scale_table[scale_sum];
  667. }
  668. }
  669. }
  670. /* Joint subband scale factor codebook select */
  671. for (j = base_channel; j < s->prim_channels; j++) {
  672. /* Transmitted only if joint subband coding enabled */
  673. if (s->joint_intensity[j] > 0)
  674. s->joint_huff[j] = get_bits(&s->gb, 3);
  675. }
  676. if (get_bits_left(&s->gb) < 0)
  677. return AVERROR_INVALIDDATA;
  678. /* Scale factors for joint subband coding */
  679. for (j = base_channel; j < s->prim_channels; j++) {
  680. int source_channel;
  681. /* Transmitted only if joint subband coding enabled */
  682. if (s->joint_intensity[j] > 0) {
  683. int scale = 0;
  684. source_channel = s->joint_intensity[j] - 1;
  685. /* When huffman coded, only the difference is encoded
  686. * (is this valid as well for joint scales ???) */
  687. for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
  688. scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
  689. s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
  690. }
  691. if (!(s->debug_flag & 0x02)) {
  692. av_log(s->avctx, AV_LOG_DEBUG,
  693. "Joint stereo coding not supported\n");
  694. s->debug_flag |= 0x02;
  695. }
  696. }
  697. }
  698. /* Stereo downmix coefficients */
  699. if (!base_channel && s->prim_channels > 2) {
  700. if (s->downmix) {
  701. for (j = base_channel; j < s->prim_channels; j++) {
  702. s->downmix_coef[j][0] = get_bits(&s->gb, 7);
  703. s->downmix_coef[j][1] = get_bits(&s->gb, 7);
  704. }
  705. } else {
  706. int am = s->amode & DCA_CHANNEL_MASK;
  707. if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
  708. av_log(s->avctx, AV_LOG_ERROR,
  709. "Invalid channel mode %d\n", am);
  710. return AVERROR_INVALIDDATA;
  711. }
  712. if (s->prim_channels > FF_ARRAY_ELEMS(dca_default_coeffs[0])) {
  713. avpriv_request_sample(s->avctx, "Downmixing %d channels",
  714. s->prim_channels);
  715. return AVERROR_PATCHWELCOME;
  716. }
  717. for (j = base_channel; j < s->prim_channels; j++) {
  718. s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
  719. s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
  720. }
  721. }
  722. }
  723. /* Dynamic range coefficient */
  724. if (!base_channel && s->dynrange)
  725. s->dynrange_coef = get_bits(&s->gb, 8);
  726. /* Side information CRC check word */
  727. if (s->crc_present) {
  728. get_bits(&s->gb, 16);
  729. }
  730. /*
  731. * Primary audio data arrays
  732. */
  733. /* VQ encoded high frequency subbands */
  734. for (j = base_channel; j < s->prim_channels; j++)
  735. for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
  736. /* 1 vector -> 32 samples */
  737. s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
  738. /* Low frequency effect data */
  739. if (!base_channel && s->lfe) {
  740. /* LFE samples */
  741. int lfe_samples = 2 * s->lfe * (4 + block_index);
  742. int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
  743. float lfe_scale;
  744. for (j = lfe_samples; j < lfe_end_sample; j++) {
  745. /* Signed 8 bits int */
  746. s->lfe_data[j] = get_sbits(&s->gb, 8);
  747. }
  748. /* Scale factor index */
  749. skip_bits(&s->gb, 1);
  750. s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 7)];
  751. /* Quantization step size * scale factor */
  752. lfe_scale = 0.035 * s->lfe_scale_factor;
  753. for (j = lfe_samples; j < lfe_end_sample; j++)
  754. s->lfe_data[j] *= lfe_scale;
  755. }
  756. #ifdef TRACE
  757. av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
  758. s->subsubframes[s->current_subframe]);
  759. av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
  760. s->partial_samples[s->current_subframe]);
  761. for (j = base_channel; j < s->prim_channels; j++) {
  762. av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
  763. for (k = 0; k < s->subband_activity[j]; k++)
  764. av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
  765. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  766. }
  767. for (j = base_channel; j < s->prim_channels; j++) {
  768. for (k = 0; k < s->subband_activity[j]; k++)
  769. av_log(s->avctx, AV_LOG_DEBUG,
  770. "prediction coefs: %f, %f, %f, %f\n",
  771. (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
  772. (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
  773. (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
  774. (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
  775. }
  776. for (j = base_channel; j < s->prim_channels; j++) {
  777. av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
  778. for (k = 0; k < s->vq_start_subband[j]; k++)
  779. av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
  780. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  781. }
  782. for (j = base_channel; j < s->prim_channels; j++) {
  783. av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
  784. for (k = 0; k < s->subband_activity[j]; k++)
  785. av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
  786. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  787. }
  788. for (j = base_channel; j < s->prim_channels; j++) {
  789. av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
  790. for (k = 0; k < s->subband_activity[j]; k++) {
  791. if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
  792. av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
  793. if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
  794. av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
  795. }
  796. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  797. }
  798. for (j = base_channel; j < s->prim_channels; j++) {
  799. if (s->joint_intensity[j] > 0) {
  800. int source_channel = s->joint_intensity[j] - 1;
  801. av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
  802. for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
  803. av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
  804. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  805. }
  806. }
  807. if (!base_channel && s->prim_channels > 2 && s->downmix) {
  808. av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
  809. for (j = 0; j < s->prim_channels; j++) {
  810. av_log(s->avctx, AV_LOG_DEBUG, "Channel 0, %d = %f\n", j,
  811. dca_downmix_coeffs[s->downmix_coef[j][0]]);
  812. av_log(s->avctx, AV_LOG_DEBUG, "Channel 1, %d = %f\n", j,
  813. dca_downmix_coeffs[s->downmix_coef[j][1]]);
  814. }
  815. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  816. }
  817. for (j = base_channel; j < s->prim_channels; j++)
  818. for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
  819. av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
  820. if (!base_channel && s->lfe) {
  821. int lfe_samples = 2 * s->lfe * (4 + block_index);
  822. int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
  823. av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
  824. for (j = lfe_samples; j < lfe_end_sample; j++)
  825. av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
  826. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  827. }
  828. #endif
  829. return 0;
  830. }
  831. static void qmf_32_subbands(DCAContext *s, int chans,
  832. float samples_in[32][8], float *samples_out,
  833. float scale)
  834. {
  835. const float *prCoeff;
  836. int sb_act = s->subband_activity[chans];
  837. scale *= sqrt(1 / 8.0);
  838. /* Select filter */
  839. if (!s->multirate_inter) /* Non-perfect reconstruction */
  840. prCoeff = fir_32bands_nonperfect;
  841. else /* Perfect reconstruction */
  842. prCoeff = fir_32bands_perfect;
  843. s->dcadsp.qmf_32_subbands(samples_in, sb_act, &s->synth, &s->imdct,
  844. s->subband_fir_hist[chans],
  845. &s->hist_index[chans],
  846. s->subband_fir_noidea[chans], prCoeff,
  847. samples_out, s->raXin, scale);
  848. }
  849. static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
  850. int num_deci_sample, float *samples_in,
  851. float *samples_out, float scale)
  852. {
  853. /* samples_in: An array holding decimated samples.
  854. * Samples in current subframe starts from samples_in[0],
  855. * while samples_in[-1], samples_in[-2], ..., stores samples
  856. * from last subframe as history.
  857. *
  858. * samples_out: An array holding interpolated samples
  859. */
  860. int decifactor;
  861. const float *prCoeff;
  862. int deciindex;
  863. /* Select decimation filter */
  864. if (decimation_select == 1) {
  865. decifactor = 64;
  866. prCoeff = lfe_fir_128;
  867. } else {
  868. decifactor = 32;
  869. prCoeff = lfe_fir_64;
  870. }
  871. /* Interpolation */
  872. for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
  873. s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor, scale);
  874. samples_in++;
  875. samples_out += 2 * decifactor;
  876. }
  877. }
  878. /* downmixing routines */
  879. #define MIX_REAR1(samples, s1, rs, coef) \
  880. samples[0][i] += samples[s1][i] * coef[rs][0]; \
  881. samples[1][i] += samples[s1][i] * coef[rs][1];
  882. #define MIX_REAR2(samples, s1, s2, rs, coef) \
  883. samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
  884. samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
  885. #define MIX_FRONT3(samples, coef) \
  886. t = samples[c][i]; \
  887. u = samples[l][i]; \
  888. v = samples[r][i]; \
  889. samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
  890. samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
  891. #define DOWNMIX_TO_STEREO(op1, op2) \
  892. for (i = 0; i < 256; i++) { \
  893. op1 \
  894. op2 \
  895. }
  896. static void dca_downmix(float **samples, int srcfmt,
  897. int downmix_coef[DCA_PRIM_CHANNELS_MAX][2],
  898. const int8_t *channel_mapping)
  899. {
  900. int c, l, r, sl, sr, s;
  901. int i;
  902. float t, u, v;
  903. float coef[DCA_PRIM_CHANNELS_MAX][2];
  904. for (i = 0; i < DCA_PRIM_CHANNELS_MAX; i++) {
  905. coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
  906. coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
  907. }
  908. switch (srcfmt) {
  909. case DCA_MONO:
  910. case DCA_CHANNEL:
  911. case DCA_STEREO_TOTAL:
  912. case DCA_STEREO_SUMDIFF:
  913. case DCA_4F2R:
  914. av_log(NULL, 0, "Not implemented!\n");
  915. break;
  916. case DCA_STEREO:
  917. break;
  918. case DCA_3F:
  919. c = channel_mapping[0];
  920. l = channel_mapping[1];
  921. r = channel_mapping[2];
  922. DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
  923. break;
  924. case DCA_2F1R:
  925. s = channel_mapping[2];
  926. DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
  927. break;
  928. case DCA_3F1R:
  929. c = channel_mapping[0];
  930. l = channel_mapping[1];
  931. r = channel_mapping[2];
  932. s = channel_mapping[3];
  933. DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
  934. MIX_REAR1(samples, s, 3, coef));
  935. break;
  936. case DCA_2F2R:
  937. sl = channel_mapping[2];
  938. sr = channel_mapping[3];
  939. DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
  940. break;
  941. case DCA_3F2R:
  942. c = channel_mapping[0];
  943. l = channel_mapping[1];
  944. r = channel_mapping[2];
  945. sl = channel_mapping[3];
  946. sr = channel_mapping[4];
  947. DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
  948. MIX_REAR2(samples, sl, sr, 3, coef));
  949. break;
  950. }
  951. }
  952. #ifndef decode_blockcodes
  953. /* Very compact version of the block code decoder that does not use table
  954. * look-up but is slightly slower */
  955. static int decode_blockcode(int code, int levels, int32_t *values)
  956. {
  957. int i;
  958. int offset = (levels - 1) >> 1;
  959. for (i = 0; i < 4; i++) {
  960. int div = FASTDIV(code, levels);
  961. values[i] = code - offset - div * levels;
  962. code = div;
  963. }
  964. return code;
  965. }
  966. static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
  967. {
  968. return decode_blockcode(code1, levels, values) |
  969. decode_blockcode(code2, levels, values + 4);
  970. }
  971. #endif
  972. static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
  973. static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
  974. #ifndef int8x8_fmul_int32
  975. static inline void int8x8_fmul_int32(float *dst, const int8_t *src, int scale)
  976. {
  977. float fscale = scale / 16.0;
  978. int i;
  979. for (i = 0; i < 8; i++)
  980. dst[i] = src[i] * fscale;
  981. }
  982. #endif
  983. static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
  984. {
  985. int k, l;
  986. int subsubframe = s->current_subsubframe;
  987. const float *quant_step_table;
  988. /* FIXME */
  989. float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
  990. LOCAL_ALIGNED_16(int32_t, block, [8 * DCA_SUBBANDS]);
  991. /*
  992. * Audio data
  993. */
  994. /* Select quantization step size table */
  995. if (s->bit_rate_index == 0x1f)
  996. quant_step_table = lossless_quant_d;
  997. else
  998. quant_step_table = lossy_quant_d;
  999. for (k = base_channel; k < s->prim_channels; k++) {
  1000. float rscale[DCA_SUBBANDS];
  1001. if (get_bits_left(&s->gb) < 0)
  1002. return AVERROR_INVALIDDATA;
  1003. for (l = 0; l < s->vq_start_subband[k]; l++) {
  1004. int m;
  1005. /* Select the mid-tread linear quantizer */
  1006. int abits = s->bitalloc[k][l];
  1007. float quant_step_size = quant_step_table[abits];
  1008. /*
  1009. * Determine quantization index code book and its type
  1010. */
  1011. /* Select quantization index code book */
  1012. int sel = s->quant_index_huffman[k][abits];
  1013. /*
  1014. * Extract bits from the bit stream
  1015. */
  1016. if (!abits) {
  1017. rscale[l] = 0;
  1018. memset(block + 8 * l, 0, 8 * sizeof(block[0]));
  1019. } else {
  1020. /* Deal with transients */
  1021. int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
  1022. rscale[l] = quant_step_size * s->scale_factor[k][l][sfi] *
  1023. s->scalefactor_adj[k][sel];
  1024. if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
  1025. if (abits <= 7) {
  1026. /* Block code */
  1027. int block_code1, block_code2, size, levels, err;
  1028. size = abits_sizes[abits - 1];
  1029. levels = abits_levels[abits - 1];
  1030. block_code1 = get_bits(&s->gb, size);
  1031. block_code2 = get_bits(&s->gb, size);
  1032. err = decode_blockcodes(block_code1, block_code2,
  1033. levels, block + 8 * l);
  1034. if (err) {
  1035. av_log(s->avctx, AV_LOG_ERROR,
  1036. "ERROR: block code look-up failed\n");
  1037. return AVERROR_INVALIDDATA;
  1038. }
  1039. } else {
  1040. /* no coding */
  1041. for (m = 0; m < 8; m++)
  1042. block[8 * l + m] = get_sbits(&s->gb, abits - 3);
  1043. }
  1044. } else {
  1045. /* Huffman coded */
  1046. for (m = 0; m < 8; m++)
  1047. block[8 * l + m] = get_bitalloc(&s->gb,
  1048. &dca_smpl_bitalloc[abits], sel);
  1049. }
  1050. }
  1051. }
  1052. s->fmt_conv.int32_to_float_fmul_array8(&s->fmt_conv, subband_samples[k][0],
  1053. block, rscale, 8 * s->vq_start_subband[k]);
  1054. for (l = 0; l < s->vq_start_subband[k]; l++) {
  1055. int m;
  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. return AVERROR_INVALIDDATA;
  1100. }
  1101. }
  1102. /* Backup predictor history for adpcm */
  1103. for (k = base_channel; k < s->prim_channels; k++)
  1104. for (l = 0; l < s->vq_start_subband[k]; l++)
  1105. memcpy(s->subband_samples_hist[k][l],
  1106. &subband_samples[k][l][4],
  1107. 4 * sizeof(subband_samples[0][0][0]));
  1108. return 0;
  1109. }
  1110. static int dca_filter_channels(DCAContext *s, int block_index)
  1111. {
  1112. float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
  1113. int k;
  1114. /* 32 subbands QMF */
  1115. for (k = 0; k < s->prim_channels; k++) {
  1116. /* static float pcm_to_double[8] = { 32768.0, 32768.0, 524288.0, 524288.0,
  1117. 0, 8388608.0, 8388608.0 };*/
  1118. if (s->channel_order_tab[k] >= 0)
  1119. qmf_32_subbands(s, k, subband_samples[k],
  1120. s->samples_chanptr[s->channel_order_tab[k]],
  1121. M_SQRT1_2 / 32768.0 /* pcm_to_double[s->source_pcm_res] */);
  1122. }
  1123. /* Down mixing */
  1124. if (s->avctx->request_channels == 2 && s->prim_channels > 2) {
  1125. dca_downmix(s->samples_chanptr, s->amode, s->downmix_coef, s->channel_order_tab);
  1126. }
  1127. /* Generate LFE samples for this subsubframe FIXME!!! */
  1128. if (s->output & DCA_LFE) {
  1129. lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
  1130. s->lfe_data + 2 * s->lfe * (block_index + 4),
  1131. s->samples_chanptr[dca_lfe_index[s->amode]],
  1132. 1.0 / (256.0 * 32768.0));
  1133. /* Outputs 20bits pcm samples */
  1134. }
  1135. return 0;
  1136. }
  1137. static int dca_subframe_footer(DCAContext *s, int base_channel)
  1138. {
  1139. int aux_data_count = 0, i;
  1140. /*
  1141. * Unpack optional information
  1142. */
  1143. /* presumably optional information only appears in the core? */
  1144. if (!base_channel) {
  1145. if (s->timestamp)
  1146. skip_bits_long(&s->gb, 32);
  1147. if (s->aux_data)
  1148. aux_data_count = get_bits(&s->gb, 6);
  1149. for (i = 0; i < aux_data_count; i++)
  1150. get_bits(&s->gb, 8);
  1151. if (s->crc_present && (s->downmix || s->dynrange))
  1152. get_bits(&s->gb, 16);
  1153. }
  1154. return 0;
  1155. }
  1156. /**
  1157. * Decode a dca frame block
  1158. *
  1159. * @param s pointer to the DCAContext
  1160. */
  1161. static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
  1162. {
  1163. int ret;
  1164. /* Sanity check */
  1165. if (s->current_subframe >= s->subframes) {
  1166. av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
  1167. s->current_subframe, s->subframes);
  1168. return AVERROR_INVALIDDATA;
  1169. }
  1170. if (!s->current_subsubframe) {
  1171. #ifdef TRACE
  1172. av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
  1173. #endif
  1174. /* Read subframe header */
  1175. if ((ret = dca_subframe_header(s, base_channel, block_index)))
  1176. return ret;
  1177. }
  1178. /* Read subsubframe */
  1179. #ifdef TRACE
  1180. av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
  1181. #endif
  1182. if ((ret = dca_subsubframe(s, base_channel, block_index)))
  1183. return ret;
  1184. /* Update state */
  1185. s->current_subsubframe++;
  1186. if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
  1187. s->current_subsubframe = 0;
  1188. s->current_subframe++;
  1189. }
  1190. if (s->current_subframe >= s->subframes) {
  1191. #ifdef TRACE
  1192. av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
  1193. #endif
  1194. /* Read subframe footer */
  1195. if ((ret = dca_subframe_footer(s, base_channel)))
  1196. return ret;
  1197. }
  1198. return 0;
  1199. }
  1200. /**
  1201. * Return the number of channels in an ExSS speaker mask (HD)
  1202. */
  1203. static int dca_exss_mask2count(int mask)
  1204. {
  1205. /* count bits that mean speaker pairs twice */
  1206. return av_popcount(mask) +
  1207. av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
  1208. DCA_EXSS_FRONT_LEFT_RIGHT |
  1209. DCA_EXSS_FRONT_HIGH_LEFT_RIGHT |
  1210. DCA_EXSS_WIDE_LEFT_RIGHT |
  1211. DCA_EXSS_SIDE_LEFT_RIGHT |
  1212. DCA_EXSS_SIDE_HIGH_LEFT_RIGHT |
  1213. DCA_EXSS_SIDE_REAR_LEFT_RIGHT |
  1214. DCA_EXSS_REAR_LEFT_RIGHT |
  1215. DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
  1216. }
  1217. /**
  1218. * Skip mixing coefficients of a single mix out configuration (HD)
  1219. */
  1220. static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
  1221. {
  1222. int i;
  1223. for (i = 0; i < channels; i++) {
  1224. int mix_map_mask = get_bits(gb, out_ch);
  1225. int num_coeffs = av_popcount(mix_map_mask);
  1226. skip_bits_long(gb, num_coeffs * 6);
  1227. }
  1228. }
  1229. /**
  1230. * Parse extension substream asset header (HD)
  1231. */
  1232. static int dca_exss_parse_asset_header(DCAContext *s)
  1233. {
  1234. int header_pos = get_bits_count(&s->gb);
  1235. int header_size;
  1236. int channels;
  1237. int embedded_stereo = 0;
  1238. int embedded_6ch = 0;
  1239. int drc_code_present;
  1240. int extensions_mask;
  1241. int i, j;
  1242. if (get_bits_left(&s->gb) < 16)
  1243. return -1;
  1244. /* We will parse just enough to get to the extensions bitmask with which
  1245. * we can set the profile value. */
  1246. header_size = get_bits(&s->gb, 9) + 1;
  1247. skip_bits(&s->gb, 3); // asset index
  1248. if (s->static_fields) {
  1249. if (get_bits1(&s->gb))
  1250. skip_bits(&s->gb, 4); // asset type descriptor
  1251. if (get_bits1(&s->gb))
  1252. skip_bits_long(&s->gb, 24); // language descriptor
  1253. if (get_bits1(&s->gb)) {
  1254. /* How can one fit 1024 bytes of text here if the maximum value
  1255. * for the asset header size field above was 512 bytes? */
  1256. int text_length = get_bits(&s->gb, 10) + 1;
  1257. if (get_bits_left(&s->gb) < text_length * 8)
  1258. return -1;
  1259. skip_bits_long(&s->gb, text_length * 8); // info text
  1260. }
  1261. skip_bits(&s->gb, 5); // bit resolution - 1
  1262. skip_bits(&s->gb, 4); // max sample rate code
  1263. channels = get_bits(&s->gb, 8) + 1;
  1264. if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
  1265. int spkr_remap_sets;
  1266. int spkr_mask_size = 16;
  1267. int num_spkrs[7];
  1268. if (channels > 2)
  1269. embedded_stereo = get_bits1(&s->gb);
  1270. if (channels > 6)
  1271. embedded_6ch = get_bits1(&s->gb);
  1272. if (get_bits1(&s->gb)) {
  1273. spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
  1274. skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
  1275. }
  1276. spkr_remap_sets = get_bits(&s->gb, 3);
  1277. for (i = 0; i < spkr_remap_sets; i++) {
  1278. /* std layout mask for each remap set */
  1279. num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
  1280. }
  1281. for (i = 0; i < spkr_remap_sets; i++) {
  1282. int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
  1283. if (get_bits_left(&s->gb) < 0)
  1284. return -1;
  1285. for (j = 0; j < num_spkrs[i]; j++) {
  1286. int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
  1287. int num_dec_ch = av_popcount(remap_dec_ch_mask);
  1288. skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
  1289. }
  1290. }
  1291. } else {
  1292. skip_bits(&s->gb, 3); // representation type
  1293. }
  1294. }
  1295. drc_code_present = get_bits1(&s->gb);
  1296. if (drc_code_present)
  1297. get_bits(&s->gb, 8); // drc code
  1298. if (get_bits1(&s->gb))
  1299. skip_bits(&s->gb, 5); // dialog normalization code
  1300. if (drc_code_present && embedded_stereo)
  1301. get_bits(&s->gb, 8); // drc stereo code
  1302. if (s->mix_metadata && get_bits1(&s->gb)) {
  1303. skip_bits(&s->gb, 1); // external mix
  1304. skip_bits(&s->gb, 6); // post mix gain code
  1305. if (get_bits(&s->gb, 2) != 3) // mixer drc code
  1306. skip_bits(&s->gb, 3); // drc limit
  1307. else
  1308. skip_bits(&s->gb, 8); // custom drc code
  1309. if (get_bits1(&s->gb)) // channel specific scaling
  1310. for (i = 0; i < s->num_mix_configs; i++)
  1311. skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
  1312. else
  1313. skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
  1314. for (i = 0; i < s->num_mix_configs; i++) {
  1315. if (get_bits_left(&s->gb) < 0)
  1316. return -1;
  1317. dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
  1318. if (embedded_6ch)
  1319. dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
  1320. if (embedded_stereo)
  1321. dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
  1322. }
  1323. }
  1324. switch (get_bits(&s->gb, 2)) {
  1325. case 0: extensions_mask = get_bits(&s->gb, 12); break;
  1326. case 1: extensions_mask = DCA_EXT_EXSS_XLL; break;
  1327. case 2: extensions_mask = DCA_EXT_EXSS_LBR; break;
  1328. case 3: extensions_mask = 0; /* aux coding */ break;
  1329. }
  1330. /* not parsed further, we were only interested in the extensions mask */
  1331. if (get_bits_left(&s->gb) < 0)
  1332. return -1;
  1333. if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
  1334. av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
  1335. return -1;
  1336. }
  1337. skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
  1338. if (extensions_mask & DCA_EXT_EXSS_XLL)
  1339. s->profile = FF_PROFILE_DTS_HD_MA;
  1340. else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
  1341. DCA_EXT_EXSS_XXCH))
  1342. s->profile = FF_PROFILE_DTS_HD_HRA;
  1343. if (!(extensions_mask & DCA_EXT_CORE))
  1344. av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
  1345. if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
  1346. av_log(s->avctx, AV_LOG_WARNING,
  1347. "DTS extensions detection mismatch (%d, %d)\n",
  1348. extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
  1349. return 0;
  1350. }
  1351. /**
  1352. * Parse extension substream header (HD)
  1353. */
  1354. static void dca_exss_parse_header(DCAContext *s)
  1355. {
  1356. int ss_index;
  1357. int blownup;
  1358. int num_audiop = 1;
  1359. int num_assets = 1;
  1360. int active_ss_mask[8];
  1361. int i, j;
  1362. if (get_bits_left(&s->gb) < 52)
  1363. return;
  1364. skip_bits(&s->gb, 8); // user data
  1365. ss_index = get_bits(&s->gb, 2);
  1366. blownup = get_bits1(&s->gb);
  1367. skip_bits(&s->gb, 8 + 4 * blownup); // header_size
  1368. skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
  1369. s->static_fields = get_bits1(&s->gb);
  1370. if (s->static_fields) {
  1371. skip_bits(&s->gb, 2); // reference clock code
  1372. skip_bits(&s->gb, 3); // frame duration code
  1373. if (get_bits1(&s->gb))
  1374. skip_bits_long(&s->gb, 36); // timestamp
  1375. /* a single stream can contain multiple audio assets that can be
  1376. * combined to form multiple audio presentations */
  1377. num_audiop = get_bits(&s->gb, 3) + 1;
  1378. if (num_audiop > 1) {
  1379. avpriv_request_sample(s->avctx,
  1380. "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. avpriv_request_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. AVFrame *frame = data;
  1425. const uint8_t *buf = avpkt->data;
  1426. int buf_size = avpkt->size;
  1427. int lfe_samples;
  1428. int num_core_channels = 0;
  1429. int i, ret;
  1430. float **samples_flt;
  1431. DCAContext *s = avctx->priv_data;
  1432. int channels, full_channels;
  1433. int core_ss_end;
  1434. s->xch_present = 0;
  1435. s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
  1436. DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
  1437. if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
  1438. av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
  1439. return AVERROR_INVALIDDATA;
  1440. }
  1441. init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
  1442. if ((ret = dca_parse_frame_header(s)) < 0) {
  1443. //seems like the frame is corrupt, try with the next one
  1444. return ret;
  1445. }
  1446. //set AVCodec values with parsed data
  1447. avctx->sample_rate = s->sample_rate;
  1448. avctx->bit_rate = s->bit_rate;
  1449. s->profile = FF_PROFILE_DTS;
  1450. for (i = 0; i < (s->sample_blocks / 8); i++) {
  1451. if ((ret = dca_decode_block(s, 0, i))) {
  1452. av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
  1453. return ret;
  1454. }
  1455. }
  1456. /* record number of core channels incase less than max channels are requested */
  1457. num_core_channels = s->prim_channels;
  1458. if (s->ext_coding)
  1459. s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
  1460. else
  1461. s->core_ext_mask = 0;
  1462. core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
  1463. /* only scan for extensions if ext_descr was unknown or indicated a
  1464. * supported XCh extension */
  1465. if (s->core_ext_mask < 0 || s->core_ext_mask & DCA_EXT_XCH) {
  1466. /* if ext_descr was unknown, clear s->core_ext_mask so that the
  1467. * extensions scan can fill it up */
  1468. s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
  1469. /* extensions start at 32-bit boundaries into bitstream */
  1470. skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
  1471. while (core_ss_end - get_bits_count(&s->gb) >= 32) {
  1472. uint32_t bits = get_bits_long(&s->gb, 32);
  1473. switch (bits) {
  1474. case 0x5a5a5a5a: {
  1475. int ext_amode, xch_fsize;
  1476. s->xch_base_channel = s->prim_channels;
  1477. /* validate sync word using XCHFSIZE field */
  1478. xch_fsize = show_bits(&s->gb, 10);
  1479. if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
  1480. (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
  1481. continue;
  1482. /* skip length-to-end-of-frame field for the moment */
  1483. skip_bits(&s->gb, 10);
  1484. s->core_ext_mask |= DCA_EXT_XCH;
  1485. /* extension amode(number of channels in extension) should be 1 */
  1486. /* AFAIK XCh is not used for more channels */
  1487. if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
  1488. av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
  1489. " supported!\n", ext_amode);
  1490. continue;
  1491. }
  1492. /* much like core primary audio coding header */
  1493. dca_parse_audio_coding_header(s, s->xch_base_channel);
  1494. for (i = 0; i < (s->sample_blocks / 8); i++)
  1495. if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
  1496. av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
  1497. continue;
  1498. }
  1499. s->xch_present = 1;
  1500. break;
  1501. }
  1502. case 0x47004a03:
  1503. /* XXCh: extended channels */
  1504. /* usually found either in core or HD part in DTS-HD HRA streams,
  1505. * but not in DTS-ES which contains XCh extensions instead */
  1506. s->core_ext_mask |= DCA_EXT_XXCH;
  1507. break;
  1508. case 0x1d95f262: {
  1509. int fsize96 = show_bits(&s->gb, 12) + 1;
  1510. if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
  1511. continue;
  1512. av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
  1513. get_bits_count(&s->gb));
  1514. skip_bits(&s->gb, 12);
  1515. av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
  1516. av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
  1517. s->core_ext_mask |= DCA_EXT_X96;
  1518. break;
  1519. }
  1520. }
  1521. skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
  1522. }
  1523. } else {
  1524. /* no supported extensions, skip the rest of the core substream */
  1525. skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
  1526. }
  1527. if (s->core_ext_mask & DCA_EXT_X96)
  1528. s->profile = FF_PROFILE_DTS_96_24;
  1529. else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
  1530. s->profile = FF_PROFILE_DTS_ES;
  1531. /* check for ExSS (HD part) */
  1532. if (s->dca_buffer_size - s->frame_size > 32 &&
  1533. get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
  1534. dca_exss_parse_header(s);
  1535. avctx->profile = s->profile;
  1536. full_channels = channels = s->prim_channels + !!s->lfe;
  1537. if (s->amode < 16) {
  1538. avctx->channel_layout = dca_core_channel_layout[s->amode];
  1539. if (s->xch_present && (!avctx->request_channels ||
  1540. avctx->request_channels > num_core_channels + !!s->lfe)) {
  1541. avctx->channel_layout |= AV_CH_BACK_CENTER;
  1542. if (s->lfe) {
  1543. avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
  1544. s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
  1545. } else {
  1546. s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
  1547. }
  1548. } else {
  1549. channels = num_core_channels + !!s->lfe;
  1550. s->xch_present = 0; /* disable further xch processing */
  1551. if (s->lfe) {
  1552. avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
  1553. s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
  1554. } else
  1555. s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
  1556. }
  1557. if (channels > !!s->lfe &&
  1558. s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
  1559. return AVERROR_INVALIDDATA;
  1560. if (avctx->request_channels == 2 && s->prim_channels > 2) {
  1561. channels = 2;
  1562. s->output = DCA_STEREO;
  1563. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  1564. }
  1565. } else {
  1566. av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n", s->amode);
  1567. return AVERROR_INVALIDDATA;
  1568. }
  1569. avctx->channels = channels;
  1570. /* get output buffer */
  1571. frame->nb_samples = 256 * (s->sample_blocks / 8);
  1572. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  1573. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1574. return ret;
  1575. }
  1576. samples_flt = (float **)frame->extended_data;
  1577. /* allocate buffer for extra channels if downmixing */
  1578. if (avctx->channels < full_channels) {
  1579. ret = av_samples_get_buffer_size(NULL, full_channels - channels,
  1580. frame->nb_samples,
  1581. avctx->sample_fmt, 0);
  1582. if (ret < 0)
  1583. return ret;
  1584. av_fast_malloc(&s->extra_channels_buffer,
  1585. &s->extra_channels_buffer_size, ret);
  1586. if (!s->extra_channels_buffer)
  1587. return AVERROR(ENOMEM);
  1588. ret = av_samples_fill_arrays((uint8_t **)s->extra_channels, NULL,
  1589. s->extra_channels_buffer,
  1590. full_channels - channels,
  1591. frame->nb_samples, avctx->sample_fmt, 0);
  1592. if (ret < 0)
  1593. return ret;
  1594. }
  1595. /* filter to get final output */
  1596. for (i = 0; i < (s->sample_blocks / 8); i++) {
  1597. int ch;
  1598. for (ch = 0; ch < channels; ch++)
  1599. s->samples_chanptr[ch] = samples_flt[ch] + i * 256;
  1600. for (; ch < full_channels; ch++)
  1601. s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * 256;
  1602. dca_filter_channels(s, i);
  1603. /* If this was marked as a DTS-ES stream we need to subtract back- */
  1604. /* channel from SL & SR to remove matrixed back-channel signal */
  1605. if ((s->source_pcm_res & 1) && s->xch_present) {
  1606. float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
  1607. float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
  1608. float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
  1609. s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
  1610. s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
  1611. }
  1612. }
  1613. /* update lfe history */
  1614. lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
  1615. for (i = 0; i < 2 * s->lfe * 4; i++)
  1616. s->lfe_data[i] = s->lfe_data[i + lfe_samples];
  1617. *got_frame_ptr = 1;
  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. return 0;
  1642. }
  1643. static av_cold int dca_decode_end(AVCodecContext *avctx)
  1644. {
  1645. DCAContext *s = avctx->priv_data;
  1646. ff_mdct_end(&s->imdct);
  1647. av_freep(&s->extra_channels_buffer);
  1648. return 0;
  1649. }
  1650. static const AVProfile profiles[] = {
  1651. { FF_PROFILE_DTS, "DTS" },
  1652. { FF_PROFILE_DTS_ES, "DTS-ES" },
  1653. { FF_PROFILE_DTS_96_24, "DTS 96/24" },
  1654. { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
  1655. { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
  1656. { FF_PROFILE_UNKNOWN },
  1657. };
  1658. AVCodec ff_dca_decoder = {
  1659. .name = "dca",
  1660. .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
  1661. .type = AVMEDIA_TYPE_AUDIO,
  1662. .id = AV_CODEC_ID_DTS,
  1663. .priv_data_size = sizeof(DCAContext),
  1664. .init = dca_decode_init,
  1665. .decode = dca_decode_frame,
  1666. .close = dca_decode_end,
  1667. .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
  1668. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
  1669. AV_SAMPLE_FMT_NONE },
  1670. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  1671. };