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.

1261 lines
43KB

  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. int *use_ltp; ///< contains use_ltp flags for all channels
  138. int *ltp_lag; ///< contains ltp lag values for all channels
  139. int **ltp_gain; ///< gain values for ltp 5-tap filter for a channel
  140. int *ltp_gain_buffer; ///< contains all gain values for ltp 5-tap filter
  141. int32_t *quant_cof; ///< quantized parcor coefficients
  142. int32_t *lpc_cof; ///< coefficients of the direct form prediction filter
  143. int32_t *prev_raw_samples; ///< contains unshifted raw samples from the previous block
  144. int32_t **raw_samples; ///< decoded raw samples for each channel
  145. int32_t *raw_buffer; ///< contains all decoded raw samples including carryover samples
  146. } ALSDecContext;
  147. typedef struct {
  148. unsigned int block_length; ///< number of samples within the block
  149. unsigned int ra_block; ///< if true, this is a random access block
  150. int const_block; ///< if true, this is a constant value block
  151. int32_t const_val; ///< the sample value of a constant block
  152. int js_blocks; ///< true if this block contains a difference signal
  153. unsigned int shift_lsbs; ///< shift of values for this block
  154. unsigned int opt_order; ///< prediction order of this block
  155. int store_prev_samples;///< if true, carryover samples have to be stored
  156. int *use_ltp; ///< if true, long-term prediction is used
  157. int *ltp_lag; ///< lag value for long-term prediction
  158. int *ltp_gain; ///< gain values for ltp 5-tap filter
  159. int32_t *quant_cof; ///< quantized parcor coefficients
  160. int32_t *lpc_cof; ///< coefficients of the direct form prediction
  161. int32_t *raw_samples; ///< decoded raw samples / residuals for this block
  162. int32_t *prev_raw_samples; ///< contains unshifted raw samples from the previous block
  163. int32_t *raw_other; ///< decoded raw samples of the other channel of a channel pair
  164. } ALSBlockData;
  165. static av_cold void dprint_specific_config(ALSDecContext *ctx)
  166. {
  167. #ifdef DEBUG
  168. AVCodecContext *avctx = ctx->avctx;
  169. ALSSpecificConfig *sconf = &ctx->sconf;
  170. dprintf(avctx, "resolution = %i\n", sconf->resolution);
  171. dprintf(avctx, "floating = %i\n", sconf->floating);
  172. dprintf(avctx, "frame_length = %i\n", sconf->frame_length);
  173. dprintf(avctx, "ra_distance = %i\n", sconf->ra_distance);
  174. dprintf(avctx, "ra_flag = %i\n", sconf->ra_flag);
  175. dprintf(avctx, "adapt_order = %i\n", sconf->adapt_order);
  176. dprintf(avctx, "coef_table = %i\n", sconf->coef_table);
  177. dprintf(avctx, "long_term_prediction = %i\n", sconf->long_term_prediction);
  178. dprintf(avctx, "max_order = %i\n", sconf->max_order);
  179. dprintf(avctx, "block_switching = %i\n", sconf->block_switching);
  180. dprintf(avctx, "bgmc = %i\n", sconf->bgmc);
  181. dprintf(avctx, "sb_part = %i\n", sconf->sb_part);
  182. dprintf(avctx, "joint_stereo = %i\n", sconf->joint_stereo);
  183. dprintf(avctx, "mc_coding = %i\n", sconf->mc_coding);
  184. dprintf(avctx, "chan_config = %i\n", sconf->chan_config);
  185. dprintf(avctx, "chan_sort = %i\n", sconf->chan_sort);
  186. dprintf(avctx, "RLSLMS = %i\n", sconf->rlslms);
  187. dprintf(avctx, "chan_config_info = %i\n", sconf->chan_config_info);
  188. dprintf(avctx, "header_size = %i\n", sconf->header_size);
  189. dprintf(avctx, "trailer_size = %i\n", sconf->trailer_size);
  190. #endif
  191. }
  192. /** Reads an ALSSpecificConfig from a buffer into the output struct.
  193. */
  194. static av_cold int read_specific_config(ALSDecContext *ctx)
  195. {
  196. GetBitContext gb;
  197. uint64_t ht_size;
  198. int i, config_offset, crc_enabled;
  199. MPEG4AudioConfig m4ac;
  200. ALSSpecificConfig *sconf = &ctx->sconf;
  201. AVCodecContext *avctx = ctx->avctx;
  202. uint32_t als_id;
  203. init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
  204. config_offset = ff_mpeg4audio_get_config(&m4ac, avctx->extradata,
  205. avctx->extradata_size);
  206. if (config_offset < 0)
  207. return -1;
  208. skip_bits_long(&gb, config_offset);
  209. if (get_bits_left(&gb) < (30 << 3))
  210. return -1;
  211. // read the fixed items
  212. als_id = get_bits_long(&gb, 32);
  213. avctx->sample_rate = m4ac.sample_rate;
  214. skip_bits_long(&gb, 32); // sample rate already known
  215. sconf->samples = get_bits_long(&gb, 32);
  216. avctx->channels = m4ac.channels;
  217. skip_bits(&gb, 16); // number of channels already knwon
  218. skip_bits(&gb, 3); // skip file_type
  219. sconf->resolution = get_bits(&gb, 3);
  220. sconf->floating = get_bits1(&gb);
  221. skip_bits1(&gb); // skip msb_first
  222. sconf->frame_length = get_bits(&gb, 16) + 1;
  223. sconf->ra_distance = get_bits(&gb, 8);
  224. sconf->ra_flag = get_bits(&gb, 2);
  225. sconf->adapt_order = get_bits1(&gb);
  226. sconf->coef_table = get_bits(&gb, 2);
  227. sconf->long_term_prediction = get_bits1(&gb);
  228. sconf->max_order = get_bits(&gb, 10);
  229. sconf->block_switching = get_bits(&gb, 2);
  230. sconf->bgmc = get_bits1(&gb);
  231. sconf->sb_part = get_bits1(&gb);
  232. sconf->joint_stereo = get_bits1(&gb);
  233. sconf->mc_coding = get_bits1(&gb);
  234. sconf->chan_config = get_bits1(&gb);
  235. sconf->chan_sort = get_bits1(&gb);
  236. crc_enabled = get_bits1(&gb);
  237. sconf->rlslms = get_bits1(&gb);
  238. skip_bits(&gb, 5); // skip 5 reserved bits
  239. skip_bits1(&gb); // skip aux_data_enabled
  240. // check for ALSSpecificConfig struct
  241. if (als_id != MKBETAG('A','L','S','\0'))
  242. return -1;
  243. ctx->cur_frame_length = sconf->frame_length;
  244. // allocate quantized parcor coefficient buffer
  245. if (!(ctx->quant_cof = av_malloc(sizeof(*ctx->quant_cof) * sconf->max_order)) ||
  246. !(ctx->lpc_cof = av_malloc(sizeof(*ctx->lpc_cof) * sconf->max_order))) {
  247. av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
  248. return AVERROR(ENOMEM);
  249. }
  250. // read channel config
  251. if (sconf->chan_config)
  252. sconf->chan_config_info = get_bits(&gb, 16);
  253. // TODO: use this to set avctx->channel_layout
  254. // read channel sorting
  255. if (sconf->chan_sort && avctx->channels > 1) {
  256. int chan_pos_bits = av_ceil_log2(avctx->channels);
  257. int bits_needed = avctx->channels * chan_pos_bits + 7;
  258. if (get_bits_left(&gb) < bits_needed)
  259. return -1;
  260. if (!(sconf->chan_pos = av_malloc(avctx->channels * sizeof(*sconf->chan_pos))))
  261. return AVERROR(ENOMEM);
  262. for (i = 0; i < avctx->channels; i++)
  263. sconf->chan_pos[i] = get_bits(&gb, chan_pos_bits);
  264. align_get_bits(&gb);
  265. // TODO: use this to actually do channel sorting
  266. } else {
  267. sconf->chan_sort = 0;
  268. }
  269. // read fixed header and trailer sizes,
  270. // if size = 0xFFFFFFFF then there is no data field!
  271. if (get_bits_left(&gb) < 64)
  272. return -1;
  273. sconf->header_size = get_bits_long(&gb, 32);
  274. sconf->trailer_size = get_bits_long(&gb, 32);
  275. if (sconf->header_size == 0xFFFFFFFF)
  276. sconf->header_size = 0;
  277. if (sconf->trailer_size == 0xFFFFFFFF)
  278. sconf->trailer_size = 0;
  279. ht_size = ((int64_t)(sconf->header_size) + (int64_t)(sconf->trailer_size)) << 3;
  280. // skip the header and trailer data
  281. if (get_bits_left(&gb) < ht_size)
  282. return -1;
  283. if (ht_size > INT32_MAX)
  284. return -1;
  285. skip_bits_long(&gb, ht_size);
  286. // skip the crc data
  287. if (crc_enabled) {
  288. if (get_bits_left(&gb) < 32)
  289. return -1;
  290. skip_bits_long(&gb, 32);
  291. }
  292. // no need to read the rest of ALSSpecificConfig (ra_unit_size & aux data)
  293. dprint_specific_config(ctx);
  294. return 0;
  295. }
  296. /** Checks the ALSSpecificConfig for unsupported features.
  297. */
  298. static int check_specific_config(ALSDecContext *ctx)
  299. {
  300. ALSSpecificConfig *sconf = &ctx->sconf;
  301. int error = 0;
  302. // report unsupported feature and set error value
  303. #define MISSING_ERR(cond, str, errval) \
  304. { \
  305. if (cond) { \
  306. av_log_missing_feature(ctx->avctx, str, 0); \
  307. error = errval; \
  308. } \
  309. }
  310. MISSING_ERR(sconf->floating, "Floating point decoding", -1);
  311. MISSING_ERR(sconf->bgmc, "BGMC entropy decoding", -1);
  312. MISSING_ERR(sconf->mc_coding, "Multi-channel correlation", -1);
  313. MISSING_ERR(sconf->rlslms, "Adaptive RLS-LMS prediction", -1);
  314. MISSING_ERR(sconf->chan_sort, "Channel sorting", 0);
  315. return error;
  316. }
  317. /** Parses the bs_info field to extract the block partitioning used in
  318. * block switching mode, refer to ISO/IEC 14496-3, section 11.6.2.
  319. */
  320. static void parse_bs_info(const uint32_t bs_info, unsigned int n,
  321. unsigned int div, unsigned int **div_blocks,
  322. unsigned int *num_blocks)
  323. {
  324. if (n < 31 && ((bs_info << n) & 0x40000000)) {
  325. // if the level is valid and the investigated bit n is set
  326. // then recursively check both children at bits (2n+1) and (2n+2)
  327. n *= 2;
  328. div += 1;
  329. parse_bs_info(bs_info, n + 1, div, div_blocks, num_blocks);
  330. parse_bs_info(bs_info, n + 2, div, div_blocks, num_blocks);
  331. } else {
  332. // else the bit is not set or the last level has been reached
  333. // (bit implicitly not set)
  334. **div_blocks = div;
  335. (*div_blocks)++;
  336. (*num_blocks)++;
  337. }
  338. }
  339. /** Reads and decodes a Rice codeword.
  340. */
  341. static int32_t decode_rice(GetBitContext *gb, unsigned int k)
  342. {
  343. int max = get_bits_left(gb) - k;
  344. int q = get_unary(gb, 0, max);
  345. int r = k ? get_bits1(gb) : !(q & 1);
  346. if (k > 1) {
  347. q <<= (k - 1);
  348. q += get_bits_long(gb, k - 1);
  349. } else if (!k) {
  350. q >>= 1;
  351. }
  352. return r ? q : ~q;
  353. }
  354. /** Converts PARCOR coefficient k to direct filter coefficient.
  355. */
  356. static void parcor_to_lpc(unsigned int k, const int32_t *par, int32_t *cof)
  357. {
  358. int i, j;
  359. for (i = 0, j = k - 1; i < j; i++, j--) {
  360. int tmp1 = ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20);
  361. cof[j] += ((MUL64(par[k], cof[i]) + (1 << 19)) >> 20);
  362. cof[i] += tmp1;
  363. }
  364. if (i == j)
  365. cof[i] += ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20);
  366. cof[k] = par[k];
  367. }
  368. /** Reads block switching field if necessary and sets actual block sizes.
  369. * Also assures that the block sizes of the last frame correspond to the
  370. * actual number of samples.
  371. */
  372. static void get_block_sizes(ALSDecContext *ctx, unsigned int *div_blocks,
  373. uint32_t *bs_info)
  374. {
  375. ALSSpecificConfig *sconf = &ctx->sconf;
  376. GetBitContext *gb = &ctx->gb;
  377. unsigned int *ptr_div_blocks = div_blocks;
  378. unsigned int b;
  379. if (sconf->block_switching) {
  380. unsigned int bs_info_len = 1 << (sconf->block_switching + 2);
  381. *bs_info = get_bits_long(gb, bs_info_len);
  382. *bs_info <<= (32 - bs_info_len);
  383. }
  384. ctx->num_blocks = 0;
  385. parse_bs_info(*bs_info, 0, 0, &ptr_div_blocks, &ctx->num_blocks);
  386. // The last frame may have an overdetermined block structure given in
  387. // the bitstream. In that case the defined block structure would need
  388. // more samples than available to be consistent.
  389. // The block structure is actually used but the block sizes are adapted
  390. // to fit the actual number of available samples.
  391. // Example: 5 samples, 2nd level block sizes: 2 2 2 2.
  392. // This results in the actual block sizes: 2 2 1 0.
  393. // This is not specified in 14496-3 but actually done by the reference
  394. // codec RM22 revision 2.
  395. // This appears to happen in case of an odd number of samples in the last
  396. // frame which is actually not allowed by the block length switching part
  397. // of 14496-3.
  398. // The ALS conformance files feature an odd number of samples in the last
  399. // frame.
  400. for (b = 0; b < ctx->num_blocks; b++)
  401. div_blocks[b] = ctx->sconf.frame_length >> div_blocks[b];
  402. if (ctx->cur_frame_length != ctx->sconf.frame_length) {
  403. unsigned int remaining = ctx->cur_frame_length;
  404. for (b = 0; b < ctx->num_blocks; b++) {
  405. if (remaining < div_blocks[b]) {
  406. div_blocks[b] = remaining;
  407. ctx->num_blocks = b + 1;
  408. break;
  409. }
  410. remaining -= div_blocks[b];
  411. }
  412. }
  413. }
  414. /** Reads the block data for a constant block
  415. */
  416. static void read_const_block_data(ALSDecContext *ctx, ALSBlockData *bd)
  417. {
  418. ALSSpecificConfig *sconf = &ctx->sconf;
  419. AVCodecContext *avctx = ctx->avctx;
  420. GetBitContext *gb = &ctx->gb;
  421. bd->const_val = 0;
  422. bd->const_block = get_bits1(gb); // 1 = constant value, 0 = zero block (silence)
  423. bd->js_blocks = get_bits1(gb);
  424. // skip 5 reserved bits
  425. skip_bits(gb, 5);
  426. if (bd->const_block) {
  427. unsigned int const_val_bits = sconf->floating ? 24 : avctx->bits_per_raw_sample;
  428. bd->const_val = get_sbits_long(gb, const_val_bits);
  429. }
  430. // ensure constant block decoding by reusing this field
  431. bd->const_block = 1;
  432. }
  433. /** Decodes the block data for a constant block
  434. */
  435. static void decode_const_block_data(ALSDecContext *ctx, ALSBlockData *bd)
  436. {
  437. int smp = bd->block_length;
  438. int32_t val = bd->const_val;
  439. int32_t *dst = bd->raw_samples;
  440. // write raw samples into buffer
  441. for (; smp; smp--)
  442. *dst++ = val;
  443. }
  444. /** Reads the block data for a non-constant block
  445. */
  446. static int read_var_block_data(ALSDecContext *ctx, ALSBlockData *bd)
  447. {
  448. ALSSpecificConfig *sconf = &ctx->sconf;
  449. AVCodecContext *avctx = ctx->avctx;
  450. GetBitContext *gb = &ctx->gb;
  451. unsigned int k;
  452. unsigned int s[8];
  453. unsigned int sub_blocks, log2_sub_blocks, sb_length;
  454. unsigned int start = 0;
  455. unsigned int opt_order;
  456. int sb;
  457. int32_t *quant_cof = bd->quant_cof;
  458. // ensure variable block decoding by reusing this field
  459. bd->const_block = 0;
  460. bd->opt_order = 1;
  461. bd->js_blocks = get_bits1(gb);
  462. opt_order = bd->opt_order;
  463. // determine the number of subblocks for entropy decoding
  464. if (!sconf->bgmc && !sconf->sb_part) {
  465. log2_sub_blocks = 0;
  466. } else {
  467. if (sconf->bgmc && sconf->sb_part)
  468. log2_sub_blocks = get_bits(gb, 2);
  469. else
  470. log2_sub_blocks = 2 * get_bits1(gb);
  471. }
  472. sub_blocks = 1 << log2_sub_blocks;
  473. // do not continue in case of a damaged stream since
  474. // block_length must be evenly divisible by sub_blocks
  475. if (bd->block_length & (sub_blocks - 1)) {
  476. av_log(avctx, AV_LOG_WARNING,
  477. "Block length is not evenly divisible by the number of subblocks.\n");
  478. return -1;
  479. }
  480. sb_length = bd->block_length >> log2_sub_blocks;
  481. if (sconf->bgmc) {
  482. // TODO: BGMC mode
  483. } else {
  484. s[0] = get_bits(gb, 4 + (sconf->resolution > 1));
  485. for (k = 1; k < sub_blocks; k++)
  486. s[k] = s[k - 1] + decode_rice(gb, 0);
  487. }
  488. if (get_bits1(gb))
  489. bd->shift_lsbs = get_bits(gb, 4) + 1;
  490. bd->store_prev_samples = (bd->js_blocks && bd->raw_other) || bd->shift_lsbs;
  491. if (!sconf->rlslms) {
  492. if (sconf->adapt_order) {
  493. int opt_order_length = av_ceil_log2(av_clip((bd->block_length >> 3) - 1,
  494. 2, sconf->max_order + 1));
  495. bd->opt_order = get_bits(gb, opt_order_length);
  496. } else {
  497. bd->opt_order = sconf->max_order;
  498. }
  499. opt_order = bd->opt_order;
  500. if (opt_order) {
  501. int add_base;
  502. if (sconf->coef_table == 3) {
  503. add_base = 0x7F;
  504. // read coefficient 0
  505. quant_cof[0] = 32 * parcor_scaled_values[get_bits(gb, 7)];
  506. // read coefficient 1
  507. if (opt_order > 1)
  508. quant_cof[1] = -32 * parcor_scaled_values[get_bits(gb, 7)];
  509. // read coefficients 2 to opt_order
  510. for (k = 2; k < opt_order; k++)
  511. quant_cof[k] = get_bits(gb, 7);
  512. } else {
  513. int k_max;
  514. add_base = 1;
  515. // read coefficient 0 to 19
  516. k_max = FFMIN(opt_order, 20);
  517. for (k = 0; k < k_max; k++) {
  518. int rice_param = parcor_rice_table[sconf->coef_table][k][1];
  519. int offset = parcor_rice_table[sconf->coef_table][k][0];
  520. quant_cof[k] = decode_rice(gb, rice_param) + offset;
  521. }
  522. // read coefficients 20 to 126
  523. k_max = FFMIN(opt_order, 127);
  524. for (; k < k_max; k++)
  525. quant_cof[k] = decode_rice(gb, 2) + (k & 1);
  526. // read coefficients 127 to opt_order
  527. for (; k < opt_order; k++)
  528. quant_cof[k] = decode_rice(gb, 1);
  529. quant_cof[0] = 32 * parcor_scaled_values[quant_cof[0] + 64];
  530. if (opt_order > 1)
  531. quant_cof[1] = -32 * parcor_scaled_values[quant_cof[1] + 64];
  532. }
  533. for (k = 2; k < opt_order; k++)
  534. quant_cof[k] = (quant_cof[k] << 14) + (add_base << 13);
  535. }
  536. }
  537. // read LTP gain and lag values
  538. if (sconf->long_term_prediction) {
  539. *bd->use_ltp = get_bits1(gb);
  540. if (*bd->use_ltp) {
  541. bd->ltp_gain[0] = decode_rice(gb, 1) << 3;
  542. bd->ltp_gain[1] = decode_rice(gb, 2) << 3;
  543. bd->ltp_gain[2] = ltp_gain_values[get_unary(gb, 0, 4)][get_bits(gb, 2)];
  544. bd->ltp_gain[3] = decode_rice(gb, 2) << 3;
  545. bd->ltp_gain[4] = decode_rice(gb, 1) << 3;
  546. *bd->ltp_lag = get_bits(gb, ctx->ltp_lag_length);
  547. *bd->ltp_lag += FFMAX(4, opt_order + 1);
  548. }
  549. }
  550. // read first value and residuals in case of a random access block
  551. if (bd->ra_block) {
  552. if (opt_order)
  553. bd->raw_samples[0] = decode_rice(gb, avctx->bits_per_raw_sample - 4);
  554. if (opt_order > 1)
  555. bd->raw_samples[1] = decode_rice(gb, s[0] + 3);
  556. if (opt_order > 2)
  557. bd->raw_samples[2] = decode_rice(gb, s[0] + 1);
  558. start = FFMIN(opt_order, 3);
  559. }
  560. // read all residuals
  561. if (sconf->bgmc) {
  562. // TODO: BGMC mode
  563. } else {
  564. int32_t *current_res = bd->raw_samples + start;
  565. for (sb = 0; sb < sub_blocks; sb++, start = 0)
  566. for (; start < sb_length; start++)
  567. *current_res++ = decode_rice(gb, s[sb]);
  568. }
  569. if (!sconf->mc_coding || ctx->js_switch)
  570. align_get_bits(gb);
  571. return 0;
  572. }
  573. /** Decodes the block data for a non-constant block
  574. */
  575. static int decode_var_block_data(ALSDecContext *ctx, ALSBlockData *bd)
  576. {
  577. ALSSpecificConfig *sconf = &ctx->sconf;
  578. unsigned int block_length = bd->block_length;
  579. unsigned int smp = 0;
  580. unsigned int k;
  581. unsigned int opt_order = bd->opt_order;
  582. int sb;
  583. int64_t y;
  584. int32_t *quant_cof = bd->quant_cof;
  585. int32_t *lpc_cof = bd->lpc_cof;
  586. int32_t *raw_samples = bd->raw_samples;
  587. // reverse long-term prediction
  588. if (*bd->use_ltp) {
  589. int ltp_smp;
  590. for (ltp_smp = FFMAX(*bd->ltp_lag - 2, 0); ltp_smp < block_length; ltp_smp++) {
  591. int center = ltp_smp - *bd->ltp_lag;
  592. int begin = FFMAX(0, center - 2);
  593. int end = center + 3;
  594. int tab = 5 - (end - begin);
  595. int base;
  596. y = 1 << 6;
  597. for (base = begin; base < end; base++, tab++)
  598. y += MUL64(bd->ltp_gain[tab], raw_samples[base]);
  599. raw_samples[ltp_smp] += y >> 7;
  600. }
  601. }
  602. // reconstruct all samples from residuals
  603. if (bd->ra_block) {
  604. for (smp = 0; smp < opt_order; smp++) {
  605. y = 1 << 19;
  606. for (sb = 0; sb < smp; sb++)
  607. y += MUL64(lpc_cof[sb],raw_samples[smp - (sb + 1)]);
  608. raw_samples[smp] -= y >> 20;
  609. parcor_to_lpc(smp, quant_cof, lpc_cof);
  610. }
  611. } else {
  612. for (k = 0; k < opt_order; k++)
  613. parcor_to_lpc(k, quant_cof, lpc_cof);
  614. // store previous samples in case that they have to be altered
  615. if (bd->store_prev_samples)
  616. memcpy(bd->prev_raw_samples, raw_samples - sconf->max_order,
  617. sizeof(*bd->prev_raw_samples) * sconf->max_order);
  618. // reconstruct difference signal for prediction (joint-stereo)
  619. if (bd->js_blocks && bd->raw_other) {
  620. int32_t *left, *right;
  621. if (bd->raw_other > raw_samples) { // D = R - L
  622. left = raw_samples;
  623. right = bd->raw_other;
  624. } else { // D = R - L
  625. left = bd->raw_other;
  626. right = raw_samples;
  627. }
  628. for (sb = -1; sb >= -sconf->max_order; sb--)
  629. raw_samples[sb] = right[sb] - left[sb];
  630. }
  631. // reconstruct shifted signal
  632. if (bd->shift_lsbs)
  633. for (sb = -1; sb >= -sconf->max_order; sb--)
  634. raw_samples[sb] >>= bd->shift_lsbs;
  635. }
  636. // reconstruct raw samples
  637. for (; smp < bd->block_length; smp++) {
  638. y = 1 << 19;
  639. for (sb = 0; sb < opt_order; sb++)
  640. y += MUL64(bd->lpc_cof[sb],raw_samples[smp - (sb + 1)]);
  641. raw_samples[smp] -= y >> 20;
  642. }
  643. // restore previous samples in case that they have been altered
  644. if (bd->store_prev_samples)
  645. memcpy(raw_samples - sconf->max_order, bd->prev_raw_samples,
  646. sizeof(*raw_samples) * sconf->max_order);
  647. return 0;
  648. }
  649. /** Reads the block data.
  650. */
  651. static int read_block(ALSDecContext *ctx, ALSBlockData *bd)
  652. {
  653. GetBitContext *gb = &ctx->gb;
  654. // read block type flag and read the samples accordingly
  655. if (get_bits1(gb)) {
  656. if (read_var_block_data(ctx, bd))
  657. return -1;
  658. } else {
  659. read_const_block_data(ctx, bd);
  660. }
  661. return 0;
  662. }
  663. /** Decodes the block data.
  664. */
  665. static int decode_block(ALSDecContext *ctx, ALSBlockData *bd)
  666. {
  667. unsigned int smp;
  668. // read block type flag and read the samples accordingly
  669. if (bd->const_block)
  670. decode_const_block_data(ctx, bd);
  671. else if (decode_var_block_data(ctx, bd))
  672. return -1;
  673. // TODO: read RLSLMS extension data
  674. if (bd->shift_lsbs)
  675. for (smp = 0; smp < bd->block_length; smp++)
  676. bd->raw_samples[smp] <<= bd->shift_lsbs;
  677. return 0;
  678. }
  679. /** Reads and decodes block data successively.
  680. */
  681. static int read_decode_block(ALSDecContext *ctx, ALSBlockData *bd)
  682. {
  683. int ret;
  684. ret = read_block(ctx, bd);
  685. if (ret)
  686. return ret;
  687. ret = decode_block(ctx, bd);
  688. return ret;
  689. }
  690. /** Computes the number of samples left to decode for the current frame and
  691. * sets these samples to zero.
  692. */
  693. static void zero_remaining(unsigned int b, unsigned int b_max,
  694. const unsigned int *div_blocks, int32_t *buf)
  695. {
  696. unsigned int count = 0;
  697. while (b < b_max)
  698. count += div_blocks[b];
  699. if (count)
  700. memset(buf, 0, sizeof(*buf) * count);
  701. }
  702. /** Decodes blocks independently.
  703. */
  704. static int decode_blocks_ind(ALSDecContext *ctx, unsigned int ra_frame,
  705. unsigned int c, const unsigned int *div_blocks,
  706. unsigned int *js_blocks)
  707. {
  708. unsigned int b;
  709. ALSBlockData bd;
  710. memset(&bd, 0, sizeof(ALSBlockData));
  711. bd.ra_block = ra_frame;
  712. bd.use_ltp = ctx->use_ltp;
  713. bd.ltp_lag = ctx->ltp_lag;
  714. bd.ltp_gain = ctx->ltp_gain[0];
  715. bd.quant_cof = ctx->quant_cof;
  716. bd.lpc_cof = ctx->lpc_cof;
  717. bd.prev_raw_samples = ctx->prev_raw_samples;
  718. bd.raw_samples = ctx->raw_samples[c];
  719. for (b = 0; b < ctx->num_blocks; b++) {
  720. bd.shift_lsbs = 0;
  721. bd.block_length = div_blocks[b];
  722. if (read_decode_block(ctx, &bd)) {
  723. // damaged block, write zero for the rest of the frame
  724. zero_remaining(b, ctx->num_blocks, div_blocks, bd.raw_samples);
  725. return -1;
  726. }
  727. bd.raw_samples += div_blocks[b];
  728. bd.ra_block = 0;
  729. }
  730. return 0;
  731. }
  732. /** Decodes blocks dependently.
  733. */
  734. static int decode_blocks(ALSDecContext *ctx, unsigned int ra_frame,
  735. unsigned int c, const unsigned int *div_blocks,
  736. unsigned int *js_blocks)
  737. {
  738. ALSSpecificConfig *sconf = &ctx->sconf;
  739. unsigned int offset = 0;
  740. unsigned int b;
  741. ALSBlockData bd[2];
  742. memset(bd, 0, 2 * sizeof(ALSBlockData));
  743. bd[0].ra_block = ra_frame;
  744. bd[0].use_ltp = ctx->use_ltp;
  745. bd[0].ltp_lag = ctx->ltp_lag;
  746. bd[0].ltp_gain = ctx->ltp_gain[0];
  747. bd[0].quant_cof = ctx->quant_cof;
  748. bd[0].lpc_cof = ctx->lpc_cof;
  749. bd[0].prev_raw_samples = ctx->prev_raw_samples;
  750. bd[0].js_blocks = *js_blocks;
  751. bd[1].ra_block = ra_frame;
  752. bd[1].use_ltp = ctx->use_ltp;
  753. bd[1].ltp_lag = ctx->ltp_lag;
  754. bd[1].ltp_gain = ctx->ltp_gain[0];
  755. bd[1].quant_cof = ctx->quant_cof;
  756. bd[1].lpc_cof = ctx->lpc_cof;
  757. bd[1].prev_raw_samples = ctx->prev_raw_samples;
  758. bd[1].js_blocks = *(js_blocks + 1);
  759. // decode all blocks
  760. for (b = 0; b < ctx->num_blocks; b++) {
  761. unsigned int s;
  762. bd[0].shift_lsbs = 0;
  763. bd[1].shift_lsbs = 0;
  764. bd[0].block_length = div_blocks[b];
  765. bd[1].block_length = div_blocks[b];
  766. bd[0].raw_samples = ctx->raw_samples[c ] + offset;
  767. bd[1].raw_samples = ctx->raw_samples[c + 1] + offset;
  768. bd[0].raw_other = bd[1].raw_samples;
  769. bd[1].raw_other = bd[0].raw_samples;
  770. if(read_decode_block(ctx, &bd[0]) || read_decode_block(ctx, &bd[1])) {
  771. // damaged block, write zero for the rest of the frame
  772. zero_remaining(b, ctx->num_blocks, div_blocks, bd[0].raw_samples);
  773. zero_remaining(b, ctx->num_blocks, div_blocks, bd[1].raw_samples);
  774. return -1;
  775. }
  776. // reconstruct joint-stereo blocks
  777. if (bd[0].js_blocks) {
  778. if (bd[1].js_blocks)
  779. av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel pair!\n");
  780. for (s = 0; s < div_blocks[b]; s++)
  781. bd[0].raw_samples[s] = bd[1].raw_samples[s] - bd[0].raw_samples[s];
  782. } else if (bd[1].js_blocks) {
  783. for (s = 0; s < div_blocks[b]; s++)
  784. bd[1].raw_samples[s] = bd[1].raw_samples[s] + bd[0].raw_samples[s];
  785. }
  786. offset += div_blocks[b];
  787. bd[0].ra_block = 0;
  788. bd[1].ra_block = 0;
  789. }
  790. // store carryover raw samples,
  791. // the others channel raw samples are stored by the calling function.
  792. memmove(ctx->raw_samples[c] - sconf->max_order,
  793. ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
  794. sizeof(*ctx->raw_samples[c]) * sconf->max_order);
  795. return 0;
  796. }
  797. /** Reads the frame data.
  798. */
  799. static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)
  800. {
  801. ALSSpecificConfig *sconf = &ctx->sconf;
  802. AVCodecContext *avctx = ctx->avctx;
  803. GetBitContext *gb = &ctx->gb;
  804. unsigned int div_blocks[32]; ///< block sizes.
  805. unsigned int c;
  806. unsigned int js_blocks[2];
  807. uint32_t bs_info = 0;
  808. // skip the size of the ra unit if present in the frame
  809. if (sconf->ra_flag == RA_FLAG_FRAMES && ra_frame)
  810. skip_bits_long(gb, 32);
  811. if (sconf->mc_coding && sconf->joint_stereo) {
  812. ctx->js_switch = get_bits1(gb);
  813. align_get_bits(gb);
  814. }
  815. if (!sconf->mc_coding || ctx->js_switch) {
  816. int independent_bs = !sconf->joint_stereo;
  817. for (c = 0; c < avctx->channels; c++) {
  818. js_blocks[0] = 0;
  819. js_blocks[1] = 0;
  820. get_block_sizes(ctx, div_blocks, &bs_info);
  821. // if joint_stereo and block_switching is set, independent decoding
  822. // is signaled via the first bit of bs_info
  823. if (sconf->joint_stereo && sconf->block_switching)
  824. if (bs_info >> 31)
  825. independent_bs = 2;
  826. // if this is the last channel, it has to be decoded independently
  827. if (c == avctx->channels - 1)
  828. independent_bs = 1;
  829. if (independent_bs) {
  830. if (decode_blocks_ind(ctx, ra_frame, c, div_blocks, js_blocks))
  831. return -1;
  832. independent_bs--;
  833. } else {
  834. if (decode_blocks(ctx, ra_frame, c, div_blocks, js_blocks))
  835. return -1;
  836. c++;
  837. }
  838. // store carryover raw samples
  839. memmove(ctx->raw_samples[c] - sconf->max_order,
  840. ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
  841. sizeof(*ctx->raw_samples[c]) * sconf->max_order);
  842. }
  843. } else { // multi-channel coding
  844. get_block_sizes(ctx, div_blocks, &bs_info);
  845. // TODO: multi channel coding might use a temporary buffer instead as
  846. // the actual channel is not known when read_block-data is called
  847. if (decode_blocks_ind(ctx, ra_frame, 0, div_blocks, js_blocks))
  848. return -1;
  849. // TODO: read_channel_data
  850. }
  851. // TODO: read_diff_float_data
  852. return 0;
  853. }
  854. /** Decodes an ALS frame.
  855. */
  856. static int decode_frame(AVCodecContext *avctx,
  857. void *data, int *data_size,
  858. AVPacket *avpkt)
  859. {
  860. ALSDecContext *ctx = avctx->priv_data;
  861. ALSSpecificConfig *sconf = &ctx->sconf;
  862. const uint8_t *buffer = avpkt->data;
  863. int buffer_size = avpkt->size;
  864. int invalid_frame, size;
  865. unsigned int c, sample, ra_frame, bytes_read, shift;
  866. init_get_bits(&ctx->gb, buffer, buffer_size * 8);
  867. // In the case that the distance between random access frames is set to zero
  868. // (sconf->ra_distance == 0) no frame is treated as a random access frame.
  869. // For the first frame, if prediction is used, all samples used from the
  870. // previous frame are assumed to be zero.
  871. ra_frame = sconf->ra_distance && !(ctx->frame_id % sconf->ra_distance);
  872. // the last frame to decode might have a different length
  873. if (sconf->samples != 0xFFFFFFFF)
  874. ctx->cur_frame_length = FFMIN(sconf->samples - ctx->frame_id * (uint64_t) sconf->frame_length,
  875. sconf->frame_length);
  876. else
  877. ctx->cur_frame_length = sconf->frame_length;
  878. // decode the frame data
  879. if ((invalid_frame = read_frame_data(ctx, ra_frame) < 0))
  880. av_log(ctx->avctx, AV_LOG_WARNING,
  881. "Reading frame data failed. Skipping RA unit.\n");
  882. ctx->frame_id++;
  883. // check for size of decoded data
  884. size = ctx->cur_frame_length * avctx->channels *
  885. (av_get_bits_per_sample_format(avctx->sample_fmt) >> 3);
  886. if (size > *data_size) {
  887. av_log(avctx, AV_LOG_ERROR, "Decoded data exceeds buffer size.\n");
  888. return -1;
  889. }
  890. *data_size = size;
  891. // transform decoded frame into output format
  892. #define INTERLEAVE_OUTPUT(bps) \
  893. { \
  894. int##bps##_t *dest = (int##bps##_t*) data; \
  895. shift = bps - ctx->avctx->bits_per_raw_sample; \
  896. for (sample = 0; sample < ctx->cur_frame_length; sample++) \
  897. for (c = 0; c < avctx->channels; c++) \
  898. *dest++ = ctx->raw_samples[c][sample] << shift; \
  899. }
  900. if (ctx->avctx->bits_per_raw_sample <= 16) {
  901. INTERLEAVE_OUTPUT(16)
  902. } else {
  903. INTERLEAVE_OUTPUT(32)
  904. }
  905. bytes_read = invalid_frame ? buffer_size :
  906. (get_bits_count(&ctx->gb) + 7) >> 3;
  907. return bytes_read;
  908. }
  909. /** Uninitializes the ALS decoder.
  910. */
  911. static av_cold int decode_end(AVCodecContext *avctx)
  912. {
  913. ALSDecContext *ctx = avctx->priv_data;
  914. av_freep(&ctx->sconf.chan_pos);
  915. av_freep(&ctx->use_ltp);
  916. av_freep(&ctx->ltp_lag);
  917. av_freep(&ctx->ltp_gain);
  918. av_freep(&ctx->ltp_gain_buffer);
  919. av_freep(&ctx->quant_cof);
  920. av_freep(&ctx->lpc_cof);
  921. av_freep(&ctx->prev_raw_samples);
  922. av_freep(&ctx->raw_samples);
  923. av_freep(&ctx->raw_buffer);
  924. return 0;
  925. }
  926. /** Initializes the ALS decoder.
  927. */
  928. static av_cold int decode_init(AVCodecContext *avctx)
  929. {
  930. unsigned int c;
  931. unsigned int channel_size;
  932. int num_buffers;
  933. ALSDecContext *ctx = avctx->priv_data;
  934. ALSSpecificConfig *sconf = &ctx->sconf;
  935. ctx->avctx = avctx;
  936. if (!avctx->extradata) {
  937. av_log(avctx, AV_LOG_ERROR, "Missing required ALS extradata.\n");
  938. return -1;
  939. }
  940. if (read_specific_config(ctx)) {
  941. av_log(avctx, AV_LOG_ERROR, "Reading ALSSpecificConfig failed.\n");
  942. decode_end(avctx);
  943. return -1;
  944. }
  945. if (check_specific_config(ctx)) {
  946. decode_end(avctx);
  947. return -1;
  948. }
  949. if (sconf->floating) {
  950. avctx->sample_fmt = SAMPLE_FMT_FLT;
  951. avctx->bits_per_raw_sample = 32;
  952. } else {
  953. avctx->sample_fmt = sconf->resolution > 1
  954. ? SAMPLE_FMT_S32 : SAMPLE_FMT_S16;
  955. avctx->bits_per_raw_sample = (sconf->resolution + 1) * 8;
  956. }
  957. // set lag value for long-term prediction
  958. ctx->ltp_lag_length = 8 + (avctx->sample_rate >= 96000) +
  959. (avctx->sample_rate >= 192000);
  960. // allocate quantized parcor coefficient buffer
  961. num_buffers = sconf->mc_coding ? avctx->channels : 1;
  962. // allocate and assign lag and gain data buffer for ltp mode
  963. ctx->use_ltp = av_mallocz(sizeof(*ctx->use_ltp) * num_buffers);
  964. ctx->ltp_lag = av_malloc (sizeof(*ctx->ltp_lag) * num_buffers);
  965. ctx->ltp_gain = av_malloc (sizeof(*ctx->ltp_gain) * num_buffers);
  966. ctx->ltp_gain_buffer = av_malloc (sizeof(*ctx->ltp_gain_buffer) *
  967. num_buffers * 5);
  968. if (!ctx->use_ltp || !ctx->ltp_lag ||
  969. !ctx->ltp_gain || !ctx->ltp_gain_buffer) {
  970. av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
  971. decode_end(avctx);
  972. return AVERROR(ENOMEM);
  973. }
  974. for (c = 0; c < num_buffers; c++)
  975. ctx->ltp_gain[c] = ctx->ltp_gain_buffer + c * 5;
  976. avctx->frame_size = sconf->frame_length;
  977. channel_size = sconf->frame_length + sconf->max_order;
  978. ctx->prev_raw_samples = av_malloc (sizeof(*ctx->prev_raw_samples) * sconf->max_order);
  979. ctx->raw_buffer = av_mallocz(sizeof(*ctx-> raw_buffer) * avctx->channels * channel_size);
  980. ctx->raw_samples = av_malloc (sizeof(*ctx-> raw_samples) * avctx->channels);
  981. // allocate previous raw sample buffer
  982. if (!ctx->prev_raw_samples || !ctx->raw_buffer|| !ctx->raw_samples) {
  983. av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
  984. decode_end(avctx);
  985. return AVERROR(ENOMEM);
  986. }
  987. // assign raw samples buffers
  988. ctx->raw_samples[0] = ctx->raw_buffer + sconf->max_order;
  989. for (c = 1; c < avctx->channels; c++)
  990. ctx->raw_samples[c] = ctx->raw_samples[c - 1] + channel_size;
  991. return 0;
  992. }
  993. /** Flushes (resets) the frame ID after seeking.
  994. */
  995. static av_cold void flush(AVCodecContext *avctx)
  996. {
  997. ALSDecContext *ctx = avctx->priv_data;
  998. ctx->frame_id = 0;
  999. }
  1000. AVCodec als_decoder = {
  1001. "als",
  1002. CODEC_TYPE_AUDIO,
  1003. CODEC_ID_MP4ALS,
  1004. sizeof(ALSDecContext),
  1005. decode_init,
  1006. NULL,
  1007. decode_end,
  1008. decode_frame,
  1009. .flush = flush,
  1010. .capabilities = CODEC_CAP_SUBFRAMES,
  1011. .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Audio Lossless Coding (ALS)"),
  1012. };