vorbis.c
来自「linux下的MPEG1」· C语言 代码 · 共 1,815 行 · 第 1/5 页
C
1,815 行
/** * @file vorbis.c * Vorbis I decoder * @author Denes Balatoni ( dbalatoni programozo hu ) * This file is part of FFmpeg. * * FFmpeg 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.1 of the License, or (at your option) any later version. * * FFmpeg 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 FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */#undef V_DEBUG//#define V_DEBUG//#define AV_DEBUG(...) av_log(NULL, AV_LOG_INFO, __VA_ARGS__)#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>typedef struct { uint_fast8_t dimensions; uint_fast8_t lookup_type; uint_fast8_t maxdepth; VLC vlc; float *codevectors; unsigned int nb_bits;} vorbis_codebook;typedef union vorbis_floor_u vorbis_floor_data;typedef struct vorbis_floor0_s vorbis_floor0;typedef struct vorbis_floor1_s vorbis_floor1;struct vorbis_context_s;typedefuint_fast8_t (* vorbis_floor_decode_func) (struct vorbis_context_s *, vorbis_floor_data *, float *);typedef struct { uint_fast8_t floor_type; vorbis_floor_decode_func decode; union vorbis_floor_u { struct vorbis_floor0_s { uint_fast8_t order; uint_fast16_t rate; uint_fast16_t bark_map_size; int_fast32_t * map[2]; uint_fast32_t map_size[2]; uint_fast8_t amplitude_bits; uint_fast8_t amplitude_offset; uint_fast8_t num_books; uint_fast8_t * book_list; float * lsp; } t0; struct vorbis_floor1_s { uint_fast8_t partitions; uint_fast8_t maximum_class; uint_fast8_t partition_class[32]; uint_fast8_t class_dimensions[16]; uint_fast8_t class_subclasses[16]; uint_fast8_t class_masterbook[16]; int_fast16_t subclass_books[16][8]; uint_fast8_t multiplier; uint_fast16_t x_list_dim; floor1_entry_t * list; } t1; } data;} vorbis_floor;typedef struct { uint_fast16_t type; uint_fast32_t begin; uint_fast32_t end; uint_fast32_t partition_size; uint_fast8_t classifications; uint_fast8_t classbook; int_fast16_t books[64][8]; uint_fast8_t maxpass;} vorbis_residue;typedef struct { uint_fast8_t submaps; uint_fast16_t coupling_steps; uint_fast8_t *magnitude; uint_fast8_t *angle; uint_fast8_t *mux; uint_fast8_t submap_floor[16]; uint_fast8_t submap_residue[16];} vorbis_mapping;typedef struct { uint_fast8_t blockflag; uint_fast16_t windowtype; uint_fast16_t transformtype; uint_fast8_t mapping;} vorbis_mode;typedef struct vorbis_context_s { AVCodecContext *avccontext; GetBitContext gb; DSPContext dsp; MDCTContext mdct[2]; uint_fast8_t first_frame; uint_fast32_t version; uint_fast8_t audio_channels; uint_fast32_t audio_samplerate; uint_fast32_t bitrate_maximum; uint_fast32_t bitrate_nominal; uint_fast32_t bitrate_minimum; uint_fast32_t blocksize[2]; const float * win[2]; uint_fast16_t codebook_count; vorbis_codebook *codebooks; uint_fast8_t floor_count; vorbis_floor *floors; uint_fast8_t residue_count; vorbis_residue *residues; uint_fast8_t mapping_count; vorbis_mapping *mappings; uint_fast8_t mode_count; vorbis_mode *modes; uint_fast8_t mode_number; // mode number for the current packet float *channel_residues; float *channel_floors; float *saved; uint_fast16_t saved_start; float *ret; float *buf; float *buf_tmp; uint_fast32_t add_bias; // for float->int conversion uint_fast32_t exp_bias;} vorbis_context;/* Helper functions */#define BARK(x) \ (13.1f*atan(0.00074f*(x))+2.24f*atan(1.85e-8f*(x)*(x))+1e-4f*(x))unsigned int ff_vorbis_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 lengthsint ff_vorbis_len2vlc(uint8_t *bits, uint32_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(NULL, 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(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0"); } av_log(NULL, 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(NULL, 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(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0"); } av_log(NULL, AV_LOG_INFO, "\n");#endif } //no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC) for (p=1; p<33; p++) if (exit_at_level[p]) return 1; return 0;}void ff_vorbis_ready_floor1_list(floor1_entry_t * list, int values) { int i; list[0].sort = 0; list[1].sort = 1; for (i = 2; i < values; i++) { int j; list[i].low = 0; list[i].high = 1; list[i].sort = i; for (j = 2; j < i; j++) { int tmp = list[j].x; if (tmp < list[i].x) { if (tmp > list[list[i].low].x) list[i].low = j; } else { if (tmp < list[list[i].high].x) list[i].high = j; } } } for (i = 0; i < values - 1; i++) { int j; for (j = i + 1; j < values; j++) { if (list[list[i].sort].x > list[list[j].sort].x) { int tmp = list[i].sort; list[i].sort = list[j].sort; list[j].sort = tmp; } } }}// 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->mdct[0]); ff_mdct_end(&vc->mdct[1]); 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) { if(vc->floors[i].floor_type==0) { av_free(vc->floors[i].data.t0.map[0]); av_free(vc->floors[i].data.t0.map[1]); av_free(vc->floors[i].data.t0.book_list); av_free(vc->floors[i].data.t0.lsp); } else { av_free(vc->floors[i].data.t1.list); } } 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); if(vc->exp_bias){ av_freep(&vc->win[0]); av_freep(&vc->win[1]); }}// Parse setup header -------------------------------------------------// Process codebooks partstatic int vorbis_parse_setup_hdr_codebooks(vorbis_context *vc) { uint_fast16_t cb; uint8_t *tmp_vlc_bits; uint32_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=(uint8_t *)av_mallocz(V_MAX_VLCS * sizeof(uint8_t)); tmp_vlc_codes=(uint32_t *)av_mallocz(V_MAX_VLCS * sizeof(uint32_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, " %"PRIdFAST16". Codebook setup data corrupt. \n", cb); goto error;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?