From: kramm Date: Tue, 4 Jun 2002 15:56:51 +0000 (+0000) Subject: made bitio visible from outside. X-Git-Tag: release-0-4-0~63 X-Git-Url: http://git.asbjorn.it/?a=commitdiff_plain;h=994d5705f3ab0c9c3bb57dcd9fa8bb2a5398de2c;p=swftools.git made bitio visible from outside. --- diff --git a/lib/Makefile.in b/lib/Makefile.in index 8a6b4e3..5c79cea 100644 --- a/lib/Makefile.in +++ b/lib/Makefile.in @@ -72,8 +72,8 @@ WORDS_BIGENDIAN = @WORDS_BIGENDIAN@ SUBDIRS = lame noinst_LIBRARIES = librfxswf.a -librfxswf_a_SOURCES = rfxswf.c rfxswf.h log.c log.h rfxswf.o -librfxswf_a_DEPENDENCIES = rfxswf.o rfxswf.h log.c log.h modules/swfbits.c modules/swfaction.c modules/swfdump.c modules/swfcgi.c modules/swfbutton.c modules/swftext.c modules/swftools.c modules/swfsound.c lame/psymodel.o lame/fft.o lame/newmdct.o lame/quantize.o lame/takehiro.o lame/reservoir.o lame/quantize_pvt.o lame/vbrquantize.o lame/encoder.o lame/id3tag.o lame/version.o lame/tables.o lame/util.o lame/bitstream.o lame/set_get.o lame/VbrTag.o lame/lame.o +librfxswf_a_SOURCES = bitio.c bitio.h rfxswf.c rfxswf.h log.c log.h rfxswf.o +librfxswf_a_DEPENDENCIES = bitio.c bitio.h rfxswf.o rfxswf.h log.c log.h modules/swfbits.c modules/swfaction.c modules/swfdump.c modules/swfcgi.c modules/swfbutton.c modules/swftext.c modules/swftools.c modules/swfsound.c lame/psymodel.o lame/fft.o lame/newmdct.o lame/quantize.o lame/takehiro.o lame/reservoir.o lame/quantize_pvt.o lame/vbrquantize.o lame/encoder.o lame/id3tag.o lame/version.o lame/tables.o lame/util.o lame/bitstream.o lame/set_get.o lame/VbrTag.o lame/lame.o librfxswf_a_LIBADD = lame/psymodel.o lame/fft.o lame/newmdct.o lame/quantize.o lame/takehiro.o lame/reservoir.o lame/quantize_pvt.o lame/vbrquantize.o lame/encoder.o lame/id3tag.o lame/version.o lame/tables.o lame/util.o lame/bitstream.o lame/set_get.o lame/VbrTag.o lame/lame.o @@ -90,7 +90,7 @@ DEFS = @DEFS@ -I. -I$(srcdir) -I.. CPPFLAGS = @CPPFLAGS@ LDFLAGS = @LDFLAGS@ LIBS = @LIBS@ -librfxswf_a_OBJECTS = rfxswf.o log.o +librfxswf_a_OBJECTS = bitio.o rfxswf.o log.o AR = ar CFLAGS = @CFLAGS@ CCLD = $(CC) @@ -341,7 +341,7 @@ uninstall-am uninstall all-redirect all-am all installdirs-am \ installdirs mostlyclean-generic distclean-generic clean-generic \ maintainer-clean-generic clean mostlyclean distclean maintainer-clean -rfxswf.o: rfxswf.c rfxswf.h modules/swfbits.c \ +rfxswf.o: rfxswf.c rfxswf.h bitio.c bitio.h modules/swfbits.c \ modules/swfaction.c modules/swfdump.c modules/swfcgi.c \ modules/swfbutton.c modules/swftext.c modules/swftools.c \ modules/swfsound.c diff --git a/lib/bitio.c b/lib/bitio.c index 60b752b..dde7b7c 100644 --- a/lib/bitio.c +++ b/lib/bitio.c @@ -1,37 +1,61 @@ -#define READER_TYPE_FILE 0 -#define READER_TYPE_MEM 1 -#define READER_TYPE_ZLIB 2 -#define WRITER_TYPE_FILE 0 -#define WRITER_TYPE_MEM 1 -#define WRITER_TYPE_ZLIB 2 - -struct reader_t +/* bitio.c + implementation of bitio.h. + + Part of the swftools package. + + Copyright (c) 2001 Matthias Kramm + + This file is distributed under the GPL, see file COPYING for details */#include + +#include +#include +#include +#include +#include "./bitio.h" + +#define ZLIB_BUFFER_SIZE 16384 + +struct memread_t { - int (*read)(struct reader_t*, void*data, int len); - void *internal; - int type; - unsigned char mybyte; - unsigned char bitpos; + unsigned char*data; + int pos; + int length; }; -struct writer_t + +struct memwrite_t { - int (*write)(struct writer_t*, void*data, int len); - void (*finish)(struct writer_t*); - void *internal; - int type; - unsigned char mybyte; - unsigned char bitpos; + unsigned char*data; + int pos; + int length; }; -static int reader_fileread(struct reader_t*reader, void* data, int len) + +struct zlibinflate_t { - return read((int)reader->internal, data, len); -} -static void reader_resetbits(struct reader_t*r) + z_stream zs; + struct reader_t*input; + unsigned char readbuffer[ZLIB_BUFFER_SIZE]; +}; + +struct zlibdeflate_t +{ + z_stream zs; + struct writer_t*output; + unsigned char writebuffer[ZLIB_BUFFER_SIZE]; +}; + +void reader_resetbits(struct reader_t*r) { r->mybyte = 0; r->bitpos = 8; + } -static void reader_init_filereader(struct reader_t*r, int handle) + +static int reader_zlibinflate(struct reader_t*reader, void* data, int len); +static int reader_fileread(struct reader_t*reader, void* data, int len); +static int reader_memread(struct reader_t*reader, void* data, int len); +static void zlib_error(int ret, char* msg, z_stream*zs); + +void reader_init_filereader(struct reader_t*r, int handle) { r->read = reader_fileread; r->internal = (void*)handle; @@ -39,24 +63,20 @@ static void reader_init_filereader(struct reader_t*r, int handle) r->mybyte = 0; r->bitpos = 8; } -#define ZLIB_BUFFER_SIZE 16384 -struct zlibinflate_t -{ - z_stream zs; - struct reader_t*input; - U8 readbuffer[ZLIB_BUFFER_SIZE]; -}; -static void zlib_error(int ret, char* msg, z_stream*zs) + +void reader_init_memreader(struct reader_t*r, void*newdata, int newlength) { - fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n", - msg, - ret, - zs->msg?zs->msg:"unknown"); - perror("errno:"); - exit(1); + struct memread_t*mr = malloc(sizeof(struct memread_t)); + mr->data = newdata; + mr->length = newlength; + r->read = reader_memread; + r->internal = (void*)mr; + r->type = READER_TYPE_MEM; + r->mybyte = 0; + r->bitpos = 8; } -static int reader_zlibinflate(struct reader_t*reader, void* data, int len); -static void reader_init_zlibinflate(struct reader_t*r, struct reader_t*input) + +void reader_init_zlibinflate(struct reader_t*r, struct reader_t*input) { struct zlibinflate_t*z; int ret; @@ -75,6 +95,37 @@ static void reader_init_zlibinflate(struct reader_t*r, struct reader_t*input) if (ret != Z_OK) zlib_error(ret, "bitio:inflate_init", &z->zs); reader_resetbits(r); } + +static void zlib_error(int ret, char* msg, z_stream*zs) +{ + fprintf(stderr, "%s: zlib error (%d): last zlib error: %s\n", + msg, + ret, + zs->msg?zs->msg:"unknown"); + perror("errno:"); + exit(1); +} + +static int reader_fileread(struct reader_t*reader, void* data, int len) +{ + return read((int)reader->internal, data, len); +} + +static int reader_memread(struct reader_t*reader, void* data, int len) +{ + struct memread_t*mr = (struct memread_t*)reader->internal; + + if(mr->length - mr->pos > len) { + memcpy(data, &mr->data[mr->pos], len); + mr->pos += len; + return len; + } else { + memcpy(data, &mr->data[mr->pos], mr->length - mr->pos); + mr->pos = mr->length; + return mr->length - mr->pos; + } +} + static int reader_zlibinflate(struct reader_t*reader, void* data, int len) { struct zlibinflate_t*z = (struct zlibinflate_t*)reader->internal; @@ -112,7 +163,7 @@ static int reader_zlibinflate(struct reader_t*reader, void* data, int len) } return len; } -static unsigned int reader_readbit(struct reader_t*r) +unsigned int reader_readbit(struct reader_t*r) { if(r->bitpos==8) { @@ -121,7 +172,7 @@ static unsigned int reader_readbit(struct reader_t*r) } return (r->mybyte>>(7-r->bitpos++))&1; } -static unsigned int reader_readbits(struct reader_t*r, int num) +unsigned int reader_readbits(struct reader_t*r, int num) { int t; int val = 0; @@ -132,6 +183,12 @@ static unsigned int reader_readbits(struct reader_t*r, int num) } return val; } + +static int writer_zlibdeflate_write(struct writer_t*writer, void* data, int len); +static void writer_zlibdeflate_finish(struct writer_t*writer); +static int writer_filewrite_write(struct writer_t*w, void* data, int len); +static void writer_filewrite_finish(struct writer_t*w); + static int writer_filewrite_write(struct writer_t*w, void* data, int len) { return write((int)w->internal, data, len); @@ -139,14 +196,33 @@ static int writer_filewrite_write(struct writer_t*w, void* data, int len) static void writer_filewrite_finish(struct writer_t*w) { } -static void writer_resetbits(struct writer_t*w) + +static int writer_memwrite_write(struct writer_t*w, void* data, int len) +{ + struct memread_t*mw = (struct memread_t*)w->internal; + if(mw->length - mw->pos > len) { + memcpy(&mw->data[mw->pos], data, len); + mw->pos += len; + return len; + } else { + memcpy(&mw->data[mw->pos], data, mw->length - mw->pos); + mw->pos = mw->length; + return mw->length - mw->pos; + } +} +static void writer_memwrite_finish(struct writer_t*w) +{ + free(w->internal); +} + +void writer_resetbits(struct writer_t*w) { if(w->bitpos) w->write(w, &w->mybyte, 1); w->bitpos = 0; w->mybyte = 0; } -static void writer_init_filewriter(struct writer_t*w, int handle) +void writer_init_filewriter(struct writer_t*w, int handle) { memset(w, 0, sizeof(struct writer_t)); w->write = writer_filewrite_write; @@ -156,15 +232,22 @@ static void writer_init_filewriter(struct writer_t*w, int handle) w->bitpos = 0; w->mybyte = 0; } -struct zlibdeflate_t +void writer_init_memwriter(struct writer_t*w, void*data, int len) { - z_stream zs; - struct writer_t*output; - U8 writebuffer[ZLIB_BUFFER_SIZE]; -}; -static int writer_zlibdeflate_write(struct writer_t*writer, void* data, int len); -static void writer_zlibdeflate_finish(struct writer_t*writer); -static void writer_init_zlibdeflate(struct writer_t*w, struct writer_t*output) + struct memwrite_t *mr; + mr = malloc(sizeof(struct memwrite_t)); + mr->data = data; + mr->length = len; + memset(w, 0, sizeof(struct writer_t)); + w->write = writer_memwrite_write; + w->finish = writer_memwrite_finish; + w->internal = (void*)mr; + w->type = WRITER_TYPE_FILE; + w->bitpos = 0; + w->mybyte = 0; +} + +void writer_init_zlibdeflate(struct writer_t*w, struct writer_t*output) { struct zlibdeflate_t*z; int ret; @@ -245,7 +328,7 @@ static void writer_zlibdeflate_finish(struct writer_t*writer) output->finish(output); } -static void writer_writebit(struct writer_t*w, int bit) +void writer_writebit(struct writer_t*w, int bit) { if(w->bitpos==8) { @@ -257,7 +340,7 @@ static void writer_writebit(struct writer_t*w, int bit) w->mybyte |= 1 << (7 - w->bitpos); w->bitpos ++; } -static void writer_writebits(struct writer_t*w, U32 data, int bits) +void writer_writebits(struct writer_t*w, unsigned int data, int bits) { int t; for(t=0;tframeCount = frameCount; } - if(compress) { + if(swf->compressed) { char*id = "CWS"; writer->write(writer, id, 3); } @@ -916,7 +916,7 @@ int swf_WriteSWF2(struct writer_t*writer, SWF * swf, bool compress) // Writ PUT32(b4, swf->fileSize); writer->write(writer, b4, 4); - if(compress) { + if(swf->compressed) { writer_init_zlibdeflate(&zwriter, writer); writer = &zwriter; } @@ -952,21 +952,30 @@ int swf_WriteSWF2(struct writer_t*writer, SWF * swf, bool compress) // Writ int swf_WriteSWF(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails { struct writer_t writer; + swf->compressed = 0; if(handle<0) - return swf_WriteSWF2(&writer, swf, FALSE); + return swf_WriteSWF2(&writer, swf); writer_init_filewriter(&writer, handle); - return swf_WriteSWF2(&writer, swf, FALSE); + return swf_WriteSWF2(&writer, swf); } int swf_WriteSWC(int handle, SWF * swf) // Writes SWF to file, returns length or <0 if fails { struct writer_t writer; + swf->compressed = 1; if(handle<0) - return swf_WriteSWF2(&writer, swf, TRUE); + return swf_WriteSWF2(&writer, swf); writer_init_filewriter(&writer, handle); - return swf_WriteSWF2(&writer, swf, TRUE); + return swf_WriteSWF2(&writer, swf); } +int swf_WriteHeader2(struct writer_t*writer,SWF * swf) +{ + SWF myswf; + memcpy(&myswf,swf,sizeof(SWF)); + myswf.firstTag = 0; + return swf_WriteSWF2(writer, &myswf); +} int swf_WriteHeader(int handle,SWF * swf) { diff --git a/lib/rfxswf.h b/lib/rfxswf.h index c5c26d5..4c1e7ad 100644 --- a/lib/rfxswf.h +++ b/lib/rfxswf.h @@ -20,6 +20,7 @@ #include #include #include "../config.h" +#include "./bitio.h" #define LAME #include "lame/lame.h" @@ -157,15 +158,21 @@ typedef struct _SWF // Basic Functions +int swf_ReadSWF2(struct reader_t*reader, SWF * swf); // Reads SWF via callback int swf_ReadSWF(int handle,SWF * swf); // Reads SWF to memory (malloc'ed), returns length or <0 if fails +int swf_WriteSWF2(struct writer_t*writer, SWF * swf); // Writes SWF via callback, returns length or <0 if fails int swf_WriteSWF(int handle,SWF * swf); // Writes SWF to file, returns length or <0 if fails -int swf_WriteSWC(int handle, SWF * swf); // Write Compressed SWF to file, returns length or <0 if fails +int swf_WriteSWC(int handle, SWF * swf); // for convenience, equal to swf->compressed=1;swf_WriteSWF(..) int swf_WriteCGI(SWF * swf); // Outputs SWF with valid CGI header to stdout void swf_FreeTags(SWF * swf); // Frees all malloc'ed memory for swf // for streaming: int swf_WriteHeader(int handle,SWF * swf); // Writes Header of swf to file +int swf_WriteHeader2(struct writer_t*writer,SWF * swf); // Writes Header of swf to file int swf_WriteTag(int handle,TAG * tag); // Writes TAG to file +int swf_WriteTag2(struct writer_t*writer, TAG * t); //Write TAG via callback + +int swf_ReadHeader(struct reader_t*reader, SWF * swf); // Reads SWF Header via callback // folding/unfolding: @@ -613,6 +620,7 @@ U8 swf_isDefiningTag(TAG * t); U8 swf_isPseudoDefiningTag(TAG * t); U8 swf_isAllowedSpriteTag(TAG * t); U16 swf_GetDefineID(TAG * t); +void swf_SetDefineID(TAG * t, U16 newid); U16 swf_GetPlaceID(TAG * t); //PLACEOBJECT, PLACEOBJECT2 (sometimes), REMOVEOBJECT U16 swf_GetDepth(TAG * t); //PLACEOBJECT,PLACEOBJECT2,REMOVEOBJECT,REMOVEOBJECT2 char* swf_GetName(TAG * t); //PLACEOBJECT2, FRAMELABEL @@ -621,6 +629,7 @@ MATRIX * swf_MatrixMapTriangle(MATRIX * m,int dx,int dy, int x0,int y0,int x1,int y1,int x2,int y2); int swf_GetNumUsedIDs(TAG * t); void swf_GetUsedIDs(TAG * t, int * positions); +void swf_Relocate(SWF*swf, char*bitmap); // bitmap is 65536 bytes, bitmap[a]==0 means id a is free // swfcgi.c