wmalosslessdec.c 47 KB

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