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