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.

1839 lines
56KB

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