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.

1114 lines
38KB

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