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
|
|
|
*/
|
|
|
|
|
|
|
|
#include "vpx_ports/mem.h"
|
|
|
|
#include "vpx_mem/vpx_mem.h"
|
|
|
|
|
2013-12-20 20:22:25 +01:00
|
|
|
#include "vp9/decoder/vp9_reader.h"
|
2013-03-05 23:12:16 +01:00
|
|
|
|
2013-07-15 21:26:58 +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.
|
2013-08-23 03:40:34 +02:00
|
|
|
#define LOTS_OF_BITS 0x40000000
|
2013-07-15 21:26:58 +02:00
|
|
|
|
2013-04-19 19:37:24 +02:00
|
|
|
int vp9_reader_init(vp9_reader *r, const uint8_t *buffer, size_t size) {
|
2013-11-26 21:38:58 +01:00
|
|
|
if (size && !buffer) {
|
2012-07-14 00:21:29 +02:00
|
|
|
return 1;
|
2013-11-26 21:38:58 +01:00
|
|
|
} else {
|
|
|
|
r->buffer_end = buffer + size;
|
|
|
|
r->buffer = buffer;
|
|
|
|
r->value = 0;
|
|
|
|
r->count = -8;
|
|
|
|
r->range = 255;
|
|
|
|
vp9_reader_fill(r);
|
|
|
|
return vp9_read_bit(r) != 0; // marker bit
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2013-04-19 19:37:24 +02:00
|
|
|
void vp9_reader_fill(vp9_reader *r) {
|
|
|
|
const uint8_t *const buffer_end = r->buffer_end;
|
|
|
|
const uint8_t *buffer = r->buffer;
|
2013-11-26 21:38:58 +01:00
|
|
|
BD_VALUE value = r->value;
|
2013-04-19 19:37:24 +02:00
|
|
|
int count = r->count;
|
2013-11-26 21:38:58 +01:00
|
|
|
int shift = BD_VALUE_SIZE - CHAR_BIT - (count + CHAR_BIT);
|
2013-02-21 22:50:15 +01:00
|
|
|
int loop_end = 0;
|
2013-11-26 21:38:58 +01:00
|
|
|
const int bits_left = (int)((buffer_end - buffer) * CHAR_BIT);
|
2013-04-15 23:54:19 +02:00
|
|
|
const int x = shift + CHAR_BIT - bits_left;
|
2010-05-05 23:58:19 +02:00
|
|
|
|
2013-02-21 22:50:15 +01:00
|
|
|
if (x >= 0) {
|
2013-08-23 03:40:34 +02:00
|
|
|
count += LOTS_OF_BITS;
|
2013-02-21 22:50:15 +01:00
|
|
|
loop_end = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x < 0 || bits_left) {
|
|
|
|
while (shift >= loop_end) {
|
|
|
|
count += CHAR_BIT;
|
2013-11-26 21:38:58 +01:00
|
|
|
value |= (BD_VALUE)*buffer++ << shift;
|
2013-02-21 22:50:15 +01:00
|
|
|
shift -= CHAR_BIT;
|
|
|
|
}
|
|
|
|
}
|
2010-05-05 23:58:19 +02:00
|
|
|
|
2013-04-19 19:37:24 +02:00
|
|
|
r->buffer = buffer;
|
|
|
|
r->value = value;
|
|
|
|
r->count = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *vp9_reader_find_end(vp9_reader *r) {
|
|
|
|
// Find the end of the coded buffer
|
2013-08-23 03:40:34 +02:00
|
|
|
while (r->count > CHAR_BIT && r->count < BD_VALUE_SIZE) {
|
2013-04-19 19:37:24 +02:00
|
|
|
r->count -= CHAR_BIT;
|
|
|
|
r->buffer--;
|
|
|
|
}
|
|
|
|
return r->buffer;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
2012-04-12 18:24:03 +02:00
|
|
|
|
2013-07-15 21:26:58 +02:00
|
|
|
int vp9_reader_has_error(vp9_reader *r) {
|
|
|
|
// 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
|
2013-08-23 03:40:34 +02:00
|
|
|
// data, count is additionally filled with LOTS_OF_BITS. So when
|
|
|
|
// count == LOTS_OF_BITS - 1, the user's data has been exhausted.
|
2013-07-15 21:26:58 +02:00
|
|
|
//
|
|
|
|
// 1 if we have tried to decode bits after the end of stream was encountered.
|
|
|
|
// 0 No error.
|
2013-08-23 03:40:34 +02:00
|
|
|
return r->count > BD_VALUE_SIZE && r->count < LOTS_OF_BITS;
|
2013-07-15 21:26:58 +02:00
|
|
|
}
|