annotate src/libvorbis-1.3.3/examples/encoder_example.c @ 168:ceec0dd9ec9c

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