Use the right parameters to initialize bit rate context
[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 #include <va/va_enc_mpeg2.h>
32
33 #include "i965_encoder_utils.h"
34
35 #define BITSTREAM_ALLOCATE_STEPPING     4096
36
37 #define NAL_REF_IDC_NONE        0
38 #define NAL_REF_IDC_LOW         1
39 #define NAL_REF_IDC_MEDIUM      2
40 #define NAL_REF_IDC_HIGH        3
41
42 #define NAL_NON_IDR             1
43 #define NAL_IDR                 5
44 #define NAL_SPS                 7
45 #define NAL_PPS                 8
46 #define NAL_SEI                 6
47
48 #define SLICE_TYPE_P            0
49 #define SLICE_TYPE_B            1
50 #define SLICE_TYPE_I            2
51
52 #define IS_I_SLICE(type) (SLICE_TYPE_I == (type) || SLICE_TYPE_I == (type - 5))
53 #define IS_P_SLICE(type) (SLICE_TYPE_P == (type) || SLICE_TYPE_P == (type - 5))
54 #define IS_B_SLICE(type) (SLICE_TYPE_B == (type) || SLICE_TYPE_B == (type - 5))
55
56 #define ENTROPY_MODE_CAVLC      0
57 #define ENTROPY_MODE_CABAC      1
58
59 #define PROFILE_IDC_BASELINE    66
60 #define PROFILE_IDC_MAIN        77
61 #define PROFILE_IDC_HIGH        100
62
63 struct __avc_bitstream {
64     unsigned int *buffer;
65     int bit_offset;
66     int max_size_in_dword;
67 };
68
69 typedef struct __avc_bitstream avc_bitstream;
70
71 static unsigned int 
72 swap32(unsigned int val)
73 {
74     unsigned char *pval = (unsigned char *)&val;
75
76     return ((pval[0] << 24)     |
77             (pval[1] << 16)     |
78             (pval[2] << 8)      |
79             (pval[3] << 0));
80 }
81
82 static void
83 avc_bitstream_start(avc_bitstream *bs)
84 {
85     bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
86     bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
87     bs->bit_offset = 0;
88 }
89
90 static void
91 avc_bitstream_end(avc_bitstream *bs)
92 {
93     int pos = (bs->bit_offset >> 5);
94     int bit_offset = (bs->bit_offset & 0x1f);
95     int bit_left = 32 - bit_offset;
96
97     if (bit_offset) {
98         bs->buffer[pos] = swap32((bs->buffer[pos] << bit_left));
99     }
100
101     // free(bs->buffer);
102 }
103
104 static void
105 avc_bitstream_put_ui(avc_bitstream *bs, unsigned int val, int size_in_bits)
106 {
107     int pos = (bs->bit_offset >> 5);
108     int bit_offset = (bs->bit_offset & 0x1f);
109     int bit_left = 32 - bit_offset;
110
111     if (!size_in_bits)
112         return;
113
114     if (size_in_bits < 32)
115         val &= (( 1 << size_in_bits) - 1);
116
117     bs->bit_offset += size_in_bits;
118
119     if (bit_left > size_in_bits) {
120         bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
121     } else {
122         size_in_bits -= bit_left;
123         bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
124         bs->buffer[pos] = swap32(bs->buffer[pos]);
125
126         if (pos + 1 == bs->max_size_in_dword) {
127             bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
128             bs->buffer = realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
129             
130             if (!bs->buffer)
131                 return;
132         }
133
134         bs->buffer[pos + 1] = val;
135     }
136 }
137
138 static void
139 avc_bitstream_put_ue(avc_bitstream *bs, unsigned int val)
140 {
141     int size_in_bits = 0;
142     int tmp_val = ++val;
143
144     while (tmp_val) {
145         tmp_val >>= 1;
146         size_in_bits++;
147     }
148
149     avc_bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
150     avc_bitstream_put_ui(bs, val, size_in_bits);
151 }
152
153 static void
154 avc_bitstream_put_se(avc_bitstream *bs, int val)
155 {
156     unsigned int new_val;
157
158     if (val <= 0)
159         new_val = -2 * val;
160     else
161         new_val = 2 * val - 1;
162
163     avc_bitstream_put_ue(bs, new_val);
164 }
165
166 static void
167 avc_bitstream_byte_aligning(avc_bitstream *bs, int bit)
168 {
169     int bit_offset = (bs->bit_offset & 0x7);
170     int bit_left = 8 - bit_offset;
171     int new_val;
172
173     if (!bit_offset)
174         return;
175
176     assert(bit == 0 || bit == 1);
177
178     if (bit)
179         new_val = (1 << bit_left) - 1;
180     else
181         new_val = 0;
182
183     avc_bitstream_put_ui(bs, new_val, bit_left);
184 }
185 static void avc_rbsp_trailing_bits(avc_bitstream *bs)
186 {
187     avc_bitstream_put_ui(bs, 1, 1);
188     avc_bitstream_byte_aligning(bs, 0);
189 }
190 static void nal_start_code_prefix(avc_bitstream *bs)
191 {
192     avc_bitstream_put_ui(bs, 0x00000001, 32);
193 }
194
195 static void nal_header(avc_bitstream *bs, int nal_ref_idc, int nal_unit_type)
196 {
197     avc_bitstream_put_ui(bs, 0, 1);                /* forbidden_zero_bit: 0 */
198     avc_bitstream_put_ui(bs, nal_ref_idc, 2);
199     avc_bitstream_put_ui(bs, nal_unit_type, 5);
200 }
201
202 static void 
203 slice_header(avc_bitstream *bs,
204              VAEncSequenceParameterBufferH264 *sps_param,
205              VAEncPictureParameterBufferH264 *pic_param,
206              VAEncSliceParameterBufferH264 *slice_param)
207 {
208     int first_mb_in_slice = slice_param->macroblock_address;
209
210     avc_bitstream_put_ue(bs, first_mb_in_slice);        /* first_mb_in_slice: 0 */
211     avc_bitstream_put_ue(bs, slice_param->slice_type);  /* slice_type */
212     avc_bitstream_put_ue(bs, slice_param->pic_parameter_set_id);        /* pic_parameter_set_id: 0 */
213     avc_bitstream_put_ui(bs, pic_param->frame_num, sps_param->seq_fields.bits.log2_max_frame_num_minus4 + 4); /* frame_num */
214
215     /* frame_mbs_only_flag == 1 */
216     if (!sps_param->seq_fields.bits.frame_mbs_only_flag) {
217         /* FIXME: */
218         assert(0);
219     }
220
221     if (pic_param->pic_fields.bits.idr_pic_flag)
222         avc_bitstream_put_ue(bs, slice_param->idr_pic_id);              /* idr_pic_id: 0 */
223
224     if (sps_param->seq_fields.bits.pic_order_cnt_type == 0) {
225         avc_bitstream_put_ui(bs, pic_param->CurrPic.TopFieldOrderCnt, sps_param->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 + 4);
226         /* pic_order_present_flag == 0 */
227     } else {
228         /* FIXME: */
229         assert(0);
230     }
231
232     /* redundant_pic_cnt_present_flag == 0 */
233     
234     /* slice type */
235     if (IS_P_SLICE(slice_param->slice_type)) {
236         avc_bitstream_put_ui(bs, slice_param->num_ref_idx_active_override_flag, 1);            /* num_ref_idx_active_override_flag: */
237
238         if (slice_param->num_ref_idx_active_override_flag)
239             avc_bitstream_put_ue(bs, slice_param->num_ref_idx_l0_active_minus1);
240
241         /* ref_pic_list_reordering */
242         avc_bitstream_put_ui(bs, 0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
243     } else if (IS_B_SLICE(slice_param->slice_type)) {
244         avc_bitstream_put_ui(bs, slice_param->direct_spatial_mv_pred_flag, 1);            /* direct_spatial_mv_pred: 1 */
245
246         avc_bitstream_put_ui(bs, slice_param->num_ref_idx_active_override_flag, 1);       /* num_ref_idx_active_override_flag: */
247
248         if (slice_param->num_ref_idx_active_override_flag) {
249             avc_bitstream_put_ue(bs, slice_param->num_ref_idx_l0_active_minus1);
250             avc_bitstream_put_ue(bs, slice_param->num_ref_idx_l1_active_minus1);
251         }
252
253         /* ref_pic_list_reordering */
254         avc_bitstream_put_ui(bs, 0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
255         avc_bitstream_put_ui(bs, 0, 1);            /* ref_pic_list_reordering_flag_l1: 0 */
256     } 
257
258     if ((pic_param->pic_fields.bits.weighted_pred_flag && 
259          IS_P_SLICE(slice_param->slice_type)) ||
260         ((pic_param->pic_fields.bits.weighted_bipred_idc == 1) &&
261          IS_B_SLICE(slice_param->slice_type))) {
262         /* FIXME: fill weight/offset table */
263         assert(0);
264     }
265
266     /* dec_ref_pic_marking */
267     if (pic_param->pic_fields.bits.reference_pic_flag) {     /* nal_ref_idc != 0 */
268         unsigned char no_output_of_prior_pics_flag = 0;
269         unsigned char long_term_reference_flag = 0;
270         unsigned char adaptive_ref_pic_marking_mode_flag = 0;
271
272         if (pic_param->pic_fields.bits.idr_pic_flag) {
273             avc_bitstream_put_ui(bs, no_output_of_prior_pics_flag, 1);            /* no_output_of_prior_pics_flag: 0 */
274             avc_bitstream_put_ui(bs, long_term_reference_flag, 1);            /* long_term_reference_flag: 0 */
275         } else {
276             avc_bitstream_put_ui(bs, adaptive_ref_pic_marking_mode_flag, 1);            /* adaptive_ref_pic_marking_mode_flag: 0 */
277         }
278     }
279
280     if (pic_param->pic_fields.bits.entropy_coding_mode_flag &&
281         !IS_I_SLICE(slice_param->slice_type))
282         avc_bitstream_put_ue(bs, slice_param->cabac_init_idc);               /* cabac_init_idc: 0 */
283
284     avc_bitstream_put_se(bs, slice_param->slice_qp_delta);                   /* slice_qp_delta: 0 */
285
286     /* ignore for SP/SI */
287
288     if (pic_param->pic_fields.bits.deblocking_filter_control_present_flag) {
289         avc_bitstream_put_ue(bs, slice_param->disable_deblocking_filter_idc);           /* disable_deblocking_filter_idc: 0 */
290
291         if (slice_param->disable_deblocking_filter_idc != 1) {
292             avc_bitstream_put_se(bs, slice_param->slice_alpha_c0_offset_div2);          /* slice_alpha_c0_offset_div2: 2 */
293             avc_bitstream_put_se(bs, slice_param->slice_beta_offset_div2);              /* slice_beta_offset_div2: 2 */
294         }
295     }
296
297     if (pic_param->pic_fields.bits.entropy_coding_mode_flag) {
298         avc_bitstream_byte_aligning(bs, 1);
299     }
300 }
301
302 int 
303 build_avc_slice_header(VAEncSequenceParameterBufferH264 *sps_param,
304                        VAEncPictureParameterBufferH264 *pic_param,
305                        VAEncSliceParameterBufferH264 *slice_param,
306                        unsigned char **slice_header_buffer)
307 {
308     avc_bitstream bs;
309     int is_idr = !!pic_param->pic_fields.bits.idr_pic_flag;
310     int is_ref = !!pic_param->pic_fields.bits.reference_pic_flag;
311
312     avc_bitstream_start(&bs);
313     nal_start_code_prefix(&bs);
314
315     if (IS_I_SLICE(slice_param->slice_type)) {
316         nal_header(&bs, NAL_REF_IDC_HIGH, is_idr ? NAL_IDR : NAL_NON_IDR);
317     } else if (IS_P_SLICE(slice_param->slice_type)) {
318         assert(!is_idr);
319         nal_header(&bs, NAL_REF_IDC_MEDIUM, NAL_NON_IDR);
320     } else {
321         assert(IS_B_SLICE(slice_param->slice_type));
322         assert(!is_idr);
323         nal_header(&bs, is_ref ? NAL_REF_IDC_LOW : NAL_REF_IDC_NONE, NAL_NON_IDR);
324     }
325
326     slice_header(&bs, sps_param, pic_param, slice_param);
327
328     avc_bitstream_end(&bs);
329     *slice_header_buffer = (unsigned char *)bs.buffer;
330
331     return bs.bit_offset;
332 }
333
334 int 
335 build_avc_sei_buffering_period(int cpb_removal_length,
336                                unsigned int init_cpb_removal_delay, 
337                                unsigned int init_cpb_removal_delay_offset,
338                                unsigned char **sei_buffer) 
339 {
340     unsigned char *byte_buf;
341     int byte_size, i;
342
343     avc_bitstream nal_bs;
344     avc_bitstream sei_bs;
345
346     avc_bitstream_start(&sei_bs);
347     avc_bitstream_put_ue(&sei_bs, 0);       /*seq_parameter_set_id*/
348     avc_bitstream_put_ui(&sei_bs, init_cpb_removal_delay, cpb_removal_length); 
349     avc_bitstream_put_ui(&sei_bs, init_cpb_removal_delay_offset, cpb_removal_length); 
350     if ( sei_bs.bit_offset & 0x7) {
351         avc_bitstream_put_ui(&sei_bs, 1, 1);
352     }
353     avc_bitstream_end(&sei_bs);
354     byte_size = (sei_bs.bit_offset + 7) / 8;
355     
356     avc_bitstream_start(&nal_bs);
357     nal_start_code_prefix(&nal_bs);
358     nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
359     
360     avc_bitstream_put_ui(&nal_bs, 0, 8);
361     avc_bitstream_put_ui(&nal_bs, byte_size, 8);
362     
363     byte_buf = (unsigned char *)sei_bs.buffer;
364     for(i = 0; i < byte_size; i++) {
365         avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
366     }
367     free(byte_buf);
368
369     avc_rbsp_trailing_bits(&nal_bs);
370     avc_bitstream_end(&nal_bs);
371
372     *sei_buffer = (unsigned char *)nal_bs.buffer; 
373    
374     return nal_bs.bit_offset;
375 }
376
377 int 
378 build_avc_sei_pic_timing(unsigned int cpb_removal_length, unsigned int cpb_removal_delay, 
379                          unsigned int dpb_output_length, unsigned int dpb_output_delay,
380                          unsigned char **sei_buffer)
381 {
382     unsigned char *byte_buf;
383     int byte_size, i;
384
385     avc_bitstream nal_bs;
386     avc_bitstream sei_bs;
387
388     avc_bitstream_start(&sei_bs);
389     avc_bitstream_put_ui(&sei_bs, cpb_removal_delay, cpb_removal_length); 
390     avc_bitstream_put_ui(&sei_bs, dpb_output_delay, dpb_output_length); 
391     if ( sei_bs.bit_offset & 0x7) {
392         avc_bitstream_put_ui(&sei_bs, 1, 1);
393     }
394     avc_bitstream_end(&sei_bs);
395     byte_size = (sei_bs.bit_offset + 7) / 8;
396     
397     avc_bitstream_start(&nal_bs);
398     nal_start_code_prefix(&nal_bs);
399     nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
400     
401     avc_bitstream_put_ui(&nal_bs, 0x01, 8);
402     avc_bitstream_put_ui(&nal_bs, byte_size, 8);
403     
404     byte_buf = (unsigned char *)sei_bs.buffer;
405     for(i = 0; i < byte_size; i++) {
406         avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
407     }
408     free(byte_buf);
409
410     avc_rbsp_trailing_bits(&nal_bs);
411     avc_bitstream_end(&nal_bs);
412
413     *sei_buffer = (unsigned char *)nal_bs.buffer; 
414    
415     return nal_bs.bit_offset;
416 }
417
418
419 int 
420 build_avc_sei_buffer_timing(unsigned int init_cpb_removal_length,
421                                 unsigned int init_cpb_removal_delay,
422                                 unsigned int init_cpb_removal_delay_offset,
423                                 unsigned int cpb_removal_length,
424                                 unsigned int cpb_removal_delay,
425                                 unsigned int dpb_output_length,
426                                 unsigned int dpb_output_delay,
427                                 unsigned char **sei_buffer)
428 {
429     unsigned char *byte_buf;
430     int bp_byte_size, i, pic_byte_size;
431
432     avc_bitstream nal_bs;
433     avc_bitstream sei_bp_bs, sei_pic_bs;
434
435     avc_bitstream_start(&sei_bp_bs);
436     avc_bitstream_put_ue(&sei_bp_bs, 0);       /*seq_parameter_set_id*/
437     avc_bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay, cpb_removal_length); 
438     avc_bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay_offset, cpb_removal_length); 
439     if ( sei_bp_bs.bit_offset & 0x7) {
440         avc_bitstream_put_ui(&sei_bp_bs, 1, 1);
441     }
442     avc_bitstream_end(&sei_bp_bs);
443     bp_byte_size = (sei_bp_bs.bit_offset + 7) / 8;
444     
445     avc_bitstream_start(&sei_pic_bs);
446     avc_bitstream_put_ui(&sei_pic_bs, cpb_removal_delay, cpb_removal_length); 
447     avc_bitstream_put_ui(&sei_pic_bs, dpb_output_delay, dpb_output_length); 
448     if ( sei_pic_bs.bit_offset & 0x7) {
449         avc_bitstream_put_ui(&sei_pic_bs, 1, 1);
450     }
451     avc_bitstream_end(&sei_pic_bs);
452     pic_byte_size = (sei_pic_bs.bit_offset + 7) / 8;
453     
454     avc_bitstream_start(&nal_bs);
455     nal_start_code_prefix(&nal_bs);
456     nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
457
458         /* Write the SEI buffer period data */    
459     avc_bitstream_put_ui(&nal_bs, 0, 8);
460     avc_bitstream_put_ui(&nal_bs, bp_byte_size, 8);
461     
462     byte_buf = (unsigned char *)sei_bp_bs.buffer;
463     for(i = 0; i < bp_byte_size; i++) {
464         avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
465     }
466     free(byte_buf);
467         /* write the SEI timing data */
468     avc_bitstream_put_ui(&nal_bs, 0x01, 8);
469     avc_bitstream_put_ui(&nal_bs, pic_byte_size, 8);
470     
471     byte_buf = (unsigned char *)sei_pic_bs.buffer;
472     for(i = 0; i < pic_byte_size; i++) {
473         avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
474     }
475     free(byte_buf);
476
477     avc_rbsp_trailing_bits(&nal_bs);
478     avc_bitstream_end(&nal_bs);
479
480     *sei_buffer = (unsigned char *)nal_bs.buffer; 
481    
482     return nal_bs.bit_offset;
483 }
484
485 int 
486 build_mpeg2_slice_header(VAEncSequenceParameterBufferMPEG2 *sps_param,
487                          VAEncPictureParameterBufferMPEG2 *pic_param,
488                          VAEncSliceParameterBufferMPEG2 *slice_param,
489                          unsigned char **slice_header_buffer)
490 {
491     avc_bitstream bs;
492
493     avc_bitstream_start(&bs);
494     avc_bitstream_end(&bs);
495     *slice_header_buffer = (unsigned char *)bs.buffer;
496
497     return bs.bit_offset;
498 }