LosaDev source snapshot 2019
This commit is contained in:
commit
c198676e97
20588 files changed
+8812878
No files matched your search
@@ -0,0 +1,71 @@
|
||||
/********************************************************************
|
||||
* *
|
||||
* THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
|
||||
* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
|
||||
* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
|
||||
* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
|
||||
* *
|
||||
* THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
|
||||
* by the XIPHOPHORUS Company http://www.xiph.org/ *
|
||||
* *
|
||||
********************************************************************
|
||||
|
||||
function: illustrate simple use of chained bitstream and vorbisfile.a
|
||||
last mod: $Id: chaining_example.c,v 1.17 2002/07/11 06:40:47 xiphmont Exp $
|
||||
|
||||
********************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <vorbis/codec.h>
|
||||
#include <vorbis/vorbisfile.h>
|
||||
|
||||
#ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
|
||||
#include <io.h>
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
int main(){
|
||||
OggVorbis_File ov;
|
||||
int i;
|
||||
|
||||
#ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
|
||||
/* Beware the evil ifdef. We avoid these where we can, but this one we
|
||||
cannot. Don't add any more, you'll probably go to hell if you do. */
|
||||
_setmode( _fileno( stdin ), _O_BINARY );
|
||||
_setmode( _fileno( stdout ), _O_BINARY );
|
||||
#endif
|
||||
|
||||
/* open the file/pipe on stdin */
|
||||
if(ov_open(stdin,&ov,NULL,-1)<0){
|
||||
printf("Could not open input as an OggVorbis file.\n\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* print details about each logical bitstream in the input */
|
||||
if(ov_seekable(&ov)){
|
||||
printf("Input bitstream contained %ld logical bitstream section(s).\n",
|
||||
ov_streams(&ov));
|
||||
printf("Total bitstream playing time: %ld seconds\n\n",
|
||||
(long)ov_time_total(&ov,-1));
|
||||
|
||||
}else{
|
||||
printf("Standard input was not seekable.\n"
|
||||
"First logical bitstream information:\n\n");
|
||||
}
|
||||
|
||||
for(i=0;i<ov_streams(&ov);i++){
|
||||
vorbis_info *vi=ov_info(&ov,i);
|
||||
printf("\tlogical bitstream section %d information:\n",i+1);
|
||||
printf("\t\t%ldHz %d channels bitrate %ldkbps serial number=%ld\n",
|
||||
vi->rate,vi->channels,ov_bitrate(&ov,i)/1000,
|
||||
ov_serialnumber(&ov,i));
|
||||
printf("\t\theader length: %ld bytes\n",(long)
|
||||
(ov.dataoffsets[i]-ov.offsets[i]));
|
||||
printf("\t\tcompressed length: %ld bytes\n",(long)(ov_raw_total(&ov,i)));
|
||||
printf("\t\tplay time: %lds\n",(long)ov_time_total(&ov,i));
|
||||
}
|
||||
|
||||
ov_clear(&ov);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,307 @@
|
||||
/********************************************************************
|
||||
* *
|
||||
* THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
|
||||
* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
|
||||
* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
|
||||
* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
|
||||
* *
|
||||
* THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
|
||||
* by the XIPHOPHORUS Company http://www.xiph.org/ *
|
||||
* *
|
||||
********************************************************************
|
||||
|
||||
function: simple example decoder
|
||||
last mod: $Id: decoder_example.c,v 1.27 2002/07/12 15:07:52 giles Exp $
|
||||
|
||||
********************************************************************/
|
||||
|
||||
/* Takes a vorbis bitstream from stdin and writes raw stereo PCM to
|
||||
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 */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <vorbis/codec.h>
|
||||
|
||||
#ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
|
||||
#include <io.h>
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
#if defined(__MACOS__) && defined(__MWERKS__)
|
||||
#include <console.h> /* CodeWarrior's Mac "command-line" support */
|
||||
#endif
|
||||
|
||||
ogg_int16_t convbuffer[4096]; /* take 8k out of the data segment, not the stack */
|
||||
int convsize=4096;
|
||||
|
||||
extern void _VDBG_dump(void);
|
||||
|
||||
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 */
|
||||
vorbis_comment vc; /* struct that stores all the bitstream user comments */
|
||||
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;
|
||||
|
||||
#ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
|
||||
/* Beware the evil ifdef. We avoid these where we can, but this one we
|
||||
cannot. Don't add any more, you'll probably go to hell if you do. */
|
||||
_setmode( _fileno( stdin ), _O_BINARY );
|
||||
_setmode( _fileno( stdout ), _O_BINARY );
|
||||
#endif
|
||||
|
||||
#if defined(macintosh) && defined(__MWERKS__)
|
||||
{
|
||||
int argc;
|
||||
char **argv;
|
||||
argc=ccommand(&argv); /* get a "command line" from the Mac user */
|
||||
/* this also lets the user set stdin and stdout */
|
||||
}
|
||||
#endif
|
||||
|
||||
/********** Decode setup ************/
|
||||
|
||||
ogg_sync_init(&oy); /* Now we can read pages */
|
||||
|
||||
while(1){ /* we repeat if the bitstream is chained */
|
||||
int eos=0;
|
||||
int i;
|
||||
|
||||
/* 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. */
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
||||
/* 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);
|
||||
vorbis_comment_init(&vc);
|
||||
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);
|
||||
}
|
||||
|
||||
if(vorbis_synthesis_headerin(&vi,&vc,&op)<0){
|
||||
/* 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;
|
||||
while(i<2){
|
||||
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<0){
|
||||
/* 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);
|
||||
}
|
||||
vorbis_synthesis_headerin(&vi,&vc,&op);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* no harm in not checking before adding more */
|
||||
buffer=ogg_sync_buffer(&oy,4096);
|
||||
bytes=fread(buffer,1,4096,stdin);
|
||||
if(bytes==0 && i<2){
|
||||
fprintf(stderr,"End of file before finding all Vorbis headers!\n");
|
||||
exit(1);
|
||||
}
|
||||
ogg_sync_wrote(&oy,bytes);
|
||||
}
|
||||
|
||||
/* Throw the comments plus a few lines about the bitstream we're
|
||||
decoding */
|
||||
{
|
||||
char **ptr=vc.user_comments;
|
||||
while(*ptr){
|
||||
fprintf(stderr,"%s\n",*ptr);
|
||||
++ptr;
|
||||
}
|
||||
fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi.channels,vi.rate);
|
||||
fprintf(stderr,"Encoded by: %s\n\n",vc.vendor);
|
||||
}
|
||||
|
||||
convsize=4096/vi.channels;
|
||||
|
||||
/* 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 */
|
||||
while(!eos){
|
||||
while(!eos){
|
||||
int result=ogg_sync_pageout(&oy,&og);
|
||||
if(result==0)break; /* need more data */
|
||||
if(result<0){ /* 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<0){ /* missing or corrupt data at this page position */
|
||||
/* no reason to complain; already complained above */
|
||||
}else{
|
||||
/* we have a packet. Decode it */
|
||||
float **pcm;
|
||||
int samples;
|
||||
|
||||
if(vorbis_synthesis(&vb,&op)==0) /* test for success! */
|
||||
vorbis_synthesis_blockin(&vd,&vb);
|
||||
/*
|
||||
|
||||
**pcm is a multichannel float 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 bout=(samples<convsize?samples:convsize);
|
||||
|
||||
/* convert floats to 16 bit signed ints (host order) and
|
||||
interleave */
|
||||
for(i=0;i<vi.channels;i++){
|
||||
ogg_int16_t *ptr=convbuffer+i;
|
||||
float *mono=pcm[i];
|
||||
for(j=0;j<bout;j++){
|
||||
#if 1
|
||||
int val=mono[j]*32767.f;
|
||||
#else /* optional dither */
|
||||
int val=mono[j]*32767.f+drand48()-0.5f;
|
||||
#endif
|
||||
/* might as well guard against clipping */
|
||||
if(val>32767){
|
||||
val=32767;
|
||||
clipflag=1;
|
||||
}
|
||||
if(val<-32768){
|
||||
val=-32768;
|
||||
clipflag=1;
|
||||
}
|
||||
*ptr=val;
|
||||
ptr+=vi.channels;
|
||||
}
|
||||
}
|
||||
|
||||
if(clipflag)
|
||||
fprintf(stderr,"Clipping in frame %ld\n",(long)(vd.sequence));
|
||||
|
||||
|
||||
fwrite(convbuffer,2*vi.channels,bout,stdout);
|
||||
|
||||
vorbis_synthesis_read(&vd,bout); /* tell libvorbis how
|
||||
many samples we
|
||||
actually consumed */
|
||||
}
|
||||
}
|
||||
}
|
||||
if(ogg_page_eos(&og))eos=1;
|
||||
}
|
||||
}
|
||||
if(!eos){
|
||||
buffer=ogg_sync_buffer(&oy,4096);
|
||||
bytes=fread(buffer,1,4096,stdin);
|
||||
ogg_sync_wrote(&oy,bytes);
|
||||
if(bytes==0)eos=1;
|
||||
}
|
||||
}
|
||||
|
||||
/* clean up this logical bitstream; before exit we see if we're
|
||||
followed by another [chained] */
|
||||
|
||||
ogg_stream_clear(&os);
|
||||
|
||||
/* ogg_page and ogg_packet structs always point to storage in
|
||||
libvorbis. They're never freed or manipulated directly */
|
||||
|
||||
vorbis_block_clear(&vb);
|
||||
vorbis_dsp_clear(&vd);
|
||||
vorbis_comment_clear(&vc);
|
||||
vorbis_info_clear(&vi); /* must be called last */
|
||||
}
|
||||
|
||||
/* OK, clean up the framer */
|
||||
ogg_sync_clear(&oy);
|
||||
|
||||
fprintf(stderr,"Done.\n");
|
||||
return(0);
|
||||
}
|
||||
@@ -0,0 +1,253 @@
|
||||
/********************************************************************
|
||||
* *
|
||||
* THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
|
||||
* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
|
||||
* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
|
||||
* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
|
||||
* *
|
||||
* THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
|
||||
* by the XIPHOPHORUS Company http://www.xiph.org/ *
|
||||
* *
|
||||
********************************************************************
|
||||
|
||||
function: simple example encoder
|
||||
last mod: $Id: encoder_example.c,v 1.50 2002/07/16 09:26:07 xiphmont Exp $
|
||||
|
||||
********************************************************************/
|
||||
|
||||
/* takes a stereo 16bit 44.1kHz WAV file from stdin and encodes it into
|
||||
a Vorbis bitstream */
|
||||
|
||||
/* Note that this is POSIX, not ANSI, code */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <vorbis/vorbisenc.h>
|
||||
|
||||
#ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
|
||||
#include <io.h>
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
#if defined(__MACOS__) && defined(__MWERKS__)
|
||||
#include <console.h> /* CodeWarrior's Mac "command-line" support */
|
||||
#endif
|
||||
|
||||
#define READ 1024
|
||||
signed char readbuffer[READ*4+44]; /* out of the data segment, not the stack */
|
||||
|
||||
int main(){
|
||||
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 */
|
||||
vorbis_comment vc; /* struct that stores all the user comments */
|
||||
|
||||
vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
|
||||
vorbis_block vb; /* local working space for packet->PCM decode */
|
||||
|
||||
int eos=0,ret;
|
||||
int i, founddata;
|
||||
|
||||
#if defined(macintosh) && defined(__MWERKS__)
|
||||
int argc = 0;
|
||||
char **argv = NULL;
|
||||
argc = ccommand(&argv); /* get a "command line" from the Mac user */
|
||||
/* this also lets the user set stdin and stdout */
|
||||
#endif
|
||||
|
||||
/* we cheat on the WAV header; we just bypass 44 bytes and never
|
||||
verify that it matches 16bit/stereo/44.1kHz. This is just an
|
||||
example, after all. */
|
||||
|
||||
#ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
|
||||
/* if we were reading/writing a file, it would also need to in
|
||||
binary mode, eg, fopen("file.wav","wb"); */
|
||||
/* Beware the evil ifdef. We avoid these where we can, but this one we
|
||||
cannot. Don't add any more, you'll probably go to hell if you do. */
|
||||
_setmode( _fileno( stdin ), _O_BINARY );
|
||||
_setmode( _fileno( stdout ), _O_BINARY );
|
||||
#endif
|
||||
|
||||
|
||||
/* we cheat on the WAV header; we just bypass the header and never
|
||||
verify that it matches 16bit/stereo/44.1kHz. This is just an
|
||||
example, after all. */
|
||||
|
||||
readbuffer[0] = '\0';
|
||||
for (i=0, founddata=0; i<30 && ! feof(stdin) && ! ferror(stdin); i++)
|
||||
{
|
||||
fread(readbuffer,1,2,stdin);
|
||||
|
||||
if ( ! strncmp((char*)readbuffer, "da", 2) )
|
||||
{
|
||||
founddata = 1;
|
||||
fread(readbuffer,1,6,stdin);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/********** Encode setup ************/
|
||||
|
||||
vorbis_info_init(&vi);
|
||||
|
||||
/* choose an encoding mode. A few possibilities commented out, one
|
||||
actually used: */
|
||||
|
||||
/*********************************************************************
|
||||
Encoding using a VBR quality mode. The usable range is -.1
|
||||
(lowest quality, smallest file) to 1. (highest quality, largest file).
|
||||
Example quality mode .4: 44kHz stereo coupled, roughly 128kbps VBR
|
||||
|
||||
ret = vorbis_encode_init_vbr(&vi,2,44100,.4);
|
||||
|
||||
---------------------------------------------------------------------
|
||||
|
||||
Encoding using an average bitrate mode (ABR).
|
||||
example: 44kHz stereo coupled, average 128kbps VBR
|
||||
|
||||
ret = vorbis_encode_init(&vi,2,44100,-1,128000,-1);
|
||||
|
||||
---------------------------------------------------------------------
|
||||
|
||||
Encode using a qulity mode, but select that quality mode by asking for
|
||||
an approximate bitrate. This is not ABR, it is true VBR, but selected
|
||||
using the bitrate interface, and then turning bitrate management off:
|
||||
|
||||
ret = ( vorbis_encode_setup_managed(&vi,2,44100,-1,128000,-1) ||
|
||||
vorbis_encode_ctl(&vi,OV_ECTL_RATEMANAGE_AVG,NULL) ||
|
||||
vorbis_encode_setup_init(&vi));
|
||||
|
||||
*********************************************************************/
|
||||
|
||||
ret=vorbis_encode_init_vbr(&vi,2,44100,.5);
|
||||
|
||||
/* do not continue if setup failed; this can happen if we ask for a
|
||||
mode that libVorbis does not support (eg, too low a bitrate, etc,
|
||||
will return 'OV_EIMPL') */
|
||||
|
||||
if(ret)exit(1);
|
||||
|
||||
/* add a comment */
|
||||
vorbis_comment_init(&vc);
|
||||
vorbis_comment_add_tag(&vc,"ENCODER","encoder_example.c");
|
||||
|
||||
/* set up the analysis state and auxiliary encoding storage */
|
||||
vorbis_analysis_init(&vd,&vi);
|
||||
vorbis_block_init(&vd,&vb);
|
||||
|
||||
/* set up our packet->stream encoder */
|
||||
/* pick a random serial number; that way we can more likely build
|
||||
chained streams just by concatenation */
|
||||
srand(time(NULL));
|
||||
ogg_stream_init(&os,rand());
|
||||
|
||||
/* Vorbis streams begin with three headers; the initial header (with
|
||||
most of the codec setup parameters) which is mandated by the Ogg
|
||||
bitstream spec. The second header holds any comment fields. The
|
||||
third header holds the bitstream codebook. We merely need to
|
||||
make the headers, then pass them to libvorbis one at a time;
|
||||
libvorbis handles the additional Ogg bitstream constraints */
|
||||
|
||||
{
|
||||
ogg_packet header;
|
||||
ogg_packet header_comm;
|
||||
ogg_packet header_code;
|
||||
|
||||
vorbis_analysis_headerout(&vd,&vc,&header,&header_comm,&header_code);
|
||||
ogg_stream_packetin(&os,&header); /* automatically placed in its own
|
||||
page */
|
||||
ogg_stream_packetin(&os,&header_comm);
|
||||
ogg_stream_packetin(&os,&header_code);
|
||||
|
||||
/* This ensures the actual
|
||||
* audio data will start on a new page, as per spec
|
||||
*/
|
||||
while(!eos){
|
||||
int result=ogg_stream_flush(&os,&og);
|
||||
if(result==0)break;
|
||||
fwrite(og.header,1,og.header_len,stdout);
|
||||
fwrite(og.body,1,og.body_len,stdout);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
while(!eos){
|
||||
long i;
|
||||
long bytes=fread(readbuffer,1,READ*4,stdin); /* stereo hardwired here */
|
||||
|
||||
if(bytes==0){
|
||||
/* end of file. this can be done implicitly in the mainline,
|
||||
but it's easier to see here in non-clever fashion.
|
||||
Tell the library we're at end of stream so that it can handle
|
||||
the last frame and mark end of stream in the output properly */
|
||||
vorbis_analysis_wrote(&vd,0);
|
||||
|
||||
}else{
|
||||
/* data to encode */
|
||||
|
||||
/* expose the buffer to submit data */
|
||||
float **buffer=vorbis_analysis_buffer(&vd,READ);
|
||||
|
||||
/* uninterleave samples */
|
||||
for(i=0;i<bytes/4;i++){
|
||||
buffer[0][i]=((readbuffer[i*4+1]<<8)|
|
||||
(0x00ff&(int)readbuffer[i*4]))/32768.f;
|
||||
buffer[1][i]=((readbuffer[i*4+3]<<8)|
|
||||
(0x00ff&(int)readbuffer[i*4+2]))/32768.f;
|
||||
}
|
||||
|
||||
/* tell the library how much we actually submitted */
|
||||
vorbis_analysis_wrote(&vd,i);
|
||||
}
|
||||
|
||||
/* vorbis does some data preanalysis, then divvies up blocks for
|
||||
more involved (potentially parallel) processing. Get a single
|
||||
block for encoding now */
|
||||
while(vorbis_analysis_blockout(&vd,&vb)==1){
|
||||
|
||||
/* analysis, assume we want to use bitrate management */
|
||||
vorbis_analysis(&vb,NULL);
|
||||
vorbis_bitrate_addblock(&vb);
|
||||
|
||||
while(vorbis_bitrate_flushpacket(&vd,&op)){
|
||||
|
||||
/* weld the packet into the bitstream */
|
||||
ogg_stream_packetin(&os,&op);
|
||||
|
||||
/* write out pages (if any) */
|
||||
while(!eos){
|
||||
int result=ogg_stream_pageout(&os,&og);
|
||||
if(result==0)break;
|
||||
fwrite(og.header,1,og.header_len,stdout);
|
||||
fwrite(og.body,1,og.body_len,stdout);
|
||||
|
||||
/* this could be set above, but for illustrative purposes, I do
|
||||
it here (to show that vorbis does know where the stream ends) */
|
||||
|
||||
if(ogg_page_eos(&og))eos=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* clean up and exit. vorbis_info_clear() must be called last */
|
||||
|
||||
ogg_stream_clear(&os);
|
||||
vorbis_block_clear(&vb);
|
||||
vorbis_dsp_clear(&vd);
|
||||
vorbis_comment_clear(&vc);
|
||||
vorbis_info_clear(&vi);
|
||||
|
||||
/* ogg_page and ogg_packet structs always point to storage in
|
||||
libvorbis. They're never freed or manipulated directly */
|
||||
|
||||
fprintf(stderr,"Done.\n");
|
||||
return(0);
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
/********************************************************************
|
||||
* *
|
||||
* THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
|
||||
* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
|
||||
* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
|
||||
* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
|
||||
* *
|
||||
* THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
|
||||
* by the XIPHOPHORUS Company http://www.xiph.org/ *
|
||||
* *
|
||||
********************************************************************
|
||||
|
||||
function: illustrate seeking, and test it too
|
||||
last mod: $Id: seeking_example.c,v 1.15 2002/07/11 06:40:47 xiphmont Exp $
|
||||
|
||||
********************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "vorbis/codec.h"
|
||||
#include "vorbis/vorbisfile.h"
|
||||
|
||||
#ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
|
||||
# include <io.h>
|
||||
# include <fcntl.h>
|
||||
#endif
|
||||
|
||||
void _verify(OggVorbis_File *ov,ogg_int64_t pos,
|
||||
ogg_int64_t val,ogg_int64_t pcmval,
|
||||
ogg_int64_t pcmlength,
|
||||
char *bigassbuffer){
|
||||
int j;
|
||||
long bread;
|
||||
char buffer[4096];
|
||||
int dummy;
|
||||
|
||||
/* verify the raw position, the pcm position and position decode */
|
||||
if(val!=-1 && ov_raw_tell(ov)<val){
|
||||
printf("raw position out of tolerance: requested %ld, got %ld\n",
|
||||
(long)val,(long)ov_raw_tell(ov));
|
||||
exit(1);
|
||||
}
|
||||
if(pcmval!=-1 && ov_pcm_tell(ov)>pcmval){
|
||||
printf("pcm position out of tolerance: requested %ld, got %ld\n",
|
||||
(long)pcmval,(long)ov_pcm_tell(ov));
|
||||
exit(1);
|
||||
}
|
||||
pos=ov_pcm_tell(ov);
|
||||
if(pos<0 || pos>pcmlength){
|
||||
printf("pcm position out of bounds: got %ld\n",(long)pos);
|
||||
exit(1);
|
||||
}
|
||||
bread=ov_read(ov,buffer,4096,1,1,1,&dummy);
|
||||
for(j=0;j<bread;j++){
|
||||
if(buffer[j]!=bigassbuffer[j+pos*2]){
|
||||
printf("data position after seek doesn't match pcm position\n");
|
||||
|
||||
{
|
||||
FILE *f=fopen("a.m","w");
|
||||
for(j=0;j<bread;j++)fprintf(f,"%d\n",(int)buffer[j]);
|
||||
fclose(f);
|
||||
f=fopen("b.m","w");
|
||||
for(j=0;j<bread;j++)fprintf(f,"%d\n",(int)bigassbuffer[j+pos*2]);
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
OggVorbis_File ov;
|
||||
int i,ret;
|
||||
ogg_int64_t pcmlength;
|
||||
char *bigassbuffer;
|
||||
int dummy;
|
||||
|
||||
#ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
|
||||
_setmode( _fileno( stdin ), _O_BINARY );
|
||||
_setmode( _fileno( stdout ), _O_BINARY );
|
||||
#endif
|
||||
|
||||
|
||||
/* open the file/pipe on stdin */
|
||||
if(ov_open(stdin,&ov,NULL,-1)<0){
|
||||
printf("Could not open input as an OggVorbis file.\n\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if(ov_seekable(&ov)){
|
||||
|
||||
/* to simplify our own lives, we want to assume the whole file is
|
||||
stereo. Verify this to avoid potentially mystifying users
|
||||
(pissing them off is OK, just don't confuse them) */
|
||||
for(i=0;i<ov.links;i++){
|
||||
vorbis_info *vi=ov_info(&ov,i);
|
||||
if(vi->channels!=2){
|
||||
printf("Sorry; right now seeking_test can only use Vorbis files\n"
|
||||
"that are entirely stereo.\n\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/* because we want to do sample-level verification that the seek
|
||||
does what it claimed, decode the entire file into memory */
|
||||
fflush(stdout);
|
||||
pcmlength=ov_pcm_total(&ov,-1);
|
||||
bigassbuffer=malloc(pcmlength*2); /* w00t */
|
||||
i=0;
|
||||
while(i<pcmlength*2){
|
||||
int ret=ov_read(&ov,bigassbuffer+i,pcmlength*2-i,1,1,1,&dummy);
|
||||
if(ret<0)continue;
|
||||
if(ret){
|
||||
i+=ret;
|
||||
}else{
|
||||
pcmlength=i/2;
|
||||
}
|
||||
fprintf(stderr,"\rloading.... [%ld left] ",
|
||||
(long)(pcmlength*2-i));
|
||||
}
|
||||
|
||||
/* Exercise all the real seeking cases; ov_raw_seek,
|
||||
ov_pcm_seek_page and ov_pcm_seek. time seek is just a wrapper
|
||||
on pcm_seek */
|
||||
{
|
||||
ogg_int64_t length=ov.end;
|
||||
printf("\rtesting raw seeking to random places in %ld bytes....\n",
|
||||
(long)length);
|
||||
|
||||
for(i=0;i<1000;i++){
|
||||
ogg_int64_t val=(double)rand()/RAND_MAX*length;
|
||||
ogg_int64_t pos;
|
||||
printf("\r\t%d [raw position %ld]... ",i,(long)val);
|
||||
fflush(stdout);
|
||||
ret=ov_raw_seek(&ov,val);
|
||||
if(ret<0){
|
||||
printf("seek failed: %d\n",ret);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
_verify(&ov,pos,val,-1,pcmlength,bigassbuffer);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
printf("\r");
|
||||
{
|
||||
printf("testing pcm page seeking to random places in %ld samples....\n",
|
||||
(long)pcmlength);
|
||||
|
||||
for(i=0;i<1000;i++){
|
||||
ogg_int64_t val=(double)rand()/RAND_MAX*pcmlength;
|
||||
ogg_int64_t pos;
|
||||
printf("\r\t%d [pcm position %ld]... ",i,(long)val);
|
||||
fflush(stdout);
|
||||
ret=ov_pcm_seek_page(&ov,val);
|
||||
if(ret<0){
|
||||
printf("seek failed: %d\n",ret);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
_verify(&ov,pos,-1,val,pcmlength,bigassbuffer);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
printf("\r");
|
||||
{
|
||||
ogg_int64_t length=ov.end;
|
||||
printf("testing pcm exact seeking to random places in %ld samples....\n",
|
||||
(long)pcmlength);
|
||||
|
||||
for(i=0;i<1000;i++){
|
||||
ogg_int64_t val=(double)rand()/RAND_MAX*pcmlength;
|
||||
ogg_int64_t pos;
|
||||
printf("\r\t%d [pcm position %ld]... ",i,(long)val);
|
||||
fflush(stdout);
|
||||
ret=ov_pcm_seek(&ov,val);
|
||||
if(ret<0){
|
||||
printf("seek failed: %d\n",ret);
|
||||
exit(1);
|
||||
}
|
||||
if(ov_pcm_tell(&ov)!=val){
|
||||
printf("Declared position didn't perfectly match request: %ld != %ld\n",
|
||||
(long)val,(long)ov_pcm_tell(&ov));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
_verify(&ov,pos,-1,val,pcmlength,bigassbuffer);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
printf("\r \nOK.\n\n");
|
||||
|
||||
|
||||
}else{
|
||||
printf("Standard input was not seekable.\n");
|
||||
}
|
||||
|
||||
ov_clear(&ov);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
/********************************************************************
|
||||
* *
|
||||
* THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
|
||||
* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
|
||||
* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
|
||||
* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
|
||||
* *
|
||||
* THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
|
||||
* by the XIPHOPHORUS Company http://www.xiph.org/ *
|
||||
* *
|
||||
********************************************************************
|
||||
|
||||
function: simple example decoder using vorbisfile
|
||||
last mod: $Id: vorbisfile_example.c,v 1.10 2002/07/11 06:40:47 xiphmont Exp $
|
||||
|
||||
********************************************************************/
|
||||
|
||||
/* Takes a vorbis bitstream from stdin and writes raw stereo PCM to
|
||||
stdout using vorbisfile. Using vorbisfile is much simpler than
|
||||
dealing with libvorbis. */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <vorbis/codec.h>
|
||||
#include <vorbis/vorbisfile.h>
|
||||
|
||||
#ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
|
||||
#include <io.h>
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
char pcmout[4096]; /* take 4k out of the data segment, not the stack */
|
||||
|
||||
int main(){
|
||||
OggVorbis_File vf;
|
||||
int eof=0;
|
||||
int current_section;
|
||||
|
||||
#ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
|
||||
/* Beware the evil ifdef. We avoid these where we can, but this one we
|
||||
cannot. Don't add any more, you'll probably go to hell if you do. */
|
||||
_setmode( _fileno( stdin ), _O_BINARY );
|
||||
_setmode( _fileno( stdout ), _O_BINARY );
|
||||
#endif
|
||||
|
||||
if(ov_open(stdin, &vf, NULL, 0) < 0) {
|
||||
fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Throw the comments plus a few lines about the bitstream we're
|
||||
decoding */
|
||||
{
|
||||
char **ptr=ov_comment(&vf,-1)->user_comments;
|
||||
vorbis_info *vi=ov_info(&vf,-1);
|
||||
while(*ptr){
|
||||
fprintf(stderr,"%s\n",*ptr);
|
||||
++ptr;
|
||||
}
|
||||
fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate);
|
||||
fprintf(stderr,"\nDecoded length: %ld samples\n",
|
||||
(long)ov_pcm_total(&vf,-1));
|
||||
fprintf(stderr,"Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor);
|
||||
}
|
||||
|
||||
while(!eof){
|
||||
long ret=ov_read(&vf,pcmout,sizeof(pcmout),0,2,1,¤t_section);
|
||||
if (ret == 0) {
|
||||
/* EOF */
|
||||
eof=1;
|
||||
} else if (ret < 0) {
|
||||
/* error in the stream. Not a problem, just reporting it in
|
||||
case we (the app) cares. In this case, we don't. */
|
||||
} else {
|
||||
/* we don't bother dealing with sample rate changes, etc, but
|
||||
you'll have to*/
|
||||
fwrite(pcmout,1,ret,stdout);
|
||||
}
|
||||
}
|
||||
|
||||
/* cleanup */
|
||||
ov_clear(&vf);
|
||||
|
||||
fprintf(stderr,"Done.\n");
|
||||
return(0);
|
||||
}
|
||||
Reference in new issue
Block a user