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