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 encoder
|
Chris@1
|
14 last mod: $Id: encoder_example.c 16946 2010-03-03 16:12:40Z xiphmont $
|
Chris@1
|
15
|
Chris@1
|
16 ********************************************************************/
|
Chris@1
|
17
|
Chris@1
|
18 /* takes a stereo 16bit 44.1kHz WAV file from stdin and encodes it into
|
Chris@1
|
19 a Vorbis bitstream */
|
Chris@1
|
20
|
Chris@1
|
21 /* Note that this is POSIX, not ANSI, code */
|
Chris@1
|
22
|
Chris@1
|
23 #include <stdio.h>
|
Chris@1
|
24 #include <stdlib.h>
|
Chris@1
|
25 #include <string.h>
|
Chris@1
|
26 #include <time.h>
|
Chris@1
|
27 #include <math.h>
|
Chris@1
|
28 #include <vorbis/vorbisenc.h>
|
Chris@1
|
29
|
Chris@1
|
30 #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
|
Chris@1
|
31 #include <io.h>
|
Chris@1
|
32 #include <fcntl.h>
|
Chris@1
|
33 #endif
|
Chris@1
|
34
|
Chris@1
|
35 #if defined(__MACOS__) && defined(__MWERKS__)
|
Chris@1
|
36 #include <console.h> /* CodeWarrior's Mac "command-line" support */
|
Chris@1
|
37 #endif
|
Chris@1
|
38
|
Chris@1
|
39 #define READ 1024
|
Chris@1
|
40 signed char readbuffer[READ*4+44]; /* out of the data segment, not the stack */
|
Chris@1
|
41
|
Chris@1
|
42 int main(){
|
Chris@1
|
43 ogg_stream_state os; /* take physical pages, weld into a logical
|
Chris@1
|
44 stream of packets */
|
Chris@1
|
45 ogg_page og; /* one Ogg bitstream page. Vorbis packets are inside */
|
Chris@1
|
46 ogg_packet op; /* one raw packet of data for decode */
|
Chris@1
|
47
|
Chris@1
|
48 vorbis_info vi; /* struct that stores all the static vorbis bitstream
|
Chris@1
|
49 settings */
|
Chris@1
|
50 vorbis_comment vc; /* struct that stores all the user comments */
|
Chris@1
|
51
|
Chris@1
|
52 vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
|
Chris@1
|
53 vorbis_block vb; /* local working space for packet->PCM decode */
|
Chris@1
|
54
|
Chris@1
|
55 int eos=0,ret;
|
Chris@1
|
56 int i, founddata;
|
Chris@1
|
57
|
Chris@1
|
58 #if defined(macintosh) && defined(__MWERKS__)
|
Chris@1
|
59 int argc = 0;
|
Chris@1
|
60 char **argv = NULL;
|
Chris@1
|
61 argc = ccommand(&argv); /* get a "command line" from the Mac user */
|
Chris@1
|
62 /* this also lets the user set stdin and stdout */
|
Chris@1
|
63 #endif
|
Chris@1
|
64
|
Chris@1
|
65 /* we cheat on the WAV header; we just bypass 44 bytes (simplest WAV
|
Chris@1
|
66 header is 44 bytes) and assume that the data is 44.1khz, stereo, 16 bit
|
Chris@1
|
67 little endian pcm samples. This is just an example, after all. */
|
Chris@1
|
68
|
Chris@1
|
69 #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
|
Chris@1
|
70 /* if we were reading/writing a file, it would also need to in
|
Chris@1
|
71 binary mode, eg, fopen("file.wav","wb"); */
|
Chris@1
|
72 /* Beware the evil ifdef. We avoid these where we can, but this one we
|
Chris@1
|
73 cannot. Don't add any more, you'll probably go to hell if you do. */
|
Chris@1
|
74 _setmode( _fileno( stdin ), _O_BINARY );
|
Chris@1
|
75 _setmode( _fileno( stdout ), _O_BINARY );
|
Chris@1
|
76 #endif
|
Chris@1
|
77
|
Chris@1
|
78
|
Chris@1
|
79 /* we cheat on the WAV header; we just bypass the header and never
|
Chris@1
|
80 verify that it matches 16bit/stereo/44.1kHz. This is just an
|
Chris@1
|
81 example, after all. */
|
Chris@1
|
82
|
Chris@1
|
83 readbuffer[0] = '\0';
|
Chris@1
|
84 for (i=0, founddata=0; i<30 && ! feof(stdin) && ! ferror(stdin); i++)
|
Chris@1
|
85 {
|
Chris@1
|
86 fread(readbuffer,1,2,stdin);
|
Chris@1
|
87
|
Chris@1
|
88 if ( ! strncmp((char*)readbuffer, "da", 2) ){
|
Chris@1
|
89 founddata = 1;
|
Chris@1
|
90 fread(readbuffer,1,6,stdin);
|
Chris@1
|
91 break;
|
Chris@1
|
92 }
|
Chris@1
|
93 }
|
Chris@1
|
94
|
Chris@1
|
95 /********** Encode setup ************/
|
Chris@1
|
96
|
Chris@1
|
97 vorbis_info_init(&vi);
|
Chris@1
|
98
|
Chris@1
|
99 /* choose an encoding mode. A few possibilities commented out, one
|
Chris@1
|
100 actually used: */
|
Chris@1
|
101
|
Chris@1
|
102 /*********************************************************************
|
Chris@1
|
103 Encoding using a VBR quality mode. The usable range is -.1
|
Chris@1
|
104 (lowest quality, smallest file) to 1. (highest quality, largest file).
|
Chris@1
|
105 Example quality mode .4: 44kHz stereo coupled, roughly 128kbps VBR
|
Chris@1
|
106
|
Chris@1
|
107 ret = vorbis_encode_init_vbr(&vi,2,44100,.4);
|
Chris@1
|
108
|
Chris@1
|
109 ---------------------------------------------------------------------
|
Chris@1
|
110
|
Chris@1
|
111 Encoding using an average bitrate mode (ABR).
|
Chris@1
|
112 example: 44kHz stereo coupled, average 128kbps VBR
|
Chris@1
|
113
|
Chris@1
|
114 ret = vorbis_encode_init(&vi,2,44100,-1,128000,-1);
|
Chris@1
|
115
|
Chris@1
|
116 ---------------------------------------------------------------------
|
Chris@1
|
117
|
Chris@1
|
118 Encode using a quality mode, but select that quality mode by asking for
|
Chris@1
|
119 an approximate bitrate. This is not ABR, it is true VBR, but selected
|
Chris@1
|
120 using the bitrate interface, and then turning bitrate management off:
|
Chris@1
|
121
|
Chris@1
|
122 ret = ( vorbis_encode_setup_managed(&vi,2,44100,-1,128000,-1) ||
|
Chris@1
|
123 vorbis_encode_ctl(&vi,OV_ECTL_RATEMANAGE2_SET,NULL) ||
|
Chris@1
|
124 vorbis_encode_setup_init(&vi));
|
Chris@1
|
125
|
Chris@1
|
126 *********************************************************************/
|
Chris@1
|
127
|
Chris@1
|
128 ret=vorbis_encode_init_vbr(&vi,2,44100,0.1);
|
Chris@1
|
129
|
Chris@1
|
130 /* do not continue if setup failed; this can happen if we ask for a
|
Chris@1
|
131 mode that libVorbis does not support (eg, too low a bitrate, etc,
|
Chris@1
|
132 will return 'OV_EIMPL') */
|
Chris@1
|
133
|
Chris@1
|
134 if(ret)exit(1);
|
Chris@1
|
135
|
Chris@1
|
136 /* add a comment */
|
Chris@1
|
137 vorbis_comment_init(&vc);
|
Chris@1
|
138 vorbis_comment_add_tag(&vc,"ENCODER","encoder_example.c");
|
Chris@1
|
139
|
Chris@1
|
140 /* set up the analysis state and auxiliary encoding storage */
|
Chris@1
|
141 vorbis_analysis_init(&vd,&vi);
|
Chris@1
|
142 vorbis_block_init(&vd,&vb);
|
Chris@1
|
143
|
Chris@1
|
144 /* set up our packet->stream encoder */
|
Chris@1
|
145 /* pick a random serial number; that way we can more likely build
|
Chris@1
|
146 chained streams just by concatenation */
|
Chris@1
|
147 srand(time(NULL));
|
Chris@1
|
148 ogg_stream_init(&os,rand());
|
Chris@1
|
149
|
Chris@1
|
150 /* Vorbis streams begin with three headers; the initial header (with
|
Chris@1
|
151 most of the codec setup parameters) which is mandated by the Ogg
|
Chris@1
|
152 bitstream spec. The second header holds any comment fields. The
|
Chris@1
|
153 third header holds the bitstream codebook. We merely need to
|
Chris@1
|
154 make the headers, then pass them to libvorbis one at a time;
|
Chris@1
|
155 libvorbis handles the additional Ogg bitstream constraints */
|
Chris@1
|
156
|
Chris@1
|
157 {
|
Chris@1
|
158 ogg_packet header;
|
Chris@1
|
159 ogg_packet header_comm;
|
Chris@1
|
160 ogg_packet header_code;
|
Chris@1
|
161
|
Chris@1
|
162 vorbis_analysis_headerout(&vd,&vc,&header,&header_comm,&header_code);
|
Chris@1
|
163 ogg_stream_packetin(&os,&header); /* automatically placed in its own
|
Chris@1
|
164 page */
|
Chris@1
|
165 ogg_stream_packetin(&os,&header_comm);
|
Chris@1
|
166 ogg_stream_packetin(&os,&header_code);
|
Chris@1
|
167
|
Chris@1
|
168 /* This ensures the actual
|
Chris@1
|
169 * audio data will start on a new page, as per spec
|
Chris@1
|
170 */
|
Chris@1
|
171 while(!eos){
|
Chris@1
|
172 int result=ogg_stream_flush(&os,&og);
|
Chris@1
|
173 if(result==0)break;
|
Chris@1
|
174 fwrite(og.header,1,og.header_len,stdout);
|
Chris@1
|
175 fwrite(og.body,1,og.body_len,stdout);
|
Chris@1
|
176 }
|
Chris@1
|
177
|
Chris@1
|
178 }
|
Chris@1
|
179
|
Chris@1
|
180 while(!eos){
|
Chris@1
|
181 long i;
|
Chris@1
|
182 long bytes=fread(readbuffer,1,READ*4,stdin); /* stereo hardwired here */
|
Chris@1
|
183
|
Chris@1
|
184 if(bytes==0){
|
Chris@1
|
185 /* end of file. this can be done implicitly in the mainline,
|
Chris@1
|
186 but it's easier to see here in non-clever fashion.
|
Chris@1
|
187 Tell the library we're at end of stream so that it can handle
|
Chris@1
|
188 the last frame and mark end of stream in the output properly */
|
Chris@1
|
189 vorbis_analysis_wrote(&vd,0);
|
Chris@1
|
190
|
Chris@1
|
191 }else{
|
Chris@1
|
192 /* data to encode */
|
Chris@1
|
193
|
Chris@1
|
194 /* expose the buffer to submit data */
|
Chris@1
|
195 float **buffer=vorbis_analysis_buffer(&vd,READ);
|
Chris@1
|
196
|
Chris@1
|
197 /* uninterleave samples */
|
Chris@1
|
198 for(i=0;i<bytes/4;i++){
|
Chris@1
|
199 buffer[0][i]=((readbuffer[i*4+1]<<8)|
|
Chris@1
|
200 (0x00ff&(int)readbuffer[i*4]))/32768.f;
|
Chris@1
|
201 buffer[1][i]=((readbuffer[i*4+3]<<8)|
|
Chris@1
|
202 (0x00ff&(int)readbuffer[i*4+2]))/32768.f;
|
Chris@1
|
203 }
|
Chris@1
|
204
|
Chris@1
|
205 /* tell the library how much we actually submitted */
|
Chris@1
|
206 vorbis_analysis_wrote(&vd,i);
|
Chris@1
|
207 }
|
Chris@1
|
208
|
Chris@1
|
209 /* vorbis does some data preanalysis, then divvies up blocks for
|
Chris@1
|
210 more involved (potentially parallel) processing. Get a single
|
Chris@1
|
211 block for encoding now */
|
Chris@1
|
212 while(vorbis_analysis_blockout(&vd,&vb)==1){
|
Chris@1
|
213
|
Chris@1
|
214 /* analysis, assume we want to use bitrate management */
|
Chris@1
|
215 vorbis_analysis(&vb,NULL);
|
Chris@1
|
216 vorbis_bitrate_addblock(&vb);
|
Chris@1
|
217
|
Chris@1
|
218 while(vorbis_bitrate_flushpacket(&vd,&op)){
|
Chris@1
|
219
|
Chris@1
|
220 /* weld the packet into the bitstream */
|
Chris@1
|
221 ogg_stream_packetin(&os,&op);
|
Chris@1
|
222
|
Chris@1
|
223 /* write out pages (if any) */
|
Chris@1
|
224 while(!eos){
|
Chris@1
|
225 int result=ogg_stream_pageout(&os,&og);
|
Chris@1
|
226 if(result==0)break;
|
Chris@1
|
227 fwrite(og.header,1,og.header_len,stdout);
|
Chris@1
|
228 fwrite(og.body,1,og.body_len,stdout);
|
Chris@1
|
229
|
Chris@1
|
230 /* this could be set above, but for illustrative purposes, I do
|
Chris@1
|
231 it here (to show that vorbis does know where the stream ends) */
|
Chris@1
|
232
|
Chris@1
|
233 if(ogg_page_eos(&og))eos=1;
|
Chris@1
|
234 }
|
Chris@1
|
235 }
|
Chris@1
|
236 }
|
Chris@1
|
237 }
|
Chris@1
|
238
|
Chris@1
|
239 /* clean up and exit. vorbis_info_clear() must be called last */
|
Chris@1
|
240
|
Chris@1
|
241 ogg_stream_clear(&os);
|
Chris@1
|
242 vorbis_block_clear(&vb);
|
Chris@1
|
243 vorbis_dsp_clear(&vd);
|
Chris@1
|
244 vorbis_comment_clear(&vc);
|
Chris@1
|
245 vorbis_info_clear(&vi);
|
Chris@1
|
246
|
Chris@1
|
247 /* ogg_page and ogg_packet structs always point to storage in
|
Chris@1
|
248 libvorbis. They're never freed or manipulated directly */
|
Chris@1
|
249
|
Chris@1
|
250 fprintf(stderr,"Done.\n");
|
Chris@1
|
251 return(0);
|
Chris@1
|
252 }
|