2014-01-24 10:37:15 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* This file is part of Kvazaar HEVC encoder.
|
2014-02-21 12:47:52 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2014 Tampere University of Technology and others (see
|
2014-01-24 10:37:15 +00:00
|
|
|
* COPYING file).
|
|
|
|
*
|
|
|
|
* Kvazaar is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as published
|
|
|
|
* by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* Kvazaar is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Kvazaar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* \file
|
2012-06-04 10:47:12 +00:00
|
|
|
*/
|
|
|
|
|
2013-09-18 09:16:03 +00:00
|
|
|
#include "bitstream.h"
|
|
|
|
|
2012-05-30 12:37:42 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
2014-04-14 06:11:31 +00:00
|
|
|
#include <stdlib.h>
|
2014-04-23 08:18:18 +00:00
|
|
|
#include <assert.h>
|
2013-09-18 12:45:46 +00:00
|
|
|
//for hton
|
2013-09-23 14:32:07 +00:00
|
|
|
#ifdef _WIN32
|
2012-06-06 08:59:02 +00:00
|
|
|
#include <Winsock2.h>
|
|
|
|
#else
|
2014-02-21 12:47:52 +00:00
|
|
|
#include <arpa/inet.h>
|
2012-06-06 08:59:02 +00:00
|
|
|
#endif
|
2014-02-21 12:47:52 +00:00
|
|
|
|
2014-03-24 13:31:15 +00:00
|
|
|
const uint32_t bit_set_mask[] =
|
|
|
|
{
|
|
|
|
0x00000001,0x00000002,0x00000004,0x00000008,
|
|
|
|
0x00000010,0x00000020,0x00000040,0x00000080,
|
|
|
|
0x00000100,0x00000200,0x00000400,0x00000800,
|
|
|
|
0x00001000,0x00002000,0x00004000,0x00008000,
|
|
|
|
0x00010000,0x00020000,0x00040000,0x00080000,
|
|
|
|
0x00100000,0x00200000,0x00400000,0x00800000,
|
|
|
|
0x01000000,0x02000000,0x04000000,0x08000000,
|
|
|
|
0x10000000,0x20000000,0x40000000,0x80000000
|
|
|
|
};
|
|
|
|
|
2012-06-05 14:45:17 +00:00
|
|
|
|
|
|
|
//#define VERBOSE
|
|
|
|
|
2012-06-01 12:31:06 +00:00
|
|
|
#ifdef VERBOSE
|
2012-05-30 12:37:42 +00:00
|
|
|
void printf_bitstream(char *msg, ...)
|
2014-02-21 12:47:52 +00:00
|
|
|
{
|
2012-06-01 12:31:06 +00:00
|
|
|
va_list fmtargs;
|
2014-02-21 12:47:52 +00:00
|
|
|
char buffer[1024];
|
2012-05-30 12:37:42 +00:00
|
|
|
va_start(fmtargs,msg);
|
|
|
|
vsnprintf(buffer,sizeof(buffer)-1,msg,fmtargs);
|
|
|
|
va_end(fmtargs);
|
2013-09-18 12:45:46 +00:00
|
|
|
printf("%s",buffer);
|
2014-02-21 12:47:52 +00:00
|
|
|
}
|
2012-06-01 12:31:06 +00:00
|
|
|
#endif
|
2012-06-05 11:01:47 +00:00
|
|
|
|
2014-04-15 12:37:16 +00:00
|
|
|
const bit_table *g_exp_table;
|
2012-06-05 11:01:47 +00:00
|
|
|
|
|
|
|
//From wikipedia
|
|
|
|
//http://en.wikipedia.org/wiki/Binary_logarithm#Algorithm
|
2014-04-17 05:39:57 +00:00
|
|
|
static int floor_log2(unsigned int n) {
|
2012-06-05 11:01:47 +00:00
|
|
|
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; }
|
2014-02-21 12:47:52 +00:00
|
|
|
return ((n == 0) ? (-1) : pos);
|
2012-06-05 11:01:47 +00:00
|
|
|
}
|
|
|
|
|
2013-09-18 12:45:46 +00:00
|
|
|
/**
|
|
|
|
* \brief Initialize the Exp Golomb code table with desired number of values
|
|
|
|
* \param len table length to init
|
2014-01-31 18:34:50 +00:00
|
|
|
* \return 1 on success, 0 on failure
|
|
|
|
*
|
2013-09-18 12:45:46 +00:00
|
|
|
* Allocates g_exp_table with len*sizeof(bit_table) and fills it with exponential golomb codes
|
|
|
|
*/
|
2014-04-17 05:39:57 +00:00
|
|
|
int init_exp_golomb(const uint32_t len)
|
2012-06-05 11:01:47 +00:00
|
|
|
{
|
2013-09-18 12:45:46 +00:00
|
|
|
uint32_t code_num;
|
2014-02-21 09:57:25 +00:00
|
|
|
uint8_t M;
|
2013-09-18 12:45:46 +00:00
|
|
|
uint32_t info;
|
2014-04-17 05:39:57 +00:00
|
|
|
bit_table *exp_table;
|
2014-04-15 12:37:16 +00:00
|
|
|
exp_table = (bit_table*)malloc(len*sizeof(bit_table));
|
|
|
|
if(!exp_table)
|
2014-01-31 18:34:50 +00:00
|
|
|
return 0;
|
2013-09-18 12:45:46 +00:00
|
|
|
|
|
|
|
for (code_num = 0; code_num < len; code_num++) {
|
2014-02-21 09:57:25 +00:00
|
|
|
M = (uint8_t)floor_log2(code_num + 1);
|
2013-09-18 12:45:46 +00:00
|
|
|
info = code_num + 1 - (uint32_t)pow(2, M);
|
2014-04-15 12:37:16 +00:00
|
|
|
exp_table[code_num].len = M * 2 + 1;
|
|
|
|
exp_table[code_num].value = (1<<M) | info;
|
2013-09-18 12:45:46 +00:00
|
|
|
}
|
2014-04-15 12:37:16 +00:00
|
|
|
|
|
|
|
g_exp_table = exp_table;
|
2014-01-31 18:34:50 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-04 08:04:24 +00:00
|
|
|
/**
|
|
|
|
* \brief Free Exp Golomb tables
|
|
|
|
*/
|
2014-04-07 04:53:32 +00:00
|
|
|
void free_exp_golomb()
|
2014-04-04 08:04:24 +00:00
|
|
|
{
|
|
|
|
FREE_POINTER(g_exp_table);
|
|
|
|
}
|
|
|
|
|
2014-01-31 18:34:50 +00:00
|
|
|
/**
|
2014-04-17 06:34:56 +00:00
|
|
|
* \brief Initialize a new bitstream
|
2014-01-31 18:34:50 +00:00
|
|
|
*/
|
2014-04-17 06:34:56 +00:00
|
|
|
int bitstream_init(bitstream * const stream, const bitstream_type type) {
|
|
|
|
switch (type) {
|
|
|
|
case BITSTREAM_TYPE_MEMORY:
|
|
|
|
stream->mem.allocated_length = 0;
|
|
|
|
stream->mem.output_data = NULL;
|
|
|
|
stream->mem.output_length = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITSTREAM_TYPE_FILE:
|
|
|
|
stream->file.output = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unknown type for bitstream!\n");
|
|
|
|
return 0;
|
2014-04-14 06:11:31 +00:00
|
|
|
}
|
2014-04-14 08:23:09 +00:00
|
|
|
|
2014-04-17 06:34:56 +00:00
|
|
|
stream->base.cur_bit = 0;
|
|
|
|
stream->base.data = 0;
|
|
|
|
stream->base.zerocount = 0;
|
|
|
|
stream->base.type = type;
|
2014-04-14 08:23:09 +00:00
|
|
|
|
2014-04-17 06:34:56 +00:00
|
|
|
return 1;
|
2014-02-21 12:47:52 +00:00
|
|
|
}
|
|
|
|
|
2014-04-14 06:11:31 +00:00
|
|
|
/**
|
2014-04-17 06:34:56 +00:00
|
|
|
* \brief Finalize bitstream internal structures
|
2014-04-14 06:11:31 +00:00
|
|
|
*/
|
2014-04-17 06:34:56 +00:00
|
|
|
|
|
|
|
int bitstream_finalize(bitstream * const stream) {
|
|
|
|
switch (stream->base.type) {
|
|
|
|
case BITSTREAM_TYPE_MEMORY:
|
|
|
|
FREE_POINTER(stream->mem.output_data);
|
|
|
|
stream->mem.allocated_length = 0;
|
|
|
|
stream->mem.output_length = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITSTREAM_TYPE_FILE:
|
|
|
|
//FIXME: if we fix create_bitstream, we would maybe have to do something here
|
|
|
|
stream->file.output = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unknown type for bitstream!\n");
|
|
|
|
return 0;
|
2014-04-14 06:11:31 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 06:34:56 +00:00
|
|
|
return 1;
|
2014-04-14 06:11:31 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 06:34:56 +00:00
|
|
|
|
2014-04-14 06:11:31 +00:00
|
|
|
/**
|
|
|
|
* \brief Write a byte to bitstream
|
2014-04-17 06:34:56 +00:00
|
|
|
* \param stream pointer bitstream to put the data
|
2014-04-14 06:11:31 +00:00
|
|
|
* \param byte byte to write
|
|
|
|
* \return 1 on success, 0 on failure
|
|
|
|
*/
|
2014-04-17 06:34:56 +00:00
|
|
|
int bitstream_writebyte(bitstream * const stream, const uint8_t byte) {
|
|
|
|
switch (stream->base.type) {
|
|
|
|
case BITSTREAM_TYPE_FILE:
|
|
|
|
if (fwrite(&byte, 1, 1, stream->file.output) != 1) {
|
|
|
|
fprintf(stderr, "Could not write byte to bitstream_file object.");
|
2014-04-14 06:11:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-04-17 06:34:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BITSTREAM_TYPE_MEMORY:
|
|
|
|
if (stream->mem.allocated_length==stream->mem.output_length) {
|
|
|
|
//Need to reallocate
|
|
|
|
uint32_t new_size = stream->mem.allocated_length + BITSTREAM_MEMORY_CHUNK_SIZE;
|
|
|
|
uint8_t* new_data = realloc(stream->mem.output_data, new_size);
|
|
|
|
if (!new_data) {
|
|
|
|
fprintf(stderr, "Failed to allocate memory for bitstream_mem object");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
stream->mem.output_data = new_data;
|
|
|
|
stream->mem.allocated_length = new_size;
|
|
|
|
}
|
|
|
|
//Write byte
|
|
|
|
stream->mem.output_data[stream->mem.output_length++] = byte;
|
2014-04-23 08:18:18 +00:00
|
|
|
break;
|
2014-04-17 06:34:56 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unknown stream type!\n");
|
2014-04-23 08:18:18 +00:00
|
|
|
assert(0);
|
2014-04-17 06:34:56 +00:00
|
|
|
return 0;
|
2014-04-14 06:11:31 +00:00
|
|
|
}
|
2014-04-17 06:34:56 +00:00
|
|
|
return 1;
|
2014-04-14 06:11:31 +00:00
|
|
|
}
|
|
|
|
|
2014-05-05 06:21:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Get the bit position in bitstream
|
|
|
|
* \param stream pointer bitstream
|
|
|
|
* \return position
|
|
|
|
*/
|
|
|
|
long long unsigned int bitstream_tell(const bitstream * const stream) {
|
|
|
|
long long unsigned int position;
|
|
|
|
|
|
|
|
switch (stream->base.type) {
|
|
|
|
case BITSTREAM_TYPE_FILE:
|
|
|
|
position = ftell(stream->file.output);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BITSTREAM_TYPE_MEMORY:
|
|
|
|
position = stream->mem.output_length;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unknown stream type!\n");
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return position*8 + stream->base.cur_bit;
|
|
|
|
}
|
|
|
|
|
2014-04-23 08:19:11 +00:00
|
|
|
int bitstream_append(bitstream * const dst, const bitstream * const src) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
switch(src->base.type) {
|
|
|
|
case BITSTREAM_TYPE_MEMORY:
|
|
|
|
for (i = 0; i < src->mem.output_length; ++i) {
|
|
|
|
bitstream_writebyte(dst, src->mem.output_data[i]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unsupported source stream type for bitstream_append!\n");
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bitstream_clear(bitstream * const bitstream) {
|
|
|
|
switch(bitstream->base.type) {
|
|
|
|
case BITSTREAM_TYPE_MEMORY:
|
|
|
|
bitstream->mem.output_length = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unsupported stream type for bitstream_clear!\n");
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-09-18 12:45:46 +00:00
|
|
|
/**
|
2014-02-21 12:47:52 +00:00
|
|
|
* \brief Put bits to bitstream
|
2013-09-18 12:45:46 +00:00
|
|
|
* \param stream pointer bitstream to put the data
|
|
|
|
* \param data input data
|
|
|
|
* \param bits number of bits to write from data to stream
|
2014-02-21 12:47:52 +00:00
|
|
|
*/
|
2014-04-17 05:39:57 +00:00
|
|
|
void bitstream_put(bitstream * const stream, const uint32_t data, uint8_t bits)
|
2012-05-30 12:37:42 +00:00
|
|
|
{
|
2014-03-24 13:31:15 +00:00
|
|
|
const uint8_t emulation_prevention_three_byte = 0x03;
|
|
|
|
while(bits--) {
|
2014-04-17 06:34:56 +00:00
|
|
|
stream->base.data <<= 1;
|
2014-02-21 12:47:52 +00:00
|
|
|
|
2014-03-24 13:31:15 +00:00
|
|
|
if (data & bit_set_mask[bits]) {
|
2014-04-17 06:34:56 +00:00
|
|
|
stream->base.data |= 1;
|
2012-05-30 12:37:42 +00:00
|
|
|
}
|
2014-04-17 06:34:56 +00:00
|
|
|
stream->base.cur_bit++;
|
2014-02-21 12:47:52 +00:00
|
|
|
|
2014-03-24 13:31:15 +00:00
|
|
|
// write byte to output
|
2014-04-17 06:34:56 +00:00
|
|
|
if (stream->base.cur_bit==8) {
|
|
|
|
if((stream->base.zerocount == 2) && (stream->base.data < 4)) {
|
2014-04-14 06:11:31 +00:00
|
|
|
bitstream_writebyte(stream, emulation_prevention_three_byte);
|
2014-04-17 06:34:56 +00:00
|
|
|
stream->base.zerocount = 0;
|
2014-03-24 13:31:15 +00:00
|
|
|
}
|
2014-04-17 06:34:56 +00:00
|
|
|
if(stream->base.data == 0) {
|
|
|
|
stream->base.zerocount++;
|
2014-03-24 13:31:15 +00:00
|
|
|
} else {
|
2014-04-17 06:34:56 +00:00
|
|
|
stream->base.zerocount = 0;
|
2014-03-24 13:31:15 +00:00
|
|
|
}
|
2014-04-17 06:34:56 +00:00
|
|
|
bitstream_writebyte(stream, stream->base.data);
|
|
|
|
stream->base.cur_bit = 0;
|
2014-03-24 13:31:15 +00:00
|
|
|
}
|
2014-02-21 12:47:52 +00:00
|
|
|
}
|
2012-05-30 12:37:42 +00:00
|
|
|
}
|
2014-02-21 12:47:52 +00:00
|
|
|
|
2013-09-18 12:45:46 +00:00
|
|
|
/**
|
|
|
|
* \brief Align the bitstream with one-bit padding
|
|
|
|
*/
|
2014-04-17 05:39:57 +00:00
|
|
|
void bitstream_align(bitstream * const stream)
|
2014-02-21 12:47:52 +00:00
|
|
|
{
|
2013-09-18 12:45:46 +00:00
|
|
|
bitstream_put(stream, 1, 1);
|
2014-04-17 06:34:56 +00:00
|
|
|
if ((stream->base.cur_bit & 7) != 0) {
|
|
|
|
bitstream_put(stream, 0, 8 - (stream->base.cur_bit & 7));
|
2013-02-21 14:45:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-18 12:45:46 +00:00
|
|
|
/**
|
2014-02-21 12:47:52 +00:00
|
|
|
* \brief Align the bitstream with zero
|
2013-09-18 12:45:46 +00:00
|
|
|
*/
|
2014-04-17 05:39:57 +00:00
|
|
|
void bitstream_align_zero(bitstream * const stream)
|
2013-05-16 12:27:54 +00:00
|
|
|
{
|
2014-04-17 06:34:56 +00:00
|
|
|
if ((stream->base.cur_bit & 7) != 0) {
|
|
|
|
bitstream_put(stream, 0, 8 - (stream->base.cur_bit & 7));
|
2012-06-05 11:01:47 +00:00
|
|
|
}
|
2014-04-17 06:34:56 +00:00
|
|
|
}
|