diff options
author | Monty <xiphmont@xiph.org> | 2001-11-22 06:21:11 +0000 |
---|---|---|
committer | Monty <xiphmont@xiph.org> | 2001-11-22 06:21:11 +0000 |
commit | 9473cf9fdcbca1a350ade44cb59de687afda5df5 (patch) | |
tree | 2d2f1a6b57ba63ded43fb602c5b93fea7fec5278 | |
parent | 4f02af3cfc348422a9d1362486cc0246e44ea5e3 (diff) | |
download | libvorbis-git-9473cf9fdcbca1a350ade44cb59de687afda5df5.tar.gz |
bitrate management mark II running in 'full blown' mode. Likely still
issues with partially enabled modes. testing continues...
svn path=/branches/branch_monty_20011009/vorbis/; revision=2396
-rw-r--r-- | examples/chaining_example.c | 4 | ||||
-rw-r--r-- | include/vorbis/codec.h | 231 | ||||
-rw-r--r-- | lib/Makefile.am | 43 | ||||
-rw-r--r-- | lib/analysis.c | 22 | ||||
-rw-r--r-- | lib/backends.h | 4 | ||||
-rw-r--r-- | lib/bitrate.c | 403 | ||||
-rw-r--r-- | lib/bitrate.h | 13 | ||||
-rw-r--r-- | lib/block.c | 17 | ||||
-rw-r--r-- | lib/codebook.c | 580 | ||||
-rw-r--r-- | lib/codec_internal.h | 6 | ||||
-rw-r--r-- | lib/floor0.c | 468 | ||||
-rw-r--r-- | lib/floor1.c | 1152 | ||||
-rw-r--r-- | lib/mapping0.c | 309 | ||||
-rw-r--r-- | lib/modes/mode_44c_A.h | 24 | ||||
-rw-r--r-- | lib/res0.c | 74 |
15 files changed, 2834 insertions, 516 deletions
diff --git a/examples/chaining_example.c b/examples/chaining_example.c index 21b6b1c3..d0ce1c81 100644 --- a/examples/chaining_example.c +++ b/examples/chaining_example.c @@ -11,7 +11,7 @@ ******************************************************************** function: illustrate simple use of chained bitstream and vorbisfile.a - last mod: $Id: chaining_example.c,v 1.13.2.1 2001/11/22 06:19:52 xiphmont Exp $ + last mod: $Id: chaining_example.c,v 1.13.2.2 2001/11/22 06:21:05 xiphmont Exp $ ********************************************************************/ @@ -61,8 +61,6 @@ int main(){ printf("\t\theader length: %ld bytes\n",(long) (ov.dataoffsets[i]-ov.offsets[i])); printf("\t\tcompressed length: %ld bytes\n",(long)(ov_raw_total(&ov,i))); - printf("\t\tuncompressed length: %ld bytes\n", - (long)(ov_pcm_total(&ov,i))*vi->channels*2+44); printf("\t\tplay time: %lds\n",(long)ov_time_total(&ov,i)); } diff --git a/include/vorbis/codec.h b/include/vorbis/codec.h new file mode 100644 index 00000000..dbe40ad8 --- /dev/null +++ b/include/vorbis/codec.h @@ -0,0 +1,231 @@ +/******************************************************************** + * * + * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * + * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * + * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * + * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * + * * + * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 * + * by the XIPHOPHORUS Company http://www.xiph.org/ * + + ******************************************************************** + + function: libvorbis codec headers + last mod: $Id: codec.h,v 1.38.6.1 2001/11/22 06:21:06 xiphmont Exp $ + + ********************************************************************/ + +#ifndef _vorbis_codec_h_ +#define _vorbis_codec_h_ + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +#include <ogg/ogg.h> + +typedef struct vorbis_info{ + int version; + int channels; + long rate; + + /* The below bitrate declarations are *hints*. + Combinations of the three values carry the following implications: + + all three set to the same value: + implies a fixed rate bitstream + only nominal set: + implies a VBR stream that averages the nominal bitrate. No hard + upper/lower limit + upper and or lower set: + implies a VBR bitstream that obeys the bitrate limits. nominal + may also be set to give a nominal rate. + none set: + the coder does not care to speculate. + */ + + long bitrate_upper; + long bitrate_nominal; + long bitrate_lower; + long bitrate_window; + + void *codec_setup; +} vorbis_info; + +/* vorbis_dsp_state buffers the current vorbis audio + analysis/synthesis state. The DSP state belongs to a specific + logical bitstream ****************************************************/ +typedef struct vorbis_dsp_state{ + int analysisp; + vorbis_info *vi; + + float **pcm; + float **pcmret; + int pcm_storage; + int pcm_current; + int pcm_returned; + + int preextrapolate; + int eofflag; + + long lW; + long W; + long nW; + long centerW; + + ogg_int64_t granulepos; + ogg_int64_t sequence; + + ogg_int64_t glue_bits; + ogg_int64_t time_bits; + ogg_int64_t floor_bits; + ogg_int64_t res_bits; + + void *backend_state; +} vorbis_dsp_state; + +typedef struct vorbis_block{ + /* necessary stream state for linking to the framing abstraction */ + float **pcm; /* this is a pointer into local storage */ + oggpack_buffer opb; + + long lW; + long W; + long nW; + int pcmend; + int mode; + + int eofflag; + ogg_int64_t granulepos; + ogg_int64_t sequence; + vorbis_dsp_state *vd; /* For read-only access of configuration */ + + /* local storage to avoid remallocing; it's up to the mapping to + structure it */ + void *localstore; + long localtop; + long localalloc; + long totaluse; + struct alloc_chain *reap; + + /* bitmetrics for the frame */ + long glue_bits; + long time_bits; + long floor_bits; + long res_bits; + + void *internal; + +} vorbis_block; + +/* vorbis_block is a single block of data to be processed as part of +the analysis/synthesis stream; it belongs to a specific logical +bitstream, but is independant from other vorbis_blocks belonging to +that logical bitstream. *************************************************/ + +struct alloc_chain{ + void *ptr; + struct alloc_chain *next; +}; + +/* vorbis_info contains all the setup information specific to the + specific compression/decompression mode in progress (eg, + psychoacoustic settings, channel setup, options, codebook + etc). vorbis_info and substructures are in backends.h. +*********************************************************************/ + +/* the comments are not part of vorbis_info so that vorbis_info can be + static storage */ +typedef struct vorbis_comment{ + /* unlimited user comment fields. libvorbis writes 'libvorbis' + whatever vendor is set to in encode */ + char **user_comments; + int *comment_lengths; + int comments; + char *vendor; + +} vorbis_comment; + + +/* libvorbis encodes in two abstraction layers; first we perform DSP + and produce a packet (see docs/analysis.txt). The packet is then + coded into a framed OggSquish bitstream by the second layer (see + docs/framing.txt). Decode is the reverse process; we sync/frame + the bitstream and extract individual packets, then decode the + packet back into PCM audio. + + The extra framing/packetizing is used in streaming formats, such as + files. Over the net (such as with UDP), the framing and + packetization aren't necessary as they're provided by the transport + and the streaming layer is not used */ + +/* Vorbis PRIMITIVES: general ***************************************/ + +extern void vorbis_info_init(vorbis_info *vi); +extern void vorbis_info_clear(vorbis_info *vi); +extern void vorbis_comment_init(vorbis_comment *vc); +extern void vorbis_comment_add(vorbis_comment *vc, char *comment); +extern void vorbis_comment_add_tag(vorbis_comment *vc, + char *tag, char *contents); +extern char *vorbis_comment_query(vorbis_comment *vc, char *tag, int count); +extern int vorbis_comment_query_count(vorbis_comment *vc, char *tag); +extern void vorbis_comment_clear(vorbis_comment *vc); + +extern int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb); +extern int vorbis_block_clear(vorbis_block *vb); +extern void vorbis_dsp_clear(vorbis_dsp_state *v); + +/* Vorbis PRIMITIVES: analysis/DSP layer ****************************/ + +extern int vorbis_analysis_init(vorbis_dsp_state *v,vorbis_info *vi); +extern int vorbis_commentheader_out(vorbis_comment *vc, ogg_packet *op); +extern int vorbis_analysis_headerout(vorbis_dsp_state *v, + vorbis_comment *vc, + ogg_packet *op, + ogg_packet *op_comm, + ogg_packet *op_code); +extern float **vorbis_analysis_buffer(vorbis_dsp_state *v,int vals); +extern int vorbis_analysis_wrote(vorbis_dsp_state *v,int vals); +extern int vorbis_analysis_blockout(vorbis_dsp_state *v,vorbis_block *vb); +extern int vorbis_analysis(vorbis_block *vb,ogg_packet *op); + +extern int vorbis_bitrate_addblock(vorbis_block *vb); +extern int vorbis_bitrate_flushpacket(vorbis_dsp_state *vd, + ogg_packet *op); + +/* Vorbis PRIMITIVES: synthesis layer *******************************/ +extern int vorbis_synthesis_headerin(vorbis_info *vi,vorbis_comment *vc, + ogg_packet *op); + +extern int vorbis_synthesis_init(vorbis_dsp_state *v,vorbis_info *vi); +extern int vorbis_synthesis(vorbis_block *vb,ogg_packet *op); +extern int vorbis_synthesis_blockin(vorbis_dsp_state *v,vorbis_block *vb); +extern int vorbis_synthesis_pcmout(vorbis_dsp_state *v,float ***pcm); +extern int vorbis_synthesis_read(vorbis_dsp_state *v,int samples); +extern long vorbis_packet_blocksize(vorbis_info *vi,ogg_packet *op); + +/* Vorbis ERRORS and return codes ***********************************/ + +#define OV_FALSE -1 +#define OV_EOF -2 +#define OV_HOLE -3 + +#define OV_EREAD -128 +#define OV_EFAULT -129 +#define OV_EIMPL -130 +#define OV_EINVAL -131 +#define OV_ENOTVORBIS -132 +#define OV_EBADHEADER -133 +#define OV_EVERSION -134 +#define OV_ENOTAUDIO -135 +#define OV_EBADPACKET -136 +#define OV_EBADLINK -137 +#define OV_ENOSEEK -138 + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif + diff --git a/lib/Makefile.am b/lib/Makefile.am new file mode 100644 index 00000000..7e94d55c --- /dev/null +++ b/lib/Makefile.am @@ -0,0 +1,43 @@ +## Process this file with automake to produce Makefile.in + +AUTOMAKE_OPTIONS = foreign + +SUBDIRS = modes books + +INCLUDES = -I$(top_srcdir)/include @OGG_CFLAGS@ + +lib_LTLIBRARIES = libvorbis.la libvorbisfile.la libvorbisenc.la + +libvorbis_la_SOURCES = mdct.c smallft.c block.c envelope.c window.c lsp.c \ + lpc.c analysis.c synthesis.c psy.c info.c time0.c \ + floor1.c floor0.c\ + res0.c mapping0.c registry.c codebook.c sharedbook.c\ + lookup.c bitrate.c\ + envelope.h lpc.h lsp.h codebook.h misc.h psy.h\ + masking.h iir.h os.h mdct.h smallft.h\ + registry.h scales.h window.h lookup.h lookup_data.h\ + codec_internal.h backends.h bitrate.h +libvorbis_la_LDFLAGS = -version-info @V_LIB_CURRENT@:@V_LIB_REVISION@:@V_LIB_AGE@ + +libvorbisfile_la_SOURCES = vorbisfile.c +libvorbisfile_la_LDFLAGS = -version-info @VF_LIB_CURRENT@:@VF_LIB_REVISION@:@VF_LIB_AGE@ + +libvorbisenc_la_SOURCES = vorbisenc.c +libvorbisenc_la_LDFLAGS = -version-info @VE_LIB_CURRENT@:@VE_LIB_REVISION@:@VE_LIB_AGE@ + +EXTRA_PROGRAMS = barkmel tone psytune +CLEANFILES = $(EXTRA_PROGRAMS) + +barkmel_SOURCES = barkmel.c +tone_SOURCES = tone.c +psytune_SOURCES = psytune.c +psytune_LDFLAGS = -static +psytune_LDADD = libvorbis.la + +EXTRA_DIST = lookups.pl iir.c + +debug: + $(MAKE) all CFLAGS="@DEBUG@" + +profile: + $(MAKE) all CFLAGS="@PROFILE@" diff --git a/lib/analysis.c b/lib/analysis.c index a0bac6f1..9d5cbdca 100644 --- a/lib/analysis.c +++ b/lib/analysis.c @@ -11,7 +11,7 @@ ******************************************************************** function: single-block PCM analysis mode dispatch - last mod: $Id: analysis.c,v 1.46.4.1 2001/11/16 08:17:04 xiphmont Exp $ + last mod: $Id: analysis.c,v 1.46.4.2 2001/11/22 06:21:07 xiphmont Exp $ ********************************************************************/ @@ -27,12 +27,8 @@ int analysis_noisy=1; -int vorbis_analysis_packetout(vorbis_block *vb,ogg_packet *op){ - return(vorbis_bitrate_flushpacket(&(vb->bms),op)); -} - /* decides between modes, dispatches to the appropriate mapping. */ -int vorbis_analysis(vorbis_block *vb){ +int vorbis_analysis(vorbis_block *vb, ogg_packet *op){ vorbis_dsp_state *vd=vb->vd; backend_lookup_state *b=vd->backend_state; vorbis_info *vi=vd->vi; @@ -49,9 +45,9 @@ int vorbis_analysis(vorbis_block *vb){ oggpack_reset(&vb->opb); /* Encode the packet type */ oggpack_write(&vb->opb,0,1); - + /* currently lazy. Short block dispatches to 0, long to 1. */ - + if(vb->W &&ci->modes>1)mode=1; type=ci->map_type[ci->mode_param[mode]->mapping]; vb->mode=mode; @@ -69,7 +65,15 @@ int vorbis_analysis(vorbis_block *vb){ if((ret=_mapping_P[type]->forward(vb,b->mode[mode]))) return(ret); - return(vorbis_bitrate_addblock(vb)); + if(op){ + op->packet=oggpack_get_buffer(&vb->opb); + op->bytes=oggpack_bytes(&vb->opb); + op->b_o_s=0; + op->e_o_s=vb->eofflag; + op->granulepos=vb->granulepos; + op->packetno=vb->sequence; /* for sake of completeness */ + } + return(0); } /* there was no great place to put this.... */ diff --git a/lib/backends.h b/lib/backends.h index 16c2438c..2f3885d7 100644 --- a/lib/backends.h +++ b/lib/backends.h @@ -12,7 +12,7 @@ function: libvorbis backend and mapping structures; needed for static mode headers - last mod: $Id: backends.h,v 1.10.4.1 2001/10/09 04:34:45 xiphmont Exp $ + last mod: $Id: backends.h,v 1.10.4.2 2001/11/22 06:21:07 xiphmont Exp $ ********************************************************************/ @@ -130,7 +130,7 @@ typedef struct{ long **(*class) (struct vorbis_block *,vorbis_look_residue *, float **,int *,int); int (*forward) (struct vorbis_block *,vorbis_look_residue *, - float **,float **,int *,int,int,long **,double,ogg_uint32_t *); + float **,float **,int *,int,int,long **,ogg_uint32_t *); int (*inverse) (struct vorbis_block *,vorbis_look_residue *, float **,int *,int); } vorbis_func_residue; diff --git a/lib/bitrate.c b/lib/bitrate.c index 2eaa9cfc..1fe8f033 100644 --- a/lib/bitrate.c +++ b/lib/bitrate.c @@ -11,11 +11,13 @@ ******************************************************************** function: bitrate tracking and management - last mod: $Id: bitrate.c,v 1.1.2.1 2001/11/16 08:17:04 xiphmont Exp $ + last mod: $Id: bitrate.c,v 1.1.2.2 2001/11/22 06:21:07 xiphmont Exp $ ********************************************************************/ #include <stdio.h> +#include <stdlib.h> +#include <unistd.h> #include <string.h> #include <math.h> #include <ogg/ogg.h> @@ -61,7 +63,8 @@ static double floater_interpolate(bitrate_manager_state *bm,vorbis_info *vi, static long limit_sum(bitrate_manager_state *bm,int limit){ int i=bm->minmax_stackptr; long acc=bm->minmax_acctotal; - + long bins=bm->queue_bins; + acc-=LIMITBITS(i,0); acc+=LIMITBITS(i,limit); @@ -73,60 +76,71 @@ static long limit_sum(bitrate_manager_state *bm,int limit){ return(acc); } +/* compute bitrate tracking setup, allocate circular packet size queue */ void vorbis_bitrate_init(vorbis_info *vi,bitrate_manager_state *bm){ - /* compute bitrate tracking setup, allocate circular packet size queue */ - { - codec_setup_info *ci=vi->codec_setup; - bitrate_manager_info *bi=ci->bi; - long maxlatency; - - memset(bm,0,sizeof(*bm)); + int i; + codec_setup_info *ci=vi->codec_setup; + bitrate_manager_info *bi=ci->bi; + long maxlatency; + + memset(bm,0,sizeof(*bm)); + + bm->avg_sampledesired=bi->queue_avg_time*vi->rate; + bm->avg_centerdesired=bi->queue_avg_time*vi->rate*bi->queue_avg_center; + bm->minmax_sampledesired=bi->queue_minmax_time*vi->rate; + + if(bm->avg_sampledesired<0)bm->avg_sampledesired=0; + if(bm->avg_centerdesired<0)bm->avg_centerdesired=0; + if(bm->minmax_sampledesired<0)bm->minmax_sampledesired=0; + + /* first find the max possible needed queue size */ + maxlatency=max(bm->avg_sampledesired-bm->avg_centerdesired, + bi->queue_minmax_time)+bm->avg_centerdesired; + + if(maxlatency>0){ + long maxpackets=maxlatency/ci->blocksizes[0]+3; + long bins=BITTRACK_DIVISOR*ci->passlimit[ci->coupling_passes-1]; - bm->avg_sampledesired=bi->queue_avg_time*vi->rate; - bm->avg_centerdesired=bi->queue_avg_time*vi->rate*bi->queue_avg_center; - bm->minmax_sampledesired=bi->queue_minmax_time*vi->rate; - - if(bm->avg_sampledesired<0)bm->avg_sampledesired=0; - if(bm->avg_centerdesired<0)bm->avg_centerdesired=0; - if(bm->minmax_sampledesired<0)bm->minmax_sampledesired=0; - - /* first find the max possible needed queue size */ - maxlatency=max(bm->avg_sampledesired-bm->avg_centerdesired, - bi->queue_minmax_time)+bm->avg_centerdesired; + bm->queue_size=maxpackets; + bm->queue_bins=bins; + bm->queue_binned=_ogg_malloc(maxpackets*bins*sizeof(*bm->queue_binned)); + bm->queue_actual=_ogg_malloc(maxpackets*sizeof(*bm->queue_actual)); - if(maxlatency>0){ - long maxpackets=maxlatency/ci->blocksizes[0]+3; - long bins=BITTRACK_DIVISOR*ci->passlimit[ci->coupling_passes-1]; + if((bi->queue_avgmin>0 || bi->queue_avgmax>0) && + bi->queue_avg_time>0){ - bm->queue_size=maxpackets; - bm->queue_bins=bins; - b->queue_binned=_ogg_malloc(maxpackets*bins*sizeof(*b->queue_binned)); - b->queue_actual=_ogg_malloc(maxpackets*sizeof(*b->queue_actual)); + bm->avg_binacc=_ogg_malloc(bins*sizeof(*bm->avg_binacc)); + bm->avgfloat=bi->avgfloat_initial; - if((bi->queue_avgmin>0 || bi->queue_avgmax>0) && - bi->queue_avg_time>0){ - - bm->avg_binacc=_ogg_malloc(bins*sizeof(*bm->binacc)); - bm->avgfloat=bi->avgfloat_initial; - - - } - - if((bi->queue_hardmin>0 || bi->queue_hardmax>0) && + + } + + if((bi->queue_hardmin>0 || bi->queue_hardmax>0) && bi->queue_minmax_time>0){ - - bm->minmax_binstack=_ogg_malloc((bins+1)*bins*2* - sizeof(bm->minmax_binstack)); - bm->minmax_posstack=_ogg_malloc((bins+1)* - sizeof(bm->minmax_posstack)); - bm->minmax_limitstack=_ogg_malloc((bins+1)* - sizeof(bm->minmax_limitstack)); - } + + bm->minmax_binstack=_ogg_malloc((bins+1)*bins*2* + sizeof(bm->minmax_binstack)); + bm->minmax_posstack=_ogg_malloc((bins+1)* + sizeof(bm->minmax_posstack)); + bm->minmax_limitstack=_ogg_malloc((bins+1)* + sizeof(bm->minmax_limitstack)); } - } + + /* space for the packet queueing */ + bm->queue_packet_buffers=calloc(maxpackets,sizeof(*bm->queue_packet_buffers)); + bm->queue_packets=calloc(maxpackets,sizeof(*bm->queue_packets)); + for(i=0;i<maxpackets;i++) + oggpack_writeinit(bm->queue_packet_buffers+i); + + }else{ + bm->queue_packet_buffers=calloc(1,sizeof(*bm->queue_packet_buffers)); + bm->queue_packets=calloc(1,sizeof(*bm->queue_packets)); + oggpack_writeinit(bm->queue_packet_buffers); + } } void vorbis_bitrate_clear(bitrate_manager_state *bm){ + int i; if(bm){ if(bm->queue_binned)_ogg_free(bm->queue_binned); if(bm->queue_actual)_ogg_free(bm->queue_actual); @@ -134,56 +148,118 @@ void vorbis_bitrate_clear(bitrate_manager_state *bm){ if(bm->minmax_binstack)_ogg_free(bm->minmax_binstack); if(bm->minmax_posstack)_ogg_free(bm->minmax_posstack); if(bm->minmax_limitstack)_ogg_free(bm->minmax_limitstack); + if(bm->queue_packet_buffers){ + if(bm->queue_size==0){ + oggpack_writeclear(bm->queue_packet_buffers); + _ogg_free(bm->queue_packet_buffers); + }else{ + for(i=0;i<bm->queue_size;i++) + oggpack_writeclear(bm->queue_packet_buffers+i); + _ogg_free(bm->queue_packet_buffers); + } + } + if(bm->queue_packets)_ogg_free(bm->queue_packets); memset(bm,0,sizeof(*bm)); } } -/* makes the accumulation buffer available to the encode process */ -ogg_uint32_t vorbis_bitrate_bufferblock(vorbis_block *vb){ - bitrate_manager_state *bm=&vb->bms; - int next_head=bm->queue_head+1; - if(!bm->queue_binned)return(NULL); - if(next_head>bm->queue_size)next_head==0; +int vorbis_bitrate_managed(vorbis_block *vb){ + vorbis_dsp_state *vd=vb->vd; + backend_lookup_state *b=vd->backend_state; + bitrate_manager_state *bm=&b->bms; - /* is there room to add a block? In proper use of the API, this will - never come up... but guard it anyway. */ - if(next_head==bm->avg_tail || next_head==bm->minmax_tail)return(NULL); - return(bm->queue_binned+bm->queue_head*bm->queue_bins); + if(bm->queue_binned)return(1); + return(0); +} + +int vorbis_bitrate_maxmarkers(void){ + return 8*BITTRACK_DIVISOR; } /* finish taking in the block we just processed */ int vorbis_bitrate_addblock(vorbis_block *vb){ - int i,j; - bitrate_manager_state *bm=&vb->bms; + int i; + vorbis_block_internal *vbi=vb->internal; vorbis_dsp_state *vd=vb->vd; - backend_lookup_state *b=vd->backend_state; + backend_lookup_state *b=vd->backend_state; + bitrate_manager_state *bm=&b->bms; vorbis_info *vi=vd->vi; codec_setup_info *ci=vi->codec_setup; - int eofflag=vd->eofflag; + bitrate_manager_info *bi=ci->bi; + int eofflag=vb->eofflag; int head=bm->queue_head; int next_head=head+1; int bins=bm->queue_bins; - int avg_head=head; int minmax_head,new_minmax_head; + long maxbits; ogg_uint32_t *head_ptr; - - if(!bm->queue_binned)return(0); - if(next_head>bm->queue_size)next_head==0; + oggpack_buffer temp; + + /* enforce maxbits; min is enforced in the mapping */ + /*if(bi->queue_hardmax){ + maxbits=bi->queue_hardmax/vi->rate*ci->blocksizes[vb->W?1:0]/2; + if(maxbits<oggpack_bits(&vb->opb)){ + vb->opb.endbyte=maxbits/8; + vb->opb.endbit=maxbits%8; + } + }*/ + + if(!bm->queue_binned){ + oggpack_buffer temp; + /* not a bitrate managed stream, but for API simplicity, we'll + buffer one packet to keep the code path clean */ + + if(bm->queue_head)return(-1); /* one has been submitted without + being claimed */ + bm->queue_head++; + + bm->queue_packets[0].packet=oggpack_get_buffer(&vb->opb); + bm->queue_packets[0].bytes=oggpack_bytes(&vb->opb); + bm->queue_packets[0].b_o_s=0; + bm->queue_packets[0].e_o_s=vb->eofflag; + bm->queue_packets[0].granulepos=vb->granulepos; + bm->queue_packets[0].packetno=vb->sequence; /* for sake of completeness */ + + memcpy(&temp,bm->queue_packet_buffers,sizeof(vb->opb)); + memcpy(bm->queue_packet_buffers,&vb->opb,sizeof(vb->opb)); + memcpy(&vb->opb,&temp,sizeof(vb->opb)); + + return(0); + } + + /* add encoded packet to head */ + if(next_head>=bm->queue_size)next_head=0; head_ptr=bm->queue_binned+bins*head; /* is there room to add a block? In proper use of the API, this will never come up... but guard it anyway */ if(next_head==bm->avg_tail || next_head==bm->minmax_tail)return(-1); - /* add the block to the toplevel queue; the buffer is already set */ - bm->head=next_head; + /* add the block to the toplevel queue */ + bm->queue_head=next_head; bm->queue_actual[head]=(vb->W?0x80000000UL:0); + /* buffer packet fields */ + bm->queue_packets[head].packet=oggpack_get_buffer(&vb->opb); + bm->queue_packets[head].bytes=oggpack_bytes(&vb->opb); + bm->queue_packets[head].b_o_s=0; + bm->queue_packets[head].e_o_s=vb->eofflag; + bm->queue_packets[head].granulepos=vb->granulepos; + bm->queue_packets[head].packetno=vb->sequence; /* for sake of completeness */ + + /* swap packet buffers */ + memcpy(&temp,bm->queue_packet_buffers+head,sizeof(vb->opb)); + memcpy(bm->queue_packet_buffers+head,&vb->opb,sizeof(vb->opb)); + memcpy(&vb->opb,&temp,sizeof(vb->opb)); + + /* save markers */ + memcpy(head_ptr,vbi->packet_markers,sizeof(*head_ptr)*bins); + if(bm->avg_binacc) - minmax_head=bm->avg_center; + new_minmax_head=minmax_head=bm->avg_center; else - minmax_head=head; + new_minmax_head=minmax_head=head; /* the average tracking queue is updated first; its results (if it's in use) are taken into account by the min/max limiter (if min/max @@ -200,18 +276,17 @@ int vorbis_bitrate_addblock(vorbis_block *vb){ /* update the avg tail if needed */ while(bm->avg_sampleacc>bm->avg_sampledesired){ - int samples=(bm->queue_actual[b->avg_tail]&0x80000000UL? - samples=ci->blocksizes[1]>>1: - samples=ci->blocksizes[0]>>1); + int samples= + ci->blocksizes[bm->queue_actual[bm->avg_tail]&0x80000000UL?1:0]>>1; for(i=0;i<bm->queue_bins;i++) - bm->avg_binacc[i]-=bm->queue_binned[bins*bm->queue_tail+i]; + bm->avg_binacc[i]-=bm->queue_binned[bins*bm->avg_tail+i]; bm->avg_sampleacc-=samples; bm->avg_tail++; if(bm->avg_tail>=bm->queue_size)bm->avg_tail=0; } /* update the avg center */ - { + if(bm->avg_centeracc>desired_center){ /* choose the new average floater */ double upper=floater_interpolate(bm,vi,bi->queue_avgmax); double lower=floater_interpolate(bm,vi,bi->queue_avgmin); @@ -220,7 +295,7 @@ int vorbis_bitrate_addblock(vorbis_block *vb){ if(upper>0. && upper<new)new=upper; if(lower<bi->avgfloat_minimum) - lower=bi->bitrate_avgfloat_minimum; + lower=bi->avgfloat_minimum; if(lower>new)new=lower; slew=new-bm->avgfloat; @@ -236,10 +311,11 @@ int vorbis_bitrate_addblock(vorbis_block *vb){ /* apply the average floater to new blocks */ bin=bm->avgfloat*BITTRACK_DIVISOR; /* truncate on purpose */ + //fprintf(stderr,"float:%d ",bin); while(bm->avg_centeracc>desired_center){ - int samples=(bm->queue_actual[b->avg_tail]&0x80000000UL? - samples=ci->blocksizes[1]>>1: - samples=ci->blocksizes[0]>>1); + int samples= + samples=ci->blocksizes[bm->queue_actual[bm->avg_center]& + 0x80000000UL?1:0]>>1; bm->queue_actual[bm->avg_center]|=bin; @@ -278,29 +354,29 @@ int vorbis_bitrate_addblock(vorbis_block *vb){ if(bm->avgnoise<0) bm->noisetrigger_request= +1.; - if(bm->avgnoise<bi->avgfloat_noisetrigger_minoff) - bm->avgnoise=bi->avgfloat_noisetrigger_minoff; - if(bm->avgnoise>bi->avgfloat_noisetrigger_maxoff) - bm->avgnoise=bi->avgfloat_noisetrigger_maxoff; + if(bm->avgnoise<bi->avgfloat_noise_minval) + bm->avgnoise=bi->avgfloat_noise_minval; + if(bm->avgnoise>bi->avgfloat_noise_maxval) + bm->avgnoise=bi->avgfloat_noise_maxval; } } }else{ /* if we're not using an average tracker, the 'float' is nailed to the avgfloat_initial value. It needs to be set for the min/max to deal properly */ - bin=bi->avgfloat_initial*BITTRACK_DIVISOR; /* truncate on purpose */ + long bin=bi->avgfloat_initial*BITTRACK_DIVISOR; /* truncate on purpose */ bm->queue_actual[head]|=bin; - new_minmax_head=new_head; + new_minmax_head=next_head; } /* update the min/max queues and enforce limits */ if(bm->minmax_binstack){ - + long sampledesired=eofflag?0:bm->minmax_sampledesired; + /* add to stack recent */ while(minmax_head!=new_minmax_head){ - int samples=(vb->W? - samples=ci->blocksizes[1]>>1: - samples=ci->blocksizes[0]>>1); + int samples=ci->blocksizes[bm->queue_actual[minmax_head]& + 0x80000000UL?1:0]>>1; /* the construction here is not parallel to the floater's stack. @@ -328,15 +404,17 @@ int vorbis_bitrate_addblock(vorbis_block *vb){ (bm->queue_actual[minmax_head]&0x7fffffffUL)>i+1? (bm->queue_actual[minmax_head]&0x7fffffffUL):i+1); - bm->minmax_binstack[bm->max_stackptr*bins*2+i]+= + bm->minmax_binstack[bm->minmax_stackptr*bins*2+i]+= BINBITS(minmax_head, (bm->queue_actual[minmax_head]&0x7fffffffUL)<i+1? (bm->queue_actual[minmax_head]&0x7fffffffUL):i+1); } - bm->minmax_posstack[bm->max_stackptr]=minmax_head; /* not one past like - typical */ - bm->minmax_limitstack[bm->max_stackptr]=0; + bm->minmax_posstack[bm->minmax_stackptr]=minmax_head; /* not one + past + like + typical */ + bm->minmax_limitstack[bm->minmax_stackptr]=0; bm->minmax_sampleacc+=samples; bm->minmax_acctotal+= BINBITS(minmax_head,(bm->queue_actual[minmax_head]&0x7fffffffUL)); @@ -345,70 +423,32 @@ int vorbis_bitrate_addblock(vorbis_block *vb){ if(minmax_head>=bm->queue_size)minmax_head=0; } - /* remove from tail */ - while(bm->minmax_sampleacc>bm->minmax_sampledesired){ - int samples=(bm->queue_actual[bm->minmax_tail]&0x80000000UL? - samples=ci->blocksizes[1]>>1: - samples=ci->blocksizes[0]>>1); - int actual=bm->queue_actual[bm->minmax_tail]&0x7fffffffUL; - - for(i=0;i<bins;i++){ - bm->min_binstack[bins+i]-= /* always comes off the stack bottom */ - BINBITS(bm->minmax_tail, - (bm->queue_actual[bm->minmax_tail]&0x7fffffffUL)>i+1? - (bm->queue_actual[bm->minmax_tail]&0x7fffffffUL):i+1); - bm->max_binstack[i]-= - BINBITS(bm->minmax_tail, - (bm->queue_actual[bm->minmax_tail]&0x7fffffffUL)<i+1? - (bm->queue_actual[bm->minmax_tail]&0x7fffffffUL):i+1); - } - - if(bm->minmax_tail==bm->minmax_posstack[0]){ - /* the stack becomes a FILO; we've fallen off the end of the - first data */ - memmove(bm->minmax_binstack,bm->minmax_binstack+bins*2, - sizeof(*bm->minmax_binstack)*bins*2*bm->minmax_stackptr); - memmove(bm->minmax_posstack,bm->minmax_posstack+1, - sizeof(*bm->minmax_posstack)*bm->minmax_stackptr); - memmove(bm->minmax_limitstack,bm->minmax_limitstack+1, - sizeof(*bm->minmax_limitstack)*bm->minmax_stackptr); - bm->minmax_stackptr--; - } - - /* always perform in this order; max overrules min */ - if(bm->minmax_limitstack[0]>actual) - actual=bm->minmax_limitstack[0]; - if(bins+bm->minmax_limitstack[0]<actual) - actual=bins+bm->minmax_limitstack[0]; - - bm->minmax_acctotal-=BINBITS(bm->minmax_tail,actual); - bm->minmax_sampleacc-=samples; - bm->minmax_tail++; - if(bm->minmax_tail>bm->queue_size)bm->minmax_tail=bm->queue_size; - } - /* check limits, enforce changes */ - if(bm->minmax_binstack){ + if(bm->minmax_sampleacc>sampledesired){ double bitrate=(double)bm->minmax_acctotal/bm->minmax_sampleacc*vi->rate; int limit=0; - if(bitrate>bi->queue_hardmax || bitrate<bi->queue_hardmax){ - int limit=0; + + //fprintf(stderr,"prelimit:%dkbps ",(int)bitrate); + if(bitrate>bi->queue_hardmax || bitrate<bi->queue_hardmin){ int newstack; - int stcakctr; + int stackctr; + long bitsum=limit_sum(bm,0); + bitrate=(double)bitsum/bm->minmax_sampleacc*vi->rate; + /* we're off rate. Iteratively try out new hard floater limits until we find one that brings us inside. Here's where we see the whole point of the limit stacks. */ if(bitrate>bi->queue_hardmax){ - for(limit=-1;limit>=-bins;limit--){ + for(limit=-1;limit>-bins;limit--){ long bitsum=limit_sum(bm,limit); bitrate=(double)bitsum/bm->minmax_sampleacc*vi->rate; if(bitrate<=bi->queue_hardmax)break; } }else if(bitrate<bi->queue_hardmin){ - for(limit=1;limit<bins*2;limit++){ + for(limit=1;limit<bins;limit++){ long bitsum=limit_sum(bm,limit); bitrate=(double)bitsum/bm->minmax_sampleacc*vi->rate; - if(bitrate>=bi->queue_hardmax)break; + if(bitrate>=bi->queue_hardmin)break; } } @@ -418,7 +458,7 @@ int vorbis_bitrate_addblock(vorbis_block *vb){ if(bm->minmax_limitstack[newstack]<limit)break; newstack--; } - + /* update bit counter with new limit and replace any stack limits that have been replaced by our new lower limit */ stackctr=bm->minmax_stackptr; @@ -437,6 +477,7 @@ int vorbis_bitrate_addblock(vorbis_block *vb){ stackctr++; bm->minmax_posstack[stackctr]=bm->minmax_posstack[bm->minmax_stackptr]; bm->minmax_limitstack[stackctr]=limit; + //fprintf(stderr,"limit:%d\n",limit); /* set up new blank stack entry */ stackctr++; @@ -446,27 +487,89 @@ int vorbis_bitrate_addblock(vorbis_block *vb){ sizeof(*bm->minmax_binstack)*bins*2); bm->minmax_limitstack[stackctr]=0; bm->minmax_posstack[stackctr]=-1; - + } } + + /* remove from tail */ + while(bm->minmax_sampleacc>sampledesired){ + int samples= + ci->blocksizes[bm->queue_actual[bm->minmax_tail]&0x80000000UL?1:0]>>1; + int actual=bm->queue_actual[bm->minmax_tail]&0x7fffffffUL; + for(i=0;i<bins;i++){ + bm->minmax_binstack[bins+i]-= /* always comes off the stack bottom */ + BINBITS(bm->minmax_tail,actual>i+1?actual:i+1); + bm->minmax_binstack[i]-= + BINBITS(bm->minmax_tail,actual<i+1?actual:i+1); + } + + /* always perform in this order; max overrules min */ + if(bm->minmax_limitstack[0]>actual) + actual=bm->minmax_limitstack[0]; + if(bins+bm->minmax_limitstack[0]<actual) + actual=bins+bm->minmax_limitstack[0]; + + bm->minmax_acctotal-=BINBITS(bm->minmax_tail,actual); + bm->minmax_sampleacc-=samples; + + /* revise queue_actual to reflect the limit */ + bm->queue_actual[bm->minmax_tail]=actual; + + if(bm->minmax_tail==bm->minmax_posstack[0]){ + /* the stack becomes a FIFO; the first data has fallen off */ + memmove(bm->minmax_binstack,bm->minmax_binstack+bins*2, + sizeof(*bm->minmax_binstack)*bins*2*bm->minmax_stackptr); + memmove(bm->minmax_posstack,bm->minmax_posstack+1, + sizeof(*bm->minmax_posstack)*bm->minmax_stackptr); + memmove(bm->minmax_limitstack,bm->minmax_limitstack+1, + sizeof(*bm->minmax_limitstack)*bm->minmax_stackptr); + bm->minmax_stackptr--; + } + + bm->minmax_tail++; + if(bm->minmax_tail>=bm->queue_size)bm->minmax_tail=0; + } + + bm->last_to_flush=bm->minmax_tail; }else{ bm->last_to_flush=bm->avg_center; } if(eofflag) - bm->last_to_flush=bm->head; + bm->last_to_flush=bm->queue_head; return(0); } -int vorbis_bitrate_flushpacket(bitrate_manager_state *bm, vorbis_packet *op){ - vorbis_dsp_state *vd=vb->vd; - backend_lookup_state *b=vd->backend_state; - vorbis_info *vi=vd->vi; - codec_setup_info *ci=vi->codec_setup; - int eofflag=vd->eofflag; +int vorbis_bitrate_flushpacket(vorbis_dsp_state *vd,ogg_packet *op){ + backend_lookup_state *b=vd->backend_state; + bitrate_manager_state *bm=&b->bms; + + if(bm->queue_size==0){ + if(bm->queue_head==0)return(0); + + memcpy(op,bm->queue_packets,sizeof(*op)); + bm->queue_head=0; - if(bm + }else{ + long bin; + long bytes; + + fprintf(stderr,"in "); + if(bm->next_to_flush==bm->last_to_flush)return(0); + + bin=bm->queue_actual[bm->next_to_flush]; + bytes=(bm->queue_binned[bm->queue_bins*bm->next_to_flush+bin]+7)/8; + + memcpy(op,bm->queue_packets+bm->next_to_flush,sizeof(*op)); + if(bytes<op->bytes)op->bytes=bytes; + bm->next_to_flush++; + if(bm->next_to_flush>=bm->queue_size)bm->next_to_flush=0; + + fprintf(stderr,"eof %d %ld\n",op->e_o_s,(long)op->granulepos); + + } + return(1); } diff --git a/lib/bitrate.h b/lib/bitrate.h index b7436d66..e1942954 100644 --- a/lib/bitrate.h +++ b/lib/bitrate.h @@ -11,7 +11,7 @@ ******************************************************************** function: bitrate tracking and management - last mod: $Id: bitrate.h,v 1.1.2.1 2001/11/16 08:17:06 xiphmont Exp $ + last mod: $Id: bitrate.h,v 1.1.2.2 2001/11/22 06:21:07 xiphmont Exp $ ********************************************************************/ @@ -59,10 +59,8 @@ typedef struct bitrate_manager_state { long noisetrigger_request; /* unfortunately, we need to hold queued packet data somewhere */ - unsigned char *packetdoublebuffer[2]; - unsigned char doublebufferhead[2]; - unsigned char doublebuffertail[2]; - oggpack_buffer *queue_packets; + oggpack_buffer *queue_packet_buffers; + ogg_packet *queue_packets; } bitrate_manager_state; @@ -74,6 +72,7 @@ typedef struct bitrate_manager_info{ double absolute_max_long; double queue_avg_time; + double queue_avg_center; double queue_minmax_time; double queue_hardmin; double queue_hardmax; @@ -94,7 +93,9 @@ typedef struct bitrate_manager_info{ extern void vorbis_bitrate_init(vorbis_info *vi,bitrate_manager_state *bs); extern void vorbis_bitrate_clear(bitrate_manager_state *bs); +extern int vorbis_bitrate_managed(vorbis_block *vb); +extern int vorbis_bitrate_maxmarkers(void); extern int vorbis_bitrate_addblock(vorbis_block *vb); -extern int vorbis_bitrate_flushpacket(vorbis_block *vb, vorbis_packet *op); +extern int vorbis_bitrate_flushpacket(vorbis_dsp_state *vd, ogg_packet *op); #endif diff --git a/lib/block.c b/lib/block.c index 1e1d0767..468939ea 100644 --- a/lib/block.c +++ b/lib/block.c @@ -11,7 +11,7 @@ ******************************************************************** function: PCM data vector blocking, windowing and dis/reassembly - last mod: $Id: block.c,v 1.50.2.5 2001/11/16 08:17:05 xiphmont Exp $ + last mod: $Id: block.c,v 1.50.2.6 2001/11/22 06:21:07 xiphmont Exp $ Handle windowing, overlap-add, etc of the PCM vectors. This is made more amusing by Vorbis' current two allowed block sizes. @@ -90,9 +90,12 @@ int vorbis_block_init(vorbis_dsp_state *v, vorbis_block *vb){ vb->localalloc=0; vb->localstore=NULL; if(v->analysisp){ + vorbis_block_internal *vbi= + vb->internal=_ogg_calloc(1,sizeof(vorbis_block_internal)); oggpack_writeinit(&vb->opb); - vb->internal=_ogg_calloc(1,sizeof(vorbis_block_internal)); - ((vorbis_block_internal *)vb->internal)->ampmax=-9999; + vbi->ampmax=-9999; + vbi->packet_markers=_ogg_malloc(vorbis_bitrate_maxmarkers()* + sizeof(*vbi->packet_markers)); } return(0); @@ -150,7 +153,13 @@ int vorbis_block_clear(vorbis_block *vb){ oggpack_writeclear(&vb->opb); _vorbis_block_ripcord(vb); if(vb->localstore)_ogg_free(vb->localstore); - if(vb->internal)_ogg_free(vb->internal); + + if(vb->internal){ + vorbis_block_internal *vbi=(vorbis_block_internal *)vb->internal; + if(vbi->packet_markers)_ogg_free(vbi->packet_markers); + + _ogg_free(vb->internal); + } memset(vb,0,sizeof(*vb)); return(0); diff --git a/lib/codebook.c b/lib/codebook.c new file mode 100644 index 00000000..3dcba95e --- /dev/null +++ b/lib/codebook.c @@ -0,0 +1,580 @@ +/******************************************************************** + * * + * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * + * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * + * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * + * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * + * * + * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 * + * by the XIPHOPHORUS Company http://www.xiph.org/ * + + ******************************************************************** + + function: basic codebook pack/unpack/code/decode operations + last mod: $Id: codebook.c,v 1.30.2.1 2001/11/22 06:21:07 xiphmont Exp $ + + ********************************************************************/ + +#include <stdlib.h> +#include <string.h> +#include <math.h> +#include <ogg/ogg.h> +#include "vorbis/codec.h" +#include "codebook.h" +#include "scales.h" +#include "misc.h" +#include "os.h" + +/* packs the given codebook into the bitstream **************************/ + +int vorbis_staticbook_pack(const static_codebook *c,oggpack_buffer *opb){ + long i,j; + int ordered=0; + + /* first the basic parameters */ + oggpack_write(opb,0x564342,24); + oggpack_write(opb,c->dim,16); + oggpack_write(opb,c->entries,24); + + /* pack the codewords. There are two packings; length ordered and + length random. Decide between the two now. */ + + for(i=1;i<c->entries;i++) + if(c->lengthlist[i-1]==0 || c->lengthlist[i]<c->lengthlist[i-1])break; + if(i==c->entries)ordered=1; + + if(ordered){ + /* length ordered. We only need to say how many codewords of + each length. The actual codewords are generated + deterministically */ + + long count=0; + oggpack_write(opb,1,1); /* ordered */ + oggpack_write(opb,c->lengthlist[0]-1,5); /* 1 to 32 */ + + for(i=1;i<c->entries;i++){ + long this=c->lengthlist[i]; + long last=c->lengthlist[i-1]; + if(this>last){ + for(j=last;j<this;j++){ + oggpack_write(opb,i-count,_ilog(c->entries-count)); + count=i; + } + } + } + oggpack_write(opb,i-count,_ilog(c->entries-count)); + + }else{ + /* length random. Again, we don't code the codeword itself, just + the length. This time, though, we have to encode each length */ + oggpack_write(opb,0,1); /* unordered */ + + /* algortihmic mapping has use for 'unused entries', which we tag + here. The algorithmic mapping happens as usual, but the unused + entry has no codeword. */ + for(i=0;i<c->entries;i++) + if(c->lengthlist[i]==0)break; + + if(i==c->entries){ + oggpack_write(opb,0,1); /* no unused entries */ + for(i=0;i<c->entries;i++) + oggpack_write(opb,c->lengthlist[i]-1,5); + }else{ + oggpack_write(opb,1,1); /* we have unused entries; thus we tag */ + for(i=0;i<c->entries;i++){ + if(c->lengthlist[i]==0){ + oggpack_write(opb,0,1); + }else{ + oggpack_write(opb,1,1); + oggpack_write(opb,c->lengthlist[i]-1,5); + } + } + } + } + + /* is the entry number the desired return value, or do we have a + mapping? If we have a mapping, what type? */ + oggpack_write(opb,c->maptype,4); + switch(c->maptype){ + case 0: + /* no mapping */ + break; + case 1:case 2: + /* implicitly populated value mapping */ + /* explicitly populated value mapping */ + + if(!c->quantlist){ + /* no quantlist? error */ + return(-1); + } + + /* values that define the dequantization */ + oggpack_write(opb,c->q_min,32); + oggpack_write(opb,c->q_delta,32); + oggpack_write(opb,c->q_quant-1,4); + oggpack_write(opb,c->q_sequencep,1); + + { + int quantvals; + switch(c->maptype){ + case 1: + /* a single column of (c->entries/c->dim) quantized values for + building a full value list algorithmically (square lattice) */ + quantvals=_book_maptype1_quantvals(c); + break; + case 2: + /* every value (c->entries*c->dim total) specified explicitly */ + quantvals=c->entries*c->dim; + break; + default: /* NOT_REACHABLE */ + quantvals=-1; + } + + /* quantized values */ + for(i=0;i<quantvals;i++) + oggpack_write(opb,labs(c->quantlist[i]),c->q_quant); + + } + break; + default: + /* error case; we don't have any other map types now */ + return(-1); + } + + return(0); +} + +/* unpacks a codebook from the packet buffer into the codebook struct, + readies the codebook auxiliary structures for decode *************/ +int vorbis_staticbook_unpack(oggpack_buffer *opb,static_codebook *s){ + long i,j; + memset(s,0,sizeof(*s)); + s->allocedp=1; + + /* make sure alignment is correct */ + if(oggpack_read(opb,24)!=0x564342)goto _eofout; + + /* first the basic parameters */ + s->dim=oggpack_read(opb,16); + s->entries=oggpack_read(opb,24); + if(s->entries==-1)goto _eofout; + + /* codeword ordering.... length ordered or unordered? */ + switch(oggpack_read(opb,1)){ + case 0: + /* unordered */ + s->lengthlist=_ogg_malloc(sizeof(*s->lengthlist)*s->entries); + + /* allocated but unused entries? */ + if(oggpack_read(opb,1)){ + /* yes, unused entries */ + + for(i=0;i<s->entries;i++){ + if(oggpack_read(opb,1)){ + long num=oggpack_read(opb,5); + if(num==-1)goto _eofout; + s->lengthlist[i]=num+1; + }else + s->lengthlist[i]=0; + } + }else{ + /* all entries used; no tagging */ + for(i=0;i<s->entries;i++){ + long num=oggpack_read(opb,5); + if(num==-1)goto _eofout; + s->lengthlist[i]=num+1; + } + } + + break; + case 1: + /* ordered */ + { + long length=oggpack_read(opb,5)+1; + s->lengthlist=_ogg_malloc(sizeof(*s->lengthlist)*s->entries); + + for(i=0;i<s->entries;){ + long num=oggpack_read(opb,_ilog(s->entries-i)); + if(num==-1)goto _eofout; + for(j=0;j<num;j++,i++) + s->lengthlist[i]=length; + length++; + } + } + break; + default: + /* EOF */ + return(-1); + } + + /* Do we have a mapping to unpack? */ + switch((s->maptype=oggpack_read(opb,4))){ + case 0: + /* no mapping */ + break; + case 1: case 2: + /* implicitly populated value mapping */ + /* explicitly populated value mapping */ + + s->q_min=oggpack_read(opb,32); + s->q_delta=oggpack_read(opb,32); + s->q_quant=oggpack_read(opb,4)+1; + s->q_sequencep=oggpack_read(opb,1); + + { + int quantvals; + switch(s->maptype){ + case 1: + quantvals=_book_maptype1_quantvals(s); + break; + case 2: + quantvals=s->entries*s->dim; + break; + } + + /* quantized values */ + s->quantlist=_ogg_malloc(sizeof(*s->quantlist)*quantvals); + for(i=0;i<quantvals;i++) + s->quantlist[i]=oggpack_read(opb,s->q_quant); + + if(s->quantlist[quantvals-1]==-1)goto _eofout; + } + break; + default: + goto _errout; + } + + /* all set */ + return(0); + + _errout: + _eofout: + vorbis_staticbook_clear(s); + return(-1); +} + +/* returns the number of bits ************************************************/ +int vorbis_book_encode(codebook *book, int a, oggpack_buffer *b){ + oggpack_write(b,book->codelist[a],book->c->lengthlist[a]); + return(book->c->lengthlist[a]); +} + +/* One the encode side, our vector writers are each designed for a +specific purpose, and the encoder is not flexible without modification: + +The LSP vector coder uses a single stage nearest-match with no +interleave, so no step and no error return. This is specced by floor0 +and doesn't change. + +Residue0 encoding interleaves, uses multiple stages, and each stage +peels of a specific amount of resolution from a lattice (thus we want +to match by threshold, not nearest match). Residue doesn't *have* to +be encoded that way, but to change it, one will need to add more +infrastructure on the encode side (decode side is specced and simpler) */ + +/* floor0 LSP (single stage, non interleaved, nearest match) */ +/* returns entry number and *modifies a* to the quantization value *****/ +int vorbis_book_errorv(codebook *book,float *a){ + int dim=book->dim,k; + int best=_best(book,a,1); + for(k=0;k<dim;k++) + a[k]=(book->valuelist+best*dim)[k]; + return(best); +} + +/* returns the number of bits and *modifies a* to the quantization value *****/ +int vorbis_book_encodev(codebook *book,int best,float *a,oggpack_buffer *b){ + int k,dim=book->dim; + for(k=0;k<dim;k++) + a[k]=(book->valuelist+best*dim)[k]; + return(vorbis_book_encode(book,best,b)); +} + +/* res0 (multistage, interleave, lattice) */ +/* returns the number of bits and *modifies a* to the remainder value ********/ +int vorbis_book_encodevs(codebook *book,float *a,oggpack_buffer *b, + int step,int addmul){ + + int best=vorbis_book_besterror(book,a,step,addmul); + return(vorbis_book_encode(book,best,b)); +} + +/* Decode side is specced and easier, because we don't need to find + matches using different criteria; we simply read and map. There are + two things we need to do 'depending': + + We may need to support interleave. We don't really, but it's + convenient to do it here rather than rebuild the vector later. + + Cascades may be additive or multiplicitive; this is not inherent in + the codebook, but set in the code using the codebook. Like + interleaving, it's easiest to do it here. + addmul==0 -> declarative (set the value) + addmul==1 -> additive + addmul==2 -> multiplicitive */ + +/* returns the entry number or -1 on eof *************************************/ +long vorbis_book_decode(codebook *book, oggpack_buffer *b){ + long ptr=0; + decode_aux *t=book->decode_tree; + int lok = oggpack_look(b, t->tabn); + + if (lok >= 0) { + ptr = t->tab[lok]; + oggpack_adv(b, t->tabl[lok]); + if (ptr <= 0) + return -ptr; + } + + do{ + switch(oggpack_read1(b)){ + case 0: + ptr=t->ptr0[ptr]; + break; + case 1: + ptr=t->ptr1[ptr]; + break; + case -1: + return(-1); + } + }while(ptr>0); + return(-ptr); +} + +/* returns 0 on OK or -1 on eof *************************************/ +long vorbis_book_decodevs_add(codebook *book,float *a,oggpack_buffer *b,int n){ + int step=n/book->dim; + long *entry = alloca(sizeof(*entry)*step); + float **t = alloca(sizeof(*t)*step); + int i,j,o; + + for (i = 0; i < step; i++) { + entry[i]=vorbis_book_decode(book,b); + if(entry[i]==-1)return(-1); + t[i] = book->valuelist+entry[i]*book->dim; + } + for(i=0,o=0;i<book->dim;i++,o+=step) + for (j=0;j<step;j++) + a[o+j]+=t[j][i]; + return(0); +} + +long vorbis_book_decodev_add(codebook *book,float *a,oggpack_buffer *b,int n){ + int i,j,entry; + float *t; + + if(book->dim>8){ + for(i=0;i<n;){ + entry = vorbis_book_decode(book,b); + if(entry==-1)return(-1); + t = book->valuelist+entry*book->dim; + for (j=0;j<book->dim;) + a[i++]+=t[j++]; + } + }else{ + for(i=0;i<n;){ + entry = vorbis_book_decode(book,b); + if(entry==-1)return(-1); + t = book->valuelist+entry*book->dim; + j=0; + switch(book->dim){ + case 8: + a[i++]+=t[j++]; + case 7: + a[i++]+=t[j++]; + case 6: + a[i++]+=t[j++]; + case 5: + a[i++]+=t[j++]; + case 4: + a[i++]+=t[j++]; + case 3: + a[i++]+=t[j++]; + case 2: + a[i++]+=t[j++]; + case 1: + a[i++]+=t[j++]; + case 0: + break; + } + } + } + return(0); +} + +long vorbis_book_decodev_set(codebook *book,float *a,oggpack_buffer *b,int n){ + int i,j,entry; + float *t; + + for(i=0;i<n;){ + entry = vorbis_book_decode(book,b); + if(entry==-1)return(-1); + t = book->valuelist+entry*book->dim; + for (j=0;j<book->dim;) + a[i++]=t[j++]; + } + return(0); +} + +long vorbis_book_decodevv_add(codebook *book,float **a,long offset,int ch, + oggpack_buffer *b,int n){ + long i,j,entry; + int chptr=0; + + for(i=offset/ch;i<(offset+n)/ch;){ + entry = vorbis_book_decode(book,b); + if(entry==-1)return(-1); + { + const float *t = book->valuelist+entry*book->dim; + for (j=0;j<book->dim;j++){ + a[chptr++][i]+=t[j]; + if(chptr==ch){ + chptr=0; + i++; + } + } + } + } + return(0); +} + +#ifdef _V_SELFTEST +/* Simple enough; pack a few candidate codebooks, unpack them. Code a + number of vectors through (keeping track of the quantized values), + and decode using the unpacked book. quantized version of in should + exactly equal out */ + +#include <stdio.h> + +#include "vorbis/book/lsp20_0.vqh" +#include "vorbis/book/res0a_13.vqh" +#define TESTSIZE 40 + +float test1[TESTSIZE]={ + 0.105939f, + 0.215373f, + 0.429117f, + 0.587974f, + + 0.181173f, + 0.296583f, + 0.515707f, + 0.715261f, + + 0.162327f, + 0.263834f, + 0.342876f, + 0.406025f, + + 0.103571f, + 0.223561f, + 0.368513f, + 0.540313f, + + 0.136672f, + 0.395882f, + 0.587183f, + 0.652476f, + + 0.114338f, + 0.417300f, + 0.525486f, + 0.698679f, + + 0.147492f, + 0.324481f, + 0.643089f, + 0.757582f, + + 0.139556f, + 0.215795f, + 0.324559f, + 0.399387f, + + 0.120236f, + 0.267420f, + 0.446940f, + 0.608760f, + + 0.115587f, + 0.287234f, + 0.571081f, + 0.708603f, +}; + +float test3[TESTSIZE]={ + 0,1,-2,3,4,-5,6,7,8,9, + 8,-2,7,-1,4,6,8,3,1,-9, + 10,11,12,13,14,15,26,17,18,19, + 30,-25,-30,-1,-5,-32,4,3,-2,0}; + +static_codebook *testlist[]={&_vq_book_lsp20_0, + &_vq_book_res0a_13,NULL}; +float *testvec[]={test1,test3}; + +int main(){ + oggpack_buffer write; + oggpack_buffer read; + long ptr=0,i; + oggpack_writeinit(&write); + + fprintf(stderr,"Testing codebook abstraction...:\n"); + + while(testlist[ptr]){ + codebook c; + static_codebook s; + float *qv=alloca(sizeof(*qv)*TESTSIZE); + float *iv=alloca(sizeof(*iv)*TESTSIZE); + memcpy(qv,testvec[ptr],sizeof(*qv)*TESTSIZE); + memset(iv,0,sizeof(*iv)*TESTSIZE); + + fprintf(stderr,"\tpacking/coding %ld... ",ptr); + + /* pack the codebook, write the testvector */ + oggpack_reset(&write); + vorbis_book_init_encode(&c,testlist[ptr]); /* get it into memory + we can write */ + vorbis_staticbook_pack(testlist[ptr],&write); + fprintf(stderr,"Codebook size %ld bytes... ",oggpack_bytes(&write)); + for(i=0;i<TESTSIZE;i+=c.dim){ + int best=_best(&c,qv+i,1); + vorbis_book_encodev(&c,best,qv+i,&write); + } + vorbis_book_clear(&c); + + fprintf(stderr,"OK.\n"); + fprintf(stderr,"\tunpacking/decoding %ld... ",ptr); + + /* transfer the write data to a read buffer and unpack/read */ + oggpack_readinit(&read,oggpack_get_buffer(&write),oggpack_bytes(&write)); + if(vorbis_staticbook_unpack(&read,&s)){ + fprintf(stderr,"Error unpacking codebook.\n"); + exit(1); + } + if(vorbis_book_init_decode(&c,&s)){ + fprintf(stderr,"Error initializing codebook.\n"); + exit(1); + } + + for(i=0;i<TESTSIZE;i+=c.dim) + if(vorbis_book_decodev_set(&c,iv+i,&read,c.dim)==-1){ + fprintf(stderr,"Error reading codebook test data (EOP).\n"); + exit(1); + } + for(i=0;i<TESTSIZE;i++) + if(fabs(qv[i]-iv[i])>.000001){ + fprintf(stderr,"read (%g) != written (%g) at position (%ld)\n", + iv[i],qv[i],i); + exit(1); + } + + fprintf(stderr,"OK\n"); + ptr++; + } + + /* The above is the trivial stuff; now try unquantizing a log scale codebook */ + + exit(0); +} + +#endif diff --git a/lib/codec_internal.h b/lib/codec_internal.h index 6b692f73..d2a5bed7 100644 --- a/lib/codec_internal.h +++ b/lib/codec_internal.h @@ -10,7 +10,7 @@ ******************************************************************** function: libvorbis codec headers - last mod: $Id: codec_internal.h,v 1.9.4.5 2001/11/16 08:17:06 xiphmont Exp $ + last mod: $Id: codec_internal.h,v 1.9.4.6 2001/11/22 06:21:07 xiphmont Exp $ ********************************************************************/ @@ -23,6 +23,8 @@ typedef struct vorbis_block_internal{ float **pcmdelay; /* this is a pointer into local storage */ float ampmax; + + ogg_uint32_t *packet_markers; } vorbis_block_internal; typedef void vorbis_look_time; @@ -45,7 +47,7 @@ typedef void vorbis_info_residue; typedef void vorbis_info_mapping; #include "psy.h" -#inclide "bitrate.h" +#include "bitrate.h" typedef struct backend_lookup_state { /* local lookup storage */ diff --git a/lib/floor0.c b/lib/floor0.c new file mode 100644 index 00000000..5358efdf --- /dev/null +++ b/lib/floor0.c @@ -0,0 +1,468 @@ +/******************************************************************** + * * + * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * + * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * + * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * + * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * + * * + * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 * + * by the XIPHOPHORUS Company http://www.xiph.org/ * + + ******************************************************************** + + function: floor backend 0 implementation + last mod: $Id: floor0.c,v 1.46.2.1 2001/11/22 06:21:07 xiphmont Exp $ + + ********************************************************************/ + +#include <stdlib.h> +#include <string.h> +#include <math.h> +#include <ogg/ogg.h> +#include "vorbis/codec.h" +#include "codec_internal.h" +#include "registry.h" +#include "lpc.h" +#include "lsp.h" +#include "codebook.h" +#include "scales.h" +#include "misc.h" +#include "os.h" + +#include "misc.h" +#include <stdio.h> + +typedef struct { + long n; + int ln; + int m; + int *linearmap; + + vorbis_info_floor0 *vi; + lpc_lookup lpclook; + float *lsp_look; + + long bits; + long frames; +} vorbis_look_floor0; + +/* infrastructure for finding fit */ +static long _f0_fit(codebook *book, + float *orig, + float *workfit, + int cursor){ + int dim=book->dim; + float norm,base=0.f; + int i,best=0; + float *lsp=workfit+cursor; + + if(cursor)base=workfit[cursor-1]; + norm=orig[cursor+dim-1]-base; + + for(i=0;i<dim;i++) + lsp[i]=(orig[i+cursor]-base); + best=_best(book,lsp,1); + + memcpy(lsp,book->valuelist+best*dim,dim*sizeof(*lsp)); + for(i=0;i<dim;i++) + lsp[i]+=base; + return(best); +} + +/***********************************************/ + +static vorbis_info_floor *floor0_copy_info (vorbis_info_floor *i){ + vorbis_info_floor0 *info=(vorbis_info_floor0 *)i; + vorbis_info_floor0 *ret=_ogg_malloc(sizeof(*ret)); + memcpy(ret,info,sizeof(*ret)); + return(ret); +} + +static void floor0_free_info(vorbis_info_floor *i){ + vorbis_info_floor0 *info=(vorbis_info_floor0 *)i; + if(info){ + memset(info,0,sizeof(*info)); + _ogg_free(info); + } +} + +static void floor0_free_look(vorbis_look_floor *i){ + vorbis_look_floor0 *look=(vorbis_look_floor0 *)i; + if(look){ + + /*fprintf(stderr,"floor 0 bit usage %f\n", + (float)look->bits/look->frames);*/ + + if(look->linearmap)_ogg_free(look->linearmap); + if(look->lsp_look)_ogg_free(look->lsp_look); + lpc_clear(&look->lpclook); + memset(look,0,sizeof(*look)); + _ogg_free(look); + } +} + +static void floor0_pack (vorbis_info_floor *i,oggpack_buffer *opb){ + vorbis_info_floor0 *info=(vorbis_info_floor0 *)i; + int j; + oggpack_write(opb,info->order,8); + oggpack_write(opb,info->rate,16); + oggpack_write(opb,info->barkmap,16); + oggpack_write(opb,info->ampbits,6); + oggpack_write(opb,info->ampdB,8); + oggpack_write(opb,info->numbooks-1,4); + for(j=0;j<info->numbooks;j++) + oggpack_write(opb,info->books[j],8); +} + +static vorbis_info_floor *floor0_unpack (vorbis_info *vi,oggpack_buffer *opb){ + codec_setup_info *ci=vi->codec_setup; + int j; + + vorbis_info_floor0 *info=_ogg_malloc(sizeof(*info)); + info->order=oggpack_read(opb,8); + info->rate=oggpack_read(opb,16); + info->barkmap=oggpack_read(opb,16); + info->ampbits=oggpack_read(opb,6); + info->ampdB=oggpack_read(opb,8); + info->numbooks=oggpack_read(opb,4)+1; + + if(info->order<1)goto err_out; + if(info->rate<1)goto err_out; + if(info->barkmap<1)goto err_out; + if(info->numbooks<1)goto err_out; + + for(j=0;j<info->numbooks;j++){ + info->books[j]=oggpack_read(opb,8); + if(info->books[j]<0 || info->books[j]>=ci->books)goto err_out; + } + return(info); + + err_out: + floor0_free_info(info); + return(NULL); +} + +/* initialize Bark scale and normalization lookups. We could do this + with static tables, but Vorbis allows a number of possible + combinations, so it's best to do it computationally. + + The below is authoritative in terms of defining scale mapping. + Note that the scale depends on the sampling rate as well as the + linear block and mapping sizes */ + +static vorbis_look_floor *floor0_look (vorbis_dsp_state *vd,vorbis_info_mode *mi, + vorbis_info_floor *i){ + int j; + float scale; + vorbis_info *vi=vd->vi; + codec_setup_info *ci=vi->codec_setup; + vorbis_info_floor0 *info=(vorbis_info_floor0 *)i; + vorbis_look_floor0 *look=_ogg_calloc(1,sizeof(*look)); + look->m=info->order; + look->n=ci->blocksizes[mi->blockflag]/2; + look->ln=info->barkmap; + look->vi=info; + + if(vd->analysisp) + lpc_init(&look->lpclook,look->ln,look->m); + + /* we choose a scaling constant so that: + floor(bark(rate/2-1)*C)=mapped-1 + floor(bark(rate/2)*C)=mapped */ + scale=look->ln/toBARK(info->rate/2.f); + + /* the mapping from a linear scale to a smaller bark scale is + straightforward. We do *not* make sure that the linear mapping + does not skip bark-scale bins; the decoder simply skips them and + the encoder may do what it wishes in filling them. They're + necessary in some mapping combinations to keep the scale spacing + accurate */ + look->linearmap=_ogg_malloc((look->n+1)*sizeof(*look->linearmap)); + for(j=0;j<look->n;j++){ + int val=floor( toBARK((info->rate/2.f)/look->n*j) + *scale); /* bark numbers represent band edges */ + if(val>=look->ln)val=look->ln; /* guard against the approximation */ + look->linearmap[j]=val; + } + look->linearmap[j]=-1; + + look->lsp_look=_ogg_malloc(look->ln*sizeof(*look->lsp_look)); + for(j=0;j<look->ln;j++) + look->lsp_look[j]=2*cos(M_PI/look->ln*j); + + return look; +} + +/* less efficient than the decode side (written for clarity). We're + not bottlenecked here anyway */ + +float _curve_to_lpc(float *curve,float *lpc, + vorbis_look_floor0 *l){ + /* map the input curve to a bark-scale curve for encoding */ + + int mapped=l->ln; + float *work=alloca(sizeof(*work)*mapped); + int i,j,last=0; + int bark=0; + static int seq=0; + + memset(work,0,sizeof(*work)*mapped); + + /* Only the decode side is behavior-specced; for now in the encoder, + we select the maximum value of each band as representative (this + helps make sure peaks don't go out of range. In error terms, + selecting min would make more sense, but the codebook is trained + numerically, so we don't actually lose. We'd still want to + use the original curve for error and noise estimation */ + + for(i=0;i<l->n;i++){ + bark=l->linearmap[i]; + if(work[bark]<curve[i])work[bark]=curve[i]; + if(bark>last+1){ + /* If the bark scale is climbing rapidly, some bins may end up + going unused. This isn't a waste actually; it keeps the + scale resolution even so that the LPC generator has an easy + time. However, if we leave the bins empty we lose energy. + So, fill 'em in. The decoder does not do anything with he + unused bins, so we can fill them anyway we like to end up + with a better spectral curve */ + + /* we'll always have a bin zero, so we don't need to guard init */ + long span=bark-last; + for(j=1;j<span;j++){ + float del=(float)j/span; + work[j+last]=work[bark]*del+work[last]*(1.f-del); + } + } + last=bark; + } + + /* If we're over-ranged to avoid edge effects, fill in the end of spectrum gap */ + for(i=bark+1;i<mapped;i++) + work[i]=work[i-1]; + + + /**********************/ + + for(i=0;i<l->n;i++) + curve[i]-=150; + + _analysis_output("barkfloor",seq,work,bark,0,0); + _analysis_output("barkcurve",seq++,curve,l->n,1,0); + + for(i=0;i<l->n;i++) + curve[i]+=150; + + /**********************/ + + return vorbis_lpc_from_curve(work,lpc,&(l->lpclook)); +} + +static int floor0_forward(vorbis_block *vb,vorbis_look_floor *in, + float *mdct, const float *logmdct, /* in */ + const float *logmask, const float *logmax, /* in */ + float *codedflr){ /* out */ + long j; + vorbis_look_floor0 *look=(vorbis_look_floor0 *)in; + vorbis_info_floor0 *info=look->vi; + float amp; + long val=0; + static int seq=0; + +#ifdef TRAIN_LSP + FILE *of; + FILE *ef; + char buffer[80]; + +#if 1 + sprintf(buffer,"lsp0coeff_%d.vqd",vb->mode); + of=fopen(buffer,"a"); +#endif +#endif + + seq++; + + + /* our floor comes in on a [-Inf...0] dB scale. The curve has to be + positive, so we offset it. */ + + for(j=0;j<look->n;j++) + codedflr[j]=logmask[j]+info->ampdB; + + /* use 'out' as temp storage */ + /* Convert our floor to a set of lpc coefficients */ + amp=sqrt(_curve_to_lpc(codedflr,codedflr,look)); + + /* amp is in the range (0. to ampdB]. Encode that range using + ampbits bits */ + + { + long maxval=(1L<<info->ampbits)-1; + + val=rint(amp/info->ampdB*maxval); + + if(val<0)val=0; /* likely */ + if(val>maxval)val=maxval; /* not bloody likely */ + + if(val>0) + amp=(float)val/maxval*info->ampdB; + else + amp=0; + } + + if(val){ + /* LSP <-> LPC is orthogonal and LSP quantizes more stably */ + _analysis_output("lpc",seq-1,codedflr,look->m,0,0); + if(vorbis_lpc_to_lsp(codedflr,codedflr,look->m)) + val=0; + + } + + oggpack_write(&vb->opb,val,info->ampbits); + look->bits+=info->ampbits+1; + look->frames++; + + if(val){ + float *lspwork=alloca(look->m*sizeof(*lspwork)); + + /* the spec supports using one of a number of codebooks. Right + now, encode using this lib supports only one */ + backend_lookup_state *be=vb->vd->backend_state; + codebook *b; + int booknum; + + _analysis_output("lsp",seq-1,codedflr,look->m,0,0); + + /* which codebook to use? We do it only by range right now. */ + if(info->numbooks>1){ + float last=0.; + for(j=0;j<look->m;j++){ + float val=codedflr[j]-last; + if(val<info->lessthan || val>info->greaterthan)break; + last=codedflr[j]; + } + if(j<look->m) + booknum=0; + else + booknum=1; + }else + booknum=0; + + b=be->fullbooks+info->books[booknum]; + oggpack_write(&vb->opb,booknum,_ilog(info->numbooks)); + look->bits+=_ilog(info->numbooks); + +#ifdef TRAIN_LSP + { + float last=0.f; + for(j=0;j<look->m;j++){ + fprintf(of,"%.12g, ",codedflr[j]-last); + last=codedflr[j]; + } + } + fprintf(of,"\n"); + fclose(of); + + sprintf(buffer,"lsp0ent_m%d_b%d.vqd",vb->mode,booknum); + ef=fopen(buffer,"a"); + +#endif + + /* code the spectral envelope, and keep track of the actual + quantized values; we don't want creeping error as each block is + nailed to the last quantized value of the previous block. */ + + for(j=0;j<look->m;j+=b->dim){ + int entry=_f0_fit(b,codedflr,lspwork,j); + look->bits+=vorbis_book_encode(b,entry,&vb->opb); + +#ifdef TRAIN_LSP + fprintf(ef,"%d,\n",entry); +#endif + + } + +#ifdef TRAIN_LSP + fclose(ef); +#endif + + _analysis_output("lsp2",seq-1,lspwork,look->m,0,0); + + /* take the coefficients back to a spectral envelope curve */ + for(j=0;j<look->n;j++) + codedflr[j]=1.f; + vorbis_lsp_to_curve(codedflr,look->linearmap,look->n,look->ln, + lspwork,look->m,amp,info->ampdB); + + _analysis_output("barklsp",seq-1,codedflr,look->n,1,1); + _analysis_output("lsp3",seq-1,codedflr,look->n,0,1); + + return(val); + } + +#ifdef TRAIN_LSP + fclose(of); +#endif + + memset(codedflr,0,sizeof(*codedflr)*look->n); + memset(mdct,0,sizeof(*mdct)*look->n); + return(val); +} + +static void *floor0_inverse1(vorbis_block *vb,vorbis_look_floor *i){ + vorbis_look_floor0 *look=(vorbis_look_floor0 *)i; + vorbis_info_floor0 *info=look->vi; + int j,k; + + int ampraw=oggpack_read(&vb->opb,info->ampbits); + if(ampraw>0){ /* also handles the -1 out of data case */ + long maxval=(1<<info->ampbits)-1; + float amp=(float)ampraw/maxval*info->ampdB; + int booknum=oggpack_read(&vb->opb,_ilog(info->numbooks)); + + if(booknum!=-1 && booknum<info->numbooks){ /* be paranoid */ + backend_lookup_state *be=vb->vd->backend_state; + codebook *b=be->fullbooks+info->books[booknum]; + float last=0.f; + float *lsp=_vorbis_block_alloc(vb,sizeof(*lsp)*(look->m+1)); + + for(j=0;j<look->m;j+=b->dim) + if(vorbis_book_decodev_set(b,lsp+j,&vb->opb,b->dim)==-1)goto eop; + for(j=0;j<look->m;){ + for(k=0;k<b->dim;k++,j++)lsp[j]+=last; + last=lsp[j-1]; + } + + lsp[look->m]=amp; + return(lsp); + } + } + eop: + return(NULL); +} + +static int floor0_inverse2(vorbis_block *vb,vorbis_look_floor *i, + void *memo,float *out){ + vorbis_look_floor0 *look=(vorbis_look_floor0 *)i; + vorbis_info_floor0 *info=look->vi; + + if(memo){ + float *lsp=(float *)memo; + float amp=lsp[look->m]; + + /* take the coefficients back to a spectral envelope curve */ + vorbis_lsp_to_curve(out,look->linearmap,look->n,look->ln, + lsp,look->m,amp,info->ampdB); + return(1); + } + memset(out,0,sizeof(*out)*look->n); + return(0); +} + +/* export hooks */ +vorbis_func_floor floor0_exportbundle={ + &floor0_pack,&floor0_unpack,&floor0_look,&floor0_copy_info,&floor0_free_info, + &floor0_free_look,&floor0_forward,&floor0_inverse1,&floor0_inverse2 +}; + + diff --git a/lib/floor1.c b/lib/floor1.c new file mode 100644 index 00000000..1e9e3059 --- /dev/null +++ b/lib/floor1.c @@ -0,0 +1,1152 @@ +/******************************************************************** + * * + * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * + * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * + * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * + * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * + * * + * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 * + * by the XIPHOPHORUS Company http://www.xiph.org/ * + * * + ******************************************************************** + + function: floor backend 1 implementation + last mod: $Id: floor1.c,v 1.16.2.1 2001/11/22 06:21:08 xiphmont Exp $ + + ********************************************************************/ + +#include <stdlib.h> +#include <string.h> +#include <math.h> +#include <ogg/ogg.h> +#include "vorbis/codec.h" +#include "codec_internal.h" +#include "registry.h" +#include "codebook.h" +#include "misc.h" +#include "scales.h" + +#include <stdio.h> + +#define floor1_rangedB 140 /* floor 1 fixed at -140dB to 0dB range */ + +typedef struct { + int sorted_index[VIF_POSIT+2]; + int forward_index[VIF_POSIT+2]; + int reverse_index[VIF_POSIT+2]; + + int hineighbor[VIF_POSIT]; + int loneighbor[VIF_POSIT]; + int posts; + + int n; + int quant_q; + vorbis_info_floor1 *vi; + + long phrasebits; + long postbits; + long frames; +} vorbis_look_floor1; + +typedef struct lsfit_acc{ + long x0; + long x1; + + long xa; + long ya; + long x2a; + long y2a; + long xya; + long n; + long an; + long un; + long edgey0; + long edgey1; +} lsfit_acc; + +/***********************************************/ + +static vorbis_info_floor *floor1_copy_info (vorbis_info_floor *i){ + vorbis_info_floor1 *info=(vorbis_info_floor1 *)i; + vorbis_info_floor1 *ret=_ogg_malloc(sizeof(*ret)); + memcpy(ret,info,sizeof(*ret)); + return(ret); +} + +static void floor1_free_info(vorbis_info_floor *i){ + vorbis_info_floor1 *info=(vorbis_info_floor1 *)i; + if(info){ + memset(info,0,sizeof(*info)); + _ogg_free(info); + } +} + +static void floor1_free_look(vorbis_look_floor *i){ + vorbis_look_floor1 *look=(vorbis_look_floor1 *)i; + if(look){ + /*fprintf(stderr,"floor 1 bit usage %f:%f (%f total)\n", + (float)look->phrasebits/look->frames, + (float)look->postbits/look->frames, + (float)(look->postbits+look->phrasebits)/look->frames);*/ + + memset(look,0,sizeof(*look)); + free(look); + } +} + +static int ilog(unsigned int v){ + int ret=0; + while(v){ + ret++; + v>>=1; + } + return(ret); +} + +static int ilog2(unsigned int v){ + int ret=0; + while(v>1){ + ret++; + v>>=1; + } + return(ret); +} + +static void floor1_pack (vorbis_info_floor *i,oggpack_buffer *opb){ + vorbis_info_floor1 *info=(vorbis_info_floor1 *)i; + int j,k; + int count=0; + int rangebits; + int maxposit=info->postlist[1]; + int maxclass=-1; + + /* save out partitions */ + oggpack_write(opb,info->partitions,5); /* only 0 to 31 legal */ + for(j=0;j<info->partitions;j++){ + oggpack_write(opb,info->partitionclass[j],4); /* only 0 to 15 legal */ + if(maxclass<info->partitionclass[j])maxclass=info->partitionclass[j]; + } + + /* save out partition classes */ + for(j=0;j<maxclass+1;j++){ + oggpack_write(opb,info->class_dim[j]-1,3); /* 1 to 8 */ + oggpack_write(opb,info->class_subs[j],2); /* 0 to 3 */ + if(info->class_subs[j])oggpack_write(opb,info->class_book[j],8); + for(k=0;k<(1<<info->class_subs[j]);k++) + oggpack_write(opb,info->class_subbook[j][k]+1,8); + } + + /* save out the post list */ + oggpack_write(opb,info->mult-1,2); /* only 1,2,3,4 legal now */ + oggpack_write(opb,ilog2(maxposit),4); + rangebits=ilog2(maxposit); + + for(j=0,k=0;j<info->partitions;j++){ + count+=info->class_dim[info->partitionclass[j]]; + for(;k<count;k++) + oggpack_write(opb,info->postlist[k+2],rangebits); + } +} + + +static vorbis_info_floor *floor1_unpack (vorbis_info *vi,oggpack_buffer *opb){ + codec_setup_info *ci=vi->codec_setup; + int j,k,count=0,maxclass=-1,rangebits; + + vorbis_info_floor1 *info=_ogg_calloc(1,sizeof(*info)); + /* read partitions */ + info->partitions=oggpack_read(opb,5); /* only 0 to 31 legal */ + for(j=0;j<info->partitions;j++){ + info->partitionclass[j]=oggpack_read(opb,4); /* only 0 to 15 legal */ + if(maxclass<info->partitionclass[j])maxclass=info->partitionclass[j]; + } + + /* read partition classes */ + for(j=0;j<maxclass+1;j++){ + info->class_dim[j]=oggpack_read(opb,3)+1; /* 1 to 8 */ + info->class_subs[j]=oggpack_read(opb,2); /* 0,1,2,3 bits */ + if(info->class_subs[j]<0) + goto err_out; + if(info->class_subs[j])info->class_book[j]=oggpack_read(opb,8); + if(info->class_book[j]<0 || info->class_book[j]>=ci->books) + goto err_out; + for(k=0;k<(1<<info->class_subs[j]);k++){ + info->class_subbook[j][k]=oggpack_read(opb,8)-1; + if(info->class_subbook[j][k]<-1 || info->class_subbook[j][k]>=ci->books) + goto err_out; + } + } + + /* read the post list */ + info->mult=oggpack_read(opb,2)+1; /* only 1,2,3,4 legal now */ + rangebits=oggpack_read(opb,4); + + for(j=0,k=0;j<info->partitions;j++){ + count+=info->class_dim[info->partitionclass[j]]; + for(;k<count;k++){ + int t=info->postlist[k+2]=oggpack_read(opb,rangebits); + if(t<0 || t>=(1<<rangebits)) + goto err_out; + } + } + info->postlist[0]=0; + info->postlist[1]=1<<rangebits; + + return(info); + + err_out: + floor1_free_info(info); + return(NULL); +} + +static int icomp(const void *a,const void *b){ + return(**(int **)a-**(int **)b); +} + +static vorbis_look_floor *floor1_look(vorbis_dsp_state *vd,vorbis_info_mode *mi, + vorbis_info_floor *in){ + + int *sortpointer[VIF_POSIT+2]; + vorbis_info_floor1 *info=(vorbis_info_floor1 *)in; + vorbis_look_floor1 *look=_ogg_calloc(1,sizeof(*look)); + int i,j,n=0; + + look->vi=info; + look->n=info->postlist[1]; + + /* we drop each position value in-between already decoded values, + and use linear interpolation to predict each new value past the + edges. The positions are read in the order of the position + list... we precompute the bounding positions in the lookup. Of + course, the neighbors can change (if a position is declined), but + this is an initial mapping */ + + for(i=0;i<info->partitions;i++)n+=info->class_dim[info->partitionclass[i]]; + n+=2; + look->posts=n; + + /* also store a sorted position index */ + for(i=0;i<n;i++)sortpointer[i]=info->postlist+i; + qsort(sortpointer,n,sizeof(*sortpointer),icomp); + + /* points from sort order back to range number */ + for(i=0;i<n;i++)look->forward_index[i]=sortpointer[i]-info->postlist; + /* points from range order to sorted position */ + for(i=0;i<n;i++)look->reverse_index[look->forward_index[i]]=i; + /* we actually need the post values too */ + for(i=0;i<n;i++)look->sorted_index[i]=info->postlist[look->forward_index[i]]; + + /* quantize values to multiplier spec */ + switch(info->mult){ + case 1: /* 1024 -> 256 */ + look->quant_q=256; + break; + case 2: /* 1024 -> 128 */ + look->quant_q=128; + break; + case 3: /* 1024 -> 86 */ + look->quant_q=86; + break; + case 4: /* 1024 -> 64 */ + look->quant_q=64; + break; + } + + /* discover our neighbors for decode where we don't use fit flags + (that would push the neighbors outward) */ + for(i=0;i<n-2;i++){ + int lo=0; + int hi=1; + int lx=0; + int hx=look->n; + int currentx=info->postlist[i+2]; + for(j=0;j<i+2;j++){ + int x=info->postlist[j]; + if(x>lx && x<currentx){ + lo=j; + lx=x; + } + if(x<hx && x>currentx){ + hi=j; + hx=x; + } + } + look->loneighbor[i]=lo; + look->hineighbor[i]=hi; + } + + return(look); +} + +static int render_point(int x0,int x1,int y0,int y1,int x){ + y0&=0x7fff; /* mask off flag */ + y1&=0x7fff; + + { + int dy=y1-y0; + int adx=x1-x0; + int ady=abs(dy); + int err=ady*(x-x0); + + int off=err/adx; + if(dy<0)return(y0-off); + return(y0+off); + } +} + +static int vorbis_dBquant(const float *x){ + int i= *x*7.3142857f+1023.5f; + if(i>1023)return(1023); + if(i<0)return(0); + return i; +} + +static float FLOOR_fromdB_LOOKUP[256]={ + 1.0649863e-07F, 1.1341951e-07F, 1.2079015e-07F, 1.2863978e-07F, + 1.3699951e-07F, 1.4590251e-07F, 1.5538408e-07F, 1.6548181e-07F, + 1.7623575e-07F, 1.8768855e-07F, 1.9988561e-07F, 2.128753e-07F, + 2.2670913e-07F, 2.4144197e-07F, 2.5713223e-07F, 2.7384213e-07F, + 2.9163793e-07F, 3.1059021e-07F, 3.3077411e-07F, 3.5226968e-07F, + 3.7516214e-07F, 3.9954229e-07F, 4.2550680e-07F, 4.5315863e-07F, + 4.8260743e-07F, 5.1396998e-07F, 5.4737065e-07F, 5.8294187e-07F, + 6.2082472e-07F, 6.6116941e-07F, 7.0413592e-07F, 7.4989464e-07F, + 7.9862701e-07F, 8.5052630e-07F, 9.0579828e-07F, 9.6466216e-07F, + 1.0273513e-06F, 1.0941144e-06F, 1.1652161e-06F, 1.2409384e-06F, + 1.3215816e-06F, 1.4074654e-06F, 1.4989305e-06F, 1.5963394e-06F, + 1.7000785e-06F, 1.8105592e-06F, 1.9282195e-06F, 2.0535261e-06F, + 2.1869758e-06F, 2.3290978e-06F, 2.4804557e-06F, 2.6416497e-06F, + 2.8133190e-06F, 2.9961443e-06F, 3.1908506e-06F, 3.3982101e-06F, + 3.6190449e-06F, 3.8542308e-06F, 4.1047004e-06F, 4.3714470e-06F, + 4.6555282e-06F, 4.9580707e-06F, 5.2802740e-06F, 5.6234160e-06F, + 5.9888572e-06F, 6.3780469e-06F, 6.7925283e-06F, 7.2339451e-06F, + 7.7040476e-06F, 8.2047000e-06F, 8.7378876e-06F, 9.3057248e-06F, + 9.9104632e-06F, 1.0554501e-05F, 1.1240392e-05F, 1.1970856e-05F, + 1.2748789e-05F, 1.3577278e-05F, 1.4459606e-05F, 1.5399272e-05F, + 1.6400004e-05F, 1.7465768e-05F, 1.8600792e-05F, 1.9809576e-05F, + 2.1096914e-05F, 2.2467911e-05F, 2.3928002e-05F, 2.5482978e-05F, + 2.7139006e-05F, 2.8902651e-05F, 3.0780908e-05F, 3.2781225e-05F, + 3.4911534e-05F, 3.7180282e-05F, 3.9596466e-05F, 4.2169667e-05F, + 4.4910090e-05F, 4.7828601e-05F, 5.0936773e-05F, 5.4246931e-05F, + 5.7772202e-05F, 6.1526565e-05F, 6.5524908e-05F, 6.9783085e-05F, + 7.4317983e-05F, 7.9147585e-05F, 8.4291040e-05F, 8.9768747e-05F, + 9.5602426e-05F, 0.00010181521F, 0.00010843174F, 0.00011547824F, + 0.00012298267F, 0.00013097477F, 0.00013948625F, 0.00014855085F, + 0.00015820453F, 0.00016848555F, 0.00017943469F, 0.00019109536F, + 0.00020351382F, 0.00021673929F, 0.00023082423F, 0.00024582449F, + 0.00026179955F, 0.00027881276F, 0.00029693158F, 0.00031622787F, + 0.00033677814F, 0.00035866388F, 0.00038197188F, 0.00040679456F, + 0.00043323036F, 0.00046138411F, 0.00049136745F, 0.00052329927F, + 0.00055730621F, 0.00059352311F, 0.00063209358F, 0.00067317058F, + 0.00071691700F, 0.00076350630F, 0.00081312324F, 0.00086596457F, + 0.00092223983F, 0.00098217216F, 0.0010459992F, 0.0011139742F, + 0.0011863665F, 0.0012634633F, 0.0013455702F, 0.0014330129F, + 0.0015261382F, 0.0016253153F, 0.0017309374F, 0.0018434235F, + 0.0019632195F, 0.0020908006F, 0.0022266726F, 0.0023713743F, + 0.0025254795F, 0.0026895994F, 0.0028643847F, 0.0030505286F, + 0.0032487691F, 0.0034598925F, 0.0036847358F, 0.0039241906F, + 0.0041792066F, 0.0044507950F, 0.0047400328F, 0.0050480668F, + 0.0053761186F, 0.0057254891F, 0.0060975636F, 0.0064938176F, + 0.0069158225F, 0.0073652516F, 0.0078438871F, 0.0083536271F, + 0.0088964928F, 0.009474637F, 0.010090352F, 0.010746080F, + 0.011444421F, 0.012188144F, 0.012980198F, 0.013823725F, + 0.014722068F, 0.015678791F, 0.016697687F, 0.017782797F, + 0.018938423F, 0.020169149F, 0.021479854F, 0.022875735F, + 0.024362330F, 0.025945531F, 0.027631618F, 0.029427276F, + 0.031339626F, 0.033376252F, 0.035545228F, 0.037855157F, + 0.040315199F, 0.042935108F, 0.045725273F, 0.048696758F, + 0.051861348F, 0.055231591F, 0.058820850F, 0.062643361F, + 0.066714279F, 0.071049749F, 0.075666962F, 0.080584227F, + 0.085821044F, 0.091398179F, 0.097337747F, 0.10366330F, + 0.11039993F, 0.11757434F, 0.12521498F, 0.13335215F, + 0.14201813F, 0.15124727F, 0.16107617F, 0.17154380F, + 0.18269168F, 0.19456402F, 0.20720788F, 0.22067342F, + 0.23501402F, 0.25028656F, 0.26655159F, 0.28387361F, + 0.30232132F, 0.32196786F, 0.34289114F, 0.36517414F, + 0.38890521F, 0.41417847F, 0.44109412F, 0.46975890F, + 0.50028648F, 0.53279791F, 0.56742212F, 0.60429640F, + 0.64356699F, 0.68538959F, 0.72993007F, 0.77736504F, + 0.82788260F, 0.88168307F, 0.9389798F, 1.F, +}; + +static void render_line(int x0,int x1,int y0,int y1,float *d){ + int dy=y1-y0; + int adx=x1-x0; + int ady=abs(dy); + int base=dy/adx; + int sy=(dy<0?base-1:base+1); + int x=x0; + int y=y0; + int err=0; + + ady-=abs(base*adx); + + d[x]*=FLOOR_fromdB_LOOKUP[y]; + while(++x<x1){ + err=err+ady; + if(err>=adx){ + err-=adx; + y+=sy; + }else{ + y+=base; + } + d[x]*=FLOOR_fromdB_LOOKUP[y]; + } +} + +static void render_line0(int x0,int x1,int y0,int y1,float *d){ + int dy=y1-y0; + int adx=x1-x0; + int ady=abs(dy); + int base=dy/adx; + int sy=(dy<0?base-1:base+1); + int x=x0; + int y=y0; + int err=0; + + ady-=abs(base*adx); + + d[x]=FLOOR_fromdB_LOOKUP[y]; + while(++x<x1){ + err=err+ady; + if(err>=adx){ + err-=adx; + y+=sy; + }else{ + y+=base; + } + d[x]=FLOOR_fromdB_LOOKUP[y]; + } +} + +/* the floor has already been filtered to only include relevant sections */ +static int accumulate_fit(const float *flr,const float *mdct, + int x0, int x1,lsfit_acc *a, + int n,vorbis_info_floor1 *info){ + long i; + int quantized=vorbis_dBquant(flr+x0); + + long xa=0,ya=0,x2a=0,y2a=0,xya=0,na=0, xb=0,yb=0,x2b=0,y2b=0,xyb=0,nb=0; + + memset(a,0,sizeof(*a)); + a->x0=x0; + a->x1=x1; + a->edgey0=quantized; + if(x1>n)x1=n; + + for(i=x0;i<x1;i++){ + int quantized=vorbis_dBquant(flr+i); + if(quantized){ + if(mdct[i]+info->twofitatten>=flr[i]){ + xa += i; + ya += quantized; + x2a += i*i; + y2a += quantized*quantized; + xya += i*quantized; + na++; + }else{ + xb += i; + yb += quantized; + x2b += i*i; + y2b += quantized*quantized; + xyb += i*quantized; + nb++; + } + } + } + + xb+=xa; + yb+=ya; + x2b+=x2a; + y2b+=y2a; + xyb+=xya; + nb+=na; + + /* weight toward the actually used frequencies if we meet the threshhold */ + { + int weight; + if(nb<info->twofitminsize || na<info->twofitminused){ + weight=0; + }else{ + weight=nb*info->twofitweight/na; + } + a->xa=xa*weight+xb; + a->ya=ya*weight+yb; + a->x2a=x2a*weight+x2b; + a->y2a=y2a*weight+y2b; + a->xya=xya*weight+xyb; + a->an=na*weight+nb; + a->n=nb; + a->un=na; + if(nb>=info->unusedminsize)a->un++; + } + + a->edgey1=-200; + if(x1<n){ + int quantized=vorbis_dBquant(flr+i); + a->edgey1=quantized; + } + return(a->n); +} + +/* returns < 0 on too few points to fit, >=0 (meansq error) on success */ +static int fit_line(lsfit_acc *a,int fits,int *y0,int *y1){ + long x=0,y=0,x2=0,y2=0,xy=0,n=0,an=0,i; + long x0=a[0].x0; + long x1=a[fits-1].x1; + + for(i=0;i<fits;i++){ + if(a[i].un){ + x+=a[i].xa; + y+=a[i].ya; + x2+=a[i].x2a; + y2+=a[i].y2a; + xy+=a[i].xya; + n+=a[i].n; + an+=a[i].an; + } + } + + if(*y0>=0){ /* hint used to break degenerate cases */ + x+= x0; + y+= *y0; + x2+= x0 * x0; + y2+= *y0 * *y0; + xy+= *y0 * x0; + n++; + an++; + } + + if(*y1>=0){ /* hint used to break degenerate cases */ + x+= x1; + y+= *y1; + x2+= x1 * x1; + y2+= *y1 * *y1; + xy+= *y1 * x1; + n++; + an++; + } + + if(n<2)return(n-2); + + { + /* need 64 bit multiplies, which C doesn't give portably as int */ + double fx=x; + double fy=y; + double fx2=x2; + double fxy=xy; + double denom=1./(an*fx2-fx*fx); + double a=(fy*fx2-fxy*fx)*denom; + double b=(an*fxy-fx*fy)*denom; + *y0=rint(a+b*x0); + *y1=rint(a+b*x1); + + /* limit to our range! */ + if(*y0>1023)*y0=1023; + if(*y1>1023)*y1=1023; + if(*y0<0)*y0=0; + if(*y1<0)*y1=0; + + return(0); + } +} + +/*static void fit_line_point(lsfit_acc *a,int fits,int *y0,int *y1){ + long y=0; + int i; + + for(i=0;i<fits && y==0;i++) + y+=a[i].ya; + + *y0=*y1=y; + }*/ + +static int inspect_error(int x0,int x1,int y0,int y1,const float *mask, + const float *mdct, + vorbis_info_floor1 *info){ + int dy=y1-y0; + int adx=x1-x0; + int ady=abs(dy); + int base=dy/adx; + int sy=(dy<0?base-1:base+1); + int x=x0; + int y=y0; + int err=0; + int val=vorbis_dBquant(mask+x); + int mse=0; + int n=0; + + ady-=abs(base*adx); + + if(mdct[x]+info->twofitatten>=mask[x]){ + if(y+info->maxover<val)return(1); + if(y-info->maxunder>val)return(1); + mse=(y-val); + mse*=mse; + n++; + } + + while(++x<x1){ + err=err+ady; + if(err>=adx){ + err-=adx; + y+=sy; + }else{ + y+=base; + } + + if(mdct[x]+info->twofitatten>=mask[x]){ + val=vorbis_dBquant(mask+x); + if(val){ + if(y+info->maxover<val)return(1); + if(y-info->maxunder>val)return(1); + mse+=((y-val)*(y-val)); + n++; + } + } + } + + if(n){ + if(info->maxover*info->maxover/n>info->maxerr)return(0); + if(info->maxunder*info->maxunder/n>info->maxerr)return(0); + if(mse/n>info->maxerr)return(1); + } + return(0); +} + +static int post_Y(int *A,int *B,int pos){ + if(A[pos]<0) + return B[pos]; + if(B[pos]<0) + return A[pos]; + return (A[pos]+B[pos])>>1; +} + +static int floor1_forward(vorbis_block *vb,vorbis_look_floor *in, + float *mdct, const float *logmdct, /* in */ + const float *logmask, const float *logmax, /* in */ + float *codedflr){ /* out */ + static int seq=0; + long i,j,k,l; + vorbis_look_floor1 *look=(vorbis_look_floor1 *)in; + vorbis_info_floor1 *info=look->vi; + long n=info->n; + long posts=look->posts; + long nonzero=0; + lsfit_acc fits[VIF_POSIT+1]; + int fit_valueA[VIF_POSIT+2]; /* index by range list position */ + int fit_valueB[VIF_POSIT+2]; /* index by range list position */ + int fit_flag[VIF_POSIT+2]; + + int loneighbor[VIF_POSIT+2]; /* sorted index of range list position (+2) */ + int hineighbor[VIF_POSIT+2]; + int memo[VIF_POSIT+2]; + codec_setup_info *ci=vb->vd->vi->codec_setup; + static_codebook **sbooks=ci->book_param; + codebook *books=NULL; + int writeflag=0; + + if(vb->vd->backend_state){ + books=((backend_lookup_state *)(vb->vd->backend_state))-> + fullbooks; + writeflag=1; + } + + memset(fit_flag,0,sizeof(fit_flag)); + for(i=0;i<posts;i++)loneighbor[i]=0; /* 0 for the implicit 0 post */ + for(i=0;i<posts;i++)hineighbor[i]=1; /* 1 for the implicit post at n */ + for(i=0;i<posts;i++)memo[i]=-1; /* no neighbor yet */ + + /* Scan back from high edge to first 'used' frequency */ + for(;n>info->unusedmin_n;n--) + if(logmdct[n-1]>-floor1_rangedB && + logmdct[n-1]+info->twofitatten>logmask[n-1])break; + + /* quantize the relevant floor points and collect them into line fit + structures (one per minimal division) at the same time */ + if(posts==0){ + nonzero+=accumulate_fit(logmask,logmax,0,n,fits,n,info); + }else{ + for(i=0;i<posts-1;i++) + nonzero+=accumulate_fit(logmask,logmax,look->sorted_index[i], + look->sorted_index[i+1],fits+i, + n,info); + } + + if(nonzero){ + /* start by fitting the implicit base case.... */ + int y0=-200; + int y1=-200; + int mse=fit_line(fits,posts-1,&y0,&y1); + if(mse<0){ + /* Only a single nonzero point */ + y0=-200; + y1=0; + fit_line(fits,posts-1,&y0,&y1); + } + + fit_flag[0]=1; + fit_flag[1]=1; + fit_valueA[0]=y0; + fit_valueB[0]=y0; + fit_valueB[1]=y1; + fit_valueA[1]=y1; + + if(mse>=0){ + /* Non degenerate case */ + /* start progressive splitting. This is a greedy, non-optimal + algorithm, but simple and close enough to the best + answer. */ + for(i=2;i<posts;i++){ + int sortpos=look->reverse_index[i]; + int ln=loneighbor[sortpos]; + int hn=hineighbor[sortpos]; + + /* eliminate repeat searches of a particular range with a memo */ + if(memo[ln]!=hn){ + /* haven't performed this error search yet */ + int lsortpos=look->reverse_index[ln]; + int hsortpos=look->reverse_index[hn]; + memo[ln]=hn; + + /* if this is an empty segment, its endpoints don't matter. + Mark as such */ + for(j=lsortpos;j<hsortpos;j++) + if(fits[j].un)break; + if(j==hsortpos){ + /* empty segment; important to note that this does not + break 0/n post case */ + fit_valueB[ln]=-200; + if(fit_valueA[ln]<0) + fit_flag[ln]=0; + fit_valueA[hn]=-200; + if(fit_valueB[hn]<0) + fit_flag[hn]=0; + + }else{ + /* A note: we want to bound/minimize *local*, not global, error */ + int lx=info->postlist[ln]; + int hx=info->postlist[hn]; + int ly=post_Y(fit_valueA,fit_valueB,ln); + int hy=post_Y(fit_valueA,fit_valueB,hn); + + if(inspect_error(lx,hx,ly,hy,logmask,logmdct,info)){ + /* outside error bounds/begin search area. Split it. */ + int ly0=-200; + int ly1=-200; + int hy0=-200; + int hy1=-200; + int lmse=fit_line(fits+lsortpos,sortpos-lsortpos,&ly0,&ly1); + int hmse=fit_line(fits+sortpos,hsortpos-sortpos,&hy0,&hy1); + + /* the boundary/sparsity cases are the hard part. They + don't happen often given that we use the full mask + curve (weighted) now, but when they do happen they + can go boom. Pay them detailed attention */ + /* cases for a segment: + >=0) normal fit (>=2 unique points) + -1) one point on x0; + one point on x1; <-- disallowed by fit_line + -2) one point in between x0 and x1 + -3) no points */ + + switch(lmse){ + case -2: + /* no points in the low segment */ + break; + case -1: + ly0=fits[lsortpos].edgey0; + break; + /*default: + break;*/ + } + + switch(hmse){ + case -2: + /* no points in the hi segment */ + break; + case -1: + hy0=fits[sortpos].edgey0; + break; + } + + /* store new edge values */ + fit_valueB[ln]=ly0; + if(ln==0 && ly0>=0)fit_valueA[ln]=ly0; + fit_valueA[i]=ly1; + fit_valueB[i]=hy0; + fit_valueA[hn]=hy1; + if(hn==1 && hy1>=0)fit_valueB[hn]=hy1; + + if(ly0<0 && fit_valueA[ln]<0) + fit_flag[ln]=0; + if(hy1<0 && fit_valueB[hn]<0) + fit_flag[hn]=0; + + if(ly1>=0 || hy0>=0){ + /* store new neighbor values */ + for(j=sortpos-1;j>=0;j--) + if(hineighbor[j]==hn) + hineighbor[j]=i; + else + break; + for(j=sortpos+1;j<posts;j++) + if(loneighbor[j]==ln) + loneighbor[j]=i; + else + break; + + /* store flag (set) */ + fit_flag[i]=1; + } + } + } + } + } + } + + /* quantize values to multiplier spec */ + switch(info->mult){ + case 1: /* 1024 -> 256 */ + for(i=0;i<posts;i++) + if(fit_flag[i]) + fit_valueA[i]=post_Y(fit_valueA,fit_valueB,i)>>2; + break; + case 2: /* 1024 -> 128 */ + for(i=0;i<posts;i++) + if(fit_flag[i]) + fit_valueA[i]=post_Y(fit_valueA,fit_valueB,i)>>3; + break; + case 3: /* 1024 -> 86 */ + for(i=0;i<posts;i++) + if(fit_flag[i]) + fit_valueA[i]=post_Y(fit_valueA,fit_valueB,i)/12; + break; + case 4: /* 1024 -> 64 */ + for(i=0;i<posts;i++) + if(fit_flag[i]) + fit_valueA[i]=post_Y(fit_valueA,fit_valueB,i)>>4; + break; + } + + /* find prediction values for each post and subtract them */ + for(i=2;i<posts;i++){ + int sp=look->reverse_index[i]; + int ln=look->loneighbor[i-2]; + int hn=look->hineighbor[i-2]; + int x0=info->postlist[ln]; + int x1=info->postlist[hn]; + int y0=fit_valueA[ln]; + int y1=fit_valueA[hn]; + + int predicted=render_point(x0,x1,y0,y1,info->postlist[i]); + + if(fit_flag[i]){ + int headroom=(look->quant_q-predicted<predicted? + look->quant_q-predicted:predicted); + + int val=fit_valueA[i]-predicted; + + /* at this point the 'deviation' value is in the range +/- max + range, but the real, unique range can always be mapped to + only [0-maxrange). So we want to wrap the deviation into + this limited range, but do it in the way that least screws + an essentially gaussian probability distribution. */ + + if(val<0) + if(val<-headroom) + val=headroom-val-1; + else + val=-1-(val<<1); + else + if(val>=headroom) + val= val+headroom; + else + val<<=1; + + fit_valueB[i]=val; + + /* unroll the neighbor arrays */ + for(j=sp+1;j<posts;j++) + if(loneighbor[j]==i) + loneighbor[j]=loneighbor[sp]; + else + break; + for(j=sp-1;j>=0;j--) + if(hineighbor[j]==i) + hineighbor[j]=hineighbor[sp]; + else + break; + + }else{ + fit_valueA[i]=predicted; + fit_valueB[i]=0; + } + if(fit_valueB[i]==0) + fit_valueA[i]|=0x8000; + else{ + fit_valueA[look->loneighbor[i-2]]&=0x7fff; + fit_valueA[look->hineighbor[i-2]]&=0x7fff; + } + } + + /* we have everything we need. pack it out */ + /* mark nontrivial floor */ + if(writeflag){ + oggpack_write(&vb->opb,1,1); + + /* beginning/end post */ + look->frames++; + look->postbits+=ilog(look->quant_q-1)*2; + oggpack_write(&vb->opb,fit_valueA[0],ilog(look->quant_q-1)); + oggpack_write(&vb->opb,fit_valueA[1],ilog(look->quant_q-1)); + + + /* partition by partition */ + for(i=0,j=2;i<info->partitions;i++){ + int class=info->partitionclass[i]; + int cdim=info->class_dim[class]; + int csubbits=info->class_subs[class]; + int csub=1<<csubbits; + int bookas[8]={0,0,0,0,0,0,0,0}; + int cval=0; + int cshift=0; + + /* generate the partition's first stage cascade value */ + if(csubbits){ + int maxval[8]; + for(k=0;k<csub;k++){ + int booknum=info->class_subbook[class][k]; + if(booknum<0){ + maxval[k]=1; + }else{ + maxval[k]=sbooks[info->class_subbook[class][k]]->entries; + } + } + for(k=0;k<cdim;k++){ + for(l=0;l<csub;l++){ + int val=fit_valueB[j+k]; + if(val<maxval[l]){ + bookas[k]=l; + break; + } + } + cval|= bookas[k]<<cshift; + cshift+=csubbits; + } + /* write it */ + look->phrasebits+= + vorbis_book_encode(books+info->class_book[class],cval,&vb->opb); + +#ifdef TRAIN_FLOOR1 + { + FILE *of; + char buffer[80]; + sprintf(buffer,"line_%dx%ld_class%d.vqd", + vb->pcmend/2,posts-2,class); + of=fopen(buffer,"a"); + fprintf(of,"%d\n",cval); + fclose(of); + } +#endif + } + + /* write post values */ + for(k=0;k<cdim;k++){ + int book=info->class_subbook[class][bookas[k]]; + if(book>=0){ + /* hack to allow training with 'bad' books */ + if(fit_valueB[j+k]<(books+book)->entries) + look->postbits+=vorbis_book_encode(books+book, + fit_valueB[j+k],&vb->opb); + /*else + fprintf(stderr,"+!");*/ + +#ifdef TRAIN_FLOOR1 + { + FILE *of; + char buffer[80]; + sprintf(buffer,"line_%dx%ld_%dsub%d.vqd", + vb->pcmend/2,posts-2,class,bookas[k]); + of=fopen(buffer,"a"); + fprintf(of,"%d\n",fit_valueB[j+k]); + fclose(of); + } +#endif + } + } + j+=cdim; + } + } + + { + /* generate quantized floor equivalent to what we'd unpack in decode */ + int hx; + int lx=0; + int ly=fit_valueA[0]*info->mult; + + for(j=1;j<posts;j++){ + int current=look->forward_index[j]; + if(!(fit_valueA[current]&0x8000)){ + int hy=(fit_valueA[current]&0x7fff)*info->mult; + hx=info->postlist[current]; + + render_line0(lx,hx,ly,hy,codedflr); + + lx=hx; + ly=hy; + } + } + for(j=lx;j<vb->pcmend/2;j++)codedflr[j]=codedflr[j-1]; /* be certain */ + + /* use it to create residue vector. Eliminate mdct elements + that were below the error training attenuation relative to + the original mask. This avoids portions of the floor fit + that were considered 'unused' in fitting from being used in + coding residue if the unfit values are significantly below + the original input mask */ + + for(j=0;j<n;j++) + if(logmdct[j]+info->twofitatten<logmask[j]) + mdct[j]=0.f; + for(j=n;j<vb->pcmend/2;j++)mdct[j]=0.f; + + } + + }else{ + if(writeflag)oggpack_write(&vb->opb,0,1); + memset(codedflr,0,n*sizeof(*codedflr)); + memset(mdct,0,n*sizeof(*mdct)); + } + seq++; + return(nonzero); +} + +static void *floor1_inverse1(vorbis_block *vb,vorbis_look_floor *in){ + vorbis_look_floor1 *look=(vorbis_look_floor1 *)in; + vorbis_info_floor1 *info=look->vi; + + int i,j,k; + codebook *books=((backend_lookup_state *)(vb->vd->backend_state))-> + fullbooks; + + /* unpack wrapped/predicted values from stream */ + if(oggpack_read(&vb->opb,1)==1){ + int *fit_value=_vorbis_block_alloc(vb,(look->posts)*sizeof(*fit_value)); + + fit_value[0]=oggpack_read(&vb->opb,ilog(look->quant_q-1)); + fit_value[1]=oggpack_read(&vb->opb,ilog(look->quant_q-1)); + + /* partition by partition */ + /* partition by partition */ + for(i=0,j=2;i<info->partitions;i++){ + int class=info->partitionclass[i]; + int cdim=info->class_dim[class]; + int csubbits=info->class_subs[class]; + int csub=1<<csubbits; + int cval=0; + + /* decode the partition's first stage cascade value */ + if(csubbits){ + cval=vorbis_book_decode(books+info->class_book[class],&vb->opb); + + if(cval==-1)goto eop; + } + + for(k=0;k<cdim;k++){ + int book=info->class_subbook[class][cval&(csub-1)]; + cval>>=csubbits; + if(book>=0){ + if((fit_value[j+k]=vorbis_book_decode(books+book,&vb->opb))==-1) + goto eop; + }else{ + fit_value[j+k]=0; + } + } + j+=cdim; + } + + /* unwrap positive values and reconsitute via linear interpolation */ + for(i=2;i<look->posts;i++){ + int predicted=render_point(info->postlist[look->loneighbor[i-2]], + info->postlist[look->hineighbor[i-2]], + fit_value[look->loneighbor[i-2]], + fit_value[look->hineighbor[i-2]], + info->postlist[i]); + int hiroom=look->quant_q-predicted; + int loroom=predicted; + int room=(hiroom<loroom?hiroom:loroom)<<1; + int val=fit_value[i]; + + if(val){ + if(val>=room){ + if(hiroom>loroom){ + val = val-loroom; + }else{ + val = -1-(val-hiroom); + } + }else{ + if(val&1){ + val= -((val+1)>>1); + }else{ + val>>=1; + } + } + + fit_value[i]=val+predicted; + fit_value[look->loneighbor[i-2]]&=0x7fff; + fit_value[look->hineighbor[i-2]]&=0x7fff; + + }else{ + fit_value[i]=predicted|0x8000; + } + + } + + return(fit_value); + } + eop: + return(NULL); +} + +static int floor1_inverse2(vorbis_block *vb,vorbis_look_floor *in,void *memo, + float *out){ + vorbis_look_floor1 *look=(vorbis_look_floor1 *)in; + vorbis_info_floor1 *info=look->vi; + + codec_setup_info *ci=vb->vd->vi->codec_setup; + int n=ci->blocksizes[vb->mode]/2; + int j; + + if(memo){ + /* render the lines */ + int *fit_value=(int *)memo; + int hx; + int lx=0; + int ly=fit_value[0]*info->mult; + for(j=1;j<look->posts;j++){ + int current=look->forward_index[j]; + int hy=fit_value[current]&0x7fff; + if(hy==fit_value[current]){ + + hy*=info->mult; + hx=info->postlist[current]; + + render_line(lx,hx,ly,hy,out); + + lx=hx; + ly=hy; + } + } + for(j=hx;j<n;j++)out[j]*=out[j-1]; /* be certain */ + return(1); + } + memset(out,0,sizeof(*out)*n); + return(0); +} + +/* export hooks */ +vorbis_func_floor floor1_exportbundle={ + &floor1_pack,&floor1_unpack,&floor1_look,&floor1_copy_info,&floor1_free_info, + &floor1_free_look,&floor1_forward,&floor1_inverse1,&floor1_inverse2 +}; + diff --git a/lib/mapping0.c b/lib/mapping0.c index dd0f9394..17c6fc16 100644 --- a/lib/mapping0.c +++ b/lib/mapping0.c @@ -11,7 +11,7 @@ ******************************************************************** function: channel mapping 0 implementation - last mod: $Id: mapping0.c,v 1.37.2.7 2001/11/16 08:17:05 xiphmont Exp $ + last mod: $Id: mapping0.c,v 1.37.2.8 2001/11/22 06:21:08 xiphmont Exp $ ********************************************************************/ @@ -287,7 +287,9 @@ static int mapping0_forward(vorbis_block *vb,vorbis_look_mapping *l){ vorbis_dsp_state *vd=vb->vd; vorbis_info *vi=vd->vi; codec_setup_info *ci=vi->codec_setup; + bitrate_manager_info *bi=ci->bi; backend_lookup_state *b=vb->vd->backend_state; + bitrate_manager_state *bm=&b->bms; vorbis_look_mapping0 *look=(vorbis_look_mapping0 *)l; vorbis_info_mapping0 *info=look->map; vorbis_info_mode *mode=look->mode; @@ -392,7 +394,7 @@ static int mapping0_forward(vorbis_block *vb,vorbis_look_mapping *l){ global_ampmax, local_ampmax[i], ci->blocksizes[vb->lW]/2, - b->bitrate_avgnoise); + bm->avgnoise); _analysis_output("mask",seq+i,logmask,n/2,1,0); /* perform floor encoding */ @@ -464,11 +466,10 @@ static int mapping0_forward(vorbis_block *vb,vorbis_look_mapping *l){ int *chbundle=alloca(sizeof(*chbundle)*info->submaps); int chcounter=0; - long maxbits,minbits; + long minbits; /* play a little loose with this abstraction */ int quant_passes=ci->coupling_passes; - int stopflag=0,stoppos=0; for(i=0;i<vi->channels;i++){ quantized[i]=_vorbis_block_alloc(vb,n*sizeof(*sofar[i])); @@ -532,129 +533,29 @@ static int mapping0_forward(vorbis_block *vb,vorbis_look_mapping *l){ class(vb,look->residue_look[i],pcmbundle[i],zerobundle[i],chbundle[i]); } - /* basic bitrate fitting algorithm: - determine a current-packet maximum size from the bound queue and - point maximums - determine a current-packet minimum size from the bound queue and - point minimums - determine a desired packet size: - if there's a requested average, get that from the floater - else, use the bits sunk by a single iteration (bounded by min/max) - */ - { - long maxbits_absolute= - (vb->W? - (ci->bitrate_absolute_max_long>0? - ci->bitrate_absolute_max_long/vi->rate*ci->blocksizes[1]/2:-1): - (ci->bitrate_absolute_max_short>0? - ci->bitrate_absolute_max_short/vi->rate*ci->blocksizes[0]/2:-1)); - long minbits_absolute= - (vb->W? - (ci->bitrate_absolute_min_long>0? - ci->bitrate_absolute_min_long/vi->rate*ci->blocksizes[1]/2:-1): - (ci->bitrate_absolute_min_short>0? - ci->bitrate_absolute_min_short/vi->rate*ci->blocksizes[0]/2:-1)); - - long minbits_period=ci->bitrate_queue_hardmin/vi->rate* - (b->bitrate_queue_sampleacc[0]+ci->blocksizes[vb->W]/2)- - b->bitrate_queue_bitacc[0]; - - long period_samples=max(ci->bitrate_queue_time*vi->rate, - b->bitrate_queue_sampleacc[0]); - long maxbits_period=-1; - - maxbits=-1; - minbits=-1; - - /* pessimistic checkahead */ - for(i=0;i<8;i++){ - long ahead_samples=period_samples-b->bitrate_queue_sampleacc[i]; - if(ahead_samples>=0){ - long maxbits_local=ci->bitrate_queue_hardmax/vi->rate* - (period_samples+ci->blocksizes[vb->W]/2)- - b->bitrate_queue_bitacc[i]- - ci->bitrate_queue_hardmax/vi->rate*ahead_samples; - - if(maxbits_period==-1 || maxbits_local<maxbits_period) - maxbits_period=maxbits_local; - - } - } - - - fprintf(stderr,"maxbits_a %ld maxbit_period %ld\n",maxbits_absolute,maxbits_period); - - if(maxbits_absolute>=0.){ - if(maxbits_period>=0.){ - maxbits=min(maxbits_period,maxbits_absolute); - }else{ - maxbits=maxbits_absolute; - } - }else - if(maxbits_period>=0)maxbits=maxbits_period; - minbits=max(minbits_period,minbits_absolute); - - if(maxbits>=0)minbits=min(minbits,maxbits); - } + /* this is the only good place to enforce minimum by-packet bitrate */ + if(vb->W) + minbits=bi->absolute_min_long/vi->rate*ci->blocksizes[1]/2; + else + minbits=bi->absolute_min_short/vi->rate*ci->blocksizes[0]/2; - /* actual encoding loop; if we have a desired stopping point, pack - slightly past it so that the end of the packet is not - uninitialized data that could pollute the decoded audio on the - decode side. We want to truncate at a clean byte boundary. + /* actual encoding loop; we pack all the iterations to collect + management data */ - If we're doing an average bitrate, we need to encode to the - bitter end then truncate (so that we can collect bit usage - statistics for floater adjustment) */ - for(i=0;!stopflag;){ + for(i=0;i<quant_passes;){ /* perform residue encoding of this pass's quantized residue vector, according residue mapping */ for(j=0;j<info->submaps;j++){ - ogg_uint32_t *queueptr=b->bitrate_queue_binned; - if(queueptr)queueptr+=b->bitrate_queue_head*b->bitrate_bins; - - if(stoppos){ - look->residue_func[j]-> - forward(vb,look->residue_look[j], - qbundle[j],sobundle[j],zerobundle[j],chbundle[j], - i,classifications[j],b->bitrate_avgfloat,queueptr); - - }else{ - stoppos=look->residue_func[j]-> - forward(vb,look->residue_look[j], - qbundle[j],sobundle[j],zerobundle[j],chbundle[j], - i,classifications[j],b->bitrate_avgfloat,queueptr); - } + look->residue_func[j]-> + forward(vb,look->residue_look[j], + qbundle[j],sobundle[j],zerobundle[j],chbundle[j], + i,classifications[j],vbi->packet_markers); + } i++; - - /* bitrate management.... deciding when it's time to stop. */ - if(i<quant_passes){ - if(b->bitrate_bins==0){ /* average bitrate always runs - encode to the bitter end in - order to collect statistics */ - - long current_bytes=oggpack_bits(&vb->opb)/8; - - if(maxbits>=0 && current_bytes>maxbits/8){ - /* maxbits trumps all... */ - stoppos=maxbits/8; - stopflag=1; - }else{ - if(current_bytes>(minbits+7)/8){ - if(ci->passlimit[i-1]>=b->bitrate_avgfloat){ - if(!stoppos)stoppos=current_bytes; - if(stoppos<current_bytes) - stopflag=1; - } - } - } - } - }else - stopflag=1; - /* down-couple/down-quantize from perfect-'so-far' -> new quantized vector */ if(info->coupling_steps==0){ @@ -684,182 +585,8 @@ static int mapping0_forward(vorbis_block *vb,vorbis_look_mapping *l){ _analysis_output(buf,seq+j,quantized[j],n/2,1,0); } - - /* steady as she goes */ } - - /* truncate the packet according to stoppos */ - if(!stoppos)stoppos=oggpack_bytes(&vb->opb); - if(minbits>=0 && stoppos*8<minbits)stoppos=(minbits+7)/8; - if(maxbits>=0 && stoppos*8>maxbits)stoppos=maxbits/8; - if(stoppos>oggpack_bytes(&vb->opb))stoppos=oggpack_bytes(&vb->opb); - vb->opb.endbyte=stoppos; - vb->opb.endbit=0; - seq+=vi->channels; - - fprintf(stderr,"Bitrate: cav %d, cmin %ld, cmax %ld, float %.1f," - " this %ld\n", - (int)((double)b->bitrate_queue_bitacc[0]*vi->rate/b->bitrate_queue_sampleacc[0]), - minbits,maxbits,b->bitrate_avgfloat, - oggpack_bytes(&vb->opb)*8); - - /* track bitrate*/ - /* update boundary accumulators */ - for(i=0;i<8;i++){ - long desired=ci->bitrate_queue_time*vi->rate; - switch(i){ - case 0: - break; - case 1: - desired-=ci->blocksizes[0]/2; - break; - case 2: - desired-=ci->blocksizes[0]*2; - break; - case 3: - desired-=ci->blocksizes[0]*8; - break; - case 4: - desired-=ci->bitrate_queue_time*vi->rate*(1./16.); - break; - case 5: - desired-=ci->bitrate_queue_time*vi->rate*(1./8.); - break; - case 6: - desired-=ci->bitrate_queue_time*vi->rate*(1./4.); - break; - case 7: - desired-=ci->bitrate_queue_time*vi->rate*(1./2.); - break; - } - - while(b->bitrate_queue_sampleacc[i]>desired){ - int samples=ci->blocksizes[0]>>1; - if(b->bitrate_queue_actual[b->bitrate_queue_tail[i]]&0x80000000UL) - samples=ci->blocksizes[1]>>1; - b->bitrate_queue_sampleacc[i]-=samples; - b->bitrate_queue_bitacc[i]-= - b->bitrate_queue_actual[b->bitrate_queue_tail[i]]&0x7fffffffUL; - - /* update moving average accumulators */ - if(i==0){ - for(j=0;j<b->bitrate_bins;j++) - b->bitrate_queue_binacc[j]-= - b->bitrate_queue_binned[b->bitrate_queue_tail[0]*b->bitrate_bins+j]; - - /* watch the running noise offset trigger */ - if(b->bitrate_noisetrigger_postpone)--b->bitrate_noisetrigger_postpone; - } - - b->bitrate_queue_tail[i]++; - if(b->bitrate_queue_tail[i]>=b->bitrate_queue_size) - b->bitrate_queue_tail[i]=0; - - } - } - - /* update queue head */ - if(oggpack_bytes(&vb->opb)>2){ - - int bits=oggpack_bytes(&vb->opb)*8; - - /* boundaries */ - b->bitrate_queue_actual[b->bitrate_queue_head]=bits; - if(vb->W)b->bitrate_queue_actual[b->bitrate_queue_head]|=0x80000000UL; - for(i=0;i<8;i++){ - b->bitrate_queue_bitacc[i]+=bits; - b->bitrate_queue_sampleacc[i]+=ci->blocksizes[vb->W]>>1; - } - - /* bins */ - if(b->bitrate_bins){ - for(i=0;i<b->bitrate_bins;i++) - b->bitrate_queue_binacc[i]+= - b->bitrate_queue_binned[b->bitrate_queue_head*b->bitrate_bins+i]; - } - - b->bitrate_queue_head++; - if(b->bitrate_queue_head>=b->bitrate_queue_size)b->bitrate_queue_head=0; - - } - - /* adjust the floater to offset bitrate above/below desired average */ - /* look for the bin settings in recent history that bracket - the desired bitrate, and interpolate twixt them for the - flaoter setting we want */ - - if(b->bitrate_bins>0 && - (b->bitrate_queue_sampleacc[0]>ci->bitrate_queue_time*vi->rate/8 || - b->bitrate_queue_sampleacc[0]>8192 || b->bitrate_queue_head>16)){ - - double upper=floater_interpolate(b,vi,ci->bitrate_queue_avgmax); - double lower=floater_interpolate(b,vi,ci->bitrate_queue_avgmin); - double new=ci->bitrate_avgfloat_initial; - double slew; - - if(upper>0. && upper<new)new=upper; - if(lower<ci->bitrate_avgfloat_minimum) - lower=ci->bitrate_avgfloat_minimum; - if(lower>new)new=lower; - - slew=new-b->bitrate_avgfloat; - - if(slew<ci->bitrate_avgfloat_downhyst || slew>ci->bitrate_avgfloat_uphyst){ - if(slew<ci->bitrate_avgfloat_downslew_max) - new=b->bitrate_avgfloat+ci->bitrate_avgfloat_downslew_max; - if(slew>ci->bitrate_avgfloat_upslew_max) - new=b->bitrate_avgfloat+ci->bitrate_avgfloat_upslew_max; - - b->bitrate_avgfloat=new; - } - - { - long queueusage=b->bitrate_queue_head; - if(b->bitrate_queue_tail[0]>b->bitrate_queue_head) - queueusage+=b->bitrate_queue_size; - queueusage-=b->bitrate_queue_tail[0]; - - if(b->bitrate_avgfloat<ci->bitrate_avgfloat_noisetrigger_low) - b->bitrate_noisetrigger_request+=1.f; - - if(b->bitrate_avgfloat>ci->bitrate_avgfloat_noisetrigger_high) - b->bitrate_noisetrigger_request-=1.f; - - if(b->bitrate_noisetrigger_postpone==0){ - if(b->bitrate_noisetrigger_request<0.){ - b->bitrate_avgnoise-=1.f; - if(b->bitrate_noisetrigger_request<10.) - b->bitrate_avgnoise-=1.f; - b->bitrate_noisetrigger_postpone=queueusage; - } - if(b->bitrate_noisetrigger_request>0.){ - b->bitrate_avgnoise+=1.f; - if(b->bitrate_noisetrigger_request>10.) - b->bitrate_avgnoise+=1.f; - b->bitrate_noisetrigger_postpone=queueusage; - } - - b->bitrate_noisetrigger_request=0.f; - if(b->bitrate_avgnoise>0) - b->bitrate_noisetrigger_request= -1.; - if(b->bitrate_avgnoise<0) - b->bitrate_noisetrigger_request= +1.; - - if(b->bitrate_avgnoise<ci->bitrate_avgfloat_noisetrigger_minoff) - b->bitrate_avgnoise=ci->bitrate_avgfloat_noisetrigger_minoff; - if(b->bitrate_avgnoise>ci->bitrate_avgfloat_noisetrigger_maxoff) - b->bitrate_avgnoise=ci->bitrate_avgfloat_noisetrigger_maxoff; - } - } - - fprintf(stderr,"\tupper:%g :: lower:%g (noise offset=%g, pending=%d, trigger=%d)\n", - upper,lower,b->bitrate_avgnoise,b->bitrate_noisetrigger_request, - b->bitrate_noisetrigger_postpone); - - - - } } diff --git a/lib/modes/mode_44c_A.h b/lib/modes/mode_44c_A.h index ce2c5094..d270f8be 100644 --- a/lib/modes/mode_44c_A.h +++ b/lib/modes/mode_44c_A.h @@ -11,7 +11,7 @@ ******************************************************************** function: predefined encoding modes; 44kHz stereo ~64kbps true VBR - last mod: $Id: mode_44c_A.h,v 1.4.2.8 2001/10/22 07:34:22 xiphmont Exp $ + last mod: $Id: mode_44c_A.h,v 1.4.2.9 2001/11/22 06:21:11 xiphmont Exp $ ********************************************************************/ @@ -80,6 +80,19 @@ #include "maskadj_A.h" +static bitrate_manager_info _bm_set_44c_A={ + /* progressive coding and bitrate controls */ + 110000,90000, -1,-1, + 2.,.5, + 1., 112000, 140000, + 124000, 128000, + + 4.0, 0., -1., .05, + -.05, .05, + 3.0,5.0, + -10.f,+4.f +}; + static vorbis_info_psy_global _psy_set_44c_AG={ 0, /* decaydBpms */ 8, /* lines per eighth octave */ @@ -377,14 +390,7 @@ codec_setup_info info_44c_A={ /* psy */ {&_psy_set_44c_A0,&_psy_set_44c_AT,&_psy_set_44c_A}, &_psy_set_44c_AG, - - /* progressive coding and bitrate controls */ - 110000,90000, 0,0, - 3., 112000, 140000, - 124000, 128000, - - 4.0, 0., -1., .05, - -.05, .05, + &_bm_set_44c_A, {3,4,6,8}, 4, @@ -11,7 +11,7 @@ ******************************************************************** function: residue backend 0, 1 and 2 implementation - last mod: $Id: res0.c,v 1.37.2.6 2001/10/22 09:09:29 xiphmont Exp $ + last mod: $Id: res0.c,v 1.37.2.7 2001/11/22 06:21:08 xiphmont Exp $ ********************************************************************/ @@ -49,7 +49,7 @@ typedef struct { long phrasebits; long frames; - int qoffsets[BITTRACK_DIVISOR]; + int qoffsets[BITTRACK_DIVISOR+1]; } vorbis_look_residue0; @@ -245,6 +245,8 @@ vorbis_look_residue *res0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm, for(i=0;i<BITTRACK_DIVISOR;i++) look->qoffsets[i]=partvals*(i+1)/BITTRACK_DIVISOR; + + look->qoffsets[i]=9999999; } return(look); @@ -467,7 +469,7 @@ static int _01forward(vorbis_block *vb,vorbis_look_residue *vl, int pass,long **partword, int (*encode)(oggpack_buffer *,float *,int, codebook *,vorbis_look_residue0 *), - double passlimit,ogg_uint32_t *stats){ + ogg_uint32_t *stats){ long i,j,k,s; vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl; vorbis_info_residue0 *info=look->info; @@ -487,10 +489,6 @@ static int _01forward(vorbis_block *vb,vorbis_look_residue *vl, long resbits[128]; long resvals[128]; - long wholepasses; - long partialpass; - int stoppos=0; - #ifdef TRAIN_RES FILE *of; char buffer[80]; @@ -511,9 +509,6 @@ static int _01forward(vorbis_block *vb,vorbis_look_residue *vl, } #endif - wholepasses=passlimit; - partialpass=(passlimit-wholepasses)*partvals; - memset(resbits,0,sizeof(resbits)); memset(resvals,0,sizeof(resvals)); @@ -529,11 +524,6 @@ static int _01forward(vorbis_block *vb,vorbis_look_residue *vl, for(i=0;i<partvals;){ - if(!stoppos && - s>=wholepasses && - i>=partialpass) - stoppos=oggpack_bytes(&vb->opb); - /* first we encode a partition codeword for each channel */ if(s==0){ for(j=0;j<ch;j++){ @@ -559,12 +549,10 @@ static int _01forward(vorbis_block *vb,vorbis_look_residue *vl, /* now we encode interleaved residual values for the partitions */ for(k=0;k<partitions_per_word && i<partvals;k++,i++){ long offset=i*samples_per_partition+info->begin; - - if(!stoppos && - s>=wholepasses && - i>=partialpass) - stoppos=oggpack_bytes(&vb->opb); + if(qptr)while(i>=look->qoffsets[bin]) + qptr[bin++]=oggpack_bits(&vb->opb); + for(j=0;j<ch;j++){ if(s==0)resvals[partword[j][i]]+=samples_per_partition; if(info->secondstages[partword[j][i]]&(1<<s)){ @@ -572,20 +560,14 @@ static int _01forward(vorbis_block *vb,vorbis_look_residue *vl, if(statebook){ int ret=encode(&vb->opb,in[j]+offset,samples_per_partition, statebook,look); - if(!stoppos){ - look->postbits+=ret; - resbits[partword[j][i]]+=ret; - } + look->postbits+=ret; + resbits[partword[j][i]]+=ret; } } } - - if(qptr)while(i>=look->qoffsets[bin]) - qptr[bin++]=oggpack_bits(&vb->opb); - - - } + if(qptr)while(i>=look->qoffsets[bin]) + qptr[bin++]=oggpack_bits(&vb->opb); } } @@ -601,7 +583,7 @@ static int _01forward(vorbis_block *vb,vorbis_look_residue *vl, fprintf(stderr,":: %ld:%1.2g\n",total,(double)totalbits/total); }*/ - return(stoppos); + return(0); } /* a truncated packet here just means 'stop working'; it's not an error */ @@ -679,7 +661,7 @@ long **res0_class(vorbis_block *vb,vorbis_look_residue *vl, int res0_forward(vorbis_block *vb,vorbis_look_residue *vl, float **in,float **out,int *nonzero,int ch, - int pass, long **partword,double passlimit,ogg_uint32_t *stats){ + int pass, long **partword,ogg_uint32_t *stats){ /* we encode only the nonzero parts of a bundle */ int i,j,used=0,n=vb->pcmend/2; for(i=0;i<ch;i++) @@ -690,7 +672,7 @@ int res0_forward(vorbis_block *vb,vorbis_look_residue *vl, } if(used){ int ret=_01forward(vb,vl,in,used,pass,partword, - _interleaved_encodepart,passlimit,stats); + _interleaved_encodepart,stats); used=0; for(i=0;i<ch;i++) if(nonzero[i]){ @@ -699,8 +681,12 @@ int res0_forward(vorbis_block *vb,vorbis_look_residue *vl, used++; } return(ret); - }else + }else{ + for(i=0;i<vorbis_bitrate_maxmarkers();i++) + stats[i]=oggpack_bits(&vb->opb); + return(0); + } } int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl, @@ -717,7 +703,7 @@ int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl, int res1_forward(vorbis_block *vb,vorbis_look_residue *vl, float **in,float **out,int *nonzero,int ch, - int pass, long **partword, double passlimit,ogg_uint32_t *stats){ + int pass, long **partword, ogg_uint32_t *stats){ int i,j,used=0,n=vb->pcmend/2; for(i=0;i<ch;i++) if(nonzero[i]){ @@ -727,7 +713,7 @@ int res1_forward(vorbis_block *vb,vorbis_look_residue *vl, } if(used){ - int ret=_01forward(vb,vl,in,used,pass,partword,_encodepart,passlimit,stats); + int ret=_01forward(vb,vl,in,used,pass,partword,_encodepart,stats); used=0; for(i=0;i<ch;i++) if(nonzero[i]){ @@ -736,8 +722,12 @@ int res1_forward(vorbis_block *vb,vorbis_look_residue *vl, used++; } return(ret); - }else + }else{ + for(i=0;i<vorbis_bitrate_maxmarkers();i++) + stats[i]=oggpack_bits(&vb->opb); + return(0); + } } long **res1_class(vorbis_block *vb,vorbis_look_residue *vl, @@ -781,7 +771,7 @@ long **res2_class(vorbis_block *vb,vorbis_look_residue *vl, int res2_forward(vorbis_block *vb,vorbis_look_residue *vl, float **in,float **out,int *nonzero,int ch, - int pass,long **partword,double passlimit, ogg_uint32_t *stats){ + int pass,long **partword,ogg_uint32_t *stats){ long i,j,k,n=vb->pcmend/2,used=0; /* don't duplicate the code; use a working vector hack for now and @@ -796,7 +786,7 @@ int res2_forward(vorbis_block *vb,vorbis_look_residue *vl, } if(used){ - int ret=_01forward(vb,vl,&work,1,pass,partword,_encodepart,passlimit,stats); + int ret=_01forward(vb,vl,&work,1,pass,partword,_encodepart,stats); /* update the sofar vector */ for(i=0;i<ch;i++){ float *pcm=in[i]; @@ -810,8 +800,12 @@ int res2_forward(vorbis_block *vb,vorbis_look_residue *vl, #endif } return(ret); - }else + }else{ + for(i=0;i<vorbis_bitrate_maxmarkers();i++) + stats[i]=oggpack_bits(&vb->opb); + return(0); + } } /* duplicate code here as speed is somewhat more important */ |