Incremental commit toward b3. Optionally should still retrain mode E,
[platform/upstream/libvorbis.git] / lib / analysis.c
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5  * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH    *
6  * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.        *
7  *                                                                  *
8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2000             *
9  * by Monty <monty@xiph.org> and the XIPHOPHORUS Company            *
10  * http://www.xiph.org/                                             *
11  *                                                                  *
12  ********************************************************************
13
14  function: single-block PCM analysis mode dispatch
15  last mod: $Id: analysis.c,v 1.37 2000/11/08 13:16:27 xiphmont Exp $
16
17  ********************************************************************/
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <math.h>
22 #include <ogg/ogg.h>
23 #include "vorbis/codec.h"
24 #include "registry.h"
25 #include "scales.h"
26 #include "os.h"
27
28 /* decides between modes, dispatches to the appropriate mapping. */
29 int vorbis_analysis(vorbis_block *vb,ogg_packet *op){
30   vorbis_dsp_state     *vd=vb->vd;
31   backend_lookup_state *b=vd->backend_state;
32   vorbis_info          *vi=vd->vi;
33   codec_setup_info     *ci=vi->codec_setup;
34   int                   type,ret;
35   int                   mode=0;
36
37   vb->glue_bits=0;
38   vb->time_bits=0;
39   vb->floor_bits=0;
40   vb->res_bits=0;
41
42   /* first things first.  Make sure encode is ready */
43   oggpack_reset(&vb->opb);
44   /* Encode the packet type */
45   oggpack_write(&vb->opb,0,1);
46
47   /* currently lazy.  Short block dispatches to 0, long to 1. */
48
49   if(vb->W &&ci->modes>1)mode=1;
50   type=ci->map_type[ci->mode_param[mode]->mapping];
51   vb->mode=mode;
52
53   /* Encode frame mode, pre,post windowsize, then dispatch */
54   oggpack_write(&vb->opb,mode,b->modebits);
55   if(vb->W){
56     oggpack_write(&vb->opb,vb->lW,1);
57     oggpack_write(&vb->opb,vb->nW,1);
58     /*fprintf(stderr,"*");
59   }else{
60   fprintf(stderr,".");*/
61   }
62
63   if((ret=_mapping_P[type]->forward(vb,b->mode[mode])))
64     return(ret);
65   
66   /* set up the packet wrapper */
67   
68   op->packet=oggpack_get_buffer(&vb->opb);
69   op->bytes=oggpack_bytes(&vb->opb);
70   op->b_o_s=0;
71   op->e_o_s=vb->eofflag;
72   op->granulepos=vb->granulepos;
73   op->packetno=vb->sequence; /* for sake of completeness */
74   
75   return(0);
76 }
77
78 /* there was no great place to put this.... */
79 void _analysis_output(char *base,int i,float *v,int n,int bark,int dB){
80 #ifdef ANALYSIS
81   int j;
82   FILE *of;
83   char buffer[80];
84   sprintf(buffer,"%s_%d.m",base,i);
85   of=fopen(buffer,"w");
86
87   if(!of)perror("failed to open data dump file");
88
89   for(j=0;j<n;j++){
90     if(dB && v[j]==0)
91           fprintf(of,"\n\n");
92     else{
93       if(bark)
94         fprintf(of,"%g ",toBARK(22050.*j/n));
95       else
96         fprintf(of,"%g ",(double)j);
97       
98       if(dB){
99         fprintf(of,"%g\n",todB(fabs(v[j])));
100       }else{
101         fprintf(of,"%g\n",v[j]);
102       }
103     }
104   }
105   fclose(of);
106 #endif
107 }