00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "libavutil/random.h"
00029 #include "avcodec.h"
00030 #include "bitstream.h"
00031 #include "dsputil.h"
00032 #include "mpegaudio.h"
00033
00034 #include "mpc.h"
00035 #include "mpc7data.h"
00036
00037 #define BANDS 32
00038 #define SAMPLES_PER_BAND 36
00039 #define MPC_FRAME_SIZE (BANDS * SAMPLES_PER_BAND)
00040
00041 static VLC scfi_vlc, dscf_vlc, hdr_vlc, quant_vlc[MPC7_QUANT_VLC_TABLES][2];
00042
00043 static av_cold int mpc7_decode_init(AVCodecContext * avctx)
00044 {
00045 int i, j;
00046 MPCContext *c = avctx->priv_data;
00047 GetBitContext gb;
00048 uint8_t buf[16];
00049 static int vlc_initialized = 0;
00050
00051 if(avctx->extradata_size < 16){
00052 av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size);
00053 return -1;
00054 }
00055 memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
00056 av_random_init(&c->rnd, 0xDEADBEEF);
00057 dsputil_init(&c->dsp, avctx);
00058 c->dsp.bswap_buf((uint32_t*)buf, (const uint32_t*)avctx->extradata, 4);
00059 ff_mpc_init();
00060 init_get_bits(&gb, buf, 128);
00061
00062 c->IS = get_bits1(&gb);
00063 c->MSS = get_bits1(&gb);
00064 c->maxbands = get_bits(&gb, 6);
00065 if(c->maxbands >= BANDS){
00066 av_log(avctx, AV_LOG_ERROR, "Too many bands: %i\n", c->maxbands);
00067 return -1;
00068 }
00069 skip_bits(&gb, 88);
00070 c->gapless = get_bits1(&gb);
00071 c->lastframelen = get_bits(&gb, 11);
00072 av_log(avctx, AV_LOG_DEBUG, "IS: %d, MSS: %d, TG: %d, LFL: %d, bands: %d\n",
00073 c->IS, c->MSS, c->gapless, c->lastframelen, c->maxbands);
00074 c->frames_to_skip = 0;
00075
00076 if(vlc_initialized) return 0;
00077 av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
00078 if(init_vlc(&scfi_vlc, MPC7_SCFI_BITS, MPC7_SCFI_SIZE,
00079 &mpc7_scfi[1], 2, 1,
00080 &mpc7_scfi[0], 2, 1, INIT_VLC_USE_STATIC)){
00081 av_log(avctx, AV_LOG_ERROR, "Cannot init SCFI VLC\n");
00082 return -1;
00083 }
00084 if(init_vlc(&dscf_vlc, MPC7_DSCF_BITS, MPC7_DSCF_SIZE,
00085 &mpc7_dscf[1], 2, 1,
00086 &mpc7_dscf[0], 2, 1, INIT_VLC_USE_STATIC)){
00087 av_log(avctx, AV_LOG_ERROR, "Cannot init DSCF VLC\n");
00088 return -1;
00089 }
00090 if(init_vlc(&hdr_vlc, MPC7_HDR_BITS, MPC7_HDR_SIZE,
00091 &mpc7_hdr[1], 2, 1,
00092 &mpc7_hdr[0], 2, 1, INIT_VLC_USE_STATIC)){
00093 av_log(avctx, AV_LOG_ERROR, "Cannot init HDR VLC\n");
00094 return -1;
00095 }
00096 for(i = 0; i < MPC7_QUANT_VLC_TABLES; i++){
00097 for(j = 0; j < 2; j++){
00098 if(init_vlc(&quant_vlc[i][j], 9, mpc7_quant_vlc_sizes[i],
00099 &mpc7_quant_vlc[i][j][1], 4, 2,
00100 &mpc7_quant_vlc[i][j][0], 4, 2, INIT_VLC_USE_STATIC)){
00101 av_log(avctx, AV_LOG_ERROR, "Cannot init QUANT VLC %i,%i\n",i,j);
00102 return -1;
00103 }
00104 }
00105 }
00106 vlc_initialized = 1;
00107 avctx->sample_fmt = SAMPLE_FMT_S16;
00108 avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
00109 return 0;
00110 }
00111
00115 static inline void idx_to_quant(MPCContext *c, GetBitContext *gb, int idx, int *dst)
00116 {
00117 int i, i1, t;
00118 switch(idx){
00119 case -1:
00120 for(i = 0; i < SAMPLES_PER_BAND; i++){
00121 *dst++ = (av_random(&c->rnd) & 0x3FC) - 510;
00122 }
00123 break;
00124 case 1:
00125 i1 = get_bits1(gb);
00126 for(i = 0; i < SAMPLES_PER_BAND/3; i++){
00127 t = get_vlc2(gb, quant_vlc[0][i1].table, 9, 2);
00128 *dst++ = mpc7_idx30[t];
00129 *dst++ = mpc7_idx31[t];
00130 *dst++ = mpc7_idx32[t];
00131 }
00132 break;
00133 case 2:
00134 i1 = get_bits1(gb);
00135 for(i = 0; i < SAMPLES_PER_BAND/2; i++){
00136 t = get_vlc2(gb, quant_vlc[1][i1].table, 9, 2);
00137 *dst++ = mpc7_idx50[t];
00138 *dst++ = mpc7_idx51[t];
00139 }
00140 break;
00141 case 3: case 4: case 5: case 6: case 7:
00142 i1 = get_bits1(gb);
00143 for(i = 0; i < SAMPLES_PER_BAND; i++)
00144 *dst++ = get_vlc2(gb, quant_vlc[idx-1][i1].table, 9, 2) - mpc7_quant_vlc_off[idx-1];
00145 break;
00146 case 8: case 9: case 10: case 11: case 12:
00147 case 13: case 14: case 15: case 16: case 17:
00148 t = (1 << (idx - 2)) - 1;
00149 for(i = 0; i < SAMPLES_PER_BAND; i++)
00150 *dst++ = get_bits(gb, idx - 1) - t;
00151 break;
00152 default:
00153 return;
00154 }
00155 }
00156
00157 static int mpc7_decode_frame(AVCodecContext * avctx,
00158 void *data, int *data_size,
00159 const uint8_t * buf, int buf_size)
00160 {
00161 MPCContext *c = avctx->priv_data;
00162 GetBitContext gb;
00163 uint8_t *bits;
00164 int i, ch, t;
00165 int mb = -1;
00166 Band *bands = c->bands;
00167 int off, out_size;
00168 int bits_used, bits_avail;
00169
00170 memset(bands, 0, sizeof(bands));
00171 if(buf_size <= 4){
00172 av_log(avctx, AV_LOG_ERROR, "Too small buffer passed (%i bytes)\n", buf_size);
00173 return AVERROR(EINVAL);
00174 }
00175
00176 out_size = (buf[1] ? c->lastframelen : MPC_FRAME_SIZE) * 4;
00177 if (*data_size < out_size) {
00178 av_log(avctx, AV_LOG_ERROR, "Output buffer is too small\n");
00179 return AVERROR(EINVAL);
00180 }
00181
00182 bits = av_malloc(((buf_size - 1) & ~3) + FF_INPUT_BUFFER_PADDING_SIZE);
00183 c->dsp.bswap_buf((uint32_t*)bits, (const uint32_t*)(buf + 4), (buf_size - 4) >> 2);
00184 init_get_bits(&gb, bits, (buf_size - 4)* 8);
00185 skip_bits(&gb, buf[0]);
00186
00187
00188 for(i = 0; i <= c->maxbands; i++){
00189 for(ch = 0; ch < 2; ch++){
00190 if(i) t = get_vlc2(&gb, hdr_vlc.table, MPC7_HDR_BITS, 1) - 5;
00191 if(!i || (t == 4)) bands[i].res[ch] = get_bits(&gb, 4);
00192 else bands[i].res[ch] = bands[i-1].res[ch] + t;
00193 }
00194
00195 if(bands[i].res[0] || bands[i].res[1]){
00196 mb = i;
00197 if(c->MSS) bands[i].msf = get_bits1(&gb);
00198 }
00199 }
00200
00201 for(i = 0; i <= mb; i++)
00202 for(ch = 0; ch < 2; ch++)
00203 if(bands[i].res[ch]) bands[i].scfi[ch] = get_vlc2(&gb, scfi_vlc.table, MPC7_SCFI_BITS, 1);
00204
00205 for(i = 0; i <= mb; i++){
00206 for(ch = 0; ch < 2; ch++){
00207 if(bands[i].res[ch]){
00208 bands[i].scf_idx[ch][2] = c->oldDSCF[ch][i];
00209 t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
00210 bands[i].scf_idx[ch][0] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][2] + t);
00211 switch(bands[i].scfi[ch]){
00212 case 0:
00213 t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
00214 bands[i].scf_idx[ch][1] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][0] + t);
00215 t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
00216 bands[i].scf_idx[ch][2] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][1] + t);
00217 break;
00218 case 1:
00219 t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
00220 bands[i].scf_idx[ch][1] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][0] + t);
00221 bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1];
00222 break;
00223 case 2:
00224 bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
00225 t = get_vlc2(&gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7;
00226 bands[i].scf_idx[ch][2] = (t == 8) ? get_bits(&gb, 6) : (bands[i].scf_idx[ch][1] + t);
00227 break;
00228 case 3:
00229 bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0];
00230 break;
00231 }
00232 c->oldDSCF[ch][i] = bands[i].scf_idx[ch][2];
00233 }
00234 }
00235 }
00236
00237 memset(c->Q, 0, sizeof(c->Q));
00238 off = 0;
00239 for(i = 0; i < BANDS; i++, off += SAMPLES_PER_BAND)
00240 for(ch = 0; ch < 2; ch++)
00241 idx_to_quant(c, &gb, bands[i].res[ch], c->Q[ch] + off);
00242
00243 ff_mpc_dequantize_and_synth(c, mb, data);
00244
00245 av_free(bits);
00246
00247 bits_used = get_bits_count(&gb);
00248 bits_avail = (buf_size - 4) * 8;
00249 if(!buf[1] && ((bits_avail < bits_used) || (bits_used + 32 <= bits_avail))){
00250 av_log(NULL,0, "Error decoding frame: used %i of %i bits\n", bits_used, bits_avail);
00251 return -1;
00252 }
00253 if(c->frames_to_skip){
00254 c->frames_to_skip--;
00255 *data_size = 0;
00256 return buf_size;
00257 }
00258 *data_size = out_size;
00259
00260 return buf_size;
00261 }
00262
00263 static void mpc7_decode_flush(AVCodecContext *avctx)
00264 {
00265 MPCContext *c = avctx->priv_data;
00266
00267 memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
00268 c->frames_to_skip = 32;
00269 }
00270
00271 AVCodec mpc7_decoder = {
00272 "mpc7",
00273 CODEC_TYPE_AUDIO,
00274 CODEC_ID_MUSEPACK7,
00275 sizeof(MPCContext),
00276 mpc7_decode_init,
00277 NULL,
00278 NULL,
00279 mpc7_decode_frame,
00280 .flush = mpc7_decode_flush,
00281 .long_name = NULL_IF_CONFIG_SMALL("Musepack SV7"),
00282 };