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.

1362 lines
49KB

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