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.

1250 lines
43KB

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