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.

2068 lines
77KB

  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. * Copyright (C) 2012 Paul B Mahol
  8. * Copyright (C) 2014 Niels Möller
  9. *
  10. * This file is part of FFmpeg.
  11. *
  12. * FFmpeg is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * FFmpeg is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with FFmpeg; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #include <math.h>
  27. #include <stddef.h>
  28. #include <stdio.h>
  29. #include "libavutil/attributes.h"
  30. #include "libavutil/channel_layout.h"
  31. #include "libavutil/common.h"
  32. #include "libavutil/float_dsp.h"
  33. #include "libavutil/internal.h"
  34. #include "libavutil/intreadwrite.h"
  35. #include "libavutil/mathematics.h"
  36. #include "libavutil/opt.h"
  37. #include "libavutil/samplefmt.h"
  38. #include "avcodec.h"
  39. #include "dca.h"
  40. #include "dca_syncwords.h"
  41. #include "dcadata.h"
  42. #include "dcadsp.h"
  43. #include "dcahuff.h"
  44. #include "fft.h"
  45. #include "fmtconvert.h"
  46. #include "get_bits.h"
  47. #include "internal.h"
  48. #include "mathops.h"
  49. #include "profiles.h"
  50. #include "synth_filter.h"
  51. #if ARCH_ARM
  52. # include "arm/dca.h"
  53. #endif
  54. enum DCAMode {
  55. DCA_MONO = 0,
  56. DCA_CHANNEL,
  57. DCA_STEREO,
  58. DCA_STEREO_SUMDIFF,
  59. DCA_STEREO_TOTAL,
  60. DCA_3F,
  61. DCA_2F1R,
  62. DCA_3F1R,
  63. DCA_2F2R,
  64. DCA_3F2R,
  65. DCA_4F2R
  66. };
  67. enum DCAXxchSpeakerMask {
  68. DCA_XXCH_FRONT_CENTER = 0x0000001,
  69. DCA_XXCH_FRONT_LEFT = 0x0000002,
  70. DCA_XXCH_FRONT_RIGHT = 0x0000004,
  71. DCA_XXCH_SIDE_REAR_LEFT = 0x0000008,
  72. DCA_XXCH_SIDE_REAR_RIGHT = 0x0000010,
  73. DCA_XXCH_LFE1 = 0x0000020,
  74. DCA_XXCH_REAR_CENTER = 0x0000040,
  75. DCA_XXCH_SURROUND_REAR_LEFT = 0x0000080,
  76. DCA_XXCH_SURROUND_REAR_RIGHT = 0x0000100,
  77. DCA_XXCH_SIDE_SURROUND_LEFT = 0x0000200,
  78. DCA_XXCH_SIDE_SURROUND_RIGHT = 0x0000400,
  79. DCA_XXCH_FRONT_CENTER_LEFT = 0x0000800,
  80. DCA_XXCH_FRONT_CENTER_RIGHT = 0x0001000,
  81. DCA_XXCH_FRONT_HIGH_LEFT = 0x0002000,
  82. DCA_XXCH_FRONT_HIGH_CENTER = 0x0004000,
  83. DCA_XXCH_FRONT_HIGH_RIGHT = 0x0008000,
  84. DCA_XXCH_LFE2 = 0x0010000,
  85. DCA_XXCH_SIDE_FRONT_LEFT = 0x0020000,
  86. DCA_XXCH_SIDE_FRONT_RIGHT = 0x0040000,
  87. DCA_XXCH_OVERHEAD = 0x0080000,
  88. DCA_XXCH_SIDE_HIGH_LEFT = 0x0100000,
  89. DCA_XXCH_SIDE_HIGH_RIGHT = 0x0200000,
  90. DCA_XXCH_REAR_HIGH_CENTER = 0x0400000,
  91. DCA_XXCH_REAR_HIGH_LEFT = 0x0800000,
  92. DCA_XXCH_REAR_HIGH_RIGHT = 0x1000000,
  93. DCA_XXCH_REAR_LOW_CENTER = 0x2000000,
  94. DCA_XXCH_REAR_LOW_LEFT = 0x4000000,
  95. DCA_XXCH_REAR_LOW_RIGHT = 0x8000000,
  96. };
  97. #define DCA_DOLBY 101 /* FIXME */
  98. #define DCA_CHANNEL_BITS 6
  99. #define DCA_CHANNEL_MASK 0x3F
  100. #define DCA_LFE 0x80
  101. #define HEADER_SIZE 14
  102. #define DCA_NSYNCAUX 0x9A1105A0
  103. /** Bit allocation */
  104. typedef struct BitAlloc {
  105. int offset; ///< code values offset
  106. int maxbits[8]; ///< max bits in VLC
  107. int wrap; ///< wrap for get_vlc2()
  108. VLC vlc[8]; ///< actual codes
  109. } BitAlloc;
  110. static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
  111. static BitAlloc dca_tmode; ///< transition mode VLCs
  112. static BitAlloc dca_scalefactor; ///< scalefactor VLCs
  113. static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
  114. static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba,
  115. int idx)
  116. {
  117. return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
  118. ba->offset;
  119. }
  120. static float dca_dmix_code(unsigned code);
  121. static av_cold void dca_init_vlcs(void)
  122. {
  123. static int vlcs_initialized = 0;
  124. int i, j, c = 14;
  125. static VLC_TYPE dca_table[23622][2];
  126. if (vlcs_initialized)
  127. return;
  128. dca_bitalloc_index.offset = 1;
  129. dca_bitalloc_index.wrap = 2;
  130. for (i = 0; i < 5; i++) {
  131. dca_bitalloc_index.vlc[i].table = &dca_table[ff_dca_vlc_offs[i]];
  132. dca_bitalloc_index.vlc[i].table_allocated = ff_dca_vlc_offs[i + 1] - ff_dca_vlc_offs[i];
  133. init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
  134. bitalloc_12_bits[i], 1, 1,
  135. bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
  136. }
  137. dca_scalefactor.offset = -64;
  138. dca_scalefactor.wrap = 2;
  139. for (i = 0; i < 5; i++) {
  140. dca_scalefactor.vlc[i].table = &dca_table[ff_dca_vlc_offs[i + 5]];
  141. dca_scalefactor.vlc[i].table_allocated = ff_dca_vlc_offs[i + 6] - ff_dca_vlc_offs[i + 5];
  142. init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
  143. scales_bits[i], 1, 1,
  144. scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
  145. }
  146. dca_tmode.offset = 0;
  147. dca_tmode.wrap = 1;
  148. for (i = 0; i < 4; i++) {
  149. dca_tmode.vlc[i].table = &dca_table[ff_dca_vlc_offs[i + 10]];
  150. dca_tmode.vlc[i].table_allocated = ff_dca_vlc_offs[i + 11] - ff_dca_vlc_offs[i + 10];
  151. init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
  152. tmode_bits[i], 1, 1,
  153. tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
  154. }
  155. for (i = 0; i < 10; i++)
  156. for (j = 0; j < 7; j++) {
  157. if (!bitalloc_codes[i][j])
  158. break;
  159. dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
  160. dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
  161. dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[ff_dca_vlc_offs[c]];
  162. dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = ff_dca_vlc_offs[c + 1] - ff_dca_vlc_offs[c];
  163. init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
  164. bitalloc_sizes[i],
  165. bitalloc_bits[i][j], 1, 1,
  166. bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
  167. c++;
  168. }
  169. vlcs_initialized = 1;
  170. }
  171. static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
  172. {
  173. while (len--)
  174. *dst++ = get_bits(gb, bits);
  175. }
  176. static inline int dca_xxch2index(DCAContext *s, int xxch_ch)
  177. {
  178. int i, base, mask;
  179. /* locate channel set containing the channel */
  180. for (i = -1, base = 0, mask = (s->xxch_core_spkmask & ~DCA_XXCH_LFE1);
  181. i <= s->xxch_chset && !(mask & xxch_ch); mask = s->xxch_spk_masks[++i])
  182. base += av_popcount(mask);
  183. return base + av_popcount(mask & (xxch_ch - 1));
  184. }
  185. static int dca_parse_audio_coding_header(DCAContext *s, int base_channel,
  186. int xxch)
  187. {
  188. int i, j;
  189. static const uint8_t adj_table[4] = { 16, 18, 20, 23 };
  190. static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
  191. static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
  192. int hdr_pos = 0, hdr_size = 0;
  193. float scale_factor;
  194. int this_chans, acc_mask;
  195. int embedded_downmix;
  196. int nchans, mask[8];
  197. int coeff, ichan;
  198. /* xxch has arbitrary sized audio coding headers */
  199. if (xxch) {
  200. hdr_pos = get_bits_count(&s->gb);
  201. hdr_size = get_bits(&s->gb, 7) + 1;
  202. }
  203. nchans = get_bits(&s->gb, 3) + 1;
  204. if (xxch && nchans >= 3) {
  205. av_log(s->avctx, AV_LOG_ERROR, "nchans %d is too large\n", nchans);
  206. return AVERROR_INVALIDDATA;
  207. } else if (nchans + base_channel > DCA_PRIM_CHANNELS_MAX) {
  208. av_log(s->avctx, AV_LOG_ERROR, "channel sum %d + %d is too large\n", nchans, base_channel);
  209. return AVERROR_INVALIDDATA;
  210. }
  211. s->audio_header.total_channels = nchans + base_channel;
  212. s->audio_header.prim_channels = s->audio_header.total_channels;
  213. /* obtain speaker layout mask & downmix coefficients for XXCH */
  214. if (xxch) {
  215. acc_mask = s->xxch_core_spkmask;
  216. this_chans = get_bits(&s->gb, s->xxch_nbits_spk_mask - 6) << 6;
  217. s->xxch_spk_masks[s->xxch_chset] = this_chans;
  218. s->xxch_chset_nch[s->xxch_chset] = nchans;
  219. for (i = 0; i <= s->xxch_chset; i++)
  220. acc_mask |= s->xxch_spk_masks[i];
  221. /* check for downmixing information */
  222. if (get_bits1(&s->gb)) {
  223. embedded_downmix = get_bits1(&s->gb);
  224. coeff = get_bits(&s->gb, 6);
  225. if (coeff<1 || coeff>61) {
  226. av_log(s->avctx, AV_LOG_ERROR, "6bit coeff %d is out of range\n", coeff);
  227. return AVERROR_INVALIDDATA;
  228. }
  229. scale_factor = -1.0f / dca_dmix_code((coeff<<2)-3);
  230. s->xxch_dmix_sf[s->xxch_chset] = scale_factor;
  231. for (i = base_channel; i < s->audio_header.prim_channels; i++) {
  232. mask[i] = get_bits(&s->gb, s->xxch_nbits_spk_mask);
  233. }
  234. for (j = base_channel; j < s->audio_header.prim_channels; j++) {
  235. memset(s->xxch_dmix_coeff[j], 0, sizeof(s->xxch_dmix_coeff[0]));
  236. s->xxch_dmix_embedded |= (embedded_downmix << j);
  237. for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
  238. if (mask[j] & (1 << i)) {
  239. if ((1 << i) == DCA_XXCH_LFE1) {
  240. av_log(s->avctx, AV_LOG_WARNING,
  241. "DCA-XXCH: dmix to LFE1 not supported.\n");
  242. continue;
  243. }
  244. coeff = get_bits(&s->gb, 7);
  245. ichan = dca_xxch2index(s, 1 << i);
  246. if ((coeff&63)<1 || (coeff&63)>61) {
  247. av_log(s->avctx, AV_LOG_ERROR, "7bit coeff %d is out of range\n", coeff);
  248. return AVERROR_INVALIDDATA;
  249. }
  250. s->xxch_dmix_coeff[j][ichan] = dca_dmix_code((coeff<<2)-3);
  251. }
  252. }
  253. }
  254. }
  255. }
  256. if (s->audio_header.prim_channels > DCA_PRIM_CHANNELS_MAX)
  257. s->audio_header.prim_channels = DCA_PRIM_CHANNELS_MAX;
  258. for (i = base_channel; i < s->audio_header.prim_channels; i++) {
  259. s->audio_header.subband_activity[i] = get_bits(&s->gb, 5) + 2;
  260. if (s->audio_header.subband_activity[i] > DCA_SUBBANDS)
  261. s->audio_header.subband_activity[i] = DCA_SUBBANDS;
  262. }
  263. for (i = base_channel; i < s->audio_header.prim_channels; i++) {
  264. s->audio_header.vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
  265. if (s->audio_header.vq_start_subband[i] > DCA_SUBBANDS)
  266. s->audio_header.vq_start_subband[i] = DCA_SUBBANDS;
  267. }
  268. get_array(&s->gb, s->audio_header.joint_intensity + base_channel,
  269. s->audio_header.prim_channels - base_channel, 3);
  270. get_array(&s->gb, s->audio_header.transient_huffman + base_channel,
  271. s->audio_header.prim_channels - base_channel, 2);
  272. get_array(&s->gb, s->audio_header.scalefactor_huffman + base_channel,
  273. s->audio_header.prim_channels - base_channel, 3);
  274. get_array(&s->gb, s->audio_header.bitalloc_huffman + base_channel,
  275. s->audio_header.prim_channels - base_channel, 3);
  276. /* Get codebooks quantization indexes */
  277. if (!base_channel)
  278. memset(s->audio_header.quant_index_huffman, 0, sizeof(s->audio_header.quant_index_huffman));
  279. for (j = 1; j < 11; j++)
  280. for (i = base_channel; i < s->audio_header.prim_channels; i++)
  281. s->audio_header.quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
  282. /* Get scale factor adjustment */
  283. for (j = 0; j < 11; j++)
  284. for (i = base_channel; i < s->audio_header.prim_channels; i++)
  285. s->audio_header.scalefactor_adj[i][j] = 16;
  286. for (j = 1; j < 11; j++)
  287. for (i = base_channel; i < s->audio_header.prim_channels; i++)
  288. if (s->audio_header.quant_index_huffman[i][j] < thr[j])
  289. s->audio_header.scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
  290. if (!xxch) {
  291. if (s->crc_present) {
  292. /* Audio header CRC check */
  293. get_bits(&s->gb, 16);
  294. }
  295. } else {
  296. /* Skip to the end of the header, also ignore CRC if present */
  297. i = get_bits_count(&s->gb);
  298. if (hdr_pos + 8 * hdr_size > i)
  299. skip_bits_long(&s->gb, hdr_pos + 8 * hdr_size - i);
  300. }
  301. s->current_subframe = 0;
  302. s->current_subsubframe = 0;
  303. return 0;
  304. }
  305. static int dca_parse_frame_header(DCAContext *s)
  306. {
  307. init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
  308. /* Sync code */
  309. skip_bits_long(&s->gb, 32);
  310. /* Frame header */
  311. s->frame_type = get_bits(&s->gb, 1);
  312. s->samples_deficit = get_bits(&s->gb, 5) + 1;
  313. s->crc_present = get_bits(&s->gb, 1);
  314. s->sample_blocks = get_bits(&s->gb, 7) + 1;
  315. s->frame_size = get_bits(&s->gb, 14) + 1;
  316. if (s->frame_size < 95)
  317. return AVERROR_INVALIDDATA;
  318. s->amode = get_bits(&s->gb, 6);
  319. s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
  320. if (!s->sample_rate)
  321. return AVERROR_INVALIDDATA;
  322. s->bit_rate_index = get_bits(&s->gb, 5);
  323. s->bit_rate = ff_dca_bit_rates[s->bit_rate_index];
  324. if (!s->bit_rate)
  325. return AVERROR_INVALIDDATA;
  326. skip_bits1(&s->gb); // always 0 (reserved, cf. ETSI TS 102 114 V1.4.1)
  327. s->dynrange = get_bits(&s->gb, 1);
  328. s->timestamp = get_bits(&s->gb, 1);
  329. s->aux_data = get_bits(&s->gb, 1);
  330. s->hdcd = get_bits(&s->gb, 1);
  331. s->ext_descr = get_bits(&s->gb, 3);
  332. s->ext_coding = get_bits(&s->gb, 1);
  333. s->aspf = get_bits(&s->gb, 1);
  334. s->lfe = get_bits(&s->gb, 2);
  335. s->predictor_history = get_bits(&s->gb, 1);
  336. if (s->lfe > 2) {
  337. s->lfe = 0;
  338. av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE value: %d\n", s->lfe);
  339. return AVERROR_INVALIDDATA;
  340. }
  341. /* TODO: check CRC */
  342. if (s->crc_present)
  343. s->header_crc = get_bits(&s->gb, 16);
  344. s->multirate_inter = get_bits(&s->gb, 1);
  345. s->version = get_bits(&s->gb, 4);
  346. s->copy_history = get_bits(&s->gb, 2);
  347. s->source_pcm_res = get_bits(&s->gb, 3);
  348. s->front_sum = get_bits(&s->gb, 1);
  349. s->surround_sum = get_bits(&s->gb, 1);
  350. s->dialog_norm = get_bits(&s->gb, 4);
  351. /* FIXME: channels mixing levels */
  352. s->output = s->amode;
  353. if (s->lfe)
  354. s->output |= DCA_LFE;
  355. /* Primary audio coding header */
  356. s->audio_header.subframes = get_bits(&s->gb, 4) + 1;
  357. return dca_parse_audio_coding_header(s, 0, 0);
  358. }
  359. static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
  360. {
  361. if (level < 5) {
  362. /* huffman encoded */
  363. value += get_bitalloc(gb, &dca_scalefactor, level);
  364. value = av_clip(value, 0, (1 << log2range) - 1);
  365. } else if (level < 8) {
  366. if (level + 1 > log2range) {
  367. skip_bits(gb, level + 1 - log2range);
  368. value = get_bits(gb, log2range);
  369. } else {
  370. value = get_bits(gb, level + 1);
  371. }
  372. }
  373. return value;
  374. }
  375. static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
  376. {
  377. /* Primary audio coding side information */
  378. int j, k;
  379. if (get_bits_left(&s->gb) < 0)
  380. return AVERROR_INVALIDDATA;
  381. if (!base_channel) {
  382. s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
  383. if (block_index + s->subsubframes[s->current_subframe] > (s->sample_blocks / SAMPLES_PER_SUBBAND)) {
  384. s->subsubframes[s->current_subframe] = 1;
  385. return AVERROR_INVALIDDATA;
  386. }
  387. s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
  388. }
  389. for (j = base_channel; j < s->audio_header.prim_channels; j++) {
  390. for (k = 0; k < s->audio_header.subband_activity[j]; k++)
  391. s->dca_chan[j].prediction_mode[k] = get_bits(&s->gb, 1);
  392. }
  393. /* Get prediction codebook */
  394. for (j = base_channel; j < s->audio_header.prim_channels; j++) {
  395. for (k = 0; k < s->audio_header.subband_activity[j]; k++) {
  396. if (s->dca_chan[j].prediction_mode[k] > 0) {
  397. /* (Prediction coefficient VQ address) */
  398. s->dca_chan[j].prediction_vq[k] = get_bits(&s->gb, 12);
  399. }
  400. }
  401. }
  402. /* Bit allocation index */
  403. for (j = base_channel; j < s->audio_header.prim_channels; j++) {
  404. for (k = 0; k < s->audio_header.vq_start_subband[j]; k++) {
  405. if (s->audio_header.bitalloc_huffman[j] == 6)
  406. s->dca_chan[j].bitalloc[k] = get_bits(&s->gb, 5);
  407. else if (s->audio_header.bitalloc_huffman[j] == 5)
  408. s->dca_chan[j].bitalloc[k] = get_bits(&s->gb, 4);
  409. else if (s->audio_header.bitalloc_huffman[j] == 7) {
  410. av_log(s->avctx, AV_LOG_ERROR,
  411. "Invalid bit allocation index\n");
  412. return AVERROR_INVALIDDATA;
  413. } else {
  414. s->dca_chan[j].bitalloc[k] =
  415. get_bitalloc(&s->gb, &dca_bitalloc_index, s->audio_header.bitalloc_huffman[j]);
  416. }
  417. if (s->dca_chan[j].bitalloc[k] > 26) {
  418. ff_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n",
  419. j, k, s->dca_chan[j].bitalloc[k]);
  420. return AVERROR_INVALIDDATA;
  421. }
  422. }
  423. }
  424. /* Transition mode */
  425. for (j = base_channel; j < s->audio_header.prim_channels; j++) {
  426. for (k = 0; k < s->audio_header.subband_activity[j]; k++) {
  427. s->dca_chan[j].transition_mode[k] = 0;
  428. if (s->subsubframes[s->current_subframe] > 1 &&
  429. k < s->audio_header.vq_start_subband[j] && s->dca_chan[j].bitalloc[k] > 0) {
  430. s->dca_chan[j].transition_mode[k] =
  431. get_bitalloc(&s->gb, &dca_tmode, s->audio_header.transient_huffman[j]);
  432. }
  433. }
  434. }
  435. if (get_bits_left(&s->gb) < 0)
  436. return AVERROR_INVALIDDATA;
  437. for (j = base_channel; j < s->audio_header.prim_channels; j++) {
  438. const uint32_t *scale_table;
  439. int scale_sum, log_size;
  440. memset(s->dca_chan[j].scale_factor, 0,
  441. s->audio_header.subband_activity[j] * sizeof(s->dca_chan[j].scale_factor[0][0]) * 2);
  442. if (s->audio_header.scalefactor_huffman[j] == 6) {
  443. scale_table = ff_dca_scale_factor_quant7;
  444. log_size = 7;
  445. } else {
  446. scale_table = ff_dca_scale_factor_quant6;
  447. log_size = 6;
  448. }
  449. /* When huffman coded, only the difference is encoded */
  450. scale_sum = 0;
  451. for (k = 0; k < s->audio_header.subband_activity[j]; k++) {
  452. if (k >= s->audio_header.vq_start_subband[j] || s->dca_chan[j].bitalloc[k] > 0) {
  453. scale_sum = get_scale(&s->gb, s->audio_header.scalefactor_huffman[j], scale_sum, log_size);
  454. s->dca_chan[j].scale_factor[k][0] = scale_table[scale_sum];
  455. }
  456. if (k < s->audio_header.vq_start_subband[j] && s->dca_chan[j].transition_mode[k]) {
  457. /* Get second scale factor */
  458. scale_sum = get_scale(&s->gb, s->audio_header.scalefactor_huffman[j], scale_sum, log_size);
  459. s->dca_chan[j].scale_factor[k][1] = scale_table[scale_sum];
  460. }
  461. }
  462. }
  463. /* Joint subband scale factor codebook select */
  464. for (j = base_channel; j < s->audio_header.prim_channels; j++) {
  465. /* Transmitted only if joint subband coding enabled */
  466. if (s->audio_header.joint_intensity[j] > 0)
  467. s->dca_chan[j].joint_huff = get_bits(&s->gb, 3);
  468. }
  469. if (get_bits_left(&s->gb) < 0)
  470. return AVERROR_INVALIDDATA;
  471. /* Scale factors for joint subband coding */
  472. for (j = base_channel; j < s->audio_header.prim_channels; j++) {
  473. int source_channel;
  474. /* Transmitted only if joint subband coding enabled */
  475. if (s->audio_header.joint_intensity[j] > 0) {
  476. int scale = 0;
  477. source_channel = s->audio_header.joint_intensity[j] - 1;
  478. /* When huffman coded, only the difference is encoded
  479. * (is this valid as well for joint scales ???) */
  480. for (k = s->audio_header.subband_activity[j];
  481. k < s->audio_header.subband_activity[source_channel]; k++) {
  482. scale = get_scale(&s->gb, s->dca_chan[j].joint_huff, 64 /* bias */, 7);
  483. s->dca_chan[j].joint_scale_factor[k] = scale; /*joint_scale_table[scale]; */
  484. }
  485. if (!(s->debug_flag & 0x02)) {
  486. av_log(s->avctx, AV_LOG_DEBUG,
  487. "Joint stereo coding not supported\n");
  488. s->debug_flag |= 0x02;
  489. }
  490. }
  491. }
  492. /* Dynamic range coefficient */
  493. if (!base_channel && s->dynrange)
  494. s->dynrange_coef = get_bits(&s->gb, 8);
  495. /* Side information CRC check word */
  496. if (s->crc_present) {
  497. get_bits(&s->gb, 16);
  498. }
  499. /*
  500. * Primary audio data arrays
  501. */
  502. /* VQ encoded high frequency subbands */
  503. for (j = base_channel; j < s->audio_header.prim_channels; j++)
  504. for (k = s->audio_header.vq_start_subband[j]; k < s->audio_header.subband_activity[j]; k++)
  505. /* 1 vector -> 32 samples */
  506. s->dca_chan[j].high_freq_vq[k] = get_bits(&s->gb, 10);
  507. /* Low frequency effect data */
  508. if (!base_channel && s->lfe) {
  509. int quant7;
  510. /* LFE samples */
  511. int lfe_samples = 2 * s->lfe * (4 + block_index);
  512. int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
  513. float lfe_scale;
  514. for (j = lfe_samples; j < lfe_end_sample; j++) {
  515. /* Signed 8 bits int */
  516. s->lfe_data[j] = get_sbits(&s->gb, 8);
  517. }
  518. /* Scale factor index */
  519. quant7 = get_bits(&s->gb, 8);
  520. if (quant7 > 127) {
  521. avpriv_request_sample(s->avctx, "LFEScaleIndex larger than 127");
  522. return AVERROR_INVALIDDATA;
  523. }
  524. s->lfe_scale_factor = ff_dca_scale_factor_quant7[quant7];
  525. /* Quantization step size * scale factor */
  526. lfe_scale = 0.035 * s->lfe_scale_factor;
  527. for (j = lfe_samples; j < lfe_end_sample; j++)
  528. s->lfe_data[j] *= lfe_scale;
  529. }
  530. return 0;
  531. }
  532. static void qmf_32_subbands(DCAContext *s, int chans,
  533. float samples_in[DCA_SUBBANDS][SAMPLES_PER_SUBBAND], float *samples_out,
  534. float scale)
  535. {
  536. const float *prCoeff;
  537. int sb_act = s->audio_header.subband_activity[chans];
  538. scale *= sqrt(1 / 8.0);
  539. /* Select filter */
  540. if (!s->multirate_inter) /* Non-perfect reconstruction */
  541. prCoeff = ff_dca_fir_32bands_nonperfect;
  542. else /* Perfect reconstruction */
  543. prCoeff = ff_dca_fir_32bands_perfect;
  544. s->dcadsp.qmf_32_subbands(samples_in, sb_act, &s->synth, &s->imdct,
  545. s->dca_chan[chans].subband_fir_hist,
  546. &s->dca_chan[chans].hist_index,
  547. s->dca_chan[chans].subband_fir_noidea, prCoeff,
  548. samples_out, s->raXin, scale);
  549. }
  550. static QMF64_table *qmf64_precompute(void)
  551. {
  552. unsigned i, j;
  553. QMF64_table *table = av_malloc(sizeof(*table));
  554. if (!table)
  555. return NULL;
  556. for (i = 0; i < 32; i++)
  557. for (j = 0; j < 32; j++)
  558. table->dct4_coeff[i][j] = cos((2 * i + 1) * (2 * j + 1) * M_PI / 128);
  559. for (i = 0; i < 32; i++)
  560. for (j = 0; j < 32; j++)
  561. table->dct2_coeff[i][j] = cos((2 * i + 1) * j * M_PI / 64);
  562. /* FIXME: Is the factor 0.125 = 1/8 right? */
  563. for (i = 0; i < 32; i++)
  564. table->rcos[i] = 0.125 / cos((2 * i + 1) * M_PI / 256);
  565. for (i = 0; i < 32; i++)
  566. table->rsin[i] = -0.125 / sin((2 * i + 1) * M_PI / 256);
  567. return table;
  568. }
  569. /* FIXME: Totally unoptimized. Based on the reference code and
  570. * http://multimedia.cx/mirror/dca-transform.pdf, with guessed tweaks
  571. * for doubling the size. */
  572. static void qmf_64_subbands(DCAContext *s, int chans,
  573. float samples_in[DCA_SUBBANDS_X96K][SAMPLES_PER_SUBBAND],
  574. float *samples_out, float scale)
  575. {
  576. float raXin[64];
  577. float A[32], B[32];
  578. float *raX = s->dca_chan[chans].subband_fir_hist;
  579. float *raZ = s->dca_chan[chans].subband_fir_noidea;
  580. unsigned i, j, k, subindex;
  581. for (i = s->audio_header.subband_activity[chans]; i < DCA_SUBBANDS_X96K; i++)
  582. raXin[i] = 0.0;
  583. for (subindex = 0; subindex < SAMPLES_PER_SUBBAND; subindex++) {
  584. for (i = 0; i < s->audio_header.subband_activity[chans]; i++)
  585. raXin[i] = samples_in[i][subindex];
  586. for (k = 0; k < 32; k++) {
  587. A[k] = 0.0;
  588. for (i = 0; i < 32; i++)
  589. A[k] += (raXin[2 * i] + raXin[2 * i + 1]) * s->qmf64_table->dct4_coeff[k][i];
  590. }
  591. for (k = 0; k < 32; k++) {
  592. B[k] = raXin[0] * s->qmf64_table->dct2_coeff[k][0];
  593. for (i = 1; i < 32; i++)
  594. B[k] += (raXin[2 * i] + raXin[2 * i - 1]) * s->qmf64_table->dct2_coeff[k][i];
  595. }
  596. for (k = 0; k < 32; k++) {
  597. raX[k] = s->qmf64_table->rcos[k] * (A[k] + B[k]);
  598. raX[63 - k] = s->qmf64_table->rsin[k] * (A[k] - B[k]);
  599. }
  600. for (i = 0; i < DCA_SUBBANDS_X96K; i++) {
  601. float out = raZ[i];
  602. for (j = 0; j < 1024; j += 128)
  603. out += ff_dca_fir_64bands[j + i] * (raX[j + i] - raX[j + 63 - i]);
  604. *samples_out++ = out * scale;
  605. }
  606. for (i = 0; i < DCA_SUBBANDS_X96K; i++) {
  607. float hist = 0.0;
  608. for (j = 0; j < 1024; j += 128)
  609. hist += ff_dca_fir_64bands[64 + j + i] * (-raX[i + j] - raX[j + 63 - i]);
  610. raZ[i] = hist;
  611. }
  612. /* FIXME: Make buffer circular, to avoid this move. */
  613. memmove(raX + 64, raX, (1024 - 64) * sizeof(*raX));
  614. }
  615. }
  616. static void lfe_interpolation_fir(DCAContext *s, const float *samples_in,
  617. float *samples_out)
  618. {
  619. /* samples_in: An array holding decimated samples.
  620. * Samples in current subframe starts from samples_in[0],
  621. * while samples_in[-1], samples_in[-2], ..., stores samples
  622. * from last subframe as history.
  623. *
  624. * samples_out: An array holding interpolated samples
  625. */
  626. int idx;
  627. const float *prCoeff;
  628. int deciindex;
  629. /* Select decimation filter */
  630. if (s->lfe == 1) {
  631. idx = 1;
  632. prCoeff = ff_dca_lfe_fir_128;
  633. } else {
  634. idx = 0;
  635. if (s->exss_ext_mask & DCA_EXT_EXSS_XLL)
  636. prCoeff = ff_dca_lfe_xll_fir_64;
  637. else
  638. prCoeff = ff_dca_lfe_fir_64;
  639. }
  640. /* Interpolation */
  641. for (deciindex = 0; deciindex < 2 * s->lfe; deciindex++) {
  642. s->dcadsp.lfe_fir[idx](samples_out, samples_in, prCoeff);
  643. samples_in++;
  644. samples_out += 2 * 32 * (1 + idx);
  645. }
  646. }
  647. /* downmixing routines */
  648. #define MIX_REAR1(samples, s1, rs, coef) \
  649. samples[0][i] += samples[s1][i] * coef[rs][0]; \
  650. samples[1][i] += samples[s1][i] * coef[rs][1];
  651. #define MIX_REAR2(samples, s1, s2, rs, coef) \
  652. samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
  653. samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
  654. #define MIX_FRONT3(samples, coef) \
  655. t = samples[c][i]; \
  656. u = samples[l][i]; \
  657. v = samples[r][i]; \
  658. samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
  659. samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
  660. #define DOWNMIX_TO_STEREO(op1, op2) \
  661. for (i = 0; i < 256; i++) { \
  662. op1 \
  663. op2 \
  664. }
  665. static void dca_downmix(float **samples, int srcfmt, int lfe_present,
  666. float coef[DCA_PRIM_CHANNELS_MAX + 1][2],
  667. const int8_t *channel_mapping)
  668. {
  669. int c, l, r, sl, sr, s;
  670. int i;
  671. float t, u, v;
  672. switch (srcfmt) {
  673. case DCA_MONO:
  674. case DCA_4F2R:
  675. av_log(NULL, AV_LOG_ERROR, "Not implemented!\n");
  676. break;
  677. case DCA_CHANNEL:
  678. case DCA_STEREO:
  679. case DCA_STEREO_TOTAL:
  680. case DCA_STEREO_SUMDIFF:
  681. break;
  682. case DCA_3F:
  683. c = channel_mapping[0];
  684. l = channel_mapping[1];
  685. r = channel_mapping[2];
  686. DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
  687. break;
  688. case DCA_2F1R:
  689. s = channel_mapping[2];
  690. DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
  691. break;
  692. case DCA_3F1R:
  693. c = channel_mapping[0];
  694. l = channel_mapping[1];
  695. r = channel_mapping[2];
  696. s = channel_mapping[3];
  697. DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
  698. MIX_REAR1(samples, s, 3, coef));
  699. break;
  700. case DCA_2F2R:
  701. sl = channel_mapping[2];
  702. sr = channel_mapping[3];
  703. DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
  704. break;
  705. case DCA_3F2R:
  706. c = channel_mapping[0];
  707. l = channel_mapping[1];
  708. r = channel_mapping[2];
  709. sl = channel_mapping[3];
  710. sr = channel_mapping[4];
  711. DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
  712. MIX_REAR2(samples, sl, sr, 3, coef));
  713. break;
  714. }
  715. if (lfe_present) {
  716. int lf_buf = ff_dca_lfe_index[srcfmt];
  717. int lf_idx = ff_dca_channels[srcfmt];
  718. for (i = 0; i < 256; i++) {
  719. samples[0][i] += samples[lf_buf][i] * coef[lf_idx][0];
  720. samples[1][i] += samples[lf_buf][i] * coef[lf_idx][1];
  721. }
  722. }
  723. }
  724. #ifndef decode_blockcodes
  725. /* Very compact version of the block code decoder that does not use table
  726. * look-up but is slightly slower */
  727. static int decode_blockcode(int code, int levels, int32_t *values)
  728. {
  729. int i;
  730. int offset = (levels - 1) >> 1;
  731. for (i = 0; i < 4; i++) {
  732. int div = FASTDIV(code, levels);
  733. values[i] = code - offset - div * levels;
  734. code = div;
  735. }
  736. return code;
  737. }
  738. static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
  739. {
  740. return decode_blockcode(code1, levels, values) |
  741. decode_blockcode(code2, levels, values + 4);
  742. }
  743. #endif
  744. static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
  745. static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
  746. static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
  747. {
  748. int k, l;
  749. int subsubframe = s->current_subsubframe;
  750. const uint32_t *quant_step_table;
  751. /*
  752. * Audio data
  753. */
  754. /* Select quantization step size table */
  755. if (s->bit_rate_index == 0x1f)
  756. quant_step_table = ff_dca_lossless_quant;
  757. else
  758. quant_step_table = ff_dca_lossy_quant;
  759. for (k = base_channel; k < s->audio_header.prim_channels; k++) {
  760. int32_t (*subband_samples)[8] = s->dca_chan[k].subband_samples[block_index];
  761. if (get_bits_left(&s->gb) < 0)
  762. return AVERROR_INVALIDDATA;
  763. for (l = 0; l < s->audio_header.vq_start_subband[k]; l++) {
  764. int m;
  765. /* Select the mid-tread linear quantizer */
  766. int abits = s->dca_chan[k].bitalloc[l];
  767. uint32_t quant_step_size = quant_step_table[abits];
  768. /*
  769. * Extract bits from the bit stream
  770. */
  771. if (!abits)
  772. memset(subband_samples[l], 0, SAMPLES_PER_SUBBAND *
  773. sizeof(subband_samples[l][0]));
  774. else {
  775. uint32_t rscale;
  776. /* Deal with transients */
  777. int sfi = s->dca_chan[k].transition_mode[l] &&
  778. subsubframe >= s->dca_chan[k].transition_mode[l];
  779. /* Determine quantization index code book and its type.
  780. Select quantization index code book */
  781. int sel = s->audio_header.quant_index_huffman[k][abits];
  782. rscale = (s->dca_chan[k].scale_factor[l][sfi] *
  783. s->audio_header.scalefactor_adj[k][sel] + 8) >> 4;
  784. if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
  785. if (abits <= 7) {
  786. /* Block code */
  787. int block_code1, block_code2, size, levels, err;
  788. size = abits_sizes[abits - 1];
  789. levels = abits_levels[abits - 1];
  790. block_code1 = get_bits(&s->gb, size);
  791. block_code2 = get_bits(&s->gb, size);
  792. err = decode_blockcodes(block_code1, block_code2,
  793. levels, subband_samples[l]);
  794. if (err) {
  795. av_log(s->avctx, AV_LOG_ERROR,
  796. "ERROR: block code look-up failed\n");
  797. return AVERROR_INVALIDDATA;
  798. }
  799. } else {
  800. /* no coding */
  801. for (m = 0; m < SAMPLES_PER_SUBBAND; m++)
  802. subband_samples[l][m] = get_sbits(&s->gb, abits - 3);
  803. }
  804. } else {
  805. /* Huffman coded */
  806. for (m = 0; m < SAMPLES_PER_SUBBAND; m++)
  807. subband_samples[l][m] = get_bitalloc(&s->gb,
  808. &dca_smpl_bitalloc[abits], sel);
  809. }
  810. s->dcadsp.dequantize(subband_samples[l], quant_step_size, rscale);
  811. }
  812. }
  813. for (l = 0; l < s->audio_header.vq_start_subband[k]; l++) {
  814. int m;
  815. /*
  816. * Inverse ADPCM if in prediction mode
  817. */
  818. if (s->dca_chan[k].prediction_mode[l]) {
  819. int n;
  820. if (s->predictor_history)
  821. subband_samples[l][0] += (ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][0] *
  822. (int64_t)s->dca_chan[k].subband_samples_hist[l][3] +
  823. ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][1] *
  824. (int64_t)s->dca_chan[k].subband_samples_hist[l][2] +
  825. ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][2] *
  826. (int64_t)s->dca_chan[k].subband_samples_hist[l][1] +
  827. ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][3] *
  828. (int64_t)s->dca_chan[k].subband_samples_hist[l][0]) +
  829. (1 << 12) >> 13;
  830. for (m = 1; m < SAMPLES_PER_SUBBAND; m++) {
  831. int64_t sum = ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][0] *
  832. (int64_t)subband_samples[l][m - 1];
  833. for (n = 2; n <= 4; n++)
  834. if (m >= n)
  835. sum += ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][n - 1] *
  836. (int64_t)subband_samples[l][m - n];
  837. else if (s->predictor_history)
  838. sum += ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][n - 1] *
  839. (int64_t)s->dca_chan[k].subband_samples_hist[l][m - n + 4];
  840. subband_samples[l][m] += (int32_t)(sum + (1 << 12) >> 13);
  841. }
  842. }
  843. }
  844. /* Backup predictor history for adpcm */
  845. for (l = 0; l < DCA_SUBBANDS; l++)
  846. AV_COPY128(s->dca_chan[k].subband_samples_hist[l], &subband_samples[l][4]);
  847. /*
  848. * Decode VQ encoded high frequencies
  849. */
  850. if (s->audio_header.subband_activity[k] > s->audio_header.vq_start_subband[k]) {
  851. if (!(s->debug_flag & 0x01)) {
  852. av_log(s->avctx, AV_LOG_DEBUG,
  853. "Stream with high frequencies VQ coding\n");
  854. s->debug_flag |= 0x01;
  855. }
  856. s->dcadsp.decode_hf(subband_samples, s->dca_chan[k].high_freq_vq,
  857. ff_dca_high_freq_vq,
  858. subsubframe * SAMPLES_PER_SUBBAND,
  859. s->dca_chan[k].scale_factor,
  860. s->audio_header.vq_start_subband[k],
  861. s->audio_header.subband_activity[k]);
  862. }
  863. }
  864. /* Check for DSYNC after subsubframe */
  865. if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
  866. if (get_bits(&s->gb, 16) != 0xFFFF) {
  867. av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
  868. return AVERROR_INVALIDDATA;
  869. }
  870. }
  871. return 0;
  872. }
  873. static int dca_filter_channels(DCAContext *s, int block_index, int upsample)
  874. {
  875. int k;
  876. if (upsample) {
  877. LOCAL_ALIGNED(32, float, samples, [DCA_SUBBANDS_X96K], [SAMPLES_PER_SUBBAND]);
  878. if (!s->qmf64_table) {
  879. s->qmf64_table = qmf64_precompute();
  880. if (!s->qmf64_table)
  881. return AVERROR(ENOMEM);
  882. }
  883. /* 64 subbands QMF */
  884. for (k = 0; k < s->audio_header.prim_channels; k++) {
  885. int32_t (*subband_samples)[SAMPLES_PER_SUBBAND] =
  886. s->dca_chan[k].subband_samples[block_index];
  887. s->fmt_conv.int32_to_float(samples[0], subband_samples[0],
  888. DCA_SUBBANDS_X96K * SAMPLES_PER_SUBBAND);
  889. if (s->channel_order_tab[k] >= 0)
  890. qmf_64_subbands(s, k, samples,
  891. s->samples_chanptr[s->channel_order_tab[k]],
  892. /* Upsampling needs a factor 2 here. */
  893. M_SQRT2 / 32768.0);
  894. }
  895. } else {
  896. /* 32 subbands QMF */
  897. LOCAL_ALIGNED(32, float, samples, [DCA_SUBBANDS], [SAMPLES_PER_SUBBAND]);
  898. for (k = 0; k < s->audio_header.prim_channels; k++) {
  899. int32_t (*subband_samples)[SAMPLES_PER_SUBBAND] =
  900. s->dca_chan[k].subband_samples[block_index];
  901. s->fmt_conv.int32_to_float(samples[0], subband_samples[0],
  902. DCA_SUBBANDS * SAMPLES_PER_SUBBAND);
  903. if (s->channel_order_tab[k] >= 0)
  904. qmf_32_subbands(s, k, samples,
  905. s->samples_chanptr[s->channel_order_tab[k]],
  906. M_SQRT1_2 / 32768.0);
  907. }
  908. }
  909. /* Generate LFE samples for this subsubframe FIXME!!! */
  910. if (s->lfe) {
  911. float *samples = s->samples_chanptr[s->lfe_index];
  912. lfe_interpolation_fir(s,
  913. s->lfe_data + 2 * s->lfe * (block_index + 4),
  914. samples);
  915. if (upsample) {
  916. unsigned i;
  917. /* Should apply the filter in Table 6-11 when upsampling. For
  918. * now, just duplicate. */
  919. for (i = 255; i > 0; i--) {
  920. samples[2 * i] =
  921. samples[2 * i + 1] = samples[i];
  922. }
  923. samples[1] = samples[0];
  924. }
  925. }
  926. /* FIXME: This downmixing is probably broken with upsample.
  927. * Probably totally broken also with XLL in general. */
  928. /* Downmixing to Stereo */
  929. if (s->audio_header.prim_channels + !!s->lfe > 2 &&
  930. s->avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
  931. dca_downmix(s->samples_chanptr, s->amode, !!s->lfe, s->downmix_coef,
  932. s->channel_order_tab);
  933. }
  934. return 0;
  935. }
  936. static int dca_subframe_footer(DCAContext *s, int base_channel)
  937. {
  938. int in, out, aux_data_count, aux_data_end, reserved;
  939. uint32_t nsyncaux;
  940. /*
  941. * Unpack optional information
  942. */
  943. /* presumably optional information only appears in the core? */
  944. if (!base_channel) {
  945. if (s->timestamp)
  946. skip_bits_long(&s->gb, 32);
  947. if (s->aux_data) {
  948. aux_data_count = get_bits(&s->gb, 6);
  949. // align (32-bit)
  950. skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
  951. aux_data_end = 8 * aux_data_count + get_bits_count(&s->gb);
  952. if ((nsyncaux = get_bits_long(&s->gb, 32)) != DCA_NSYNCAUX) {
  953. av_log(s->avctx, AV_LOG_ERROR, "nSYNCAUX mismatch %#"PRIx32"\n",
  954. nsyncaux);
  955. return AVERROR_INVALIDDATA;
  956. }
  957. if (get_bits1(&s->gb)) { // bAUXTimeStampFlag
  958. avpriv_request_sample(s->avctx,
  959. "Auxiliary Decode Time Stamp Flag");
  960. // align (4-bit)
  961. skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 4);
  962. // 44 bits: nMSByte (8), nMarker (4), nLSByte (28), nMarker (4)
  963. skip_bits_long(&s->gb, 44);
  964. }
  965. if ((s->core_downmix = get_bits1(&s->gb))) {
  966. int am = get_bits(&s->gb, 3);
  967. switch (am) {
  968. case 0:
  969. s->core_downmix_amode = DCA_MONO;
  970. break;
  971. case 1:
  972. s->core_downmix_amode = DCA_STEREO;
  973. break;
  974. case 2:
  975. s->core_downmix_amode = DCA_STEREO_TOTAL;
  976. break;
  977. case 3:
  978. s->core_downmix_amode = DCA_3F;
  979. break;
  980. case 4:
  981. s->core_downmix_amode = DCA_2F1R;
  982. break;
  983. case 5:
  984. s->core_downmix_amode = DCA_2F2R;
  985. break;
  986. case 6:
  987. s->core_downmix_amode = DCA_3F1R;
  988. break;
  989. default:
  990. av_log(s->avctx, AV_LOG_ERROR,
  991. "Invalid mode %d for embedded downmix coefficients\n",
  992. am);
  993. return AVERROR_INVALIDDATA;
  994. }
  995. for (out = 0; out < ff_dca_channels[s->core_downmix_amode]; out++) {
  996. for (in = 0; in < s->audio_header.prim_channels + !!s->lfe; in++) {
  997. uint16_t tmp = get_bits(&s->gb, 9);
  998. if ((tmp & 0xFF) > 241) {
  999. av_log(s->avctx, AV_LOG_ERROR,
  1000. "Invalid downmix coefficient code %"PRIu16"\n",
  1001. tmp);
  1002. return AVERROR_INVALIDDATA;
  1003. }
  1004. s->core_downmix_codes[in][out] = tmp;
  1005. }
  1006. }
  1007. }
  1008. align_get_bits(&s->gb); // byte align
  1009. skip_bits(&s->gb, 16); // nAUXCRC16
  1010. /*
  1011. * additional data (reserved, cf. ETSI TS 102 114 V1.4.1)
  1012. *
  1013. * Note: don't check for overreads, aux_data_count can't be trusted.
  1014. */
  1015. if ((reserved = (aux_data_end - get_bits_count(&s->gb))) > 0) {
  1016. avpriv_request_sample(s->avctx,
  1017. "Core auxiliary data reserved content");
  1018. skip_bits_long(&s->gb, reserved);
  1019. }
  1020. }
  1021. if (s->crc_present && s->dynrange)
  1022. get_bits(&s->gb, 16);
  1023. }
  1024. return 0;
  1025. }
  1026. /**
  1027. * Decode a dca frame block
  1028. *
  1029. * @param s pointer to the DCAContext
  1030. */
  1031. static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
  1032. {
  1033. int ret;
  1034. /* Sanity check */
  1035. if (s->current_subframe >= s->audio_header.subframes) {
  1036. av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
  1037. s->current_subframe, s->audio_header.subframes);
  1038. return AVERROR_INVALIDDATA;
  1039. }
  1040. if (!s->current_subsubframe) {
  1041. /* Read subframe header */
  1042. if ((ret = dca_subframe_header(s, base_channel, block_index)))
  1043. return ret;
  1044. }
  1045. /* Read subsubframe */
  1046. if ((ret = dca_subsubframe(s, base_channel, block_index)))
  1047. return ret;
  1048. /* Update state */
  1049. s->current_subsubframe++;
  1050. if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
  1051. s->current_subsubframe = 0;
  1052. s->current_subframe++;
  1053. }
  1054. if (s->current_subframe >= s->audio_header.subframes) {
  1055. /* Read subframe footer */
  1056. if ((ret = dca_subframe_footer(s, base_channel)))
  1057. return ret;
  1058. }
  1059. return 0;
  1060. }
  1061. int ff_dca_xbr_parse_frame(DCAContext *s)
  1062. {
  1063. int scale_table_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS][2];
  1064. int active_bands[DCA_CHSETS_MAX][DCA_CHSET_CHANS_MAX];
  1065. int abits_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS];
  1066. int anctemp[DCA_CHSET_CHANS_MAX];
  1067. int chset_fsize[DCA_CHSETS_MAX];
  1068. int n_xbr_ch[DCA_CHSETS_MAX];
  1069. int hdr_size, num_chsets, xbr_tmode, hdr_pos;
  1070. int i, j, k, l, chset, chan_base;
  1071. av_log(s->avctx, AV_LOG_DEBUG, "DTS-XBR: decoding XBR extension\n");
  1072. /* get bit position of sync header */
  1073. hdr_pos = get_bits_count(&s->gb) - 32;
  1074. hdr_size = get_bits(&s->gb, 6) + 1;
  1075. num_chsets = get_bits(&s->gb, 2) + 1;
  1076. for(i = 0; i < num_chsets; i++)
  1077. chset_fsize[i] = get_bits(&s->gb, 14) + 1;
  1078. xbr_tmode = get_bits1(&s->gb);
  1079. for(i = 0; i < num_chsets; i++) {
  1080. n_xbr_ch[i] = get_bits(&s->gb, 3) + 1;
  1081. k = get_bits(&s->gb, 2) + 5;
  1082. for(j = 0; j < n_xbr_ch[i]; j++) {
  1083. active_bands[i][j] = get_bits(&s->gb, k) + 1;
  1084. if (active_bands[i][j] > DCA_SUBBANDS) {
  1085. av_log(s->avctx, AV_LOG_ERROR, "too many active subbands (%d)\n", active_bands[i][j]);
  1086. return AVERROR_INVALIDDATA;
  1087. }
  1088. }
  1089. }
  1090. /* skip to the end of the header */
  1091. i = get_bits_count(&s->gb);
  1092. if(hdr_pos + hdr_size * 8 > i)
  1093. skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
  1094. /* loop over the channel data sets */
  1095. /* only decode as many channels as we've decoded base data for */
  1096. for(chset = 0, chan_base = 0;
  1097. chset < num_chsets && chan_base + n_xbr_ch[chset] <= s->audio_header.prim_channels;
  1098. chan_base += n_xbr_ch[chset++]) {
  1099. int start_posn = get_bits_count(&s->gb);
  1100. int subsubframe = 0;
  1101. int subframe = 0;
  1102. /* loop over subframes */
  1103. for (k = 0; k < (s->sample_blocks / 8); k++) {
  1104. /* parse header if we're on first subsubframe of a block */
  1105. if(subsubframe == 0) {
  1106. /* Parse subframe header */
  1107. for(i = 0; i < n_xbr_ch[chset]; i++) {
  1108. anctemp[i] = get_bits(&s->gb, 2) + 2;
  1109. }
  1110. for(i = 0; i < n_xbr_ch[chset]; i++) {
  1111. get_array(&s->gb, abits_high[i], active_bands[chset][i], anctemp[i]);
  1112. }
  1113. for(i = 0; i < n_xbr_ch[chset]; i++) {
  1114. anctemp[i] = get_bits(&s->gb, 3);
  1115. if(anctemp[i] < 1) {
  1116. av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: SYNC ERROR\n");
  1117. return AVERROR_INVALIDDATA;
  1118. }
  1119. }
  1120. /* generate scale factors */
  1121. for(i = 0; i < n_xbr_ch[chset]; i++) {
  1122. const uint32_t *scale_table;
  1123. int nbits;
  1124. int scale_table_size;
  1125. if (s->audio_header.scalefactor_huffman[chan_base+i] == 6) {
  1126. scale_table = ff_dca_scale_factor_quant7;
  1127. scale_table_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
  1128. } else {
  1129. scale_table = ff_dca_scale_factor_quant6;
  1130. scale_table_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
  1131. }
  1132. nbits = anctemp[i];
  1133. for(j = 0; j < active_bands[chset][i]; j++) {
  1134. if(abits_high[i][j] > 0) {
  1135. int index = get_bits(&s->gb, nbits);
  1136. if (index >= scale_table_size) {
  1137. av_log(s->avctx, AV_LOG_ERROR, "scale table index %d invalid\n", index);
  1138. return AVERROR_INVALIDDATA;
  1139. }
  1140. scale_table_high[i][j][0] = scale_table[index];
  1141. if(xbr_tmode && s->dca_chan[i].transition_mode[j]) {
  1142. int index = get_bits(&s->gb, nbits);
  1143. if (index >= scale_table_size) {
  1144. av_log(s->avctx, AV_LOG_ERROR, "scale table index %d invalid\n", index);
  1145. return AVERROR_INVALIDDATA;
  1146. }
  1147. scale_table_high[i][j][1] = scale_table[index];
  1148. }
  1149. }
  1150. }
  1151. }
  1152. }
  1153. /* decode audio array for this block */
  1154. for(i = 0; i < n_xbr_ch[chset]; i++) {
  1155. for(j = 0; j < active_bands[chset][i]; j++) {
  1156. const int xbr_abits = abits_high[i][j];
  1157. const uint32_t quant_step_size = ff_dca_lossless_quant[xbr_abits];
  1158. const int sfi = xbr_tmode && s->dca_chan[i].transition_mode[j] && subsubframe >= s->dca_chan[i].transition_mode[j];
  1159. const uint32_t rscale = scale_table_high[i][j][sfi];
  1160. int32_t *subband_samples = s->dca_chan[chan_base+i].subband_samples[k][j];
  1161. int32_t block[SAMPLES_PER_SUBBAND];
  1162. if(xbr_abits <= 0)
  1163. continue;
  1164. if(xbr_abits > 7) {
  1165. get_array(&s->gb, block, SAMPLES_PER_SUBBAND, xbr_abits - 3);
  1166. } else {
  1167. int block_code1, block_code2, size, levels, err;
  1168. size = abits_sizes[xbr_abits - 1];
  1169. levels = abits_levels[xbr_abits - 1];
  1170. block_code1 = get_bits(&s->gb, size);
  1171. block_code2 = get_bits(&s->gb, size);
  1172. err = decode_blockcodes(block_code1, block_code2,
  1173. levels, block);
  1174. if (err) {
  1175. av_log(s->avctx, AV_LOG_ERROR,
  1176. "ERROR: DTS-XBR: block code look-up failed\n");
  1177. return AVERROR_INVALIDDATA;
  1178. }
  1179. }
  1180. /* scale & sum into subband */
  1181. s->dcadsp.dequantize(block, quant_step_size, rscale);
  1182. for(l = 0; l < SAMPLES_PER_SUBBAND; l++)
  1183. subband_samples[l] += block[l];
  1184. }
  1185. }
  1186. /* check DSYNC marker */
  1187. if(s->aspf || subsubframe == s->subsubframes[subframe] - 1) {
  1188. if(get_bits(&s->gb, 16) != 0xffff) {
  1189. av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: Didn't get subframe DSYNC\n");
  1190. return AVERROR_INVALIDDATA;
  1191. }
  1192. }
  1193. /* advance sub-sub-frame index */
  1194. if(++subsubframe >= s->subsubframes[subframe]) {
  1195. subsubframe = 0;
  1196. subframe++;
  1197. }
  1198. }
  1199. /* skip to next channel set */
  1200. i = get_bits_count(&s->gb);
  1201. if(start_posn + chset_fsize[chset] * 8 != i) {
  1202. j = start_posn + chset_fsize[chset] * 8 - i;
  1203. if(j < 0 || j >= 8)
  1204. av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: end of channel set,"
  1205. " skipping further than expected (%d bits)\n", j);
  1206. skip_bits_long(&s->gb, j);
  1207. }
  1208. }
  1209. return 0;
  1210. }
  1211. /* parse initial header for XXCH and dump details */
  1212. int ff_dca_xxch_decode_frame(DCAContext *s)
  1213. {
  1214. int hdr_size, spkmsk_bits, num_chsets, core_spk, hdr_pos;
  1215. int i, chset, base_channel, chstart, fsize[8];
  1216. /* assume header word has already been parsed */
  1217. hdr_pos = get_bits_count(&s->gb) - 32;
  1218. hdr_size = get_bits(&s->gb, 6) + 1;
  1219. /*chhdr_crc =*/ skip_bits1(&s->gb);
  1220. spkmsk_bits = get_bits(&s->gb, 5) + 1;
  1221. num_chsets = get_bits(&s->gb, 2) + 1;
  1222. for (i = 0; i < num_chsets; i++)
  1223. fsize[i] = get_bits(&s->gb, 14) + 1;
  1224. core_spk = get_bits(&s->gb, spkmsk_bits);
  1225. s->xxch_core_spkmask = core_spk;
  1226. s->xxch_nbits_spk_mask = spkmsk_bits;
  1227. s->xxch_dmix_embedded = 0;
  1228. /* skip to the end of the header */
  1229. i = get_bits_count(&s->gb);
  1230. if (hdr_pos + hdr_size * 8 > i)
  1231. skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
  1232. for (chset = 0; chset < num_chsets; chset++) {
  1233. chstart = get_bits_count(&s->gb);
  1234. base_channel = s->audio_header.prim_channels;
  1235. s->xxch_chset = chset;
  1236. /* XXCH and Core headers differ, see 6.4.2 "XXCH Channel Set Header" vs.
  1237. 5.3.2 "Primary Audio Coding Header", DTS Spec 1.3.1 */
  1238. dca_parse_audio_coding_header(s, base_channel, 1);
  1239. /* decode channel data */
  1240. for (i = 0; i < (s->sample_blocks / 8); i++) {
  1241. if (dca_decode_block(s, base_channel, i)) {
  1242. av_log(s->avctx, AV_LOG_ERROR,
  1243. "Error decoding DTS-XXCH extension\n");
  1244. continue;
  1245. }
  1246. }
  1247. /* skip to end of this section */
  1248. i = get_bits_count(&s->gb);
  1249. if (chstart + fsize[chset] * 8 > i)
  1250. skip_bits_long(&s->gb, chstart + fsize[chset] * 8 - i);
  1251. }
  1252. s->xxch_chset = num_chsets;
  1253. return 0;
  1254. }
  1255. static float dca_dmix_code(unsigned code)
  1256. {
  1257. int sign = (code >> 8) - 1;
  1258. code &= 0xff;
  1259. return ((ff_dca_dmixtable[code] ^ sign) - sign) * (1.0 / (1 << 15));
  1260. }
  1261. static int scan_for_extensions(AVCodecContext *avctx)
  1262. {
  1263. DCAContext *s = avctx->priv_data;
  1264. int core_ss_end, ret = 0;
  1265. core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
  1266. /* only scan for extensions if ext_descr was unknown or indicated a
  1267. * supported XCh extension */
  1268. if (s->core_ext_mask < 0 || s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH)) {
  1269. /* if ext_descr was unknown, clear s->core_ext_mask so that the
  1270. * extensions scan can fill it up */
  1271. s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
  1272. /* extensions start at 32-bit boundaries into bitstream */
  1273. skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
  1274. while (core_ss_end - get_bits_count(&s->gb) >= 32) {
  1275. uint32_t bits = get_bits_long(&s->gb, 32);
  1276. int i;
  1277. switch (bits) {
  1278. case DCA_SYNCWORD_XCH: {
  1279. int ext_amode, xch_fsize;
  1280. s->xch_base_channel = s->audio_header.prim_channels;
  1281. /* validate sync word using XCHFSIZE field */
  1282. xch_fsize = show_bits(&s->gb, 10);
  1283. if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
  1284. (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
  1285. continue;
  1286. /* skip length-to-end-of-frame field for the moment */
  1287. skip_bits(&s->gb, 10);
  1288. s->core_ext_mask |= DCA_EXT_XCH;
  1289. /* extension amode(number of channels in extension) should be 1 */
  1290. /* AFAIK XCh is not used for more channels */
  1291. if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
  1292. av_log(avctx, AV_LOG_ERROR,
  1293. "XCh extension amode %d not supported!\n",
  1294. ext_amode);
  1295. continue;
  1296. }
  1297. if (s->xch_base_channel < 2) {
  1298. avpriv_request_sample(avctx, "XCh with fewer than 2 base channels");
  1299. continue;
  1300. }
  1301. /* much like core primary audio coding header */
  1302. dca_parse_audio_coding_header(s, s->xch_base_channel, 0);
  1303. for (i = 0; i < (s->sample_blocks / 8); i++)
  1304. if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
  1305. av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
  1306. continue;
  1307. }
  1308. s->xch_present = 1;
  1309. break;
  1310. }
  1311. case DCA_SYNCWORD_XXCH:
  1312. /* XXCh: extended channels */
  1313. /* usually found either in core or HD part in DTS-HD HRA streams,
  1314. * but not in DTS-ES which contains XCh extensions instead */
  1315. s->core_ext_mask |= DCA_EXT_XXCH;
  1316. ff_dca_xxch_decode_frame(s);
  1317. break;
  1318. case 0x1d95f262: {
  1319. int fsize96 = show_bits(&s->gb, 12) + 1;
  1320. if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
  1321. continue;
  1322. av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
  1323. get_bits_count(&s->gb));
  1324. skip_bits(&s->gb, 12);
  1325. av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
  1326. av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
  1327. s->core_ext_mask |= DCA_EXT_X96;
  1328. break;
  1329. }
  1330. }
  1331. skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
  1332. }
  1333. } else {
  1334. /* no supported extensions, skip the rest of the core substream */
  1335. skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
  1336. }
  1337. if (s->core_ext_mask & DCA_EXT_X96)
  1338. s->profile = FF_PROFILE_DTS_96_24;
  1339. else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
  1340. s->profile = FF_PROFILE_DTS_ES;
  1341. /* check for ExSS (HD part) */
  1342. if (s->dca_buffer_size - s->frame_size > 32 &&
  1343. get_bits_long(&s->gb, 32) == DCA_SYNCWORD_SUBSTREAM)
  1344. ff_dca_exss_parse_header(s);
  1345. return ret;
  1346. }
  1347. static int set_channel_layout(AVCodecContext *avctx, int *channels, int num_core_channels)
  1348. {
  1349. DCAContext *s = avctx->priv_data;
  1350. int i, j, chset, mask;
  1351. int channel_layout, channel_mask;
  1352. int posn, lavc;
  1353. /* If we have XXCH then the channel layout is managed differently */
  1354. /* note that XLL will also have another way to do things */
  1355. if (!(s->core_ext_mask & DCA_EXT_XXCH)) {
  1356. /* xxx should also do MA extensions */
  1357. if (s->amode < 16) {
  1358. avctx->channel_layout = ff_dca_core_channel_layout[s->amode];
  1359. if (s->audio_header.prim_channels + !!s->lfe > 2 &&
  1360. avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
  1361. /*
  1362. * Neither the core's auxiliary data nor our default tables contain
  1363. * downmix coefficients for the additional channel coded in the XCh
  1364. * extension, so when we're doing a Stereo downmix, don't decode it.
  1365. */
  1366. s->xch_disable = 1;
  1367. }
  1368. if (s->xch_present && !s->xch_disable) {
  1369. if (avctx->channel_layout & AV_CH_BACK_CENTER) {
  1370. avpriv_request_sample(avctx, "XCh with Back center channel");
  1371. return AVERROR_INVALIDDATA;
  1372. }
  1373. avctx->channel_layout |= AV_CH_BACK_CENTER;
  1374. if (s->lfe) {
  1375. avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
  1376. s->channel_order_tab = ff_dca_channel_reorder_lfe_xch[s->amode];
  1377. } else {
  1378. s->channel_order_tab = ff_dca_channel_reorder_nolfe_xch[s->amode];
  1379. }
  1380. if (s->channel_order_tab[s->xch_base_channel] < 0)
  1381. return AVERROR_INVALIDDATA;
  1382. } else {
  1383. *channels = num_core_channels + !!s->lfe;
  1384. s->xch_present = 0; /* disable further xch processing */
  1385. if (s->lfe) {
  1386. avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
  1387. s->channel_order_tab = ff_dca_channel_reorder_lfe[s->amode];
  1388. } else
  1389. s->channel_order_tab = ff_dca_channel_reorder_nolfe[s->amode];
  1390. }
  1391. if (*channels > !!s->lfe &&
  1392. s->channel_order_tab[*channels - 1 - !!s->lfe] < 0)
  1393. return AVERROR_INVALIDDATA;
  1394. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != *channels) {
  1395. av_log(avctx, AV_LOG_ERROR, "Number of channels %d mismatches layout %d\n", *channels, av_get_channel_layout_nb_channels(avctx->channel_layout));
  1396. return AVERROR_INVALIDDATA;
  1397. }
  1398. if (num_core_channels + !!s->lfe > 2 &&
  1399. avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
  1400. *channels = 2;
  1401. s->output = s->audio_header.prim_channels == 2 ? s->amode : DCA_STEREO;
  1402. avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  1403. }
  1404. else if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
  1405. static const int8_t dca_channel_order_native[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
  1406. s->channel_order_tab = dca_channel_order_native;
  1407. }
  1408. s->lfe_index = ff_dca_lfe_index[s->amode];
  1409. } else {
  1410. av_log(avctx, AV_LOG_ERROR,
  1411. "Non standard configuration %d !\n", s->amode);
  1412. return AVERROR_INVALIDDATA;
  1413. }
  1414. s->xxch_dmix_embedded = 0;
  1415. } else {
  1416. /* we only get here if an XXCH channel set can be added to the mix */
  1417. channel_mask = s->xxch_core_spkmask;
  1418. {
  1419. *channels = s->audio_header.prim_channels + !!s->lfe;
  1420. for (i = 0; i < s->xxch_chset; i++) {
  1421. channel_mask |= s->xxch_spk_masks[i];
  1422. }
  1423. }
  1424. /* Given the DTS spec'ed channel mask, generate an avcodec version */
  1425. channel_layout = 0;
  1426. for (i = 0; i < s->xxch_nbits_spk_mask; ++i) {
  1427. if (channel_mask & (1 << i)) {
  1428. channel_layout |= ff_dca_map_xxch_to_native[i];
  1429. }
  1430. }
  1431. /* make sure that we have managed to get equivalent dts/avcodec channel
  1432. * masks in some sense -- unfortunately some channels could overlap */
  1433. if (av_popcount(channel_mask) != av_popcount(channel_layout)) {
  1434. av_log(avctx, AV_LOG_DEBUG,
  1435. "DTS-XXCH: Inconsistent avcodec/dts channel layouts\n");
  1436. return AVERROR_INVALIDDATA;
  1437. }
  1438. avctx->channel_layout = channel_layout;
  1439. if (!(avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE)) {
  1440. /* Estimate DTS --> avcodec ordering table */
  1441. for (chset = -1, j = 0; chset < s->xxch_chset; ++chset) {
  1442. mask = chset >= 0 ? s->xxch_spk_masks[chset]
  1443. : s->xxch_core_spkmask;
  1444. for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
  1445. if (mask & ~(DCA_XXCH_LFE1 | DCA_XXCH_LFE2) & (1 << i)) {
  1446. lavc = ff_dca_map_xxch_to_native[i];
  1447. posn = av_popcount(channel_layout & (lavc - 1));
  1448. s->xxch_order_tab[j++] = posn;
  1449. }
  1450. }
  1451. }
  1452. s->lfe_index = av_popcount(channel_layout & (AV_CH_LOW_FREQUENCY-1));
  1453. } else { /* native ordering */
  1454. for (i = 0; i < *channels; i++)
  1455. s->xxch_order_tab[i] = i;
  1456. s->lfe_index = *channels - 1;
  1457. }
  1458. s->channel_order_tab = s->xxch_order_tab;
  1459. }
  1460. return 0;
  1461. }
  1462. /**
  1463. * Main frame decoding function
  1464. * FIXME add arguments
  1465. */
  1466. static int dca_decode_frame(AVCodecContext *avctx, void *data,
  1467. int *got_frame_ptr, AVPacket *avpkt)
  1468. {
  1469. AVFrame *frame = data;
  1470. const uint8_t *buf = avpkt->data;
  1471. int buf_size = avpkt->size;
  1472. int lfe_samples;
  1473. int num_core_channels = 0;
  1474. int i, ret;
  1475. float **samples_flt;
  1476. float *src_chan;
  1477. float *dst_chan;
  1478. DCAContext *s = avctx->priv_data;
  1479. int channels, full_channels;
  1480. float scale;
  1481. int achan;
  1482. int chset;
  1483. int mask;
  1484. int j, k;
  1485. int endch;
  1486. int upsample = 0;
  1487. s->exss_ext_mask = 0;
  1488. s->xch_present = 0;
  1489. s->dca_buffer_size = AVERROR_INVALIDDATA;
  1490. for (i = 0; i < buf_size - 3 && s->dca_buffer_size == AVERROR_INVALIDDATA; i++)
  1491. s->dca_buffer_size = avpriv_dca_convert_bitstream(buf + i, buf_size - i, s->dca_buffer,
  1492. DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
  1493. if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
  1494. av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
  1495. return AVERROR_INVALIDDATA;
  1496. }
  1497. if ((ret = dca_parse_frame_header(s)) < 0) {
  1498. // seems like the frame is corrupt, try with the next one
  1499. return ret;
  1500. }
  1501. // set AVCodec values with parsed data
  1502. avctx->sample_rate = s->sample_rate;
  1503. s->profile = FF_PROFILE_DTS;
  1504. for (i = 0; i < (s->sample_blocks / SAMPLES_PER_SUBBAND); i++) {
  1505. if ((ret = dca_decode_block(s, 0, i))) {
  1506. av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
  1507. return ret;
  1508. }
  1509. }
  1510. /* record number of core channels incase less than max channels are requested */
  1511. num_core_channels = s->audio_header.prim_channels;
  1512. if (s->audio_header.prim_channels + !!s->lfe > 2 &&
  1513. avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
  1514. /* Stereo downmix coefficients
  1515. *
  1516. * The decoder can only downmix to 2-channel, so we need to ensure
  1517. * embedded downmix coefficients are actually targeting 2-channel.
  1518. */
  1519. if (s->core_downmix && (s->core_downmix_amode == DCA_STEREO ||
  1520. s->core_downmix_amode == DCA_STEREO_TOTAL)) {
  1521. for (i = 0; i < num_core_channels + !!s->lfe; i++) {
  1522. /* Range checked earlier */
  1523. s->downmix_coef[i][0] = dca_dmix_code(s->core_downmix_codes[i][0]);
  1524. s->downmix_coef[i][1] = dca_dmix_code(s->core_downmix_codes[i][1]);
  1525. }
  1526. s->output = s->core_downmix_amode;
  1527. } else {
  1528. int am = s->amode & DCA_CHANNEL_MASK;
  1529. if (am >= FF_ARRAY_ELEMS(ff_dca_default_coeffs)) {
  1530. av_log(s->avctx, AV_LOG_ERROR,
  1531. "Invalid channel mode %d\n", am);
  1532. return AVERROR_INVALIDDATA;
  1533. }
  1534. if (num_core_channels + !!s->lfe >
  1535. FF_ARRAY_ELEMS(ff_dca_default_coeffs[0])) {
  1536. avpriv_request_sample(s->avctx, "Downmixing %d channels",
  1537. s->audio_header.prim_channels + !!s->lfe);
  1538. return AVERROR_PATCHWELCOME;
  1539. }
  1540. for (i = 0; i < num_core_channels + !!s->lfe; i++) {
  1541. s->downmix_coef[i][0] = ff_dca_default_coeffs[am][i][0];
  1542. s->downmix_coef[i][1] = ff_dca_default_coeffs[am][i][1];
  1543. }
  1544. }
  1545. ff_dlog(s->avctx, "Stereo downmix coeffs:\n");
  1546. for (i = 0; i < num_core_channels + !!s->lfe; i++) {
  1547. ff_dlog(s->avctx, "L, input channel %d = %f\n", i,
  1548. s->downmix_coef[i][0]);
  1549. ff_dlog(s->avctx, "R, input channel %d = %f\n", i,
  1550. s->downmix_coef[i][1]);
  1551. }
  1552. ff_dlog(s->avctx, "\n");
  1553. }
  1554. if (s->ext_coding)
  1555. s->core_ext_mask = ff_dca_ext_audio_descr_mask[s->ext_descr];
  1556. else
  1557. s->core_ext_mask = 0;
  1558. ret = scan_for_extensions(avctx);
  1559. avctx->profile = s->profile;
  1560. full_channels = channels = s->audio_header.prim_channels + !!s->lfe;
  1561. ret = set_channel_layout(avctx, &channels, num_core_channels);
  1562. if (ret < 0)
  1563. return ret;
  1564. /* get output buffer */
  1565. frame->nb_samples = 256 * (s->sample_blocks / SAMPLES_PER_SUBBAND);
  1566. if (s->exss_ext_mask & DCA_EXT_EXSS_XLL) {
  1567. int xll_nb_samples = s->xll_segments * s->xll_smpl_in_seg;
  1568. /* Check for invalid/unsupported conditions first */
  1569. if (s->xll_residual_channels > channels) {
  1570. av_log(s->avctx, AV_LOG_WARNING,
  1571. "DCA: too many residual channels (%d, core channels %d). Disabling XLL\n",
  1572. s->xll_residual_channels, channels);
  1573. s->exss_ext_mask &= ~DCA_EXT_EXSS_XLL;
  1574. } else if (xll_nb_samples != frame->nb_samples &&
  1575. 2 * frame->nb_samples != xll_nb_samples) {
  1576. av_log(s->avctx, AV_LOG_WARNING,
  1577. "DCA: unsupported upsampling (%d XLL samples, %d core samples). Disabling XLL\n",
  1578. xll_nb_samples, frame->nb_samples);
  1579. s->exss_ext_mask &= ~DCA_EXT_EXSS_XLL;
  1580. } else {
  1581. if (2 * frame->nb_samples == xll_nb_samples) {
  1582. av_log(s->avctx, AV_LOG_INFO,
  1583. "XLL: upsampling core channels by a factor of 2\n");
  1584. upsample = 1;
  1585. frame->nb_samples = xll_nb_samples;
  1586. // FIXME: Is it good enough to copy from the first channel set?
  1587. avctx->sample_rate = s->xll_chsets[0].sampling_frequency;
  1588. }
  1589. /* If downmixing to stereo, don't decode additional channels.
  1590. * FIXME: Using the xch_disable flag for this doesn't seem right. */
  1591. if (!s->xch_disable)
  1592. channels = s->xll_channels;
  1593. }
  1594. }
  1595. if (avctx->channels != channels) {
  1596. if (avctx->channels)
  1597. av_log(avctx, AV_LOG_INFO, "Number of channels changed in DCA decoder (%d -> %d)\n", avctx->channels, channels);
  1598. avctx->channels = channels;
  1599. }
  1600. /* FIXME: This is an ugly hack, to just revert to the default
  1601. * layout if we have additional channels. Need to convert the XLL
  1602. * channel masks to ffmpeg channel_layout mask. */
  1603. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels)
  1604. avctx->channel_layout = 0;
  1605. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  1606. return ret;
  1607. samples_flt = (float **) frame->extended_data;
  1608. /* allocate buffer for extra channels if downmixing */
  1609. if (avctx->channels < full_channels) {
  1610. ret = av_samples_get_buffer_size(NULL, full_channels - channels,
  1611. frame->nb_samples,
  1612. avctx->sample_fmt, 0);
  1613. if (ret < 0)
  1614. return ret;
  1615. av_fast_malloc(&s->extra_channels_buffer,
  1616. &s->extra_channels_buffer_size, ret);
  1617. if (!s->extra_channels_buffer)
  1618. return AVERROR(ENOMEM);
  1619. ret = av_samples_fill_arrays((uint8_t **) s->extra_channels, NULL,
  1620. s->extra_channels_buffer,
  1621. full_channels - channels,
  1622. frame->nb_samples, avctx->sample_fmt, 0);
  1623. if (ret < 0)
  1624. return ret;
  1625. }
  1626. /* filter to get final output */
  1627. for (i = 0; i < (s->sample_blocks / SAMPLES_PER_SUBBAND); i++) {
  1628. int ch;
  1629. unsigned block = upsample ? 512 : 256;
  1630. for (ch = 0; ch < channels; ch++)
  1631. s->samples_chanptr[ch] = samples_flt[ch] + i * block;
  1632. for (; ch < full_channels; ch++)
  1633. s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * block;
  1634. dca_filter_channels(s, i, upsample);
  1635. /* If this was marked as a DTS-ES stream we need to subtract back- */
  1636. /* channel from SL & SR to remove matrixed back-channel signal */
  1637. if ((s->source_pcm_res & 1) && s->xch_present) {
  1638. float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
  1639. float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
  1640. float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
  1641. s->fdsp->vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
  1642. s->fdsp->vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
  1643. }
  1644. /* If stream contains XXCH, we might need to undo an embedded downmix */
  1645. if (s->xxch_dmix_embedded) {
  1646. /* Loop over channel sets in turn */
  1647. ch = num_core_channels;
  1648. for (chset = 0; chset < s->xxch_chset; chset++) {
  1649. endch = ch + s->xxch_chset_nch[chset];
  1650. mask = s->xxch_dmix_embedded;
  1651. /* undo downmix */
  1652. for (j = ch; j < endch; j++) {
  1653. if (mask & (1 << j)) { /* this channel has been mixed-out */
  1654. src_chan = s->samples_chanptr[s->channel_order_tab[j]];
  1655. for (k = 0; k < endch; k++) {
  1656. achan = s->channel_order_tab[k];
  1657. scale = s->xxch_dmix_coeff[j][k];
  1658. if (scale != 0.0) {
  1659. dst_chan = s->samples_chanptr[achan];
  1660. s->fdsp->vector_fmac_scalar(dst_chan, src_chan,
  1661. -scale, 256);
  1662. }
  1663. }
  1664. }
  1665. }
  1666. /* if a downmix has been embedded then undo the pre-scaling */
  1667. if ((mask & (1 << ch)) && s->xxch_dmix_sf[chset] != 1.0f) {
  1668. scale = s->xxch_dmix_sf[chset];
  1669. for (j = 0; j < ch; j++) {
  1670. src_chan = s->samples_chanptr[s->channel_order_tab[j]];
  1671. for (k = 0; k < 256; k++)
  1672. src_chan[k] *= scale;
  1673. }
  1674. /* LFE channel is always part of core, scale if it exists */
  1675. if (s->lfe) {
  1676. src_chan = s->samples_chanptr[s->lfe_index];
  1677. for (k = 0; k < 256; k++)
  1678. src_chan[k] *= scale;
  1679. }
  1680. }
  1681. ch = endch;
  1682. }
  1683. }
  1684. }
  1685. /* update lfe history */
  1686. lfe_samples = 2 * s->lfe * (s->sample_blocks / SAMPLES_PER_SUBBAND);
  1687. for (i = 0; i < 2 * s->lfe * 4; i++)
  1688. s->lfe_data[i] = s->lfe_data[i + lfe_samples];
  1689. if (s->exss_ext_mask & DCA_EXT_EXSS_XLL) {
  1690. ret = ff_dca_xll_decode_audio(s, frame);
  1691. if (ret < 0)
  1692. return ret;
  1693. }
  1694. /* AVMatrixEncoding
  1695. *
  1696. * DCA_STEREO_TOTAL (Lt/Rt) is equivalent to Dolby Surround */
  1697. ret = ff_side_data_update_matrix_encoding(frame,
  1698. (s->output & ~DCA_LFE) == DCA_STEREO_TOTAL ?
  1699. AV_MATRIX_ENCODING_DOLBY : AV_MATRIX_ENCODING_NONE);
  1700. if (ret < 0)
  1701. return ret;
  1702. if ( avctx->profile != FF_PROFILE_DTS_HD_MA
  1703. && avctx->profile != FF_PROFILE_DTS_HD_HRA)
  1704. avctx->bit_rate = s->bit_rate;
  1705. *got_frame_ptr = 1;
  1706. return buf_size;
  1707. }
  1708. /**
  1709. * DCA initialization
  1710. *
  1711. * @param avctx pointer to the AVCodecContext
  1712. */
  1713. static av_cold int dca_decode_init(AVCodecContext *avctx)
  1714. {
  1715. DCAContext *s = avctx->priv_data;
  1716. s->avctx = avctx;
  1717. dca_init_vlcs();
  1718. s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
  1719. if (!s->fdsp)
  1720. return AVERROR(ENOMEM);
  1721. ff_mdct_init(&s->imdct, 6, 1, 1.0);
  1722. ff_synth_filter_init(&s->synth);
  1723. ff_dcadsp_init(&s->dcadsp);
  1724. ff_fmt_convert_init(&s->fmt_conv, avctx);
  1725. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  1726. /* allow downmixing to stereo */
  1727. if (avctx->channels > 2 &&
  1728. avctx->request_channel_layout == AV_CH_LAYOUT_STEREO)
  1729. avctx->channels = 2;
  1730. return 0;
  1731. }
  1732. static av_cold int dca_decode_end(AVCodecContext *avctx)
  1733. {
  1734. DCAContext *s = avctx->priv_data;
  1735. ff_mdct_end(&s->imdct);
  1736. av_freep(&s->extra_channels_buffer);
  1737. av_freep(&s->fdsp);
  1738. av_freep(&s->xll_sample_buf);
  1739. av_freep(&s->qmf64_table);
  1740. return 0;
  1741. }
  1742. static const AVOption options[] = {
  1743. { "disable_xch", "disable decoding of the XCh extension", offsetof(DCAContext, xch_disable), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM },
  1744. { "disable_xll", "disable decoding of the XLL extension", offsetof(DCAContext, xll_disable), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM },
  1745. { NULL },
  1746. };
  1747. static const AVClass dca_decoder_class = {
  1748. .class_name = "DCA decoder",
  1749. .item_name = av_default_item_name,
  1750. .option = options,
  1751. .version = LIBAVUTIL_VERSION_INT,
  1752. .category = AV_CLASS_CATEGORY_DECODER,
  1753. };
  1754. AVCodec ff_dca_decoder = {
  1755. .name = "dca",
  1756. .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
  1757. .type = AVMEDIA_TYPE_AUDIO,
  1758. .id = AV_CODEC_ID_DTS,
  1759. .priv_data_size = sizeof(DCAContext),
  1760. .init = dca_decode_init,
  1761. .decode = dca_decode_frame,
  1762. .close = dca_decode_end,
  1763. .capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
  1764. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
  1765. AV_SAMPLE_FMT_NONE },
  1766. .profiles = NULL_IF_CONFIG_SMALL(ff_dca_profiles),
  1767. .priv_class = &dca_decoder_class,
  1768. };