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