Libav 0.7.1
|
00001 /* 00002 * Musepack SV7 decoder 00003 * Copyright (c) 2006 Konstantin Shishkov 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav 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 * Libav 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 Libav; 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 "mpegaudiodsp.h" 00033 #include "libavutil/audioconvert.h" 00034 00035 #include "mpc.h" 00036 #include "mpc7data.h" 00037 00038 #define BANDS 32 00039 #define SAMPLES_PER_BAND 36 00040 #define MPC_FRAME_SIZE (BANDS * SAMPLES_PER_BAND) 00041 00042 static VLC scfi_vlc, dscf_vlc, hdr_vlc, quant_vlc[MPC7_QUANT_VLC_TABLES][2]; 00043 00044 static const uint16_t quant_offsets[MPC7_QUANT_VLC_TABLES*2 + 1] = 00045 { 00046 0, 512, 1024, 1536, 2052, 2564, 3076, 3588, 4100, 4612, 5124, 00047 5636, 6164, 6676, 7224 00048 }; 00049 00050 00051 static av_cold int mpc7_decode_init(AVCodecContext * avctx) 00052 { 00053 int i, j; 00054 MPCContext *c = avctx->priv_data; 00055 GetBitContext gb; 00056 uint8_t buf[16]; 00057 static int vlc_initialized = 0; 00058 00059 static VLC_TYPE scfi_table[1 << MPC7_SCFI_BITS][2]; 00060 static VLC_TYPE dscf_table[1 << MPC7_DSCF_BITS][2]; 00061 static VLC_TYPE hdr_table[1 << MPC7_HDR_BITS][2]; 00062 static VLC_TYPE quant_tables[7224][2]; 00063 00064 if(avctx->extradata_size < 16){ 00065 av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size); 00066 return -1; 00067 } 00068 memset(c->oldDSCF, 0, sizeof(c->oldDSCF)); 00069 av_lfg_init(&c->rnd, 0xDEADBEEF); 00070 dsputil_init(&c->dsp, avctx); 00071 ff_mpadsp_init(&c->mpadsp); 00072 c->dsp.bswap_buf((uint32_t*)buf, (const uint32_t*)avctx->extradata, 4); 00073 ff_mpc_init(); 00074 init_get_bits(&gb, buf, 128); 00075 00076 c->IS = get_bits1(&gb); 00077 c->MSS = get_bits1(&gb); 00078 c->maxbands = get_bits(&gb, 6); 00079 if(c->maxbands >= BANDS){ 00080 av_log(avctx, AV_LOG_ERROR, "Too many bands: %i\n", c->maxbands); 00081 return -1; 00082 } 00083 skip_bits_long(&gb, 88); 00084 c->gapless = get_bits1(&gb); 00085 c->lastframelen = get_bits(&gb, 11); 00086 av_log(avctx, AV_LOG_DEBUG, "IS: %d, MSS: %d, TG: %d, LFL: %d, bands: %d\n", 00087 c->IS, c->MSS, c->gapless, c->lastframelen, c->maxbands); 00088 c->frames_to_skip = 0; 00089 00090 avctx->sample_fmt = AV_SAMPLE_FMT_S16; 00091 avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO; 00092 00093 if(vlc_initialized) return 0; 00094 av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n"); 00095 scfi_vlc.table = scfi_table; 00096 scfi_vlc.table_allocated = 1 << MPC7_SCFI_BITS; 00097 if(init_vlc(&scfi_vlc, MPC7_SCFI_BITS, MPC7_SCFI_SIZE, 00098 &mpc7_scfi[1], 2, 1, 00099 &mpc7_scfi[0], 2, 1, INIT_VLC_USE_NEW_STATIC)){ 00100 av_log(avctx, AV_LOG_ERROR, "Cannot init SCFI VLC\n"); 00101 return -1; 00102 } 00103 dscf_vlc.table = dscf_table; 00104 dscf_vlc.table_allocated = 1 << MPC7_DSCF_BITS; 00105 if(init_vlc(&dscf_vlc, MPC7_DSCF_BITS, MPC7_DSCF_SIZE, 00106 &mpc7_dscf[1], 2, 1, 00107 &mpc7_dscf[0], 2, 1, INIT_VLC_USE_NEW_STATIC)){ 00108 av_log(avctx, AV_LOG_ERROR, "Cannot init DSCF VLC\n"); 00109 return -1; 00110 } 00111 hdr_vlc.table = hdr_table; 00112 hdr_vlc.table_allocated = 1 << MPC7_HDR_BITS; 00113 if(init_vlc(&hdr_vlc, MPC7_HDR_BITS, MPC7_HDR_SIZE, 00114 &mpc7_hdr[1], 2, 1, 00115 &mpc7_hdr[0], 2, 1, INIT_VLC_USE_NEW_STATIC)){ 00116 av_log(avctx, AV_LOG_ERROR, "Cannot init HDR VLC\n"); 00117 return -1; 00118 } 00119 for(i = 0; i < MPC7_QUANT_VLC_TABLES; i++){ 00120 for(j = 0; j < 2; j++){ 00121 quant_vlc[i][j].table = &quant_tables[quant_offsets[i*2 + j]]; 00122 quant_vlc[i][j].table_allocated = quant_offsets[i*2 + j + 1] - quant_offsets[i*2 + j]; 00123 if(init_vlc(&quant_vlc[i][j], 9, mpc7_quant_vlc_sizes[i], 00124 &mpc7_quant_vlc[i][j][1], 4, 2, 00125 &mpc7_quant_vlc[i][j][0], 4, 2, INIT_VLC_USE_NEW_STATIC)){ 00126 av_log(avctx, AV_LOG_ERROR, "Cannot init QUANT VLC %i,%i\n",i,j); 00127 return -1; 00128 } 00129 } 00130 } 00131 vlc_initialized = 1; 00132 return 0; 00133 } 00134 00138 static inline void idx_to_quant(MPCContext *c, GetBitContext *gb, int idx, int *dst) 00139 { 00140 int i, i1, t; 00141 switch(idx){ 00142 case -1: 00143 for(i = 0; i < SAMPLES_PER_BAND; i++){ 00144 *dst++ = (av_lfg_get(&c->rnd) & 0x3FC) - 510; 00145 } 00146 break; 00147 case 1: 00148 i1 = get_bits1(gb); 00149 for(i = 0; i < SAMPLES_PER_BAND/3; i++){ 00150 t = get_vlc2(gb, quant_vlc[0][i1].table, 9, 2); 00151 *dst++ = mpc7_idx30[t]; 00152 *dst++ = mpc7_idx31[t]; 00153 *dst++ = mpc7_idx32[t]; 00154 } 00155 break; 00156 case 2: 00157 i1 = get_bits1(gb); 00158 for(i = 0; i < SAMPLES_PER_BAND/2; i++){ 00159 t = get_vlc2(gb, quant_vlc[1][i1].table, 9, 2); 00160 *dst++ = mpc7_idx50[t]; 00161 *dst++ = mpc7_idx51[t]; 00162 } 00163 break; 00164 case 3: case 4: case 5: case 6: case 7: 00165 i1 = get_bits1(gb); 00166 for(i = 0; i < SAMPLES_PER_BAND; i++) 00167 *dst++ = get_vlc2(gb, quant_vlc[idx-1][i1].table, 9, 2) - mpc7_quant_vlc_off[idx-1]; 00168 break; 00169 case 8: case 9: case 10: case 11: case 12: 00170 case 13: case 14: case 15: case 16: case 17: 00171 t = (1 << (idx - 2)) - 1; 00172 for(i = 0; i < SAMPLES_PER_BAND; i++) 00173 *dst++ = get_bits(gb, idx - 1) - t; 00174 break; 00175 default: // case 0 and -2..-17 00176 return; 00177 } 00178 } 00179 00180 static int get_scale_idx(GetBitContext *gb, int ref) 00181 { 00182 int t = get_vlc2(gb, dscf_vlc.table, MPC7_DSCF_BITS, 1) - 7; 00183 if (t == 8) 00184 return get_bits(gb, 6); 00185 return ref + t; 00186 } 00187 00188 static int mpc7_decode_frame(AVCodecContext * avctx, 00189 void *data, int *data_size, 00190 AVPacket *avpkt) 00191 { 00192 const uint8_t *buf = avpkt->data; 00193 int buf_size = avpkt->size; 00194 MPCContext *c = avctx->priv_data; 00195 GetBitContext gb; 00196 uint8_t *bits; 00197 int i, ch; 00198 int mb = -1; 00199 Band *bands = c->bands; 00200 int off; 00201 int bits_used, bits_avail; 00202 00203 memset(bands, 0, sizeof(bands)); 00204 if(buf_size <= 4){ 00205 av_log(avctx, AV_LOG_ERROR, "Too small buffer passed (%i bytes)\n", buf_size); 00206 } 00207 00208 bits = av_malloc(((buf_size - 1) & ~3) + FF_INPUT_BUFFER_PADDING_SIZE); 00209 c->dsp.bswap_buf((uint32_t*)bits, (const uint32_t*)(buf + 4), (buf_size - 4) >> 2); 00210 init_get_bits(&gb, bits, (buf_size - 4)* 8); 00211 skip_bits_long(&gb, buf[0]); 00212 00213 /* read subband indexes */ 00214 for(i = 0; i <= c->maxbands; i++){ 00215 for(ch = 0; ch < 2; ch++){ 00216 int t = 4; 00217 if(i) t = get_vlc2(&gb, hdr_vlc.table, MPC7_HDR_BITS, 1) - 5; 00218 if(t == 4) bands[i].res[ch] = get_bits(&gb, 4); 00219 else bands[i].res[ch] = bands[i-1].res[ch] + t; 00220 } 00221 00222 if(bands[i].res[0] || bands[i].res[1]){ 00223 mb = i; 00224 if(c->MSS) bands[i].msf = get_bits1(&gb); 00225 } 00226 } 00227 /* get scale indexes coding method */ 00228 for(i = 0; i <= mb; i++) 00229 for(ch = 0; ch < 2; ch++) 00230 if(bands[i].res[ch]) bands[i].scfi[ch] = get_vlc2(&gb, scfi_vlc.table, MPC7_SCFI_BITS, 1); 00231 /* get scale indexes */ 00232 for(i = 0; i <= mb; i++){ 00233 for(ch = 0; ch < 2; ch++){ 00234 if(bands[i].res[ch]){ 00235 bands[i].scf_idx[ch][2] = c->oldDSCF[ch][i]; 00236 bands[i].scf_idx[ch][0] = get_scale_idx(&gb, bands[i].scf_idx[ch][2]); 00237 switch(bands[i].scfi[ch]){ 00238 case 0: 00239 bands[i].scf_idx[ch][1] = get_scale_idx(&gb, bands[i].scf_idx[ch][0]); 00240 bands[i].scf_idx[ch][2] = get_scale_idx(&gb, bands[i].scf_idx[ch][1]); 00241 break; 00242 case 1: 00243 bands[i].scf_idx[ch][1] = get_scale_idx(&gb, bands[i].scf_idx[ch][0]); 00244 bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1]; 00245 break; 00246 case 2: 00247 bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0]; 00248 bands[i].scf_idx[ch][2] = get_scale_idx(&gb, bands[i].scf_idx[ch][1]); 00249 break; 00250 case 3: 00251 bands[i].scf_idx[ch][2] = bands[i].scf_idx[ch][1] = bands[i].scf_idx[ch][0]; 00252 break; 00253 } 00254 c->oldDSCF[ch][i] = bands[i].scf_idx[ch][2]; 00255 } 00256 } 00257 } 00258 /* get quantizers */ 00259 memset(c->Q, 0, sizeof(c->Q)); 00260 off = 0; 00261 for(i = 0; i < BANDS; i++, off += SAMPLES_PER_BAND) 00262 for(ch = 0; ch < 2; ch++) 00263 idx_to_quant(c, &gb, bands[i].res[ch], c->Q[ch] + off); 00264 00265 ff_mpc_dequantize_and_synth(c, mb, data, 2); 00266 00267 av_free(bits); 00268 00269 bits_used = get_bits_count(&gb); 00270 bits_avail = (buf_size - 4) * 8; 00271 if(!buf[1] && ((bits_avail < bits_used) || (bits_used + 32 <= bits_avail))){ 00272 av_log(NULL,0, "Error decoding frame: used %i of %i bits\n", bits_used, bits_avail); 00273 return -1; 00274 } 00275 if(c->frames_to_skip){ 00276 c->frames_to_skip--; 00277 *data_size = 0; 00278 return buf_size; 00279 } 00280 *data_size = (buf[1] ? c->lastframelen : MPC_FRAME_SIZE) * 4; 00281 00282 return buf_size; 00283 } 00284 00285 static void mpc7_decode_flush(AVCodecContext *avctx) 00286 { 00287 MPCContext *c = avctx->priv_data; 00288 00289 memset(c->oldDSCF, 0, sizeof(c->oldDSCF)); 00290 c->frames_to_skip = 32; 00291 } 00292 00293 AVCodec ff_mpc7_decoder = { 00294 "mpc7", 00295 AVMEDIA_TYPE_AUDIO, 00296 CODEC_ID_MUSEPACK7, 00297 sizeof(MPCContext), 00298 mpc7_decode_init, 00299 NULL, 00300 NULL, 00301 mpc7_decode_frame, 00302 .flush = mpc7_decode_flush, 00303 .long_name = NULL_IF_CONFIG_SMALL("Musepack SV7"), 00304 };