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