decoder_example.c 9.02 KB
Newer Older
Monty's avatar
   
Monty committed
1
2
3
4
5
6
7
/********************************************************************
 *                                                                  *
 * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE.  *
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
 * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE.    *
 * PLEASE READ THESE TERMS DISTRIBUTING.                            *
 *                                                                  *
Monty's avatar
   
Monty committed
8
9
 * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000             *
 * by Monty <monty@xiph.org> and The XIPHOPHORUS Company            *
Monty's avatar
   
Monty committed
10
11
12
13
14
 * http://www.xiph.org/                                             *
 *                                                                  *
 ********************************************************************

 function: simple example decoder
Monty's avatar
   
Monty committed
15
 last mod: $Id: decoder_example.c,v 1.5 2000/01/28 15:25:06 xiphmont Exp $
Monty's avatar
   
Monty committed
16
17
18
19

 ********************************************************************/

/* Takes a vorbis bitstream from stdin and writes raw stereo PCM to
Monty's avatar
   
Monty committed
20
21
22
23
   stdout.  Decodes simple and chained OggVorbis files from beginning
   to end.  Vorbisfile.a is somewhat more complex than the code below.  */

/* Note that this is POSIX, not ANSI code */
Monty's avatar
   
Monty committed
24
25
26

#include <stdio.h>
#include <math.h>
Monty's avatar
   
Monty committed
27
#include "vorbis/codec.h"
Monty's avatar
   
Monty committed
28
29
30
31
32
33
34
35
36
37
38
39
40

int16_t convbuffer[4096]; /* take 8k out of the data segment, not the stack */
int convsize=4096;

