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.

1522 lines
52KB

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