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.

1341 lines
48KB

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