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.

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