Merge branch 'vaapi-ext' into staging-work
[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 #include <va/va_enc_h264.h>
31
32 #include "i965_encoder_utils.h"
33
34 #define BITSTREAM_ALLOCATE_STEPPING     4096
35
36 #define NAL_REF_IDC_NONE        0
37 #define NAL_REF_IDC_LOW         1
38 #define NAL_REF_IDC_MEDIUM      2
39 #define NAL_REF_IDC_HIGH        3
40
41 #define NAL_NON_IDR             1
42 #define NAL_IDR                 5
43 #define NAL_SPS                 7
44 #define NAL_PPS                 8
45 #define NAL_SEI                 6
46
47 #define SLICE_TYPE_P            0
48 #define SLICE_TYPE_B            1
49 #define SLICE_TYPE_I            2
50
51 #define IS_I_SLICE(type) (SLICE_TYPE_I == (type) || SLICE_TYPE_I == (type - 5))
52 #define IS_P_SLICE(type) (SLICE_TYPE_P == (type) || SLICE_TYPE_P == (type - 5))
53 #define IS_B_SLICE(type) (SLICE_TYPE_B == (type) || SLICE_TYPE_B == (type - 5))
54
55 #define ENTROPY_MODE_CAVLC      0
56 #define ENTROPY_MODE_CABAC      1
57
58 #define PROFILE_IDC_BASELINE    66
59 #define PROFILE_IDC_MAIN        77
60 #define PROFILE_IDC_HIGH        100
61
62 struct __avc_bitstream {
63     unsigned int *buffer;
64     int bit_offset;
65     int max_size_in_dword;
66 };
67
68 typedef struct __avc_bitstream avc_bitstream;
69
70 static unsigned int 
71 swap32(unsigned int val)
72 {
73     unsigned char *pval = (unsigned char *)&val;
74
75     return ((pval[0] << 24)     |
76             (pval[1] << 16)     |
77             (pval[2] << 8)      |
78             (pval[3] << 0));
79 }
80
81 static void
82 avc_bitstream_start(avc_bitstream *bs)
83 {
84     bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
85     bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
86     bs->bit_offset = 0;
87 }
88
89 static void
90 avc_bitstream_end(avc_bitstream *bs)
91 {
92     int pos = (bs->bit_offset >> 5);
93     int bit_offset = (bs->bit_offset & 0x1f);
94     int bit_left = 32 - bit_offset;
95
96     if (bit_offset) {
97         bs->buffer[pos] = swap32((bs->buffer[pos] << bit_left));
98     }
99
100     // free(bs->buffer);
101 }
102
103 static void
104 avc_bitstream_put_ui(avc_bitstream *bs, unsigned int val, int size_in_bits)
105 {
106     int pos = (bs->bit_offset >> 5);
107     int bit_offset = (bs->bit_offset & 0x1f);
108     int bit_left = 32 - bit_offset;
109
110     if (!size_in_bits)
111         return;
112
113     if (size_in_bits < 32)
114         val &= (( 1 << size_in_bits) - 1);
115
116     bs->bit_offset += size_in_bits;
117
118     if (bit_left > size_in_bits) {
119         bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
120     } else {
121         size_in_bits -= bit_left;
122         bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
123         bs->buffer[pos] = swap32(bs->buffer[pos]);
124
125         if (pos + 1 == bs->max_size_in_dword) {
126             bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
127             bs->buffer = realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
128         }
129
130         bs->buffer[pos + 1] = val;
131     }
132 }
133
134 static void
135 avc_bitstream_put_ue(avc_bitstream *bs, unsigned int val)
136 {
137     int size_in_bits = 0;
138     int tmp_val = ++val;
139
140     while (tmp_val) {
141         tmp_val >>= 1;
142         size_in_bits++;
143     }
144
145     avc_bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
146     avc_bitstream_put_ui(bs, val, size_in_bits);
147 }
148
149 static void
150 avc_bitstream_put_se(avc_bitstream *bs, int val)
151 {
152     unsigned int new_val;
153
154     if (val <= 0)
155         new_val = -2 * val;
156     else
157         new_val = 2 * val - 1;
158
159     avc_bitstream_put_ue(bs, new_val);
160 }
161
162 static void
163 avc_bitstream_byte_aligning(avc_bitstream *bs, int bit)
164 {
165     int bit_offset = (bs->bit_offset & 0x7);
166     int bit_left = 8 - bit_offset;
167     int new_val;
168
169     if (!bit_offset)
170         return;
171
172     assert(bit == 0 || bit == 1);
173
174     if (bit)
175         new_val = (1 << bit_left) - 1;
176     else
177         new_val = 0;
178
179     avc_bitstream_put_ui(bs, new_val, bit_left);
180 }
181 static void avc_rbsp_trailing_bits(avc_bitstream *bs)
182 {
183     avc_bitstream_put_ui(bs, 1, 1);
184     avc_bitstream_byte_aligning(bs, 0);
185 }
186 static void nal_start_code_prefix(avc_bitstream *bs)
187 {
188     avc_bitstream_put_ui(bs, 0x00000001, 32);
189 }
190
191 static void nal_header(avc_bitstream *bs, int nal_ref_idc, int nal_unit_type)
192 {
193     avc_bitstream_put_ui(bs, 0, 1);                /* forbidden_zero_bit: 0 */
194     avc_bitstream_put_ui(bs, nal_ref_idc, 2);
195     avc_bitstream_put_ui(bs, nal_unit_type, 5);
196 }
197
198 static void 
199 slice_header(avc_bitstream *bs,
200              VAEncSequenceParameterBufferH264 *sps_param,
201              VAEncPictureParameterBufferH264 *pic_param,
202              VAEncSliceParameterBufferH264 *slice_param)
203 {
204     int first_mb_in_slice = slice_param->macroblock_address;
205
206     avc_bitstream_put_ue(bs, first_mb_in_slice);        /* first_mb_in_slice: 0 */
207     avc_bitstream_put_ue(bs, slice_param->slice_type);  /* slice_type */
208     avc_bitstream_put_ue(bs, slice_param->pic_parameter_set_id);        /* pic_parameter_set_id: 0 */
209     avc_bitstream_put_ui(bs, pic_param->frame_num, sps_param->seq_fields.bits.log2_max_frame_num_minus4 + 4); /* frame_num */
210
211     /* frame_mbs_only_flag == 1 */
212     if (!sps_param->seq_fields.bits.frame_mbs_only_flag) {
213         /* FIXME: */
214         assert(0);
215     }
216
217     if (pic_param->pic_fields.bits.idr_pic_flag)
218         avc_bitstream_put_ue(bs, slice_param->idr_pic_id);              /* idr_pic_id: 0 */
219
220     if (sps_param->seq_fields.bits.pic_order_cnt_type == 0) {
221         avc_bitstream_put_ui(bs, pic_param->CurrPic.TopFieldOrderCnt, sps_param->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 + 4);
222         /* pic_order_present_flag == 0 */
223     } else {
224         /* FIXME: */
225         assert(0);
226     }
227
228     /* redundant_pic_cnt_present_flag == 0 */
229     
230     /* slice type */
231     if (IS_P_SLICE(slice_param->slice_type)) {
232         avc_bitstream_put_ui(bs, 0, 1);            /* num_ref_idx_active_override_flag: 0 */
233
234         /* ref_pic_list_reordering */
235         avc_bitstream_put_ui(bs, 0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
236     } else if (IS_B_SLICE(slice_param->slice_type)) {
237         avc_bitstream_put_ui(bs, slice_param->direct_spatial_mv_pred_flag, 1);            /* direct_spatial_mv_pred: 1 */
238         avc_bitstream_put_ui(bs, 0, 1);            /* num_ref_idx_active_override_flag: 0 */
239
240         /* ref_pic_list_reordering */
241         avc_bitstream_put_ui(bs, 0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
242         avc_bitstream_put_ui(bs, 0, 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 (pic_param->pic_fields.bits.idr_pic_flag) {
260             avc_bitstream_put_ui(bs, no_output_of_prior_pics_flag, 1);            /* no_output_of_prior_pics_flag: 0 */
261             avc_bitstream_put_ui(bs, long_term_reference_flag, 1);            /* long_term_reference_flag: 0 */
262         } else {
263             avc_bitstream_put_ui(bs, adaptive_ref_pic_marking_mode_flag, 1);            /* adaptive_ref_pic_marking_mode_flag: 0 */
264         }
265     }
266
267     if (pic_param->pic_fields.bits.entropy_coding_mode_flag &&
268         !IS_I_SLICE(slice_param->slice_type))
269         avc_bitstream_put_ue(bs, slice_param->cabac_init_idc);               /* cabac_init_idc: 0 */
270
271     avc_bitstream_put_se(bs, slice_param->slice_qp_delta);                   /* slice_qp_delta: 0 */
272
273     /* ignore for SP/SI */
274
275     if (pic_param->pic_fields.bits.deblocking_filter_control_present_flag) {
276         avc_bitstream_put_ue(bs, slice_param->disable_deblocking_filter_idc);           /* disable_deblocking_filter_idc: 0 */
277
278         if (slice_param->disable_deblocking_filter_idc != 1) {
279             avc_bitstream_put_se(bs, slice_param->slice_alpha_c0_offset_div2);          /* slice_alpha_c0_offset_div2: 2 */
280             avc_bitstream_put_se(bs, slice_param->slice_beta_offset_div2);              /* slice_beta_offset_div2: 2 */
281         }
282     }
283
284     if (pic_param->pic_fields.bits.entropy_coding_mode_flag) {
285         avc_bitstream_byte_aligning(bs, 1);
286     }
287 }
288
289 int 
290 build_avc_slice_header(VAEncSequenceParameterBufferH264 *sps_param,
291                        VAEncPictureParameterBufferH264 *pic_param,
292                        VAEncSliceParameterBufferH264 *slice_param,
293                        unsigned char **slice_header_buffer)
294 {
295     avc_bitstream bs;
296     int is_idr = !!pic_param->pic_fields.bits.idr_pic_flag;
297
298     avc_bitstream_start(&bs);
299     nal_start_code_prefix(&bs);
300
301     if (IS_I_SLICE(slice_param->slice_type)) {
302         nal_header(&bs, NAL_REF_IDC_HIGH, is_idr ? NAL_IDR : NAL_NON_IDR);
303     } else if (IS_P_SLICE(slice_param->slice_type)) {
304         nal_header(&bs, NAL_REF_IDC_MEDIUM, is_idr ? NAL_IDR : NAL_NON_IDR);
305     } else {
306         assert(IS_B_SLICE(slice_param->slice_type));
307         nal_header(&bs, NAL_REF_IDC_NONE, is_idr ? NAL_IDR : NAL_NON_IDR);
308     }
309
310     slice_header(&bs, sps_param, pic_param, slice_param);
311
312     avc_bitstream_end(&bs);
313     *slice_header_buffer = (unsigned char *)bs.buffer;
314
315     return bs.bit_offset;
316 }
317
318 int 
319 build_avc_sei_buffering_period(int cpb_removal_length,
320                                unsigned int init_cpb_removal_delay, 
321                                unsigned int init_cpb_removal_delay_offset,
322                                unsigned char **sei_buffer) 
323 {
324     unsigned char *byte_buf;
325     int byte_size, i;
326
327     avc_bitstream nal_bs;
328     avc_bitstream sei_bs;
329
330     avc_bitstream_start(&sei_bs);
331     avc_bitstream_put_ue(&sei_bs, 0);       /*seq_parameter_set_id*/
332     avc_bitstream_put_ui(&sei_bs, init_cpb_removal_delay, cpb_removal_length); 
333     avc_bitstream_put_ui(&sei_bs, init_cpb_removal_delay_offset, cpb_removal_length); 
334     if ( sei_bs.bit_offset & 0x7) {
335         avc_bitstream_put_ui(&sei_bs, 1, 1);
336     }
337     avc_bitstream_end(&sei_bs);
338     byte_size = (sei_bs.bit_offset + 7) / 8;
339     
340     avc_bitstream_start(&nal_bs);
341     nal_start_code_prefix(&nal_bs);
342     nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
343     
344     avc_bitstream_put_ui(&nal_bs, 0, 8);
345     avc_bitstream_put_ui(&nal_bs, byte_size, 8);
346     
347     byte_buf = (unsigned char *)sei_bs.buffer;
348     for(i = 0; i < byte_size; i++) {
349         avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
350     }
351     free(byte_buf);
352
353     avc_rbsp_trailing_bits(&nal_bs);
354     avc_bitstream_end(&nal_bs);
355
356     *sei_buffer = (unsigned char *)nal_bs.buffer; 
357    
358     return nal_bs.bit_offset;
359 }
360
361 int 
362 build_avc_sei_pic_timing(unsigned int cpb_removal_length, unsigned int cpb_removal_delay, 
363                          unsigned int dpb_output_length, unsigned int dpb_output_delay,
364                          unsigned char **sei_buffer)
365 {
366     unsigned char *byte_buf;
367     int byte_size, i;
368
369     avc_bitstream nal_bs;
370     avc_bitstream sei_bs;
371
372     avc_bitstream_start(&sei_bs);
373     avc_bitstream_put_ui(&sei_bs, cpb_removal_delay, cpb_removal_length); 
374     avc_bitstream_put_ui(&sei_bs, dpb_output_delay, dpb_output_length); 
375     if ( sei_bs.bit_offset & 0x7) {
376         avc_bitstream_put_ui(&sei_bs, 1, 1);
377     }
378     avc_bitstream_end(&sei_bs);
379     byte_size = (sei_bs.bit_offset + 7) / 8;
380     
381     avc_bitstream_start(&nal_bs);
382     nal_start_code_prefix(&nal_bs);
383     nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
384     
385     avc_bitstream_put_ui(&nal_bs, 0x01, 8);
386     avc_bitstream_put_ui(&nal_bs, byte_size, 8);
387     
388     byte_buf = (unsigned char *)sei_bs.buffer;
389     for(i = 0; i < byte_size; i++) {
390         avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
391     }
392     free(byte_buf);
393
394     avc_rbsp_trailing_bits(&nal_bs);
395     avc_bitstream_end(&nal_bs);
396
397     *sei_buffer = (unsigned char *)nal_bs.buffer; 
398    
399     return nal_bs.bit_offset;
400 }
401
402