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.

1249 lines
45KB

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