114#define WMAPRO_MAX_CHANNELS 8
115#define MAX_SUBFRAMES 32
117#define MAX_FRAMESIZE 32768
118#define XMA_MAX_STREAMS 8
119#define XMA_MAX_CHANNELS_STREAM 2
120#define XMA_MAX_CHANNELS (XMA_MAX_STREAMS * XMA_MAX_CHANNELS_STREAM)
122#define WMAPRO_BLOCK_MIN_BITS 6
123#define WMAPRO_BLOCK_MAX_BITS 13
124#define WMAPRO_BLOCK_MIN_SIZE (1 << WMAPRO_BLOCK_MIN_BITS)
125#define WMAPRO_BLOCK_MAX_SIZE (1 << WMAPRO_BLOCK_MAX_BITS)
126#define WMAPRO_BLOCK_SIZES (WMAPRO_BLOCK_MAX_BITS - WMAPRO_BLOCK_MIN_BITS + 1)
130#define SCALEVLCBITS 8
131#define VEC4MAXDEPTH ((HUFF_VEC4_MAXBITS+VLCBITS-1)/VLCBITS)
132#define VEC2MAXDEPTH ((HUFF_VEC2_MAXBITS+VLCBITS-1)/VLCBITS)
133#define VEC1MAXDEPTH ((HUFF_VEC1_MAXBITS+VLCBITS-1)/VLCBITS)
134#define SCALEMAXDEPTH ((HUFF_SCALE_MAXBITS+SCALEVLCBITS-1)/SCALEVLCBITS)
135#define SCALERLMAXDEPTH ((HUFF_SCALE_RL_MAXBITS+VLCBITS-1)/VLCBITS)
270#define PRINT(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %d\n", a, b);
271#define PRINT_HEX(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %"PRIx32"\n", a, b);
273 PRINT(
"ed sample bit depth",
s->bits_per_sample);
274 PRINT_HEX(
"ed decode flags",
s->decode_flags);
275 PRINT(
"samples per frame",
s->samples_per_frame);
276 PRINT(
"log2 frame size",
s->log2_frame_size);
277 PRINT(
"max num subframes",
s->max_num_subframes);
278 PRINT(
"len prefix",
s->len_prefix);
279 PRINT(
"num channels",
s->nb_channels);
325 static VLCElem vlc_buf[2108 + 3912];
353 for (
int i = 0;
i < 33;
i++)
369 unsigned int channel_mask;
371 int log2_max_num_subframes;
372 int num_possible_block_sizes;
387 s->decode_flags = 0x10d6;
388 s->bits_per_sample = 16;
395 s->decode_flags = 0x10d6;
396 s->bits_per_sample = 16;
398 s->nb_channels = edata_ptr[32 + ((edata_ptr[0]==3)?0:8) + 4*num_stream + 0];
400 s->decode_flags = 0x10d6;
401 s->bits_per_sample = 16;
403 s->nb_channels = edata_ptr[8 + 20*num_stream + 17];
405 s->decode_flags =
AV_RL16(edata_ptr+14);
406 channel_mask =
AV_RL32(edata_ptr+2);
407 s->bits_per_sample =
AV_RL16(edata_ptr);
410 if (
s->bits_per_sample > 32 ||
s->bits_per_sample < 1) {
421 if (
s->log2_frame_size > 25) {
430 s->len_prefix = (
s->decode_flags & 0x40);
439 s->samples_per_frame = 1 <<
bits;
441 s->samples_per_frame = 512;
445 log2_max_num_subframes = ((
s->decode_flags & 0x38) >> 3);
446 s->max_num_subframes = 1 << log2_max_num_subframes;
447 if (
s->max_num_subframes == 16 ||
s->max_num_subframes == 4)
448 s->max_subframe_len_bit = 1;
449 s->subframe_len_bits =
av_log2(log2_max_num_subframes) + 1;
451 num_possible_block_sizes = log2_max_num_subframes + 1;
452 s->min_samples_per_subframe =
s->samples_per_frame /
s->max_num_subframes;
453 s->dynamic_range_compression = (
s->decode_flags & 0x80);
457 s->max_num_subframes);
463 s->min_samples_per_subframe);
467 if (
s->nb_channels <= 0) {
482 for (
i = 0;
i <
s->nb_channels;
i++)
483 s->channel[
i].prev_block_len =
s->samples_per_frame;
488 if (channel_mask & 8) {
491 if (channel_mask &
mask)
498 for (
i = 0;
i < num_possible_block_sizes;
i++) {
499 int subframe_len =
s->samples_per_frame >>
i;
504 s->sfb_offsets[
i][0] = 0;
506 for (x = 0; x <
MAX_BANDS-1 &&
s->sfb_offsets[
i][band - 1] < subframe_len; x++) {
509 if (
offset >
s->sfb_offsets[
i][band - 1])
512 if (
offset >= subframe_len)
515 s->sfb_offsets[
i][band - 1] = subframe_len;
516 s->num_sfb[
i] = band - 1;
517 if (
s->num_sfb[
i] <= 0) {
529 for (
i = 0;
i < num_possible_block_sizes;
i++) {
531 for (
b = 0;
b <
s->num_sfb[
i];
b++) {
534 +
s->sfb_offsets[
i][
b + 1] - 1) <<
i) >> 1;
535 for (x = 0; x < num_possible_block_sizes; x++) {
537 while (
s->sfb_offsets[x][v + 1] << x <
offset) {
541 s->sf_offsets[
i][x][
b] = v;
553 / (1ll << (
s->bits_per_sample - 1));
567 for (
i = 0;
i < num_possible_block_sizes;
i++) {
568 int block_size =
s->samples_per_frame >>
i;
569 int cutoff = (440*block_size + 3LL * (
s->avctx->sample_rate >> 1) - 1)
570 /
s->avctx->sample_rate;
571 s->subwoofer_cutoffs[
i] =
av_clip(cutoff, 4, block_size);
615 int frame_len_shift = 0;
619 if (
offset ==
s->samples_per_frame -
s->min_samples_per_subframe)
620 return s->min_samples_per_subframe;
626 if (
s->max_subframe_len_bit) {
628 frame_len_shift = 1 +
get_bits(&
s->gb,
s->subframe_len_bits-1);
630 frame_len_shift =
get_bits(&
s->gb,
s->subframe_len_bits);
632 subframe_len =
s->samples_per_frame >> frame_len_shift;
635 if (subframe_len < s->min_samples_per_subframe ||
636 subframe_len >
s->samples_per_frame) {
668 int channels_for_cur_subframe =
s->nb_channels;
669 int fixed_channel_layout = 0;
670 int min_channel_len = 0;
680 for (
c = 0;
c <
s->nb_channels;
c++)
681 s->channel[
c].num_subframes = 0;
683 if (
s->max_num_subframes == 1 ||
get_bits1(&
s->gb))
684 fixed_channel_layout = 1;
691 for (
c = 0;
c <
s->nb_channels;
c++) {
692 if (num_samples[
c] == min_channel_len) {
693 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
694 (min_channel_len ==
s->samples_per_frame -
s->min_samples_per_subframe))
695 contains_subframe[
c] = 1;
699 contains_subframe[
c] = 0;
707 min_channel_len += subframe_len;
708 for (
c = 0;
c <
s->nb_channels;
c++) {
711 if (contains_subframe[
c]) {
714 "broken frame: num subframes > 31\n");
718 num_samples[
c] += subframe_len;
720 if (num_samples[
c] >
s->samples_per_frame) {
722 "channel len > samples_per_frame\n");
725 }
else if (num_samples[
c] <= min_channel_len) {
726 if (num_samples[
c] < min_channel_len) {
727 channels_for_cur_subframe = 0;
728 min_channel_len = num_samples[
c];
730 ++channels_for_cur_subframe;
733 }
while (min_channel_len < s->samples_per_frame);
735 for (
c = 0;
c <
s->nb_channels;
c++) {
738 for (
i = 0;
i <
s->channel[
c].num_subframes;
i++) {
739 ff_dlog(
s->avctx,
"frame[%"PRIu32
"] channel[%i] subframe[%i]"
740 " len %i\n",
s->frame_num,
c,
i,
741 s->channel[
c].subframe_len[
i]);
742 s->channel[
c].subframe_offset[
i] =
offset;
773 for (x = 0; x <
i; x++) {
775 for (y = 0; y <
i + 1; y++) {
778 int n = rotation_offset[
offset + x];
784 cosv =
sin64[32 - n];
786 sinv =
sin64[64 - n];
787 cosv = -
sin64[n - 32];
791 (v1 * sinv) - (v2 * cosv);
793 (v1 * cosv) + (v2 * sinv);
815 if (
s->nb_channels > 1) {
816 int remaining_channels =
s->channels_for_cur_subframe;
820 "Channel transform bit");
824 for (
s->num_chgroups = 0; remaining_channels &&
825 s->num_chgroups <
s->channels_for_cur_subframe;
s->num_chgroups++) {
832 if (remaining_channels > 2) {
833 for (
i = 0;
i <
s->channels_for_cur_subframe;
i++) {
834 int channel_idx =
s->channel_indexes_for_cur_subframe[
i];
835 if (!
s->channel[channel_idx].grouped
838 s->channel[channel_idx].grouped = 1;
839 *channel_data++ =
s->channel[channel_idx].coeffs;
844 for (
i = 0;
i <
s->channels_for_cur_subframe;
i++) {
845 int channel_idx =
s->channel_indexes_for_cur_subframe[
i];
846 if (!
s->channel[channel_idx].grouped)
847 *channel_data++ =
s->channel[channel_idx].coeffs;
848 s->channel[channel_idx].grouped = 1;
857 "Unknown channel transform type");
862 if (
s->nb_channels == 2) {
884 "Coupled channels > 6");
900 for (
i = 0;
i <
s->num_bands;
i++) {
924 static const uint32_t fval_tab[16] = {
925 0x00000000, 0x3f800000, 0x40000000, 0x40400000,
926 0x40800000, 0x40a00000, 0x40c00000, 0x40e00000,
927 0x41000000, 0x41100000, 0x41200000, 0x41300000,
928 0x41400000, 0x41500000, 0x41600000, 0x41700000,
939 ff_dlog(
s->avctx,
"decode coefficients for channel %i\n",
c);
954 while ((
s->transmit_num_vec_coeffs || !rl_mode) &&
963 for (
i = 0;
i < 4;
i += 2) {
976 vals[
i] = fval_tab[idx >> 4 ];
977 vals[
i+1] = fval_tab[idx & 0xF];
981 vals[0] = fval_tab[ idx >> 12 ];
982 vals[1] = fval_tab[(idx >> 8) & 0xF];
983 vals[2] = fval_tab[(idx >> 4) & 0xF];
984 vals[3] = fval_tab[ idx & 0xF];
988 for (
i = 0;
i < 4;
i++) {
994 ci->
coeffs[cur_coeff] = 0;
997 rl_mode |= (++num_zeros >
s->subframe_len >> 8);
1004 if (cur_coeff < s->subframe_len) {
1007 memset(&ci->
coeffs[cur_coeff], 0,
1008 sizeof(*ci->
coeffs) * (
s->subframe_len - cur_coeff));
1011 cur_coeff,
s->subframe_len,
1012 s->subframe_len,
s->esc_len, 0);
1033 for (
i = 0;
i <
s->channels_for_cur_subframe;
i++) {
1034 int c =
s->channel_indexes_for_cur_subframe[
i];
1037 s->channel[
c].scale_factors =
s->channel[
c].saved_scale_factors[!
s->channel[
c].scale_factor_idx];
1038 sf_end =
s->channel[
c].scale_factors +
s->num_bands;
1045 if (
s->channel[
c].reuse_sf) {
1046 const int8_t* sf_offsets =
s->sf_offsets[
s->table_idx][
s->channel[
c].table_idx];
1048 for (
b = 0;
b <
s->num_bands;
b++)
1049 s->channel[
c].scale_factors[
b] =
1050 s->channel[
c].saved_scale_factors[
s->channel[
c].scale_factor_idx][*sf_offsets++];
1053 if (!
s->channel[
c].cur_subframe ||
get_bits1(&
s->gb)) {
1055 if (!
s->channel[
c].reuse_sf) {
1058 s->channel[
c].scale_factor_step =
get_bits(&
s->gb, 2) + 1;
1059 val = 45 /
s->channel[
c].scale_factor_step;
1060 for (sf =
s->channel[
c].scale_factors; sf < sf_end; sf++) {
1067 for (
i = 0;
i <
s->num_bands;
i++) {
1078 sign = (
code & 1) - 1;
1080 }
else if (idx == 1) {
1089 if (
i >=
s->num_bands) {
1091 "invalid scale factor coding\n");
1094 s->channel[
c].scale_factors[
i] += (
val ^ sign) - sign;
1098 s->channel[
c].scale_factor_idx = !
s->channel[
c].scale_factor_idx;
1099 s->channel[
c].table_idx =
s->table_idx;
1100 s->channel[
c].reuse_sf = 1;
1104 s->channel[
c].max_scale_factor =
s->channel[
c].scale_factors[0];
1105 for (sf =
s->channel[
c].scale_factors + 1; sf < sf_end; sf++) {
1106 s->channel[
c].max_scale_factor =
1107 FFMAX(
s->channel[
c].max_scale_factor, *sf);
1122 for (
i = 0;
i <
s->num_chgroups;
i++) {
1123 if (
s->chgroup[
i].transform) {
1125 const int num_channels =
s->chgroup[
i].num_channels;
1126 float** ch_data =
s->chgroup[
i].channel_data;
1127 float** ch_end = ch_data + num_channels;
1128 const int8_t* tb =
s->chgroup[
i].transform_band;
1132 for (sfb =
s->cur_sfb_offsets;
1133 sfb < s->cur_sfb_offsets +
s->num_bands; sfb++) {
1137 for (y = sfb[0]; y <
FFMIN(sfb[1],
s->subframe_len); y++) {
1138 const float* mat =
s->chgroup[
i].decorrelation_matrix;
1139 const float* data_end =
data + num_channels;
1140 float* data_ptr =
data;
1143 for (ch = ch_data; ch < ch_end; ch++)
1144 *data_ptr++ = (*ch)[y];
1146 for (ch = ch_data; ch < ch_end; ch++) {
1149 while (data_ptr < data_end)
1150 sum += *data_ptr++ * *mat++;
1155 }
else if (
s->nb_channels == 2) {
1156 int len =
FFMIN(sfb[1],
s->subframe_len) - sfb[0];
1157 s->fdsp->vector_fmul_scalar(ch_data[0] + sfb[0],
1158 ch_data[0] + sfb[0],
1160 s->fdsp->vector_fmul_scalar(ch_data[1] + sfb[0],
1161 ch_data[1] + sfb[0],
1176 for (
i = 0;
i <
s->channels_for_cur_subframe;
i++) {
1177 int c =
s->channel_indexes_for_cur_subframe[
i];
1179 int winlen =
s->channel[
c].prev_block_len;
1180 float* start =
s->channel[
c].coeffs - (winlen >> 1);
1182 if (
s->subframe_len < winlen) {
1183 start += (winlen -
s->subframe_len) >> 1;
1184 winlen =
s->subframe_len;
1191 s->fdsp->vector_fmul_window(start, start, start + winlen,
1194 s->channel[
c].prev_block_len =
s->subframe_len;
1205 int offset =
s->samples_per_frame;
1206 int subframe_len =
s->samples_per_frame;
1208 int total_samples =
s->samples_per_frame *
s->nb_channels;
1209 int transmit_coeffs = 0;
1210 int cur_subwoofer_cutoff;
1218 for (
i = 0;
i <
s->nb_channels;
i++) {
1219 s->channel[
i].grouped = 0;
1220 if (
offset >
s->channel[
i].decoded_samples) {
1221 offset =
s->channel[
i].decoded_samples;
1223 s->channel[
i].subframe_len[
s->channel[
i].cur_subframe];
1228 "processing subframe with offset %i len %i\n",
offset, subframe_len);
1231 s->channels_for_cur_subframe = 0;
1232 for (
i = 0;
i <
s->nb_channels;
i++) {
1233 const int cur_subframe =
s->channel[
i].cur_subframe;
1235 total_samples -=
s->channel[
i].decoded_samples;
1238 if (
offset ==
s->channel[
i].decoded_samples &&
1239 subframe_len ==
s->channel[
i].subframe_len[cur_subframe]) {
1240 total_samples -=
s->channel[
i].subframe_len[cur_subframe];
1241 s->channel[
i].decoded_samples +=
1242 s->channel[
i].subframe_len[cur_subframe];
1243 s->channel_indexes_for_cur_subframe[
s->channels_for_cur_subframe] =
i;
1244 ++
s->channels_for_cur_subframe;
1251 s->parsed_all_subframes = 1;
1254 ff_dlog(
s->avctx,
"subframe is part of %i channels\n",
1255 s->channels_for_cur_subframe);
1258 s->table_idx =
av_log2(
s->samples_per_frame/subframe_len);
1259 s->num_bands =
s->num_sfb[
s->table_idx];
1260 s->cur_sfb_offsets =
s->sfb_offsets[
s->table_idx];
1261 cur_subwoofer_cutoff =
s->subwoofer_cutoffs[
s->table_idx];
1264 offset +=
s->samples_per_frame >> 1;
1266 for (
i = 0;
i <
s->channels_for_cur_subframe;
i++) {
1267 int c =
s->channel_indexes_for_cur_subframe[
i];
1269 s->channel[
c].coeffs = &
s->channel[
c].out[
offset];
1272 s->subframe_len = subframe_len;
1273 s->esc_len =
av_log2(
s->subframe_len - 1) + 1;
1278 if (!(num_fill_bits =
get_bits(&
s->gb, 2))) {
1283 if (num_fill_bits >= 0) {
1304 for (
i = 0;
i <
s->channels_for_cur_subframe;
i++) {
1305 int c =
s->channel_indexes_for_cur_subframe[
i];
1306 if ((
s->channel[
c].transmit_coefs =
get_bits1(&
s->gb)))
1307 transmit_coeffs = 1;
1311 if (transmit_coeffs) {
1313 int quant_step = 90 *
s->bits_per_sample >> 4;
1316 if ((
s->transmit_num_vec_coeffs =
get_bits1(&
s->gb))) {
1317 int num_bits =
av_log2((
s->subframe_len + 3)/4) + 1;
1318 for (
i = 0;
i <
s->channels_for_cur_subframe;
i++) {
1319 int c =
s->channel_indexes_for_cur_subframe[
i];
1320 int num_vec_coeffs =
get_bits(&
s->gb, num_bits) << 2;
1321 if (num_vec_coeffs >
s->subframe_len) {
1326 s->channel[
c].num_vec_coeffs = num_vec_coeffs;
1329 for (
i = 0;
i <
s->channels_for_cur_subframe;
i++) {
1330 int c =
s->channel_indexes_for_cur_subframe[
i];
1331 s->channel[
c].num_vec_coeffs =
s->subframe_len;
1337 if (step == -32 || step == 31) {
1338 const int sign = (step == 31) - 1;
1344 quant_step += ((
quant + step) ^ sign) - sign;
1346 if (quant_step < 0) {
1352 if (
s->channels_for_cur_subframe == 1) {
1353 s->channel[
s->channel_indexes_for_cur_subframe[0]].quant_step = quant_step;
1356 for (
i = 0;
i <
s->channels_for_cur_subframe;
i++) {
1357 int c =
s->channel_indexes_for_cur_subframe[
i];
1358 s->channel[
c].quant_step = quant_step;
1361 s->channel[
c].quant_step +=
get_bits(&
s->gb, modifier_len) + 1;
1363 ++
s->channel[
c].quant_step;
1373 ff_dlog(
s->avctx,
"BITSTREAM: subframe header length was %i\n",
1377 for (
i = 0;
i <
s->channels_for_cur_subframe;
i++) {
1378 int c =
s->channel_indexes_for_cur_subframe[
i];
1379 if (
s->channel[
c].transmit_coefs &&
1383 memset(
s->channel[
c].coeffs, 0,
1384 sizeof(*
s->channel[
c].coeffs) * subframe_len);
1387 ff_dlog(
s->avctx,
"BITSTREAM: subframe length was %i\n",
1390 if (transmit_coeffs) {
1395 for (
i = 0;
i <
s->channels_for_cur_subframe;
i++) {
1396 int c =
s->channel_indexes_for_cur_subframe[
i];
1397 const int* sf =
s->channel[
c].scale_factors;
1400 if (
c ==
s->lfe_channel)
1401 memset(&
s->tmp[cur_subwoofer_cutoff], 0,
sizeof(*
s->tmp) *
1402 (subframe_len - cur_subwoofer_cutoff));
1405 for (
b = 0;
b <
s->num_bands;
b++) {
1406 const int end =
FFMIN(
s->cur_sfb_offsets[
b+1],
s->subframe_len);
1407 const int exp =
s->channel[
c].quant_step -
1408 (
s->channel[
c].max_scale_factor - *sf++) *
1409 s->channel[
c].scale_factor_step;
1411 int start =
s->cur_sfb_offsets[
b];
1412 s->fdsp->vector_fmul_scalar(
s->tmp + start,
1413 s->channel[
c].coeffs + start,
1414 quant, end - start);
1418 tx_fn(tx,
s->channel[
c].coeffs,
s->tmp,
sizeof(
float));
1426 for (
i = 0;
i <
s->channels_for_cur_subframe;
i++) {
1427 int c =
s->channel_indexes_for_cur_subframe[
i];
1428 if (
s->channel[
c].cur_subframe >=
s->channel[
c].num_subframes) {
1432 ++
s->channel[
c].cur_subframe;
1447 int more_frames = 0;
1455 ff_dlog(
s->avctx,
"decoding frame with length %x\n",
len);
1466 for (
i = 0;
i <
s->nb_channels *
s->nb_channels;
i++)
1472 if (
s->dynamic_range_compression) {
1474 ff_dlog(
s->avctx,
"drc_gain %i\n",
s->drc_gain);
1484 s->trim_start =
s->trim_end = 0;
1487 ff_dlog(
s->avctx,
"BITSTREAM: frame header length was %i\n",
1491 s->parsed_all_subframes = 0;
1492 for (
i = 0;
i <
s->nb_channels;
i++) {
1493 s->channel[
i].decoded_samples = 0;
1494 s->channel[
i].cur_subframe = 0;
1495 s->channel[
i].reuse_sf = 0;
1499 while (!
s->parsed_all_subframes) {
1507 for (
i = 0;
i <
s->nb_channels;
i++)
1508 memcpy(
frame->extended_data[
i],
s->channel[
i].out,
1509 s->samples_per_frame *
sizeof(*
s->channel[
i].out));
1511 for (
i = 0;
i <
s->nb_channels;
i++) {
1513 memcpy(&
s->channel[
i].out[0],
1514 &
s->channel[
i].out[
s->samples_per_frame],
1515 s->samples_per_frame *
sizeof(*
s->channel[
i].out) >> 1);
1518 if (
s->skip_frame) {
1526 if (
s->len_prefix) {
1530 "frame[%"PRIu32
"] would have to skip %i bits\n",
1580 s->num_saved_bits =
s->frame_offset;
1582 buflen = (
s->num_saved_bits +
len + 7) >> 3;
1594 s->num_saved_bits +=
len;
1620 const uint8_t* buf = avpkt->
data;
1621 int buf_size = avpkt->
size;
1622 int num_bits_prev_frame;
1623 int packet_sequence_number;
1638 for (
i = 0;
i <
s->nb_channels;
i++) {
1639 memset(
frame->extended_data[
i], 0,
1640 s->samples_per_frame *
sizeof(*
s->channel[
i].out));
1642 memcpy(
frame->extended_data[
i],
s->channel[
i].out,
1643 s->samples_per_frame *
sizeof(*
s->channel[
i].out) >> 1);
1651 else if (
s->packet_done ||
s->packet_loss) {
1669 s->buf_bit_size = buf_size << 3;
1676 packet_sequence_number =
get_bits(gb, 4);
1680 ff_dlog(avctx,
"packet[%"PRId64
"]: number of frames %d\n", avctx->
frame_num, num_frames);
1681 packet_sequence_number = 0;
1685 num_bits_prev_frame =
get_bits(gb,
s->log2_frame_size);
1689 ff_dlog(avctx,
"packet[%"PRId64
"]: skip packets %d\n", avctx->
frame_num,
s->skip_packets);
1693 num_bits_prev_frame);
1697 ((
s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
1700 "Packet loss detected! seq %"PRIx8
" vs %x\n",
1701 s->packet_sequence_number, packet_sequence_number);
1703 s->packet_sequence_number = packet_sequence_number;
1705 if (num_bits_prev_frame > 0) {
1707 if (num_bits_prev_frame >= remaining_packet_bits) {
1708 num_bits_prev_frame = remaining_packet_bits;
1715 ff_dlog(avctx,
"accumulated %x bits of frame data\n",
1716 s->num_saved_bits -
s->frame_offset);
1719 if (!
s->packet_loss)
1721 }
else if (
s->num_saved_bits -
s->frame_offset) {
1722 ff_dlog(avctx,
"ignoring %x previously saved bits\n",
1723 s->num_saved_bits -
s->frame_offset);
1726 if (
s->packet_loss) {
1730 s->num_saved_bits = 0;
1736 if (avpkt->
size <
s->next_packet_start) {
1741 s->buf_bit_size = (avpkt->
size -
s->next_packet_start) << 3;
1750 if (!
s->packet_loss)
1752 }
else if (!
s->len_prefix
1772 if (
s->packet_done && !
s->packet_loss &&
1784 if (
s->trim_start <
frame->nb_samples) {
1785 for (
int ch = 0; ch <
frame->ch_layout.nb_channels; ch++)
1786 frame->extended_data[ch] +=
s->trim_start * 4;
1788 frame->nb_samples -=
s->trim_start;
1797 if (
s->trim_end <
frame->nb_samples) {
1798 frame->nb_samples -=
s->trim_end;
1817 int *got_frame_ptr,
AVPacket *avpkt)
1823 frame->nb_samples =
s->samples_per_frame;
1833 int *got_frame_ptr,
AVPacket *avpkt)
1836 int got_stream_frame_ptr = 0;
1837 int i, ret = 0, eof = 0;
1839 if (!
s->frames[
s->current_stream]->data[0]) {
1841 s->frames[
s->current_stream]->nb_samples = 512;
1842 if ((ret =
ff_get_buffer(avctx,
s->frames[
s->current_stream], 0)) < 0)
1844 }
else if (
s->frames[
s->current_stream]->nb_samples != 512) {
1847 s->frames[
s->current_stream]->nb_samples = 512;
1848 if ((ret =
ff_get_buffer(avctx,
s->frames[
s->current_stream], 0)) < 0)
1852 if (!
s->xma[
s->current_stream].eof_done) {
1853 ret =
decode_packet(avctx, &
s->xma[
s->current_stream],
s->frames[
s->current_stream],
1854 &got_stream_frame_ptr, avpkt);
1860 for (
i = 0;
i <
s->num_streams;
i++) {
1861 if (!
s->xma[
i].eof_done &&
s->frames[
i]->data[0]) {
1863 &got_stream_frame_ptr, avpkt);
1866 eof &=
s->xma[
i].eof_done;
1870 if (
s->xma[0].trim_start)
1871 s->trim_start =
s->xma[0].trim_start;
1872 if (
s->xma[0].trim_end)
1873 s->trim_end =
s->xma[0].trim_end;
1876 if (got_stream_frame_ptr) {
1877 const int nb_samples =
s->frames[
s->current_stream]->nb_samples;
1878 void *
left[1] = {
s->frames[
s->current_stream]->extended_data[0] };
1879 void *right[1] = {
s->frames[
s->current_stream]->extended_data[1] };
1882 if (
s->xma[
s->current_stream].nb_channels > 1)
1884 }
else if (ret < 0) {
1885 s->current_stream = 0;
1892 if (
s->xma[
s->current_stream].packet_done ||
1893 s->xma[
s->current_stream].packet_loss) {
1894 int nb_samples = INT_MAX;
1897 if (
s->xma[
s->current_stream].skip_packets != 0) {
1900 min[0] =
s->xma[0].skip_packets;
1903 for (
i = 1;
i <
s->num_streams;
i++) {
1904 if (
s->xma[
i].skip_packets <
min[0]) {
1905 min[0] =
s->xma[
i].skip_packets;
1910 s->current_stream =
min[1];
1914 for (
i = 0;
i <
s->num_streams;
i++) {
1915 s->xma[
i].skip_packets =
FFMAX(0,
s->xma[
i].skip_packets - 1);
1919 if (!eof && avpkt->
size)
1920 nb_samples -=
FFMIN(nb_samples, 4096);
1923 if ((nb_samples > 0 || eof || !avpkt->
size) && !
s->flushed) {
1927 nb_samples -=
av_clip(
s->trim_end +
s->trim_start - 128 - 64, 0, nb_samples);
1931 frame->nb_samples = nb_samples;
1935 for (
i = 0;
i <
s->num_streams;
i++) {
1936 const int start_ch =
s->start_channel[
i];
1937 void *
left[1] = {
frame->extended_data[start_ch + 0] };
1940 if (
s->xma[
i].nb_channels > 1) {
1941 void *right[1] = {
frame->extended_data[start_ch + 1] };
1946 *got_frame_ptr = nb_samples > 0;
1956 int i, ret, start_channels = 0;
2001 for (
i = 0;
i <
s->num_streams;
i++) {
2009 s->start_channel[
i] = start_channels;
2010 start_channels +=
s->xma[
i].nb_channels;
2018 if (!
s->samples[0][
i] || !
s->samples[1][
i])
2030 for (
i = 0;
i <
s->num_streams;
i++) {
2049 for (
i = 0;
i <
s->nb_channels;
i++)
2050 memset(
s->channel[
i].out, 0,
s->samples_per_frame *
2051 sizeof(*
s->channel[
i].out));
2053 s->skip_packets = 0;
2079 for (
i = 0;
i <
s->num_streams;
i++)
2082 s->current_stream = 0;
static double val(void *priv, double ch)
const FFCodec ff_wmapro_decoder
wmapro decoder
const FFCodec ff_xma1_decoder
const FFCodec ff_xma2_decoder
static av_cold void close(AVCodecParserContext *s)
static const VLCElem * sf_vlc[2][8]
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
#define FF_DEBUG_BITSTREAM
void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
static const uint8_t *BS_FUNC align(BSCTX *bc)
Skip bits to a byte boundary.
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define i(width, name, range_min, range_max)
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
void(* flush)(AVBSFContext *ctx)
int(* init)(AVBSFContext *ctx)
static struct @346255127015250356166251341105367306144006377143 state
static const uint8_t bits[8]
internal math functions header
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
static SDL_Window * window
static const uint8_t frame_size[4]
bitstream reader API header.
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
static int get_sbits(GetBitContext *s, int n)
static int get_bits_left(GetBitContext *gb)
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
static unsigned int get_bits1(GetBitContext *s)
static void skip_bits(GetBitContext *s, int n)
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
static int get_bits_count(const GetBitContext *s)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
void av_audio_fifo_reset(AVAudioFifo *af)
Reset the AVAudioFifo buffer.
int av_audio_fifo_write(AVAudioFifo *af, void *const *data, int nb_samples)
Write data to an AVAudioFifo.
int av_audio_fifo_read(AVAudioFifo *af, void *const *data, int nb_samples)
Read data from an AVAudioFifo.
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
@ AV_SAMPLE_FMT_FLTP
float, planar
static const uint8_t scale_table[]
static av_cold void decode_init_static(void)
static av_always_inline uint32_t av_float2int(float f)
Reinterpret a float as a 32-bit integer.
static void scale(int *out, const int *in, const int w, const int h, const int shift)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
common internal api header.
#define MAX_SUBFRAMES
max number of subframes per channel
Macro definitions for various function/variable attributes.
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
static int ff_thread_once(char *control, void(*routine)(void))
static const uint16_t mask[17]
Memory handling functions.
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
static uint8_t * append(uint8_t *buf, const uint8_t *src, int size)
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static int put_bits_count(PutBitContext *s)
static int put_bits_left(PutBitContext *s)
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
void ff_init_ff_sine_windows(int index)
initialize the specified entry of ff_sine_windows
SINETABLE_CONST float *const ff_sine_windows[]
#define FF_ARRAY_ELEMS(a)
Context for an Audio FIFO Buffer.
enum AVChannelOrder order
Channel order used in this layout.
int nb_channels
Number of channels in this layout.
main external API structure.
AVChannelLayout ch_layout
Audio channel layout.
enum AVSampleFormat sample_fmt
audio sample format
int64_t frame_num
Frame counter, set by libavcodec.
int sample_rate
samples per second
int flags
AV_CODEC_FLAG_*.
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
struct AVCodecInternal * internal
Private context used for internal data.
int skip_samples
Number of audio samples to skip at the start of the next decoded frame.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
For static VLCs, the number of bits can often be hardcoded at each get_vlc2() callsite.
frame specific decoder context for a single channel
int16_t prev_block_len
length of the previous block
int quant_step
quantization step for the current subframe
uint16_t num_vec_coeffs
number of vector coded coefficients
uint8_t table_idx
index in sf_offsets for the scale factor reference block
float * coeffs
pointer to the subframe decode buffer
uint16_t subframe_offset[MAX_SUBFRAMES]
subframe positions in the current frame
uint8_t cur_subframe
current subframe number
int * scale_factors
pointer to the scale factor values used for decoding
int8_t scale_factor_step
scaling step for the current subframe
int8_t reuse_sf
share scale factors between subframes
uint8_t grouped
channel is part of a group
float out[WMAPRO_BLOCK_MAX_SIZE+WMAPRO_BLOCK_MAX_SIZE/2]
output buffer
int max_scale_factor
maximum scale factor for the current subframe
uint16_t decoded_samples
number of already processed samples
int saved_scale_factors[2][MAX_BANDS]
resampled and (previously) transmitted scale factor values
int8_t scale_factor_idx
index for the transmitted scale factor values (used for resampling)
uint16_t subframe_len[MAX_SUBFRAMES]
subframe length in samples
channel group for channel transformations
int8_t transform_band[MAX_BANDS]
controls if the transform is enabled for a certain band
float decorrelation_matrix[WMAPRO_MAX_CHANNELS *WMAPRO_MAX_CHANNELS]
float * channel_data[WMAPRO_MAX_CHANNELS]
transformation coefficients
int8_t transform
transform on / off
uint8_t num_channels
number of channels in the group
int num_saved_bits
saved number of bits
uint8_t packet_sequence_number
current packet number
int8_t parsed_all_subframes
all subframes decoded?
WMAProChannelGrp chgroup[WMAPRO_MAX_CHANNELS]
channel group information
GetBitContext gb
bitstream reader context
int8_t esc_len
length of escaped coefficients
int8_t channels_for_cur_subframe
number of channels that contain the subframe
uint32_t frame_num
current frame number (not used for decoding)
int16_t subwoofer_cutoffs[WMAPRO_BLOCK_SIZES]
subwoofer cutoff values
int8_t channel_indexes_for_cur_subframe[WMAPRO_MAX_CHANNELS]
uint8_t len_prefix
frame is prefixed with its length
int frame_offset
frame offset in the bit reservoir
int16_t sfb_offsets[WMAPRO_BLOCK_SIZES][MAX_BANDS]
scale factor band offsets (multiples of 4)
uint32_t decode_flags
used compression features
uint16_t samples_per_frame
number of samples to output
av_tx_fn tx_fn[WMAPRO_BLOCK_SIZES]
uint8_t packet_done
set when a packet is fully decoded
int8_t nb_channels
number of channels in stream (XMA1/2)
AVCodecContext * avctx
codec context for av_log
int subframe_offset
subframe offset in the bit reservoir
uint8_t eof_done
set when EOF reached and extra subframe is written (XMA1/2)
uint16_t trim_end
number of samples to skip at end
int buf_bit_size
buffer size in bits
uint8_t dynamic_range_compression
frame contains DRC data
int16_t subframe_len
current subframe length
AVTXContext * tx[WMAPRO_BLOCK_SIZES]
MDCT context per block size.
uint8_t max_subframe_len_bit
flag indicating that the subframe is of maximum size when the first subframe length bit is 1
int8_t skip_frame
skip output step
uint8_t num_chgroups
number of channel groups
int8_t lfe_channel
lfe channel index
uint8_t table_idx
index for the num_sfb, sfb_offsets, sf_offsets and subwoofer_cutoffs tables
int16_t * cur_sfb_offsets
sfb offsets for the current block
WMAProChannelCtx channel[WMAPRO_MAX_CHANNELS]
per channel data
uint8_t subframe_len_bits
number of bits used for the subframe length
uint8_t packet_loss
set in case of bitstream error
int8_t num_bands
number of scale factor bands
uint8_t drc_gain
gain for the DRC tool
uint8_t frame_data[MAX_FRAMESIZE+AV_INPUT_BUFFER_PADDING_SIZE]
compressed frame data
uint8_t max_num_subframes
float tmp[WMAPRO_BLOCK_MAX_SIZE]
IMDCT output buffer.
int next_packet_start
start offset of the next wma packet in the demuxer packet
int8_t num_sfb[WMAPRO_BLOCK_SIZES]
scale factor bands per block size
PutBitContext pb
context for filling the frame_data buffer
uint8_t bits_per_sample
integer audio sample size for the unscaled IMDCT output (used to scale to [-1.0, 1....
GetBitContext pgb
bitstream reader context for the packet
uint16_t min_samples_per_subframe
uint8_t packet_offset
frame offset in the packet
int8_t sf_offsets[WMAPRO_BLOCK_SIZES][WMAPRO_BLOCK_SIZES][MAX_BANDS]
scale factor resample matrix
uint16_t trim_start
number of samples to skip at start
uint8_t skip_packets
packets to skip to find next packet in a stream (XMA1/2)
int8_t transmit_num_vec_coeffs
number of vector coded coefficients is part of the bitstream
const float * windows[WMAPRO_BLOCK_SIZES]
windows for the different block sizes
AVAudioFifo * samples[2][XMA_MAX_STREAMS]
WMAProDecodeCtx xma[XMA_MAX_STREAMS]
AVFrame * frames[XMA_MAX_STREAMS]
int start_channel[XMA_MAX_STREAMS]
#define avpriv_request_sample(...)
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
@ AV_TX_FLOAT_MDCT
Standard MDCT with a sample data type of float, double or int32_t, respectively.
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
av_cold const VLCElem * ff_vlc_init_tables_from_lengths(VLCInitState *state, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags)
#define VLC_INIT_STATE(_table)
#define VLC_INIT_STATIC_TABLE_FROM_LENGTHS(vlc_table, nb_bits, nb_codes, lens, lens_wrap, syms, syms_wrap, syms_size, offset, flags)
static const uint8_t quant[64]
unsigned int ff_wma_get_large_val(GetBitContext *gb)
Decode an uncompressed coefficient.
int ff_wma_run_level_decode(AVCodecContext *avctx, GetBitContext *gb, const VLCElem *vlc, const float *level_table, const uint16_t *run_table, int version, WMACoef *ptr, int offset, int num_coefs, int block_len, int frame_len_bits, int coef_nb_bits)
Decode run level compressed coefficients.
av_cold int ff_wma_get_frame_len_bits(int sample_rate, int version, unsigned int decode_flags)
Get the samples per frame for this stream.
#define MAX_BANDS
max number of scale factor bands
static int remaining_bits(WmallDecodeCtx *s, GetBitContext *gb)
Calculate remaining input buffer length.
#define MAX_FRAMESIZE
maximum compressed frame size
tables for wmapro decoding
static const uint8_t vec2_table[HUFF_VEC2_SIZE][2]
static const uint16_t vec4_syms[HUFF_VEC4_SIZE]
static const uint8_t scale_rl_table[HUFF_SCALE_RL_SIZE][2]
static const uint8_t vec4_lens[HUFF_VEC4_SIZE]
#define HUFF_SCALE_RL_SIZE
static const uint8_t scale_rl_level[HUFF_SCALE_RL_SIZE]
static const uint16_t coef1_run[HUFF_COEF1_SIZE]
static const uint8_t scale_rl_run[HUFF_SCALE_RL_SIZE]
static const uint16_t coef0_syms[HUFF_COEF0_SIZE]
static const uint16_t coef0_run[HUFF_COEF0_SIZE]
static const uint8_t coef1_table[HUFF_COEF1_SIZE][2]
static const float *const default_decorrelation[]
default decorrelation matrix offsets
static const uint8_t coef0_lens[HUFF_COEF0_SIZE]
static const uint8_t vec1_table[HUFF_VEC1_SIZE][2]
static const float coef0_level[HUFF_COEF0_SIZE]
static const float coef1_level[HUFF_COEF1_SIZE]
static const uint16_t critical_freq[]
frequencies to divide the frequency spectrum into scale factor bands
#define MAX_BANDS
max number of scale factor bands
static void wmapro_window(WMAProDecodeCtx *s)
Apply sine window and reconstruct the output buffer.
static int remaining_bits(WMAProDecodeCtx *s, GetBitContext *gb)
Calculate remaining input buffer length.
#define WMAPRO_BLOCK_MAX_SIZE
maximum block size
static av_cold void wmapro_flush(AVCodecContext *avctx)
Clear decoder buffers (for seeking).
#define WMAPRO_BLOCK_SIZES
possible block sizes
static av_cold void dump_context(WMAProDecodeCtx *s)
helper function to print the most important members of the context
#define WMAPRO_BLOCK_MIN_SIZE
minimum block size
static av_cold void decode_init_static(void)
static float sin64[33]
sine table for decorrelation
static av_cold void xma_flush(AVCodecContext *avctx)
#define WMAPRO_BLOCK_MIN_BITS
log2 of min block size
static int decode_frame(WMAProDecodeCtx *s, AVFrame *frame, int *got_frame_ptr)
Decode one WMA frame.
static av_cold int decode_init(WMAProDecodeCtx *s, AVCodecContext *avctx, int num_stream)
Initialize the decoder.
static av_cold int xma_decode_init(AVCodecContext *avctx)
static int xma_decode_packet(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
static VLCElem vec4_vlc[604]
4 coefficients per symbol
static av_cold int xma_decode_end(AVCodecContext *avctx)
static av_cold int get_rate(AVCodecContext *avctx)
static void save_bits(WMAProDecodeCtx *s, GetBitContext *gb, int len, int append)
Fill the bit reservoir with a (partial) frame.
static VLCElem vec1_vlc[562]
1 coefficient per symbol
static VLCElem sf_rl_vlc[1406]
scale factor run length vlc
static int decode_channel_transform(WMAProDecodeCtx *s)
Decode channel transformation parameters.
static int wmapro_decode_packet(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Decode a single WMA packet.
static void decode_decorrelation_matrix(WMAProDecodeCtx *s, WMAProChannelGrp *chgroup)
Calculate a decorrelation matrix from the bitstream parameters.
static int decode_packet(AVCodecContext *avctx, WMAProDecodeCtx *s, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
static VLCElem vec2_vlc[562]
2 coefficients per symbol
static void inverse_channel_transform(WMAProDecodeCtx *s)
Reconstruct the individual channel data.
static const VLCElem * coef_vlc[2]
coefficient run length vlc codes
#define MAX_SUBFRAMES
max number of subframes per channel
static int decode_subframe_length(WMAProDecodeCtx *s, int offset)
Decode the subframe length.
static int decode_subframe(WMAProDecodeCtx *s)
Decode a single subframe (block).
static av_cold int wmapro_decode_end(AVCodecContext *avctx)
static av_cold int wmapro_decode_init(AVCodecContext *avctx)
Initialize the decoder.
static int decode_coeffs(WMAProDecodeCtx *s, int c)
Extract the coefficients from the bitstream.
#define XMA_MAX_CHANNELS_STREAM
#define WMAPRO_BLOCK_MAX_BITS
log2 of max block size
#define MAX_FRAMESIZE
maximum compressed frame size
static int decode_tilehdr(WMAProDecodeCtx *s)
Decode how the data in the frame is split into subframes.
static av_cold int decode_end(WMAProDecodeCtx *s)
Uninitialize the decoder and free all resources.
static int decode_scale_factors(WMAProDecodeCtx *s)
Extract scale factors from the bitstream.
#define WMAPRO_MAX_CHANNELS
current decoder limitations