• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • 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 Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; 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/lfg.h"
00029 #include "avcodec.h"
00030 #include "get_bits.h"
00031 #include "dsputil.h"
00032 #include "mpegaudiodsp.h"
00033 #include "libavutil/audioconvert.h"
00034 
00035 #include "mpc.h"
00036 #include "mpc7data.h"
00037 
00038 #define BANDS            32
00039 #define SAMPLES_PER_BAND 36
00040 #define MPC_FRAME_SIZE   (BANDS * SAMPLES_PER_BAND)
00041 
00042 static VLC scfi_vlc, dscf_vlc, hdr_vlc, quant_vlc[MPC7_QUANT_VLC_TABLES][2];
00043 
00044 static const uint16_t quant_offsets[MPC7_QUANT_VLC_TABLES*2 + 1] =
00045 {
00046        0, 512, 1024, 1536, 2052, 2564, 3076, 3588, 4100, 4612, 5124,
00047        5636, 6164, 6676, 7224
00048 };
00049 
00050 
00051 static av_cold int mpc7_decode_init(AVCodecContext * avctx)
00052 {
00053     int i, j;
00054     MPCContext *c = avctx->priv_data;
00055     GetBitContext gb;
00056     uint8_t buf[16];
00057     static int vlc_initialized = 0;
00058 
00059     static VLC_TYPE scfi_table[1 << MPC7_SCFI_BITS][2];
00060     static VLC_TYPE dscf_table[1 << MPC7_DSCF_BITS][2];
00061     static VLC_TYPE hdr_table[1 << MPC7_HDR_BITS][2];
00062     static VLC_TYPE quant_tables[7224][2];
00063 
00064     /* Musepack SV7 is always stereo */
00065     if (avctx->channels != 2) {
00066         av_log_ask_for_sample(avctx, "Unsupported number of channels: %d\n",
00067                               avctx->channels);
00068         return AVERROR_PATCHWELCOME;
00069     }
00070 
00071     if(avctx->extradata_size < 16){
00072         av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size);
00073         return -1;
00074     }
00075     memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
00076     av_lfg_init(&c->rnd, 0xDEADBEEF);
00077     dsputil_init(&c->dsp, avctx);
00078     ff_mpadsp_init(&c->mpadsp);
00079     c->dsp.bswap_buf((uint32_t*)buf, (const uint32_t*)avctx->extradata, 4);
00080     ff_mpc_init();
00081     init_get_bits(&gb, buf, 128);
00082 
00083     c->IS = get_bits1(&gb);
00084     c->MSS = get_bits1(&gb);
00085     c->maxbands = get_bits(&gb, 6);
00086     if(c->maxbands >= BANDS){
00087         av_log(avctx, AV_LOG_ERROR, "Too many bands: %i\n", c->maxbands);
00088         return -1;
00089     }
00090     skip_bits_long(&gb, 88);
00091     c->gapless = get_bits1(&gb);
00092     c->lastframelen = get_bits(&gb, 11);
00093     av_log(avctx, AV_LOG_DEBUG, "IS: %d, MSS: %d, TG: %d, LFL: %d, bands: %d\n",
00094             c->IS, c->MSS, c->gapless, c->lastframelen, c->maxbands);
00095     c->frames_to_skip = 0;
00096 
00097     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00098     avctx->channel_layout = AV_CH_LAYOUT_STEREO;
00099 
00100     if(vlc_initialized) return 0;
00101     av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
00102     scfi_vlc.table = scfi_table;
00103     scfi_vlc.table_allocated = 1 << MPC7_SCFI_BITS;
00104     if(init_vlc(&scfi_vlc, MPC7_SCFI_BITS, MPC7_SCFI_SIZE,
00105                 &mpc7_scfi[1], 2, 1,
00106                 &mpc7_scfi[0], 2, 1, INIT_VLC_USE_NEW_STATIC)){
00107         av_log(avctx, AV_LOG_ERROR, "Cannot init SCFI VLC\n");
00108         return -1;
00109     }
00110     dscf_vlc.table = dscf_table;
00111     dscf_vlc.table_allocated = 1 << MPC7_DSCF_BITS;
00112     if(init_vlc(&dscf_vlc, MPC7_DSCF_BITS, MPC7_DSCF_SIZE,
00113                 &mpc7_dscf[1], 2, 1,
00114                 &mpc7_dscf[0], 2, 1, INIT_VLC_USE_NEW_STATIC)){
00115         av_log(avctx, AV_LOG_ERROR, "Cannot init DSCF VLC\n");
00116         return -1;
00117     }
00118     hdr_vlc.table = hdr_table;
00119     hdr_vlc.table_allocated = 1 << MPC7_HDR_BITS;
00120     if(init_vlc(&hdr_vlc, MPC7_HDR_BITS, MPC7_HDR_SIZE,
00121                 &mpc7_hdr[1], 2, 1,
00122                 &mpc7_hdr[0], 2, 1, INIT_VLC_USE_NEW_STATIC)){
00123         av_log(avctx, AV_LOG_ERROR, "Cannot init HDR VLC\n");
00124         return -1;
00125     }
00126     for(i = 0; i < MPC7_QUANT_VLC_TABLES; i++){
00127         for(j = 0; j < 2; j++){
00128             quant_vlc[i][j].table = &quant_tables[quant_offsets[i*2 + j]];
00129             quant_vlc[i][j].table_allocated = quant_offsets[i*2 + j + 1] - quant_offsets[i*2 + j];
00130             if(init_vlc(&quant_vlc[i][j], 9, mpc7_quant_vlc_sizes[i],
00131                         &mpc7_quant_vlc[i][j][1], 4, 2,
00132                         &mpc7_quant_vlc[i][j][0], 4, 2, INIT_VLC_USE_NEW_STATIC)){
00133                 av_log(avctx, AV_LOG_ERROR, "Cannot init QUANT VLC %i,%i\n",i,j);
00134                 return -1;
00135             }
00136         }
00137     }
00138     vlc_initialized = 1;
00139 
00140     avcodec_get_frame_defaults(&c->frame);
00141     avctx->coded_frame = &c->frame;
00142 
00143     return 0;
00144 }
00145 
00149 static inline void idx_to_quant(MPCContext *c, GetBitContext *gb, int idx, int *dst)
00150 {
00151     int i, i1, t;
00152     switch(idx){
00153     case -1:
00154         for(i = 0; i < SAMPLES_PER_BAND; i++){
00155             *dst++ = (av_lfg_get(&c->rnd) & 0x3FC) - 510;
00156         }
00157         break;
00158     case 1:
00159         i1 = get_bits1(gb);
00160         for(i = 0; i < SAMPLES_PER_BAND/3; i++){
00161             t = get_vlc2(gb, quant_vlc[0][i1].table, 9, 2);
00162             *dst++ = mpc7_idx30[t];
00163             *dst++ = mpc7_idx31[t];
00164             *dst++ = mpc7_idx32[t];
00165         }
00166         break;
00167     case 2:
00168         i1 = get_bits1(gb);
00169         for(i = 0; i < SAMPLES_PER_BAND/2; i++){
00170             t = get_vlc2(gb, quant_vlc[1][i1].table, 9, 2);
00171             *dst++ = mpc7_idx50[t];
00172             *dst++ = mpc7_idx51[t];
00173         }
00174         break;
00175     case  3: case  4: case  5: case  6: case  7:
00176         i1 = get_bits1(gb);
00177         for(i = 0; i < SAMPLES_PER_BAND; i++)
00178             *dst++ = get_vlc2(gb, quant_vlc[idx-1][i1].table, 9, 2) - mpc7_quant_vlc_off[idx-1];
00179         break;
00180     case  8: case  9: case 10: case 11: case 12:
00181     case 13: case 14: case 15: case 16: case 17:
00182         t = (1 << (idx - 2)) - 1;
00183         for(i = 0; i < SAMPLES_PER_BAND; i++)
00184             *dst++ = get_bits(gb, idx - 1) - t;
00185         break;
00186     default: // case 0 and -2..-17
00187         return;
00188     }
00189 }
00190 
00191 static int get_scale_idx(GetBitContext *gb, int ref)
00192 {
00193     int t = get_vlc2(gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
00194     if (t == 8)
00195         return get_bits(gb, 6);
00196     return ref + t;
00197 }
00198 
00199 static int mpc7_decode_frame(AVCodecContext * avctx, void *data,
00200                              int *got_frame_ptr, AVPacket *avpkt)
00201 {
00202     const uint8_t *buf = avpkt->data;
00203     int buf_size = avpkt->size;
00204     MPCContext *c = avctx->priv_data;
00205     GetBitContext gb;
00206     uint8_t *bits;
00207     int i, ch;
00208     int mb = -1;
00209     Band *bands = c->bands;
00210     int off, ret;
00211     int bits_used, bits_avail;
00212 
00213     memset(bands, 0, sizeof(*bands) * (c->maxbands + 1));
00214     if(buf_size <= 4){
00215         av_log(avctx, AV_LOG_ERROR, "Too small buffer passed (%i bytes)\n", buf_size);
00216         return AVERROR(EINVAL);
00217     }
00218 
00219     /* get output buffer */
00220     c->frame.nb_samples = buf[1] ? c->lastframelen : MPC_FRAME_SIZE;
00221     if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
00222         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00223         return ret;
00224     }
00225 
00226     bits = av_malloc(((buf_size - 1) & ~3) + FF_INPUT_BUFFER_PADDING_SIZE);
00227     c->dsp.bswap_buf((uint32_t*)bits, (const uint32_t*)(buf + 4), (buf_size - 4) >> 2);
00228     init_get_bits(&gb, bits, (buf_size - 4)* 8);
00229     skip_bits_long(&gb, buf[0]);
00230 
00231     /* read subband indexes */
00232     for(i = 0; i <= c->maxbands; i++){
00233         for(ch = 0; ch < 2; ch++){
00234             int t = 4;
00235             if(i) t = get_vlc2(&gb, hdr_vlc.table, MPC7_HDR_BITS, 1) - 5;
00236             if(t == 4) bands[i].res[ch] = get_bits(&gb, 4);
00237             else bands[i].res[ch] = bands[i-1].res[ch] + t;
00238         }
00239 
00240         if(bands[i].res[0] || bands[i].res[1]){
00241             mb = i;
00242             if(c->MSS) bands[i].msf = get_bits1(&gb);
00243         }
00244     }
00245     /* get scale indexes coding method */
00246     for(i = 0; i <= mb; i++)
00247         for(ch = 0; ch < 2; ch++)
00248             if(bands[i].res[ch]) bands[i].scfi[ch] = get_vlc2(&gb, scfi_vlc.table, MPC7_SCFI_BITS, 1);
00249     /* get scale indexes */
00250     for(i = 0; i <= mb; i++){
00251         for(ch = 0; ch < 2; ch++){
00252             if(bands[i].res[ch]){
00253                 bands[i].scf_idx[ch][2] = c->oldDSCF[ch][i];
00254                 bands[i].scf_idx[ch][0] = get_scale_idx(&gb, bands[i].scf_idx[ch][2]);
00255                 switch(bands[i].scfi[ch]){
00256                 case 0:
00257                     bands[i].scf_idx[ch][1] = get_scale_idx(&gb, bands[i].scf_idx[ch][0]);
00258                     bands[i].scf_idx[ch][2] = get_scale_idx(&gb, bands[i].scf_idx[ch][1]);
00259                     break;
00260                 case 1:
00261                     bands[i].scf_idx[ch][1] = get_scale_idx(&gb, bands[i].scf_idx[ch][0]);
00262                     bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1];
00263                     break;
00264                 case 2:
00265                     bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
00266                     bands[i].scf_idx[ch][2] = get_scale_idx(&gb, bands[i].scf_idx[ch][1]);
00267                     break;
00268                 case 3:
00269                     bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
00270                     break;
00271                 }
00272                 c->oldDSCF[ch][i] = bands[i].scf_idx[ch][2];
00273             }
00274         }
00275     }
00276     /* get quantizers */
00277     memset(c->Q, 0, sizeof(c->Q));
00278     off = 0;
00279     for(i = 0; i < BANDS; i++, off += SAMPLES_PER_BAND)
00280         for(ch = 0; ch < 2; ch++)
00281             idx_to_quant(c, &gb, bands[i].res[ch], c->Q[ch] + off);
00282 
00283     ff_mpc_dequantize_and_synth(c, mb, c->frame.data[0], 2);
00284 
00285     av_free(bits);
00286 
00287     bits_used = get_bits_count(&gb);
00288     bits_avail = (buf_size - 4) * 8;
00289     if(!buf[1] && ((bits_avail < bits_used) || (bits_used + 32 <= bits_avail))){
00290         av_log(NULL,0, "Error decoding frame: used %i of %i bits\n", bits_used, bits_avail);
00291         return -1;
00292     }
00293     if(c->frames_to_skip){
00294         c->frames_to_skip--;
00295         *got_frame_ptr = 0;
00296         return buf_size;
00297     }
00298 
00299     *got_frame_ptr   = 1;
00300     *(AVFrame *)data = c->frame;
00301 
00302     return buf_size;
00303 }
00304 
00305 static void mpc7_decode_flush(AVCodecContext *avctx)
00306 {
00307     MPCContext *c = avctx->priv_data;
00308 
00309     memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
00310     c->frames_to_skip = 32;
00311 }
00312 
00313 AVCodec ff_mpc7_decoder = {
00314     .name           = "mpc7",
00315     .type           = AVMEDIA_TYPE_AUDIO,
00316     .id             = CODEC_ID_MUSEPACK7,
00317     .priv_data_size = sizeof(MPCContext),
00318     .init           = mpc7_decode_init,
00319     .decode         = mpc7_decode_frame,
00320     .flush = mpc7_decode_flush,
00321     .capabilities   = CODEC_CAP_DR1,
00322     .long_name = NULL_IF_CONFIG_SMALL("Musepack SV7"),
00323 };
Generated on Sun Apr 22 2012 21:54:02 for Libav by doxygen 1.7.1