xine-lib  1.2.10
bits_reader.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2013 the xine project
3  *
4  * This file is part of xine, a free video player.
5  *
6  * xine is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * xine is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19  *
20  */
21 
22 #include <sys/types.h>
23 
24 
25 
26 typedef struct {
27  const uint8_t *buffer, *start;
28  int offbits, length, oflow;
30 
31 
32 
33 static void bits_reader_set( bits_reader_t *br, const uint8_t *buf, int len )
34 {
35  br->buffer = br->start = buf;
36  br->offbits = 0;
37  br->length = len;
38  br->oflow = 0;
39 }
40 
41 
42 
43 static void skip_bits( bits_reader_t *br, int nbits )
44 {
45  br->offbits += nbits;
46  br->buffer += br->offbits / 8;
47  br->offbits %= 8;
48  if ( br->buffer > (br->start + br->length) ) {
49  br->oflow = 1;
50  }
51 }
52 
53 
54 
55 static uint32_t get_bits( bits_reader_t *br, int nbits )
56 {
57  int i, nbytes;
58  uint32_t ret = 0;
59  const uint8_t *buf;
60 
61  buf = br->buffer;
62  nbytes = (br->offbits + nbits)/8;
63  if ( ((br->offbits + nbits) %8 ) > 0 )
64  nbytes++;
65  if ( (buf + nbytes) > (br->start + br->length) ) {
66  br->oflow = 1;
67  return 0;
68  }
69  for ( i=0; i<nbytes; i++ )
70  ret += buf[i]<<((nbytes-i-1)*8);
71  i = (4-nbytes)*8+br->offbits;
72  ret = ((ret<<i)>>i)>>((nbytes*8)-nbits-br->offbits);
73 
74  return ret;
75 }
76 
77 
78 
79 static uint32_t read_bits( bits_reader_t *br, int nbits )
80 {
81  uint32_t ret = get_bits(br, nbits);
82 
83  br->offbits += nbits;
84  br->buffer += br->offbits / 8;
85  br->offbits %= 8;
86 
87  return ret;
88 }
bits_reader_t
Definition: alterh264_bits_reader.h:31
get_bits
static uint32_t get_bits(bits_reader_t *br, int nbits)
Definition: bits_reader.h:55
bits_reader_t::oflow
int oflow
Definition: alterh264_bits_reader.h:33
read_bits
static uint32_t read_bits(bits_reader_t *br, int nbits)
Definition: bits_reader.h:79
bits_reader_t::buffer
const uint8_t * buffer
Definition: alterh264_bits_reader.h:32
bits_reader_set
static void bits_reader_set(bits_reader_t *br, const uint8_t *buf, int len)
Definition: bits_reader.h:33
bits_reader_t::offbits
int offbits
Definition: alterh264_bits_reader.h:33
bits_reader_t::length
int length
Definition: alterh264_bits_reader.h:33
skip_bits
static void skip_bits(bits_reader_t *br, int nbits)
Definition: bits_reader.h:43
bits_reader_t::start
const uint8_t * start
Definition: alterh264_bits_reader.h:32