From 77b98cab57b0e67a0427d5543623ac54ee22673f Mon Sep 17 00:00:00 2001 From: shatty Date: Tue, 24 Feb 2004 13:52:39 +0000 Subject: [PATCH] ogg brought up to cvs as of today, required for theora git-svn-id: file:///srv/svn/repos/haiku/trunk/current@6729 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../media/plugins/ogg/libogg/bitwise.c | 470 +++++++++++++++--- .../media/plugins/ogg/libogg/framing.c | 12 +- .../media/plugins/ogg/libogg/ogg/ogg.h | 20 +- .../media/plugins/ogg/libogg/ogg/os_types.h | 24 +- 4 files changed, 448 insertions(+), 78 deletions(-) diff --git a/src/add-ons/media/plugins/ogg/libogg/bitwise.c b/src/add-ons/media/plugins/ogg/libogg/bitwise.c index 2d01ad011b..fde3104369 100644 --- a/src/add-ons/media/plugins/ogg/libogg/bitwise.c +++ b/src/add-ons/media/plugins/ogg/libogg/bitwise.c @@ -11,7 +11,7 @@ ******************************************************************** function: packing variable sized words into an octet stream - last mod: $Id: bitwise.c,v 1.1 2003/12/13 20:12:32 shatty Exp $ + last mod: $Id: bitwise.c,v 1.2 2004/02/24 13:52:39 shatty Exp $ ********************************************************************/ @@ -33,6 +33,9 @@ static unsigned long mask[]= 0x01ffffff,0x03ffffff,0x07ffffff,0x0fffffff,0x1fffffff, 0x3fffffff,0x7fffffff,0xffffffff }; +static unsigned int mask8B[]= +{0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff}; + void oggpack_writeinit(oggpack_buffer *b){ memset(b,0,sizeof(*b)); b->ptr=b->buffer=_ogg_malloc(BUFFER_INCREMENT); @@ -40,65 +43,26 @@ void oggpack_writeinit(oggpack_buffer *b){ b->storage=BUFFER_INCREMENT; } +void oggpackB_writeinit(oggpack_buffer *b){ + oggpack_writeinit(b); +} + void oggpack_writetrunc(oggpack_buffer *b,long bits){ long bytes=bits>>3; bits-=bytes*8; b->ptr=b->buffer+bytes; b->endbit=bits; b->endbyte=bytes; - *b->ptr|=mask[bits]; + *b->ptr&=mask[bits]; } -void oggpack_writealign(oggpack_buffer *b){ - int bits=8-b->endbit; - if(bits<8) - oggpack_write(b,0,bits); -} - -void oggpack_writecopy(oggpack_buffer *b,void *source,long bits){ - unsigned char *ptr=(unsigned char *)source; - - long bytes=bits/8; +void oggpackB_writetrunc(oggpack_buffer *b,long bits){ + long bytes=bits>>3; bits-=bytes*8; - - if(b->endbit){ - int i; - /* unaligned copy. Do it the hard way. */ - for(i=0;iendbyte+bytes+1>=b->storage){ - b->storage=b->endbyte+bytes+BUFFER_INCREMENT; - b->buffer=_ogg_realloc(b->buffer,b->storage); - b->ptr=b->buffer+b->endbyte; - } - - memmove(b->ptr,source,bytes); - b->ptr+=bytes; - b->buffer+=bytes; - *b->ptr=0; - - } - if(bits) - oggpack_write(b,(long)(ptr[bytes]),bits); -} - -void oggpack_reset(oggpack_buffer *b){ - b->ptr=b->buffer; - b->buffer[0]=0; - b->endbit=b->endbyte=0; -} - -void oggpack_writeclear(oggpack_buffer *b){ - _ogg_free(b->buffer); - memset(b,0,sizeof(*b)); -} - -void oggpack_readinit(oggpack_buffer *b,unsigned char *buf,int bytes){ - memset(b,0,sizeof(*b)); - b->buffer=b->ptr=buf; - b->storage=bytes; + b->ptr=b->buffer+bytes; + b->endbit=bits; + b->endbyte=bytes; + *b->ptr&=mask8B[bits]; } /* Takes only up to 32 bits. */ @@ -135,6 +99,128 @@ void oggpack_write(oggpack_buffer *b,unsigned long value,int bits){ b->endbit=bits&7; } +/* Takes only up to 32 bits. */ +void oggpackB_write(oggpack_buffer *b,unsigned long value,int bits){ + if(b->endbyte+4>=b->storage){ + b->buffer=_ogg_realloc(b->buffer,b->storage+BUFFER_INCREMENT); + b->storage+=BUFFER_INCREMENT; + b->ptr=b->buffer+b->endbyte; + } + + value=(value&mask[bits])<<(32-bits); + bits+=b->endbit; + + b->ptr[0]|=value>>(24+b->endbit); + + if(bits>=8){ + b->ptr[1]=value>>(16+b->endbit); + if(bits>=16){ + b->ptr[2]=value>>(8+b->endbit); + if(bits>=24){ + b->ptr[3]=value>>(b->endbit); + if(bits>=32){ + if(b->endbit) + b->ptr[4]=value<<(8-b->endbit); + else + b->ptr[4]=0; + } + } + } + } + + b->endbyte+=bits/8; + b->ptr+=bits/8; + b->endbit=bits&7; +} + +void oggpack_writealign(oggpack_buffer *b){ + int bits=8-b->endbit; + if(bits<8) + oggpack_write(b,0,bits); +} + +void oggpackB_writealign(oggpack_buffer *b){ + int bits=8-b->endbit; + if(bits<8) + oggpackB_write(b,0,bits); +} + +static void oggpack_writecopy_helper(oggpack_buffer *b, + void *source, + long bits, + void (*w)(oggpack_buffer *, + unsigned long, + int), + int msb){ + unsigned char *ptr=(unsigned char *)source; + + long bytes=bits/8; + bits-=bytes*8; + + if(b->endbit){ + int i; + /* unaligned copy. Do it the hard way. */ + for(i=0;iendbyte+bytes+1>=b->storage){ + b->storage=b->endbyte+bytes+BUFFER_INCREMENT; + b->buffer=_ogg_realloc(b->buffer,b->storage); + b->ptr=b->buffer+b->endbyte; + } + + memmove(b->ptr,source,bytes); + b->ptr+=bytes; + b->buffer+=bytes; + *b->ptr=0; + + } + if(bits){ + if(msb) + w(b,(unsigned long)(ptr[bytes]>>(8-bits)),bits); + else + w(b,(unsigned long)(ptr[bytes]),bits); + } +} + +void oggpack_writecopy(oggpack_buffer *b,void *source,long bits){ + oggpack_writecopy_helper(b,source,bits,oggpack_write,0); +} + +void oggpackB_writecopy(oggpack_buffer *b,void *source,long bits){ + oggpack_writecopy_helper(b,source,bits,oggpackB_write,1); +} + +void oggpack_reset(oggpack_buffer *b){ + b->ptr=b->buffer; + b->buffer[0]=0; + b->endbit=b->endbyte=0; +} + +void oggpackB_reset(oggpack_buffer *b){ + oggpack_reset(b); +} + +void oggpack_writeclear(oggpack_buffer *b){ + _ogg_free(b->buffer); + memset(b,0,sizeof(*b)); +} + +void oggpackB_writeclear(oggpack_buffer *b){ + oggpack_writeclear(b); +} + +void oggpack_readinit(oggpack_buffer *b,unsigned char *buf,int bytes){ + memset(b,0,sizeof(*b)); + b->buffer=b->ptr=buf; + b->storage=bytes; +} + +void oggpackB_readinit(oggpack_buffer *b,unsigned char *buf,int bytes){ + oggpack_readinit(b,buf,bytes); +} + /* Read in bits without advancing the bitptr; bits <= 32 */ long oggpack_look(oggpack_buffer *b,int bits){ unsigned long ret; @@ -162,11 +248,43 @@ long oggpack_look(oggpack_buffer *b,int bits){ return(m&ret); } +/* Read in bits without advancing the bitptr; bits <= 32 */ +long oggpackB_look(oggpack_buffer *b,int bits){ + unsigned long ret; + int m=32-bits; + + bits+=b->endbit; + + if(b->endbyte+4>=b->storage){ + /* not the main path */ + if(b->endbyte*8+bits>b->storage*8)return(-1); + } + + ret=b->ptr[0]<<(24+b->endbit); + if(bits>8){ + ret|=b->ptr[1]<<(16+b->endbit); + if(bits>16){ + ret|=b->ptr[2]<<(8+b->endbit); + if(bits>24){ + ret|=b->ptr[3]<<(b->endbit); + if(bits>32 && b->endbit) + ret|=b->ptr[4]>>(8-b->endbit); + } + } + } + return (ret>>(m>>1))>>((m+1)>>1); +} + long oggpack_look1(oggpack_buffer *b){ if(b->endbyte>=b->storage)return(-1); return((b->ptr[0]>>b->endbit)&1); } +long oggpackB_look1(oggpack_buffer *b){ + if(b->endbyte>=b->storage)return(-1); + return((b->ptr[0]>>(7-b->endbit))&1); +} + void oggpack_adv(oggpack_buffer *b,int bits){ bits+=b->endbit; b->ptr+=bits/8; @@ -174,6 +292,10 @@ void oggpack_adv(oggpack_buffer *b,int bits){ b->endbit=bits&7; } +void oggpackB_adv(oggpack_buffer *b,int bits){ + oggpack_adv(b,bits); +} + void oggpack_adv1(oggpack_buffer *b){ if(++(b->endbit)>7){ b->endbit=0; @@ -182,6 +304,10 @@ void oggpack_adv1(oggpack_buffer *b){ } } +void oggpackB_adv1(oggpack_buffer *b){ + oggpack_adv1(b); +} + /* bits <= 32 */ long oggpack_read(oggpack_buffer *b,int bits){ unsigned long ret; @@ -218,6 +344,41 @@ long oggpack_read(oggpack_buffer *b,int bits){ return(ret); } +/* bits <= 32 */ +long oggpackB_read(oggpack_buffer *b,int bits){ + unsigned long ret; + long m=32-bits; + + bits+=b->endbit; + + if(b->endbyte+4>=b->storage){ + /* not the main path */ + ret=-1UL; + if(b->endbyte*8+bits>b->storage*8)goto overflow; + } + + ret=b->ptr[0]<<(24+b->endbit); + if(bits>8){ + ret|=b->ptr[1]<<(16+b->endbit); + if(bits>16){ + ret|=b->ptr[2]<<(8+b->endbit); + if(bits>24){ + ret|=b->ptr[3]<<(b->endbit); + if(bits>32 && b->endbit) + ret|=b->ptr[4]>>(8-b->endbit); + } + } + } + ret=(ret>>(m>>1))>>((m+1)>>1); + + overflow: + + b->ptr+=bits/8; + b->endbyte+=bits/8; + b->endbit=bits&7; + return(ret); +} + long oggpack_read1(oggpack_buffer *b){ unsigned long ret; @@ -240,6 +401,28 @@ long oggpack_read1(oggpack_buffer *b){ return(ret); } +long oggpackB_read1(oggpack_buffer *b){ + unsigned long ret; + + if(b->endbyte>=b->storage){ + /* not the main path */ + ret=-1UL; + goto overflow; + } + + ret=(b->ptr[0]>>(7-b->endbit))&1; + + overflow: + + b->endbit++; + if(b->endbit>7){ + b->endbit=0; + b->ptr++; + b->endbyte++; + } + return(ret); +} + long oggpack_bytes(oggpack_buffer *b){ return(b->endbyte+(b->endbit+7)/8); } @@ -248,10 +431,22 @@ long oggpack_bits(oggpack_buffer *b){ return(b->endbyte*8+b->endbit); } +long oggpackB_bytes(oggpack_buffer *b){ + return oggpack_bytes(b); +} + +long oggpackB_bits(oggpack_buffer *b){ + return oggpack_bits(b); +} + unsigned char *oggpack_get_buffer(oggpack_buffer *b){ return(b->buffer); } +unsigned char *oggpackB_get_buffer(oggpack_buffer *b){ + return oggpack_get_buffer(b); +} + /* Self test of the bitwise routines; everything else is based on them, so they damned well better be solid. */ @@ -310,6 +505,41 @@ void cliptest(unsigned long *b,int vals,int bits,int *comp,int compsize){ if(oggpack_bytes(&r)!=bytes)report("leftover bytes after read!\n"); } +void cliptestB(unsigned long *b,int vals,int bits,int *comp,int compsize){ + long bytes,i; + unsigned char *buffer; + + oggpackB_reset(&o); + for(i=0;i @@ -39,12 +40,14 @@ typedef _G_int32_t ogg_int32_t; typedef _G_uint32_t ogg_uint32_t; typedef _G_int16_t ogg_int16_t; + typedef _G_uint16_t ogg_uint16_t; # endif #elif defined(__MACOS__) # include typedef SInt16 ogg_int16_t; + typedef UInt16 ogg_uint16_t; typedef SInt32 ogg_int32_t; typedef UInt32 ogg_uint32_t; typedef SInt64 ogg_int64_t; @@ -53,6 +56,7 @@ # include typedef int16_t ogg_int16_t; + typedef u_int16_t ogg_uint16_t; typedef int32_t ogg_int32_t; typedef u_int32_t ogg_uint32_t; typedef int64_t ogg_int64_t; @@ -62,6 +66,7 @@ /* Be */ # include typedef int16_t ogg_int16_t; + typedef u_int16_t ogg_uint16_t; typedef int32_t ogg_int32_t; typedef u_int32_t ogg_uint32_t; typedef int64_t ogg_int64_t; @@ -70,10 +75,27 @@ /* OS/2 GCC */ typedef short ogg_int16_t; + typedef unsigned short ogg_uint16_t; typedef int ogg_int32_t; typedef unsigned int ogg_uint32_t; typedef long long ogg_int64_t; +#elif defined (DJGPP) + + /* DJGPP */ + typedef short ogg_int16_t; + typedef int ogg_int32_t; + typedef unsigned int ogg_uint32_t; + typedef long long ogg_int64_t; + +#elif defined(R5900) + + /* PS2 EE */ + typedef long ogg_int64_t; + typedef int ogg_int32_t; + typedef unsigned ogg_uint32_t; + typedef short ogg_int16_t; + #else # include