Libav 0.7.1
libavcodec/wmaprodec.c
Go to the documentation of this file.
00001 /*
00002  * Wmapro compatible decoder
00003  * Copyright (c) 2007 Baptiste Coudurier, Benjamin Larsson, Ulion
00004  * Copyright (c) 2008 - 2011 Sascha Sommer, Benjamin Larsson
00005  *
00006  * This file is part of Libav.
00007  *
00008  * Libav is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * Libav is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with Libav; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00089 #include "avcodec.h"
00090 #include "internal.h"
00091 #include "get_bits.h"
00092 #include "put_bits.h"
00093 #include "wmaprodata.h"
00094 #include "dsputil.h"
00095 #include "sinewin.h"
00096 #include "wma.h"
00097 
00099 #define WMAPRO_MAX_CHANNELS    8                             ///< max number of handled channels
00100 #define MAX_SUBFRAMES  32                                    ///< max number of subframes per channel
00101 #define MAX_BANDS      29                                    ///< max number of scale factor bands
00102 #define MAX_FRAMESIZE  32768                                 ///< maximum compressed frame size
00103 
00104 #define WMAPRO_BLOCK_MIN_BITS  6                                           ///< log2 of min block size
00105 #define WMAPRO_BLOCK_MAX_BITS 12                                           ///< log2 of max block size
00106 #define WMAPRO_BLOCK_MAX_SIZE (1 << WMAPRO_BLOCK_MAX_BITS)                 ///< maximum block size
00107 #define WMAPRO_BLOCK_SIZES    (WMAPRO_BLOCK_MAX_BITS - WMAPRO_BLOCK_MIN_BITS + 1) ///< possible block sizes
00108 
00109 
00110 #define VLCBITS            9
00111 #define SCALEVLCBITS       8
00112 #define VEC4MAXDEPTH    ((HUFF_VEC4_MAXBITS+VLCBITS-1)/VLCBITS)
00113 #define VEC2MAXDEPTH    ((HUFF_VEC2_MAXBITS+VLCBITS-1)/VLCBITS)
00114 #define VEC1MAXDEPTH    ((HUFF_VEC1_MAXBITS+VLCBITS-1)/VLCBITS)
00115 #define SCALEMAXDEPTH   ((HUFF_SCALE_MAXBITS+SCALEVLCBITS-1)/SCALEVLCBITS)
00116 #define SCALERLMAXDEPTH ((HUFF_SCALE_RL_MAXBITS+VLCBITS-1)/VLCBITS)
00117 
00118 static VLC              sf_vlc;           
00119 static VLC              sf_rl_vlc;        
00120 static VLC              vec4_vlc;         
00121 static VLC              vec2_vlc;         
00122 static VLC              vec1_vlc;         
00123 static VLC              coef_vlc[2];      
00124 static float            sin64[33];        
00125 
00129 typedef struct {
00130     int16_t  prev_block_len;                          
00131     uint8_t  transmit_coefs;
00132     uint8_t  num_subframes;
00133     uint16_t subframe_len[MAX_SUBFRAMES];             
00134     uint16_t subframe_offset[MAX_SUBFRAMES];          
00135     uint8_t  cur_subframe;                            
00136     uint16_t decoded_samples;                         
00137     uint8_t  grouped;                                 
00138     int      quant_step;                              
00139     int8_t   reuse_sf;                                
00140     int8_t   scale_factor_step;                       
00141     int      max_scale_factor;                        
00142     int      saved_scale_factors[2][MAX_BANDS];       
00143     int8_t   scale_factor_idx;                        
00144     int*     scale_factors;                           
00145     uint8_t  table_idx;                               
00146     float*   coeffs;                                  
00147     uint16_t num_vec_coeffs;                          
00148     DECLARE_ALIGNED(32, float, out)[WMAPRO_BLOCK_MAX_SIZE + WMAPRO_BLOCK_MAX_SIZE / 2]; 
00149 } WMAProChannelCtx;
00150 
00154 typedef struct {
00155     uint8_t num_channels;                                     
00156     int8_t  transform;                                        
00157     int8_t  transform_band[MAX_BANDS];                        
00158     float   decorrelation_matrix[WMAPRO_MAX_CHANNELS*WMAPRO_MAX_CHANNELS];
00159     float*  channel_data[WMAPRO_MAX_CHANNELS];                
00160 } WMAProChannelGrp;
00161 
00165 typedef struct WMAProDecodeCtx {
00166     /* generic decoder variables */
00167     AVCodecContext*  avctx;                         
00168     DSPContext       dsp;                           
00169     uint8_t          frame_data[MAX_FRAMESIZE +
00170                       FF_INPUT_BUFFER_PADDING_SIZE];
00171     PutBitContext    pb;                            
00172     FFTContext       mdct_ctx[WMAPRO_BLOCK_SIZES];  
00173     DECLARE_ALIGNED(32, float, tmp)[WMAPRO_BLOCK_MAX_SIZE]; 
00174     float*           windows[WMAPRO_BLOCK_SIZES];   
00175 
00176     /* frame size dependent frame information (set during initialization) */
00177     uint32_t         decode_flags;                  
00178     uint8_t          len_prefix;                    
00179     uint8_t          dynamic_range_compression;     
00180     uint8_t          bits_per_sample;               
00181     uint16_t         samples_per_frame;             
00182     uint16_t         log2_frame_size;
00183     int8_t           num_channels;                  
00184     int8_t           lfe_channel;                   
00185     uint8_t          max_num_subframes;
00186     uint8_t          subframe_len_bits;             
00187     uint8_t          max_subframe_len_bit;          
00188     uint16_t         min_samples_per_subframe;
00189     int8_t           num_sfb[WMAPRO_BLOCK_SIZES];   
00190     int16_t          sfb_offsets[WMAPRO_BLOCK_SIZES][MAX_BANDS];                    
00191     int8_t           sf_offsets[WMAPRO_BLOCK_SIZES][WMAPRO_BLOCK_SIZES][MAX_BANDS]; 
00192     int16_t          subwoofer_cutoffs[WMAPRO_BLOCK_SIZES]; 
00193 
00194     /* packet decode state */
00195     GetBitContext    pgb;                           
00196     int              next_packet_start;             
00197     uint8_t          packet_offset;                 
00198     uint8_t          packet_sequence_number;        
00199     int              num_saved_bits;                
00200     int              frame_offset;                  
00201     int              subframe_offset;               
00202     uint8_t          packet_loss;                   
00203     uint8_t          packet_done;                   
00204 
00205     /* frame decode state */
00206     uint32_t         frame_num;                     
00207     GetBitContext    gb;                            
00208     int              buf_bit_size;                  
00209     float*           samples;                       
00210     float*           samples_end;                   
00211     uint8_t          drc_gain;                      
00212     int8_t           skip_frame;                    
00213     int8_t           parsed_all_subframes;          
00214 
00215     /* subframe/block decode state */
00216     int16_t          subframe_len;                  
00217     int8_t           channels_for_cur_subframe;     
00218     int8_t           channel_indexes_for_cur_subframe[WMAPRO_MAX_CHANNELS];
00219     int8_t           num_bands;                     
00220     int8_t           transmit_num_vec_coeffs;       
00221     int16_t*         cur_sfb_offsets;               
00222     uint8_t          table_idx;                     
00223     int8_t           esc_len;                       
00224 
00225     uint8_t          num_chgroups;                  
00226     WMAProChannelGrp chgroup[WMAPRO_MAX_CHANNELS];  
00227 
00228     WMAProChannelCtx channel[WMAPRO_MAX_CHANNELS];  
00229 } WMAProDecodeCtx;
00230 
00231 
00236 static void av_cold dump_context(WMAProDecodeCtx *s)
00237 {
00238 #define PRINT(a, b)     av_log(s->avctx, AV_LOG_DEBUG, " %s = %d\n", a, b);
00239 #define PRINT_HEX(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %x\n", a, b);
00240 
00241     PRINT("ed sample bit depth", s->bits_per_sample);
00242     PRINT_HEX("ed decode flags", s->decode_flags);
00243     PRINT("samples per frame",   s->samples_per_frame);
00244     PRINT("log2 frame size",     s->log2_frame_size);
00245     PRINT("max num subframes",   s->max_num_subframes);
00246     PRINT("len prefix",          s->len_prefix);
00247     PRINT("num channels",        s->num_channels);
00248 }
00249 
00255 static av_cold int decode_end(AVCodecContext *avctx)
00256 {
00257     WMAProDecodeCtx *s = avctx->priv_data;
00258     int i;
00259 
00260     for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
00261         ff_mdct_end(&s->mdct_ctx[i]);
00262 
00263     return 0;
00264 }
00265 
00271 static av_cold int decode_init(AVCodecContext *avctx)
00272 {
00273     WMAProDecodeCtx *s = avctx->priv_data;
00274     uint8_t *edata_ptr = avctx->extradata;
00275     unsigned int channel_mask;
00276     int i;
00277     int log2_max_num_subframes;
00278     int num_possible_block_sizes;
00279 
00280     s->avctx = avctx;
00281     dsputil_init(&s->dsp, avctx);
00282     init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
00283 
00284     avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00285 
00286     if (avctx->extradata_size >= 18) {
00287         s->decode_flags    = AV_RL16(edata_ptr+14);
00288         channel_mask       = AV_RL32(edata_ptr+2);
00289         s->bits_per_sample = AV_RL16(edata_ptr);
00291         for (i = 0; i < avctx->extradata_size; i++)
00292             av_dlog(avctx, "[%x] ", avctx->extradata[i]);
00293         av_dlog(avctx, "\n");
00294 
00295     } else {
00296         av_log_ask_for_sample(avctx, "Unknown extradata size\n");
00297         return AVERROR_INVALIDDATA;
00298     }
00299 
00301     s->log2_frame_size = av_log2(avctx->block_align) + 4;
00302 
00304     s->skip_frame  = 1; /* skip first frame */
00305     s->packet_loss = 1;
00306     s->len_prefix  = (s->decode_flags & 0x40);
00307 
00309     s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(avctx->sample_rate,
00310                                                           3, s->decode_flags);
00311 
00313     log2_max_num_subframes       = ((s->decode_flags & 0x38) >> 3);
00314     s->max_num_subframes         = 1 << log2_max_num_subframes;
00315     if (s->max_num_subframes == 16 || s->max_num_subframes == 4)
00316         s->max_subframe_len_bit = 1;
00317     s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
00318 
00319     num_possible_block_sizes     = log2_max_num_subframes + 1;
00320     s->min_samples_per_subframe  = s->samples_per_frame / s->max_num_subframes;
00321     s->dynamic_range_compression = (s->decode_flags & 0x80);
00322 
00323     if (s->max_num_subframes > MAX_SUBFRAMES) {
00324         av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %i\n",
00325                s->max_num_subframes);
00326         return AVERROR_INVALIDDATA;
00327     }
00328 
00329     s->num_channels = avctx->channels;
00330 
00331     if (s->num_channels < 0) {
00332         av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n", s->num_channels);
00333         return AVERROR_INVALIDDATA;
00334     } else if (s->num_channels > WMAPRO_MAX_CHANNELS) {
00335         av_log_ask_for_sample(avctx, "unsupported number of channels\n");
00336         return AVERROR_PATCHWELCOME;
00337     }
00338 
00340     for (i = 0; i < s->num_channels; i++)
00341         s->channel[i].prev_block_len = s->samples_per_frame;
00342 
00344     s->lfe_channel = -1;
00345 
00346     if (channel_mask & 8) {
00347         unsigned int mask;
00348         for (mask = 1; mask < 16; mask <<= 1) {
00349             if (channel_mask & mask)
00350                 ++s->lfe_channel;
00351         }
00352     }
00353 
00354     INIT_VLC_STATIC(&sf_vlc, SCALEVLCBITS, HUFF_SCALE_SIZE,
00355                     scale_huffbits, 1, 1,
00356                     scale_huffcodes, 2, 2, 616);
00357 
00358     INIT_VLC_STATIC(&sf_rl_vlc, VLCBITS, HUFF_SCALE_RL_SIZE,
00359                     scale_rl_huffbits, 1, 1,
00360                     scale_rl_huffcodes, 4, 4, 1406);
00361 
00362     INIT_VLC_STATIC(&coef_vlc[0], VLCBITS, HUFF_COEF0_SIZE,
00363                     coef0_huffbits, 1, 1,
00364                     coef0_huffcodes, 4, 4, 2108);
00365 
00366     INIT_VLC_STATIC(&coef_vlc[1], VLCBITS, HUFF_COEF1_SIZE,
00367                     coef1_huffbits, 1, 1,
00368                     coef1_huffcodes, 4, 4, 3912);
00369 
00370     INIT_VLC_STATIC(&vec4_vlc, VLCBITS, HUFF_VEC4_SIZE,
00371                     vec4_huffbits, 1, 1,
00372                     vec4_huffcodes, 2, 2, 604);
00373 
00374     INIT_VLC_STATIC(&vec2_vlc, VLCBITS, HUFF_VEC2_SIZE,
00375                     vec2_huffbits, 1, 1,
00376                     vec2_huffcodes, 2, 2, 562);
00377 
00378     INIT_VLC_STATIC(&vec1_vlc, VLCBITS, HUFF_VEC1_SIZE,
00379                     vec1_huffbits, 1, 1,
00380                     vec1_huffcodes, 2, 2, 562);
00381 
00384     for (i = 0; i < num_possible_block_sizes; i++) {
00385         int subframe_len = s->samples_per_frame >> i;
00386         int x;
00387         int band = 1;
00388 
00389         s->sfb_offsets[i][0] = 0;
00390 
00391         for (x = 0; x < MAX_BANDS-1 && s->sfb_offsets[i][band - 1] < subframe_len; x++) {
00392             int offset = (subframe_len * 2 * critical_freq[x])
00393                           / s->avctx->sample_rate + 2;
00394             offset &= ~3;
00395             if (offset > s->sfb_offsets[i][band - 1])
00396                 s->sfb_offsets[i][band++] = offset;
00397         }
00398         s->sfb_offsets[i][band - 1] = subframe_len;
00399         s->num_sfb[i]               = band - 1;
00400     }
00401 
00402 
00408     for (i = 0; i < num_possible_block_sizes; i++) {
00409         int b;
00410         for (b = 0; b < s->num_sfb[i]; b++) {
00411             int x;
00412             int offset = ((s->sfb_offsets[i][b]
00413                            + s->sfb_offsets[i][b + 1] - 1) << i) >> 1;
00414             for (x = 0; x < num_possible_block_sizes; x++) {
00415                 int v = 0;
00416                 while (s->sfb_offsets[x][v + 1] << x < offset)
00417                     ++v;
00418                 s->sf_offsets[i][x][b] = v;
00419             }
00420         }
00421     }
00422 
00424     for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
00425         ff_mdct_init(&s->mdct_ctx[i], WMAPRO_BLOCK_MIN_BITS+1+i, 1,
00426                      1.0 / (1 << (WMAPRO_BLOCK_MIN_BITS + i - 1))
00427                      / (1 << (s->bits_per_sample - 1)));
00428 
00430     for (i = 0; i < WMAPRO_BLOCK_SIZES; i++) {
00431         const int win_idx = WMAPRO_BLOCK_MAX_BITS - i;
00432         ff_init_ff_sine_windows(win_idx);
00433         s->windows[WMAPRO_BLOCK_SIZES - i - 1] = ff_sine_windows[win_idx];
00434     }
00435 
00437     for (i = 0; i < num_possible_block_sizes; i++) {
00438         int block_size = s->samples_per_frame >> i;
00439         int cutoff = (440*block_size + 3 * (s->avctx->sample_rate >> 1) - 1)
00440                      / s->avctx->sample_rate;
00441         s->subwoofer_cutoffs[i] = av_clip(cutoff, 4, block_size);
00442     }
00443 
00445     for (i = 0; i < 33; i++)
00446         sin64[i] = sin(i*M_PI / 64.0);
00447 
00448     if (avctx->debug & FF_DEBUG_BITSTREAM)
00449         dump_context(s);
00450 
00451     avctx->channel_layout = channel_mask;
00452     return 0;
00453 }
00454 
00461 static int decode_subframe_length(WMAProDecodeCtx *s, int offset)
00462 {
00463     int frame_len_shift = 0;
00464     int subframe_len;
00465 
00467     if (offset == s->samples_per_frame - s->min_samples_per_subframe)
00468         return s->min_samples_per_subframe;
00469 
00471     if (s->max_subframe_len_bit) {
00472         if (get_bits1(&s->gb))
00473             frame_len_shift = 1 + get_bits(&s->gb, s->subframe_len_bits-1);
00474     } else
00475         frame_len_shift = get_bits(&s->gb, s->subframe_len_bits);
00476 
00477     subframe_len = s->samples_per_frame >> frame_len_shift;
00478 
00480     if (subframe_len < s->min_samples_per_subframe ||
00481         subframe_len > s->samples_per_frame) {
00482         av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
00483                subframe_len);
00484         return AVERROR_INVALIDDATA;
00485     }
00486     return subframe_len;
00487 }
00488 
00509 static int decode_tilehdr(WMAProDecodeCtx *s)
00510 {
00511     uint16_t num_samples[WMAPRO_MAX_CHANNELS];        
00512     uint8_t  contains_subframe[WMAPRO_MAX_CHANNELS];  
00513     int channels_for_cur_subframe = s->num_channels;  
00514     int fixed_channel_layout = 0;                     
00515     int min_channel_len = 0;                          
00516     int c;
00517 
00518     /* Should never consume more than 3073 bits (256 iterations for the
00519      * while loop when always the minimum amount of 128 samples is substracted
00520      * from missing samples in the 8 channel case).
00521      * 1 + BLOCK_MAX_SIZE * MAX_CHANNELS / BLOCK_MIN_SIZE * (MAX_CHANNELS  + 4)
00522      */
00523 
00525     for (c = 0; c < s->num_channels; c++)
00526         s->channel[c].num_subframes = 0;
00527 
00528     memset(num_samples, 0, sizeof(num_samples));
00529 
00530     if (s->max_num_subframes == 1 || get_bits1(&s->gb))
00531         fixed_channel_layout = 1;
00532 
00534     do {
00535         int subframe_len;
00536 
00538         for (c = 0; c < s->num_channels; c++) {
00539             if (num_samples[c] == min_channel_len) {
00540                 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
00541                    (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe))
00542                     contains_subframe[c] = 1;
00543                 else
00544                     contains_subframe[c] = get_bits1(&s->gb);
00545             } else
00546                 contains_subframe[c] = 0;
00547         }
00548 
00550         if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
00551             return AVERROR_INVALIDDATA;
00552 
00554         min_channel_len += subframe_len;
00555         for (c = 0; c < s->num_channels; c++) {
00556             WMAProChannelCtx* chan = &s->channel[c];
00557 
00558             if (contains_subframe[c]) {
00559                 if (chan->num_subframes >= MAX_SUBFRAMES) {
00560                     av_log(s->avctx, AV_LOG_ERROR,
00561                            "broken frame: num subframes > 31\n");
00562                     return AVERROR_INVALIDDATA;
00563                 }
00564                 chan->subframe_len[chan->num_subframes] = subframe_len;
00565                 num_samples[c] += subframe_len;
00566                 ++chan->num_subframes;
00567                 if (num_samples[c] > s->samples_per_frame) {
00568                     av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
00569                            "channel len > samples_per_frame\n");
00570                     return AVERROR_INVALIDDATA;
00571                 }
00572             } else if (num_samples[c] <= min_channel_len) {
00573                 if (num_samples[c] < min_channel_len) {
00574                     channels_for_cur_subframe = 0;
00575                     min_channel_len = num_samples[c];
00576                 }
00577                 ++channels_for_cur_subframe;
00578             }
00579         }
00580     } while (min_channel_len < s->samples_per_frame);
00581 
00582     for (c = 0; c < s->num_channels; c++) {
00583         int i;
00584         int offset = 0;
00585         for (i = 0; i < s->channel[c].num_subframes; i++) {
00586             av_dlog(s->avctx, "frame[%i] channel[%i] subframe[%i]"
00587                     " len %i\n", s->frame_num, c, i,
00588                     s->channel[c].subframe_len[i]);
00589             s->channel[c].subframe_offset[i] = offset;
00590             offset += s->channel[c].subframe_len[i];
00591         }
00592     }
00593 
00594     return 0;
00595 }
00596 
00602 static void decode_decorrelation_matrix(WMAProDecodeCtx *s,
00603                                         WMAProChannelGrp *chgroup)
00604 {
00605     int i;
00606     int offset = 0;
00607     int8_t rotation_offset[WMAPRO_MAX_CHANNELS * WMAPRO_MAX_CHANNELS];
00608     memset(chgroup->decorrelation_matrix, 0, s->num_channels *
00609            s->num_channels * sizeof(*chgroup->decorrelation_matrix));
00610 
00611     for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++)
00612         rotation_offset[i] = get_bits(&s->gb, 6);
00613 
00614     for (i = 0; i < chgroup->num_channels; i++)
00615         chgroup->decorrelation_matrix[chgroup->num_channels * i + i] =
00616             get_bits1(&s->gb) ? 1.0 : -1.0;
00617 
00618     for (i = 1; i < chgroup->num_channels; i++) {
00619         int x;
00620         for (x = 0; x < i; x++) {
00621             int y;
00622             for (y = 0; y < i + 1; y++) {
00623                 float v1 = chgroup->decorrelation_matrix[x * chgroup->num_channels + y];
00624                 float v2 = chgroup->decorrelation_matrix[i * chgroup->num_channels + y];
00625                 int n = rotation_offset[offset + x];
00626                 float sinv;
00627                 float cosv;
00628 
00629                 if (n < 32) {
00630                     sinv = sin64[n];
00631                     cosv = sin64[32 - n];
00632                 } else {
00633                     sinv =  sin64[64 -  n];
00634                     cosv = -sin64[n  - 32];
00635                 }
00636 
00637                 chgroup->decorrelation_matrix[y + x * chgroup->num_channels] =
00638                                                (v1 * sinv) - (v2 * cosv);
00639                 chgroup->decorrelation_matrix[y + i * chgroup->num_channels] =
00640                                                (v1 * cosv) + (v2 * sinv);
00641             }
00642         }
00643         offset += i;
00644     }
00645 }
00646 
00652 static int decode_channel_transform(WMAProDecodeCtx* s)
00653 {
00654     int i;
00655     /* should never consume more than 1921 bits for the 8 channel case
00656      * 1 + MAX_CHANNELS * (MAX_CHANNELS + 2 + 3 * MAX_CHANNELS * MAX_CHANNELS
00657      * + MAX_CHANNELS + MAX_BANDS + 1)
00658      */
00659 
00661     s->num_chgroups = 0;
00662     if (s->num_channels > 1) {
00663         int remaining_channels = s->channels_for_cur_subframe;
00664 
00665         if (get_bits1(&s->gb)) {
00666             av_log_ask_for_sample(s->avctx,
00667                                   "unsupported channel transform bit\n");
00668             return AVERROR_INVALIDDATA;
00669         }
00670 
00671         for (s->num_chgroups = 0; remaining_channels &&
00672              s->num_chgroups < s->channels_for_cur_subframe; s->num_chgroups++) {
00673             WMAProChannelGrp* chgroup = &s->chgroup[s->num_chgroups];
00674             float** channel_data = chgroup->channel_data;
00675             chgroup->num_channels = 0;
00676             chgroup->transform = 0;
00677 
00679             if (remaining_channels > 2) {
00680                 for (i = 0; i < s->channels_for_cur_subframe; i++) {
00681                     int channel_idx = s->channel_indexes_for_cur_subframe[i];
00682                     if (!s->channel[channel_idx].grouped
00683                         && get_bits1(&s->gb)) {
00684                         ++chgroup->num_channels;
00685                         s->channel[channel_idx].grouped = 1;
00686                         *channel_data++ = s->channel[channel_idx].coeffs;
00687                     }
00688                 }
00689             } else {
00690                 chgroup->num_channels = remaining_channels;
00691                 for (i = 0; i < s->channels_for_cur_subframe; i++) {
00692                     int channel_idx = s->channel_indexes_for_cur_subframe[i];
00693                     if (!s->channel[channel_idx].grouped)
00694                         *channel_data++ = s->channel[channel_idx].coeffs;
00695                     s->channel[channel_idx].grouped = 1;
00696                 }
00697             }
00698 
00700             if (chgroup->num_channels == 2) {
00701                 if (get_bits1(&s->gb)) {
00702                     if (get_bits1(&s->gb)) {
00703                         av_log_ask_for_sample(s->avctx,
00704                                               "unsupported channel transform type\n");
00705                     }
00706                 } else {
00707                     chgroup->transform = 1;
00708                     if (s->num_channels == 2) {
00709                         chgroup->decorrelation_matrix[0] =  1.0;
00710                         chgroup->decorrelation_matrix[1] = -1.0;
00711                         chgroup->decorrelation_matrix[2] =  1.0;
00712                         chgroup->decorrelation_matrix[3] =  1.0;
00713                     } else {
00715                         chgroup->decorrelation_matrix[0] =  0.70703125;
00716                         chgroup->decorrelation_matrix[1] = -0.70703125;
00717                         chgroup->decorrelation_matrix[2] =  0.70703125;
00718                         chgroup->decorrelation_matrix[3] =  0.70703125;
00719                     }
00720                 }
00721             } else if (chgroup->num_channels > 2) {
00722                 if (get_bits1(&s->gb)) {
00723                     chgroup->transform = 1;
00724                     if (get_bits1(&s->gb)) {
00725                         decode_decorrelation_matrix(s, chgroup);
00726                     } else {
00728                         if (chgroup->num_channels > 6) {
00729                             av_log_ask_for_sample(s->avctx,
00730                                                   "coupled channels > 6\n");
00731                         } else {
00732                             memcpy(chgroup->decorrelation_matrix,
00733                                    default_decorrelation[chgroup->num_channels],
00734                                    chgroup->num_channels * chgroup->num_channels *
00735                                    sizeof(*chgroup->decorrelation_matrix));
00736                         }
00737                     }
00738                 }
00739             }
00740 
00742             if (chgroup->transform) {
00743                 if (!get_bits1(&s->gb)) {
00744                     int i;
00746                     for (i = 0; i < s->num_bands; i++) {
00747                         chgroup->transform_band[i] = get_bits1(&s->gb);
00748                     }
00749                 } else {
00750                     memset(chgroup->transform_band, 1, s->num_bands);
00751                 }
00752             }
00753             remaining_channels -= chgroup->num_channels;
00754         }
00755     }
00756     return 0;
00757 }
00758 
00765 static int decode_coeffs(WMAProDecodeCtx *s, int c)
00766 {
00767     /* Integers 0..15 as single-precision floats.  The table saves a
00768        costly int to float conversion, and storing the values as
00769        integers allows fast sign-flipping. */
00770     static const int fval_tab[16] = {
00771         0x00000000, 0x3f800000, 0x40000000, 0x40400000,
00772         0x40800000, 0x40a00000, 0x40c00000, 0x40e00000,
00773         0x41000000, 0x41100000, 0x41200000, 0x41300000,
00774         0x41400000, 0x41500000, 0x41600000, 0x41700000,
00775     };
00776     int vlctable;
00777     VLC* vlc;
00778     WMAProChannelCtx* ci = &s->channel[c];
00779     int rl_mode = 0;
00780     int cur_coeff = 0;
00781     int num_zeros = 0;
00782     const uint16_t* run;
00783     const float* level;
00784 
00785     av_dlog(s->avctx, "decode coefficients for channel %i\n", c);
00786 
00787     vlctable = get_bits1(&s->gb);
00788     vlc = &coef_vlc[vlctable];
00789 
00790     if (vlctable) {
00791         run = coef1_run;
00792         level = coef1_level;
00793     } else {
00794         run = coef0_run;
00795         level = coef0_level;
00796     }
00797 
00800     while ((s->transmit_num_vec_coeffs || !rl_mode) &&
00801            (cur_coeff + 3 < ci->num_vec_coeffs)) {
00802         int vals[4];
00803         int i;
00804         unsigned int idx;
00805 
00806         idx = get_vlc2(&s->gb, vec4_vlc.table, VLCBITS, VEC4MAXDEPTH);
00807 
00808         if (idx == HUFF_VEC4_SIZE - 1) {
00809             for (i = 0; i < 4; i += 2) {
00810                 idx = get_vlc2(&s->gb, vec2_vlc.table, VLCBITS, VEC2MAXDEPTH);
00811                 if (idx == HUFF_VEC2_SIZE - 1) {
00812                     int v0, v1;
00813                     v0 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
00814                     if (v0 == HUFF_VEC1_SIZE - 1)
00815                         v0 += ff_wma_get_large_val(&s->gb);
00816                     v1 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
00817                     if (v1 == HUFF_VEC1_SIZE - 1)
00818                         v1 += ff_wma_get_large_val(&s->gb);
00819                     ((float*)vals)[i  ] = v0;
00820                     ((float*)vals)[i+1] = v1;
00821                 } else {
00822                     vals[i]   = fval_tab[symbol_to_vec2[idx] >> 4 ];
00823                     vals[i+1] = fval_tab[symbol_to_vec2[idx] & 0xF];
00824                 }
00825             }
00826         } else {
00827             vals[0] = fval_tab[ symbol_to_vec4[idx] >> 12      ];
00828             vals[1] = fval_tab[(symbol_to_vec4[idx] >> 8) & 0xF];
00829             vals[2] = fval_tab[(symbol_to_vec4[idx] >> 4) & 0xF];
00830             vals[3] = fval_tab[ symbol_to_vec4[idx]       & 0xF];
00831         }
00832 
00834         for (i = 0; i < 4; i++) {
00835             if (vals[i]) {
00836                 int sign = get_bits1(&s->gb) - 1;
00837                 *(uint32_t*)&ci->coeffs[cur_coeff] = vals[i] ^ sign<<31;
00838                 num_zeros = 0;
00839             } else {
00840                 ci->coeffs[cur_coeff] = 0;
00843                 rl_mode |= (++num_zeros > s->subframe_len >> 8);
00844             }
00845             ++cur_coeff;
00846         }
00847     }
00848 
00850     if (cur_coeff < s->subframe_len) {
00851         memset(&ci->coeffs[cur_coeff], 0,
00852                sizeof(*ci->coeffs) * (s->subframe_len - cur_coeff));
00853         if (ff_wma_run_level_decode(s->avctx, &s->gb, vlc,
00854                                     level, run, 1, ci->coeffs,
00855                                     cur_coeff, s->subframe_len,
00856                                     s->subframe_len, s->esc_len, 0))
00857             return AVERROR_INVALIDDATA;
00858     }
00859 
00860     return 0;
00861 }
00862 
00868 static int decode_scale_factors(WMAProDecodeCtx* s)
00869 {
00870     int i;
00871 
00876     for (i = 0; i < s->channels_for_cur_subframe; i++) {
00877         int c = s->channel_indexes_for_cur_subframe[i];
00878         int* sf;
00879         int* sf_end;
00880         s->channel[c].scale_factors = s->channel[c].saved_scale_factors[!s->channel[c].scale_factor_idx];
00881         sf_end = s->channel[c].scale_factors + s->num_bands;
00882 
00888         if (s->channel[c].reuse_sf) {
00889             const int8_t* sf_offsets = s->sf_offsets[s->table_idx][s->channel[c].table_idx];
00890             int b;
00891             for (b = 0; b < s->num_bands; b++)
00892                 s->channel[c].scale_factors[b] =
00893                     s->channel[c].saved_scale_factors[s->channel[c].scale_factor_idx][*sf_offsets++];
00894         }
00895 
00896         if (!s->channel[c].cur_subframe || get_bits1(&s->gb)) {
00897 
00898             if (!s->channel[c].reuse_sf) {
00899                 int val;
00901                 s->channel[c].scale_factor_step = get_bits(&s->gb, 2) + 1;
00902                 val = 45 / s->channel[c].scale_factor_step;
00903                 for (sf = s->channel[c].scale_factors; sf < sf_end; sf++) {
00904                     val += get_vlc2(&s->gb, sf_vlc.table, SCALEVLCBITS, SCALEMAXDEPTH) - 60;
00905                     *sf = val;
00906                 }
00907             } else {
00908                 int i;
00910                 for (i = 0; i < s->num_bands; i++) {
00911                     int idx;
00912                     int skip;
00913                     int val;
00914                     int sign;
00915 
00916                     idx = get_vlc2(&s->gb, sf_rl_vlc.table, VLCBITS, SCALERLMAXDEPTH);
00917 
00918                     if (!idx) {
00919                         uint32_t code = get_bits(&s->gb, 14);
00920                         val  =  code >> 6;
00921                         sign = (code & 1) - 1;
00922                         skip = (code & 0x3f) >> 1;
00923                     } else if (idx == 1) {
00924                         break;
00925                     } else {
00926                         skip = scale_rl_run[idx];
00927                         val  = scale_rl_level[idx];
00928                         sign = get_bits1(&s->gb)-1;
00929                     }
00930 
00931                     i += skip;
00932                     if (i >= s->num_bands) {
00933                         av_log(s->avctx, AV_LOG_ERROR,
00934                                "invalid scale factor coding\n");
00935                         return AVERROR_INVALIDDATA;
00936                     }
00937                     s->channel[c].scale_factors[i] += (val ^ sign) - sign;
00938                 }
00939             }
00941             s->channel[c].scale_factor_idx = !s->channel[c].scale_factor_idx;
00942             s->channel[c].table_idx = s->table_idx;
00943             s->channel[c].reuse_sf  = 1;
00944         }
00945 
00947         s->channel[c].max_scale_factor = s->channel[c].scale_factors[0];
00948         for (sf = s->channel[c].scale_factors + 1; sf < sf_end; sf++) {
00949             s->channel[c].max_scale_factor =
00950                 FFMAX(s->channel[c].max_scale_factor, *sf);
00951         }
00952 
00953     }
00954     return 0;
00955 }
00956 
00961 static void inverse_channel_transform(WMAProDecodeCtx *s)
00962 {
00963     int i;
00964 
00965     for (i = 0; i < s->num_chgroups; i++) {
00966         if (s->chgroup[i].transform) {
00967             float data[WMAPRO_MAX_CHANNELS];
00968             const int num_channels = s->chgroup[i].num_channels;
00969             float** ch_data = s->chgroup[i].channel_data;
00970             float** ch_end = ch_data + num_channels;
00971             const int8_t* tb = s->chgroup[i].transform_band;
00972             int16_t* sfb;
00973 
00975             for (sfb = s->cur_sfb_offsets;
00976                  sfb < s->cur_sfb_offsets + s->num_bands; sfb++) {
00977                 int y;
00978                 if (*tb++ == 1) {
00980                     for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) {
00981                         const float* mat = s->chgroup[i].decorrelation_matrix;
00982                         const float* data_end = data + num_channels;
00983                         float* data_ptr = data;
00984                         float** ch;
00985 
00986                         for (ch = ch_data; ch < ch_end; ch++)
00987                             *data_ptr++ = (*ch)[y];
00988 
00989                         for (ch = ch_data; ch < ch_end; ch++) {
00990                             float sum = 0;
00991                             data_ptr = data;
00992                             while (data_ptr < data_end)
00993                                 sum += *data_ptr++ * *mat++;
00994 
00995                             (*ch)[y] = sum;
00996                         }
00997                     }
00998                 } else if (s->num_channels == 2) {
00999                     int len = FFMIN(sfb[1], s->subframe_len) - sfb[0];
01000                     s->dsp.vector_fmul_scalar(ch_data[0] + sfb[0],
01001                                               ch_data[0] + sfb[0],
01002                                               181.0 / 128, len);
01003                     s->dsp.vector_fmul_scalar(ch_data[1] + sfb[0],
01004                                               ch_data[1] + sfb[0],
01005                                               181.0 / 128, len);
01006                 }
01007             }
01008         }
01009     }
01010 }
01011 
01016 static void wmapro_window(WMAProDecodeCtx *s)
01017 {
01018     int i;
01019     for (i = 0; i < s->channels_for_cur_subframe; i++) {
01020         int c = s->channel_indexes_for_cur_subframe[i];
01021         float* window;
01022         int winlen = s->channel[c].prev_block_len;
01023         float* start = s->channel[c].coeffs - (winlen >> 1);
01024 
01025         if (s->subframe_len < winlen) {
01026             start += (winlen - s->subframe_len) >> 1;
01027             winlen = s->subframe_len;
01028         }
01029 
01030         window = s->windows[av_log2(winlen) - WMAPRO_BLOCK_MIN_BITS];
01031 
01032         winlen >>= 1;
01033 
01034         s->dsp.vector_fmul_window(start, start, start + winlen,
01035                                   window, winlen);
01036 
01037         s->channel[c].prev_block_len = s->subframe_len;
01038     }
01039 }
01040 
01046 static int decode_subframe(WMAProDecodeCtx *s)
01047 {
01048     int offset = s->samples_per_frame;
01049     int subframe_len = s->samples_per_frame;
01050     int i;
01051     int total_samples   = s->samples_per_frame * s->num_channels;
01052     int transmit_coeffs = 0;
01053     int cur_subwoofer_cutoff;
01054 
01055     s->subframe_offset = get_bits_count(&s->gb);
01056 
01061     for (i = 0; i < s->num_channels; i++) {
01062         s->channel[i].grouped = 0;
01063         if (offset > s->channel[i].decoded_samples) {
01064             offset = s->channel[i].decoded_samples;
01065             subframe_len =
01066                 s->channel[i].subframe_len[s->channel[i].cur_subframe];
01067         }
01068     }
01069 
01070     av_dlog(s->avctx,
01071             "processing subframe with offset %i len %i\n", offset, subframe_len);
01072 
01074     s->channels_for_cur_subframe = 0;
01075     for (i = 0; i < s->num_channels; i++) {
01076         const int cur_subframe = s->channel[i].cur_subframe;
01078         total_samples -= s->channel[i].decoded_samples;
01079 
01081         if (offset == s->channel[i].decoded_samples &&
01082             subframe_len == s->channel[i].subframe_len[cur_subframe]) {
01083             total_samples -= s->channel[i].subframe_len[cur_subframe];
01084             s->channel[i].decoded_samples +=
01085                 s->channel[i].subframe_len[cur_subframe];
01086             s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
01087             ++s->channels_for_cur_subframe;
01088         }
01089     }
01090 
01093     if (!total_samples)
01094         s->parsed_all_subframes = 1;
01095 
01096 
01097     av_dlog(s->avctx, "subframe is part of %i channels\n",
01098             s->channels_for_cur_subframe);
01099 
01101     s->table_idx         = av_log2(s->samples_per_frame/subframe_len);
01102     s->num_bands         = s->num_sfb[s->table_idx];
01103     s->cur_sfb_offsets   = s->sfb_offsets[s->table_idx];
01104     cur_subwoofer_cutoff = s->subwoofer_cutoffs[s->table_idx];
01105 
01107     for (i = 0; i < s->channels_for_cur_subframe; i++) {
01108         int c = s->channel_indexes_for_cur_subframe[i];
01109 
01110         s->channel[c].coeffs = &s->channel[c].out[(s->samples_per_frame >> 1)
01111                                                   + offset];
01112     }
01113 
01114     s->subframe_len = subframe_len;
01115     s->esc_len = av_log2(s->subframe_len - 1) + 1;
01116 
01118     if (get_bits1(&s->gb)) {
01119         int num_fill_bits;
01120         if (!(num_fill_bits = get_bits(&s->gb, 2))) {
01121             int len = get_bits(&s->gb, 4);
01122             num_fill_bits = get_bits(&s->gb, len) + 1;
01123         }
01124 
01125         if (num_fill_bits >= 0) {
01126             if (get_bits_count(&s->gb) + num_fill_bits > s->num_saved_bits) {
01127                 av_log(s->avctx, AV_LOG_ERROR, "invalid number of fill bits\n");
01128                 return AVERROR_INVALIDDATA;
01129             }
01130 
01131             skip_bits_long(&s->gb, num_fill_bits);
01132         }
01133     }
01134 
01136     if (get_bits1(&s->gb)) {
01137         av_log_ask_for_sample(s->avctx, "reserved bit set\n");
01138         return AVERROR_INVALIDDATA;
01139     }
01140 
01141 
01142     if (decode_channel_transform(s) < 0)
01143         return AVERROR_INVALIDDATA;
01144 
01145 
01146     for (i = 0; i < s->channels_for_cur_subframe; i++) {
01147         int c = s->channel_indexes_for_cur_subframe[i];
01148         if ((s->channel[c].transmit_coefs = get_bits1(&s->gb)))
01149             transmit_coeffs = 1;
01150     }
01151 
01152     if (transmit_coeffs) {
01153         int step;
01154         int quant_step = 90 * s->bits_per_sample >> 4;
01155 
01157         if ((s->transmit_num_vec_coeffs = get_bits1(&s->gb))) {
01158             int num_bits = av_log2((s->subframe_len + 3)/4) + 1;
01159             for (i = 0; i < s->channels_for_cur_subframe; i++) {
01160                 int c = s->channel_indexes_for_cur_subframe[i];
01161                 int num_vec_coeffs = get_bits(&s->gb, num_bits) << 2;
01162                 if (num_vec_coeffs > WMAPRO_BLOCK_MAX_SIZE) {
01163                     av_log(s->avctx, AV_LOG_ERROR, "num_vec_coeffs %d is too large\n", num_vec_coeffs);
01164                     return AVERROR_INVALIDDATA;
01165                 }
01166                 s->channel[c].num_vec_coeffs = num_vec_coeffs;
01167             }
01168         } else {
01169             for (i = 0; i < s->channels_for_cur_subframe; i++) {
01170                 int c = s->channel_indexes_for_cur_subframe[i];
01171                 s->channel[c].num_vec_coeffs = s->subframe_len;
01172             }
01173         }
01175         step = get_sbits(&s->gb, 6);
01176         quant_step += step;
01177         if (step == -32 || step == 31) {
01178             const int sign = (step == 31) - 1;
01179             int quant = 0;
01180             while (get_bits_count(&s->gb) + 5 < s->num_saved_bits &&
01181                    (step = get_bits(&s->gb, 5)) == 31) {
01182                 quant += 31;
01183             }
01184             quant_step += ((quant + step) ^ sign) - sign;
01185         }
01186         if (quant_step < 0) {
01187             av_log(s->avctx, AV_LOG_DEBUG, "negative quant step\n");
01188         }
01189 
01192         if (s->channels_for_cur_subframe == 1) {
01193             s->channel[s->channel_indexes_for_cur_subframe[0]].quant_step = quant_step;
01194         } else {
01195             int modifier_len = get_bits(&s->gb, 3);
01196             for (i = 0; i < s->channels_for_cur_subframe; i++) {
01197                 int c = s->channel_indexes_for_cur_subframe[i];
01198                 s->channel[c].quant_step = quant_step;
01199                 if (get_bits1(&s->gb)) {
01200                     if (modifier_len) {
01201                         s->channel[c].quant_step += get_bits(&s->gb, modifier_len) + 1;
01202                     } else
01203                         ++s->channel[c].quant_step;
01204                 }
01205             }
01206         }
01207 
01209         if (decode_scale_factors(s) < 0)
01210             return AVERROR_INVALIDDATA;
01211     }
01212 
01213     av_dlog(s->avctx, "BITSTREAM: subframe header length was %i\n",
01214             get_bits_count(&s->gb) - s->subframe_offset);
01215 
01217     for (i = 0; i < s->channels_for_cur_subframe; i++) {
01218         int c = s->channel_indexes_for_cur_subframe[i];
01219         if (s->channel[c].transmit_coefs &&
01220             get_bits_count(&s->gb) < s->num_saved_bits) {
01221             decode_coeffs(s, c);
01222         } else
01223             memset(s->channel[c].coeffs, 0,
01224                    sizeof(*s->channel[c].coeffs) * subframe_len);
01225     }
01226 
01227     av_dlog(s->avctx, "BITSTREAM: subframe length was %i\n",
01228             get_bits_count(&s->gb) - s->subframe_offset);
01229 
01230     if (transmit_coeffs) {
01231         FFTContext *mdct = &s->mdct_ctx[av_log2(subframe_len) - WMAPRO_BLOCK_MIN_BITS];
01233         inverse_channel_transform(s);
01234         for (i = 0; i < s->channels_for_cur_subframe; i++) {
01235             int c = s->channel_indexes_for_cur_subframe[i];
01236             const int* sf = s->channel[c].scale_factors;
01237             int b;
01238 
01239             if (c == s->lfe_channel)
01240                 memset(&s->tmp[cur_subwoofer_cutoff], 0, sizeof(*s->tmp) *
01241                        (subframe_len - cur_subwoofer_cutoff));
01242 
01244             for (b = 0; b < s->num_bands; b++) {
01245                 const int end = FFMIN(s->cur_sfb_offsets[b+1], s->subframe_len);
01246                 const int exp = s->channel[c].quant_step -
01247                             (s->channel[c].max_scale_factor - *sf++) *
01248                             s->channel[c].scale_factor_step;
01249                 const float quant = pow(10.0, exp / 20.0);
01250                 int start = s->cur_sfb_offsets[b];
01251                 s->dsp.vector_fmul_scalar(s->tmp + start,
01252                                           s->channel[c].coeffs + start,
01253                                           quant, end - start);
01254             }
01255 
01257             mdct->imdct_half(mdct, s->channel[c].coeffs, s->tmp);
01258         }
01259     }
01260 
01262     wmapro_window(s);
01263 
01265     for (i = 0; i < s->channels_for_cur_subframe; i++) {
01266         int c = s->channel_indexes_for_cur_subframe[i];
01267         if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
01268             av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
01269             return AVERROR_INVALIDDATA;
01270         }
01271         ++s->channel[c].cur_subframe;
01272     }
01273 
01274     return 0;
01275 }
01276 
01283 static int decode_frame(WMAProDecodeCtx *s)
01284 {
01285     GetBitContext* gb = &s->gb;
01286     int more_frames = 0;
01287     int len = 0;
01288     int i;
01289 
01291     if (s->num_channels * s->samples_per_frame > s->samples_end - s->samples) {
01293         av_log(s->avctx, AV_LOG_ERROR,
01294                "not enough space for the output samples\n");
01295         s->packet_loss = 1;
01296         return 0;
01297     }
01298 
01300     if (s->len_prefix)
01301         len = get_bits(gb, s->log2_frame_size);
01302 
01303     av_dlog(s->avctx, "decoding frame with length %x\n", len);
01304 
01306     if (decode_tilehdr(s)) {
01307         s->packet_loss = 1;
01308         return 0;
01309     }
01310 
01312     if (s->num_channels > 1 && get_bits1(gb)) {
01313         if (get_bits1(gb)) {
01314             for (i = 0; i < s->num_channels * s->num_channels; i++)
01315                 skip_bits(gb, 4);
01316         }
01317     }
01318 
01320     if (s->dynamic_range_compression) {
01321         s->drc_gain = get_bits(gb, 8);
01322         av_dlog(s->avctx, "drc_gain %i\n", s->drc_gain);
01323     }
01324 
01327     if (get_bits1(gb)) {
01328         int av_unused skip;
01329 
01331         if (get_bits1(gb)) {
01332             skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
01333             av_dlog(s->avctx, "start skip: %i\n", skip);
01334         }
01335 
01337         if (get_bits1(gb)) {
01338             skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
01339             av_dlog(s->avctx, "end skip: %i\n", skip);
01340         }
01341 
01342     }
01343 
01344     av_dlog(s->avctx, "BITSTREAM: frame header length was %i\n",
01345             get_bits_count(gb) - s->frame_offset);
01346 
01348     s->parsed_all_subframes = 0;
01349     for (i = 0; i < s->num_channels; i++) {
01350         s->channel[i].decoded_samples = 0;
01351         s->channel[i].cur_subframe    = 0;
01352         s->channel[i].reuse_sf        = 0;
01353     }
01354 
01356     while (!s->parsed_all_subframes) {
01357         if (decode_subframe(s) < 0) {
01358             s->packet_loss = 1;
01359             return 0;
01360         }
01361     }
01362 
01364     for (i = 0; i < s->num_channels; i++) {
01365         float* ptr  = s->samples + i;
01366         int incr = s->num_channels;
01367         float* iptr = s->channel[i].out;
01368         float* iend = iptr + s->samples_per_frame;
01369 
01370         // FIXME should create/use a DSP function here
01371         while (iptr < iend) {
01372             *ptr = *iptr++;
01373             ptr += incr;
01374         }
01375 
01377         memcpy(&s->channel[i].out[0],
01378                &s->channel[i].out[s->samples_per_frame],
01379                s->samples_per_frame * sizeof(*s->channel[i].out) >> 1);
01380     }
01381 
01382     if (s->skip_frame) {
01383         s->skip_frame = 0;
01384     } else
01385         s->samples += s->num_channels * s->samples_per_frame;
01386 
01387     if (s->len_prefix) {
01388         if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
01390             av_log(s->avctx, AV_LOG_ERROR,
01391                    "frame[%i] would have to skip %i bits\n", s->frame_num,
01392                    len - (get_bits_count(gb) - s->frame_offset) - 1);
01393             s->packet_loss = 1;
01394             return 0;
01395         }
01396 
01398         skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
01399     } else {
01400         while (get_bits_count(gb) < s->num_saved_bits && get_bits1(gb) == 0) {
01401         }
01402     }
01403 
01405     more_frames = get_bits1(gb);
01406 
01407     ++s->frame_num;
01408     return more_frames;
01409 }
01410 
01417 static int remaining_bits(WMAProDecodeCtx *s, GetBitContext *gb)
01418 {
01419     return s->buf_bit_size - get_bits_count(gb);
01420 }
01421 
01429 static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len,
01430                       int append)
01431 {
01432     int buflen;
01433 
01438     if (!append) {
01439         s->frame_offset = get_bits_count(gb) & 7;
01440         s->num_saved_bits = s->frame_offset;
01441         init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
01442     }
01443 
01444     buflen = (s->num_saved_bits + len + 8) >> 3;
01445 
01446     if (len <= 0 || buflen > MAX_FRAMESIZE) {
01447         av_log_ask_for_sample(s->avctx, "input buffer too small\n");
01448         s->packet_loss = 1;
01449         return;
01450     }
01451 
01452     s->num_saved_bits += len;
01453     if (!append) {
01454         ff_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
01455                      s->num_saved_bits);
01456     } else {
01457         int align = 8 - (get_bits_count(gb) & 7);
01458         align = FFMIN(align, len);
01459         put_bits(&s->pb, align, get_bits(gb, align));
01460         len -= align;
01461         ff_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
01462     }
01463     skip_bits_long(gb, len);
01464 
01465     {
01466         PutBitContext tmp = s->pb;
01467         flush_put_bits(&tmp);
01468     }
01469 
01470     init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
01471     skip_bits(&s->gb, s->frame_offset);
01472 }
01473 
01482 static int decode_packet(AVCodecContext *avctx,
01483                          void *data, int *data_size, AVPacket* avpkt)
01484 {
01485     WMAProDecodeCtx *s = avctx->priv_data;
01486     GetBitContext* gb  = &s->pgb;
01487     const uint8_t* buf = avpkt->data;
01488     int buf_size       = avpkt->size;
01489     int num_bits_prev_frame;
01490     int packet_sequence_number;
01491 
01492     s->samples       = data;
01493     s->samples_end   = (float*)((int8_t*)data + *data_size);
01494     *data_size = 0;
01495 
01496     if (s->packet_done || s->packet_loss) {
01497         s->packet_done = 0;
01498 
01500         if (buf_size < avctx->block_align)
01501             return 0;
01502 
01503         s->next_packet_start = buf_size - avctx->block_align;
01504         buf_size = avctx->block_align;
01505         s->buf_bit_size = buf_size << 3;
01506 
01508         init_get_bits(gb, buf, s->buf_bit_size);
01509         packet_sequence_number = get_bits(gb, 4);
01510         skip_bits(gb, 2);
01511 
01513         num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
01514         av_dlog(avctx, "packet[%d]: nbpf %x\n", avctx->frame_number,
01515                 num_bits_prev_frame);
01516 
01518         if (!s->packet_loss &&
01519             ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
01520             s->packet_loss = 1;
01521             av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n",
01522                    s->packet_sequence_number, packet_sequence_number);
01523         }
01524         s->packet_sequence_number = packet_sequence_number;
01525 
01526         if (num_bits_prev_frame > 0) {
01527             int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
01528             if (num_bits_prev_frame >= remaining_packet_bits) {
01529                 num_bits_prev_frame = remaining_packet_bits;
01530                 s->packet_done = 1;
01531             }
01532 
01535             save_bits(s, gb, num_bits_prev_frame, 1);
01536             av_dlog(avctx, "accumulated %x bits of frame data\n",
01537                     s->num_saved_bits - s->frame_offset);
01538 
01540             if (!s->packet_loss)
01541                 decode_frame(s);
01542         } else if (s->num_saved_bits - s->frame_offset) {
01543             av_dlog(avctx, "ignoring %x previously saved bits\n",
01544                     s->num_saved_bits - s->frame_offset);
01545         }
01546 
01547         if (s->packet_loss) {
01551             s->num_saved_bits = 0;
01552             s->packet_loss = 0;
01553         }
01554 
01555     } else {
01556         int frame_size;
01557         s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
01558         init_get_bits(gb, avpkt->data, s->buf_bit_size);
01559         skip_bits(gb, s->packet_offset);
01560         if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
01561             (frame_size = show_bits(gb, s->log2_frame_size)) &&
01562             frame_size <= remaining_bits(s, gb)) {
01563             save_bits(s, gb, frame_size, 0);
01564             s->packet_done = !decode_frame(s);
01565         } else if (!s->len_prefix
01566                    && s->num_saved_bits > get_bits_count(&s->gb)) {
01574             s->packet_done = !decode_frame(s);
01575         } else
01576             s->packet_done = 1;
01577     }
01578 
01579     if (s->packet_done && !s->packet_loss &&
01580         remaining_bits(s, gb) > 0) {
01583         save_bits(s, gb, remaining_bits(s, gb), 0);
01584     }
01585 
01586     *data_size = (int8_t *)s->samples - (int8_t *)data;
01587     s->packet_offset = get_bits_count(gb) & 7;
01588 
01589     return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
01590 }
01591 
01596 static void flush(AVCodecContext *avctx)
01597 {
01598     WMAProDecodeCtx *s = avctx->priv_data;
01599     int i;
01602     for (i = 0; i < s->num_channels; i++)
01603         memset(s->channel[i].out, 0, s->samples_per_frame *
01604                sizeof(*s->channel[i].out));
01605     s->packet_loss = 1;
01606 }
01607 
01608 
01612 AVCodec ff_wmapro_decoder = {
01613     "wmapro",
01614     AVMEDIA_TYPE_AUDIO,
01615     CODEC_ID_WMAPRO,
01616     sizeof(WMAProDecodeCtx),
01617     decode_init,
01618     NULL,
01619     decode_end,
01620     decode_packet,
01621     .capabilities = CODEC_CAP_SUBFRAMES,
01622     .flush= flush,
01623     .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 9 Professional"),
01624 };