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.

1840 lines
56KB

  1. /*
  2. * Copyright (C) 2016 foo86
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #define BITSTREAM_READER_LE
  21. #include "libavutil/channel_layout.h"
  22. #include "libavutil/mem_internal.h"
  23. #include "dcadec.h"
  24. #include "dcadata.h"
  25. #include "dcahuff.h"
  26. #include "dca_syncwords.h"
  27. #include "bytestream.h"
  28. #define AMP_MAX 56
  29. enum LBRFlags {
  30. LBR_FLAG_24_BIT = 0x01,
  31. LBR_FLAG_LFE_PRESENT = 0x02,
  32. LBR_FLAG_BAND_LIMIT_2_3 = 0x04,
  33. LBR_FLAG_BAND_LIMIT_1_2 = 0x08,
  34. LBR_FLAG_BAND_LIMIT_1_3 = 0x0c,
  35. LBR_FLAG_BAND_LIMIT_1_4 = 0x10,
  36. LBR_FLAG_BAND_LIMIT_1_8 = 0x18,
  37. LBR_FLAG_BAND_LIMIT_NONE = 0x14,
  38. LBR_FLAG_BAND_LIMIT_MASK = 0x1c,
  39. LBR_FLAG_DMIX_STEREO = 0x20,
  40. LBR_FLAG_DMIX_MULTI_CH = 0x40
  41. };
  42. enum LBRChunkTypes {
  43. LBR_CHUNK_NULL = 0x00,
  44. LBR_CHUNK_PAD = 0x01,
  45. LBR_CHUNK_FRAME = 0x04,
  46. LBR_CHUNK_FRAME_NO_CSUM = 0x06,
  47. LBR_CHUNK_LFE = 0x0a,
  48. LBR_CHUNK_ECS = 0x0b,
  49. LBR_CHUNK_RESERVED_1 = 0x0c,
  50. LBR_CHUNK_RESERVED_2 = 0x0d,
  51. LBR_CHUNK_SCF = 0x0e,
  52. LBR_CHUNK_TONAL = 0x10,
  53. LBR_CHUNK_TONAL_GRP_1 = 0x11,
  54. LBR_CHUNK_TONAL_GRP_2 = 0x12,
  55. LBR_CHUNK_TONAL_GRP_3 = 0x13,
  56. LBR_CHUNK_TONAL_GRP_4 = 0x14,
  57. LBR_CHUNK_TONAL_GRP_5 = 0x15,
  58. LBR_CHUNK_TONAL_SCF = 0x16,
  59. LBR_CHUNK_TONAL_SCF_GRP_1 = 0x17,
  60. LBR_CHUNK_TONAL_SCF_GRP_2 = 0x18,
  61. LBR_CHUNK_TONAL_SCF_GRP_3 = 0x19,
  62. LBR_CHUNK_TONAL_SCF_GRP_4 = 0x1a,
  63. LBR_CHUNK_TONAL_SCF_GRP_5 = 0x1b,
  64. LBR_CHUNK_RES_GRID_LR = 0x30,
  65. LBR_CHUNK_RES_GRID_LR_LAST = 0x3f,
  66. LBR_CHUNK_RES_GRID_HR = 0x40,
  67. LBR_CHUNK_RES_GRID_HR_LAST = 0x4f,
  68. LBR_CHUNK_RES_TS_1 = 0x50,
  69. LBR_CHUNK_RES_TS_1_LAST = 0x5f,
  70. LBR_CHUNK_RES_TS_2 = 0x60,
  71. LBR_CHUNK_RES_TS_2_LAST = 0x6f,
  72. LBR_CHUNK_EXTENSION = 0x7f
  73. };
  74. typedef struct LBRChunk {
  75. int id, len;
  76. const uint8_t *data;
  77. } LBRChunk;
  78. static const int8_t channel_reorder_nolfe[7][5] = {
  79. { 0, -1, -1, -1, -1 }, // C
  80. { 0, 1, -1, -1, -1 }, // LR
  81. { 0, 1, 2, -1, -1 }, // LR C
  82. { 0, 1, -1, -1, -1 }, // LsRs
  83. { 1, 2, 0, -1, -1 }, // LsRs C
  84. { 0, 1, 2, 3, -1 }, // LR LsRs
  85. { 0, 1, 3, 4, 2 }, // LR LsRs C
  86. };
  87. static const int8_t channel_reorder_lfe[7][5] = {
  88. { 0, -1, -1, -1, -1 }, // C
  89. { 0, 1, -1, -1, -1 }, // LR
  90. { 0, 1, 2, -1, -1 }, // LR C
  91. { 1, 2, -1, -1, -1 }, // LsRs
  92. { 2, 3, 0, -1, -1 }, // LsRs C
  93. { 0, 1, 3, 4, -1 }, // LR LsRs
  94. { 0, 1, 4, 5, 2 }, // LR LsRs C
  95. };
  96. static const uint8_t lfe_index[7] = {
  97. 1, 2, 3, 0, 1, 2, 3
  98. };
  99. static const uint8_t channel_counts[7] = {
  100. 1, 2, 3, 2, 3, 4, 5
  101. };
  102. static const uint16_t channel_layouts[7] = {
  103. AV_CH_LAYOUT_MONO,
  104. AV_CH_LAYOUT_STEREO,
  105. AV_CH_LAYOUT_SURROUND,
  106. AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT,
  107. AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT,
  108. AV_CH_LAYOUT_2_2,
  109. AV_CH_LAYOUT_5POINT0
  110. };
  111. static float cos_tab[256];
  112. static float lpc_tab[16];
  113. static av_cold void init_tables(void)
  114. {
  115. static int initialized;
  116. int i;
  117. if (initialized)
  118. return;
  119. for (i = 0; i < 256; i++)
  120. cos_tab[i] = cos(M_PI * i / 128);
  121. for (i = 0; i < 16; i++)
  122. lpc_tab[i] = sin((i - 8) * (M_PI / ((i < 8) ? 17 : 15)));
  123. initialized = 1;
  124. }
  125. static int parse_lfe_24(DCALbrDecoder *s)
  126. {
  127. int step_max = FF_ARRAY_ELEMS(ff_dca_lfe_step_size_24) - 1;
  128. int i, ps, si, code, step_i;
  129. float step, value, delta;
  130. ps = get_bits(&s->gb, 24);
  131. si = ps >> 23;
  132. value = (((ps & 0x7fffff) ^ -si) + si) * (1.0f / 0x7fffff);
  133. step_i = get_bits(&s->gb, 8);
  134. if (step_i > step_max) {
  135. av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE step size index\n");
  136. return AVERROR_INVALIDDATA;
  137. }
  138. step = ff_dca_lfe_step_size_24[step_i];
  139. for (i = 0; i < 64; i++) {
  140. code = get_bits(&s->gb, 6);
  141. delta = step * 0.03125f;
  142. if (code & 16)
  143. delta += step;
  144. if (code & 8)
  145. delta += step * 0.5f;
  146. if (code & 4)
  147. delta += step * 0.25f;
  148. if (code & 2)
  149. delta += step * 0.125f;
  150. if (code & 1)
  151. delta += step * 0.0625f;
  152. if (code & 32) {
  153. value -= delta;
  154. if (value < -3.0f)
  155. value = -3.0f;
  156. } else {
  157. value += delta;
  158. if (value > 3.0f)
  159. value = 3.0f;
  160. }
  161. step_i += ff_dca_lfe_delta_index_24[code & 31];
  162. step_i = av_clip(step_i, 0, step_max);
  163. step = ff_dca_lfe_step_size_24[step_i];
  164. s->lfe_data[i] = value * s->lfe_scale;
  165. }
  166. return 0;
  167. }
  168. static int parse_lfe_16(DCALbrDecoder *s)
  169. {
  170. int step_max = FF_ARRAY_ELEMS(ff_dca_lfe_step_size_16) - 1;
  171. int i, ps, si, code, step_i;
  172. float step, value, delta;
  173. ps = get_bits(&s->gb, 16);
  174. si = ps >> 15;
  175. value = (((ps & 0x7fff) ^ -si) + si) * (1.0f / 0x7fff);
  176. step_i = get_bits(&s->gb, 8);
  177. if (step_i > step_max) {
  178. av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE step size index\n");
  179. return AVERROR_INVALIDDATA;
  180. }
  181. step = ff_dca_lfe_step_size_16[step_i];
  182. for (i = 0; i < 64; i++) {
  183. code = get_bits(&s->gb, 4);
  184. delta = step * 0.125f;
  185. if (code & 4)
  186. delta += step;
  187. if (code & 2)
  188. delta += step * 0.5f;
  189. if (code & 1)
  190. delta += step * 0.25f;
  191. if (code & 8) {
  192. value -= delta;
  193. if (value < -3.0f)
  194. value = -3.0f;
  195. } else {
  196. value += delta;
  197. if (value > 3.0f)
  198. value = 3.0f;
  199. }
  200. step_i += ff_dca_lfe_delta_index_16[code & 7];
  201. step_i = av_clip(step_i, 0, step_max);
  202. step = ff_dca_lfe_step_size_16[step_i];
  203. s->lfe_data[i] = value * s->lfe_scale;
  204. }
  205. return 0;
  206. }
  207. static int parse_lfe_chunk(DCALbrDecoder *s, LBRChunk *chunk)
  208. {
  209. int ret;
  210. if (!(s->flags & LBR_FLAG_LFE_PRESENT))
  211. return 0;
  212. if (!chunk->len)
  213. return 0;
  214. ret = init_get_bits8(&s->gb, chunk->data, chunk->len);
  215. if (ret < 0)
  216. return ret;
  217. // Determine bit depth from chunk size
  218. if (chunk->len >= 52)
  219. return parse_lfe_24(s);
  220. if (chunk->len >= 35)
  221. return parse_lfe_16(s);
  222. av_log(s->avctx, AV_LOG_ERROR, "LFE chunk too short\n");
  223. return AVERROR_INVALIDDATA;
  224. }
  225. static inline int parse_vlc(GetBitContext *s, VLC *vlc, int max_depth)
  226. {
  227. int v = get_vlc2(s, vlc->table, vlc->bits, max_depth);
  228. if (v > 0)
  229. return v - 1;
  230. // Rare value
  231. return get_bits(s, get_bits(s, 3) + 1);
  232. }
  233. static int parse_tonal(DCALbrDecoder *s, int group)
  234. {
  235. unsigned int amp[DCA_LBR_CHANNELS_TOTAL];
  236. unsigned int phs[DCA_LBR_CHANNELS_TOTAL];
  237. unsigned int diff, main_amp, shift;
  238. int sf, sf_idx, ch, main_ch, freq;
  239. int ch_nbits = av_ceil_log2(s->nchannels_total);
  240. // Parse subframes for this group
  241. for (sf = 0; sf < 1 << group; sf += diff ? 8 : 1) {
  242. sf_idx = ((s->framenum << group) + sf) & 31;
  243. s->tonal_bounds[group][sf_idx][0] = s->ntones;
  244. // Parse tones for this subframe
  245. for (freq = 1;; freq++) {
  246. if (get_bits_left(&s->gb) < 1) {
  247. av_log(s->avctx, AV_LOG_ERROR, "Tonal group chunk too short\n");
  248. return AVERROR_INVALIDDATA;
  249. }
  250. diff = parse_vlc(&s->gb, &ff_dca_vlc_tnl_grp[group], 2);
  251. if (diff >= FF_ARRAY_ELEMS(ff_dca_fst_amp)) {
  252. av_log(s->avctx, AV_LOG_ERROR, "Invalid tonal frequency diff\n");
  253. return AVERROR_INVALIDDATA;
  254. }
  255. diff = get_bitsz(&s->gb, diff >> 2) + ff_dca_fst_amp[diff];
  256. if (diff <= 1)
  257. break; // End of subframe
  258. freq += diff - 2;
  259. if (freq >> (5 - group) > s->nsubbands * 4 - 6) {
  260. av_log(s->avctx, AV_LOG_ERROR, "Invalid spectral line offset\n");
  261. return AVERROR_INVALIDDATA;
  262. }
  263. // Main channel
  264. main_ch = get_bitsz(&s->gb, ch_nbits);
  265. main_amp = parse_vlc(&s->gb, &ff_dca_vlc_tnl_scf, 2)
  266. + s->tonal_scf[ff_dca_freq_to_sb[freq >> (7 - group)]]
  267. + s->limited_range - 2;
  268. amp[main_ch] = main_amp < AMP_MAX ? main_amp : 0;
  269. phs[main_ch] = get_bits(&s->gb, 3);
  270. // Secondary channels
  271. for (ch = 0; ch < s->nchannels_total; ch++) {
  272. if (ch == main_ch)
  273. continue;
  274. if (get_bits1(&s->gb)) {
  275. amp[ch] = amp[main_ch] - parse_vlc(&s->gb, &ff_dca_vlc_damp, 1);
  276. phs[ch] = phs[main_ch] - parse_vlc(&s->gb, &ff_dca_vlc_dph, 1);
  277. } else {
  278. amp[ch] = 0;
  279. phs[ch] = 0;
  280. }
  281. }
  282. if (amp[main_ch]) {
  283. // Allocate new tone
  284. DCALbrTone *t = &s->tones[s->ntones];
  285. s->ntones = (s->ntones + 1) & (DCA_LBR_TONES - 1);
  286. t->x_freq = freq >> (5 - group);
  287. t->f_delt = (freq & ((1 << (5 - group)) - 1)) << group;
  288. t->ph_rot = 256 - (t->x_freq & 1) * 128 - t->f_delt * 4;
  289. shift = ff_dca_ph0_shift[(t->x_freq & 3) * 2 + (freq & 1)]
  290. - ((t->ph_rot << (5 - group)) - t->ph_rot);
  291. for (ch = 0; ch < s->nchannels; ch++) {
  292. t->amp[ch] = amp[ch] < AMP_MAX ? amp[ch] : 0;
  293. t->phs[ch] = 128 - phs[ch] * 32 + shift;
  294. }
  295. }
  296. }
  297. s->tonal_bounds[group][sf_idx][1] = s->ntones;
  298. }
  299. return 0;
  300. }
  301. static int parse_tonal_chunk(DCALbrDecoder *s, LBRChunk *chunk)
  302. {
  303. int sb, group, ret;
  304. if (!chunk->len)
  305. return 0;
  306. ret = init_get_bits8(&s->gb, chunk->data, chunk->len);
  307. if (ret < 0)
  308. return ret;
  309. // Scale factors
  310. if (chunk->id == LBR_CHUNK_SCF || chunk->id == LBR_CHUNK_TONAL_SCF) {
  311. if (get_bits_left(&s->gb) < 36) {
  312. av_log(s->avctx, AV_LOG_ERROR, "Tonal scale factor chunk too short\n");
  313. return AVERROR_INVALIDDATA;
  314. }
  315. for (sb = 0; sb < 6; sb++)
  316. s->tonal_scf[sb] = get_bits(&s->gb, 6);
  317. }
  318. // Tonal groups
  319. if (chunk->id == LBR_CHUNK_TONAL || chunk->id == LBR_CHUNK_TONAL_SCF)
  320. for (group = 0; group < 5; group++) {
  321. ret = parse_tonal(s, group);
  322. if (ret < 0)
  323. return ret;
  324. }
  325. return 0;
  326. }
  327. static int parse_tonal_group(DCALbrDecoder *s, LBRChunk *chunk)
  328. {
  329. int ret;
  330. if (!chunk->len)
  331. return 0;
  332. ret = init_get_bits8(&s->gb, chunk->data, chunk->len);
  333. if (ret < 0)
  334. return ret;
  335. return parse_tonal(s, chunk->id);
  336. }
  337. /**
  338. * Check point to ensure that enough bits are left. Aborts decoding
  339. * by skipping to the end of chunk otherwise.
  340. */
  341. static int ensure_bits(GetBitContext *s, int n)
  342. {
  343. int left = get_bits_left(s);
  344. if (left < 0)
  345. return AVERROR_INVALIDDATA;
  346. if (left < n) {
  347. skip_bits_long(s, left);
  348. return 1;
  349. }
  350. return 0;
  351. }
  352. static int parse_scale_factors(DCALbrDecoder *s, uint8_t *scf)
  353. {
  354. int i, sf, prev, next, dist;
  355. // Truncated scale factors remain zero
  356. if (ensure_bits(&s->gb, 20))
  357. return 0;
  358. // Initial scale factor
  359. prev = parse_vlc(&s->gb, &ff_dca_vlc_fst_rsd_amp, 2);
  360. for (sf = 0; sf < 7; sf += dist) {
  361. scf[sf] = prev; // Store previous value
  362. if (ensure_bits(&s->gb, 20))
  363. return 0;
  364. // Interpolation distance
  365. dist = parse_vlc(&s->gb, &ff_dca_vlc_rsd_apprx, 1) + 1;
  366. if (dist > 7 - sf) {
  367. av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor distance\n");
  368. return AVERROR_INVALIDDATA;
  369. }
  370. if (ensure_bits(&s->gb, 20))
  371. return 0;
  372. // Final interpolation point
  373. next = parse_vlc(&s->gb, &ff_dca_vlc_rsd_amp, 2);
  374. if (next & 1)
  375. next = prev + ((next + 1) >> 1);
  376. else
  377. next = prev - ( next >> 1);
  378. // Interpolate
  379. switch (dist) {
  380. case 2:
  381. if (next > prev)
  382. scf[sf + 1] = prev + ((next - prev) >> 1);
  383. else
  384. scf[sf + 1] = prev - ((prev - next) >> 1);
  385. break;
  386. case 4:
  387. if (next > prev) {
  388. scf[sf + 1] = prev + ( (next - prev) >> 2);
  389. scf[sf + 2] = prev + ( (next - prev) >> 1);
  390. scf[sf + 3] = prev + (((next - prev) * 3) >> 2);
  391. } else {
  392. scf[sf + 1] = prev - ( (prev - next) >> 2);
  393. scf[sf + 2] = prev - ( (prev - next) >> 1);
  394. scf[sf + 3] = prev - (((prev - next) * 3) >> 2);
  395. }
  396. break;
  397. default:
  398. for (i = 1; i < dist; i++)
  399. scf[sf + i] = prev + (next - prev) * i / dist;
  400. break;
  401. }
  402. prev = next;
  403. }
  404. scf[sf] = next; // Store final value
  405. return 0;
  406. }
  407. static int parse_st_code(GetBitContext *s, int min_v)
  408. {
  409. unsigned int v = parse_vlc(s, &ff_dca_vlc_st_grid, 2) + min_v;
  410. if (v & 1)
  411. v = 16 + (v >> 1);
  412. else
  413. v = 16 - (v >> 1);
  414. if (v >= FF_ARRAY_ELEMS(ff_dca_st_coeff))
  415. v = 16;
  416. return v;
  417. }
  418. static int parse_grid_1_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
  419. {
  420. int ch, sb, sf, nsubbands, ret;
  421. if (!chunk->len)
  422. return 0;
  423. ret = init_get_bits8(&s->gb, chunk->data, chunk->len);
  424. if (ret < 0)
  425. return ret;
  426. // Scale factors
  427. nsubbands = ff_dca_scf_to_grid_1[s->nsubbands - 1] + 1;
  428. for (sb = 2; sb < nsubbands; sb++) {
  429. ret = parse_scale_factors(s, s->grid_1_scf[ch1][sb]);
  430. if (ret < 0)
  431. return ret;
  432. if (ch1 != ch2 && ff_dca_grid_1_to_scf[sb] < s->min_mono_subband) {
  433. ret = parse_scale_factors(s, s->grid_1_scf[ch2][sb]);
  434. if (ret < 0)
  435. return ret;
  436. }
  437. }
  438. if (get_bits_left(&s->gb) < 1)
  439. return 0; // Should not happen, but a sample exists that proves otherwise
  440. // Average values for third grid
  441. for (sb = 0; sb < s->nsubbands - 4; sb++) {
  442. s->grid_3_avg[ch1][sb] = parse_vlc(&s->gb, &ff_dca_vlc_avg_g3, 2) - 16;
  443. if (ch1 != ch2) {
  444. if (sb + 4 < s->min_mono_subband)
  445. s->grid_3_avg[ch2][sb] = parse_vlc(&s->gb, &ff_dca_vlc_avg_g3, 2) - 16;
  446. else
  447. s->grid_3_avg[ch2][sb] = s->grid_3_avg[ch1][sb];
  448. }
  449. }
  450. if (get_bits_left(&s->gb) < 0) {
  451. av_log(s->avctx, AV_LOG_ERROR, "First grid chunk too short\n");
  452. return AVERROR_INVALIDDATA;
  453. }
  454. // Stereo image for partial mono mode
  455. if (ch1 != ch2) {
  456. int min_v[2];
  457. if (ensure_bits(&s->gb, 8))
  458. return 0;
  459. min_v[0] = get_bits(&s->gb, 4);
  460. min_v[1] = get_bits(&s->gb, 4);
  461. nsubbands = (s->nsubbands - s->min_mono_subband + 3) / 4;
  462. for (sb = 0; sb < nsubbands; sb++)
  463. for (ch = ch1; ch <= ch2; ch++)
  464. for (sf = 1; sf <= 4; sf++)
  465. s->part_stereo[ch][sb][sf] = parse_st_code(&s->gb, min_v[ch - ch1]);
  466. if (get_bits_left(&s->gb) >= 0)
  467. s->part_stereo_pres |= 1 << ch1;
  468. }
  469. // Low resolution spatial information is not decoded
  470. return 0;
  471. }
  472. static int parse_grid_1_sec_ch(DCALbrDecoder *s, int ch2)
  473. {
  474. int sb, nsubbands, ret;
  475. // Scale factors
  476. nsubbands = ff_dca_scf_to_grid_1[s->nsubbands - 1] + 1;
  477. for (sb = 2; sb < nsubbands; sb++) {
  478. if (ff_dca_grid_1_to_scf[sb] >= s->min_mono_subband) {
  479. ret = parse_scale_factors(s, s->grid_1_scf[ch2][sb]);
  480. if (ret < 0)
  481. return ret;
  482. }
  483. }
  484. // Average values for third grid
  485. for (sb = 0; sb < s->nsubbands - 4; sb++) {
  486. if (sb + 4 >= s->min_mono_subband) {
  487. if (ensure_bits(&s->gb, 20))
  488. return 0;
  489. s->grid_3_avg[ch2][sb] = parse_vlc(&s->gb, &ff_dca_vlc_avg_g3, 2) - 16;
  490. }
  491. }
  492. return 0;
  493. }
  494. static void parse_grid_3(DCALbrDecoder *s, int ch1, int ch2, int sb, int flag)
  495. {
  496. int i, ch;
  497. for (ch = ch1; ch <= ch2; ch++) {
  498. if ((ch != ch1 && sb + 4 >= s->min_mono_subband) != flag)
  499. continue;
  500. if (s->grid_3_pres[ch] & (1U << sb))
  501. continue; // Already parsed
  502. for (i = 0; i < 8; i++) {
  503. if (ensure_bits(&s->gb, 20))
  504. return;
  505. s->grid_3_scf[ch][sb][i] = parse_vlc(&s->gb, &ff_dca_vlc_grid_3, 2) - 16;
  506. }
  507. // Flag scale factors for this subband parsed
  508. s->grid_3_pres[ch] |= 1U << sb;
  509. }
  510. }
  511. static float lbr_rand(DCALbrDecoder *s, int sb)
  512. {
  513. s->lbr_rand = 1103515245U * s->lbr_rand + 12345U;
  514. return s->lbr_rand * s->sb_scf[sb];
  515. }
  516. /**
  517. * Parse time samples for one subband, filling truncated samples with randomness
  518. */
  519. static void parse_ch(DCALbrDecoder *s, int ch, int sb, int quant_level, int flag)
  520. {
  521. float *samples = s->time_samples[ch][sb];
  522. int i, j, code, nblocks, coding_method;
  523. if (ensure_bits(&s->gb, 20))
  524. return; // Too few bits left
  525. coding_method = get_bits1(&s->gb);
  526. switch (quant_level) {
  527. case 1:
  528. nblocks = FFMIN(get_bits_left(&s->gb) / 8, DCA_LBR_TIME_SAMPLES / 8);
  529. for (i = 0; i < nblocks; i++, samples += 8) {
  530. code = get_bits(&s->gb, 8);
  531. for (j = 0; j < 8; j++)
  532. samples[j] = ff_dca_rsd_level_2a[(code >> j) & 1];
  533. }
  534. i = nblocks * 8;
  535. break;
  536. case 2:
  537. if (coding_method) {
  538. for (i = 0; i < DCA_LBR_TIME_SAMPLES && get_bits_left(&s->gb) >= 2; i++) {
  539. if (get_bits1(&s->gb))
  540. samples[i] = ff_dca_rsd_level_2b[get_bits1(&s->gb)];
  541. else
  542. samples[i] = 0;
  543. }
  544. } else {
  545. nblocks = FFMIN(get_bits_left(&s->gb) / 8, (DCA_LBR_TIME_SAMPLES + 4) / 5);
  546. for (i = 0; i < nblocks; i++, samples += 5) {
  547. code = ff_dca_rsd_pack_5_in_8[get_bits(&s->gb, 8)];
  548. for (j = 0; j < 5; j++)
  549. samples[j] = ff_dca_rsd_level_3[(code >> j * 2) & 3];
  550. }
  551. i = nblocks * 5;
  552. }
  553. break;
  554. case 3:
  555. nblocks = FFMIN(get_bits_left(&s->gb) / 7, (DCA_LBR_TIME_SAMPLES + 2) / 3);
  556. for (i = 0; i < nblocks; i++, samples += 3) {
  557. code = get_bits(&s->gb, 7);
  558. for (j = 0; j < 3; j++)
  559. samples[j] = ff_dca_rsd_level_5[ff_dca_rsd_pack_3_in_7[code][j]];
  560. }
  561. i = nblocks * 3;
  562. break;
  563. case 4:
  564. for (i = 0; i < DCA_LBR_TIME_SAMPLES && get_bits_left(&s->gb) >= 6; i++)
  565. samples[i] = ff_dca_rsd_level_8[get_vlc2(&s->gb, ff_dca_vlc_rsd.table, 6, 1)];
  566. break;
  567. case 5:
  568. nblocks = FFMIN(get_bits_left(&s->gb) / 4, DCA_LBR_TIME_SAMPLES);
  569. for (i = 0; i < nblocks; i++)
  570. samples[i] = ff_dca_rsd_level_16[get_bits(&s->gb, 4)];
  571. break;
  572. default:
  573. av_assert0(0);
  574. }
  575. if (flag && get_bits_left(&s->gb) < 20)
  576. return; // Skip incomplete mono subband
  577. for (; i < DCA_LBR_TIME_SAMPLES; i++)
  578. s->time_samples[ch][sb][i] = lbr_rand(s, sb);
  579. s->ch_pres[ch] |= 1U << sb;
  580. }
  581. static int parse_ts(DCALbrDecoder *s, int ch1, int ch2,
  582. int start_sb, int end_sb, int flag)
  583. {
  584. int sb, sb_g3, sb_reorder, quant_level;
  585. for (sb = start_sb; sb < end_sb; sb++) {
  586. // Subband number before reordering
  587. if (sb < 6) {
  588. sb_reorder = sb;
  589. } else if (flag && sb < s->max_mono_subband) {
  590. sb_reorder = s->sb_indices[sb];
  591. } else {
  592. if (ensure_bits(&s->gb, 28))
  593. break;
  594. sb_reorder = get_bits(&s->gb, s->limited_range + 3);
  595. if (sb_reorder < 6)
  596. sb_reorder = 6;
  597. s->sb_indices[sb] = sb_reorder;
  598. }
  599. if (sb_reorder >= s->nsubbands)
  600. return AVERROR_INVALIDDATA;
  601. // Third grid scale factors
  602. if (sb == 12) {
  603. for (sb_g3 = 0; sb_g3 < s->g3_avg_only_start_sb - 4; sb_g3++)
  604. parse_grid_3(s, ch1, ch2, sb_g3, flag);
  605. } else if (sb < 12 && sb_reorder >= 4) {
  606. parse_grid_3(s, ch1, ch2, sb_reorder - 4, flag);
  607. }
  608. // Secondary channel flags
  609. if (ch1 != ch2) {
  610. if (ensure_bits(&s->gb, 20))
  611. break;
  612. if (!flag || sb_reorder >= s->max_mono_subband)
  613. s->sec_ch_sbms[ch1 / 2][sb_reorder] = get_bits(&s->gb, 8);
  614. if (flag && sb_reorder >= s->min_mono_subband)
  615. s->sec_ch_lrms[ch1 / 2][sb_reorder] = get_bits(&s->gb, 8);
  616. }
  617. quant_level = s->quant_levels[ch1 / 2][sb];
  618. if (!quant_level)
  619. return AVERROR_INVALIDDATA;
  620. // Time samples for one or both channels
  621. if (sb < s->max_mono_subband && sb_reorder >= s->min_mono_subband) {
  622. if (!flag)
  623. parse_ch(s, ch1, sb_reorder, quant_level, 0);
  624. else if (ch1 != ch2)
  625. parse_ch(s, ch2, sb_reorder, quant_level, 1);
  626. } else {
  627. parse_ch(s, ch1, sb_reorder, quant_level, 0);
  628. if (ch1 != ch2)
  629. parse_ch(s, ch2, sb_reorder, quant_level, 0);
  630. }
  631. }
  632. return 0;
  633. }
  634. /**
  635. * Convert from reflection coefficients to direct form coefficients
  636. */
  637. static void convert_lpc(float *coeff, const int *codes)
  638. {
  639. int i, j;
  640. for (i = 0; i < 8; i++) {
  641. float rc = lpc_tab[codes[i]];
  642. for (j = 0; j < (i + 1) / 2; j++) {
  643. float tmp1 = coeff[ j ];
  644. float tmp2 = coeff[i - j - 1];
  645. coeff[ j ] = tmp1 + rc * tmp2;
  646. coeff[i - j - 1] = tmp2 + rc * tmp1;
  647. }
  648. coeff[i] = rc;
  649. }
  650. }
  651. static int parse_lpc(DCALbrDecoder *s, int ch1, int ch2, int start_sb, int end_sb)
  652. {
  653. int f = s->framenum & 1;
  654. int i, sb, ch, codes[16];
  655. // First two subbands have two sets of coefficients, third subband has one
  656. for (sb = start_sb; sb < end_sb; sb++) {
  657. int ncodes = 8 * (1 + (sb < 2));
  658. for (ch = ch1; ch <= ch2; ch++) {
  659. if (ensure_bits(&s->gb, 4 * ncodes))
  660. return 0;
  661. for (i = 0; i < ncodes; i++)
  662. codes[i] = get_bits(&s->gb, 4);
  663. for (i = 0; i < ncodes / 8; i++)
  664. convert_lpc(s->lpc_coeff[f][ch][sb][i], &codes[i * 8]);
  665. }
  666. }
  667. return 0;
  668. }
  669. static int parse_high_res_grid(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
  670. {
  671. int quant_levels[DCA_LBR_SUBBANDS];
  672. int sb, ch, ol, st, max_sb, profile, ret;
  673. if (!chunk->len)
  674. return 0;
  675. ret = init_get_bits8(&s->gb, chunk->data, chunk->len);
  676. if (ret < 0)
  677. return ret;
  678. // Quantizer profile
  679. profile = get_bits(&s->gb, 8);
  680. // Overall level
  681. ol = (profile >> 3) & 7;
  682. // Steepness
  683. st = profile >> 6;
  684. // Max energy subband
  685. max_sb = profile & 7;
  686. // Calculate quantization levels
  687. for (sb = 0; sb < s->nsubbands; sb++) {
  688. int f = sb * s->limited_rate / s->nsubbands;
  689. int a = 18000 / (12 * f / 1000 + 100 + 40 * st) + 20 * ol;
  690. if (a <= 95)
  691. quant_levels[sb] = 1;
  692. else if (a <= 140)
  693. quant_levels[sb] = 2;
  694. else if (a <= 180)
  695. quant_levels[sb] = 3;
  696. else if (a <= 230)
  697. quant_levels[sb] = 4;
  698. else
  699. quant_levels[sb] = 5;
  700. }
  701. // Reorder quantization levels for lower subbands
  702. for (sb = 0; sb < 8; sb++)
  703. s->quant_levels[ch1 / 2][sb] = quant_levels[ff_dca_sb_reorder[max_sb][sb]];
  704. for (; sb < s->nsubbands; sb++)
  705. s->quant_levels[ch1 / 2][sb] = quant_levels[sb];
  706. // LPC for the first two subbands
  707. ret = parse_lpc(s, ch1, ch2, 0, 2);
  708. if (ret < 0)
  709. return ret;
  710. // Time-samples for the first two subbands of main channel
  711. ret = parse_ts(s, ch1, ch2, 0, 2, 0);
  712. if (ret < 0)
  713. return ret;
  714. // First two bands of the first grid
  715. for (sb = 0; sb < 2; sb++)
  716. for (ch = ch1; ch <= ch2; ch++)
  717. if ((ret = parse_scale_factors(s, s->grid_1_scf[ch][sb])) < 0)
  718. return ret;
  719. return 0;
  720. }
  721. static int parse_grid_2(DCALbrDecoder *s, int ch1, int ch2,
  722. int start_sb, int end_sb, int flag)
  723. {
  724. int i, j, sb, ch, nsubbands;
  725. nsubbands = ff_dca_scf_to_grid_2[s->nsubbands - 1] + 1;
  726. if (end_sb > nsubbands)
  727. end_sb = nsubbands;
  728. for (sb = start_sb; sb < end_sb; sb++) {
  729. for (ch = ch1; ch <= ch2; ch++) {
  730. uint8_t *g2_scf = s->grid_2_scf[ch][sb];
  731. if ((ch != ch1 && ff_dca_grid_2_to_scf[sb] >= s->min_mono_subband) != flag) {
  732. if (!flag)
  733. memcpy(g2_scf, s->grid_2_scf[ch1][sb], 64);
  734. continue;
  735. }
  736. // Scale factors in groups of 8
  737. for (i = 0; i < 8; i++, g2_scf += 8) {
  738. if (get_bits_left(&s->gb) < 1) {
  739. memset(g2_scf, 0, 64 - i * 8);
  740. break;
  741. }
  742. // Bit indicating if whole group has zero values
  743. if (get_bits1(&s->gb)) {
  744. for (j = 0; j < 8; j++) {
  745. if (ensure_bits(&s->gb, 20))
  746. break;
  747. g2_scf[j] = parse_vlc(&s->gb, &ff_dca_vlc_grid_2, 2);
  748. }
  749. } else {
  750. memset(g2_scf, 0, 8);
  751. }
  752. }
  753. }
  754. }
  755. return 0;
  756. }
  757. static int parse_ts1_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
  758. {
  759. int ret;
  760. if (!chunk->len)
  761. return 0;
  762. if ((ret = init_get_bits8(&s->gb, chunk->data, chunk->len)) < 0)
  763. return ret;
  764. if ((ret = parse_lpc(s, ch1, ch2, 2, 3)) < 0)
  765. return ret;
  766. if ((ret = parse_ts(s, ch1, ch2, 2, 4, 0)) < 0)
  767. return ret;
  768. if ((ret = parse_grid_2(s, ch1, ch2, 0, 1, 0)) < 0)
  769. return ret;
  770. if ((ret = parse_ts(s, ch1, ch2, 4, 6, 0)) < 0)
  771. return ret;
  772. return 0;
  773. }
  774. static int parse_ts2_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
  775. {
  776. int ret;
  777. if (!chunk->len)
  778. return 0;
  779. if ((ret = init_get_bits8(&s->gb, chunk->data, chunk->len)) < 0)
  780. return ret;
  781. if ((ret = parse_grid_2(s, ch1, ch2, 1, 3, 0)) < 0)
  782. return ret;
  783. if ((ret = parse_ts(s, ch1, ch2, 6, s->max_mono_subband, 0)) < 0)
  784. return ret;
  785. if (ch1 != ch2) {
  786. if ((ret = parse_grid_1_sec_ch(s, ch2)) < 0)
  787. return ret;
  788. if ((ret = parse_grid_2(s, ch1, ch2, 0, 3, 1)) < 0)
  789. return ret;
  790. }
  791. if ((ret = parse_ts(s, ch1, ch2, s->min_mono_subband, s->nsubbands, 1)) < 0)
  792. return ret;
  793. return 0;
  794. }
  795. static int init_sample_rate(DCALbrDecoder *s)
  796. {
  797. double scale = (-1.0 / (1 << 17)) * sqrt(1 << (2 - s->limited_range));
  798. int i, br_per_ch = s->bit_rate_scaled / s->nchannels_total;
  799. int ret;
  800. ff_mdct_end(&s->imdct);
  801. ret = ff_mdct_init(&s->imdct, s->freq_range + 6, 1, scale);
  802. if (ret < 0)
  803. return ret;
  804. for (i = 0; i < 32 << s->freq_range; i++)
  805. s->window[i] = ff_dca_long_window[i << (2 - s->freq_range)];
  806. if (br_per_ch < 14000)
  807. scale = 0.85;
  808. else if (br_per_ch < 32000)
  809. scale = (br_per_ch - 14000) * (1.0 / 120000) + 0.85;
  810. else
  811. scale = 1.0;
  812. scale *= 1.0 / INT_MAX;
  813. for (i = 0; i < s->nsubbands; i++) {
  814. if (i < 2)
  815. s->sb_scf[i] = 0; // The first two subbands are always zero
  816. else if (i < 5)
  817. s->sb_scf[i] = (i - 1) * 0.25 * 0.785 * scale;
  818. else
  819. s->sb_scf[i] = 0.785 * scale;
  820. }
  821. s->lfe_scale = (16 << s->freq_range) * 0.0000078265894;
  822. return 0;
  823. }
  824. static int alloc_sample_buffer(DCALbrDecoder *s)
  825. {
  826. // Reserve space for history and padding
  827. int nchsamples = DCA_LBR_TIME_SAMPLES + DCA_LBR_TIME_HISTORY * 2;
  828. int nsamples = nchsamples * s->nchannels * s->nsubbands;
  829. int ch, sb;
  830. float *ptr;
  831. // Reallocate time sample buffer
  832. av_fast_mallocz(&s->ts_buffer, &s->ts_size, nsamples * sizeof(float));
  833. if (!s->ts_buffer)
  834. return AVERROR(ENOMEM);
  835. ptr = s->ts_buffer + DCA_LBR_TIME_HISTORY;
  836. for (ch = 0; ch < s->nchannels; ch++) {
  837. for (sb = 0; sb < s->nsubbands; sb++) {
  838. s->time_samples[ch][sb] = ptr;
  839. ptr += nchsamples;
  840. }
  841. }
  842. return 0;
  843. }
  844. static int parse_decoder_init(DCALbrDecoder *s, GetByteContext *gb)
  845. {
  846. int old_rate = s->sample_rate;
  847. int old_band_limit = s->band_limit;
  848. int old_nchannels = s->nchannels;
  849. int version, bit_rate_hi;
  850. unsigned int sr_code;
  851. // Sample rate of LBR audio
  852. sr_code = bytestream2_get_byte(gb);
  853. if (sr_code >= FF_ARRAY_ELEMS(ff_dca_sampling_freqs)) {
  854. av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR sample rate\n");
  855. return AVERROR_INVALIDDATA;
  856. }
  857. s->sample_rate = ff_dca_sampling_freqs[sr_code];
  858. if (s->sample_rate > 48000) {
  859. avpriv_report_missing_feature(s->avctx, "%d Hz LBR sample rate", s->sample_rate);
  860. return AVERROR_PATCHWELCOME;
  861. }
  862. // LBR speaker mask
  863. s->ch_mask = bytestream2_get_le16(gb);
  864. if (!(s->ch_mask & 0x7)) {
  865. avpriv_report_missing_feature(s->avctx, "LBR channel mask %#x", s->ch_mask);
  866. return AVERROR_PATCHWELCOME;
  867. }
  868. if ((s->ch_mask & 0xfff0) && !(s->warned & 1)) {
  869. avpriv_report_missing_feature(s->avctx, "LBR channel mask %#x", s->ch_mask);
  870. s->warned |= 1;
  871. }
  872. // LBR bitstream version
  873. version = bytestream2_get_le16(gb);
  874. if ((version & 0xff00) != 0x0800) {
  875. avpriv_report_missing_feature(s->avctx, "LBR stream version %#x", version);
  876. return AVERROR_PATCHWELCOME;
  877. }
  878. // Flags for LBR decoder initialization
  879. s->flags = bytestream2_get_byte(gb);
  880. if (s->flags & LBR_FLAG_DMIX_MULTI_CH) {
  881. avpriv_report_missing_feature(s->avctx, "LBR multi-channel downmix");
  882. return AVERROR_PATCHWELCOME;
  883. }
  884. if ((s->flags & LBR_FLAG_LFE_PRESENT) && s->sample_rate != 48000) {
  885. if (!(s->warned & 2)) {
  886. avpriv_report_missing_feature(s->avctx, "%d Hz LFE interpolation", s->sample_rate);
  887. s->warned |= 2;
  888. }
  889. s->flags &= ~LBR_FLAG_LFE_PRESENT;
  890. }
  891. // Most significant bit rate nibbles
  892. bit_rate_hi = bytestream2_get_byte(gb);
  893. // Least significant original bit rate word
  894. s->bit_rate_orig = bytestream2_get_le16(gb) | ((bit_rate_hi & 0x0F) << 16);
  895. // Least significant scaled bit rate word
  896. s->bit_rate_scaled = bytestream2_get_le16(gb) | ((bit_rate_hi & 0xF0) << 12);
  897. // Setup number of fullband channels
  898. s->nchannels_total = ff_dca_count_chs_for_mask(s->ch_mask & ~DCA_SPEAKER_PAIR_LFE1);
  899. s->nchannels = FFMIN(s->nchannels_total, DCA_LBR_CHANNELS);
  900. // Setup band limit
  901. switch (s->flags & LBR_FLAG_BAND_LIMIT_MASK) {
  902. case LBR_FLAG_BAND_LIMIT_NONE:
  903. s->band_limit = 0;
  904. break;
  905. case LBR_FLAG_BAND_LIMIT_1_2:
  906. s->band_limit = 1;
  907. break;
  908. case LBR_FLAG_BAND_LIMIT_1_4:
  909. s->band_limit = 2;
  910. break;
  911. default:
  912. avpriv_report_missing_feature(s->avctx, "LBR band limit %#x", s->flags & LBR_FLAG_BAND_LIMIT_MASK);
  913. return AVERROR_PATCHWELCOME;
  914. }
  915. // Setup frequency range
  916. s->freq_range = ff_dca_freq_ranges[sr_code];
  917. // Setup resolution profile
  918. if (s->bit_rate_orig >= 44000 * (s->nchannels_total + 2))
  919. s->res_profile = 2;
  920. else if (s->bit_rate_orig >= 25000 * (s->nchannels_total + 2))
  921. s->res_profile = 1;
  922. else
  923. s->res_profile = 0;
  924. // Setup limited sample rate, number of subbands, etc
  925. s->limited_rate = s->sample_rate >> s->band_limit;
  926. s->limited_range = s->freq_range - s->band_limit;
  927. if (s->limited_range < 0) {
  928. av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR band limit for frequency range\n");
  929. return AVERROR_INVALIDDATA;
  930. }
  931. s->nsubbands = 8 << s->limited_range;
  932. s->g3_avg_only_start_sb = s->nsubbands * ff_dca_avg_g3_freqs[s->res_profile] / (s->limited_rate / 2);
  933. if (s->g3_avg_only_start_sb > s->nsubbands)
  934. s->g3_avg_only_start_sb = s->nsubbands;
  935. s->min_mono_subband = s->nsubbands * 2000 / (s->limited_rate / 2);
  936. if (s->min_mono_subband > s->nsubbands)
  937. s->min_mono_subband = s->nsubbands;
  938. s->max_mono_subband = s->nsubbands * 14000 / (s->limited_rate / 2);
  939. if (s->max_mono_subband > s->nsubbands)
  940. s->max_mono_subband = s->nsubbands;
  941. // Handle change of sample rate
  942. if ((old_rate != s->sample_rate || old_band_limit != s->band_limit) && init_sample_rate(s) < 0)
  943. return AVERROR(ENOMEM);
  944. // Setup stereo downmix
  945. if (s->flags & LBR_FLAG_DMIX_STEREO) {
  946. DCAContext *dca = s->avctx->priv_data;
  947. if (s->nchannels_total < 3 || s->nchannels_total > DCA_LBR_CHANNELS_TOTAL - 2) {
  948. av_log(s->avctx, AV_LOG_ERROR, "Invalid number of channels for LBR stereo downmix\n");
  949. return AVERROR_INVALIDDATA;
  950. }
  951. // This decoder doesn't support ECS chunk
  952. if (dca->request_channel_layout != DCA_SPEAKER_LAYOUT_STEREO && !(s->warned & 4)) {
  953. avpriv_report_missing_feature(s->avctx, "Embedded LBR stereo downmix");
  954. s->warned |= 4;
  955. }
  956. // Account for extra downmixed channel pair
  957. s->nchannels_total += 2;
  958. s->nchannels = 2;
  959. s->ch_mask = DCA_SPEAKER_PAIR_LR;
  960. s->flags &= ~LBR_FLAG_LFE_PRESENT;
  961. }
  962. // Handle change of sample rate or number of channels
  963. if (old_rate != s->sample_rate
  964. || old_band_limit != s->band_limit
  965. || old_nchannels != s->nchannels) {
  966. if (alloc_sample_buffer(s) < 0)
  967. return AVERROR(ENOMEM);
  968. ff_dca_lbr_flush(s);
  969. }
  970. return 0;
  971. }
  972. int ff_dca_lbr_parse(DCALbrDecoder *s, uint8_t *data, DCAExssAsset *asset)
  973. {
  974. struct {
  975. LBRChunk lfe;
  976. LBRChunk tonal;
  977. LBRChunk tonal_grp[5];
  978. LBRChunk grid1[DCA_LBR_CHANNELS / 2];
  979. LBRChunk hr_grid[DCA_LBR_CHANNELS / 2];
  980. LBRChunk ts1[DCA_LBR_CHANNELS / 2];
  981. LBRChunk ts2[DCA_LBR_CHANNELS / 2];
  982. } chunk = { {0} };
  983. GetByteContext gb;
  984. int i, ch, sb, sf, ret, group, chunk_id, chunk_len;
  985. bytestream2_init(&gb, data + asset->lbr_offset, asset->lbr_size);
  986. // LBR sync word
  987. if (bytestream2_get_be32(&gb) != DCA_SYNCWORD_LBR) {
  988. av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR sync word\n");
  989. return AVERROR_INVALIDDATA;
  990. }
  991. // LBR header type
  992. switch (bytestream2_get_byte(&gb)) {
  993. case DCA_LBR_HEADER_SYNC_ONLY:
  994. if (!s->sample_rate) {
  995. av_log(s->avctx, AV_LOG_ERROR, "LBR decoder not initialized\n");
  996. return AVERROR_INVALIDDATA;
  997. }
  998. break;
  999. case DCA_LBR_HEADER_DECODER_INIT:
  1000. if ((ret = parse_decoder_init(s, &gb)) < 0) {
  1001. s->sample_rate = 0;
  1002. return ret;
  1003. }
  1004. break;
  1005. default:
  1006. av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR header type\n");
  1007. return AVERROR_INVALIDDATA;
  1008. }
  1009. // LBR frame chunk header
  1010. chunk_id = bytestream2_get_byte(&gb);
  1011. chunk_len = (chunk_id & 0x80) ? bytestream2_get_be16(&gb) : bytestream2_get_byte(&gb);
  1012. if (chunk_len > bytestream2_get_bytes_left(&gb)) {
  1013. chunk_len = bytestream2_get_bytes_left(&gb);
  1014. av_log(s->avctx, AV_LOG_WARNING, "LBR frame chunk was truncated\n");
  1015. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  1016. return AVERROR_INVALIDDATA;
  1017. }
  1018. bytestream2_init(&gb, gb.buffer, chunk_len);
  1019. switch (chunk_id & 0x7f) {
  1020. case LBR_CHUNK_FRAME:
  1021. if (s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL)) {
  1022. int checksum = bytestream2_get_be16(&gb);
  1023. uint16_t res = chunk_id;
  1024. res += (chunk_len >> 8) & 0xff;
  1025. res += chunk_len & 0xff;
  1026. for (i = 0; i < chunk_len - 2; i++)
  1027. res += gb.buffer[i];
  1028. if (checksum != res) {
  1029. av_log(s->avctx, AV_LOG_WARNING, "Invalid LBR checksum\n");
  1030. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  1031. return AVERROR_INVALIDDATA;
  1032. }
  1033. } else {
  1034. bytestream2_skip(&gb, 2);
  1035. }
  1036. break;
  1037. case LBR_CHUNK_FRAME_NO_CSUM:
  1038. break;
  1039. default:
  1040. av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR frame chunk ID\n");
  1041. return AVERROR_INVALIDDATA;
  1042. }
  1043. // Clear current frame
  1044. memset(s->quant_levels, 0, sizeof(s->quant_levels));
  1045. memset(s->sb_indices, 0xff, sizeof(s->sb_indices));
  1046. memset(s->sec_ch_sbms, 0, sizeof(s->sec_ch_sbms));
  1047. memset(s->sec_ch_lrms, 0, sizeof(s->sec_ch_lrms));
  1048. memset(s->ch_pres, 0, sizeof(s->ch_pres));
  1049. memset(s->grid_1_scf, 0, sizeof(s->grid_1_scf));
  1050. memset(s->grid_2_scf, 0, sizeof(s->grid_2_scf));
  1051. memset(s->grid_3_avg, 0, sizeof(s->grid_3_avg));
  1052. memset(s->grid_3_scf, 0, sizeof(s->grid_3_scf));
  1053. memset(s->grid_3_pres, 0, sizeof(s->grid_3_pres));
  1054. memset(s->tonal_scf, 0, sizeof(s->tonal_scf));
  1055. memset(s->lfe_data, 0, sizeof(s->lfe_data));
  1056. s->part_stereo_pres = 0;
  1057. s->framenum = (s->framenum + 1) & 31;
  1058. for (ch = 0; ch < s->nchannels; ch++) {
  1059. for (sb = 0; sb < s->nsubbands / 4; sb++) {
  1060. s->part_stereo[ch][sb][0] = s->part_stereo[ch][sb][4];
  1061. s->part_stereo[ch][sb][4] = 16;
  1062. }
  1063. }
  1064. memset(s->lpc_coeff[s->framenum & 1], 0, sizeof(s->lpc_coeff[0]));
  1065. for (group = 0; group < 5; group++) {
  1066. for (sf = 0; sf < 1 << group; sf++) {
  1067. int sf_idx = ((s->framenum << group) + sf) & 31;
  1068. s->tonal_bounds[group][sf_idx][0] =
  1069. s->tonal_bounds[group][sf_idx][1] = s->ntones;
  1070. }
  1071. }
  1072. // Parse chunk headers
  1073. while (bytestream2_get_bytes_left(&gb) > 0) {
  1074. chunk_id = bytestream2_get_byte(&gb);
  1075. chunk_len = (chunk_id & 0x80) ? bytestream2_get_be16(&gb) : bytestream2_get_byte(&gb);
  1076. chunk_id &= 0x7f;
  1077. if (chunk_len > bytestream2_get_bytes_left(&gb)) {
  1078. chunk_len = bytestream2_get_bytes_left(&gb);
  1079. av_log(s->avctx, AV_LOG_WARNING, "LBR chunk %#x was truncated\n", chunk_id);
  1080. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  1081. return AVERROR_INVALIDDATA;
  1082. }
  1083. switch (chunk_id) {
  1084. case LBR_CHUNK_LFE:
  1085. chunk.lfe.len = chunk_len;
  1086. chunk.lfe.data = gb.buffer;
  1087. break;
  1088. case LBR_CHUNK_SCF:
  1089. case LBR_CHUNK_TONAL:
  1090. case LBR_CHUNK_TONAL_SCF:
  1091. chunk.tonal.id = chunk_id;
  1092. chunk.tonal.len = chunk_len;
  1093. chunk.tonal.data = gb.buffer;
  1094. break;
  1095. case LBR_CHUNK_TONAL_GRP_1:
  1096. case LBR_CHUNK_TONAL_GRP_2:
  1097. case LBR_CHUNK_TONAL_GRP_3:
  1098. case LBR_CHUNK_TONAL_GRP_4:
  1099. case LBR_CHUNK_TONAL_GRP_5:
  1100. i = LBR_CHUNK_TONAL_GRP_5 - chunk_id;
  1101. chunk.tonal_grp[i].id = i;
  1102. chunk.tonal_grp[i].len = chunk_len;
  1103. chunk.tonal_grp[i].data = gb.buffer;
  1104. break;
  1105. case LBR_CHUNK_TONAL_SCF_GRP_1:
  1106. case LBR_CHUNK_TONAL_SCF_GRP_2:
  1107. case LBR_CHUNK_TONAL_SCF_GRP_3:
  1108. case LBR_CHUNK_TONAL_SCF_GRP_4:
  1109. case LBR_CHUNK_TONAL_SCF_GRP_5:
  1110. i = LBR_CHUNK_TONAL_SCF_GRP_5 - chunk_id;
  1111. chunk.tonal_grp[i].id = i;
  1112. chunk.tonal_grp[i].len = chunk_len;
  1113. chunk.tonal_grp[i].data = gb.buffer;
  1114. break;
  1115. case LBR_CHUNK_RES_GRID_LR:
  1116. case LBR_CHUNK_RES_GRID_LR + 1:
  1117. case LBR_CHUNK_RES_GRID_LR + 2:
  1118. i = chunk_id - LBR_CHUNK_RES_GRID_LR;
  1119. chunk.grid1[i].len = chunk_len;
  1120. chunk.grid1[i].data = gb.buffer;
  1121. break;
  1122. case LBR_CHUNK_RES_GRID_HR:
  1123. case LBR_CHUNK_RES_GRID_HR + 1:
  1124. case LBR_CHUNK_RES_GRID_HR + 2:
  1125. i = chunk_id - LBR_CHUNK_RES_GRID_HR;
  1126. chunk.hr_grid[i].len = chunk_len;
  1127. chunk.hr_grid[i].data = gb.buffer;
  1128. break;
  1129. case LBR_CHUNK_RES_TS_1:
  1130. case LBR_CHUNK_RES_TS_1 + 1:
  1131. case LBR_CHUNK_RES_TS_1 + 2:
  1132. i = chunk_id - LBR_CHUNK_RES_TS_1;
  1133. chunk.ts1[i].len = chunk_len;
  1134. chunk.ts1[i].data = gb.buffer;
  1135. break;
  1136. case LBR_CHUNK_RES_TS_2:
  1137. case LBR_CHUNK_RES_TS_2 + 1:
  1138. case LBR_CHUNK_RES_TS_2 + 2:
  1139. i = chunk_id - LBR_CHUNK_RES_TS_2;
  1140. chunk.ts2[i].len = chunk_len;
  1141. chunk.ts2[i].data = gb.buffer;
  1142. break;
  1143. }
  1144. bytestream2_skip(&gb, chunk_len);
  1145. }
  1146. // Parse the chunks
  1147. ret = parse_lfe_chunk(s, &chunk.lfe);
  1148. ret |= parse_tonal_chunk(s, &chunk.tonal);
  1149. for (i = 0; i < 5; i++)
  1150. ret |= parse_tonal_group(s, &chunk.tonal_grp[i]);
  1151. for (i = 0; i < (s->nchannels + 1) / 2; i++) {
  1152. int ch1 = i * 2;
  1153. int ch2 = FFMIN(ch1 + 1, s->nchannels - 1);
  1154. if (parse_grid_1_chunk (s, &chunk.grid1 [i], ch1, ch2) < 0 ||
  1155. parse_high_res_grid(s, &chunk.hr_grid[i], ch1, ch2) < 0) {
  1156. ret = -1;
  1157. continue;
  1158. }
  1159. // TS chunks depend on both grids. TS_2 depends on TS_1.
  1160. if (!chunk.grid1[i].len || !chunk.hr_grid[i].len || !chunk.ts1[i].len)
  1161. continue;
  1162. if (parse_ts1_chunk(s, &chunk.ts1[i], ch1, ch2) < 0 ||
  1163. parse_ts2_chunk(s, &chunk.ts2[i], ch1, ch2) < 0) {
  1164. ret = -1;
  1165. continue;
  1166. }
  1167. }
  1168. if (ret < 0 && (s->avctx->err_recognition & AV_EF_EXPLODE))
  1169. return AVERROR_INVALIDDATA;
  1170. return 0;
  1171. }
  1172. /**
  1173. * Reconstruct high-frequency resolution grid from first and third grids
  1174. */
  1175. static void decode_grid(DCALbrDecoder *s, int ch1, int ch2)
  1176. {
  1177. int i, ch, sb;
  1178. for (ch = ch1; ch <= ch2; ch++) {
  1179. for (sb = 0; sb < s->nsubbands; sb++) {
  1180. int g1_sb = ff_dca_scf_to_grid_1[sb];
  1181. uint8_t *g1_scf_a = s->grid_1_scf[ch][g1_sb ];
  1182. uint8_t *g1_scf_b = s->grid_1_scf[ch][g1_sb + 1];
  1183. int w1 = ff_dca_grid_1_weights[g1_sb ][sb];
  1184. int w2 = ff_dca_grid_1_weights[g1_sb + 1][sb];
  1185. uint8_t *hr_scf = s->high_res_scf[ch][sb];
  1186. if (sb < 4) {
  1187. for (i = 0; i < 8; i++) {
  1188. int scf = w1 * g1_scf_a[i] + w2 * g1_scf_b[i];
  1189. hr_scf[i] = scf >> 7;
  1190. }
  1191. } else {
  1192. int8_t *g3_scf = s->grid_3_scf[ch][sb - 4];
  1193. int g3_avg = s->grid_3_avg[ch][sb - 4];
  1194. for (i = 0; i < 8; i++) {
  1195. int scf = w1 * g1_scf_a[i] + w2 * g1_scf_b[i];
  1196. hr_scf[i] = (scf >> 7) - g3_avg - g3_scf[i];
  1197. }
  1198. }
  1199. }
  1200. }
  1201. }
  1202. /**
  1203. * Fill unallocated subbands with randomness
  1204. */
  1205. static void random_ts(DCALbrDecoder *s, int ch1, int ch2)
  1206. {
  1207. int i, j, k, ch, sb;
  1208. for (ch = ch1; ch <= ch2; ch++) {
  1209. for (sb = 0; sb < s->nsubbands; sb++) {
  1210. float *samples = s->time_samples[ch][sb];
  1211. if (s->ch_pres[ch] & (1U << sb))
  1212. continue; // Skip allocated subband
  1213. if (sb < 2) {
  1214. // The first two subbands are always zero
  1215. memset(samples, 0, DCA_LBR_TIME_SAMPLES * sizeof(float));
  1216. } else if (sb < 10) {
  1217. for (i = 0; i < DCA_LBR_TIME_SAMPLES; i++)
  1218. samples[i] = lbr_rand(s, sb);
  1219. } else {
  1220. for (i = 0; i < DCA_LBR_TIME_SAMPLES / 8; i++, samples += 8) {
  1221. float accum[8] = { 0 };
  1222. // Modulate by subbands 2-5 in blocks of 8
  1223. for (k = 2; k < 6; k++) {
  1224. float *other = &s->time_samples[ch][k][i * 8];
  1225. for (j = 0; j < 8; j++)
  1226. accum[j] += fabs(other[j]);
  1227. }
  1228. for (j = 0; j < 8; j++)
  1229. samples[j] = (accum[j] * 0.25f + 0.5f) * lbr_rand(s, sb);
  1230. }
  1231. }
  1232. }
  1233. }
  1234. }
  1235. static void predict(float *samples, const float *coeff, int nsamples)
  1236. {
  1237. int i, j;
  1238. for (i = 0; i < nsamples; i++) {
  1239. float res = 0;
  1240. for (j = 0; j < 8; j++)
  1241. res += coeff[j] * samples[i - j - 1];
  1242. samples[i] -= res;
  1243. }
  1244. }
  1245. static void synth_lpc(DCALbrDecoder *s, int ch1, int ch2, int sb)
  1246. {
  1247. int f = s->framenum & 1;
  1248. int ch;
  1249. for (ch = ch1; ch <= ch2; ch++) {
  1250. float *samples = s->time_samples[ch][sb];
  1251. if (!(s->ch_pres[ch] & (1U << sb)))
  1252. continue;
  1253. if (sb < 2) {
  1254. predict(samples, s->lpc_coeff[f^1][ch][sb][1], 16);
  1255. predict(samples + 16, s->lpc_coeff[f ][ch][sb][0], 64);
  1256. predict(samples + 80, s->lpc_coeff[f ][ch][sb][1], 48);
  1257. } else {
  1258. predict(samples, s->lpc_coeff[f^1][ch][sb][0], 16);
  1259. predict(samples + 16, s->lpc_coeff[f ][ch][sb][0], 112);
  1260. }
  1261. }
  1262. }
  1263. static void filter_ts(DCALbrDecoder *s, int ch1, int ch2)
  1264. {
  1265. int i, j, sb, ch;
  1266. for (sb = 0; sb < s->nsubbands; sb++) {
  1267. // Scale factors
  1268. for (ch = ch1; ch <= ch2; ch++) {
  1269. float *samples = s->time_samples[ch][sb];
  1270. uint8_t *hr_scf = s->high_res_scf[ch][sb];
  1271. if (sb < 4) {
  1272. for (i = 0; i < DCA_LBR_TIME_SAMPLES / 16; i++, samples += 16) {
  1273. unsigned int scf = hr_scf[i];
  1274. if (scf > AMP_MAX)
  1275. scf = AMP_MAX;
  1276. for (j = 0; j < 16; j++)
  1277. samples[j] *= ff_dca_quant_amp[scf];
  1278. }
  1279. } else {
  1280. uint8_t *g2_scf = s->grid_2_scf[ch][ff_dca_scf_to_grid_2[sb]];
  1281. for (i = 0; i < DCA_LBR_TIME_SAMPLES / 2; i++, samples += 2) {
  1282. unsigned int scf = hr_scf[i / 8] - g2_scf[i];
  1283. if (scf > AMP_MAX)
  1284. scf = AMP_MAX;
  1285. samples[0] *= ff_dca_quant_amp[scf];
  1286. samples[1] *= ff_dca_quant_amp[scf];
  1287. }
  1288. }
  1289. }
  1290. // Mid-side stereo
  1291. if (ch1 != ch2) {
  1292. float *samples_l = s->time_samples[ch1][sb];
  1293. float *samples_r = s->time_samples[ch2][sb];
  1294. int ch2_pres = s->ch_pres[ch2] & (1U << sb);
  1295. for (i = 0; i < DCA_LBR_TIME_SAMPLES / 16; i++) {
  1296. int sbms = (s->sec_ch_sbms[ch1 / 2][sb] >> i) & 1;
  1297. int lrms = (s->sec_ch_lrms[ch1 / 2][sb] >> i) & 1;
  1298. if (sb >= s->min_mono_subband) {
  1299. if (lrms && ch2_pres) {
  1300. if (sbms) {
  1301. for (j = 0; j < 16; j++) {
  1302. float tmp = samples_l[j];
  1303. samples_l[j] = samples_r[j];
  1304. samples_r[j] = -tmp;
  1305. }
  1306. } else {
  1307. for (j = 0; j < 16; j++) {
  1308. float tmp = samples_l[j];
  1309. samples_l[j] = samples_r[j];
  1310. samples_r[j] = tmp;
  1311. }
  1312. }
  1313. } else if (!ch2_pres) {
  1314. if (sbms && (s->part_stereo_pres & (1 << ch1))) {
  1315. for (j = 0; j < 16; j++)
  1316. samples_r[j] = -samples_l[j];
  1317. } else {
  1318. for (j = 0; j < 16; j++)
  1319. samples_r[j] = samples_l[j];
  1320. }
  1321. }
  1322. } else if (sbms && ch2_pres) {
  1323. for (j = 0; j < 16; j++) {
  1324. float tmp = samples_l[j];
  1325. samples_l[j] = (tmp + samples_r[j]) * 0.5f;
  1326. samples_r[j] = (tmp - samples_r[j]) * 0.5f;
  1327. }
  1328. }
  1329. samples_l += 16;
  1330. samples_r += 16;
  1331. }
  1332. }
  1333. // Inverse prediction
  1334. if (sb < 3)
  1335. synth_lpc(s, ch1, ch2, sb);
  1336. }
  1337. }
  1338. /**
  1339. * Modulate by interpolated partial stereo coefficients
  1340. */
  1341. static void decode_part_stereo(DCALbrDecoder *s, int ch1, int ch2)
  1342. {
  1343. int i, ch, sb, sf;
  1344. for (ch = ch1; ch <= ch2; ch++) {
  1345. for (sb = s->min_mono_subband; sb < s->nsubbands; sb++) {
  1346. uint8_t *pt_st = s->part_stereo[ch][(sb - s->min_mono_subband) / 4];
  1347. float *samples = s->time_samples[ch][sb];
  1348. if (s->ch_pres[ch2] & (1U << sb))
  1349. continue;
  1350. for (sf = 1; sf <= 4; sf++, samples += 32) {
  1351. float prev = ff_dca_st_coeff[pt_st[sf - 1]];
  1352. float next = ff_dca_st_coeff[pt_st[sf ]];
  1353. for (i = 0; i < 32; i++)
  1354. samples[i] *= (32 - i) * prev + i * next;
  1355. }
  1356. }
  1357. }
  1358. }
  1359. /**
  1360. * Synthesise tones in the given group for the given tonal subframe
  1361. */
  1362. static void synth_tones(DCALbrDecoder *s, int ch, float *values,
  1363. int group, int group_sf, int synth_idx)
  1364. {
  1365. int i, start, count;
  1366. if (synth_idx < 0)
  1367. return;
  1368. start = s->tonal_bounds[group][group_sf][0];
  1369. count = (s->tonal_bounds[group][group_sf][1] - start) & (DCA_LBR_TONES - 1);
  1370. for (i = 0; i < count; i++) {
  1371. DCALbrTone *t = &s->tones[(start + i) & (DCA_LBR_TONES - 1)];
  1372. if (t->amp[ch]) {
  1373. float amp = ff_dca_synth_env[synth_idx] * ff_dca_quant_amp[t->amp[ch]];
  1374. float c = amp * cos_tab[(t->phs[ch] ) & 255];
  1375. float s = amp * cos_tab[(t->phs[ch] + 64) & 255];
  1376. const float *cf = ff_dca_corr_cf[t->f_delt];
  1377. int x_freq = t->x_freq;
  1378. switch (x_freq) {
  1379. case 0:
  1380. goto p0;
  1381. case 1:
  1382. values[3] += cf[0] * -s;
  1383. values[2] += cf[1] * c;
  1384. values[1] += cf[2] * s;
  1385. values[0] += cf[3] * -c;
  1386. goto p1;
  1387. case 2:
  1388. values[2] += cf[0] * -s;
  1389. values[1] += cf[1] * c;
  1390. values[0] += cf[2] * s;
  1391. goto p2;
  1392. case 3:
  1393. values[1] += cf[0] * -s;
  1394. values[0] += cf[1] * c;
  1395. goto p3;
  1396. case 4:
  1397. values[0] += cf[0] * -s;
  1398. goto p4;
  1399. }
  1400. values[x_freq - 5] += cf[ 0] * -s;
  1401. p4: values[x_freq - 4] += cf[ 1] * c;
  1402. p3: values[x_freq - 3] += cf[ 2] * s;
  1403. p2: values[x_freq - 2] += cf[ 3] * -c;
  1404. p1: values[x_freq - 1] += cf[ 4] * -s;
  1405. p0: values[x_freq ] += cf[ 5] * c;
  1406. values[x_freq + 1] += cf[ 6] * s;
  1407. values[x_freq + 2] += cf[ 7] * -c;
  1408. values[x_freq + 3] += cf[ 8] * -s;
  1409. values[x_freq + 4] += cf[ 9] * c;
  1410. values[x_freq + 5] += cf[10] * s;
  1411. }
  1412. t->phs[ch] += t->ph_rot;
  1413. }
  1414. }
  1415. /**
  1416. * Synthesise all tones in all groups for the given residual subframe
  1417. */
  1418. static void base_func_synth(DCALbrDecoder *s, int ch, float *values, int sf)
  1419. {
  1420. int group;
  1421. // Tonal vs residual shift is 22 subframes
  1422. for (group = 0; group < 5; group++) {
  1423. int group_sf = (s->framenum << group) + ((sf - 22) >> (5 - group));
  1424. int synth_idx = ((((sf - 22) & 31) << group) & 31) + (1 << group) - 1;
  1425. synth_tones(s, ch, values, group, (group_sf - 1) & 31, 30 - synth_idx);
  1426. synth_tones(s, ch, values, group, (group_sf ) & 31, synth_idx);
  1427. }
  1428. }
  1429. static void transform_channel(DCALbrDecoder *s, int ch, float *output)
  1430. {
  1431. LOCAL_ALIGNED_32(float, values, [DCA_LBR_SUBBANDS ], [4]);
  1432. LOCAL_ALIGNED_32(float, result, [DCA_LBR_SUBBANDS * 2], [4]);
  1433. int sf, sb, nsubbands = s->nsubbands, noutsubbands = 8 << s->freq_range;
  1434. // Clear inactive subbands
  1435. if (nsubbands < noutsubbands)
  1436. memset(values[nsubbands], 0, (noutsubbands - nsubbands) * sizeof(values[0]));
  1437. for (sf = 0; sf < DCA_LBR_TIME_SAMPLES / 4; sf++) {
  1438. // Hybrid filterbank
  1439. s->dcadsp->lbr_bank(values, s->time_samples[ch],
  1440. ff_dca_bank_coeff, sf * 4, nsubbands);
  1441. base_func_synth(s, ch, values[0], sf);
  1442. s->imdct.imdct_calc(&s->imdct, result[0], values[0]);
  1443. // Long window and overlap-add
  1444. s->fdsp->vector_fmul_add(output, result[0], s->window,
  1445. s->history[ch], noutsubbands * 4);
  1446. s->fdsp->vector_fmul_reverse(s->history[ch], result[noutsubbands],
  1447. s->window, noutsubbands * 4);
  1448. output += noutsubbands * 4;
  1449. }
  1450. // Update history for LPC and forward MDCT
  1451. for (sb = 0; sb < nsubbands; sb++) {
  1452. float *samples = s->time_samples[ch][sb] - DCA_LBR_TIME_HISTORY;
  1453. memcpy(samples, samples + DCA_LBR_TIME_SAMPLES, DCA_LBR_TIME_HISTORY * sizeof(float));
  1454. }
  1455. }
  1456. int ff_dca_lbr_filter_frame(DCALbrDecoder *s, AVFrame *frame)
  1457. {
  1458. AVCodecContext *avctx = s->avctx;
  1459. int i, ret, nchannels, ch_conf = (s->ch_mask & 0x7) - 1;
  1460. const int8_t *reorder;
  1461. avctx->channel_layout = channel_layouts[ch_conf];
  1462. avctx->channels = nchannels = channel_counts[ch_conf];
  1463. avctx->sample_rate = s->sample_rate;
  1464. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  1465. avctx->bits_per_raw_sample = 0;
  1466. avctx->profile = FF_PROFILE_DTS_EXPRESS;
  1467. avctx->bit_rate = s->bit_rate_scaled;
  1468. if (s->flags & LBR_FLAG_LFE_PRESENT) {
  1469. avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
  1470. avctx->channels++;
  1471. reorder = channel_reorder_lfe[ch_conf];
  1472. } else {
  1473. reorder = channel_reorder_nolfe[ch_conf];
  1474. }
  1475. frame->nb_samples = 1024 << s->freq_range;
  1476. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  1477. return ret;
  1478. // Filter fullband channels
  1479. for (i = 0; i < (s->nchannels + 1) / 2; i++) {
  1480. int ch1 = i * 2;
  1481. int ch2 = FFMIN(ch1 + 1, s->nchannels - 1);
  1482. decode_grid(s, ch1, ch2);
  1483. random_ts(s, ch1, ch2);
  1484. filter_ts(s, ch1, ch2);
  1485. if (ch1 != ch2 && (s->part_stereo_pres & (1 << ch1)))
  1486. decode_part_stereo(s, ch1, ch2);
  1487. if (ch1 < nchannels)
  1488. transform_channel(s, ch1, (float *)frame->extended_data[reorder[ch1]]);
  1489. if (ch1 != ch2 && ch2 < nchannels)
  1490. transform_channel(s, ch2, (float *)frame->extended_data[reorder[ch2]]);
  1491. }
  1492. // Interpolate LFE channel
  1493. if (s->flags & LBR_FLAG_LFE_PRESENT) {
  1494. s->dcadsp->lfe_iir((float *)frame->extended_data[lfe_index[ch_conf]],
  1495. s->lfe_data, ff_dca_lfe_iir,
  1496. s->lfe_history, 16 << s->freq_range);
  1497. }
  1498. if ((ret = ff_side_data_update_matrix_encoding(frame, AV_MATRIX_ENCODING_NONE)) < 0)
  1499. return ret;
  1500. return 0;
  1501. }
  1502. av_cold void ff_dca_lbr_flush(DCALbrDecoder *s)
  1503. {
  1504. int ch, sb;
  1505. if (!s->sample_rate)
  1506. return;
  1507. // Clear history
  1508. memset(s->part_stereo, 16, sizeof(s->part_stereo));
  1509. memset(s->lpc_coeff, 0, sizeof(s->lpc_coeff));
  1510. memset(s->history, 0, sizeof(s->history));
  1511. memset(s->tonal_bounds, 0, sizeof(s->tonal_bounds));
  1512. memset(s->lfe_history, 0, sizeof(s->lfe_history));
  1513. s->framenum = 0;
  1514. s->ntones = 0;
  1515. for (ch = 0; ch < s->nchannels; ch++) {
  1516. for (sb = 0; sb < s->nsubbands; sb++) {
  1517. float *samples = s->time_samples[ch][sb] - DCA_LBR_TIME_HISTORY;
  1518. memset(samples, 0, DCA_LBR_TIME_HISTORY * sizeof(float));
  1519. }
  1520. }
  1521. }
  1522. av_cold int ff_dca_lbr_init(DCALbrDecoder *s)
  1523. {
  1524. init_tables();
  1525. if (!(s->fdsp = avpriv_float_dsp_alloc(0)))
  1526. return AVERROR(ENOMEM);
  1527. s->lbr_rand = 1;
  1528. return 0;
  1529. }
  1530. av_cold void ff_dca_lbr_close(DCALbrDecoder *s)
  1531. {
  1532. s->sample_rate = 0;
  1533. av_freep(&s->ts_buffer);
  1534. s->ts_size = 0;
  1535. av_freep(&s->fdsp);
  1536. ff_mdct_end(&s->imdct);
  1537. }