i965_drv_video: utility for encoder
[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     bs->bit_offset += size_in_bits;
112
113     if (bit_left > size_in_bits) {
114         bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
115     } else {
116         size_in_bits -= bit_left;
117         bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
118         bs->buffer[pos] = swap32(bs->buffer[pos]);
119
120         if (pos + 1 == bs->max_size_in_dword) {
121             bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
122             bs->buffer = realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
123         }
124
125         bs->buffer[pos + 1] = val;
126     }
127 }
128
129 static void
130 avc_bitstream_put_ue(avc_bitstream *bs, unsigned int val)
131 {
132     int size_in_bits = 0;
133     int tmp_val = ++val;
134
135     while (tmp_val) {
136         tmp_val >>= 1;
137         size_in_bits++;
138     }
139
140     avc_bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
141     avc_bitstream_put_ui(bs, val, size_in_bits);
142 }
143
144 static void
145 avc_bitstream_put_se(avc_bitstream *bs, int val)
146 {
147     unsigned int new_val;
148
149     if (val <= 0)
150         new_val = -2 * val;
151     else
152         new_val = 2 * val - 1;
153
154     avc_bitstream_put_ue(bs, new_val);
155 }
156
157 static void
158 avc_bitstream_byte_aligning(avc_bitstream *bs, int bit)
159 {
160     int bit_offset = (bs->bit_offset & 0x7);
161     int bit_left = 8 - bit_offset;
162     int new_val;
163
164     if (!bit_offset)
165         return;
166
167     assert(bit == 0 || bit == 1);
168
169     if (bit)
170         new_val = (1 << bit_left) - 1;
171     else
172         new_val = 0;
173
174     avc_bitstream_put_ui(bs, new_val, bit_left);
175 }
176
177 static void nal_start_code_prefix(avc_bitstream *bs)
178 {
179     avc_bitstream_put_ui(bs, 0x00000001, 32);
180 }
181
182 static void nal_header(avc_bitstream *bs, int nal_ref_idc, int nal_unit_type)
183 {
184     avc_bitstream_put_ui(bs, 0, 1);                /* forbidden_zero_bit: 0 */
185     avc_bitstream_put_ui(bs, nal_ref_idc, 2);
186     avc_bitstream_put_ui(bs, nal_unit_type, 5);
187 }
188
189 static void 
190 slice_header(avc_bitstream *bs,
191              VAEncSequenceParameterBufferH264Ext *sps_param,
192              VAEncPictureParameterBufferH264Ext *pic_param,
193              VAEncSliceParameterBufferH264Ext *slice_param,
194              VAEncH264DecRefPicMarkingBuffer *dec_ref_pic_marking_param)
195 {
196     int first_mb_in_slice = sps_param->picture_width_in_mbs * slice_param->start_row_number;
197
198     avc_bitstream_put_ue(bs, first_mb_in_slice);        /* first_mb_in_slice: 0 */
199     avc_bitstream_put_ue(bs, slice_param->slice_type);  /* slice_type */
200     avc_bitstream_put_ue(bs, slice_param->pic_parameter_set_id);        /* pic_parameter_set_id: 0 */
201     avc_bitstream_put_ui(bs, pic_param->frame_num, sps_param->log2_max_frame_num_minus4 + 4); /* frame_num */
202
203     /* frame_mbs_only_flag == 1 */
204     if (!sps_param->frame_mbs_only_flag) {
205         /* FIXME: */
206         assert(0);
207     }
208
209     if (pic_param->pic_fields.bits.idr_pic_flag)
210         avc_bitstream_put_ue(bs, slice_param->idr_pic_id);              /* idr_pic_id: 0 */
211
212     if (sps_param->pic_order_cnt_type == 0) {
213         avc_bitstream_put_ui(bs, pic_param->CurrPic.TopFieldOrderCnt, sps_param->log2_max_pic_order_cnt_lsb_minus4 + 4);
214         /* pic_order_present_flag == 0 */
215     } else {
216         /* FIXME: */
217         assert(0);
218     }
219
220     /* redundant_pic_cnt_present_flag == 0 */
221     
222     /* slice type */
223     if (IS_P_SLICE(slice_param->slice_type)) {
224         avc_bitstream_put_ui(bs, 0, 1);            /* num_ref_idx_active_override_flag: 0 */
225
226         /* ref_pic_list_reordering */
227         assert(slice_param->ref_pic_list_modification_flag_l0 == 0);
228         avc_bitstream_put_ui(bs, slice_param->ref_pic_list_modification_flag_l0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
229     } else if (IS_B_SLICE(slice_param->slice_type)) {
230         avc_bitstream_put_ui(bs, slice_param->direct_spatial_mv_pred_flag, 1);            /* direct_spatial_mv_pred: 1 */
231         avc_bitstream_put_ui(bs, 0, 1);            /* num_ref_idx_active_override_flag: 0 */
232
233         /* ref_pic_list_reordering */
234         assert(slice_param->ref_pic_list_modification_flag_l0 == 0);
235         assert(slice_param->ref_pic_list_modification_flag_l1 == 0);
236         avc_bitstream_put_ui(bs, slice_param->ref_pic_list_modification_flag_l0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
237         avc_bitstream_put_ui(bs, slice_param->ref_pic_list_modification_flag_l1, 1);            /* ref_pic_list_reordering_flag_l1: 0 */
238     } 
239
240     if ((pic_param->pic_fields.bits.weighted_pred_flag && 
241          IS_P_SLICE(slice_param->slice_type)) ||
242         ((pic_param->pic_fields.bits.weighted_bipred_idc == 1) &&
243          IS_B_SLICE(slice_param->slice_type))) {
244         /* FIXME: fill weight/offset table */
245         assert(0);
246     }
247
248     /* dec_ref_pic_marking */
249     if (pic_param->pic_fields.bits.reference_pic_flag) {     /* nal_ref_idc != 0 */
250         unsigned char no_output_of_prior_pics_flag = 0;
251         unsigned char long_term_reference_flag = 0;
252         unsigned char adaptive_ref_pic_marking_mode_flag = 0;
253
254         if (dec_ref_pic_marking_param) {
255             no_output_of_prior_pics_flag = dec_ref_pic_marking_param->no_output_of_prior_pics_flag;
256             long_term_reference_flag = dec_ref_pic_marking_param->long_term_reference_flag;
257             adaptive_ref_pic_marking_mode_flag = dec_ref_pic_marking_param->adaptive_ref_pic_marking_mode_flag;
258             /* FIXME: XXX */
259             assert(adaptive_ref_pic_marking_mode_flag == 0);
260         }
261
262         if (pic_param->pic_fields.bits.idr_pic_flag) {
263             avc_bitstream_put_ui(bs, no_output_of_prior_pics_flag, 1);            /* no_output_of_prior_pics_flag: 0 */
264             avc_bitstream_put_ui(bs, long_term_reference_flag, 1);            /* long_term_reference_flag: 0 */
265         } else {
266             avc_bitstream_put_ui(bs, adaptive_ref_pic_marking_mode_flag, 1);            /* adaptive_ref_pic_marking_mode_flag: 0 */
267         }
268     }
269
270     if (pic_param->pic_fields.bits.entropy_coding_mode_flag &&
271         !IS_I_SLICE(slice_param->slice_type))
272         avc_bitstream_put_ue(bs, slice_param->cabac_init_idc);               /* cabac_init_idc: 0 */
273
274     avc_bitstream_put_se(bs, slice_param->slice_qp_delta);                   /* slice_qp_delta: 0 */
275
276     /* ignore for SP/SI */
277
278     if (pic_param->pic_fields.bits.deblocking_filter_control_present_flag) {
279         avc_bitstream_put_ue(bs, slice_param->disable_deblocking_filter_idc);           /* disable_deblocking_filter_idc: 0 */
280
281         if (slice_param->disable_deblocking_filter_idc != 0) {
282             avc_bitstream_put_se(bs, slice_param->slice_alpha_c0_offset_div2);          /* slice_alpha_c0_offset_div2: 2 */
283             avc_bitstream_put_se(bs, slice_param->slice_beta_offset_div2);              /* slice_beta_offset_div2: 2 */
284         }
285     }
286
287     if (pic_param->pic_fields.bits.entropy_coding_mode_flag) {
288         avc_bitstream_byte_aligning(bs, 1);
289     }
290 }
291
292 int 
293 build_avc_slice_header(VAEncSequenceParameterBufferH264Ext *sps_param,
294                        VAEncPictureParameterBufferH264Ext *pic_param,
295                        VAEncSliceParameterBufferH264Ext *slice_param,
296                        VAEncH264DecRefPicMarkingBuffer *dec_ref_pic_marking_param,
297                        unsigned char **slice_header_buffer)
298 {
299     avc_bitstream bs;
300     int is_idr = !!pic_param->pic_fields.bits.idr_pic_flag;
301
302     avc_bitstream_start(&bs);
303     nal_start_code_prefix(&bs);
304
305     if (IS_I_SLICE(slice_param->slice_type)) {
306         nal_header(&bs, NAL_REF_IDC_HIGH, is_idr ? NAL_IDR : NAL_NON_IDR);
307     } else if (IS_P_SLICE(slice_param->slice_type)) {
308         nal_header(&bs, NAL_REF_IDC_MEDIUM, is_idr ? NAL_IDR : NAL_NON_IDR);
309     } else {
310         assert(IS_B_SLICE(slice_param->slice_type));
311         nal_header(&bs, NAL_REF_IDC_NONE, is_idr ? NAL_IDR : NAL_NON_IDR);
312     }
313
314     slice_header(&bs, sps_param, pic_param, slice_param, dec_ref_pic_marking_param);
315
316     avc_bitstream_end(&bs);
317     *slice_header_buffer = (unsigned char *)bs.buffer;
318
319     return ((bs.bit_offset + 7) >> 3);
320 }