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.

1459 lines
54KB

  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 FFmpeg.
  9. *
  10. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/intmath.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "avcodec.h"
  30. #include "dsputil.h"
  31. #include "fft.h"
  32. #include "get_bits.h"
  33. #include "put_bits.h"
  34. #include "dcadata.h"
  35. #include "dcahuff.h"
  36. #include "dca.h"
  37. #include "synth_filter.h"
  38. #include "dcadsp.h"
  39. //#define TRACE
  40. #define DCA_PRIM_CHANNELS_MAX (7)
  41. #define DCA_SUBBANDS (32)
  42. #define DCA_ABITS_MAX (32) /* Should be 28 */
  43. #define DCA_SUBSUBFRAMES_MAX (4)
  44. #define DCA_SUBFRAMES_MAX (16)
  45. #define DCA_BLOCKS_MAX (16)
  46. #define DCA_LFE_MAX (3)
  47. enum DCAMode {
  48. DCA_MONO = 0,
  49. DCA_CHANNEL,
  50. DCA_STEREO,
  51. DCA_STEREO_SUMDIFF,
  52. DCA_STEREO_TOTAL,
  53. DCA_3F,
  54. DCA_2F1R,
  55. DCA_3F1R,
  56. DCA_2F2R,
  57. DCA_3F2R,
  58. DCA_4F2R
  59. };
  60. /* Tables for mapping dts channel configurations to libavcodec multichannel api.
  61. * Some compromises have been made for special configurations. Most configurations
  62. * are never used so complete accuracy is not needed.
  63. *
  64. * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
  65. * S -> side, when both rear and back are configured move one of them to the side channel
  66. * OV -> center back
  67. * All 2 channel configurations -> CH_LAYOUT_STEREO
  68. */
  69. static const int64_t dca_core_channel_layout[] = {
  70. CH_FRONT_CENTER, ///< 1, A
  71. CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
  72. CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
  73. CH_LAYOUT_STEREO, ///< 2, (L+R) + (L-R) (sum-difference)
  74. CH_LAYOUT_STEREO, ///< 2, LT +RT (left and right total)
  75. CH_LAYOUT_STEREO|CH_FRONT_CENTER, ///< 3, C+L+R
  76. CH_LAYOUT_STEREO|CH_BACK_CENTER, ///< 3, L+R+S
  77. CH_LAYOUT_STEREO|CH_FRONT_CENTER|CH_BACK_CENTER, ///< 4, C + L + R+ S
  78. CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT, ///< 4, L + R +SL+ SR
  79. CH_LAYOUT_STEREO|CH_FRONT_CENTER|CH_SIDE_LEFT|CH_SIDE_RIGHT, ///< 5, C + L + R+ SL+SR
  80. CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT|CH_FRONT_LEFT_OF_CENTER|CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR
  81. CH_LAYOUT_STEREO|CH_BACK_LEFT|CH_BACK_RIGHT|CH_FRONT_CENTER|CH_BACK_CENTER, ///< 6, C + L + R+ LR + RR + OV
  82. CH_FRONT_CENTER|CH_FRONT_RIGHT_OF_CENTER|CH_FRONT_LEFT_OF_CENTER|CH_BACK_CENTER|CH_BACK_LEFT|CH_BACK_RIGHT, ///< 6, CF+ CR+LF+ RF+LR + RR
  83. CH_FRONT_LEFT_OF_CENTER|CH_FRONT_CENTER|CH_FRONT_RIGHT_OF_CENTER|CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
  84. CH_FRONT_LEFT_OF_CENTER|CH_FRONT_RIGHT_OF_CENTER|CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_SIDE_RIGHT|CH_BACK_LEFT|CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2+ SR1 + SR2
  85. CH_FRONT_LEFT_OF_CENTER|CH_FRONT_CENTER|CH_FRONT_RIGHT_OF_CENTER|CH_LAYOUT_STEREO|CH_SIDE_LEFT|CH_BACK_CENTER|CH_SIDE_RIGHT, ///< 8, CL + C+ CR + L + R + SL + S+ SR
  86. };
  87. static const int8_t dca_lfe_index[] = {
  88. 1,2,2,2,2,3,2,3,2,3,2,3,1,3,2,3
  89. };
  90. static const int8_t dca_channel_reorder_lfe[][9] = {
  91. { 0, -1, -1, -1, -1, -1, -1, -1, -1},
  92. { 0, 1, -1, -1, -1, -1, -1, -1, -1},
  93. { 0, 1, -1, -1, -1, -1, -1, -1, -1},
  94. { 0, 1, -1, -1, -1, -1, -1, -1, -1},
  95. { 0, 1, -1, -1, -1, -1, -1, -1, -1},
  96. { 2, 0, 1, -1, -1, -1, -1, -1, -1},
  97. { 0, 1, 3, -1, -1, -1, -1, -1, -1},
  98. { 2, 0, 1, 4, -1, -1, -1, -1, -1},
  99. { 0, 1, 3, 4, -1, -1, -1, -1, -1},
  100. { 2, 0, 1, 4, 5, -1, -1, -1, -1},
  101. { 3, 4, 0, 1, 5, 6, -1, -1, -1},
  102. { 2, 0, 1, 4, 5, 6, -1, -1, -1},
  103. { 0, 6, 4, 5, 2, 3, -1, -1, -1},
  104. { 4, 2, 5, 0, 1, 6, 7, -1, -1},
  105. { 5, 6, 0, 1, 7, 3, 8, 4, -1},
  106. { 4, 2, 5, 0, 1, 6, 8, 7, -1},
  107. };
  108. static const int8_t dca_channel_reorder_lfe_xch[][9] = {
  109. { 0, 2, -1, -1, -1, -1, -1, -1, -1},
  110. { 0, 1, 3, -1, -1, -1, -1, -1, -1},
  111. { 0, 1, 3, -1, -1, -1, -1, -1, -1},
  112. { 0, 1, 3, -1, -1, -1, -1, -1, -1},
  113. { 0, 1, 3, -1, -1, -1, -1, -1, -1},
  114. { 2, 0, 1, 4, -1, -1, -1, -1, -1},
  115. { 0, 1, 3, 4, -1, -1, -1, -1, -1},
  116. { 2, 0, 1, 4, 5, -1, -1, -1, -1},
  117. { 0, 1, 4, 5, 3, -1, -1, -1, -1},
  118. { 2, 0, 1, 5, 6, 4, -1, -1, -1},
  119. { 3, 4, 0, 1, 6, 7, 5, -1, -1},
  120. { 2, 0, 1, 4, 5, 6, 7, -1, -1},
  121. { 0, 6, 4, 5, 2, 3, 7, -1, -1},
  122. { 4, 2, 5, 0, 1, 7, 8, 6, -1},
  123. { 5, 6, 0, 1, 8, 3, 9, 4, 7},
  124. { 4, 2, 5, 0, 1, 6, 9, 8, 7},
  125. };
  126. static const int8_t dca_channel_reorder_nolfe[][9] = {
  127. { 0, -1, -1, -1, -1, -1, -1, -1, -1},
  128. { 0, 1, -1, -1, -1, -1, -1, -1, -1},
  129. { 0, 1, -1, -1, -1, -1, -1, -1, -1},
  130. { 0, 1, -1, -1, -1, -1, -1, -1, -1},
  131. { 0, 1, -1, -1, -1, -1, -1, -1, -1},
  132. { 2, 0, 1, -1, -1, -1, -1, -1, -1},
  133. { 0, 1, 2, -1, -1, -1, -1, -1, -1},
  134. { 2, 0, 1, 3, -1, -1, -1, -1, -1},
  135. { 0, 1, 2, 3, -1, -1, -1, -1, -1},
  136. { 2, 0, 1, 3, 4, -1, -1, -1, -1},
  137. { 2, 3, 0, 1, 4, 5, -1, -1, -1},
  138. { 2, 0, 1, 3, 4, 5, -1, -1, -1},
  139. { 0, 5, 3, 4, 1, 2, -1, -1, -1},
  140. { 3, 2, 4, 0, 1, 5, 6, -1, -1},
  141. { 4, 5, 0, 1, 6, 2, 7, 3, -1},
  142. { 3, 2, 4, 0, 1, 5, 7, 6, -1},
  143. };
  144. static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
  145. { 0, 1, -1, -1, -1, -1, -1, -1, -1},
  146. { 0, 1, 2, -1, -1, -1, -1, -1, -1},
  147. { 0, 1, 2, -1, -1, -1, -1, -1, -1},
  148. { 0, 1, 2, -1, -1, -1, -1, -1, -1},
  149. { 0, 1, 2, -1, -1, -1, -1, -1, -1},
  150. { 2, 0, 1, 3, -1, -1, -1, -1, -1},
  151. { 0, 1, 2, 3, -1, -1, -1, -1, -1},
  152. { 2, 0, 1, 3, 4, -1, -1, -1, -1},
  153. { 0, 1, 3, 4, 2, -1, -1, -1, -1},
  154. { 2, 0, 1, 4, 5, 3, -1, -1, -1},
  155. { 2, 3, 0, 1, 5, 6, 4, -1, -1},
  156. { 2, 0, 1, 3, 4, 5, 6, -1, -1},
  157. { 0, 5, 3, 4, 1, 2, 6, -1, -1},
  158. { 3, 2, 4, 0, 1, 6, 7, 5, -1},
  159. { 4, 5, 0, 1, 7, 2, 8, 3, 6},
  160. { 3, 2, 4, 0, 1, 5, 8, 7, 6},
  161. };
  162. #define DCA_DOLBY 101 /* FIXME */
  163. #define DCA_CHANNEL_BITS 6
  164. #define DCA_CHANNEL_MASK 0x3F
  165. #define DCA_LFE 0x80
  166. #define HEADER_SIZE 14
  167. #define DCA_MAX_FRAME_SIZE 16384
  168. /** Bit allocation */
  169. typedef struct {
  170. int offset; ///< code values offset
  171. int maxbits[8]; ///< max bits in VLC
  172. int wrap; ///< wrap for get_vlc2()
  173. VLC vlc[8]; ///< actual codes
  174. } BitAlloc;
  175. static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
  176. static BitAlloc dca_tmode; ///< transition mode VLCs
  177. static BitAlloc dca_scalefactor; ///< scalefactor VLCs
  178. static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
  179. static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba, int idx)
  180. {
  181. return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) + ba->offset;
  182. }
  183. typedef struct {
  184. AVCodecContext *avctx;
  185. /* Frame header */
  186. int frame_type; ///< type of the current frame
  187. int samples_deficit; ///< deficit sample count
  188. int crc_present; ///< crc is present in the bitstream
  189. int sample_blocks; ///< number of PCM sample blocks
  190. int frame_size; ///< primary frame byte size
  191. int amode; ///< audio channels arrangement
  192. int sample_rate; ///< audio sampling rate
  193. int bit_rate; ///< transmission bit rate
  194. int bit_rate_index; ///< transmission bit rate index
  195. int downmix; ///< embedded downmix enabled
  196. int dynrange; ///< embedded dynamic range flag
  197. int timestamp; ///< embedded time stamp flag
  198. int aux_data; ///< auxiliary data flag
  199. int hdcd; ///< source material is mastered in HDCD
  200. int ext_descr; ///< extension audio descriptor flag
  201. int ext_coding; ///< extended coding flag
  202. int aspf; ///< audio sync word insertion flag
  203. int lfe; ///< low frequency effects flag
  204. int predictor_history; ///< predictor history flag
  205. int header_crc; ///< header crc check bytes
  206. int multirate_inter; ///< multirate interpolator switch
  207. int version; ///< encoder software revision
  208. int copy_history; ///< copy history
  209. int source_pcm_res; ///< source pcm resolution
  210. int front_sum; ///< front sum/difference flag
  211. int surround_sum; ///< surround sum/difference flag
  212. int dialog_norm; ///< dialog normalisation parameter
  213. /* Primary audio coding header */
  214. int subframes; ///< number of subframes
  215. int total_channels; ///< number of channels including extensions
  216. int prim_channels; ///< number of primary audio channels
  217. int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
  218. int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
  219. int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
  220. int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
  221. int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
  222. int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
  223. int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
  224. float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
  225. /* Primary audio coding side information */
  226. int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes
  227. int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count
  228. int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
  229. int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
  230. int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
  231. int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
  232. int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient)
  233. int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
  234. int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
  235. int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]; ///< stereo downmix coefficients
  236. int dynrange_coef; ///< dynamic range coefficient
  237. int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
  238. float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
  239. int lfe_scale_factor;
  240. /* Subband samples history (for ADPCM) */
  241. float subband_samples_hist[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
  242. DECLARE_ALIGNED(16, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
  243. DECLARE_ALIGNED(16, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
  244. int hist_index[DCA_PRIM_CHANNELS_MAX];
  245. DECLARE_ALIGNED(16, float, raXin)[32];
  246. int output; ///< type of output
  247. float add_bias; ///< output bias
  248. float scale_bias; ///< output scale
  249. DECLARE_ALIGNED(16, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
  250. DECLARE_ALIGNED(16, float, samples)[(DCA_PRIM_CHANNELS_MAX+1)*256];
  251. const float *samples_chanptr[DCA_PRIM_CHANNELS_MAX+1];
  252. uint8_t dca_buffer[DCA_MAX_FRAME_SIZE];
  253. int dca_buffer_size; ///< how much data is in the dca_buffer
  254. const int8_t* channel_order_tab; ///< channel reordering table, lfe and non lfe
  255. GetBitContext gb;
  256. /* Current position in DCA frame */
  257. int current_subframe;
  258. int current_subsubframe;
  259. int debug_flag; ///< used for suppressing repeated error messages output
  260. DSPContext dsp;
  261. FFTContext imdct;
  262. SynthFilterContext synth;
  263. DCADSPContext dcadsp;
  264. } DCAContext;
  265. static const uint16_t dca_vlc_offs[] = {
  266. 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
  267. 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
  268. 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
  269. 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
  270. 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
  271. 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
  272. };
  273. static av_cold void dca_init_vlcs(void)
  274. {
  275. static int vlcs_initialized = 0;
  276. int i, j, c = 14;
  277. static VLC_TYPE dca_table[23622][2];
  278. if (vlcs_initialized)
  279. return;
  280. dca_bitalloc_index.offset = 1;
  281. dca_bitalloc_index.wrap = 2;
  282. for (i = 0; i < 5; i++) {
  283. dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
  284. dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
  285. init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
  286. bitalloc_12_bits[i], 1, 1,
  287. bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
  288. }
  289. dca_scalefactor.offset = -64;
  290. dca_scalefactor.wrap = 2;
  291. for (i = 0; i < 5; i++) {
  292. dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
  293. dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
  294. init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
  295. scales_bits[i], 1, 1,
  296. scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
  297. }
  298. dca_tmode.offset = 0;
  299. dca_tmode.wrap = 1;
  300. for (i = 0; i < 4; i++) {
  301. dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
  302. dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
  303. init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
  304. tmode_bits[i], 1, 1,
  305. tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
  306. }
  307. for (i = 0; i < 10; i++)
  308. for (j = 0; j < 7; j++){
  309. if (!bitalloc_codes[i][j]) break;
  310. dca_smpl_bitalloc[i+1].offset = bitalloc_offsets[i];
  311. dca_smpl_bitalloc[i+1].wrap = 1 + (j > 4);
  312. dca_smpl_bitalloc[i+1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
  313. dca_smpl_bitalloc[i+1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
  314. init_vlc(&dca_smpl_bitalloc[i+1].vlc[j], bitalloc_maxbits[i][j],
  315. bitalloc_sizes[i],
  316. bitalloc_bits[i][j], 1, 1,
  317. bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
  318. c++;
  319. }
  320. vlcs_initialized = 1;
  321. }
  322. static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
  323. {
  324. while(len--)
  325. *dst++ = get_bits(gb, bits);
  326. }
  327. static int dca_parse_audio_coding_header(DCAContext * s, int base_channel)
  328. {
  329. int i, j;
  330. static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
  331. static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
  332. static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
  333. s->total_channels = get_bits(&s->gb, 3) + 1 + base_channel;
  334. s->prim_channels = s->total_channels;
  335. if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
  336. s->prim_channels = DCA_PRIM_CHANNELS_MAX;
  337. for (i = base_channel; i < s->prim_channels; i++) {
  338. s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
  339. if (s->subband_activity[i] > DCA_SUBBANDS)
  340. s->subband_activity[i] = DCA_SUBBANDS;
  341. }
  342. for (i = base_channel; i < s->prim_channels; i++) {
  343. s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
  344. if (s->vq_start_subband[i] > DCA_SUBBANDS)
  345. s->vq_start_subband[i] = DCA_SUBBANDS;
  346. }
  347. get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
  348. get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
  349. get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
  350. get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
  351. /* Get codebooks quantization indexes */
  352. if (!base_channel)
  353. memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
  354. for (j = 1; j < 11; j++)
  355. for (i = base_channel; i < s->prim_channels; i++)
  356. s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
  357. /* Get scale factor adjustment */
  358. for (j = 0; j < 11; j++)
  359. for (i = base_channel; i < s->prim_channels; i++)
  360. s->scalefactor_adj[i][j] = 1;
  361. for (j = 1; j < 11; j++)
  362. for (i = base_channel; i < s->prim_channels; i++)
  363. if (s->quant_index_huffman[i][j] < thr[j])
  364. s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
  365. if (s->crc_present) {
  366. /* Audio header CRC check */
  367. get_bits(&s->gb, 16);
  368. }
  369. s->current_subframe = 0;
  370. s->current_subsubframe = 0;
  371. #ifdef TRACE
  372. av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
  373. av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
  374. for (i = base_channel; i < s->prim_channels; i++){
  375. av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n", s->subband_activity[i]);
  376. av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n", s->vq_start_subband[i]);
  377. av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n", s->joint_intensity[i]);
  378. av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n", s->transient_huffman[i]);
  379. av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n", s->scalefactor_huffman[i]);
  380. av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n", s->bitalloc_huffman[i]);
  381. av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
  382. for (j = 0; j < 11; j++)
  383. av_log(s->avctx, AV_LOG_DEBUG, " %i",
  384. s->quant_index_huffman[i][j]);
  385. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  386. av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
  387. for (j = 0; j < 11; j++)
  388. av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
  389. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  390. }
  391. #endif
  392. return 0;
  393. }
  394. static int dca_parse_frame_header(DCAContext * s)
  395. {
  396. init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
  397. /* Sync code */
  398. get_bits(&s->gb, 32);
  399. /* Frame header */
  400. s->frame_type = get_bits(&s->gb, 1);
  401. s->samples_deficit = get_bits(&s->gb, 5) + 1;
  402. s->crc_present = get_bits(&s->gb, 1);
  403. s->sample_blocks = get_bits(&s->gb, 7) + 1;
  404. s->frame_size = get_bits(&s->gb, 14) + 1;
  405. if (s->frame_size < 95)
  406. return -1;
  407. s->amode = get_bits(&s->gb, 6);
  408. s->sample_rate = dca_sample_rates[get_bits(&s->gb, 4)];
  409. if (!s->sample_rate)
  410. return -1;
  411. s->bit_rate_index = get_bits(&s->gb, 5);
  412. s->bit_rate = dca_bit_rates[s->bit_rate_index];
  413. if (!s->bit_rate)
  414. return -1;
  415. s->downmix = get_bits(&s->gb, 1);
  416. s->dynrange = get_bits(&s->gb, 1);
  417. s->timestamp = get_bits(&s->gb, 1);
  418. s->aux_data = get_bits(&s->gb, 1);
  419. s->hdcd = get_bits(&s->gb, 1);
  420. s->ext_descr = get_bits(&s->gb, 3);
  421. s->ext_coding = get_bits(&s->gb, 1);
  422. s->aspf = get_bits(&s->gb, 1);
  423. s->lfe = get_bits(&s->gb, 2);
  424. s->predictor_history = get_bits(&s->gb, 1);
  425. /* TODO: check CRC */
  426. if (s->crc_present)
  427. s->header_crc = get_bits(&s->gb, 16);
  428. s->multirate_inter = get_bits(&s->gb, 1);
  429. s->version = get_bits(&s->gb, 4);
  430. s->copy_history = get_bits(&s->gb, 2);
  431. s->source_pcm_res = get_bits(&s->gb, 3);
  432. s->front_sum = get_bits(&s->gb, 1);
  433. s->surround_sum = get_bits(&s->gb, 1);
  434. s->dialog_norm = get_bits(&s->gb, 4);
  435. /* FIXME: channels mixing levels */
  436. s->output = s->amode;
  437. if (s->lfe) s->output |= DCA_LFE;
  438. #ifdef TRACE
  439. av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
  440. av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
  441. av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
  442. av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
  443. s->sample_blocks, s->sample_blocks * 32);
  444. av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
  445. av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
  446. s->amode, dca_channels[s->amode]);
  447. av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
  448. s->sample_rate);
  449. av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
  450. s->bit_rate);
  451. av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
  452. av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
  453. av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
  454. av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
  455. av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
  456. av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
  457. av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
  458. av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
  459. av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
  460. av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
  461. s->predictor_history);
  462. av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
  463. av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
  464. s->multirate_inter);
  465. av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
  466. av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
  467. av_log(s->avctx, AV_LOG_DEBUG,
  468. "source pcm resolution: %i (%i bits/sample)\n",
  469. s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
  470. av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
  471. av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
  472. av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
  473. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  474. #endif
  475. /* Primary audio coding header */
  476. s->subframes = get_bits(&s->gb, 4) + 1;
  477. return dca_parse_audio_coding_header(s, 0);
  478. }
  479. static inline int get_scale(GetBitContext *gb, int level, int value)
  480. {
  481. if (level < 5) {
  482. /* huffman encoded */
  483. value += get_bitalloc(gb, &dca_scalefactor, level);
  484. } else if (level < 8)
  485. value = get_bits(gb, level + 1);
  486. return value;
  487. }
  488. static int dca_subframe_header(DCAContext * s, int base_channel, int block_index)
  489. {
  490. /* Primary audio coding side information */
  491. int j, k;
  492. if (!base_channel) {
  493. s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
  494. s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
  495. }
  496. for (j = base_channel; j < s->prim_channels; j++) {
  497. for (k = 0; k < s->subband_activity[j]; k++)
  498. s->prediction_mode[j][k] = get_bits(&s->gb, 1);
  499. }
  500. /* Get prediction codebook */
  501. for (j = base_channel; j < s->prim_channels; j++) {
  502. for (k = 0; k < s->subband_activity[j]; k++) {
  503. if (s->prediction_mode[j][k] > 0) {
  504. /* (Prediction coefficient VQ address) */
  505. s->prediction_vq[j][k] = get_bits(&s->gb, 12);
  506. }
  507. }
  508. }
  509. /* Bit allocation index */
  510. for (j = base_channel; j < s->prim_channels; j++) {
  511. for (k = 0; k < s->vq_start_subband[j]; k++) {
  512. if (s->bitalloc_huffman[j] == 6)
  513. s->bitalloc[j][k] = get_bits(&s->gb, 5);
  514. else if (s->bitalloc_huffman[j] == 5)
  515. s->bitalloc[j][k] = get_bits(&s->gb, 4);
  516. else if (s->bitalloc_huffman[j] == 7) {
  517. av_log(s->avctx, AV_LOG_ERROR,
  518. "Invalid bit allocation index\n");
  519. return -1;
  520. } else {
  521. s->bitalloc[j][k] =
  522. get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
  523. }
  524. if (s->bitalloc[j][k] > 26) {
  525. // av_log(s->avctx,AV_LOG_DEBUG,"bitalloc index [%i][%i] too big (%i)\n",
  526. // j, k, s->bitalloc[j][k]);
  527. return -1;
  528. }
  529. }
  530. }
  531. /* Transition mode */
  532. for (j = base_channel; j < s->prim_channels; j++) {
  533. for (k = 0; k < s->subband_activity[j]; k++) {
  534. s->transition_mode[j][k] = 0;
  535. if (s->subsubframes[s->current_subframe] > 1 &&
  536. k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
  537. s->transition_mode[j][k] =
  538. get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
  539. }
  540. }
  541. }
  542. for (j = base_channel; j < s->prim_channels; j++) {
  543. const uint32_t *scale_table;
  544. int scale_sum;
  545. memset(s->scale_factor[j], 0, s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
  546. if (s->scalefactor_huffman[j] == 6)
  547. scale_table = scale_factor_quant7;
  548. else
  549. scale_table = scale_factor_quant6;
  550. /* When huffman coded, only the difference is encoded */
  551. scale_sum = 0;
  552. for (k = 0; k < s->subband_activity[j]; k++) {
  553. if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
  554. scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
  555. s->scale_factor[j][k][0] = scale_table[scale_sum];
  556. }
  557. if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
  558. /* Get second scale factor */
  559. scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
  560. s->scale_factor[j][k][1] = scale_table[scale_sum];
  561. }
  562. }
  563. }
  564. /* Joint subband scale factor codebook select */
  565. for (j = base_channel; j < s->prim_channels; j++) {
  566. /* Transmitted only if joint subband coding enabled */
  567. if (s->joint_intensity[j] > 0)
  568. s->joint_huff[j] = get_bits(&s->gb, 3);
  569. }
  570. /* Scale factors for joint subband coding */
  571. for (j = base_channel; j < s->prim_channels; j++) {
  572. int source_channel;
  573. /* Transmitted only if joint subband coding enabled */
  574. if (s->joint_intensity[j] > 0) {
  575. int scale = 0;
  576. source_channel = s->joint_intensity[j] - 1;
  577. /* When huffman coded, only the difference is encoded
  578. * (is this valid as well for joint scales ???) */
  579. for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
  580. scale = get_scale(&s->gb, s->joint_huff[j], 0);
  581. scale += 64; /* bias */
  582. s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
  583. }
  584. if (!(s->debug_flag & 0x02)) {
  585. av_log(s->avctx, AV_LOG_DEBUG,
  586. "Joint stereo coding not supported\n");
  587. s->debug_flag |= 0x02;
  588. }
  589. }
  590. }
  591. /* Stereo downmix coefficients */
  592. if (!base_channel && s->prim_channels > 2) {
  593. if (s->downmix) {
  594. for (j = base_channel; j < s->prim_channels; j++) {
  595. s->downmix_coef[j][0] = get_bits(&s->gb, 7);
  596. s->downmix_coef[j][1] = get_bits(&s->gb, 7);
  597. }
  598. } else {
  599. int am = s->amode & DCA_CHANNEL_MASK;
  600. for (j = base_channel; j < s->prim_channels; j++) {
  601. s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
  602. s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
  603. }
  604. }
  605. }
  606. /* Dynamic range coefficient */
  607. if (s->dynrange)
  608. s->dynrange_coef = get_bits(&s->gb, 8);
  609. /* Side information CRC check word */
  610. if (s->crc_present) {
  611. get_bits(&s->gb, 16);
  612. }
  613. /*
  614. * Primary audio data arrays
  615. */
  616. /* VQ encoded high frequency subbands */
  617. for (j = base_channel; j < s->prim_channels; j++)
  618. for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
  619. /* 1 vector -> 32 samples */
  620. s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
  621. /* Low frequency effect data */
  622. if (!base_channel && s->lfe) {
  623. /* LFE samples */
  624. int lfe_samples = 2 * s->lfe * (4 + block_index);
  625. int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
  626. float lfe_scale;
  627. for (j = lfe_samples; j < lfe_end_sample; j++) {
  628. /* Signed 8 bits int */
  629. s->lfe_data[j] = get_sbits(&s->gb, 8);
  630. }
  631. /* Scale factor index */
  632. s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 8)];
  633. /* Quantization step size * scale factor */
  634. lfe_scale = 0.035 * s->lfe_scale_factor;
  635. for (j = lfe_samples; j < lfe_end_sample; j++)
  636. s->lfe_data[j] *= lfe_scale;
  637. }
  638. #ifdef TRACE
  639. av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n", s->subsubframes[s->current_subframe]);
  640. av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
  641. s->partial_samples[s->current_subframe]);
  642. for (j = base_channel; j < s->prim_channels; j++) {
  643. av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
  644. for (k = 0; k < s->subband_activity[j]; k++)
  645. av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
  646. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  647. }
  648. for (j = base_channel; j < s->prim_channels; j++) {
  649. for (k = 0; k < s->subband_activity[j]; k++)
  650. av_log(s->avctx, AV_LOG_DEBUG,
  651. "prediction coefs: %f, %f, %f, %f\n",
  652. (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
  653. (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
  654. (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
  655. (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
  656. }
  657. for (j = base_channel; j < s->prim_channels; j++) {
  658. av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
  659. for (k = 0; k < s->vq_start_subband[j]; k++)
  660. av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
  661. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  662. }
  663. for (j = base_channel; j < s->prim_channels; j++) {
  664. av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
  665. for (k = 0; k < s->subband_activity[j]; k++)
  666. av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
  667. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  668. }
  669. for (j = base_channel; j < s->prim_channels; j++) {
  670. av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
  671. for (k = 0; k < s->subband_activity[j]; k++) {
  672. if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
  673. av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
  674. if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
  675. av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
  676. }
  677. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  678. }
  679. for (j = base_channel; j < s->prim_channels; j++) {
  680. if (s->joint_intensity[j] > 0) {
  681. int source_channel = s->joint_intensity[j] - 1;
  682. av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
  683. for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
  684. av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
  685. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  686. }
  687. }
  688. if (!base_channel && s->prim_channels > 2 && s->downmix) {
  689. av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
  690. for (j = 0; j < s->prim_channels; j++) {
  691. av_log(s->avctx, AV_LOG_DEBUG, "Channel 0,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][0]]);
  692. av_log(s->avctx, AV_LOG_DEBUG, "Channel 1,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][1]]);
  693. }
  694. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  695. }
  696. for (j = base_channel; j < s->prim_channels; j++)
  697. for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
  698. av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
  699. if (!base_channel && s->lfe) {
  700. int lfe_samples = 2 * s->lfe * (4 + block_index);
  701. int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
  702. av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
  703. for (j = lfe_samples; j < lfe_end_sample; j++)
  704. av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
  705. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  706. }
  707. #endif
  708. return 0;
  709. }
  710. static void qmf_32_subbands(DCAContext * s, int chans,
  711. float samples_in[32][8], float *samples_out,
  712. float scale, float bias)
  713. {
  714. const float *prCoeff;
  715. int i;
  716. int sb_act = s->subband_activity[chans];
  717. int subindex;
  718. scale *= sqrt(1/8.0);
  719. /* Select filter */
  720. if (!s->multirate_inter) /* Non-perfect reconstruction */
  721. prCoeff = fir_32bands_nonperfect;
  722. else /* Perfect reconstruction */
  723. prCoeff = fir_32bands_perfect;
  724. /* Reconstructed channel sample index */
  725. for (subindex = 0; subindex < 8; subindex++) {
  726. /* Load in one sample from each subband and clear inactive subbands */
  727. for (i = 0; i < sb_act; i++){
  728. uint32_t v = AV_RN32A(&samples_in[i][subindex]) ^ ((i-1)&2)<<30;
  729. AV_WN32A(&s->raXin[i], v);
  730. }
  731. for (; i < 32; i++)
  732. s->raXin[i] = 0.0;
  733. s->synth.synth_filter_float(&s->imdct,
  734. s->subband_fir_hist[chans], &s->hist_index[chans],
  735. s->subband_fir_noidea[chans], prCoeff,
  736. samples_out, s->raXin, scale, bias);
  737. samples_out+= 32;
  738. }
  739. }
  740. static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
  741. int num_deci_sample, float *samples_in,
  742. float *samples_out, float scale,
  743. float bias)
  744. {
  745. /* samples_in: An array holding decimated samples.
  746. * Samples in current subframe starts from samples_in[0],
  747. * while samples_in[-1], samples_in[-2], ..., stores samples
  748. * from last subframe as history.
  749. *
  750. * samples_out: An array holding interpolated samples
  751. */
  752. int decifactor;
  753. const float *prCoeff;
  754. int deciindex;
  755. /* Select decimation filter */
  756. if (decimation_select == 1) {
  757. decifactor = 64;
  758. prCoeff = lfe_fir_128;
  759. } else {
  760. decifactor = 32;
  761. prCoeff = lfe_fir_64;
  762. }
  763. /* Interpolation */
  764. for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
  765. s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor,
  766. scale, bias);
  767. samples_in++;
  768. samples_out += 2 * decifactor;
  769. }
  770. }
  771. /* downmixing routines */
  772. #define MIX_REAR1(samples, si1, rs, coef) \
  773. samples[i] += samples[si1] * coef[rs][0]; \
  774. samples[i+256] += samples[si1] * coef[rs][1];
  775. #define MIX_REAR2(samples, si1, si2, rs, coef) \
  776. samples[i] += samples[si1] * coef[rs][0] + samples[si2] * coef[rs+1][0]; \
  777. samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs+1][1];
  778. #define MIX_FRONT3(samples, coef) \
  779. t = samples[i]; \
  780. samples[i] = t * coef[0][0] + samples[i+256] * coef[1][0] + samples[i+512] * coef[2][0]; \
  781. samples[i+256] = t * coef[0][1] + samples[i+256] * coef[1][1] + samples[i+512] * coef[2][1];
  782. #define DOWNMIX_TO_STEREO(op1, op2) \
  783. for (i = 0; i < 256; i++){ \
  784. op1 \
  785. op2 \
  786. }
  787. static void dca_downmix(float *samples, int srcfmt,
  788. int downmix_coef[DCA_PRIM_CHANNELS_MAX][2])
  789. {
  790. int i;
  791. float t;
  792. float coef[DCA_PRIM_CHANNELS_MAX][2];
  793. for (i=0; i<DCA_PRIM_CHANNELS_MAX; i++) {
  794. coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
  795. coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
  796. }
  797. switch (srcfmt) {
  798. case DCA_MONO:
  799. case DCA_CHANNEL:
  800. case DCA_STEREO_TOTAL:
  801. case DCA_STEREO_SUMDIFF:
  802. case DCA_4F2R:
  803. av_log(NULL, 0, "Not implemented!\n");
  804. break;
  805. case DCA_STEREO:
  806. break;
  807. case DCA_3F:
  808. DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),);
  809. break;
  810. case DCA_2F1R:
  811. DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + 512, 2, coef),);
  812. break;
  813. case DCA_3F1R:
  814. DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
  815. MIX_REAR1(samples, i + 768, 3, coef));
  816. break;
  817. case DCA_2F2R:
  818. DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + 512, i + 768, 2, coef),);
  819. break;
  820. case DCA_3F2R:
  821. DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
  822. MIX_REAR2(samples, i + 768, i + 1024, 3, coef));
  823. break;
  824. }
  825. }
  826. /* Very compact version of the block code decoder that does not use table
  827. * look-up but is slightly slower */
  828. static int decode_blockcode(int code, int levels, int *values)
  829. {
  830. int i;
  831. int offset = (levels - 1) >> 1;
  832. for (i = 0; i < 4; i++) {
  833. int div = FASTDIV(code, levels);
  834. values[i] = code - offset - div*levels;
  835. code = div;
  836. }
  837. if (code == 0)
  838. return 0;
  839. else {
  840. av_log(NULL, AV_LOG_ERROR, "ERROR: block code look-up failed\n");
  841. return -1;
  842. }
  843. }
  844. static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
  845. static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
  846. static int dca_subsubframe(DCAContext * s, int base_channel, int block_index)
  847. {
  848. int k, l;
  849. int subsubframe = s->current_subsubframe;
  850. const float *quant_step_table;
  851. /* FIXME */
  852. float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
  853. LOCAL_ALIGNED_16(int, block, [8]);
  854. /*
  855. * Audio data
  856. */
  857. /* Select quantization step size table */
  858. if (s->bit_rate_index == 0x1f)
  859. quant_step_table = lossless_quant_d;
  860. else
  861. quant_step_table = lossy_quant_d;
  862. for (k = base_channel; k < s->prim_channels; k++) {
  863. for (l = 0; l < s->vq_start_subband[k]; l++) {
  864. int m;
  865. /* Select the mid-tread linear quantizer */
  866. int abits = s->bitalloc[k][l];
  867. float quant_step_size = quant_step_table[abits];
  868. /*
  869. * Determine quantization index code book and its type
  870. */
  871. /* Select quantization index code book */
  872. int sel = s->quant_index_huffman[k][abits];
  873. /*
  874. * Extract bits from the bit stream
  875. */
  876. if (!abits){
  877. memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
  878. } else {
  879. /* Deal with transients */
  880. int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
  881. float rscale = quant_step_size * s->scale_factor[k][l][sfi] * s->scalefactor_adj[k][sel];
  882. if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table){
  883. if (abits <= 7){
  884. /* Block code */
  885. int block_code1, block_code2, size, levels;
  886. size = abits_sizes[abits-1];
  887. levels = abits_levels[abits-1];
  888. block_code1 = get_bits(&s->gb, size);
  889. /* FIXME Should test return value */
  890. decode_blockcode(block_code1, levels, block);
  891. block_code2 = get_bits(&s->gb, size);
  892. decode_blockcode(block_code2, levels, &block[4]);
  893. }else{
  894. /* no coding */
  895. for (m = 0; m < 8; m++)
  896. block[m] = get_sbits(&s->gb, abits - 3);
  897. }
  898. }else{
  899. /* Huffman coded */
  900. for (m = 0; m < 8; m++)
  901. block[m] = get_bitalloc(&s->gb, &dca_smpl_bitalloc[abits], sel);
  902. }
  903. s->dsp.int32_to_float_fmul_scalar(subband_samples[k][l],
  904. block, rscale, 8);
  905. }
  906. /*
  907. * Inverse ADPCM if in prediction mode
  908. */
  909. if (s->prediction_mode[k][l]) {
  910. int n;
  911. for (m = 0; m < 8; m++) {
  912. for (n = 1; n <= 4; n++)
  913. if (m >= n)
  914. subband_samples[k][l][m] +=
  915. (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
  916. subband_samples[k][l][m - n] / 8192);
  917. else if (s->predictor_history)
  918. subband_samples[k][l][m] +=
  919. (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
  920. s->subband_samples_hist[k][l][m - n +
  921. 4] / 8192);
  922. }
  923. }
  924. }
  925. /*
  926. * Decode VQ encoded high frequencies
  927. */
  928. for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
  929. /* 1 vector -> 32 samples but we only need the 8 samples
  930. * for this subsubframe. */
  931. int m;
  932. if (!s->debug_flag & 0x01) {
  933. av_log(s->avctx, AV_LOG_DEBUG, "Stream with high frequencies VQ coding\n");
  934. s->debug_flag |= 0x01;
  935. }
  936. for (m = 0; m < 8; m++) {
  937. subband_samples[k][l][m] =
  938. high_freq_vq[s->high_freq_vq[k][l]][subsubframe * 8 +
  939. m]
  940. * (float) s->scale_factor[k][l][0] / 16.0;
  941. }
  942. }
  943. }
  944. /* Check for DSYNC after subsubframe */
  945. if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
  946. if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
  947. #ifdef TRACE
  948. av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
  949. #endif
  950. } else {
  951. av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
  952. }
  953. }
  954. /* Backup predictor history for adpcm */
  955. for (k = base_channel; k < s->prim_channels; k++)
  956. for (l = 0; l < s->vq_start_subband[k]; l++)
  957. memcpy(s->subband_samples_hist[k][l], &subband_samples[k][l][4],
  958. 4 * sizeof(subband_samples[0][0][0]));
  959. return 0;
  960. }
  961. static int dca_filter_channels(DCAContext * s, int block_index)
  962. {
  963. float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
  964. int k;
  965. /* 32 subbands QMF */
  966. for (k = 0; k < s->prim_channels; k++) {
  967. /* static float pcm_to_double[8] =
  968. {32768.0, 32768.0, 524288.0, 524288.0, 0, 8388608.0, 8388608.0};*/
  969. qmf_32_subbands(s, k, subband_samples[k], &s->samples[256 * s->channel_order_tab[k]],
  970. M_SQRT1_2*s->scale_bias /*pcm_to_double[s->source_pcm_res] */ ,
  971. s->add_bias );
  972. }
  973. /* Down mixing */
  974. if (s->avctx->request_channels == 2 && s->prim_channels > 2) {
  975. dca_downmix(s->samples, s->amode, s->downmix_coef);
  976. }
  977. /* Generate LFE samples for this subsubframe FIXME!!! */
  978. if (s->output & DCA_LFE) {
  979. lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
  980. s->lfe_data + 2 * s->lfe * (block_index + 4),
  981. &s->samples[256 * dca_lfe_index[s->amode]],
  982. (1.0/256.0)*s->scale_bias, s->add_bias);
  983. /* Outputs 20bits pcm samples */
  984. }
  985. return 0;
  986. }
  987. static int dca_subframe_footer(DCAContext * s, int base_channel)
  988. {
  989. int aux_data_count = 0, i;
  990. /*
  991. * Unpack optional information
  992. */
  993. /* presumably optional information only appears in the core? */
  994. if (!base_channel) {
  995. if (s->timestamp)
  996. get_bits(&s->gb, 32);
  997. if (s->aux_data)
  998. aux_data_count = get_bits(&s->gb, 6);
  999. for (i = 0; i < aux_data_count; i++)
  1000. get_bits(&s->gb, 8);
  1001. if (s->crc_present && (s->downmix || s->dynrange))
  1002. get_bits(&s->gb, 16);
  1003. }
  1004. return 0;
  1005. }
  1006. /**
  1007. * Decode a dca frame block
  1008. *
  1009. * @param s pointer to the DCAContext
  1010. */
  1011. static int dca_decode_block(DCAContext * s, int base_channel, int block_index)
  1012. {
  1013. /* Sanity check */
  1014. if (s->current_subframe >= s->subframes) {
  1015. av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
  1016. s->current_subframe, s->subframes);
  1017. return -1;
  1018. }
  1019. if (!s->current_subsubframe) {
  1020. #ifdef TRACE
  1021. av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
  1022. #endif
  1023. /* Read subframe header */
  1024. if (dca_subframe_header(s, base_channel, block_index))
  1025. return -1;
  1026. }
  1027. /* Read subsubframe */
  1028. #ifdef TRACE
  1029. av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
  1030. #endif
  1031. if (dca_subsubframe(s, base_channel, block_index))
  1032. return -1;
  1033. /* Update state */
  1034. s->current_subsubframe++;
  1035. if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
  1036. s->current_subsubframe = 0;
  1037. s->current_subframe++;
  1038. }
  1039. if (s->current_subframe >= s->subframes) {
  1040. #ifdef TRACE
  1041. av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
  1042. #endif
  1043. /* Read subframe footer */
  1044. if (dca_subframe_footer(s, base_channel))
  1045. return -1;
  1046. }
  1047. return 0;
  1048. }
  1049. /**
  1050. * Convert bitstream to one representation based on sync marker
  1051. */
  1052. static int dca_convert_bitstream(const uint8_t * src, int src_size, uint8_t * dst,
  1053. int max_size)
  1054. {
  1055. uint32_t mrk;
  1056. int i, tmp;
  1057. const uint16_t *ssrc = (const uint16_t *) src;
  1058. uint16_t *sdst = (uint16_t *) dst;
  1059. PutBitContext pb;
  1060. if ((unsigned)src_size > (unsigned)max_size) {
  1061. // av_log(NULL, AV_LOG_ERROR, "Input frame size larger then DCA_MAX_FRAME_SIZE!\n");
  1062. // return -1;
  1063. src_size = max_size;
  1064. }
  1065. mrk = AV_RB32(src);
  1066. switch (mrk) {
  1067. case DCA_MARKER_RAW_BE:
  1068. memcpy(dst, src, src_size);
  1069. return src_size;
  1070. case DCA_MARKER_RAW_LE:
  1071. for (i = 0; i < (src_size + 1) >> 1; i++)
  1072. *sdst++ = bswap_16(*ssrc++);
  1073. return src_size;
  1074. case DCA_MARKER_14B_BE:
  1075. case DCA_MARKER_14B_LE:
  1076. init_put_bits(&pb, dst, max_size);
  1077. for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
  1078. tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
  1079. put_bits(&pb, 14, tmp);
  1080. }
  1081. flush_put_bits(&pb);
  1082. return (put_bits_count(&pb) + 7) >> 3;
  1083. default:
  1084. return -1;
  1085. }
  1086. }
  1087. /**
  1088. * Main frame decoding function
  1089. * FIXME add arguments
  1090. */
  1091. static int dca_decode_frame(AVCodecContext * avctx,
  1092. void *data, int *data_size,
  1093. AVPacket *avpkt)
  1094. {
  1095. const uint8_t *buf = avpkt->data;
  1096. int buf_size = avpkt->size;
  1097. int lfe_samples;
  1098. int num_core_channels = 0;
  1099. int i;
  1100. int xch_present = 0;
  1101. int16_t *samples = data;
  1102. DCAContext *s = avctx->priv_data;
  1103. int channels;
  1104. s->dca_buffer_size = dca_convert_bitstream(buf, buf_size, s->dca_buffer, DCA_MAX_FRAME_SIZE);
  1105. if (s->dca_buffer_size == -1) {
  1106. av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
  1107. return -1;
  1108. }
  1109. init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
  1110. if (dca_parse_frame_header(s) < 0) {
  1111. //seems like the frame is corrupt, try with the next one
  1112. *data_size=0;
  1113. return buf_size;
  1114. }
  1115. //set AVCodec values with parsed data
  1116. avctx->sample_rate = s->sample_rate;
  1117. avctx->bit_rate = s->bit_rate;
  1118. for (i = 0; i < (s->sample_blocks / 8); i++) {
  1119. dca_decode_block(s, 0, i);
  1120. }
  1121. /* record number of core channels incase less than max channels are requested */
  1122. num_core_channels = s->prim_channels;
  1123. /* extensions start at 32-bit boundaries into bitstream */
  1124. skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 31);
  1125. while(get_bits_left(&s->gb) >= 32) {
  1126. uint32_t bits = get_bits(&s->gb, 32);
  1127. switch(bits) {
  1128. case 0x5a5a5a5a: {
  1129. int ext_base_ch = s->prim_channels;
  1130. int ext_amode;
  1131. /* skip length-to-end-of-frame field for the moment */
  1132. skip_bits(&s->gb, 10);
  1133. /* extension amode should == 1, number of channels in extension */
  1134. /* AFAIK XCh is not used for more channels */
  1135. if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
  1136. av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
  1137. " supported!\n",ext_amode);
  1138. continue;
  1139. }
  1140. /* much like core primary audio coding header */
  1141. dca_parse_audio_coding_header(s, ext_base_ch);
  1142. for (i = 0; i < (s->sample_blocks / 8); i++) {
  1143. dca_decode_block(s, ext_base_ch, i);
  1144. }
  1145. xch_present = 1;
  1146. break;
  1147. }
  1148. case 0x1d95f262:
  1149. av_log(avctx, AV_LOG_DEBUG, "Possible X96 extension found at %d bits\n", get_bits_count(&s->gb));
  1150. av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", get_bits(&s->gb, 12)+1);
  1151. av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
  1152. break;
  1153. }
  1154. skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 31);
  1155. }
  1156. channels = s->prim_channels + !!s->lfe;
  1157. if (s->amode<16) {
  1158. avctx->channel_layout = dca_core_channel_layout[s->amode];
  1159. if (xch_present && (!avctx->request_channels ||
  1160. avctx->request_channels > num_core_channels)) {
  1161. avctx->channel_layout |= CH_BACK_CENTER;
  1162. if (s->lfe) {
  1163. avctx->channel_layout |= CH_LOW_FREQUENCY;
  1164. s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
  1165. } else {
  1166. s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
  1167. }
  1168. } else {
  1169. if (s->lfe) {
  1170. avctx->channel_layout |= CH_LOW_FREQUENCY;
  1171. s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
  1172. } else
  1173. s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
  1174. }
  1175. if (s->prim_channels > 0 &&
  1176. s->channel_order_tab[s->prim_channels - 1] < 0)
  1177. return -1;
  1178. if (avctx->request_channels == 2 && s->prim_channels > 2) {
  1179. channels = 2;
  1180. s->output = DCA_STEREO;
  1181. avctx->channel_layout = CH_LAYOUT_STEREO;
  1182. }
  1183. } else {
  1184. av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n",s->amode);
  1185. return -1;
  1186. }
  1187. /* There is nothing that prevents a dts frame to change channel configuration
  1188. but FFmpeg doesn't support that so only set the channels if it is previously
  1189. unset. Ideally during the first probe for channels the crc should be checked
  1190. and only set avctx->channels when the crc is ok. Right now the decoder could
  1191. set the channels based on a broken first frame.*/
  1192. if (!avctx->channels)
  1193. avctx->channels = channels;
  1194. if (*data_size < (s->sample_blocks / 8) * 256 * sizeof(int16_t) * channels)
  1195. return -1;
  1196. *data_size = 256 / 8 * s->sample_blocks * sizeof(int16_t) * channels;
  1197. /* filter to get final output */
  1198. for (i = 0; i < (s->sample_blocks / 8); i++) {
  1199. dca_filter_channels(s, i);
  1200. s->dsp.float_to_int16_interleave(samples, s->samples_chanptr, 256, channels);
  1201. samples += 256 * channels;
  1202. }
  1203. /* update lfe history */
  1204. lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
  1205. for (i = 0; i < 2 * s->lfe * 4; i++) {
  1206. s->lfe_data[i] = s->lfe_data[i + lfe_samples];
  1207. }
  1208. return buf_size;
  1209. }
  1210. /**
  1211. * DCA initialization
  1212. *
  1213. * @param avctx pointer to the AVCodecContext
  1214. */
  1215. static av_cold int dca_decode_init(AVCodecContext * avctx)
  1216. {
  1217. DCAContext *s = avctx->priv_data;
  1218. int i;
  1219. s->avctx = avctx;
  1220. dca_init_vlcs();
  1221. dsputil_init(&s->dsp, avctx);
  1222. ff_mdct_init(&s->imdct, 6, 1, 1.0);
  1223. ff_synth_filter_init(&s->synth);
  1224. ff_dcadsp_init(&s->dcadsp);
  1225. for (i = 0; i < DCA_PRIM_CHANNELS_MAX+1; i++)
  1226. s->samples_chanptr[i] = s->samples + i * 256;
  1227. avctx->sample_fmt = SAMPLE_FMT_S16;
  1228. if (s->dsp.float_to_int16_interleave == ff_float_to_int16_interleave_c) {
  1229. s->add_bias = 385.0f;
  1230. s->scale_bias = 1.0 / 32768.0;
  1231. } else {
  1232. s->add_bias = 0.0f;
  1233. s->scale_bias = 1.0;
  1234. /* allow downmixing to stereo */
  1235. if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
  1236. avctx->request_channels == 2) {
  1237. avctx->channels = avctx->request_channels;
  1238. }
  1239. }
  1240. return 0;
  1241. }
  1242. static av_cold int dca_decode_end(AVCodecContext * avctx)
  1243. {
  1244. DCAContext *s = avctx->priv_data;
  1245. ff_mdct_end(&s->imdct);
  1246. return 0;
  1247. }
  1248. AVCodec dca_decoder = {
  1249. .name = "dca",
  1250. .type = AVMEDIA_TYPE_AUDIO,
  1251. .id = CODEC_ID_DTS,
  1252. .priv_data_size = sizeof(DCAContext),
  1253. .init = dca_decode_init,
  1254. .decode = dca_decode_frame,
  1255. .close = dca_decode_end,
  1256. .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
  1257. };