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