2010-05-18 17:58:33 +02:00
|
|
|
/*
|
2010-09-09 14:16:39 +02:00
|
|
|
* Copyright (c) 2010 The WebM project authors. All Rights Reserved.
|
2010-05-18 17:58:33 +02:00
|
|
|
*
|
2010-06-18 18:39:21 +02:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
2010-06-04 22:19:40 +02: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 18:39:21 +02:00
|
|
|
* in the file PATENTS. All contributing project authors may
|
2010-06-04 22:19:40 +02:00
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2010-05-18 17:58:33 +02:00
|
|
|
*/
|
|
|
|
|
2015-07-17 23:09:05 +02:00
|
|
|
#ifndef VPX_DSP_BITREADER_H_
|
|
|
|
#define VPX_DSP_BITREADER_H_
|
2012-12-19 00:31:19 +01:00
|
|
|
|
2010-05-05 23:58:19 +02:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <limits.h>
|
2013-03-05 23:12:16 +01:00
|
|
|
|
2012-12-23 16:20:10 +01:00
|
|
|
#include "./vpx_config.h"
|
2010-05-18 17:58:33 +02:00
|
|
|
#include "vpx_ports/mem.h"
|
2014-04-15 23:10:58 +02:00
|
|
|
#include "vpx/vp8dx.h"
|
2010-05-24 17:39:59 +02:00
|
|
|
#include "vpx/vpx_integer.h"
|
2015-07-17 23:09:05 +02:00
|
|
|
#include "vpx_dsp/prob.h"
|
2010-05-05 23:58:19 +02:00
|
|
|
|
2014-01-18 21:16:11 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2013-11-26 21:38:58 +01:00
|
|
|
typedef size_t BD_VALUE;
|
|
|
|
|
|
|
|
#define BD_VALUE_SIZE ((int)sizeof(BD_VALUE) * CHAR_BIT)
|
|
|
|
|
2015-07-02 04:10:43 +02: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 00:21:29 +02:00
|
|
|
typedef struct {
|
2014-11-25 04:27:07 +01:00
|
|
|
// Be careful when reordering this struct, it may impact the cache negatively.
|
2013-11-26 21:38:58 +01:00
|
|
|
BD_VALUE value;
|
2013-04-15 23:54:19 +02:00
|
|
|
unsigned int range;
|
2014-11-25 04:27:07 +01:00
|
|
|
int count;
|
|
|
|
const uint8_t *buffer_end;
|
|
|
|
const uint8_t *buffer;
|
2014-04-15 23:10:58 +02:00
|
|
|
vpx_decrypt_cb decrypt_cb;
|
|
|
|
void *decrypt_state;
|
2014-11-25 04:27:07 +01:00
|
|
|
uint8_t clear_buffer[sizeof(BD_VALUE) + 1];
|
2015-07-20 22:49:15 +02:00
|
|
|
} vpx_reader;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2016-07-23 05:07:03 +02:00
|
|
|
int vpx_reader_init(vpx_reader *r, const uint8_t *buffer, size_t size,
|
|
|
|
vpx_decrypt_cb decrypt_cb, void *decrypt_state);
|
2011-02-04 22:00:00 +01:00
|
|
|
|
2015-07-20 22:49:15 +02:00
|
|
|
void vpx_reader_fill(vpx_reader *r);
|
2010-05-05 23:58:19 +02:00
|
|
|
|
2015-07-20 22:49:15 +02:00
|
|
|
const uint8_t *vpx_reader_find_end(vpx_reader *r);
|
2013-04-15 23:54:19 +02:00
|
|
|
|
2015-07-20 22:49:15 +02:00
|
|
|
static INLINE int vpx_reader_has_error(vpx_reader *r) {
|
2015-07-02 04:10:43 +02: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;
|
|
|
|
}
|
|
|
|
|
2015-07-20 22:49:15 +02:00
|
|
|
static INLINE int vpx_read(vpx_reader *r, int prob) {
|
2012-07-14 00:21:29 +02:00
|
|
|
unsigned int bit = 0;
|
2013-11-26 21:38:58 +01:00
|
|
|
BD_VALUE value;
|
|
|
|
BD_VALUE bigsplit;
|
2012-07-14 00:21:29 +02:00
|
|
|
int count;
|
|
|
|
unsigned int range;
|
2013-11-26 21:38:58 +01:00
|
|
|
unsigned int split = (r->range * prob + (256 - prob)) >> CHAR_BIT;
|
2012-07-14 00:21:29 +02:00
|
|
|
|
2016-07-23 05:07:03 +02:00
|
|
|
if (r->count < 0) vpx_reader_fill(r);
|
2012-07-14 00:21:29 +02:00
|
|
|
|
2013-11-26 21:38:58 +01:00
|
|
|
value = r->value;
|
|
|
|
count = r->count;
|
2012-07-14 00:21:29 +02:00
|
|
|
|
2013-11-26 21:38:58 +01:00
|
|
|
bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
|
2012-07-14 00:21:29 +02:00
|
|
|
|
|
|
|
range = split;
|
|
|
|
|
|
|
|
if (value >= bigsplit) {
|
2013-11-26 21:38:58 +01:00
|
|
|
range = r->range - split;
|
2012-07-14 00:21:29 +02:00
|
|
|
value = value - bigsplit;
|
|
|
|
bit = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2015-11-20 00:07:55 +01:00
|
|
|
register int shift = vpx_norm[range];
|
2012-07-14 00:21:29 +02:00
|
|
|
range <<= shift;
|
|
|
|
value <<= shift;
|
|
|
|
count -= shift;
|
|
|
|
}
|
2013-11-26 21:38:58 +01:00
|
|
|
r->value = value;
|
|
|
|
r->count = count;
|
|
|
|
r->range = range;
|
2012-07-14 00:21:29 +02:00
|
|
|
|
|
|
|
return bit;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 22:49:15 +02:00
|
|
|
static INLINE int vpx_read_bit(vpx_reader *r) {
|
|
|
|
return vpx_read(r, 128); // vpx_prob_half
|
2013-04-15 23:54:19 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 22:49:15 +02:00
|
|
|
static INLINE int vpx_read_literal(vpx_reader *r, int bits) {
|
2013-11-26 21:38:58 +01:00
|
|
|
int literal = 0, bit;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2016-07-23 05:07:03 +02:00
|
|
|
for (bit = bits - 1; bit >= 0; bit--) literal |= vpx_read_bit(r) << bit;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2013-11-26 21:38:58 +01:00
|
|
|
return literal;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
2010-12-16 16:46:31 +01:00
|
|
|
|
2015-07-20 23:04:21 +02:00
|
|
|
static INLINE int vpx_read_tree(vpx_reader *r, const vpx_tree_index *tree,
|
2015-07-20 22:49:15 +02:00
|
|
|
const vpx_prob *probs) {
|
2015-07-20 23:04:21 +02:00
|
|
|
vpx_tree_index i = 0;
|
2013-11-26 21:38:58 +01:00
|
|
|
|
2016-07-23 05:07:03 +02:00
|
|
|
while ((i = tree[i + vpx_read(r, probs[i >> 1])]) > 0) continue;
|
2013-11-26 21:38:58 +01:00
|
|
|
|
|
|
|
return -i;
|
|
|
|
}
|
2012-04-12 18:24:03 +02:00
|
|
|
|
2014-01-18 21:16:11 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
2015-07-17 23:09:05 +02:00
|
|
|
#endif // VPX_DSP_BITREADER_H_
|