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.

1721 lines
59KB

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