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

libavcodec/mpc8.c

Go to the documentation of this file.
00001 /*
00002  * Musepack SV8 decoder
00003  * Copyright (c) 2007 Konstantin Shishkov
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00028 #include "libavutil/random.h"
00029 #include "avcodec.h"
00030 #include "bitstream.h"
00031 #include "dsputil.h"
00032 #include "mpegaudio.h"
00033 
00034 #include "mpc.h"
00035 #include "mpcdata.h"
00036 #include "mpc8data.h"
00037 #include "mpc8huff.h"
00038 
00039 static VLC band_vlc, scfi_vlc[2], dscf_vlc[2], res_vlc[2];
00040 static VLC q1_vlc, q2_vlc[2], q3_vlc[2], quant_vlc[4][2], q9up_vlc;
00041 
00042 static const int q3_offsets[2] = { MPC8_Q3_OFFSET, MPC8_Q4_OFFSET };
00043 static const int quant_offsets[6] = { MPC8_Q5_OFFSET, MPC8_Q6_OFFSET, MPC8_Q7_OFFSET, MPC8_Q8_OFFSET };
00044 
00045 static inline int mpc8_dec_base(GetBitContext *gb, int k, int n)
00046 {
00047     int code = get_bits(gb, mpc8_cnk_len[k-1][n-1] - 1);
00048 
00049     if (code >= mpc8_cnk_lost[k-1][n-1])
00050         code = ((code << 1) | get_bits1(gb)) - mpc8_cnk_lost[k-1][n-1];
00051 
00052     return code;
00053 }
00054 
00055 static inline int mpc8_dec_enum(GetBitContext *gb, int k, int n)
00056 {
00057     int bits = 0;
00058     const uint32_t * C = mpc8_cnk[k-1];
00059     int code = mpc8_dec_base(gb, k, n);
00060 
00061     do {
00062         n--;
00063         if (code >= C[n]) {
00064             bits |= 1 << n;
00065             code -= C[n];
00066             C -= 32;
00067             k--;
00068         }
00069     } while(k > 0);
00070 
00071     return bits;
00072 }
00073 
00074 static inline int mpc8_get_mod_golomb(GetBitContext *gb, int m)
00075 {
00076     if(mpc8_cnk_len[0][m] < 1) return 0;
00077     return mpc8_dec_base(gb, 1, m+1);
00078 }
00079 
00080 static int mpc8_get_mask(GetBitContext *gb, int size, int t)
00081 {
00082     int mask = 0;
00083 
00084     if(t && t != size)
00085          mask = mpc8_dec_enum(gb, FFMIN(t, size - t), size);
00086     if((t << 1) > size) mask = ~mask;
00087 
00088     return mask;
00089 }
00090 
00091 static av_cold int mpc8_decode_init(AVCodecContext * avctx)
00092 {
00093     int i;
00094     MPCContext *c = avctx->priv_data;
00095     GetBitContext gb;
00096     static int vlc_initialized = 0;
00097 
00098     if(avctx->extradata_size < 2){
00099         av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size);
00100         return -1;
00101     }
00102     memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
00103     av_random_init(&c->rnd, 0xDEADBEEF);
00104     dsputil_init(&c->dsp, avctx);
00105 
00106     ff_mpc_init();
00107 
00108     init_get_bits(&gb, avctx->extradata, 16);
00109 
00110     skip_bits(&gb, 3);//sample rate
00111     c->maxbands = get_bits(&gb, 5) + 1;
00112     skip_bits(&gb, 4);//channels
00113     c->MSS = get_bits1(&gb);
00114     c->frames = 1 << (get_bits(&gb, 3) * 2);
00115 
00116     if(vlc_initialized) return 0;
00117     av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
00118 
00119     init_vlc(&band_vlc, MPC8_BANDS_BITS, MPC8_BANDS_SIZE,
00120              mpc8_bands_bits,  1, 1,
00121              mpc8_bands_codes, 1, 1, INIT_VLC_USE_STATIC);
00122 
00123     init_vlc(&q1_vlc, MPC8_Q1_BITS, MPC8_Q1_SIZE,
00124              mpc8_q1_bits,  1, 1,
00125              mpc8_q1_codes, 1, 1, INIT_VLC_USE_STATIC);
00126     init_vlc(&q9up_vlc, MPC8_Q9UP_BITS, MPC8_Q9UP_SIZE,
00127              mpc8_q9up_bits,  1, 1,
00128              mpc8_q9up_codes, 1, 1, INIT_VLC_USE_STATIC);
00129 
00130     init_vlc(&scfi_vlc[0], MPC8_SCFI0_BITS, MPC8_SCFI0_SIZE,
00131              mpc8_scfi0_bits,  1, 1,
00132              mpc8_scfi0_codes, 1, 1, INIT_VLC_USE_STATIC);
00133     init_vlc(&scfi_vlc[1], MPC8_SCFI1_BITS, MPC8_SCFI1_SIZE,
00134              mpc8_scfi1_bits,  1, 1,
00135              mpc8_scfi1_codes, 1, 1, INIT_VLC_USE_STATIC);
00136 
00137     init_vlc(&dscf_vlc[0], MPC8_DSCF0_BITS, MPC8_DSCF0_SIZE,
00138              mpc8_dscf0_bits,  1, 1,
00139              mpc8_dscf0_codes, 1, 1, INIT_VLC_USE_STATIC);
00140     init_vlc(&dscf_vlc[1], MPC8_DSCF1_BITS, MPC8_DSCF1_SIZE,
00141              mpc8_dscf1_bits,  1, 1,
00142              mpc8_dscf1_codes, 1, 1, INIT_VLC_USE_STATIC);
00143 
00144     init_vlc_sparse(&q3_vlc[0], MPC8_Q3_BITS, MPC8_Q3_SIZE,
00145              mpc8_q3_bits,  1, 1,
00146              mpc8_q3_codes, 1, 1,
00147              mpc8_q3_syms,  1, 1, INIT_VLC_USE_STATIC);
00148     init_vlc_sparse(&q3_vlc[1], MPC8_Q4_BITS, MPC8_Q4_SIZE,
00149              mpc8_q4_bits,  1, 1,
00150              mpc8_q4_codes, 1, 1,
00151              mpc8_q4_syms,  1, 1, INIT_VLC_USE_STATIC);
00152 
00153     for(i = 0; i < 2; i++){
00154         init_vlc(&res_vlc[i], MPC8_RES_BITS, MPC8_RES_SIZE,
00155                  &mpc8_res_bits[i],  1, 1,
00156                  &mpc8_res_codes[i], 1, 1, INIT_VLC_USE_STATIC);
00157 
00158         init_vlc(&q2_vlc[i], MPC8_Q2_BITS, MPC8_Q2_SIZE,
00159                  &mpc8_q2_bits[i],  1, 1,
00160                  &mpc8_q2_codes[i], 1, 1, INIT_VLC_USE_STATIC);
00161 
00162         init_vlc(&quant_vlc[0][i], MPC8_Q5_BITS, MPC8_Q5_SIZE,
00163                  &mpc8_q5_bits[i],  1, 1,
00164                  &mpc8_q5_codes[i], 1, 1, INIT_VLC_USE_STATIC);
00165         init_vlc(&quant_vlc[1][i], MPC8_Q6_BITS, MPC8_Q6_SIZE,
00166                  &mpc8_q6_bits[i],  1, 1,
00167                  &mpc8_q6_codes[i], 1, 1, INIT_VLC_USE_STATIC);
00168         init_vlc(&quant_vlc[2][i], MPC8_Q7_BITS, MPC8_Q7_SIZE,
00169                  &mpc8_q7_bits[i],  1, 1,
00170                  &mpc8_q7_codes[i], 1, 1, INIT_VLC_USE_STATIC);
00171         init_vlc(&quant_vlc[3][i], MPC8_Q8_BITS, MPC8_Q8_SIZE,
00172                  &mpc8_q8_bits[i],  1, 1,
00173                  &mpc8_q8_codes[i], 1, 1, INIT_VLC_USE_STATIC);
00174     }
00175     vlc_initialized = 1;
00176     avctx->sample_fmt = SAMPLE_FMT_S16;
00177     avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
00178     return 0;
00179 }
00180 
00181 static int mpc8_decode_frame(AVCodecContext * avctx,
00182                             void *data, int *data_size,
00183                             const uint8_t * buf, int buf_size)
00184 {
00185     MPCContext *c = avctx->priv_data;
00186     GetBitContext gb2, *gb = &gb2;
00187     int i, j, k, ch, cnt, res, t;
00188     Band *bands = c->bands;
00189     int off;
00190     int maxband, keyframe;
00191     int last[2];
00192 
00193     keyframe = c->cur_frame == 0;
00194 
00195     if(keyframe){
00196         memset(c->Q, 0, sizeof(c->Q));
00197         c->last_bits_used = 0;
00198     }
00199     init_get_bits(gb, buf, buf_size * 8);
00200     skip_bits(gb, c->last_bits_used & 7);
00201 
00202     if(keyframe)
00203         maxband = mpc8_get_mod_golomb(gb, c->maxbands + 1);
00204     else{
00205         maxband = c->last_max_band + get_vlc2(gb, band_vlc.table, MPC8_BANDS_BITS, 2);
00206         if(maxband > 32) maxband -= 33;
00207     }
00208     c->last_max_band = maxband;
00209 
00210     /* read subband indexes */
00211     if(maxband){
00212         last[0] = last[1] = 0;
00213         for(i = maxband - 1; i >= 0; i--){
00214             for(ch = 0; ch < 2; ch++){
00215                 last[ch] = get_vlc2(gb, res_vlc[last[ch] > 2].table, MPC8_RES_BITS, 2) + last[ch];
00216                 if(last[ch] > 15) last[ch] -= 17;
00217                 bands[i].res[ch] = last[ch];
00218             }
00219         }
00220         if(c->MSS){
00221             int mask;
00222 
00223             cnt = 0;
00224             for(i = 0; i < maxband; i++)
00225                 if(bands[i].res[0] || bands[i].res[1])
00226                     cnt++;
00227             t = mpc8_get_mod_golomb(gb, cnt);
00228             mask = mpc8_get_mask(gb, cnt, t);
00229             for(i = maxband - 1; i >= 0; i--)
00230                 if(bands[i].res[0] || bands[i].res[1]){
00231                     bands[i].msf = mask & 1;
00232                     mask >>= 1;
00233                 }
00234         }
00235     }
00236     for(i = maxband; i < c->maxbands; i++)
00237         bands[i].res[0] = bands[i].res[1] = 0;
00238 
00239     if(keyframe){
00240         for(i = 0; i < 32; i++)
00241             c->oldDSCF[0][i] = c->oldDSCF[1][i] = 1;
00242     }
00243 
00244     for(i = 0; i < maxband; i++){
00245         if(bands[i].res[0] || bands[i].res[1]){
00246             cnt = !!bands[i].res[0] + !!bands[i].res[1] - 1;
00247             if(cnt >= 0){
00248                 t = get_vlc2(gb, scfi_vlc[cnt].table, scfi_vlc[cnt].bits, 1);
00249                 if(bands[i].res[0]) bands[i].scfi[0] = t >> (2 * cnt);
00250                 if(bands[i].res[1]) bands[i].scfi[1] = t & 3;
00251             }
00252         }
00253     }
00254 
00255     for(i = 0; i < maxband; i++){
00256         for(ch = 0; ch < 2; ch++){
00257             if(!bands[i].res[ch]) continue;
00258 
00259             if(c->oldDSCF[ch][i]){
00260                 bands[i].scf_idx[ch][0] = get_bits(gb, 7) - 6;
00261                 c->oldDSCF[ch][i] = 0;
00262             }else{
00263                 t = get_vlc2(gb, dscf_vlc[1].table, MPC8_DSCF1_BITS, 2);
00264                 if(t == 64)
00265                     t += get_bits(gb, 6);
00266                 bands[i].scf_idx[ch][0] = ((bands[i].scf_idx[ch][2] + t - 25) & 0x7F) - 6;
00267             }
00268             for(j = 0; j < 2; j++){
00269                 if((bands[i].scfi[ch] << j) & 2)
00270                     bands[i].scf_idx[ch][j + 1] = bands[i].scf_idx[ch][j];
00271                 else{
00272                     t = get_vlc2(gb, dscf_vlc[0].table, MPC8_DSCF0_BITS, 2);
00273                     if(t == 31)
00274                         t = 64 + get_bits(gb, 6);
00275                     bands[i].scf_idx[ch][j + 1] = ((bands[i].scf_idx[ch][j] + t - 25) & 0x7F) - 6;
00276                 }
00277             }
00278         }
00279     }
00280 
00281     for(i = 0, off = 0; i < maxband; i++, off += SAMPLES_PER_BAND){
00282         for(ch = 0; ch < 2; ch++){
00283             res = bands[i].res[ch];
00284             switch(res){
00285             case -1:
00286                 for(j = 0; j < SAMPLES_PER_BAND; j++)
00287                     c->Q[ch][off + j] = (av_random(&c->rnd) & 0x3FC) - 510;
00288                 break;
00289             case 0:
00290                 break;
00291             case 1:
00292                 for(j = 0; j < SAMPLES_PER_BAND; j += SAMPLES_PER_BAND / 2){
00293                     cnt = get_vlc2(gb, q1_vlc.table, MPC8_Q1_BITS, 2);
00294                     t = mpc8_get_mask(gb, 18, cnt);
00295                     for(k = 0; k < SAMPLES_PER_BAND / 2; k++, t <<= 1)
00296                         c->Q[ch][off + j + k] = (t & 0x20000) ? (get_bits1(gb) << 1) - 1 : 0;
00297                 }
00298                 break;
00299             case 2:
00300                 cnt = 6;//2*mpc8_thres[res]
00301                 for(j = 0; j < SAMPLES_PER_BAND; j += 3){
00302                     t = get_vlc2(gb, q2_vlc[cnt > 3].table, MPC8_Q2_BITS, 2);
00303                     c->Q[ch][off + j + 0] = mpc8_idx50[t];
00304                     c->Q[ch][off + j + 1] = mpc8_idx51[t];
00305                     c->Q[ch][off + j + 2] = mpc8_idx52[t];
00306                     cnt = (cnt >> 1) + mpc8_huffq2[t];
00307                 }
00308                 break;
00309             case 3:
00310             case 4:
00311                 for(j = 0; j < SAMPLES_PER_BAND; j += 2){
00312                     t = get_vlc2(gb, q3_vlc[res - 3].table, MPC8_Q3_BITS, 2) + q3_offsets[res - 3];
00313                     c->Q[ch][off + j + 1] = t >> 4;
00314                     c->Q[ch][off + j + 0] = (t & 8) ? (t & 0xF) - 16 : (t & 0xF);
00315                 }
00316                 break;
00317             case 5:
00318             case 6:
00319             case 7:
00320             case 8:
00321                 cnt = 2 * mpc8_thres[res];
00322                 for(j = 0; j < SAMPLES_PER_BAND; j++){
00323                     t = get_vlc2(gb, quant_vlc[res - 5][cnt > mpc8_thres[res]].table, quant_vlc[res - 5][cnt > mpc8_thres[res]].bits, 2) + quant_offsets[res - 5];
00324                     c->Q[ch][off + j] = t;
00325                     cnt = (cnt >> 1) + FFABS(c->Q[ch][off + j]);
00326                 }
00327                 break;
00328             default:
00329                 for(j = 0; j < SAMPLES_PER_BAND; j++){
00330                     c->Q[ch][off + j] = get_vlc2(gb, q9up_vlc.table, MPC8_Q9UP_BITS, 2);
00331                     if(res != 9){
00332                         c->Q[ch][off + j] <<= res - 9;
00333                         c->Q[ch][off + j] |= get_bits(gb, res - 9);
00334                     }
00335                     c->Q[ch][off + j] -= (1 << (res - 2)) - 1;
00336                 }
00337             }
00338         }
00339     }
00340 
00341     ff_mpc_dequantize_and_synth(c, maxband, data);
00342 
00343     c->cur_frame++;
00344 
00345     c->last_bits_used = get_bits_count(gb);
00346     if(c->cur_frame >= c->frames)
00347         c->cur_frame = 0;
00348     *data_size =  MPC_FRAME_SIZE * 4;
00349 
00350     return c->cur_frame ? c->last_bits_used >> 3 : buf_size;
00351 }
00352 
00353 AVCodec mpc8_decoder = {
00354     "mpc8",
00355     CODEC_TYPE_AUDIO,
00356     CODEC_ID_MUSEPACK8,
00357     sizeof(MPCContext),
00358     mpc8_decode_init,
00359     NULL,
00360     NULL,
00361     mpc8_decode_frame,
00362     .long_name = NULL_IF_CONFIG_SMALL("Musepack SV8"),
00363 };

Generated on Sat Feb 16 2013 09:23:13 for ffmpeg by  doxygen 1.7.1