📄 vorbis.c
字号:
/** * @file vorbis.c * Vorbis I decoder * @author Denes Balatoni ( dbalatoni programozo hu ) * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */#undef V_DEBUG#include <math.h>#define ALT_BITSTREAM_READER_LE#include "avcodec.h"#include "bitstream.h"#include "dsputil.h"#include "vorbis.h"#define V_NB_BITS 8#define V_NB_BITS2 11#define V_MAX_VLCS (1<<16)#ifndef V_DEBUG#define AV_DEBUG(...)#endif#undef NDEBUG#include <assert.h>/* Helper functions *//** * reads 0-32 bits when using the ALT_BITSTREAM_READER_LE bitstream reader */unsigned int get_bits_long_le(GetBitContext *s, int n){ if(n<=17) return get_bits(s, n); else{ int ret= get_bits(s, 16); return ret | (get_bits(s, n-16) << 16); }}#define ilog(i) av_log2(2*(i))static unsigned int nth_root(unsigned int x, unsigned int n) { // x^(1/n) unsigned int ret=0, i, j; do { ++ret; for(i=0,j=ret;i<n-1;i++) j*=ret; } while (j<=x); return (ret-1);}static float vorbisfloat2float(uint_fast32_t val) { double mant=val&0x1fffff; long exp=(val&0x7fe00000L)>>21; if (val&0x80000000) mant=-mant; return(ldexp(mant, exp-20-768));}// Generate vlc codes from vorbis huffman code lengthsstatic int vorbis_len2vlc(vorbis_context *vc, uint_fast8_t *bits, uint_fast32_t *codes, uint_fast32_t num) { uint_fast32_t exit_at_level[33]={404,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; uint_fast8_t i,j; uint_fast32_t code,p;#ifdef V_DEBUG GetBitContext gb;#endif for(p=0;(bits[p]==0) && (p<num);++p); if (p==num) {// av_log(vc->avccontext, AV_LOG_INFO, "An empty codebook. Heh?! \n"); return 0; } codes[p]=0; for(i=0;i<bits[p];++i) { exit_at_level[i+1]=1<<i; }#ifdef V_DEBUG av_log(vc->avccontext, AV_LOG_INFO, " %d. of %d code len %d code %d - ", p, num, bits[p], codes[p]); init_get_bits(&gb, (uint_fast8_t *)&codes[p], bits[p]); for(i=0;i<bits[p];++i) { av_log(vc->avccontext, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0"); } av_log(vc->avccontext, AV_LOG_INFO, "\n");#endif ++p; for(;p<num;++p) { if (bits[p]==0) continue; // find corresponding exit(node which the tree can grow further from) for(i=bits[p];i>0;--i) { if (exit_at_level[i]) break; } if (!i) return 1; // overspecified tree code=exit_at_level[i]; exit_at_level[i]=0; // construct code (append 0s to end) and introduce new exits for(j=i+1;j<=bits[p];++j) { exit_at_level[j]=code+(1<<(j-1)); } codes[p]=code;#ifdef V_DEBUG av_log(vc->avccontext, AV_LOG_INFO, " %d. code len %d code %d - ", p, bits[p], codes[p]); init_get_bits(&gb, (uint_fast8_t *)&codes[p], bits[p]); for(i=0;i<bits[p];++i) { av_log(vc->avccontext, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0"); } av_log(vc->avccontext, AV_LOG_INFO, "\n");#endif } //FIXME no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC) return 0;}// Free all allocated memory -----------------------------------------static void vorbis_free(vorbis_context *vc) { int_fast16_t i; av_freep(&vc->channel_residues); av_freep(&vc->channel_floors); av_freep(&vc->saved); av_freep(&vc->ret); av_freep(&vc->buf); av_freep(&vc->buf_tmp); av_freep(&vc->residues); av_freep(&vc->modes); ff_mdct_end(&vc->mdct0); ff_mdct_end(&vc->mdct1); for(i=0;i<vc->codebook_count;++i) { av_free(vc->codebooks[i].codevectors); free_vlc(&vc->codebooks[i].vlc); } av_freep(&vc->codebooks); for(i=0;i<vc->floor_count;++i) { av_free(vc->floors[i].x_list); av_free(vc->floors[i].x_list_order); av_free(vc->floors[i].low_neighbour); av_free(vc->floors[i].high_neighbour); } av_freep(&vc->floors); for(i=0;i<vc->mapping_count;++i) { av_free(vc->mappings[i].magnitude); av_free(vc->mappings[i].angle); av_free(vc->mappings[i].mux); } av_freep(&vc->mappings);}// Parse setup header -------------------------------------------------// Process codebooks partstatic int vorbis_parse_setup_hdr_codebooks(vorbis_context *vc) { uint_fast16_t cb; uint_fast8_t *tmp_vlc_bits; uint_fast32_t *tmp_vlc_codes; GetBitContext *gb=&vc->gb; vc->codebook_count=get_bits(gb,8)+1; AV_DEBUG(" Codebooks: %d \n", vc->codebook_count); vc->codebooks=(vorbis_codebook *)av_mallocz(vc->codebook_count * sizeof(vorbis_codebook)); tmp_vlc_bits=(uint_fast8_t *)av_mallocz(V_MAX_VLCS * sizeof(uint_fast8_t)); tmp_vlc_codes=(uint_fast32_t *)av_mallocz(V_MAX_VLCS * sizeof(uint_fast32_t)); for(cb=0;cb<vc->codebook_count;++cb) { vorbis_codebook *codebook_setup=&vc->codebooks[cb]; uint_fast8_t ordered; uint_fast32_t t, used_entries=0; uint_fast32_t entries; AV_DEBUG(" %d. Codebook \n", cb); if (get_bits(gb, 24)!=0x564342) { av_log(vc->avccontext, AV_LOG_ERROR, " %d. Codebook setup data corrupt. \n", cb); goto error; } codebook_setup->dimensions=get_bits(gb, 16); if (codebook_setup->dimensions>16) { av_log(vc->avccontext, AV_LOG_ERROR, " %d. Codebook's dimension is too large (%d). \n", cb, codebook_setup->dimensions); goto error; } entries=get_bits(gb, 24); if (entries>V_MAX_VLCS) { av_log(vc->avccontext, AV_LOG_ERROR, " %d. Codebook has too many entries (%d). \n", cb, entries); goto error; } ordered=get_bits1(gb); AV_DEBUG(" codebook_dimensions %d, codebook_entries %d \n", codebook_setup->dimensions, entries); if (!ordered) { uint_fast16_t ce; uint_fast8_t flag; uint_fast8_t sparse=get_bits1(gb); AV_DEBUG(" not ordered \n"); if (sparse) { AV_DEBUG(" sparse \n"); used_entries=0; for(ce=0;ce<entries;++ce) { flag=get_bits1(gb); if (flag) { tmp_vlc_bits[ce]=get_bits(gb, 5)+1; ++used_entries; } else tmp_vlc_bits[ce]=0; } } else { AV_DEBUG(" not sparse \n"); used_entries=entries; for(ce=0;ce<entries;++ce) { tmp_vlc_bits[ce]=get_bits(gb, 5)+1; } } } else { uint_fast16_t current_entry=0; uint_fast8_t current_length=get_bits(gb, 5)+1; AV_DEBUG(" ordered, current length: %d \n", current_length); //FIXME used_entries=entries; for(;current_entry<used_entries;++current_length) { uint_fast16_t i, number; AV_DEBUG(" number bits: %d ", ilog(entries - current_entry)); number=get_bits(gb, ilog(entries - current_entry)); AV_DEBUG(" number: %d \n", number); for(i=current_entry;i<number+current_entry;++i) { if (i<used_entries) tmp_vlc_bits[i]=current_length; } current_entry+=number; } if (current_entry>used_entries) { av_log(vc->avccontext, AV_LOG_ERROR, " More codelengths than codes in codebook. \n"); goto error; } } codebook_setup->lookup_type=get_bits(gb, 4); AV_DEBUG(" lookup type: %d : %s \n", codebook_setup->lookup_type, codebook_setup->lookup_type ? "vq" : "no lookup" );// If the codebook is used for (inverse) VQ, calculate codevectors. if (codebook_setup->lookup_type==1) { uint_fast16_t i, j, k; uint_fast16_t codebook_lookup_values=nth_root(entries, codebook_setup->dimensions); uint_fast16_t codebook_multiplicands[codebook_lookup_values]; float codebook_minimum_value=vorbisfloat2float(get_bits_long_le(gb, 32)); float codebook_delta_value=vorbisfloat2float(get_bits_long_le(gb, 32)); uint_fast8_t codebook_value_bits=get_bits(gb, 4)+1; uint_fast8_t codebook_sequence_p=get_bits1(gb); AV_DEBUG(" We expect %d numbers for building the codevectors. \n", codebook_lookup_values); AV_DEBUG(" delta %f minmum %f \n", codebook_delta_value, codebook_minimum_value); for(i=0;i<codebook_lookup_values;++i) { codebook_multiplicands[i]=get_bits(gb, codebook_value_bits); AV_DEBUG(" multiplicands*delta+minmum : %e \n", (float)codebook_multiplicands[i]*codebook_delta_value+codebook_minimum_value); AV_DEBUG(" multiplicand %d \n", codebook_multiplicands[i]); }// Weed out unused vlcs and build codevector vector codebook_setup->codevectors=(float *)av_mallocz(used_entries*codebook_setup->dimensions * sizeof(float)); for(j=0, i=0;i<entries;++i) { uint_fast8_t dim=codebook_setup->dimensions; if (tmp_vlc_bits[i]) { float last=0.0; uint_fast32_t lookup_offset=i;#ifdef V_DEBUG av_log(vc->avccontext, AV_LOG_INFO, "Lookup offset %d ,", i);#endif for(k=0;k<dim;++k) { uint_fast32_t multiplicand_offset = lookup_offset % codebook_lookup_values; codebook_setup->codevectors[j*dim+k]=codebook_multiplicands[multiplicand_offset]*codebook_delta_value+codebook_minimum_value+last; if (codebook_sequence_p) { last=codebook_setup->codevectors[j*dim+k]; } lookup_offset/=codebook_lookup_values; } tmp_vlc_bits[j]=tmp_vlc_bits[i];#ifdef V_DEBUG av_log(vc->avccontext, AV_LOG_INFO, "real lookup offset %d, vector: ", j); for(k=0;k<dim;++k) { av_log(vc->avccontext, AV_LOG_INFO, " %f ", codebook_setup->codevectors[j*dim+k]); } av_log(vc->avccontext, AV_LOG_INFO, "\n");#endif ++j; } } if (j!=used_entries) { av_log(vc->avccontext, AV_LOG_ERROR, "Bug in codevector vector building code. \n"); goto error; } entries=used_entries; } else if (codebook_setup->lookup_type>=2) { av_log(vc->avccontext, AV_LOG_ERROR, "Codebook lookup type not supported. \n"); goto error; }// Initialize VLC table if (vorbis_len2vlc(vc, tmp_vlc_bits, tmp_vlc_codes, entries)) { av_log(vc->avccontext, AV_LOG_ERROR, " Invalid code lengths while generating vlcs. \n"); goto error; } codebook_setup->maxdepth=0; for(t=0;t<entries;++t) if (tmp_vlc_bits[t]>=codebook_setup->maxdepth) codebook_setup->maxdepth=tmp_vlc_bits[t]; if(codebook_setup->maxdepth > 3*V_NB_BITS) codebook_setup->nb_bits=V_NB_BITS2; else codebook_setup->nb_bits=V_NB_BITS; codebook_setup->maxdepth=(codebook_setup->maxdepth+codebook_setup->nb_bits-1)/codebook_setup->nb_bits;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -