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.

1336 lines
48KB

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