media/libvpx/vp9/decoder/vp9_read_bit_buffer.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libvpx/vp9/decoder/vp9_read_bit_buffer.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,60 @@
     1.4 +/*
     1.5 + *  Copyright (c) 2013 The WebM project authors. All Rights Reserved.
     1.6 + *
     1.7 + *  Use of this source code is governed by a BSD-style license
     1.8 + *  that can be found in the LICENSE file in the root of the source
     1.9 + *  tree. An additional intellectual property rights grant can be found
    1.10 + *  in the file PATENTS.  All contributing project authors may
    1.11 + *  be found in the AUTHORS file in the root of the source tree.
    1.12 + */
    1.13 +
    1.14 +#ifndef VP9_DECODER_VP9_READ_BIT_BUFFER_H_
    1.15 +#define VP9_DECODER_VP9_READ_BIT_BUFFER_H_
    1.16 +
    1.17 +#include <limits.h>
    1.18 +
    1.19 +#include "vpx/vpx_integer.h"
    1.20 +
    1.21 +typedef void (*vp9_rb_error_handler)(void *data, size_t bit_offset);
    1.22 +
    1.23 +struct vp9_read_bit_buffer {
    1.24 +  const uint8_t *bit_buffer;
    1.25 +  const uint8_t *bit_buffer_end;
    1.26 +  size_t bit_offset;
    1.27 +
    1.28 +  void *error_handler_data;
    1.29 +  vp9_rb_error_handler error_handler;
    1.30 +};
    1.31 +
    1.32 +static size_t vp9_rb_bytes_read(struct vp9_read_bit_buffer *rb) {
    1.33 +  return rb->bit_offset / CHAR_BIT + (rb->bit_offset % CHAR_BIT > 0);
    1.34 +}
    1.35 +
    1.36 +static int vp9_rb_read_bit(struct vp9_read_bit_buffer *rb) {
    1.37 +  const size_t off = rb->bit_offset;
    1.38 +  const size_t p = off / CHAR_BIT;
    1.39 +  const int q = CHAR_BIT - 1 - (int)off % CHAR_BIT;
    1.40 +  if (rb->bit_buffer + p >= rb->bit_buffer_end) {
    1.41 +    rb->error_handler(rb->error_handler_data, rb->bit_offset);
    1.42 +    return 0;
    1.43 +  } else {
    1.44 +    const int bit = (rb->bit_buffer[p] & (1 << q)) >> q;
    1.45 +    rb->bit_offset = off + 1;
    1.46 +    return bit;
    1.47 +  }
    1.48 +}
    1.49 +
    1.50 +static int vp9_rb_read_literal(struct vp9_read_bit_buffer *rb, int bits) {
    1.51 +  int value = 0, bit;
    1.52 +  for (bit = bits - 1; bit >= 0; bit--)
    1.53 +    value |= vp9_rb_read_bit(rb) << bit;
    1.54 +  return value;
    1.55 +}
    1.56 +
    1.57 +static int vp9_rb_read_signed_literal(struct vp9_read_bit_buffer *rb,
    1.58 +                                      int bits) {
    1.59 +  const int value = vp9_rb_read_literal(rb, bits);
    1.60 +  return vp9_rb_read_bit(rb) ? -value : value;
    1.61 +}
    1.62 +
    1.63 +#endif  // VP9_DECODER_VP9_READ_BIT_BUFFER_H_

mercurial