i965_drv_video: generate slice header in driver
[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              VAEncSequenceParameterBufferH264Ext *sps_param,
195              VAEncPictureParameterBufferH264Ext *pic_param,
196              VAEncSliceParameterBufferH264Ext *slice_param,
197              VAEncH264DecRefPicMarkingBuffer *dec_ref_pic_marking_param)
198 {
199     int first_mb_in_slice = sps_param->picture_width_in_mbs * slice_param->start_row_number;
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     } else {
219         /* FIXME: */
220         assert(0);
221     }
222
223     /* redundant_pic_cnt_present_flag == 0 */
224     
225     /* slice type */
226     if (IS_P_SLICE(slice_param->slice_type)) {
227         avc_bitstream_put_ui(bs, 0, 1);            /* num_ref_idx_active_override_flag: 0 */
228
229         /* ref_pic_list_reordering */
230         assert(slice_param->ref_pic_list_modification_flag_l0 == 0);
231         avc_bitstream_put_ui(bs, slice_param->ref_pic_list_modification_flag_l0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
232     } else if (IS_B_SLICE(slice_param->slice_type)) {
233         avc_bitstream_put_ui(bs, slice_param->direct_spatial_mv_pred_flag, 1);            /* direct_spatial_mv_pred: 1 */
234         avc_bitstream_put_ui(bs, 0, 1);            /* num_ref_idx_active_override_flag: 0 */
235
236         /* ref_pic_list_reordering */
237         assert(slice_param->ref_pic_list_modification_flag_l0 == 0);
238         assert(slice_param->ref_pic_list_modification_flag_l1 == 0);
239         avc_bitstream_put_ui(bs, slice_param->ref_pic_list_modification_flag_l0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
240         avc_bitstream_put_ui(bs, slice_param->ref_pic_list_modification_flag_l1, 1);            /* ref_pic_list_reordering_flag_l1: 0 */
241     } 
242
243     if ((pic_param->pic_fields.bits.weighted_pred_flag && 
244          IS_P_SLICE(slice_param->slice_type)) ||
245         ((pic_param->pic_fields.bits.weighted_bipred_idc == 1) &&
246          IS_B_SLICE(slice_param->slice_type))) {
247         /* FIXME: fill weight/offset table */
248         assert(0);
249     }
250
251     /* dec_ref_pic_marking */
252     if (pic_param->pic_fields.bits.reference_pic_flag) {     /* nal_ref_idc != 0 */
253         unsigned char no_output_of_prior_pics_flag = 0;
254         unsigned char long_term_reference_flag = 0;
255         unsigned char adaptive_ref_pic_marking_mode_flag = 0;
256
257         if (dec_ref_pic_marking_param) {
258             no_output_of_prior_pics_flag = dec_ref_pic_marking_param->no_output_of_prior_pics_flag;
259             long_term_reference_flag = dec_ref_pic_marking_param->long_term_reference_flag;
260             adaptive_ref_pic_marking_mode_flag = dec_ref_pic_marking_param->adaptive_ref_pic_marking_mode_flag;
261             /* FIXME: XXX */
262             assert(adaptive_ref_pic_marking_mode_flag == 0);
263         }
264
265         if (pic_param->pic_fields.bits.idr_pic_flag) {
266             avc_bitstream_put_ui(bs, no_output_of_prior_pics_flag, 1);            /* no_output_of_prior_pics_flag: 0 */
267             avc_bitstream_put_ui(bs, long_term_reference_flag, 1);            /* long_term_reference_flag: 0 */
268         } else {
269             avc_bitstream_put_ui(bs, adaptive_ref_pic_marking_mode_flag, 1);            /* adaptive_ref_pic_marking_mode_flag: 0 */
270         }
271     }
272
273     if (pic_param->pic_fields.bits.entropy_coding_mode_flag &&
274         !IS_I_SLICE(slice_param->slice_type))
275         avc_bitstream_put_ue(bs, slice_param->cabac_init_idc);               /* cabac_init_idc: 0 */
276
277     avc_bitstream_put_se(bs, slice_param->slice_qp_delta);                   /* slice_qp_delta: 0 */
278
279     /* ignore for SP/SI */
280
281     if (pic_param->pic_fields.bits.deblocking_filter_control_present_flag) {
282         avc_bitstream_put_ue(bs, slice_param->disable_deblocking_filter_idc);           /* disable_deblocking_filter_idc: 0 */
283
284         if (slice_param->disable_deblocking_filter_idc != 1) {
285             avc_bitstream_put_se(bs, slice_param->slice_alpha_c0_offset_div2);          /* slice_alpha_c0_offset_div2: 2 */
286             avc_bitstream_put_se(bs, slice_param->slice_beta_offset_div2);              /* slice_beta_offset_div2: 2 */
287         }
288     }
289
290     if (pic_param->pic_fields.bits.entropy_coding_mode_flag) {
291         avc_bitstream_byte_aligning(bs, 1);
292     }
293 }
294
295 int 
296 build_avc_slice_header(VAEncSequenceParameterBufferH264Ext *sps_param,
297                        VAEncPictureParameterBufferH264Ext *pic_param,
298                        VAEncSliceParameterBufferH264Ext *slice_param,
299                        VAEncH264DecRefPicMarkingBuffer *dec_ref_pic_marking_param,
300                        unsigned char **slice_header_buffer)
301 {
302     avc_bitstream bs;
303     int is_idr = !!pic_param->pic_fields.bits.idr_pic_flag;
304
305     avc_bitstream_start(&bs);
306     nal_start_code_prefix(&bs);
307
308     if (IS_I_SLICE(slice_param->slice_type)) {
309         nal_header(&bs, NAL_REF_IDC_HIGH, is_idr ? NAL_IDR : NAL_NON_IDR);
310     } else if (IS_P_SLICE(slice_param->slice_type)) {
311         nal_header(&bs, NAL_REF_IDC_MEDIUM, is_idr ? NAL_IDR : NAL_NON_IDR);
312     } else {
313         assert(IS_B_SLICE(slice_param->slice_type));
314         nal_header(&bs, NAL_REF_IDC_NONE, is_idr ? NAL_IDR : NAL_NON_IDR);
315     }
316
317     slice_header(&bs, sps_param, pic_param, slice_param, dec_ref_pic_marking_param);
318
319     avc_bitstream_end(&bs);
320     *slice_header_buffer = (unsigned char *)bs.buffer;
321
322     return bs.bit_offset;
323 }