res012.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:9k
源码类别:

Windows CE

开发平台:

C/C++

  1. /********************************************************************
  2.  *                                                                  *
  3.  * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE.   *
  4.  *                                                                  *
  5.  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
  6.  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  7.  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
  8.  *                                                                  *
  9.  * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002    *
  10.  * BY THE Xiph.Org FOUNDATION http://www.xiph.org/                  *
  11.  *                                                                  *
  12.  ********************************************************************
  13.  function: residue backend 0, 1 and 2 implementation
  14.  ********************************************************************/
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "ogg.h"
  18. #include "ivorbiscodec.h"
  19. #include "codec_internal.h"
  20. #include "registry.h"
  21. #include "codebook.h"
  22. #include "misc.h"
  23. #include "os.h"
  24. typedef struct {
  25.   vorbis_info_residue0 *info;
  26.   int         map;
  27.   
  28.   int         parts;
  29.   int         stages;
  30.   codebook   *fullbooks;
  31.   codebook   *phrasebook;
  32.   codebook ***partbooks;
  33.   int         partvals;
  34.   int       **decodemap;
  35. } vorbis_look_residue0;
  36. void res0_free_info(vorbis_info_residue *i){
  37.   vorbis_info_residue0 *info=(vorbis_info_residue0 *)i;
  38.   if(info){
  39.     memset(info,0,sizeof(*info));
  40.     _ogg_free(info);
  41.   }
  42. }
  43. void res0_free_look(vorbis_look_residue *i){
  44.   int j;
  45.   if(i){
  46.     vorbis_look_residue0 *look=(vorbis_look_residue0 *)i;
  47.     for(j=0;j<look->parts;j++)
  48.       if(look->partbooks[j])_ogg_free(look->partbooks[j]);
  49.     _ogg_free(look->partbooks);
  50.     for(j=0;j<look->partvals;j++)
  51.       _ogg_free(look->decodemap[j]);
  52.     _ogg_free(look->decodemap);
  53.     memset(look,0,sizeof(*look));
  54.     _ogg_free(look);
  55.   }
  56. }
  57. static int ilog(unsigned int v){
  58.   int ret=0;
  59.   while(v){
  60.     ret++;
  61.     v>>=1;
  62.   }
  63.   return(ret);
  64. }
  65. static int icount(unsigned int v){
  66.   int ret=0;
  67.   while(v){
  68.     ret+=v&1;
  69.     v>>=1;
  70.   }
  71.   return(ret);
  72. }
  73. /* vorbis_info is for range checking */
  74. vorbis_info_residue *res0_unpack(vorbis_info *vi,oggpack_buffer *opb){
  75.   int j,acc=0;
  76.   vorbis_info_residue0 *info=(vorbis_info_residue0 *)_ogg_calloc(1,sizeof(*info));
  77.   codec_setup_info     *ci=(codec_setup_info *)vi->codec_setup;
  78.   info->begin=oggpack_read(opb,24);
  79.   info->end=oggpack_read(opb,24);
  80.   info->grouping=oggpack_read(opb,24)+1;
  81.   info->partitions=oggpack_read(opb,6)+1;
  82.   info->groupbook=oggpack_read(opb,8);
  83.   for(j=0;j<info->partitions;j++){
  84.     int cascade=oggpack_read(opb,3);
  85.     if(oggpack_read(opb,1))
  86.       cascade|=(oggpack_read(opb,5)<<3);
  87.     info->secondstages[j]=cascade;
  88.     acc+=icount(cascade);
  89.   }
  90.   for(j=0;j<acc;j++)
  91.     info->booklist[j]=oggpack_read(opb,8);
  92.   if(info->groupbook>=ci->books)goto errout;
  93.   for(j=0;j<acc;j++)
  94.     if(info->booklist[j]>=ci->books)goto errout;
  95.   return(info);
  96.  errout:
  97.   res0_free_info(info);
  98.   return(NULL);
  99. }
  100. vorbis_look_residue *res0_look(vorbis_dsp_state *vd,vorbis_info_mode *vm,
  101.   vorbis_info_residue *vr){
  102.   vorbis_info_residue0 *info=(vorbis_info_residue0 *)vr;
  103.   vorbis_look_residue0 *look=(vorbis_look_residue0 *)_ogg_calloc(1,sizeof(*look));
  104.   codec_setup_info     *ci=(codec_setup_info *)vd->vi->codec_setup;
  105.   int j,k,acc=0;
  106.   int dim;
  107.   int maxstage=0;
  108.   look->info=info;
  109.   look->map=vm->mapping;
  110.   look->parts=info->partitions;
  111.   look->fullbooks=ci->fullbooks;
  112.   look->phrasebook=ci->fullbooks+info->groupbook;
  113.   dim=look->phrasebook->dim;
  114.   look->partbooks=(codebook ***)_ogg_calloc(look->parts,sizeof(*look->partbooks));
  115.   for(j=0;j<look->parts;j++){
  116.     int stages=ilog(info->secondstages[j]);
  117.     if(stages){
  118.       if(stages>maxstage)maxstage=stages;
  119.       look->partbooks[j]=(codebook **)_ogg_calloc(stages,sizeof(*look->partbooks[j]));
  120.       for(k=0;k<stages;k++)
  121. if(info->secondstages[j]&(1<<k)){
  122.   look->partbooks[j][k]=ci->fullbooks+info->booklist[acc++];
  123. #ifdef TRAIN_RES
  124.   look->training_data[k][j]=calloc(look->partbooks[j][k]->entries,
  125.    sizeof(***look->training_data));
  126. #endif
  127. }
  128.     }
  129.   }
  130.   look->partvals=look->parts;
  131.   for(j=1;j<dim;j++)look->partvals*=look->parts;
  132.   look->stages=maxstage;
  133.   look->decodemap=(int **)_ogg_malloc(look->partvals*sizeof(*look->decodemap));
  134.   for(j=0;j<look->partvals;j++){
  135.     long val=j;
  136.     long mult=look->partvals/look->parts;
  137.     look->decodemap[j]=(int *)_ogg_malloc(dim*sizeof(*look->decodemap[j]));
  138.     for(k=0;k<dim;k++){
  139.       long deco=val/mult;
  140.       val-=deco*mult;
  141.       mult/=look->parts;
  142.       look->decodemap[j][k]=deco;
  143.     }
  144.   }
  145.   return(look);
  146. }
  147. /* a truncated packet here just means 'stop working'; it's not an error */
  148. static int _01inverse(vorbis_block *vb,vorbis_look_residue *vl,
  149.       ogg_int32_t **in,int ch,
  150.       long (*decodepart)(codebook *, ogg_int32_t *, 
  151.  oggpack_buffer *,int,int)){
  152.   long i,j,k,l,s;
  153.   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  154.   vorbis_info_residue0 *info=look->info;
  155.   /* move all this setup out later */
  156.   int samples_per_partition=info->grouping;
  157.   int partitions_per_word=look->phrasebook->dim;
  158.   int n=info->end-info->begin;
  159.   
  160.   int partvals=n/samples_per_partition;
  161.   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
  162.   int ***partword=(int ***)alloca(ch*sizeof(*partword));
  163.   for(j=0;j<ch;j++)
  164.     partword[j]=(int **)_vorbis_block_alloc(vb,partwords*sizeof(*partword[j]));
  165.   for(s=0;s<look->stages;s++){
  166.     /* each loop decodes on partition codeword containing 
  167.        partitions_pre_word partitions */
  168.     for(i=0,l=0;i<partvals;l++){
  169.       if(s==0){
  170. /* fetch the partition word for each channel */
  171. for(j=0;j<ch;j++){
  172.   int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
  173.   if(temp==-1)goto eopbreak;
  174.   partword[j][l]=look->decodemap[temp];
  175.   if(partword[j][l]==NULL)goto errout;
  176. }
  177.       }
  178.       
  179.       /* now we decode residual values for the partitions */
  180.       for(k=0;k<partitions_per_word && i<partvals;k++,i++)
  181. for(j=0;j<ch;j++){
  182.   long offset=info->begin+i*samples_per_partition;
  183.   if(info->secondstages[partword[j][l][k]]&(1<<s)){
  184.     codebook *stagebook=look->partbooks[partword[j][l][k]][s];
  185.     if(stagebook){
  186.       if(decodepart(stagebook,in[j]+offset,&vb->opb,
  187.     samples_per_partition,-8)==-1)goto eopbreak;
  188.     }
  189.   }
  190. }
  191.     } 
  192.   }
  193.   
  194.  errout:
  195.  eopbreak:
  196.   return(0);
  197. }
  198. int res0_inverse(vorbis_block *vb,vorbis_look_residue *vl,
  199.  ogg_int32_t **in,int *nonzero,int ch){
  200.   int i,used=0;
  201.   for(i=0;i<ch;i++)
  202.     if(nonzero[i])
  203.       in[used++]=in[i];
  204.   if(used)
  205.     return(_01inverse(vb,vl,in,used,vorbis_book_decodevs_add));
  206.   else
  207.     return(0);
  208. }
  209. int res1_inverse(vorbis_block *vb,vorbis_look_residue *vl,
  210.  ogg_int32_t **in,int *nonzero,int ch){
  211.   int i,used=0;
  212.   for(i=0;i<ch;i++)
  213.     if(nonzero[i])
  214.       in[used++]=in[i];
  215.   if(used)
  216.     return(_01inverse(vb,vl,in,used,vorbis_book_decodev_add));
  217.   else
  218.     return(0);
  219. }
  220. /* duplicate code here as speed is somewhat more important */
  221. int res2_inverse(vorbis_block *vb,vorbis_look_residue *vl,
  222.  ogg_int32_t **in,int *nonzero,int ch){
  223.   long i,k,l,s;
  224.   vorbis_look_residue0 *look=(vorbis_look_residue0 *)vl;
  225.   vorbis_info_residue0 *info=look->info;
  226.   /* move all this setup out later */
  227.   int samples_per_partition=info->grouping;
  228.   int partitions_per_word=look->phrasebook->dim;
  229.   int n=info->end-info->begin;
  230.   int partvals=n/samples_per_partition;
  231.   int partwords=(partvals+partitions_per_word-1)/partitions_per_word;
  232.   int **partword=(int **)_vorbis_block_alloc(vb,partwords*sizeof(*partword));
  233.   int beginoff=info->begin/ch;
  234.   for(i=0;i<ch;i++)if(nonzero[i])break;
  235.   if(i==ch)return(0); /* no nonzero vectors */
  236.   samples_per_partition/=ch;
  237.   for(s=0;s<look->stages;s++){
  238.     for(i=0,l=0;i<partvals;l++){
  239.       if(s==0){
  240. /* fetch the partition word */
  241. int temp=vorbis_book_decode(look->phrasebook,&vb->opb);
  242. if(temp==-1)goto eopbreak;
  243. partword[l]=look->decodemap[temp];
  244. if(partword[l]==NULL)goto errout;
  245.       }
  246.       /* now we decode residual values for the partitions */
  247.       for(k=0;k<partitions_per_word && i<partvals;k++,i++)
  248. if(info->secondstages[partword[l][k]]&(1<<s)){
  249.   codebook *stagebook=look->partbooks[partword[l][k]][s];
  250.   
  251.   if(stagebook){
  252.     if(vorbis_book_decodevv_add(stagebook,in,
  253. i*samples_per_partition+beginoff,ch,
  254. &vb->opb,
  255. samples_per_partition,-8)==-1)
  256.       goto eopbreak;
  257.   }
  258. }
  259.     } 
  260.   }
  261.   
  262.  errout:
  263.  eopbreak:
  264.   return(0);
  265. }
  266. const vorbis_func_residue residue0_exportbundle={
  267.   &res0_unpack,
  268.   &res0_look,
  269.   &res0_free_info,
  270.   &res0_free_look,
  271.   &res0_inverse
  272. };
  273. const vorbis_func_residue residue1_exportbundle={
  274.   &res0_unpack,
  275.   &res0_look,
  276.   &res0_free_info,
  277.   &res0_free_look,
  278.   &res1_inverse
  279. };
  280. const vorbis_func_residue residue2_exportbundle={
  281.   &res0_unpack,
  282.   &res0_look,
  283.   &res0_free_info,
  284.   &res0_free_look,
  285.   &res2_inverse
  286. };