Chris@1
|
1 /********************************************************************
|
Chris@1
|
2 * *
|
Chris@1
|
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
|
Chris@1
|
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
|
Chris@1
|
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
|
Chris@1
|
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
|
Chris@1
|
7 * *
|
Chris@1
|
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007 *
|
Chris@1
|
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
|
Chris@1
|
10 * *
|
Chris@1
|
11 ********************************************************************
|
Chris@1
|
12
|
Chris@1
|
13 function: simple example decoder using vorbisfile
|
Chris@1
|
14 last mod: $Id: vorbisfile_example.c 16328 2009-07-24 01:51:10Z xiphmont $
|
Chris@1
|
15
|
Chris@1
|
16 ********************************************************************/
|
Chris@1
|
17
|
Chris@1
|
18 /* Takes a vorbis bitstream from stdin and writes raw stereo PCM to
|
Chris@1
|
19 stdout using vorbisfile. Using vorbisfile is much simpler than
|
Chris@1
|
20 dealing with libvorbis. */
|
Chris@1
|
21
|
Chris@1
|
22 #include <stdio.h>
|
Chris@1
|
23 #include <stdlib.h>
|
Chris@1
|
24 #include <math.h>
|
Chris@1
|
25 #include <vorbis/codec.h>
|
Chris@1
|
26 #include <vorbis/vorbisfile.h>
|
Chris@1
|
27
|
Chris@1
|
28 #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
|
Chris@1
|
29 #include <io.h>
|
Chris@1
|
30 #include <fcntl.h>
|
Chris@1
|
31 #endif
|
Chris@1
|
32
|
Chris@1
|
33 char pcmout[4096]; /* take 4k out of the data segment, not the stack */
|
Chris@1
|
34
|
Chris@1
|
35 int main(){
|
Chris@1
|
36 OggVorbis_File vf;
|
Chris@1
|
37 int eof=0;
|
Chris@1
|
38 int current_section;
|
Chris@1
|
39
|
Chris@1
|
40 #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
|
Chris@1
|
41 /* Beware the evil ifdef. We avoid these where we can, but this one we
|
Chris@1
|
42 cannot. Don't add any more, you'll probably go to hell if you do. */
|
Chris@1
|
43 _setmode( _fileno( stdin ), _O_BINARY );
|
Chris@1
|
44 _setmode( _fileno( stdout ), _O_BINARY );
|
Chris@1
|
45 #endif
|
Chris@1
|
46
|
Chris@1
|
47 if(ov_open_callbacks(stdin, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE) < 0) {
|
Chris@1
|
48 fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
|
Chris@1
|
49 exit(1);
|
Chris@1
|
50 }
|
Chris@1
|
51
|
Chris@1
|
52 /* Throw the comments plus a few lines about the bitstream we're
|
Chris@1
|
53 decoding */
|
Chris@1
|
54 {
|
Chris@1
|
55 char **ptr=ov_comment(&vf,-1)->user_comments;
|
Chris@1
|
56 vorbis_info *vi=ov_info(&vf,-1);
|
Chris@1
|
57 while(*ptr){
|
Chris@1
|
58 fprintf(stderr,"%s\n",*ptr);
|
Chris@1
|
59 ++ptr;
|
Chris@1
|
60 }
|
Chris@1
|
61 fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate);
|
Chris@1
|
62 fprintf(stderr,"\nDecoded length: %ld samples\n",
|
Chris@1
|
63 (long)ov_pcm_total(&vf,-1));
|
Chris@1
|
64 fprintf(stderr,"Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor);
|
Chris@1
|
65 }
|
Chris@1
|
66
|
Chris@1
|
67 while(!eof){
|
Chris@1
|
68 long ret=ov_read(&vf,pcmout,sizeof(pcmout),0,2,1,¤t_section);
|
Chris@1
|
69 if (ret == 0) {
|
Chris@1
|
70 /* EOF */
|
Chris@1
|
71 eof=1;
|
Chris@1
|
72 } else if (ret < 0) {
|
Chris@1
|
73 if(ret==OV_EBADLINK){
|
Chris@1
|
74 fprintf(stderr,"Corrupt bitstream section! Exiting.\n");
|
Chris@1
|
75 exit(1);
|
Chris@1
|
76 }
|
Chris@1
|
77
|
Chris@1
|
78 /* some other error in the stream. Not a problem, just reporting it in
|
Chris@1
|
79 case we (the app) cares. In this case, we don't. */
|
Chris@1
|
80 } else {
|
Chris@1
|
81 /* we don't bother dealing with sample rate changes, etc, but
|
Chris@1
|
82 you'll have to*/
|
Chris@1
|
83 fwrite(pcmout,1,ret,stdout);
|
Chris@1
|
84 }
|
Chris@1
|
85 }
|
Chris@1
|
86
|
Chris@1
|
87 /* cleanup */
|
Chris@1
|
88 ov_clear(&vf);
|
Chris@1
|
89
|
Chris@1
|
90 fprintf(stderr,"Done.\n");
|
Chris@1
|
91 return(0);
|
Chris@1
|
92 }
|