2013-09-19 07:35:34 +00:00
|
|
|
#ifndef BITSTREAM_H_
|
|
|
|
#define BITSTREAM_H_
|
2014-01-24 10:37:15 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* This file is part of Kvazaar HEVC encoder.
|
2014-03-06 16:14:01 +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/>.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
2013-09-18 14:29:30 +00:00
|
|
|
* \file
|
|
|
|
* \brief Bitstream can be written to one or several bits at a time.
|
|
|
|
*/
|
2014-03-06 16:14:01 +00:00
|
|
|
|
2013-09-18 09:16:03 +00:00
|
|
|
#include "global.h"
|
|
|
|
|
2014-04-14 06:11:31 +00:00
|
|
|
typedef enum {BITSTREAM_TYPE_FILE, BITSTREAM_TYPE_MEMORY} bitstream_type;
|
|
|
|
|
|
|
|
#define BASE_BITSTREAM uint8_t data; uint8_t cur_bit; uint8_t zerocount; bitstream_type type;
|
|
|
|
|
|
|
|
//Size of the allocation for a memory bitstream in bytes
|
|
|
|
#define BITSTREAM_MEMORY_CHUNK_SIZE 4096
|
2014-03-06 16:14:01 +00:00
|
|
|
|
2012-05-30 12:37:42 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2014-04-14 06:11:31 +00:00
|
|
|
BASE_BITSTREAM
|
2014-04-17 06:34:56 +00:00
|
|
|
} bitstream_base;
|
2012-06-05 11:01:47 +00:00
|
|
|
|
2014-04-14 06:11:31 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
BASE_BITSTREAM
|
|
|
|
FILE* output;
|
|
|
|
} bitstream_file;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
BASE_BITSTREAM
|
|
|
|
uint8_t* output_data;
|
|
|
|
uint32_t output_length;
|
|
|
|
uint32_t allocated_length;
|
|
|
|
} bitstream_mem;
|
|
|
|
|
2014-04-17 06:34:56 +00:00
|
|
|
typedef union
|
|
|
|
{
|
|
|
|
bitstream_base base;
|
|
|
|
bitstream_file file;
|
|
|
|
bitstream_mem mem;
|
|
|
|
} bitstream;
|
|
|
|
|
2012-06-05 11:01:47 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint8_t len;
|
|
|
|
uint32_t value;
|
2013-09-18 09:42:16 +00:00
|
|
|
} bit_table;
|
2012-06-05 11:01:47 +00:00
|
|
|
|
2014-04-15 12:37:16 +00:00
|
|
|
extern const bit_table *g_exp_table;
|
2012-06-05 12:38:54 +00:00
|
|
|
|
2014-04-17 06:34:56 +00:00
|
|
|
int bitstream_init(bitstream * stream, bitstream_type type);
|
|
|
|
int bitstream_finalize(bitstream * stream);
|
2014-04-17 05:39:57 +00:00
|
|
|
void bitstream_put(bitstream *stream, uint32_t data, uint8_t bits);
|
2014-04-17 04:55:35 +00:00
|
|
|
int bitstream_writebyte(bitstream *stream_abstract, uint8_t byte);
|
2013-02-05 13:48:06 +00:00
|
|
|
|
2014-04-23 08:19:11 +00:00
|
|
|
int bitstream_append(bitstream *dst, const bitstream *src);
|
|
|
|
int bitstream_clear(bitstream *stream);
|
|
|
|
|
2013-02-05 13:48:06 +00:00
|
|
|
/* Use macros to force inlining */
|
2012-06-05 14:45:17 +00:00
|
|
|
#define bitstream_put_ue(stream, data) { bitstream_put(stream,g_exp_table[data].value,g_exp_table[data].len); }
|
2013-03-07 15:42:00 +00:00
|
|
|
#define bitstream_put_se(stream, data) { uint32_t index=(uint32_t)(((data)<=0)?(-(data))<<1:((data)<<1)-1); \
|
2012-06-05 14:45:17 +00:00
|
|
|
bitstream_put(stream,g_exp_table[index].value,g_exp_table[index].len); }
|
2012-06-05 11:01:47 +00:00
|
|
|
|
2014-04-17 05:39:57 +00:00
|
|
|
void bitstream_align(bitstream *stream);
|
|
|
|
void bitstream_align_zero(bitstream *stream);
|
2014-01-31 18:34:50 +00:00
|
|
|
int init_exp_golomb(uint32_t len);
|
2014-04-07 04:53:32 +00:00
|
|
|
void free_exp_golomb();
|
2012-06-05 11:01:47 +00:00
|
|
|
|
2013-02-05 13:48:06 +00:00
|
|
|
|
|
|
|
/* In debug mode print out some extra info */
|
2013-03-07 15:42:00 +00:00
|
|
|
#ifdef NOTDEFINED//_DEBUG
|
2013-02-05 13:48:06 +00:00
|
|
|
/* Counter to keep up with bits written */
|
2014-04-15 09:18:19 +00:00
|
|
|
#define WRITE_U(stream, data, bits, name) { printf("%-40s u(%d) : %d\n", name,bits,data); bitstream_put(stream,data,bits);}
|
|
|
|
#define WRITE_UE(stream, data, name) { printf("%-40s ue(v): %d\n", name,data); bitstream_put_ue(stream,data);}
|
|
|
|
#define WRITE_SE(stream, data, name) { printf("%-40s se(v): %d\n", name,data); bitstream_put_se(stream,(data));}
|
2012-06-05 11:01:47 +00:00
|
|
|
#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
|
|
|
|
|
2014-03-06 16:14:01 +00:00
|
|
|
|
2014-01-31 18:34:50 +00:00
|
|
|
#endif
|