vp9_reader.c 3.42 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9 10 11 12 13
 */

#include "vpx_ports/mem.h"
#include "vpx_mem/vpx_mem.h"

14
#include "vp9/decoder/vp9_reader.h"
Dmitry Kovalev's avatar
Dmitry Kovalev committed
15

16 17 18
// 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.
19
#define LOTS_OF_BITS 0x40000000
20

21 22 23 24 25
int vp9_reader_init(vp9_reader *r,
                    const uint8_t *buffer,
                    size_t size,
                    vpx_decrypt_cb decrypt_cb,
                    void *decrypt_state) {
26
  if (size && !buffer) {
John Koleszar's avatar
John Koleszar committed
27
    return 1;
28 29 30 31 32 33
  } else {
    r->buffer_end = buffer + size;
    r->buffer = buffer;
    r->value = 0;
    r->count = -8;
    r->range = 255;
34 35
    r->decrypt_cb = decrypt_cb;
    r->decrypt_state = decrypt_state;
36 37 38
    vp9_reader_fill(r);
    return vp9_read_bit(r) != 0;  // marker bit
  }
John Koleszar's avatar
John Koleszar committed
39 40
}

41 42 43
void vp9_reader_fill(vp9_reader *r) {
  const uint8_t *const buffer_end = r->buffer_end;
  const uint8_t *buffer = r->buffer;
44
  const uint8_t *buffer_start = buffer;
45
  BD_VALUE value = r->value;
46
  int count = r->count;
47
  int shift = BD_VALUE_SIZE - CHAR_BIT - (count + CHAR_BIT);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
48
  int loop_end = 0;
49 50 51 52 53 54 55 56 57 58
  const size_t bytes_left = buffer_end - buffer;
  const size_t bits_left = bytes_left * CHAR_BIT;
  const int x = (int)(shift + CHAR_BIT - bits_left);

  if (r->decrypt_cb) {
    size_t n = MIN(sizeof(r->clear_buffer), bytes_left);
    r->decrypt_cb(r->decrypt_state, buffer, r->clear_buffer, (int)n);
    buffer = r->clear_buffer;
    buffer_start = r->clear_buffer;
  }
59

Dmitry Kovalev's avatar
Dmitry Kovalev committed
60
  if (x >= 0) {
61
    count += LOTS_OF_BITS;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
62 63 64 65 66 67
    loop_end = x;
  }

  if (x < 0 || bits_left) {
    while (shift >= loop_end) {
      count += CHAR_BIT;
68
      value |= (BD_VALUE)*buffer++ << shift;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
69 70 71
      shift -= CHAR_BIT;
    }
  }
72

73 74 75 76
  // NOTE: Variable 'buffer' may not relate to 'r->buffer' after decryption,
  // so we increase 'r->buffer' by the amount that 'buffer' moved, rather than
  // assign 'buffer' to 'r->buffer'.
  r->buffer += buffer - buffer_start;
77 78 79 80 81 82
  r->value = value;
  r->count = count;
}

const uint8_t *vp9_reader_find_end(vp9_reader *r) {
  // Find the end of the coded buffer
83
  while (r->count > CHAR_BIT && r->count < BD_VALUE_SIZE) {
84 85 86 87
    r->count -= CHAR_BIT;
    r->buffer--;
  }
  return r->buffer;
John Koleszar's avatar
John Koleszar committed
88
}
89

90 91 92 93 94 95 96 97 98 99
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
100 101
  // data, count is additionally filled with LOTS_OF_BITS. So when
  // count == LOTS_OF_BITS - 1, the user's data has been exhausted.
102 103 104
  //
  // 1 if we have tried to decode bits after the end of stream was encountered.
  // 0 No error.
105
  return r->count > BD_VALUE_SIZE && r->count < LOTS_OF_BITS;
106
}