2 * Copyright © 2011 Intel Corporation
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:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
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.
30 #include <va/va_enc_h264.h>
31 #include <va/va_enc_mpeg2.h>
33 #include "i965_encoder_utils.h"
35 #define BITSTREAM_ALLOCATE_STEPPING 4096
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
48 #define SLICE_TYPE_P 0
49 #define SLICE_TYPE_B 1
50 #define SLICE_TYPE_I 2
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))
56 #define ENTROPY_MODE_CAVLC 0
57 #define ENTROPY_MODE_CABAC 1
59 #define PROFILE_IDC_BASELINE 66
60 #define PROFILE_IDC_MAIN 77
61 #define PROFILE_IDC_HIGH 100
63 struct __avc_bitstream {
66 int max_size_in_dword;
69 typedef struct __avc_bitstream avc_bitstream;
72 swap32(unsigned int val)
74 unsigned char *pval = (unsigned char *)&val;
76 return ((pval[0] << 24) |
83 avc_bitstream_start(avc_bitstream *bs)
85 bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
86 bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
91 avc_bitstream_end(avc_bitstream *bs)
93 int pos = (bs->bit_offset >> 5);
94 int bit_offset = (bs->bit_offset & 0x1f);
95 int bit_left = 32 - bit_offset;
98 bs->buffer[pos] = swap32((bs->buffer[pos] << bit_left));
105 avc_bitstream_put_ui(avc_bitstream *bs, unsigned int val, int size_in_bits)
107 int pos = (bs->bit_offset >> 5);
108 int bit_offset = (bs->bit_offset & 0x1f);
109 int bit_left = 32 - bit_offset;
114 if (size_in_bits < 32)
115 val &= (( 1 << size_in_bits) - 1);
117 bs->bit_offset += size_in_bits;
119 if (bit_left > size_in_bits) {
120 bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
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]);
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));
131 bs->buffer[pos + 1] = val;
136 avc_bitstream_put_ue(avc_bitstream *bs, unsigned int val)
138 int size_in_bits = 0;
146 avc_bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
147 avc_bitstream_put_ui(bs, val, size_in_bits);
151 avc_bitstream_put_se(avc_bitstream *bs, int val)
153 unsigned int new_val;
158 new_val = 2 * val - 1;
160 avc_bitstream_put_ue(bs, new_val);
164 avc_bitstream_byte_aligning(avc_bitstream *bs, int bit)
166 int bit_offset = (bs->bit_offset & 0x7);
167 int bit_left = 8 - bit_offset;
173 assert(bit == 0 || bit == 1);
176 new_val = (1 << bit_left) - 1;
180 avc_bitstream_put_ui(bs, new_val, bit_left);
182 static void avc_rbsp_trailing_bits(avc_bitstream *bs)
184 avc_bitstream_put_ui(bs, 1, 1);
185 avc_bitstream_byte_aligning(bs, 0);
187 static void nal_start_code_prefix(avc_bitstream *bs)
189 avc_bitstream_put_ui(bs, 0x00000001, 32);
192 static void nal_header(avc_bitstream *bs, int nal_ref_idc, int nal_unit_type)
194 avc_bitstream_put_ui(bs, 0, 1); /* forbidden_zero_bit: 0 */
195 avc_bitstream_put_ui(bs, nal_ref_idc, 2);
196 avc_bitstream_put_ui(bs, nal_unit_type, 5);
200 slice_header(avc_bitstream *bs,
201 VAEncSequenceParameterBufferH264 *sps_param,
202 VAEncPictureParameterBufferH264 *pic_param,
203 VAEncSliceParameterBufferH264 *slice_param)
205 int first_mb_in_slice = slice_param->macroblock_address;
207 avc_bitstream_put_ue(bs, first_mb_in_slice); /* first_mb_in_slice: 0 */
208 avc_bitstream_put_ue(bs, slice_param->slice_type); /* slice_type */
209 avc_bitstream_put_ue(bs, slice_param->pic_parameter_set_id); /* pic_parameter_set_id: 0 */
210 avc_bitstream_put_ui(bs, pic_param->frame_num, sps_param->seq_fields.bits.log2_max_frame_num_minus4 + 4); /* frame_num */
212 /* frame_mbs_only_flag == 1 */
213 if (!sps_param->seq_fields.bits.frame_mbs_only_flag) {
218 if (pic_param->pic_fields.bits.idr_pic_flag)
219 avc_bitstream_put_ue(bs, slice_param->idr_pic_id); /* idr_pic_id: 0 */
221 if (sps_param->seq_fields.bits.pic_order_cnt_type == 0) {
222 avc_bitstream_put_ui(bs, pic_param->CurrPic.TopFieldOrderCnt, sps_param->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 + 4);
223 /* pic_order_present_flag == 0 */
229 /* redundant_pic_cnt_present_flag == 0 */
232 if (IS_P_SLICE(slice_param->slice_type)) {
233 avc_bitstream_put_ui(bs, 0, 1); /* num_ref_idx_active_override_flag: 0 */
235 /* ref_pic_list_reordering */
236 avc_bitstream_put_ui(bs, 0, 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 */
241 /* ref_pic_list_reordering */
242 avc_bitstream_put_ui(bs, 0, 1); /* ref_pic_list_reordering_flag_l0: 0 */
243 avc_bitstream_put_ui(bs, 0, 1); /* ref_pic_list_reordering_flag_l1: 0 */
246 if ((pic_param->pic_fields.bits.weighted_pred_flag &&
247 IS_P_SLICE(slice_param->slice_type)) ||
248 ((pic_param->pic_fields.bits.weighted_bipred_idc == 1) &&
249 IS_B_SLICE(slice_param->slice_type))) {
250 /* FIXME: fill weight/offset table */
254 /* dec_ref_pic_marking */
255 if (pic_param->pic_fields.bits.reference_pic_flag) { /* nal_ref_idc != 0 */
256 unsigned char no_output_of_prior_pics_flag = 0;
257 unsigned char long_term_reference_flag = 0;
258 unsigned char adaptive_ref_pic_marking_mode_flag = 0;
260 if (pic_param->pic_fields.bits.idr_pic_flag) {
261 avc_bitstream_put_ui(bs, no_output_of_prior_pics_flag, 1); /* no_output_of_prior_pics_flag: 0 */
262 avc_bitstream_put_ui(bs, long_term_reference_flag, 1); /* long_term_reference_flag: 0 */
264 avc_bitstream_put_ui(bs, adaptive_ref_pic_marking_mode_flag, 1); /* adaptive_ref_pic_marking_mode_flag: 0 */
268 if (pic_param->pic_fields.bits.entropy_coding_mode_flag &&
269 !IS_I_SLICE(slice_param->slice_type))
270 avc_bitstream_put_ue(bs, slice_param->cabac_init_idc); /* cabac_init_idc: 0 */
272 avc_bitstream_put_se(bs, slice_param->slice_qp_delta); /* slice_qp_delta: 0 */
274 /* ignore for SP/SI */
276 if (pic_param->pic_fields.bits.deblocking_filter_control_present_flag) {
277 avc_bitstream_put_ue(bs, slice_param->disable_deblocking_filter_idc); /* disable_deblocking_filter_idc: 0 */
279 if (slice_param->disable_deblocking_filter_idc != 1) {
280 avc_bitstream_put_se(bs, slice_param->slice_alpha_c0_offset_div2); /* slice_alpha_c0_offset_div2: 2 */
281 avc_bitstream_put_se(bs, slice_param->slice_beta_offset_div2); /* slice_beta_offset_div2: 2 */
285 if (pic_param->pic_fields.bits.entropy_coding_mode_flag) {
286 avc_bitstream_byte_aligning(bs, 1);
291 build_avc_slice_header(VAEncSequenceParameterBufferH264 *sps_param,
292 VAEncPictureParameterBufferH264 *pic_param,
293 VAEncSliceParameterBufferH264 *slice_param,
294 unsigned char **slice_header_buffer)
297 int is_idr = !!pic_param->pic_fields.bits.idr_pic_flag;
299 avc_bitstream_start(&bs);
300 nal_start_code_prefix(&bs);
302 if (IS_I_SLICE(slice_param->slice_type)) {
303 nal_header(&bs, NAL_REF_IDC_HIGH, is_idr ? NAL_IDR : NAL_NON_IDR);
304 } else if (IS_P_SLICE(slice_param->slice_type)) {
305 nal_header(&bs, NAL_REF_IDC_MEDIUM, is_idr ? NAL_IDR : NAL_NON_IDR);
307 assert(IS_B_SLICE(slice_param->slice_type));
308 nal_header(&bs, NAL_REF_IDC_NONE, is_idr ? NAL_IDR : NAL_NON_IDR);
311 slice_header(&bs, sps_param, pic_param, slice_param);
313 avc_bitstream_end(&bs);
314 *slice_header_buffer = (unsigned char *)bs.buffer;
316 return bs.bit_offset;
320 build_avc_sei_buffering_period(int cpb_removal_length,
321 unsigned int init_cpb_removal_delay,
322 unsigned int init_cpb_removal_delay_offset,
323 unsigned char **sei_buffer)
325 unsigned char *byte_buf;
328 avc_bitstream nal_bs;
329 avc_bitstream sei_bs;
331 avc_bitstream_start(&sei_bs);
332 avc_bitstream_put_ue(&sei_bs, 0); /*seq_parameter_set_id*/
333 avc_bitstream_put_ui(&sei_bs, init_cpb_removal_delay, cpb_removal_length);
334 avc_bitstream_put_ui(&sei_bs, init_cpb_removal_delay_offset, cpb_removal_length);
335 if ( sei_bs.bit_offset & 0x7) {
336 avc_bitstream_put_ui(&sei_bs, 1, 1);
338 avc_bitstream_end(&sei_bs);
339 byte_size = (sei_bs.bit_offset + 7) / 8;
341 avc_bitstream_start(&nal_bs);
342 nal_start_code_prefix(&nal_bs);
343 nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
345 avc_bitstream_put_ui(&nal_bs, 0, 8);
346 avc_bitstream_put_ui(&nal_bs, byte_size, 8);
348 byte_buf = (unsigned char *)sei_bs.buffer;
349 for(i = 0; i < byte_size; i++) {
350 avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
354 avc_rbsp_trailing_bits(&nal_bs);
355 avc_bitstream_end(&nal_bs);
357 *sei_buffer = (unsigned char *)nal_bs.buffer;
359 return nal_bs.bit_offset;
363 build_avc_sei_pic_timing(unsigned int cpb_removal_length, unsigned int cpb_removal_delay,
364 unsigned int dpb_output_length, unsigned int dpb_output_delay,
365 unsigned char **sei_buffer)
367 unsigned char *byte_buf;
370 avc_bitstream nal_bs;
371 avc_bitstream sei_bs;
373 avc_bitstream_start(&sei_bs);
374 avc_bitstream_put_ui(&sei_bs, cpb_removal_delay, cpb_removal_length);
375 avc_bitstream_put_ui(&sei_bs, dpb_output_delay, dpb_output_length);
376 if ( sei_bs.bit_offset & 0x7) {
377 avc_bitstream_put_ui(&sei_bs, 1, 1);
379 avc_bitstream_end(&sei_bs);
380 byte_size = (sei_bs.bit_offset + 7) / 8;
382 avc_bitstream_start(&nal_bs);
383 nal_start_code_prefix(&nal_bs);
384 nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
386 avc_bitstream_put_ui(&nal_bs, 0x01, 8);
387 avc_bitstream_put_ui(&nal_bs, byte_size, 8);
389 byte_buf = (unsigned char *)sei_bs.buffer;
390 for(i = 0; i < byte_size; i++) {
391 avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
395 avc_rbsp_trailing_bits(&nal_bs);
396 avc_bitstream_end(&nal_bs);
398 *sei_buffer = (unsigned char *)nal_bs.buffer;
400 return nal_bs.bit_offset;
405 build_avc_sei_buffer_timing(unsigned int init_cpb_removal_length,
406 unsigned int init_cpb_removal_delay,
407 unsigned int init_cpb_removal_delay_offset,
408 unsigned int cpb_removal_length,
409 unsigned int cpb_removal_delay,
410 unsigned int dpb_output_length,
411 unsigned int dpb_output_delay,
412 unsigned char **sei_buffer)
414 unsigned char *byte_buf;
415 int bp_byte_size, i, pic_byte_size;
417 avc_bitstream nal_bs;
418 avc_bitstream sei_bp_bs, sei_pic_bs;
420 avc_bitstream_start(&sei_bp_bs);
421 avc_bitstream_put_ue(&sei_bp_bs, 0); /*seq_parameter_set_id*/
422 avc_bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay, cpb_removal_length);
423 avc_bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay_offset, cpb_removal_length);
424 if ( sei_bp_bs.bit_offset & 0x7) {
425 avc_bitstream_put_ui(&sei_bp_bs, 1, 1);
427 avc_bitstream_end(&sei_bp_bs);
428 bp_byte_size = (sei_bp_bs.bit_offset + 7) / 8;
430 avc_bitstream_start(&sei_pic_bs);
431 avc_bitstream_put_ui(&sei_pic_bs, cpb_removal_delay, cpb_removal_length);
432 avc_bitstream_put_ui(&sei_pic_bs, dpb_output_delay, dpb_output_length);
433 if ( sei_pic_bs.bit_offset & 0x7) {
434 avc_bitstream_put_ui(&sei_pic_bs, 1, 1);
436 avc_bitstream_end(&sei_pic_bs);
437 pic_byte_size = (sei_pic_bs.bit_offset + 7) / 8;
439 avc_bitstream_start(&nal_bs);
440 nal_start_code_prefix(&nal_bs);
441 nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
443 /* Write the SEI buffer period data */
444 avc_bitstream_put_ui(&nal_bs, 0, 8);
445 avc_bitstream_put_ui(&nal_bs, bp_byte_size, 8);
447 byte_buf = (unsigned char *)sei_bp_bs.buffer;
448 for(i = 0; i < bp_byte_size; i++) {
449 avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
452 /* write the SEI timing data */
453 avc_bitstream_put_ui(&nal_bs, 0x01, 8);
454 avc_bitstream_put_ui(&nal_bs, pic_byte_size, 8);
456 byte_buf = (unsigned char *)sei_pic_bs.buffer;
457 for(i = 0; i < pic_byte_size; i++) {
458 avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
462 avc_rbsp_trailing_bits(&nal_bs);
463 avc_bitstream_end(&nal_bs);
465 *sei_buffer = (unsigned char *)nal_bs.buffer;
467 return nal_bs.bit_offset;
471 build_mpeg2_slice_header(VAEncSequenceParameterBufferMPEG2 *sps_param,
472 VAEncPictureParameterBufferMPEG2 *pic_param,
473 VAEncSliceParameterBufferMPEG2 *slice_param,
474 unsigned char **slice_header_buffer)
478 avc_bitstream_start(&bs);
479 avc_bitstream_end(&bs);
480 *slice_header_buffer = (unsigned char *)bs.buffer;
482 return bs.bit_offset;