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.

1300 lines
47KB

  1. /*
  2. * Windows Media Audio Lossless decoder
  3. * Copyright (c) 2007 Baptiste Coudurier, Benjamin Larsson, Ulion
  4. * Copyright (c) 2008 - 2011 Sascha Sommer, Benjamin Larsson
  5. * Copyright (c) 2011 Andreas Öman
  6. * Copyright (c) 2011 - 2012 Mashiat Sarker Shakkhar
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "libavutil/attributes.h"
  25. #include "avcodec.h"
  26. #include "internal.h"
  27. #include "get_bits.h"
  28. #include "put_bits.h"
  29. #include "wma.h"
  30. #include "wma_common.h"
  31. /** current decoder limitations */
  32. #define WMALL_MAX_CHANNELS 8 ///< max number of handled channels
  33. #define MAX_SUBFRAMES 32 ///< max number of subframes per channel
  34. #define MAX_BANDS 29 ///< max number of scale factor bands
  35. #define MAX_FRAMESIZE 32768 ///< maximum compressed frame size
  36. #define MAX_ORDER 256
  37. #define WMALL_BLOCK_MIN_BITS 6 ///< log2 of min block size
  38. #define WMALL_BLOCK_MAX_BITS 12 ///< log2 of max block size
  39. #define WMALL_BLOCK_MAX_SIZE (1 << WMALL_BLOCK_MAX_BITS) ///< maximum block size
  40. #define WMALL_BLOCK_SIZES (WMALL_BLOCK_MAX_BITS - WMALL_BLOCK_MIN_BITS + 1) ///< possible block sizes
  41. /**
  42. * @brief frame-specific decoder context for a single channel
  43. */
  44. typedef struct {
  45. int16_t prev_block_len; ///< length of the previous block
  46. uint8_t transmit_coefs;
  47. uint8_t num_subframes;
  48. uint16_t subframe_len[MAX_SUBFRAMES]; ///< subframe length in samples
  49. uint16_t subframe_offsets[MAX_SUBFRAMES]; ///< subframe positions in the current frame
  50. uint8_t cur_subframe; ///< current subframe number
  51. uint16_t decoded_samples; ///< number of already processed samples
  52. int quant_step; ///< quantization step for the current subframe
  53. int transient_counter; ///< number of transient samples from the beginning of the transient zone
  54. } WmallChannelCtx;
  55. /**
  56. * @brief main decoder context
  57. */
  58. typedef struct WmallDecodeCtx {
  59. /* generic decoder variables */
  60. AVCodecContext *avctx;
  61. AVFrame frame;
  62. uint8_t frame_data[MAX_FRAMESIZE + FF_INPUT_BUFFER_PADDING_SIZE]; ///< compressed frame data
  63. PutBitContext pb; ///< context for filling the frame_data buffer
  64. /* frame size dependent frame information (set during initialization) */
  65. uint32_t decode_flags; ///< used compression features
  66. int len_prefix; ///< frame is prefixed with its length
  67. int dynamic_range_compression; ///< frame contains DRC data
  68. uint8_t bits_per_sample; ///< integer audio sample size for the unscaled IMDCT output (used to scale to [-1.0, 1.0])
  69. uint16_t samples_per_frame; ///< number of samples to output
  70. uint16_t log2_frame_size;
  71. int8_t num_channels; ///< number of channels in the stream (same as AVCodecContext.num_channels)
  72. int8_t lfe_channel; ///< lfe channel index
  73. uint8_t max_num_subframes;
  74. uint8_t subframe_len_bits; ///< number of bits used for the subframe length
  75. uint8_t max_subframe_len_bit; ///< flag indicating that the subframe is of maximum size when the first subframe length bit is 1
  76. uint16_t min_samples_per_subframe;
  77. /* packet decode state */
  78. GetBitContext pgb; ///< bitstream reader context for the packet
  79. int next_packet_start; ///< start offset of the next WMA packet in the demuxer packet
  80. uint8_t packet_offset; ///< offset to the frame in the packet
  81. uint8_t packet_sequence_number; ///< current packet number
  82. int num_saved_bits; ///< saved number of bits
  83. int frame_offset; ///< frame offset in the bit reservoir
  84. int subframe_offset; ///< subframe offset in the bit reservoir
  85. uint8_t packet_loss; ///< set in case of bitstream error
  86. uint8_t packet_done; ///< set when a packet is fully decoded
  87. /* frame decode state */
  88. uint32_t frame_num; ///< current frame number (not used for decoding)
  89. GetBitContext gb; ///< bitstream reader context
  90. int buf_bit_size; ///< buffer size in bits
  91. int16_t *samples_16[WMALL_MAX_CHANNELS]; ///< current samplebuffer pointer (16-bit)
  92. int32_t *samples_32[WMALL_MAX_CHANNELS]; ///< current samplebuffer pointer (24-bit)
  93. uint8_t drc_gain; ///< gain for the DRC tool
  94. int8_t skip_frame; ///< skip output step
  95. int8_t parsed_all_subframes; ///< all subframes decoded?
  96. /* subframe/block decode state */
  97. int16_t subframe_len; ///< current subframe length
  98. int8_t channels_for_cur_subframe; ///< number of channels that contain the subframe
  99. int8_t channel_indexes_for_cur_subframe[WMALL_MAX_CHANNELS];
  100. WmallChannelCtx channel[WMALL_MAX_CHANNELS]; ///< per channel data
  101. // WMA Lossless-specific
  102. uint8_t do_arith_coding;
  103. uint8_t do_ac_filter;
  104. uint8_t do_inter_ch_decorr;
  105. uint8_t do_mclms;
  106. uint8_t do_lpc;
  107. int8_t acfilter_order;
  108. int8_t acfilter_scaling;
  109. int64_t acfilter_coeffs[16];
  110. int acfilter_prevvalues[2][16];
  111. int8_t mclms_order;
  112. int8_t mclms_scaling;
  113. int16_t mclms_coeffs[128];
  114. int16_t mclms_coeffs_cur[4];
  115. int16_t mclms_prevvalues[WMALL_MAX_CHANNELS * 2 * 32];
  116. int16_t mclms_updates[WMALL_MAX_CHANNELS * 2 * 32];
  117. int mclms_recent;
  118. int movave_scaling;
  119. int quant_stepsize;
  120. struct {
  121. int order;
  122. int scaling;
  123. int coefsend;
  124. int bitsend;
  125. int16_t coefs[MAX_ORDER];
  126. int16_t lms_prevvalues[MAX_ORDER * 2];
  127. int16_t lms_updates[MAX_ORDER * 2];
  128. int recent;
  129. } cdlms[2][9];
  130. int cdlms_ttl[2];
  131. int bV3RTM;
  132. int is_channel_coded[2];
  133. int update_speed[2];
  134. int transient[2];
  135. int transient_pos[2];
  136. int seekable_tile;
  137. int ave_sum[2];
  138. int channel_residues[2][WMALL_BLOCK_MAX_SIZE];
  139. int lpc_coefs[2][40];
  140. int lpc_order;
  141. int lpc_scaling;
  142. int lpc_intbits;
  143. int channel_coeffs[2][WMALL_BLOCK_MAX_SIZE];
  144. } WmallDecodeCtx;
  145. static av_cold int decode_init(AVCodecContext *avctx)
  146. {
  147. WmallDecodeCtx *s = avctx->priv_data;
  148. uint8_t *edata_ptr = avctx->extradata;
  149. unsigned int channel_mask;
  150. int i, bits, log2_max_num_subframes, num_possible_block_sizes;
  151. s->avctx = avctx;
  152. init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
  153. if (avctx->extradata_size >= 18) {
  154. s->decode_flags = AV_RL16(edata_ptr + 14);
  155. channel_mask = AV_RL32(edata_ptr + 2);
  156. s->bits_per_sample = AV_RL16(edata_ptr);
  157. if (s->bits_per_sample == 16)
  158. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  159. else if (s->bits_per_sample == 24) {
  160. avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  161. av_log_missing_feature(avctx, "bit-depth higher than 16", 0);
  162. return AVERROR_PATCHWELCOME;
  163. } else {
  164. av_log(avctx, AV_LOG_ERROR, "Unknown bit-depth: %d\n",
  165. s->bits_per_sample);
  166. return AVERROR_INVALIDDATA;
  167. }
  168. /* dump the extradata */
  169. for (i = 0; i < avctx->extradata_size; i++)
  170. av_dlog(avctx, "[%x] ", avctx->extradata[i]);
  171. av_dlog(avctx, "\n");
  172. } else {
  173. av_log_ask_for_sample(avctx, "Unsupported extradata size\n");
  174. return AVERROR_INVALIDDATA;
  175. }
  176. /* generic init */
  177. s->log2_frame_size = av_log2(avctx->block_align) + 4;
  178. /* frame info */
  179. s->skip_frame = 1; /* skip first frame */
  180. s->packet_loss = 1;
  181. s->len_prefix = s->decode_flags & 0x40;
  182. /* get frame len */
  183. bits = ff_wma_get_frame_len_bits(avctx->sample_rate, 3, s->decode_flags);
  184. if (bits > WMALL_BLOCK_MAX_BITS) {
  185. av_log_missing_feature(avctx, "big-bits block sizes", 1);
  186. return AVERROR_INVALIDDATA;
  187. }
  188. s->samples_per_frame = 1 << bits;
  189. /* init previous block len */
  190. for (i = 0; i < avctx->channels; i++)
  191. s->channel[i].prev_block_len = s->samples_per_frame;
  192. /* subframe info */
  193. log2_max_num_subframes = (s->decode_flags & 0x38) >> 3;
  194. s->max_num_subframes = 1 << log2_max_num_subframes;
  195. s->max_subframe_len_bit = 0;
  196. s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
  197. s->min_samples_per_subframe = s->samples_per_frame / s->max_num_subframes;
  198. s->dynamic_range_compression = s->decode_flags & 0x80;
  199. s->bV3RTM = s->decode_flags & 0x100;
  200. if (s->max_num_subframes > MAX_SUBFRAMES) {
  201. av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %i\n",
  202. s->max_num_subframes);
  203. return AVERROR_INVALIDDATA;
  204. }
  205. s->num_channels = avctx->channels;
  206. /* extract lfe channel position */
  207. s->lfe_channel = -1;
  208. if (channel_mask & 8) {
  209. unsigned int mask;
  210. for (mask = 1; mask < 16; mask <<= 1)
  211. if (channel_mask & mask)
  212. ++s->lfe_channel;
  213. }
  214. if (s->num_channels < 0) {
  215. av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n",
  216. s->num_channels);
  217. return AVERROR_INVALIDDATA;
  218. } else if (s->num_channels > WMALL_MAX_CHANNELS) {
  219. av_log_ask_for_sample(avctx, "unsupported number of channels\n");
  220. return AVERROR_PATCHWELCOME;
  221. }
  222. avcodec_get_frame_defaults(&s->frame);
  223. avctx->coded_frame = &s->frame;
  224. avctx->channel_layout = channel_mask;
  225. return 0;
  226. }
  227. /**
  228. * @brief Decode the subframe length.
  229. * @param s context
  230. * @param offset sample offset in the frame
  231. * @return decoded subframe length on success, < 0 in case of an error
  232. */
  233. static int decode_subframe_length(WmallDecodeCtx *s, int offset)
  234. {
  235. int frame_len_ratio, subframe_len, len;
  236. /* no need to read from the bitstream when only one length is possible */
  237. if (offset == s->samples_per_frame - s->min_samples_per_subframe)
  238. return s->min_samples_per_subframe;
  239. len = av_log2(s->max_num_subframes - 1) + 1;
  240. frame_len_ratio = get_bits(&s->gb, len);
  241. subframe_len = s->min_samples_per_subframe * (frame_len_ratio + 1);
  242. /* sanity check the length */
  243. if (subframe_len < s->min_samples_per_subframe ||
  244. subframe_len > s->samples_per_frame) {
  245. av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
  246. subframe_len);
  247. return AVERROR_INVALIDDATA;
  248. }
  249. return subframe_len;
  250. }
  251. /**
  252. * @brief Decode how the data in the frame is split into subframes.
  253. * Every WMA frame contains the encoded data for a fixed number of
  254. * samples per channel. The data for every channel might be split
  255. * into several subframes. This function will reconstruct the list of
  256. * subframes for every channel.
  257. *
  258. * If the subframes are not evenly split, the algorithm estimates the
  259. * channels with the lowest number of total samples.
  260. * Afterwards, for each of these channels a bit is read from the
  261. * bitstream that indicates if the channel contains a subframe with the
  262. * next subframe size that is going to be read from the bitstream or not.
  263. * If a channel contains such a subframe, the subframe size gets added to
  264. * the channel's subframe list.
  265. * The algorithm repeats these steps until the frame is properly divided
  266. * between the individual channels.
  267. *
  268. * @param s context
  269. * @return 0 on success, < 0 in case of an error
  270. */
  271. static int decode_tilehdr(WmallDecodeCtx *s)
  272. {
  273. uint16_t num_samples[WMALL_MAX_CHANNELS] = { 0 }; /* sum of samples for all currently known subframes of a channel */
  274. uint8_t contains_subframe[WMALL_MAX_CHANNELS]; /* flag indicating if a channel contains the current subframe */
  275. int channels_for_cur_subframe = s->num_channels; /* number of channels that contain the current subframe */
  276. int fixed_channel_layout = 0; /* flag indicating that all channels use the same subfra2me offsets and sizes */
  277. int min_channel_len = 0; /* smallest sum of samples (channels with this length will be processed first) */
  278. int c, tile_aligned;
  279. /* reset tiling information */
  280. for (c = 0; c < s->num_channels; c++)
  281. s->channel[c].num_subframes = 0;
  282. tile_aligned = get_bits1(&s->gb);
  283. if (s->max_num_subframes == 1 || tile_aligned)
  284. fixed_channel_layout = 1;
  285. /* loop until the frame data is split between the subframes */
  286. do {
  287. int subframe_len, in_use = 0;
  288. /* check which channels contain the subframe */
  289. for (c = 0; c < s->num_channels; c++) {
  290. if (num_samples[c] == min_channel_len) {
  291. if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
  292. (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe)) {
  293. contains_subframe[c] = in_use = 1;
  294. } else {
  295. if (get_bits1(&s->gb))
  296. contains_subframe[c] = in_use = 1;
  297. }
  298. } else
  299. contains_subframe[c] = 0;
  300. }
  301. if (!in_use) {
  302. av_log(s->avctx, AV_LOG_ERROR,
  303. "Found empty subframe\n");
  304. return AVERROR_INVALIDDATA;
  305. }
  306. /* get subframe length, subframe_len == 0 is not allowed */
  307. if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
  308. return AVERROR_INVALIDDATA;
  309. /* add subframes to the individual channels and find new min_channel_len */
  310. min_channel_len += subframe_len;
  311. for (c = 0; c < s->num_channels; c++) {
  312. WmallChannelCtx *chan = &s->channel[c];
  313. if (contains_subframe[c]) {
  314. if (chan->num_subframes >= MAX_SUBFRAMES) {
  315. av_log(s->avctx, AV_LOG_ERROR,
  316. "broken frame: num subframes > 31\n");
  317. return AVERROR_INVALIDDATA;
  318. }
  319. chan->subframe_len[chan->num_subframes] = subframe_len;
  320. num_samples[c] += subframe_len;
  321. ++chan->num_subframes;
  322. if (num_samples[c] > s->samples_per_frame) {
  323. av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
  324. "channel len(%d) > samples_per_frame(%d)\n",
  325. num_samples[c], s->samples_per_frame);
  326. return AVERROR_INVALIDDATA;
  327. }
  328. } else if (num_samples[c] <= min_channel_len) {
  329. if (num_samples[c] < min_channel_len) {
  330. channels_for_cur_subframe = 0;
  331. min_channel_len = num_samples[c];
  332. }
  333. ++channels_for_cur_subframe;
  334. }
  335. }
  336. } while (min_channel_len < s->samples_per_frame);
  337. for (c = 0; c < s->num_channels; c++) {
  338. int i, offset = 0;
  339. for (i = 0; i < s->channel[c].num_subframes; i++) {
  340. s->channel[c].subframe_offsets[i] = offset;
  341. offset += s->channel[c].subframe_len[i];
  342. }
  343. }
  344. return 0;
  345. }
  346. static void decode_ac_filter(WmallDecodeCtx *s)
  347. {
  348. int i;
  349. s->acfilter_order = get_bits(&s->gb, 4) + 1;
  350. s->acfilter_scaling = get_bits(&s->gb, 4);
  351. for (i = 0; i < s->acfilter_order; i++)
  352. s->acfilter_coeffs[i] = (s->acfilter_scaling ? get_bits(&s->gb, s->acfilter_scaling) : 0) + 1;
  353. }
  354. static void decode_mclms(WmallDecodeCtx *s)
  355. {
  356. s->mclms_order = (get_bits(&s->gb, 4) + 1) * 2;
  357. s->mclms_scaling = get_bits(&s->gb, 4);
  358. if (get_bits1(&s->gb)) {
  359. int i, send_coef_bits;
  360. int cbits = av_log2(s->mclms_scaling + 1);
  361. if (1 << cbits < s->mclms_scaling + 1)
  362. cbits++;
  363. send_coef_bits = (cbits ? get_bits(&s->gb, cbits) : 0) + 2;
  364. for (i = 0; i < s->mclms_order * s->num_channels * s->num_channels; i++)
  365. s->mclms_coeffs[i] = get_bits(&s->gb, send_coef_bits);
  366. for (i = 0; i < s->num_channels; i++) {
  367. int c;
  368. for (c = 0; c < i; c++)
  369. s->mclms_coeffs_cur[i * s->num_channels + c] = get_bits(&s->gb, send_coef_bits);
  370. }
  371. }
  372. }
  373. static int decode_cdlms(WmallDecodeCtx *s)
  374. {
  375. int c, i;
  376. int cdlms_send_coef = get_bits1(&s->gb);
  377. for (c = 0; c < s->num_channels; c++) {
  378. s->cdlms_ttl[c] = get_bits(&s->gb, 3) + 1;
  379. for (i = 0; i < s->cdlms_ttl[c]; i++) {
  380. s->cdlms[c][i].order = (get_bits(&s->gb, 7) + 1) * 8;
  381. if (s->cdlms[c][i].order > MAX_ORDER) {
  382. av_log(s->avctx, AV_LOG_ERROR,
  383. "Order[%d][%d] %d > max (%d), not supported\n",
  384. c, i, s->cdlms[c][i].order, MAX_ORDER);
  385. s->cdlms[0][0].order = 0;
  386. return AVERROR_INVALIDDATA;
  387. }
  388. }
  389. for (i = 0; i < s->cdlms_ttl[c]; i++)
  390. s->cdlms[c][i].scaling = get_bits(&s->gb, 4);
  391. if (cdlms_send_coef) {
  392. for (i = 0; i < s->cdlms_ttl[c]; i++) {
  393. int cbits, shift_l, shift_r, j;
  394. cbits = av_log2(s->cdlms[c][i].order);
  395. if ((1 << cbits) < s->cdlms[c][i].order)
  396. cbits++;
  397. s->cdlms[c][i].coefsend = get_bits(&s->gb, cbits) + 1;
  398. cbits = av_log2(s->cdlms[c][i].scaling + 1);
  399. if ((1 << cbits) < s->cdlms[c][i].scaling + 1)
  400. cbits++;
  401. s->cdlms[c][i].bitsend = get_bits(&s->gb, cbits) + 2;
  402. shift_l = 32 - s->cdlms[c][i].bitsend;
  403. shift_r = 32 - s->cdlms[c][i].scaling - 2;
  404. for (j = 0; j < s->cdlms[c][i].coefsend; j++)
  405. s->cdlms[c][i].coefs[j] =
  406. (get_bits(&s->gb, s->cdlms[c][i].bitsend) << shift_l) >> shift_r;
  407. }
  408. }
  409. }
  410. return 0;
  411. }
  412. static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
  413. {
  414. int i = 0;
  415. unsigned int ave_mean;
  416. s->transient[ch] = get_bits1(&s->gb);
  417. if (s->transient[ch]) {
  418. s->transient_pos[ch] = get_bits(&s->gb, av_log2(tile_size));
  419. if (s->transient_pos[ch])
  420. s->transient[ch] = 0;
  421. s->channel[ch].transient_counter =
  422. FFMAX(s->channel[ch].transient_counter, s->samples_per_frame / 2);
  423. } else if (s->channel[ch].transient_counter)
  424. s->transient[ch] = 1;
  425. if (s->seekable_tile) {
  426. ave_mean = get_bits(&s->gb, s->bits_per_sample);
  427. s->ave_sum[ch] = ave_mean << (s->movave_scaling + 1);
  428. }
  429. if (s->seekable_tile) {
  430. if (s->do_inter_ch_decorr)
  431. s->channel_residues[ch][0] = get_sbits_long(&s->gb, s->bits_per_sample + 1);
  432. else
  433. s->channel_residues[ch][0] = get_sbits_long(&s->gb, s->bits_per_sample);
  434. i++;
  435. }
  436. for (; i < tile_size; i++) {
  437. int quo = 0, rem, rem_bits, residue;
  438. while(get_bits1(&s->gb)) {
  439. quo++;
  440. if (get_bits_left(&s->gb) <= 0)
  441. return -1;
  442. }
  443. if (quo >= 32)
  444. quo += get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1);
  445. ave_mean = (s->ave_sum[ch] + (1 << s->movave_scaling)) >> (s->movave_scaling + 1);
  446. if (ave_mean <= 1)
  447. residue = quo;
  448. else {
  449. rem_bits = av_ceil_log2(ave_mean);
  450. rem = get_bits_long(&s->gb, rem_bits);
  451. residue = (quo << rem_bits) + rem;
  452. }
  453. s->ave_sum[ch] = residue + s->ave_sum[ch] -
  454. (s->ave_sum[ch] >> s->movave_scaling);
  455. if (residue & 1)
  456. residue = -(residue >> 1) - 1;
  457. else
  458. residue = residue >> 1;
  459. s->channel_residues[ch][i] = residue;
  460. }
  461. return 0;
  462. }
  463. static void decode_lpc(WmallDecodeCtx *s)
  464. {
  465. int ch, i, cbits;
  466. s->lpc_order = get_bits(&s->gb, 5) + 1;
  467. s->lpc_scaling = get_bits(&s->gb, 4);
  468. s->lpc_intbits = get_bits(&s->gb, 3) + 1;
  469. cbits = s->lpc_scaling + s->lpc_intbits;
  470. for (ch = 0; ch < s->num_channels; ch++)
  471. for (i = 0; i < s->lpc_order; i++)
  472. s->lpc_coefs[ch][i] = get_sbits(&s->gb, cbits);
  473. }
  474. static void clear_codec_buffers(WmallDecodeCtx *s)
  475. {
  476. int ich, ilms;
  477. memset(s->acfilter_coeffs, 0, sizeof(s->acfilter_coeffs));
  478. memset(s->acfilter_prevvalues, 0, sizeof(s->acfilter_prevvalues));
  479. memset(s->lpc_coefs, 0, sizeof(s->lpc_coefs));
  480. memset(s->mclms_coeffs, 0, sizeof(s->mclms_coeffs));
  481. memset(s->mclms_coeffs_cur, 0, sizeof(s->mclms_coeffs_cur));
  482. memset(s->mclms_prevvalues, 0, sizeof(s->mclms_prevvalues));
  483. memset(s->mclms_updates, 0, sizeof(s->mclms_updates));
  484. for (ich = 0; ich < s->num_channels; ich++) {
  485. for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++) {
  486. memset(s->cdlms[ich][ilms].coefs, 0,
  487. sizeof(s->cdlms[ich][ilms].coefs));
  488. memset(s->cdlms[ich][ilms].lms_prevvalues, 0,
  489. sizeof(s->cdlms[ich][ilms].lms_prevvalues));
  490. memset(s->cdlms[ich][ilms].lms_updates, 0,
  491. sizeof(s->cdlms[ich][ilms].lms_updates));
  492. }
  493. s->ave_sum[ich] = 0;
  494. }
  495. }
  496. /**
  497. * @brief Reset filter parameters and transient area at new seekable tile.
  498. */
  499. static void reset_codec(WmallDecodeCtx *s)
  500. {
  501. int ich, ilms;
  502. s->mclms_recent = s->mclms_order * s->num_channels;
  503. for (ich = 0; ich < s->num_channels; ich++) {
  504. for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++)
  505. s->cdlms[ich][ilms].recent = s->cdlms[ich][ilms].order;
  506. /* first sample of a seekable subframe is considered as the starting of
  507. a transient area which is samples_per_frame samples long */
  508. s->channel[ich].transient_counter = s->samples_per_frame;
  509. s->transient[ich] = 1;
  510. s->transient_pos[ich] = 0;
  511. }
  512. }
  513. static void mclms_update(WmallDecodeCtx *s, int icoef, int *pred)
  514. {
  515. int i, j, ich, pred_error;
  516. int order = s->mclms_order;
  517. int num_channels = s->num_channels;
  518. int range = 1 << (s->bits_per_sample - 1);
  519. for (ich = 0; ich < num_channels; ich++) {
  520. pred_error = s->channel_residues[ich][icoef] - pred[ich];
  521. if (pred_error > 0) {
  522. for (i = 0; i < order * num_channels; i++)
  523. s->mclms_coeffs[i + ich * order * num_channels] +=
  524. s->mclms_updates[s->mclms_recent + i];
  525. for (j = 0; j < ich; j++) {
  526. if (s->channel_residues[j][icoef] > 0)
  527. s->mclms_coeffs_cur[ich * num_channels + j] += 1;
  528. else if (s->channel_residues[j][icoef] < 0)
  529. s->mclms_coeffs_cur[ich * num_channels + j] -= 1;
  530. }
  531. } else if (pred_error < 0) {
  532. for (i = 0; i < order * num_channels; i++)
  533. s->mclms_coeffs[i + ich * order * num_channels] -=
  534. s->mclms_updates[s->mclms_recent + i];
  535. for (j = 0; j < ich; j++) {
  536. if (s->channel_residues[j][icoef] > 0)
  537. s->mclms_coeffs_cur[ich * num_channels + j] -= 1;
  538. else if (s->channel_residues[j][icoef] < 0)
  539. s->mclms_coeffs_cur[ich * num_channels + j] += 1;
  540. }
  541. }
  542. }
  543. for (ich = num_channels - 1; ich >= 0; ich--) {
  544. s->mclms_recent--;
  545. s->mclms_prevvalues[s->mclms_recent] = s->channel_residues[ich][icoef];
  546. if (s->channel_residues[ich][icoef] > range - 1)
  547. s->mclms_prevvalues[s->mclms_recent] = range - 1;
  548. else if (s->channel_residues[ich][icoef] < -range)
  549. s->mclms_prevvalues[s->mclms_recent] = -range;
  550. s->mclms_updates[s->mclms_recent] = 0;
  551. if (s->channel_residues[ich][icoef] > 0)
  552. s->mclms_updates[s->mclms_recent] = 1;
  553. else if (s->channel_residues[ich][icoef] < 0)
  554. s->mclms_updates[s->mclms_recent] = -1;
  555. }
  556. if (s->mclms_recent == 0) {
  557. memcpy(&s->mclms_prevvalues[order * num_channels],
  558. s->mclms_prevvalues,
  559. 2 * order * num_channels);
  560. memcpy(&s->mclms_updates[order * num_channels],
  561. s->mclms_updates,
  562. 2 * order * num_channels);
  563. s->mclms_recent = num_channels * order;
  564. }
  565. }
  566. static void mclms_predict(WmallDecodeCtx *s, int icoef, int *pred)
  567. {
  568. int ich, i;
  569. int order = s->mclms_order;
  570. int num_channels = s->num_channels;
  571. for (ich = 0; ich < num_channels; ich++) {
  572. pred[ich] = 0;
  573. if (!s->is_channel_coded[ich])
  574. continue;
  575. for (i = 0; i < order * num_channels; i++)
  576. pred[ich] += s->mclms_prevvalues[i + s->mclms_recent] *
  577. s->mclms_coeffs[i + order * num_channels * ich];
  578. for (i = 0; i < ich; i++)
  579. pred[ich] += s->channel_residues[i][icoef] *
  580. s->mclms_coeffs_cur[i + num_channels * ich];
  581. pred[ich] += 1 << s->mclms_scaling - 1;
  582. pred[ich] >>= s->mclms_scaling;
  583. s->channel_residues[ich][icoef] += pred[ich];
  584. }
  585. }
  586. static void revert_mclms(WmallDecodeCtx *s, int tile_size)
  587. {
  588. int icoef, pred[WMALL_MAX_CHANNELS] = { 0 };
  589. for (icoef = 0; icoef < tile_size; icoef++) {
  590. mclms_predict(s, icoef, pred);
  591. mclms_update(s, icoef, pred);
  592. }
  593. }
  594. static int lms_predict(WmallDecodeCtx *s, int ich, int ilms)
  595. {
  596. int pred = 0, icoef;
  597. int recent = s->cdlms[ich][ilms].recent;
  598. for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
  599. pred += s->cdlms[ich][ilms].coefs[icoef] *
  600. s->cdlms[ich][ilms].lms_prevvalues[icoef + recent];
  601. return pred;
  602. }
  603. static void lms_update(WmallDecodeCtx *s, int ich, int ilms,
  604. int input, int residue)
  605. {
  606. int icoef;
  607. int recent = s->cdlms[ich][ilms].recent;
  608. int range = 1 << s->bits_per_sample - 1;
  609. if (residue < 0) {
  610. for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
  611. s->cdlms[ich][ilms].coefs[icoef] -=
  612. s->cdlms[ich][ilms].lms_updates[icoef + recent];
  613. } else if (residue > 0) {
  614. for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
  615. s->cdlms[ich][ilms].coefs[icoef] +=
  616. s->cdlms[ich][ilms].lms_updates[icoef + recent];
  617. }
  618. if (recent)
  619. recent--;
  620. else {
  621. memcpy(&s->cdlms[ich][ilms].lms_prevvalues[s->cdlms[ich][ilms].order],
  622. s->cdlms[ich][ilms].lms_prevvalues,
  623. 2 * s->cdlms[ich][ilms].order);
  624. memcpy(&s->cdlms[ich][ilms].lms_updates[s->cdlms[ich][ilms].order],
  625. s->cdlms[ich][ilms].lms_updates,
  626. 2 * s->cdlms[ich][ilms].order);
  627. recent = s->cdlms[ich][ilms].order - 1;
  628. }
  629. s->cdlms[ich][ilms].lms_prevvalues[recent] = av_clip(input, -range, range - 1);
  630. if (!input)
  631. s->cdlms[ich][ilms].lms_updates[recent] = 0;
  632. else if (input < 0)
  633. s->cdlms[ich][ilms].lms_updates[recent] = -s->update_speed[ich];
  634. else
  635. s->cdlms[ich][ilms].lms_updates[recent] = s->update_speed[ich];
  636. s->cdlms[ich][ilms].lms_updates[recent + (s->cdlms[ich][ilms].order >> 4)] >>= 2;
  637. s->cdlms[ich][ilms].lms_updates[recent + (s->cdlms[ich][ilms].order >> 3)] >>= 1;
  638. s->cdlms[ich][ilms].recent = recent;
  639. }
  640. static void use_high_update_speed(WmallDecodeCtx *s, int ich)
  641. {
  642. int ilms, recent, icoef;
  643. for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
  644. recent = s->cdlms[ich][ilms].recent;
  645. if (s->update_speed[ich] == 16)
  646. continue;
  647. if (s->bV3RTM) {
  648. for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
  649. s->cdlms[ich][ilms].lms_updates[icoef + recent] *= 2;
  650. } else {
  651. for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
  652. s->cdlms[ich][ilms].lms_updates[icoef] *= 2;
  653. }
  654. }
  655. s->update_speed[ich] = 16;
  656. }
  657. static void use_normal_update_speed(WmallDecodeCtx *s, int ich)
  658. {
  659. int ilms, recent, icoef;
  660. for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
  661. recent = s->cdlms[ich][ilms].recent;
  662. if (s->update_speed[ich] == 8)
  663. continue;
  664. if (s->bV3RTM)
  665. for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
  666. s->cdlms[ich][ilms].lms_updates[icoef + recent] /= 2;
  667. else
  668. for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
  669. s->cdlms[ich][ilms].lms_updates[icoef] /= 2;
  670. }
  671. s->update_speed[ich] = 8;
  672. }
  673. static void revert_cdlms(WmallDecodeCtx *s, int ch,
  674. int coef_begin, int coef_end)
  675. {
  676. int icoef, pred, ilms, num_lms, residue, input;
  677. num_lms = s->cdlms_ttl[ch];
  678. for (ilms = num_lms - 1; ilms >= 0; ilms--) {
  679. for (icoef = coef_begin; icoef < coef_end; icoef++) {
  680. pred = 1 << (s->cdlms[ch][ilms].scaling - 1);
  681. residue = s->channel_residues[ch][icoef];
  682. pred += lms_predict(s, ch, ilms);
  683. input = residue + (pred >> s->cdlms[ch][ilms].scaling);
  684. lms_update(s, ch, ilms, input, residue);
  685. s->channel_residues[ch][icoef] = input;
  686. }
  687. }
  688. }
  689. static void revert_inter_ch_decorr(WmallDecodeCtx *s, int tile_size)
  690. {
  691. if (s->num_channels != 2)
  692. return;
  693. else if (s->is_channel_coded[0] || s->is_channel_coded[1]) {
  694. int icoef;
  695. for (icoef = 0; icoef < tile_size; icoef++) {
  696. s->channel_residues[0][icoef] -= s->channel_residues[1][icoef] >> 1;
  697. s->channel_residues[1][icoef] += s->channel_residues[0][icoef];
  698. }
  699. }
  700. }
  701. static void revert_acfilter(WmallDecodeCtx *s, int tile_size)
  702. {
  703. int ich, pred, i, j;
  704. int64_t *filter_coeffs = s->acfilter_coeffs;
  705. int scaling = s->acfilter_scaling;
  706. int order = s->acfilter_order;
  707. for (ich = 0; ich < s->num_channels; ich++) {
  708. int *prevvalues = s->acfilter_prevvalues[ich];
  709. for (i = 0; i < order; i++) {
  710. pred = 0;
  711. for (j = 0; j < order; j++) {
  712. if (i <= j)
  713. pred += filter_coeffs[j] * prevvalues[j - i];
  714. else
  715. pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
  716. }
  717. pred >>= scaling;
  718. s->channel_residues[ich][i] += pred;
  719. }
  720. for (i = order; i < tile_size; i++) {
  721. pred = 0;
  722. for (j = 0; j < order; j++)
  723. pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
  724. pred >>= scaling;
  725. s->channel_residues[ich][i] += pred;
  726. }
  727. for (j = 0; j < order; j++)
  728. prevvalues[j] = s->channel_residues[ich][tile_size - j - 1];
  729. }
  730. }
  731. static int decode_subframe(WmallDecodeCtx *s)
  732. {
  733. int offset = s->samples_per_frame;
  734. int subframe_len = s->samples_per_frame;
  735. int total_samples = s->samples_per_frame * s->num_channels;
  736. int i, j, rawpcm_tile, padding_zeroes, res;
  737. s->subframe_offset = get_bits_count(&s->gb);
  738. /* reset channel context and find the next block offset and size
  739. == the next block of the channel with the smallest number of
  740. decoded samples */
  741. for (i = 0; i < s->num_channels; i++) {
  742. if (offset > s->channel[i].decoded_samples) {
  743. offset = s->channel[i].decoded_samples;
  744. subframe_len =
  745. s->channel[i].subframe_len[s->channel[i].cur_subframe];
  746. }
  747. }
  748. /* get a list of all channels that contain the estimated block */
  749. s->channels_for_cur_subframe = 0;
  750. for (i = 0; i < s->num_channels; i++) {
  751. const int cur_subframe = s->channel[i].cur_subframe;
  752. /* subtract already processed samples */
  753. total_samples -= s->channel[i].decoded_samples;
  754. /* and count if there are multiple subframes that match our profile */
  755. if (offset == s->channel[i].decoded_samples &&
  756. subframe_len == s->channel[i].subframe_len[cur_subframe]) {
  757. total_samples -= s->channel[i].subframe_len[cur_subframe];
  758. s->channel[i].decoded_samples +=
  759. s->channel[i].subframe_len[cur_subframe];
  760. s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
  761. ++s->channels_for_cur_subframe;
  762. }
  763. }
  764. /* check if the frame will be complete after processing the
  765. estimated block */
  766. if (!total_samples)
  767. s->parsed_all_subframes = 1;
  768. s->seekable_tile = get_bits1(&s->gb);
  769. if (s->seekable_tile) {
  770. clear_codec_buffers(s);
  771. s->do_arith_coding = get_bits1(&s->gb);
  772. if (s->do_arith_coding) {
  773. av_log_missing_feature(s->avctx, "arithmetic coding", 1);
  774. return AVERROR_PATCHWELCOME;
  775. }
  776. s->do_ac_filter = get_bits1(&s->gb);
  777. s->do_inter_ch_decorr = get_bits1(&s->gb);
  778. s->do_mclms = get_bits1(&s->gb);
  779. if (s->do_ac_filter)
  780. decode_ac_filter(s);
  781. if (s->do_mclms)
  782. decode_mclms(s);
  783. if ((res = decode_cdlms(s)) < 0)
  784. return res;
  785. s->movave_scaling = get_bits(&s->gb, 3);
  786. s->quant_stepsize = get_bits(&s->gb, 8) + 1;
  787. reset_codec(s);
  788. } else if (!s->cdlms[0][0].order) {
  789. av_log(s->avctx, AV_LOG_DEBUG,
  790. "Waiting for seekable tile\n");
  791. s->frame.nb_samples = 0;
  792. return -1;
  793. }
  794. rawpcm_tile = get_bits1(&s->gb);
  795. for (i = 0; i < s->num_channels; i++)
  796. s->is_channel_coded[i] = 1;
  797. if (!rawpcm_tile) {
  798. for (i = 0; i < s->num_channels; i++)
  799. s->is_channel_coded[i] = get_bits1(&s->gb);
  800. if (s->bV3RTM) {
  801. // LPC
  802. s->do_lpc = get_bits1(&s->gb);
  803. if (s->do_lpc) {
  804. decode_lpc(s);
  805. av_log_ask_for_sample(s->avctx, "Inverse LPC filter not "
  806. "implemented. Expect wrong output.\n");
  807. }
  808. } else
  809. s->do_lpc = 0;
  810. }
  811. if (get_bits1(&s->gb))
  812. padding_zeroes = get_bits(&s->gb, 5);
  813. else
  814. padding_zeroes = 0;
  815. if (rawpcm_tile) {
  816. int bits = s->bits_per_sample - padding_zeroes;
  817. if (bits <= 0) {
  818. av_log(s->avctx, AV_LOG_ERROR,
  819. "Invalid number of padding bits in raw PCM tile\n");
  820. return AVERROR_INVALIDDATA;
  821. }
  822. av_dlog(s->avctx, "RAWPCM %d bits per sample. "
  823. "total %d bits, remain=%d\n", bits,
  824. bits * s->num_channels * subframe_len, get_bits_count(&s->gb));
  825. for (i = 0; i < s->num_channels; i++)
  826. for (j = 0; j < subframe_len; j++)
  827. s->channel_coeffs[i][j] = get_sbits_long(&s->gb, bits);
  828. } else {
  829. for (i = 0; i < s->num_channels; i++)
  830. if (s->is_channel_coded[i]) {
  831. decode_channel_residues(s, i, subframe_len);
  832. if (s->seekable_tile)
  833. use_high_update_speed(s, i);
  834. else
  835. use_normal_update_speed(s, i);
  836. revert_cdlms(s, i, 0, subframe_len);
  837. } else {
  838. memset(s->channel_residues[i], 0, sizeof(**s->channel_residues) * subframe_len);
  839. }
  840. }
  841. if (s->do_mclms)
  842. revert_mclms(s, subframe_len);
  843. if (s->do_inter_ch_decorr)
  844. revert_inter_ch_decorr(s, subframe_len);
  845. if (s->do_ac_filter)
  846. revert_acfilter(s, subframe_len);
  847. /* Dequantize */
  848. if (s->quant_stepsize != 1)
  849. for (i = 0; i < s->num_channels; i++)
  850. for (j = 0; j < subframe_len; j++)
  851. s->channel_residues[i][j] *= s->quant_stepsize;
  852. /* Write to proper output buffer depending on bit-depth */
  853. for (i = 0; i < s->channels_for_cur_subframe; i++) {
  854. int c = s->channel_indexes_for_cur_subframe[i];
  855. int subframe_len = s->channel[c].subframe_len[s->channel[c].cur_subframe];
  856. for (j = 0; j < subframe_len; j++) {
  857. if (s->bits_per_sample == 16) {
  858. *s->samples_16[c] = (int16_t) s->channel_residues[c][j] << padding_zeroes;
  859. s->samples_16[c] += s->num_channels;
  860. } else {
  861. *s->samples_32[c] = s->channel_residues[c][j] << padding_zeroes;
  862. s->samples_32[c] += s->num_channels;
  863. }
  864. }
  865. }
  866. /* handled one subframe */
  867. for (i = 0; i < s->channels_for_cur_subframe; i++) {
  868. int c = s->channel_indexes_for_cur_subframe[i];
  869. if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
  870. av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
  871. return AVERROR_INVALIDDATA;
  872. }
  873. ++s->channel[c].cur_subframe;
  874. }
  875. return 0;
  876. }
  877. /**
  878. * @brief Decode one WMA frame.
  879. * @param s codec context
  880. * @return 0 if the trailer bit indicates that this is the last frame,
  881. * 1 if there are additional frames
  882. */
  883. static int decode_frame(WmallDecodeCtx *s)
  884. {
  885. GetBitContext* gb = &s->gb;
  886. int more_frames = 0, len = 0, i, ret;
  887. s->frame.nb_samples = s->samples_per_frame;
  888. if ((ret = s->avctx->get_buffer(s->avctx, &s->frame)) < 0) {
  889. /* return an error if no frame could be decoded at all */
  890. av_log(s->avctx, AV_LOG_ERROR,
  891. "not enough space for the output samples\n");
  892. s->packet_loss = 1;
  893. return ret;
  894. }
  895. for (i = 0; i < s->num_channels; i++) {
  896. s->samples_16[i] = (int16_t *)s->frame.data[0] + i;
  897. s->samples_32[i] = (int32_t *)s->frame.data[0] + i;
  898. }
  899. /* get frame length */
  900. if (s->len_prefix)
  901. len = get_bits(gb, s->log2_frame_size);
  902. /* decode tile information */
  903. if (decode_tilehdr(s)) {
  904. s->packet_loss = 1;
  905. return 0;
  906. }
  907. /* read drc info */
  908. if (s->dynamic_range_compression)
  909. s->drc_gain = get_bits(gb, 8);
  910. /* no idea what these are for, might be the number of samples
  911. that need to be skipped at the beginning or end of a stream */
  912. if (get_bits1(gb)) {
  913. int av_unused skip;
  914. /* usually true for the first frame */
  915. if (get_bits1(gb)) {
  916. skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
  917. av_dlog(s->avctx, "start skip: %i\n", skip);
  918. }
  919. /* sometimes true for the last frame */
  920. if (get_bits1(gb)) {
  921. skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
  922. av_dlog(s->avctx, "end skip: %i\n", skip);
  923. }
  924. }
  925. /* reset subframe states */
  926. s->parsed_all_subframes = 0;
  927. for (i = 0; i < s->num_channels; i++) {
  928. s->channel[i].decoded_samples = 0;
  929. s->channel[i].cur_subframe = 0;
  930. }
  931. /* decode all subframes */
  932. while (!s->parsed_all_subframes) {
  933. if (decode_subframe(s) < 0) {
  934. s->packet_loss = 1;
  935. return 0;
  936. }
  937. }
  938. av_dlog(s->avctx, "Frame done\n");
  939. if (s->skip_frame)
  940. s->skip_frame = 0;
  941. if (s->len_prefix) {
  942. if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
  943. /* FIXME: not sure if this is always an error */
  944. av_log(s->avctx, AV_LOG_ERROR,
  945. "frame[%i] would have to skip %i bits\n", s->frame_num,
  946. len - (get_bits_count(gb) - s->frame_offset) - 1);
  947. s->packet_loss = 1;
  948. return 0;
  949. }
  950. /* skip the rest of the frame data */
  951. skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
  952. }
  953. /* decode trailer bit */
  954. more_frames = get_bits1(gb);
  955. ++s->frame_num;
  956. return more_frames;
  957. }
  958. /**
  959. * @brief Calculate remaining input buffer length.
  960. * @param s codec context
  961. * @param gb bitstream reader context
  962. * @return remaining size in bits
  963. */
  964. static int remaining_bits(WmallDecodeCtx *s, GetBitContext *gb)
  965. {
  966. return s->buf_bit_size - get_bits_count(gb);
  967. }
  968. /**
  969. * @brief Fill the bit reservoir with a (partial) frame.
  970. * @param s codec context
  971. * @param gb bitstream reader context
  972. * @param len length of the partial frame
  973. * @param append decides whether to reset the buffer or not
  974. */
  975. static void save_bits(WmallDecodeCtx *s, GetBitContext* gb, int len,
  976. int append)
  977. {
  978. int buflen;
  979. PutBitContext tmp;
  980. /* when the frame data does not need to be concatenated, the input buffer
  981. is reset and additional bits from the previous frame are copied
  982. and skipped later so that a fast byte copy is possible */
  983. if (!append) {
  984. s->frame_offset = get_bits_count(gb) & 7;
  985. s->num_saved_bits = s->frame_offset;
  986. init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
  987. }
  988. buflen = (s->num_saved_bits + len + 8) >> 3;
  989. if (len <= 0 || buflen > MAX_FRAMESIZE) {
  990. av_log_ask_for_sample(s->avctx, "input buffer too small\n");
  991. s->packet_loss = 1;
  992. return;
  993. }
  994. s->num_saved_bits += len;
  995. if (!append) {
  996. avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
  997. s->num_saved_bits);
  998. } else {
  999. int align = 8 - (get_bits_count(gb) & 7);
  1000. align = FFMIN(align, len);
  1001. put_bits(&s->pb, align, get_bits(gb, align));
  1002. len -= align;
  1003. avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
  1004. }
  1005. skip_bits_long(gb, len);
  1006. tmp = s->pb;
  1007. flush_put_bits(&tmp);
  1008. init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
  1009. skip_bits(&s->gb, s->frame_offset);
  1010. }
  1011. static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr,
  1012. AVPacket* avpkt)
  1013. {
  1014. WmallDecodeCtx *s = avctx->priv_data;
  1015. GetBitContext* gb = &s->pgb;
  1016. const uint8_t* buf = avpkt->data;
  1017. int buf_size = avpkt->size;
  1018. int num_bits_prev_frame, packet_sequence_number, spliced_packet;
  1019. s->frame.nb_samples = 0;
  1020. if (s->packet_done || s->packet_loss) {
  1021. s->packet_done = 0;
  1022. /* sanity check for the buffer length */
  1023. if (buf_size < avctx->block_align)
  1024. return 0;
  1025. s->next_packet_start = buf_size - avctx->block_align;
  1026. buf_size = avctx->block_align;
  1027. s->buf_bit_size = buf_size << 3;
  1028. /* parse packet header */
  1029. init_get_bits(gb, buf, s->buf_bit_size);
  1030. packet_sequence_number = get_bits(gb, 4);
  1031. skip_bits(gb, 1); // Skip seekable_frame_in_packet, currently ununused
  1032. spliced_packet = get_bits1(gb);
  1033. if (spliced_packet)
  1034. av_log_missing_feature(avctx, "Bitstream splicing", 1);
  1035. /* get number of bits that need to be added to the previous frame */
  1036. num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
  1037. /* check for packet loss */
  1038. if (!s->packet_loss &&
  1039. ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
  1040. s->packet_loss = 1;
  1041. av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n",
  1042. s->packet_sequence_number, packet_sequence_number);
  1043. }
  1044. s->packet_sequence_number = packet_sequence_number;
  1045. if (num_bits_prev_frame > 0) {
  1046. int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
  1047. if (num_bits_prev_frame >= remaining_packet_bits) {
  1048. num_bits_prev_frame = remaining_packet_bits;
  1049. s->packet_done = 1;
  1050. }
  1051. /* Append the previous frame data to the remaining data from the
  1052. * previous packet to create a full frame. */
  1053. save_bits(s, gb, num_bits_prev_frame, 1);
  1054. /* decode the cross packet frame if it is valid */
  1055. if (num_bits_prev_frame < remaining_packet_bits && !s->packet_loss)
  1056. decode_frame(s);
  1057. } else if (s->num_saved_bits - s->frame_offset) {
  1058. av_dlog(avctx, "ignoring %x previously saved bits\n",
  1059. s->num_saved_bits - s->frame_offset);
  1060. }
  1061. if (s->packet_loss) {
  1062. /* Reset number of saved bits so that the decoder does not start
  1063. * to decode incomplete frames in the s->len_prefix == 0 case. */
  1064. s->num_saved_bits = 0;
  1065. init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
  1066. s->packet_loss = 0;
  1067. }
  1068. } else {
  1069. int frame_size;
  1070. s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
  1071. init_get_bits(gb, avpkt->data, s->buf_bit_size);
  1072. skip_bits(gb, s->packet_offset);
  1073. if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
  1074. (frame_size = show_bits(gb, s->log2_frame_size)) &&
  1075. frame_size <= remaining_bits(s, gb)) {
  1076. save_bits(s, gb, frame_size, 0);
  1077. s->packet_done = !decode_frame(s);
  1078. } else if (!s->len_prefix
  1079. && s->num_saved_bits > get_bits_count(&s->gb)) {
  1080. /* when the frames do not have a length prefix, we don't know the
  1081. * compressed length of the individual frames however, we know what
  1082. * part of a new packet belongs to the previous frame therefore we
  1083. * save the incoming packet first, then we append the "previous
  1084. * frame" data from the next packet so that we get a buffer that
  1085. * only contains full frames */
  1086. s->packet_done = !decode_frame(s);
  1087. } else {
  1088. s->packet_done = 1;
  1089. }
  1090. }
  1091. if (s->packet_done && !s->packet_loss &&
  1092. remaining_bits(s, gb) > 0) {
  1093. /* save the rest of the data so that it can be decoded
  1094. * with the next packet */
  1095. save_bits(s, gb, remaining_bits(s, gb), 0);
  1096. }
  1097. *(AVFrame *)data = s->frame;
  1098. *got_frame_ptr = s->frame.nb_samples > 0;
  1099. s->packet_offset = get_bits_count(gb) & 7;
  1100. return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
  1101. }
  1102. static void flush(AVCodecContext *avctx)
  1103. {
  1104. WmallDecodeCtx *s = avctx->priv_data;
  1105. s->packet_loss = 1;
  1106. s->packet_done = 0;
  1107. s->num_saved_bits = 0;
  1108. init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
  1109. s->frame_offset = 0;
  1110. s->next_packet_start = 0;
  1111. s->cdlms[0][0].order = 0;
  1112. s->frame.nb_samples = 0;
  1113. }
  1114. AVCodec ff_wmalossless_decoder = {
  1115. .name = "wmalossless",
  1116. .type = AVMEDIA_TYPE_AUDIO,
  1117. .id = AV_CODEC_ID_WMALOSSLESS,
  1118. .priv_data_size = sizeof(WmallDecodeCtx),
  1119. .init = decode_init,
  1120. .decode = decode_packet,
  1121. .flush = flush,
  1122. .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1 | CODEC_CAP_DELAY,
  1123. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio Lossless"),
  1124. };