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.

1042 lines
35KB

  1. /*
  2. * MPEG-4 ALS decoder
  3. * Copyright (c) 2009 Thilo Borgmann <thilo.borgmann _at_ googlemail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file libavcodec/alsdec.c
  23. * MPEG-4 ALS decoder
  24. * @author Thilo Borgmann <thilo.borgmann _at_ googlemail.com>
  25. */
  26. //#define DEBUG
  27. #include "avcodec.h"
  28. #include "get_bits.h"
  29. #include "unary.h"
  30. #include "mpeg4audio.h"
  31. #include "bytestream.h"
  32. #include "als_data.h"
  33. enum RA_Flag {
  34. RA_FLAG_NONE,
  35. RA_FLAG_FRAMES,
  36. RA_FLAG_HEADER
  37. };
  38. typedef struct {
  39. uint32_t samples; ///< number of samples, 0xFFFFFFFF if unknown
  40. int resolution; ///< 000 = 8-bit; 001 = 16-bit; 010 = 24-bit; 011 = 32-bit
  41. int floating; ///< 1 = IEEE 32-bit floating-point, 0 = integer
  42. int frame_length; ///< frame length for each frame (last frame may differ)
  43. int ra_distance; ///< distance between RA frames (in frames, 0...255)
  44. enum RA_Flag ra_flag; ///< indicates where the size of ra units is stored
  45. int adapt_order; ///< adaptive order: 1 = on, 0 = off
  46. int coef_table; ///< table index of Rice code parameters
  47. int long_term_prediction; ///< long term prediction (LTP): 1 = on, 0 = off
  48. int max_order; ///< maximum prediction order (0..1023)
  49. int block_switching; ///< number of block switching levels
  50. int bgmc; ///< "Block Gilbert-Moore Code": 1 = on, 0 = off (Rice coding only)
  51. int sb_part; ///< sub-block partition
  52. int joint_stereo; ///< joint stereo: 1 = on, 0 = off
  53. int mc_coding; ///< extended inter-channel coding (multi channel coding): 1 = on, 0 = off
  54. int chan_config; ///< indicates that a chan_config_info field is present
  55. int chan_sort; ///< channel rearrangement: 1 = on, 0 = off
  56. int rlslms; ///< use "Recursive Least Square-Least Mean Square" predictor: 1 = on, 0 = off
  57. int chan_config_info; ///< mapping of channels to loudspeaker locations. Unused until setting channel configuration is implemented.
  58. int *chan_pos; ///< original channel positions
  59. uint32_t header_size; ///< header size of original audio file in bytes, provided for debugging
  60. uint32_t trailer_size; ///< trailer size of original audio file in bytes, provided for debugging
  61. } ALSSpecificConfig;
  62. typedef struct {
  63. AVCodecContext *avctx;
  64. ALSSpecificConfig sconf;
  65. GetBitContext gb;
  66. unsigned int cur_frame_length; ///< length of the current frame to decode
  67. unsigned int frame_id; ///< the frame ID / number of the current frame
  68. unsigned int js_switch; ///< if true, joint-stereo decoding is enforced
  69. unsigned int num_blocks; ///< number of blocks used in the current frame
  70. int ltp_lag_length; ///< number of bits used for ltp lag value
  71. int32_t *quant_cof; ///< quantized parcor coefficients
  72. int32_t *lpc_cof; ///< coefficients of the direct form prediction filter
  73. int32_t *prev_raw_samples; ///< contains unshifted raw samples from the previous block
  74. int32_t **raw_samples; ///< decoded raw samples for each channel
  75. int32_t *raw_buffer; ///< contains all decoded raw samples including carryover samples
  76. } ALSDecContext;
  77. static av_cold void dprint_specific_config(ALSDecContext *ctx)
  78. {
  79. #ifdef DEBUG
  80. AVCodecContext *avctx = ctx->avctx;
  81. ALSSpecificConfig *sconf = &ctx->sconf;
  82. dprintf(avctx, "resolution = %i\n", sconf->resolution);
  83. dprintf(avctx, "floating = %i\n", sconf->floating);
  84. dprintf(avctx, "frame_length = %i\n", sconf->frame_length);
  85. dprintf(avctx, "ra_distance = %i\n", sconf->ra_distance);
  86. dprintf(avctx, "ra_flag = %i\n", sconf->ra_flag);
  87. dprintf(avctx, "adapt_order = %i\n", sconf->adapt_order);
  88. dprintf(avctx, "coef_table = %i\n", sconf->coef_table);
  89. dprintf(avctx, "long_term_prediction = %i\n", sconf->long_term_prediction);
  90. dprintf(avctx, "max_order = %i\n", sconf->max_order);
  91. dprintf(avctx, "block_switching = %i\n", sconf->block_switching);
  92. dprintf(avctx, "bgmc = %i\n", sconf->bgmc);
  93. dprintf(avctx, "sb_part = %i\n", sconf->sb_part);
  94. dprintf(avctx, "joint_stereo = %i\n", sconf->joint_stereo);
  95. dprintf(avctx, "mc_coding = %i\n", sconf->mc_coding);
  96. dprintf(avctx, "chan_config = %i\n", sconf->chan_config);
  97. dprintf(avctx, "chan_sort = %i\n", sconf->chan_sort);
  98. dprintf(avctx, "RLSLMS = %i\n", sconf->rlslms);
  99. dprintf(avctx, "chan_config_info = %i\n", sconf->chan_config_info);
  100. dprintf(avctx, "header_size = %i\n", sconf->header_size);
  101. dprintf(avctx, "trailer_size = %i\n", sconf->trailer_size);
  102. #endif
  103. }
  104. /** Reads an ALSSpecificConfig from a buffer into the output struct.
  105. */
  106. static av_cold int read_specific_config(ALSDecContext *ctx)
  107. {
  108. GetBitContext gb;
  109. uint64_t ht_size;
  110. int i, config_offset, crc_enabled;
  111. MPEG4AudioConfig m4ac;
  112. ALSSpecificConfig *sconf = &ctx->sconf;
  113. AVCodecContext *avctx = ctx->avctx;
  114. uint32_t als_id;
  115. init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
  116. config_offset = ff_mpeg4audio_get_config(&m4ac, avctx->extradata,
  117. avctx->extradata_size);
  118. if (config_offset < 0)
  119. return -1;
  120. skip_bits_long(&gb, config_offset);
  121. if (get_bits_left(&gb) < (30 << 3))
  122. return -1;
  123. // read the fixed items
  124. als_id = get_bits_long(&gb, 32);
  125. avctx->sample_rate = m4ac.sample_rate;
  126. skip_bits_long(&gb, 32); // sample rate already known
  127. sconf->samples = get_bits_long(&gb, 32);
  128. avctx->channels = m4ac.channels;
  129. skip_bits(&gb, 16); // number of channels already knwon
  130. skip_bits(&gb, 3); // skip file_type
  131. sconf->resolution = get_bits(&gb, 3);
  132. sconf->floating = get_bits1(&gb);
  133. skip_bits1(&gb); // skip msb_first
  134. sconf->frame_length = get_bits(&gb, 16) + 1;
  135. sconf->ra_distance = get_bits(&gb, 8);
  136. sconf->ra_flag = get_bits(&gb, 2);
  137. sconf->adapt_order = get_bits1(&gb);
  138. sconf->coef_table = get_bits(&gb, 2);
  139. sconf->long_term_prediction = get_bits1(&gb);
  140. sconf->max_order = get_bits(&gb, 10);
  141. sconf->block_switching = get_bits(&gb, 2);
  142. sconf->bgmc = get_bits1(&gb);
  143. sconf->sb_part = get_bits1(&gb);
  144. sconf->joint_stereo = get_bits1(&gb);
  145. sconf->mc_coding = get_bits1(&gb);
  146. sconf->chan_config = get_bits1(&gb);
  147. sconf->chan_sort = get_bits1(&gb);
  148. crc_enabled = get_bits1(&gb);
  149. sconf->rlslms = get_bits1(&gb);
  150. skip_bits(&gb, 5); // skip 5 reserved bits
  151. skip_bits1(&gb); // skip aux_data_enabled
  152. // check for ALSSpecificConfig struct
  153. if (als_id != MKBETAG('A','L','S','\0'))
  154. return -1;
  155. ctx->cur_frame_length = sconf->frame_length;
  156. // allocate quantized parcor coefficient buffer
  157. if (!(ctx->quant_cof = av_malloc(sizeof(*ctx->quant_cof) * sconf->max_order)) ||
  158. !(ctx->lpc_cof = av_malloc(sizeof(*ctx->lpc_cof) * sconf->max_order))) {
  159. av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
  160. return AVERROR(ENOMEM);
  161. }
  162. // read channel config
  163. if (sconf->chan_config)
  164. sconf->chan_config_info = get_bits(&gb, 16);
  165. // TODO: use this to set avctx->channel_layout
  166. // read channel sorting
  167. if (sconf->chan_sort && avctx->channels > 1) {
  168. int chan_pos_bits = av_ceil_log2(avctx->channels);
  169. int bits_needed = avctx->channels * chan_pos_bits + 7;
  170. if (get_bits_left(&gb) < bits_needed)
  171. return -1;
  172. if (!(sconf->chan_pos = av_malloc(avctx->channels * sizeof(*sconf->chan_pos))))
  173. return AVERROR(ENOMEM);
  174. for (i = 0; i < avctx->channels; i++)
  175. sconf->chan_pos[i] = get_bits(&gb, chan_pos_bits);
  176. align_get_bits(&gb);
  177. // TODO: use this to actually do channel sorting
  178. } else {
  179. sconf->chan_sort = 0;
  180. }
  181. // read fixed header and trailer sizes,
  182. // if size = 0xFFFFFFFF then there is no data field!
  183. if (get_bits_left(&gb) < 64)
  184. return -1;
  185. sconf->header_size = get_bits_long(&gb, 32);
  186. sconf->trailer_size = get_bits_long(&gb, 32);
  187. if (sconf->header_size == 0xFFFFFFFF)
  188. sconf->header_size = 0;
  189. if (sconf->trailer_size == 0xFFFFFFFF)
  190. sconf->trailer_size = 0;
  191. ht_size = ((int64_t)(sconf->header_size) + (int64_t)(sconf->trailer_size)) << 3;
  192. // skip the header and trailer data
  193. if (get_bits_left(&gb) < ht_size)
  194. return -1;
  195. if (ht_size > INT32_MAX)
  196. return -1;
  197. skip_bits_long(&gb, ht_size);
  198. // skip the crc data
  199. if (crc_enabled) {
  200. if (get_bits_left(&gb) < 32)
  201. return -1;
  202. skip_bits_long(&gb, 32);
  203. }
  204. // no need to read the rest of ALSSpecificConfig (ra_unit_size & aux data)
  205. dprint_specific_config(ctx);
  206. return 0;
  207. }
  208. /** Checks the ALSSpecificConfig for unsupported features.
  209. */
  210. static int check_specific_config(ALSDecContext *ctx)
  211. {
  212. ALSSpecificConfig *sconf = &ctx->sconf;
  213. int error = 0;
  214. // report unsupported feature and set error value
  215. #define MISSING_ERR(cond, str, errval) \
  216. { \
  217. if (cond) { \
  218. av_log_missing_feature(ctx->avctx, str, 0); \
  219. error = errval; \
  220. } \
  221. }
  222. MISSING_ERR(sconf->floating, "Floating point decoding", -1);
  223. MISSING_ERR(sconf->bgmc, "BGMC entropy decoding", -1);
  224. MISSING_ERR(sconf->mc_coding, "Multi-channel correlation", -1);
  225. MISSING_ERR(sconf->rlslms, "Adaptive RLS-LMS prediction", -1);
  226. MISSING_ERR(sconf->chan_sort, "Channel sorting", 0);
  227. return error;
  228. }
  229. /** Parses the bs_info field to extract the block partitioning used in
  230. * block switching mode, refer to ISO/IEC 14496-3, section 11.6.2.
  231. */
  232. static void parse_bs_info(const uint32_t bs_info, unsigned int n,
  233. unsigned int div, unsigned int **div_blocks,
  234. unsigned int *num_blocks)
  235. {
  236. if (n < 31 && ((bs_info << n) & 0x40000000)) {
  237. // if the level is valid and the investigated bit n is set
  238. // then recursively check both children at bits (2n+1) and (2n+2)
  239. n *= 2;
  240. div += 1;
  241. parse_bs_info(bs_info, n + 1, div, div_blocks, num_blocks);
  242. parse_bs_info(bs_info, n + 2, div, div_blocks, num_blocks);
  243. } else {
  244. // else the bit is not set or the last level has been reached
  245. // (bit implicitly not set)
  246. **div_blocks = div;
  247. (*div_blocks)++;
  248. (*num_blocks)++;
  249. }
  250. }
  251. /** Reads and decodes a Rice codeword.
  252. */
  253. static int32_t decode_rice(GetBitContext *gb, unsigned int k)
  254. {
  255. int max = gb->size_in_bits - get_bits_count(gb) - k;
  256. int q = get_unary(gb, 0, max);
  257. int r = k ? get_bits1(gb) : !(q & 1);
  258. if (k > 1) {
  259. q <<= (k - 1);
  260. q += get_bits_long(gb, k - 1);
  261. } else if (!k) {
  262. q >>= 1;
  263. }
  264. return r ? q : ~q;
  265. }
  266. /** Converts PARCOR coefficient k to direct filter coefficient.
  267. */
  268. static void parcor_to_lpc(unsigned int k, const int32_t *par, int32_t *cof)
  269. {
  270. int i, j;
  271. for (i = 0, j = k - 1; i < j; i++, j--) {
  272. int tmp1 = ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20);
  273. cof[j] += ((MUL64(par[k], cof[i]) + (1 << 19)) >> 20);
  274. cof[i] += tmp1;
  275. }
  276. if (i == j)
  277. cof[i] += ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20);
  278. cof[k] = par[k];
  279. }
  280. /** Reads block switching field if necessary and sets actual block sizes.
  281. * Also assures that the block sizes of the last frame correspond to the
  282. * actual number of samples.
  283. */
  284. static void get_block_sizes(ALSDecContext *ctx, unsigned int *div_blocks,
  285. uint32_t *bs_info)
  286. {
  287. ALSSpecificConfig *sconf = &ctx->sconf;
  288. GetBitContext *gb = &ctx->gb;
  289. unsigned int *ptr_div_blocks = div_blocks;
  290. unsigned int b;
  291. if (sconf->block_switching) {
  292. unsigned int bs_info_len = 1 << (sconf->block_switching + 2);
  293. *bs_info = get_bits_long(gb, bs_info_len);
  294. *bs_info <<= (32 - bs_info_len);
  295. }
  296. ctx->num_blocks = 0;
  297. parse_bs_info(*bs_info, 0, 0, &ptr_div_blocks, &ctx->num_blocks);
  298. // The last frame may have an overdetermined block structure given in
  299. // the bitstream. In that case the defined block structure would need
  300. // more samples than available to be consistent.
  301. // The block structure is actually used but the block sizes are adapted
  302. // to fit the actual number of available samples.
  303. // Example: 5 samples, 2nd level block sizes: 2 2 2 2.
  304. // This results in the actual block sizes: 2 2 1 0.
  305. // This is not specified in 14496-3 but actually done by the reference
  306. // codec RM22 revision 2.
  307. // This appears to happen in case of an odd number of samples in the last
  308. // frame which is actually not allowed by the block length switching part
  309. // of 14496-3.
  310. // The ALS conformance files feature an odd number of samples in the last
  311. // frame.
  312. for (b = 0; b < ctx->num_blocks; b++)
  313. div_blocks[b] = ctx->sconf.frame_length >> div_blocks[b];
  314. if (ctx->cur_frame_length != ctx->sconf.frame_length) {
  315. unsigned int remaining = ctx->cur_frame_length;
  316. for (b = 0; b < ctx->num_blocks; b++) {
  317. if (remaining < div_blocks[b]) {
  318. div_blocks[b] = remaining;
  319. ctx->num_blocks = b + 1;
  320. break;
  321. }
  322. remaining -= div_blocks[b];
  323. }
  324. }
  325. }
  326. /** Reads the block data for a constant block
  327. */
  328. static void read_const_block(ALSDecContext *ctx, int32_t *raw_samples,
  329. unsigned int block_length, unsigned int *js_blocks)
  330. {
  331. ALSSpecificConfig *sconf = &ctx->sconf;
  332. AVCodecContext *avctx = ctx->avctx;
  333. GetBitContext *gb = &ctx->gb;
  334. int32_t const_val = 0;
  335. unsigned int const_block, k;
  336. const_block = get_bits1(gb); // 1 = constant value, 0 = zero block (silence)
  337. *js_blocks = get_bits1(gb);
  338. // skip 5 reserved bits
  339. skip_bits(gb, 5);
  340. if (const_block) {
  341. unsigned int const_val_bits = sconf->floating ? 24 : avctx->bits_per_raw_sample;
  342. const_val = get_sbits_long(gb, const_val_bits);
  343. }
  344. // write raw samples into buffer
  345. for (k = 0; k < block_length; k++)
  346. raw_samples[k] = const_val;
  347. }
  348. /** Reads the block data for a non-constant block
  349. */
  350. static int read_var_block(ALSDecContext *ctx, unsigned int ra_block,
  351. int32_t *raw_samples, unsigned int block_length,
  352. unsigned int *js_blocks, int32_t *raw_other,
  353. unsigned int *shift_lsbs)
  354. {
  355. ALSSpecificConfig *sconf = &ctx->sconf;
  356. AVCodecContext *avctx = ctx->avctx;
  357. GetBitContext *gb = &ctx->gb;
  358. unsigned int k;
  359. unsigned int s[8];
  360. unsigned int sub_blocks, log2_sub_blocks, sb_length;
  361. unsigned int opt_order = 1;
  362. int32_t *quant_cof = ctx->quant_cof;
  363. int32_t *lpc_cof = ctx->lpc_cof;
  364. unsigned int start = 0;
  365. int smp = 0;
  366. int sb, store_prev_samples;
  367. int64_t y;
  368. int use_ltp = 0;
  369. int ltp_lag = 0;
  370. int ltp_gain[5];
  371. *js_blocks = get_bits1(gb);
  372. // determine the number of subblocks for entropy decoding
  373. if (!sconf->bgmc && !sconf->sb_part) {
  374. log2_sub_blocks = 0;
  375. } else {
  376. if (sconf->bgmc && sconf->sb_part)
  377. log2_sub_blocks = get_bits(gb, 2);
  378. else
  379. log2_sub_blocks = 2 * get_bits1(gb);
  380. }
  381. sub_blocks = 1 << log2_sub_blocks;
  382. // do not continue in case of a damaged stream since
  383. // block_length must be evenly divisible by sub_blocks
  384. if (block_length & (sub_blocks - 1)) {
  385. av_log(avctx, AV_LOG_WARNING,
  386. "Block length is not evenly divisible by the number of subblocks.\n");
  387. return -1;
  388. }
  389. sb_length = block_length >> log2_sub_blocks;
  390. if (sconf->bgmc) {
  391. // TODO: BGMC mode
  392. } else {
  393. s[0] = get_bits(gb, 4 + (sconf->resolution > 1));
  394. for (k = 1; k < sub_blocks; k++)
  395. s[k] = s[k - 1] + decode_rice(gb, 0);
  396. }
  397. if (get_bits1(gb))
  398. *shift_lsbs = get_bits(gb, 4) + 1;
  399. store_prev_samples = (*js_blocks && raw_other) || *shift_lsbs;
  400. if (!sconf->rlslms) {
  401. if (sconf->adapt_order) {
  402. int opt_order_length = av_ceil_log2(av_clip((block_length >> 3) - 1,
  403. 2, sconf->max_order + 1));
  404. opt_order = get_bits(gb, opt_order_length);
  405. } else {
  406. opt_order = sconf->max_order;
  407. }
  408. if (opt_order) {
  409. int add_base;
  410. if (sconf->coef_table == 3) {
  411. add_base = 0x7F;
  412. // read coefficient 0
  413. quant_cof[0] = 32 * parcor_scaled_values[get_bits(gb, 7)];
  414. // read coefficient 1
  415. if (opt_order > 1)
  416. quant_cof[1] = -32 * parcor_scaled_values[get_bits(gb, 7)];
  417. // read coefficients 2 to opt_order
  418. for (k = 2; k < opt_order; k++)
  419. quant_cof[k] = get_bits(gb, 7);
  420. } else {
  421. int k_max;
  422. add_base = 1;
  423. // read coefficient 0 to 19
  424. k_max = FFMIN(opt_order, 20);
  425. for (k = 0; k < k_max; k++) {
  426. int rice_param = parcor_rice_table[sconf->coef_table][k][1];
  427. int offset = parcor_rice_table[sconf->coef_table][k][0];
  428. quant_cof[k] = decode_rice(gb, rice_param) + offset;
  429. }
  430. // read coefficients 20 to 126
  431. k_max = FFMIN(opt_order, 127);
  432. for (; k < k_max; k++)
  433. quant_cof[k] = decode_rice(gb, 2) + (k & 1);
  434. // read coefficients 127 to opt_order
  435. for (; k < opt_order; k++)
  436. quant_cof[k] = decode_rice(gb, 1);
  437. quant_cof[0] = 32 * parcor_scaled_values[quant_cof[0] + 64];
  438. if (opt_order > 1)
  439. quant_cof[1] = -32 * parcor_scaled_values[quant_cof[1] + 64];
  440. }
  441. for (k = 2; k < opt_order; k++)
  442. quant_cof[k] = (quant_cof[k] << 14) + (add_base << 13);
  443. }
  444. }
  445. // read LTP gain and lag values
  446. if (sconf->long_term_prediction) {
  447. use_ltp = get_bits1(gb);
  448. if (use_ltp) {
  449. ltp_gain[0] = decode_rice(gb, 1) << 3;
  450. ltp_gain[1] = decode_rice(gb, 2) << 3;
  451. ltp_gain[2] = ltp_gain_values[get_unary(gb, 0, 4)][get_bits(gb, 2)];
  452. ltp_gain[3] = decode_rice(gb, 2) << 3;
  453. ltp_gain[4] = decode_rice(gb, 1) << 3;
  454. ltp_lag = get_bits(gb, ctx->ltp_lag_length);
  455. ltp_lag += FFMAX(4, opt_order + 1);
  456. }
  457. }
  458. // read first value and residuals in case of a random access block
  459. if (ra_block) {
  460. if (opt_order)
  461. raw_samples[0] = decode_rice(gb, avctx->bits_per_raw_sample - 4);
  462. if (opt_order > 1)
  463. raw_samples[1] = decode_rice(gb, s[0] + 3);
  464. if (opt_order > 2)
  465. raw_samples[2] = decode_rice(gb, s[0] + 1);
  466. start = FFMIN(opt_order, 3);
  467. }
  468. // read all residuals
  469. if (sconf->bgmc) {
  470. // TODO: BGMC mode
  471. } else {
  472. int32_t *current_res = raw_samples + start;
  473. for (sb = 0; sb < sub_blocks; sb++, start = 0)
  474. for (; start < sb_length; start++)
  475. *current_res++ = decode_rice(gb, s[sb]);
  476. }
  477. // reverse long-term prediction
  478. if (use_ltp) {
  479. int ltp_smp;
  480. for (ltp_smp = FFMAX(ltp_lag - 2, 0); ltp_smp < block_length; ltp_smp++) {
  481. int center = ltp_smp - ltp_lag;
  482. int begin = FFMAX(0, center - 2);
  483. int end = center + 3;
  484. int tab = 5 - (end - begin);
  485. int base;
  486. y = 1 << 6;
  487. for (base = begin; base < end; base++, tab++)
  488. y += MUL64(ltp_gain[tab], raw_samples[base]);
  489. raw_samples[ltp_smp] += y >> 7;
  490. }
  491. }
  492. // reconstruct all samples from residuals
  493. if (ra_block) {
  494. for (smp = 0; smp < opt_order; smp++) {
  495. y = 1 << 19;
  496. for (sb = 0; sb < smp; sb++)
  497. y += MUL64(lpc_cof[sb],raw_samples[smp - (sb + 1)]);
  498. raw_samples[smp] -= y >> 20;
  499. parcor_to_lpc(smp, quant_cof, lpc_cof);
  500. }
  501. } else {
  502. for (k = 0; k < opt_order; k++)
  503. parcor_to_lpc(k, quant_cof, lpc_cof);
  504. // store previous samples in case that they have to be altered
  505. if (store_prev_samples)
  506. memcpy(ctx->prev_raw_samples, raw_samples - sconf->max_order,
  507. sizeof(*ctx->prev_raw_samples) * sconf->max_order);
  508. // reconstruct difference signal for prediction (joint-stereo)
  509. if (*js_blocks && raw_other) {
  510. int32_t *left, *right;
  511. if (raw_other > raw_samples) { // D = R - L
  512. left = raw_samples;
  513. right = raw_other;
  514. } else { // D = R - L
  515. left = raw_other;
  516. right = raw_samples;
  517. }
  518. for (sb = -1; sb >= -sconf->max_order; sb--)
  519. raw_samples[sb] = right[sb] - left[sb];
  520. }
  521. // reconstruct shifted signal
  522. if (*shift_lsbs)
  523. for (sb = -1; sb >= -sconf->max_order; sb--)
  524. raw_samples[sb] >>= *shift_lsbs;
  525. }
  526. // reconstruct raw samples
  527. for (; smp < block_length; smp++) {
  528. y = 1 << 19;
  529. for (sb = 0; sb < opt_order; sb++)
  530. y += MUL64(lpc_cof[sb],raw_samples[smp - (sb + 1)]);
  531. raw_samples[smp] -= y >> 20;
  532. }
  533. // restore previous samples in case that they have been altered
  534. if (store_prev_samples)
  535. memcpy(raw_samples - sconf->max_order, ctx->prev_raw_samples,
  536. sizeof(*raw_samples) * sconf->max_order);
  537. return 0;
  538. }
  539. /** Reads the block data.
  540. */
  541. static int read_block_data(ALSDecContext *ctx, unsigned int ra_block,
  542. int32_t *raw_samples, unsigned int block_length,
  543. unsigned int *js_blocks, int32_t *raw_other)
  544. {
  545. ALSSpecificConfig *sconf = &ctx->sconf;
  546. GetBitContext *gb = &ctx->gb;
  547. unsigned int shift_lsbs = 0;
  548. unsigned int k;
  549. // read block type flag and read the samples accordingly
  550. if (get_bits1(gb)) {
  551. if (read_var_block(ctx, ra_block, raw_samples, block_length, js_blocks,
  552. raw_other, &shift_lsbs))
  553. return -1;
  554. } else {
  555. read_const_block(ctx, raw_samples, block_length, js_blocks);
  556. }
  557. // TODO: read RLSLMS extension data
  558. if (!sconf->mc_coding || ctx->js_switch)
  559. align_get_bits(gb);
  560. if (shift_lsbs)
  561. for (k = 0; k < block_length; k++)
  562. raw_samples[k] <<= shift_lsbs;
  563. return 0;
  564. }
  565. /** Computes the number of samples left to decode for the current frame and
  566. * sets these samples to zero.
  567. */
  568. static void zero_remaining(unsigned int b, unsigned int b_max,
  569. const unsigned int *div_blocks, int32_t *buf)
  570. {
  571. unsigned int count = 0;
  572. while (b < b_max)
  573. count += div_blocks[b];
  574. if (count)
  575. memset(buf, 0, sizeof(*buf) * count);
  576. }
  577. /** Decodes blocks independently.
  578. */
  579. static int decode_blocks_ind(ALSDecContext *ctx, unsigned int ra_frame,
  580. unsigned int c, const unsigned int *div_blocks,
  581. unsigned int *js_blocks)
  582. {
  583. int32_t *raw_sample;
  584. unsigned int b;
  585. raw_sample = ctx->raw_samples[c];
  586. for (b = 0; b < ctx->num_blocks; b++) {
  587. if (read_block_data(ctx, ra_frame, raw_sample,
  588. div_blocks[b], &js_blocks[0], NULL)) {
  589. // damaged block, write zero for the rest of the frame
  590. zero_remaining(b, ctx->num_blocks, div_blocks, raw_sample);
  591. return -1;
  592. }
  593. raw_sample += div_blocks[b];
  594. ra_frame = 0;
  595. }
  596. return 0;
  597. }
  598. /** Decodes blocks dependently.
  599. */
  600. static int decode_blocks(ALSDecContext *ctx, unsigned int ra_frame,
  601. unsigned int c, const unsigned int *div_blocks,
  602. unsigned int *js_blocks)
  603. {
  604. ALSSpecificConfig *sconf = &ctx->sconf;
  605. unsigned int offset = 0;
  606. int32_t *raw_samples_R;
  607. int32_t *raw_samples_L;
  608. unsigned int b;
  609. // decode all blocks
  610. for (b = 0; b < ctx->num_blocks; b++) {
  611. unsigned int s;
  612. raw_samples_L = ctx->raw_samples[c ] + offset;
  613. raw_samples_R = ctx->raw_samples[c + 1] + offset;
  614. if (read_block_data(ctx, ra_frame, raw_samples_L, div_blocks[b],
  615. &js_blocks[0], raw_samples_R) ||
  616. read_block_data(ctx, ra_frame, raw_samples_R, div_blocks[b],
  617. &js_blocks[1], raw_samples_L)) {
  618. // damaged block, write zero for the rest of the frame
  619. zero_remaining(b, ctx->num_blocks, div_blocks, raw_samples_L);
  620. zero_remaining(b, ctx->num_blocks, div_blocks, raw_samples_R);
  621. return -1;
  622. }
  623. // reconstruct joint-stereo blocks
  624. if (js_blocks[0]) {
  625. if (js_blocks[1])
  626. av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel pair!\n");
  627. for (s = 0; s < div_blocks[b]; s++)
  628. raw_samples_L[s] = raw_samples_R[s] - raw_samples_L[s];
  629. } else if (js_blocks[1]) {
  630. for (s = 0; s < div_blocks[b]; s++)
  631. raw_samples_R[s] = raw_samples_R[s] + raw_samples_L[s];
  632. }
  633. offset += div_blocks[b];
  634. ra_frame = 0;
  635. }
  636. // store carryover raw samples,
  637. // the others channel raw samples are stored by the calling function.
  638. memmove(ctx->raw_samples[c] - sconf->max_order,
  639. ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
  640. sizeof(*ctx->raw_samples[c]) * sconf->max_order);
  641. return 0;
  642. }
  643. /** Reads the frame data.
  644. */
  645. static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)
  646. {
  647. ALSSpecificConfig *sconf = &ctx->sconf;
  648. AVCodecContext *avctx = ctx->avctx;
  649. GetBitContext *gb = &ctx->gb;
  650. unsigned int div_blocks[32]; ///< block sizes.
  651. unsigned int c;
  652. unsigned int js_blocks[2];
  653. uint32_t bs_info = 0;
  654. // skip the size of the ra unit if present in the frame
  655. if (sconf->ra_flag == RA_FLAG_FRAMES && ra_frame)
  656. skip_bits_long(gb, 32);
  657. if (sconf->mc_coding && sconf->joint_stereo) {
  658. ctx->js_switch = get_bits1(gb);
  659. align_get_bits(gb);
  660. }
  661. if (!sconf->mc_coding || ctx->js_switch) {
  662. int independent_bs = !sconf->joint_stereo;
  663. for (c = 0; c < avctx->channels; c++) {
  664. js_blocks[0] = 0;
  665. js_blocks[1] = 0;
  666. get_block_sizes(ctx, div_blocks, &bs_info);
  667. // if joint_stereo and block_switching is set, independent decoding
  668. // is signaled via the first bit of bs_info
  669. if (sconf->joint_stereo && sconf->block_switching)
  670. if (bs_info >> 31)
  671. independent_bs = 2;
  672. // if this is the last channel, it has to be decoded independently
  673. if (c == avctx->channels - 1)
  674. independent_bs = 1;
  675. if (independent_bs) {
  676. if (decode_blocks_ind(ctx, ra_frame, c, div_blocks, js_blocks))
  677. return -1;
  678. independent_bs--;
  679. } else {
  680. if (decode_blocks(ctx, ra_frame, c, div_blocks, js_blocks))
  681. return -1;
  682. c++;
  683. }
  684. // store carryover raw samples
  685. memmove(ctx->raw_samples[c] - sconf->max_order,
  686. ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
  687. sizeof(*ctx->raw_samples[c]) * sconf->max_order);
  688. }
  689. } else { // multi-channel coding
  690. get_block_sizes(ctx, div_blocks, &bs_info);
  691. // TODO: multi channel coding might use a temporary buffer instead as
  692. // the actual channel is not known when read_block-data is called
  693. if (decode_blocks_ind(ctx, ra_frame, 0, div_blocks, js_blocks))
  694. return -1;
  695. // TODO: read_channel_data
  696. }
  697. // TODO: read_diff_float_data
  698. return 0;
  699. }
  700. /** Decodes an ALS frame.
  701. */
  702. static int decode_frame(AVCodecContext *avctx,
  703. void *data, int *data_size,
  704. AVPacket *avpkt)
  705. {
  706. ALSDecContext *ctx = avctx->priv_data;
  707. ALSSpecificConfig *sconf = &ctx->sconf;
  708. const uint8_t *buffer = avpkt->data;
  709. int buffer_size = avpkt->size;
  710. int invalid_frame, size;
  711. unsigned int c, sample, ra_frame, bytes_read, shift;
  712. init_get_bits(&ctx->gb, buffer, buffer_size * 8);
  713. // In the case that the distance between random access frames is set to zero
  714. // (sconf->ra_distance == 0) no frame is treated as a random access frame.
  715. // For the first frame, if prediction is used, all samples used from the
  716. // previous frame are assumed to be zero.
  717. ra_frame = sconf->ra_distance && !(ctx->frame_id % sconf->ra_distance);
  718. // the last frame to decode might have a different length
  719. if (sconf->samples != 0xFFFFFFFF)
  720. ctx->cur_frame_length = FFMIN(sconf->samples - ctx->frame_id * (uint64_t) sconf->frame_length,
  721. sconf->frame_length);
  722. else
  723. ctx->cur_frame_length = sconf->frame_length;
  724. // decode the frame data
  725. if ((invalid_frame = read_frame_data(ctx, ra_frame) < 0))
  726. av_log(ctx->avctx, AV_LOG_WARNING,
  727. "Reading frame data failed. Skipping RA unit.\n");
  728. ctx->frame_id++;
  729. // check for size of decoded data
  730. size = ctx->cur_frame_length * avctx->channels *
  731. (av_get_bits_per_sample_format(avctx->sample_fmt) >> 3);
  732. if (size > *data_size) {
  733. av_log(avctx, AV_LOG_ERROR, "Decoded data exceeds buffer size.\n");
  734. return -1;
  735. }
  736. *data_size = size;
  737. // transform decoded frame into output format
  738. #define INTERLEAVE_OUTPUT(bps) \
  739. { \
  740. int##bps##_t *dest = (int##bps##_t*) data; \
  741. shift = bps - ctx->avctx->bits_per_raw_sample; \
  742. for (sample = 0; sample < ctx->cur_frame_length; sample++) \
  743. for (c = 0; c < avctx->channels; c++) \
  744. *dest++ = ctx->raw_samples[c][sample] << shift; \
  745. }
  746. if (ctx->avctx->bits_per_raw_sample <= 16) {
  747. INTERLEAVE_OUTPUT(16)
  748. } else {
  749. INTERLEAVE_OUTPUT(32)
  750. }
  751. bytes_read = invalid_frame ? buffer_size :
  752. (get_bits_count(&ctx->gb) + 7) >> 3;
  753. return bytes_read;
  754. }
  755. /** Uninitializes the ALS decoder.
  756. */
  757. static av_cold int decode_end(AVCodecContext *avctx)
  758. {
  759. ALSDecContext *ctx = avctx->priv_data;
  760. av_freep(&ctx->sconf.chan_pos);
  761. av_freep(&ctx->quant_cof);
  762. av_freep(&ctx->lpc_cof);
  763. av_freep(&ctx->prev_raw_samples);
  764. av_freep(&ctx->raw_samples);
  765. av_freep(&ctx->raw_buffer);
  766. return 0;
  767. }
  768. /** Initializes the ALS decoder.
  769. */
  770. static av_cold int decode_init(AVCodecContext *avctx)
  771. {
  772. unsigned int c;
  773. unsigned int channel_size;
  774. ALSDecContext *ctx = avctx->priv_data;
  775. ALSSpecificConfig *sconf = &ctx->sconf;
  776. ctx->avctx = avctx;
  777. if (!avctx->extradata) {
  778. av_log(avctx, AV_LOG_ERROR, "Missing required ALS extradata.\n");
  779. return -1;
  780. }
  781. if (read_specific_config(ctx)) {
  782. av_log(avctx, AV_LOG_ERROR, "Reading ALSSpecificConfig failed.\n");
  783. decode_end(avctx);
  784. return -1;
  785. }
  786. if (check_specific_config(ctx)) {
  787. decode_end(avctx);
  788. return -1;
  789. }
  790. if (sconf->floating) {
  791. avctx->sample_fmt = SAMPLE_FMT_FLT;
  792. avctx->bits_per_raw_sample = 32;
  793. } else {
  794. avctx->sample_fmt = sconf->resolution > 1
  795. ? SAMPLE_FMT_S32 : SAMPLE_FMT_S16;
  796. avctx->bits_per_raw_sample = (sconf->resolution + 1) * 8;
  797. }
  798. // set lag value for long-term prediction
  799. ctx->ltp_lag_length = 8 + (avctx->sample_rate >= 96000) +
  800. (avctx->sample_rate >= 192000);
  801. avctx->frame_size = sconf->frame_length;
  802. channel_size = sconf->frame_length + sconf->max_order;
  803. ctx->prev_raw_samples = av_malloc (sizeof(*ctx->prev_raw_samples) * sconf->max_order);
  804. ctx->raw_buffer = av_mallocz(sizeof(*ctx-> raw_buffer) * avctx->channels * channel_size);
  805. ctx->raw_samples = av_malloc (sizeof(*ctx-> raw_samples) * avctx->channels);
  806. // allocate previous raw sample buffer
  807. if (!ctx->prev_raw_samples || !ctx->raw_buffer|| !ctx->raw_samples) {
  808. av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
  809. decode_end(avctx);
  810. return AVERROR(ENOMEM);
  811. }
  812. // assign raw samples buffers
  813. ctx->raw_samples[0] = ctx->raw_buffer + sconf->max_order;
  814. for (c = 1; c < avctx->channels; c++)
  815. ctx->raw_samples[c] = ctx->raw_samples[c - 1] + channel_size;
  816. return 0;
  817. }
  818. /** Flushes (resets) the frame ID after seeking.
  819. */
  820. static av_cold void flush(AVCodecContext *avctx)
  821. {
  822. ALSDecContext *ctx = avctx->priv_data;
  823. ctx->frame_id = 0;
  824. }
  825. AVCodec als_decoder = {
  826. "als",
  827. CODEC_TYPE_AUDIO,
  828. CODEC_ID_MP4ALS,
  829. sizeof(ALSDecContext),
  830. decode_init,
  831. NULL,
  832. decode_end,
  833. decode_frame,
  834. .flush = flush,
  835. .capabilities = CODEC_CAP_SUBFRAMES,
  836. .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Audio Lossless Coding (ALS)"),
  837. };