bitreader.c 3 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
 */
Jim Bankoski's avatar
Jim Bankoski committed
10
#include <stdlib.h>
John Koleszar's avatar
John Koleszar committed
11

Jim Bankoski's avatar
Jim Bankoski committed
12 13 14 15
#include "./vpx_config.h"

#include "vpx_dsp/bitreader.h"
#include "vpx_dsp/prob.h"
16
#include "vpx_dsp/vpx_dsp_common.h"
John Koleszar's avatar
John Koleszar committed
17 18
#include "vpx_ports/mem.h"
#include "vpx_mem/vpx_mem.h"
Jim Bankoski's avatar
Jim Bankoski committed
19
#include "vpx_util/endian_inl.h"
John Koleszar's avatar
John Koleszar committed
20

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

Yaowu Xu's avatar
Yaowu Xu committed
38
void vpx_reader_fill(vpx_reader *r) {
39 40
  const uint8_t *const buffer_end = r->buffer_end;
  const uint8_t *buffer = r->buffer;
41
  const uint8_t *buffer_start = buffer;
42
  BD_VALUE value = r->value;
43
  int count = r->count;
44 45
  const size_t bytes_left = buffer_end - buffer;
  const size_t bits_left = bytes_left * CHAR_BIT;
Jim Bankoski's avatar
Jim Bankoski committed
46
  int shift = BD_VALUE_SIZE - CHAR_BIT - (count + CHAR_BIT);
47 48

  if (r->decrypt_cb) {
49
    size_t n = VPXMIN(sizeof(r->clear_buffer), bytes_left);
50 51 52 53
    r->decrypt_cb(r->decrypt_state, buffer, r->clear_buffer, (int)n);
    buffer = r->clear_buffer;
    buffer_start = r->clear_buffer;
  }
Jim Bankoski's avatar
Jim Bankoski committed
54
  if (bits_left > BD_VALUE_SIZE) {
clang-format's avatar
clang-format committed
55 56 57 58
    const int bits = (shift & 0xfffffff8) + CHAR_BIT;
    BD_VALUE nv;
    BD_VALUE big_endian_values;
    memcpy(&big_endian_values, buffer, sizeof(BD_VALUE));
Jim Bankoski's avatar
Jim Bankoski committed
59
#if SIZE_MAX == 0xffffffffffffffffULL
clang-format's avatar
clang-format committed
60
    big_endian_values = HToBE64(big_endian_values);
Jim Bankoski's avatar
Jim Bankoski committed
61
#else
clang-format's avatar
clang-format committed
62
    big_endian_values = HToBE32(big_endian_values);
Jim Bankoski's avatar
Jim Bankoski committed
63
#endif
clang-format's avatar
clang-format committed
64 65 66 67
    nv = big_endian_values >> (BD_VALUE_SIZE - bits);
    count += bits;
    buffer += (bits >> 3);
    value = r->value | (nv << (shift & 0x7));
Jim Bankoski's avatar
Jim Bankoski committed
68
  } else {
69
    const int bits_over = (int)(shift + CHAR_BIT - (int)bits_left);
Jim Bankoski's avatar
Jim Bankoski committed
70 71 72 73 74
    int loop_end = 0;
    if (bits_over >= 0) {
      count += LOTS_OF_BITS;
      loop_end = bits_over;
    }
75

Jim Bankoski's avatar
Jim Bankoski committed
76 77 78 79 80 81
    if (bits_over < 0 || bits_left) {
      while (shift >= loop_end) {
        count += CHAR_BIT;
        value |= (BD_VALUE)*buffer++ << shift;
        shift -= CHAR_BIT;
      }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
82 83
    }
  }
84

85 86 87 88
  // 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;
89 90 91 92
  r->value = value;
  r->count = count;
}

Yaowu Xu's avatar
Yaowu Xu committed
93
const uint8_t *vpx_reader_find_end(vpx_reader *r) {
94
  // Find the end of the coded buffer
95
  while (r->count > CHAR_BIT && r->count < BD_VALUE_SIZE) {
96 97 98 99
    r->count -= CHAR_BIT;
    r->buffer--;
  }
  return r->buffer;
John Koleszar's avatar
John Koleszar committed
100
}