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.

1627 lines
55KB

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