Libav
|
00001 /* 00002 * Musepack SV7 decoder 00003 * Copyright (c) 2006 Konstantin Shishkov 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00028 #include "libavutil/lfg.h" 00029 #include "avcodec.h" 00030 #include "get_bits.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 const uint16_t quant_offsets[MPC7_QUANT_VLC_TABLES*2 + 1] = 00044 { 00045 0, 512, 1024, 1536, 2052, 2564, 3076, 3588, 4100, 4612, 5124, 00046 5636, 6164, 6676, 7224 00047 }; 00048 00049 00050 static av_cold int mpc7_decode_init(AVCodecContext * avctx) 00051 { 00052 int i, j; 00053 MPCContext *c = avctx->priv_data; 00054 GetBitContext gb; 00055 uint8_t buf[16]; 00056 static int vlc_initialized = 0; 00057 00058 static VLC_TYPE scfi_table[1 << MPC7_SCFI_BITS][2]; 00059 static VLC_TYPE dscf_table[1 << MPC7_DSCF_BITS][2]; 00060 static VLC_TYPE hdr_table[1 << MPC7_HDR_BITS][2]; 00061 static VLC_TYPE quant_tables[7224][2]; 00062 00063 if(avctx->extradata_size < 16){ 00064 av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size); 00065 return -1; 00066 } 00067 memset(c->oldDSCF, 0, sizeof(c->oldDSCF)); 00068 av_lfg_init(&c->rnd, 0xDEADBEEF); 00069 dsputil_init(&c->dsp, avctx); 00070 c->dsp.bswap_buf((uint32_t*)buf, (const uint32_t*)avctx->extradata, 4); 00071 ff_mpc_init(); 00072 init_get_bits(&gb, buf, 128); 00073 00074 c->IS = get_bits1(&gb); 00075 c->MSS = get_bits1(&gb); 00076 c->maxbands = get_bits(&gb, 6); 00077 if(c->maxbands >= BANDS){ 00078 av_log(avctx, AV_LOG_ERROR, "Too many bands: %i\n", c->maxbands); 00079 return -1; 00080 } 00081 skip_bits_long(&gb, 88); 00082 c->gapless = get_bits1(&gb); 00083 c->lastframelen = get_bits(&gb, 11); 00084 av_log(avctx, AV_LOG_DEBUG, "IS: %d, MSS: %d, TG: %d, LFL: %d, bands: %d\n", 00085 c->IS, c->MSS, c->gapless, c->lastframelen, c->maxbands); 00086 c->frames_to_skip = 0; 00087 00088 avctx->sample_fmt = SAMPLE_FMT_S16; 00089 avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO; 00090 00091 if(vlc_initialized) return 0; 00092 av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n"); 00093 scfi_vlc.table = scfi_table; 00094 scfi_vlc.table_allocated = 1 << MPC7_SCFI_BITS; 00095 if(init_vlc(&scfi_vlc, MPC7_SCFI_BITS, MPC7_SCFI_SIZE, 00096 &mpc7_scfi[1], 2, 1, 00097 &mpc7_scfi[0], 2, 1, INIT_VLC_USE_NEW_STATIC)){ 00098 av_log(avctx, AV_LOG_ERROR, "Cannot init SCFI VLC\n"); 00099 return -1; 00100 } 00101 dscf_vlc.table = dscf_table; 00102 dscf_vlc.table_allocated = 1 << MPC7_DSCF_BITS; 00103 if(init_vlc(&dscf_vlc, MPC7_DSCF_BITS, MPC7_DSCF_SIZE, 00104 &mpc7_dscf[1], 2, 1, 00105 &mpc7_dscf[0], 2, 1, INIT_VLC_USE_NEW_STATIC)){ 00106 av_log(avctx, AV_LOG_ERROR, "Cannot init DSCF VLC\n"); 00107 return -1; 00108 } 00109 hdr_vlc.table = hdr_table; 00110 hdr_vlc.table_allocated = 1 << MPC7_HDR_BITS; 00111 if(init_vlc(&hdr_vlc, MPC7_HDR_BITS, MPC7_HDR_SIZE, 00112 &mpc7_hdr[1], 2, 1, 00113 &mpc7_hdr[0], 2, 1, INIT_VLC_USE_NEW_STATIC)){ 00114 av_log(avctx, AV_LOG_ERROR, "Cannot init HDR VLC\n"); 00115 return -1; 00116 } 00117 for(i = 0; i < MPC7_QUANT_VLC_TABLES; i++){ 00118 for(j = 0; j < 2; j++){ 00119 quant_vlc[i][j].table = &quant_tables[quant_offsets[i*2 + j]]; 00120 quant_vlc[i][j].table_allocated = quant_offsets[i*2 + j + 1] - quant_offsets[i*2 + j]; 00121 if(init_vlc(&quant_vlc[i][j], 9, mpc7_quant_vlc_sizes[i], 00122 &mpc7_quant_vlc[i][j][1], 4, 2, 00123 &mpc7_quant_vlc[i][j][0], 4, 2, INIT_VLC_USE_NEW_STATIC)){ 00124 av_log(avctx, AV_LOG_ERROR, "Cannot init QUANT VLC %i,%i\n",i,j); 00125 return -1; 00126 } 00127 } 00128 } 00129 vlc_initialized = 1; 00130 return 0; 00131 } 00132 00136 static inline void idx_to_quant(MPCContext *c, GetBitContext *gb, int idx, int *dst) 00137 { 00138 int i, i1, t; 00139 switch(idx){ 00140 case -1: 00141 for(i = 0; i < SAMPLES_PER_BAND; i++){ 00142 *dst++ = (av_lfg_get(&c->rnd) & 0x3FC) - 510; 00143 } 00144 break; 00145 case 1: 00146 i1 = get_bits1(gb); 00147 for(i = 0; i < SAMPLES_PER_BAND/3; i++){ 00148 t = get_vlc2(gb, quant_vlc[0][i1].table, 9, 2); 00149 *dst++ = mpc7_idx30[t]; 00150 *dst++ = mpc7_idx31[t]; 00151 *dst++ = mpc7_idx32[t]; 00152 } 00153 break; 00154 case 2: 00155 i1 = get_bits1(gb); 00156 for(i = 0; i < SAMPLES_PER_BAND/2; i++){ 00157 t = get_vlc2(gb, quant_vlc[1][i1].table, 9, 2); 00158 *dst++ = mpc7_idx50[t]; 00159 *dst++ = mpc7_idx51[t]; 00160 } 00161 break; 00162 case 3: case 4: case 5: case 6: case 7: 00163 i1 = get_bits1(gb); 00164 for(i = 0; i < SAMPLES_PER_BAND; i++) 00165 *dst++ = get_vlc2(gb, quant_vlc[idx-1][i1].table, 9, 2) - mpc7_quant_vlc_off[idx-1]; 00166 break; 00167 case 8: case 9: case 10: case 11: case 12: 00168 case 13: case 14: case 15: case 16: case 17: 00169 t = (1 << (idx - 2)) - 1; 00170 for(i = 0; i < SAMPLES_PER_BAND; i++) 00171 *dst++ = get_bits(gb, idx - 1) - t; 00172 break; 00173 default: // case 0 and -2..-17 00174 return; 00175 } 00176 } 00177 00178 static int get_scale_idx(GetBitContext *gb, int ref) 00179 { 00180 int t = get_vlc2(gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7; 00181 if (t == 8) 00182 return get_bits(gb, 6); 00183 return ref + t; 00184 } 00185 00186 static int mpc7_decode_frame(AVCodecContext * avctx, 00187 void *data, int *data_size, 00188 AVPacket *avpkt) 00189 { 00190 const uint8_t *buf = avpkt->data; 00191 int buf_size = avpkt->size; 00192 MPCContext *c = avctx->priv_data; 00193 GetBitContext gb; 00194 uint8_t *bits; 00195 int i, ch; 00196 int mb = -1; 00197 Band *bands = c->bands; 00198 int off; 00199 int bits_used, bits_avail; 00200 00201 memset(bands, 0, sizeof(bands)); 00202 if(buf_size <= 4){ 00203 av_log(avctx, AV_LOG_ERROR, "Too small buffer passed (%i bytes)\n", buf_size); 00204 } 00205 00206 bits = av_malloc(((buf_size - 1) & ~3) + FF_INPUT_BUFFER_PADDING_SIZE); 00207 c->dsp.bswap_buf((uint32_t*)bits, (const uint32_t*)(buf + 4), (buf_size - 4) >> 2); 00208 init_get_bits(&gb, bits, (buf_size - 4)* 8); 00209 skip_bits(&gb, buf[0]); 00210 00211 /* read subband indexes */ 00212 for(i = 0; i <= c->maxbands; i++){ 00213 for(ch = 0; ch < 2; ch++){ 00214 int t = 4; 00215 if(i) t = get_vlc2(&gb, hdr_vlc.table, MPC7_HDR_BITS, 1) - 5; 00216 if(t == 4) bands[i].res[ch] = get_bits(&gb, 4); 00217 else bands[i].res[ch] = bands[i-1].res[ch] + t; 00218 } 00219 00220 if(bands[i].res[0] || bands[i].res[1]){ 00221 mb = i; 00222 if(c->MSS) bands[i].msf = get_bits1(&gb); 00223 } 00224 } 00225 /* get scale indexes coding method */ 00226 for(i = 0; i <= mb; i++) 00227 for(ch = 0; ch < 2; ch++) 00228 if(bands[i].res[ch]) bands[i].scfi[ch] = get_vlc2(&gb, scfi_vlc.table, MPC7_SCFI_BITS, 1); 00229 /* get scale indexes */ 00230 for(i = 0; i <= mb; i++){ 00231 for(ch = 0; ch < 2; ch++){ 00232 if(bands[i].res[ch]){ 00233 bands[i].scf_idx[ch][2] = c->oldDSCF[ch][i]; 00234 bands[i].scf_idx[ch][0] = get_scale_idx(&gb, bands[i].scf_idx[ch][2]); 00235 switch(bands[i].scfi[ch]){ 00236 case 0: 00237 bands[i].scf_idx[ch][1] = get_scale_idx(&gb, bands[i].scf_idx[ch][0]); 00238 bands[i].scf_idx[ch][2] = get_scale_idx(&gb, bands[i].scf_idx[ch][1]); 00239 break; 00240 case 1: 00241 bands[i].scf_idx[ch][1] = get_scale_idx(&gb, bands[i].scf_idx[ch][0]); 00242 bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1]; 00243 break; 00244 case 2: 00245 bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0]; 00246 bands[i].scf_idx[ch][2] = get_scale_idx(&gb, bands[i].scf_idx[ch][1]); 00247 break; 00248 case 3: 00249 bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0]; 00250 break; 00251 } 00252 c->oldDSCF[ch][i] = bands[i].scf_idx[ch][2]; 00253 } 00254 } 00255 } 00256 /* get quantizers */ 00257 memset(c->Q, 0, sizeof(c->Q)); 00258 off = 0; 00259 for(i = 0; i < BANDS; i++, off += SAMPLES_PER_BAND) 00260 for(ch = 0; ch < 2; ch++) 00261 idx_to_quant(c, &gb, bands[i].res[ch], c->Q[ch] + off); 00262 00263 ff_mpc_dequantize_and_synth(c, mb, data); 00264 00265 av_free(bits); 00266 00267 bits_used = get_bits_count(&gb); 00268 bits_avail = (buf_size - 4) * 8; 00269 if(!buf[1] && ((bits_avail < bits_used) || (bits_used + 32 <= bits_avail))){ 00270 av_log(NULL,0, "Error decoding frame: used %i of %i bits\n", bits_used, bits_avail); 00271 return -1; 00272 } 00273 if(c->frames_to_skip){ 00274 c->frames_to_skip--; 00275 *data_size = 0; 00276 return buf_size; 00277 } 00278 *data_size = (buf[1] ? c->lastframelen : MPC_FRAME_SIZE) * 4; 00279 00280 return buf_size; 00281 } 00282 00283 static void mpc7_decode_flush(AVCodecContext *avctx) 00284 { 00285 MPCContext *c = avctx->priv_data; 00286 00287 memset(c->oldDSCF, 0, sizeof(c->oldDSCF)); 00288 c->frames_to_skip = 32; 00289 } 00290 00291 AVCodec mpc7_decoder = { 00292 "mpc7", 00293 AVMEDIA_TYPE_AUDIO, 00294 CODEC_ID_MUSEPACK7, 00295 sizeof(MPCContext), 00296 mpc7_decode_init, 00297 NULL, 00298 NULL, 00299 mpc7_decode_frame, 00300 .flush = mpc7_decode_flush, 00301 .long_name = NULL_IF_CONFIG_SMALL("Musepack SV7"), 00302 };