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

libavcodec/mpc7.c

Go to the documentation of this file.
00001 /*
00002  * Musepack SV7 decoder
00003  * Copyright (c) 2006 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 "mpc7data.h"
00036 
00037 #define BANDS            32
00038 #define SAMPLES_PER_BAND 36
00039 #define MPC_FRAME_SIZE   (BANDS * SAMPLES_PER_BAND)
00040 
00041 static VLC scfi_vlc, dscf_vlc, hdr_vlc, quant_vlc[MPC7_QUANT_VLC_TABLES][2];
00042 
00043 static av_cold int mpc7_decode_init(AVCodecContext * avctx)
00044 {
00045     int i, j;
00046     MPCContext *c = avctx->priv_data;
00047     GetBitContext gb;
00048     uint8_t buf[16];
00049     static int vlc_initialized = 0;
00050 
00051     if(avctx->extradata_size < 16){
00052         av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size);
00053         return -1;
00054     }
00055     memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
00056     av_random_init(&c->rnd, 0xDEADBEEF);
00057     dsputil_init(&c->dsp, avctx);
00058     c->dsp.bswap_buf((uint32_t*)buf, (const uint32_t*)avctx->extradata, 4);
00059     ff_mpc_init();
00060     init_get_bits(&gb, buf, 128);
00061 
00062     c->IS = get_bits1(&gb);
00063     c->MSS = get_bits1(&gb);
00064     c->maxbands = get_bits(&gb, 6);
00065     if(c->maxbands >= BANDS){
00066         av_log(avctx, AV_LOG_ERROR, "Too many bands: %i\n", c->maxbands);
00067         return -1;
00068     }
00069     skip_bits(&gb, 88);
00070     c->gapless = get_bits1(&gb);
00071     c->lastframelen = get_bits(&gb, 11);
00072     av_log(avctx, AV_LOG_DEBUG, "IS: %d, MSS: %d, TG: %d, LFL: %d, bands: %d\n",
00073             c->IS, c->MSS, c->gapless, c->lastframelen, c->maxbands);
00074     c->frames_to_skip = 0;
00075 
00076     if(vlc_initialized) return 0;
00077     av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
00078     if(init_vlc(&scfi_vlc, MPC7_SCFI_BITS, MPC7_SCFI_SIZE,
00079                 &mpc7_scfi[1], 2, 1,
00080                 &mpc7_scfi[0], 2, 1, INIT_VLC_USE_STATIC)){
00081         av_log(avctx, AV_LOG_ERROR, "Cannot init SCFI VLC\n");
00082         return -1;
00083     }
00084     if(init_vlc(&dscf_vlc, MPC7_DSCF_BITS, MPC7_DSCF_SIZE,
00085                 &mpc7_dscf[1], 2, 1,
00086                 &mpc7_dscf[0], 2, 1, INIT_VLC_USE_STATIC)){
00087         av_log(avctx, AV_LOG_ERROR, "Cannot init DSCF VLC\n");
00088         return -1;
00089     }
00090     if(init_vlc(&hdr_vlc, MPC7_HDR_BITS, MPC7_HDR_SIZE,
00091                 &mpc7_hdr[1], 2, 1,
00092                 &mpc7_hdr[0], 2, 1, INIT_VLC_USE_STATIC)){
00093         av_log(avctx, AV_LOG_ERROR, "Cannot init HDR VLC\n");
00094         return -1;
00095     }
00096     for(i = 0; i < MPC7_QUANT_VLC_TABLES; i++){
00097         for(j = 0; j < 2; j++){
00098             if(init_vlc(&quant_vlc[i][j], 9, mpc7_quant_vlc_sizes[i],
00099                         &mpc7_quant_vlc[i][j][1], 4, 2,
00100                         &mpc7_quant_vlc[i][j][0], 4, 2, INIT_VLC_USE_STATIC)){
00101                 av_log(avctx, AV_LOG_ERROR, "Cannot init QUANT VLC %i,%i\n",i,j);
00102                 return -1;
00103             }
00104         }
00105     }
00106     vlc_initialized = 1;
00107     avctx->sample_fmt = SAMPLE_FMT_S16;
00108     avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
00109     return 0;
00110 }
00111 
00115 static inline void idx_to_quant(MPCContext *c, GetBitContext *gb, int idx, int *dst)
00116 {
00117     int i, i1, t;
00118     switch(idx){
00119     case -1:
00120         for(i = 0; i < SAMPLES_PER_BAND; i++){
00121             *dst++ = (av_random(&c->rnd) & 0x3FC) - 510;
00122         }
00123         break;
00124     case 1:
00125         i1 = get_bits1(gb);
00126         for(i = 0; i < SAMPLES_PER_BAND/3; i++){
00127             t = get_vlc2(gb, quant_vlc[0][i1].table, 9, 2);
00128             *dst++ = mpc7_idx30[t];
00129             *dst++ = mpc7_idx31[t];
00130             *dst++ = mpc7_idx32[t];
00131         }
00132         break;
00133     case 2:
00134         i1 = get_bits1(gb);
00135         for(i = 0; i < SAMPLES_PER_BAND/2; i++){
00136             t = get_vlc2(gb, quant_vlc[1][i1].table, 9, 2);
00137             *dst++ = mpc7_idx50[t];
00138             *dst++ = mpc7_idx51[t];
00139         }
00140         break;
00141     case  3: case  4: case  5: case  6: case  7:
00142         i1 = get_bits1(gb);
00143         for(i = 0; i < SAMPLES_PER_BAND; i++)
00144             *dst++ = get_vlc2(gb, quant_vlc[idx-1][i1].table, 9, 2) - mpc7_quant_vlc_off[idx-1];
00145         break;
00146     case  8: case  9: case 10: case 11: case 12:
00147     case 13: case 14: case 15: case 16: case 17:
00148         t = (1 << (idx - 2)) - 1;
00149         for(i = 0; i < SAMPLES_PER_BAND; i++)
00150             *dst++ = get_bits(gb, idx - 1) - t;
00151         break;
00152     default: // case 0 and -2..-17
00153         return;
00154     }
00155 }
00156 
00157 static int mpc7_decode_frame(AVCodecContext * avctx,
00158                             void *data, int *data_size,
00159                             const uint8_t * buf, int buf_size)
00160 {
00161     MPCContext *c = avctx->priv_data;
00162     GetBitContext gb;
00163     uint8_t *bits;
00164     int i, ch, t;
00165     int mb = -1;
00166     Band *bands = c->bands;
00167     int off;
00168     int bits_used, bits_avail;
00169 
00170     memset(bands, 0, sizeof(bands));
00171     if(buf_size <= 4){
00172         av_log(avctx, AV_LOG_ERROR, "Too small buffer passed (%i bytes)\n", buf_size);
00173     }
00174 
00175     bits = av_malloc(((buf_size - 1) & ~3) + FF_INPUT_BUFFER_PADDING_SIZE);
00176     c->dsp.bswap_buf((uint32_t*)bits, (const uint32_t*)(buf + 4), (buf_size - 4) >> 2);
00177     init_get_bits(&gb, bits, (buf_size - 4)* 8);
00178     skip_bits(&gb, buf[0]);
00179 
00180     /* read subband indexes */
00181     for(i = 0; i <= c->maxbands; i++){
00182         for(ch = 0; ch < 2; ch++){
00183             if(i) t = get_vlc2(&gb, hdr_vlc.table, MPC7_HDR_BITS, 1) - 5;
00184             if(!i || (t == 4)) bands[i].res[ch] = get_bits(&gb, 4);
00185             else bands[i].res[ch] = bands[i-1].res[ch] + t;
00186         }
00187 
00188         if(bands[i].res[0] || bands[i].res[1]){
00189             mb = i;
00190             if(c->MSS) bands[i].msf = get_bits1(&gb);
00191         }
00192     }
00193     /* get scale indexes coding method */
00194     for(i = 0; i <= mb; i++)
00195         for(ch = 0; ch < 2; ch++)
00196             if(bands[i].res[ch]) bands[i].scfi[ch] = get_vlc2(&gb, scfi_vlc.table, MPC7_SCFI_BITS, 1);
00197     /* get scale indexes */
00198     for(i = 0; i <= mb; i++){
00199         for(ch = 0; ch < 2; ch++){
00200             if(bands[i].res[ch]){
00201                 bands[i].scf_idx[ch][2] = c->oldDSCF[ch][i];
00202                 t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
00203                 bands[i].scf_idx[ch][0] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][2] + t);
00204                 switch(bands[i].scfi[ch]){
00205                 case 0:
00206                     t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
00207                     bands[i].scf_idx[ch][1] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][0] + t);
00208                     t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
00209                     bands[i].scf_idx[ch][2] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][1] + t);
00210                     break;
00211                 case 1:
00212                     t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
00213                     bands[i].scf_idx[ch][1] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][0] + t);
00214                     bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1];
00215                     break;
00216                 case 2:
00217                     bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
00218                     t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
00219                     bands[i].scf_idx[ch][2] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][1] + t);
00220                     break;
00221                 case 3:
00222                     bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
00223                     break;
00224                 }
00225                 c->oldDSCF[ch][i] = bands[i].scf_idx[ch][2];
00226             }
00227         }
00228     }
00229     /* get quantizers */
00230     memset(c->Q, 0, sizeof(c->Q));
00231     off = 0;
00232     for(i = 0; i < BANDS; i++, off += SAMPLES_PER_BAND)
00233         for(ch = 0; ch < 2; ch++)
00234             idx_to_quant(c, &gb, bands[i].res[ch], c->Q[ch] + off);
00235 
00236     ff_mpc_dequantize_and_synth(c, mb, data);
00237 
00238     av_free(bits);
00239 
00240     bits_used = get_bits_count(&gb);
00241     bits_avail = (buf_size - 4) * 8;
00242     if(!buf[1] && ((bits_avail < bits_used) || (bits_used + 32 <= bits_avail))){
00243         av_log(NULL,0, "Error decoding frame: used %i of %i bits\n", bits_used, bits_avail);
00244         return -1;
00245     }
00246     if(c->frames_to_skip){
00247         c->frames_to_skip--;
00248         *data_size = 0;
00249         return buf_size;
00250     }
00251     *data_size = (buf[1] ? c->lastframelen : MPC_FRAME_SIZE) * 4;
00252 
00253     return buf_size;
00254 }
00255 
00256 static void mpc7_decode_flush(AVCodecContext *avctx)
00257 {
00258     MPCContext *c = avctx->priv_data;
00259 
00260     memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
00261     c->frames_to_skip = 32;
00262 }
00263 
00264 AVCodec mpc7_decoder = {
00265     "mpc7",
00266     CODEC_TYPE_AUDIO,
00267     CODEC_ID_MUSEPACK7,
00268     sizeof(MPCContext),
00269     mpc7_decode_init,
00270     NULL,
00271     NULL,
00272     mpc7_decode_frame,
00273     .flush = mpc7_decode_flush,
00274     .long_name = NULL_IF_CONFIG_SMALL("Musepack SV7"),
00275 };

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