vp9_reader.c 2.36 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
19
20
int vp9_reader_init(vp9_reader *r,
                    const uint8_t *buffer,
                    size_t size,
                    vpx_decrypt_cb decrypt_cb,
                    void *decrypt_state) {
21
  if (size && !buffer) {
John Koleszar's avatar
John Koleszar committed
22
    return 1;
23
24
25
26
27
28
  } else {
    r->buffer_end = buffer + size;
    r->buffer = buffer;
    r->value = 0;
    r->count = -8;
    r->range = 255;
29
30
    r->decrypt_cb = decrypt_cb;
    r->decrypt_state = decrypt_state;
31
32
33
    vp9_reader_fill(r);
    return vp9_read_bit(r) != 0;  // marker bit
  }
John Koleszar's avatar
John Koleszar committed
34
35
}

36
37
38
void vp9_reader_fill(vp9_reader *r) {
  const uint8_t *const buffer_end = r->buffer_end;
  const uint8_t *buffer = r->buffer;
39
  const uint8_t *buffer_start = buffer;
40
  BD_VALUE value = r->value;
41
  int count = r->count;
42
  int shift = BD_VALUE_SIZE - CHAR_BIT - (count + CHAR_BIT);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
43
  int loop_end = 0;
44
45
46
47
48
49
50
51
52
53
  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;
  }
54

Dmitry Kovalev's avatar
Dmitry Kovalev committed
55
  if (x >= 0) {
56
    count += LOTS_OF_BITS;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
57
58
59
60
61
62
    loop_end = x;
  }

  if (x < 0 || bits_left) {
    while (shift >= loop_end) {
      count += CHAR_BIT;
63
      value |= (BD_VALUE)*buffer++ << shift;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
64
65
66
      shift -= CHAR_BIT;
    }
  }
67

68
69
70
71
  // 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;
72
73
74
75
76
77
  r->value = value;
  r->count = count;
}

const uint8_t *vp9_reader_find_end(vp9_reader *r) {
  // Find the end of the coded buffer
78
  while (r->count > CHAR_BIT && r->count < BD_VALUE_SIZE) {
79
80
81
82
    r->count -= CHAR_BIT;
    r->buffer--;
  }
  return r->buffer;
John Koleszar's avatar
John Koleszar committed
83
}