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.

1255 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 = 2;
  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. int source_channel = s->joint_intensity[j] - 1;
  533. av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
  534. for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
  535. av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
  536. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  537. }
  538. }
  539. if (s->prim_channels > 2 && s->downmix) {
  540. av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
  541. for (j = 0; j < s->prim_channels; j++) {
  542. av_log(s->avctx, AV_LOG_DEBUG, "Channel 0,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][0]]);
  543. av_log(s->avctx, AV_LOG_DEBUG, "Channel 1,%d = %f\n", j, dca_downmix_coeffs[s->downmix_coef[j][1]]);
  544. }
  545. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  546. }
  547. for (j = 0; j < s->prim_channels; j++)
  548. for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
  549. av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
  550. if(s->lfe){
  551. int lfe_samples = 2 * s->lfe * s->subsubframes;
  552. av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
  553. for (j = lfe_samples; j < lfe_samples * 2; j++)
  554. av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
  555. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  556. }
  557. #endif
  558. return 0;
  559. }
  560. static void qmf_32_subbands(DCAContext * s, int chans,
  561. float samples_in[32][8], float *samples_out,
  562. float scale, float bias)
  563. {
  564. float *prCoeff;
  565. int i, j, k;
  566. float praXin[33], *raXin = &praXin[1];
  567. float *subband_fir_hist = s->subband_fir_hist[chans];
  568. float *subband_fir_hist2 = s->subband_fir_noidea[chans];
  569. int chindex = 0, subindex;
  570. praXin[0] = 0.0;
  571. /* Select filter */
  572. if (!s->multirate_inter) /* Non-perfect reconstruction */
  573. prCoeff = (float *) fir_32bands_nonperfect;
  574. else /* Perfect reconstruction */
  575. prCoeff = (float *) fir_32bands_perfect;
  576. /* Reconstructed channel sample index */
  577. for (subindex = 0; subindex < 8; subindex++) {
  578. float t1, t2, sum[16], diff[16];
  579. /* Load in one sample from each subband and clear inactive subbands */
  580. for (i = 0; i < s->subband_activity[chans]; i++)
  581. raXin[i] = samples_in[i][subindex];
  582. for (; i < 32; i++)
  583. raXin[i] = 0.0;
  584. /* Multiply by cosine modulation coefficients and
  585. * create temporary arrays SUM and DIFF */
  586. for (j = 0, k = 0; k < 16; k++) {
  587. t1 = 0.0;
  588. t2 = 0.0;
  589. for (i = 0; i < 16; i++, j++){
  590. t1 += (raXin[2 * i] + raXin[2 * i + 1]) * cos_mod[j];
  591. t2 += (raXin[2 * i] + raXin[2 * i - 1]) * cos_mod[j + 256];
  592. }
  593. sum[k] = t1 + t2;
  594. diff[k] = t1 - t2;
  595. }
  596. j = 512;
  597. /* Store history */
  598. for (k = 0; k < 16; k++)
  599. subband_fir_hist[k] = cos_mod[j++] * sum[k];
  600. for (k = 0; k < 16; k++)
  601. subband_fir_hist[32-k-1] = cos_mod[j++] * diff[k];
  602. /* Multiply by filter coefficients */
  603. for (k = 31, i = 0; i < 32; i++, k--)
  604. for (j = 0; j < 512; j += 64){
  605. subband_fir_hist2[i] += prCoeff[i+j] * ( subband_fir_hist[i+j] - subband_fir_hist[j+k]);
  606. subband_fir_hist2[i+32] += prCoeff[i+j+32]*(-subband_fir_hist[i+j] - subband_fir_hist[j+k]);
  607. }
  608. /* Create 32 PCM output samples */
  609. for (i = 0; i < 32; i++)
  610. samples_out[chindex++] = subband_fir_hist2[i] * scale + bias;
  611. /* Update working arrays */
  612. memmove(&subband_fir_hist[32], &subband_fir_hist[0], (512 - 32) * sizeof(float));
  613. memmove(&subband_fir_hist2[0], &subband_fir_hist2[32], 32 * sizeof(float));
  614. memset(&subband_fir_hist2[32], 0, 32 * sizeof(float));
  615. }
  616. }
  617. static void lfe_interpolation_fir(int decimation_select,
  618. int num_deci_sample, float *samples_in,
  619. float *samples_out, float scale,
  620. float bias)
  621. {
  622. /* samples_in: An array holding decimated samples.
  623. * Samples in current subframe starts from samples_in[0],
  624. * while samples_in[-1], samples_in[-2], ..., stores samples
  625. * from last subframe as history.
  626. *
  627. * samples_out: An array holding interpolated samples
  628. */
  629. int decifactor, k, j;
  630. const float *prCoeff;
  631. int interp_index = 0; /* Index to the interpolated samples */
  632. int deciindex;
  633. /* Select decimation filter */
  634. if (decimation_select == 1) {
  635. decifactor = 128;
  636. prCoeff = lfe_fir_128;
  637. } else {
  638. decifactor = 64;
  639. prCoeff = lfe_fir_64;
  640. }
  641. /* Interpolation */
  642. for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
  643. /* One decimated sample generates decifactor interpolated ones */
  644. for (k = 0; k < decifactor; k++) {
  645. float rTmp = 0.0;
  646. //FIXME the coeffs are symetric, fix that
  647. for (j = 0; j < 512 / decifactor; j++)
  648. rTmp += samples_in[deciindex - j] * prCoeff[k + j * decifactor];
  649. samples_out[interp_index++] = rTmp / scale + bias;
  650. }
  651. }
  652. }
  653. /* downmixing routines */
  654. #define MIX_REAR1(samples, si1, rs, coef) \
  655. samples[i] += samples[si1] * coef[rs][0]; \
  656. samples[i+256] += samples[si1] * coef[rs][1];
  657. #define MIX_REAR2(samples, si1, si2, rs, coef) \
  658. samples[i] += samples[si1] * coef[rs][0] + samples[si2] * coef[rs+1][0]; \
  659. samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs+1][1];
  660. #define MIX_FRONT3(samples, coef) \
  661. t = samples[i]; \
  662. samples[i] = t * coef[0][0] + samples[i+256] * coef[1][0] + samples[i+512] * coef[2][0]; \
  663. samples[i+256] = t * coef[0][1] + samples[i+256] * coef[1][1] + samples[i+512] * coef[2][1];
  664. #define DOWNMIX_TO_STEREO(op1, op2) \
  665. for(i = 0; i < 256; i++){ \
  666. op1 \
  667. op2 \
  668. }
  669. static void dca_downmix(float *samples, int srcfmt,
  670. int downmix_coef[DCA_PRIM_CHANNELS_MAX][2])
  671. {
  672. int i;
  673. float t;
  674. float coef[DCA_PRIM_CHANNELS_MAX][2];
  675. for(i=0; i<DCA_PRIM_CHANNELS_MAX; i++) {
  676. coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
  677. coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
  678. }
  679. switch (srcfmt) {
  680. case DCA_MONO:
  681. case DCA_CHANNEL:
  682. case DCA_STEREO_TOTAL:
  683. case DCA_STEREO_SUMDIFF:
  684. case DCA_4F2R:
  685. av_log(NULL, 0, "Not implemented!\n");
  686. break;
  687. case DCA_STEREO:
  688. break;
  689. case DCA_3F:
  690. DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),);
  691. break;
  692. case DCA_2F1R:
  693. DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + 512, 2, coef),);
  694. break;
  695. case DCA_3F1R:
  696. DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
  697. MIX_REAR1(samples, i + 768, 3, coef));
  698. break;
  699. case DCA_2F2R:
  700. DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + 512, i + 768, 2, coef),);
  701. break;
  702. case DCA_3F2R:
  703. DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
  704. MIX_REAR2(samples, i + 768, i + 1024, 3, coef));
  705. break;
  706. }
  707. }
  708. /* Very compact version of the block code decoder that does not use table
  709. * look-up but is slightly slower */
  710. static int decode_blockcode(int code, int levels, int *values)
  711. {
  712. int i;
  713. int offset = (levels - 1) >> 1;
  714. for (i = 0; i < 4; i++) {
  715. values[i] = (code % levels) - offset;
  716. code /= levels;
  717. }
  718. if (code == 0)
  719. return 0;
  720. else {
  721. av_log(NULL, AV_LOG_ERROR, "ERROR: block code look-up failed\n");
  722. return -1;
  723. }
  724. }
  725. static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
  726. static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
  727. static int dca_subsubframe(DCAContext * s)
  728. {
  729. int k, l;
  730. int subsubframe = s->current_subsubframe;
  731. float *quant_step_table;
  732. /* FIXME */
  733. float subband_samples[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
  734. /*
  735. * Audio data
  736. */
  737. /* Select quantization step size table */
  738. if (s->bit_rate == 0x1f)
  739. quant_step_table = (float *) lossless_quant_d;
  740. else
  741. quant_step_table = (float *) lossy_quant_d;
  742. for (k = 0; k < s->prim_channels; k++) {
  743. for (l = 0; l < s->vq_start_subband[k]; l++) {
  744. int m;
  745. /* Select the mid-tread linear quantizer */
  746. int abits = s->bitalloc[k][l];
  747. float quant_step_size = quant_step_table[abits];
  748. float rscale;
  749. /*
  750. * Determine quantization index code book and its type
  751. */
  752. /* Select quantization index code book */
  753. int sel = s->quant_index_huffman[k][abits];
  754. /*
  755. * Extract bits from the bit stream
  756. */
  757. if(!abits){
  758. memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
  759. }else if(abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table){
  760. if(abits <= 7){
  761. /* Block code */
  762. int block_code1, block_code2, size, levels;
  763. int block[8];
  764. size = abits_sizes[abits-1];
  765. levels = abits_levels[abits-1];
  766. block_code1 = get_bits(&s->gb, size);
  767. /* FIXME Should test return value */
  768. decode_blockcode(block_code1, levels, block);
  769. block_code2 = get_bits(&s->gb, size);
  770. decode_blockcode(block_code2, levels, &block[4]);
  771. for (m = 0; m < 8; m++)
  772. subband_samples[k][l][m] = block[m];
  773. }else{
  774. /* no coding */
  775. for (m = 0; m < 8; m++)
  776. subband_samples[k][l][m] = get_sbits(&s->gb, abits - 3);
  777. }
  778. }else{
  779. /* Huffman coded */
  780. for (m = 0; m < 8; m++)
  781. subband_samples[k][l][m] = get_bitalloc(&s->gb, &dca_smpl_bitalloc[abits], sel);
  782. }
  783. /* Deal with transients */
  784. if (s->transition_mode[k][l] &&
  785. subsubframe >= s->transition_mode[k][l])
  786. rscale = quant_step_size * s->scale_factor[k][l][1];
  787. else
  788. rscale = quant_step_size * s->scale_factor[k][l][0];
  789. rscale *= s->scalefactor_adj[k][sel];
  790. for (m = 0; m < 8; m++)
  791. subband_samples[k][l][m] *= rscale;
  792. /*
  793. * Inverse ADPCM if in prediction mode
  794. */
  795. if (s->prediction_mode[k][l]) {
  796. int n;
  797. for (m = 0; m < 8; m++) {
  798. for (n = 1; n <= 4; n++)
  799. if (m >= n)
  800. subband_samples[k][l][m] +=
  801. (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
  802. subband_samples[k][l][m - n] / 8192);
  803. else if (s->predictor_history)
  804. subband_samples[k][l][m] +=
  805. (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
  806. s->subband_samples_hist[k][l][m - n +
  807. 4] / 8192);
  808. }
  809. }
  810. }
  811. /*
  812. * Decode VQ encoded high frequencies
  813. */
  814. for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
  815. /* 1 vector -> 32 samples but we only need the 8 samples
  816. * for this subsubframe. */
  817. int m;
  818. if (!s->debug_flag & 0x01) {
  819. av_log(s->avctx, AV_LOG_DEBUG, "Stream with high frequencies VQ coding\n");
  820. s->debug_flag |= 0x01;
  821. }
  822. for (m = 0; m < 8; m++) {
  823. subband_samples[k][l][m] =
  824. high_freq_vq[s->high_freq_vq[k][l]][subsubframe * 8 +
  825. m]
  826. * (float) s->scale_factor[k][l][0] / 16.0;
  827. }
  828. }
  829. }
  830. /* Check for DSYNC after subsubframe */
  831. if (s->aspf || subsubframe == s->subsubframes - 1) {
  832. if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
  833. #ifdef TRACE
  834. av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
  835. #endif
  836. } else {
  837. av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
  838. }
  839. }
  840. /* Backup predictor history for adpcm */
  841. for (k = 0; k < s->prim_channels; k++)
  842. for (l = 0; l < s->vq_start_subband[k]; l++)
  843. memcpy(s->subband_samples_hist[k][l], &subband_samples[k][l][4],
  844. 4 * sizeof(subband_samples[0][0][0]));
  845. /* 32 subbands QMF */
  846. for (k = 0; k < s->prim_channels; k++) {
  847. /* static float pcm_to_double[8] =
  848. {32768.0, 32768.0, 524288.0, 524288.0, 0, 8388608.0, 8388608.0};*/
  849. qmf_32_subbands(s, k, subband_samples[k], &s->samples[256 * k],
  850. 2.0 / 3 /*pcm_to_double[s->source_pcm_res] */ ,
  851. 0 /*s->bias */ );
  852. }
  853. /* Down mixing */
  854. if (s->prim_channels > dca_channels[s->output & DCA_CHANNEL_MASK]) {
  855. dca_downmix(s->samples, s->amode, s->downmix_coef);
  856. }
  857. /* Generate LFE samples for this subsubframe FIXME!!! */
  858. if (s->output & DCA_LFE) {
  859. int lfe_samples = 2 * s->lfe * s->subsubframes;
  860. int i_channels = dca_channels[s->output & DCA_CHANNEL_MASK];
  861. lfe_interpolation_fir(s->lfe, 2 * s->lfe,
  862. s->lfe_data + lfe_samples +
  863. 2 * s->lfe * subsubframe,
  864. &s->samples[256 * i_channels],
  865. 8388608.0, s->bias);
  866. /* Outputs 20bits pcm samples */
  867. }
  868. return 0;
  869. }
  870. static int dca_subframe_footer(DCAContext * s)
  871. {
  872. int aux_data_count = 0, i;
  873. int lfe_samples;
  874. /*
  875. * Unpack optional information
  876. */
  877. if (s->timestamp)
  878. get_bits(&s->gb, 32);
  879. if (s->aux_data)
  880. aux_data_count = get_bits(&s->gb, 6);
  881. for (i = 0; i < aux_data_count; i++)
  882. get_bits(&s->gb, 8);
  883. if (s->crc_present && (s->downmix || s->dynrange))
  884. get_bits(&s->gb, 16);
  885. lfe_samples = 2 * s->lfe * s->subsubframes;
  886. for (i = 0; i < lfe_samples; i++) {
  887. s->lfe_data[i] = s->lfe_data[i + lfe_samples];
  888. }
  889. return 0;
  890. }
  891. /**
  892. * Decode a dca frame block
  893. *
  894. * @param s pointer to the DCAContext
  895. */
  896. static int dca_decode_block(DCAContext * s)
  897. {
  898. /* Sanity check */
  899. if (s->current_subframe >= s->subframes) {
  900. av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
  901. s->current_subframe, s->subframes);
  902. return -1;
  903. }
  904. if (!s->current_subsubframe) {
  905. #ifdef TRACE
  906. av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
  907. #endif
  908. /* Read subframe header */
  909. if (dca_subframe_header(s))
  910. return -1;
  911. }
  912. /* Read subsubframe */
  913. #ifdef TRACE
  914. av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
  915. #endif
  916. if (dca_subsubframe(s))
  917. return -1;
  918. /* Update state */
  919. s->current_subsubframe++;
  920. if (s->current_subsubframe >= s->subsubframes) {
  921. s->current_subsubframe = 0;
  922. s->current_subframe++;
  923. }
  924. if (s->current_subframe >= s->subframes) {
  925. #ifdef TRACE
  926. av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
  927. #endif
  928. /* Read subframe footer */
  929. if (dca_subframe_footer(s))
  930. return -1;
  931. }
  932. return 0;
  933. }
  934. /**
  935. * Convert bitstream to one representation based on sync marker
  936. */
  937. static int dca_convert_bitstream(uint8_t * src, int src_size, uint8_t * dst,
  938. int max_size)
  939. {
  940. uint32_t mrk;
  941. int i, tmp;
  942. uint16_t *ssrc = (uint16_t *) src, *sdst = (uint16_t *) dst;
  943. PutBitContext pb;
  944. if((unsigned)src_size > (unsigned)max_size) {
  945. av_log(NULL, AV_LOG_ERROR, "Input frame size larger then DCA_MAX_FRAME_SIZE!\n");
  946. return -1;
  947. }
  948. mrk = AV_RB32(src);
  949. switch (mrk) {
  950. case DCA_MARKER_RAW_BE:
  951. memcpy(dst, src, FFMIN(src_size, max_size));
  952. return FFMIN(src_size, max_size);
  953. case DCA_MARKER_RAW_LE:
  954. for (i = 0; i < (FFMIN(src_size, max_size) + 1) >> 1; i++)
  955. *sdst++ = bswap_16(*ssrc++);
  956. return FFMIN(src_size, max_size);
  957. case DCA_MARKER_14B_BE:
  958. case DCA_MARKER_14B_LE:
  959. init_put_bits(&pb, dst, max_size);
  960. for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
  961. tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
  962. put_bits(&pb, 14, tmp);
  963. }
  964. flush_put_bits(&pb);
  965. return (put_bits_count(&pb) + 7) >> 3;
  966. default:
  967. return -1;
  968. }
  969. }
  970. /**
  971. * Main frame decoding function
  972. * FIXME add arguments
  973. */
  974. static int dca_decode_frame(AVCodecContext * avctx,
  975. void *data, int *data_size,
  976. uint8_t * buf, int buf_size)
  977. {
  978. int i, j, k;
  979. int16_t *samples = data;
  980. DCAContext *s = avctx->priv_data;
  981. int channels;
  982. s->dca_buffer_size = dca_convert_bitstream(buf, buf_size, s->dca_buffer, DCA_MAX_FRAME_SIZE);
  983. if (s->dca_buffer_size == -1) {
  984. av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
  985. return -1;
  986. }
  987. init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
  988. if (dca_parse_frame_header(s) < 0) {
  989. //seems like the frame is corrupt, try with the next one
  990. return buf_size;
  991. }
  992. //set AVCodec values with parsed data
  993. avctx->sample_rate = s->sample_rate;
  994. avctx->bit_rate = s->bit_rate;
  995. channels = s->prim_channels + !!s->lfe;
  996. avctx->channels = avctx->request_channels;
  997. if(avctx->channels == 0) {
  998. avctx->channels = channels;
  999. } else if(channels < avctx->channels) {
  1000. av_log(avctx, AV_LOG_WARNING, "DTS source channels are less than "
  1001. "specified: output to %d channels.\n", channels);
  1002. avctx->channels = channels;
  1003. }
  1004. if(avctx->channels == 2) {
  1005. s->output = DCA_STEREO;
  1006. } else if(avctx->channels != channels) {
  1007. av_log(avctx, AV_LOG_ERROR, "Cannot downmix DTS to %d channels.\n",
  1008. avctx->channels);
  1009. return -1;
  1010. }
  1011. channels = avctx->channels;
  1012. if(*data_size < (s->sample_blocks / 8) * 256 * sizeof(int16_t) * channels)
  1013. return -1;
  1014. *data_size = 0;
  1015. for (i = 0; i < (s->sample_blocks / 8); i++) {
  1016. dca_decode_block(s);
  1017. s->dsp.float_to_int16(s->tsamples, s->samples, 256 * channels);
  1018. /* interleave samples */
  1019. for (j = 0; j < 256; j++) {
  1020. for (k = 0; k < channels; k++)
  1021. samples[k] = s->tsamples[j + k * 256];
  1022. samples += channels;
  1023. }
  1024. *data_size += 256 * sizeof(int16_t) * channels;
  1025. }
  1026. return buf_size;
  1027. }
  1028. /**
  1029. * Build the cosine modulation tables for the QMF
  1030. *
  1031. * @param s pointer to the DCAContext
  1032. */
  1033. static void pre_calc_cosmod(DCAContext * s)
  1034. {
  1035. int i, j, k;
  1036. static int cosmod_inited = 0;
  1037. if(cosmod_inited) return;
  1038. for (j = 0, k = 0; k < 16; k++)
  1039. for (i = 0; i < 16; i++)
  1040. cos_mod[j++] = cos((2 * i + 1) * (2 * k + 1) * M_PI / 64);
  1041. for (k = 0; k < 16; k++)
  1042. for (i = 0; i < 16; i++)
  1043. cos_mod[j++] = cos((i) * (2 * k + 1) * M_PI / 32);
  1044. for (k = 0; k < 16; k++)
  1045. cos_mod[j++] = 0.25 / (2 * cos((2 * k + 1) * M_PI / 128));
  1046. for (k = 0; k < 16; k++)
  1047. cos_mod[j++] = -0.25 / (2.0 * sin((2 * k + 1) * M_PI / 128));
  1048. cosmod_inited = 1;
  1049. }
  1050. /**
  1051. * DCA initialization
  1052. *
  1053. * @param avctx pointer to the AVCodecContext
  1054. */
  1055. static int dca_decode_init(AVCodecContext * avctx)
  1056. {
  1057. DCAContext *s = avctx->priv_data;
  1058. s->avctx = avctx;
  1059. dca_init_vlcs();
  1060. pre_calc_cosmod(s);
  1061. dsputil_init(&s->dsp, avctx);
  1062. return 0;
  1063. }
  1064. AVCodec dca_decoder = {
  1065. .name = "dca",
  1066. .type = CODEC_TYPE_AUDIO,
  1067. .id = CODEC_ID_DTS,
  1068. .priv_data_size = sizeof(DCAContext),
  1069. .init = dca_decode_init,
  1070. .decode = dca_decode_frame,
  1071. };