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 "libavutil/intfloat.h"
00090 #include "libavutil/intreadwrite.h"
00091 #include "avcodec.h"
00092 #include "internal.h"
00093 #include "get_bits.h"
00094 #include "put_bits.h"
00095 #include "wmaprodata.h"
00096 #include "dsputil.h"
00097 #include "fmtconvert.h"
00098 #include "sinewin.h"
00099 #include "wma.h"
00100 
00102 #define WMAPRO_MAX_CHANNELS    8                             ///< max number of handled channels
00103 #define MAX_SUBFRAMES  32                                    ///< max number of subframes per channel
00104 #define MAX_BANDS      29                                    ///< max number of scale factor bands
00105 #define MAX_FRAMESIZE  32768                                 ///< maximum compressed frame size
00106 
00107 #define WMAPRO_BLOCK_MIN_BITS  6                                           ///< log2 of min block size
00108 #define WMAPRO_BLOCK_MAX_BITS 12                                           ///< log2 of max block size
00109 #define WMAPRO_BLOCK_MAX_SIZE (1 << WMAPRO_BLOCK_MAX_BITS)                 ///< maximum block size
00110 #define WMAPRO_BLOCK_SIZES    (WMAPRO_BLOCK_MAX_BITS - WMAPRO_BLOCK_MIN_BITS + 1) ///< possible block sizes
00111 
00112 
00113 #define VLCBITS            9
00114 #define SCALEVLCBITS       8
00115 #define VEC4MAXDEPTH    ((HUFF_VEC4_MAXBITS+VLCBITS-1)/VLCBITS)
00116 #define VEC2MAXDEPTH    ((HUFF_VEC2_MAXBITS+VLCBITS-1)/VLCBITS)
00117 #define VEC1MAXDEPTH    ((HUFF_VEC1_MAXBITS+VLCBITS-1)/VLCBITS)
00118 #define SCALEMAXDEPTH   ((HUFF_SCALE_MAXBITS+SCALEVLCBITS-1)/SCALEVLCBITS)
00119 #define SCALERLMAXDEPTH ((HUFF_SCALE_RL_MAXBITS+VLCBITS-1)/VLCBITS)
00120 
00121 static VLC              sf_vlc;           
00122 static VLC              sf_rl_vlc;        
00123 static VLC              vec4_vlc;         
00124 static VLC              vec2_vlc;         
00125 static VLC              vec1_vlc;         
00126 static VLC              coef_vlc[2];      
00127 static float            sin64[33];        
00128 
00132 typedef struct {
00133     int16_t  prev_block_len;                          
00134     uint8_t  transmit_coefs;
00135     uint8_t  num_subframes;
00136     uint16_t subframe_len[MAX_SUBFRAMES];             
00137     uint16_t subframe_offset[MAX_SUBFRAMES];          
00138     uint8_t  cur_subframe;                            
00139     uint16_t decoded_samples;                         
00140     uint8_t  grouped;                                 
00141     int      quant_step;                              
00142     int8_t   reuse_sf;                                
00143     int8_t   scale_factor_step;                       
00144     int      max_scale_factor;                        
00145     int      saved_scale_factors[2][MAX_BANDS];       
00146     int8_t   scale_factor_idx;                        
00147     int*     scale_factors;                           
00148     uint8_t  table_idx;                               
00149     float*   coeffs;                                  
00150     uint16_t num_vec_coeffs;                          
00151     DECLARE_ALIGNED(32, float, out)[WMAPRO_BLOCK_MAX_SIZE + WMAPRO_BLOCK_MAX_SIZE / 2]; 
00152 } WMAProChannelCtx;
00153 
00157 typedef struct {
00158     uint8_t num_channels;                                     
00159     int8_t  transform;                                        
00160     int8_t  transform_band[MAX_BANDS];                        
00161     float   decorrelation_matrix[WMAPRO_MAX_CHANNELS*WMAPRO_MAX_CHANNELS];
00162     float*  channel_data[WMAPRO_MAX_CHANNELS];                
00163 } WMAProChannelGrp;
00164 
00168 typedef struct WMAProDecodeCtx {
00169     /* generic decoder variables */
00170     AVCodecContext*  avctx;                         
00171     AVFrame          frame;                         
00172     DSPContext       dsp;                           
00173     FmtConvertContext fmt_conv;
00174     uint8_t          frame_data[MAX_FRAMESIZE +
00175                       FF_INPUT_BUFFER_PADDING_SIZE];
00176     PutBitContext    pb;                            
00177     FFTContext       mdct_ctx[WMAPRO_BLOCK_SIZES];  
00178     DECLARE_ALIGNED(32, float, tmp)[WMAPRO_BLOCK_MAX_SIZE]; 
00179     float*           windows[WMAPRO_BLOCK_SIZES];   
00180 
00181     /* frame size dependent frame information (set during initialization) */
00182     uint32_t         decode_flags;                  
00183     uint8_t          len_prefix;                    
00184     uint8_t          dynamic_range_compression;     
00185     uint8_t          bits_per_sample;               
00186     uint16_t         samples_per_frame;             
00187     uint16_t         log2_frame_size;
00188     int8_t           num_channels;                  
00189     int8_t           lfe_channel;                   
00190     uint8_t          max_num_subframes;
00191     uint8_t          subframe_len_bits;             
00192     uint8_t          max_subframe_len_bit;          
00193     uint16_t         min_samples_per_subframe;
00194     int8_t           num_sfb[WMAPRO_BLOCK_SIZES];   
00195     int16_t          sfb_offsets[WMAPRO_BLOCK_SIZES][MAX_BANDS];                    
00196     int8_t           sf_offsets[WMAPRO_BLOCK_SIZES][WMAPRO_BLOCK_SIZES][MAX_BANDS]; 
00197     int16_t          subwoofer_cutoffs[WMAPRO_BLOCK_SIZES]; 
00198 
00199     /* packet decode state */
00200     GetBitContext    pgb;                           
00201     int              next_packet_start;             
00202     uint8_t          packet_offset;                 
00203     uint8_t          packet_sequence_number;        
00204     int              num_saved_bits;                
00205     int              frame_offset;                  
00206     int              subframe_offset;               
00207     uint8_t          packet_loss;                   
00208     uint8_t          packet_done;                   
00209 
00210     /* frame decode state */
00211     uint32_t         frame_num;                     
00212     GetBitContext    gb;                            
00213     int              buf_bit_size;                  
00214     uint8_t          drc_gain;                      
00215     int8_t           skip_frame;                    
00216     int8_t           parsed_all_subframes;          
00217 
00218     /* subframe/block decode state */
00219     int16_t          subframe_len;                  
00220     int8_t           channels_for_cur_subframe;     
00221     int8_t           channel_indexes_for_cur_subframe[WMAPRO_MAX_CHANNELS];
00222     int8_t           num_bands;                     
00223     int8_t           transmit_num_vec_coeffs;       
00224     int16_t*         cur_sfb_offsets;               
00225     uint8_t          table_idx;                     
00226     int8_t           esc_len;                       
00227 
00228     uint8_t          num_chgroups;                  
00229     WMAProChannelGrp chgroup[WMAPRO_MAX_CHANNELS];  
00230 
00231     WMAProChannelCtx channel[WMAPRO_MAX_CHANNELS];  
00232 } WMAProDecodeCtx;
00233 
00234 
00239 static void av_cold dump_context(WMAProDecodeCtx *s)
00240 {
00241 #define PRINT(a, b)     av_log(s->avctx, AV_LOG_DEBUG, " %s = %d\n", a, b);
00242 #define PRINT_HEX(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %x\n", a, b);
00243 
00244     PRINT("ed sample bit depth", s->bits_per_sample);
00245     PRINT_HEX("ed decode flags", s->decode_flags);
00246     PRINT("samples per frame",   s->samples_per_frame);
00247     PRINT("log2 frame size",     s->log2_frame_size);
00248     PRINT("max num subframes",   s->max_num_subframes);
00249     PRINT("len prefix",          s->len_prefix);
00250     PRINT("num channels",        s->num_channels);
00251 }
00252 
00258 static av_cold int decode_end(AVCodecContext *avctx)
00259 {
00260     WMAProDecodeCtx *s = avctx->priv_data;
00261     int i;
00262 
00263     for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
00264         ff_mdct_end(&s->mdct_ctx[i]);
00265 
00266     return 0;
00267 }
00268 
00274 static av_cold int decode_init(AVCodecContext *avctx)
00275 {
00276     WMAProDecodeCtx *s = avctx->priv_data;
00277     uint8_t *edata_ptr = avctx->extradata;
00278     unsigned int channel_mask;
00279     int i;
00280     int log2_max_num_subframes;
00281     int num_possible_block_sizes;
00282 
00283     s->avctx = avctx;
00284     dsputil_init(&s->dsp, avctx);
00285     ff_fmt_convert_init(&s->fmt_conv, avctx);
00286     init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
00287 
00288     avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00289 
00290     if (avctx->extradata_size >= 18) {
00291         s->decode_flags    = AV_RL16(edata_ptr+14);
00292         channel_mask       = AV_RL32(edata_ptr+2);
00293         s->bits_per_sample = AV_RL16(edata_ptr);
00295         for (i = 0; i < avctx->extradata_size; i++)
00296             av_dlog(avctx, "[%x] ", avctx->extradata[i]);
00297         av_dlog(avctx, "\n");
00298 
00299     } else {
00300         av_log_ask_for_sample(avctx, "Unknown extradata size\n");
00301         return AVERROR_INVALIDDATA;
00302     }
00303 
00305     s->log2_frame_size = av_log2(avctx->block_align) + 4;
00306 
00308     s->skip_frame  = 1; /* skip first frame */
00309     s->packet_loss = 1;
00310     s->len_prefix  = (s->decode_flags & 0x40);
00311 
00313     s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(avctx->sample_rate,
00314                                                           3, s->decode_flags);
00315 
00317     log2_max_num_subframes       = ((s->decode_flags & 0x38) >> 3);
00318     s->max_num_subframes         = 1 << log2_max_num_subframes;
00319     if (s->max_num_subframes == 16 || s->max_num_subframes == 4)
00320         s->max_subframe_len_bit = 1;
00321     s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
00322 
00323     num_possible_block_sizes     = log2_max_num_subframes + 1;
00324     s->min_samples_per_subframe  = s->samples_per_frame / s->max_num_subframes;
00325     s->dynamic_range_compression = (s->decode_flags & 0x80);
00326 
00327     if (s->max_num_subframes > MAX_SUBFRAMES) {
00328         av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %i\n",
00329                s->max_num_subframes);
00330         return AVERROR_INVALIDDATA;
00331     }
00332 
00333     s->num_channels = avctx->channels;
00334 
00335     if (s->num_channels < 0) {
00336         av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n", s->num_channels);
00337         return AVERROR_INVALIDDATA;
00338     } else if (s->num_channels > WMAPRO_MAX_CHANNELS) {
00339         av_log_ask_for_sample(avctx, "unsupported number of channels\n");
00340         return AVERROR_PATCHWELCOME;
00341     }
00342 
00344     for (i = 0; i < s->num_channels; i++)
00345         s->channel[i].prev_block_len = s->samples_per_frame;
00346 
00348     s->lfe_channel = -1;
00349 
00350     if (channel_mask & 8) {
00351         unsigned int mask;
00352         for (mask = 1; mask < 16; mask <<= 1) {
00353             if (channel_mask & mask)
00354                 ++s->lfe_channel;
00355         }
00356     }
00357 
00358     INIT_VLC_STATIC(&sf_vlc, SCALEVLCBITS, HUFF_SCALE_SIZE,
00359                     scale_huffbits, 1, 1,
00360                     scale_huffcodes, 2, 2, 616);
00361 
00362     INIT_VLC_STATIC(&sf_rl_vlc, VLCBITS, HUFF_SCALE_RL_SIZE,
00363                     scale_rl_huffbits, 1, 1,
00364                     scale_rl_huffcodes, 4, 4, 1406);
00365 
00366     INIT_VLC_STATIC(&coef_vlc[0], VLCBITS, HUFF_COEF0_SIZE,
00367                     coef0_huffbits, 1, 1,
00368                     coef0_huffcodes, 4, 4, 2108);
00369 
00370     INIT_VLC_STATIC(&coef_vlc[1], VLCBITS, HUFF_COEF1_SIZE,
00371                     coef1_huffbits, 1, 1,
00372                     coef1_huffcodes, 4, 4, 3912);
00373 
00374     INIT_VLC_STATIC(&vec4_vlc, VLCBITS, HUFF_VEC4_SIZE,
00375                     vec4_huffbits, 1, 1,
00376                     vec4_huffcodes, 2, 2, 604);
00377 
00378     INIT_VLC_STATIC(&vec2_vlc, VLCBITS, HUFF_VEC2_SIZE,
00379                     vec2_huffbits, 1, 1,
00380                     vec2_huffcodes, 2, 2, 562);
00381 
00382     INIT_VLC_STATIC(&vec1_vlc, VLCBITS, HUFF_VEC1_SIZE,
00383                     vec1_huffbits, 1, 1,
00384                     vec1_huffcodes, 2, 2, 562);
00385 
00388     for (i = 0; i < num_possible_block_sizes; i++) {
00389         int subframe_len = s->samples_per_frame >> i;
00390         int x;
00391         int band = 1;
00392 
00393         s->sfb_offsets[i][0] = 0;
00394 
00395         for (x = 0; x < MAX_BANDS-1 && s->sfb_offsets[i][band - 1] < subframe_len; x++) {
00396             int offset = (subframe_len * 2 * critical_freq[x])
00397                           / s->avctx->sample_rate + 2;
00398             offset &= ~3;
00399             if (offset > s->sfb_offsets[i][band - 1])
00400                 s->sfb_offsets[i][band++] = offset;
00401         }
00402         s->sfb_offsets[i][band - 1] = subframe_len;
00403         s->num_sfb[i]               = band - 1;
00404     }
00405 
00406 
00412     for (i = 0; i < num_possible_block_sizes; i++) {
00413         int b;
00414         for (b = 0; b < s->num_sfb[i]; b++) {
00415             int x;
00416             int offset = ((s->sfb_offsets[i][b]
00417                            + s->sfb_offsets[i][b + 1] - 1) << i) >> 1;
00418             for (x = 0; x < num_possible_block_sizes; x++) {
00419                 int v = 0;
00420                 while (s->sfb_offsets[x][v + 1] << x < offset)
00421                     ++v;
00422                 s->sf_offsets[i][x][b] = v;
00423             }
00424         }
00425     }
00426 
00428     for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
00429         ff_mdct_init(&s->mdct_ctx[i], WMAPRO_BLOCK_MIN_BITS+1+i, 1,
00430                      1.0 / (1 << (WMAPRO_BLOCK_MIN_BITS + i - 1))
00431                      / (1 << (s->bits_per_sample - 1)));
00432 
00434     for (i = 0; i < WMAPRO_BLOCK_SIZES; i++) {
00435         const int win_idx = WMAPRO_BLOCK_MAX_BITS - i;
00436         ff_init_ff_sine_windows(win_idx);
00437         s->windows[WMAPRO_BLOCK_SIZES - i - 1] = ff_sine_windows[win_idx];
00438     }
00439 
00441     for (i = 0; i < num_possible_block_sizes; i++) {
00442         int block_size = s->samples_per_frame >> i;
00443         int cutoff = (440*block_size + 3 * (s->avctx->sample_rate >> 1) - 1)
00444                      / s->avctx->sample_rate;
00445         s->subwoofer_cutoffs[i] = av_clip(cutoff, 4, block_size);
00446     }
00447 
00449     for (i = 0; i < 33; i++)
00450         sin64[i] = sin(i*M_PI / 64.0);
00451 
00452     if (avctx->debug & FF_DEBUG_BITSTREAM)
00453         dump_context(s);
00454 
00455     avctx->channel_layout = channel_mask;
00456 
00457     avcodec_get_frame_defaults(&s->frame);
00458     avctx->coded_frame = &s->frame;
00459 
00460     return 0;
00461 }
00462 
00469 static int decode_subframe_length(WMAProDecodeCtx *s, int offset)
00470 {
00471     int frame_len_shift = 0;
00472     int subframe_len;
00473 
00475     if (offset == s->samples_per_frame - s->min_samples_per_subframe)
00476         return s->min_samples_per_subframe;
00477 
00479     if (s->max_subframe_len_bit) {
00480         if (get_bits1(&s->gb))
00481             frame_len_shift = 1 + get_bits(&s->gb, s->subframe_len_bits-1);
00482     } else
00483         frame_len_shift = get_bits(&s->gb, s->subframe_len_bits);
00484 
00485     subframe_len = s->samples_per_frame >> frame_len_shift;
00486 
00488     if (subframe_len < s->min_samples_per_subframe ||
00489         subframe_len > s->samples_per_frame) {
00490         av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
00491                subframe_len);
00492         return AVERROR_INVALIDDATA;
00493     }
00494     return subframe_len;
00495 }
00496 
00517 static int decode_tilehdr(WMAProDecodeCtx *s)
00518 {
00519     uint16_t num_samples[WMAPRO_MAX_CHANNELS];        
00520     uint8_t  contains_subframe[WMAPRO_MAX_CHANNELS];  
00521     int channels_for_cur_subframe = s->num_channels;  
00522     int fixed_channel_layout = 0;                     
00523     int min_channel_len = 0;                          
00524     int c;
00525 
00526     /* Should never consume more than 3073 bits (256 iterations for the
00527      * while loop when always the minimum amount of 128 samples is substracted
00528      * from missing samples in the 8 channel case).
00529      * 1 + BLOCK_MAX_SIZE * MAX_CHANNELS / BLOCK_MIN_SIZE * (MAX_CHANNELS  + 4)
00530      */
00531 
00533     for (c = 0; c < s->num_channels; c++)
00534         s->channel[c].num_subframes = 0;
00535 
00536     memset(num_samples, 0, sizeof(num_samples));
00537 
00538     if (s->max_num_subframes == 1 || get_bits1(&s->gb))
00539         fixed_channel_layout = 1;
00540 
00542     do {
00543         int subframe_len;
00544 
00546         for (c = 0; c < s->num_channels; c++) {
00547             if (num_samples[c] == min_channel_len) {
00548                 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
00549                    (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe))
00550                     contains_subframe[c] = 1;
00551                 else
00552                     contains_subframe[c] = get_bits1(&s->gb);
00553             } else
00554                 contains_subframe[c] = 0;
00555         }
00556 
00558         if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
00559             return AVERROR_INVALIDDATA;
00560 
00562         min_channel_len += subframe_len;
00563         for (c = 0; c < s->num_channels; c++) {
00564             WMAProChannelCtx* chan = &s->channel[c];
00565 
00566             if (contains_subframe[c]) {
00567                 if (chan->num_subframes >= MAX_SUBFRAMES) {
00568                     av_log(s->avctx, AV_LOG_ERROR,
00569                            "broken frame: num subframes > 31\n");
00570                     return AVERROR_INVALIDDATA;
00571                 }
00572                 chan->subframe_len[chan->num_subframes] = subframe_len;
00573                 num_samples[c] += subframe_len;
00574                 ++chan->num_subframes;
00575                 if (num_samples[c] > s->samples_per_frame) {
00576                     av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
00577                            "channel len > samples_per_frame\n");
00578                     return AVERROR_INVALIDDATA;
00579                 }
00580             } else if (num_samples[c] <= min_channel_len) {
00581                 if (num_samples[c] < min_channel_len) {
00582                     channels_for_cur_subframe = 0;
00583                     min_channel_len = num_samples[c];
00584                 }
00585                 ++channels_for_cur_subframe;
00586             }
00587         }
00588     } while (min_channel_len < s->samples_per_frame);
00589 
00590     for (c = 0; c < s->num_channels; c++) {
00591         int i;
00592         int offset = 0;
00593         for (i = 0; i < s->channel[c].num_subframes; i++) {
00594             av_dlog(s->avctx, "frame[%i] channel[%i] subframe[%i]"
00595                     " len %i\n", s->frame_num, c, i,
00596                     s->channel[c].subframe_len[i]);
00597             s->channel[c].subframe_offset[i] = offset;
00598             offset += s->channel[c].subframe_len[i];
00599         }
00600     }
00601 
00602     return 0;
00603 }
00604 
00610 static void decode_decorrelation_matrix(WMAProDecodeCtx *s,
00611                                         WMAProChannelGrp *chgroup)
00612 {
00613     int i;
00614     int offset = 0;
00615     int8_t rotation_offset[WMAPRO_MAX_CHANNELS * WMAPRO_MAX_CHANNELS];
00616     memset(chgroup->decorrelation_matrix, 0, s->num_channels *
00617            s->num_channels * sizeof(*chgroup->decorrelation_matrix));
00618 
00619     for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++)
00620         rotation_offset[i] = get_bits(&s->gb, 6);
00621 
00622     for (i = 0; i < chgroup->num_channels; i++)
00623         chgroup->decorrelation_matrix[chgroup->num_channels * i + i] =
00624             get_bits1(&s->gb) ? 1.0 : -1.0;
00625 
00626     for (i = 1; i < chgroup->num_channels; i++) {
00627         int x;
00628         for (x = 0; x < i; x++) {
00629             int y;
00630             for (y = 0; y < i + 1; y++) {
00631                 float v1 = chgroup->decorrelation_matrix[x * chgroup->num_channels + y];
00632                 float v2 = chgroup->decorrelation_matrix[i * chgroup->num_channels + y];
00633                 int n = rotation_offset[offset + x];
00634                 float sinv;
00635                 float cosv;
00636 
00637                 if (n < 32) {
00638                     sinv = sin64[n];
00639                     cosv = sin64[32 - n];
00640                 } else {
00641                     sinv =  sin64[64 -  n];
00642                     cosv = -sin64[n  - 32];
00643                 }
00644 
00645                 chgroup->decorrelation_matrix[y + x * chgroup->num_channels] =
00646                                                (v1 * sinv) - (v2 * cosv);
00647                 chgroup->decorrelation_matrix[y + i * chgroup->num_channels] =
00648                                                (v1 * cosv) + (v2 * sinv);
00649             }
00650         }
00651         offset += i;
00652     }
00653 }
00654 
00660 static int decode_channel_transform(WMAProDecodeCtx* s)
00661 {
00662     int i;
00663     /* should never consume more than 1921 bits for the 8 channel case
00664      * 1 + MAX_CHANNELS * (MAX_CHANNELS + 2 + 3 * MAX_CHANNELS * MAX_CHANNELS
00665      * + MAX_CHANNELS + MAX_BANDS + 1)
00666      */
00667 
00669     s->num_chgroups = 0;
00670     if (s->num_channels > 1) {
00671         int remaining_channels = s->channels_for_cur_subframe;
00672 
00673         if (get_bits1(&s->gb)) {
00674             av_log_ask_for_sample(s->avctx,
00675                                   "unsupported channel transform bit\n");
00676             return AVERROR_INVALIDDATA;
00677         }
00678 
00679         for (s->num_chgroups = 0; remaining_channels &&
00680              s->num_chgroups < s->channels_for_cur_subframe; s->num_chgroups++) {
00681             WMAProChannelGrp* chgroup = &s->chgroup[s->num_chgroups];
00682             float** channel_data = chgroup->channel_data;
00683             chgroup->num_channels = 0;
00684             chgroup->transform = 0;
00685 
00687             if (remaining_channels > 2) {
00688                 for (i = 0; i < s->channels_for_cur_subframe; i++) {
00689                     int channel_idx = s->channel_indexes_for_cur_subframe[i];
00690                     if (!s->channel[channel_idx].grouped
00691                         && get_bits1(&s->gb)) {
00692                         ++chgroup->num_channels;
00693                         s->channel[channel_idx].grouped = 1;
00694                         *channel_data++ = s->channel[channel_idx].coeffs;
00695                     }
00696                 }
00697             } else {
00698                 chgroup->num_channels = remaining_channels;
00699                 for (i = 0; i < s->channels_for_cur_subframe; i++) {
00700                     int channel_idx = s->channel_indexes_for_cur_subframe[i];
00701                     if (!s->channel[channel_idx].grouped)
00702                         *channel_data++ = s->channel[channel_idx].coeffs;
00703                     s->channel[channel_idx].grouped = 1;
00704                 }
00705             }
00706 
00708             if (chgroup->num_channels == 2) {
00709                 if (get_bits1(&s->gb)) {
00710                     if (get_bits1(&s->gb)) {
00711                         av_log_ask_for_sample(s->avctx,
00712                                               "unsupported channel transform type\n");
00713                     }
00714                 } else {
00715                     chgroup->transform = 1;
00716                     if (s->num_channels == 2) {
00717                         chgroup->decorrelation_matrix[0] =  1.0;
00718                         chgroup->decorrelation_matrix[1] = -1.0;
00719                         chgroup->decorrelation_matrix[2] =  1.0;
00720                         chgroup->decorrelation_matrix[3] =  1.0;
00721                     } else {
00723                         chgroup->decorrelation_matrix[0] =  0.70703125;
00724                         chgroup->decorrelation_matrix[1] = -0.70703125;
00725                         chgroup->decorrelation_matrix[2] =  0.70703125;
00726                         chgroup->decorrelation_matrix[3] =  0.70703125;
00727                     }
00728                 }
00729             } else if (chgroup->num_channels > 2) {
00730                 if (get_bits1(&s->gb)) {
00731                     chgroup->transform = 1;
00732                     if (get_bits1(&s->gb)) {
00733                         decode_decorrelation_matrix(s, chgroup);
00734                     } else {
00736                         if (chgroup->num_channels > 6) {
00737                             av_log_ask_for_sample(s->avctx,
00738                                                   "coupled channels > 6\n");
00739                         } else {
00740                             memcpy(chgroup->decorrelation_matrix,
00741                                    default_decorrelation[chgroup->num_channels],
00742                                    chgroup->num_channels * chgroup->num_channels *
00743                                    sizeof(*chgroup->decorrelation_matrix));
00744                         }
00745                     }
00746                 }
00747             }
00748 
00750             if (chgroup->transform) {
00751                 if (!get_bits1(&s->gb)) {
00752                     int i;
00754                     for (i = 0; i < s->num_bands; i++) {
00755                         chgroup->transform_band[i] = get_bits1(&s->gb);
00756                     }
00757                 } else {
00758                     memset(chgroup->transform_band, 1, s->num_bands);
00759                 }
00760             }
00761             remaining_channels -= chgroup->num_channels;
00762         }
00763     }
00764     return 0;
00765 }
00766 
00773 static int decode_coeffs(WMAProDecodeCtx *s, int c)
00774 {
00775     /* Integers 0..15 as single-precision floats.  The table saves a
00776        costly int to float conversion, and storing the values as
00777        integers allows fast sign-flipping. */
00778     static const uint32_t fval_tab[16] = {
00779         0x00000000, 0x3f800000, 0x40000000, 0x40400000,
00780         0x40800000, 0x40a00000, 0x40c00000, 0x40e00000,
00781         0x41000000, 0x41100000, 0x41200000, 0x41300000,
00782         0x41400000, 0x41500000, 0x41600000, 0x41700000,
00783     };
00784     int vlctable;
00785     VLC* vlc;
00786     WMAProChannelCtx* ci = &s->channel[c];
00787     int rl_mode = 0;
00788     int cur_coeff = 0;
00789     int num_zeros = 0;
00790     const uint16_t* run;
00791     const float* level;
00792 
00793     av_dlog(s->avctx, "decode coefficients for channel %i\n", c);
00794 
00795     vlctable = get_bits1(&s->gb);
00796     vlc = &coef_vlc[vlctable];
00797 
00798     if (vlctable) {
00799         run = coef1_run;
00800         level = coef1_level;
00801     } else {
00802         run = coef0_run;
00803         level = coef0_level;
00804     }
00805 
00808     while ((s->transmit_num_vec_coeffs || !rl_mode) &&
00809            (cur_coeff + 3 < ci->num_vec_coeffs)) {
00810         uint32_t vals[4];
00811         int i;
00812         unsigned int idx;
00813 
00814         idx = get_vlc2(&s->gb, vec4_vlc.table, VLCBITS, VEC4MAXDEPTH);
00815 
00816         if (idx == HUFF_VEC4_SIZE - 1) {
00817             for (i = 0; i < 4; i += 2) {
00818                 idx = get_vlc2(&s->gb, vec2_vlc.table, VLCBITS, VEC2MAXDEPTH);
00819                 if (idx == HUFF_VEC2_SIZE - 1) {
00820                     uint32_t v0, v1;
00821                     v0 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
00822                     if (v0 == HUFF_VEC1_SIZE - 1)
00823                         v0 += ff_wma_get_large_val(&s->gb);
00824                     v1 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
00825                     if (v1 == HUFF_VEC1_SIZE - 1)
00826                         v1 += ff_wma_get_large_val(&s->gb);
00827                     vals[i  ] = av_float2int(v0);
00828                     vals[i+1] = av_float2int(v1);
00829                 } else {
00830                     vals[i]   = fval_tab[symbol_to_vec2[idx] >> 4 ];
00831                     vals[i+1] = fval_tab[symbol_to_vec2[idx] & 0xF];
00832                 }
00833             }
00834         } else {
00835             vals[0] = fval_tab[ symbol_to_vec4[idx] >> 12      ];
00836             vals[1] = fval_tab[(symbol_to_vec4[idx] >> 8) & 0xF];
00837             vals[2] = fval_tab[(symbol_to_vec4[idx] >> 4) & 0xF];
00838             vals[3] = fval_tab[ symbol_to_vec4[idx]       & 0xF];
00839         }
00840 
00842         for (i = 0; i < 4; i++) {
00843             if (vals[i]) {
00844                 uint32_t sign = get_bits1(&s->gb) - 1;
00845                 AV_WN32A(&ci->coeffs[cur_coeff], vals[i] ^ sign << 31);
00846                 num_zeros = 0;
00847             } else {
00848                 ci->coeffs[cur_coeff] = 0;
00851                 rl_mode |= (++num_zeros > s->subframe_len >> 8);
00852             }
00853             ++cur_coeff;
00854         }
00855     }
00856 
00858     if (cur_coeff < s->subframe_len) {
00859         memset(&ci->coeffs[cur_coeff], 0,
00860                sizeof(*ci->coeffs) * (s->subframe_len - cur_coeff));
00861         if (ff_wma_run_level_decode(s->avctx, &s->gb, vlc,
00862                                     level, run, 1, ci->coeffs,
00863                                     cur_coeff, s->subframe_len,
00864                                     s->subframe_len, s->esc_len, 0))
00865             return AVERROR_INVALIDDATA;
00866     }
00867 
00868     return 0;
00869 }
00870 
00876 static int decode_scale_factors(WMAProDecodeCtx* s)
00877 {
00878     int i;
00879 
00884     for (i = 0; i < s->channels_for_cur_subframe; i++) {
00885         int c = s->channel_indexes_for_cur_subframe[i];
00886         int* sf;
00887         int* sf_end;
00888         s->channel[c].scale_factors = s->channel[c].saved_scale_factors[!s->channel[c].scale_factor_idx];
00889         sf_end = s->channel[c].scale_factors + s->num_bands;
00890 
00896         if (s->channel[c].reuse_sf) {
00897             const int8_t* sf_offsets = s->sf_offsets[s->table_idx][s->channel[c].table_idx];
00898             int b;
00899             for (b = 0; b < s->num_bands; b++)
00900                 s->channel[c].scale_factors[b] =
00901                     s->channel[c].saved_scale_factors[s->channel[c].scale_factor_idx][*sf_offsets++];
00902         }
00903 
00904         if (!s->channel[c].cur_subframe || get_bits1(&s->gb)) {
00905 
00906             if (!s->channel[c].reuse_sf) {
00907                 int val;
00909                 s->channel[c].scale_factor_step = get_bits(&s->gb, 2) + 1;
00910                 val = 45 / s->channel[c].scale_factor_step;
00911                 for (sf = s->channel[c].scale_factors; sf < sf_end; sf++) {
00912                     val += get_vlc2(&s->gb, sf_vlc.table, SCALEVLCBITS, SCALEMAXDEPTH) - 60;
00913                     *sf = val;
00914                 }
00915             } else {
00916                 int i;
00918                 for (i = 0; i < s->num_bands; i++) {
00919                     int idx;
00920                     int skip;
00921                     int val;
00922                     int sign;
00923 
00924                     idx = get_vlc2(&s->gb, sf_rl_vlc.table, VLCBITS, SCALERLMAXDEPTH);
00925 
00926                     if (!idx) {
00927                         uint32_t code = get_bits(&s->gb, 14);
00928                         val  =  code >> 6;
00929                         sign = (code & 1) - 1;
00930                         skip = (code & 0x3f) >> 1;
00931                     } else if (idx == 1) {
00932                         break;
00933                     } else {
00934                         skip = scale_rl_run[idx];
00935                         val  = scale_rl_level[idx];
00936                         sign = get_bits1(&s->gb)-1;
00937                     }
00938 
00939                     i += skip;
00940                     if (i >= s->num_bands) {
00941                         av_log(s->avctx, AV_LOG_ERROR,
00942                                "invalid scale factor coding\n");
00943                         return AVERROR_INVALIDDATA;
00944                     }
00945                     s->channel[c].scale_factors[i] += (val ^ sign) - sign;
00946                 }
00947             }
00949             s->channel[c].scale_factor_idx = !s->channel[c].scale_factor_idx;
00950             s->channel[c].table_idx = s->table_idx;
00951             s->channel[c].reuse_sf  = 1;
00952         }
00953 
00955         s->channel[c].max_scale_factor = s->channel[c].scale_factors[0];
00956         for (sf = s->channel[c].scale_factors + 1; sf < sf_end; sf++) {
00957             s->channel[c].max_scale_factor =
00958                 FFMAX(s->channel[c].max_scale_factor, *sf);
00959         }
00960 
00961     }
00962     return 0;
00963 }
00964 
00969 static void inverse_channel_transform(WMAProDecodeCtx *s)
00970 {
00971     int i;
00972 
00973     for (i = 0; i < s->num_chgroups; i++) {
00974         if (s->chgroup[i].transform) {
00975             float data[WMAPRO_MAX_CHANNELS];
00976             const int num_channels = s->chgroup[i].num_channels;
00977             float** ch_data = s->chgroup[i].channel_data;
00978             float** ch_end = ch_data + num_channels;
00979             const int8_t* tb = s->chgroup[i].transform_band;
00980             int16_t* sfb;
00981 
00983             for (sfb = s->cur_sfb_offsets;
00984                  sfb < s->cur_sfb_offsets + s->num_bands; sfb++) {
00985                 int y;
00986                 if (*tb++ == 1) {
00988                     for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) {
00989                         const float* mat = s->chgroup[i].decorrelation_matrix;
00990                         const float* data_end = data + num_channels;
00991                         float* data_ptr = data;
00992                         float** ch;
00993 
00994                         for (ch = ch_data; ch < ch_end; ch++)
00995                             *data_ptr++ = (*ch)[y];
00996 
00997                         for (ch = ch_data; ch < ch_end; ch++) {
00998                             float sum = 0;
00999                             data_ptr = data;
01000                             while (data_ptr < data_end)
01001                                 sum += *data_ptr++ * *mat++;
01002 
01003                             (*ch)[y] = sum;
01004                         }
01005                     }
01006                 } else if (s->num_channels == 2) {
01007                     int len = FFMIN(sfb[1], s->subframe_len) - sfb[0];
01008                     s->dsp.vector_fmul_scalar(ch_data[0] + sfb[0],
01009                                               ch_data[0] + sfb[0],
01010                                               181.0 / 128, len);
01011                     s->dsp.vector_fmul_scalar(ch_data[1] + sfb[0],
01012                                               ch_data[1] + sfb[0],
01013                                               181.0 / 128, len);
01014                 }
01015             }
01016         }
01017     }
01018 }
01019 
01024 static void wmapro_window(WMAProDecodeCtx *s)
01025 {
01026     int i;
01027     for (i = 0; i < s->channels_for_cur_subframe; i++) {
01028         int c = s->channel_indexes_for_cur_subframe[i];
01029         float* window;
01030         int winlen = s->channel[c].prev_block_len;
01031         float* start = s->channel[c].coeffs - (winlen >> 1);
01032 
01033         if (s->subframe_len < winlen) {
01034             start += (winlen - s->subframe_len) >> 1;
01035             winlen = s->subframe_len;
01036         }
01037 
01038         window = s->windows[av_log2(winlen) - WMAPRO_BLOCK_MIN_BITS];
01039 
01040         winlen >>= 1;
01041 
01042         s->dsp.vector_fmul_window(start, start, start + winlen,
01043                                   window, winlen);
01044 
01045         s->channel[c].prev_block_len = s->subframe_len;
01046     }
01047 }
01048 
01054 static int decode_subframe(WMAProDecodeCtx *s)
01055 {
01056     int offset = s->samples_per_frame;
01057     int subframe_len = s->samples_per_frame;
01058     int i;
01059     int total_samples   = s->samples_per_frame * s->num_channels;
01060     int transmit_coeffs = 0;
01061     int cur_subwoofer_cutoff;
01062 
01063     s->subframe_offset = get_bits_count(&s->gb);
01064 
01069     for (i = 0; i < s->num_channels; i++) {
01070         s->channel[i].grouped = 0;
01071         if (offset > s->channel[i].decoded_samples) {
01072             offset = s->channel[i].decoded_samples;
01073             subframe_len =
01074                 s->channel[i].subframe_len[s->channel[i].cur_subframe];
01075         }
01076     }
01077 
01078     av_dlog(s->avctx,
01079             "processing subframe with offset %i len %i\n", offset, subframe_len);
01080 
01082     s->channels_for_cur_subframe = 0;
01083     for (i = 0; i < s->num_channels; i++) {
01084         const int cur_subframe = s->channel[i].cur_subframe;
01086         total_samples -= s->channel[i].decoded_samples;
01087 
01089         if (offset == s->channel[i].decoded_samples &&
01090             subframe_len == s->channel[i].subframe_len[cur_subframe]) {
01091             total_samples -= s->channel[i].subframe_len[cur_subframe];
01092             s->channel[i].decoded_samples +=
01093                 s->channel[i].subframe_len[cur_subframe];
01094             s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
01095             ++s->channels_for_cur_subframe;
01096         }
01097     }
01098 
01101     if (!total_samples)
01102         s->parsed_all_subframes = 1;
01103 
01104 
01105     av_dlog(s->avctx, "subframe is part of %i channels\n",
01106             s->channels_for_cur_subframe);
01107 
01109     s->table_idx         = av_log2(s->samples_per_frame/subframe_len);
01110     s->num_bands         = s->num_sfb[s->table_idx];
01111     s->cur_sfb_offsets   = s->sfb_offsets[s->table_idx];
01112     cur_subwoofer_cutoff = s->subwoofer_cutoffs[s->table_idx];
01113 
01115     for (i = 0; i < s->channels_for_cur_subframe; i++) {
01116         int c = s->channel_indexes_for_cur_subframe[i];
01117 
01118         s->channel[c].coeffs = &s->channel[c].out[(s->samples_per_frame >> 1)
01119                                                   + offset];
01120     }
01121 
01122     s->subframe_len = subframe_len;
01123     s->esc_len = av_log2(s->subframe_len - 1) + 1;
01124 
01126     if (get_bits1(&s->gb)) {
01127         int num_fill_bits;
01128         if (!(num_fill_bits = get_bits(&s->gb, 2))) {
01129             int len = get_bits(&s->gb, 4);
01130             num_fill_bits = get_bits(&s->gb, len) + 1;
01131         }
01132 
01133         if (num_fill_bits >= 0) {
01134             if (get_bits_count(&s->gb) + num_fill_bits > s->num_saved_bits) {
01135                 av_log(s->avctx, AV_LOG_ERROR, "invalid number of fill bits\n");
01136                 return AVERROR_INVALIDDATA;
01137             }
01138 
01139             skip_bits_long(&s->gb, num_fill_bits);
01140         }
01141     }
01142 
01144     if (get_bits1(&s->gb)) {
01145         av_log_ask_for_sample(s->avctx, "reserved bit set\n");
01146         return AVERROR_INVALIDDATA;
01147     }
01148 
01149 
01150     if (decode_channel_transform(s) < 0)
01151         return AVERROR_INVALIDDATA;
01152 
01153 
01154     for (i = 0; i < s->channels_for_cur_subframe; i++) {
01155         int c = s->channel_indexes_for_cur_subframe[i];
01156         if ((s->channel[c].transmit_coefs = get_bits1(&s->gb)))
01157             transmit_coeffs = 1;
01158     }
01159 
01160     if (transmit_coeffs) {
01161         int step;
01162         int quant_step = 90 * s->bits_per_sample >> 4;
01163 
01165         if ((s->transmit_num_vec_coeffs = get_bits1(&s->gb))) {
01166             int num_bits = av_log2((s->subframe_len + 3)/4) + 1;
01167             for (i = 0; i < s->channels_for_cur_subframe; i++) {
01168                 int c = s->channel_indexes_for_cur_subframe[i];
01169                 s->channel[c].num_vec_coeffs = get_bits(&s->gb, num_bits) << 2;
01170             }
01171         } else {
01172             for (i = 0; i < s->channels_for_cur_subframe; i++) {
01173                 int c = s->channel_indexes_for_cur_subframe[i];
01174                 s->channel[c].num_vec_coeffs = s->subframe_len;
01175             }
01176         }
01178         step = get_sbits(&s->gb, 6);
01179         quant_step += step;
01180         if (step == -32 || step == 31) {
01181             const int sign = (step == 31) - 1;
01182             int quant = 0;
01183             while (get_bits_count(&s->gb) + 5 < s->num_saved_bits &&
01184                    (step = get_bits(&s->gb, 5)) == 31) {
01185                 quant += 31;
01186             }
01187             quant_step += ((quant + step) ^ sign) - sign;
01188         }
01189         if (quant_step < 0) {
01190             av_log(s->avctx, AV_LOG_DEBUG, "negative quant step\n");
01191         }
01192 
01195         if (s->channels_for_cur_subframe == 1) {
01196             s->channel[s->channel_indexes_for_cur_subframe[0]].quant_step = quant_step;
01197         } else {
01198             int modifier_len = get_bits(&s->gb, 3);
01199             for (i = 0; i < s->channels_for_cur_subframe; i++) {
01200                 int c = s->channel_indexes_for_cur_subframe[i];
01201                 s->channel[c].quant_step = quant_step;
01202                 if (get_bits1(&s->gb)) {
01203                     if (modifier_len) {
01204                         s->channel[c].quant_step += get_bits(&s->gb, modifier_len) + 1;
01205                     } else
01206                         ++s->channel[c].quant_step;
01207                 }
01208             }
01209         }
01210 
01212         if (decode_scale_factors(s) < 0)
01213             return AVERROR_INVALIDDATA;
01214     }
01215 
01216     av_dlog(s->avctx, "BITSTREAM: subframe header length was %i\n",
01217             get_bits_count(&s->gb) - s->subframe_offset);
01218 
01220     for (i = 0; i < s->channels_for_cur_subframe; i++) {
01221         int c = s->channel_indexes_for_cur_subframe[i];
01222         if (s->channel[c].transmit_coefs &&
01223             get_bits_count(&s->gb) < s->num_saved_bits) {
01224             decode_coeffs(s, c);
01225         } else
01226             memset(s->channel[c].coeffs, 0,
01227                    sizeof(*s->channel[c].coeffs) * subframe_len);
01228     }
01229 
01230     av_dlog(s->avctx, "BITSTREAM: subframe length was %i\n",
01231             get_bits_count(&s->gb) - s->subframe_offset);
01232 
01233     if (transmit_coeffs) {
01234         FFTContext *mdct = &s->mdct_ctx[av_log2(subframe_len) - WMAPRO_BLOCK_MIN_BITS];
01236         inverse_channel_transform(s);
01237         for (i = 0; i < s->channels_for_cur_subframe; i++) {
01238             int c = s->channel_indexes_for_cur_subframe[i];
01239             const int* sf = s->channel[c].scale_factors;
01240             int b;
01241 
01242             if (c == s->lfe_channel)
01243                 memset(&s->tmp[cur_subwoofer_cutoff], 0, sizeof(*s->tmp) *
01244                        (subframe_len - cur_subwoofer_cutoff));
01245 
01247             for (b = 0; b < s->num_bands; b++) {
01248                 const int end = FFMIN(s->cur_sfb_offsets[b+1], s->subframe_len);
01249                 const int exp = s->channel[c].quant_step -
01250                             (s->channel[c].max_scale_factor - *sf++) *
01251                             s->channel[c].scale_factor_step;
01252                 const float quant = pow(10.0, exp / 20.0);
01253                 int start = s->cur_sfb_offsets[b];
01254                 s->dsp.vector_fmul_scalar(s->tmp + start,
01255                                           s->channel[c].coeffs + start,
01256                                           quant, end - start);
01257             }
01258 
01260             mdct->imdct_half(mdct, s->channel[c].coeffs, s->tmp);
01261         }
01262     }
01263 
01265     wmapro_window(s);
01266 
01268     for (i = 0; i < s->channels_for_cur_subframe; i++) {
01269         int c = s->channel_indexes_for_cur_subframe[i];
01270         if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
01271             av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
01272             return AVERROR_INVALIDDATA;
01273         }
01274         ++s->channel[c].cur_subframe;
01275     }
01276 
01277     return 0;
01278 }
01279 
01286 static int decode_frame(WMAProDecodeCtx *s, int *got_frame_ptr)
01287 {
01288     AVCodecContext *avctx = s->avctx;
01289     GetBitContext* gb = &s->gb;
01290     int more_frames = 0;
01291     int len = 0;
01292     int i, ret;
01293     const float *out_ptr[WMAPRO_MAX_CHANNELS];
01294     float *samples;
01295 
01297     if (s->len_prefix)
01298         len = get_bits(gb, s->log2_frame_size);
01299 
01300     av_dlog(s->avctx, "decoding frame with length %x\n", len);
01301 
01303     if (decode_tilehdr(s)) {
01304         s->packet_loss = 1;
01305         return 0;
01306     }
01307 
01309     if (s->num_channels > 1 && get_bits1(gb)) {
01310         if (get_bits1(gb)) {
01311             for (i = 0; i < s->num_channels * s->num_channels; i++)
01312                 skip_bits(gb, 4);
01313         }
01314     }
01315 
01317     if (s->dynamic_range_compression) {
01318         s->drc_gain = get_bits(gb, 8);
01319         av_dlog(s->avctx, "drc_gain %i\n", s->drc_gain);
01320     }
01321 
01324     if (get_bits1(gb)) {
01325         int av_unused skip;
01326 
01328         if (get_bits1(gb)) {
01329             skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
01330             av_dlog(s->avctx, "start skip: %i\n", skip);
01331         }
01332 
01334         if (get_bits1(gb)) {
01335             skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
01336             av_dlog(s->avctx, "end skip: %i\n", skip);
01337         }
01338 
01339     }
01340 
01341     av_dlog(s->avctx, "BITSTREAM: frame header length was %i\n",
01342             get_bits_count(gb) - s->frame_offset);
01343 
01345     s->parsed_all_subframes = 0;
01346     for (i = 0; i < s->num_channels; i++) {
01347         s->channel[i].decoded_samples = 0;
01348         s->channel[i].cur_subframe    = 0;
01349         s->channel[i].reuse_sf        = 0;
01350     }
01351 
01353     while (!s->parsed_all_subframes) {
01354         if (decode_subframe(s) < 0) {
01355             s->packet_loss = 1;
01356             return 0;
01357         }
01358     }
01359 
01360     /* get output buffer */
01361     s->frame.nb_samples = s->samples_per_frame;
01362     if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
01363         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01364         s->packet_loss = 1;
01365         return 0;
01366     }
01367     samples = (float *)s->frame.data[0];
01368 
01370     for (i = 0; i < s->num_channels; i++)
01371         out_ptr[i] = s->channel[i].out;
01372     s->fmt_conv.float_interleave(samples, out_ptr, s->samples_per_frame,
01373                                  s->num_channels);
01374 
01375     for (i = 0; i < s->num_channels; i++) {
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         *got_frame_ptr = 0;
01385     } else {
01386         *got_frame_ptr = 1;
01387     }
01388 
01389     if (s->len_prefix) {
01390         if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
01392             av_log(s->avctx, AV_LOG_ERROR,
01393                    "frame[%i] would have to skip %i bits\n", s->frame_num,
01394                    len - (get_bits_count(gb) - s->frame_offset) - 1);
01395             s->packet_loss = 1;
01396             return 0;
01397         }
01398 
01400         skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
01401     } else {
01402         while (get_bits_count(gb) < s->num_saved_bits && get_bits1(gb) == 0) {
01403         }
01404     }
01405 
01407     more_frames = get_bits1(gb);
01408 
01409     ++s->frame_num;
01410     return more_frames;
01411 }
01412 
01419 static int remaining_bits(WMAProDecodeCtx *s, GetBitContext *gb)
01420 {
01421     return s->buf_bit_size - get_bits_count(gb);
01422 }
01423 
01431 static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len,
01432                       int append)
01433 {
01434     int buflen;
01435 
01440     if (!append) {
01441         s->frame_offset = get_bits_count(gb) & 7;
01442         s->num_saved_bits = s->frame_offset;
01443         init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
01444     }
01445 
01446     buflen = (s->num_saved_bits + len + 8) >> 3;
01447 
01448     if (len <= 0 || buflen > MAX_FRAMESIZE) {
01449         av_log_ask_for_sample(s->avctx, "input buffer too small\n");
01450         s->packet_loss = 1;
01451         return;
01452     }
01453 
01454     s->num_saved_bits += len;
01455     if (!append) {
01456         avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
01457                      s->num_saved_bits);
01458     } else {
01459         int align = 8 - (get_bits_count(gb) & 7);
01460         align = FFMIN(align, len);
01461         put_bits(&s->pb, align, get_bits(gb, align));
01462         len -= align;
01463         avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
01464     }
01465     skip_bits_long(gb, len);
01466 
01467     {
01468         PutBitContext tmp = s->pb;
01469         flush_put_bits(&tmp);
01470     }
01471 
01472     init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
01473     skip_bits(&s->gb, s->frame_offset);
01474 }
01475 
01484 static int decode_packet(AVCodecContext *avctx, void *data,
01485                          int *got_frame_ptr, AVPacket* avpkt)
01486 {
01487     WMAProDecodeCtx *s = avctx->priv_data;
01488     GetBitContext* gb  = &s->pgb;
01489     const uint8_t* buf = avpkt->data;
01490     int buf_size       = avpkt->size;
01491     int num_bits_prev_frame;
01492     int packet_sequence_number;
01493 
01494     *got_frame_ptr = 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, got_frame_ptr);
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, got_frame_ptr);
01565         } else if (!s->len_prefix
01566                    && s->num_saved_bits > get_bits_count(&s->gb)) {
01574             s->packet_done = !decode_frame(s, got_frame_ptr);
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     s->packet_offset = get_bits_count(gb) & 7;
01587     if (s->packet_loss)
01588         return AVERROR_INVALIDDATA;
01589 
01590     if (*got_frame_ptr)
01591         *(AVFrame *)data = s->frame;
01592 
01593     return get_bits_count(gb) >> 3;
01594 }
01595 
01600 static void flush(AVCodecContext *avctx)
01601 {
01602     WMAProDecodeCtx *s = avctx->priv_data;
01603     int i;
01606     for (i = 0; i < s->num_channels; i++)
01607         memset(s->channel[i].out, 0, s->samples_per_frame *
01608                sizeof(*s->channel[i].out));
01609     s->packet_loss = 1;
01610 }
01611 
01612 
01616 AVCodec ff_wmapro_decoder = {
01617     .name           = "wmapro",
01618     .type           = AVMEDIA_TYPE_AUDIO,
01619     .id             = CODEC_ID_WMAPRO,
01620     .priv_data_size = sizeof(WMAProDecodeCtx),
01621     .init           = decode_init,
01622     .close          = decode_end,
01623     .decode         = decode_packet,
01624     .capabilities   = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
01625     .flush= flush,
01626     .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 9 Professional"),
01627 };