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.
31 #include "i965_encoder_utils.h"
33 #define BITSTREAM_ALLOCATE_STEPPING 4096
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
46 #define SLICE_TYPE_P 0
47 #define SLICE_TYPE_B 1
48 #define SLICE_TYPE_I 2
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))
54 #define ENTROPY_MODE_CAVLC 0
55 #define ENTROPY_MODE_CABAC 1
57 #define PROFILE_IDC_BASELINE 66
58 #define PROFILE_IDC_MAIN 77
59 #define PROFILE_IDC_HIGH 100
61 struct __avc_bitstream {
64 int max_size_in_dword;
67 typedef struct __avc_bitstream avc_bitstream;
70 swap32(unsigned int val)
72 unsigned char *pval = (unsigned char *)&val;
74 return ((pval[0] << 24) |
81 avc_bitstream_start(avc_bitstream *bs)
83 bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
84 bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
89 avc_bitstream_end(avc_bitstream *bs)
91 int pos = (bs->bit_offset >> 5);
92 int bit_offset = (bs->bit_offset & 0x1f);
93 int bit_left = 32 - bit_offset;
96 bs->buffer[pos] = swap32((bs->buffer[pos] << bit_left));
103 avc_bitstream_put_ui(avc_bitstream *bs, unsigned int val, int size_in_bits)
105 int pos = (bs->bit_offset >> 5);
106 int bit_offset = (bs->bit_offset & 0x1f);
107 int bit_left = 32 - bit_offset;
112 if (size_in_bits < 32)
113 val &= (( 1 << size_in_bits) - 1);
115 bs->bit_offset += size_in_bits;
117 if (bit_left > size_in_bits) {
118 bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
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]);
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));
129 bs->buffer[pos + 1] = val;
134 avc_bitstream_put_ue(avc_bitstream *bs, unsigned int val)
136 int size_in_bits = 0;
144 avc_bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
145 avc_bitstream_put_ui(bs, val, size_in_bits);
149 avc_bitstream_put_se(avc_bitstream *bs, int val)
151 unsigned int new_val;
156 new_val = 2 * val - 1;
158 avc_bitstream_put_ue(bs, new_val);
162 avc_bitstream_byte_aligning(avc_bitstream *bs, int bit)
164 int bit_offset = (bs->bit_offset & 0x7);
165 int bit_left = 8 - bit_offset;
171 assert(bit == 0 || bit == 1);
174 new_val = (1 << bit_left) - 1;
178 avc_bitstream_put_ui(bs, new_val, bit_left);
180 static void avc_rbsp_trailing_bits(avc_bitstream *bs)
182 avc_bitstream_put_ui(bs, 1, 1);
183 avc_bitstream_byte_aligning(bs, 0);
185 static void nal_start_code_prefix(avc_bitstream *bs)
187 avc_bitstream_put_ui(bs, 0x00000001, 32);
190 static void nal_header(avc_bitstream *bs, int nal_ref_idc, int nal_unit_type)
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);
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)
204 int first_mb_in_slice = slice_param->starting_macroblock_address;
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 */
211 /* frame_mbs_only_flag == 1 */
212 if (!sps_param->frame_mbs_only_flag) {
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 */
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 */
228 /* redundant_pic_cnt_present_flag == 0 */
231 if (IS_P_SLICE(slice_param->slice_type)) {
232 avc_bitstream_put_ui(bs, 0, 1); /* num_ref_idx_active_override_flag: 0 */
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 */
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 */
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 */
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;
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;
267 assert(adaptive_ref_pic_marking_mode_flag == 0);
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 */
274 avc_bitstream_put_ui(bs, adaptive_ref_pic_marking_mode_flag, 1); /* adaptive_ref_pic_marking_mode_flag: 0 */
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 */
282 avc_bitstream_put_se(bs, slice_param->slice_qp_delta); /* slice_qp_delta: 0 */
284 /* ignore for SP/SI */
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 */
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 */
295 if (pic_param->pic_fields.bits.entropy_coding_mode_flag) {
296 avc_bitstream_byte_aligning(bs, 1);
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)
308 int is_idr = !!pic_param->pic_fields.bits.idr_pic_flag;
310 avc_bitstream_start(&bs);
311 nal_start_code_prefix(&bs);
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);
318 assert(IS_B_SLICE(slice_param->slice_type));
319 nal_header(&bs, NAL_REF_IDC_NONE, is_idr ? NAL_IDR : NAL_NON_IDR);
322 slice_header(&bs, sps_param, pic_param, slice_param, dec_ref_pic_marking_param);
324 avc_bitstream_end(&bs);
325 *slice_header_buffer = (unsigned char *)bs.buffer;
327 return bs.bit_offset;
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)
336 unsigned char *byte_buf;
339 avc_bitstream nal_bs;
340 avc_bitstream sei_bs;
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);
349 avc_bitstream_end(&sei_bs);
350 byte_size = (sei_bs.bit_offset + 7) / 8;
352 avc_bitstream_start(&nal_bs);
353 nal_start_code_prefix(&nal_bs);
354 nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
356 avc_bitstream_put_ui(&nal_bs, 0, 8);
357 avc_bitstream_put_ui(&nal_bs, byte_size, 8);
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);
365 avc_rbsp_trailing_bits(&nal_bs);
366 avc_bitstream_end(&nal_bs);
368 *sei_buffer = (unsigned char *)nal_bs.buffer;
370 return nal_bs.bit_offset;
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)
378 unsigned char *byte_buf;
381 avc_bitstream nal_bs;
382 avc_bitstream sei_bs;
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);
390 avc_bitstream_end(&sei_bs);
391 byte_size = (sei_bs.bit_offset + 7) / 8;
393 avc_bitstream_start(&nal_bs);
394 nal_start_code_prefix(&nal_bs);
395 nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
397 avc_bitstream_put_ui(&nal_bs, 0x01, 8);
398 avc_bitstream_put_ui(&nal_bs, byte_size, 8);
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);
406 avc_rbsp_trailing_bits(&nal_bs);
407 avc_bitstream_end(&nal_bs);
409 *sei_buffer = (unsigned char *)nal_bs.buffer;
411 return nal_bs.bit_offset;