comparison wmaprodec.c @ 10123:e1bd75a1eab2 libavcodec

cosmetics: indentation and other prettyprinting fixes
author diego
date Wed, 02 Sep 2009 18:03:13 +0000
parents fe081db7c09c
children 6ff9347c8042
comparison
equal deleted inserted replaced
10122:fe081db7c09c 10123:e1bd75a1eab2
260 *@return 0 on success, -1 otherwise 260 *@return 0 on success, -1 otherwise
261 */ 261 */
262 static av_cold int decode_init(AVCodecContext *avctx) 262 static av_cold int decode_init(AVCodecContext *avctx)
263 { 263 {
264 WMAProDecodeCtx *s = avctx->priv_data; 264 WMAProDecodeCtx *s = avctx->priv_data;
265 uint8_t *edata_ptr = avctx->extradata; 265 uint8_t *edata_ptr = avctx->extradata;
266 unsigned int channel_mask; 266 unsigned int channel_mask;
267 int i; 267 int i;
268 int log2_max_num_subframes; 268 int log2_max_num_subframes;
269 int num_possible_block_sizes; 269 int num_possible_block_sizes;
270 270
379 int x; 379 int x;
380 int band = 1; 380 int band = 1;
381 381
382 s->sfb_offsets[i][0] = 0; 382 s->sfb_offsets[i][0] = 0;
383 383
384 for (x = 0; x < MAX_BANDS-1 && s->sfb_offsets[i][band-1] < subframe_len; x++) { 384 for (x = 0; x < MAX_BANDS-1 && s->sfb_offsets[i][band - 1] < subframe_len; x++) {
385 int offset = (subframe_len * 2 * critical_freq[x]) 385 int offset = (subframe_len * 2 * critical_freq[x])
386 / s->avctx->sample_rate + 2; 386 / s->avctx->sample_rate + 2;
387 offset &= ~3; 387 offset &= ~3;
388 if ( offset > s->sfb_offsets[i][band - 1] ) 388 if (offset > s->sfb_offsets[i][band - 1])
389 s->sfb_offsets[i][band++] = offset; 389 s->sfb_offsets[i][band++] = offset;
390 } 390 }
391 s->sfb_offsets[i][band - 1] = subframe_len; 391 s->sfb_offsets[i][band - 1] = subframe_len;
392 s->num_sfb[i] = band - 1; 392 s->num_sfb[i] = band - 1;
393 } 393 }
401 for (i = 0; i < num_possible_block_sizes; i++) { 401 for (i = 0; i < num_possible_block_sizes; i++) {
402 int b; 402 int b;
403 for (b = 0; b < s->num_sfb[i]; b++) { 403 for (b = 0; b < s->num_sfb[i]; b++) {
404 int x; 404 int x;
405 int offset = ((s->sfb_offsets[i][b] 405 int offset = ((s->sfb_offsets[i][b]
406 + s->sfb_offsets[i][b + 1] - 1)<<i) >> 1; 406 + s->sfb_offsets[i][b + 1] - 1) << i) >> 1;
407 for (x = 0; x < num_possible_block_sizes; x++) { 407 for (x = 0; x < num_possible_block_sizes; x++) {
408 int v = 0; 408 int v = 0;
409 while (s->sfb_offsets[x][v + 1] << x < offset) 409 while (s->sfb_offsets[x][v + 1] << x < offset)
410 ++v; 410 ++v;
411 s->sf_offsets[i][x][b] = v; 411 s->sf_offsets[i][x][b] = v;
414 } 414 }
415 415
416 /** init MDCT, FIXME: only init needed sizes */ 416 /** init MDCT, FIXME: only init needed sizes */
417 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++) 417 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
418 ff_mdct_init(&s->mdct_ctx[i], BLOCK_MIN_BITS+1+i, 1, 418 ff_mdct_init(&s->mdct_ctx[i], BLOCK_MIN_BITS+1+i, 1,
419 1.0 / (1 <<(BLOCK_MIN_BITS + i - 1)) 419 1.0 / (1 << (BLOCK_MIN_BITS + i - 1))
420 / (1 << (s->bits_per_sample - 1))); 420 / (1 << (s->bits_per_sample - 1)));
421 421
422 /** init MDCT windows: simple sinus window */ 422 /** init MDCT windows: simple sinus window */
423 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++) { 423 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++) {
424 const int n = 1 << (WMAPRO_BLOCK_MAX_BITS - i); 424 const int n = 1 << (WMAPRO_BLOCK_MAX_BITS - i);
425 const int win_idx = WMAPRO_BLOCK_MAX_BITS - i - 7; 425 const int win_idx = WMAPRO_BLOCK_MAX_BITS - i - 7;
426 ff_sine_window_init(ff_sine_windows[win_idx], n); 426 ff_sine_window_init(ff_sine_windows[win_idx], n);
427 s->windows[WMAPRO_BLOCK_SIZES-i-1] = ff_sine_windows[win_idx]; 427 s->windows[WMAPRO_BLOCK_SIZES - i - 1] = ff_sine_windows[win_idx];
428 } 428 }
429 429
430 /** calculate subwoofer cutoff values */ 430 /** calculate subwoofer cutoff values */
431 for (i = 0; i < num_possible_block_sizes; i++) { 431 for (i = 0; i < num_possible_block_sizes; i++) {
432 int block_size = s->samples_per_frame >> i; 432 int block_size = s->samples_per_frame >> i;
469 frame_len_shift = get_bits(&s->gb, s->subframe_len_bits); 469 frame_len_shift = get_bits(&s->gb, s->subframe_len_bits);
470 470
471 subframe_len = s->samples_per_frame >> frame_len_shift; 471 subframe_len = s->samples_per_frame >> frame_len_shift;
472 472
473 /** sanity check the length */ 473 /** sanity check the length */
474 if (subframe_len < s->min_samples_per_subframe 474 if (subframe_len < s->min_samples_per_subframe ||
475 || subframe_len > s->samples_per_frame) { 475 subframe_len > s->samples_per_frame) {
476 av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n", 476 av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
477 subframe_len); 477 subframe_len);
478 return AVERROR_INVALIDDATA; 478 return AVERROR_INVALIDDATA;
479 } 479 }
480 return subframe_len; 480 return subframe_len;
557 } 557 }
558 chan->subframe_len[chan->num_subframes] = subframe_len; 558 chan->subframe_len[chan->num_subframes] = subframe_len;
559 num_samples[c] += subframe_len; 559 num_samples[c] += subframe_len;
560 ++chan->num_subframes; 560 ++chan->num_subframes;
561 if (num_samples[c] > s->samples_per_frame) { 561 if (num_samples[c] > s->samples_per_frame) {
562 av_log(s->avctx, AV_LOG_ERROR,"broken frame: " 562 av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
563 "channel len > samples_per_frame\n"); 563 "channel len > samples_per_frame\n");
564 return AVERROR_INVALIDDATA; 564 return AVERROR_INVALIDDATA;
565 } 565 }
566 } else if(num_samples[c] <= min_channel_len) { 566 } else if (num_samples[c] <= min_channel_len) {
567 if (num_samples[c] < min_channel_len) { 567 if (num_samples[c] < min_channel_len) {
568 channels_for_cur_subframe = 0; 568 channels_for_cur_subframe = 0;
569 min_channel_len = num_samples[c]; 569 min_channel_len = num_samples[c];
570 } 570 }
571 ++channels_for_cur_subframe; 571 ++channels_for_cur_subframe;
576 for (c = 0; c < s->num_channels; c++) { 576 for (c = 0; c < s->num_channels; c++) {
577 int i; 577 int i;
578 int offset = 0; 578 int offset = 0;
579 for (i = 0; i < s->channel[c].num_subframes; i++) { 579 for (i = 0; i < s->channel[c].num_subframes; i++) {
580 dprintf(s->avctx, "frame[%i] channel[%i] subframe[%i]" 580 dprintf(s->avctx, "frame[%i] channel[%i] subframe[%i]"
581 " len %i\n", s->frame_num, c, i, s->channel[c].subframe_len[i]); 581 " len %i\n", s->frame_num, c, i,
582 s->channel[c].subframe_len[i]);
582 s->channel[c].subframe_offset[i] = offset; 583 s->channel[c].subframe_offset[i] = offset;
583 offset += s->channel[c].subframe_len[i]; 584 offset += s->channel[c].subframe_len[i];
584 } 585 }
585 } 586 }
586 587
604 for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++) 605 for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++)
605 rotation_offset[i] = get_bits(&s->gb, 6); 606 rotation_offset[i] = get_bits(&s->gb, 6);
606 607
607 for (i = 0; i < chgroup->num_channels; i++) 608 for (i = 0; i < chgroup->num_channels; i++)
608 chgroup->decorrelation_matrix[chgroup->num_channels * i + i] = 609 chgroup->decorrelation_matrix[chgroup->num_channels * i + i] =
609 get_bits1(&s->gb) ? 1.0 : -1.0; 610 get_bits1(&s->gb) ? 1.0 : -1.0;
610 611
611 for (i = 1; i < chgroup->num_channels; i++) { 612 for (i = 1; i < chgroup->num_channels; i++) {
612 int x; 613 int x;
613 for (x = 0; x < i; x++) { 614 for (x = 0; x < i; x++) {
614 int y; 615 int y;
619 float sinv; 620 float sinv;
620 float cosv; 621 float cosv;
621 622
622 if (n < 32) { 623 if (n < 32) {
623 sinv = sin64[n]; 624 sinv = sin64[n];
624 cosv = sin64[32-n]; 625 cosv = sin64[32 - n];
625 } else { 626 } else {
626 sinv = sin64[64-n]; 627 sinv = sin64[64 - n];
627 cosv = -sin64[n-32]; 628 cosv = -sin64[n - 32];
628 } 629 }
629 630
630 chgroup->decorrelation_matrix[y + x * chgroup->num_channels] = 631 chgroup->decorrelation_matrix[y + x * chgroup->num_channels] =
631 (v1 * sinv) - (v2 * cosv); 632 (v1 * sinv) - (v2 * cosv);
632 chgroup->decorrelation_matrix[y + i * chgroup->num_channels] = 633 chgroup->decorrelation_matrix[y + i * chgroup->num_channels] =
644 */ 645 */
645 static int decode_channel_transform(WMAProDecodeCtx* s) 646 static int decode_channel_transform(WMAProDecodeCtx* s)
646 { 647 {
647 int i; 648 int i;
648 /* should never consume more than 1921 bits for the 8 channel case 649 /* should never consume more than 1921 bits for the 8 channel case
649 * 1 + MAX_CHANNELS * ( MAX_CHANNELS + 2 + 3 * MAX_CHANNELS * MAX_CHANNELS 650 * 1 + MAX_CHANNELS * (MAX_CHANNELS + 2 + 3 * MAX_CHANNELS * MAX_CHANNELS
650 * + MAX_CHANNELS + MAX_BANDS + 1) 651 * + MAX_CHANNELS + MAX_BANDS + 1)
651 */ 652 */
652 653
653 /** in the one channel case channel transforms are pointless */ 654 /** in the one channel case channel transforms are pointless */
654 s->num_chgroups = 0; 655 s->num_chgroups = 0;
660 "unsupported channel transform bit\n"); 661 "unsupported channel transform bit\n");
661 return AVERROR_INVALIDDATA; 662 return AVERROR_INVALIDDATA;
662 } 663 }
663 664
664 for (s->num_chgroups = 0; remaining_channels && 665 for (s->num_chgroups = 0; remaining_channels &&
665 s->num_chgroups < s->channels_for_cur_subframe; s->num_chgroups++) { 666 s->num_chgroups < s->channels_for_cur_subframe; s->num_chgroups++) {
666 WMAProChannelGrp* chgroup = &s->chgroup[s->num_chgroups]; 667 WMAProChannelGrp* chgroup = &s->chgroup[s->num_chgroups];
667 float** channel_data = chgroup->channel_data; 668 float** channel_data = chgroup->channel_data;
668 chgroup->num_channels = 0; 669 chgroup->num_channels = 0;
669 chgroup->transform = 0; 670 chgroup->transform = 0;
670 671
692 /** decode transform type */ 693 /** decode transform type */
693 if (chgroup->num_channels == 2) { 694 if (chgroup->num_channels == 2) {
694 if (get_bits1(&s->gb)) { 695 if (get_bits1(&s->gb)) {
695 if (get_bits1(&s->gb)) { 696 if (get_bits1(&s->gb)) {
696 av_log_ask_for_sample(s->avctx, 697 av_log_ask_for_sample(s->avctx,
697 "unsupported channel transform type\n"); 698 "unsupported channel transform type\n");
698 } 699 }
699 } else { 700 } else {
700 chgroup->transform = 1; 701 chgroup->transform = 1;
701 if (s->num_channels == 2) { 702 if (s->num_channels == 2) {
702 chgroup->decorrelation_matrix[0] = 1.0; 703 chgroup->decorrelation_matrix[0] = 1.0;
718 decode_decorrelation_matrix(s, chgroup); 719 decode_decorrelation_matrix(s, chgroup);
719 } else { 720 } else {
720 /** FIXME: more than 6 coupled channels not supported */ 721 /** FIXME: more than 6 coupled channels not supported */
721 if (chgroup->num_channels > 6) { 722 if (chgroup->num_channels > 6) {
722 av_log_ask_for_sample(s->avctx, 723 av_log_ask_for_sample(s->avctx,
723 "coupled channels > 6\n"); 724 "coupled channels > 6\n");
724 } else { 725 } else {
725 memcpy(chgroup->decorrelation_matrix, 726 memcpy(chgroup->decorrelation_matrix,
726 default_decorrelation[chgroup->num_channels], 727 default_decorrelation[chgroup->num_channels],
727 chgroup->num_channels * chgroup->num_channels * 728 chgroup->num_channels * chgroup->num_channels *
728 sizeof(*chgroup->decorrelation_matrix)); 729 sizeof(*chgroup->decorrelation_matrix));
729 } 730 }
730 } 731 }
731 } 732 }
732 } 733 }
733 734
802 vals[i] = symbol_to_vec2[idx] >> 4; 803 vals[i] = symbol_to_vec2[idx] >> 4;
803 vals[i+1] = symbol_to_vec2[idx] & 0xF; 804 vals[i+1] = symbol_to_vec2[idx] & 0xF;
804 } 805 }
805 } 806 }
806 } else { 807 } else {
807 vals[0] = symbol_to_vec4[idx] >> 12; 808 vals[0] = symbol_to_vec4[idx] >> 12;
808 vals[1] = (symbol_to_vec4[idx] >> 8) & 0xF; 809 vals[1] = (symbol_to_vec4[idx] >> 8) & 0xF;
809 vals[2] = (symbol_to_vec4[idx] >> 4) & 0xF; 810 vals[2] = (symbol_to_vec4[idx] >> 4) & 0xF;
810 vals[3] = symbol_to_vec4[idx] & 0xF; 811 vals[3] = symbol_to_vec4[idx] & 0xF;
811 } 812 }
812 813
813 /** decode sign */ 814 /** decode sign */
814 for (i = 0; i < 4; i++) { 815 for (i = 0; i < 4; i++) {
815 if (vals[i]) { 816 if (vals[i]) {
816 int sign = get_bits1(&s->gb) - 1; 817 int sign = get_bits1(&s->gb) - 1;
817 ci->coeffs[cur_coeff] = (vals[i]^sign) - sign; 818 ci->coeffs[cur_coeff] = (vals[i] ^ sign) - sign;
818 num_zeros = 0; 819 num_zeros = 0;
819 } else { 820 } else {
820 ci->coeffs[cur_coeff] = 0; 821 ci->coeffs[cur_coeff] = 0;
821 /** switch to run level mode when subframe_len / 128 zeros 822 /** switch to run level mode when subframe_len / 128 zeros
822 were found in a row */ 823 were found in a row */
823 rl_mode |= (++num_zeros > s->subframe_len>>8); 824 rl_mode |= (++num_zeros > s->subframe_len >> 8);
824 } 825 }
825 ++cur_coeff; 826 ++cur_coeff;
826 } 827 }
827 } 828 }
828 829
891 int val; 892 int val;
892 int sign; 893 int sign;
893 894
894 idx = get_vlc2(&s->gb, sf_rl_vlc.table, VLCBITS, SCALERLMAXDEPTH); 895 idx = get_vlc2(&s->gb, sf_rl_vlc.table, VLCBITS, SCALERLMAXDEPTH);
895 896
896 if ( !idx ) { 897 if (!idx) {
897 uint32_t code = get_bits(&s->gb, 14); 898 uint32_t code = get_bits(&s->gb, 14);
898 val = code >> 6; 899 val = code >> 6;
899 sign = (code & 1) - 1; 900 sign = (code & 1) - 1;
900 skip = (code & 0x3f) >> 1; 901 skip = (code & 0x3f) >> 1;
901 } else if (idx == 1) { 902 } else if (idx == 1) {
906 sign = get_bits1(&s->gb)-1; 907 sign = get_bits1(&s->gb)-1;
907 } 908 }
908 909
909 i += skip; 910 i += skip;
910 if (i >= s->num_bands) { 911 if (i >= s->num_bands) {
911 av_log(s->avctx,AV_LOG_ERROR, 912 av_log(s->avctx, AV_LOG_ERROR,
912 "invalid scale factor coding\n"); 913 "invalid scale factor coding\n");
913 return AVERROR_INVALIDDATA; 914 return AVERROR_INVALIDDATA;
914 } 915 }
915 s->channel[c].scale_factors[i] += (val ^ sign) - sign; 916 s->channel[c].scale_factors[i] += (val ^ sign) - sign;
916 } 917 }
953 const int8_t* tb = s->chgroup[i].transform_band; 954 const int8_t* tb = s->chgroup[i].transform_band;
954 int16_t* sfb; 955 int16_t* sfb;
955 956
956 /** multichannel decorrelation */ 957 /** multichannel decorrelation */
957 for (sfb = s->cur_sfb_offsets; 958 for (sfb = s->cur_sfb_offsets;
958 sfb < s->cur_sfb_offsets + s->num_bands;sfb++) { 959 sfb < s->cur_sfb_offsets + s->num_bands; sfb++) {
959 int y; 960 int y;
960 if (*tb++ == 1) { 961 if (*tb++ == 1) {
961 /** multiply values with the decorrelation_matrix */ 962 /** multiply values with the decorrelation_matrix */
962 for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) { 963 for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) {
963 const float* mat = s->chgroup[i].decorrelation_matrix; 964 const float* mat = s->chgroup[i].decorrelation_matrix;
964 const float* data_end = data + num_channels; 965 const float* data_end = data + num_channels;
965 float* data_ptr = data; 966 float* data_ptr = data;
966 float** ch; 967 float** ch;
967 968
968 for (ch = ch_data; ch < ch_end; ch++) 969 for (ch = ch_data; ch < ch_end; ch++)
969 *data_ptr++ = (*ch)[y]; 970 *data_ptr++ = (*ch)[y];
970 971
971 for (ch = ch_data; ch < ch_end; ch++) { 972 for (ch = ch_data; ch < ch_end; ch++) {
972 float sum = 0; 973 float sum = 0;
973 data_ptr = data; 974 data_ptr = data;
974 while (data_ptr < data_end) 975 while (data_ptr < data_end)
993 *@param s codec context 994 *@param s codec context
994 */ 995 */
995 static void wmapro_window(WMAProDecodeCtx *s) 996 static void wmapro_window(WMAProDecodeCtx *s)
996 { 997 {
997 int i; 998 int i;
998 for (i = 0; i< s->channels_for_cur_subframe; i++) { 999 for (i = 0; i < s->channels_for_cur_subframe; i++) {
999 int c = s->channel_indexes_for_cur_subframe[i]; 1000 int c = s->channel_indexes_for_cur_subframe[i];
1000 float* window; 1001 float* window;
1001 int winlen = s->channel[c].prev_block_len; 1002 int winlen = s->channel[c].prev_block_len;
1002 float* start = s->channel[c].coeffs - (winlen >> 1); 1003 float* start = s->channel[c].coeffs - (winlen >> 1);
1003 1004
1004 if (s->subframe_len < winlen) { 1005 if (s->subframe_len < winlen) {
1005 start += (winlen - s->subframe_len)>>1; 1006 start += (winlen - s->subframe_len) >> 1;
1006 winlen = s->subframe_len; 1007 winlen = s->subframe_len;
1007 } 1008 }
1008 1009
1009 window = s->windows[av_log2(winlen)-BLOCK_MIN_BITS]; 1010 window = s->windows[av_log2(winlen) - BLOCK_MIN_BITS];
1010 1011
1011 winlen >>= 1; 1012 winlen >>= 1;
1012 1013
1013 s->dsp.vector_fmul_window(start, start, start + winlen, 1014 s->dsp.vector_fmul_window(start, start, start + winlen,
1014 window, 0, winlen); 1015 window, 0, winlen);
1045 s->channel[i].subframe_len[s->channel[i].cur_subframe]; 1046 s->channel[i].subframe_len[s->channel[i].cur_subframe];
1046 } 1047 }
1047 } 1048 }
1048 1049
1049 dprintf(s->avctx, 1050 dprintf(s->avctx,
1050 "processing subframe with offset %i len %i\n", offset, subframe_len); 1051 "processing subframe with offset %i len %i\n", offset, subframe_len);
1051 1052
1052 /** get a list of all channels that contain the estimated block */ 1053 /** get a list of all channels that contain the estimated block */
1053 s->channels_for_cur_subframe = 0; 1054 s->channels_for_cur_subframe = 0;
1054 for (i = 0; i < s->num_channels; i++) { 1055 for (i = 0; i < s->num_channels; i++) {
1055 const int cur_subframe = s->channel[i].cur_subframe; 1056 const int cur_subframe = s->channel[i].cur_subframe;
1056 /** substract already processed samples */ 1057 /** substract already processed samples */
1057 total_samples -= s->channel[i].decoded_samples; 1058 total_samples -= s->channel[i].decoded_samples;
1058 1059
1059 /** and count if there are multiple subframes that match our profile */ 1060 /** and count if there are multiple subframes that match our profile */
1060 if (offset == s->channel[i].decoded_samples && 1061 if (offset == s->channel[i].decoded_samples &&
1061 subframe_len == s->channel[i].subframe_len[cur_subframe]) { 1062 subframe_len == s->channel[i].subframe_len[cur_subframe]) {
1062 total_samples -= s->channel[i].subframe_len[cur_subframe]; 1063 total_samples -= s->channel[i].subframe_len[cur_subframe];
1063 s->channel[i].decoded_samples += 1064 s->channel[i].decoded_samples +=
1064 s->channel[i].subframe_len[cur_subframe]; 1065 s->channel[i].subframe_len[cur_subframe];
1065 s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i; 1066 s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
1066 ++s->channels_for_cur_subframe; 1067 ++s->channels_for_cur_subframe;
1072 if (!total_samples) 1073 if (!total_samples)
1073 s->parsed_all_subframes = 1; 1074 s->parsed_all_subframes = 1;
1074 1075
1075 1076
1076 dprintf(s->avctx, "subframe is part of %i channels\n", 1077 dprintf(s->avctx, "subframe is part of %i channels\n",
1077 s->channels_for_cur_subframe); 1078 s->channels_for_cur_subframe);
1078 1079
1079 /** calculate number of scale factor bands and their offsets */ 1080 /** calculate number of scale factor bands and their offsets */
1080 s->table_idx = av_log2(s->samples_per_frame/subframe_len); 1081 s->table_idx = av_log2(s->samples_per_frame/subframe_len);
1081 s->num_bands = s->num_sfb[s->table_idx]; 1082 s->num_bands = s->num_sfb[s->table_idx];
1082 s->cur_sfb_offsets = s->sfb_offsets[s->table_idx]; 1083 s->cur_sfb_offsets = s->sfb_offsets[s->table_idx];
1084 1085
1085 /** configure the decoder for the current subframe */ 1086 /** configure the decoder for the current subframe */
1086 for (i = 0; i < s->channels_for_cur_subframe; i++) { 1087 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1087 int c = s->channel_indexes_for_cur_subframe[i]; 1088 int c = s->channel_indexes_for_cur_subframe[i];
1088 1089
1089 s->channel[c].coeffs = &s->channel[c].out[(s->samples_per_frame>>1) 1090 s->channel[c].coeffs = &s->channel[c].out[(s->samples_per_frame >> 1)
1090 + offset]; 1091 + offset];
1091 } 1092 }
1092 1093
1093 s->subframe_len = subframe_len; 1094 s->subframe_len = subframe_len;
1094 s->esc_len = av_log2(s->subframe_len - 1) + 1; 1095 s->esc_len = av_log2(s->subframe_len - 1) + 1;
1101 num_fill_bits = get_bits(&s->gb, len) + 1; 1102 num_fill_bits = get_bits(&s->gb, len) + 1;
1102 } 1103 }
1103 1104
1104 if (num_fill_bits >= 0) { 1105 if (num_fill_bits >= 0) {
1105 if (get_bits_count(&s->gb) + num_fill_bits > s->num_saved_bits) { 1106 if (get_bits_count(&s->gb) + num_fill_bits > s->num_saved_bits) {
1106 av_log(s->avctx,AV_LOG_ERROR,"invalid number of fill bits\n"); 1107 av_log(s->avctx, AV_LOG_ERROR, "invalid number of fill bits\n");
1107 return AVERROR_INVALIDDATA; 1108 return AVERROR_INVALIDDATA;
1108 } 1109 }
1109 1110
1110 skip_bits_long(&s->gb, num_fill_bits); 1111 skip_bits_long(&s->gb, num_fill_bits);
1111 } 1112 }
1141 quant_step += step; 1142 quant_step += step;
1142 if (step == -32 || step == 31) { 1143 if (step == -32 || step == 31) {
1143 const int sign = (step == 31) - 1; 1144 const int sign = (step == 31) - 1;
1144 int quant = 0; 1145 int quant = 0;
1145 while (get_bits_count(&s->gb) + 5 < s->num_saved_bits && 1146 while (get_bits_count(&s->gb) + 5 < s->num_saved_bits &&
1146 (step = get_bits(&s->gb, 5)) == 31 ) { 1147 (step = get_bits(&s->gb, 5)) == 31) {
1147 quant += 31; 1148 quant += 31;
1148 } 1149 }
1149 quant_step += ((quant + step) ^ sign) - sign; 1150 quant_step += ((quant + step) ^ sign) - sign;
1150 } 1151 }
1151 if (quant_step < 0) { 1152 if (quant_step < 0) {
1152 av_log(s->avctx,AV_LOG_DEBUG,"negative quant step\n"); 1153 av_log(s->avctx, AV_LOG_DEBUG, "negative quant step\n");
1153 } 1154 }
1154 1155
1155 /** decode quantization step modifiers for every channel */ 1156 /** decode quantization step modifiers for every channel */
1156 1157
1157 if (s->channels_for_cur_subframe == 1) { 1158 if (s->channels_for_cur_subframe == 1) {
1161 for (i = 0; i < s->channels_for_cur_subframe; i++) { 1162 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1162 int c = s->channel_indexes_for_cur_subframe[i]; 1163 int c = s->channel_indexes_for_cur_subframe[i];
1163 s->channel[c].quant_step = quant_step; 1164 s->channel[c].quant_step = quant_step;
1164 if (get_bits1(&s->gb)) { 1165 if (get_bits1(&s->gb)) {
1165 if (modifier_len) { 1166 if (modifier_len) {
1166 s->channel[c].quant_step += 1167 s->channel[c].quant_step += get_bits(&s->gb, modifier_len) + 1;
1167 get_bits(&s->gb, modifier_len) + 1;
1168 } else 1168 } else
1169 ++s->channel[c].quant_step; 1169 ++s->channel[c].quant_step;
1170 } 1170 }
1171 } 1171 }
1172 } 1172 }
1175 if (decode_scale_factors(s) < 0) 1175 if (decode_scale_factors(s) < 0)
1176 return AVERROR_INVALIDDATA; 1176 return AVERROR_INVALIDDATA;
1177 } 1177 }
1178 1178
1179 dprintf(s->avctx, "BITSTREAM: subframe header length was %i\n", 1179 dprintf(s->avctx, "BITSTREAM: subframe header length was %i\n",
1180 get_bits_count(&s->gb) - s->subframe_offset); 1180 get_bits_count(&s->gb) - s->subframe_offset);
1181 1181
1182 /** parse coefficients */ 1182 /** parse coefficients */
1183 for (i = 0; i < s->channels_for_cur_subframe; i++) { 1183 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1184 int c = s->channel_indexes_for_cur_subframe[i]; 1184 int c = s->channel_indexes_for_cur_subframe[i];
1185 if (s->channel[c].transmit_coefs && 1185 if (s->channel[c].transmit_coefs &&
1186 get_bits_count(&s->gb) < s->num_saved_bits) { 1186 get_bits_count(&s->gb) < s->num_saved_bits) {
1187 decode_coeffs(s, c); 1187 decode_coeffs(s, c);
1188 } else 1188 } else
1189 memset(s->channel[c].coeffs, 0, 1189 memset(s->channel[c].coeffs, 0,
1190 sizeof(*s->channel[c].coeffs) * subframe_len); 1190 sizeof(*s->channel[c].coeffs) * subframe_len);
1191 } 1191 }
1192 1192
1193 dprintf(s->avctx, "BITSTREAM: subframe length was %i\n", 1193 dprintf(s->avctx, "BITSTREAM: subframe length was %i\n",
1194 get_bits_count(&s->gb) - s->subframe_offset); 1194 get_bits_count(&s->gb) - s->subframe_offset);
1195 1195
1196 if (transmit_coeffs) { 1196 if (transmit_coeffs) {
1197 /** reconstruct the per channel data */ 1197 /** reconstruct the per channel data */
1198 inverse_channel_transform(s); 1198 inverse_channel_transform(s);
1199 for (i = 0; i < s->channels_for_cur_subframe; i++) { 1199 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1217 for (start = s->cur_sfb_offsets[b]; start < end; start++) 1217 for (start = s->cur_sfb_offsets[b]; start < end; start++)
1218 s->tmp[start] = s->channel[c].coeffs[start] * quant; 1218 s->tmp[start] = s->channel[c].coeffs[start] * quant;
1219 } 1219 }
1220 1220
1221 /** apply imdct (ff_imdct_half == DCTIV with reverse) */ 1221 /** apply imdct (ff_imdct_half == DCTIV with reverse) */
1222 ff_imdct_half(&s->mdct_ctx[av_log2(subframe_len)-BLOCK_MIN_BITS], 1222 ff_imdct_half(&s->mdct_ctx[av_log2(subframe_len) - BLOCK_MIN_BITS],
1223 s->channel[c].coeffs, s->tmp); 1223 s->channel[c].coeffs, s->tmp);
1224 } 1224 }
1225 } 1225 }
1226 1226
1227 /** window and overlapp-add */ 1227 /** window and overlapp-add */
1229 1229
1230 /** handled one subframe */ 1230 /** handled one subframe */
1231 for (i = 0; i < s->channels_for_cur_subframe; i++) { 1231 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1232 int c = s->channel_indexes_for_cur_subframe[i]; 1232 int c = s->channel_indexes_for_cur_subframe[i];
1233 if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) { 1233 if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
1234 av_log(s->avctx,AV_LOG_ERROR,"broken subframe\n"); 1234 av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
1235 return AVERROR_INVALIDDATA; 1235 return AVERROR_INVALIDDATA;
1236 } 1236 }
1237 ++s->channel[c].cur_subframe; 1237 ++s->channel[c].cur_subframe;
1238 } 1238 }
1239 1239
1253 int len = 0; 1253 int len = 0;
1254 int i; 1254 int i;
1255 1255
1256 /** check for potential output buffer overflow */ 1256 /** check for potential output buffer overflow */
1257 if (s->num_channels * s->samples_per_frame > s->samples_end - s->samples) { 1257 if (s->num_channels * s->samples_per_frame > s->samples_end - s->samples) {
1258 av_log(s->avctx,AV_LOG_ERROR, 1258 av_log(s->avctx, AV_LOG_ERROR,
1259 "not enough space for the output samples\n"); 1259 "not enough space for the output samples\n");
1260 s->packet_loss = 1; 1260 s->packet_loss = 1;
1261 return 0; 1261 return 0;
1262 } 1262 }
1263 1263
1304 } 1304 }
1305 1305
1306 } 1306 }
1307 1307
1308 dprintf(s->avctx, "BITSTREAM: frame header length was %i\n", 1308 dprintf(s->avctx, "BITSTREAM: frame header length was %i\n",
1309 get_bits_count(gb) - s->frame_offset); 1309 get_bits_count(gb) - s->frame_offset);
1310 1310
1311 /** reset subframe states */ 1311 /** reset subframe states */
1312 s->parsed_all_subframes = 0; 1312 s->parsed_all_subframes = 0;
1313 for (i = 0; i < s->num_channels; i++) { 1313 for (i = 0; i < s->num_channels; i++) {
1314 s->channel[i].decoded_samples = 0; 1314 s->channel[i].decoded_samples = 0;
1349 } else 1349 } else
1350 s->samples += s->num_channels * s->samples_per_frame; 1350 s->samples += s->num_channels * s->samples_per_frame;
1351 1351
1352 if (len != (get_bits_count(gb) - s->frame_offset) + 2) { 1352 if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
1353 /** FIXME: not sure if this is always an error */ 1353 /** FIXME: not sure if this is always an error */
1354 av_log(s->avctx,AV_LOG_ERROR,"frame[%i] would have to skip %i bits\n", 1354 av_log(s->avctx, AV_LOG_ERROR, "frame[%i] would have to skip %i bits\n",
1355 s->frame_num, len - (get_bits_count(gb) - s->frame_offset) - 1); 1355 s->frame_num, len - (get_bits_count(gb) - s->frame_offset) - 1);
1356 s->packet_loss = 1; 1356 s->packet_loss = 1;
1357 return 0; 1357 return 0;
1358 } 1358 }
1359 1359
1371 *@brief Calculate remaining input buffer length. 1371 *@brief Calculate remaining input buffer length.
1372 *@param s codec context 1372 *@param s codec context
1373 *@param gb bitstream reader context 1373 *@param gb bitstream reader context
1374 *@return remaining size in bits 1374 *@return remaining size in bits
1375 */ 1375 */
1376 static int remaining_bits(WMAProDecodeCtx *s, GetBitContext* gb) 1376 static int remaining_bits(WMAProDecodeCtx *s, GetBitContext *gb)
1377 { 1377 {
1378 return s->buf_bit_size - get_bits_count(gb); 1378 return s->buf_bit_size - get_bits_count(gb);
1379 } 1379 }
1380 1380
1381 /** 1381 /**
1384 *@param gb bitstream reader context 1384 *@param gb bitstream reader context
1385 *@param len length of the partial frame 1385 *@param len length of the partial frame
1386 *@param append decides wether to reset the buffer or not 1386 *@param append decides wether to reset the buffer or not
1387 */ 1387 */
1388 static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len, 1388 static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len,
1389 int append) 1389 int append)
1390 { 1390 {
1391 int buflen; 1391 int buflen;
1392 1392
1393 /** when the frame data does not need to be concatenated, the input buffer 1393 /** when the frame data does not need to be concatenated, the input buffer
1394 is resetted and additional bits from the previous frame are copyed 1394 is resetted and additional bits from the previous frame are copyed
1401 } 1401 }
1402 1402
1403 buflen = (s->num_saved_bits + len + 8) >> 3; 1403 buflen = (s->num_saved_bits + len + 8) >> 3;
1404 1404
1405 if (len <= 0 || buflen > MAX_FRAMESIZE) { 1405 if (len <= 0 || buflen > MAX_FRAMESIZE) {
1406 av_log_ask_for_sample(s->avctx, "input buffer too small\n"); 1406 av_log_ask_for_sample(s->avctx, "input buffer too small\n");
1407 s->packet_loss = 1; 1407 s->packet_loss = 1;
1408 return; 1408 return;
1409 } 1409 }
1410 1410
1411 s->num_saved_bits += len; 1411 s->num_saved_bits += len;
1412 if (!append) { 1412 if (!append) {
1413 ff_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), s->num_saved_bits); 1413 ff_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
1414 s->num_saved_bits);
1414 } else { 1415 } else {
1415 int align = 8 - (get_bits_count(gb) & 7); 1416 int align = 8 - (get_bits_count(gb) & 7);
1416 align = FFMIN(align, len); 1417 align = FFMIN(align, len);
1417 put_bits(&s->pb, align, get_bits(gb, align)); 1418 put_bits(&s->pb, align, get_bits(gb, align));
1418 len -= align; 1419 len -= align;
1419 ff_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len); 1420 ff_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
1420 } 1421 }
1421 skip_bits_long(gb, len); 1422 skip_bits_long(gb, len);
1422 1423
1423 { 1424 {
1424 PutBitContext tmp = s->pb; 1425 PutBitContext tmp = s->pb;
1425 flush_put_bits(&tmp); 1426 flush_put_bits(&tmp);
1426 } 1427 }
1427 1428
1428 init_get_bits(&s->gb, s->frame_data, s->num_saved_bits); 1429 init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
1429 skip_bits(&s->gb, s->frame_offset); 1430 skip_bits(&s->gb, s->frame_offset);
1430 } 1431 }
1436 *@param data_size number of bytes that were written to the output buffer 1437 *@param data_size number of bytes that were written to the output buffer
1437 *@param avpkt input packet 1438 *@param avpkt input packet
1438 *@return number of bytes that were read from the input buffer 1439 *@return number of bytes that were read from the input buffer
1439 */ 1440 */
1440 static int decode_packet(AVCodecContext *avctx, 1441 static int decode_packet(AVCodecContext *avctx,
1441 void *data, int *data_size, AVPacket* avpkt) 1442 void *data, int *data_size, AVPacket* avpkt)
1442 { 1443 {
1443 GetBitContext gb; 1444 GetBitContext gb;
1444 WMAProDecodeCtx *s = avctx->priv_data; 1445 WMAProDecodeCtx *s = avctx->priv_data;
1445 const uint8_t* buf = avpkt->data; 1446 const uint8_t* buf = avpkt->data;
1446 int buf_size = avpkt->size; 1447 int buf_size = avpkt->size;
1467 skip_bits(&gb, 2); 1468 skip_bits(&gb, 2);
1468 1469
1469 /** get number of bits that need to be added to the previous frame */ 1470 /** get number of bits that need to be added to the previous frame */
1470 num_bits_prev_frame = get_bits(&gb, s->log2_frame_size); 1471 num_bits_prev_frame = get_bits(&gb, s->log2_frame_size);
1471 dprintf(avctx, "packet[%d]: nbpf %x\n", avctx->frame_number, 1472 dprintf(avctx, "packet[%d]: nbpf %x\n", avctx->frame_number,
1472 num_bits_prev_frame); 1473 num_bits_prev_frame);
1473 1474
1474 /** check for packet loss */ 1475 /** check for packet loss */
1475 if (!s->packet_loss && 1476 if (!s->packet_loss &&
1476 ((s->packet_sequence_number + 1)&0xF) != packet_sequence_number) { 1477 ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
1477 s->packet_loss = 1; 1478 s->packet_loss = 1;
1478 av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n", 1479 av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n",
1479 s->packet_sequence_number, packet_sequence_number); 1480 s->packet_sequence_number, packet_sequence_number);
1480 } 1481 }
1481 s->packet_sequence_number = packet_sequence_number; 1482 s->packet_sequence_number = packet_sequence_number;
1482 1483
1483 if (num_bits_prev_frame > 0) { 1484 if (num_bits_prev_frame > 0) {
1484 /** append the previous frame data to the remaining data from the 1485 /** append the previous frame data to the remaining data from the
1485 previous packet to create a full frame */ 1486 previous packet to create a full frame */
1486 save_bits(s, &gb, num_bits_prev_frame, 1); 1487 save_bits(s, &gb, num_bits_prev_frame, 1);
1487 dprintf(avctx, "accumulated %x bits of frame data\n", 1488 dprintf(avctx, "accumulated %x bits of frame data\n",
1488 s->num_saved_bits - s->frame_offset); 1489 s->num_saved_bits - s->frame_offset);
1489 1490
1490 /** decode the cross packet frame if it is valid */ 1491 /** decode the cross packet frame if it is valid */
1491 if (!s->packet_loss) 1492 if (!s->packet_loss)
1492 decode_frame(s); 1493 decode_frame(s);
1493 } else if (s->num_saved_bits - s->frame_offset) { 1494 } else if (s->num_saved_bits - s->frame_offset) {
1494 dprintf(avctx, "ignoring %x previously saved bits\n", 1495 dprintf(avctx, "ignoring %x previously saved bits\n",
1495 s->num_saved_bits - s->frame_offset); 1496 s->num_saved_bits - s->frame_offset);
1496 } 1497 }
1497 1498
1498 s->packet_loss = 0; 1499 s->packet_loss = 0;
1499 /** decode the rest of the packet */ 1500 /** decode the rest of the packet */
1500 while (!s->packet_loss && more_frames && 1501 while (!s->packet_loss && more_frames &&
1501 remaining_bits(s, &gb) > s->log2_frame_size) { 1502 remaining_bits(s, &gb) > s->log2_frame_size) {
1502 int frame_size = show_bits(&gb, s->log2_frame_size); 1503 int frame_size = show_bits(&gb, s->log2_frame_size);
1503 1504
1504 /** there is enough data for a full frame */ 1505 /** there is enough data for a full frame */
1505 if (remaining_bits(s,&gb) >= frame_size && frame_size > 0) { 1506 if (remaining_bits(s, &gb) >= frame_size && frame_size > 0) {
1506 save_bits(s, &gb, frame_size, 0); 1507 save_bits(s, &gb, frame_size, 0);
1507 1508
1508 /** decode the frame */ 1509 /** decode the frame */
1509 more_frames = decode_frame(s); 1510 more_frames = decode_frame(s);
1510 1511
1513 } 1514 }
1514 } else 1515 } else
1515 more_frames = 0; 1516 more_frames = 0;
1516 } 1517 }
1517 1518
1518 if (!s->packet_loss && remaining_bits(s,&gb) > 0) { 1519 if (!s->packet_loss && remaining_bits(s, &gb) > 0) {
1519 /** save the rest of the data so that it can be decoded 1520 /** save the rest of the data so that it can be decoded
1520 with the next packet */ 1521 with the next packet */
1521 save_bits(s, &gb, remaining_bits(s,&gb), 0); 1522 save_bits(s, &gb, remaining_bits(s, &gb), 0);
1522 } 1523 }
1523 1524
1524 *data_size = (int8_t *)s->samples - (int8_t *)data; 1525 *data_size = (int8_t *)s->samples - (int8_t *)data;
1525 1526
1526 return avctx->block_align; 1527 return avctx->block_align;