1af69d88dSmrg/**************************************************************************
2af69d88dSmrg *
3af69d88dSmrg * Copyright 2013 Advanced Micro Devices, Inc.
4af69d88dSmrg * All Rights Reserved.
5af69d88dSmrg *
6af69d88dSmrg * Permission is hereby granted, free of charge, to any person obtaining a
7af69d88dSmrg * copy of this software and associated documentation files (the
8af69d88dSmrg * "Software"), to deal in the Software without restriction, including
9af69d88dSmrg * without limitation the rights to use, copy, modify, merge, publish,
10af69d88dSmrg * distribute, sub license, and/or sell copies of the Software, and to
11af69d88dSmrg * permit persons to whom the Software is furnished to do so, subject to
12af69d88dSmrg * the following conditions:
13af69d88dSmrg *
14af69d88dSmrg * The above copyright notice and this permission notice (including the
15af69d88dSmrg * next paragraph) shall be included in all copies or substantial portions
16af69d88dSmrg * of the Software.
17af69d88dSmrg *
18af69d88dSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19af69d88dSmrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20af69d88dSmrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21af69d88dSmrg * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22af69d88dSmrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23af69d88dSmrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24af69d88dSmrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25af69d88dSmrg *
26af69d88dSmrg **************************************************************************/
27af69d88dSmrg
28af69d88dSmrg/*
29af69d88dSmrg * Authors:
30af69d88dSmrg *      Christian König <christian.koenig@amd.com>
31af69d88dSmrg *
32af69d88dSmrg */
33af69d88dSmrg
34af69d88dSmrg/*
35af69d88dSmrg * Functions for reading the raw byte sequence payload of H.264
36af69d88dSmrg */
37af69d88dSmrg
38af69d88dSmrg#ifndef vl_rbsp_h
39af69d88dSmrg#define vl_rbsp_h
40af69d88dSmrg
41af69d88dSmrg#include "vl/vl_vlc.h"
42af69d88dSmrg
43af69d88dSmrgstruct vl_rbsp {
44af69d88dSmrg   struct vl_vlc nal;
45af69d88dSmrg   unsigned escaped;
46af69d88dSmrg};
47af69d88dSmrg
48af69d88dSmrg/**
49af69d88dSmrg * Initialize the RBSP object
50af69d88dSmrg */
5101e04c3fSmrgstatic inline void vl_rbsp_init(struct vl_rbsp *rbsp, struct vl_vlc *nal, unsigned num_bits)
52af69d88dSmrg{
5301e04c3fSmrg   unsigned valid, bits_left = vl_vlc_bits_left(nal);
5401e04c3fSmrg   int i;
55af69d88dSmrg
56af69d88dSmrg   /* copy the position */
57af69d88dSmrg   rbsp->nal = *nal;
58af69d88dSmrg
59af69d88dSmrg   /* search for the end of the NAL unit */
60af69d88dSmrg   while (vl_vlc_search_byte(nal, num_bits, 0x00)) {
61af69d88dSmrg      if (vl_vlc_peekbits(nal, 24) == 0x000001 ||
62af69d88dSmrg          vl_vlc_peekbits(nal, 32) == 0x00000001) {
63af69d88dSmrg         vl_vlc_limit(&rbsp->nal, bits_left - vl_vlc_bits_left(nal));
6401e04c3fSmrg         break;
65af69d88dSmrg      }
66af69d88dSmrg      vl_vlc_eatbits(nal, 8);
67af69d88dSmrg   }
6801e04c3fSmrg
6901e04c3fSmrg   valid = vl_vlc_valid_bits(&rbsp->nal);
7001e04c3fSmrg   /* search for the emulation prevention three byte */
7101e04c3fSmrg   for (i = 24; i <= valid; i += 8) {
7201e04c3fSmrg      if ((vl_vlc_peekbits(&rbsp->nal, i) & 0xffffff) == 0x3) {
7301e04c3fSmrg         vl_vlc_removebits(&rbsp->nal, i - 8, 8);
7401e04c3fSmrg         i += 8;
7501e04c3fSmrg      }
7601e04c3fSmrg   }
7701e04c3fSmrg
7801e04c3fSmrg   valid = vl_vlc_valid_bits(&rbsp->nal);
7901e04c3fSmrg
8001e04c3fSmrg   rbsp->escaped = (valid >= 16) ? 16 : ((valid >= 8) ? 8 : 0);
81af69d88dSmrg}
82af69d88dSmrg
83af69d88dSmrg/**
84af69d88dSmrg * Make at least 16 more bits available
85af69d88dSmrg */
8601e04c3fSmrgstatic inline void vl_rbsp_fillbits(struct vl_rbsp *rbsp)
87af69d88dSmrg{
88af69d88dSmrg   unsigned valid = vl_vlc_valid_bits(&rbsp->nal);
89af69d88dSmrg   unsigned i, bits;
90af69d88dSmrg
91af69d88dSmrg   /* abort if we still have enough bits */
92af69d88dSmrg   if (valid >= 32)
93af69d88dSmrg      return;
94af69d88dSmrg
95af69d88dSmrg   vl_vlc_fillbits(&rbsp->nal);
96af69d88dSmrg
97af69d88dSmrg   /* abort if we have less than 24 bits left in this nal */
98af69d88dSmrg   if (vl_vlc_bits_left(&rbsp->nal) < 24)
99af69d88dSmrg      return;
100af69d88dSmrg
101af69d88dSmrg   /* check that we have enough bits left from the last fillbits */
102af69d88dSmrg   assert(valid >= rbsp->escaped);
103af69d88dSmrg
104af69d88dSmrg   /* handle the already escaped bits */
105af69d88dSmrg   valid -= rbsp->escaped;
106af69d88dSmrg
107af69d88dSmrg   /* search for the emulation prevention three byte */
108af69d88dSmrg   rbsp->escaped = 16;
109af69d88dSmrg   bits = vl_vlc_valid_bits(&rbsp->nal);
110af69d88dSmrg   for (i = valid + 24; i <= bits; i += 8) {
111af69d88dSmrg      if ((vl_vlc_peekbits(&rbsp->nal, i) & 0xffffff) == 0x3) {
112af69d88dSmrg         vl_vlc_removebits(&rbsp->nal, i - 8, 8);
113af69d88dSmrg         rbsp->escaped = bits - i;
114af69d88dSmrg         bits -= 8;
115af69d88dSmrg         i += 8;
116af69d88dSmrg      }
117af69d88dSmrg   }
118af69d88dSmrg}
119af69d88dSmrg
120af69d88dSmrg/**
121af69d88dSmrg * Return an unsigned integer from the first n bits
122af69d88dSmrg */
12301e04c3fSmrgstatic inline unsigned vl_rbsp_u(struct vl_rbsp *rbsp, unsigned n)
124af69d88dSmrg{
125af69d88dSmrg   if (n == 0)
126af69d88dSmrg      return 0;
127af69d88dSmrg
128af69d88dSmrg   vl_rbsp_fillbits(rbsp);
129af69d88dSmrg   return vl_vlc_get_uimsbf(&rbsp->nal, n);
130af69d88dSmrg}
131af69d88dSmrg
132af69d88dSmrg/**
133af69d88dSmrg * Return an unsigned exponential Golomb encoded integer
134af69d88dSmrg */
13501e04c3fSmrgstatic inline unsigned vl_rbsp_ue(struct vl_rbsp *rbsp)
136af69d88dSmrg{
137af69d88dSmrg   unsigned bits = 0;
138af69d88dSmrg
139af69d88dSmrg   vl_rbsp_fillbits(rbsp);
140af69d88dSmrg   while (!vl_vlc_get_uimsbf(&rbsp->nal, 1))
141af69d88dSmrg      ++bits;
142af69d88dSmrg
143af69d88dSmrg   return (1 << bits) - 1 + vl_rbsp_u(rbsp, bits);
144af69d88dSmrg}
145af69d88dSmrg
146af69d88dSmrg/**
147af69d88dSmrg * Return an signed exponential Golomb encoded integer
148af69d88dSmrg */
14901e04c3fSmrgstatic inline signed vl_rbsp_se(struct vl_rbsp *rbsp)
150af69d88dSmrg{
151af69d88dSmrg   signed codeNum = vl_rbsp_ue(rbsp);
152af69d88dSmrg   if (codeNum & 1)
153af69d88dSmrg      return (codeNum + 1) >> 1;
154af69d88dSmrg   else
155af69d88dSmrg      return -(codeNum >> 1);
156af69d88dSmrg}
157af69d88dSmrg
158af69d88dSmrg/**
159af69d88dSmrg * Are more data available in the RBSP ?
160af69d88dSmrg */
16101e04c3fSmrgstatic inline bool vl_rbsp_more_data(struct vl_rbsp *rbsp)
162af69d88dSmrg{
163af69d88dSmrg   unsigned bits, value;
164af69d88dSmrg
165af69d88dSmrg   if (vl_vlc_bits_left(&rbsp->nal) > 8)
166af69d88dSmrg      return TRUE;
167af69d88dSmrg
168af69d88dSmrg   bits = vl_vlc_valid_bits(&rbsp->nal);
169af69d88dSmrg   value = vl_vlc_peekbits(&rbsp->nal, bits);
170af69d88dSmrg   if (value == 0 || value == (1 << (bits - 1)))
171af69d88dSmrg      return FALSE;
172af69d88dSmrg
173af69d88dSmrg   return TRUE;
174af69d88dSmrg}
175af69d88dSmrg
176af69d88dSmrg#endif /* vl_rbsp_h */
177