[Mndiff-dev] [mndiff]: r23 - trunk/mnzip/mnzip.c
michael
subversion at mplayerhq.hu
Fri Jun 15 03:29:25 CEST 2007
Author: michael
Date: Fri Jun 15 03:29:25 2007
New Revision: 23
Log:
remove dependancy to libav*
Modified:
trunk/mnzip/mnzip.c
Modified: trunk/mnzip/mnzip.c
==============================================================================
--- trunk/mnzip/mnzip.c (original)
+++ trunk/mnzip/mnzip.c Fri Jun 15 03:29:25 2007
@@ -24,9 +24,6 @@
#include <limits.h>
#include <string.h>
#include <assert.h>
-#include "common.h" //for av_log2 FIXME avoid this
-#define attribute_unused
-#include "rangecoder.h"
#undef NDEBUG
@@ -37,6 +34,131 @@
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) > (b) ? (b) : (a))
+#define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
+
+//---------------- Range coder taken from libavcodec copied here to avoid a nasty dependancy
+typedef struct RangeCoder{
+ int low;
+ int range;
+ int outstanding_count;
+ int outstanding_byte;
+ uint8_t *bytestream_start;
+ uint8_t *bytestream;
+ uint8_t *bytestream_end;
+}RangeCoder;
+
+//void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size);
+//void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size);
+//int ff_rac_terminate(RangeCoder *c);
+
+static inline void renorm_encoder(RangeCoder *c){
+ //FIXME optimize
+ while(c->range < 0x100){
+ if(c->outstanding_byte < 0){
+ c->outstanding_byte= c->low>>8;
+ }else if(c->low <= 0xFF00){
+ *c->bytestream++ = c->outstanding_byte;
+ for(;c->outstanding_count; c->outstanding_count--)
+ *c->bytestream++ = 0xFF;
+ c->outstanding_byte= c->low>>8;
+ }else if(c->low >= 0x10000){
+ *c->bytestream++ = c->outstanding_byte + 1;
+ for(;c->outstanding_count; c->outstanding_count--)
+ *c->bytestream++ = 0x00;
+ c->outstanding_byte= (c->low>>8) & 0xFF;
+ }else{
+ c->outstanding_count++;
+ }
+
+ c->low = (c->low & 0xFF)<<8;
+ c->range <<= 8;
+ }
+}
+
+static inline void put_rac(RangeCoder *c, uint8_t * const state, int bit){
+ int range1= (c->range * (*state)) >> 8;
+
+ assert(*state);
+ assert(range1 < c->range);
+ assert(range1 > 0);
+ if(!bit){
+ c->range -= range1;
+// *state= c->zero_state[*state];
+ }else{
+ c->low += c->range - range1;
+ c->range = range1;
+// *state= c->one_state[*state];
+ }
+
+ renorm_encoder(c);
+}
+
+static inline void refill(RangeCoder *c){
+ if(c->range < 0x100){
+ c->range <<= 8;
+ c->low <<= 8;
+ if(c->bytestream < c->bytestream_end)
+ c->low+= c->bytestream[0];
+ c->bytestream++;
+ }
+}
+
+static inline int get_rac(RangeCoder *c, uint8_t * const state){
+ int range1= (c->range * (*state)) >> 8;
+
+ c->range -= range1;
+
+ if(c->low < c->range){
+// *state= c->zero_state[*state];
+ refill(c);
+ return 0;
+ }else{
+ c->low -= c->range;
+// *state= c->one_state[*state];
+ c->range = range1;
+ refill(c);
+ return 1;
+ }
+}
+
+void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size){
+ c->bytestream_start=
+ c->bytestream= buf;
+ c->bytestream_end= buf + buf_size;
+
+ c->low= 0;
+ c->range= 0xFF00;
+ c->outstanding_count= 0;
+ c->outstanding_byte= -1;
+}
+
+void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size){
+ /* cast to avoid compiler warning */
+ ff_init_range_encoder(c, (uint8_t *) buf, buf_size);
+
+ c->low = (c->bytestream[0]<<8) + c->bytestream[1];
+ c->bytestream+=2;
+}
+
+/**
+ *
+ * @return the number of bytes written
+ */
+int ff_rac_terminate(RangeCoder *c){
+ c->range=0xFF;
+ c->low +=0xFF;
+ renorm_encoder(c);
+ c->range=0xFF;
+ renorm_encoder(c);
+
+ assert(c->low == 0);
+ assert(c->range >= 0x100);
+
+ return c->bytestream - c->bytestream_start;
+}
+
+//--------------------------------------------------
+
static void qsort2(uint8_t **ptr, int *idx, int len){
int pivot;
@@ -531,7 +653,6 @@ fprintf(stderr,"range coding (%d %d)\n",
mtf[i]= i;
ff_init_range_encoder(&c, out, len);
- ff_build_rac_states(&c, 0.04*(1LL<<32), 256-4); //FIXME shouldnt be needed
for(i=len-1; i>=0; i--){
int v= tmp[i];
@@ -606,7 +727,6 @@ fprintf(stderr," block (%d %d %d)\n", ou
memset(state, 0, sizeof(state));
ff_init_range_decoder(&c, in, in_len);
- ff_build_rac_states(&c, 0.04*(1LL<<32), 256-4);
for(i=0; i<256; i++){
int test[256]={0};
More information about the Mndiff-dev
mailing list