libavcodec/wmalosslessdec.c
Go to the documentation of this file.
00001 /*
00002  * Wmall compatible decoder
00003  * Copyright (c) 2007 Baptiste Coudurier, Benjamin Larsson, Ulion
00004  * Copyright (c) 2008 - 2011 Sascha Sommer, Benjamin Larsson
00005  * Copyright (c) 2011 Andreas Ă–man
00006  *
00007  * This file is part of FFmpeg.
00008  *
00009  * FFmpeg is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  * FFmpeg is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with FFmpeg; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00022  */
00023 
00090 #include "avcodec.h"
00091 #include "internal.h"
00092 #include "get_bits.h"
00093 #include "put_bits.h"
00094 #include "dsputil.h"
00095 #include "wma.h"
00096 
00098 #define WMALL_MAX_CHANNELS    8                             ///< max number of handled channels
00099 #define MAX_SUBFRAMES  32                                    ///< max number of subframes per channel
00100 #define MAX_BANDS      29                                    ///< max number of scale factor bands
00101 #define MAX_FRAMESIZE  32768                                 ///< maximum compressed frame size
00102 
00103 #define WMALL_BLOCK_MIN_BITS  6                                           ///< log2 of min block size
00104 #define WMALL_BLOCK_MAX_BITS 12                                           ///< log2 of max block size
00105 #define WMALL_BLOCK_MAX_SIZE (1 << WMALL_BLOCK_MAX_BITS)                 ///< maximum block size
00106 #define WMALL_BLOCK_SIZES    (WMALL_BLOCK_MAX_BITS - WMALL_BLOCK_MIN_BITS + 1) ///< possible block sizes
00107 
00108 
00109 #define VLCBITS            9
00110 #define SCALEVLCBITS       8
00111 #define VEC4MAXDEPTH    ((HUFF_VEC4_MAXBITS+VLCBITS-1)/VLCBITS)
00112 #define VEC2MAXDEPTH    ((HUFF_VEC2_MAXBITS+VLCBITS-1)/VLCBITS)
00113 #define VEC1MAXDEPTH    ((HUFF_VEC1_MAXBITS+VLCBITS-1)/VLCBITS)
00114 #define SCALEMAXDEPTH   ((HUFF_SCALE_MAXBITS+SCALEVLCBITS-1)/SCALEVLCBITS)
00115 #define SCALERLMAXDEPTH ((HUFF_SCALE_RL_MAXBITS+VLCBITS-1)/VLCBITS)
00116 
00117 static float            sin64[33];        
00118 
00122 typedef struct {
00123     int16_t  prev_block_len;                          
00124     uint8_t  transmit_coefs;
00125     uint8_t  num_subframes;
00126     uint16_t subframe_len[MAX_SUBFRAMES];             
00127     uint16_t subframe_offset[MAX_SUBFRAMES];          
00128     uint8_t  cur_subframe;                            
00129     uint16_t decoded_samples;                         
00130     uint8_t  grouped;                                 
00131     int      quant_step;                              
00132     int8_t   reuse_sf;                                
00133     int8_t   scale_factor_step;                       
00134     int      max_scale_factor;                        
00135     int      saved_scale_factors[2][MAX_BANDS];       
00136     int8_t   scale_factor_idx;                        
00137     int*     scale_factors;                           
00138     uint8_t  table_idx;                               
00139     float*   coeffs;                                  
00140     uint16_t num_vec_coeffs;                          
00141     DECLARE_ALIGNED(16, float, out)[WMALL_BLOCK_MAX_SIZE + WMALL_BLOCK_MAX_SIZE / 2]; 
00142     int      transient_counter;                       
00143 } WmallChannelCtx;
00144 
00148 typedef struct {
00149     uint8_t num_channels;                                     
00150     int8_t  transform;                                        
00151     int8_t  transform_band[MAX_BANDS];                        
00152     float   decorrelation_matrix[WMALL_MAX_CHANNELS*WMALL_MAX_CHANNELS];
00153     float*  channel_data[WMALL_MAX_CHANNELS];                
00154 } WmallChannelGrp;
00155 
00159 typedef struct WmallDecodeCtx {
00160     /* generic decoder variables */
00161     AVCodecContext*  avctx;                         
00162     DSPContext       dsp;                           
00163     uint8_t          frame_data[MAX_FRAMESIZE +
00164                       FF_INPUT_BUFFER_PADDING_SIZE];
00165     PutBitContext    pb;                            
00166     FFTContext       mdct_ctx[WMALL_BLOCK_SIZES];  
00167     DECLARE_ALIGNED(16, float, tmp)[WMALL_BLOCK_MAX_SIZE]; 
00168     float*           windows[WMALL_BLOCK_SIZES];   
00169 
00170     /* frame size dependent frame information (set during initialization) */
00171     uint32_t         decode_flags;                  
00172     uint8_t          len_prefix;                    
00173     uint8_t          dynamic_range_compression;     
00174     uint8_t          bits_per_sample;               
00175     uint16_t         samples_per_frame;             
00176     uint16_t         log2_frame_size;
00177     int8_t           num_channels;                  
00178     int8_t           lfe_channel;                   
00179     uint8_t          max_num_subframes;
00180     uint8_t          subframe_len_bits;             
00181     uint8_t          max_subframe_len_bit;          
00182     uint16_t         min_samples_per_subframe;
00183     int8_t           num_sfb[WMALL_BLOCK_SIZES];   
00184     int16_t          sfb_offsets[WMALL_BLOCK_SIZES][MAX_BANDS];                    
00185     int8_t           sf_offsets[WMALL_BLOCK_SIZES][WMALL_BLOCK_SIZES][MAX_BANDS]; 
00186     int16_t          subwoofer_cutoffs[WMALL_BLOCK_SIZES]; 
00187 
00188     /* packet decode state */
00189     GetBitContext    pgb;                           
00190     int              next_packet_start;             
00191     uint8_t          packet_offset;                 
00192     uint8_t          packet_sequence_number;        
00193     int              num_saved_bits;                
00194     int              frame_offset;                  
00195     int              subframe_offset;               
00196     uint8_t          packet_loss;                   
00197     uint8_t          packet_done;                   
00198 
00199     /* frame decode state */
00200     uint32_t         frame_num;                     
00201     GetBitContext    gb;                            
00202     int              buf_bit_size;                  
00203     int16_t*         samples_16;                    
00204     int16_t*         samples_16_end;                
00205     int16_t*         samples_32;                    
00206     int16_t*         samples_32_end;                
00207     uint8_t          drc_gain;                      
00208     int8_t           skip_frame;                    
00209     int8_t           parsed_all_subframes;          
00210 
00211     /* subframe/block decode state */
00212     int16_t          subframe_len;                  
00213     int8_t           channels_for_cur_subframe;     
00214     int8_t           channel_indexes_for_cur_subframe[WMALL_MAX_CHANNELS];
00215     int8_t           num_bands;                     
00216     int8_t           transmit_num_vec_coeffs;       
00217     int16_t*         cur_sfb_offsets;               
00218     uint8_t          table_idx;                     
00219     int8_t           esc_len;                       
00220 
00221     uint8_t          num_chgroups;                  
00222     WmallChannelGrp chgroup[WMALL_MAX_CHANNELS];    
00223 
00224     WmallChannelCtx channel[WMALL_MAX_CHANNELS];    
00225 
00226     // WMA lossless
00227 
00228     uint8_t do_arith_coding;
00229     uint8_t do_ac_filter;
00230     uint8_t do_inter_ch_decorr;
00231     uint8_t do_mclms;
00232     uint8_t do_lpc;
00233 
00234     int8_t acfilter_order;
00235     int8_t acfilter_scaling;
00236     int64_t acfilter_coeffs[16];
00237     int acfilter_prevvalues[2][16];
00238 
00239     int8_t mclms_order;
00240     int8_t mclms_scaling;
00241     int16_t mclms_coeffs[128];
00242     int16_t mclms_coeffs_cur[4];
00243     int16_t mclms_prevvalues[64];   // FIXME: should be 32-bit / 16-bit depending on bit-depth
00244     int16_t mclms_updates[64];
00245     int mclms_recent;
00246 
00247     int movave_scaling;
00248     int quant_stepsize;
00249 
00250     struct {
00251         int order;
00252         int scaling;
00253         int coefsend;
00254         int bitsend;
00255         int16_t coefs[256];
00256     int16_t lms_prevvalues[512];    // FIXME: see above
00257     int16_t lms_updates[512];   // and here too
00258     int recent;
00259     } cdlms[2][9];              /* XXX: Here, 2 is the max. no. of channels allowed,
00260                                         9 is the maximum no. of filters per channel.
00261                                         Question is, why 2 if WMALL_MAX_CHANNELS == 8 */
00262 
00263 
00264     int cdlms_ttl[2];
00265 
00266     int bV3RTM;
00267 
00268     int is_channel_coded[2];    // XXX: same question as above applies here too (and below)
00269     int update_speed[2];
00270 
00271     int transient[2];
00272     int transient_pos[2];
00273     int seekable_tile;
00274 
00275     int ave_sum[2];
00276 
00277     int channel_residues[2][2048];
00278 
00279 
00280     int lpc_coefs[2][40];
00281     int lpc_order;
00282     int lpc_scaling;
00283     int lpc_intbits;
00284 
00285     int channel_coeffs[2][2048]; // FIXME: should be 32-bit / 16-bit depending on bit-depth
00286 
00287 } WmallDecodeCtx;
00288 
00289 
00290 #undef dprintf
00291 #define dprintf(pctx, ...) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__)
00292 
00293 
00294 static int num_logged_tiles = 0;
00295 static int num_logged_subframes = 0;
00296 static int num_lms_update_call = 0;
00297 
00302 static void av_cold dump_context(WmallDecodeCtx *s)
00303 {
00304 #define PRINT(a, b)     av_log(s->avctx, AV_LOG_DEBUG, " %s = %d\n", a, b);
00305 #define PRINT_HEX(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %x\n", a, b);
00306 
00307     PRINT("ed sample bit depth", s->bits_per_sample);
00308     PRINT_HEX("ed decode flags", s->decode_flags);
00309     PRINT("samples per frame",   s->samples_per_frame);
00310     PRINT("log2 frame size",     s->log2_frame_size);
00311     PRINT("max num subframes",   s->max_num_subframes);
00312     PRINT("len prefix",          s->len_prefix);
00313     PRINT("num channels",        s->num_channels);
00314 }
00315 
00316 static void dump_int_buffer(uint8_t *buffer, int size, int length, int delimiter)
00317 {
00318     int i;
00319 
00320     for (i=0 ; i<length ; i++) {
00321         if (!(i%delimiter))
00322             av_log(0, 0, "\n[%d] ", i);
00323         av_log(0, 0, "%d, ", *(int16_t *)(buffer + i * size));
00324     }
00325     av_log(0, 0, "\n");
00326 }
00327 
00333 static av_cold int decode_end(AVCodecContext *avctx)
00334 {
00335     WmallDecodeCtx *s = avctx->priv_data;
00336     int i;
00337 
00338     for (i = 0; i < WMALL_BLOCK_SIZES; i++)
00339         ff_mdct_end(&s->mdct_ctx[i]);
00340 
00341     return 0;
00342 }
00343 
00349 static av_cold int decode_init(AVCodecContext *avctx)
00350 {
00351     WmallDecodeCtx *s = avctx->priv_data;
00352     uint8_t *edata_ptr = avctx->extradata;
00353     unsigned int channel_mask;
00354     int i;
00355     int log2_max_num_subframes;
00356     int num_possible_block_sizes;
00357 
00358     s->avctx = avctx;
00359     dsputil_init(&s->dsp, avctx);
00360     init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
00361 
00362     if (avctx->extradata_size >= 18) {
00363         s->decode_flags    = AV_RL16(edata_ptr+14);
00364         channel_mask       = AV_RL32(edata_ptr+2);
00365         s->bits_per_sample = AV_RL16(edata_ptr);
00366         if (s->bits_per_sample == 16)
00367             avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00368         else if (s->bits_per_sample == 24)
00369             avctx->sample_fmt = AV_SAMPLE_FMT_S32;
00370         else {
00371             av_log(avctx, AV_LOG_ERROR, "Unknown bit-depth: %d\n",
00372                    s->bits_per_sample);
00373             return AVERROR_INVALIDDATA;
00374         }
00376         for (i = 0; i < avctx->extradata_size; i++)
00377             dprintf(avctx, "[%x] ", avctx->extradata[i]);
00378         dprintf(avctx, "\n");
00379 
00380     } else {
00381         av_log_ask_for_sample(avctx, "Unknown extradata size\n");
00382         return AVERROR_INVALIDDATA;
00383     }
00384 
00386     s->log2_frame_size = av_log2(avctx->block_align) + 4;
00387 
00389     s->skip_frame  = 1; /* skip first frame */
00390     s->packet_loss = 1;
00391     s->len_prefix  = (s->decode_flags & 0x40);
00392 
00394     s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(avctx->sample_rate,
00395                                                           3, s->decode_flags);
00396 
00398     for (i = 0; i < avctx->channels; i++)
00399         s->channel[i].prev_block_len = s->samples_per_frame;
00400 
00402     log2_max_num_subframes  = ((s->decode_flags & 0x38) >> 3);
00403     s->max_num_subframes    = 1 << log2_max_num_subframes;
00404     s->max_subframe_len_bit = 0;
00405     s->subframe_len_bits    = av_log2(log2_max_num_subframes) + 1;
00406 
00407     num_possible_block_sizes     = log2_max_num_subframes + 1;
00408     s->min_samples_per_subframe  = s->samples_per_frame / s->max_num_subframes;
00409     s->dynamic_range_compression = (s->decode_flags & 0x80);
00410 
00411     s->bV3RTM = s->decode_flags & 0x100;
00412 
00413     if (s->max_num_subframes > MAX_SUBFRAMES) {
00414         av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %i\n",
00415                s->max_num_subframes);
00416         return AVERROR_INVALIDDATA;
00417     }
00418 
00419     s->num_channels = avctx->channels;
00420 
00422     s->lfe_channel = -1;
00423 
00424     if (channel_mask & 8) {
00425         unsigned int mask;
00426         for (mask = 1; mask < 16; mask <<= 1) {
00427             if (channel_mask & mask)
00428                 ++s->lfe_channel;
00429         }
00430     }
00431 
00432     if (s->num_channels < 0) {
00433         av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n", s->num_channels);
00434         return AVERROR_INVALIDDATA;
00435     } else if (s->num_channels > WMALL_MAX_CHANNELS) {
00436         av_log_ask_for_sample(avctx, "unsupported number of channels\n");
00437         return AVERROR_PATCHWELCOME;
00438     }
00439 
00440     avctx->channel_layout = channel_mask;
00441     return 0;
00442 }
00443 
00450 static int decode_subframe_length(WmallDecodeCtx *s, int offset)
00451 {
00452     int frame_len_ratio;
00453     int subframe_len, len;
00454 
00456     if (offset == s->samples_per_frame - s->min_samples_per_subframe)
00457         return s->min_samples_per_subframe;
00458 
00459     len = av_log2(s->max_num_subframes - 1) + 1;
00460     frame_len_ratio = get_bits(&s->gb, len);
00461 
00462     subframe_len = s->min_samples_per_subframe * (frame_len_ratio + 1);
00463 
00465     if (subframe_len < s->min_samples_per_subframe ||
00466         subframe_len > s->samples_per_frame) {
00467         av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
00468                subframe_len);
00469         return AVERROR_INVALIDDATA;
00470     }
00471     return subframe_len;
00472 }
00473 
00494 static int decode_tilehdr(WmallDecodeCtx *s)
00495 {
00496     uint16_t num_samples[WMALL_MAX_CHANNELS];        
00497     uint8_t  contains_subframe[WMALL_MAX_CHANNELS];  
00498     int channels_for_cur_subframe = s->num_channels;  
00499     int fixed_channel_layout = 0;                     
00500     int min_channel_len = 0;                          
00501     int c;
00502 
00503     /* Should never consume more than 3073 bits (256 iterations for the
00504      * while loop when always the minimum amount of 128 samples is substracted
00505      * from missing samples in the 8 channel case).
00506      * 1 + BLOCK_MAX_SIZE * MAX_CHANNELS / BLOCK_MIN_SIZE * (MAX_CHANNELS  + 4)
00507      */
00508 
00510     for (c = 0; c < s->num_channels; c++)
00511         s->channel[c].num_subframes = 0;
00512 
00513     memset(num_samples, 0, sizeof(num_samples));
00514 
00515     if (s->max_num_subframes == 1 || get_bits1(&s->gb))
00516         fixed_channel_layout = 1;
00517 
00519     do {
00520         int subframe_len;
00521 
00523         for (c = 0; c < s->num_channels; c++) {
00524             if (num_samples[c] == min_channel_len) {
00525                 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
00526                     (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe)) {
00527                     contains_subframe[c] = 1;
00528                 }
00529                 else {
00530                     contains_subframe[c] = get_bits1(&s->gb);
00531                 }
00532             } else
00533                 contains_subframe[c] = 0;
00534         }
00535 
00537         if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
00538             return AVERROR_INVALIDDATA;
00540         min_channel_len += subframe_len;
00541         for (c = 0; c < s->num_channels; c++) {
00542             WmallChannelCtx* chan = &s->channel[c];
00543 
00544             if (contains_subframe[c]) {
00545                 if (chan->num_subframes >= MAX_SUBFRAMES) {
00546                     av_log(s->avctx, AV_LOG_ERROR,
00547                            "broken frame: num subframes > 31\n");
00548                     return AVERROR_INVALIDDATA;
00549                 }
00550                 chan->subframe_len[chan->num_subframes] = subframe_len;
00551                 num_samples[c] += subframe_len;
00552                 ++chan->num_subframes;
00553                 if (num_samples[c] > s->samples_per_frame) {
00554                     av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
00555                            "channel len(%d) > samples_per_frame(%d)\n",
00556                            num_samples[c], s->samples_per_frame);
00557                     return AVERROR_INVALIDDATA;
00558                 }
00559             } else if (num_samples[c] <= min_channel_len) {
00560                 if (num_samples[c] < min_channel_len) {
00561                     channels_for_cur_subframe = 0;
00562                     min_channel_len = num_samples[c];
00563                 }
00564                 ++channels_for_cur_subframe;
00565             }
00566         }
00567     } while (min_channel_len < s->samples_per_frame);
00568 
00569     for (c = 0; c < s->num_channels; c++) {
00570         int i;
00571         int offset = 0;
00572         for (i = 0; i < s->channel[c].num_subframes; i++) {
00573             s->channel[c].subframe_offset[i] = offset;
00574             offset += s->channel[c].subframe_len[i];
00575         }
00576     }
00577 
00578     return 0;
00579 }
00580 
00581 
00582 static int my_log2(unsigned int i)
00583 {
00584     unsigned int iLog2 = 0;
00585     while ((i >> iLog2) > 1)
00586         iLog2++;
00587     return iLog2;
00588 }
00589 
00590 
00594 static void decode_ac_filter(WmallDecodeCtx *s)
00595 {
00596     int i;
00597     s->acfilter_order = get_bits(&s->gb, 4) + 1;
00598     s->acfilter_scaling = get_bits(&s->gb, 4);
00599 
00600     for(i = 0; i < s->acfilter_order; i++) {
00601         s->acfilter_coeffs[i] = get_bits(&s->gb, s->acfilter_scaling) + 1;
00602     }
00603 }
00604 
00605 
00609 static void decode_mclms(WmallDecodeCtx *s)
00610 {
00611     s->mclms_order = (get_bits(&s->gb, 4) + 1) * 2;
00612     s->mclms_scaling = get_bits(&s->gb, 4);
00613     if(get_bits1(&s->gb)) {
00614         // mclms_send_coef
00615         int i;
00616         int send_coef_bits;
00617         int cbits = av_log2(s->mclms_scaling + 1);
00618         assert(cbits == my_log2(s->mclms_scaling + 1));
00619         if(1 << cbits < s->mclms_scaling + 1)
00620             cbits++;
00621 
00622         send_coef_bits = (cbits ? get_bits(&s->gb, cbits) : 0) + 2;
00623 
00624         for(i = 0; i < s->mclms_order * s->num_channels * s->num_channels; i++) {
00625             s->mclms_coeffs[i] = get_bits(&s->gb, send_coef_bits);
00626         }
00627 
00628         for(i = 0; i < s->num_channels; i++) {
00629             int c;
00630             for(c = 0; c < i; c++) {
00631                 s->mclms_coeffs_cur[i * s->num_channels + c] = get_bits(&s->gb, send_coef_bits);
00632             }
00633         }
00634     }
00635 }
00636 
00637 
00641 static void decode_cdlms(WmallDecodeCtx *s)
00642 {
00643     int c, i;
00644     int cdlms_send_coef = get_bits1(&s->gb);
00645 
00646     for(c = 0; c < s->num_channels; c++) {
00647         s->cdlms_ttl[c] = get_bits(&s->gb, 3) + 1;
00648         for(i = 0; i < s->cdlms_ttl[c]; i++) {
00649             s->cdlms[c][i].order = (get_bits(&s->gb, 7) + 1) * 8;
00650         }
00651 
00652         for(i = 0; i < s->cdlms_ttl[c]; i++) {
00653             s->cdlms[c][i].scaling = get_bits(&s->gb, 4);
00654         }
00655 
00656         if(cdlms_send_coef) {
00657             for(i = 0; i < s->cdlms_ttl[c]; i++) {
00658                 int cbits, shift_l, shift_r, j;
00659                 cbits = av_log2(s->cdlms[c][i].order);
00660                 if(1 << cbits < s->cdlms[c][i].order)
00661                     cbits++;
00662                 s->cdlms[c][i].coefsend = get_bits(&s->gb, cbits) + 1;
00663 
00664                 cbits = av_log2(s->cdlms[c][i].scaling + 1);
00665                 if(1 << cbits < s->cdlms[c][i].scaling + 1)
00666                     cbits++;
00667 
00668                 s->cdlms[c][i].bitsend = get_bits(&s->gb, cbits) + 2;
00669                 shift_l = 32 - s->cdlms[c][i].bitsend;
00670                 shift_r = 32 - 2 - s->cdlms[c][i].scaling;
00671                 for(j = 0; j < s->cdlms[c][i].coefsend; j++) {
00672                     s->cdlms[c][i].coefs[j] =
00673                         (get_bits(&s->gb, s->cdlms[c][i].bitsend) << shift_l) >> shift_r;
00674                 }
00675             }
00676         }
00677     }
00678 }
00679 
00683 static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
00684 {
00685     int i = 0;
00686     unsigned int ave_mean;
00687     s->transient[ch] = get_bits1(&s->gb);
00688     if(s->transient[ch]) {
00689             s->transient_pos[ch] = get_bits(&s->gb, av_log2(tile_size));
00690         if (s->transient_pos[ch])
00691                 s->transient[ch] = 0;
00692             s->channel[ch].transient_counter =
00693                 FFMAX(s->channel[ch].transient_counter, s->samples_per_frame / 2);
00694         } else if (s->channel[ch].transient_counter)
00695             s->transient[ch] = 1;
00696 
00697     if(s->seekable_tile) {
00698         ave_mean = get_bits(&s->gb, s->bits_per_sample);
00699         s->ave_sum[ch] = ave_mean << (s->movave_scaling + 1);
00700 //        s->ave_sum[ch] *= 2;
00701     }
00702 
00703     if(s->seekable_tile) {
00704         if(s->do_inter_ch_decorr)
00705             s->channel_residues[ch][0] = get_sbits(&s->gb, s->bits_per_sample + 1);
00706         else
00707             s->channel_residues[ch][0] = get_sbits(&s->gb, s->bits_per_sample);
00708         i++;
00709     }
00710     //av_log(0, 0, "%8d: ", num_logged_tiles++);
00711     for(; i < tile_size; i++) {
00712         int quo = 0, rem, rem_bits, residue;
00713         while(get_bits1(&s->gb))
00714             quo++;
00715         if(quo >= 32)
00716             quo += get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1);
00717 
00718                ave_mean = (s->ave_sum[ch] + (1 << s->movave_scaling)) >> (s->movave_scaling + 1);
00719         rem_bits = av_ceil_log2(ave_mean);
00720         rem = rem_bits ? get_bits(&s->gb, rem_bits) : 0;
00721         residue = (quo << rem_bits) + rem;
00722 
00723         s->ave_sum[ch] = residue + s->ave_sum[ch] - (s->ave_sum[ch] >> s->movave_scaling);
00724 
00725         if(residue & 1)
00726             residue = -(residue >> 1) - 1;
00727         else
00728             residue = residue >> 1;
00729         s->channel_residues[ch][i] = residue;
00730     }
00731     //dump_int_buffer(s->channel_residues[ch], 4, tile_size, 16);
00732 
00733     return 0;
00734 
00735 }
00736 
00737 
00741 static void
00742 decode_lpc(WmallDecodeCtx *s)
00743 {
00744     int ch, i, cbits;
00745     s->lpc_order = get_bits(&s->gb, 5) + 1;
00746     s->lpc_scaling = get_bits(&s->gb, 4);
00747     s->lpc_intbits = get_bits(&s->gb, 3) + 1;
00748     cbits = s->lpc_scaling + s->lpc_intbits;
00749     for(ch = 0; ch < s->num_channels; ch++) {
00750         for(i = 0; i < s->lpc_order; i++) {
00751             s->lpc_coefs[ch][i] = get_sbits(&s->gb, cbits);
00752         }
00753     }
00754 }
00755 
00756 
00757 static void clear_codec_buffers(WmallDecodeCtx *s)
00758 {
00759     int ich, ilms;
00760 
00761     memset(s->acfilter_coeffs    , 0, 16 * sizeof(int));
00762     memset(s->acfilter_prevvalues, 0, 16 * 2 * sizeof(int)); // may be wrong
00763     memset(s->lpc_coefs          , 0, 40 * 2 * sizeof(int));
00764 
00765     memset(s->mclms_coeffs    , 0, 128 * sizeof(int16_t));
00766     memset(s->mclms_coeffs_cur, 0,   4 * sizeof(int16_t));
00767     memset(s->mclms_prevvalues, 0,  64 * sizeof(int));
00768     memset(s->mclms_updates   , 0,  64 * sizeof(int16_t));
00769 
00770     for (ich = 0; ich < s->num_channels; ich++) {
00771         for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++) {
00772             memset(s->cdlms[ich][ilms].coefs         , 0, 256 * sizeof(int16_t));
00773             memset(s->cdlms[ich][ilms].lms_prevvalues, 0, 512 * sizeof(int16_t));
00774             memset(s->cdlms[ich][ilms].lms_updates   , 0, 512 * sizeof(int16_t));
00775         }
00776         s->ave_sum[ich] = 0;
00777     }
00778 }
00779 
00783 static void reset_codec(WmallDecodeCtx *s)
00784 {
00785     int ich, ilms;
00786     s->mclms_recent = s->mclms_order * s->num_channels;
00787     for (ich = 0; ich < s->num_channels; ich++) {
00788         for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++)
00789             s->cdlms[ich][ilms].recent = s->cdlms[ich][ilms].order;
00790         /* first sample of a seekable subframe is considered as the starting of
00791            a transient area which is samples_per_frame samples long */
00792         s->channel[ich].transient_counter = s->samples_per_frame;
00793         s->transient[ich] = 1;
00794         s->transient_pos[ich] = 0;
00795     }
00796 }
00797 
00798 
00799 
00800 static void mclms_update(WmallDecodeCtx *s, int icoef, int *pred)
00801 {
00802     int i, j, ich;
00803     int pred_error;
00804     int order = s->mclms_order;
00805     int num_channels = s->num_channels;
00806     int range = 1 << (s->bits_per_sample - 1);
00807     int bps = s->bits_per_sample > 16 ? 4 : 2; // bytes per sample
00808 
00809     for (ich = 0; ich < num_channels; ich++) {
00810         pred_error = s->channel_residues[ich][icoef] - pred[ich];
00811         if (pred_error > 0) {
00812             for (i = 0; i < order * num_channels; i++)
00813                 s->mclms_coeffs[i + ich * order * num_channels] +=
00814                     s->mclms_updates[s->mclms_recent + i];
00815             for (j = 0; j < ich; j++) {
00816                 if (s->channel_residues[j][icoef] > 0)
00817                     s->mclms_coeffs_cur[ich * num_channels + j] += 1;
00818                 else if (s->channel_residues[j][icoef] < 0)
00819                     s->mclms_coeffs_cur[ich * num_channels + j] -= 1;
00820             }
00821         } else if (pred_error < 0) {
00822             for (i = 0; i < order * num_channels; i++)
00823                 s->mclms_coeffs[i + ich * order * num_channels] -=
00824                     s->mclms_updates[s->mclms_recent + i];
00825             for (j = 0; j < ich; j++) {
00826                 if (s->channel_residues[j][icoef] > 0)
00827                     s->mclms_coeffs_cur[ich * num_channels + j] -= 1;
00828                 else if (s->channel_residues[j][icoef] < 0)
00829                     s->mclms_coeffs_cur[ich * num_channels + j] += 1;
00830             }
00831         }
00832     }
00833 
00834     for (ich = num_channels - 1; ich >= 0; ich--) {
00835         s->mclms_recent--;
00836         s->mclms_prevvalues[s->mclms_recent] = s->channel_residues[ich][icoef];
00837         if (s->channel_residues[ich][icoef] > range - 1)
00838             s->mclms_prevvalues[s->mclms_recent] = range - 1;
00839         else if (s->channel_residues[ich][icoef] < -range)
00840             s->mclms_prevvalues[s->mclms_recent] = -range;
00841 
00842         s->mclms_updates[s->mclms_recent] = 0;
00843         if (s->channel_residues[ich][icoef] > 0)
00844             s->mclms_updates[s->mclms_recent] = 1;
00845         else if (s->channel_residues[ich][icoef] < 0)
00846             s->mclms_updates[s->mclms_recent] = -1;
00847     }
00848 
00849     if (s->mclms_recent == 0) {
00850         memcpy(&s->mclms_prevvalues[order * num_channels],
00851                s->mclms_prevvalues,
00852                bps * order * num_channels);
00853         memcpy(&s->mclms_updates[order * num_channels],
00854                s->mclms_updates,
00855                bps * order * num_channels);
00856         s->mclms_recent = num_channels * order;
00857     }
00858 }
00859 
00860 static void mclms_predict(WmallDecodeCtx *s, int icoef, int *pred)
00861 {
00862     int ich, i;
00863     int order = s->mclms_order;
00864     int num_channels = s->num_channels;
00865 
00866     for (ich = 0; ich < num_channels; ich++) {
00867         if (!s->is_channel_coded[ich])
00868             continue;
00869         pred[ich] = 0;
00870         for (i = 0; i < order * num_channels; i++)
00871             pred[ich] += s->mclms_prevvalues[i + s->mclms_recent] *
00872                          s->mclms_coeffs[i + order * num_channels * ich];
00873         for (i = 0; i < ich; i++)
00874             pred[ich] += s->channel_residues[i][icoef] *
00875                          s->mclms_coeffs_cur[i + num_channels * ich];
00876         pred[ich] += 1 << s->mclms_scaling - 1;
00877         pred[ich] >>= s->mclms_scaling;
00878         s->channel_residues[ich][icoef] += pred[ich];
00879     }
00880 }
00881 
00882 static void revert_mclms(WmallDecodeCtx *s, int tile_size)
00883 {
00884     int icoef, pred[s->num_channels];
00885     for (icoef = 0; icoef < tile_size; icoef++) {
00886         mclms_predict(s, icoef, pred);
00887         mclms_update(s, icoef, pred);
00888     }
00889 }
00890 
00891 static int lms_predict(WmallDecodeCtx *s, int ich, int ilms)
00892 {
00893     int pred = 0;
00894     int icoef;
00895     int recent = s->cdlms[ich][ilms].recent;
00896 
00897     for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
00898         pred += s->cdlms[ich][ilms].coefs[icoef] *
00899                     s->cdlms[ich][ilms].lms_prevvalues[icoef + recent];
00900 
00901     //pred += (1 << (s->cdlms[ich][ilms].scaling - 1));
00902     /* XXX: Table 29 has:
00903             iPred >= cdlms[iCh][ilms].scaling;
00904        seems to me like a missing > */
00905     //pred >>= s->cdlms[ich][ilms].scaling;
00906     return pred;
00907 }
00908 
00909 static void lms_update(WmallDecodeCtx *s, int ich, int ilms, int input, int residue)
00910 {
00911     int icoef;
00912     int recent = s->cdlms[ich][ilms].recent;
00913     int range = 1 << s->bits_per_sample - 1;
00914     int bps = s->bits_per_sample > 16 ? 4 : 2; // bytes per sample
00915 
00916     if (residue < 0) {
00917         for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
00918             s->cdlms[ich][ilms].coefs[icoef] -=
00919                 s->cdlms[ich][ilms].lms_updates[icoef + recent];
00920     } else if (residue > 0) {
00921         for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
00922             s->cdlms[ich][ilms].coefs[icoef] +=
00923                 s->cdlms[ich][ilms].lms_updates[icoef + recent];    /* spec mistakenly
00924                                                                     dropped the recent */
00925     }
00926 
00927     if (recent)
00928         recent--;
00929     else {
00930         /* XXX: This memcpy()s will probably fail if a fixed 32-bit buffer is used.
00931                 follow kshishkov's suggestion of using a union. */
00932         memcpy(&s->cdlms[ich][ilms].lms_prevvalues[s->cdlms[ich][ilms].order],
00933                s->cdlms[ich][ilms].lms_prevvalues,
00934                bps * s->cdlms[ich][ilms].order);
00935         memcpy(&s->cdlms[ich][ilms].lms_updates[s->cdlms[ich][ilms].order],
00936                s->cdlms[ich][ilms].lms_updates,
00937                bps * s->cdlms[ich][ilms].order);
00938         recent = s->cdlms[ich][ilms].order - 1;
00939     }
00940 
00941     s->cdlms[ich][ilms].lms_prevvalues[recent] = av_clip(input, -range, range - 1);
00942     if (!input)
00943         s->cdlms[ich][ilms].lms_updates[recent] = 0;
00944     else if (input < 0)
00945         s->cdlms[ich][ilms].lms_updates[recent] = -s->update_speed[ich];
00946     else
00947         s->cdlms[ich][ilms].lms_updates[recent] = s->update_speed[ich];
00948 
00949     /* XXX: spec says:
00950     cdlms[iCh][ilms].updates[iRecent + cdlms[iCh][ilms].order >> 4] >>= 2;
00951     lms_updates[iCh][ilms][iRecent + cdlms[iCh][ilms].order >> 3] >>= 1;
00952 
00953         Questions is - are cdlms[iCh][ilms].updates[] and lms_updates[][][] two
00954         seperate buffers? Here I've assumed that the two are same which makes
00955         more sense to me.
00956     */
00957     s->cdlms[ich][ilms].lms_updates[recent + (s->cdlms[ich][ilms].order >> 4)] >>= 2;
00958     s->cdlms[ich][ilms].lms_updates[recent + (s->cdlms[ich][ilms].order >> 3)] >>= 1;
00959     s->cdlms[ich][ilms].recent = recent;
00960 }
00961 
00962 static void use_high_update_speed(WmallDecodeCtx *s, int ich)
00963 {
00964     int ilms, recent, icoef;
00965     for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
00966         recent = s->cdlms[ich][ilms].recent;
00967         if (s->update_speed[ich] == 16)
00968             continue;
00969         if (s->bV3RTM) {
00970             for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
00971                 s->cdlms[ich][ilms].lms_updates[icoef + recent] *= 2;
00972         } else {
00973             for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
00974                 s->cdlms[ich][ilms].lms_updates[icoef] *= 2;
00975         }
00976     }
00977     s->update_speed[ich] = 16;
00978 }
00979 
00980 static void use_normal_update_speed(WmallDecodeCtx *s, int ich)
00981 {
00982     int ilms, recent, icoef;
00983     for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
00984         recent = s->cdlms[ich][ilms].recent;
00985         if (s->update_speed[ich] == 8)
00986             continue;
00987         if (s->bV3RTM) {
00988             for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
00989                 s->cdlms[ich][ilms].lms_updates[icoef + recent] /= 2;
00990         } else {
00991             for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
00992                 s->cdlms[ich][ilms].lms_updates[icoef] /= 2;
00993         }
00994     }
00995     s->update_speed[ich] = 8;
00996 }
00997 
00998 static void revert_cdlms(WmallDecodeCtx *s, int ch, int coef_begin, int coef_end)
00999 {
01000     int icoef;
01001     int pred;
01002     int ilms, num_lms;
01003     int residue, input;
01004 
01005     num_lms = s->cdlms_ttl[ch];
01006     for (ilms = num_lms - 1; ilms >= 0; ilms--) {
01007         //s->cdlms[ch][ilms].recent = s->cdlms[ch][ilms].order;
01008         for (icoef = coef_begin; icoef < coef_end; icoef++) {
01009             pred = 1 << (s->cdlms[ch][ilms].scaling - 1);
01010             residue = s->channel_residues[ch][icoef];
01011             pred += lms_predict(s, ch, ilms);
01012             input = residue + (pred >> s->cdlms[ch][ilms].scaling);
01013             lms_update(s, ch, ilms, input, residue);
01014             s->channel_residues[ch][icoef] = input;
01015         }
01016     }
01017 }
01018 
01019 static void revert_inter_ch_decorr(WmallDecodeCtx *s, int tile_size)
01020 {
01021     int icoef;
01022     if (s->num_channels != 2)
01023         return;
01024     else {
01025         for (icoef = 0; icoef < tile_size; icoef++) {
01026             s->channel_residues[0][icoef] -= s->channel_residues[1][icoef] >> 1;
01027             s->channel_residues[1][icoef] += s->channel_residues[0][icoef];
01028         }
01029     }
01030 }
01031 
01032 static void revert_acfilter(WmallDecodeCtx *s, int tile_size)
01033 {
01034     int ich, icoef;
01035     int pred;
01036     int i, j;
01037     int64_t *filter_coeffs = s->acfilter_coeffs;
01038     int scaling = s->acfilter_scaling;
01039     int order = s->acfilter_order;
01040 
01041     for (ich = 0; ich < s->num_channels; ich++) {
01042         int *prevvalues = s->acfilter_prevvalues[ich];
01043         for (i = 0; i < order; i++) {
01044             pred = 0;
01045             for (j = 0; j < order; j++) {
01046                 if (i <= j)
01047                     pred += filter_coeffs[j] * prevvalues[j - i];
01048                 else
01049                     pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
01050             }
01051             pred >>= scaling;
01052             s->channel_residues[ich][i] += pred;
01053         }
01054         for (i = order; i < tile_size; i++) {
01055             pred = 0;
01056             for (j = 0; j < order; j++)
01057                 pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
01058             pred >>= scaling;
01059             s->channel_residues[ich][i] += pred;
01060         }
01061         for (j = 0; j < order; j++)
01062             prevvalues[j] = s->channel_residues[ich][tile_size - j - 1];
01063     }
01064 }
01065 
01071 static int decode_subframe(WmallDecodeCtx *s)
01072 {
01073     int offset = s->samples_per_frame;
01074     int subframe_len = s->samples_per_frame;
01075     int i, j;
01076     int total_samples   = s->samples_per_frame * s->num_channels;
01077     int rawpcm_tile;
01078     int padding_zeroes;
01079 
01080     s->subframe_offset = get_bits_count(&s->gb);
01081 
01086     for (i = 0; i < s->num_channels; i++) {
01087         s->channel[i].grouped = 0;
01088         if (offset > s->channel[i].decoded_samples) {
01089             offset = s->channel[i].decoded_samples;
01090             subframe_len =
01091                 s->channel[i].subframe_len[s->channel[i].cur_subframe];
01092         }
01093     }
01094 
01096     s->channels_for_cur_subframe = 0;
01097     for (i = 0; i < s->num_channels; i++) {
01098         const int cur_subframe = s->channel[i].cur_subframe;
01100         total_samples -= s->channel[i].decoded_samples;
01101 
01103         if (offset == s->channel[i].decoded_samples &&
01104             subframe_len == s->channel[i].subframe_len[cur_subframe]) {
01105             total_samples -= s->channel[i].subframe_len[cur_subframe];
01106             s->channel[i].decoded_samples +=
01107                 s->channel[i].subframe_len[cur_subframe];
01108             s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
01109             ++s->channels_for_cur_subframe;
01110         }
01111     }
01112 
01115     if (!total_samples)
01116         s->parsed_all_subframes = 1;
01117 
01118 
01119     s->seekable_tile = get_bits1(&s->gb);
01120     if(s->seekable_tile) {
01121         clear_codec_buffers(s);
01122 
01123         s->do_arith_coding    = get_bits1(&s->gb);
01124         if(s->do_arith_coding) {
01125             dprintf(s->avctx, "do_arith_coding == 1");
01126             abort();
01127         }
01128         s->do_ac_filter       = get_bits1(&s->gb);
01129         s->do_inter_ch_decorr = get_bits1(&s->gb);
01130         s->do_mclms           = get_bits1(&s->gb);
01131 
01132         if(s->do_ac_filter)
01133             decode_ac_filter(s);
01134 
01135         if(s->do_mclms)
01136             decode_mclms(s);
01137 
01138         decode_cdlms(s);
01139         s->movave_scaling = get_bits(&s->gb, 3);
01140         s->quant_stepsize = get_bits(&s->gb, 8) + 1;
01141 
01142             reset_codec(s);
01143     }
01144 
01145     rawpcm_tile = get_bits1(&s->gb);
01146 
01147     for(i = 0; i < s->num_channels; i++) {
01148         s->is_channel_coded[i] = 1;
01149     }
01150 
01151     if(!rawpcm_tile) {
01152 
01153         for(i = 0; i < s->num_channels; i++) {
01154             s->is_channel_coded[i] = get_bits1(&s->gb);
01155         }
01156 
01157         if(s->bV3RTM) {
01158             // LPC
01159             s->do_lpc = get_bits1(&s->gb);
01160             if(s->do_lpc) {
01161                 decode_lpc(s);
01162             }
01163         } else {
01164             s->do_lpc = 0;
01165         }
01166     }
01167 
01168 
01169     if(get_bits1(&s->gb)) {
01170         padding_zeroes = get_bits(&s->gb, 5);
01171     } else {
01172         padding_zeroes = 0;
01173     }
01174 
01175     if(rawpcm_tile) {
01176 
01177         int bits = s->bits_per_sample - padding_zeroes;
01178         dprintf(s->avctx, "RAWPCM %d bits per sample. total %d bits, remain=%d\n", bits,
01179                 bits * s->num_channels * subframe_len, get_bits_count(&s->gb));
01180         for(i = 0; i < s->num_channels; i++) {
01181             for(j = 0; j < subframe_len; j++) {
01182                 s->channel_coeffs[i][j] = get_sbits(&s->gb, bits);
01183 //                dprintf(s->avctx, "PCM[%d][%d] = 0x%04x\n", i, j, s->channel_coeffs[i][j]);
01184             }
01185         }
01186     } else {
01187         for(i = 0; i < s->num_channels; i++)
01188             if(s->is_channel_coded[i]) {
01189             decode_channel_residues(s, i, subframe_len);
01190             if (s->seekable_tile)
01191                 use_high_update_speed(s, i);
01192             else
01193                 use_normal_update_speed(s, i);
01194             revert_cdlms(s, i, 0, subframe_len);
01195         }
01196     }
01197     if (s->do_mclms)
01198         revert_mclms(s, subframe_len);
01199     if (s->do_inter_ch_decorr)
01200         revert_inter_ch_decorr(s, subframe_len);
01201     if(s->do_ac_filter)
01202         revert_acfilter(s, subframe_len);
01203 
01204     /* Dequantize */
01205     if (s->quant_stepsize != 1)
01206         for (i = 0; i < s->num_channels; i++)
01207             for (j = 0; j < subframe_len; j++)
01208                 s->channel_residues[i][j] *= s->quant_stepsize;
01209 
01210     // Write to proper output buffer depending on bit-depth
01211     for (i = 0; i < subframe_len; i++)
01212         for (j = 0; j < s->num_channels; j++) {
01213             if (s->bits_per_sample == 16)
01214                 *s->samples_16++ = (int16_t) s->channel_residues[j][i];
01215             else
01216                 *s->samples_32++ = s->channel_residues[j][i];
01217         }
01218 
01221     for (i = 0; i < s->channels_for_cur_subframe; i++) {
01222         int c = s->channel_indexes_for_cur_subframe[i];
01223         if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
01224             av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
01225             return AVERROR_INVALIDDATA;
01226         }
01227         ++s->channel[c].cur_subframe;
01228     }
01229     num_logged_subframes++;
01230     return 0;
01231 }
01232 
01239 static int decode_frame(WmallDecodeCtx *s)
01240 {
01241     GetBitContext* gb = &s->gb;
01242     int more_frames = 0;
01243     int len = 0;
01244     int i;
01245     int buffer_len;
01246 
01248     if (s->bits_per_sample == 16)
01249         buffer_len = s->samples_16_end - s->samples_16;
01250     else
01251         buffer_len = s->samples_32_end - s->samples_32;
01252     if (s->num_channels * s->samples_per_frame > buffer_len) {
01254         av_log(s->avctx, AV_LOG_ERROR,
01255                "not enough space for the output samples\n");
01256         s->packet_loss = 1;
01257         return 0;
01258     }
01259 
01261     if (s->len_prefix)
01262         len = get_bits(gb, s->log2_frame_size);
01263 
01265     if (decode_tilehdr(s)) {
01266         s->packet_loss = 1;
01267         return 0;
01268     }
01269 
01271     if (s->dynamic_range_compression) {
01272         s->drc_gain = get_bits(gb, 8);
01273     }
01274 
01277     if (get_bits1(gb)) {
01278         int skip;
01279 
01281         if (get_bits1(gb)) {
01282             skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
01283             dprintf(s->avctx, "start skip: %i\n", skip);
01284         }
01285 
01287         if (get_bits1(gb)) {
01288             skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
01289             dprintf(s->avctx, "end skip: %i\n", skip);
01290         }
01291 
01292     }
01293 
01295     s->parsed_all_subframes = 0;
01296     for (i = 0; i < s->num_channels; i++) {
01297         s->channel[i].decoded_samples = 0;
01298         s->channel[i].cur_subframe    = 0;
01299         s->channel[i].reuse_sf        = 0;
01300     }
01301 
01303     while (!s->parsed_all_subframes) {
01304         if (decode_subframe(s) < 0) {
01305             s->packet_loss = 1;
01306             return 0;
01307         }
01308     }
01309 
01310     dprintf(s->avctx, "Frame done\n");
01311 
01312     if (s->skip_frame) {
01313         s->skip_frame = 0;
01314     }
01315 
01316     if (s->len_prefix) {
01317         if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
01319             av_log(s->avctx, AV_LOG_ERROR,
01320                    "frame[%i] would have to skip %i bits\n", s->frame_num,
01321                    len - (get_bits_count(gb) - s->frame_offset) - 1);
01322             s->packet_loss = 1;
01323             return 0;
01324         }
01325 
01327         skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
01328     } else {
01329 /*
01330         while (get_bits_count(gb) < s->num_saved_bits && get_bits1(gb) == 0) {
01331             dprintf(s->avctx, "skip1\n");
01332         }
01333 */
01334     }
01335 
01337     more_frames = get_bits1(gb);
01338     ++s->frame_num;
01339     return more_frames;
01340 }
01341 
01348 static int remaining_bits(WmallDecodeCtx *s, GetBitContext *gb)
01349 {
01350     return s->buf_bit_size - get_bits_count(gb);
01351 }
01352 
01360 static void save_bits(WmallDecodeCtx *s, GetBitContext* gb, int len,
01361                       int append)
01362 {
01363     int buflen;
01364 
01369     if (!append) {
01370         s->frame_offset = get_bits_count(gb) & 7;
01371         s->num_saved_bits = s->frame_offset;
01372         init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
01373     }
01374 
01375     buflen = (s->num_saved_bits + len + 8) >> 3;
01376 
01377     if (len <= 0 || buflen > MAX_FRAMESIZE) {
01378         av_log_ask_for_sample(s->avctx, "input buffer too small\n");
01379         s->packet_loss = 1;
01380         return;
01381     }
01382 
01383     s->num_saved_bits += len;
01384     if (!append) {
01385         avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
01386                      s->num_saved_bits);
01387     } else {
01388         int align = 8 - (get_bits_count(gb) & 7);
01389         align = FFMIN(align, len);
01390         put_bits(&s->pb, align, get_bits(gb, align));
01391         len -= align;
01392         avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
01393     }
01394     skip_bits_long(gb, len);
01395 
01396     {
01397         PutBitContext tmp = s->pb;
01398         flush_put_bits(&tmp);
01399     }
01400 
01401     init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
01402     skip_bits(&s->gb, s->frame_offset);
01403 }
01404 
01413 static int decode_packet(AVCodecContext *avctx,
01414                          void *data, int *data_size, AVPacket* avpkt)
01415 {
01416     WmallDecodeCtx *s = avctx->priv_data;
01417     GetBitContext* gb  = &s->pgb;
01418     const uint8_t* buf = avpkt->data;
01419     int buf_size       = avpkt->size;
01420     int num_bits_prev_frame;
01421     int packet_sequence_number;
01422 
01423     if (s->bits_per_sample == 16) {
01424         s->samples_16     = (int16_t *) data;
01425         s->samples_16_end = (int16_t *) ((int8_t*)data + *data_size);
01426     } else {
01427         s->samples_32     = (void *) data;
01428         s->samples_32_end = (void *) ((int8_t*)data + *data_size);
01429     }
01430     *data_size = 0;
01431 
01432     if (s->packet_done || s->packet_loss) {
01433         int seekable_frame_in_packet, spliced_packet;
01434         s->packet_done = 0;
01435 
01437         if (buf_size < avctx->block_align)
01438             return 0;
01439 
01440         s->next_packet_start = buf_size - avctx->block_align;
01441         buf_size = avctx->block_align;
01442         s->buf_bit_size = buf_size << 3;
01443 
01445         init_get_bits(gb, buf, s->buf_bit_size);
01446         packet_sequence_number = get_bits(gb, 4);
01447         seekable_frame_in_packet = get_bits1(gb);
01448         spliced_packet = get_bits1(gb);
01449 
01451         num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
01452 
01454         if (!s->packet_loss &&
01455             ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
01456             s->packet_loss = 1;
01457             av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n",
01458                    s->packet_sequence_number, packet_sequence_number);
01459         }
01460         s->packet_sequence_number = packet_sequence_number;
01461 
01462         if (num_bits_prev_frame > 0) {
01463             int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
01464             if (num_bits_prev_frame >= remaining_packet_bits) {
01465                 num_bits_prev_frame = remaining_packet_bits;
01466                 s->packet_done = 1;
01467             }
01468 
01471             save_bits(s, gb, num_bits_prev_frame, 1);
01472 
01474             if (!s->packet_loss)
01475                 decode_frame(s);
01476         } else if (s->num_saved_bits - s->frame_offset) {
01477             dprintf(avctx, "ignoring %x previously saved bits\n",
01478                     s->num_saved_bits - s->frame_offset);
01479         }
01480 
01481         if (s->packet_loss) {
01485             s->num_saved_bits = 0;
01486             s->packet_loss = 0;
01487         }
01488 
01489     } else {
01490         int frame_size;
01491 
01492         s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
01493         init_get_bits(gb, avpkt->data, s->buf_bit_size);
01494         skip_bits(gb, s->packet_offset);
01495 
01496         if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
01497             (frame_size = show_bits(gb, s->log2_frame_size)) &&
01498             frame_size <= remaining_bits(s, gb)) {
01499             save_bits(s, gb, frame_size, 0);
01500             s->packet_done = !decode_frame(s);
01501         } else if (!s->len_prefix
01502                    && s->num_saved_bits > get_bits_count(&s->gb)) {
01510             s->packet_done = !decode_frame(s);
01511         } else {
01512             s->packet_done = 1;
01513         }
01514     }
01515 
01516     if (s->packet_done && !s->packet_loss &&
01517         remaining_bits(s, gb) > 0) {
01520         save_bits(s, gb, remaining_bits(s, gb), 0);
01521     }
01522 
01523     if (s->bits_per_sample == 16)
01524         *data_size = (int8_t *)s->samples_16 - (int8_t *)data;
01525     else
01526         *data_size = (int8_t *)s->samples_32 - (int8_t *)data;
01527     s->packet_offset = get_bits_count(gb) & 7;
01528 
01529     return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
01530 }
01531 
01536 static void flush(AVCodecContext *avctx)
01537 {
01538     WmallDecodeCtx *s = avctx->priv_data;
01539     int i;
01542     for (i = 0; i < s->num_channels; i++)
01543         memset(s->channel[i].out, 0, s->samples_per_frame *
01544                sizeof(*s->channel[i].out));
01545     s->packet_loss = 1;
01546 }
01547 
01548 
01552 AVCodec ff_wmalossless_decoder = {
01553     "wmalossless",
01554     AVMEDIA_TYPE_AUDIO,
01555     CODEC_ID_WMALOSSLESS,
01556     sizeof(WmallDecodeCtx),
01557     decode_init,
01558     NULL,
01559     decode_end,
01560     decode_packet,
01561     .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_EXPERIMENTAL,
01562     .flush= flush,
01563     .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 9 Lossless"),
01564 };