2cc2be806a1999fd39575f8f4a9ef8da27d64d3c
[platform/upstream/libvorbis.git] / lib / analysis.c
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE.  *
4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5  * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE.    *
6  * PLEASE READ THESE TERMS DISTRIBUTING.                            *
7  *                                                                  *
8  * THE OggSQUISH 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.34 2000/10/12 03:12:52 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   vorbis_info      *vi=vd->vi;
32   int              type;
33   int              mode=0;
34
35   vb->glue_bits=0;
36   vb->time_bits=0;
37   vb->floor_bits=0;
38   vb->res_bits=0;
39
40   /* first things first.  Make sure encode is ready */
41   oggpack_reset(&vb->opb);
42   /* Encode the packet type */
43   oggpack_write(&vb->opb,0,1);
44
45   /* currently lazy.  Short block dispatches to 0, long to 1. */
46
47   if(vb->W &&vi->modes>1)mode=1;
48   type=vi->map_type[vi->mode_param[mode]->mapping];
49   vb->mode=mode;
50
51   /* Encode frame mode, pre,post windowsize, then dispatch */
52   oggpack_write(&vb->opb,mode,vd->modebits);
53   if(vb->W){
54     oggpack_write(&vb->opb,vb->lW,1);
55     oggpack_write(&vb->opb,vb->nW,1);
56     /*fprintf(stderr,"*");*/
57   }/*else{
58     fprintf(stderr,".");
59     }*/
60
61   if(_mapping_P[type]->forward(vb,vd->mode[mode]))
62     return(-1);
63
64   /* set up the packet wrapper */
65
66   op->packet=oggpack_get_buffer(&vb->opb);
67   op->bytes=oggpack_bytes(&vb->opb);
68   op->b_o_s=0;
69   op->e_o_s=vb->eofflag;
70   op->granulepos=vb->granulepos;
71   op->packetno=vb->sequence; /* for sake of completeness */
72
73   return(0);
74 }
75
76 /* there was no great place to put this.... */
77 void _analysis_output(char *base,int i,float *v,int n,int bark,int dB){
78 #ifdef ANALYSIS
79   int j;
80   FILE *of;
81   char buffer[80];
82   sprintf(buffer,"%s_%d.m",base,i);
83   of=fopen(buffer,"w");
84
85   if(!of)perror("failed to open data dump file");
86
87   for(j=0;j<n;j++){
88     if(dB && v[j]==0)
89           fprintf(of,"\n\n");
90     else{
91       if(bark)
92         fprintf(of,"%g ",toBARK(22050.*j/n));
93       else
94         fprintf(of,"%g ",(double)j);
95       
96       if(dB){
97         fprintf(of,"%g\n",todB(fabs(v[j])));
98       }else{
99         fprintf(of,"%g\n",v[j]);
100       }
101     }
102   }
103   fclose(of);
104 #endif
105 }