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>
|
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
|
|
|
|
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
|
|
|
|
2013-09-18 09:42:16 +00:00
|
|
|
bit_table *g_exp_table;
|
2012-06-05 11:01:47 +00:00
|
|
|
|
|
|
|
//From wikipedia
|
|
|
|
//http://en.wikipedia.org/wiki/Binary_logarithm#Algorithm
|
2013-09-18 09:42:16 +00:00
|
|
|
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-01-31 18:34:50 +00:00
|
|
|
int init_exp_golomb(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;
|
|
|
|
g_exp_table = (bit_table*)malloc(len*sizeof(bit_table));
|
2014-01-31 18:34:50 +00:00
|
|
|
if(!g_exp_table)
|
|
|
|
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);
|
|
|
|
g_exp_table[code_num].len = M * 2 + 1;
|
|
|
|
g_exp_table[code_num].value = (1<<M) | info;
|
|
|
|
}
|
2014-01-31 18:34:50 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Create and initialize a new bitstream
|
|
|
|
*/
|
|
|
|
bitstream *create_bitstream(int32_t width)
|
|
|
|
{
|
|
|
|
bitstream *stream = malloc(sizeof(bitstream));
|
|
|
|
if (!stream) {
|
|
|
|
fprintf(stderr, "Failed to allocate the bitstream object!\n");
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the bitstream
|
|
|
|
bitstream_reinit(stream);
|
|
|
|
|
|
|
|
// Initialize buffer-related values
|
|
|
|
stream->output = NULL;
|
|
|
|
stream->buffer = NULL;
|
|
|
|
stream->buffer_pos = 0;
|
|
|
|
stream->bufferlen = 0;
|
|
|
|
|
|
|
|
// Alloc 2kB*width for bitstream buffer (for one coded frame)
|
|
|
|
bitstream_alloc(stream, 1024*2*width);
|
|
|
|
if (!stream->buffer) {
|
|
|
|
fprintf(stderr, "Failed to allocate the bitstream buffer!\n");
|
|
|
|
goto creation_failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Clear buffer just to be sure
|
|
|
|
bitstream_clear_buffer(stream);
|
|
|
|
|
|
|
|
// Return the created bitstream
|
|
|
|
return stream;
|
|
|
|
|
|
|
|
creation_failure:
|
|
|
|
// In case of failure, free whatever was allocated
|
|
|
|
free(stream->buffer);
|
|
|
|
free(stream);
|
|
|
|
|
|
|
|
return NULL;
|
2012-06-05 11:01:47 +00:00
|
|
|
}
|
|
|
|
|
2013-09-18 12:45:46 +00:00
|
|
|
/**
|
2014-01-31 18:34:50 +00:00
|
|
|
* \brief Reinitialize bitstream
|
2012-05-30 12:37:42 +00:00
|
|
|
*/
|
2014-01-31 18:34:50 +00:00
|
|
|
void bitstream_reinit(bitstream *stream)
|
2012-05-30 12:37:42 +00:00
|
|
|
{
|
2013-09-18 12:45:46 +00:00
|
|
|
stream->cur_byte = 0;
|
|
|
|
stream->cur_bit = 0;
|
2014-02-21 12:47:52 +00:00
|
|
|
memset(stream->data, 0, sizeof(uint32_t)*32);
|
2012-05-30 12:37:42 +00:00
|
|
|
}
|
2012-06-05 11:01:47 +00:00
|
|
|
|
2013-09-18 12:45:46 +00:00
|
|
|
/**
|
|
|
|
* \brief Allocate buffer
|
|
|
|
* \param stream pointer bitstream to put the data
|
|
|
|
* \param alloc size to allocate
|
2012-06-05 11:01:47 +00:00
|
|
|
*/
|
2013-09-18 12:45:46 +00:00
|
|
|
void bitstream_alloc(bitstream *stream, uint32_t alloc)
|
2012-06-05 11:01:47 +00:00
|
|
|
{
|
|
|
|
stream->buffer = (uint8_t*)malloc(alloc);
|
2014-01-31 18:34:50 +00:00
|
|
|
if (stream->buffer)
|
|
|
|
stream->bufferlen = alloc;
|
2012-06-06 10:42:02 +00:00
|
|
|
}
|
|
|
|
|
2014-02-03 09:52:43 +00:00
|
|
|
/**
|
|
|
|
* \brief Free bitstream buffer
|
|
|
|
*/
|
|
|
|
void bitstream_free(bitstream *stream)
|
|
|
|
{
|
|
|
|
free(stream->buffer);
|
|
|
|
stream->bufferlen = 0;
|
|
|
|
}
|
|
|
|
|
2013-09-18 12:45:46 +00:00
|
|
|
/**
|
|
|
|
* \brief clear output buffer
|
|
|
|
*/
|
|
|
|
void bitstream_clear_buffer(bitstream *stream)
|
2012-06-06 10:42:02 +00:00
|
|
|
{
|
2013-09-18 12:45:46 +00:00
|
|
|
memset(stream->buffer, 0, stream->bufferlen);
|
2012-06-05 11:01:47 +00:00
|
|
|
stream->buffer_pos = 0;
|
2014-02-21 12:47:52 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2013-09-18 12:45:46 +00:00
|
|
|
void bitstream_put(bitstream *stream, uint32_t data, uint8_t bits)
|
2012-05-30 12:37:42 +00:00
|
|
|
{
|
2014-02-21 09:57:25 +00:00
|
|
|
uint8_t bitsleft = 32 - stream->cur_bit;
|
2012-06-05 14:45:17 +00:00
|
|
|
#ifdef VERBOSE
|
|
|
|
uint8_t i=0;
|
|
|
|
printf_bitstream("put: ");
|
2013-09-18 12:45:46 +00:00
|
|
|
for (i = 0; i < bits; i++) {
|
2012-06-05 14:45:17 +00:00
|
|
|
printf("%i",(data&(1<<(bits-i-1)))?1:0);
|
|
|
|
}
|
|
|
|
printf_bitstream("\n");
|
|
|
|
//printf_bitstream(" count: %i\n",bits);
|
|
|
|
#endif
|
2014-02-21 12:47:52 +00:00
|
|
|
|
2013-09-18 12:45:46 +00:00
|
|
|
//There's space for all the bits
|
|
|
|
if (bits <= bitsleft) {
|
2012-06-05 14:45:17 +00:00
|
|
|
stream->data[stream->cur_byte] |= (data<<((bitsleft-bits)));
|
2013-09-18 12:45:46 +00:00
|
|
|
stream->cur_bit += bits;
|
|
|
|
bits = 0;
|
|
|
|
} else { //No space for everything, store the bits we can and continue later
|
2012-06-05 14:45:17 +00:00
|
|
|
stream->data[stream->cur_byte] |= (data>>(bits-bitsleft));
|
2013-09-18 12:45:46 +00:00
|
|
|
stream->cur_bit = 32;
|
|
|
|
bits -= bitsleft;
|
2012-06-05 14:45:17 +00:00
|
|
|
}
|
2014-02-21 12:47:52 +00:00
|
|
|
|
2013-09-18 12:45:46 +00:00
|
|
|
//Check if the buffer is full, and flush to output if it is
|
|
|
|
if (stream->cur_bit == 32) {
|
|
|
|
bitsleft = 32;
|
2012-06-05 14:45:17 +00:00
|
|
|
stream->cur_byte++;
|
|
|
|
stream->cur_bit = 0;
|
2013-09-18 12:45:46 +00:00
|
|
|
if (stream->cur_byte == 32) {
|
|
|
|
//Flush data out
|
|
|
|
bitstream_flush(stream);
|
2012-05-30 12:37:42 +00:00
|
|
|
}
|
2012-06-05 14:45:17 +00:00
|
|
|
}
|
2014-02-21 12:47:52 +00:00
|
|
|
|
2013-09-18 12:45:46 +00:00
|
|
|
//Write the last of the bits (if buffer was full and flushed before)
|
|
|
|
if (bits != 0) {
|
2012-06-05 14:45:17 +00:00
|
|
|
stream->data[stream->cur_byte] |= (data<<(bitsleft-bits));
|
2013-09-18 12:45:46 +00:00
|
|
|
stream->cur_bit += bits;
|
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
|
|
|
|
*/
|
|
|
|
void bitstream_align(bitstream *stream)
|
2014-02-21 12:47:52 +00:00
|
|
|
{
|
2013-09-18 12:45:46 +00:00
|
|
|
bitstream_put(stream, 1, 1);
|
|
|
|
if ((stream->cur_bit & 7) != 0) {
|
|
|
|
bitstream_put(stream, 0, 8 - (stream->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
|
|
|
*/
|
|
|
|
void bitstream_align_zero(bitstream *stream)
|
2013-05-16 12:27:54 +00:00
|
|
|
{
|
2013-09-18 12:45:46 +00:00
|
|
|
if ((stream->cur_bit & 7) != 0) {
|
|
|
|
bitstream_put(stream, 0, 8 - (stream->cur_bit & 7));
|
2012-06-05 11:01:47 +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 Flush bitstream to output
|
|
|
|
*/
|
|
|
|
void bitstream_flush(bitstream *stream)
|
2012-05-30 12:37:42 +00:00
|
|
|
{
|
2012-06-08 12:26:07 +00:00
|
|
|
int i;
|
2012-06-06 08:59:02 +00:00
|
|
|
uint32_t correct_endian;
|
2013-09-18 12:45:46 +00:00
|
|
|
//If output open, write to output
|
|
|
|
if (stream->output) {
|
|
|
|
if (stream->cur_byte) fwrite(&stream->data[0], stream->cur_byte * 4, 1, stream->output);
|
|
|
|
if (stream->cur_bit>>3) fwrite(&stream->data[stream->cur_byte], stream->cur_bit>>3, 1, stream->output);
|
|
|
|
} else { //No file open, write to buffer
|
|
|
|
if (stream->cur_byte) {
|
|
|
|
//Handle endianness issue
|
|
|
|
for (i = 0; i < stream->cur_byte; i++) {
|
|
|
|
//"network" is big-endian
|
2012-06-06 08:59:02 +00:00
|
|
|
correct_endian = htonl(stream->data[i]);
|
2013-09-18 12:45:46 +00:00
|
|
|
memcpy((uint8_t*)&stream->buffer[stream->buffer_pos], &correct_endian, 4);
|
2012-06-06 08:59:02 +00:00
|
|
|
stream->buffer_pos += 4;
|
2012-06-05 14:45:17 +00:00
|
|
|
}
|
2012-06-05 11:01:47 +00:00
|
|
|
}
|
2014-02-21 12:47:52 +00:00
|
|
|
|
2013-09-18 12:45:46 +00:00
|
|
|
if (stream->cur_bit>>3) {
|
2012-06-06 08:59:02 +00:00
|
|
|
correct_endian = htonl(stream->data[stream->cur_byte]);
|
2013-09-18 12:45:46 +00:00
|
|
|
memcpy((uint8_t*)&stream->buffer[stream->buffer_pos], &correct_endian, stream->cur_bit>>3);
|
2012-06-05 14:45:17 +00:00
|
|
|
stream->buffer_pos += stream->cur_bit>>3;
|
2012-06-06 08:59:02 +00:00
|
|
|
}
|
2012-06-05 11:01:47 +00:00
|
|
|
}
|
2012-06-06 08:59:02 +00:00
|
|
|
//Stream flushed, zero out the values
|
2014-01-31 18:34:50 +00:00
|
|
|
bitstream_reinit(stream);
|
2012-05-30 12:37:42 +00:00
|
|
|
}
|