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.

2056 lines
76KB

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