LCOV - code coverage report
Current view: top level - media/libvorbis/lib - vorbis_res0.c (source / functions) Hit Total Coverage
Test: output.info Lines: 0 363 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 19 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /********************************************************************
       2             :  *                                                                  *
       3             :  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
       4             :  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
       5             :  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
       6             :  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
       7             :  *                                                                  *
       8             :  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010             *
       9             :  * by the Xiph.Org Foundation http://www.xiph.org/                  *
      10             :  *                                                                  *
      11             :  ********************************************************************
      12             : 
      13             :  function: residue backend 0, 1 and 2 implementation
      14             :  last mod: $Id$
      15             : 
      16             :  ********************************************************************/
      17             : 
      18             : /* Slow, slow, slow, simpleminded and did I mention it was slow?  The
      19             :    encode/decode loops are coded for clarity and performance is not
      20             :    yet even a nagging little idea lurking in the shadows.  Oh and BTW,
      21             :    it's slow. */
      22             : 
      23             : #include <stdlib.h>
      24             : #include <string.h>
      25             : #include <math.h>
      26             : #include <ogg/ogg.h>
      27             : #include "vorbis/codec.h"
      28             : #include "codec_internal.h"
      29             : #include "registry.h"
      30             : #include "codebook.h"
      31             : #include "misc.h"
      32             : #include "os.h"
      33             : 
      34             : //#define TRAIN_RES 1
      35             : //#define TRAIN_RESAUX 1
      36             : 
      37             : #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
      38             : #include <stdio.h>
      39             : #endif
      40             : 
      41             : typedef struct {
      42             :   vorbis_info_residue0 *info;
      43             : 
      44             :   int         parts;
      45             :   int         stages;
      46             :   codebook   *fullbooks;
      47             :   codebook   *phrasebook;
      48             :   codebook ***partbooks;
      49             : 
      50             :   int         partvals;
      51             :   int       **decodemap;
      52             : 
      53             :   long      postbits;
      54             :   long      phrasebits;
      55             :   long      frames;
      56             : 
      57             : #if defined(TRAIN_RES) || defined(TRAIN_RESAUX)
      58             :   int        train_seq;
      59             :   long      *training_data[8][64];
      60             :   float      training_max[8][64];
      61             :   float      training_min[8][64];
      62             :   float     tmin;
      63             :   float     tmax;
      64             :   int       submap;
      65             : #endif
      66             : 
      67             : } vorbis_look_residue0;
      68             : 
      69           0 : void res0_free_info(vorbis_info_residue *i){
      70           0 :   vorbis_info_residue0 *info=(vorbis_info_residue0 *)i;
      71           0 :   if(info){
      72           0 :     memset(info,0,sizeof(*info));
      73           0 :     _ogg_free(info);
      74             :   }
      75           0 : }
      76             : 
      77           0 : void res0_free_look(vorbis_look_residue *i){
      78             :   int j;
      79           0 :   if(i){
      80             : 
      81           0 :     vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
      82             : 
      83             : #ifdef TRAIN_RES
      84             :     {
      85             :       int j,k,l;
      86             :       for(j=0;j<look->parts;j++){
      87             :         /*fprintf(stderr,"partition %d: ",j);*/
      88             :         for(k=0;k<8;k++)
      89             :           if(look->training_data[k][j]){
      90             :             char buffer[80];
      91             :             FILE *of;
      92             :             codebook *statebook=look->partbooks[j][k];
      93             : 
      94             :             /* long and short into the same bucket by current convention */
      95             :             sprintf(buffer,"res_sub%d_part%d_pass%d.vqd",look->submap,j,k);
      96             :             of=fopen(buffer,"a");
      97             : 
      98             :             for(l=0;l<statebook->entries;l++)
      99             :               fprintf(of,"%d:%ld\n",l,look->training_data[k][j][l]);
     100             : 
     101             :             fclose(of);
     102             : 
     103             :             /*fprintf(stderr,"%d(%.2f|%.2f) ",k,
     104             :               look->training_min[k][j],look->training_max[k][j]);*/
     105             : 
     106             :             _ogg_free(look->training_data[k][j]);
     107             :             look->training_data[k][j]=NULL;
     108             :           }
     109             :         /*fprintf(stderr,"\n");*/
     110             :       }
     111             :     }
     112             :     fprintf(stderr,"min/max residue: %g::%g\n",look->tmin,look->tmax);
     113             : 
     114             :     /*fprintf(stderr,"residue bit usage %f:%f (%f total)\n",
     115             :             (float)look->phrasebits/look->frames,
     116             :             (float)look->postbits/look->frames,
     117             :             (float)(look->postbits+look->phrasebits)/look->frames);*/
     118             : #endif
     119             : 
     120             : 
     121             :     /*vorbis_info_residue0 *info=look->info;
     122             : 
     123             :     fprintf(stderr,
     124             :             "%ld frames encoded in %ld phrasebits and %ld residue bits "
     125             :             "(%g/frame) \n",look->frames,look->phrasebits,
     126             :             look->resbitsflat,
     127             :             (look->phrasebits+look->resbitsflat)/(float)look->frames);
     128             : 
     129             :     for(j=0;j<look->parts;j++){
     130             :       long acc=0;
     131             :       fprintf(stderr,"\t[%d] == ",j);
     132             :       for(k=0;k<look->stages;k++)
     133             :         if((info->secondstages[j]>>k)&1){
     134             :           fprintf(stderr,"%ld,",look->resbits[j][k]);
     135             :           acc+=look->resbits[j][k];
     136             :         }
     137             : 
     138             :       fprintf(stderr,":: (%ld vals) %1.2fbits/sample\n",look->resvals[j],
     139             :               acc?(float)acc/(look->resvals[j]*info->grouping):0);
     140             :     }
     141             :     fprintf(stderr,"\n");*/
     142             : 
     143           0 :     for(j=0;j<look->parts;j++)
     144           0 :       if(look->partbooks[j])_ogg_free(look->partbooks[j]);
     145           0 :     _ogg_free(look->partbooks);
     146           0 :     for(j=0;j<look->partvals;j++)
     147           0 :       _ogg_free(look->decodemap[j]);
     148           0 :     _ogg_free(look->decodemap);
     149             : 
     150           0 :     memset(look,0,sizeof(*look));
     151           0 :     _ogg_free(look);
     152             :   }
     153           0 : }
     154             : 
     155           0 : static int icount(unsigned int v){
     156           0 :   int ret=0;
     157           0 :   while(v){
     158           0 :     ret+=v&1;
     159           0 :     v>>=1;
     160             :   }
     161           0 :   return(ret);
     162             : }
     163             : 
     164             : 
     165           0 : void res0_pack(vorbis_info_residue *vr,oggpack_buffer *opb){
     166           0 :   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
     167           0 :   int j,acc=0;
     168           0 :   oggpack_write(opb,info->begin,24);
     169           0 :   oggpack_write(opb,info->end,24);
     170             : 
     171           0 :   oggpack_write(opb,info->grouping-1,24);  /* residue vectors to group and
     172             :                                              code with a partitioned book */
     173           0 :   oggpack_write(opb,info->partitions-1,6); /* possible partition choices */
     174           0 :   oggpack_write(opb,info->groupbook,8);  /* group huffman book */
     175             : 
     176             :   /* secondstages is a bitmask; as encoding progresses pass by pass, a
     177             :      bitmask of one indicates this partition class has bits to write
     178             :      this pass */
     179           0 :   for(j=0;j<info->partitions;j++){
     180           0 :     if(ov_ilog(info->secondstages[j])>3){
     181             :       /* yes, this is a minor hack due to not thinking ahead */
     182           0 :       oggpack_write(opb,info->secondstages[j],3);
     183           0 :       oggpack_write(opb,1,1);
     184           0 :       oggpack_write(opb,info->secondstages[j]>>3,5);
     185             :     }else
     186           0 :       oggpack_write(opb,info->secondstages[j],4); /* trailing zero */
     187           0 :     acc+=icount(info->secondstages[j]);
     188             :   }
     189           0 :   for(j=0;j<acc;j++)
     190           0 :     oggpack_write(opb,info->booklist[j],8);
     191             : 
     192           0 : }
     193             : 
     194             : /* vorbis_info is for range checking */
     195           0 : vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
     196           0 :   int j,acc=0;
     197           0 :   vorbis_info_residue0 *info=_ogg_calloc(1,sizeof(*info));
     198           0 :   codec_setup_info     *ci=vi->codec_setup;
     199             : 
     200           0 :   info->begin=oggpack_read(opb,24);
     201           0 :   info->end=oggpack_read(opb,24);
     202           0 :   info->grouping=oggpack_read(opb,24)+1;
     203           0 :   info->partitions=oggpack_read(opb,6)+1;
     204           0 :   info->groupbook=oggpack_read(opb,8);
     205             : 
     206             :   /* check for premature EOP */
     207           0 :   if(info->groupbook<0)goto errout;
     208             : 
     209           0 :   for(j=0;j<info->partitions;j++){
     210           0 :     int cascade=oggpack_read(opb,3);
     211           0 :     int cflag=oggpack_read(opb,1);
     212           0 :     if(cflag<0) goto errout;
     213           0 :     if(cflag){
     214           0 :       int c=oggpack_read(opb,5);
     215           0 :       if(c<0) goto errout;
     216           0 :       cascade|=(c<<3);
     217             :     }
     218           0 :     info->secondstages[j]=cascade;
     219             : 
     220           0 :     acc+=icount(cascade);
     221             :   }
     222           0 :   for(j=0;j<acc;j++){
     223           0 :     int book=oggpack_read(opb,8);
     224           0 :     if(book<0) goto errout;
     225           0 :     info->booklist[j]=book;
     226             :   }
     227             : 
     228           0 :   if(info->groupbook>=ci->books)goto errout;
     229           0 :   for(j=0;j<acc;j++){
     230           0 :     if(info->booklist[j]>=ci->books)goto errout;
     231           0 :     if(ci->book_param[info->booklist[j]]->maptype==0)goto errout;
     232             :   }
     233             : 
     234             :   /* verify the phrasebook is not specifying an impossible or
     235             :      inconsistent partitioning scheme. */
     236             :   /* modify the phrasebook ranging check from r16327; an early beta
     237             :      encoder had a bug where it used an oversized phrasebook by
     238             :      accident.  These files should continue to be playable, but don't
     239             :      allow an exploit */
     240             :   {
     241           0 :     int entries = ci->book_param[info->groupbook]->entries;
     242           0 :     int dim = ci->book_param[info->groupbook]->dim;
     243           0 :     int partvals = 1;
     244           0 :     if (dim<1) goto errout;
     245           0 :     while(dim>0){
     246           0 :       partvals *= info->partitions;
     247           0 :       if(partvals > entries) goto errout;
     248           0 :       dim--;
     249             :     }
     250           0 :     info->partvals = partvals;
     251             :   }
     252             : 
     253           0 :   return(info);
     254             :  errout:
     255           0 :   res0_free_info(info);
     256           0 :   return(NULL);
     257             : }
     258             : 
     259           0 : vorbis_look_residue *res0_look(vorbis_dsp_state *vd,
     260             :                                vorbis_info_residue *vr){
     261           0 :   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
     262           0 :   vorbis_look_residue0 *look=_ogg_calloc(1,sizeof(*look));
     263           0 :   codec_setup_info     *ci=vd->vi->codec_setup;
     264             : 
     265           0 :   int j,k,acc=0;
     266             :   int dim;
     267           0 :   int maxstage=0;
     268           0 :   look->info=info;
     269             : 
     270           0 :   look->parts=info->partitions;
     271           0 :   look->fullbooks=ci->fullbooks;
     272           0 :   look->phrasebook=ci->fullbooks+info->groupbook;
     273           0 :   dim=look->phrasebook->dim;
     274             : 
     275           0 :   look->partbooks=_ogg_calloc(look->parts,sizeof(*look->partbooks));
     276             : 
     277           0 :   for(j=0;j<look->parts;j++){
     278           0 :     int stages=ov_ilog(info->secondstages[j]);
     279           0 :     if(stages){
     280           0 :       if(stages>maxstage)maxstage=stages;
     281           0 :       look->partbooks[j]=_ogg_calloc(stages,sizeof(*look->partbooks[j]));
     282           0 :       for(k=0;k<stages;k++)
     283           0 :         if(info->secondstages[j]&(1<<k)){
     284           0 :           look->partbooks[j][k]=ci->fullbooks+info->booklist[acc++];
     285             : #ifdef TRAIN_RES
     286             :           look->training_data[k][j]=_ogg_calloc(look->partbooks[j][k]->entries,
     287             :                                            sizeof(***look->training_data));
     288             : #endif
     289             :         }
     290             :     }
     291             :   }
     292             : 
     293           0 :   look->partvals=1;
     294           0 :   for(j=0;j<dim;j++)
     295           0 :       look->partvals*=look->parts;
     296             : 
     297           0 :   look->stages=maxstage;
     298           0 :   look->decodemap=_ogg_malloc(look->partvals*sizeof(*look->decodemap));
     299           0 :   for(j=0;j<look->partvals;j++){
     300           0 :     long val=j;
     301           0 :     long mult=look->partvals/look->parts;
     302           0 :     look->decodemap[j]=_ogg_malloc(dim*sizeof(*look->decodemap[j]));
     303           0 :     for(k=0;k<dim;k++){
     304           0 :       long deco=val/mult;
     305           0 :       val-=deco*mult;
     306           0 :       mult/=look->parts;
     307           0 :       look->decodemap[j][k]=deco;
     308             :     }
     309             :   }
     310             : #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
     311             :   {
     312             :     static int train_seq=0;
     313             :     look->train_seq=train_seq++;
     314             :   }
     315             : #endif
     316           0 :   return(look);
     317             : }
     318             : 
     319             : /* break an abstraction and copy some code for performance purposes */
     320           0 : static int local_book_besterror(codebook *book,int *a){
     321           0 :   int dim=book->dim;
     322             :   int i,j,o;
     323           0 :   int minval=book->minval;
     324           0 :   int del=book->delta;
     325           0 :   int qv=book->quantvals;
     326           0 :   int ze=(qv>>1);
     327           0 :   int index=0;
     328             :   /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */
     329           0 :   int p[8]={0,0,0,0,0,0,0,0};
     330             : 
     331           0 :   if(del!=1){
     332           0 :     for(i=0,o=dim;i<dim;i++){
     333           0 :       int v = (a[--o]-minval+(del>>1))/del;
     334           0 :       int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1));
     335           0 :       index = index*qv+ (m<0?0:(m>=qv?qv-1:m));
     336           0 :       p[o]=v*del+minval;
     337             :     }
     338             :   }else{
     339           0 :     for(i=0,o=dim;i<dim;i++){
     340           0 :       int v = a[--o]-minval;
     341           0 :       int m = (v<ze ? ((ze-v)<<1)-1 : ((v-ze)<<1));
     342           0 :       index = index*qv+ (m<0?0:(m>=qv?qv-1:m));
     343           0 :       p[o]=v*del+minval;
     344             :     }
     345             :   }
     346             : 
     347           0 :   if(book->c->lengthlist[index]<=0){
     348           0 :     const static_codebook *c=book->c;
     349           0 :     int best=-1;
     350             :     /* assumes integer/centered encoder codebook maptype 1 no more than dim 8 */
     351           0 :     int e[8]={0,0,0,0,0,0,0,0};
     352           0 :     int maxval = book->minval + book->delta*(book->quantvals-1);
     353           0 :     for(i=0;i<book->entries;i++){
     354           0 :       if(c->lengthlist[i]>0){
     355           0 :         int this=0;
     356           0 :         for(j=0;j<dim;j++){
     357           0 :           int val=(e[j]-a[j]);
     358           0 :           this+=val*val;
     359             :         }
     360           0 :         if(best==-1 || this<best){
     361           0 :           memcpy(p,e,sizeof(p));
     362           0 :           best=this;
     363           0 :           index=i;
     364             :         }
     365             :       }
     366             :       /* assumes the value patterning created by the tools in vq/ */
     367           0 :       j=0;
     368           0 :       while(e[j]>=maxval)
     369           0 :         e[j++]=0;
     370           0 :       if(e[j]>=0)
     371           0 :         e[j]+=book->delta;
     372           0 :       e[j]= -e[j];
     373             :     }
     374             :   }
     375             : 
     376           0 :   if(index>-1){
     377           0 :     for(i=0;i<dim;i++)
     378           0 :       *a++ -= p[i];
     379             :   }
     380             : 
     381           0 :   return(index);
     382             : }
     383             : 
     384             : #ifdef TRAIN_RES
     385             : static int _encodepart(oggpack_buffer *opb,int *vec, int n,
     386             :                        codebook *book,long *acc){
     387             : #else
     388           0 : static int _encodepart(oggpack_buffer *opb,int *vec, int n,
     389             :                        codebook *book){
     390             : #endif
     391           0 :   int i,bits=0;
     392           0 :   int dim=book->dim;
     393           0 :   int step=n/dim;
     394             : 
     395           0 :   for(i=0;i<step;i++){
     396           0 :     int entry=local_book_besterror(book,vec+i*dim);
     397             : 
     398             : #ifdef TRAIN_RES
     399             :     if(entry>=0)
     400             :       acc[entry]++;
     401             : #endif
     402             : 
     403           0 :     bits+=vorbis_book_encode(book,entry,opb);
     404             : 
     405             :   }
     406             : 
     407           0 :   return(bits);
     408             : }
     409             : 
     410           0 : static long **_01class(vorbis_block *vb,vorbis_look_residue *vl,
     411             :                        int **in,int ch){
     412             :   long i,j,k;
     413           0 :   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
     414           0 :   vorbis_info_residue0 *info=look->info;
     415             : 
     416             :   /* move all this setup out later */
     417           0 :   int samples_per_partition=info->grouping;
     418           0 :   int possible_partitions=info->partitions;
     419           0 :   int n=info->end-info->begin;
     420             : 
     421           0 :   int partvals=n/samples_per_partition;
     422           0 :   long **partword=_vorbis_block_alloc(vb,ch*sizeof(*partword));
     423           0 :   float scale=100./samples_per_partition;
     424             : 
     425             :   /* we find the partition type for each partition of each
     426             :      channel.  We'll go back and do the interleaved encoding in a
     427             :      bit.  For now, clarity */
     428             : 
     429           0 :   for(i=0;i<ch;i++){
     430           0 :     partword[i]=_vorbis_block_alloc(vb,n/samples_per_partition*sizeof(*partword[i]));
     431           0 :     memset(partword[i],0,n/samples_per_partition*sizeof(*partword[i]));
     432             :   }
     433             : 
     434           0 :   for(i=0;i<partvals;i++){
     435           0 :     int offset=i*samples_per_partition+info->begin;
     436           0 :     for(j=0;j<ch;j++){
     437           0 :       int max=0;
     438           0 :       int ent=0;
     439           0 :       for(k=0;k<samples_per_partition;k++){
     440           0 :         if(abs(in[j][offset+k])>max)max=abs(in[j][offset+k]);
     441           0 :         ent+=abs(in[j][offset+k]);
     442             :       }
     443           0 :       ent*=scale;
     444             : 
     445           0 :       for(k=0;k<possible_partitions-1;k++)
     446           0 :         if(max<=info->classmetric1[k] &&
     447           0 :            (info->classmetric2[k]<0 || ent<info->classmetric2[k]))
     448             :           break;
     449             : 
     450           0 :       partword[j][i]=k;
     451             :     }
     452             :   }
     453             : 
     454             : #ifdef TRAIN_RESAUX
     455             :   {
     456             :     FILE *of;
     457             :     char buffer[80];
     458             : 
     459             :     for(i=0;i<ch;i++){
     460             :       sprintf(buffer,"resaux_%d.vqd",look->train_seq);
     461             :       of=fopen(buffer,"a");
     462             :       for(j=0;j<partvals;j++)
     463             :         fprintf(of,"%ld, ",partword[i][j]);
     464             :       fprintf(of,"\n");
     465             :       fclose(of);
     466             :     }
     467             :   }
     468             : #endif
     469           0 :   look->frames++;
     470             : 
     471           0 :   return(partword);
     472             : }
     473             : 
     474             : /* designed for stereo or other modes where the partition size is an
     475             :    integer multiple of the number of channels encoded in the current
     476             :    submap */
     477           0 : static long **_2class(vorbis_block *vb,vorbis_look_residue *vl,int **in,
     478             :                       int ch){
     479             :   long i,j,k,l;
     480           0 :   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
     481           0 :   vorbis_info_residue0 *info=look->info;
     482             : 
     483             :   /* move all this setup out later */
     484           0 :   int samples_per_partition=info->grouping;
     485           0 :   int possible_partitions=info->partitions;
     486           0 :   int n=info->end-info->begin;
     487             : 
     488           0 :   int partvals=n/samples_per_partition;
     489           0 :   long **partword=_vorbis_block_alloc(vb,sizeof(*partword));
     490             : 
     491             : #if defined(TRAIN_RES) || defined (TRAIN_RESAUX)
     492             :   FILE *of;
     493             :   char buffer[80];
     494             : #endif
     495             : 
     496           0 :   partword[0]=_vorbis_block_alloc(vb,partvals*sizeof(*partword[0]));
     497           0 :   memset(partword[0],0,partvals*sizeof(*partword[0]));
     498             : 
     499           0 :   for(i=0,l=info->begin/ch;i<partvals;i++){
     500           0 :     int magmax=0;
     501           0 :     int angmax=0;
     502           0 :     for(j=0;j<samples_per_partition;j+=ch){
     503           0 :       if(abs(in[0][l])>magmax)magmax=abs(in[0][l]);
     504           0 :       for(k=1;k<ch;k++)
     505           0 :         if(abs(in[k][l])>angmax)angmax=abs(in[k][l]);
     506           0 :       l++;
     507             :     }
     508             : 
     509           0 :     for(j=0;j<possible_partitions-1;j++)
     510           0 :       if(magmax<=info->classmetric1[j] &&
     511           0 :          angmax<=info->classmetric2[j])
     512           0 :         break;
     513             : 
     514           0 :     partword[0][i]=j;
     515             : 
     516             :   }
     517             : 
     518             : #ifdef TRAIN_RESAUX
     519             :   sprintf(buffer,"resaux_%d.vqd",look->train_seq);
     520             :   of=fopen(buffer,"a");
     521             :   for(i=0;i<partvals;i++)
     522             :     fprintf(of,"%ld, ",partword[0][i]);
     523             :   fprintf(of,"\n");
     524             :   fclose(of);
     525             : #endif
     526             : 
     527           0 :   look->frames++;
     528             : 
     529           0 :   return(partword);
     530             : }
     531             : 
     532           0 : static int _01forward(oggpack_buffer *opb,
     533             :                       vorbis_look_residue *vl,
     534             :                       int **in,int ch,
     535             :                       long **partword,
     536             : #ifdef TRAIN_RES
     537             :                       int (*encode)(oggpack_buffer *,int *,int,
     538             :                                     codebook *,long *),
     539             :                       int submap
     540             : #else
     541             :                       int (*encode)(oggpack_buffer *,int *,int,
     542             :                                     codebook *)
     543             : #endif
     544             : ){
     545             :   long i,j,k,s;
     546           0 :   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
     547           0 :   vorbis_info_residue0 *info=look->info;
     548             : 
     549             : #ifdef TRAIN_RES
     550             :   look->submap=submap;
     551             : #endif
     552             : 
     553             :   /* move all this setup out later */
     554           0 :   int samples_per_partition=info->grouping;
     555           0 :   int possible_partitions=info->partitions;
     556           0 :   int partitions_per_word=look->phrasebook->dim;
     557           0 :   int n=info->end-info->begin;
     558             : 
     559           0 :   int partvals=n/samples_per_partition;
     560             :   long resbits[128];
     561             :   long resvals[128];
     562             : 
     563             : #ifdef TRAIN_RES
     564             :   for(i=0;i<ch;i++)
     565             :     for(j=info->begin;j<info->end;j++){
     566             :       if(in[i][j]>look->tmax)look->tmax=in[i][j];
     567             :       if(in[i][j]<look->tmin)look->tmin=in[i][j];
     568             :     }
     569             : #endif
     570             : 
     571           0 :   memset(resbits,0,sizeof(resbits));
     572           0 :   memset(resvals,0,sizeof(resvals));
     573             : 
     574             :   /* we code the partition words for each channel, then the residual
     575             :      words for a partition per channel until we've written all the
     576             :      residual words for that partition word.  Then write the next
     577             :      partition channel words... */
     578             : 
     579           0 :   for(s=0;s<look->stages;s++){
     580             : 
     581           0 :     for(i=0;i<partvals;){
     582             : 
     583             :       /* first we encode a partition codeword for each channel */
     584           0 :       if(s==0){
     585           0 :         for(j=0;j<ch;j++){
     586           0 :           long val=partword[j][i];
     587           0 :           for(k=1;k<partitions_per_word;k++){
     588           0 :             val*=possible_partitions;
     589           0 :             if(i+k<partvals)
     590           0 :               val+=partword[j][i+k];
     591             :           }
     592             : 
     593             :           /* training hack */
     594           0 :           if(val<look->phrasebook->entries)
     595           0 :             look->phrasebits+=vorbis_book_encode(look->phrasebook,val,opb);
     596             : #if 0 /*def TRAIN_RES*/
     597             :           else
     598             :             fprintf(stderr,"!");
     599             : #endif
     600             : 
     601             :         }
     602             :       }
     603             : 
     604             :       /* now we encode interleaved residual values for the partitions */
     605           0 :       for(k=0;k<partitions_per_word && i<partvals;k++,i++){
     606           0 :         long offset=i*samples_per_partition+info->begin;
     607             : 
     608           0 :         for(j=0;j<ch;j++){
     609           0 :           if(s==0)resvals[partword[j][i]]+=samples_per_partition;
     610           0 :           if(info->secondstages[partword[j][i]]&(1<<s)){
     611           0 :             codebook *statebook=look->partbooks[partword[j][i]][s];
     612           0 :             if(statebook){
     613             :               int ret;
     614             : #ifdef TRAIN_RES
     615             :               long *accumulator=NULL;
     616             :               accumulator=look->training_data[s][partword[j][i]];
     617             :               {
     618             :                 int l;
     619             :                 int *samples=in[j]+offset;
     620             :                 for(l=0;l<samples_per_partition;l++){
     621             :                   if(samples[l]<look->training_min[s][partword[j][i]])
     622             :                     look->training_min[s][partword[j][i]]=samples[l];
     623             :                   if(samples[l]>look->training_max[s][partword[j][i]])
     624             :                     look->training_max[s][partword[j][i]]=samples[l];
     625             :                 }
     626             :               }
     627             :               ret=encode(opb,in[j]+offset,samples_per_partition,
     628             :                          statebook,accumulator);
     629             : #else
     630           0 :               ret=encode(opb,in[j]+offset,samples_per_partition,
     631             :                          statebook);
     632             : #endif
     633             : 
     634           0 :               look->postbits+=ret;
     635           0 :               resbits[partword[j][i]]+=ret;
     636             :             }
     637             :           }
     638             :         }
     639             :       }
     640             :     }
     641             :   }
     642             : 
     643           0 :   return(0);
     644             : }
     645             : 
     646             : /* a truncated packet here just means 'stop working'; it's not an error */
     647           0 : static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
     648             :                       float **in,int ch,
     649             :                       long (*decodepart)(codebook *, float *,
     650             :                                          oggpack_buffer *,int)){
     651             : 
     652             :   long i,j,k,l,s;
     653           0 :   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
     654           0 :   vorbis_info_residue0 *info=look->info;
     655             : 
     656             :   /* move all this setup out later */
     657           0 :   int samples_per_partition=info->grouping;
     658           0 :   int partitions_per_word=look->phrasebook->dim;
     659           0 :   int max=vb->pcmend>>1;
     660           0 :   int end=(info->end<max?info->end:max);
     661           0 :   int n=end-info->begin;
     662             : 
     663           0 :   if(n>0){
     664           0 :     int partvals=n/samples_per_partition;
     665           0 :     int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
     666           0 :     int ***partword=alloca(ch*sizeof(*partword));
     667             : 
     668           0 :     for(j=0;j<ch;j++)
     669           0 :       partword[j]=_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
     670             : 
     671           0 :     for(s=0;s<look->stages;s++){
     672             : 
     673             :       /* each loop decodes on partition codeword containing
     674             :          partitions_per_word partitions */
     675           0 :       for(i=0,l=0;i<partvals;l++){
     676           0 :         if(s==0){
     677             :           /* fetch the partition word for each channel */
     678           0 :           for(j=0;j<ch;j++){
     679           0 :             int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
     680             : 
     681           0 :             if(temp==-1 || temp>=info->partvals)goto eopbreak;
     682           0 :             partword[j][l]=look->decodemap[temp];
     683           0 :             if(partword[j][l]==NULL)goto errout;
     684             :           }
     685             :         }
     686             : 
     687             :         /* now we decode residual values for the partitions */
     688           0 :         for(k=0;k<partitions_per_word && i<partvals;k++,i++)
     689           0 :           for(j=0;j<ch;j++){
     690           0 :             long offset=info->begin+i*samples_per_partition;
     691           0 :             if(info->secondstages[partword[j][l][k]]&(1<<s)){
     692           0 :               codebook *stagebook=look->partbooks[partword[j][l][k]][s];
     693           0 :               if(stagebook){
     694           0 :                 if(decodepart(stagebook,in[j]+offset,&vb->opb,
     695           0 :                               samples_per_partition)==-1)goto eopbreak;
     696             :               }
     697             :             }
     698             :           }
     699             :       }
     700             :     }
     701             :   }
     702             :  errout:
     703             :  eopbreak:
     704           0 :   return(0);
     705             : }
     706             : 
     707           0 : int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
     708             :                  float **in,int *nonzero,int ch){
     709           0 :   int i,used=0;
     710           0 :   for(i=0;i<ch;i++)
     711           0 :     if(nonzero[i])
     712           0 :       in[used++]=in[i];
     713           0 :   if(used)
     714           0 :     return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
     715             :   else
     716           0 :     return(0);
     717             : }
     718             : 
     719           0 : int res1_forward(oggpack_buffer *opb,vorbis_block *vb,vorbis_look_residue *vl,
     720             :                  int **in,int *nonzero,int ch, long **partword, int submap){
     721           0 :   int i,used=0;
     722             :   (void)vb;
     723           0 :   for(i=0;i<ch;i++)
     724           0 :     if(nonzero[i])
     725           0 :       in[used++]=in[i];
     726             : 
     727           0 :   if(used){
     728             : #ifdef TRAIN_RES
     729             :     return _01forward(opb,vl,in,used,partword,_encodepart,submap);
     730             : #else
     731             :     (void)submap;
     732           0 :     return _01forward(opb,vl,in,used,partword,_encodepart);
     733             : #endif
     734             :   }else{
     735           0 :     return(0);
     736             :   }
     737             : }
     738             : 
     739           0 : long **res1_class(vorbis_block *vb,vorbis_look_residue *vl,
     740             :                   int **in,int *nonzero,int ch){
     741           0 :   int i,used=0;
     742           0 :   for(i=0;i<ch;i++)
     743           0 :     if(nonzero[i])
     744           0 :       in[used++]=in[i];
     745           0 :   if(used)
     746           0 :     return(_01class(vb,vl,in,used));
     747             :   else
     748           0 :     return(0);
     749             : }
     750             : 
     751           0 : int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
     752             :                  float **in,int *nonzero,int ch){
     753           0 :   int i,used=0;
     754           0 :   for(i=0;i<ch;i++)
     755           0 :     if(nonzero[i])
     756           0 :       in[used++]=in[i];
     757           0 :   if(used)
     758           0 :     return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
     759             :   else
     760           0 :     return(0);
     761             : }
     762             : 
     763           0 : long **res2_class(vorbis_block *vb,vorbis_look_residue *vl,
     764             :                   int **in,int *nonzero,int ch){
     765           0 :   int i,used=0;
     766           0 :   for(i=0;i<ch;i++)
     767           0 :     if(nonzero[i])used++;
     768           0 :   if(used)
     769           0 :     return(_2class(vb,vl,in,ch));
     770             :   else
     771           0 :     return(0);
     772             : }
     773             : 
     774             : /* res2 is slightly more different; all the channels are interleaved
     775             :    into a single vector and encoded. */
     776             : 
     777           0 : int res2_forward(oggpack_buffer *opb,
     778             :                  vorbis_block *vb,vorbis_look_residue *vl,
     779             :                  int **in,int *nonzero,int ch, long **partword,int submap){
     780           0 :   long i,j,k,n=vb->pcmend/2,used=0;
     781             : 
     782             :   /* don't duplicate the code; use a working vector hack for now and
     783             :      reshape ourselves into a single channel res1 */
     784             :   /* ugly; reallocs for each coupling pass :-( */
     785           0 :   int *work=_vorbis_block_alloc(vb,ch*n*sizeof(*work));
     786           0 :   for(i=0;i<ch;i++){
     787           0 :     int *pcm=in[i];
     788           0 :     if(nonzero[i])used++;
     789           0 :     for(j=0,k=i;j<n;j++,k+=ch)
     790           0 :       work[k]=pcm[j];
     791             :   }
     792             : 
     793           0 :   if(used){
     794             : #ifdef TRAIN_RES
     795             :     return _01forward(opb,vl,&work,1,partword,_encodepart,submap);
     796             : #else
     797             :     (void)submap;
     798           0 :     return _01forward(opb,vl,&work,1,partword,_encodepart);
     799             : #endif
     800             :   }else{
     801           0 :     return(0);
     802             :   }
     803             : }
     804             : 
     805             : /* duplicate code here as speed is somewhat more important */
     806           0 : int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
     807             :                  float **in,int *nonzero,int ch){
     808             :   long i,k,l,s;
     809           0 :   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
     810           0 :   vorbis_info_residue0 *info=look->info;
     811             : 
     812             :   /* move all this setup out later */
     813           0 :   int samples_per_partition=info->grouping;
     814           0 :   int partitions_per_word=look->phrasebook->dim;
     815           0 :   int max=(vb->pcmend*ch)>>1;
     816           0 :   int end=(info->end<max?info->end:max);
     817           0 :   int n=end-info->begin;
     818             : 
     819           0 :   if(n>0){
     820           0 :     int partvals=n/samples_per_partition;
     821           0 :     int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
     822           0 :     int **partword=_vorbis_block_alloc(vb,partwords*sizeof(*partword));
     823             : 
     824           0 :     for(i=0;i<ch;i++)if(nonzero[i])break;
     825           0 :     if(i==ch)return(0); /* no nonzero vectors */
     826             : 
     827           0 :     for(s=0;s<look->stages;s++){
     828           0 :       for(i=0,l=0;i<partvals;l++){
     829             : 
     830           0 :         if(s==0){
     831             :           /* fetch the partition word */
     832           0 :           int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
     833           0 :           if(temp==-1 || temp>=info->partvals)goto eopbreak;
     834           0 :           partword[l]=look->decodemap[temp];
     835           0 :           if(partword[l]==NULL)goto errout;
     836             :         }
     837             : 
     838             :         /* now we decode residual values for the partitions */
     839           0 :         for(k=0;k<partitions_per_word && i<partvals;k++,i++)
     840           0 :           if(info->secondstages[partword[l][k]]&(1<<s)){
     841           0 :             codebook *stagebook=look->partbooks[partword[l][k]][s];
     842             : 
     843           0 :             if(stagebook){
     844           0 :               if(vorbis_book_decodevv_add(stagebook,in,
     845           0 :                                           i*samples_per_partition+info->begin,ch,
     846             :                                           &vb->opb,samples_per_partition)==-1)
     847           0 :                 goto eopbreak;
     848             :             }
     849             :           }
     850             :       }
     851             :     }
     852             :   }
     853             :  errout:
     854             :  eopbreak:
     855           0 :   return(0);
     856             : }
     857             : 
     858             : 
     859             : const vorbis_func_residue residue0_exportbundle={
     860             :   NULL,
     861             :   &res0_unpack,
     862             :   &res0_look,
     863             :   &res0_free_info,
     864             :   &res0_free_look,
     865             :   NULL,
     866             :   NULL,
     867             :   &res0_inverse
     868             : };
     869             : 
     870             : const vorbis_func_residue residue1_exportbundle={
     871             :   &res0_pack,
     872             :   &res0_unpack,
     873             :   &res0_look,
     874             :   &res0_free_info,
     875             :   &res0_free_look,
     876             :   &res1_class,
     877             :   &res1_forward,
     878             :   &res1_inverse
     879             : };
     880             : 
     881             : const vorbis_func_residue residue2_exportbundle={
     882             :   &res0_pack,
     883             :   &res0_unpack,
     884             :   &res0_look,
     885             :   &res0_free_info,
     886             :   &res0_free_look,
     887             :   &res2_class,
     888             :   &res2_forward,
     889             :   &res2_inverse
     890             : };

Generated by: LCOV version 1.13