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.

1283 lines
46KB

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