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
45 #define SLICE_TYPE_P 0
46 #define SLICE_TYPE_B 1
47 #define SLICE_TYPE_I 2
49 #define IS_I_SLICE(type) (SLICE_TYPE_I == (type) || SLICE_TYPE_I == (type - 5))
50 #define IS_P_SLICE(type) (SLICE_TYPE_P == (type) || SLICE_TYPE_P == (type - 5))
51 #define IS_B_SLICE(type) (SLICE_TYPE_B == (type) || SLICE_TYPE_B == (type - 5))
53 #define ENTROPY_MODE_CAVLC 0
54 #define ENTROPY_MODE_CABAC 1
56 #define PROFILE_IDC_BASELINE 66
57 #define PROFILE_IDC_MAIN 77
58 #define PROFILE_IDC_HIGH 100
60 struct __avc_bitstream {
63 int max_size_in_dword;
66 typedef struct __avc_bitstream avc_bitstream;
69 swap32(unsigned int val)
71 unsigned char *pval = (unsigned char *)&val;
73 return ((pval[0] << 24) |
80 avc_bitstream_start(avc_bitstream *bs)
82 bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
83 bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
88 avc_bitstream_end(avc_bitstream *bs)
90 int pos = (bs->bit_offset >> 5);
91 int bit_offset = (bs->bit_offset & 0x1f);
92 int bit_left = 32 - bit_offset;
95 bs->buffer[pos] = swap32((bs->buffer[pos] << bit_left));
102 avc_bitstream_put_ui(avc_bitstream *bs, unsigned int val, int size_in_bits)
104 int pos = (bs->bit_offset >> 5);
105 int bit_offset = (bs->bit_offset & 0x1f);
106 int bit_left = 32 - bit_offset;
111 bs->bit_offset += size_in_bits;
113 if (bit_left > size_in_bits) {
114 bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
116 size_in_bits -= bit_left;
117 bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
118 bs->buffer[pos] = swap32(bs->buffer[pos]);
120 if (pos + 1 == bs->max_size_in_dword) {
121 bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
122 bs->buffer = realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
125 bs->buffer[pos + 1] = val;
130 avc_bitstream_put_ue(avc_bitstream *bs, unsigned int val)
132 int size_in_bits = 0;
140 avc_bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
141 avc_bitstream_put_ui(bs, val, size_in_bits);
145 avc_bitstream_put_se(avc_bitstream *bs, int val)
147 unsigned int new_val;
152 new_val = 2 * val - 1;
154 avc_bitstream_put_ue(bs, new_val);
158 avc_bitstream_byte_aligning(avc_bitstream *bs, int bit)
160 int bit_offset = (bs->bit_offset & 0x7);
161 int bit_left = 8 - bit_offset;
167 assert(bit == 0 || bit == 1);
170 new_val = (1 << bit_left) - 1;
174 avc_bitstream_put_ui(bs, new_val, bit_left);
177 static void nal_start_code_prefix(avc_bitstream *bs)
179 avc_bitstream_put_ui(bs, 0x00000001, 32);
182 static void nal_header(avc_bitstream *bs, int nal_ref_idc, int nal_unit_type)
184 avc_bitstream_put_ui(bs, 0, 1); /* forbidden_zero_bit: 0 */
185 avc_bitstream_put_ui(bs, nal_ref_idc, 2);
186 avc_bitstream_put_ui(bs, nal_unit_type, 5);
190 slice_header(avc_bitstream *bs,
191 VAEncSequenceParameterBufferH264Ext *sps_param,
192 VAEncPictureParameterBufferH264Ext *pic_param,
193 VAEncSliceParameterBufferH264Ext *slice_param,
194 VAEncH264DecRefPicMarkingBuffer *dec_ref_pic_marking_param)
196 int first_mb_in_slice = sps_param->picture_width_in_mbs * slice_param->start_row_number;
198 avc_bitstream_put_ue(bs, first_mb_in_slice); /* first_mb_in_slice: 0 */
199 avc_bitstream_put_ue(bs, slice_param->slice_type); /* slice_type */
200 avc_bitstream_put_ue(bs, slice_param->pic_parameter_set_id); /* pic_parameter_set_id: 0 */
201 avc_bitstream_put_ui(bs, pic_param->frame_num, sps_param->log2_max_frame_num_minus4 + 4); /* frame_num */
203 /* frame_mbs_only_flag == 1 */
204 if (!sps_param->frame_mbs_only_flag) {
209 if (pic_param->pic_fields.bits.idr_pic_flag)
210 avc_bitstream_put_ue(bs, slice_param->idr_pic_id); /* idr_pic_id: 0 */
212 if (sps_param->pic_order_cnt_type == 0) {
213 avc_bitstream_put_ui(bs, pic_param->CurrPic.TopFieldOrderCnt, sps_param->log2_max_pic_order_cnt_lsb_minus4 + 4);
214 /* pic_order_present_flag == 0 */
220 /* redundant_pic_cnt_present_flag == 0 */
223 if (IS_P_SLICE(slice_param->slice_type)) {
224 avc_bitstream_put_ui(bs, 0, 1); /* num_ref_idx_active_override_flag: 0 */
226 /* ref_pic_list_reordering */
227 assert(slice_param->ref_pic_list_modification_flag_l0 == 0);
228 avc_bitstream_put_ui(bs, slice_param->ref_pic_list_modification_flag_l0, 1); /* ref_pic_list_reordering_flag_l0: 0 */
229 } else if (IS_B_SLICE(slice_param->slice_type)) {
230 avc_bitstream_put_ui(bs, slice_param->direct_spatial_mv_pred_flag, 1); /* direct_spatial_mv_pred: 1 */
231 avc_bitstream_put_ui(bs, 0, 1); /* num_ref_idx_active_override_flag: 0 */
233 /* ref_pic_list_reordering */
234 assert(slice_param->ref_pic_list_modification_flag_l0 == 0);
235 assert(slice_param->ref_pic_list_modification_flag_l1 == 0);
236 avc_bitstream_put_ui(bs, slice_param->ref_pic_list_modification_flag_l0, 1); /* ref_pic_list_reordering_flag_l0: 0 */
237 avc_bitstream_put_ui(bs, slice_param->ref_pic_list_modification_flag_l1, 1); /* ref_pic_list_reordering_flag_l1: 0 */
240 if ((pic_param->pic_fields.bits.weighted_pred_flag &&
241 IS_P_SLICE(slice_param->slice_type)) ||
242 ((pic_param->pic_fields.bits.weighted_bipred_idc == 1) &&
243 IS_B_SLICE(slice_param->slice_type))) {
244 /* FIXME: fill weight/offset table */
248 /* dec_ref_pic_marking */
249 if (pic_param->pic_fields.bits.reference_pic_flag) { /* nal_ref_idc != 0 */
250 unsigned char no_output_of_prior_pics_flag = 0;
251 unsigned char long_term_reference_flag = 0;
252 unsigned char adaptive_ref_pic_marking_mode_flag = 0;
254 if (dec_ref_pic_marking_param) {
255 no_output_of_prior_pics_flag = dec_ref_pic_marking_param->no_output_of_prior_pics_flag;
256 long_term_reference_flag = dec_ref_pic_marking_param->long_term_reference_flag;
257 adaptive_ref_pic_marking_mode_flag = dec_ref_pic_marking_param->adaptive_ref_pic_marking_mode_flag;
259 assert(adaptive_ref_pic_marking_mode_flag == 0);
262 if (pic_param->pic_fields.bits.idr_pic_flag) {
263 avc_bitstream_put_ui(bs, no_output_of_prior_pics_flag, 1); /* no_output_of_prior_pics_flag: 0 */
264 avc_bitstream_put_ui(bs, long_term_reference_flag, 1); /* long_term_reference_flag: 0 */
266 avc_bitstream_put_ui(bs, adaptive_ref_pic_marking_mode_flag, 1); /* adaptive_ref_pic_marking_mode_flag: 0 */
270 if (pic_param->pic_fields.bits.entropy_coding_mode_flag &&
271 !IS_I_SLICE(slice_param->slice_type))
272 avc_bitstream_put_ue(bs, slice_param->cabac_init_idc); /* cabac_init_idc: 0 */
274 avc_bitstream_put_se(bs, slice_param->slice_qp_delta); /* slice_qp_delta: 0 */
276 /* ignore for SP/SI */
278 if (pic_param->pic_fields.bits.deblocking_filter_control_present_flag) {
279 avc_bitstream_put_ue(bs, slice_param->disable_deblocking_filter_idc); /* disable_deblocking_filter_idc: 0 */
281 if (slice_param->disable_deblocking_filter_idc != 0) {
282 avc_bitstream_put_se(bs, slice_param->slice_alpha_c0_offset_div2); /* slice_alpha_c0_offset_div2: 2 */
283 avc_bitstream_put_se(bs, slice_param->slice_beta_offset_div2); /* slice_beta_offset_div2: 2 */
287 if (pic_param->pic_fields.bits.entropy_coding_mode_flag) {
288 avc_bitstream_byte_aligning(bs, 1);
293 build_avc_slice_header(VAEncSequenceParameterBufferH264Ext *sps_param,
294 VAEncPictureParameterBufferH264Ext *pic_param,
295 VAEncSliceParameterBufferH264Ext *slice_param,
296 VAEncH264DecRefPicMarkingBuffer *dec_ref_pic_marking_param,
297 unsigned char **slice_header_buffer)
300 int is_idr = !!pic_param->pic_fields.bits.idr_pic_flag;
302 avc_bitstream_start(&bs);
303 nal_start_code_prefix(&bs);
305 if (IS_I_SLICE(slice_param->slice_type)) {
306 nal_header(&bs, NAL_REF_IDC_HIGH, is_idr ? NAL_IDR : NAL_NON_IDR);
307 } else if (IS_P_SLICE(slice_param->slice_type)) {
308 nal_header(&bs, NAL_REF_IDC_MEDIUM, is_idr ? NAL_IDR : NAL_NON_IDR);
310 assert(IS_B_SLICE(slice_param->slice_type));
311 nal_header(&bs, NAL_REF_IDC_NONE, is_idr ? NAL_IDR : NAL_NON_IDR);
314 slice_header(&bs, sps_param, pic_param, slice_param, dec_ref_pic_marking_param);
316 avc_bitstream_end(&bs);
317 *slice_header_buffer = (unsigned char *)bs.buffer;
319 return ((bs.bit_offset + 7) >> 3);