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
|
|
|
*/
|
|
|
|
|
2013-12-20 20:22:25 +01:00
|
|
|
#ifndef VP9_DECODER_VP9_READER_H_
|
|
|
|
#define VP9_DECODER_VP9_READER_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"
|
2010-05-24 17:39:59 +02:00
|
|
|
#include "vpx/vpx_integer.h"
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2013-12-16 21:53:09 +01:00
|
|
|
#include "vp9/common/vp9_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)
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
typedef struct {
|
2013-04-15 23:54:19 +02:00
|
|
|
const uint8_t *buffer_end;
|
|
|
|
const uint8_t *buffer;
|
2013-11-26 21:38:58 +01:00
|
|
|
BD_VALUE value;
|
2013-04-15 23:54:19 +02:00
|
|
|
int count;
|
|
|
|
unsigned int range;
|
2013-04-19 19:37:24 +02:00
|
|
|
} vp9_reader;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2013-04-19 19:37:24 +02:00
|
|
|
int vp9_reader_init(vp9_reader *r, const uint8_t *buffer, size_t size);
|
2011-02-04 22:00:00 +01:00
|
|
|
|
2013-04-19 19:37:24 +02:00
|
|
|
void vp9_reader_fill(vp9_reader *r);
|
2010-05-05 23:58:19 +02:00
|
|
|
|
2013-11-26 21:38:58 +01:00
|
|
|
int vp9_reader_has_error(vp9_reader *r);
|
|
|
|
|
2013-04-19 19:37:24 +02:00
|
|
|
const uint8_t *vp9_reader_find_end(vp9_reader *r);
|
2013-04-15 23:54:19 +02:00
|
|
|
|
2013-11-26 21:38:58 +01:00
|
|
|
static int vp9_read(vp9_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
|
|
|
|
2013-11-26 21:38:58 +01:00
|
|
|
if (r->count < 0)
|
|
|
|
vp9_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2012-10-31 01:12:12 +01:00
|
|
|
register unsigned int shift = vp9_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
|
|
|
}
|
|
|
|
|
2013-04-19 19:37:24 +02:00
|
|
|
static int vp9_read_bit(vp9_reader *r) {
|
2013-04-15 23:54:19 +02:00
|
|
|
return vp9_read(r, 128); // vp9_prob_half
|
|
|
|
}
|
|
|
|
|
2013-11-26 21:38:58 +01:00
|
|
|
static int vp9_read_literal(vp9_reader *r, int bits) {
|
|
|
|
int literal = 0, bit;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2013-04-19 19:37:24 +02:00
|
|
|
for (bit = bits - 1; bit >= 0; bit--)
|
2013-11-26 21:38:58 +01:00
|
|
|
literal |= vp9_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
|
|
|
|
2013-11-26 21:38:58 +01:00
|
|
|
static int vp9_read_tree(vp9_reader *r, const vp9_tree_index *tree,
|
|
|
|
const vp9_prob *probs) {
|
|
|
|
vp9_tree_index i = 0;
|
|
|
|
|
|
|
|
while ((i = tree[i + vp9_read(r, probs[i >> 1])]) > 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return -i;
|
|
|
|
}
|
2012-04-12 18:24:03 +02:00
|
|
|
|
2014-01-18 21:16:11 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
2013-12-20 20:22:25 +01:00
|
|
|
#endif // VP9_DECODER_VP9_READER_H_
|