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.

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