Repaired 'I must have been boozing' memory management in vorbisfile.a
[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.25 2000/03/10 13:21:18 xiphmont Exp $
16
17  ********************************************************************/
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <math.h>
22 #include "vorbis/codec.h"
23 #include "bitwise.h"
24 #include "registry.h"
25 #include "misc.h"
26
27 /* decides between modes, dispatches to the appropriate mapping. */
28 int vorbis_analysis(vorbis_block *vb,ogg_packet *op){
29   vorbis_dsp_state *vd=vb->vd;
30   vorbis_info      *vi=vd->vi;
31   int              type;
32   int              mode=0;
33
34   vb->glue_bits=0;
35   vb->time_bits=0;
36   vb->floor_bits=0;
37   vb->res_bits=0;
38
39   /* first things first.  Make sure encode is ready */
40   _oggpack_reset(&vb->opb);
41   /* Encode the packet type */
42   _oggpack_write(&vb->opb,0,1);
43
44   /* currently lazy.  Short block dispatches to 0, long to 1. */
45
46   if(vb->W &&vi->modes>1)mode=1;
47   type=vi->map_type[vi->mode_param[mode]->mapping];
48   vb->mode=mode;
49
50   /* Encode frame mode, pre,post windowsize, then dispatch */
51   _oggpack_write(&vb->opb,mode,vd->modebits);
52   if(vb->W){
53     _oggpack_write(&vb->opb,vb->lW,1);
54     _oggpack_write(&vb->opb,vb->nW,1);
55   }
56
57   if(_mapping_P[type]->forward(vb,vd->mode[mode]))
58     return(-1);
59
60   /* set up the packet wrapper */
61
62   op->packet=_oggpack_buffer(&vb->opb);
63   op->bytes=_oggpack_bytes(&vb->opb);
64   op->b_o_s=0;
65   op->e_o_s=vb->eofflag;
66   op->frameno=vb->frameno;
67   op->packetno=vb->sequence; /* for sake of completeness */
68
69   return(0);
70 }
71
72 /* there was no great place to put this.... */
73 void _analysis_output(char *base,int i,double *v,int n){
74 #ifdef ANALYSIS
75   int j;
76   FILE *of;
77   char buffer[80];
78   sprintf(buffer,"%s_%d.m",base,i);
79   of=fopen(buffer,"w");
80   for(j=0;j<n;j++)
81     fprintf(of,"%g\n",v[j]);
82   fclose(of);
83 #endif
84 }
85
86