1 /***************************************************************************
2 \fn libvaEnc_plugin
3 \brief Plugin to use libva hw encoder (intel mostly)
4 -------------------
5
6 copyright : (C) 2018 by mean
7 email : fixounet@free.fr
8 ***************************************************************************/
9
10 /***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18 /***************************************************************************/
19 /* Derived from libva sample code */
20 /*
21 * Copyright (c) 2007-2013 Intel Corporation. All Rights Reserved.
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a
24 * copy of this software and associated documentation files (the
25 * "Software"), to deal in the Software without restriction, including
26 * without limitation the rights to use, copy, modify, merge, publish,
27 * distribute, sub license, and/or sell copies of the Software, and to
28 * permit persons to whom the Software is furnished to do so, subject to
29 * the following conditions:
30 *
31 * The above copyright notice and this permission notice (including the
32 * next paragraph) shall be included in all copies or substantial portions
33 * of the Software.
34 *
35 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
36 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
37 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
38 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
39 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
40 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
41 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42 */
43 /***************************************************************************
44 * *
45 * This program is free software; you can redistribute it and/or modify *
46 * it under the terms of the GNU General Public License as published by *
47 * the Free Software Foundation; either version 2 of the License, or *
48 * (at your option) any later version. *
49 * *
50 ***************************************************************************/
51 #include "ADM_default.h"
52 #include "va/va.h"
53 #include "ADM_coreLibVA_bitstream.h"
54 /***************************************************************************
55 \fn libvaEnc_plugin
56 \brief Plugin to use libva hw encoder (intel mostly)
57 -------------------
58
59 copyright : (C) 2018 by mean
60 email : fixounet@free.fr
61 ***************************************************************************/
62
63 /***************************************************************************
64 * *
65 * This program is free software; you can redistribute it and/or modify *
66 * it under the terms of the GNU General Public License as published by *
67 * the Free Software Foundation; either version 2 of the License, or *
68 * (at your option) any later version. *
69 * *
70 ***************************************************************************/
71 /***************************************************************************/
72 /* Derived from libva sample code */
73 /*
74 * Copyright (c) 2007-2013 Intel Corporation. All Rights Reserved.
75 *
76 * Permission is hereby granted, free of charge, to any person obtaining a
77 * copy of this software and associated documentation files (the
78 * "Software"), to deal in the Software without restriction, including
79 * without limitation the rights to use, copy, modify, merge, publish,
80 * distribute, sub license, and/or sell copies of the Software, and to
81 * permit persons to whom the Software is furnished to do so, subject to
82 * the following conditions:
83 *
84 * The above copyright notice and this permission notice (including the
85 * next paragraph) shall be included in all copies or substantial portions
86 * of the Software.
87 *
88 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
89 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
90 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
91 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
92 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
93 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
94 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
95 */
96 /***************************************************************************
97 * *
98 * This program is free software; you can redistribute it and/or modify *
99 * it under the terms of the GNU General Public License as published by *
100 * the Free Software Foundation; either version 2 of the License, or *
101 * (at your option) any later version. *
102 * *
103 ***************************************************************************/
104 #define BITSTREAM_ALLOCATE_STEPPING 4096
105
106
107 static unsigned int
va_swap32(unsigned int val)108 va_swap32(unsigned int val)
109 {
110 unsigned char *pval = (unsigned char *)&val;
111
112 return ((pval[0] << 24) |
113 (pval[1] << 16) |
114 (pval[2] << 8) |
115 (pval[3] << 0));
116 }
117
vaBitstream()118 vaBitstream::vaBitstream()
119 {
120 max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
121 buffer = (unsigned int *)calloc(max_size_in_dword * sizeof(int), 1);
122 bit_offset = 0;
123 }
~vaBitstream()124 vaBitstream::~vaBitstream()
125 {
126 free(buffer);
127 buffer=NULL;
128 }
stop()129 void vaBitstream::stop()
130 {
131 int pos = (bit_offset >> 5);
132 int xbit_offset = (bit_offset & 0x1f);
133 int bit_left = 32 - xbit_offset;
134
135 if (xbit_offset)
136 {
137 buffer[pos] = va_swap32((buffer[pos] << bit_left));
138 }
139 }
140
put_ui(unsigned int val,int size_in_bits)141 void vaBitstream::put_ui(unsigned int val, int size_in_bits)
142 {
143 int pos = (bit_offset >> 5);
144 int xbit_offset = (bit_offset & 0x1f);
145 int bit_left = 32 - xbit_offset;
146
147 if (!size_in_bits)
148 return;
149
150 bit_offset += size_in_bits;
151
152 if (bit_left > size_in_bits) {
153 buffer[pos] = (buffer[pos] << size_in_bits | val);
154 } else {
155 size_in_bits -= bit_left;
156 buffer[pos] = (buffer[pos] << bit_left) | (val >> size_in_bits);
157 buffer[pos] = va_swap32(buffer[pos]);
158
159 if (pos + 1 == max_size_in_dword) {
160 max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
161 buffer = (unsigned int *)realloc(buffer, max_size_in_dword * sizeof(unsigned int));
162 }
163
164 buffer[pos + 1] = val;
165 }
166 }
167
put_ue(unsigned int val)168 void vaBitstream::put_ue(unsigned int val)
169 {
170 int size_in_bits = 0;
171 int tmp_val = ++val;
172
173 while (tmp_val) {
174 tmp_val >>= 1;
175 size_in_bits++;
176 }
177
178 put_ui( 0, size_in_bits - 1); // leading zero
179 put_ui( val, size_in_bits);
180 }
181
put_se(int val)182 void vaBitstream::put_se(int val)
183 {
184 unsigned int new_val;
185
186 if (val <= 0)
187 new_val = -2 * val;
188 else
189 new_val = 2 * val - 1;
190
191 put_ue(new_val);
192 }
193
byteAlign(int bit)194 void vaBitstream::byteAlign(int bit)
195 {
196 int xbit_offset = (bit_offset & 0x7);
197 int bit_left = 8 - xbit_offset;
198 int new_val;
199
200 if (!xbit_offset)
201 return;
202
203 assert(bit == 0 || bit == 1);
204
205 if (bit)
206 new_val = (1 << bit_left) - 1;
207 else
208 new_val = 0;
209
210 put_ui(new_val, bit_left);
211 }
212
rbspTrailingBits()213 void vaBitstream::rbspTrailingBits()
214 {
215 put_ui( 1, 1);
216 byteAlign(0);
217 }
218
startCodePrefix()219 void vaBitstream::startCodePrefix()
220 {
221 put_ui( 0x00000001, 32);
222 }
223
nalHeader(int nal_ref_idc,int nal_unit_type)224 void vaBitstream::nalHeader(int nal_ref_idc, int nal_unit_type)
225 {
226 put_ui(0, 1); /* forbidden_zero_bit: 0 */
227 put_ui(nal_ref_idc, 2);
228 put_ui(nal_unit_type, 5);
229 }
230
231 //---
232
233 // EOF
234
235
236