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 #include <va/va_enc_vp8.h>
33 #include <va/va_enc_hevc.h>
34 #include <math.h>
35 #include "gen6_mfc.h"
36 #include "i965_encoder_utils.h"
37
38 #define BITSTREAM_ALLOCATE_STEPPING 4096
39
40 #define NAL_REF_IDC_NONE 0
41 #define NAL_REF_IDC_LOW 1
42 #define NAL_REF_IDC_MEDIUM 2
43 #define NAL_REF_IDC_HIGH 3
44
45 #define NAL_NON_IDR 1
46 #define NAL_IDR 5
47 #define NAL_SPS 7
48 #define NAL_PPS 8
49 #define NAL_SEI 6
50
51 #define SLICE_TYPE_P 0
52 #define SLICE_TYPE_B 1
53 #define SLICE_TYPE_I 2
54
55 #define IS_I_SLICE(type) (SLICE_TYPE_I == (type) || SLICE_TYPE_I == (type - 5))
56 #define IS_P_SLICE(type) (SLICE_TYPE_P == (type) || SLICE_TYPE_P == (type - 5))
57 #define IS_B_SLICE(type) (SLICE_TYPE_B == (type) || SLICE_TYPE_B == (type - 5))
58
59 #define ENTROPY_MODE_CAVLC 0
60 #define ENTROPY_MODE_CABAC 1
61
62 #define PROFILE_IDC_BASELINE 66
63 #define PROFILE_IDC_MAIN 77
64 #define PROFILE_IDC_HIGH 100
65
66 /*HEVC*/
67 #define VPS_NUT 32
68 #define SPS_NUT 33
69 #define PPS_NUT 34
70 #define IDR_WRADL_NUT 19
71 #define IDR_NLP_NUT 20
72 #define SLICE_TRAIL_N_NUT 0
73 #define SLICE_TRAIL_R_NUT 1
74 #define PREFIX_SEI_NUT 39
75 #define SUFFIX_SEI_NUT 40
76
77 struct __avc_bitstream {
78 unsigned int *buffer;
79 int bit_offset;
80 int max_size_in_dword;
81 };
82
83 typedef struct __avc_bitstream avc_bitstream;
84
85 static unsigned int
swap32(unsigned int val)86 swap32(unsigned int val)
87 {
88 unsigned char *pval = (unsigned char *)&val;
89
90 return ((pval[0] << 24) |
91 (pval[1] << 16) |
92 (pval[2] << 8) |
93 (pval[3] << 0));
94 }
95
96 static void
avc_bitstream_start(avc_bitstream * bs)97 avc_bitstream_start(avc_bitstream *bs)
98 {
99 bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
100 bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
101 bs->bit_offset = 0;
102 }
103
104 static void
avc_bitstream_end(avc_bitstream * bs)105 avc_bitstream_end(avc_bitstream *bs)
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 (bit_offset) {
112 bs->buffer[pos] = swap32((bs->buffer[pos] << bit_left));
113 }
114
115 // free(bs->buffer);
116 }
117
118 static void
avc_bitstream_put_ui(avc_bitstream * bs,unsigned int val,int size_in_bits)119 avc_bitstream_put_ui(avc_bitstream *bs, unsigned int val, int size_in_bits)
120 {
121 int pos = (bs->bit_offset >> 5);
122 int bit_offset = (bs->bit_offset & 0x1f);
123 int bit_left = 32 - bit_offset;
124
125 if (!size_in_bits)
126 return;
127
128 if (size_in_bits < 32)
129 val &= ((1 << size_in_bits) - 1);
130
131 bs->bit_offset += size_in_bits;
132
133 if (bit_left > size_in_bits) {
134 bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
135 } else {
136 size_in_bits -= bit_left;
137 if (bit_left == 32) {
138 bs->buffer[pos] = val;
139 } else {
140 bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
141 }
142 bs->buffer[pos] = swap32(bs->buffer[pos]);
143
144 if (pos + 1 == bs->max_size_in_dword) {
145 bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
146 bs->buffer = realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
147
148 if (!bs->buffer)
149 return;
150 }
151
152 bs->buffer[pos + 1] = val;
153 }
154 }
155
156 static void
avc_bitstream_put_ue(avc_bitstream * bs,unsigned int val)157 avc_bitstream_put_ue(avc_bitstream *bs, unsigned int val)
158 {
159 int size_in_bits = 0;
160 int tmp_val = ++val;
161
162 while (tmp_val) {
163 tmp_val >>= 1;
164 size_in_bits++;
165 }
166
167 avc_bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
168 avc_bitstream_put_ui(bs, val, size_in_bits);
169 }
170
171 static void
avc_bitstream_put_se(avc_bitstream * bs,int val)172 avc_bitstream_put_se(avc_bitstream *bs, int val)
173 {
174 unsigned int new_val;
175
176 if (val <= 0)
177 new_val = -2 * val;
178 else
179 new_val = 2 * val - 1;
180
181 avc_bitstream_put_ue(bs, new_val);
182 }
183
184 static void
avc_bitstream_byte_aligning(avc_bitstream * bs,int bit)185 avc_bitstream_byte_aligning(avc_bitstream *bs, int bit)
186 {
187 int bit_offset = (bs->bit_offset & 0x7);
188 int bit_left = 8 - bit_offset;
189 int new_val;
190
191 if (!bit_offset)
192 return;
193
194 assert(bit == 0 || bit == 1);
195
196 if (bit)
197 new_val = (1 << bit_left) - 1;
198 else
199 new_val = 0;
200
201 avc_bitstream_put_ui(bs, new_val, bit_left);
202 }
avc_rbsp_trailing_bits(avc_bitstream * bs)203 static void avc_rbsp_trailing_bits(avc_bitstream *bs)
204 {
205 avc_bitstream_put_ui(bs, 1, 1);
206 avc_bitstream_byte_aligning(bs, 0);
207 }
nal_start_code_prefix(avc_bitstream * bs)208 static void nal_start_code_prefix(avc_bitstream *bs)
209 {
210 avc_bitstream_put_ui(bs, 0x00000001, 32);
211 }
212
nal_header(avc_bitstream * bs,int nal_ref_idc,int nal_unit_type)213 static void nal_header(avc_bitstream *bs, int nal_ref_idc, int nal_unit_type)
214 {
215 avc_bitstream_put_ui(bs, 0, 1); /* forbidden_zero_bit: 0 */
216 avc_bitstream_put_ui(bs, nal_ref_idc, 2);
217 avc_bitstream_put_ui(bs, nal_unit_type, 5);
218 }
219
220 static void
slice_header(avc_bitstream * bs,VAEncSequenceParameterBufferH264 * sps_param,VAEncPictureParameterBufferH264 * pic_param,VAEncSliceParameterBufferH264 * slice_param)221 slice_header(avc_bitstream *bs,
222 VAEncSequenceParameterBufferH264 *sps_param,
223 VAEncPictureParameterBufferH264 *pic_param,
224 VAEncSliceParameterBufferH264 *slice_param)
225 {
226 int first_mb_in_slice = slice_param->macroblock_address;
227
228 avc_bitstream_put_ue(bs, first_mb_in_slice); /* first_mb_in_slice: 0 */
229 avc_bitstream_put_ue(bs, slice_param->slice_type); /* slice_type */
230 avc_bitstream_put_ue(bs, slice_param->pic_parameter_set_id); /* pic_parameter_set_id: 0 */
231 avc_bitstream_put_ui(bs, pic_param->frame_num, sps_param->seq_fields.bits.log2_max_frame_num_minus4 + 4); /* frame_num */
232
233 /* frame_mbs_only_flag == 1 */
234 if (!sps_param->seq_fields.bits.frame_mbs_only_flag) {
235 /* FIXME: */
236 assert(0);
237 }
238
239 if (pic_param->pic_fields.bits.idr_pic_flag)
240 avc_bitstream_put_ue(bs, slice_param->idr_pic_id); /* idr_pic_id: 0 */
241
242 if (sps_param->seq_fields.bits.pic_order_cnt_type == 0) {
243 avc_bitstream_put_ui(bs, pic_param->CurrPic.TopFieldOrderCnt, sps_param->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 + 4);
244 /* pic_order_present_flag == 0 */
245 } else {
246 /* FIXME: */
247 assert(0);
248 }
249
250 /* redundant_pic_cnt_present_flag == 0 */
251
252 /* slice type */
253 if (IS_P_SLICE(slice_param->slice_type)) {
254 avc_bitstream_put_ui(bs, slice_param->num_ref_idx_active_override_flag, 1); /* num_ref_idx_active_override_flag: */
255
256 if (slice_param->num_ref_idx_active_override_flag)
257 avc_bitstream_put_ue(bs, slice_param->num_ref_idx_l0_active_minus1);
258
259 /* ref_pic_list_reordering */
260 avc_bitstream_put_ui(bs, 0, 1); /* ref_pic_list_reordering_flag_l0: 0 */
261 } else if (IS_B_SLICE(slice_param->slice_type)) {
262 avc_bitstream_put_ui(bs, slice_param->direct_spatial_mv_pred_flag, 1); /* direct_spatial_mv_pred: 1 */
263
264 avc_bitstream_put_ui(bs, slice_param->num_ref_idx_active_override_flag, 1); /* num_ref_idx_active_override_flag: */
265
266 if (slice_param->num_ref_idx_active_override_flag) {
267 avc_bitstream_put_ue(bs, slice_param->num_ref_idx_l0_active_minus1);
268 avc_bitstream_put_ue(bs, slice_param->num_ref_idx_l1_active_minus1);
269 }
270
271 /* ref_pic_list_reordering */
272 avc_bitstream_put_ui(bs, 0, 1); /* ref_pic_list_reordering_flag_l0: 0 */
273 avc_bitstream_put_ui(bs, 0, 1); /* ref_pic_list_reordering_flag_l1: 0 */
274 }
275
276 if ((pic_param->pic_fields.bits.weighted_pred_flag &&
277 IS_P_SLICE(slice_param->slice_type)) ||
278 ((pic_param->pic_fields.bits.weighted_bipred_idc == 1) &&
279 IS_B_SLICE(slice_param->slice_type))) {
280 /* FIXME: fill weight/offset table */
281 assert(0);
282 }
283
284 /* dec_ref_pic_marking */
285 if (pic_param->pic_fields.bits.reference_pic_flag) { /* nal_ref_idc != 0 */
286 unsigned char no_output_of_prior_pics_flag = 0;
287 unsigned char long_term_reference_flag = 0;
288 unsigned char adaptive_ref_pic_marking_mode_flag = 0;
289
290 if (pic_param->pic_fields.bits.idr_pic_flag) {
291 avc_bitstream_put_ui(bs, no_output_of_prior_pics_flag, 1); /* no_output_of_prior_pics_flag: 0 */
292 avc_bitstream_put_ui(bs, long_term_reference_flag, 1); /* long_term_reference_flag: 0 */
293 } else {
294 avc_bitstream_put_ui(bs, adaptive_ref_pic_marking_mode_flag, 1); /* adaptive_ref_pic_marking_mode_flag: 0 */
295 }
296 }
297
298 if (pic_param->pic_fields.bits.entropy_coding_mode_flag &&
299 !IS_I_SLICE(slice_param->slice_type))
300 avc_bitstream_put_ue(bs, slice_param->cabac_init_idc); /* cabac_init_idc: 0 */
301
302 avc_bitstream_put_se(bs, slice_param->slice_qp_delta); /* slice_qp_delta: 0 */
303
304 /* ignore for SP/SI */
305
306 if (pic_param->pic_fields.bits.deblocking_filter_control_present_flag) {
307 avc_bitstream_put_ue(bs, slice_param->disable_deblocking_filter_idc); /* disable_deblocking_filter_idc: 0 */
308
309 if (slice_param->disable_deblocking_filter_idc != 1) {
310 avc_bitstream_put_se(bs, slice_param->slice_alpha_c0_offset_div2); /* slice_alpha_c0_offset_div2: 2 */
311 avc_bitstream_put_se(bs, slice_param->slice_beta_offset_div2); /* slice_beta_offset_div2: 2 */
312 }
313 }
314
315 if (pic_param->pic_fields.bits.entropy_coding_mode_flag) {
316 avc_bitstream_byte_aligning(bs, 1);
317 }
318 }
319
320 int
build_avc_slice_header(VAEncSequenceParameterBufferH264 * sps_param,VAEncPictureParameterBufferH264 * pic_param,VAEncSliceParameterBufferH264 * slice_param,unsigned char ** slice_header_buffer)321 build_avc_slice_header(VAEncSequenceParameterBufferH264 *sps_param,
322 VAEncPictureParameterBufferH264 *pic_param,
323 VAEncSliceParameterBufferH264 *slice_param,
324 unsigned char **slice_header_buffer)
325 {
326 avc_bitstream bs;
327 int is_idr = !!pic_param->pic_fields.bits.idr_pic_flag;
328 int is_ref = !!pic_param->pic_fields.bits.reference_pic_flag;
329
330 avc_bitstream_start(&bs);
331 nal_start_code_prefix(&bs);
332
333 if (IS_I_SLICE(slice_param->slice_type)) {
334 nal_header(&bs, NAL_REF_IDC_HIGH, is_idr ? NAL_IDR : NAL_NON_IDR);
335 } else if (IS_P_SLICE(slice_param->slice_type)) {
336 assert(!is_idr);
337 nal_header(&bs, NAL_REF_IDC_MEDIUM, NAL_NON_IDR);
338 } else {
339 assert(IS_B_SLICE(slice_param->slice_type));
340 assert(!is_idr);
341 nal_header(&bs, is_ref ? NAL_REF_IDC_LOW : NAL_REF_IDC_NONE, NAL_NON_IDR);
342 }
343
344 slice_header(&bs, sps_param, pic_param, slice_param);
345
346 avc_bitstream_end(&bs);
347 *slice_header_buffer = (unsigned char *)bs.buffer;
348
349 return bs.bit_offset;
350 }
351
352 int
build_avc_sei_buffering_period(int cpb_removal_length,unsigned int init_cpb_removal_delay,unsigned int init_cpb_removal_delay_offset,unsigned char ** sei_buffer)353 build_avc_sei_buffering_period(int cpb_removal_length,
354 unsigned int init_cpb_removal_delay,
355 unsigned int init_cpb_removal_delay_offset,
356 unsigned char **sei_buffer)
357 {
358 unsigned char *byte_buf;
359 int byte_size, i;
360
361 avc_bitstream nal_bs;
362 avc_bitstream sei_bs;
363
364 avc_bitstream_start(&sei_bs);
365 avc_bitstream_put_ue(&sei_bs, 0); /*seq_parameter_set_id*/
366 avc_bitstream_put_ui(&sei_bs, init_cpb_removal_delay, cpb_removal_length);
367 avc_bitstream_put_ui(&sei_bs, init_cpb_removal_delay_offset, cpb_removal_length);
368 if (sei_bs.bit_offset & 0x7) {
369 avc_bitstream_put_ui(&sei_bs, 1, 1);
370 }
371 avc_bitstream_end(&sei_bs);
372 byte_size = (sei_bs.bit_offset + 7) / 8;
373
374 avc_bitstream_start(&nal_bs);
375 nal_start_code_prefix(&nal_bs);
376 nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
377
378 avc_bitstream_put_ui(&nal_bs, 0, 8);
379 avc_bitstream_put_ui(&nal_bs, byte_size, 8);
380
381 byte_buf = (unsigned char *)sei_bs.buffer;
382 for (i = 0; i < byte_size; i++) {
383 avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
384 }
385 free(byte_buf);
386
387 avc_rbsp_trailing_bits(&nal_bs);
388 avc_bitstream_end(&nal_bs);
389
390 *sei_buffer = (unsigned char *)nal_bs.buffer;
391
392 return nal_bs.bit_offset;
393 }
394
395 int
build_avc_sei_pic_timing(unsigned int cpb_removal_length,unsigned int cpb_removal_delay,unsigned int dpb_output_length,unsigned int dpb_output_delay,unsigned char ** sei_buffer)396 build_avc_sei_pic_timing(unsigned int cpb_removal_length, unsigned int cpb_removal_delay,
397 unsigned int dpb_output_length, unsigned int dpb_output_delay,
398 unsigned char **sei_buffer)
399 {
400 unsigned char *byte_buf;
401 int byte_size, i;
402
403 avc_bitstream nal_bs;
404 avc_bitstream sei_bs;
405
406 avc_bitstream_start(&sei_bs);
407 avc_bitstream_put_ui(&sei_bs, cpb_removal_delay, cpb_removal_length);
408 avc_bitstream_put_ui(&sei_bs, dpb_output_delay, dpb_output_length);
409 if (sei_bs.bit_offset & 0x7) {
410 avc_bitstream_put_ui(&sei_bs, 1, 1);
411 }
412 avc_bitstream_end(&sei_bs);
413 byte_size = (sei_bs.bit_offset + 7) / 8;
414
415 avc_bitstream_start(&nal_bs);
416 nal_start_code_prefix(&nal_bs);
417 nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
418
419 avc_bitstream_put_ui(&nal_bs, 0x01, 8);
420 avc_bitstream_put_ui(&nal_bs, byte_size, 8);
421
422 byte_buf = (unsigned char *)sei_bs.buffer;
423 for (i = 0; i < byte_size; i++) {
424 avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
425 }
426 free(byte_buf);
427
428 avc_rbsp_trailing_bits(&nal_bs);
429 avc_bitstream_end(&nal_bs);
430
431 *sei_buffer = (unsigned char *)nal_bs.buffer;
432
433 return nal_bs.bit_offset;
434 }
435
436
437 int
build_avc_sei_buffer_timing(unsigned int init_cpb_removal_length,unsigned int init_cpb_removal_delay,unsigned int init_cpb_removal_delay_offset,unsigned int cpb_removal_length,unsigned int cpb_removal_delay,unsigned int dpb_output_length,unsigned int dpb_output_delay,unsigned char ** sei_buffer)438 build_avc_sei_buffer_timing(unsigned int init_cpb_removal_length,
439 unsigned int init_cpb_removal_delay,
440 unsigned int init_cpb_removal_delay_offset,
441 unsigned int cpb_removal_length,
442 unsigned int cpb_removal_delay,
443 unsigned int dpb_output_length,
444 unsigned int dpb_output_delay,
445 unsigned char **sei_buffer)
446 {
447 unsigned char *byte_buf;
448 int bp_byte_size, i, pic_byte_size;
449
450 avc_bitstream nal_bs;
451 avc_bitstream sei_bp_bs, sei_pic_bs;
452
453 avc_bitstream_start(&sei_bp_bs);
454 avc_bitstream_put_ue(&sei_bp_bs, 0); /*seq_parameter_set_id*/
455 avc_bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay, cpb_removal_length);
456 avc_bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay_offset, cpb_removal_length);
457 if (sei_bp_bs.bit_offset & 0x7) {
458 avc_bitstream_put_ui(&sei_bp_bs, 1, 1);
459 }
460 avc_bitstream_end(&sei_bp_bs);
461 bp_byte_size = (sei_bp_bs.bit_offset + 7) / 8;
462
463 avc_bitstream_start(&sei_pic_bs);
464 avc_bitstream_put_ui(&sei_pic_bs, cpb_removal_delay, cpb_removal_length);
465 avc_bitstream_put_ui(&sei_pic_bs, dpb_output_delay, dpb_output_length);
466 if (sei_pic_bs.bit_offset & 0x7) {
467 avc_bitstream_put_ui(&sei_pic_bs, 1, 1);
468 }
469 avc_bitstream_end(&sei_pic_bs);
470 pic_byte_size = (sei_pic_bs.bit_offset + 7) / 8;
471
472 avc_bitstream_start(&nal_bs);
473 nal_start_code_prefix(&nal_bs);
474 nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
475
476 /* Write the SEI buffer period data */
477 avc_bitstream_put_ui(&nal_bs, 0, 8);
478 avc_bitstream_put_ui(&nal_bs, bp_byte_size, 8);
479
480 byte_buf = (unsigned char *)sei_bp_bs.buffer;
481 for (i = 0; i < bp_byte_size; i++) {
482 avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
483 }
484 free(byte_buf);
485 /* write the SEI timing data */
486 avc_bitstream_put_ui(&nal_bs, 0x01, 8);
487 avc_bitstream_put_ui(&nal_bs, pic_byte_size, 8);
488
489 byte_buf = (unsigned char *)sei_pic_bs.buffer;
490 for (i = 0; i < pic_byte_size; i++) {
491 avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
492 }
493 free(byte_buf);
494
495 avc_rbsp_trailing_bits(&nal_bs);
496 avc_bitstream_end(&nal_bs);
497
498 *sei_buffer = (unsigned char *)nal_bs.buffer;
499
500 return nal_bs.bit_offset;
501 }
502
503 int
build_mpeg2_slice_header(VAEncSequenceParameterBufferMPEG2 * sps_param,VAEncPictureParameterBufferMPEG2 * pic_param,VAEncSliceParameterBufferMPEG2 * slice_param,unsigned char ** slice_header_buffer)504 build_mpeg2_slice_header(VAEncSequenceParameterBufferMPEG2 *sps_param,
505 VAEncPictureParameterBufferMPEG2 *pic_param,
506 VAEncSliceParameterBufferMPEG2 *slice_param,
507 unsigned char **slice_header_buffer)
508 {
509 avc_bitstream bs;
510
511 avc_bitstream_start(&bs);
512 avc_bitstream_end(&bs);
513 *slice_header_buffer = (unsigned char *)bs.buffer;
514
515 return bs.bit_offset;
516 }
517
binarize_qindex_delta(avc_bitstream * bs,int qindex_delta)518 static void binarize_qindex_delta(avc_bitstream *bs, int qindex_delta)
519 {
520 if (qindex_delta == 0)
521 avc_bitstream_put_ui(bs, 0, 1);
522 else {
523 avc_bitstream_put_ui(bs, 1, 1);
524 avc_bitstream_put_ui(bs, abs(qindex_delta), 4);
525
526 if (qindex_delta < 0)
527 avc_bitstream_put_ui(bs, 1, 1);
528 else
529 avc_bitstream_put_ui(bs, 0, 1);
530 }
531 }
532
binarize_vp8_frame_header(VAEncSequenceParameterBufferVP8 * seq_param,VAEncPictureParameterBufferVP8 * pic_param,VAQMatrixBufferVP8 * q_matrix,struct gen6_mfc_context * mfc_context,struct intel_encoder_context * encoder_context)533 void binarize_vp8_frame_header(VAEncSequenceParameterBufferVP8 *seq_param,
534 VAEncPictureParameterBufferVP8 *pic_param,
535 VAQMatrixBufferVP8 *q_matrix,
536 struct gen6_mfc_context *mfc_context,
537 struct intel_encoder_context *encoder_context)
538 {
539 avc_bitstream bs;
540 int i, j;
541 int is_intra_frame = !pic_param->pic_flags.bits.frame_type;
542 int log2num = pic_param->pic_flags.bits.num_token_partitions;
543
544 /* modify picture paramters */
545 pic_param->pic_flags.bits.loop_filter_adj_enable = 1;
546 pic_param->pic_flags.bits.mb_no_coeff_skip = 1;
547 pic_param->pic_flags.bits.forced_lf_adjustment = 1;
548 pic_param->pic_flags.bits.refresh_entropy_probs = 1;
549 pic_param->pic_flags.bits.segmentation_enabled = 0;
550
551 pic_param->pic_flags.bits.loop_filter_type = pic_param->pic_flags.bits.version / 2;
552 if (pic_param->pic_flags.bits.version > 1)
553 pic_param->loop_filter_level[0] = 0;
554
555 avc_bitstream_start(&bs);
556
557 if (is_intra_frame) {
558 avc_bitstream_put_ui(&bs, 0, 1);
559 avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.clamping_type , 1);
560 }
561
562 avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.segmentation_enabled, 1);
563
564 if (pic_param->pic_flags.bits.segmentation_enabled) {
565 avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.update_mb_segmentation_map, 1);
566 avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.update_segment_feature_data, 1);
567 if (pic_param->pic_flags.bits.update_segment_feature_data) {
568 /*add it later*/
569 assert(0);
570 }
571 if (pic_param->pic_flags.bits.update_mb_segmentation_map) {
572 for (i = 0; i < 3; i++) {
573 if (mfc_context->vp8_state.mb_segment_tree_probs[i] == 255)
574 avc_bitstream_put_ui(&bs, 0, 1);
575 else {
576 avc_bitstream_put_ui(&bs, 1, 1);
577 avc_bitstream_put_ui(&bs, mfc_context->vp8_state.mb_segment_tree_probs[i], 8);
578 }
579 }
580 }
581 }
582
583 avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.loop_filter_type, 1);
584 avc_bitstream_put_ui(&bs, pic_param->loop_filter_level[0], 6);
585 avc_bitstream_put_ui(&bs, pic_param->sharpness_level, 3);
586
587 mfc_context->vp8_state.frame_header_lf_update_pos = bs.bit_offset;
588
589 if (pic_param->pic_flags.bits.forced_lf_adjustment) {
590 avc_bitstream_put_ui(&bs, 1, 1);//mode_ref_lf_delta_enable = 1
591 avc_bitstream_put_ui(&bs, 1, 1);//mode_ref_lf_delta_update = 1
592
593 for (i = 0; i < 4; i++) {
594 avc_bitstream_put_ui(&bs, 1, 1);
595 if (pic_param->ref_lf_delta[i] > 0) {
596 avc_bitstream_put_ui(&bs, (abs(pic_param->ref_lf_delta[i]) & 0x3F), 6);
597 avc_bitstream_put_ui(&bs, 0, 1);
598 } else {
599 avc_bitstream_put_ui(&bs, (abs(pic_param->ref_lf_delta[i]) & 0x3F), 6);
600 avc_bitstream_put_ui(&bs, 1, 1);
601 }
602 }
603
604 for (i = 0; i < 4; i++) {
605 avc_bitstream_put_ui(&bs, 1, 1);
606 if (pic_param->mode_lf_delta[i] > 0) {
607 avc_bitstream_put_ui(&bs, (abs(pic_param->mode_lf_delta[i]) & 0x3F), 6);
608 avc_bitstream_put_ui(&bs, 0, 1);
609 } else {
610 avc_bitstream_put_ui(&bs, (abs(pic_param->mode_lf_delta[i]) & 0x3F), 6);
611 avc_bitstream_put_ui(&bs, 1, 1);
612 }
613 }
614
615 } else {
616 avc_bitstream_put_ui(&bs, 0, 1);//mode_ref_lf_delta_enable = 0
617 }
618
619 avc_bitstream_put_ui(&bs, log2num, 2);
620
621 mfc_context->vp8_state.frame_header_qindex_update_pos = bs.bit_offset;
622
623 avc_bitstream_put_ui(&bs, q_matrix->quantization_index[0], 7);
624
625 for (i = 0; i < 5; i++)
626 binarize_qindex_delta(&bs, q_matrix->quantization_index_delta[i]);
627
628 if (!is_intra_frame) {
629 avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.refresh_golden_frame, 1);
630 avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.refresh_alternate_frame, 1);
631
632 if (!pic_param->pic_flags.bits.refresh_golden_frame)
633 avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.copy_buffer_to_golden, 2);
634
635 if (!pic_param->pic_flags.bits.refresh_alternate_frame)
636 avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.copy_buffer_to_alternate, 2);
637
638 avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.sign_bias_golden, 1);
639 avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.sign_bias_alternate, 1);
640 }
641
642 avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.refresh_entropy_probs, 1);
643
644 if (!is_intra_frame)
645 avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.refresh_last, 1);
646
647 mfc_context->vp8_state.frame_header_token_update_pos = bs.bit_offset;
648
649 for (i = 0; i < 4 * 8 * 3 * 11; i++)
650 avc_bitstream_put_ui(&bs, 0, 1); //don't update coeff_probs
651
652 avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.mb_no_coeff_skip, 1);
653 if (pic_param->pic_flags.bits.mb_no_coeff_skip)
654 avc_bitstream_put_ui(&bs, mfc_context->vp8_state.prob_skip_false, 8);
655
656 if (!is_intra_frame) {
657 avc_bitstream_put_ui(&bs, mfc_context->vp8_state.prob_intra, 8);
658 avc_bitstream_put_ui(&bs, mfc_context->vp8_state.prob_last, 8);
659 avc_bitstream_put_ui(&bs, mfc_context->vp8_state.prob_gf, 8);
660
661 avc_bitstream_put_ui(&bs, 1, 1); //y_mode_update_flag = 1
662 for (i = 0; i < 4; i++) {
663 avc_bitstream_put_ui(&bs, mfc_context->vp8_state.y_mode_probs[i], 8);
664 }
665
666 avc_bitstream_put_ui(&bs, 1, 1); //uv_mode_update_flag = 1
667 for (i = 0; i < 3; i++) {
668 avc_bitstream_put_ui(&bs, mfc_context->vp8_state.uv_mode_probs[i], 8);
669 }
670
671 mfc_context->vp8_state.frame_header_bin_mv_upate_pos = bs.bit_offset;
672
673 for (i = 0; i < 2 ; i++) {
674 for (j = 0; j < 19; j++) {
675 avc_bitstream_put_ui(&bs, 0, 1);
676 //avc_bitstream_put_ui(&bs, mfc_context->vp8_state.mv_probs[i][j], 7);
677 }
678 }
679 }
680
681 avc_bitstream_end(&bs);
682
683 mfc_context->vp8_state.vp8_frame_header = (unsigned char *)bs.buffer;
684 mfc_context->vp8_state.frame_header_bit_count = bs.bit_offset;
685 }
686
687 /* HEVC to do for internal header generated*/
688
nal_header_hevc(avc_bitstream * bs,int nal_unit_type,int temporalid)689 void nal_header_hevc(avc_bitstream *bs, int nal_unit_type, int temporalid)
690 {
691 /* forbidden_zero_bit: 0 */
692 avc_bitstream_put_ui(bs, 0, 1);
693 /* nal unit_type */
694 avc_bitstream_put_ui(bs, nal_unit_type, 6);
695 /* layer_id. currently it is zero */
696 avc_bitstream_put_ui(bs, 0, 6);
697 /* teporalid + 1 .*/
698 avc_bitstream_put_ui(bs, temporalid + 1, 3);
699 }
700
build_hevc_sei_buffering_period(int init_cpb_removal_delay_length,unsigned int init_cpb_removal_delay,unsigned int init_cpb_removal_delay_offset,unsigned char ** sei_buffer)701 int build_hevc_sei_buffering_period(int init_cpb_removal_delay_length,
702 unsigned int init_cpb_removal_delay,
703 unsigned int init_cpb_removal_delay_offset,
704 unsigned char **sei_buffer)
705 {
706 unsigned char *byte_buf;
707 int bp_byte_size, i;
708 //unsigned int cpb_removal_delay;
709
710 avc_bitstream nal_bs;
711 avc_bitstream sei_bp_bs;
712
713 avc_bitstream_start(&sei_bp_bs);
714 avc_bitstream_put_ue(&sei_bp_bs, 0); /*seq_parameter_set_id*/
715 /* SEI buffer period info */
716 /* NALHrdBpPresentFlag == 1 */
717 avc_bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay, init_cpb_removal_delay_length);
718 avc_bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay_offset, init_cpb_removal_delay_length);
719 if (sei_bp_bs.bit_offset & 0x7) {
720 avc_bitstream_put_ui(&sei_bp_bs, 1, 1);
721 }
722 avc_bitstream_end(&sei_bp_bs);
723 bp_byte_size = (sei_bp_bs.bit_offset + 7) / 8;
724
725 avc_bitstream_start(&nal_bs);
726 nal_start_code_prefix(&nal_bs);
727 nal_header_hevc(&nal_bs, PREFIX_SEI_NUT , 0);
728
729 /* Write the SEI buffer period data */
730 avc_bitstream_put_ui(&nal_bs, 0, 8);
731 avc_bitstream_put_ui(&nal_bs, bp_byte_size, 8);
732
733 byte_buf = (unsigned char *)sei_bp_bs.buffer;
734 for (i = 0; i < bp_byte_size; i++) {
735 avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
736 }
737 free(byte_buf);
738
739 avc_rbsp_trailing_bits(&nal_bs);
740 avc_bitstream_end(&nal_bs);
741
742 *sei_buffer = (unsigned char *)nal_bs.buffer;
743
744 return nal_bs.bit_offset;
745 }
746
build_hevc_idr_sei_buffer_timing(unsigned int init_cpb_removal_delay_length,unsigned int init_cpb_removal_delay,unsigned int init_cpb_removal_delay_offset,unsigned int cpb_removal_length,unsigned int cpb_removal_delay,unsigned int dpb_output_length,unsigned int dpb_output_delay,unsigned char ** sei_buffer)747 int build_hevc_idr_sei_buffer_timing(unsigned int init_cpb_removal_delay_length,
748 unsigned int init_cpb_removal_delay,
749 unsigned int init_cpb_removal_delay_offset,
750 unsigned int cpb_removal_length,
751 unsigned int cpb_removal_delay,
752 unsigned int dpb_output_length,
753 unsigned int dpb_output_delay,
754 unsigned char **sei_buffer)
755 {
756 unsigned char *byte_buf;
757 int bp_byte_size, i, pic_byte_size;
758 //unsigned int cpb_removal_delay;
759
760 avc_bitstream nal_bs;
761 avc_bitstream sei_bp_bs, sei_pic_bs;
762
763 avc_bitstream_start(&sei_bp_bs);
764 avc_bitstream_put_ue(&sei_bp_bs, 0); /*seq_parameter_set_id*/
765 /* SEI buffer period info */
766 /* NALHrdBpPresentFlag == 1 */
767 avc_bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay, init_cpb_removal_delay_length);
768 avc_bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay_offset, init_cpb_removal_delay_length);
769 if (sei_bp_bs.bit_offset & 0x7) {
770 avc_bitstream_put_ui(&sei_bp_bs, 1, 1);
771 }
772 avc_bitstream_end(&sei_bp_bs);
773 bp_byte_size = (sei_bp_bs.bit_offset + 7) / 8;
774
775 /* SEI pic timing info */
776 avc_bitstream_start(&sei_pic_bs);
777 /* The info of CPB and DPB delay is controlled by CpbDpbDelaysPresentFlag,
778 * which is derived as 1 if one of the following conditions is true:
779 * nal_hrd_parameters_present_flag is present in the avc_bitstream and is equal to 1,
780 * vcl_hrd_parameters_present_flag is present in the avc_bitstream and is equal to 1,
781 */
782 //cpb_removal_delay = (hevc_context.current_cpb_removal - hevc_context.prev_idr_cpb_removal);
783 avc_bitstream_put_ui(&sei_pic_bs, cpb_removal_delay, cpb_removal_length);
784 avc_bitstream_put_ui(&sei_pic_bs, dpb_output_delay, dpb_output_length);
785 if (sei_pic_bs.bit_offset & 0x7) {
786 avc_bitstream_put_ui(&sei_pic_bs, 1, 1);
787 }
788 /* The pic_structure_present_flag determines whether the pic_structure
789 * info is written into the SEI pic timing info.
790 * Currently it is set to zero.
791 */
792 avc_bitstream_end(&sei_pic_bs);
793 pic_byte_size = (sei_pic_bs.bit_offset + 7) / 8;
794
795 avc_bitstream_start(&nal_bs);
796 nal_start_code_prefix(&nal_bs);
797 nal_header_hevc(&nal_bs, PREFIX_SEI_NUT , 0);
798
799 /* Write the SEI buffer period data */
800 avc_bitstream_put_ui(&nal_bs, 0, 8);
801 avc_bitstream_put_ui(&nal_bs, bp_byte_size, 8);
802
803 byte_buf = (unsigned char *)sei_bp_bs.buffer;
804 for (i = 0; i < bp_byte_size; i++) {
805 avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
806 }
807 free(byte_buf);
808 /* write the SEI pic timing data */
809 avc_bitstream_put_ui(&nal_bs, 0x01, 8);
810 avc_bitstream_put_ui(&nal_bs, pic_byte_size, 8);
811
812 byte_buf = (unsigned char *)sei_pic_bs.buffer;
813 for (i = 0; i < pic_byte_size; i++) {
814 avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
815 }
816 free(byte_buf);
817
818 avc_rbsp_trailing_bits(&nal_bs);
819 avc_bitstream_end(&nal_bs);
820
821 *sei_buffer = (unsigned char *)nal_bs.buffer;
822
823 return nal_bs.bit_offset;
824 }
825
build_hevc_sei_pic_timing(unsigned int cpb_removal_length,unsigned int cpb_removal_delay,unsigned int dpb_output_length,unsigned int dpb_output_delay,unsigned char ** sei_buffer)826 int build_hevc_sei_pic_timing(unsigned int cpb_removal_length, unsigned int cpb_removal_delay,
827 unsigned int dpb_output_length, unsigned int dpb_output_delay,
828 unsigned char **sei_buffer)
829 {
830 unsigned char *byte_buf;
831 int i, pic_byte_size;
832 //unsigned int cpb_removal_delay;
833
834 avc_bitstream nal_bs;
835 avc_bitstream sei_pic_bs;
836
837 avc_bitstream_start(&sei_pic_bs);
838 /* The info of CPB and DPB delay is controlled by CpbDpbDelaysPresentFlag,
839 * which is derived as 1 if one of the following conditions is true:
840 * nal_hrd_parameters_present_flag is present in the avc_bitstream and is equal to 1,
841 * vcl_hrd_parameters_present_flag is present in the avc_bitstream and is equal to 1,
842 */
843 //cpb_removal_delay = (hevc_context.current_cpb_removal - hevc_context.current_idr_cpb_removal);
844 avc_bitstream_put_ui(&sei_pic_bs, cpb_removal_delay, cpb_removal_length);
845 avc_bitstream_put_ui(&sei_pic_bs, dpb_output_delay, dpb_output_length);
846 if (sei_pic_bs.bit_offset & 0x7) {
847 avc_bitstream_put_ui(&sei_pic_bs, 1, 1);
848 }
849
850 /* The pic_structure_present_flag determines whether the pic_structure
851 * info is written into the SEI pic timing info.
852 * Currently it is set to zero.
853 */
854 avc_bitstream_end(&sei_pic_bs);
855 pic_byte_size = (sei_pic_bs.bit_offset + 7) / 8;
856
857 avc_bitstream_start(&nal_bs);
858 nal_start_code_prefix(&nal_bs);
859 nal_header_hevc(&nal_bs, PREFIX_SEI_NUT , 0);
860
861 /* write the SEI Pic timing data */
862 avc_bitstream_put_ui(&nal_bs, 0x01, 8);
863 avc_bitstream_put_ui(&nal_bs, pic_byte_size, 8);
864
865 byte_buf = (unsigned char *)sei_pic_bs.buffer;
866 for (i = 0; i < pic_byte_size; i++) {
867 avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
868 }
869 free(byte_buf);
870
871 avc_rbsp_trailing_bits(&nal_bs);
872 avc_bitstream_end(&nal_bs);
873
874 *sei_buffer = (unsigned char *)nal_bs.buffer;
875
876 return nal_bs.bit_offset;
877 }
878
879 typedef struct _RefPicSet {
880 unsigned char num_negative_pics;
881 unsigned char num_positive_pics;
882 unsigned char delta_poc_s0_minus1[8];
883 unsigned char used_by_curr_pic_s0_flag[8];
884 unsigned char delta_poc_s1_minus1[8];
885 unsigned char used_by_curr_pic_s1_flag[8];
886 unsigned int inter_ref_pic_set_prediction_flag;
887 } hevcRefPicSet;
888
hevc_short_term_ref_pic_set(avc_bitstream * bs,VAEncSliceParameterBufferHEVC * slice_param,int curPicOrderCnt)889 void hevc_short_term_ref_pic_set(avc_bitstream *bs, VAEncSliceParameterBufferHEVC *slice_param, int curPicOrderCnt)
890 {
891 hevcRefPicSet hevc_rps;
892 int rps_idx = 1, ref_idx = 0;
893 int i = 0;
894
895 hevc_rps.inter_ref_pic_set_prediction_flag = 0;
896 /* s0: between I and P/B; s1 : between P and B */
897 hevc_rps.num_negative_pics = (slice_param->slice_type != HEVC_SLICE_I) ? 1 : 0;
898 hevc_rps.num_positive_pics = (slice_param->slice_type == HEVC_SLICE_B) ? 1 : 0;
899 hevc_rps.delta_poc_s0_minus1[0] = 0;
900 hevc_rps.used_by_curr_pic_s0_flag[0] = 0;
901 hevc_rps.delta_poc_s1_minus1[0] = 0;
902 hevc_rps.used_by_curr_pic_s1_flag[0] = 0;
903 if (slice_param->num_ref_idx_l0_active_minus1 == 0) {
904 hevc_rps.delta_poc_s0_minus1[0] = (slice_param->slice_type == HEVC_SLICE_I) ? 0 : (curPicOrderCnt - slice_param->ref_pic_list0[0].pic_order_cnt - 1); //0;
905 hevc_rps.used_by_curr_pic_s0_flag[0] = 1;
906 }
907 if (slice_param->num_ref_idx_l1_active_minus1 == 0) {
908 hevc_rps.delta_poc_s1_minus1[0] = (slice_param->slice_type == HEVC_SLICE_I) ? 0 : (slice_param->ref_pic_list1[0].pic_order_cnt - curPicOrderCnt - 1);
909 hevc_rps.used_by_curr_pic_s1_flag[0] = 1;
910 }
911
912 if (rps_idx)
913 avc_bitstream_put_ui(bs, hevc_rps.inter_ref_pic_set_prediction_flag, 1);
914
915 if (hevc_rps.inter_ref_pic_set_prediction_flag) {
916 /* not support */
917 /* to do */
918 } else {
919 avc_bitstream_put_ue(bs, hevc_rps.num_negative_pics);
920 avc_bitstream_put_ue(bs, hevc_rps.num_positive_pics);
921
922 for (i = 0; i < hevc_rps.num_negative_pics; i++) {
923 avc_bitstream_put_ue(bs, hevc_rps.delta_poc_s0_minus1[ref_idx]);
924 avc_bitstream_put_ui(bs, hevc_rps.used_by_curr_pic_s0_flag[ref_idx], 1);
925 }
926 for (i = 0; i < hevc_rps.num_positive_pics; i++) {
927 avc_bitstream_put_ue(bs, hevc_rps.delta_poc_s1_minus1[ref_idx]);
928 avc_bitstream_put_ui(bs, hevc_rps.used_by_curr_pic_s1_flag[ref_idx], 1);
929 }
930 }
931
932 return;
933 }
934
slice_rbsp(avc_bitstream * bs,int slice_index,VAEncSequenceParameterBufferHEVC * seq_param,VAEncPictureParameterBufferHEVC * pic_param,VAEncSliceParameterBufferHEVC * slice_param)935 static void slice_rbsp(avc_bitstream *bs,
936 int slice_index,
937 VAEncSequenceParameterBufferHEVC *seq_param,
938 VAEncPictureParameterBufferHEVC *pic_param,
939 VAEncSliceParameterBufferHEVC *slice_param)
940 {
941 int log2_cu_size = seq_param->log2_min_luma_coding_block_size_minus3 + 3;
942 int log2_ctb_size = seq_param->log2_diff_max_min_luma_coding_block_size + log2_cu_size;
943 int ctb_size = 1 << log2_ctb_size;
944
945 int picture_width_in_ctb = (seq_param->pic_width_in_luma_samples + ctb_size - 1) / ctb_size;
946 int picture_height_in_ctb = (seq_param->pic_height_in_luma_samples + ctb_size - 1) / ctb_size;
947
948 /* first_slice_segment_in_pic_flag */
949 if (slice_index == 0) {
950 avc_bitstream_put_ui(bs, 1, 1);
951 } else {
952 avc_bitstream_put_ui(bs, 0, 1);
953 }
954
955 /* no_output_of_prior_pics_flag */
956 if (pic_param->pic_fields.bits.idr_pic_flag)
957 avc_bitstream_put_ui(bs, 1, 1);
958
959 /* slice_pic_parameter_set_id */
960 avc_bitstream_put_ue(bs, 0);
961
962 /* not the first slice */
963 if (slice_index) {
964 /* TBD */
965 int bit_size;
966
967 float num_ctus;
968
969 num_ctus = picture_width_in_ctb * picture_height_in_ctb;
970 bit_size = ceilf(log2f(num_ctus));
971
972 if (pic_param->pic_fields.bits.dependent_slice_segments_enabled_flag) {
973 avc_bitstream_put_ui(bs,
974 slice_param->slice_fields.bits.dependent_slice_segment_flag, 1);
975 }
976 /* slice_segment_address is based on Ceil(log2(PictureSizeinCtbs)) */
977 avc_bitstream_put_ui(bs, slice_param->slice_segment_address, bit_size);
978 }
979 if (!slice_param->slice_fields.bits.dependent_slice_segment_flag) {
980 /* slice_reserved_flag */
981
982 /* slice_type */
983 avc_bitstream_put_ue(bs, slice_param->slice_type);
984 /* use the inferred the value of pic_output_flag */
985
986 /* colour_plane_id */
987 if (seq_param->seq_fields.bits.separate_colour_plane_flag) {
988 avc_bitstream_put_ui(bs, slice_param->slice_fields.bits.colour_plane_id, 1);
989 }
990
991 if (!pic_param->pic_fields.bits.idr_pic_flag) {
992 int Log2MaxPicOrderCntLsb = 8;
993 avc_bitstream_put_ui(bs, pic_param->decoded_curr_pic.pic_order_cnt, Log2MaxPicOrderCntLsb);
994
995 //if (!slice_param->short_term_ref_pic_set_sps_flag)
996 {
997 /* short_term_ref_pic_set_sps_flag.
998 * Use zero and then pass the RPS from slice_header
999 */
1000 avc_bitstream_put_ui(bs, 0, 1);
1001 /* TBD
1002 * Add the short_term reference picture set
1003 */
1004 hevc_short_term_ref_pic_set(bs, slice_param, pic_param->decoded_curr_pic.pic_order_cnt);
1005 }
1006 /* long term reference present flag. unpresent */
1007 /* TBD */
1008
1009 /* sps temporal MVP*/
1010 if (seq_param->seq_fields.bits.sps_temporal_mvp_enabled_flag) {
1011 avc_bitstream_put_ui(bs,
1012 slice_param->slice_fields.bits.slice_temporal_mvp_enabled_flag, 1);
1013 }
1014 }
1015
1016 /* long term reference present flag. unpresent */
1017
1018 /* sample adaptive offset enabled flag */
1019 if (seq_param->seq_fields.bits.sample_adaptive_offset_enabled_flag) {
1020 avc_bitstream_put_ui(bs, slice_param->slice_fields.bits.slice_sao_luma_flag, 1);
1021 avc_bitstream_put_ui(bs, slice_param->slice_fields.bits.slice_sao_chroma_flag, 1);
1022 }
1023
1024 if (slice_param->slice_type != HEVC_SLICE_I) {
1025 /* num_ref_idx_active_override_flag. 0 */
1026 avc_bitstream_put_ui(bs, 0, 1);
1027 /* lists_modification_flag is unpresent NumPocTotalCurr > 1 ,here it is 1*/
1028
1029 /* No reference picture set modification */
1030
1031 /* MVD_l1_zero_flag */
1032 if (slice_param->slice_type == HEVC_SLICE_B)
1033 avc_bitstream_put_ui(bs, slice_param->slice_fields.bits.mvd_l1_zero_flag, 1);
1034
1035 /* cabac_init_present_flag. 0 */
1036
1037 /* slice_temporal_mvp_enabled_flag. */
1038 if (slice_param->slice_fields.bits.slice_temporal_mvp_enabled_flag) {
1039 if (slice_param->slice_type == HEVC_SLICE_B)
1040 avc_bitstream_put_ui(bs, slice_param->slice_fields.bits.collocated_from_l0_flag, 1);
1041 /*
1042 * TBD: Add the collocated_ref_idx.
1043 */
1044 }
1045 if (((pic_param->pic_fields.bits.weighted_pred_flag) &&
1046 (slice_param->slice_type == HEVC_SLICE_P)) ||
1047 ((pic_param->pic_fields.bits.weighted_bipred_flag) &&
1048 (slice_param->slice_type == HEVC_SLICE_B))) {
1049 /* TBD:
1050 * add the weighted table
1051 */
1052 }
1053 avc_bitstream_put_ue(bs, 5 - slice_param->max_num_merge_cand);
1054 }
1055 /* slice_qp_delta */
1056 avc_bitstream_put_ue(bs, slice_param->slice_qp_delta);
1057
1058 /* slice_cb/cr_qp_offset is controlled by pps_slice_chroma_qp_offsets_present_flag
1059 * The present flag is set to 1.
1060 */
1061 avc_bitstream_put_ue(bs, slice_param->slice_cb_qp_offset);
1062 avc_bitstream_put_ue(bs, slice_param->slice_cr_qp_offset);
1063
1064 /*
1065 * deblocking_filter_override_flag is controlled by
1066 * deblocking_filter_override_enabled_flag.
1067 * The override_enabled_flag is zero.
1068 * deblocking_filter_override_flag is zero. then
1069 * slice_deblocking_filter_disabled_flag is also zero
1070 * (It is inferred to be equal to pps_deblocking_filter_disabled_flag.
1071 */
1072
1073 /* slice_loop_filter_across_slices_enabled_flag is controlled
1074 * by pps_loop_filter_across_slices_enabled_flag &&
1075 * (slice_sao_luma_flag | | slice_sao_chroma_flag | |
1076 * !slice_deblocking_filter_disabled_flag ))
1077 *
1078 */
1079 }
1080
1081 if (pic_param->pic_fields.bits.tiles_enabled_flag ||
1082 pic_param->pic_fields.bits.entropy_coding_sync_enabled_flag) {
1083 /* TBD.
1084 * Add the Entry-points && tile definition.
1085 */
1086 }
1087
1088 /* slice_segment_header_extension_present_flag. Not present */
1089
1090 /* byte_alignment */
1091 avc_rbsp_trailing_bits(bs);
1092 }
1093
get_hevc_slice_nalu_type(VAEncPictureParameterBufferHEVC * pic_param)1094 int get_hevc_slice_nalu_type(VAEncPictureParameterBufferHEVC *pic_param)
1095 {
1096 if (pic_param->pic_fields.bits.idr_pic_flag)
1097 return IDR_WRADL_NUT;
1098 else if (pic_param->pic_fields.bits.reference_pic_flag)
1099 return SLICE_TRAIL_R_NUT;
1100 else
1101 return SLICE_TRAIL_N_NUT;
1102 }
1103
build_hevc_slice_header(VAEncSequenceParameterBufferHEVC * seq_param,VAEncPictureParameterBufferHEVC * pic_param,VAEncSliceParameterBufferHEVC * slice_param,unsigned char ** header_buffer,int slice_index)1104 int build_hevc_slice_header(VAEncSequenceParameterBufferHEVC *seq_param,
1105 VAEncPictureParameterBufferHEVC *pic_param,
1106 VAEncSliceParameterBufferHEVC *slice_param,
1107 unsigned char **header_buffer,
1108 int slice_index)
1109 {
1110 avc_bitstream bs;
1111
1112 avc_bitstream_start(&bs);
1113 nal_start_code_prefix(&bs);
1114 nal_header_hevc(&bs, get_hevc_slice_nalu_type(pic_param), 0);
1115 slice_rbsp(&bs, slice_index, seq_param, pic_param, slice_param);
1116 avc_bitstream_end(&bs);
1117
1118 *header_buffer = (unsigned char *)bs.buffer;
1119 return bs.bit_offset;
1120 }
1121
1122 int
intel_avc_find_skipemulcnt(unsigned char * buf,int bits_length)1123 intel_avc_find_skipemulcnt(unsigned char *buf, int bits_length)
1124 {
1125 int i, found;
1126 int leading_zero_cnt, byte_length, zero_byte;
1127 int nal_unit_type;
1128 int skip_cnt = 0;
1129
1130 #define NAL_UNIT_TYPE_MASK 0x1f
1131 #define HW_MAX_SKIP_LENGTH 15
1132
1133 byte_length = ALIGN(bits_length, 32) >> 3;
1134
1135
1136 leading_zero_cnt = 0;
1137 found = 0;
1138 for (i = 0; i < byte_length - 4; i++) {
1139 if (((buf[i] == 0) && (buf[i + 1] == 0) && (buf[i + 2] == 1)) ||
1140 ((buf[i] == 0) && (buf[i + 1] == 0) && (buf[i + 2] == 0) && (buf[i + 3] == 1))) {
1141 found = 1;
1142 break;
1143 }
1144 leading_zero_cnt++;
1145 }
1146 if (!found) {
1147 /* warning message is complained. But anyway it will be inserted. */
1148 WARN_ONCE("Invalid packed header data. "
1149 "Can't find the 000001 start_prefix code\n");
1150 return 0;
1151 }
1152 i = leading_zero_cnt;
1153
1154 zero_byte = 0;
1155 if (!((buf[i] == 0) && (buf[i + 1] == 0) && (buf[i + 2] == 1)))
1156 zero_byte = 1;
1157
1158 skip_cnt = leading_zero_cnt + zero_byte + 3;
1159
1160 /* the unit header byte is accounted */
1161 nal_unit_type = (buf[skip_cnt]) & NAL_UNIT_TYPE_MASK;
1162 skip_cnt += 1;
1163
1164 if (nal_unit_type == 14 || nal_unit_type == 20 || nal_unit_type == 21) {
1165 /* more unit header bytes are accounted for MVC/SVC */
1166 skip_cnt += 3;
1167 }
1168 if (skip_cnt > HW_MAX_SKIP_LENGTH) {
1169 WARN_ONCE("Too many leading zeros are padded for packed data. "
1170 "It is beyond the HW range.!!!\n");
1171 }
1172 return skip_cnt;
1173 }
1174