diff --git a/build/VS2010/HEVC_encoder.vcxproj b/build/VS2010/HEVC_encoder.vcxproj
index cbbd3e8f..6622f69f 100644
--- a/build/VS2010/HEVC_encoder.vcxproj
+++ b/build/VS2010/HEVC_encoder.vcxproj
@@ -83,6 +83,7 @@
+
@@ -90,6 +91,7 @@
+
diff --git a/build/VS2010/HEVC_encoder.vcxproj.filters b/build/VS2010/HEVC_encoder.vcxproj.filters
index fc706a68..dce2d423 100644
--- a/build/VS2010/HEVC_encoder.vcxproj.filters
+++ b/build/VS2010/HEVC_encoder.vcxproj.filters
@@ -30,6 +30,9 @@
Source Files
+
+ Source Files
+
@@ -47,5 +50,8 @@
Header Files
+
+ Header Files
+
\ No newline at end of file
diff --git a/src/bitstream.c b/src/bitstream.c
index f67041f5..32c697bc 100644
--- a/src/bitstream.c
+++ b/src/bitstream.c
@@ -31,6 +31,39 @@ void printf_bitstream(char *msg, ...)
printf("%s",buffer);
}
#endif
+
+bitTable *exp_table;
+
+//From wikipedia
+//http://en.wikipedia.org/wiki/Binary_logarithm#Algorithm
+int floorLog2(unsigned int n) {
+ int pos = 0;
+ if (n >= 1<<16) { n >>= 16; pos += 16; }
+ if (n >= 1<< 8) { n >>= 8; pos += 8; }
+ if (n >= 1<< 4) { n >>= 4; pos += 4; }
+ if (n >= 1<< 2) { n >>= 2; pos += 2; }
+ if (n >= 1<< 1) { pos += 1; }
+ return ((n == 0) ? (-1) : pos);
+}
+
+//Initialize the Exp Golomb code table with desired number of values
+void init_exp_golomb(uint32_t len)
+{
+ uint32_t code_num;
+ uint32_t M;
+ uint32_t info;
+ exp_table=(bitTable*)malloc(len*sizeof(bitTable));
+
+ for(code_num=0;code_numcur_byte=0;
stream->cur_bit=0;
+ stream->buffer_pos = 0;
memset(stream->data, 0, sizeof(uint32_t)*32);
}
+
+/*
+ * Allocate buffer
+ */
+void bitstream_alloc(bitstream* stream, uint32_t alloc)
+{
+ stream->buffer = (uint8_t*)malloc(alloc);
+ //Clear just to be sure
+ memset(stream->buffer,0,alloc);
+ stream->buffer_pos = 0;
+}
/*
@@ -108,25 +153,55 @@ void bitstream_put(bitstream* stream, uint32_t data, uint8_t bits)
*/
void bitstream_align(bitstream* stream)
{
- if(stream->cur_byte==32)
- {
- //Stream flushed, zero out the values
- bitstream_init(stream);
- }
- else
- {
- stream->cur_byte++;
- }
+ if(stream->cur_bit&7 != 0)
+ {
+ bitstream_put(stream,0, 8-stream->cur_bit&7);
+ }
}
void bitstream_flush(bitstream* stream)
{
+ /*
+ * SAVE DATA TO OUTPUT
+ */
+ if(stream->output)
+ {
+ if(stream->cur_byte)
+ {
+ fwrite(stream->data, stream->cur_byte*4, 1, stream->output);
+ }
- /*
- * SAVE DATA TO OUTPUT
- */
-
+ if(stream->cur_bit>>3)
+ {
+ fwrite(&stream->data[stream->cur_byte], stream->cur_bit>>3, 1, stream->output);
+ }
+ }
+ else
+ {
+ if(stream->cur_byte)
+ {
+ memcpy(&stream->buffer[stream->buffer_pos],&stream->data[0],stream->cur_byte*4);
+ stream->buffer_pos = stream->cur_byte*4;
+ }
+
+ if(stream->cur_bit>>3)
+ {
+ memcpy(&stream->buffer[stream->buffer_pos],&stream->data[stream->cur_byte],stream->cur_bit>>3);
+ stream->buffer_pos = stream->cur_bit>>3;
+ }
+ }
//Stream flushed, zero out the values
bitstream_init(stream);
}
-
\ No newline at end of file
+
+/*
+void bitstream_put_ue(bitstream* stream, uint32_t data)
+{
+ bitstream_put(stream,exp_table[data].value,exp_table[data].len);
+}
+void bitstream_put_se(bitstream* stream, uint32_t data)
+{
+ uint32_t index=(data<=0)?2*(uint32_t)(-data):2*(uint32_t)(data)-1;
+ bitstream_put(stream,exp_table[index].value,exp_table[index].len);
+}
+*/
\ No newline at end of file
diff --git a/src/bitstream.h b/src/bitstream.h
index 53377137..f72101cb 100644
--- a/src/bitstream.h
+++ b/src/bitstream.h
@@ -15,14 +15,43 @@ typedef struct
uint8_t cur_byte;
uint8_t cur_bit;
FILE* output;
+ uint8_t* buffer;
+ uint32_t buffer_pos;
} bitstream;
+
+typedef struct
+{
+ uint8_t len;
+ uint32_t value;
+}bitTable;
+
+extern bitTable *exp_table;
-void bitstream_init(bitstream* stream);
-
-void bitstream_put(bitstream* stream, uint32_t data, uint8_t bits);
-
-void bitstream_align(bitstream* stream);
-
+void bitstream_alloc(bitstream* stream, uint32_t alloc);
+void bitstream_init(bitstream* stream);
+void bitstream_put(bitstream* stream, uint32_t data, uint8_t bits);
+/*
+void bitstream_put_ue(bitstream* stream, uint32_t data);
+void bitstream_put_se(bitstream* stream, uint32_t data);
+*/
+#define bitstream_put_ue(stream, data) { bitstream_put(stream,exp_table[data].value,exp_table[data].len); }
+#define bitstream_put_se(stream, data) { uint32_t index=(data<=0)?2*(uint32_t)(-data):2*(uint32_t)(data)-1; \
+ bitstream_put(stream,exp_table[index].value,exp_table[index].len); }
+
+void bitstream_align(bitstream* stream);
void bitstream_flush(bitstream* stream);
+void init_exp_golomb(uint32_t len);
+
+#ifdef _DEBUG
+static int WRITE_VALUE = 0;
+#define WRITE_U(stream, data, bits, name) { printf("%8d %40s u(%d) : %d\n",WRITE_VALUE, name,bits,data); bitstream_put(stream,data,bits); WRITE_VALUE++;}
+#define WRITE_UE(stream, data, name) { printf("%8d %40s ue(v) : %d\n",WRITE_VALUE, name,data); bitstream_put_ue(stream,data); WRITE_VALUE++;}
+#define WRITE_SE(stream, data, name) { printf("%8d %40s se(v) : %d\n",WRITE_VALUE, name,data); bitstream_put_se(stream,data); WRITE_VALUE++;}
+#else
+#define WRITE_U(stream, data, bits, name) { bitstream_put(stream,data,bits); }
+#define WRITE_UE(stream, data, name) { bitstream_put_ue(stream,data); }
+#define WRITE_SE(stream, data, name) { bitstream_put_se(stream,data); }
+#endif
+
#endif
\ No newline at end of file
diff --git a/src/cabac.c b/src/cabac.c
index d1548626..239348e2 100644
--- a/src/cabac.c
+++ b/src/cabac.c
@@ -122,8 +122,7 @@ void cabac_start(cabac_data* data)
data->bufferedByte = 0xff;
}
-#define CTX_STATE(ctx) (ctx.ucState>>1)
-#define CTX_MPS(ctx) (ctx.ucState&1)
+
void cabac_encodeBin(cabac_data* data, uint32_t binValue )
{
@@ -193,8 +192,8 @@ void cabac_write(cabac_data* data)
{
data->numBufferedBytes = 1;
data->bufferedByte = leadByte;
- }
- }
+ }
+ }
}
void cabac_encodeFlush(cabac_data* data, uint8_t end )
@@ -239,7 +238,7 @@ void cabac_finish(cabac_data* data)
{
bitstream_put(data->stream, 0xff, 8 );
data->numBufferedBytes--;
- }
+ }
}
bitstream_put(data->stream, data->uiLow >> 8, 24 - data->bitsLeft );
}
@@ -268,7 +267,7 @@ void cabac_encodeBinTrm(cabac_data* data, uint32_t binValue )
{
data->uiLow <<= 1;
data->uiRange <<= 1;
- data->bitsLeft--;
+ data->bitsLeft--;
}
if(data->bitsLeft < 12)
@@ -310,7 +309,7 @@ void cabac_encodeBinsEP(cabac_data* data, uint32_t binValues, int numBins )
while ( numBins > 8 )
{
numBins -= 8;
- pattern = binValues >> numBins;
+ pattern = binValues >> numBins;
data->uiLow <<= 8;
data->uiLow += data->uiRange * pattern;
binValues -= pattern << numBins;
diff --git a/src/cabac.h b/src/cabac.h
index 34717ef8..691eda6f 100644
--- a/src/cabac.h
+++ b/src/cabac.h
@@ -30,6 +30,8 @@ typedef struct
uint8_t ucState;
uint32_t binsCoded;
} cabac_ctx;
+#define CTX_STATE(ctx) (ctx.ucState>>1)
+#define CTX_MPS(ctx) (ctx.ucState&1)
void cxt_init(cabac_ctx* ctx,uint32_t qp, uint32_t initValue );
void cxt_buildNextStateTable();
diff --git a/src/config.h b/src/config.h
index 3141b665..1fe78644 100644
--- a/src/config.h
+++ b/src/config.h
@@ -19,12 +19,12 @@
*/
typedef struct
{
- char *input; /*!< \brief Pointer to input filename */
- char *output;/*!< \brief Pointer to output filename */
- char *debug; /*!< \brief Pointer to debug output */
- int frames; /*!< \brief Number of frames to decode */
- int width; /*!< \brief frame width */
- int height; /*!< \brief frame height */
+ char *input; /*!< \brief Pointer to input filename */
+ char *output; /*!< \brief Pointer to output filename */
+ char *debug; /*!< \brief Pointer to debug output */
+ uint32_t frames; /*!< \brief Number of frames to decode */
+ uint32_t width; /*!< \brief frame width */
+ uint32_t height; /*!< \brief frame height */
} config;
/* Function definitions */
diff --git a/src/encmain.c b/src/encmain.c
index e165b72b..934e40ab 100644
--- a/src/encmain.c
+++ b/src/encmain.c
@@ -42,6 +42,7 @@
#include "config.h"
#include "encoder.h"
#include "cabac.h"
+ #include "picture.h"
/*!
@@ -52,7 +53,7 @@
*/
int main(int argc, char* argv[])
{
-
+ uint32_t curFrame = 0;
config *cfg = NULL; /* Global configuration */
FILE *input = NULL;
FILE *output = NULL;
@@ -64,10 +65,10 @@
/* If problem with configuration, shutdown */
if(!config_init(cfg) || !config_read(cfg,argc,argv))
{
- fprintf(stderr, "/////////////////////////////////////////////////\r\n");
- fprintf(stderr, "// HEVC Encoder v. " VERSION_STRING "//\r\n");
- fprintf(stderr, "// Tampere University of Technology 2012 //\r\n");
- fprintf(stderr, "/////////////////////////////////////////////////\r\n\r\n");
+ fprintf(stderr, "/***********************************************/\r\n");
+ fprintf(stderr, " * HEVC Encoder v. " VERSION_STRING "*\r\n");
+ fprintf(stderr, " * Tampere University of Technology 2012 *\r\n");
+ fprintf(stderr, "/***********************************************/\r\n\r\n");
fprintf(stderr, "Usage:\r\n");
fprintf(stderr, "encmain -i -w -h -o