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 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 "internal.h"
00031 #include "get_bits.h"
00032 #include "dsputil.h"
00033 #include "mpegaudiodsp.h"
00034 #include "libavutil/audioconvert.h"
00035 
00036 #include "mpc.h"
00037 #include "mpc8data.h"
00038 #include "mpc8huff.h"
00039 
00040 static VLC band_vlc, scfi_vlc[2], dscf_vlc[2], res_vlc[2];
00041 static VLC q1_vlc, q2_vlc[2], q3_vlc[2], quant_vlc[4][2], q9up_vlc;
00042 
00043 static const int q3_offsets[2] = { MPC8_Q3_OFFSET, MPC8_Q4_OFFSET };
00044 static const int quant_offsets[6] = { MPC8_Q5_OFFSET, MPC8_Q6_OFFSET, MPC8_Q7_OFFSET, MPC8_Q8_OFFSET };
00045 
00046 static inline int mpc8_dec_base(GetBitContext *gb, int k, int n)
00047 {
00048     int len = mpc8_cnk_len[k-1][n-1] - 1;
00049     int code = len ? get_bits_long(gb, len) : 0;
00050 
00051     if (code >= mpc8_cnk_lost[k-1][n-1])
00052         code = ((code << 1) | get_bits1(gb)) - mpc8_cnk_lost[k-1][n-1];
00053 
00054     return code;
00055 }
00056 
00057 static inline int mpc8_dec_enum(GetBitContext *gb, int k, int n)
00058 {
00059     int bits = 0;
00060     const uint32_t * C = mpc8_cnk[k-1];
00061     int code = mpc8_dec_base(gb, k, n);
00062 
00063     do {
00064         n--;
00065         if (code >= C[n]) {
00066             bits |= 1 << n;
00067             code -= C[n];
00068             C -= 32;
00069             k--;
00070         }
00071     } while(k > 0);
00072 
00073     return bits;
00074 }
00075 
00076 static inline int mpc8_get_mod_golomb(GetBitContext *gb, int m)
00077 {
00078     if(mpc8_cnk_len[0][m] < 1) return 0;
00079     return mpc8_dec_base(gb, 1, m+1);
00080 }
00081 
00082 static int mpc8_get_mask(GetBitContext *gb, int size, int t)
00083 {
00084     int mask = 0;
00085 
00086     if(t && t != size)
00087          mask = mpc8_dec_enum(gb, FFMIN(t, size - t), size);
00088     if((t << 1) > size) mask = ~mask;
00089 
00090     return mask;
00091 }
00092 
00093 static const uint16_t vlc_offsets[13] = {
00094     0, 640, 1184, 1748, 2298, 2426, 2554, 3066, 3578, 4106, 4618, 5196, 5708
00095 };
00096 
00097 static av_cold int mpc8_decode_init(AVCodecContext * avctx)
00098 {
00099     int i;
00100     MPCContext *c = avctx->priv_data;
00101     GetBitContext gb;
00102     static int vlc_initialized = 0;
00103     int channels;
00104 
00105     static VLC_TYPE band_table[542][2];
00106     static VLC_TYPE q1_table[520][2];
00107     static VLC_TYPE q9up_table[524][2];
00108     static VLC_TYPE scfi0_table[1 << MPC8_SCFI0_BITS][2];
00109     static VLC_TYPE scfi1_table[1 << MPC8_SCFI1_BITS][2];
00110     static VLC_TYPE dscf0_table[560][2];
00111     static VLC_TYPE dscf1_table[598][2];
00112     static VLC_TYPE q3_0_table[512][2];
00113     static VLC_TYPE q3_1_table[516][2];
00114     static VLC_TYPE codes_table[5708][2];
00115 
00116     if(avctx->extradata_size < 2){
00117         av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size);
00118         return -1;
00119     }
00120     memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
00121     av_lfg_init(&c->rnd, 0xDEADBEEF);
00122     dsputil_init(&c->dsp, avctx);
00123     ff_mpadsp_init(&c->mpadsp);
00124 
00125     ff_mpc_init();
00126 
00127     init_get_bits(&gb, avctx->extradata, 16);
00128 
00129     skip_bits(&gb, 3);//sample rate
00130     c->maxbands = get_bits(&gb, 5) + 1;
00131     channels = get_bits(&gb, 4) + 1;
00132     if (channels > 2) {
00133         av_log_missing_feature(avctx, "Multichannel MPC SV8", 1);
00134         return -1;
00135     }
00136     c->MSS = get_bits1(&gb);
00137     c->frames = 1 << (get_bits(&gb, 3) * 2);
00138 
00139     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00140     avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
00141 
00142     if(vlc_initialized) return 0;
00143     av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
00144 
00145     band_vlc.table = band_table;
00146     band_vlc.table_allocated = 542;
00147     init_vlc(&band_vlc, MPC8_BANDS_BITS, MPC8_BANDS_SIZE,
00148              mpc8_bands_bits,  1, 1,
00149              mpc8_bands_codes, 1, 1, INIT_VLC_USE_NEW_STATIC);
00150 
00151     q1_vlc.table = q1_table;
00152     q1_vlc.table_allocated = 520;
00153     init_vlc(&q1_vlc, MPC8_Q1_BITS, MPC8_Q1_SIZE,
00154              mpc8_q1_bits,  1, 1,
00155              mpc8_q1_codes, 1, 1, INIT_VLC_USE_NEW_STATIC);
00156     q9up_vlc.table = q9up_table;
00157     q9up_vlc.table_allocated = 524;
00158     init_vlc(&q9up_vlc, MPC8_Q9UP_BITS, MPC8_Q9UP_SIZE,
00159              mpc8_q9up_bits,  1, 1,
00160              mpc8_q9up_codes, 1, 1, INIT_VLC_USE_NEW_STATIC);
00161 
00162     scfi_vlc[0].table = scfi0_table;
00163     scfi_vlc[0].table_allocated = 1 << MPC8_SCFI0_BITS;
00164     init_vlc(&scfi_vlc[0], MPC8_SCFI0_BITS, MPC8_SCFI0_SIZE,
00165              mpc8_scfi0_bits,  1, 1,
00166              mpc8_scfi0_codes, 1, 1, INIT_VLC_USE_NEW_STATIC);
00167     scfi_vlc[1].table = scfi1_table;
00168     scfi_vlc[1].table_allocated = 1 << MPC8_SCFI1_BITS;
00169     init_vlc(&scfi_vlc[1], MPC8_SCFI1_BITS, MPC8_SCFI1_SIZE,
00170              mpc8_scfi1_bits,  1, 1,
00171              mpc8_scfi1_codes, 1, 1, INIT_VLC_USE_NEW_STATIC);
00172 
00173     dscf_vlc[0].table = dscf0_table;
00174     dscf_vlc[0].table_allocated = 560;
00175     init_vlc(&dscf_vlc[0], MPC8_DSCF0_BITS, MPC8_DSCF0_SIZE,
00176              mpc8_dscf0_bits,  1, 1,
00177              mpc8_dscf0_codes, 1, 1, INIT_VLC_USE_NEW_STATIC);
00178     dscf_vlc[1].table = dscf1_table;
00179     dscf_vlc[1].table_allocated = 598;
00180     init_vlc(&dscf_vlc[1], MPC8_DSCF1_BITS, MPC8_DSCF1_SIZE,
00181              mpc8_dscf1_bits,  1, 1,
00182              mpc8_dscf1_codes, 1, 1, INIT_VLC_USE_NEW_STATIC);
00183 
00184     q3_vlc[0].table = q3_0_table;
00185     q3_vlc[0].table_allocated = 512;
00186     ff_init_vlc_sparse(&q3_vlc[0], MPC8_Q3_BITS, MPC8_Q3_SIZE,
00187              mpc8_q3_bits,  1, 1,
00188              mpc8_q3_codes, 1, 1,
00189              mpc8_q3_syms,  1, 1, INIT_VLC_USE_NEW_STATIC);
00190     q3_vlc[1].table = q3_1_table;
00191     q3_vlc[1].table_allocated = 516;
00192     ff_init_vlc_sparse(&q3_vlc[1], MPC8_Q4_BITS, MPC8_Q4_SIZE,
00193              mpc8_q4_bits,  1, 1,
00194              mpc8_q4_codes, 1, 1,
00195              mpc8_q4_syms,  1, 1, INIT_VLC_USE_NEW_STATIC);
00196 
00197     for(i = 0; i < 2; i++){
00198         res_vlc[i].table = &codes_table[vlc_offsets[0+i]];
00199         res_vlc[i].table_allocated = vlc_offsets[1+i] - vlc_offsets[0+i];
00200         init_vlc(&res_vlc[i], MPC8_RES_BITS, MPC8_RES_SIZE,
00201                  &mpc8_res_bits[i],  1, 1,
00202                  &mpc8_res_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
00203 
00204         q2_vlc[i].table = &codes_table[vlc_offsets[2+i]];
00205         q2_vlc[i].table_allocated = vlc_offsets[3+i] - vlc_offsets[2+i];
00206         init_vlc(&q2_vlc[i], MPC8_Q2_BITS, MPC8_Q2_SIZE,
00207                  &mpc8_q2_bits[i],  1, 1,
00208                  &mpc8_q2_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
00209 
00210         quant_vlc[0][i].table = &codes_table[vlc_offsets[4+i]];
00211         quant_vlc[0][i].table_allocated = vlc_offsets[5+i] - vlc_offsets[4+i];
00212         init_vlc(&quant_vlc[0][i], MPC8_Q5_BITS, MPC8_Q5_SIZE,
00213                  &mpc8_q5_bits[i],  1, 1,
00214                  &mpc8_q5_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
00215         quant_vlc[1][i].table = &codes_table[vlc_offsets[6+i]];
00216         quant_vlc[1][i].table_allocated = vlc_offsets[7+i] - vlc_offsets[6+i];
00217         init_vlc(&quant_vlc[1][i], MPC8_Q6_BITS, MPC8_Q6_SIZE,
00218                  &mpc8_q6_bits[i],  1, 1,
00219                  &mpc8_q6_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
00220         quant_vlc[2][i].table = &codes_table[vlc_offsets[8+i]];
00221         quant_vlc[2][i].table_allocated = vlc_offsets[9+i] - vlc_offsets[8+i];
00222         init_vlc(&quant_vlc[2][i], MPC8_Q7_BITS, MPC8_Q7_SIZE,
00223                  &mpc8_q7_bits[i],  1, 1,
00224                  &mpc8_q7_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
00225         quant_vlc[3][i].table = &codes_table[vlc_offsets[10+i]];
00226         quant_vlc[3][i].table_allocated = vlc_offsets[11+i] - vlc_offsets[10+i];
00227         init_vlc(&quant_vlc[3][i], MPC8_Q8_BITS, MPC8_Q8_SIZE,
00228                  &mpc8_q8_bits[i],  1, 1,
00229                  &mpc8_q8_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
00230     }
00231     vlc_initialized = 1;
00232 
00233     avcodec_get_frame_defaults(&c->frame);
00234     avctx->coded_frame = &c->frame;
00235 
00236     return 0;
00237 }
00238 
00239 static int mpc8_decode_frame(AVCodecContext * avctx, void *data,
00240                              int *got_frame_ptr, AVPacket *avpkt)
00241 {
00242     const uint8_t *buf = avpkt->data;
00243     int buf_size = avpkt->size;
00244     MPCContext *c = avctx->priv_data;
00245     GetBitContext gb2, *gb = &gb2;
00246     int i, j, k, ch, cnt, res, t;
00247     Band *bands = c->bands;
00248     int off;
00249     int maxband, keyframe;
00250     int last[2];
00251 
00252     /* get output buffer */
00253     c->frame.nb_samples = MPC_FRAME_SIZE;
00254     if ((res = ff_get_buffer(avctx, &c->frame)) < 0) {
00255         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00256         return res;
00257     }
00258 
00259     keyframe = c->cur_frame == 0;
00260 
00261     if(keyframe){
00262         memset(c->Q, 0, sizeof(c->Q));
00263         c->last_bits_used = 0;
00264     }
00265     init_get_bits(gb, buf, buf_size * 8);
00266     skip_bits(gb, c->last_bits_used & 7);
00267 
00268     if(keyframe)
00269         maxband = mpc8_get_mod_golomb(gb, c->maxbands + 1);
00270     else{
00271         maxband = c->last_max_band + get_vlc2(gb, band_vlc.table, MPC8_BANDS_BITS, 2);
00272         if(maxband > 32) maxband -= 33;
00273     }
00274     if(maxband > c->maxbands)
00275         return AVERROR_INVALIDDATA;
00276     c->last_max_band = maxband;
00277 
00278     /* read subband indexes */
00279     if(maxband){
00280         last[0] = last[1] = 0;
00281         for(i = maxband - 1; i >= 0; i--){
00282             for(ch = 0; ch < 2; ch++){
00283                 last[ch] = get_vlc2(gb, res_vlc[last[ch] > 2].table, MPC8_RES_BITS, 2) + last[ch];
00284                 if(last[ch] > 15) last[ch] -= 17;
00285                 bands[i].res[ch] = last[ch];
00286             }
00287         }
00288         if(c->MSS){
00289             int mask;
00290 
00291             cnt = 0;
00292             for(i = 0; i < maxband; i++)
00293                 if(bands[i].res[0] || bands[i].res[1])
00294                     cnt++;
00295             t = mpc8_get_mod_golomb(gb, cnt);
00296             mask = mpc8_get_mask(gb, cnt, t);
00297             for(i = maxband - 1; i >= 0; i--)
00298                 if(bands[i].res[0] || bands[i].res[1]){
00299                     bands[i].msf = mask & 1;
00300                     mask >>= 1;
00301                 }
00302         }
00303     }
00304     for(i = maxband; i < c->maxbands; i++)
00305         bands[i].res[0] = bands[i].res[1] = 0;
00306 
00307     if(keyframe){
00308         for(i = 0; i < 32; i++)
00309             c->oldDSCF[0][i] = c->oldDSCF[1][i] = 1;
00310     }
00311 
00312     for(i = 0; i < maxband; i++){
00313         if(bands[i].res[0] || bands[i].res[1]){
00314             cnt = !!bands[i].res[0] + !!bands[i].res[1] - 1;
00315             if(cnt >= 0){
00316                 t = get_vlc2(gb, scfi_vlc[cnt].table, scfi_vlc[cnt].bits, 1);
00317                 if(bands[i].res[0]) bands[i].scfi[0] = t >> (2 * cnt);
00318                 if(bands[i].res[1]) bands[i].scfi[1] = t & 3;
00319             }
00320         }
00321     }
00322 
00323     for(i = 0; i < maxband; i++){
00324         for(ch = 0; ch < 2; ch++){
00325             if(!bands[i].res[ch]) continue;
00326 
00327             if(c->oldDSCF[ch][i]){
00328                 bands[i].scf_idx[ch][0] = get_bits(gb, 7) - 6;
00329                 c->oldDSCF[ch][i] = 0;
00330             }else{
00331                 t = get_vlc2(gb, dscf_vlc[1].table, MPC8_DSCF1_BITS, 2);
00332                 if(t == 64)
00333                     t += get_bits(gb, 6);
00334                 bands[i].scf_idx[ch][0] = ((bands[i].scf_idx[ch][2] + t - 25) & 0x7F) - 6;
00335             }
00336             for(j = 0; j < 2; j++){
00337                 if((bands[i].scfi[ch] << j) & 2)
00338                     bands[i].scf_idx[ch][j + 1] = bands[i].scf_idx[ch][j];
00339                 else{
00340                     t = get_vlc2(gb, dscf_vlc[0].table, MPC8_DSCF0_BITS, 2);
00341                     if(t == 31)
00342                         t = 64 + get_bits(gb, 6);
00343                     bands[i].scf_idx[ch][j + 1] = ((bands[i].scf_idx[ch][j] + t - 25) & 0x7F) - 6;
00344                 }
00345             }
00346         }
00347     }
00348 
00349     for(i = 0, off = 0; i < maxband; i++, off += SAMPLES_PER_BAND){
00350         for(ch = 0; ch < 2; ch++){
00351             res = bands[i].res[ch];
00352             switch(res){
00353             case -1:
00354                 for(j = 0; j < SAMPLES_PER_BAND; j++)
00355                     c->Q[ch][off + j] = (av_lfg_get(&c->rnd) & 0x3FC) - 510;
00356                 break;
00357             case 0:
00358                 break;
00359             case 1:
00360                 for(j = 0; j < SAMPLES_PER_BAND; j += SAMPLES_PER_BAND / 2){
00361                     cnt = get_vlc2(gb, q1_vlc.table, MPC8_Q1_BITS, 2);
00362                     t = mpc8_get_mask(gb, 18, cnt);
00363                     for(k = 0; k < SAMPLES_PER_BAND / 2; k++, t <<= 1)
00364                         c->Q[ch][off + j + k] = (t & 0x20000) ? (get_bits1(gb) << 1) - 1 : 0;
00365                 }
00366                 break;
00367             case 2:
00368                 cnt = 6;//2*mpc8_thres[res]
00369                 for(j = 0; j < SAMPLES_PER_BAND; j += 3){
00370                     t = get_vlc2(gb, q2_vlc[cnt > 3].table, MPC8_Q2_BITS, 2);
00371                     c->Q[ch][off + j + 0] = mpc8_idx50[t];
00372                     c->Q[ch][off + j + 1] = mpc8_idx51[t];
00373                     c->Q[ch][off + j + 2] = mpc8_idx52[t];
00374                     cnt = (cnt >> 1) + mpc8_huffq2[t];
00375                 }
00376                 break;
00377             case 3:
00378             case 4:
00379                 for(j = 0; j < SAMPLES_PER_BAND; j += 2){
00380                     t = get_vlc2(gb, q3_vlc[res - 3].table, MPC8_Q3_BITS, 2) + q3_offsets[res - 3];
00381                     c->Q[ch][off + j + 1] = t >> 4;
00382                     c->Q[ch][off + j + 0] = (t & 8) ? (t & 0xF) - 16 : (t & 0xF);
00383                 }
00384                 break;
00385             case 5:
00386             case 6:
00387             case 7:
00388             case 8:
00389                 cnt = 2 * mpc8_thres[res];
00390                 for(j = 0; j < SAMPLES_PER_BAND; j++){
00391                     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];
00392                     c->Q[ch][off + j] = t;
00393                     cnt = (cnt >> 1) + FFABS(c->Q[ch][off + j]);
00394                 }
00395                 break;
00396             default:
00397                 for(j = 0; j < SAMPLES_PER_BAND; j++){
00398                     c->Q[ch][off + j] = get_vlc2(gb, q9up_vlc.table, MPC8_Q9UP_BITS, 2);
00399                     if(res != 9){
00400                         c->Q[ch][off + j] <<= res - 9;
00401                         c->Q[ch][off + j] |= get_bits(gb, res - 9);
00402                     }
00403                     c->Q[ch][off + j] -= (1 << (res - 2)) - 1;
00404                 }
00405             }
00406         }
00407     }
00408 
00409     ff_mpc_dequantize_and_synth(c, maxband, c->frame.data[0], avctx->channels);
00410 
00411     c->cur_frame++;
00412 
00413     c->last_bits_used = get_bits_count(gb);
00414     if(c->cur_frame >= c->frames)
00415         c->cur_frame = 0;
00416 
00417     *got_frame_ptr   = 1;
00418     *(AVFrame *)data = c->frame;
00419 
00420     return c->cur_frame ? c->last_bits_used >> 3 : buf_size;
00421 }
00422 
00423 AVCodec ff_mpc8_decoder = {
00424     .name           = "mpc8",
00425     .type           = AVMEDIA_TYPE_AUDIO,
00426     .id             = CODEC_ID_MUSEPACK8,
00427     .priv_data_size = sizeof(MPCContext),
00428     .init           = mpc8_decode_init,
00429     .decode         = mpc8_decode_frame,
00430     .capabilities   = CODEC_CAP_DR1,
00431     .long_name = NULL_IF_CONFIG_SMALL("Musepack SV8"),
00432 };