2 #include "h264encode.h"
4 #include <EGL/eglplatform.h>
9 #include <libavcodec/avcodec.h>
10 #include <libavformat/avio.h>
11 #include <libavutil/mathematics.h>
12 #include <libavutil/rational.h>
13 #include <libdrm/drm_fourcc.h>
19 #include <va/va_drmcommon.h>
20 #include <va/va_enc_h264.h>
21 #include <va/va_x11.h>
22 #include <condition_variable>
33 #define CHECK_VASTATUS(va_status, func) \
34 if (va_status != VA_STATUS_SUCCESS) { \
35 fprintf(stderr, "%s:%d (%s) failed with %d\n", __func__, __LINE__, func, va_status); \
39 //#include "loadsurface.h"
41 #define NAL_REF_IDC_NONE 0
42 #define NAL_REF_IDC_LOW 1
43 #define NAL_REF_IDC_MEDIUM 2
44 #define NAL_REF_IDC_HIGH 3
52 #define SLICE_TYPE_P 0
53 #define SLICE_TYPE_B 1
54 #define SLICE_TYPE_I 2
55 #define IS_P_SLICE(type) (SLICE_TYPE_P == (type))
56 #define IS_B_SLICE(type) (SLICE_TYPE_B == (type))
57 #define IS_I_SLICE(type) (SLICE_TYPE_I == (type))
60 #define ENTROPY_MODE_CAVLC 0
61 #define ENTROPY_MODE_CABAC 1
63 #define PROFILE_IDC_BASELINE 66
64 #define PROFILE_IDC_MAIN 77
65 #define PROFILE_IDC_HIGH 100
67 #define BITSTREAM_ALLOCATE_STEPPING 4096
69 #define SURFACE_NUM 16 /* 16 surfaces for source YUV */
70 static VADisplay va_dpy;
71 static VAProfile h264_profile = (VAProfile)~0;
72 static VAConfigAttrib config_attrib[VAConfigAttribTypeMax];
73 static int config_attrib_num = 0, enc_packed_header_idx;
76 VASurfaceID src_surface, ref_surface;
79 VAImage surface_image;
80 GLuint y_tex, cbcr_tex;
81 EGLImage y_egl_image, cbcr_egl_image;
83 GLSurface gl_surfaces[SURFACE_NUM];
85 static VAConfigID config_id;
86 static VAContextID context_id;
87 static VAEncSequenceParameterBufferH264 seq_param;
88 static VAEncPictureParameterBufferH264 pic_param;
89 static VAEncSliceParameterBufferH264 slice_param;
90 static VAPictureH264 CurrentCurrPic;
91 static VAPictureH264 ReferenceFrames[16], RefPicList0_P[32], RefPicList0_B[32], RefPicList1_B[32];
93 static unsigned int MaxFrameNum = (2<<16);
94 static unsigned int MaxPicOrderCntLsb = (2<<8);
95 static unsigned int Log2MaxFrameNum = 16;
96 static unsigned int Log2MaxPicOrderCntLsb = 8;
98 static unsigned int num_ref_frames = 2;
99 static unsigned int numShortTerm = 0;
100 static int constraint_set_flag = 0;
101 static int h264_packedheader = 0; /* support pack header? */
102 static int h264_maxref = (1<<16|1);
103 static int h264_entropy_mode = 1; /* cabac */
105 static char *coded_fn = NULL;
107 static int frame_width = 176;
108 static int frame_height = 144;
109 static int frame_width_mbaligned;
110 static int frame_height_mbaligned;
111 static int frame_rate = 60;
112 static unsigned int frame_bitrate = 0;
113 static unsigned int frame_slices = 1;
114 static double frame_size = 0;
115 static int initial_qp = 15;
116 //static int initial_qp = 28;
117 static int minimal_qp = 0;
118 static int intra_period = 30;
119 static int intra_idr_period = 60;
120 static int ip_period = 1;
121 static int rc_mode = -1;
122 static int rc_default_modes[] = {
125 VA_RC_VBR_CONSTRAINED,
130 static unsigned long long current_frame_encoding = 0;
131 static unsigned long long current_frame_display = 0;
132 static unsigned long long current_IDR_display = 0;
133 static unsigned int current_frame_num = 0;
134 static int current_frame_type;
136 static int misc_priv_type = 0;
137 static int misc_priv_value = 0;
139 /* thread to save coded data */
140 #define SRC_SURFACE_FREE 0
141 #define SRC_SURFACE_IN_ENCODING 1
144 unsigned int *buffer;
146 int max_size_in_dword;
148 typedef struct __bitstream bitstream;
153 va_swap32(unsigned int val)
155 unsigned char *pval = (unsigned char *)&val;
157 return ((pval[0] << 24) |
164 bitstream_start(bitstream *bs)
166 bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
167 bs->buffer = (unsigned int *)calloc(bs->max_size_in_dword * sizeof(int), 1);
172 bitstream_end(bitstream *bs)
174 int pos = (bs->bit_offset >> 5);
175 int bit_offset = (bs->bit_offset & 0x1f);
176 int bit_left = 32 - bit_offset;
179 bs->buffer[pos] = va_swap32((bs->buffer[pos] << bit_left));
184 bitstream_put_ui(bitstream *bs, unsigned int val, int size_in_bits)
186 int pos = (bs->bit_offset >> 5);
187 int bit_offset = (bs->bit_offset & 0x1f);
188 int bit_left = 32 - bit_offset;
193 bs->bit_offset += size_in_bits;
195 if (bit_left > size_in_bits) {
196 bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
198 size_in_bits -= bit_left;
199 bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
200 bs->buffer[pos] = va_swap32(bs->buffer[pos]);
202 if (pos + 1 == bs->max_size_in_dword) {
203 bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
204 bs->buffer = (unsigned int *)realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
207 bs->buffer[pos + 1] = val;
212 bitstream_put_ue(bitstream *bs, unsigned int val)
214 int size_in_bits = 0;
222 bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
223 bitstream_put_ui(bs, val, size_in_bits);
227 bitstream_put_se(bitstream *bs, int val)
229 unsigned int new_val;
234 new_val = 2 * val - 1;
236 bitstream_put_ue(bs, new_val);
240 bitstream_byte_aligning(bitstream *bs, int bit)
242 int bit_offset = (bs->bit_offset & 0x7);
243 int bit_left = 8 - bit_offset;
249 assert(bit == 0 || bit == 1);
252 new_val = (1 << bit_left) - 1;
256 bitstream_put_ui(bs, new_val, bit_left);
260 rbsp_trailing_bits(bitstream *bs)
262 bitstream_put_ui(bs, 1, 1);
263 bitstream_byte_aligning(bs, 0);
266 static void nal_start_code_prefix(bitstream *bs)
268 bitstream_put_ui(bs, 0x00000001, 32);
271 static void nal_header(bitstream *bs, int nal_ref_idc, int nal_unit_type)
273 bitstream_put_ui(bs, 0, 1); /* forbidden_zero_bit: 0 */
274 bitstream_put_ui(bs, nal_ref_idc, 2);
275 bitstream_put_ui(bs, nal_unit_type, 5);
278 static void sps_rbsp(bitstream *bs)
280 int profile_idc = PROFILE_IDC_BASELINE;
282 if (h264_profile == VAProfileH264High)
283 profile_idc = PROFILE_IDC_HIGH;
284 else if (h264_profile == VAProfileH264Main)
285 profile_idc = PROFILE_IDC_MAIN;
287 bitstream_put_ui(bs, profile_idc, 8); /* profile_idc */
288 bitstream_put_ui(bs, !!(constraint_set_flag & 1), 1); /* constraint_set0_flag */
289 bitstream_put_ui(bs, !!(constraint_set_flag & 2), 1); /* constraint_set1_flag */
290 bitstream_put_ui(bs, !!(constraint_set_flag & 4), 1); /* constraint_set2_flag */
291 bitstream_put_ui(bs, !!(constraint_set_flag & 8), 1); /* constraint_set3_flag */
292 bitstream_put_ui(bs, 0, 4); /* reserved_zero_4bits */
293 bitstream_put_ui(bs, seq_param.level_idc, 8); /* level_idc */
294 bitstream_put_ue(bs, seq_param.seq_parameter_set_id); /* seq_parameter_set_id */
296 if ( profile_idc == PROFILE_IDC_HIGH) {
297 bitstream_put_ue(bs, 1); /* chroma_format_idc = 1, 4:2:0 */
298 bitstream_put_ue(bs, 0); /* bit_depth_luma_minus8 */
299 bitstream_put_ue(bs, 0); /* bit_depth_chroma_minus8 */
300 bitstream_put_ui(bs, 0, 1); /* qpprime_y_zero_transform_bypass_flag */
301 bitstream_put_ui(bs, 0, 1); /* seq_scaling_matrix_present_flag */
304 bitstream_put_ue(bs, seq_param.seq_fields.bits.log2_max_frame_num_minus4); /* log2_max_frame_num_minus4 */
305 bitstream_put_ue(bs, seq_param.seq_fields.bits.pic_order_cnt_type); /* pic_order_cnt_type */
307 if (seq_param.seq_fields.bits.pic_order_cnt_type == 0)
308 bitstream_put_ue(bs, seq_param.seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4); /* log2_max_pic_order_cnt_lsb_minus4 */
313 bitstream_put_ue(bs, seq_param.max_num_ref_frames); /* num_ref_frames */
314 bitstream_put_ui(bs, 0, 1); /* gaps_in_frame_num_value_allowed_flag */
316 bitstream_put_ue(bs, seq_param.picture_width_in_mbs - 1); /* pic_width_in_mbs_minus1 */
317 bitstream_put_ue(bs, seq_param.picture_height_in_mbs - 1); /* pic_height_in_map_units_minus1 */
318 bitstream_put_ui(bs, seq_param.seq_fields.bits.frame_mbs_only_flag, 1); /* frame_mbs_only_flag */
320 if (!seq_param.seq_fields.bits.frame_mbs_only_flag) {
324 bitstream_put_ui(bs, seq_param.seq_fields.bits.direct_8x8_inference_flag, 1); /* direct_8x8_inference_flag */
325 bitstream_put_ui(bs, seq_param.frame_cropping_flag, 1); /* frame_cropping_flag */
327 if (seq_param.frame_cropping_flag) {
328 bitstream_put_ue(bs, seq_param.frame_crop_left_offset); /* frame_crop_left_offset */
329 bitstream_put_ue(bs, seq_param.frame_crop_right_offset); /* frame_crop_right_offset */
330 bitstream_put_ue(bs, seq_param.frame_crop_top_offset); /* frame_crop_top_offset */
331 bitstream_put_ue(bs, seq_param.frame_crop_bottom_offset); /* frame_crop_bottom_offset */
334 //if ( frame_bit_rate < 0 ) { //TODO EW: the vui header isn't correct
336 bitstream_put_ui(bs, 0, 1); /* vui_parameters_present_flag */
338 bitstream_put_ui(bs, 1, 1); /* vui_parameters_present_flag */
339 bitstream_put_ui(bs, 0, 1); /* aspect_ratio_info_present_flag */
340 bitstream_put_ui(bs, 0, 1); /* overscan_info_present_flag */
341 bitstream_put_ui(bs, 0, 1); /* video_signal_type_present_flag */
342 bitstream_put_ui(bs, 0, 1); /* chroma_loc_info_present_flag */
343 bitstream_put_ui(bs, 1, 1); /* timing_info_present_flag */
345 bitstream_put_ui(bs, 1, 32); // FPS
346 bitstream_put_ui(bs, frame_rate * 2, 32); // FPS
347 bitstream_put_ui(bs, 1, 1);
349 bitstream_put_ui(bs, 1, 1); /* nal_hrd_parameters_present_flag */
352 bitstream_put_ue(bs, 0); /* cpb_cnt_minus1 */
353 bitstream_put_ui(bs, 4, 4); /* bit_rate_scale */
354 bitstream_put_ui(bs, 6, 4); /* cpb_size_scale */
356 bitstream_put_ue(bs, frame_bitrate - 1); /* bit_rate_value_minus1[0] */
357 bitstream_put_ue(bs, frame_bitrate*8 - 1); /* cpb_size_value_minus1[0] */
358 bitstream_put_ui(bs, 1, 1); /* cbr_flag[0] */
360 bitstream_put_ui(bs, 23, 5); /* initial_cpb_removal_delay_length_minus1 */
361 bitstream_put_ui(bs, 23, 5); /* cpb_removal_delay_length_minus1 */
362 bitstream_put_ui(bs, 23, 5); /* dpb_output_delay_length_minus1 */
363 bitstream_put_ui(bs, 23, 5); /* time_offset_length */
365 bitstream_put_ui(bs, 0, 1); /* vcl_hrd_parameters_present_flag */
366 bitstream_put_ui(bs, 0, 1); /* low_delay_hrd_flag */
368 bitstream_put_ui(bs, 0, 1); /* pic_struct_present_flag */
369 bitstream_put_ui(bs, 0, 1); /* bitstream_restriction_flag */
372 rbsp_trailing_bits(bs); /* rbsp_trailing_bits */
376 static void pps_rbsp(bitstream *bs)
378 bitstream_put_ue(bs, pic_param.pic_parameter_set_id); /* pic_parameter_set_id */
379 bitstream_put_ue(bs, pic_param.seq_parameter_set_id); /* seq_parameter_set_id */
381 bitstream_put_ui(bs, pic_param.pic_fields.bits.entropy_coding_mode_flag, 1); /* entropy_coding_mode_flag */
383 bitstream_put_ui(bs, 0, 1); /* pic_order_present_flag: 0 */
385 bitstream_put_ue(bs, 0); /* num_slice_groups_minus1 */
387 bitstream_put_ue(bs, pic_param.num_ref_idx_l0_active_minus1); /* num_ref_idx_l0_active_minus1 */
388 bitstream_put_ue(bs, pic_param.num_ref_idx_l1_active_minus1); /* num_ref_idx_l1_active_minus1 1 */
390 bitstream_put_ui(bs, pic_param.pic_fields.bits.weighted_pred_flag, 1); /* weighted_pred_flag: 0 */
391 bitstream_put_ui(bs, pic_param.pic_fields.bits.weighted_bipred_idc, 2); /* weighted_bipred_idc: 0 */
393 bitstream_put_se(bs, pic_param.pic_init_qp - 26); /* pic_init_qp_minus26 */
394 bitstream_put_se(bs, 0); /* pic_init_qs_minus26 */
395 bitstream_put_se(bs, 0); /* chroma_qp_index_offset */
397 bitstream_put_ui(bs, pic_param.pic_fields.bits.deblocking_filter_control_present_flag, 1); /* deblocking_filter_control_present_flag */
398 bitstream_put_ui(bs, 0, 1); /* constrained_intra_pred_flag */
399 bitstream_put_ui(bs, 0, 1); /* redundant_pic_cnt_present_flag */
402 bitstream_put_ui(bs, pic_param.pic_fields.bits.transform_8x8_mode_flag, 1); /*transform_8x8_mode_flag */
403 bitstream_put_ui(bs, 0, 1); /* pic_scaling_matrix_present_flag */
404 bitstream_put_se(bs, pic_param.second_chroma_qp_index_offset ); /*second_chroma_qp_index_offset */
406 rbsp_trailing_bits(bs);
409 static void slice_header(bitstream *bs)
411 int first_mb_in_slice = slice_param.macroblock_address;
413 bitstream_put_ue(bs, first_mb_in_slice); /* first_mb_in_slice: 0 */
414 bitstream_put_ue(bs, slice_param.slice_type); /* slice_type */
415 bitstream_put_ue(bs, slice_param.pic_parameter_set_id); /* pic_parameter_set_id: 0 */
416 bitstream_put_ui(bs, pic_param.frame_num, seq_param.seq_fields.bits.log2_max_frame_num_minus4 + 4); /* frame_num */
418 /* frame_mbs_only_flag == 1 */
419 if (!seq_param.seq_fields.bits.frame_mbs_only_flag) {
424 if (pic_param.pic_fields.bits.idr_pic_flag)
425 bitstream_put_ue(bs, slice_param.idr_pic_id); /* idr_pic_id: 0 */
427 if (seq_param.seq_fields.bits.pic_order_cnt_type == 0) {
428 bitstream_put_ui(bs, pic_param.CurrPic.TopFieldOrderCnt, seq_param.seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 + 4);
429 /* pic_order_present_flag == 0 */
435 /* redundant_pic_cnt_present_flag == 0 */
437 if (IS_P_SLICE(slice_param.slice_type)) {
438 bitstream_put_ui(bs, slice_param.num_ref_idx_active_override_flag, 1); /* num_ref_idx_active_override_flag: */
440 if (slice_param.num_ref_idx_active_override_flag)
441 bitstream_put_ue(bs, slice_param.num_ref_idx_l0_active_minus1);
443 /* ref_pic_list_reordering */
444 bitstream_put_ui(bs, 0, 1); /* ref_pic_list_reordering_flag_l0: 0 */
445 } else if (IS_B_SLICE(slice_param.slice_type)) {
446 bitstream_put_ui(bs, slice_param.direct_spatial_mv_pred_flag, 1); /* direct_spatial_mv_pred: 1 */
448 bitstream_put_ui(bs, slice_param.num_ref_idx_active_override_flag, 1); /* num_ref_idx_active_override_flag: */
450 if (slice_param.num_ref_idx_active_override_flag) {
451 bitstream_put_ue(bs, slice_param.num_ref_idx_l0_active_minus1);
452 bitstream_put_ue(bs, slice_param.num_ref_idx_l1_active_minus1);
455 /* ref_pic_list_reordering */
456 bitstream_put_ui(bs, 0, 1); /* ref_pic_list_reordering_flag_l0: 0 */
457 bitstream_put_ui(bs, 0, 1); /* ref_pic_list_reordering_flag_l1: 0 */
460 if ((pic_param.pic_fields.bits.weighted_pred_flag &&
461 IS_P_SLICE(slice_param.slice_type)) ||
462 ((pic_param.pic_fields.bits.weighted_bipred_idc == 1) &&
463 IS_B_SLICE(slice_param.slice_type))) {
464 /* FIXME: fill weight/offset table */
468 /* dec_ref_pic_marking */
469 if (pic_param.pic_fields.bits.reference_pic_flag) { /* nal_ref_idc != 0 */
470 unsigned char no_output_of_prior_pics_flag = 0;
471 unsigned char long_term_reference_flag = 0;
472 unsigned char adaptive_ref_pic_marking_mode_flag = 0;
474 if (pic_param.pic_fields.bits.idr_pic_flag) {
475 bitstream_put_ui(bs, no_output_of_prior_pics_flag, 1); /* no_output_of_prior_pics_flag: 0 */
476 bitstream_put_ui(bs, long_term_reference_flag, 1); /* long_term_reference_flag: 0 */
478 bitstream_put_ui(bs, adaptive_ref_pic_marking_mode_flag, 1); /* adaptive_ref_pic_marking_mode_flag: 0 */
482 if (pic_param.pic_fields.bits.entropy_coding_mode_flag &&
483 !IS_I_SLICE(slice_param.slice_type))
484 bitstream_put_ue(bs, slice_param.cabac_init_idc); /* cabac_init_idc: 0 */
486 bitstream_put_se(bs, slice_param.slice_qp_delta); /* slice_qp_delta: 0 */
488 /* ignore for SP/SI */
490 if (pic_param.pic_fields.bits.deblocking_filter_control_present_flag) {
491 bitstream_put_ue(bs, slice_param.disable_deblocking_filter_idc); /* disable_deblocking_filter_idc: 0 */
493 if (slice_param.disable_deblocking_filter_idc != 1) {
494 bitstream_put_se(bs, slice_param.slice_alpha_c0_offset_div2); /* slice_alpha_c0_offset_div2: 2 */
495 bitstream_put_se(bs, slice_param.slice_beta_offset_div2); /* slice_beta_offset_div2: 2 */
499 if (pic_param.pic_fields.bits.entropy_coding_mode_flag) {
500 bitstream_byte_aligning(bs, 1);
505 build_packed_pic_buffer(unsigned char **header_buffer)
509 bitstream_start(&bs);
510 nal_start_code_prefix(&bs);
511 nal_header(&bs, NAL_REF_IDC_HIGH, NAL_PPS);
515 *header_buffer = (unsigned char *)bs.buffer;
516 return bs.bit_offset;
520 build_packed_seq_buffer(unsigned char **header_buffer)
524 bitstream_start(&bs);
525 nal_start_code_prefix(&bs);
526 nal_header(&bs, NAL_REF_IDC_HIGH, NAL_SPS);
530 *header_buffer = (unsigned char *)bs.buffer;
531 return bs.bit_offset;
534 static int build_packed_slice_buffer(unsigned char **header_buffer)
537 int is_idr = !!pic_param.pic_fields.bits.idr_pic_flag;
538 int is_ref = !!pic_param.pic_fields.bits.reference_pic_flag;
540 bitstream_start(&bs);
541 nal_start_code_prefix(&bs);
543 if (IS_I_SLICE(slice_param.slice_type)) {
544 nal_header(&bs, NAL_REF_IDC_HIGH, is_idr ? NAL_IDR : NAL_NON_IDR);
545 } else if (IS_P_SLICE(slice_param.slice_type)) {
546 nal_header(&bs, NAL_REF_IDC_MEDIUM, NAL_NON_IDR);
548 assert(IS_B_SLICE(slice_param.slice_type));
549 nal_header(&bs, is_ref ? NAL_REF_IDC_LOW : NAL_REF_IDC_NONE, NAL_NON_IDR);
555 *header_buffer = (unsigned char *)bs.buffer;
556 return bs.bit_offset;
561 Assume frame sequence is: Frame#0, #1, #2, ..., #M, ..., #X, ... (encoding order)
562 1) period between Frame #X and Frame #N = #X - #N
563 2) 0 means infinite for intra_period/intra_idr_period, and 0 is invalid for ip_period
564 3) intra_idr_period % intra_period (intra_period > 0) and intra_period % ip_period must be 0
565 4) intra_period and intra_idr_period take precedence over ip_period
566 5) if ip_period > 1, intra_period and intra_idr_period are not the strict periods
567 of I/IDR frames, see bellow examples
568 -------------------------------------------------------------------
569 intra_period intra_idr_period ip_period frame sequence (intra_period/intra_idr_period/ip_period)
570 0 ignored 1 IDRPPPPPPP ... (No IDR/I any more)
571 0 ignored >=2 IDR(PBB)(PBB)... (No IDR/I any more)
572 1 0 ignored IDRIIIIIII... (No IDR any more)
573 1 1 ignored IDR IDR IDR IDR...
574 1 >=2 ignored IDRII IDRII IDR... (1/3/ignore)
575 >=2 0 1 IDRPPP IPPP I... (3/0/1)
576 >=2 0 >=2 IDR(PBB)(PBB)(IBB) (6/0/3)
577 (PBB)(IBB)(PBB)(IBB)...
578 >=2 >=2 1 IDRPPPPP IPPPPP IPPPPP (6/18/1)
579 IDRPPPPP IPPPPP IPPPPP...
580 >=2 >=2 >=2 {IDR(PBB)(PBB)(IBB)(PBB)(IBB)(PBB)} (6/18/3)
581 {IDR(PBB)(PBB)(IBB)(PBB)(IBB)(PBB)}...
582 {IDR(PBB)(PBB)(IBB)(PBB)} (6/12/3)
583 {IDR(PBB)(PBB)(IBB)(PBB)}...
584 {IDR(PBB)(PBB)} (6/6/3)
589 * Return displaying order with specified periods and encoding order
590 * displaying_order: displaying order
591 * frame_type: frame type
597 void encoding2display_order(
598 unsigned long long encoding_order, int intra_period,
599 int intra_idr_period, int ip_period,
600 unsigned long long *displaying_order,
603 int encoding_order_gop = 0;
605 if (intra_period == 1) { /* all are I/IDR frames */
606 *displaying_order = encoding_order;
607 if (intra_idr_period == 0)
608 *frame_type = (encoding_order == 0)?FRAME_IDR:FRAME_I;
610 *frame_type = (encoding_order % intra_idr_period == 0)?FRAME_IDR:FRAME_I;
614 if (intra_period == 0)
615 intra_idr_period = 0;
619 * IDR (PBB)(PBB)(IBB)(PBB)
621 encoding_order_gop = (intra_idr_period == 0)? encoding_order:
622 (encoding_order % (intra_idr_period + ((ip_period == 1)?0:1)));
624 if (encoding_order_gop == 0) { /* the first frame */
625 *frame_type = FRAME_IDR;
626 *displaying_order = encoding_order;
627 } else if (((encoding_order_gop - 1) % ip_period) != 0) { /* B frames */
628 *frame_type = FRAME_B;
629 *displaying_order = encoding_order - 1;
630 } else if ((intra_period != 0) && /* have I frames */
631 (encoding_order_gop >= 2) &&
632 ((ip_period == 1 && encoding_order_gop % intra_period == 0) || /* for IDR PPPPP IPPPP */
633 /* for IDR (PBB)(PBB)(IBB) */
634 (ip_period >= 2 && ((encoding_order_gop - 1) / ip_period % (intra_period / ip_period)) == 0))) {
635 *frame_type = FRAME_I;
636 *displaying_order = encoding_order + ip_period - 1;
638 *frame_type = FRAME_P;
639 *displaying_order = encoding_order + ip_period - 1;
644 static const char *rc_to_string(int rcmode)
657 case VA_RC_VBR_CONSTRAINED:
658 return "VBR_CONSTRAINED";
665 static int process_cmdline(int argc, char *argv[])
668 const struct option long_opts[] = {
669 {"help", no_argument, NULL, 0 },
670 {"bitrate", required_argument, NULL, 1 },
671 {"minqp", required_argument, NULL, 2 },
672 {"initialqp", required_argument, NULL, 3 },
673 {"intra_period", required_argument, NULL, 4 },
674 {"idr_period", required_argument, NULL, 5 },
675 {"ip_period", required_argument, NULL, 6 },
676 {"rcmode", required_argument, NULL, 7 },
677 {"srcyuv", required_argument, NULL, 9 },
678 {"recyuv", required_argument, NULL, 10 },
679 {"fourcc", required_argument, NULL, 11 },
680 {"syncmode", no_argument, NULL, 12 },
681 {"enablePSNR", no_argument, NULL, 13 },
682 {"prit", required_argument, NULL, 14 },
683 {"priv", required_argument, NULL, 15 },
684 {"framecount", required_argument, NULL, 16 },
685 {"entropy", required_argument, NULL, 17 },
686 {"profile", required_argument, NULL, 18 },
687 {NULL, no_argument, NULL, 0 }};
690 while ((c =getopt_long_only(argc, argv, "w:h:n:f:o:?", long_opts, &long_index)) != EOF) {
693 frame_width = atoi(optarg);
696 frame_height = atoi(optarg);
700 frame_rate = atoi(optarg);
703 coded_fn = strdup(optarg);
709 frame_bitrate = atoi(optarg);
712 minimal_qp = atoi(optarg);
715 initial_qp = atoi(optarg);
718 intra_period = atoi(optarg);
721 intra_idr_period = atoi(optarg);
724 ip_period = atoi(optarg);
727 rc_mode = string_to_rc(optarg);
734 srcyuv_fn = strdup(optarg);
737 srcyuv_fourcc = string_to_fourcc(optarg);
738 if (srcyuv_fourcc <= 0) {
747 misc_priv_type = strtol(optarg, NULL, 0);
750 misc_priv_value = strtol(optarg, NULL, 0);
753 h264_entropy_mode = atoi(optarg) ? 1: 0;
756 if (strncmp(optarg, "BP", 2) == 0)
757 h264_profile = VAProfileH264Baseline;
758 else if (strncmp(optarg, "MP", 2) == 0)
759 h264_profile = VAProfileH264Main;
760 else if (strncmp(optarg, "HP", 2) == 0)
761 h264_profile = VAProfileH264High;
763 h264_profile = (VAProfile)0;
773 printf(" ip_period must be greater than 0\n");
776 if (intra_period != 1 && intra_period % ip_period != 0) {
777 printf(" intra_period must be a multiplier of ip_period\n");
780 if (intra_period != 0 && intra_idr_period % intra_period != 0) {
781 printf(" intra_idr_period must be a multiplier of intra_period\n");
785 if (frame_bitrate == 0)
786 frame_bitrate = frame_width * frame_height * 12 * frame_rate / 50;
788 if (coded_fn == NULL) {
790 if (stat("/tmp", &buf) == 0)
791 coded_fn = strdup("/tmp/test.264");
792 else if (stat("/sdcard", &buf) == 0)
793 coded_fn = strdup("/sdcard/test.264");
795 coded_fn = strdup("./test.264");
799 frame_width_mbaligned = (frame_width + 15) & (~15);
800 frame_height_mbaligned = (frame_height + 15) & (~15);
801 if (frame_width != frame_width_mbaligned ||
802 frame_height != frame_height_mbaligned) {
803 printf("Source frame is %dx%d and will code clip to %dx%d with crop\n",
804 frame_width, frame_height,
805 frame_width_mbaligned, frame_height_mbaligned
813 static Display *x11_display;
814 static Window x11_window;
817 va_open_display(void)
819 x11_display = XOpenDisplay(NULL);
821 fprintf(stderr, "error: can't connect to X server!\n");
824 return vaGetDisplay(x11_display);
828 va_close_display(VADisplay va_dpy)
834 XUnmapWindow(x11_display, x11_window);
835 XDestroyWindow(x11_display, x11_window);
838 XCloseDisplay(x11_display);
842 static int init_va(void)
844 VAProfile profile_list[]={VAProfileH264High, VAProfileH264Main, VAProfileH264Baseline, VAProfileH264ConstrainedBaseline};
845 VAEntrypoint *entrypoints;
846 int num_entrypoints, slice_entrypoint;
847 int support_encode = 0;
848 int major_ver, minor_ver;
852 va_dpy = va_open_display();
853 va_status = vaInitialize(va_dpy, &major_ver, &minor_ver);
854 CHECK_VASTATUS(va_status, "vaInitialize");
856 num_entrypoints = vaMaxNumEntrypoints(va_dpy);
857 entrypoints = (VAEntrypoint *)malloc(num_entrypoints * sizeof(*entrypoints));
859 fprintf(stderr, "error: failed to initialize VA entrypoints array\n");
863 /* use the highest profile */
864 for (i = 0; i < sizeof(profile_list)/sizeof(profile_list[0]); i++) {
865 if ((h264_profile != ~0) && h264_profile != profile_list[i])
868 h264_profile = profile_list[i];
869 vaQueryConfigEntrypoints(va_dpy, h264_profile, entrypoints, &num_entrypoints);
870 for (slice_entrypoint = 0; slice_entrypoint < num_entrypoints; slice_entrypoint++) {
871 if (entrypoints[slice_entrypoint] == VAEntrypointEncSlice) {
876 if (support_encode == 1)
880 if (support_encode == 0) {
881 printf("Can't find VAEntrypointEncSlice for H264 profiles\n");
884 switch (h264_profile) {
885 case VAProfileH264Baseline:
886 printf("Use profile VAProfileH264Baseline\n");
888 constraint_set_flag |= (1 << 0); /* Annex A.2.1 */
889 h264_entropy_mode = 0;
891 case VAProfileH264ConstrainedBaseline:
892 printf("Use profile VAProfileH264ConstrainedBaseline\n");
893 constraint_set_flag |= (1 << 0 | 1 << 1); /* Annex A.2.2 */
897 case VAProfileH264Main:
898 printf("Use profile VAProfileH264Main\n");
899 constraint_set_flag |= (1 << 1); /* Annex A.2.2 */
902 case VAProfileH264High:
903 constraint_set_flag |= (1 << 3); /* Annex A.2.4 */
904 printf("Use profile VAProfileH264High\n");
907 printf("unknow profile. Set to Baseline");
908 h264_profile = VAProfileH264Baseline;
910 constraint_set_flag |= (1 << 0); /* Annex A.2.1 */
915 VAConfigAttrib attrib[VAConfigAttribTypeMax];
917 /* find out the format for the render target, and rate control mode */
918 for (i = 0; i < VAConfigAttribTypeMax; i++)
919 attrib[i].type = (VAConfigAttribType)i;
921 va_status = vaGetConfigAttributes(va_dpy, h264_profile, VAEntrypointEncSlice,
922 &attrib[0], VAConfigAttribTypeMax);
923 CHECK_VASTATUS(va_status, "vaGetConfigAttributes");
924 /* check the interested configattrib */
925 if ((attrib[VAConfigAttribRTFormat].value & VA_RT_FORMAT_YUV420) == 0) {
926 printf("Not find desired YUV420 RT format\n");
929 config_attrib[config_attrib_num].type = VAConfigAttribRTFormat;
930 config_attrib[config_attrib_num].value = VA_RT_FORMAT_YUV420;
934 if (attrib[VAConfigAttribRateControl].value != VA_ATTRIB_NOT_SUPPORTED) {
935 int tmp = attrib[VAConfigAttribRateControl].value;
937 printf("Support rate control mode (0x%x):", tmp);
939 if (tmp & VA_RC_NONE)
949 if (tmp & VA_RC_VBR_CONSTRAINED)
950 printf("VBR_CONSTRAINED ");
954 if (rc_mode == -1 || !(rc_mode & tmp)) {
956 printf("Warning: Don't support the specified RateControl mode: %s!!!, switch to ", rc_to_string(rc_mode));
959 for (i = 0; i < sizeof(rc_default_modes) / sizeof(rc_default_modes[0]); i++) {
960 if (rc_default_modes[i] & tmp) {
961 rc_mode = rc_default_modes[i];
966 printf("RateControl mode: %s\n", rc_to_string(rc_mode));
969 config_attrib[config_attrib_num].type = VAConfigAttribRateControl;
970 config_attrib[config_attrib_num].value = rc_mode;
975 if (attrib[VAConfigAttribEncPackedHeaders].value != VA_ATTRIB_NOT_SUPPORTED) {
976 int tmp = attrib[VAConfigAttribEncPackedHeaders].value;
978 printf("Support VAConfigAttribEncPackedHeaders\n");
980 h264_packedheader = 1;
981 config_attrib[config_attrib_num].type = VAConfigAttribEncPackedHeaders;
982 config_attrib[config_attrib_num].value = VA_ENC_PACKED_HEADER_NONE;
984 if (tmp & VA_ENC_PACKED_HEADER_SEQUENCE) {
985 printf("Support packed sequence headers\n");
986 config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_SEQUENCE;
989 if (tmp & VA_ENC_PACKED_HEADER_PICTURE) {
990 printf("Support packed picture headers\n");
991 config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_PICTURE;
994 if (tmp & VA_ENC_PACKED_HEADER_SLICE) {
995 printf("Support packed slice headers\n");
996 config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_SLICE;
999 if (tmp & VA_ENC_PACKED_HEADER_MISC) {
1000 printf("Support packed misc headers\n");
1001 config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_MISC;
1004 enc_packed_header_idx = config_attrib_num;
1005 config_attrib_num++;
1008 if (attrib[VAConfigAttribEncInterlaced].value != VA_ATTRIB_NOT_SUPPORTED) {
1009 int tmp = attrib[VAConfigAttribEncInterlaced].value;
1011 printf("Support VAConfigAttribEncInterlaced\n");
1013 if (tmp & VA_ENC_INTERLACED_FRAME)
1014 printf("support VA_ENC_INTERLACED_FRAME\n");
1015 if (tmp & VA_ENC_INTERLACED_FIELD)
1016 printf("Support VA_ENC_INTERLACED_FIELD\n");
1017 if (tmp & VA_ENC_INTERLACED_MBAFF)
1018 printf("Support VA_ENC_INTERLACED_MBAFF\n");
1019 if (tmp & VA_ENC_INTERLACED_PAFF)
1020 printf("Support VA_ENC_INTERLACED_PAFF\n");
1022 config_attrib[config_attrib_num].type = VAConfigAttribEncInterlaced;
1023 config_attrib[config_attrib_num].value = VA_ENC_PACKED_HEADER_NONE;
1024 config_attrib_num++;
1027 if (attrib[VAConfigAttribEncMaxRefFrames].value != VA_ATTRIB_NOT_SUPPORTED) {
1028 h264_maxref = attrib[VAConfigAttribEncMaxRefFrames].value;
1030 printf("Support %d RefPicList0 and %d RefPicList1\n",
1031 h264_maxref & 0xffff, (h264_maxref >> 16) & 0xffff );
1034 if (attrib[VAConfigAttribEncMaxSlices].value != VA_ATTRIB_NOT_SUPPORTED)
1035 printf("Support %d slices\n", attrib[VAConfigAttribEncMaxSlices].value);
1037 if (attrib[VAConfigAttribEncSliceStructure].value != VA_ATTRIB_NOT_SUPPORTED) {
1038 int tmp = attrib[VAConfigAttribEncSliceStructure].value;
1040 printf("Support VAConfigAttribEncSliceStructure\n");
1042 if (tmp & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS)
1043 printf("Support VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS\n");
1044 if (tmp & VA_ENC_SLICE_STRUCTURE_POWER_OF_TWO_ROWS)
1045 printf("Support VA_ENC_SLICE_STRUCTURE_POWER_OF_TWO_ROWS\n");
1046 if (tmp & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS)
1047 printf("Support VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS\n");
1049 if (attrib[VAConfigAttribEncMacroblockInfo].value != VA_ATTRIB_NOT_SUPPORTED) {
1050 printf("Support VAConfigAttribEncMacroblockInfo\n");
1057 static int setup_encode()
1060 VASurfaceID *tmp_surfaceid;
1061 int codedbuf_size, i;
1062 static VASurfaceID src_surface[SURFACE_NUM];
1063 static VASurfaceID ref_surface[SURFACE_NUM];
1065 va_status = vaCreateConfig(va_dpy, h264_profile, VAEntrypointEncSlice,
1066 &config_attrib[0], config_attrib_num, &config_id);
1067 CHECK_VASTATUS(va_status, "vaCreateConfig");
1069 /* create source surfaces */
1070 va_status = vaCreateSurfaces(va_dpy,
1071 VA_RT_FORMAT_YUV420, frame_width_mbaligned, frame_height_mbaligned,
1072 &src_surface[0], SURFACE_NUM,
1074 CHECK_VASTATUS(va_status, "vaCreateSurfaces");
1076 /* create reference surfaces */
1077 va_status = vaCreateSurfaces(va_dpy,
1078 VA_RT_FORMAT_YUV420, frame_width_mbaligned, frame_height_mbaligned,
1079 &ref_surface[0], SURFACE_NUM,
1081 CHECK_VASTATUS(va_status, "vaCreateSurfaces");
1083 tmp_surfaceid = (VASurfaceID *)calloc(2 * SURFACE_NUM, sizeof(VASurfaceID));
1084 memcpy(tmp_surfaceid, src_surface, SURFACE_NUM * sizeof(VASurfaceID));
1085 memcpy(tmp_surfaceid + SURFACE_NUM, ref_surface, SURFACE_NUM * sizeof(VASurfaceID));
1087 /* Create a context for this encode pipe */
1088 va_status = vaCreateContext(va_dpy, config_id,
1089 frame_width_mbaligned, frame_height_mbaligned,
1091 tmp_surfaceid, 2 * SURFACE_NUM,
1093 CHECK_VASTATUS(va_status, "vaCreateContext");
1094 free(tmp_surfaceid);
1096 codedbuf_size = (frame_width_mbaligned * frame_height_mbaligned * 400) / (16*16);
1098 for (i = 0; i < SURFACE_NUM; i++) {
1099 /* create coded buffer once for all
1100 * other VA buffers which won't be used again after vaRenderPicture.
1101 * so APP can always vaCreateBuffer for every frame
1102 * but coded buffer need to be mapped and accessed after vaRenderPicture/vaEndPicture
1103 * so VA won't maintain the coded buffer
1105 va_status = vaCreateBuffer(va_dpy, context_id, VAEncCodedBufferType,
1106 codedbuf_size, 1, NULL, &gl_surfaces[i].coded_buf);
1107 CHECK_VASTATUS(va_status, "vaCreateBuffer");
1110 /* create OpenGL objects */
1111 //glGenFramebuffers(SURFACE_NUM, fbos);
1113 for (i = 0; i < SURFACE_NUM; i++) {
1114 glGenTextures(1, &gl_surfaces[i].y_tex);
1115 glGenTextures(1, &gl_surfaces[i].cbcr_tex);
1118 for (i = 0; i < SURFACE_NUM; i++) {
1119 gl_surfaces[i].src_surface = src_surface[i];
1120 gl_surfaces[i].ref_surface = ref_surface[i];
1128 #define partition(ref, field, key, ascending) \
1131 while (ref[i].field < key) \
1133 while (ref[j].field > key) \
1136 while (ref[i].field > key) \
1138 while (ref[j].field < key) \
1150 static void sort_one(VAPictureH264 ref[], int left, int right,
1151 int ascending, int frame_idx)
1153 int i = left, j = right;
1158 key = ref[(left + right) / 2].frame_idx;
1159 partition(ref, frame_idx, key, ascending);
1161 key = ref[(left + right) / 2].TopFieldOrderCnt;
1162 partition(ref, TopFieldOrderCnt, (signed int)key, ascending);
1167 sort_one(ref, left, j, ascending, frame_idx);
1170 sort_one(ref, i, right, ascending, frame_idx);
1173 static void sort_two(VAPictureH264 ref[], int left, int right, unsigned int key, unsigned int frame_idx,
1174 int partition_ascending, int list0_ascending, int list1_ascending)
1176 int i = left, j = right;
1180 partition(ref, frame_idx, key, partition_ascending);
1182 partition(ref, TopFieldOrderCnt, (signed int)key, partition_ascending);
1186 sort_one(ref, left, i-1, list0_ascending, frame_idx);
1187 sort_one(ref, j+1, right, list1_ascending, frame_idx);
1190 static int update_ReferenceFrames(void)
1194 if (current_frame_type == FRAME_B)
1197 CurrentCurrPic.flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
1199 if (numShortTerm > num_ref_frames)
1200 numShortTerm = num_ref_frames;
1201 for (i=numShortTerm-1; i>0; i--)
1202 ReferenceFrames[i] = ReferenceFrames[i-1];
1203 ReferenceFrames[0] = CurrentCurrPic;
1205 if (current_frame_type != FRAME_B)
1206 current_frame_num++;
1207 if (current_frame_num > MaxFrameNum)
1208 current_frame_num = 0;
1214 static int update_RefPicList(void)
1216 unsigned int current_poc = CurrentCurrPic.TopFieldOrderCnt;
1218 if (current_frame_type == FRAME_P) {
1219 memcpy(RefPicList0_P, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
1220 sort_one(RefPicList0_P, 0, numShortTerm-1, 0, 1);
1223 if (current_frame_type == FRAME_B) {
1224 memcpy(RefPicList0_B, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
1225 sort_two(RefPicList0_B, 0, numShortTerm-1, current_poc, 0,
1228 memcpy(RefPicList1_B, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
1229 sort_two(RefPicList1_B, 0, numShortTerm-1, current_poc, 0,
1237 static int render_sequence(void)
1239 VABufferID seq_param_buf, rc_param_buf, misc_param_tmpbuf, render_id[2];
1241 VAEncMiscParameterBuffer *misc_param, *misc_param_tmp;
1242 VAEncMiscParameterRateControl *misc_rate_ctrl;
1244 seq_param.level_idc = 41 /*SH_LEVEL_3*/;
1245 seq_param.picture_width_in_mbs = frame_width_mbaligned / 16;
1246 seq_param.picture_height_in_mbs = frame_height_mbaligned / 16;
1247 seq_param.bits_per_second = frame_bitrate;
1249 seq_param.intra_period = intra_period;
1250 seq_param.intra_idr_period = intra_idr_period;
1251 seq_param.ip_period = ip_period;
1253 seq_param.max_num_ref_frames = num_ref_frames;
1254 seq_param.seq_fields.bits.frame_mbs_only_flag = 1;
1255 seq_param.time_scale = frame_rate * 2;
1256 seq_param.num_units_in_tick = 1; /* Tc = num_units_in_tick / scale */
1257 seq_param.seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 = Log2MaxPicOrderCntLsb - 4;
1258 seq_param.seq_fields.bits.log2_max_frame_num_minus4 = Log2MaxFrameNum - 4;;
1259 seq_param.seq_fields.bits.frame_mbs_only_flag = 1;
1260 seq_param.seq_fields.bits.chroma_format_idc = 1;
1261 seq_param.seq_fields.bits.direct_8x8_inference_flag = 1;
1263 if (frame_width != frame_width_mbaligned ||
1264 frame_height != frame_height_mbaligned) {
1265 seq_param.frame_cropping_flag = 1;
1266 seq_param.frame_crop_left_offset = 0;
1267 seq_param.frame_crop_right_offset = (frame_width_mbaligned - frame_width)/2;
1268 seq_param.frame_crop_top_offset = 0;
1269 seq_param.frame_crop_bottom_offset = (frame_height_mbaligned - frame_height)/2;
1272 va_status = vaCreateBuffer(va_dpy, context_id,
1273 VAEncSequenceParameterBufferType,
1274 sizeof(seq_param), 1, &seq_param, &seq_param_buf);
1275 CHECK_VASTATUS(va_status, "vaCreateBuffer");
1277 va_status = vaCreateBuffer(va_dpy, context_id,
1278 VAEncMiscParameterBufferType,
1279 sizeof(VAEncMiscParameterBuffer) + sizeof(VAEncMiscParameterRateControl),
1280 1, NULL, &rc_param_buf);
1281 CHECK_VASTATUS(va_status, "vaCreateBuffer");
1283 vaMapBuffer(va_dpy, rc_param_buf, (void **)&misc_param);
1284 misc_param->type = VAEncMiscParameterTypeRateControl;
1285 misc_rate_ctrl = (VAEncMiscParameterRateControl *)misc_param->data;
1286 memset(misc_rate_ctrl, 0, sizeof(*misc_rate_ctrl));
1287 misc_rate_ctrl->bits_per_second = frame_bitrate;
1288 misc_rate_ctrl->target_percentage = 66;
1289 misc_rate_ctrl->window_size = 1000;
1290 misc_rate_ctrl->initial_qp = initial_qp;
1291 misc_rate_ctrl->min_qp = minimal_qp;
1292 misc_rate_ctrl->basic_unit_size = 0;
1293 vaUnmapBuffer(va_dpy, rc_param_buf);
1295 render_id[0] = seq_param_buf;
1296 render_id[1] = rc_param_buf;
1298 va_status = vaRenderPicture(va_dpy, context_id, &render_id[0], 2);
1299 CHECK_VASTATUS(va_status, "vaRenderPicture");;
1301 if (misc_priv_type != 0) {
1302 va_status = vaCreateBuffer(va_dpy, context_id,
1303 VAEncMiscParameterBufferType,
1304 sizeof(VAEncMiscParameterBuffer),
1305 1, NULL, &misc_param_tmpbuf);
1306 CHECK_VASTATUS(va_status, "vaCreateBuffer");
1307 vaMapBuffer(va_dpy, misc_param_tmpbuf, (void **)&misc_param_tmp);
1308 misc_param_tmp->type = (VAEncMiscParameterType)misc_priv_type;
1309 misc_param_tmp->data[0] = misc_priv_value;
1310 vaUnmapBuffer(va_dpy, misc_param_tmpbuf);
1312 va_status = vaRenderPicture(va_dpy, context_id, &misc_param_tmpbuf, 1);
1318 static int calc_poc(int pic_order_cnt_lsb)
1320 static int PicOrderCntMsb_ref = 0, pic_order_cnt_lsb_ref = 0;
1321 int prevPicOrderCntMsb, prevPicOrderCntLsb;
1322 int PicOrderCntMsb, TopFieldOrderCnt;
1324 if (current_frame_type == FRAME_IDR)
1325 prevPicOrderCntMsb = prevPicOrderCntLsb = 0;
1327 prevPicOrderCntMsb = PicOrderCntMsb_ref;
1328 prevPicOrderCntLsb = pic_order_cnt_lsb_ref;
1331 if ((pic_order_cnt_lsb < prevPicOrderCntLsb) &&
1332 ((prevPicOrderCntLsb - pic_order_cnt_lsb) >= (int)(MaxPicOrderCntLsb / 2)))
1333 PicOrderCntMsb = prevPicOrderCntMsb + MaxPicOrderCntLsb;
1334 else if ((pic_order_cnt_lsb > prevPicOrderCntLsb) &&
1335 ((pic_order_cnt_lsb - prevPicOrderCntLsb) > (int)(MaxPicOrderCntLsb / 2)))
1336 PicOrderCntMsb = prevPicOrderCntMsb - MaxPicOrderCntLsb;
1338 PicOrderCntMsb = prevPicOrderCntMsb;
1340 TopFieldOrderCnt = PicOrderCntMsb + pic_order_cnt_lsb;
1342 if (current_frame_type != FRAME_B) {
1343 PicOrderCntMsb_ref = PicOrderCntMsb;
1344 pic_order_cnt_lsb_ref = pic_order_cnt_lsb;
1347 return TopFieldOrderCnt;
1350 static int render_picture(void)
1352 VABufferID pic_param_buf;
1356 pic_param.CurrPic.picture_id = gl_surfaces[current_frame_display % SURFACE_NUM].ref_surface;
1357 pic_param.CurrPic.frame_idx = current_frame_num;
1358 pic_param.CurrPic.flags = 0;
1359 pic_param.CurrPic.TopFieldOrderCnt = calc_poc((current_frame_display - current_IDR_display) % MaxPicOrderCntLsb);
1360 pic_param.CurrPic.BottomFieldOrderCnt = pic_param.CurrPic.TopFieldOrderCnt;
1361 CurrentCurrPic = pic_param.CurrPic;
1363 if (getenv("TO_DEL")) { /* set RefPicList into ReferenceFrames */
1364 update_RefPicList(); /* calc RefPicList */
1365 memset(pic_param.ReferenceFrames, 0xff, 16 * sizeof(VAPictureH264)); /* invalid all */
1366 if (current_frame_type == FRAME_P) {
1367 pic_param.ReferenceFrames[0] = RefPicList0_P[0];
1368 } else if (current_frame_type == FRAME_B) {
1369 pic_param.ReferenceFrames[0] = RefPicList0_B[0];
1370 pic_param.ReferenceFrames[1] = RefPicList1_B[0];
1373 memcpy(pic_param.ReferenceFrames, ReferenceFrames, numShortTerm*sizeof(VAPictureH264));
1374 for (i = numShortTerm; i < SURFACE_NUM; i++) {
1375 pic_param.ReferenceFrames[i].picture_id = VA_INVALID_SURFACE;
1376 pic_param.ReferenceFrames[i].flags = VA_PICTURE_H264_INVALID;
1380 pic_param.pic_fields.bits.idr_pic_flag = (current_frame_type == FRAME_IDR);
1381 pic_param.pic_fields.bits.reference_pic_flag = (current_frame_type != FRAME_B);
1382 pic_param.pic_fields.bits.entropy_coding_mode_flag = h264_entropy_mode;
1383 pic_param.pic_fields.bits.deblocking_filter_control_present_flag = 1;
1384 pic_param.frame_num = current_frame_num;
1385 pic_param.coded_buf = gl_surfaces[current_frame_display % SURFACE_NUM].coded_buf;
1386 pic_param.last_picture = false; // FIXME
1387 pic_param.pic_init_qp = initial_qp;
1389 va_status = vaCreateBuffer(va_dpy, context_id, VAEncPictureParameterBufferType,
1390 sizeof(pic_param), 1, &pic_param, &pic_param_buf);
1391 CHECK_VASTATUS(va_status, "vaCreateBuffer");;
1393 va_status = vaRenderPicture(va_dpy, context_id, &pic_param_buf, 1);
1394 CHECK_VASTATUS(va_status, "vaRenderPicture");
1399 static int render_packedsequence(void)
1401 VAEncPackedHeaderParameterBuffer packedheader_param_buffer;
1402 VABufferID packedseq_para_bufid, packedseq_data_bufid, render_id[2];
1403 unsigned int length_in_bits;
1404 unsigned char *packedseq_buffer = NULL;
1407 length_in_bits = build_packed_seq_buffer(&packedseq_buffer);
1409 packedheader_param_buffer.type = VAEncPackedHeaderSequence;
1411 packedheader_param_buffer.bit_length = length_in_bits; /*length_in_bits*/
1412 packedheader_param_buffer.has_emulation_bytes = 0;
1413 va_status = vaCreateBuffer(va_dpy,
1415 VAEncPackedHeaderParameterBufferType,
1416 sizeof(packedheader_param_buffer), 1, &packedheader_param_buffer,
1417 &packedseq_para_bufid);
1418 CHECK_VASTATUS(va_status, "vaCreateBuffer");
1420 va_status = vaCreateBuffer(va_dpy,
1422 VAEncPackedHeaderDataBufferType,
1423 (length_in_bits + 7) / 8, 1, packedseq_buffer,
1424 &packedseq_data_bufid);
1425 CHECK_VASTATUS(va_status, "vaCreateBuffer");
1427 render_id[0] = packedseq_para_bufid;
1428 render_id[1] = packedseq_data_bufid;
1429 va_status = vaRenderPicture(va_dpy, context_id, render_id, 2);
1430 CHECK_VASTATUS(va_status, "vaRenderPicture");
1432 free(packedseq_buffer);
1438 static int render_packedpicture(void)
1440 VAEncPackedHeaderParameterBuffer packedheader_param_buffer;
1441 VABufferID packedpic_para_bufid, packedpic_data_bufid, render_id[2];
1442 unsigned int length_in_bits;
1443 unsigned char *packedpic_buffer = NULL;
1446 length_in_bits = build_packed_pic_buffer(&packedpic_buffer);
1447 packedheader_param_buffer.type = VAEncPackedHeaderPicture;
1448 packedheader_param_buffer.bit_length = length_in_bits;
1449 packedheader_param_buffer.has_emulation_bytes = 0;
1451 va_status = vaCreateBuffer(va_dpy,
1453 VAEncPackedHeaderParameterBufferType,
1454 sizeof(packedheader_param_buffer), 1, &packedheader_param_buffer,
1455 &packedpic_para_bufid);
1456 CHECK_VASTATUS(va_status, "vaCreateBuffer");
1458 va_status = vaCreateBuffer(va_dpy,
1460 VAEncPackedHeaderDataBufferType,
1461 (length_in_bits + 7) / 8, 1, packedpic_buffer,
1462 &packedpic_data_bufid);
1463 CHECK_VASTATUS(va_status, "vaCreateBuffer");
1465 render_id[0] = packedpic_para_bufid;
1466 render_id[1] = packedpic_data_bufid;
1467 va_status = vaRenderPicture(va_dpy, context_id, render_id, 2);
1468 CHECK_VASTATUS(va_status, "vaRenderPicture");
1470 free(packedpic_buffer);
1475 static void render_packedslice()
1477 VAEncPackedHeaderParameterBuffer packedheader_param_buffer;
1478 VABufferID packedslice_para_bufid, packedslice_data_bufid, render_id[2];
1479 unsigned int length_in_bits;
1480 unsigned char *packedslice_buffer = NULL;
1483 length_in_bits = build_packed_slice_buffer(&packedslice_buffer);
1484 packedheader_param_buffer.type = VAEncPackedHeaderSlice;
1485 packedheader_param_buffer.bit_length = length_in_bits;
1486 packedheader_param_buffer.has_emulation_bytes = 0;
1488 va_status = vaCreateBuffer(va_dpy,
1490 VAEncPackedHeaderParameterBufferType,
1491 sizeof(packedheader_param_buffer), 1, &packedheader_param_buffer,
1492 &packedslice_para_bufid);
1493 CHECK_VASTATUS(va_status, "vaCreateBuffer");
1495 va_status = vaCreateBuffer(va_dpy,
1497 VAEncPackedHeaderDataBufferType,
1498 (length_in_bits + 7) / 8, 1, packedslice_buffer,
1499 &packedslice_data_bufid);
1500 CHECK_VASTATUS(va_status, "vaCreateBuffer");
1502 render_id[0] = packedslice_para_bufid;
1503 render_id[1] = packedslice_data_bufid;
1504 va_status = vaRenderPicture(va_dpy, context_id, render_id, 2);
1505 CHECK_VASTATUS(va_status, "vaRenderPicture");
1507 free(packedslice_buffer);
1510 static int render_slice(void)
1512 VABufferID slice_param_buf;
1516 update_RefPicList();
1518 /* one frame, one slice */
1519 slice_param.macroblock_address = 0;
1520 slice_param.num_macroblocks = frame_width_mbaligned * frame_height_mbaligned/(16*16); /* Measured by MB */
1521 slice_param.slice_type = (current_frame_type == FRAME_IDR)?2:current_frame_type;
1522 if (current_frame_type == FRAME_IDR) {
1523 if (current_frame_encoding != 0)
1524 ++slice_param.idr_pic_id;
1525 } else if (current_frame_type == FRAME_P) {
1526 int refpiclist0_max = h264_maxref & 0xffff;
1527 memcpy(slice_param.RefPicList0, RefPicList0_P, refpiclist0_max*sizeof(VAPictureH264));
1529 for (i = refpiclist0_max; i < 32; i++) {
1530 slice_param.RefPicList0[i].picture_id = VA_INVALID_SURFACE;
1531 slice_param.RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
1533 } else if (current_frame_type == FRAME_B) {
1534 int refpiclist0_max = h264_maxref & 0xffff;
1535 int refpiclist1_max = (h264_maxref >> 16) & 0xffff;
1537 memcpy(slice_param.RefPicList0, RefPicList0_B, refpiclist0_max*sizeof(VAPictureH264));
1538 for (i = refpiclist0_max; i < 32; i++) {
1539 slice_param.RefPicList0[i].picture_id = VA_INVALID_SURFACE;
1540 slice_param.RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
1543 memcpy(slice_param.RefPicList1, RefPicList1_B, refpiclist1_max*sizeof(VAPictureH264));
1544 for (i = refpiclist1_max; i < 32; i++) {
1545 slice_param.RefPicList1[i].picture_id = VA_INVALID_SURFACE;
1546 slice_param.RefPicList1[i].flags = VA_PICTURE_H264_INVALID;
1550 slice_param.slice_alpha_c0_offset_div2 = 0;
1551 slice_param.slice_beta_offset_div2 = 0;
1552 slice_param.direct_spatial_mv_pred_flag = 1;
1553 slice_param.pic_order_cnt_lsb = (current_frame_display - current_IDR_display) % MaxPicOrderCntLsb;
1556 if (h264_packedheader &&
1557 config_attrib[enc_packed_header_idx].value & VA_ENC_PACKED_HEADER_SLICE)
1558 render_packedslice();
1560 va_status = vaCreateBuffer(va_dpy, context_id, VAEncSliceParameterBufferType,
1561 sizeof(slice_param), 1, &slice_param, &slice_param_buf);
1562 CHECK_VASTATUS(va_status, "vaCreateBuffer");;
1564 va_status = vaRenderPicture(va_dpy, context_id, &slice_param_buf, 1);
1565 CHECK_VASTATUS(va_status, "vaRenderPicture");
1572 int H264Encoder::save_codeddata(unsigned long long display_order, unsigned long long encode_order, int frame_type)
1574 VACodedBufferSegment *buf_list = NULL;
1576 unsigned int coded_size = 0;
1580 va_status = vaMapBuffer(va_dpy, gl_surfaces[display_order % SURFACE_NUM].coded_buf, (void **)(&buf_list));
1581 CHECK_VASTATUS(va_status, "vaMapBuffer");
1582 while (buf_list != NULL) {
1583 data.append(reinterpret_cast<const char *>(buf_list->buf), buf_list->size);
1584 buf_list = (VACodedBufferSegment *) buf_list->next;
1586 frame_size += coded_size;
1588 vaUnmapBuffer(va_dpy, gl_surfaces[display_order % SURFACE_NUM].coded_buf);
1591 memset(&pkt, 0, sizeof(pkt));
1593 pkt.pts = av_rescale_q(display_order, AVRational{1, frame_rate}, avstream->time_base);
1594 pkt.dts = av_rescale_q(encode_order, AVRational{1, frame_rate}, avstream->time_base);
1595 pkt.data = reinterpret_cast<uint8_t *>(&data[0]);
1596 pkt.size = data.size();
1597 pkt.stream_index = 0;
1598 if (frame_type == FRAME_IDR || frame_type == FRAME_I) {
1599 pkt.flags = AV_PKT_FLAG_KEY;
1604 av_interleaved_write_frame(avctx, &pkt);
1607 printf("\r "); /* return back to startpoint */
1608 switch (encode_order % 4) {
1622 printf("%08lld", encode_order);
1623 printf("(%06d bytes coded)", coded_size);
1630 // this is weird. but it seems to put a new frame onto the queue
1631 void H264Encoder::storage_task_enqueue(unsigned long long display_order, unsigned long long encode_order, int frame_type)
1633 std::unique_lock<std::mutex> lock(storage_task_queue_mutex);
1636 tmp.display_order = display_order;
1637 tmp.encode_order = encode_order;
1638 tmp.frame_type = frame_type;
1639 storage_task_queue.push(tmp);
1640 srcsurface_status[display_order % SURFACE_NUM] = SRC_SURFACE_IN_ENCODING;
1642 storage_task_queue_changed.notify_all();
1645 void H264Encoder::storage_task_thread()
1648 storage_task current;
1650 // wait until there's an encoded frame
1651 std::unique_lock<std::mutex> lock(storage_task_queue_mutex);
1652 storage_task_queue_changed.wait(lock, [this]{ return storage_thread_should_quit || !storage_task_queue.empty(); });
1653 if (storage_thread_should_quit) return;
1654 current = storage_task_queue.front();
1655 storage_task_queue.pop();
1660 // waits for data, then saves it to disk.
1661 va_status = vaSyncSurface(va_dpy, gl_surfaces[current.display_order % SURFACE_NUM].src_surface);
1662 CHECK_VASTATUS(va_status, "vaSyncSurface");
1663 save_codeddata(current.display_order, current.encode_order, current.frame_type);
1666 std::unique_lock<std::mutex> lock(storage_task_queue_mutex);
1667 srcsurface_status[current.display_order % SURFACE_NUM] = SRC_SURFACE_FREE;
1668 storage_task_queue_changed.notify_all();
1673 static int release_encode()
1677 for (i = 0; i < SURFACE_NUM; i++) {
1678 vaDestroyBuffer(va_dpy, gl_surfaces[i].coded_buf);
1679 vaDestroySurfaces(va_dpy, &gl_surfaces[i].src_surface, 1);
1680 vaDestroySurfaces(va_dpy, &gl_surfaces[i].ref_surface, 1);
1683 vaDestroyContext(va_dpy, context_id);
1684 vaDestroyConfig(va_dpy, config_id);
1689 static int deinit_va()
1691 vaTerminate(va_dpy);
1693 va_close_display(va_dpy);
1699 static int print_input()
1701 printf("\n\nINPUT:Try to encode H264...\n");
1703 printf("INPUT: RateControl : %s\n", rc_to_string(rc_mode));
1704 printf("INPUT: Resolution : %dx%dframes\n", frame_width, frame_height);
1705 printf("INPUT: FrameRate : %d\n", frame_rate);
1706 printf("INPUT: Bitrate : %d\n", frame_bitrate);
1707 printf("INPUT: Slieces : %d\n", frame_slices);
1708 printf("INPUT: IntraPeriod : %d\n", intra_period);
1709 printf("INPUT: IDRPeriod : %d\n", intra_idr_period);
1710 printf("INPUT: IpPeriod : %d\n", ip_period);
1711 printf("INPUT: Initial QP : %d\n", initial_qp);
1712 printf("INPUT: Min QP : %d\n", minimal_qp);
1713 printf("INPUT: Coded Clip : %s\n", coded_fn);
1715 printf("\n\n"); /* return back to startpoint */
1721 //H264Encoder::H264Encoder(SDL_Window *window, SDL_GLContext context, int width, int height, const char *output_filename)
1722 H264Encoder::H264Encoder(QSurface *surface, int width, int height, const char *output_filename)
1723 : current_storage_frame(0), surface(surface)
1724 //: width(width), height(height), current_encoding_frame(0)
1727 avctx = avformat_alloc_context();
1728 avctx->oformat = av_guess_format(NULL, output_filename, NULL);
1729 strcpy(avctx->filename, output_filename);
1730 if (avio_open2(&avctx->pb, output_filename, AVIO_FLAG_WRITE, &avctx->interrupt_callback, NULL) < 0) {
1731 fprintf(stderr, "%s: avio_open2() failed\n", output_filename);
1734 AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_H264);
1735 avstream = avformat_new_stream(avctx, codec);
1736 if (avstream == nullptr) {
1737 fprintf(stderr, "%s: avformat_new_stream() failed\n", output_filename);
1740 avstream->time_base = AVRational{1, frame_rate};
1741 avstream->codec->width = width;
1742 avstream->codec->height = height;
1743 avstream->codec->time_base = AVRational{1, frame_rate};
1744 avstream->codec->ticks_per_frame = 1; // or 2?
1746 if (avformat_write_header(avctx, NULL) < 0) {
1747 fprintf(stderr, "%s: avformat_write_header() failed\n", output_filename);
1751 frame_width = width;
1752 frame_height = height;
1753 frame_width_mbaligned = (frame_width + 15) & (~15);
1754 frame_height_mbaligned = (frame_height + 15) & (~15);
1755 frame_bitrate = 15000000; // / 60;
1756 current_frame_encoding = 0;
1763 // No frames are ready yet.
1764 memset(srcsurface_status, SRC_SURFACE_FREE, sizeof(srcsurface_status));
1766 memset(&seq_param, 0, sizeof(seq_param));
1767 memset(&pic_param, 0, sizeof(pic_param));
1768 memset(&slice_param, 0, sizeof(slice_param));
1770 storage_thread = std::thread(&H264Encoder::storage_task_thread, this);
1772 copy_thread = std::thread([this]{
1773 //SDL_GL_MakeCurrent(window, context);
1774 QOpenGLContext *context = create_context();
1775 eglBindAPI(EGL_OPENGL_API);
1776 if (!make_current(context, this->surface)) {
1777 printf("display=%p surface=%p context=%p curr=%p err=%d\n", eglGetCurrentDisplay(), this->surface, context, eglGetCurrentContext(),
1785 H264Encoder::~H264Encoder()
1788 unique_lock<mutex> lock(storage_task_queue_mutex);
1789 storage_thread_should_quit = true;
1790 storage_task_queue_changed.notify_all();
1793 unique_lock<mutex> lock(frame_queue_mutex);
1794 copy_thread_should_quit = true;
1795 frame_queue_nonempty.notify_one();
1797 storage_thread.join();
1803 av_write_trailer(avctx);
1804 avformat_free_context(avctx);
1807 bool H264Encoder::begin_frame(GLuint *y_tex, GLuint *cbcr_tex)
1810 // Wait until this frame slot is done encoding.
1811 std::unique_lock<std::mutex> lock(storage_task_queue_mutex);
1812 storage_task_queue_changed.wait(lock, [this]{ return storage_thread_should_quit || (srcsurface_status[current_storage_frame % SURFACE_NUM] == SRC_SURFACE_FREE); });
1813 if (storage_thread_should_quit) return false;
1816 //*fbo = fbos[current_storage_frame % SURFACE_NUM];
1817 GLSurface *surf = &gl_surfaces[current_storage_frame % SURFACE_NUM];
1818 *y_tex = surf->y_tex;
1819 *cbcr_tex = surf->cbcr_tex;
1821 VASurfaceID surface = surf->src_surface;
1822 VAStatus va_status = vaDeriveImage(va_dpy, surface, &surf->surface_image);
1823 CHECK_VASTATUS(va_status, "vaDeriveImage");
1825 VABufferInfo buf_info;
1826 buf_info.mem_type = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME; // or VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM?
1827 va_status = vaAcquireBufferHandle(va_dpy, surf->surface_image.buf, &buf_info);
1828 CHECK_VASTATUS(va_status, "vaAcquireBufferHandle");
1831 surf->y_egl_image = EGL_NO_IMAGE_KHR;
1832 EGLint y_attribs[] = {
1833 EGL_WIDTH, frame_width,
1834 EGL_HEIGHT, frame_height,
1835 EGL_LINUX_DRM_FOURCC_EXT, fourcc_code('R', '8', ' ', ' '),
1836 EGL_DMA_BUF_PLANE0_FD_EXT, EGLint(buf_info.handle),
1837 EGL_DMA_BUF_PLANE0_OFFSET_EXT, EGLint(surf->surface_image.offsets[0]),
1838 EGL_DMA_BUF_PLANE0_PITCH_EXT, EGLint(surf->surface_image.pitches[0]),
1842 surf->y_egl_image = eglCreateImageKHR(eglGetCurrentDisplay(), EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, y_attribs);
1843 assert(surf->y_egl_image != EGL_NO_IMAGE_KHR);
1845 // Associate Y image to a texture.
1846 glBindTexture(GL_TEXTURE_2D, *y_tex);
1847 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, surf->y_egl_image);
1849 // Create CbCr image.
1850 surf->cbcr_egl_image = EGL_NO_IMAGE_KHR;
1851 EGLint cbcr_attribs[] = {
1852 EGL_WIDTH, frame_width,
1853 EGL_HEIGHT, frame_height,
1854 EGL_LINUX_DRM_FOURCC_EXT, fourcc_code('G', 'R', '8', '8'),
1855 EGL_DMA_BUF_PLANE0_FD_EXT, EGLint(buf_info.handle),
1856 EGL_DMA_BUF_PLANE0_OFFSET_EXT, EGLint(surf->surface_image.offsets[1]),
1857 EGL_DMA_BUF_PLANE0_PITCH_EXT, EGLint(surf->surface_image.pitches[1]),
1861 surf->cbcr_egl_image = eglCreateImageKHR(eglGetCurrentDisplay(), EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, cbcr_attribs);
1862 assert(surf->cbcr_egl_image != EGL_NO_IMAGE_KHR);
1864 // Associate CbCr image to a texture.
1865 glBindTexture(GL_TEXTURE_2D, *cbcr_tex);
1866 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, surf->cbcr_egl_image);
1871 void H264Encoder::end_frame(RefCountedGLsync fence, const std::vector<RefCountedFrame> &input_frames)
1874 unique_lock<mutex> lock(frame_queue_mutex);
1875 pending_frames[current_storage_frame++] = PendingFrame{ fence, input_frames };
1877 frame_queue_nonempty.notify_one();
1880 void H264Encoder::copy_thread_func()
1884 encoding2display_order(current_frame_encoding, intra_period, intra_idr_period, ip_period,
1885 ¤t_frame_display, ¤t_frame_type);
1886 if (current_frame_type == FRAME_IDR) {
1888 current_frame_num = 0;
1889 current_IDR_display = current_frame_display;
1893 unique_lock<mutex> lock(frame_queue_mutex);
1894 frame_queue_nonempty.wait(lock, [this]{ return copy_thread_should_quit || pending_frames.count(current_frame_display) != 0; });
1895 if (copy_thread_should_quit) return;
1896 frame = pending_frames[current_frame_display];
1897 pending_frames.erase(current_frame_display);
1900 // Wait for the GPU to be done with the frame.
1901 glClientWaitSync(frame.fence.get(), 0, 0);
1903 // Release back any input frames we needed to render this frame.
1904 frame.input_frames.clear();
1907 GLSurface *surf = &gl_surfaces[current_frame_display % SURFACE_NUM];
1908 eglDestroyImageKHR(eglGetCurrentDisplay(), surf->y_egl_image);
1909 eglDestroyImageKHR(eglGetCurrentDisplay(), surf->cbcr_egl_image);
1910 VAStatus va_status = vaReleaseBufferHandle(va_dpy, surf->surface_image.buf);
1911 CHECK_VASTATUS(va_status, "vaReleaseBufferHandle");
1912 va_status = vaDestroyImage(va_dpy, surf->surface_image.image_id);
1913 CHECK_VASTATUS(va_status, "vaDestroyImage");
1915 VASurfaceID surface = surf->src_surface;
1917 // Schedule the frame for encoding.
1918 va_status = vaBeginPicture(va_dpy, context_id, surface);
1919 CHECK_VASTATUS(va_status, "vaBeginPicture");
1921 if (current_frame_type == FRAME_IDR) {
1924 if (h264_packedheader) {
1925 render_packedsequence();
1926 render_packedpicture();
1929 //render_sequence();
1934 va_status = vaEndPicture(va_dpy, context_id);
1935 CHECK_VASTATUS(va_status, "vaEndPicture");
1937 // so now the data is done encoding (well, async job kicked off)...
1938 // we send that to the storage thread
1939 storage_task_enqueue(current_frame_display, current_frame_encoding, current_frame_type);
1941 update_ReferenceFrames();
1942 ++current_frame_encoding;