00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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 if(avctx->extradata_size < 16){
00065 av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size);
00066 return -1;
00067 }
00068 memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
00069 av_lfg_init(&c->rnd, 0xDEADBEEF);
00070 dsputil_init(&c->dsp, avctx);
00071 ff_mpadsp_init(&c->mpadsp);
00072 c->dsp.bswap_buf((uint32_t*)buf, (const uint32_t*)avctx->extradata, 4);
00073 ff_mpc_init();
00074 init_get_bits(&gb, buf, 128);
00075
00076 c->IS = get_bits1(&gb);
00077 c->MSS = get_bits1(&gb);
00078 c->maxbands = get_bits(&gb, 6);
00079 if(c->maxbands >= BANDS){
00080 av_log(avctx, AV_LOG_ERROR, "Too many bands: %i\n", c->maxbands);
00081 return -1;
00082 }
00083 skip_bits_long(&gb, 88);
00084 c->gapless = get_bits1(&gb);
00085 c->lastframelen = get_bits(&gb, 11);
00086 av_log(avctx, AV_LOG_DEBUG, "IS: %d, MSS: %d, TG: %d, LFL: %d, bands: %d\n",
00087 c->IS, c->MSS, c->gapless, c->lastframelen, c->maxbands);
00088 c->frames_to_skip = 0;
00089
00090 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00091 avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
00092
00093 if(vlc_initialized) return 0;
00094 av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
00095 scfi_vlc.table = scfi_table;
00096 scfi_vlc.table_allocated = 1 << MPC7_SCFI_BITS;
00097 if(init_vlc(&scfi_vlc, MPC7_SCFI_BITS, MPC7_SCFI_SIZE,
00098 &mpc7_scfi[1], 2, 1,
00099 &mpc7_scfi[0], 2, 1, INIT_VLC_USE_NEW_STATIC)){
00100 av_log(avctx, AV_LOG_ERROR, "Cannot init SCFI VLC\n");
00101 return -1;
00102 }
00103 dscf_vlc.table = dscf_table;
00104 dscf_vlc.table_allocated = 1 << MPC7_DSCF_BITS;
00105 if(init_vlc(&dscf_vlc, MPC7_DSCF_BITS, MPC7_DSCF_SIZE,
00106 &mpc7_dscf[1], 2, 1,
00107 &mpc7_dscf[0], 2, 1, INIT_VLC_USE_NEW_STATIC)){
00108 av_log(avctx, AV_LOG_ERROR, "Cannot init DSCF VLC\n");
00109 return -1;
00110 }
00111 hdr_vlc.table = hdr_table;
00112 hdr_vlc.table_allocated = 1 << MPC7_HDR_BITS;
00113 if(init_vlc(&hdr_vlc, MPC7_HDR_BITS, MPC7_HDR_SIZE,
00114 &mpc7_hdr[1], 2, 1,
00115 &mpc7_hdr[0], 2, 1, INIT_VLC_USE_NEW_STATIC)){
00116 av_log(avctx, AV_LOG_ERROR, "Cannot init HDR VLC\n");
00117 return -1;
00118 }
00119 for(i = 0; i < MPC7_QUANT_VLC_TABLES; i++){
00120 for(j = 0; j < 2; j++){
00121 quant_vlc[i][j].table = &quant_tables[quant_offsets[i*2 + j]];
00122 quant_vlc[i][j].table_allocated = quant_offsets[i*2 + j + 1] - quant_offsets[i*2 + j];
00123 if(init_vlc(&quant_vlc[i][j], 9, mpc7_quant_vlc_sizes[i],
00124 &mpc7_quant_vlc[i][j][1], 4, 2,
00125 &mpc7_quant_vlc[i][j][0], 4, 2, INIT_VLC_USE_NEW_STATIC)){
00126 av_log(avctx, AV_LOG_ERROR, "Cannot init QUANT VLC %i,%i\n",i,j);
00127 return -1;
00128 }
00129 }
00130 }
00131 vlc_initialized = 1;
00132 return 0;
00133 }
00134
00138 static inline void idx_to_quant(MPCContext *c, GetBitContext *gb, int idx, int *dst)
00139 {
00140 int i, i1, t;
00141 switch(idx){
00142 case -1:
00143 for(i = 0; i < SAMPLES_PER_BAND; i++){
00144 *dst++ = (av_lfg_get(&c->rnd) & 0x3FC) - 510;
00145 }
00146 break;
00147 case 1:
00148 i1 = get_bits1(gb);
00149 for(i = 0; i < SAMPLES_PER_BAND/3; i++){
00150 t = get_vlc2(gb, quant_vlc[0][i1].table, 9, 2);
00151 *dst++ = mpc7_idx30[t];
00152 *dst++ = mpc7_idx31[t];
00153 *dst++ = mpc7_idx32[t];
00154 }
00155 break;
00156 case 2:
00157 i1 = get_bits1(gb);
00158 for(i = 0; i < SAMPLES_PER_BAND/2; i++){
00159 t = get_vlc2(gb, quant_vlc[1][i1].table, 9, 2);
00160 *dst++ = mpc7_idx50[t];
00161 *dst++ = mpc7_idx51[t];
00162 }
00163 break;
00164 case 3: case 4: case 5: case 6: case 7:
00165 i1 = get_bits1(gb);
00166 for(i = 0; i < SAMPLES_PER_BAND; i++)
00167 *dst++ = get_vlc2(gb, quant_vlc[idx-1][i1].table, 9, 2) - mpc7_quant_vlc_off[idx-1];
00168 break;
00169 case 8: case 9: case 10: case 11: case 12:
00170 case 13: case 14: case 15: case 16: case 17:
00171 t = (1 << (idx - 2)) - 1;
00172 for(i = 0; i < SAMPLES_PER_BAND; i++)
00173 *dst++ = get_bits(gb, idx - 1) - t;
00174 break;
00175 default:
00176 return;
00177 }
00178 }
00179
00180 static int get_scale_idx(GetBitContext *gb, int ref)
00181 {
00182 int t = get_vlc2(gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
00183 if (t == 8)
00184 return get_bits(gb, 6);
00185 return ref + t;
00186 }
00187
00188 static int mpc7_decode_frame(AVCodecContext * avctx,
00189 void *data, int *data_size,
00190 AVPacket *avpkt)
00191 {
00192 const uint8_t *buf = avpkt->data;
00193 int buf_size = avpkt->size;
00194 MPCContext *c = avctx->priv_data;
00195 GetBitContext gb;
00196 uint8_t *bits;
00197 int i, ch;
00198 int mb = -1;
00199 Band *bands = c->bands;
00200 int off, out_size;
00201 int bits_used, bits_avail;
00202
00203 memset(bands, 0, sizeof(bands));
00204 if(buf_size <= 4){
00205 av_log(avctx, AV_LOG_ERROR, "Too small buffer passed (%i bytes)\n", buf_size);
00206 return AVERROR(EINVAL);
00207 }
00208
00209 out_size = (buf[1] ? c->lastframelen : MPC_FRAME_SIZE) * 4;
00210 if (*data_size < out_size) {
00211 av_log(avctx, AV_LOG_ERROR, "Output buffer is too small\n");
00212 return AVERROR(EINVAL);
00213 }
00214
00215 bits = av_malloc(((buf_size - 1) & ~3) + FF_INPUT_BUFFER_PADDING_SIZE);
00216 c->dsp.bswap_buf((uint32_t*)bits, (const uint32_t*)(buf + 4), (buf_size - 4) >> 2);
00217 init_get_bits(&gb, bits, (buf_size - 4)* 8);
00218 skip_bits_long(&gb, buf[0]);
00219
00220
00221 for(i = 0; i <= c->maxbands; i++){
00222 for(ch = 0; ch < 2; ch++){
00223 int t = 4;
00224 if(i) t = get_vlc2(&gb, hdr_vlc.table, MPC7_HDR_BITS, 1) - 5;
00225 if(t == 4) bands[i].res[ch] = get_bits(&gb, 4);
00226 else bands[i].res[ch] = bands[i-1].res[ch] + t;
00227 }
00228
00229 if(bands[i].res[0] || bands[i].res[1]){
00230 mb = i;
00231 if(c->MSS) bands[i].msf = get_bits1(&gb);
00232 }
00233 }
00234
00235 for(i = 0; i <= mb; i++)
00236 for(ch = 0; ch < 2; ch++)
00237 if(bands[i].res[ch]) bands[i].scfi[ch] = get_vlc2(&gb, scfi_vlc.table, MPC7_SCFI_BITS, 1);
00238
00239 for(i = 0; i <= mb; i++){
00240 for(ch = 0; ch < 2; ch++){
00241 if(bands[i].res[ch]){
00242 bands[i].scf_idx[ch][2] = c->oldDSCF[ch][i];
00243 bands[i].scf_idx[ch][0] = get_scale_idx(&gb, bands[i].scf_idx[ch][2]);
00244 switch(bands[i].scfi[ch]){
00245 case 0:
00246 bands[i].scf_idx[ch][1] = get_scale_idx(&gb, bands[i].scf_idx[ch][0]);
00247 bands[i].scf_idx[ch][2] = get_scale_idx(&gb, bands[i].scf_idx[ch][1]);
00248 break;
00249 case 1:
00250 bands[i].scf_idx[ch][1] = get_scale_idx(&gb, bands[i].scf_idx[ch][0]);
00251 bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1];
00252 break;
00253 case 2:
00254 bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
00255 bands[i].scf_idx[ch][2] = get_scale_idx(&gb, bands[i].scf_idx[ch][1]);
00256 break;
00257 case 3:
00258 bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
00259 break;
00260 }
00261 c->oldDSCF[ch][i] = bands[i].scf_idx[ch][2];
00262 }
00263 }
00264 }
00265
00266 memset(c->Q, 0, sizeof(c->Q));
00267 off = 0;
00268 for(i = 0; i < BANDS; i++, off += SAMPLES_PER_BAND)
00269 for(ch = 0; ch < 2; ch++)
00270 idx_to_quant(c, &gb, bands[i].res[ch], c->Q[ch] + off);
00271
00272 ff_mpc_dequantize_and_synth(c, mb, data, 2);
00273
00274 av_free(bits);
00275
00276 bits_used = get_bits_count(&gb);
00277 bits_avail = (buf_size - 4) * 8;
00278 if(!buf[1] && ((bits_avail < bits_used) || (bits_used + 32 <= bits_avail))){
00279 av_log(NULL,0, "Error decoding frame: used %i of %i bits\n", bits_used, bits_avail);
00280 return -1;
00281 }
00282 if(c->frames_to_skip){
00283 c->frames_to_skip--;
00284 *data_size = 0;
00285 return buf_size;
00286 }
00287 *data_size = out_size;
00288
00289 return buf_size;
00290 }
00291
00292 static void mpc7_decode_flush(AVCodecContext *avctx)
00293 {
00294 MPCContext *c = avctx->priv_data;
00295
00296 memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
00297 c->frames_to_skip = 32;
00298 }
00299
00300 AVCodec ff_mpc7_decoder = {
00301 "mpc7",
00302 AVMEDIA_TYPE_AUDIO,
00303 CODEC_ID_MUSEPACK7,
00304 sizeof(MPCContext),
00305 mpc7_decode_init,
00306 NULL,
00307 NULL,
00308 mpc7_decode_frame,
00309 .flush = mpc7_decode_flush,
00310 .long_name = NULL_IF_CONFIG_SMALL("Musepack SV7"),
00311 };