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.

1821 lines
55KB

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