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 "mpcdata.h"
00040 #include "mpc8data.h"
00041 #include "mpc8huff.h"
00042
00043 static VLC band_vlc, scfi_vlc[2], dscf_vlc[2], res_vlc[2];
00044 static VLC q1_vlc, q2_vlc[2], q3_vlc[2], quant_vlc[4][2], q9up_vlc;
00045
00046 static const int q3_offsets[2] = { MPC8_Q3_OFFSET, MPC8_Q4_OFFSET };
00047 static const int quant_offsets[6] = { MPC8_Q5_OFFSET, MPC8_Q6_OFFSET, MPC8_Q7_OFFSET, MPC8_Q8_OFFSET };
00048
00049 static inline int mpc8_dec_base(GetBitContext *gb, int k, int n)
00050 {
00051 int code = get_bits(gb, mpc8_cnk_len[k-1][n-1] - 1);
00052
00053 if (code >= mpc8_cnk_lost[k-1][n-1])
00054 code = ((code << 1) | get_bits1(gb)) - mpc8_cnk_lost[k-1][n-1];
00055
00056 return code;
00057 }
00058
00059 static inline int mpc8_dec_enum(GetBitContext *gb, int k, int n)
00060 {
00061 int bits = 0;
00062 const uint32_t * C = mpc8_cnk[k-1];
00063 int code = mpc8_dec_base(gb, k, n);
00064
00065 do {
00066 n--;
00067 if (code >= C[n]) {
00068 bits |= 1 << n;
00069 code -= C[n];
00070 C -= 32;
00071 k--;
00072 }
00073 } while(k > 0);
00074
00075 return bits;
00076 }
00077
00078 static inline int mpc8_get_mod_golomb(GetBitContext *gb, int m)
00079 {
00080 if(mpc8_cnk_len[0][m] < 1) return 0;
00081 return mpc8_dec_base(gb, 1, m+1);
00082 }
00083
00084 static int mpc8_get_mask(GetBitContext *gb, int size, int t)
00085 {
00086 int mask = 0;
00087
00088 if(t && t != size)
00089 mask = mpc8_dec_enum(gb, FFMIN(t, size - t), size);
00090 if((t << 1) > size) mask = ~mask;
00091
00092 return mask;
00093 }
00094
00095 static int mpc8_decode_init(AVCodecContext * avctx)
00096 {
00097 int i;
00098 MPCContext *c = avctx->priv_data;
00099 GetBitContext gb;
00100 static int vlc_inited = 0;
00101
00102 if(avctx->extradata_size < 2){
00103 av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size);
00104 return -1;
00105 }
00106 memset(c->oldDSCF, 0, sizeof(c->oldDSCF));
00107 av_init_random(0xDEADBEEF, &c->rnd);
00108 dsputil_init(&c->dsp, avctx);
00109
00110 ff_mpc_init();
00111
00112 init_get_bits(&gb, avctx->extradata, 16);
00113
00114 skip_bits(&gb, 3);
00115 c->maxbands = get_bits(&gb, 5) + 1;
00116 skip_bits(&gb, 4);
00117 c->MSS = get_bits1(&gb);
00118 c->frames = 1 << (get_bits(&gb, 3) * 2);
00119
00120 if(vlc_inited) return 0;
00121 av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n");
00122
00123 init_vlc(&band_vlc, MPC8_BANDS_BITS, MPC8_BANDS_SIZE,
00124 mpc8_bands_bits, 1, 1,
00125 mpc8_bands_codes, 1, 1, INIT_VLC_USE_STATIC);
00126
00127 init_vlc(&q1_vlc, MPC8_Q1_BITS, MPC8_Q1_SIZE,
00128 mpc8_q1_bits, 1, 1,
00129 mpc8_q1_codes, 1, 1, INIT_VLC_USE_STATIC);
00130 init_vlc(&q9up_vlc, MPC8_Q9UP_BITS, MPC8_Q9UP_SIZE,
00131 mpc8_q9up_bits, 1, 1,
00132 mpc8_q9up_codes, 1, 1, INIT_VLC_USE_STATIC);
00133
00134 init_vlc(&scfi_vlc[0], MPC8_SCFI0_BITS, MPC8_SCFI0_SIZE,
00135 mpc8_scfi0_bits, 1, 1,
00136 mpc8_scfi0_codes, 1, 1, INIT_VLC_USE_STATIC);
00137 init_vlc(&scfi_vlc[1], MPC8_SCFI1_BITS, MPC8_SCFI1_SIZE,
00138 mpc8_scfi1_bits, 1, 1,
00139 mpc8_scfi1_codes, 1, 1, INIT_VLC_USE_STATIC);
00140
00141 init_vlc(&dscf_vlc[0], MPC8_DSCF0_BITS, MPC8_DSCF0_SIZE,
00142 mpc8_dscf0_bits, 1, 1,
00143 mpc8_dscf0_codes, 1, 1, INIT_VLC_USE_STATIC);
00144 init_vlc(&dscf_vlc[1], MPC8_DSCF1_BITS, MPC8_DSCF1_SIZE,
00145 mpc8_dscf1_bits, 1, 1,
00146 mpc8_dscf1_codes, 1, 1, INIT_VLC_USE_STATIC);
00147
00148 init_vlc_sparse(&q3_vlc[0], MPC8_Q3_BITS, MPC8_Q3_SIZE,
00149 mpc8_q3_bits, 1, 1,
00150 mpc8_q3_codes, 1, 1,
00151 mpc8_q3_syms, 1, 1, INIT_VLC_USE_STATIC);
00152 init_vlc_sparse(&q3_vlc[1], MPC8_Q4_BITS, MPC8_Q4_SIZE,
00153 mpc8_q4_bits, 1, 1,
00154 mpc8_q4_codes, 1, 1,
00155 mpc8_q4_syms, 1, 1, INIT_VLC_USE_STATIC);
00156
00157 for(i = 0; i < 2; i++){
00158 init_vlc(&res_vlc[i], MPC8_RES_BITS, MPC8_RES_SIZE,
00159 &mpc8_res_bits[i], 1, 1,
00160 &mpc8_res_codes[i], 1, 1, INIT_VLC_USE_STATIC);
00161
00162 init_vlc(&q2_vlc[i], MPC8_Q2_BITS, MPC8_Q2_SIZE,
00163 &mpc8_q2_bits[i], 1, 1,
00164 &mpc8_q2_codes[i], 1, 1, INIT_VLC_USE_STATIC);
00165
00166 init_vlc(&quant_vlc[0][i], MPC8_Q5_BITS, MPC8_Q5_SIZE,
00167 &mpc8_q5_bits[i], 1, 1,
00168 &mpc8_q5_codes[i], 1, 1, INIT_VLC_USE_STATIC);
00169 init_vlc(&quant_vlc[1][i], MPC8_Q6_BITS, MPC8_Q6_SIZE,
00170 &mpc8_q6_bits[i], 1, 1,
00171 &mpc8_q6_codes[i], 1, 1, INIT_VLC_USE_STATIC);
00172 init_vlc(&quant_vlc[2][i], MPC8_Q7_BITS, MPC8_Q7_SIZE,
00173 &mpc8_q7_bits[i], 1, 1,
00174 &mpc8_q7_codes[i], 1, 1, INIT_VLC_USE_STATIC);
00175 init_vlc(&quant_vlc[3][i], MPC8_Q8_BITS, MPC8_Q8_SIZE,
00176 &mpc8_q8_bits[i], 1, 1,
00177 &mpc8_q8_codes[i], 1, 1, INIT_VLC_USE_STATIC);
00178 }
00179 vlc_inited = 1;
00180 return 0;
00181 }
00182
00183 static int mpc8_decode_frame(AVCodecContext * avctx,
00184 void *data, int *data_size,
00185 const uint8_t * buf, int buf_size)
00186 {
00187 MPCContext *c = avctx->priv_data;
00188 GetBitContext gb2, *gb = &gb2;
00189 int i, j, k, ch, cnt, res, t;
00190 Band *bands = c->bands;
00191 int off;
00192 int maxband, keyframe;
00193 int last[2];
00194
00195 keyframe = c->cur_frame == 0;
00196
00197 if(keyframe){
00198 memset(c->Q, 0, sizeof(c->Q));
00199 c->last_bits_used = 0;
00200 }
00201 init_get_bits(gb, buf, buf_size * 8);
00202 skip_bits(gb, c->last_bits_used & 7);
00203
00204 if(keyframe)
00205 maxband = mpc8_get_mod_golomb(gb, c->maxbands + 1);
00206 else{
00207 maxband = c->last_max_band + get_vlc2(gb, band_vlc.table, MPC8_BANDS_BITS, 2);
00208 if(maxband > 32) maxband -= 33;
00209 }
00210 c->last_max_band = maxband;
00211
00212
00213 if(maxband){
00214 last[0] = last[1] = 0;
00215 for(i = maxband - 1; i >= 0; i--){
00216 for(ch = 0; ch < 2; ch++){
00217 last[ch] = get_vlc2(gb, res_vlc[last[ch] > 2].table, MPC8_RES_BITS, 2) + last[ch];
00218 if(last[ch] > 15) last[ch] -= 17;
00219 bands[i].res[ch] = last[ch];
00220 }
00221 }
00222 if(c->MSS){
00223 int mask;
00224
00225 cnt = 0;
00226 for(i = 0; i < maxband; i++)
00227 if(bands[i].res[0] || bands[i].res[1])
00228 cnt++;
00229 t = mpc8_get_mod_golomb(gb, cnt);
00230 mask = mpc8_get_mask(gb, cnt, t);
00231 for(i = maxband - 1; i >= 0; i--)
00232 if(bands[i].res[0] || bands[i].res[1]){
00233 bands[i].msf = mask & 1;
00234 mask >>= 1;
00235 }
00236 }
00237 }
00238 for(i = maxband; i < c->maxbands; i++)
00239 bands[i].res[0] = bands[i].res[1] = 0;
00240
00241 if(keyframe){
00242 for(i = 0; i < 32; i++)
00243 c->oldDSCF[0][i] = c->oldDSCF[1][i] = 1;
00244 }
00245
00246 for(i = 0; i < maxband; i++){
00247 if(bands[i].res[0] || bands[i].res[1]){
00248 cnt = !!bands[i].res[0] + !!bands[i].res[1] - 1;
00249 if(cnt >= 0){
00250 t = get_vlc2(gb, scfi_vlc[cnt].table, scfi_vlc[cnt].bits, 1);
00251 if(bands[i].res[0]) bands[i].scfi[0] = t >> (2 * cnt);
00252 if(bands[i].res[1]) bands[i].scfi[1] = t & 3;
00253 }
00254 }
00255 }
00256
00257 for(i = 0; i < maxband; i++){
00258 for(ch = 0; ch < 2; ch++){
00259 if(!bands[i].res[ch]) continue;
00260
00261 if(c->oldDSCF[ch][i]){
00262 bands[i].scf_idx[ch][0] = get_bits(gb, 7) - 6;
00263 c->oldDSCF[ch][i] = 0;
00264 }else{
00265 t = get_vlc2(gb, dscf_vlc[1].table, MPC8_DSCF1_BITS, 2);
00266 if(t == 64)
00267 t += get_bits(gb, 6);
00268 bands[i].scf_idx[ch][0] = ((bands[i].scf_idx[ch][2] + t - 25) & 0x7F) - 6;
00269 }
00270 for(j = 0; j < 2; j++){
00271 if((bands[i].scfi[ch] << j) & 2)
00272 bands[i].scf_idx[ch][j + 1] = bands[i].scf_idx[ch][j];
00273 else{
00274 t = get_vlc2(gb, dscf_vlc[0].table, MPC8_DSCF0_BITS, 2);
00275 if(t == 31)
00276 t = 64 + get_bits(gb, 6);
00277 bands[i].scf_idx[ch][j + 1] = ((bands[i].scf_idx[ch][j] + t - 25) & 0x7F) - 6;
00278 }
00279 }
00280 }
00281 }
00282
00283 for(i = 0, off = 0; i < maxband; i++, off += SAMPLES_PER_BAND){
00284 for(ch = 0; ch < 2; ch++){
00285 res = bands[i].res[ch];
00286 switch(res){
00287 case -1:
00288 for(j = 0; j < SAMPLES_PER_BAND; j++)
00289 c->Q[ch][off + j] = (av_random(&c->rnd) & 0x3FC) - 510;
00290 break;
00291 case 0:
00292 break;
00293 case 1:
00294 for(j = 0; j < SAMPLES_PER_BAND; j += SAMPLES_PER_BAND / 2){
00295 cnt = get_vlc2(gb, q1_vlc.table, MPC8_Q1_BITS, 2);
00296 t = mpc8_get_mask(gb, 18, cnt);
00297 for(k = 0; k < SAMPLES_PER_BAND / 2; k++, t <<= 1)
00298 c->Q[ch][off + j + k] = (t & 0x20000) ? (get_bits1(gb) << 1) - 1 : 0;
00299 }
00300 break;
00301 case 2:
00302 cnt = 6;
00303 for(j = 0; j < SAMPLES_PER_BAND; j += 3){
00304 t = get_vlc2(gb, q2_vlc[cnt > 3].table, MPC8_Q2_BITS, 2);
00305 c->Q[ch][off + j + 0] = mpc8_idx50[t];
00306 c->Q[ch][off + j + 1] = mpc8_idx51[t];
00307 c->Q[ch][off + j + 2] = mpc8_idx52[t];
00308 cnt = (cnt >> 1) + mpc8_huffq2[t];
00309 }
00310 break;
00311 case 3:
00312 case 4:
00313 for(j = 0; j < SAMPLES_PER_BAND; j += 2){
00314 t = get_vlc2(gb, q3_vlc[res - 3].table, MPC8_Q3_BITS, 2) + q3_offsets[res - 3];
00315 c->Q[ch][off + j + 1] = t >> 4;
00316 c->Q[ch][off + j + 0] = (t & 8) ? (t & 0xF) - 16 : (t & 0xF);
00317 }
00318 break;
00319 case 5:
00320 case 6:
00321 case 7:
00322 case 8:
00323 cnt = 2 * mpc8_thres[res];
00324 for(j = 0; j < SAMPLES_PER_BAND; j++){
00325 t = get_vlc2(gb, quant_vlc[res - 5][cnt > mpc8_thres[res]].table, quant_vlc[res - 5][cnt > mpc8_thres[res]].bits, 2) + quant_offsets[res - 5];
00326 c->Q[ch][off + j] = t;
00327 cnt = (cnt >> 1) + FFABS(c->Q[ch][off + j]);
00328 }
00329 break;
00330 default:
00331 for(j = 0; j < SAMPLES_PER_BAND; j++){
00332 c->Q[ch][off + j] = get_vlc2(gb, q9up_vlc.table, MPC8_Q9UP_BITS, 2);
00333 if(res != 9){
00334 c->Q[ch][off + j] <<= res - 9;
00335 c->Q[ch][off + j] |= get_bits(gb, res - 9);
00336 }
00337 c->Q[ch][off + j] -= (1 << (res - 2)) - 1;
00338 }
00339 }
00340 }
00341 }
00342
00343 ff_mpc_dequantize_and_synth(c, maxband, data);
00344
00345 c->cur_frame++;
00346
00347 c->last_bits_used = get_bits_count(gb);
00348 if(c->cur_frame >= c->frames)
00349 c->cur_frame = 0;
00350 *data_size = MPC_FRAME_SIZE * 4;
00351
00352 return c->cur_frame ? c->last_bits_used >> 3 : buf_size;
00353 }
00354
00355 AVCodec mpc8_decoder = {
00356 "mpc sv8",
00357 CODEC_TYPE_AUDIO,
00358 CODEC_ID_MUSEPACK8,
00359 sizeof(MPCContext),
00360 mpc8_decode_init,
00361 NULL,
00362 NULL,
00363 mpc8_decode_frame,
00364 };