summaryrefslogtreecommitdiff
path: root/src/video_dec/libvdpau/alterh264_bits_reader.h
blob: 77481f6ae8593691a88ebd77f17602d2a9f0829b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* kate: tab-indent on; indent-width 4; mixedindent off; indent-mode cstyle; remove-trailing-space on; */
/*
 * Copyright (C) 2008-2013 the xine project
 *
 * This file is part of xine, a free video player.
 *
 * xine is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * xine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
 *
 */

#ifndef ALTERH264_BITS_READER_H
#define ALTERH264_BITS_READER_H
#include <sys/types.h>
#include <inttypes.h>
#include <stdio.h>



typedef struct {
  const uint8_t *buffer, *start;
  int offbits, length, oflow;
} bits_reader_t;



static void
bits_reader_set (bits_reader_t * br, const uint8_t * buf, int len)
{
  br->buffer = br->start = buf;
  br->offbits = 0;
  br->length = len;
  br->oflow = 0;
}



static inline uint32_t
more_rbsp_data (bits_reader_t * br)
{
  uint8_t val[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
  const uint8_t *buf = br->start + br->length;
  int bit;

  while (--buf >= br->buffer)
  {
    for (bit = 7; bit > -1; bit--)
      if (*buf & val[bit])
	return ((buf - br->buffer) * 8) - br->offbits + bit;
  }
  return 0;
}



static inline uint8_t
bits_reader_shift (bits_reader_t * br)
{
  br->offbits = 0;
  if ((br->buffer + 1) > (br->start + br->length - 1))
  {
    br->oflow = 1;
    //printf("!!!!! buffer overflow !!!!!\n");
    return 0;
  }
  ++br->buffer;
  if ((*(br->buffer) == 3) && ((br->buffer - br->start) > 2)
      && (*(br->buffer - 2) == 0) && (*(br->buffer - 1) == 0))
  {
    if ((br->buffer + 1) > (br->start + br->length - 1))
    {
      br->oflow = 1;
      //printf("!!!!! buffer overflow !!!!!\n");
      return 0;
    }
    ++br->buffer;
  }
  return 1;
}



static inline uint32_t
read_bits (bits_reader_t * br, int nbits)
{
  uint8_t val[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
  uint32_t res = 0;

  while (nbits)
  {
    res = (res << 1) + ((*br->buffer & val[br->offbits]) ? 1 : 0);
    --nbits;
    ++br->offbits;
    if (br->offbits > 7)
      if (!bits_reader_shift (br))
	return 1;
  }
  return res;
}



static inline void
skip_bits (bits_reader_t * br, int nbits)
{
  while (nbits)
  {
    --nbits;
    ++br->offbits;
    if (br->offbits > 7)
      bits_reader_shift (br);
  }
}



static inline uint32_t
read_exp_ue (bits_reader_t * br)
{
  int leading = -1;
  uint8_t b;

  for (b = 0; !b; leading++)
    b = read_bits (br, 1);

  return (1 << leading) - 1 + read_bits (br, leading);
}



static inline int32_t
read_exp_se (bits_reader_t * br)
{
  uint32_t res = read_exp_ue (br);
  return (res & 0x01) ? (res + 1) / 2 : -(res / 2);
}
#endif /* ALTERH264_BITS_READER_H */