1 /*
2 * Wmapro compatible decoder
3 * Copyright (c) 2007 Baptiste Coudurier, Benjamin Larsson, Ulion
4 * Copyright (c) 2008 - 2011 Sascha Sommer, Benjamin Larsson
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * @brief wmapro decoder implementation
26 * Wmapro is an MDCT based codec comparable to wma standard or AAC.
27 * The decoding therefore consists of the following steps:
28 * - bitstream decoding
29 * - reconstruction of per-channel data
30 * - rescaling and inverse quantization
31 * - IMDCT
32 * - windowing and overlapp-add
33 *
34 * The compressed wmapro bitstream is split into individual packets.
35 * Every such packet contains one or more wma frames.
36 * The compressed frames may have a variable length and frames may
37 * cross packet boundaries.
38 * Common to all wmapro frames is the number of samples that are stored in
39 * a frame.
40 * The number of samples and a few other decode flags are stored
41 * as extradata that has to be passed to the decoder.
42 *
43 * The wmapro frames themselves are again split into a variable number of
44 * subframes. Every subframe contains the data for 2^N time domain samples
45 * where N varies between 7 and 12.
46 *
47 * Example wmapro bitstream (in samples):
48 *
49 * || packet 0 || packet 1 || packet 2 packets
50 * ---------------------------------------------------
51 * || frame 0 || frame 1 || frame 2 || frames
52 * ---------------------------------------------------
53 * || | | || | | | || || subframes of channel 0
54 * ---------------------------------------------------
55 * || | | || | | | || || subframes of channel 1
56 * ---------------------------------------------------
57 *
58 * The frame layouts for the individual channels of a wma frame does not need
59 * to be the same.
60 *
61 * However, if the offsets and lengths of several subframes of a frame are the
62 * same, the subframes of the channels can be grouped.
63 * Every group may then use special coding techniques like M/S stereo coding
64 * to improve the compression ratio. These channel transformations do not
65 * need to be applied to a whole subframe. Instead, they can also work on
66 * individual scale factor bands (see below).
67 * The coefficients that carry the audio signal in the frequency domain
68 * are transmitted as huffman-coded vectors with 4, 2 and 1 elements.
69 * In addition to that, the encoder can switch to a runlevel coding scheme
70 * by transmitting subframe_length / 128 zero coefficients.
71 *
72 * Before the audio signal can be converted to the time domain, the
73 * coefficients have to be rescaled and inverse quantized.
74 * A subframe is therefore split into several scale factor bands that get
75 * scaled individually.
76 * Scale factors are submitted for every frame but they might be shared
77 * between the subframes of a channel. Scale factors are initially DPCM-coded.
78 * Once scale factors are shared, the differences are transmitted as runlevel
79 * codes.
80 * Every subframe length and offset combination in the frame layout shares a
81 * common quantization factor that can be adjusted for every channel by a
82 * modifier.
83 * After the inverse quantization, the coefficients get processed by an IMDCT.
84 * The resulting values are then windowed with a sine window and the first half
85 * of the values are added to the second half of the output from the previous
86 * subframe in order to reconstruct the output samples.
87 */
88
100
101 /** current decoder limitations */
102 #define WMAPRO_MAX_CHANNELS 8 ///< max number of handled channels
103 #define MAX_SUBFRAMES 32
///< max number of subframes per channel
104 #define MAX_BANDS 29
///< max number of scale factor bands
105 #define MAX_FRAMESIZE 32768
///< maximum compressed frame size
106
107 #define WMAPRO_BLOCK_MIN_BITS 6 ///< log2 of min block size
108 #define WMAPRO_BLOCK_MAX_BITS 13
///< log2 of max block size
109 #define WMAPRO_BLOCK_MIN_SIZE (1 << WMAPRO_BLOCK_MIN_BITS)
///< minimum block size
110 #define WMAPRO_BLOCK_MAX_SIZE (1 << WMAPRO_BLOCK_MAX_BITS)
///< maximum block size
111 #define WMAPRO_BLOCK_SIZES (WMAPRO_BLOCK_MAX_BITS - WMAPRO_BLOCK_MIN_BITS + 1)
///< possible block sizes
112
113
115 #define SCALEVLCBITS 8
116 #define VEC4MAXDEPTH ((HUFF_VEC4_MAXBITS+VLCBITS-1)/VLCBITS)
117 #define VEC2MAXDEPTH ((HUFF_VEC2_MAXBITS+VLCBITS-1)/VLCBITS)
118 #define VEC1MAXDEPTH ((HUFF_VEC1_MAXBITS+VLCBITS-1)/VLCBITS)
119 #define SCALEMAXDEPTH ((HUFF_SCALE_MAXBITS+SCALEVLCBITS-1)/SCALEVLCBITS)
120 #define SCALERLMAXDEPTH ((HUFF_SCALE_RL_MAXBITS+VLCBITS-1)/VLCBITS)
121
128 static float sin64[33];
///< sine table for decorrelation
129
130 /**
131 * @brief frame specific decoder context for a single channel
132 */
138 uint16_t subframe_offset[
MAX_SUBFRAMES];
///< subframe positions in the current frame
142 int quant_step;
///< quantization step for the current subframe
143 int8_t
reuse_sf;
///< share scale factors between subframes
146 int saved_scale_factors[2][
MAX_BANDS];
///< resampled and (previously) transmitted scale factor values
147 int8_t
scale_factor_idx;
///< index for the transmitted scale factor values (used for resampling)
148 int*
scale_factors;
///< pointer to the scale factor values used for decoding
150 float*
coeffs;
///< pointer to the subframe decode buffer
154
155 /**
156 * @brief channel group for channel transformations
157 */
161 int8_t transform_band[
MAX_BANDS];
///< controls if the transform is enabled for a certain band
165
166 /**
167 * @brief main decoder context
168 */
170 /* generic decoder variables */
179
180 /* frame size dependent frame information (set during initialization) */
196
197 /* packet decode state */
207
208 /* frame decode state */
209 uint32_t
frame_num;
///< current frame number (not used for decoding)
215
216 /* subframe/block decode state */
223 uint8_t table_idx;
///< index for the num_sfb, sfb_offsets, sf_offsets and subwoofer_cutoffs tables
224 int8_t
esc_len;
///< length of escaped coefficients
225
228
231
232
233 /**
234 *@brief helper function to print the most important members of the context
235 *@param s context
236 */
238 {
239 #define PRINT(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %d\n", a, b);
240 #define PRINT_HEX(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %x\n", a, b);
241
249 }
250
251 /**
252 *@brief Uninitialize the decoder and free all resources.
253 *@param avctx codec context
254 *@return 0 on success, < 0 otherwise
255 */
257 {
259 int i;
260
263
264 return 0;
265 }
266
267 /**
268 *@brief Initialize the decoder.
269 *@param avctx codec context
270 *@return 0 on success, -1 otherwise
271 */
273 {
276 unsigned int channel_mask;
278 int log2_max_num_subframes;
279 int num_possible_block_sizes;
280
284 }
285
288
290
292
295 channel_mask =
AV_RL32(edata_ptr+2);
297 /** dump the extradata */
301
302 } else {
305 }
306
307 /** generic init */
312 }
313
314 /** frame info */
318
319 /** get frame len */
324 }
326
327 /** subframe info */
328 log2_max_num_subframes = ((s->
decode_flags & 0x38) >> 3);
333
334 num_possible_block_sizes = log2_max_num_subframes + 1;
337
342 }
343
348 }
349
353 }
354
363 }
364
365 /** init previous block len */
366 for (i = 0; i < avctx->
channels; i++)
368
369 /** extract lfe channel position */
371
372 if (channel_mask & 8) {
374 for (mask = 1; mask < 16; mask <<= 1) {
375 if (channel_mask & mask)
377 }
378 }
379
383
387
391
395
399
403
407
408 /** calculate number of scale factor bands and their offsets
409 for every possible block size */
410 for (i = 0; i < num_possible_block_sizes; i++) {
412 int x;
414
416
420 offset &= ~3;
423 }
429 }
430 }
431
432
433 /** Scale factors can be shared between blocks of different size
434 as every block has a different scale factor band layout.
435 The matrix sf_offsets is needed to find the correct scale factor.
436 */
437
438 for (i = 0; i < num_possible_block_sizes; i++) {
440 for (b = 0; b < s->
num_sfb[i]; b++) {
441 int x;
444 for (x = 0; x < num_possible_block_sizes; x++) {
447 v++;
449 }
451 }
452 }
453 }
454
455 /** init MDCT, FIXME: only init needed sizes */
460
461 /** init MDCT windows: simple sine window */
466 }
467
468 /** calculate subwoofer cutoff values */
469 for (i = 0; i < num_possible_block_sizes; i++) {
474 }
475
476 /** calculate sine values for the decorrelation matrix */
477 for (i = 0; i < 33; i++)
479
482
484
485 return 0;
486 }
487
488 /**
489 *@brief Decode the subframe length.
490 *@param s context
491 *@param offset sample offset in the frame
492 *@return decoded subframe length on success, < 0 in case of an error
493 */
495 {
496 int frame_len_shift = 0;
497 int subframe_len;
498
499 /** no need to read from the bitstream when only one length is possible */
502
505
506 /** 1 bit indicates if the subframe is of maximum length */
510 } else
512
514
515 /** sanity check the length */
516 if (subframe_len < s->min_samples_per_subframe ||
519 subframe_len);
521 }
522 return subframe_len;
523 }
524
525 /**
526 *@brief Decode how the data in the frame is split into subframes.
527 * Every WMA frame contains the encoded data for a fixed number of
528 * samples per channel. The data for every channel might be split
529 * into several subframes. This function will reconstruct the list of
530 * subframes for every channel.
531 *
532 * If the subframes are not evenly split, the algorithm estimates the
533 * channels with the lowest number of total samples.
534 * Afterwards, for each of these channels a bit is read from the
535 * bitstream that indicates if the channel contains a subframe with the
536 * next subframe size that is going to be read from the bitstream or not.
537 * If a channel contains such a subframe, the subframe size gets added to
538 * the channel's subframe list.
539 * The algorithm repeats these steps until the frame is properly divided
540 * between the individual channels.
541 *
542 *@param s context
543 *@return 0 on success, < 0 in case of an error
544 */
546 {
547 uint16_t num_samples[
WMAPRO_MAX_CHANNELS] = { 0 };
/**< sum of samples for all currently known subframes of a channel */
549 int channels_for_cur_subframe = s->
avctx->
channels;
/**< number of channels that contain the current subframe */
550 int fixed_channel_layout = 0; /**< flag indicating that all channels use the same subframe offsets and sizes */
551 int min_channel_len = 0; /**< smallest sum of samples (channels with this length will be processed first) */
553
554 /* Should never consume more than 3073 bits (256 iterations for the
555 * while loop when always the minimum amount of 128 samples is subtracted
556 * from missing samples in the 8 channel case).
557 * 1 + BLOCK_MAX_SIZE * MAX_CHANNELS / BLOCK_MIN_SIZE * (MAX_CHANNELS + 4)
558 */
559
560 /** reset tiling information */
563
565 fixed_channel_layout = 1;
566
567 /** loop until the frame data is split between the subframes */
568 do {
569 int subframe_len;
570
571 /** check which channels contain the subframe */
573 if (num_samples[c] == min_channel_len) {
574 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
576 contains_subframe[
c] = 1;
577 else
579 } else
580 contains_subframe[
c] = 0;
581 }
582
583 /** get subframe length, subframe_len == 0 is not allowed */
586
587 /** add subframes to the individual channels and find new min_channel_len */
588 min_channel_len += subframe_len;
591
592 if (contains_subframe[c]) {
595 "broken frame: num subframes > 31\n");
597 }
599 num_samples[
c] += subframe_len;
603 "channel len > samples_per_frame\n");
605 }
606 } else if (num_samples[c] <= min_channel_len) {
607 if (num_samples[c] < min_channel_len) {
608 channels_for_cur_subframe = 0;
609 min_channel_len = num_samples[
c];
610 }
611 ++channels_for_cur_subframe;
612 }
613 }
614 } while (min_channel_len < s->samples_per_frame);
615
617 int i;
625 }
626 }
627
628 return 0;
629 }
630
631 /**
632 *@brief Calculate a decorrelation matrix from the bitstream parameters.
633 *@param s codec context
634 *@param chgroup channel group for which the matrix needs to be calculated
635 */
638 {
639 int i;
644
647
651
653 int x;
654 for (x = 0; x < i; x++) {
656 for (y = 0; y < i + 1; y++) {
659 int n = rotation_offset[offset + x];
660 float sinv;
661 float cosv;
662
663 if (n < 32) {
666 } else {
668 cosv = -
sin64[n - 32];
669 }
670
672 (v1 * sinv) - (v2 * cosv);
674 (v1 * cosv) + (v2 * sinv);
675 }
676 }
677 offset += i;
678 }
679 }
680
681 /**
682 *@brief Decode channel transformation parameters
683 *@param s codec context
684 *@return >= 0 in case of success, < 0 in case of bitstream errors
685 */
687 {
688 int i;
689 /* should never consume more than 1921 bits for the 8 channel case
690 * 1 + MAX_CHANNELS * (MAX_CHANNELS + 2 + 3 * MAX_CHANNELS * MAX_CHANNELS
691 * + MAX_CHANNELS + MAX_BANDS + 1)
692 */
693
694 /** in the one channel case channel transforms are pointless */
698
701 "Channel transform bit");
703 }
704
711
712 /** decode channel mask */
713 if (remaining_channels > 2) {
721 }
722 }
723 } else {
730 }
731 }
732
733 /** decode transform type */
738 "Unknown channel transform type");
740 }
741 } else {
748 } else {
749 /** cos(pi/4) */
754 }
755 }
761 } else {
762 /** FIXME: more than 6 coupled channels not supported */
765 "Coupled channels > 6");
766 } else {
771 }
772 }
773 }
774 }
775
776 /** decode transform on / off */
779 int i;
780 /** transform can be enabled for individual bands */
783 }
784 } else {
786 }
787 }
789 }
790 }
791 return 0;
792 }
793
794 /**
795 *@brief Extract the coefficients from the bitstream.
796 *@param s codec context
797 *@param c current channel number
798 *@return 0 on success, < 0 in case of bitstream errors
799 */
801 {
802 /* Integers 0..15 as single-precision floats. The table saves a
803 costly int to float conversion, and storing the values as
804 integers allows fast sign-flipping. */
805 static const uint32_t fval_tab[16] = {
806 0x00000000, 0x3f800000, 0x40000000, 0x40400000,
807 0x40800000, 0x40a00000, 0x40c00000, 0x40e00000,
808 0x41000000, 0x41100000, 0x41200000, 0x41300000,
809 0x41400000, 0x41500000, 0x41600000, 0x41700000,
810 };
811 int vlctable;
814 int rl_mode = 0;
815 int cur_coeff = 0;
816 int num_zeros = 0;
819
820 av_dlog(s->
avctx,
"decode coefficients for channel %i\n", c);
821
823 vlc = &coef_vlc[vlctable];
824
825 if (vlctable) {
828 } else {
831 }
832
833 /** decode vector coefficients (consumes up to 167 bits per iteration for
834 4 vector coded large values) */
837 uint32_t vals[4];
838 int i;
839 unsigned int idx;
840
842
844 for (i = 0; i < 4; i += 2) {
856 } else {
858 vals[i+1] = fval_tab[symbol_to_vec2[idx] & 0xF];
859 }
860 }
861 } else {
863 vals[1] = fval_tab[(symbol_to_vec4[idx] >> 8) & 0xF];
864 vals[2] = fval_tab[(symbol_to_vec4[idx] >> 4) & 0xF];
865 vals[3] = fval_tab[ symbol_to_vec4[idx] & 0xF];
866 }
867
868 /** decode sign */
869 for (i = 0; i < 4; i++) {
870 if (vals[i]) {
873 num_zeros = 0;
874 } else {
875 ci->
coeffs[cur_coeff] = 0;
876 /** switch to run level mode when subframe_len / 128 zeros
877 were found in a row */
879 }
880 ++cur_coeff;
881 }
882 }
883
884 /** decode run level coded coefficients */
885 if (cur_coeff < s->subframe_len) {
886 memset(&ci->
coeffs[cur_coeff], 0,
889 level, run, 1, ci->
coeffs,
893 }
894
895 return 0;
896 }
897
898 /**
899 *@brief Extract scale factors from the bitstream.
900 *@param s codec context
901 *@return 0 on success, < 0 in case of bitstream errors
902 */
904 {
905 int i;
906
907 /** should never consume more than 5344 bits
908 * MAX_CHANNELS * (1 + MAX_BANDS * 23)
909 */
910
913 int* sf;
914 int* sf_end;
917
918 /** resample scale factors for the new block size
919 * as the scale factors might need to be resampled several times
920 * before some new values are transmitted, a backup of the last
921 * transmitted scale factors is kept in saved_scale_factors
922 */
929 }
930
932
935 /** decode DPCM coded scale factors */
941 }
942 } else {
943 int i;
944 /** run level decode differences to the resampled factors */
946 int idx;
947 int skip;
949 int sign;
950
952
953 if (!idx) {
955 val = code >> 6;
956 sign = (code & 1) - 1;
957 skip = (code & 0x3f) >> 1;
958 } else if (idx == 1) {
959 break;
960 } else {
964 }
965
966 i += skip;
969 "invalid scale factor coding\n");
971 }
973 }
974 }
975 /** swap buffers */
979 }
980
981 /** calculate new scale factor maximum */
986 }
987
988 }
989 return 0;
990 }
991
992 /**
993 *@brief Reconstruct the individual channel data.
994 *@param s codec context
995 */
997 {
998 int i;
999
1005 float** ch_end = ch_data + num_channels;
1007 int16_t* sfb;
1008
1009 /** multichannel decorrelation */
1011 sfb < s->cur_sfb_offsets + s->
num_bands; sfb++) {
1013 if (*tb++ == 1) {
1014 /** multiply values with the decorrelation_matrix */
1017 const float* data_end = data + num_channels;
1018 float* data_ptr =
data;
1019 float** ch;
1020
1021 for (ch = ch_data; ch < ch_end; ch++)
1022 *data_ptr++ = (*ch)[
y];
1023
1024 for (ch = ch_data; ch < ch_end; ch++) {
1025 float sum = 0;
1027 while (data_ptr < data_end)
1028 sum += *data_ptr++ * *mat++;
1029
1031 }
1032 }
1036 ch_data[0] + sfb[0],
1037 181.0 / 128, len);
1039 ch_data[1] + sfb[0],
1040 181.0 / 128, len);
1041 }
1042 }
1043 }
1044 }
1045 }
1046
1047 /**
1048 *@brief Apply sine window and reconstruct the output buffer.
1049 *@param s codec context
1050 */
1052 {
1053 int i;
1056 float* window;
1059
1063 }
1064
1066
1067 winlen >>= 1;
1068
1070 window, winlen);
1071
1073 }
1074 }
1075
1076 /**
1077 *@brief Decode a single subframe (block).
1078 *@param s codec context
1079 *@return 0 on success, < 0 when decoding failed
1080 */
1082 {
1085 int i;
1087 int transmit_coeffs = 0;
1088 int cur_subwoofer_cutoff;
1089
1091
1092 /** reset channel context and find the next block offset and size
1093 == the next block of the channel with the smallest number of
1094 decoded samples
1095 */
1100 subframe_len =
1102 }
1103 }
1104
1106 "processing subframe with offset %i len %i\n", offset, subframe_len);
1107
1108 /** get a list of all channels that contain the estimated block */
1112 /** subtract already processed samples */
1114
1115 /** and count if there are multiple subframes that match our profile */
1123 }
1124 }
1125
1126 /** check if the frame will be complete after processing the
1127 estimated block */
1128 if (!total_samples)
1130
1131
1134
1135 /** calculate number of scale factor bands and their offsets */
1140
1141 /** configure the decoder for the current subframe */
1143
1146
1148 }
1149
1152
1153 /** skip extended header if any */
1155 int num_fill_bits;
1156 if (!(num_fill_bits =
get_bits(&s->
gb, 2))) {
1158 num_fill_bits = (len ?
get_bits(&s->
gb, len) : 0) + 1;
1159 }
1160
1161 if (num_fill_bits >= 0) {
1165 }
1166
1168 }
1169 }
1170
1171 /** no idea for what the following bit is used */
1175 }
1176
1177
1180
1181
1185 transmit_coeffs = 1;
1186 }
1187
1189 if (transmit_coeffs) {
1190 int step;
1192
1193 /** decode number of vector coded coefficients */
1198 int num_vec_coeffs =
get_bits(&s->
gb, num_bits) << 2;
1202 }
1205 }
1206 } else {
1210 }
1211 }
1212 /** decode quantization step */
1214 quant_step += step;
1215 if (step == -32 || step == 31) {
1216 const int sign = (step == 31) - 1;
1220 quant += 31;
1221 }
1222 quant_step += ((quant + step) ^ sign) - sign;
1223 }
1224 if (quant_step < 0) {
1226 }
1227
1228 /** decode quantization step modifiers for every channel */
1229
1232 } else {
1238 if (modifier_len) {
1240 } else
1242 }
1243 }
1244 }
1245
1246 /** decode scale factors */
1249 }
1250
1251 av_dlog(s->
avctx,
"BITSTREAM: subframe header length was %i\n",
1253
1254 /** parse coefficients */
1260 } else
1263 }
1264
1265 av_dlog(s->
avctx,
"BITSTREAM: subframe length was %i\n",
1267
1268 if (transmit_coeffs) {
1270 /** reconstruct the per channel data */
1276
1278 memset(&s->
tmp[cur_subwoofer_cutoff], 0,
sizeof(*s->
tmp) *
1279 (subframe_len - cur_subwoofer_cutoff));
1280
1281 /** inverse quantization and rescaling */
1287 const float quant = pow(10.0, exp / 20.0);
1291 quant, end - start);
1292 }
1293
1294 /** apply imdct (imdct_half == DCTIV with reverse) */
1296 }
1297 }
1298
1299 /** window and overlapp-add */
1301
1302 /** handled one subframe */
1308 }
1310 }
1311
1312 return 0;
1313 }
1314
1315 /**
1316 *@brief Decode one WMA frame.
1317 *@param s codec context
1318 *@return 0 if the trailer bit indicates that this is the last frame,
1319 * 1 if there are additional frames
1320 */
1322 {
1325 int more_frames = 0;
1328
1329 /** get frame length */
1332
1333 av_dlog(s->
avctx,
"decoding frame with length %x\n", len);
1334
1335 /** decode tile information */
1338 return 0;
1339 }
1340
1341 /** read postproc transform */
1346 }
1347 }
1348
1349 /** read drc info */
1353 }
1354
1355 /** no idea what these are for, might be the number of samples
1356 that need to be skipped at the beginning or end of a stream */
1359
1360 /** usually true for the first frame */
1364 }
1365
1366 /** sometimes true for the last frame */
1370 }
1371
1372 }
1373
1374 av_dlog(s->
avctx,
"BITSTREAM: frame header length was %i\n",
1376
1377 /** reset subframe states */
1379 for (i = 0; i < avctx->
channels; i++) {
1383 }
1384
1385 /** decode all subframes */
1389 return 0;
1390 }
1391 }
1392
1393 /* get output buffer */
1397 return 0;
1398 }
1399
1400 /** copy samples to the output buffer */
1401 for (i = 0; i < avctx->
channels; i++)
1404
1405 for (i = 0; i < avctx->
channels; i++) {
1406 /** reuse second half of the IMDCT output for the next frame */
1410 }
1411
1414 *got_frame_ptr = 0;
1416 } else {
1417 *got_frame_ptr = 1;
1418 }
1419
1422 /** FIXME: not sure if this is always an error */
1424 "frame[%i] would have to skip %i bits\n", s->
frame_num,
1427 return 0;
1428 }
1429
1430 /** skip the rest of the frame data */
1432 } else {
1434 }
1435 }
1436
1437 /** decode trailer bit */
1439
1441 return more_frames;
1442 }
1443
1444 /**
1445 *@brief Calculate remaining input buffer length.
1446 *@param s codec context
1447 *@param gb bitstream reader context
1448 *@return remaining size in bits
1449 */
1451 {
1453 }
1454
1455 /**
1456 *@brief Fill the bit reservoir with a (partial) frame.
1457 *@param s codec context
1458 *@param gb bitstream reader context
1459 *@param len length of the partial frame
1460 *@param append decides whether to reset the buffer or not
1461 */
1464 {
1465 int buflen;
1466
1467 /** when the frame data does not need to be concatenated, the input buffer
1468 is reset and additional bits from the previous frame are copied
1469 and skipped later so that a fast byte copy is possible */
1470
1471 if (!append) {
1475 }
1476
1478
1482 return;
1483 }
1484
1486
1488 if (!append) {
1491 } else {
1493 align =
FFMIN(align, len);
1497 }
1499
1500 {
1503 }
1504
1507 }
1508
1509 /**
1510 *@brief Decode a single WMA packet.
1511 *@param avctx codec context
1512 *@param data the output buffer
1513 *@param avpkt input packet
1514 *@return number of bytes that were read from the input buffer
1515 */
1517 int *got_frame_ptr,
AVPacket* avpkt)
1518 {
1522 int buf_size = avpkt->
size;
1523 int num_bits_prev_frame;
1524 int packet_sequence_number;
1525
1526 *got_frame_ptr = 0;
1527
1530
1531 /** sanity check for the buffer length */
1532 if (buf_size < avctx->block_align) {
1536 }
1537
1541
1542 /** parse packet header */
1544 packet_sequence_number =
get_bits(gb, 4);
1546
1547 /** get number of bits that need to be added to the previous frame */
1550 num_bits_prev_frame);
1551
1552 /** check for packet loss */
1558 }
1560
1561 if (num_bits_prev_frame > 0) {
1563 if (num_bits_prev_frame >= remaining_packet_bits) {
1564 num_bits_prev_frame = remaining_packet_bits;
1566 }
1567
1568 /** append the previous frame data to the remaining data from the
1569 previous packet to create a full frame */
1570 save_bits(s, gb, num_bits_prev_frame, 1);
1571 av_dlog(avctx,
"accumulated %x bits of frame data\n",
1573
1574 /** decode the cross packet frame if it is valid */
1578 av_dlog(avctx,
"ignoring %x previously saved bits\n",
1580 }
1581
1583 /** reset number of saved bits so that the decoder
1584 does not start to decode incomplete frames in the
1585 s->len_prefix == 0 case */
1588 }
1589
1590 } else {
1603 /** when the frames do not have a length prefix, we don't know
1604 the compressed length of the individual frames
1605 however, we know what part of a new packet belongs to the
1606 previous frame
1607 therefore we save the incoming packet first, then we append
1608 the "previous frame" data from the next packet so that
1609 we get a buffer that only contains full frames */
1611 } else
1613 }
1614
1617 /** save the rest of the data so that it can be decoded
1618 with the next packet */
1620 }
1621
1625
1627 }
1628
1629 /**
1630 *@brief Clear decoder buffers (for seeking).
1631 *@param avctx codec context
1632 */
1634 {
1636 int i;
1637 /** reset output buffer as a part of it is used during the windowing of a
1638 new frame */
1639 for (i = 0; i < avctx->
channels; i++)
1643 }
1644
1645
1646 /**
1647 *@brief wmapro decoder
1648 */
1662 };