xine-lib  1.2.9
alterh264_bits_reader.h
Go to the documentation of this file.
1 /* kate: tab-indent on; indent-width 4; mixedindent off; indent-mode cstyle; remove-trailing-space on; */
2 /*
3  * Copyright (C) 2008-2013 the xine project
4  *
5  * This file is part of xine, a free video player.
6  *
7  * xine is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * xine is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20  *
21  */
22 
23 #ifndef ALTERH264_BITS_READER_H
24 #define ALTERH264_BITS_READER_H
25 #include <sys/types.h>
26 #include <inttypes.h>
27 #include <stdio.h>
28 
29 
30 
31 typedef struct {
32  const uint8_t *buffer, *start;
33  int offbits, length, oflow;
35 
36 
37 
38 static void
39 bits_reader_set (bits_reader_t * br, const uint8_t * buf, int len)
40 {
41  br->buffer = br->start = buf;
42  br->offbits = 0;
43  br->length = len;
44  br->oflow = 0;
45 }
46 
47 
48 
49 static inline uint32_t
51 {
52  uint8_t val[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
53  const uint8_t *buf = br->start + br->length;
54  int bit;
55 
56  while (--buf >= br->buffer)
57  {
58  for (bit = 7; bit > -1; bit--)
59  if (*buf & val[bit])
60  return ((buf - br->buffer) * 8) - br->offbits + bit;
61  }
62  return 0;
63 }
64 
65 
66 
67 static inline uint8_t
69 {
70  br->offbits = 0;
71  if ((br->buffer + 1) > (br->start + br->length - 1))
72  {
73  br->oflow = 1;
74  //printf("!!!!! buffer overflow !!!!!\n");
75  return 0;
76  }
77  ++br->buffer;
78  if ((*(br->buffer) == 3) && ((br->buffer - br->start) > 2)
79  && (*(br->buffer - 2) == 0) && (*(br->buffer - 1) == 0))
80  {
81  if ((br->buffer + 1) > (br->start + br->length - 1))
82  {
83  br->oflow = 1;
84  //printf("!!!!! buffer overflow !!!!!\n");
85  return 0;
86  }
87  ++br->buffer;
88  }
89  return 1;
90 }
91 
92 
93 
94 static inline uint32_t
95 read_bits (bits_reader_t * br, int nbits)
96 {
97  uint8_t val[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
98  uint32_t res = 0;
99 
100  while (nbits)
101  {
102  res = (res << 1) + ((*br->buffer & val[br->offbits]) ? 1 : 0);
103  --nbits;
104  ++br->offbits;
105  if (br->offbits > 7)
106  if (!bits_reader_shift (br))
107  return 1;
108  }
109  return res;
110 }
111 
112 
113 
114 static inline void
115 skip_bits (bits_reader_t * br, int nbits)
116 {
117  while (nbits)
118  {
119  --nbits;
120  ++br->offbits;
121  if (br->offbits > 7)
122  bits_reader_shift (br);
123  }
124 }
125 
126 
127 
128 static inline uint32_t
130 {
131  int leading = -1;
132  uint8_t b;
133 
134  for (b = 0; !b; leading++)
135  b = read_bits (br, 1);
136 
137  return (1 << leading) - 1 + read_bits (br, leading);
138 }
139 
140 
141 
142 static inline int32_t
144 {
145  uint32_t res = read_exp_ue (br);
146  return (res & 0x01) ? (res + 1) / 2 : -(res / 2);
147 }
148 #endif /* ALTERH264_BITS_READER_H */
static uint32_t read_exp_ue(bits_reader_t *br)
Definition: alterh264_bits_reader.h:129
int length
Definition: alterh264_bits_reader.h:33
const uint8_t * start
Definition: alterh264_bits_reader.h:32
static uint8_t bits_reader_shift(bits_reader_t *br)
Definition: alterh264_bits_reader.h:68
const uint8_t * buffer
Definition: alterh264_bits_reader.h:32
static void bits_reader_set(bits_reader_t *br, const uint8_t *buf, int len)
Definition: alterh264_bits_reader.h:39
Definition: alterh264_bits_reader.h:31
static uint32_t read_bits(bits_reader_t *br, int nbits)
Definition: alterh264_bits_reader.h:95
static int32_t read_exp_se(bits_reader_t *br)
Definition: alterh264_bits_reader.h:143
static void skip_bits(bits_reader_t *br, int nbits)
Definition: alterh264_bits_reader.h:115
int oflow
Definition: alterh264_bits_reader.h:33
int offbits
Definition: alterh264_bits_reader.h:33
static uint32_t more_rbsp_data(bits_reader_t *br)
Definition: alterh264_bits_reader.h:50