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
25 #include <inttypes.h>
26
30
38
39 /** current decoder limitations */
40 #define WMALL_MAX_CHANNELS 8 ///< max number of handled channels
41 #define MAX_SUBFRAMES 32
///< max number of subframes per channel
42 #define MAX_BANDS 29
///< max number of scale factor bands
43 #define MAX_FRAMESIZE 32768
///< maximum compressed frame size
45
46 #define WMALL_BLOCK_MIN_BITS 6 ///< log2 of min block size
47 #define WMALL_BLOCK_MAX_BITS 14
///< log2 of max block size
48 #define WMALL_BLOCK_MAX_SIZE (1 << WMALL_BLOCK_MAX_BITS)
///< maximum block size
49 #define WMALL_BLOCK_SIZES (WMALL_BLOCK_MAX_BITS - WMALL_BLOCK_MIN_BITS + 1)
///< possible block sizes
50
51 #define WMALL_COEFF_PAD_SIZE 16 ///< pad coef buffers with 0 for use with SIMD
52
53 /**
54 * @brief frame-specific decoder context for a single channel
55 */
64 int quant_step;
///< quantization step for the current subframe
65 int transient_counter;
///< number of transient samples from the beginning of the transient zone
67
68 /**
69 * @brief main decoder context
70 */
72 /* generic decoder variables */
79
80 /* frame size dependent frame information (set during initialization) */
84 uint8_t
bits_per_sample;
///< integer audio sample size for the unscaled IMDCT output (used to scale to [-1.0, 1.0])
87 int8_t
num_channels;
///< number of channels in the stream (same as AVCodecContext.num_channels)
91 uint8_t
max_subframe_len_bit;
///< flag indicating that the subframe is of maximum size when the first subframe length bit is 1
93
94 /* packet decode state */
104
105 /* frame decode state */
106 uint32_t
frame_num;
///< current frame number (not used for decoding)
114
115 /* subframe/block decode state */
119
121
122 // WMA Lossless-specific
123
129
134
142
145
146 struct {
156
158
160
163
167
169
171
177
178 /** Get sign of integer (1 for positive, -1 for negative and 0 for zero) */
179 #define WMASIGN(x) (((x) > 0) - ((x) < 0))
180
182 {
185 unsigned int channel_mask;
186 int i, log2_max_num_subframes;
187
191 }
192
194 s->decode_flags =
AV_RL16(edata_ptr + 14);
195 channel_mask =
AV_RL32(edata_ptr + 2);
196 s->bits_per_sample =
AV_RL16(edata_ptr);
197 if (
s->bits_per_sample == 16)
199 else if (
s->bits_per_sample == 24) {
202 } else {
206 }
207 /* dump the extradata */
211
212 } else {
215 }
216
217 if (channel_mask) {
220 }
226 }
227
229
230 /* extract lfe channel position */
232
233 if (channel_mask & 8) {
236 if (channel_mask &
mask)
238 }
239
244
248
249 /* generic init */
251
252 /* frame info */
253 s->skip_frame = 1;
/* skip first frame */
255 s->len_prefix =
s->decode_flags & 0x40;
256
257 /* get frame len */
261
262 /* init previous block len */
264 s->channel[
i].prev_block_len =
s->samples_per_frame;
265
266 /* subframe info */
267 log2_max_num_subframes = (
s->decode_flags & 0x38) >> 3;
268 s->max_num_subframes = 1 << log2_max_num_subframes;
269 s->max_subframe_len_bit = 0;
270 s->subframe_len_bits =
av_log2(log2_max_num_subframes) + 1;
271
272 s->min_samples_per_subframe =
s->samples_per_frame /
s->max_num_subframes;
273 s->dynamic_range_compression =
s->decode_flags & 0x80;
274 s->bV3RTM =
s->decode_flags & 0x100;
275
278 s->max_num_subframes);
280 }
281
285
286 return 0;
287 }
288
289 /**
290 * @brief Decode the subframe length.
291 * @param s context
292 * @param offset sample offset in the frame
293 * @return decoded subframe length on success, < 0 in case of an error
294 */
296 {
297 int frame_len_ratio, subframe_len,
len;
298
299 /* no need to read from the bitstream when only one length is possible */
300 if (
offset ==
s->samples_per_frame -
s->min_samples_per_subframe)
301 return s->min_samples_per_subframe;
302
305 subframe_len =
s->min_samples_per_subframe * (frame_len_ratio + 1);
306
307 /* sanity check the length */
308 if (subframe_len < s->min_samples_per_subframe ||
309 subframe_len >
s->samples_per_frame) {
311 subframe_len);
313 }
314 return subframe_len;
315 }
316
317 /**
318 * @brief Decode how the data in the frame is split into subframes.
319 * Every WMA frame contains the encoded data for a fixed number of
320 * samples per channel. The data for every channel might be split
321 * into several subframes. This function will reconstruct the list of
322 * subframes for every channel.
323 *
324 * If the subframes are not evenly split, the algorithm estimates the
325 * channels with the lowest number of total samples.
326 * Afterwards, for each of these channels a bit is read from the
327 * bitstream that indicates if the channel contains a subframe with the
328 * next subframe size that is going to be read from the bitstream or not.
329 * If a channel contains such a subframe, the subframe size gets added to
330 * the channel's subframe list.
331 * The algorithm repeats these steps until the frame is properly divided
332 * between the individual channels.
333 *
334 * @param s context
335 * @return 0 on success, < 0 in case of an error
336 */
338 {
339 uint16_t num_samples[
WMALL_MAX_CHANNELS] = { 0 };
/* sum of samples for all currently known subframes of a channel */
340 uint8_t contains_subframe[
WMALL_MAX_CHANNELS];
/* flag indicating if a channel contains the current subframe */
341 int channels_for_cur_subframe =
s->num_channels;
/* number of channels that contain the current subframe */
342 int fixed_channel_layout = 0; /* flag indicating that all channels use the same subfra2me offsets and sizes */
343 int min_channel_len = 0; /* smallest sum of samples (channels with this length will be processed first) */
345
346 /* reset tiling information */
347 for (
c = 0;
c <
s->num_channels;
c++)
348 s->channel[
c].num_subframes = 0;
349
351 if (
s->max_num_subframes == 1 || tile_aligned)
352 fixed_channel_layout = 1;
353
354 /* loop until the frame data is split between the subframes */
355 do {
356 int subframe_len, in_use = 0;
357
358 /* check which channels contain the subframe */
359 for (
c = 0;
c <
s->num_channels;
c++) {
360 if (num_samples[
c] == min_channel_len) {
361 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
362 (min_channel_len ==
s->samples_per_frame -
s->min_samples_per_subframe)) {
363 contains_subframe[
c] = 1;
364 } else {
366 }
367 in_use |= contains_subframe[
c];
368 } else
369 contains_subframe[
c] = 0;
370 }
371
372 if (!in_use) {
374 "Found empty subframe\n");
376 }
377
378 /* get subframe length, subframe_len == 0 is not allowed */
381 /* add subframes to the individual channels and find new min_channel_len */
382 min_channel_len += subframe_len;
383 for (
c = 0;
c <
s->num_channels;
c++) {
385
386 if (contains_subframe[
c]) {
389 "broken frame: num subframes > 31\n");
391 }
393 num_samples[
c] += subframe_len;
395 if (num_samples[
c] >
s->samples_per_frame) {
397 "channel len(%"PRIu16") > samples_per_frame(%"PRIu16")\n",
398 num_samples[
c],
s->samples_per_frame);
400 }
401 }
else if (num_samples[
c] <= min_channel_len) {
402 if (num_samples[
c] < min_channel_len) {
403 channels_for_cur_subframe = 0;
404 min_channel_len = num_samples[
c];
405 }
406 ++channels_for_cur_subframe;
407 }
408 }
409 } while (min_channel_len < s->samples_per_frame);
410
411 for (
c = 0;
c <
s->num_channels;
c++) {
413 for (
i = 0;
i <
s->channel[
c].num_subframes;
i++) {
414 s->channel[
c].subframe_offsets[
i] =
offset;
416 }
417 }
418
419 return 0;
420 }
421
423 {
427
428 for (
i = 0;
i <
s->acfilter_order;
i++)
429 s->acfilter_coeffs[
i] =
get_bitsz(&
s->gb,
s->acfilter_scaling) + 1;
430 }
431
433 {
434 s->mclms_order = (
get_bits(&
s->gb, 4) + 1) * 2;
437 int i, send_coef_bits;
438 int cbits =
av_log2(
s->mclms_scaling + 1);
439 if (1 << cbits < s->mclms_scaling + 1)
440 cbits++;
441
442 send_coef_bits =
get_bitsz(&
s->gb, cbits) + 2;
443
444 for (
i = 0;
i <
s->mclms_order *
s->num_channels *
s->num_channels;
i++)
445 s->mclms_coeffs[
i] =
get_bits(&
s->gb, send_coef_bits);
446
447 for (
i = 0;
i <
s->num_channels;
i++) {
449 for (
c = 0;
c <
i;
c++)
450 s->mclms_coeffs_cur[
i *
s->num_channels +
c] =
get_bits(&
s->gb, send_coef_bits);
451 }
452 }
453 }
454
456 {
459
460 for (
c = 0;
c <
s->num_channels;
c++) {
462 for (
i = 0;
i <
s->cdlms_ttl[
c];
i++) {
466 "Order[%d][%d] %d > max (%d), not supported\n",
468 s->cdlms[0][0].order = 0;
470 }
471 if(
s->cdlms[
c][
i].order & 8 &&
s->bits_per_sample == 16) {
472 static int warned;
473 if(!warned)
475 s->cdlms[
c][
i].order);
476 warned = 1;
477 }
478 }
479
480 for (
i = 0;
i <
s->cdlms_ttl[
c];
i++)
482
483 if (cdlms_send_coef) {
484 for (
i = 0;
i <
s->cdlms_ttl[
c];
i++) {
485 int cbits, shift_l, shift_r, j;
487 if ((1 << cbits) <
s->cdlms[
c][
i].order)
488 cbits++;
490
492 if ((1 << cbits) <
s->cdlms[
c][
i].scaling + 1)
493 cbits++;
494
496 shift_l = 32 -
s->cdlms[
c][
i].bitsend;
497 shift_r = 32 -
s->cdlms[
c][
i].scaling - 2;
498 for (j = 0; j <
s->cdlms[
c][
i].coefsend; j++)
499 s->cdlms[
c][
i].coefs[j] =
500 (
get_bits(&
s->gb,
s->cdlms[
c][
i].bitsend) << shift_l) >> shift_r;
501 }
502 }
503
504 for (
i = 0;
i <
s->cdlms_ttl[
c];
i++)
505 memset(
s->cdlms[
c][
i].coefs +
s->cdlms[
c][
i].order,
507 }
508
509 return 0;
510 }
511
513 {
515 unsigned int ave_mean;
517 if (
s->transient[ch]) {
519 if (
s->transient_pos[ch])
520 s->transient[ch] = 0;
521 s->channel[ch].transient_counter =
522 FFMAX(
s->channel[ch].transient_counter,
s->samples_per_frame / 2);
523 }
else if (
s->channel[ch].transient_counter)
524 s->transient[ch] = 1;
525
526 if (
s->seekable_tile) {
527 ave_mean =
get_bits(&
s->gb,
s->bits_per_sample);
528 s->ave_sum[ch] = ave_mean << (
s->movave_scaling + 1);
529 }
530
531 if (
s->seekable_tile) {
532 if (
s->do_inter_ch_decorr)
534 else
537 }
538 for (;
i < tile_size;
i++) {
539 int rem, rem_bits;
540 unsigned quo = 0, residue;
542 quo++;
544 return -1;
545 }
546 if (quo >= 32)
548
549 ave_mean = (
s->ave_sum[ch] + (1 <<
s->movave_scaling)) >> (
s->movave_scaling + 1);
550 if (ave_mean <= 1)
551 residue = quo;
552 else {
555 residue = (quo << rem_bits) + rem;
556 }
557
558 s->ave_sum[ch] = residue +
s->ave_sum[ch] -
559 (
s->ave_sum[ch] >>
s->movave_scaling);
560
561 residue = (residue >> 1) ^ -(residue & 1);
562 s->channel_residues[ch][
i] = residue;
563 }
564
565 return 0;
566
567 }
568
570 {
575 cbits =
s->lpc_scaling +
s->lpc_intbits;
576 for (ch = 0; ch <
s->num_channels; ch++)
577 for (
i = 0;
i <
s->lpc_order;
i++)
579 }
580
582 {
583 int ich, ilms;
584
585 memset(
s->acfilter_coeffs, 0,
sizeof(
s->acfilter_coeffs));
586 memset(
s->acfilter_prevvalues, 0,
sizeof(
s->acfilter_prevvalues));
587 memset(
s->lpc_coefs, 0,
sizeof(
s->lpc_coefs));
588
589 memset(
s->mclms_coeffs, 0,
sizeof(
s->mclms_coeffs));
590 memset(
s->mclms_coeffs_cur, 0,
sizeof(
s->mclms_coeffs_cur));
591 memset(
s->mclms_prevvalues, 0,
sizeof(
s->mclms_prevvalues));
592 memset(
s->mclms_updates, 0,
sizeof(
s->mclms_updates));
593
594 for (ich = 0; ich <
s->num_channels; ich++) {
595 for (ilms = 0; ilms <
s->cdlms_ttl[ich]; ilms++) {
596 memset(
s->cdlms[ich][ilms].coefs, 0,
597 sizeof(
s->cdlms[ich][ilms].coefs));
598 memset(
s->cdlms[ich][ilms].lms_prevvalues, 0,
599 sizeof(
s->cdlms[ich][ilms].lms_prevvalues));
600 memset(
s->cdlms[ich][ilms].lms_updates, 0,
601 sizeof(
s->cdlms[ich][ilms].lms_updates));
602 }
604 }
605 }
606
607 /**
608 * @brief Reset filter parameters and transient area at new seekable tile.
609 */
611 {
612 int ich, ilms;
613 s->mclms_recent =
s->mclms_order *
s->num_channels;
614 for (ich = 0; ich <
s->num_channels; ich++) {
615 for (ilms = 0; ilms <
s->cdlms_ttl[ich]; ilms++)
616 s->cdlms[ich][ilms].recent =
s->cdlms[ich][ilms].order;
617 /* first sample of a seekable subframe is considered as the starting of
618 a transient area which is samples_per_frame samples long */
619 s->channel[ich].transient_counter =
s->samples_per_frame;
620 s->transient[ich] = 1;
621 s->transient_pos[ich] = 0;
622 }
623 }
624
626 {
627 int i, j, ich, pred_error;
628 int order =
s->mclms_order;
629 int num_channels =
s->num_channels;
630 int range = 1 << (
s->bits_per_sample - 1);
631
632 for (ich = 0; ich < num_channels; ich++) {
633 pred_error =
s->channel_residues[ich][icoef] - (unsigned)
pred[ich];
634 if (pred_error > 0) {
635 for (
i = 0;
i < order * num_channels;
i++)
636 s->mclms_coeffs[
i + ich * order * num_channels] +=
637 s->mclms_updates[
s->mclms_recent +
i];
638 for (j = 0; j < ich; j++)
639 s->mclms_coeffs_cur[ich * num_channels + j] +=
WMASIGN(
s->channel_residues[j][icoef]);
640 } else if (pred_error < 0) {
641 for (
i = 0;
i < order * num_channels;
i++)
642 s->mclms_coeffs[
i + ich * order * num_channels] -=
643 s->mclms_updates[
s->mclms_recent +
i];
644 for (j = 0; j < ich; j++)
645 s->mclms_coeffs_cur[ich * num_channels + j] -=
WMASIGN(
s->channel_residues[j][icoef]);
646 }
647 }
648
649 for (ich = num_channels - 1; ich >= 0; ich--) {
651 s->mclms_prevvalues[
s->mclms_recent] =
av_clip(
s->channel_residues[ich][icoef],
653 s->mclms_updates[
s->mclms_recent] =
WMASIGN(
s->channel_residues[ich][icoef]);
654 }
655
656 if (
s->mclms_recent == 0) {
657 memcpy(&
s->mclms_prevvalues[order * num_channels],
659 sizeof(
int32_t) * order * num_channels);
660 memcpy(&
s->mclms_updates[order * num_channels],
662 sizeof(
int32_t) * order * num_channels);
663 s->mclms_recent = num_channels * order;
664 }
665 }
666
668 {
670 int order =
s->mclms_order;
671 int num_channels =
s->num_channels;
672
673 for (ich = 0; ich < num_channels; ich++) {
675 if (!
s->is_channel_coded[ich])
676 continue;
677 for (
i = 0;
i < order * num_channels;
i++)
678 pred[ich] += (uint32_t)
s->mclms_prevvalues[
i +
s->mclms_recent] *
679 s->mclms_coeffs[
i + order * num_channels * ich];
680 for (
i = 0;
i < ich;
i++)
681 pred[ich] += (uint32_t)
s->channel_residues[
i][icoef] *
682 s->mclms_coeffs_cur[
i + num_channels * ich];
683 pred[ich] += (1
U <<
s->mclms_scaling) >> 1;
684 pred[ich] >>=
s->mclms_scaling;
685 s->channel_residues[ich][icoef] += (unsigned)
pred[ich];
686 }
687 }
688
690 {
692 for (icoef = 0; icoef < tile_size; icoef++) {
695 }
696 }
697
699 {
700 int ilms, recent, icoef;
701 for (ilms =
s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
702 recent =
s->cdlms[ich][ilms].recent;
703 if (
s->update_speed[ich] == 16)
704 continue;
706 for (icoef = 0; icoef <
s->cdlms[ich][ilms].order; icoef++)
707 s->cdlms[ich][ilms].lms_updates[icoef + recent] *= 2;
708 } else {
709 for (icoef = 0; icoef <
s->cdlms[ich][ilms].order; icoef++)
710 s->cdlms[ich][ilms].lms_updates[icoef] *= 2;
711 }
712 }
713 s->update_speed[ich] = 16;
714 }
715
717 {
718 int ilms, recent, icoef;
719 for (ilms =
s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
720 recent =
s->cdlms[ich][ilms].recent;
721 if (
s->update_speed[ich] == 8)
722 continue;
724 for (icoef = 0; icoef <
s->cdlms[ich][ilms].order; icoef++)
725 s->cdlms[ich][ilms].lms_updates[icoef + recent] /= 2;
726 else
727 for (icoef = 0; icoef <
s->cdlms[ich][ilms].order; icoef++)
728 s->cdlms[ich][ilms].lms_updates[icoef] /= 2;
729 }
730 s->update_speed[ich] = 8;
731 }
732
733 #define CD_LMS(bits, ROUND) \
734 static void lms_update ## bits (WmallDecodeCtx *s, int ich, int ilms, int input) \
735 { \
736 int recent = s->cdlms[ich][ilms].recent; \
737 int range = 1 << s->bits_per_sample - 1; \
738 int order = s->cdlms[ich][ilms].order; \
739 int ##bits##_t *prev = (int##bits##_t *)s->cdlms[ich][ilms].lms_prevvalues; \
740 \
741 if (recent) \
742 recent--; \
743 else { \
744 memcpy(prev + order, prev, (bits/8) * order); \
745 memcpy(s->cdlms[ich][ilms].lms_updates + order, \
746 s->cdlms[ich][ilms].lms_updates, \
747 sizeof(*s->cdlms[ich][ilms].lms_updates) * order); \
748 recent = order - 1; \
749 } \
750 \
751 prev[recent] = av_clip(input, -range, range - 1); \
752 s->cdlms[ich][ilms].lms_updates[recent] = WMASIGN(input) * s->update_speed[ich]; \
753 \
754 s->cdlms[ich][ilms].lms_updates[recent + (order >> 4)] >>= 2; \
755 s->cdlms[ich][ilms].lms_updates[recent + (order >> 3)] >>= 1; \
756 s->cdlms[ich][ilms].recent = recent; \
757 memset(s->cdlms[ich][ilms].lms_updates + recent + order, 0, \
758 sizeof(s->cdlms[ich][ilms].lms_updates) - \
759 sizeof(*s->cdlms[ich][ilms].lms_updates)*(recent+order)); \
760 } \
761 \
762 static void revert_cdlms ## bits (WmallDecodeCtx *s, int ch, \
763 int coef_begin, int coef_end) \
764 { \
765 int icoef, ilms, num_lms, residue, input; \
766 unsigned pred;\
767 \
768 num_lms = s->cdlms_ttl[ch]; \
769 for (ilms = num_lms - 1; ilms >= 0; ilms--) { \
770 for (icoef = coef_begin; icoef < coef_end; icoef++) { \
771 int##bits##_t *prevvalues = (int##bits##_t *)s->cdlms[ch][ilms].lms_prevvalues; \
772 pred = (1 << s->cdlms[ch][ilms].scaling) >> 1; \
773 residue = s->channel_residues[ch][icoef]; \
774 pred += s->dsp.scalarproduct_and_madd_int## bits (s->cdlms[ch][ilms].coefs, \
775 prevvalues + s->cdlms[ch][ilms].recent, \
776 s->cdlms[ch][ilms].lms_updates + \
777 s->cdlms[ch][ilms].recent, \
778 FFALIGN(s->cdlms[ch][ilms].order, ROUND), \
779 WMASIGN(residue)); \
780 input = residue + (unsigned)((int)pred >> s->cdlms[ch][ilms].scaling); \
781 lms_update ## bits(s, ch, ilms, input); \
782 s->channel_residues[ch][icoef] = input; \
783 } \
784 } \
785 }
786
789
791 {
792 if (
s->num_channels != 2)
793 return;
794 else if (
s->is_channel_coded[0] ||
s->is_channel_coded[1]) {
795 int icoef;
796 for (icoef = 0; icoef < tile_size; icoef++) {
797 s->channel_residues[0][icoef] -= (unsigned)(
s->channel_residues[1][icoef] >> 1);
798 s->channel_residues[1][icoef] += (unsigned)
s->channel_residues[0][icoef];
799 }
800 }
801 }
802
804 {
806 int16_t *filter_coeffs =
s->acfilter_coeffs;
807 int scaling =
s->acfilter_scaling;
808 int order =
s->acfilter_order;
809
810 for (ich = 0; ich <
s->num_channels; ich++) {
811 int *prevvalues =
s->acfilter_prevvalues[ich];
812 for (
i = 0;
i < order;
i++) {
814 for (j = 0; j < order; j++) {
816 pred += (uint32_t)filter_coeffs[j] * prevvalues[j -
i];
817 else
818 pred += (uint32_t)
s->channel_residues[ich][
i - j - 1] * filter_coeffs[j];
819 }
821 s->channel_residues[ich][
i] += (unsigned)
pred;
822 }
823 for (
i = order;
i < tile_size;
i++) {
825 for (j = 0; j < order; j++)
826 pred += (uint32_t)
s->channel_residues[ich][
i - j - 1] * filter_coeffs[j];
828 s->channel_residues[ich][
i] += (unsigned)
pred;
829 }
830 for (j = order - 1; j >= 0; j--)
831 if (tile_size <= j) {
832 prevvalues[j] = prevvalues[j - tile_size];
833 }else
834 prevvalues[j] =
s->channel_residues[ich][tile_size - j - 1];
835 }
836 }
837
839 {
840 int offset =
s->samples_per_frame;
841 int subframe_len =
s->samples_per_frame;
842 int total_samples =
s->samples_per_frame *
s->num_channels;
843 int i, j, rawpcm_tile, padding_zeroes, res;
844
846
847 /* reset channel context and find the next block offset and size
848 == the next block of the channel with the smallest number of
849 decoded samples */
850 for (
i = 0;
i <
s->num_channels;
i++) {
851 if (
offset >
s->channel[
i].decoded_samples) {
852 offset =
s->channel[
i].decoded_samples;
853 subframe_len =
854 s->channel[
i].subframe_len[
s->channel[
i].cur_subframe];
855 }
856 }
857
858 /* get a list of all channels that contain the estimated block */
859 s->channels_for_cur_subframe = 0;
860 for (
i = 0;
i <
s->num_channels;
i++) {
861 const int cur_subframe =
s->channel[
i].cur_subframe;
862 /* subtract already processed samples */
863 total_samples -=
s->channel[
i].decoded_samples;
864
865 /* and count if there are multiple subframes that match our profile */
866 if (
offset ==
s->channel[
i].decoded_samples &&
867 subframe_len ==
s->channel[
i].subframe_len[cur_subframe]) {
868 total_samples -=
s->channel[
i].subframe_len[cur_subframe];
869 s->channel[
i].decoded_samples +=
870 s->channel[
i].subframe_len[cur_subframe];
871 s->channel_indexes_for_cur_subframe[
s->channels_for_cur_subframe] =
i;
872 ++
s->channels_for_cur_subframe;
873 }
874 }
875
876 /* check if the frame will be complete after processing the
877 estimated block */
878 if (!total_samples)
879 s->parsed_all_subframes = 1;
880
881
883 if (
s->seekable_tile) {
885
887 if (
s->do_arith_coding) {
890 }
894
897
900
902 return res;
905
907 }
908
910
911 if (!rawpcm_tile && !
s->cdlms[0][0].order) {
913 "Waiting for seekable tile\n");
915 return -1;
916 }
917
918
919 for (
i = 0;
i <
s->num_channels;
i++)
920 s->is_channel_coded[
i] = 1;
921
923 for (
i = 0;
i <
s->num_channels;
i++)
925
927 // LPC
932 "inverse LPC filter");
933 }
934 } else
936 }
937
940
943 else
944 padding_zeroes = 0;
945
946 if (rawpcm_tile) {
947 int bits =
s->bits_per_sample - padding_zeroes;
950 "Invalid number of padding bits in raw PCM tile\n");
952 }
953 ff_dlog(
s->avctx,
"RAWPCM %d bits per sample. "
954 "total %d bits, remain=%d\n",
bits,
956 for (
i = 0;
i <
s->num_channels;
i++)
957 for (j = 0; j < subframe_len; j++)
959 } else {
960 if (
s->bits_per_sample < padding_zeroes)
962 for (
i = 0;
i <
s->num_channels;
i++) {
963 if (
s->is_channel_coded[
i]) {
965 if (
s->seekable_tile)
967 else
969 if (
s->bits_per_sample > 16)
970 revert_cdlms32(
s,
i, 0, subframe_len);
971 else
972 revert_cdlms16(
s,
i, 0, subframe_len);
973 } else {
974 memset(
s->channel_residues[
i], 0,
sizeof(**
s->channel_residues) * subframe_len);
975 }
976 }
977
980 if (
s->do_inter_ch_decorr)
984
985 /* Dequantize */
986 if (
s->quant_stepsize != 1)
987 for (
i = 0;
i <
s->num_channels;
i++)
988 for (j = 0; j < subframe_len; j++)
989 s->channel_residues[
i][j] *= (
unsigned)
s->quant_stepsize;
990 }
991
992 /* Write to proper output buffer depending on bit-depth */
993 for (
i = 0;
i <
s->channels_for_cur_subframe;
i++) {
994 int c =
s->channel_indexes_for_cur_subframe[
i];
995 int subframe_len =
s->channel[
c].subframe_len[
s->channel[
c].cur_subframe];
996
997 for (j = 0; j < subframe_len; j++) {
998 if (
s->bits_per_sample == 16) {
999 *
s->samples_16[
c]++ = (int16_t)
s->channel_residues[
c][j] * (1 << padding_zeroes);
1000 } else {
1001 *
s->samples_32[
c]++ =
s->channel_residues[
c][j] * (256
U << padding_zeroes);
1002 }
1003 }
1004 }
1005
1006 /* handled one subframe */
1007 for (
i = 0;
i <
s->channels_for_cur_subframe;
i++) {
1008 int c =
s->channel_indexes_for_cur_subframe[
i];
1009 if (
s->channel[
c].cur_subframe >=
s->channel[
c].num_subframes) {
1012 }
1013 ++
s->channel[
c].cur_subframe;
1014 }
1015 return 0;
1016 }
1017
1018 /**
1019 * @brief Decode one WMA frame.
1020 * @param s codec context
1021 * @return 0 if the trailer bit indicates that this is the last frame,
1022 * 1 if there are additional frames
1023 */
1025 {
1027 int more_frames = 0,
len = 0,
i,
ret;
1028
1029 s->frame->nb_samples =
s->samples_per_frame;
1031 /* return an error if no frame could be decoded at all */
1033 s->frame->nb_samples = 0;
1035 }
1036 for (
i = 0;
i <
s->num_channels;
i++) {
1037 s->samples_16[
i] = (int16_t *)
s->frame->extended_data[
i];
1038 s->samples_32[
i] = (
int32_t *)
s->frame->extended_data[
i];
1039 }
1040
1041 /* get frame length */
1044
1045 /* decode tile information */
1050 }
1051
1052 /* read drc info */
1053 if (
s->dynamic_range_compression)
1055
1056 /* no idea what these are for, might be the number of samples
1057 that need to be skipped at the beginning or end of a stream */
1060
1061 /* usually true for the first frame */
1065 }
1066
1067 /* sometimes true for the last frame */
1071 s->frame->nb_samples -=
skip;
1072 if (
s->frame->nb_samples <= 0)
1074 }
1075
1076 }
1077
1078 /* reset subframe states */
1079 s->parsed_all_subframes = 0;
1080 for (
i = 0;
i <
s->num_channels;
i++) {
1081 s->channel[
i].decoded_samples = 0;
1082 s->channel[
i].cur_subframe = 0;
1083 }
1084
1085 /* decode all subframes */
1086 while (!
s->parsed_all_subframes) {
1087 int decoded_samples =
s->channel[0].decoded_samples;
1090 if (
s->frame->nb_samples)
1091 s->frame->nb_samples = decoded_samples;
1092 return 0;
1093 }
1094 }
1095
1097
1099
1100 if (
s->len_prefix) {
1102 /* FIXME: not sure if this is always an error */
1104 "frame[%"PRIu32"] would have to skip %i bits\n",
1108 return 0;
1109 }
1110
1111 /* skip the rest of the frame data */
1113 }
1114
1115 /* decode trailer bit */
1118 return more_frames;
1119 }
1120
1121 /**
1122 * @brief Calculate remaining input buffer length.
1123 * @param s codec context
1124 * @param gb bitstream reader context
1125 * @return remaining size in bits
1126 */
1128 {
1130 }
1131
1132 /**
1133 * @brief Fill the bit reservoir with a (partial) frame.
1134 * @param s codec context
1135 * @param gb bitstream reader context
1136 * @param len length of the partial frame
1137 * @param append decides whether to reset the buffer or not
1138 */
1141 {
1142 int buflen;
1144
1145 /* when the frame data does not need to be concatenated, the input buffer
1146 is reset and additional bits from the previous frame are copied
1147 and skipped later so that a fast byte copy is possible */
1148
1151 s->num_saved_bits =
s->frame_offset;
1153 }
1154
1155 buflen = (
s->num_saved_bits +
len + 8) >> 3;
1156
1157 if (len <= 0 || buflen >
s->max_frame_size) {
1160 s->num_saved_bits = 0;
1161 return;
1162 }
1163
1164 s->num_saved_bits +=
len;
1168 } else {
1174 }
1176
1179
1182 }
1183
1185 int *got_frame_ptr,
AVPacket* avpkt)
1186 {
1189 const uint8_t* buf = avpkt->
data;
1190 int buf_size = avpkt->
size;
1191 int num_bits_prev_frame, packet_sequence_number, spliced_packet;
1192
1193 s->frame->nb_samples = 0;
1194
1195 if (!buf_size) {
1198 return 0;
1200 s->num_saved_bits = 0;
1201 }
else if (
s->packet_done ||
s->packet_loss) {
1203
1206 s->buf_bit_size = buf_size << 3;
1207
1208 /* parse packet header */
1210 packet_sequence_number =
get_bits(gb, 4);
1211 skip_bits(gb, 1);
// Skip seekable_frame_in_packet, currently unused
1213 if (spliced_packet)
1215
1216 /* get number of bits that need to be added to the previous frame */
1217 num_bits_prev_frame =
get_bits(gb,
s->log2_frame_size);
1218
1219 /* check for packet loss */
1220 if (!
s->packet_loss &&
1221 ((
s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
1224 "Packet loss detected! seq %"PRIx8" vs %x\n",
1225 s->packet_sequence_number, packet_sequence_number);
1226 }
1227 s->packet_sequence_number = packet_sequence_number;
1228
1229 if (num_bits_prev_frame > 0) {
1231 if (num_bits_prev_frame >= remaining_packet_bits) {
1232 num_bits_prev_frame = remaining_packet_bits;
1234 }
1235
1236 /* Append the previous frame data to the remaining data from the
1237 * previous packet to create a full frame. */
1239
1240 /* decode the cross packet frame if it is valid */
1241 if (num_bits_prev_frame < remaining_packet_bits && !s->packet_loss)
1243 }
else if (
s->num_saved_bits -
s->frame_offset) {
1244 ff_dlog(avctx,
"ignoring %x previously saved bits\n",
1245 s->num_saved_bits -
s->frame_offset);
1246 }
1247
1248 if (
s->packet_loss) {
1249 /* Reset number of saved bits so that the decoder does not start
1250 * to decode incomplete frames in the s->len_prefix == 0 case. */
1251 s->num_saved_bits = 0;
1254 }
1255
1256 } else {
1258
1259 s->buf_bit_size = (avpkt->
size -
s->next_packet_start) << 3;
1262
1267
1268 if (!
s->packet_loss)
1270 }
else if (!
s->len_prefix
1272 /* when the frames do not have a length prefix, we don't know the
1273 * compressed length of the individual frames however, we know what
1274 * part of a new packet belongs to the previous frame therefore we
1275 * save the incoming packet first, then we append the "previous
1276 * frame" data from the next packet so that we get a buffer that
1277 * only contains full frames */
1279 } else {
1281 }
1282 }
1283
1287 }
1288
1289 if (
s->packet_done && !
s->packet_loss &&
1291 /* save the rest of the data so that it can be decoded
1292 * with the next packet */
1294 }
1295
1296 *got_frame_ptr =
s->frame->nb_samples > 0;
1298
1300
1302 }
1303
1305 {
1309 s->num_saved_bits = 0;
1310 s->frame_offset = 0;
1311 s->next_packet_start = 0;
1312 s->cdlms[0][0].order = 0;
1313 s->frame->nb_samples = 0;
1315 }
1316
1318 {
1320
1323
1324 return 0;
1325 }
1326
1328 .
p.
name =
"wmalossless",
1337 .p.capabilities =
1338 #if FF_API_SUBFRAMES
1339 AV_CODEC_CAP_SUBFRAMES |
1340 #endif
1346 };