• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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 FFmpeg.
00007  *
00008  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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     if (s->avctx->sample_rate <= 0) {
00330         av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
00331         return AVERROR_INVALIDDATA;
00332     }
00333 
00334     s->num_channels = avctx->channels;
00335 
00336     if (s->num_channels < 0) {
00337         av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n", s->num_channels);
00338         return AVERROR_INVALIDDATA;
00339     } else if (s->num_channels > WMAPRO_MAX_CHANNELS) {
00340         av_log_ask_for_sample(avctx, "unsupported number of channels\n");
00341         return AVERROR_PATCHWELCOME;
00342     }
00343 
00345     for (i = 0; i < s->num_channels; i++)
00346         s->channel[i].prev_block_len = s->samples_per_frame;
00347 
00349     s->lfe_channel = -1;
00350 
00351     if (channel_mask & 8) {
00352         unsigned int mask;
00353         for (mask = 1; mask < 16; mask <<= 1) {
00354             if (channel_mask & mask)
00355                 ++s->lfe_channel;
00356         }
00357     }
00358 
00359     INIT_VLC_STATIC(&sf_vlc, SCALEVLCBITS, HUFF_SCALE_SIZE,
00360                     scale_huffbits, 1, 1,
00361                     scale_huffcodes, 2, 2, 616);
00362 
00363     INIT_VLC_STATIC(&sf_rl_vlc, VLCBITS, HUFF_SCALE_RL_SIZE,
00364                     scale_rl_huffbits, 1, 1,
00365                     scale_rl_huffcodes, 4, 4, 1406);
00366 
00367     INIT_VLC_STATIC(&coef_vlc[0], VLCBITS, HUFF_COEF0_SIZE,
00368                     coef0_huffbits, 1, 1,
00369                     coef0_huffcodes, 4, 4, 2108);
00370 
00371     INIT_VLC_STATIC(&coef_vlc[1], VLCBITS, HUFF_COEF1_SIZE,
00372                     coef1_huffbits, 1, 1,
00373                     coef1_huffcodes, 4, 4, 3912);
00374 
00375     INIT_VLC_STATIC(&vec4_vlc, VLCBITS, HUFF_VEC4_SIZE,
00376                     vec4_huffbits, 1, 1,
00377                     vec4_huffcodes, 2, 2, 604);
00378 
00379     INIT_VLC_STATIC(&vec2_vlc, VLCBITS, HUFF_VEC2_SIZE,
00380                     vec2_huffbits, 1, 1,
00381                     vec2_huffcodes, 2, 2, 562);
00382 
00383     INIT_VLC_STATIC(&vec1_vlc, VLCBITS, HUFF_VEC1_SIZE,
00384                     vec1_huffbits, 1, 1,
00385                     vec1_huffcodes, 2, 2, 562);
00386 
00389     for (i = 0; i < num_possible_block_sizes; i++) {
00390         int subframe_len = s->samples_per_frame >> i;
00391         int x;
00392         int band = 1;
00393 
00394         s->sfb_offsets[i][0] = 0;
00395 
00396         for (x = 0; x < MAX_BANDS-1 && s->sfb_offsets[i][band - 1] < subframe_len; x++) {
00397             int offset = (subframe_len * 2 * critical_freq[x])
00398                           / s->avctx->sample_rate + 2;
00399             offset &= ~3;
00400             if (offset > s->sfb_offsets[i][band - 1])
00401                 s->sfb_offsets[i][band++] = offset;
00402         }
00403         s->sfb_offsets[i][band - 1] = subframe_len;
00404         s->num_sfb[i]               = band - 1;
00405     }
00406 
00407 
00413     for (i = 0; i < num_possible_block_sizes; i++) {
00414         int b;
00415         for (b = 0; b < s->num_sfb[i]; b++) {
00416             int x;
00417             int offset = ((s->sfb_offsets[i][b]
00418                            + s->sfb_offsets[i][b + 1] - 1) << i) >> 1;
00419             for (x = 0; x < num_possible_block_sizes; x++) {
00420                 int v = 0;
00421                 while (s->sfb_offsets[x][v + 1] << x < offset)
00422                     ++v;
00423                 s->sf_offsets[i][x][b] = v;
00424             }
00425         }
00426     }
00427 
00429     for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
00430         ff_mdct_init(&s->mdct_ctx[i], WMAPRO_BLOCK_MIN_BITS+1+i, 1,
00431                      1.0 / (1 << (WMAPRO_BLOCK_MIN_BITS + i - 1))
00432                      / (1 << (s->bits_per_sample - 1)));
00433 
00435     for (i = 0; i < WMAPRO_BLOCK_SIZES; i++) {
00436         const int win_idx = WMAPRO_BLOCK_MAX_BITS - i;
00437         ff_init_ff_sine_windows(win_idx);
00438         s->windows[WMAPRO_BLOCK_SIZES - i - 1] = ff_sine_windows[win_idx];
00439     }
00440 
00442     for (i = 0; i < num_possible_block_sizes; i++) {
00443         int block_size = s->samples_per_frame >> i;
00444         int cutoff = (440*block_size + 3 * (s->avctx->sample_rate >> 1) - 1)
00445                      / s->avctx->sample_rate;
00446         s->subwoofer_cutoffs[i] = av_clip(cutoff, 4, block_size);
00447     }
00448 
00450     for (i = 0; i < 33; i++)
00451         sin64[i] = sin(i*M_PI / 64.0);
00452 
00453     if (avctx->debug & FF_DEBUG_BITSTREAM)
00454         dump_context(s);
00455 
00456     avctx->channel_layout = channel_mask;
00457     return 0;
00458 }
00459 
00466 static int decode_subframe_length(WMAProDecodeCtx *s, int offset)
00467 {
00468     int frame_len_shift = 0;
00469     int subframe_len;
00470 
00472     if (offset == s->samples_per_frame - s->min_samples_per_subframe)
00473         return s->min_samples_per_subframe;
00474 
00476     if (s->max_subframe_len_bit) {
00477         if (get_bits1(&s->gb))
00478             frame_len_shift = 1 + get_bits(&s->gb, s->subframe_len_bits-1);
00479     } else
00480         frame_len_shift = get_bits(&s->gb, s->subframe_len_bits);
00481 
00482     subframe_len = s->samples_per_frame >> frame_len_shift;
00483 
00485     if (subframe_len < s->min_samples_per_subframe ||
00486         subframe_len > s->samples_per_frame) {
00487         av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
00488                subframe_len);
00489         return AVERROR_INVALIDDATA;
00490     }
00491     return subframe_len;
00492 }
00493 
00514 static int decode_tilehdr(WMAProDecodeCtx *s)
00515 {
00516     uint16_t num_samples[WMAPRO_MAX_CHANNELS];        
00517     uint8_t  contains_subframe[WMAPRO_MAX_CHANNELS];  
00518     int channels_for_cur_subframe = s->num_channels;  
00519     int fixed_channel_layout = 0;                     
00520     int min_channel_len = 0;                          
00521     int c;
00522 
00523     /* Should never consume more than 3073 bits (256 iterations for the
00524      * while loop when always the minimum amount of 128 samples is substracted
00525      * from missing samples in the 8 channel case).
00526      * 1 + BLOCK_MAX_SIZE * MAX_CHANNELS / BLOCK_MIN_SIZE * (MAX_CHANNELS  + 4)
00527      */
00528 
00530     for (c = 0; c < s->num_channels; c++)
00531         s->channel[c].num_subframes = 0;
00532 
00533     memset(num_samples, 0, sizeof(num_samples));
00534 
00535     if (s->max_num_subframes == 1 || get_bits1(&s->gb))
00536         fixed_channel_layout = 1;
00537 
00539     do {
00540         int subframe_len;
00541 
00543         for (c = 0; c < s->num_channels; c++) {
00544             if (num_samples[c] == min_channel_len) {
00545                 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
00546                    (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe))
00547                     contains_subframe[c] = 1;
00548                 else
00549                     contains_subframe[c] = get_bits1(&s->gb);
00550             } else
00551                 contains_subframe[c] = 0;
00552         }
00553 
00555         if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
00556             return AVERROR_INVALIDDATA;
00557 
00559         min_channel_len += subframe_len;
00560         for (c = 0; c < s->num_channels; c++) {
00561             WMAProChannelCtx* chan = &s->channel[c];
00562 
00563             if (contains_subframe[c]) {
00564                 if (chan->num_subframes >= MAX_SUBFRAMES) {
00565                     av_log(s->avctx, AV_LOG_ERROR,
00566                            "broken frame: num subframes > 31\n");
00567                     return AVERROR_INVALIDDATA;
00568                 }
00569                 chan->subframe_len[chan->num_subframes] = subframe_len;
00570                 num_samples[c] += subframe_len;
00571                 ++chan->num_subframes;
00572                 if (num_samples[c] > s->samples_per_frame) {
00573                     av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
00574                            "channel len > samples_per_frame\n");
00575                     return AVERROR_INVALIDDATA;
00576                 }
00577             } else if (num_samples[c] <= min_channel_len) {
00578                 if (num_samples[c] < min_channel_len) {
00579                     channels_for_cur_subframe = 0;
00580                     min_channel_len = num_samples[c];
00581                 }
00582                 ++channels_for_cur_subframe;
00583             }
00584         }
00585     } while (min_channel_len < s->samples_per_frame);
00586 
00587     for (c = 0; c < s->num_channels; c++) {
00588         int i;
00589         int offset = 0;
00590         for (i = 0; i < s->channel[c].num_subframes; i++) {
00591             av_dlog(s->avctx, "frame[%i] channel[%i] subframe[%i]"
00592                     " len %i\n", s->frame_num, c, i,
00593                     s->channel[c].subframe_len[i]);
00594             s->channel[c].subframe_offset[i] = offset;
00595             offset += s->channel[c].subframe_len[i];
00596         }
00597     }
00598 
00599     return 0;
00600 }
00601 
00607 static void decode_decorrelation_matrix(WMAProDecodeCtx *s,
00608                                         WMAProChannelGrp *chgroup)
00609 {
00610     int i;
00611     int offset = 0;
00612     int8_t rotation_offset[WMAPRO_MAX_CHANNELS * WMAPRO_MAX_CHANNELS];
00613     memset(chgroup->decorrelation_matrix, 0, s->num_channels *
00614            s->num_channels * sizeof(*chgroup->decorrelation_matrix));
00615 
00616     for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++)
00617         rotation_offset[i] = get_bits(&s->gb, 6);
00618 
00619     for (i = 0; i < chgroup->num_channels; i++)
00620         chgroup->decorrelation_matrix[chgroup->num_channels * i + i] =
00621             get_bits1(&s->gb) ? 1.0 : -1.0;
00622 
00623     for (i = 1; i < chgroup->num_channels; i++) {
00624         int x;
00625         for (x = 0; x < i; x++) {
00626             int y;
00627             for (y = 0; y < i + 1; y++) {
00628                 float v1 = chgroup->decorrelation_matrix[x * chgroup->num_channels + y];
00629                 float v2 = chgroup->decorrelation_matrix[i * chgroup->num_channels + y];
00630                 int n = rotation_offset[offset + x];
00631                 float sinv;
00632                 float cosv;
00633 
00634                 if (n < 32) {
00635                     sinv = sin64[n];
00636                     cosv = sin64[32 - n];
00637                 } else {
00638                     sinv =  sin64[64 -  n];
00639                     cosv = -sin64[n  - 32];
00640                 }
00641 
00642                 chgroup->decorrelation_matrix[y + x * chgroup->num_channels] =
00643                                                (v1 * sinv) - (v2 * cosv);
00644                 chgroup->decorrelation_matrix[y + i * chgroup->num_channels] =
00645                                                (v1 * cosv) + (v2 * sinv);
00646             }
00647         }
00648         offset += i;
00649     }
00650 }
00651 
00657 static int decode_channel_transform(WMAProDecodeCtx* s)
00658 {
00659     int i;
00660     /* should never consume more than 1921 bits for the 8 channel case
00661      * 1 + MAX_CHANNELS * (MAX_CHANNELS + 2 + 3 * MAX_CHANNELS * MAX_CHANNELS
00662      * + MAX_CHANNELS + MAX_BANDS + 1)
00663      */
00664 
00666     s->num_chgroups = 0;
00667     if (s->num_channels > 1) {
00668         int remaining_channels = s->channels_for_cur_subframe;
00669 
00670         if (get_bits1(&s->gb)) {
00671             av_log_ask_for_sample(s->avctx,
00672                                   "unsupported channel transform bit\n");
00673             return AVERROR_INVALIDDATA;
00674         }
00675 
00676         for (s->num_chgroups = 0; remaining_channels &&
00677              s->num_chgroups < s->channels_for_cur_subframe; s->num_chgroups++) {
00678             WMAProChannelGrp* chgroup = &s->chgroup[s->num_chgroups];
00679             float** channel_data = chgroup->channel_data;
00680             chgroup->num_channels = 0;
00681             chgroup->transform = 0;
00682 
00684             if (remaining_channels > 2) {
00685                 for (i = 0; i < s->channels_for_cur_subframe; i++) {
00686                     int channel_idx = s->channel_indexes_for_cur_subframe[i];
00687                     if (!s->channel[channel_idx].grouped
00688                         && get_bits1(&s->gb)) {
00689                         ++chgroup->num_channels;
00690                         s->channel[channel_idx].grouped = 1;
00691                         *channel_data++ = s->channel[channel_idx].coeffs;
00692                     }
00693                 }
00694             } else {
00695                 chgroup->num_channels = remaining_channels;
00696                 for (i = 0; i < s->channels_for_cur_subframe; i++) {
00697                     int channel_idx = s->channel_indexes_for_cur_subframe[i];
00698                     if (!s->channel[channel_idx].grouped)
00699                         *channel_data++ = s->channel[channel_idx].coeffs;
00700                     s->channel[channel_idx].grouped = 1;
00701                 }
00702             }
00703 
00705             if (chgroup->num_channels == 2) {
00706                 if (get_bits1(&s->gb)) {
00707                     if (get_bits1(&s->gb)) {
00708                         av_log_ask_for_sample(s->avctx,
00709                                               "unsupported channel transform type\n");
00710                     }
00711                 } else {
00712                     chgroup->transform = 1;
00713                     if (s->num_channels == 2) {
00714                         chgroup->decorrelation_matrix[0] =  1.0;
00715                         chgroup->decorrelation_matrix[1] = -1.0;
00716                         chgroup->decorrelation_matrix[2] =  1.0;
00717                         chgroup->decorrelation_matrix[3] =  1.0;
00718                     } else {
00720                         chgroup->decorrelation_matrix[0] =  0.70703125;
00721                         chgroup->decorrelation_matrix[1] = -0.70703125;
00722                         chgroup->decorrelation_matrix[2] =  0.70703125;
00723                         chgroup->decorrelation_matrix[3] =  0.70703125;
00724                     }
00725                 }
00726             } else if (chgroup->num_channels > 2) {
00727                 if (get_bits1(&s->gb)) {
00728                     chgroup->transform = 1;
00729                     if (get_bits1(&s->gb)) {
00730                         decode_decorrelation_matrix(s, chgroup);
00731                     } else {
00733                         if (chgroup->num_channels > 6) {
00734                             av_log_ask_for_sample(s->avctx,
00735                                                   "coupled channels > 6\n");
00736                         } else {
00737                             memcpy(chgroup->decorrelation_matrix,
00738                                    default_decorrelation[chgroup->num_channels],
00739                                    chgroup->num_channels * chgroup->num_channels *
00740                                    sizeof(*chgroup->decorrelation_matrix));
00741                         }
00742                     }
00743                 }
00744             }
00745 
00747             if (chgroup->transform) {
00748                 if (!get_bits1(&s->gb)) {
00749                     int i;
00751                     for (i = 0; i < s->num_bands; i++) {
00752                         chgroup->transform_band[i] = get_bits1(&s->gb);
00753                     }
00754                 } else {
00755                     memset(chgroup->transform_band, 1, s->num_bands);
00756                 }
00757             }
00758             remaining_channels -= chgroup->num_channels;
00759         }
00760     }
00761     return 0;
00762 }
00763 
00770 static int decode_coeffs(WMAProDecodeCtx *s, int c)
00771 {
00772     /* Integers 0..15 as single-precision floats.  The table saves a
00773        costly int to float conversion, and storing the values as
00774        integers allows fast sign-flipping. */
00775     static const int fval_tab[16] = {
00776         0x00000000, 0x3f800000, 0x40000000, 0x40400000,
00777         0x40800000, 0x40a00000, 0x40c00000, 0x40e00000,
00778         0x41000000, 0x41100000, 0x41200000, 0x41300000,
00779         0x41400000, 0x41500000, 0x41600000, 0x41700000,
00780     };
00781     int vlctable;
00782     VLC* vlc;
00783     WMAProChannelCtx* ci = &s->channel[c];
00784     int rl_mode = 0;
00785     int cur_coeff = 0;
00786     int num_zeros = 0;
00787     const uint16_t* run;
00788     const float* level;
00789 
00790     av_dlog(s->avctx, "decode coefficients for channel %i\n", c);
00791 
00792     vlctable = get_bits1(&s->gb);
00793     vlc = &coef_vlc[vlctable];
00794 
00795     if (vlctable) {
00796         run = coef1_run;
00797         level = coef1_level;
00798     } else {
00799         run = coef0_run;
00800         level = coef0_level;
00801     }
00802 
00805     while ((s->transmit_num_vec_coeffs || !rl_mode) &&
00806            (cur_coeff + 3 < ci->num_vec_coeffs)) {
00807         int vals[4];
00808         int i;
00809         unsigned int idx;
00810 
00811         idx = get_vlc2(&s->gb, vec4_vlc.table, VLCBITS, VEC4MAXDEPTH);
00812 
00813         if (idx == HUFF_VEC4_SIZE - 1) {
00814             for (i = 0; i < 4; i += 2) {
00815                 idx = get_vlc2(&s->gb, vec2_vlc.table, VLCBITS, VEC2MAXDEPTH);
00816                 if (idx == HUFF_VEC2_SIZE - 1) {
00817                     int v0, v1;
00818                     v0 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
00819                     if (v0 == HUFF_VEC1_SIZE - 1)
00820                         v0 += ff_wma_get_large_val(&s->gb);
00821                     v1 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
00822                     if (v1 == HUFF_VEC1_SIZE - 1)
00823                         v1 += ff_wma_get_large_val(&s->gb);
00824                     ((float*)vals)[i  ] = v0;
00825                     ((float*)vals)[i+1] = v1;
00826                 } else {
00827                     vals[i]   = fval_tab[symbol_to_vec2[idx] >> 4 ];
00828                     vals[i+1] = fval_tab[symbol_to_vec2[idx] & 0xF];
00829                 }
00830             }
00831         } else {
00832             vals[0] = fval_tab[ symbol_to_vec4[idx] >> 12      ];
00833             vals[1] = fval_tab[(symbol_to_vec4[idx] >> 8) & 0xF];
00834             vals[2] = fval_tab[(symbol_to_vec4[idx] >> 4) & 0xF];
00835             vals[3] = fval_tab[ symbol_to_vec4[idx]       & 0xF];
00836         }
00837 
00839         for (i = 0; i < 4; i++) {
00840             if (vals[i]) {
00841                 int sign = get_bits1(&s->gb) - 1;
00842                 *(uint32_t*)&ci->coeffs[cur_coeff] = vals[i] ^ sign<<31;
00843                 num_zeros = 0;
00844             } else {
00845                 ci->coeffs[cur_coeff] = 0;
00848                 rl_mode |= (++num_zeros > s->subframe_len >> 8);
00849             }
00850             ++cur_coeff;
00851         }
00852     }
00853 
00855     if (cur_coeff < s->subframe_len) {
00856         memset(&ci->coeffs[cur_coeff], 0,
00857                sizeof(*ci->coeffs) * (s->subframe_len - cur_coeff));
00858         if (ff_wma_run_level_decode(s->avctx, &s->gb, vlc,
00859                                     level, run, 1, ci->coeffs,
00860                                     cur_coeff, s->subframe_len,
00861                                     s->subframe_len, s->esc_len, 0))
00862             return AVERROR_INVALIDDATA;
00863     }
00864 
00865     return 0;
00866 }
00867 
00873 static int decode_scale_factors(WMAProDecodeCtx* s)
00874 {
00875     int i;
00876 
00881     for (i = 0; i < s->channels_for_cur_subframe; i++) {
00882         int c = s->channel_indexes_for_cur_subframe[i];
00883         int* sf;
00884         int* sf_end;
00885         s->channel[c].scale_factors = s->channel[c].saved_scale_factors[!s->channel[c].scale_factor_idx];
00886         sf_end = s->channel[c].scale_factors + s->num_bands;
00887 
00893         if (s->channel[c].reuse_sf) {
00894             const int8_t* sf_offsets = s->sf_offsets[s->table_idx][s->channel[c].table_idx];
00895             int b;
00896             for (b = 0; b < s->num_bands; b++)
00897                 s->channel[c].scale_factors[b] =
00898                     s->channel[c].saved_scale_factors[s->channel[c].scale_factor_idx][*sf_offsets++];
00899         }
00900 
00901         if (!s->channel[c].cur_subframe || get_bits1(&s->gb)) {
00902 
00903             if (!s->channel[c].reuse_sf) {
00904                 int val;
00906                 s->channel[c].scale_factor_step = get_bits(&s->gb, 2) + 1;
00907                 val = 45 / s->channel[c].scale_factor_step;
00908                 for (sf = s->channel[c].scale_factors; sf < sf_end; sf++) {
00909                     val += get_vlc2(&s->gb, sf_vlc.table, SCALEVLCBITS, SCALEMAXDEPTH) - 60;
00910                     *sf = val;
00911                 }
00912             } else {
00913                 int i;
00915                 for (i = 0; i < s->num_bands; i++) {
00916                     int idx;
00917                     int skip;
00918                     int val;
00919                     int sign;
00920 
00921                     idx = get_vlc2(&s->gb, sf_rl_vlc.table, VLCBITS, SCALERLMAXDEPTH);
00922 
00923                     if (!idx) {
00924                         uint32_t code = get_bits(&s->gb, 14);
00925                         val  =  code >> 6;
00926                         sign = (code & 1) - 1;
00927                         skip = (code & 0x3f) >> 1;
00928                     } else if (idx == 1) {
00929                         break;
00930                     } else {
00931                         skip = scale_rl_run[idx];
00932                         val  = scale_rl_level[idx];
00933                         sign = get_bits1(&s->gb)-1;
00934                     }
00935 
00936                     i += skip;
00937                     if (i >= s->num_bands) {
00938                         av_log(s->avctx, AV_LOG_ERROR,
00939                                "invalid scale factor coding\n");
00940                         return AVERROR_INVALIDDATA;
00941                     }
00942                     s->channel[c].scale_factors[i] += (val ^ sign) - sign;
00943                 }
00944             }
00946             s->channel[c].scale_factor_idx = !s->channel[c].scale_factor_idx;
00947             s->channel[c].table_idx = s->table_idx;
00948             s->channel[c].reuse_sf  = 1;
00949         }
00950 
00952         s->channel[c].max_scale_factor = s->channel[c].scale_factors[0];
00953         for (sf = s->channel[c].scale_factors + 1; sf < sf_end; sf++) {
00954             s->channel[c].max_scale_factor =
00955                 FFMAX(s->channel[c].max_scale_factor, *sf);
00956         }
00957 
00958     }
00959     return 0;
00960 }
00961 
00966 static void inverse_channel_transform(WMAProDecodeCtx *s)
00967 {
00968     int i;
00969 
00970     for (i = 0; i < s->num_chgroups; i++) {
00971         if (s->chgroup[i].transform) {
00972             float data[WMAPRO_MAX_CHANNELS];
00973             const int num_channels = s->chgroup[i].num_channels;
00974             float** ch_data = s->chgroup[i].channel_data;
00975             float** ch_end = ch_data + num_channels;
00976             const int8_t* tb = s->chgroup[i].transform_band;
00977             int16_t* sfb;
00978 
00980             for (sfb = s->cur_sfb_offsets;
00981                  sfb < s->cur_sfb_offsets + s->num_bands; sfb++) {
00982                 int y;
00983                 if (*tb++ == 1) {
00985                     for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) {
00986                         const float* mat = s->chgroup[i].decorrelation_matrix;
00987                         const float* data_end = data + num_channels;
00988                         float* data_ptr = data;
00989                         float** ch;
00990 
00991                         for (ch = ch_data; ch < ch_end; ch++)
00992                             *data_ptr++ = (*ch)[y];
00993 
00994                         for (ch = ch_data; ch < ch_end; ch++) {
00995                             float sum = 0;
00996                             data_ptr = data;
00997                             while (data_ptr < data_end)
00998                                 sum += *data_ptr++ * *mat++;
00999 
01000                             (*ch)[y] = sum;
01001                         }
01002                     }
01003                 } else if (s->num_channels == 2) {
01004                     int len = FFMIN(sfb[1], s->subframe_len) - sfb[0];
01005                     s->dsp.vector_fmul_scalar(ch_data[0] + sfb[0],
01006                                               ch_data[0] + sfb[0],
01007                                               181.0 / 128, len);
01008                     s->dsp.vector_fmul_scalar(ch_data[1] + sfb[0],
01009                                               ch_data[1] + sfb[0],
01010                                               181.0 / 128, len);
01011                 }
01012             }
01013         }
01014     }
01015 }
01016 
01021 static void wmapro_window(WMAProDecodeCtx *s)
01022 {
01023     int i;
01024     for (i = 0; i < s->channels_for_cur_subframe; i++) {
01025         int c = s->channel_indexes_for_cur_subframe[i];
01026         float* window;
01027         int winlen = s->channel[c].prev_block_len;
01028         float* start = s->channel[c].coeffs - (winlen >> 1);
01029 
01030         if (s->subframe_len < winlen) {
01031             start += (winlen - s->subframe_len) >> 1;
01032             winlen = s->subframe_len;
01033         }
01034 
01035         window = s->windows[av_log2(winlen) - WMAPRO_BLOCK_MIN_BITS];
01036 
01037         winlen >>= 1;
01038 
01039         s->dsp.vector_fmul_window(start, start, start + winlen,
01040                                   window, winlen);
01041 
01042         s->channel[c].prev_block_len = s->subframe_len;
01043     }
01044 }
01045 
01051 static int decode_subframe(WMAProDecodeCtx *s)
01052 {
01053     int offset = s->samples_per_frame;
01054     int subframe_len = s->samples_per_frame;
01055     int i;
01056     int total_samples   = s->samples_per_frame * s->num_channels;
01057     int transmit_coeffs = 0;
01058     int cur_subwoofer_cutoff;
01059 
01060     s->subframe_offset = get_bits_count(&s->gb);
01061 
01066     for (i = 0; i < s->num_channels; i++) {
01067         s->channel[i].grouped = 0;
01068         if (offset > s->channel[i].decoded_samples) {
01069             offset = s->channel[i].decoded_samples;
01070             subframe_len =
01071                 s->channel[i].subframe_len[s->channel[i].cur_subframe];
01072         }
01073     }
01074 
01075     av_dlog(s->avctx,
01076             "processing subframe with offset %i len %i\n", offset, subframe_len);
01077 
01079     s->channels_for_cur_subframe = 0;
01080     for (i = 0; i < s->num_channels; i++) {
01081         const int cur_subframe = s->channel[i].cur_subframe;
01083         total_samples -= s->channel[i].decoded_samples;
01084 
01086         if (offset == s->channel[i].decoded_samples &&
01087             subframe_len == s->channel[i].subframe_len[cur_subframe]) {
01088             total_samples -= s->channel[i].subframe_len[cur_subframe];
01089             s->channel[i].decoded_samples +=
01090                 s->channel[i].subframe_len[cur_subframe];
01091             s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
01092             ++s->channels_for_cur_subframe;
01093         }
01094     }
01095 
01098     if (!total_samples)
01099         s->parsed_all_subframes = 1;
01100 
01101 
01102     av_dlog(s->avctx, "subframe is part of %i channels\n",
01103             s->channels_for_cur_subframe);
01104 
01106     s->table_idx         = av_log2(s->samples_per_frame/subframe_len);
01107     s->num_bands         = s->num_sfb[s->table_idx];
01108     s->cur_sfb_offsets   = s->sfb_offsets[s->table_idx];
01109     cur_subwoofer_cutoff = s->subwoofer_cutoffs[s->table_idx];
01110 
01112     for (i = 0; i < s->channels_for_cur_subframe; i++) {
01113         int c = s->channel_indexes_for_cur_subframe[i];
01114 
01115         s->channel[c].coeffs = &s->channel[c].out[(s->samples_per_frame >> 1)
01116                                                   + offset];
01117     }
01118 
01119     s->subframe_len = subframe_len;
01120     s->esc_len = av_log2(s->subframe_len - 1) + 1;
01121 
01123     if (get_bits1(&s->gb)) {
01124         int num_fill_bits;
01125         if (!(num_fill_bits = get_bits(&s->gb, 2))) {
01126             int len = get_bits(&s->gb, 4);
01127             num_fill_bits = get_bits(&s->gb, len) + 1;
01128         }
01129 
01130         if (num_fill_bits >= 0) {
01131             if (get_bits_count(&s->gb) + num_fill_bits > s->num_saved_bits) {
01132                 av_log(s->avctx, AV_LOG_ERROR, "invalid number of fill bits\n");
01133                 return AVERROR_INVALIDDATA;
01134             }
01135 
01136             skip_bits_long(&s->gb, num_fill_bits);
01137         }
01138     }
01139 
01141     if (get_bits1(&s->gb)) {
01142         av_log_ask_for_sample(s->avctx, "reserved bit set\n");
01143         return AVERROR_INVALIDDATA;
01144     }
01145 
01146 
01147     if (decode_channel_transform(s) < 0)
01148         return AVERROR_INVALIDDATA;
01149 
01150 
01151     for (i = 0; i < s->channels_for_cur_subframe; i++) {
01152         int c = s->channel_indexes_for_cur_subframe[i];
01153         if ((s->channel[c].transmit_coefs = get_bits1(&s->gb)))
01154             transmit_coeffs = 1;
01155     }
01156 
01157     if (transmit_coeffs) {
01158         int step;
01159         int quant_step = 90 * s->bits_per_sample >> 4;
01160 
01162         if ((s->transmit_num_vec_coeffs = get_bits1(&s->gb))) {
01163             int num_bits = av_log2((s->subframe_len + 3)/4) + 1;
01164             for (i = 0; i < s->channels_for_cur_subframe; i++) {
01165                 int c = s->channel_indexes_for_cur_subframe[i];
01166                 int num_vec_coeffs = get_bits(&s->gb, num_bits) << 2;
01167                 if (num_vec_coeffs > WMAPRO_BLOCK_MAX_SIZE) {
01168                     av_log(s->avctx, AV_LOG_ERROR, "num_vec_coeffs %d is too large\n", num_vec_coeffs);
01169                     return AVERROR_INVALIDDATA;
01170                 }
01171                 s->channel[c].num_vec_coeffs = num_vec_coeffs;
01172             }
01173         } else {
01174             for (i = 0; i < s->channels_for_cur_subframe; i++) {
01175                 int c = s->channel_indexes_for_cur_subframe[i];
01176                 s->channel[c].num_vec_coeffs = s->subframe_len;
01177             }
01178         }
01180         step = get_sbits(&s->gb, 6);
01181         quant_step += step;
01182         if (step == -32 || step == 31) {
01183             const int sign = (step == 31) - 1;
01184             int quant = 0;
01185             while (get_bits_count(&s->gb) + 5 < s->num_saved_bits &&
01186                    (step = get_bits(&s->gb, 5)) == 31) {
01187                 quant += 31;
01188             }
01189             quant_step += ((quant + step) ^ sign) - sign;
01190         }
01191         if (quant_step < 0) {
01192             av_log(s->avctx, AV_LOG_DEBUG, "negative quant step\n");
01193         }
01194 
01197         if (s->channels_for_cur_subframe == 1) {
01198             s->channel[s->channel_indexes_for_cur_subframe[0]].quant_step = quant_step;
01199         } else {
01200             int modifier_len = get_bits(&s->gb, 3);
01201             for (i = 0; i < s->channels_for_cur_subframe; i++) {
01202                 int c = s->channel_indexes_for_cur_subframe[i];
01203                 s->channel[c].quant_step = quant_step;
01204                 if (get_bits1(&s->gb)) {
01205                     if (modifier_len) {
01206                         s->channel[c].quant_step += get_bits(&s->gb, modifier_len) + 1;
01207                     } else
01208                         ++s->channel[c].quant_step;
01209                 }
01210             }
01211         }
01212 
01214         if (decode_scale_factors(s) < 0)
01215             return AVERROR_INVALIDDATA;
01216     }
01217 
01218     av_dlog(s->avctx, "BITSTREAM: subframe header length was %i\n",
01219             get_bits_count(&s->gb) - s->subframe_offset);
01220 
01222     for (i = 0; i < s->channels_for_cur_subframe; i++) {
01223         int c = s->channel_indexes_for_cur_subframe[i];
01224         if (s->channel[c].transmit_coefs &&
01225             get_bits_count(&s->gb) < s->num_saved_bits) {
01226             decode_coeffs(s, c);
01227         } else
01228             memset(s->channel[c].coeffs, 0,
01229                    sizeof(*s->channel[c].coeffs) * subframe_len);
01230     }
01231 
01232     av_dlog(s->avctx, "BITSTREAM: subframe length was %i\n",
01233             get_bits_count(&s->gb) - s->subframe_offset);
01234 
01235     if (transmit_coeffs) {
01236         FFTContext *mdct = &s->mdct_ctx[av_log2(subframe_len) - WMAPRO_BLOCK_MIN_BITS];
01238         inverse_channel_transform(s);
01239         for (i = 0; i < s->channels_for_cur_subframe; i++) {
01240             int c = s->channel_indexes_for_cur_subframe[i];
01241             const int* sf = s->channel[c].scale_factors;
01242             int b;
01243 
01244             if (c == s->lfe_channel)
01245                 memset(&s->tmp[cur_subwoofer_cutoff], 0, sizeof(*s->tmp) *
01246                        (subframe_len - cur_subwoofer_cutoff));
01247 
01249             for (b = 0; b < s->num_bands; b++) {
01250                 const int end = FFMIN(s->cur_sfb_offsets[b+1], s->subframe_len);
01251                 const int exp = s->channel[c].quant_step -
01252                             (s->channel[c].max_scale_factor - *sf++) *
01253                             s->channel[c].scale_factor_step;
01254                 const float quant = pow(10.0, exp / 20.0);
01255                 int start = s->cur_sfb_offsets[b];
01256                 s->dsp.vector_fmul_scalar(s->tmp + start,
01257                                           s->channel[c].coeffs + start,
01258                                           quant, end - start);
01259             }
01260 
01262             mdct->imdct_half(mdct, s->channel[c].coeffs, s->tmp);
01263         }
01264     }
01265 
01267     wmapro_window(s);
01268 
01270     for (i = 0; i < s->channels_for_cur_subframe; i++) {
01271         int c = s->channel_indexes_for_cur_subframe[i];
01272         if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
01273             av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
01274             return AVERROR_INVALIDDATA;
01275         }
01276         ++s->channel[c].cur_subframe;
01277     }
01278 
01279     return 0;
01280 }
01281 
01288 static int decode_frame(WMAProDecodeCtx *s)
01289 {
01290     GetBitContext* gb = &s->gb;
01291     int more_frames = 0;
01292     int len = 0;
01293     int i;
01294 
01296     if (s->num_channels * s->samples_per_frame > s->samples_end - s->samples) {
01298         av_log(s->avctx, AV_LOG_ERROR,
01299                "not enough space for the output samples\n");
01300         s->packet_loss = 1;
01301         return 0;
01302     }
01303 
01305     if (s->len_prefix)
01306         len = get_bits(gb, s->log2_frame_size);
01307 
01308     av_dlog(s->avctx, "decoding frame with length %x\n", len);
01309 
01311     if (decode_tilehdr(s)) {
01312         s->packet_loss = 1;
01313         return 0;
01314     }
01315 
01317     if (s->num_channels > 1 && get_bits1(gb)) {
01318         if (get_bits1(gb)) {
01319             for (i = 0; i < s->num_channels * s->num_channels; i++)
01320                 skip_bits(gb, 4);
01321         }
01322     }
01323 
01325     if (s->dynamic_range_compression) {
01326         s->drc_gain = get_bits(gb, 8);
01327         av_dlog(s->avctx, "drc_gain %i\n", s->drc_gain);
01328     }
01329 
01332     if (get_bits1(gb)) {
01333         int av_unused skip;
01334 
01336         if (get_bits1(gb)) {
01337             skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
01338             av_dlog(s->avctx, "start skip: %i\n", skip);
01339         }
01340 
01342         if (get_bits1(gb)) {
01343             skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
01344             av_dlog(s->avctx, "end skip: %i\n", skip);
01345         }
01346 
01347     }
01348 
01349     av_dlog(s->avctx, "BITSTREAM: frame header length was %i\n",
01350             get_bits_count(gb) - s->frame_offset);
01351 
01353     s->parsed_all_subframes = 0;
01354     for (i = 0; i < s->num_channels; i++) {
01355         s->channel[i].decoded_samples = 0;
01356         s->channel[i].cur_subframe    = 0;
01357         s->channel[i].reuse_sf        = 0;
01358     }
01359 
01361     while (!s->parsed_all_subframes) {
01362         if (decode_subframe(s) < 0) {
01363             s->packet_loss = 1;
01364             return 0;
01365         }
01366     }
01367 
01369     for (i = 0; i < s->num_channels; i++) {
01370         float* ptr  = s->samples + i;
01371         int incr = s->num_channels;
01372         float* iptr = s->channel[i].out;
01373         float* iend = iptr + s->samples_per_frame;
01374 
01375         // FIXME should create/use a DSP function here
01376         while (iptr < iend) {
01377             *ptr = *iptr++;
01378             ptr += incr;
01379         }
01380 
01382         memcpy(&s->channel[i].out[0],
01383                &s->channel[i].out[s->samples_per_frame],
01384                s->samples_per_frame * sizeof(*s->channel[i].out) >> 1);
01385     }
01386 
01387     if (s->skip_frame) {
01388         s->skip_frame = 0;
01389     } else
01390         s->samples += s->num_channels * s->samples_per_frame;
01391 
01392     if (s->len_prefix) {
01393         if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
01395             av_log(s->avctx, AV_LOG_ERROR,
01396                    "frame[%i] would have to skip %i bits\n", s->frame_num,
01397                    len - (get_bits_count(gb) - s->frame_offset) - 1);
01398             s->packet_loss = 1;
01399             return 0;
01400         }
01401 
01403         skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
01404     } else {
01405         while (get_bits_count(gb) < s->num_saved_bits && get_bits1(gb) == 0) {
01406         }
01407     }
01408 
01410     more_frames = get_bits1(gb);
01411 
01412     ++s->frame_num;
01413     return more_frames;
01414 }
01415 
01422 static int remaining_bits(WMAProDecodeCtx *s, GetBitContext *gb)
01423 {
01424     return s->buf_bit_size - get_bits_count(gb);
01425 }
01426 
01434 static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len,
01435                       int append)
01436 {
01437     int buflen;
01438 
01443     if (!append) {
01444         s->frame_offset = get_bits_count(gb) & 7;
01445         s->num_saved_bits = s->frame_offset;
01446         init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
01447     }
01448 
01449     buflen = (put_bits_count(&s->pb) + len + 8) >> 3;
01450 
01451     if (len <= 0 || buflen > MAX_FRAMESIZE) {
01452         av_log_ask_for_sample(s->avctx, "input buffer too small\n");
01453         s->packet_loss = 1;
01454         return;
01455     }
01456 
01457     s->num_saved_bits += len;
01458     if (!append) {
01459         ff_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
01460                      s->num_saved_bits);
01461     } else {
01462         int align = 8 - (get_bits_count(gb) & 7);
01463         align = FFMIN(align, len);
01464         put_bits(&s->pb, align, get_bits(gb, align));
01465         len -= align;
01466         ff_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
01467     }
01468     skip_bits_long(gb, len);
01469 
01470     {
01471         PutBitContext tmp = s->pb;
01472         flush_put_bits(&tmp);
01473     }
01474 
01475     init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
01476     skip_bits(&s->gb, s->frame_offset);
01477 }
01478 
01487 static int decode_packet(AVCodecContext *avctx,
01488                          void *data, int *data_size, AVPacket* avpkt)
01489 {
01490     WMAProDecodeCtx *s = avctx->priv_data;
01491     GetBitContext* gb  = &s->pgb;
01492     const uint8_t* buf = avpkt->data;
01493     int buf_size       = avpkt->size;
01494     int num_bits_prev_frame;
01495     int packet_sequence_number;
01496 
01497     s->samples       = data;
01498     s->samples_end   = (float*)((int8_t*)data + *data_size);
01499     *data_size = 0;
01500 
01501     if (s->packet_done || s->packet_loss) {
01502         s->packet_done = 0;
01503 
01505         if (buf_size < avctx->block_align)
01506             return 0;
01507 
01508         s->next_packet_start = buf_size - avctx->block_align;
01509         buf_size = avctx->block_align;
01510         s->buf_bit_size = buf_size << 3;
01511 
01513         init_get_bits(gb, buf, s->buf_bit_size);
01514         packet_sequence_number = get_bits(gb, 4);
01515         skip_bits(gb, 2);
01516 
01518         num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
01519         av_dlog(avctx, "packet[%d]: nbpf %x\n", avctx->frame_number,
01520                 num_bits_prev_frame);
01521 
01523         if (!s->packet_loss &&
01524             ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
01525             s->packet_loss = 1;
01526             av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n",
01527                    s->packet_sequence_number, packet_sequence_number);
01528         }
01529         s->packet_sequence_number = packet_sequence_number;
01530 
01531         if (num_bits_prev_frame > 0) {
01532             int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
01533             if (num_bits_prev_frame >= remaining_packet_bits) {
01534                 num_bits_prev_frame = remaining_packet_bits;
01535                 s->packet_done = 1;
01536             }
01537 
01540             save_bits(s, gb, num_bits_prev_frame, 1);
01541             av_dlog(avctx, "accumulated %x bits of frame data\n",
01542                     s->num_saved_bits - s->frame_offset);
01543 
01545             if (!s->packet_loss)
01546                 decode_frame(s);
01547         } else if (s->num_saved_bits - s->frame_offset) {
01548             av_dlog(avctx, "ignoring %x previously saved bits\n",
01549                     s->num_saved_bits - s->frame_offset);
01550         }
01551 
01552         if (s->packet_loss) {
01556             s->num_saved_bits = 0;
01557             s->packet_loss = 0;
01558         }
01559 
01560     } else {
01561         int frame_size;
01562         s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
01563         init_get_bits(gb, avpkt->data, s->buf_bit_size);
01564         skip_bits(gb, s->packet_offset);
01565         if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
01566             (frame_size = show_bits(gb, s->log2_frame_size)) &&
01567             frame_size <= remaining_bits(s, gb)) {
01568             save_bits(s, gb, frame_size, 0);
01569             s->packet_done = !decode_frame(s);
01570         } else if (!s->len_prefix
01571                    && s->num_saved_bits > get_bits_count(&s->gb)) {
01579             s->packet_done = !decode_frame(s);
01580         } else
01581             s->packet_done = 1;
01582     }
01583 
01584     if (s->packet_done && !s->packet_loss &&
01585         remaining_bits(s, gb) > 0) {
01588         save_bits(s, gb, remaining_bits(s, gb), 0);
01589     }
01590 
01591     *data_size = (int8_t *)s->samples - (int8_t *)data;
01592     s->packet_offset = get_bits_count(gb) & 7;
01593 
01594     return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
01595 }
01596 
01601 static void flush(AVCodecContext *avctx)
01602 {
01603     WMAProDecodeCtx *s = avctx->priv_data;
01604     int i;
01607     for (i = 0; i < s->num_channels; i++)
01608         memset(s->channel[i].out, 0, s->samples_per_frame *
01609                sizeof(*s->channel[i].out));
01610     s->packet_loss = 1;
01611 }
01612 
01613 
01617 AVCodec ff_wmapro_decoder = {
01618     "wmapro",
01619     AVMEDIA_TYPE_AUDIO,
01620     CODEC_ID_WMAPRO,
01621     sizeof(WMAProDecodeCtx),
01622     decode_init,
01623     NULL,
01624     decode_end,
01625     decode_packet,
01626     .capabilities = CODEC_CAP_SUBFRAMES,
01627     .flush= flush,
01628     .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 9 Professional"),
01629 };

Generated on Fri Feb 22 2013 07:24:29 for FFmpeg by  doxygen 1.7.1