2014-01-24 10:37:15 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* This file is part of Kvazaar HEVC encoder.
|
2014-02-21 12:47:52 +00:00
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* Copyright (C) 2013-2015 Tampere University of Technology and others (see
|
2014-01-24 10:37:15 +00:00
|
|
|
* COPYING file).
|
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* Kvazaar is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2.1 of the License, or (at your
|
|
|
|
* option) any later version.
|
2014-01-24 10:37:15 +00:00
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* 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 Lesser General Public License for
|
|
|
|
* more details.
|
2014-01-24 10:37:15 +00:00
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with Kvazaar. If not, see <http://www.gnu.org/licenses/>.
|
2014-01-24 10:37:15 +00:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* \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>
|
2014-02-21 12:47:52 +00:00
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
const uint32_t kvz_bit_set_mask[] =
|
2014-03-24 13:31:15 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
bit_table_t kvz_g_exp_table[EXP_GOLOMB_TABLE_SIZE];
|
2015-06-03 08:18:45 +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
|
|
|
|
2015-06-25 08:15:34 +00:00
|
|
|
static int floor_log2(unsigned int n)
|
|
|
|
{
|
2015-05-05 08:10:08 +00:00
|
|
|
assert(n != 0);
|
|
|
|
|
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; }
|
2015-05-05 08:10:08 +00:00
|
|
|
return pos;
|
2012-06-05 11:01:47 +00:00
|
|
|
}
|
|
|
|
|
2013-09-18 12:45:46 +00:00
|
|
|
/**
|
2015-06-03 08:18:45 +00:00
|
|
|
* \brief Initialize the Exp Golomb code table.
|
2014-01-31 18:34:50 +00:00
|
|
|
*
|
2015-08-26 08:50:27 +00:00
|
|
|
* Fills kvz_g_exp_table with exponential golomb codes.
|
2013-09-18 12:45:46 +00:00
|
|
|
*/
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_init_exp_golomb()
|
2012-06-05 11:01:47 +00:00
|
|
|
{
|
2015-06-03 08:18:45 +00:00
|
|
|
static int exp_table_initialized = 0;
|
|
|
|
if (exp_table_initialized) return;
|
|
|
|
|
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;
|
2015-06-03 08:18:45 +00:00
|
|
|
for (code_num = 0; code_num < EXP_GOLOMB_TABLE_SIZE; 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);
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_g_exp_table[code_num].len = M * 2 + 1;
|
|
|
|
kvz_g_exp_table[code_num].value = (1<<M) | info;
|
2013-09-18 12:45:46 +00:00
|
|
|
}
|
2014-01-31 18:34:50 +00:00
|
|
|
|
2015-06-03 08:18:45 +00:00
|
|
|
exp_table_initialized = 1;
|
2014-04-04 08:04:24 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 18:34:50 +00:00
|
|
|
/**
|
2015-06-25 08:15:34 +00:00
|
|
|
* \brief Initialize a new bitstream.
|
2014-01-31 18:34:50 +00:00
|
|
|
*/
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_bitstream_init(bitstream_t *const stream)
|
2015-06-25 08:15:34 +00:00
|
|
|
{
|
|
|
|
memset(stream, 0, sizeof(bitstream_t));
|
2014-02-21 12:47:52 +00:00
|
|
|
}
|
|
|
|
|
2014-04-14 06:11:31 +00:00
|
|
|
/**
|
2015-06-25 08:15:34 +00:00
|
|
|
* \brief Take chunks from a bitstream.
|
|
|
|
*
|
|
|
|
* Move ownership of the chunks to the caller and clear the bitstream.
|
|
|
|
*
|
|
|
|
* The bitstream must be byte-aligned.
|
2014-04-14 06:11:31 +00:00
|
|
|
*/
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_data_chunk * kvz_bitstream_take_chunks(bitstream_t *const stream)
|
2015-06-25 08:15:34 +00:00
|
|
|
{
|
|
|
|
assert(stream->cur_bit == 0);
|
2015-06-30 09:04:00 +00:00
|
|
|
kvz_data_chunk *chunks = stream->first;
|
2015-06-25 08:15:34 +00:00
|
|
|
stream->first = stream->last = NULL;
|
|
|
|
stream->len = 0;
|
|
|
|
return chunks;
|
2014-04-14 06:11:31 +00:00
|
|
|
}
|
|
|
|
|
2015-06-25 08:15:34 +00:00
|
|
|
/**
|
|
|
|
* \brief Allocates a new bitstream chunk.
|
|
|
|
*
|
|
|
|
* \return Pointer to the new chunk, or NULL.
|
|
|
|
*/
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_data_chunk * kvz_bitstream_alloc_chunk()
|
2015-06-25 08:15:34 +00:00
|
|
|
{
|
2015-06-30 09:04:00 +00:00
|
|
|
kvz_data_chunk *chunk = malloc(sizeof(kvz_data_chunk));
|
2015-06-25 08:15:34 +00:00
|
|
|
if (chunk) {
|
|
|
|
chunk->len = 0;
|
|
|
|
chunk->next = NULL;
|
|
|
|
}
|
|
|
|
return chunk;
|
|
|
|
}
|
2014-04-17 06:34:56 +00:00
|
|
|
|
2014-04-14 06:11:31 +00:00
|
|
|
/**
|
2015-06-25 08:15:34 +00:00
|
|
|
* \brief Free a list of chunks.
|
2014-04-14 06:11:31 +00:00
|
|
|
*/
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_bitstream_free_chunks(kvz_data_chunk *chunk)
|
2015-06-25 08:15:34 +00:00
|
|
|
{
|
|
|
|
while (chunk != NULL) {
|
2015-06-30 09:04:00 +00:00
|
|
|
kvz_data_chunk *next = chunk->next;
|
2015-06-25 08:15:34 +00:00
|
|
|
free(chunk);
|
|
|
|
chunk = next;
|
2014-04-14 06:11:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 08:15:34 +00:00
|
|
|
/**
|
|
|
|
* \brief Free resources used by a bitstream.
|
|
|
|
*/
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_bitstream_finalize(bitstream_t *const stream)
|
2015-06-25 08:15:34 +00:00
|
|
|
{
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_bitstream_clear(stream);
|
2015-06-25 08:15:34 +00:00
|
|
|
}
|
2014-05-05 06:21:57 +00:00
|
|
|
|
|
|
|
/**
|
2015-06-25 08:15:34 +00:00
|
|
|
* \brief Get the number of bits written.
|
|
|
|
* \param stream bitstream
|
|
|
|
* \return position
|
2014-05-05 06:21:57 +00:00
|
|
|
*/
|
2015-08-26 08:50:27 +00:00
|
|
|
uint64_t kvz_bitstream_tell(const bitstream_t *const stream)
|
2015-06-25 08:15:34 +00:00
|
|
|
{
|
|
|
|
uint64_t position = stream->len;
|
|
|
|
return position * 8 + stream->cur_bit;
|
2014-05-05 06:21:57 +00:00
|
|
|
}
|
|
|
|
|
2015-06-25 08:15:34 +00:00
|
|
|
/**
|
|
|
|
* \brief Write a byte to bitstream
|
|
|
|
*
|
|
|
|
* The stream must be byte-aligned.
|
|
|
|
*
|
|
|
|
* \param stream pointer bitstream to put the data
|
|
|
|
* \param byte byte to write
|
|
|
|
*/
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_bitstream_writebyte(bitstream_t *const stream, const uint8_t byte)
|
2015-06-25 08:15:34 +00:00
|
|
|
{
|
|
|
|
assert(stream->cur_bit == 0);
|
|
|
|
|
2015-06-30 09:04:00 +00:00
|
|
|
if (stream->last == NULL || stream->last->len == KVZ_DATA_CHUNK_SIZE) {
|
2015-06-25 08:15:34 +00:00
|
|
|
// Need to allocate a new chunk.
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_data_chunk *new_chunk = kvz_bitstream_alloc_chunk();
|
2015-06-25 08:15:34 +00:00
|
|
|
assert(new_chunk);
|
|
|
|
|
|
|
|
if (!stream->first) stream->first = new_chunk;
|
|
|
|
if (stream->last) stream->last->next = new_chunk;
|
|
|
|
stream->last = new_chunk;
|
2014-04-23 08:19:11 +00:00
|
|
|
}
|
2015-06-30 09:04:00 +00:00
|
|
|
assert(stream->last->len < KVZ_DATA_CHUNK_SIZE);
|
2015-06-25 08:15:34 +00:00
|
|
|
|
|
|
|
stream->last->data[stream->last->len] = byte;
|
|
|
|
stream->last->len += 1;
|
|
|
|
stream->len += 1;
|
2014-04-23 08:19:11 +00:00
|
|
|
}
|
|
|
|
|
2015-06-25 11:01:26 +00:00
|
|
|
/**
|
|
|
|
* \brief Move data from one stream to another.
|
|
|
|
*
|
2015-07-08 08:08:04 +00:00
|
|
|
* Destination stream must be byte-aligned. Source stream will be cleared.
|
2015-06-25 11:01:26 +00:00
|
|
|
*/
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_bitstream_move(bitstream_t *const dst, bitstream_t *const src)
|
2015-06-25 11:01:26 +00:00
|
|
|
{
|
|
|
|
assert(dst->cur_bit == 0);
|
|
|
|
|
|
|
|
if (src->len > 0) {
|
|
|
|
if (dst->first == NULL) {
|
|
|
|
dst->first = src->first;
|
|
|
|
dst->last = src->last;
|
|
|
|
dst->len = src->len;
|
|
|
|
} else {
|
|
|
|
dst->last->next = src->first;
|
|
|
|
dst->last = src->last;
|
|
|
|
dst->len += src->len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the leftover bits.
|
|
|
|
dst->data = src->data;
|
|
|
|
dst->cur_bit = src->cur_bit;
|
|
|
|
dst->zerocount = src->zerocount;
|
|
|
|
|
|
|
|
src->first = src->last = NULL;
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_bitstream_clear(src);
|
2015-06-25 11:01:26 +00:00
|
|
|
}
|
|
|
|
|
2015-06-25 08:15:34 +00:00
|
|
|
/**
|
|
|
|
* Reset stream.
|
|
|
|
*/
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_bitstream_clear(bitstream_t *const stream)
|
2015-06-25 08:15:34 +00:00
|
|
|
{
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_bitstream_free_chunks(stream->first);
|
|
|
|
kvz_bitstream_init(stream);
|
2014-04-23 08:19:11 +00:00
|
|
|
}
|
|
|
|
|
2013-09-18 12:45:46 +00:00
|
|
|
/**
|
2015-06-25 08:15:34 +00:00
|
|
|
* \brief Write 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
|
|
|
*/
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_bitstream_put(bitstream_t *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--) {
|
2015-06-25 08:15:34 +00:00
|
|
|
stream->data <<= 1;
|
2014-02-21 12:47:52 +00:00
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
if (data & kvz_bit_set_mask[bits]) {
|
2015-06-25 08:15:34 +00:00
|
|
|
stream->data |= 1;
|
2012-05-30 12:37:42 +00:00
|
|
|
}
|
2015-06-25 08:15:34 +00:00
|
|
|
stream->cur_bit++;
|
2014-02-21 12:47:52 +00:00
|
|
|
|
2015-06-25 08:15:34 +00:00
|
|
|
// write byte to output
|
|
|
|
if (stream->cur_bit==8) {
|
|
|
|
stream->cur_bit = 0;
|
|
|
|
if((stream->zerocount == 2) && (stream->data < 4)) {
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_bitstream_writebyte(stream, emulation_prevention_three_byte);
|
2015-06-25 08:15:34 +00:00
|
|
|
stream->zerocount = 0;
|
2014-03-24 13:31:15 +00:00
|
|
|
}
|
2015-06-25 08:15:34 +00:00
|
|
|
if(stream->data == 0) {
|
|
|
|
stream->zerocount++;
|
2014-03-24 13:31:15 +00:00
|
|
|
} else {
|
2015-06-25 08:15:34 +00:00
|
|
|
stream->zerocount = 0;
|
2014-03-24 13:31:15 +00:00
|
|
|
}
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_bitstream_writebyte(stream, stream->data);
|
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
|
|
|
/**
|
2015-08-27 11:56:05 +00:00
|
|
|
* \brief Add rbsp_trailing_bits syntax element, which aligns the bitstream.
|
2013-09-18 12:45:46 +00:00
|
|
|
*/
|
2015-08-27 11:33:30 +00:00
|
|
|
void kvz_bitstream_add_rbsp_trailing_bits(bitstream_t * const stream)
|
2014-02-21 12:47:52 +00:00
|
|
|
{
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_bitstream_put(stream, 1, 1);
|
2015-06-25 08:15:34 +00:00
|
|
|
if ((stream->cur_bit & 7) != 0) {
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_bitstream_put(stream, 0, 8 - (stream->cur_bit & 7));
|
2013-02-21 14:45:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-27 11:56:05 +00:00
|
|
|
/**
|
|
|
|
* \brief Align the bitstream, unless it's already aligned.
|
|
|
|
*/
|
|
|
|
void kvz_bitstream_align(bitstream_t * const stream)
|
|
|
|
{
|
|
|
|
if ((stream->cur_bit & 7) != 0) {
|
|
|
|
kvz_bitstream_add_rbsp_trailing_bits(stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_bitstream_align_zero(bitstream_t * const stream)
|
2013-05-16 12:27:54 +00:00
|
|
|
{
|
2015-06-25 08:15:34 +00:00
|
|
|
if ((stream->cur_bit & 7) != 0) {
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_bitstream_put(stream, 0, 8 - (stream->cur_bit & 7));
|
2012-06-05 11:01:47 +00:00
|
|
|
}
|
2014-04-17 06:34:56 +00:00
|
|
|
}
|