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.

1261 lines
44KB

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