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