Merge branch 'master' into vaapi-ext
[platform/upstream/libva-intel-driver.git] / src / i965_encoder_utils.c
1 /*
2  * Copyright © 2011 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */
25
26 #include <stdlib.h>
27 #include <assert.h>
28
29 #include <va/va.h>
30
31 #include "i965_encoder_utils.h"
32
33 #define BITSTREAM_ALLOCATE_STEPPING     4096
34
35 #define NAL_REF_IDC_NONE        0
36 #define NAL_REF_IDC_LOW         1
37 #define NAL_REF_IDC_MEDIUM      2
38 #define NAL_REF_IDC_HIGH        3
39
40 #define NAL_NON_IDR             1
41 #define NAL_IDR                 5
42 #define NAL_SPS                 7
43 #define NAL_PPS                 8
44
45 #define SLICE_TYPE_P            0
46 #define SLICE_TYPE_B            1
47 #define SLICE_TYPE_I            2
48
49 #define IS_I_SLICE(type) (SLICE_TYPE_I == (type) || SLICE_TYPE_I == (type - 5))
50 #define IS_P_SLICE(type) (SLICE_TYPE_P == (type) || SLICE_TYPE_P == (type - 5))
51 #define IS_B_SLICE(type) (SLICE_TYPE_B == (type) || SLICE_TYPE_B == (type - 5))
52
53 #define ENTROPY_MODE_CAVLC      0
54 #define ENTROPY_MODE_CABAC      1
55
56 #define PROFILE_IDC_BASELINE    66
57 #define PROFILE_IDC_MAIN        77
58 #define PROFILE_IDC_HIGH        100
59
60 struct __avc_bitstream {
61     unsigned int *buffer;
62     int bit_offset;
63     int max_size_in_dword;
64 };
65
66 typedef struct __avc_bitstream avc_bitstream;
67
68 static unsigned int 
69 swap32(unsigned int val)
70 {
71     unsigned char *pval = (unsigned char *)&val;
72
73     return ((pval[0] << 24)     |
74             (pval[1] << 16)     |
75             (pval[2] << 8)      |
76             (pval[3] << 0));
77 }
78
79 static void
80 avc_bitstream_start(avc_bitstream *bs)
81 {
82     bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
83     bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
84     bs->bit_offset = 0;
85 }
86
87 static void
88 avc_bitstream_end(avc_bitstream *bs)
89 {
90     int pos = (bs->bit_offset >> 5);
91     int bit_offset = (bs->bit_offset & 0x1f);
92     int bit_left = 32 - bit_offset;
93
94     if (bit_offset) {
95         bs->buffer[pos] = swap32((bs->buffer[pos] << bit_left));
96     }
97
98     // free(bs->buffer);
99 }
100  
101 static void
102 avc_bitstream_put_ui(avc_bitstream *bs, unsigned int val, int size_in_bits)
103 {
104     int pos = (bs->bit_offset >> 5);
105     int bit_offset = (bs->bit_offset & 0x1f);
106     int bit_left = 32 - bit_offset;
107
108     if (!size_in_bits)
109         return;
110
111     if (size_in_bits < 32)
112         val &= (( 1 << size_in_bits) - 1);
113
114     bs->bit_offset += size_in_bits;
115
116     if (bit_left > size_in_bits) {
117         bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
118     } else {
119         size_in_bits -= bit_left;
120         bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
121         bs->buffer[pos] = swap32(bs->buffer[pos]);
122
123         if (pos + 1 == bs->max_size_in_dword) {
124             bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
125             bs->buffer = realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
126         }
127
128         bs->buffer[pos + 1] = val;
129     }
130 }
131
132 static void
133 avc_bitstream_put_ue(avc_bitstream *bs, unsigned int val)
134 {
135     int size_in_bits = 0;
136     int tmp_val = ++val;
137
138     while (tmp_val) {
139         tmp_val >>= 1;
140         size_in_bits++;
141     }
142
143     avc_bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
144     avc_bitstream_put_ui(bs, val, size_in_bits);
145 }
146
147 static void
148 avc_bitstream_put_se(avc_bitstream *bs, int val)
149 {
150     unsigned int new_val;
151
152     if (val <= 0)
153         new_val = -2 * val;
154     else
155         new_val = 2 * val - 1;
156
157     avc_bitstream_put_ue(bs, new_val);
158 }
159
160 static void
161 avc_bitstream_byte_aligning(avc_bitstream *bs, int bit)
162 {
163     int bit_offset = (bs->bit_offset & 0x7);
164     int bit_left = 8 - bit_offset;
165     int new_val;
166
167     if (!bit_offset)
168         return;
169
170     assert(bit == 0 || bit == 1);
171
172     if (bit)
173         new_val = (1 << bit_left) - 1;
174     else
175         new_val = 0;
176
177     avc_bitstream_put_ui(bs, new_val, bit_left);
178 }
179
180 static void nal_start_code_prefix(avc_bitstream *bs)
181 {
182     avc_bitstream_put_ui(bs, 0x00000001, 32);
183 }
184
185 static void nal_header(avc_bitstream *bs, int nal_ref_idc, int nal_unit_type)
186 {
187     avc_bitstream_put_ui(bs, 0, 1);                /* forbidden_zero_bit: 0 */
188     avc_bitstream_put_ui(bs, nal_ref_idc, 2);
189     avc_bitstream_put_ui(bs, nal_unit_type, 5);
190 }
191
192 static void 
193 slice_header(avc_bitstream *bs,
194              VAEncSequenceParameterBufferH264 *sps_param,
195              VAEncPictureParameterBufferH264 *pic_param,
196              VAEncSliceParameterBufferH264 *slice_param,
197              VAEncH264DecRefPicMarkingBuffer *dec_ref_pic_marking_param)
198 {
199     int first_mb_in_slice = slice_param->starting_macroblock_address;
200
201     avc_bitstream_put_ue(bs, first_mb_in_slice);        /* first_mb_in_slice: 0 */
202     avc_bitstream_put_ue(bs, slice_param->slice_type);  /* slice_type */
203     avc_bitstream_put_ue(bs, slice_param->pic_parameter_set_id);        /* pic_parameter_set_id: 0 */
204     avc_bitstream_put_ui(bs, pic_param->frame_num, sps_param->log2_max_frame_num_minus4 + 4); /* frame_num */
205
206     /* frame_mbs_only_flag == 1 */
207     if (!sps_param->frame_mbs_only_flag) {
208         /* FIXME: */
209         assert(0);
210     }
211
212     if (pic_param->pic_fields.bits.idr_pic_flag)
213         avc_bitstream_put_ue(bs, slice_param->idr_pic_id);              /* idr_pic_id: 0 */
214
215     if (sps_param->pic_order_cnt_type == 0) {
216         avc_bitstream_put_ui(bs, pic_param->CurrPic.TopFieldOrderCnt, sps_param->log2_max_pic_order_cnt_lsb_minus4 + 4);
217         /* pic_order_present_flag == 0 */
218     }
219
220     if (sps_param->pic_order_cnt_type == 1 && !sps_param->seq_fields.bits.delta_pic_order_always_zero_flag) {
221         /* FIXME: pack delta pic information */
222         assert(0);
223     }
224
225     /* redundant_pic_cnt_present_flag == 0 */
226     
227     /* slice type */
228     if (IS_P_SLICE(slice_param->slice_type)) {
229         avc_bitstream_put_ui(bs, 0, 1);            /* num_ref_idx_active_override_flag: 0 */
230
231         /* ref_pic_list_reordering */
232         assert(slice_param->ref_pic_list_modification_flag_l0 == 0);
233         avc_bitstream_put_ui(bs, slice_param->ref_pic_list_modification_flag_l0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
234     } else if (IS_B_SLICE(slice_param->slice_type)) {
235         avc_bitstream_put_ui(bs, slice_param->direct_spatial_mv_pred_flag, 1);            /* direct_spatial_mv_pred: 1 */
236         avc_bitstream_put_ui(bs, 0, 1);            /* num_ref_idx_active_override_flag: 0 */
237
238         /* ref_pic_list_reordering */
239         assert(slice_param->ref_pic_list_modification_flag_l0 == 0);
240         assert(slice_param->ref_pic_list_modification_flag_l1 == 0);
241         avc_bitstream_put_ui(bs, slice_param->ref_pic_list_modification_flag_l0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
242         avc_bitstream_put_ui(bs, slice_param->ref_pic_list_modification_flag_l1, 1);            /* ref_pic_list_reordering_flag_l1: 0 */
243     } 
244
245     if ((pic_param->pic_fields.bits.weighted_pred_flag && 
246          IS_P_SLICE(slice_param->slice_type)) ||
247         ((pic_param->pic_fields.bits.weighted_bipred_idc == 1) &&
248          IS_B_SLICE(slice_param->slice_type))) {
249         /* FIXME: fill weight/offset table */
250         assert(0);
251     }
252
253     /* dec_ref_pic_marking */
254     if (pic_param->pic_fields.bits.reference_pic_flag) {     /* nal_ref_idc != 0 */
255         unsigned char no_output_of_prior_pics_flag = 0;
256         unsigned char long_term_reference_flag = 0;
257         unsigned char adaptive_ref_pic_marking_mode_flag = 0;
258
259         if (dec_ref_pic_marking_param) {
260             no_output_of_prior_pics_flag = dec_ref_pic_marking_param->no_output_of_prior_pics_flag;
261             long_term_reference_flag = dec_ref_pic_marking_param->long_term_reference_flag;
262             adaptive_ref_pic_marking_mode_flag = dec_ref_pic_marking_param->adaptive_ref_pic_marking_mode_flag;
263             /* FIXME: XXX */
264             assert(adaptive_ref_pic_marking_mode_flag == 0);
265         }
266
267         if (pic_param->pic_fields.bits.idr_pic_flag) {
268             avc_bitstream_put_ui(bs, no_output_of_prior_pics_flag, 1);            /* no_output_of_prior_pics_flag: 0 */
269             avc_bitstream_put_ui(bs, long_term_reference_flag, 1);            /* long_term_reference_flag: 0 */
270         } else {
271             avc_bitstream_put_ui(bs, adaptive_ref_pic_marking_mode_flag, 1);            /* adaptive_ref_pic_marking_mode_flag: 0 */
272         }
273     }
274
275     if (pic_param->pic_fields.bits.entropy_coding_mode_flag &&
276         !IS_I_SLICE(slice_param->slice_type))
277         avc_bitstream_put_ue(bs, slice_param->cabac_init_idc);               /* cabac_init_idc: 0 */
278
279     avc_bitstream_put_se(bs, slice_param->slice_qp_delta);                   /* slice_qp_delta: 0 */
280
281     /* ignore for SP/SI */
282
283     if (pic_param->pic_fields.bits.deblocking_filter_control_present_flag) {
284         avc_bitstream_put_ue(bs, slice_param->disable_deblocking_filter_idc);           /* disable_deblocking_filter_idc: 0 */
285
286         if (slice_param->disable_deblocking_filter_idc != 1) {
287             avc_bitstream_put_se(bs, slice_param->slice_alpha_c0_offset_div2);          /* slice_alpha_c0_offset_div2: 2 */
288             avc_bitstream_put_se(bs, slice_param->slice_beta_offset_div2);              /* slice_beta_offset_div2: 2 */
289         }
290     }
291
292     if (pic_param->pic_fields.bits.entropy_coding_mode_flag) {
293         avc_bitstream_byte_aligning(bs, 1);
294     }
295 }
296
297 int 
298 build_avc_slice_header(VAEncSequenceParameterBufferH264 *sps_param,
299                        VAEncPictureParameterBufferH264 *pic_param,
300                        VAEncSliceParameterBufferH264 *slice_param,
301                        VAEncH264DecRefPicMarkingBuffer *dec_ref_pic_marking_param,
302                        unsigned char **slice_header_buffer)
303 {
304     avc_bitstream bs;
305     int is_idr = !!pic_param->pic_fields.bits.idr_pic_flag;
306
307     avc_bitstream_start(&bs);
308     nal_start_code_prefix(&bs);
309
310     if (IS_I_SLICE(slice_param->slice_type)) {
311         nal_header(&bs, NAL_REF_IDC_HIGH, is_idr ? NAL_IDR : NAL_NON_IDR);
312     } else if (IS_P_SLICE(slice_param->slice_type)) {
313         nal_header(&bs, NAL_REF_IDC_MEDIUM, is_idr ? NAL_IDR : NAL_NON_IDR);
314     } else {
315         assert(IS_B_SLICE(slice_param->slice_type));
316         nal_header(&bs, NAL_REF_IDC_NONE, is_idr ? NAL_IDR : NAL_NON_IDR);
317     }
318
319     slice_header(&bs, sps_param, pic_param, slice_param, dec_ref_pic_marking_param);
320
321     avc_bitstream_end(&bs);
322     *slice_header_buffer = (unsigned char *)bs.buffer;
323
324     return bs.bit_offset;
325 }