int main(){
  ogg_sync_state   oy; /* sync and verify incoming physical bitstream */
  ogg_stream_state os; /* take physical pages, weld into a logical
			  stream of packets */
  ogg_page         og; /* one Ogg bitstream page.  Vorbis packets are inside */
  ogg_packet       op; /* one raw packet of data for decode */
  
  vorbis_info      vi; /* struct that stores all the static vorbis bitstream
			  settings */
Monty's avatar
   
Monty committed
41
  vorbis_comment   vc; /* struct that stores all the bitstream user comments */
Monty's avatar
   
Monty committed
42
43
44
45
46
47
48
49
50
51
  vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
  vorbis_block     vb; /* local working space for packet->PCM decode */
  
  char *buffer;
  int  bytes;

  /********** Decode setup ************/

  ogg_sync_init(&oy); /* Now we can read pages */
  
Monty's avatar
   
Monty committed
52
53
54
  while(1){ /* we repeat if the bitstream is chained */
    int eos=0;
    int i;
Monty's avatar
   
Monty committed
55

Monty's avatar
   
Monty committed
56
57
58
59
    /* grab some data at the head of the stream.  We want the first page
       (which is guaranteed to be small and only contain the Vorbis
       stream initial header) We need the first page to get the stream
       serialno. */
Monty's avatar
   
Monty committed
60

Monty's avatar
   
Monty committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    /* submit a 4k block to libvorbis' Ogg layer */
    buffer=ogg_sync_buffer(&oy,4096);
    bytes=fread(buffer,1,4096,stdin);
    ogg_sync_wrote(&oy,bytes);
    
    /* Get the first page. */
    if(ogg_sync_pageout(&oy,&og)!=1){
      /* have we simply run out of data?  If so, we're done. */
      if(bytes<4096)break;
      
      /* error case.  Must not be Vorbis data */
      fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
      exit(1);
    }
Monty's avatar
   
Monty committed
75
  
Monty's avatar
   
Monty committed
76
77
78
79
80
81
82
83
84
85
86
87
88
    /* Get the serial number and set up the rest of decode. */
    /* serialno first; use it to set up a logical stream */
    ogg_stream_init(&os,ogg_page_serialno(&og));
    
    /* extract the initial header from the first page and verify that the
       Ogg bitstream is in fact Vorbis data */
    
    /* I handle the initial header first instead of just having the code
       read all three Vorbis headers at once because reading the initial
       header is an easy way to identify a Vorbis bitstream and it's
       useful to see that functionality seperated out. */
    
    vorbis_info_init(&vi);
Monty's avatar
   
Monty committed
89
    vorbis_comment_init(&vc);
Monty's avatar
   
Monty committed
90
91
92
93
94
95
96
97
98
99
100
101
    if(ogg_stream_pagein(&os,&og)<0){ 
      /* error; stream version mismatch perhaps */
      fprintf(stderr,"Error reading first page of Ogg bitstream data.\n");
      exit(1);
    }
    
    if(ogg_stream_packetout(&os,&op)!=1){ 
      /* no page? must not be vorbis */
      fprintf(stderr,"Error reading initial header packet.\n");
      exit(1);
    }
    
Monty's avatar
   
Monty committed
102
    if(vorbis_synthesis_headerin(&vi,&vc,&op)<0){ 
Monty's avatar
   
Monty committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
      /* error case; not a vorbis header */
      fprintf(stderr,"This Ogg bitstream does not contain Vorbis "
	      "audio data.\n");
      exit(1);
    }
    
    /* At this point, we're sure we're Vorbis.  We've set up the logical
       (Ogg) bitstream decoder.  Get the comment and codebook headers and
       set up the Vorbis decoder */
    
    /* The next two packets in order are the comment and codebook headers.
       They're likely large and may span multiple pages.  Thus we reead
       and submit data until we get our two pacakets, watching that no
       pages are missing.  If a page is missing, error out; losing a
       header page is the only place where missing data is fatal. */
    
    i=0;
Monty's avatar
   
Monty committed
120
    while(i<2){
Monty's avatar
   
Monty committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
      while(i<2){
	int result=ogg_sync_pageout(&oy,&og);
	if(result==0)break; /* Need more data */
	/* Don't complain about missing or corrupt data yet.  We'll
	   catch it at the packet output phase */
	if(result==1){
	  ogg_stream_pagein(&os,&og); /* we can ignore any errors here
					 as they'll also become apparent
					 at packetout */
	  while(i<2){
	    result=ogg_stream_packetout(&os,&op);
	    if(result==0)break;
	    if(result==-1){
	      /* Uh oh; data at some point was corrupted or missing!
		 We can't tolerate that in a header.  Die. */
	      fprintf(stderr,"Corrupt secondary header.  Exiting.\n");
	      exit(1);
Monty's avatar
   
Monty committed
138
139
	    }
	    vorbis_synthesis_headerin(&vi,&vc,&op);
Monty's avatar
   
Monty committed
140
	    i++;
Monty's avatar
   
Monty committed
141
142
143
	  }
	}
      }
Monty's avatar
   
Monty committed
144
145
146
147
148
149
150
151
      /* no harm in not checking before adding more */
      buffer=ogg_sync_buffer(&oy,4096);
      bytes=fread(buffer,1,4096,stdin);
      if(bytes==0){
	fprintf(stderr,"End of file before finding all Vorbis headers!\n");
	exit(1);
      }
      ogg_sync_wrote(&oy,bytes);
Monty's avatar
   
Monty committed
152
    }
Monty's avatar
   
Monty committed
153
154
155
156
    
    /* Throw the comments plus a few lines about the bitstream we're
       decoding */
    {
Monty's avatar
   
Monty committed
157
      char **ptr=vc.user_comments;
Monty's avatar
   
Monty committed
158
159
160
161
      while(*ptr){
	fprintf(stderr,"%s\n",*ptr);
	++ptr;
      }
Monty's avatar
   
Monty committed
162
      fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi.channels,vi.rate);
Monty's avatar
   
Monty committed
163
      fprintf(stderr,"Encoded by: %s\n\n",vc.vendor);
Monty's avatar
   
Monty committed
164
    }
Monty's avatar
   
Monty committed
165
166
    
    convsize=4096/vi.channels;
Monty's avatar
   
Monty committed
167

Monty's avatar
   
Monty committed
168
169
170
171
172
173
174
175
176
177
    /* OK, got and parsed all three headers. Initialize the Vorbis
       packet->PCM decoder. */
    vorbis_synthesis_init(&vd,&vi); /* central decode state */
    vorbis_block_init(&vd,&vb);     /* local state for most of the decode
				       so multiple block decodes can
				       proceed in parallel.  We could init
				       multiple vorbis_block structures
				       for vd here */
    
    /* The rest is just a straight decode loop until end of stream */
Monty's avatar
   
Monty committed
178
    while(!eos){
Monty's avatar
   
Monty committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
      while(!eos){
	int result=ogg_sync_pageout(&oy,&og);
	if(result==0)break; /* need more data */
	if(result==-1){ /* missing or corrupt data at this page position */
	  fprintf(stderr,"Corrupt or missing data in bitstream; "
		  "continuing...\n");
	}else{
	  ogg_stream_pagein(&os,&og); /* can safely ignore errors at
					 this point */
	  while(1){
	    result=ogg_stream_packetout(&os,&op);
	    if(result==0)break; /* need more data */
	    if(result==-1){ /* missing or corrupt data at this page position */
	      /* no reason to complain; already complained above */
	    }else{
	      /* we have a packet.  Decode it */
	      double **pcm;
	      int samples;
Monty's avatar
   
Monty committed
197
	      
Monty's avatar
   
Monty committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
	      vorbis_synthesis(&vb,&op);
	      vorbis_synthesis_blockin(&vd,&vb);
	      /* 
		 
	      **pcm is a multichannel double vector.  In stereo, for
	      example, pcm[0] is left, and pcm[1] is right.  samples is
	      the size of each channel.  Convert the float values
	      (-1.<=range<=1.) to whatever PCM format and write it out */
	      
	      while((samples=vorbis_synthesis_pcmout(&vd,&pcm))>0){
		int j;
		int clipflag=0;
		int out=(samples<convsize?samples:convsize);
		
		/* convert doubles to 16 bit signed ints (host order) and
		   interleave */
		for(i=0;i<vi.channels;i++){
		  int16_t *ptr=convbuffer+i;
		  double  *mono=pcm[i];
		  for(j=0;j<out;j++){
Monty's avatar
   
Monty committed
218
		    int val=mono[j]*32767.;
Monty's avatar
   
Monty committed
219
220
221
222
223
224
225
226
227
228
229
		    /* might as well guard against clipping */
		    if(val>32767){
		      val=32767;
		      clipflag=1;
		    }
		    if(val<-32768){
		      val=-32768;
		      clipflag=1;
		    }
		    *ptr=val;
		    ptr+=2;
Monty's avatar
   
Monty committed
230
		  }
Monty's avatar
   
Monty committed
231
		}
Monty's avatar
   
Monty committed
232
233
234
235
236
237
238
239
240
241
242
243
		
		if(clipflag)
		  fprintf(stderr,"Clipping in frame %ld\n",vd.sequence);
		
		
		fwrite(convbuffer,2*vi.channels,out,stdout);
		
		vorbis_synthesis_read(&vd,out); /* tell libvorbis how
						   many samples we
						   actually consumed */
	      }	    
	    }
Monty's avatar
   
Monty committed
244
	  }
Monty's avatar
   
Monty committed
245
	  if(ogg_page_eos(&og))eos=1;
Monty's avatar
   
Monty committed
246
	}
Monty's avatar
   
Monty committed
247
248
249
250
251
252
      }
      if(!eos){
	buffer=ogg_sync_buffer(&oy,4096);
	bytes=fread(buffer,1,4096,stdin);
	ogg_sync_wrote(&oy,bytes);
	if(bytes==0)eos=1;
Monty's avatar
   
Monty committed
253
254
      }
    }
Monty's avatar
   
Monty committed
255
256
257
258
259
    
    /* clean up this logical bitstream; before exit we see if we're
       followed by another [chained] */

    ogg_stream_clear(&os);
Monty's avatar
   
Monty committed
260
  
Monty's avatar
   
Monty committed
261
262
263
264
    /* ogg_page and ogg_packet structs always point to storage in
       libvorbis.  They're never freed or manipulated directly */
    
    vorbis_block_clear(&vb);
Monty's avatar
   
Monty committed
265
    vorbis_dsp_clear(&vd);
Monty's avatar
   
Monty committed
266
267
268
269
    vorbis_info_clear(&vi);  /* must be called last */
  }

  /* OK, clean up the framer */
Monty's avatar
   
Monty committed
270
271
272
273
274
275
  ogg_sync_clear(&oy);
  
  fprintf(stderr,"Done.\n");
  return(0);
}