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.

1357 lines
46KB

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