bitreader.c 3.04 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"
John Koleszar's avatar
John Koleszar committed
16 17
#include "vpx_ports/mem.h"
#include "vpx_mem/vpx_mem.h"
Jim Bankoski's avatar
Jim Bankoski committed
18
#include "vpx_util/endian_inl.h"
John Koleszar's avatar
John Koleszar committed
19

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

40
void vpx_reader_fill(vpx_reader *r) {
41 42
  const uint8_t *const buffer_end = r->buffer_end;
  const uint8_t *buffer = r->buffer;
43
  const uint8_t *buffer_start = buffer;
44
  BD_VALUE value = r->value;
45
  int count = r->count;
46 47
  const size_t bytes_left = buffer_end - buffer;
  const size_t bits_left = bytes_left * CHAR_BIT;
Jim Bankoski's avatar
Jim Bankoski committed
48
  int shift = BD_VALUE_SIZE - CHAR_BIT - (count + CHAR_BIT);
49 50 51 52 53 54 55

  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;
  }
Jim Bankoski's avatar
Jim Bankoski committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  if (bits_left > BD_VALUE_SIZE) {
      const int bits = (shift & 0xfffffff8) + CHAR_BIT;
      BD_VALUE nv;
      BD_VALUE big_endian_values;
      memcpy(&big_endian_values, buffer, sizeof(BD_VALUE));
#if SIZE_MAX == 0xffffffffffffffffULL
        big_endian_values = HToBE64(big_endian_values);
#else
        big_endian_values = HToBE32(big_endian_values);
#endif
      nv = big_endian_values >> (BD_VALUE_SIZE - bits);
      count += bits;
      buffer += (bits >> 3);
      value = r->value | (nv << (shift & 0x7));
  } else {
    const int bits_over = (int)(shift + CHAR_BIT - bits_left);
    int loop_end = 0;
    if (bits_over >= 0) {
      count += LOTS_OF_BITS;
      loop_end = bits_over;
    }
77

Jim Bankoski's avatar
Jim Bankoski committed
78 79 80 81 82 83
    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
84 85
    }
  }
86

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

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