2010-05-18 19:58:33 +04:00
|
|
|
/*
|
2010-09-09 16:16:39 +04:00
|
|
|
* Copyright (c) 2010 The WebM project authors. All Rights Reserved.
|
2010-05-18 19:58:33 +04:00
|
|
|
*
|
2010-06-18 20:39:21 +04:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
2010-06-05 00:19:40 +04:00
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
2010-06-18 20:39:21 +04:00
|
|
|
* in the file PATENTS. All contributing project authors may
|
2010-06-05 00:19:40 +04:00
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2010-05-18 19:58:33 +04:00
|
|
|
*/
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
#ifndef AOM_DSP_BITREADER_H_
|
|
|
|
#define AOM_DSP_BITREADER_H_
|
2012-12-19 03:31:19 +04:00
|
|
|
|
2010-05-06 01:58:19 +04:00
|
|
|
#include <limits.h>
|
2016-07-27 00:14:20 +03:00
|
|
|
#include <stddef.h>
|
2013-03-06 02:12:16 +04:00
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
#include "./aom_config.h"
|
2016-07-27 00:14:20 +03:00
|
|
|
|
|
|
|
#if CONFIG_BITSTREAM_DEBUG
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif // CONFIG_BITSTREAM_DEBUG
|
|
|
|
|
2016-08-23 02:08:15 +03:00
|
|
|
#include "aom_ports/mem.h"
|
2016-08-31 00:01:10 +03:00
|
|
|
#include "aom/aomdx.h"
|
|
|
|
#include "aom/aom_integer.h"
|
2016-08-23 02:08:15 +03:00
|
|
|
#include "aom_dsp/prob.h"
|
|
|
|
#include "aom_util/debug_util.h"
|
2010-05-06 01:58:19 +04:00
|
|
|
|
2014-01-19 00:16:11 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2013-11-27 00:38:58 +04:00
|
|
|
typedef size_t BD_VALUE;
|
|
|
|
|
|
|
|
#define BD_VALUE_SIZE ((int)sizeof(BD_VALUE) * CHAR_BIT)
|
|
|
|
|
2015-07-02 05:10:43 +03:00
|
|
|
// This is meant to be a large, positive constant that can still be efficiently
|
|
|
|
// loaded as an immediate (on platforms like ARM, for example).
|
|
|
|
// Even relatively modest values like 100 would work fine.
|
|
|
|
#define LOTS_OF_BITS 0x40000000
|
|
|
|
|
2012-07-14 02:21:29 +04:00
|
|
|
typedef struct {
|
2014-11-25 06:27:07 +03:00
|
|
|
// Be careful when reordering this struct, it may impact the cache negatively.
|
2013-11-27 00:38:58 +04:00
|
|
|
BD_VALUE value;
|
2013-04-16 01:54:19 +04:00
|
|
|
unsigned int range;
|
2014-11-25 06:27:07 +03:00
|
|
|
int count;
|
|
|
|
const uint8_t *buffer_end;
|
|
|
|
const uint8_t *buffer;
|
2016-08-31 00:01:10 +03:00
|
|
|
aom_decrypt_cb decrypt_cb;
|
2014-04-16 01:10:58 +04:00
|
|
|
void *decrypt_state;
|
2014-11-25 06:27:07 +03:00
|
|
|
uint8_t clear_buffer[sizeof(BD_VALUE) + 1];
|
2016-08-31 00:01:10 +03:00
|
|
|
} aom_reader;
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
int aom_reader_init(aom_reader *r, const uint8_t *buffer, size_t size,
|
|
|
|
aom_decrypt_cb decrypt_cb, void *decrypt_state);
|
2011-02-05 00:00:00 +03:00
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
void aom_reader_fill(aom_reader *r);
|
2010-05-06 01:58:19 +04:00
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
const uint8_t *aom_reader_find_end(aom_reader *r);
|
2013-04-16 01:54:19 +04:00
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
static INLINE int aom_reader_has_error(aom_reader *r) {
|
2015-07-02 05:10:43 +03:00
|
|
|
// Check if we have reached the end of the buffer.
|
|
|
|
//
|
|
|
|
// Variable 'count' stores the number of bits in the 'value' buffer, minus
|
|
|
|
// 8. The top byte is part of the algorithm, and the remainder is buffered
|
|
|
|
// to be shifted into it. So if count == 8, the top 16 bits of 'value' are
|
|
|
|
// occupied, 8 for the algorithm and 8 in the buffer.
|
|
|
|
//
|
|
|
|
// When reading a byte from the user's buffer, count is filled with 8 and
|
|
|
|
// one byte is filled into the value buffer. When we reach the end of the
|
|
|
|
// data, count is additionally filled with LOTS_OF_BITS. So when
|
|
|
|
// count == LOTS_OF_BITS - 1, the user's data has been exhausted.
|
|
|
|
//
|
|
|
|
// 1 if we have tried to decode bits after the end of stream was encountered.
|
|
|
|
// 0 No error.
|
|
|
|
return r->count > BD_VALUE_SIZE && r->count < LOTS_OF_BITS;
|
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
static INLINE int aom_read(aom_reader *r, int prob) {
|
2012-07-14 02:21:29 +04:00
|
|
|
unsigned int bit = 0;
|
2013-11-27 00:38:58 +04:00
|
|
|
BD_VALUE value;
|
|
|
|
BD_VALUE bigsplit;
|
2012-07-14 02:21:29 +04:00
|
|
|
int count;
|
|
|
|
unsigned int range;
|
2013-11-27 00:38:58 +04:00
|
|
|
unsigned int split = (r->range * prob + (256 - prob)) >> CHAR_BIT;
|
2012-07-14 02:21:29 +04:00
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
if (r->count < 0) aom_reader_fill(r);
|
2012-07-14 02:21:29 +04:00
|
|
|
|
2013-11-27 00:38:58 +04:00
|
|
|
value = r->value;
|
|
|
|
count = r->count;
|
2012-07-14 02:21:29 +04:00
|
|
|
|
2013-11-27 00:38:58 +04:00
|
|
|
bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
|
2012-07-14 02:21:29 +04:00
|
|
|
|
|
|
|
range = split;
|
|
|
|
|
|
|
|
if (value >= bigsplit) {
|
2013-11-27 00:38:58 +04:00
|
|
|
range = r->range - split;
|
2012-07-14 02:21:29 +04:00
|
|
|
value = value - bigsplit;
|
|
|
|
bit = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-08-31 00:01:10 +03:00
|
|
|
register int shift = aom_norm[range];
|
2012-07-14 02:21:29 +04:00
|
|
|
range <<= shift;
|
|
|
|
value <<= shift;
|
|
|
|
count -= shift;
|
|
|
|
}
|
2013-11-27 00:38:58 +04:00
|
|
|
r->value = value;
|
|
|
|
r->count = count;
|
|
|
|
r->range = range;
|
2012-07-14 02:21:29 +04:00
|
|
|
|
2016-07-27 00:14:20 +03:00
|
|
|
#if CONFIG_BITSTREAM_DEBUG
|
|
|
|
{
|
|
|
|
int ref_bit, ref_prob;
|
|
|
|
const int queue_r = bitstream_queue_get_read();
|
|
|
|
bitstream_queue_pop(&ref_bit, &ref_prob);
|
|
|
|
if (prob != ref_prob) {
|
|
|
|
fprintf(stderr, "prob error, prob %d ref_prob %d queue_r %d\n", prob,
|
|
|
|
ref_prob, queue_r);
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
if ((int)bit != ref_bit) {
|
|
|
|
fprintf(stderr, "bit error, bit %d ref_bit %d\n", bit, ref_bit);
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // CONFIG_BITSTREAM_DEBUG
|
2012-07-14 02:21:29 +04:00
|
|
|
return bit;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
static INLINE int aom_read_bit(aom_reader *r) {
|
|
|
|
return aom_read(r, 128); // aom_prob_half
|
2013-04-16 01:54:19 +04:00
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
static INLINE int aom_read_literal(aom_reader *r, int bits) {
|
2013-11-27 00:38:58 +04:00
|
|
|
int literal = 0, bit;
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
for (bit = bits - 1; bit >= 0; bit--) literal |= aom_read_bit(r) << bit;
|
2010-05-18 19:58:33 +04:00
|
|
|
|
2013-11-27 00:38:58 +04:00
|
|
|
return literal;
|
2010-05-18 19:58:33 +04:00
|
|
|
}
|
2010-12-16 18:46:31 +03:00
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
static INLINE int aom_read_tree(aom_reader *r, const aom_tree_index *tree,
|
|
|
|
const aom_prob *probs) {
|
|
|
|
aom_tree_index i = 0;
|
2013-11-27 00:38:58 +04:00
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
while ((i = tree[i + aom_read(r, probs[i >> 1])]) > 0) continue;
|
2013-11-27 00:38:58 +04:00
|
|
|
|
|
|
|
return -i;
|
|
|
|
}
|
2012-04-12 20:24:03 +04:00
|
|
|
|
2014-01-19 00:16:11 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
#endif // AOM_DSP_BITREADER_H_
|