2 * Copyright (c) 2007-2013 Intel Corporation. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #include <sys/types.h>
37 #include <va/va_enc_h264.h>
38 #include "va_display.h"
40 #define CHECK_VASTATUS(va_status,func) \
41 if (va_status != VA_STATUS_SUCCESS) { \
42 fprintf(stderr,"%s:%s (%d) failed,exit\n", __func__, func, __LINE__); \
46 #include "../loadsurface.h"
48 #define NAL_REF_IDC_NONE 0
49 #define NAL_REF_IDC_LOW 1
50 #define NAL_REF_IDC_MEDIUM 2
51 #define NAL_REF_IDC_HIGH 3
59 #define SLICE_TYPE_P 0
60 #define SLICE_TYPE_B 1
61 #define SLICE_TYPE_I 2
63 #define ENTROPY_MODE_CAVLC 0
64 #define ENTROPY_MODE_CABAC 1
66 #define PROFILE_IDC_BASELINE 66
67 #define PROFILE_IDC_MAIN 77
68 #define PROFILE_IDC_HIGH 100
70 #define BITSTREAM_ALLOCATE_STEPPING 4096
73 #define SURFACE_NUM 16 /* 16 surfaces for source YUV */
74 #define SURFACE_NUM 16 /* 16 surfaces for reference */
75 static VADisplay va_dpy;
76 static VAProfile h264_profile;
77 static VAConfigAttrib attrib[VAConfigAttribTypeMax];
78 static VAConfigAttrib config_attrib[VAConfigAttribTypeMax];
79 static int config_attrib_num = 0;
80 static VASurfaceID src_surface[SURFACE_NUM];
81 static VABufferID coded_buf[SURFACE_NUM];
82 static VASurfaceID ref_surface[SURFACE_NUM];
83 static VAConfigID config_id;
84 static VAContextID context_id;
85 static VAEncSequenceParameterBufferH264 seq_param;
86 static VAEncPictureParameterBufferH264 pic_param;
87 static VAEncSliceParameterBufferH264 slice_param;
88 static VAPictureH264 CurrentCurrPic;
89 static VAPictureH264 ReferenceFrames[16], RefPicList0_P[32], RefPicList0_B[32], RefPicList1_B[32];
91 static unsigned int MaxFrameNum = (2<<16);
92 static unsigned int MaxPicOrderCntLsb = (2<<8);
93 static unsigned int Log2MaxFrameNum = 16;
94 static unsigned int Log2MaxPicOrderCntLsb = 8;
96 static unsigned int num_ref_frames = 2;
97 static unsigned int numShortTerm = 0;
98 static int constraint_set_flag = 0;
99 static int h264_packedheader = 0; /* support pack header? */
100 static int h264_maxref = (1<<16|1);
101 static char *coded_fn = NULL, *srcyuv_fn = NULL, *recyuv_fn = NULL;
102 static FILE *coded_fp = NULL, *srcyuv_fp = NULL, *recyuv_fp = NULL;
103 static unsigned long long srcyuv_frames = 0;
104 static int srcyuv_fourcc = VA_FOURCC_NV12;
106 static int frame_width = 176;
107 static int frame_height = 144;
108 static int frame_rate = 30;
109 static unsigned int frame_count = 60;
110 static unsigned int frame_coded = 0;
111 static unsigned int frame_bitrate = 0;
112 static unsigned int frame_slices = 1;
113 static int initial_qp = 28;
114 static int minimal_qp = 0;
115 static int intra_period = 30;
116 static int intra_idr_period = 60;
117 static int ip_period = 1;
118 static int rc_mode = VA_RC_VBR;
119 static unsigned long long current_frame_encoding = 0;
120 static unsigned long long current_frame_display = 0;
121 static int current_frame_num = 0;
122 static int current_frame_type;
123 #define current_slot (current_frame_display % SURFACE_NUM)
125 /* thread to save coded data/upload source YUV */
126 struct storage_task_t {
128 unsigned long long display_order;
129 unsigned long long encode_order;
131 static struct storage_task_t *storage_task_header = NULL, *storage_task_tail = NULL;
132 #define SRC_SURFACE_IN_ENCODING 0
133 #define SRC_SURFACE_IN_STORAGE 1
134 static int srcsurface_status[SURFACE_NUM];
135 static int encode_syncmode = 0;
136 static pthread_mutex_t encode_mutex = PTHREAD_MUTEX_INITIALIZER;
137 static pthread_cond_t encode_cond = PTHREAD_COND_INITIALIZER;
138 static pthread_t encode_thread;
140 /* for performance profiling */
141 static unsigned int UploadPictureTicks=0;
142 static unsigned int BeginPictureTicks=0;
143 static unsigned int RenderPictureTicks=0;
144 static unsigned int EndPictureTicks=0;
145 static unsigned int SyncPictureTicks=0;
146 static unsigned int SavePictureTicks=0;
147 static unsigned int TotalTicks=0;
150 unsigned int *buffer;
152 int max_size_in_dword;
154 typedef struct __bitstream bitstream;
158 va_swap32(unsigned int val)
160 unsigned char *pval = (unsigned char *)&val;
162 return ((pval[0] << 24) |
169 bitstream_start(bitstream *bs)
171 bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
172 bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
177 bitstream_end(bitstream *bs)
179 int pos = (bs->bit_offset >> 5);
180 int bit_offset = (bs->bit_offset & 0x1f);
181 int bit_left = 32 - bit_offset;
184 bs->buffer[pos] = va_swap32((bs->buffer[pos] << bit_left));
189 bitstream_put_ui(bitstream *bs, unsigned int val, int size_in_bits)
191 int pos = (bs->bit_offset >> 5);
192 int bit_offset = (bs->bit_offset & 0x1f);
193 int bit_left = 32 - bit_offset;
198 bs->bit_offset += size_in_bits;
200 if (bit_left > size_in_bits) {
201 bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
203 size_in_bits -= bit_left;
204 bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
205 bs->buffer[pos] = va_swap32(bs->buffer[pos]);
207 if (pos + 1 == bs->max_size_in_dword) {
208 bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
209 bs->buffer = realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
212 bs->buffer[pos + 1] = val;
217 bitstream_put_ue(bitstream *bs, unsigned int val)
219 int size_in_bits = 0;
227 bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
228 bitstream_put_ui(bs, val, size_in_bits);
232 bitstream_put_se(bitstream *bs, int val)
234 unsigned int new_val;
239 new_val = 2 * val - 1;
241 bitstream_put_ue(bs, new_val);
245 bitstream_byte_aligning(bitstream *bs, int bit)
247 int bit_offset = (bs->bit_offset & 0x7);
248 int bit_left = 8 - bit_offset;
254 assert(bit == 0 || bit == 1);
257 new_val = (1 << bit_left) - 1;
261 bitstream_put_ui(bs, new_val, bit_left);
265 rbsp_trailing_bits(bitstream *bs)
267 bitstream_put_ui(bs, 1, 1);
268 bitstream_byte_aligning(bs, 0);
271 static void nal_start_code_prefix(bitstream *bs)
273 bitstream_put_ui(bs, 0x00000001, 32);
276 static void nal_header(bitstream *bs, int nal_ref_idc, int nal_unit_type)
278 bitstream_put_ui(bs, 0, 1); /* forbidden_zero_bit: 0 */
279 bitstream_put_ui(bs, nal_ref_idc, 2);
280 bitstream_put_ui(bs, nal_unit_type, 5);
283 static void sps_rbsp(bitstream *bs)
285 int profile_idc = PROFILE_IDC_BASELINE;
287 if (h264_profile == VAProfileH264High)
288 profile_idc = PROFILE_IDC_HIGH;
289 else if (h264_profile == VAProfileH264Main)
290 profile_idc = PROFILE_IDC_MAIN;
292 bitstream_put_ui(bs, profile_idc, 8); /* profile_idc */
293 bitstream_put_ui(bs, !!(constraint_set_flag & 1), 1); /* constraint_set0_flag */
294 bitstream_put_ui(bs, !!(constraint_set_flag & 2), 1); /* constraint_set1_flag */
295 bitstream_put_ui(bs, !!(constraint_set_flag & 4), 1); /* constraint_set2_flag */
296 bitstream_put_ui(bs, !!(constraint_set_flag & 8), 1); /* constraint_set3_flag */
297 bitstream_put_ui(bs, 0, 4); /* reserved_zero_4bits */
298 bitstream_put_ui(bs, seq_param.level_idc, 8); /* level_idc */
299 bitstream_put_ue(bs, seq_param.seq_parameter_set_id); /* seq_parameter_set_id */
301 if ( profile_idc == PROFILE_IDC_HIGH) {
302 bitstream_put_ue(bs, 1); /* chroma_format_idc = 1, 4:2:0 */
303 bitstream_put_ue(bs, 0); /* bit_depth_luma_minus8 */
304 bitstream_put_ue(bs, 0); /* bit_depth_chroma_minus8 */
305 bitstream_put_ui(bs, 0, 1); /* qpprime_y_zero_transform_bypass_flag */
306 bitstream_put_ui(bs, 0, 1); /* seq_scaling_matrix_present_flag */
309 bitstream_put_ue(bs, seq_param.seq_fields.bits.log2_max_frame_num_minus4); /* log2_max_frame_num_minus4 */
310 bitstream_put_ue(bs, seq_param.seq_fields.bits.pic_order_cnt_type); /* pic_order_cnt_type */
312 if (seq_param.seq_fields.bits.pic_order_cnt_type == 0)
313 bitstream_put_ue(bs, seq_param.seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4); /* log2_max_pic_order_cnt_lsb_minus4 */
318 bitstream_put_ue(bs, seq_param.max_num_ref_frames); /* num_ref_frames */
319 bitstream_put_ui(bs, 0, 1); /* gaps_in_frame_num_value_allowed_flag */
321 bitstream_put_ue(bs, seq_param.picture_width_in_mbs - 1); /* pic_width_in_mbs_minus1 */
322 bitstream_put_ue(bs, seq_param.picture_height_in_mbs - 1); /* pic_height_in_map_units_minus1 */
323 bitstream_put_ui(bs, seq_param.seq_fields.bits.frame_mbs_only_flag, 1); /* frame_mbs_only_flag */
325 if (!seq_param.seq_fields.bits.frame_mbs_only_flag) {
329 bitstream_put_ui(bs, seq_param.seq_fields.bits.direct_8x8_inference_flag, 1); /* direct_8x8_inference_flag */
330 bitstream_put_ui(bs, seq_param.frame_cropping_flag, 1); /* frame_cropping_flag */
332 if (seq_param.frame_cropping_flag) {
333 bitstream_put_ue(bs, seq_param.frame_crop_left_offset); /* frame_crop_left_offset */
334 bitstream_put_ue(bs, seq_param.frame_crop_right_offset); /* frame_crop_right_offset */
335 bitstream_put_ue(bs, seq_param.frame_crop_top_offset); /* frame_crop_top_offset */
336 bitstream_put_ue(bs, seq_param.frame_crop_bottom_offset); /* frame_crop_bottom_offset */
339 //if ( frame_bit_rate < 0 ) { //TODO EW: the vui header isn't correct
341 bitstream_put_ui(bs, 0, 1); /* vui_parameters_present_flag */
343 bitstream_put_ui(bs, 1, 1); /* vui_parameters_present_flag */
344 bitstream_put_ui(bs, 0, 1); /* aspect_ratio_info_present_flag */
345 bitstream_put_ui(bs, 0, 1); /* overscan_info_present_flag */
346 bitstream_put_ui(bs, 0, 1); /* video_signal_type_present_flag */
347 bitstream_put_ui(bs, 0, 1); /* chroma_loc_info_present_flag */
348 bitstream_put_ui(bs, 1, 1); /* timing_info_present_flag */
350 bitstream_put_ui(bs, 15, 32);
351 bitstream_put_ui(bs, 900, 32);
352 bitstream_put_ui(bs, 1, 1);
354 bitstream_put_ui(bs, 1, 1); /* nal_hrd_parameters_present_flag */
357 bitstream_put_ue(bs, 0); /* cpb_cnt_minus1 */
358 bitstream_put_ui(bs, 4, 4); /* bit_rate_scale */
359 bitstream_put_ui(bs, 6, 4); /* cpb_size_scale */
361 bitstream_put_ue(bs, frame_bitrate - 1); /* bit_rate_value_minus1[0] */
362 bitstream_put_ue(bs, frame_bitrate*8 - 1); /* cpb_size_value_minus1[0] */
363 bitstream_put_ui(bs, 1, 1); /* cbr_flag[0] */
365 bitstream_put_ui(bs, 23, 5); /* initial_cpb_removal_delay_length_minus1 */
366 bitstream_put_ui(bs, 23, 5); /* cpb_removal_delay_length_minus1 */
367 bitstream_put_ui(bs, 23, 5); /* dpb_output_delay_length_minus1 */
368 bitstream_put_ui(bs, 23, 5); /* time_offset_length */
370 bitstream_put_ui(bs, 0, 1); /* vcl_hrd_parameters_present_flag */
371 bitstream_put_ui(bs, 0, 1); /* low_delay_hrd_flag */
373 bitstream_put_ui(bs, 0, 1); /* pic_struct_present_flag */
374 bitstream_put_ui(bs, 0, 1); /* bitstream_restriction_flag */
377 rbsp_trailing_bits(bs); /* rbsp_trailing_bits */
381 static void pps_rbsp(bitstream *bs)
383 bitstream_put_ue(bs, pic_param.pic_parameter_set_id); /* pic_parameter_set_id */
384 bitstream_put_ue(bs, pic_param.seq_parameter_set_id); /* seq_parameter_set_id */
386 bitstream_put_ui(bs, pic_param.pic_fields.bits.entropy_coding_mode_flag, 1); /* entropy_coding_mode_flag */
388 bitstream_put_ui(bs, 0, 1); /* pic_order_present_flag: 0 */
390 bitstream_put_ue(bs, 0); /* num_slice_groups_minus1 */
392 bitstream_put_ue(bs, pic_param.num_ref_idx_l0_active_minus1); /* num_ref_idx_l0_active_minus1 */
393 bitstream_put_ue(bs, pic_param.num_ref_idx_l1_active_minus1); /* num_ref_idx_l1_active_minus1 1 */
395 bitstream_put_ui(bs, pic_param.pic_fields.bits.weighted_pred_flag, 1); /* weighted_pred_flag: 0 */
396 bitstream_put_ui(bs, pic_param.pic_fields.bits.weighted_bipred_idc, 2); /* weighted_bipred_idc: 0 */
398 bitstream_put_se(bs, pic_param.pic_init_qp - 26); /* pic_init_qp_minus26 */
399 bitstream_put_se(bs, 0); /* pic_init_qs_minus26 */
400 bitstream_put_se(bs, 0); /* chroma_qp_index_offset */
402 bitstream_put_ui(bs, pic_param.pic_fields.bits.deblocking_filter_control_present_flag, 1); /* deblocking_filter_control_present_flag */
403 bitstream_put_ui(bs, 0, 1); /* constrained_intra_pred_flag */
404 bitstream_put_ui(bs, 0, 1); /* redundant_pic_cnt_present_flag */
407 bitstream_put_ui(bs, pic_param.pic_fields.bits.transform_8x8_mode_flag, 1); /*transform_8x8_mode_flag */
408 bitstream_put_ui(bs, 0, 1); /* pic_scaling_matrix_present_flag */
409 bitstream_put_se(bs, pic_param.second_chroma_qp_index_offset ); /*second_chroma_qp_index_offset */
411 rbsp_trailing_bits(bs);
416 build_packed_pic_buffer(unsigned char **header_buffer)
420 bitstream_start(&bs);
421 nal_start_code_prefix(&bs);
422 nal_header(&bs, NAL_REF_IDC_HIGH, NAL_PPS);
426 *header_buffer = (unsigned char *)bs.buffer;
427 return bs.bit_offset;
431 build_packed_seq_buffer(unsigned char **header_buffer)
435 bitstream_start(&bs);
436 nal_start_code_prefix(&bs);
437 nal_header(&bs, NAL_REF_IDC_HIGH, NAL_SPS);
441 *header_buffer = (unsigned char *)bs.buffer;
442 return bs.bit_offset;
446 build_packed_sei_buffer_timing(unsigned int init_cpb_removal_length,
447 unsigned int init_cpb_removal_delay,
448 unsigned int init_cpb_removal_delay_offset,
449 unsigned int cpb_removal_length,
450 unsigned int cpb_removal_delay,
451 unsigned int dpb_output_length,
452 unsigned int dpb_output_delay,
453 unsigned char **sei_buffer)
455 unsigned char *byte_buf;
456 int bp_byte_size, i, pic_byte_size;
459 bitstream sei_bp_bs, sei_pic_bs;
461 bitstream_start(&sei_bp_bs);
462 bitstream_put_ue(&sei_bp_bs, 0); /*seq_parameter_set_id*/
463 bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay, cpb_removal_length);
464 bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay_offset, cpb_removal_length);
465 if ( sei_bp_bs.bit_offset & 0x7) {
466 bitstream_put_ui(&sei_bp_bs, 1, 1);
468 bitstream_end(&sei_bp_bs);
469 bp_byte_size = (sei_bp_bs.bit_offset + 7) / 8;
471 bitstream_start(&sei_pic_bs);
472 bitstream_put_ui(&sei_pic_bs, cpb_removal_delay, cpb_removal_length);
473 bitstream_put_ui(&sei_pic_bs, dpb_output_delay, dpb_output_length);
474 if ( sei_pic_bs.bit_offset & 0x7) {
475 bitstream_put_ui(&sei_pic_bs, 1, 1);
477 bitstream_end(&sei_pic_bs);
478 pic_byte_size = (sei_pic_bs.bit_offset + 7) / 8;
480 bitstream_start(&nal_bs);
481 nal_start_code_prefix(&nal_bs);
482 nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
484 /* Write the SEI buffer period data */
485 bitstream_put_ui(&nal_bs, 0, 8);
486 bitstream_put_ui(&nal_bs, bp_byte_size, 8);
488 byte_buf = (unsigned char *)sei_bp_bs.buffer;
489 for(i = 0; i < bp_byte_size; i++) {
490 bitstream_put_ui(&nal_bs, byte_buf[i], 8);
493 /* write the SEI timing data */
494 bitstream_put_ui(&nal_bs, 0x01, 8);
495 bitstream_put_ui(&nal_bs, pic_byte_size, 8);
497 byte_buf = (unsigned char *)sei_pic_bs.buffer;
498 for(i = 0; i < pic_byte_size; i++) {
499 bitstream_put_ui(&nal_bs, byte_buf[i], 8);
503 rbsp_trailing_bits(&nal_bs);
504 bitstream_end(&nal_bs);
506 *sei_buffer = (unsigned char *)nal_bs.buffer;
508 return nal_bs.bit_offset;
514 * Helper function for profiling purposes
516 static unsigned int GetTickCount()
519 if (gettimeofday(&tv, NULL))
521 return tv.tv_usec/1000+tv.tv_sec*1000;
525 Assume frame sequence is: Frame#0,#1,#2,...,#M,...,#X,... (encoding order)
526 1) period between Frame #X and Frame #N = #X - #N
527 2) 0 means infinite for intra_period/intra_idr_period, and 0 is invalid for ip_period
528 3) intra_idr_period % intra_period (intra_period > 0) and intra_period % ip_period must be 0
529 4) intra_period and intra_idr_period take precedence over ip_period
530 5) if ip_period > 1, intra_period and intra_idr_period are not the strict periods
531 of I/IDR frames, see bellow examples
532 -------------------------------------------------------------------
533 intra_period intra_idr_period ip_period frame sequence (intra_period/intra_idr_period/ip_period)
534 0 ignored 1 IDRPPPPPPP ... (No IDR/I any more)
535 0 ignored >=2 IDR(PBB)(PBB)... (No IDR/I any more)
536 1 0 ignored IDRIIIIIII... (No IDR any more)
537 1 1 ignored IDR IDR IDR IDR...
538 1 >=2 ignored IDRII IDRII IDR... (1/3/ignore)
539 >=2 0 1 IDRPPP IPPP I... (3/0/1)
540 >=2 0 >=2 IDR(PBB)(PBB)(IBB) (6/0/3)
541 (PBB)(IBB)(PBB)(IBB)...
542 >=2 >=2 1 IDRPPPPP IPPPPP IPPPPP (6/18/1)
543 IDRPPPPP IPPPPP IPPPPP...
544 >=2 >=2 >=2 {IDR(PBB)(PBB)(IBB)(PBB)(IBB)(PBB)} (6/18/3)
545 {IDR(PBB)(PBB)(IBB)(PBB)(IBB)(PBB)}...
546 {IDR(PBB)(PBB)(IBB)(PBB)} (6/12/3)
547 {IDR(PBB)(PBB)(IBB)(PBB)}...
548 {IDR(PBB)(PBB)} (6/6/3)
553 * Return displaying order with specified periods and encoding order
554 * displaying_order: displaying order
555 * frame_type: frame type
561 void encoding2display_order(
562 unsigned long long encoding_order,int intra_period,
563 int intra_idr_period,int ip_period,
564 unsigned long long *displaying_order,
567 int encoding_order_gop = 0;
569 if (intra_period == 1) { /* all are I/IDR frames */
570 *displaying_order = encoding_order;
571 if (intra_idr_period == 0)
572 *frame_type = (encoding_order == 0)?FRAME_IDR:FRAME_I;
574 *frame_type = (encoding_order % intra_idr_period == 0)?FRAME_IDR:FRAME_I;
578 if (intra_period == 0)
579 intra_idr_period = 0;
583 * IDR (PBB)(PBB)(IBB)(PBB)
585 encoding_order_gop = (intra_idr_period == 0)? encoding_order:
586 (encoding_order % (intra_idr_period + ((ip_period == 1)?0:1)));
588 if (encoding_order_gop == 0) { /* the first frame */
589 *frame_type = FRAME_IDR;
590 *displaying_order = encoding_order;
591 } else if (((encoding_order_gop - 1) % ip_period) != 0) { /* B frames */
592 *frame_type = FRAME_B;
593 *displaying_order = encoding_order - 1;
594 } else if ((intra_period != 0) && /* have I frames */
595 (encoding_order_gop >= 2) &&
596 ((ip_period == 1 && encoding_order_gop % intra_period == 0) || /* for IDR PPPPP IPPPP */
597 /* for IDR (PBB)(PBB)(IBB) */
598 (ip_period >= 2 && ((encoding_order_gop - 1) / ip_period % (intra_period / ip_period)) == 0))) {
599 *frame_type = FRAME_I;
600 *displaying_order = encoding_order + ip_period - 1;
602 *frame_type = FRAME_P;
603 *displaying_order = encoding_order + ip_period - 1;
608 static char *fourcc_to_string(int fourcc)
624 static int string_to_fourcc(char *str)
628 if (!strncmp(str, "NV12", 4))
629 fourcc = VA_FOURCC_NV12;
630 else if (!strncmp(str, "IYUV", 4))
631 fourcc = VA_FOURCC_IYUV;
632 else if (!strncmp(str, "YV12", 4))
633 fourcc = VA_FOURCC_YV12;
634 else if (!strncmp(str, "UYVY", 4))
635 fourcc = VA_FOURCC_UYVY;
637 printf("Unknow FOURCC\n");
644 static char *rc_to_string(int rcmode)
657 case VA_RC_VBR_CONSTRAINED:
658 return "VBR_CONSTRAINED";
664 static int string_to_rc(char *str)
668 if (!strncmp(str, "NONE", 4))
669 rc_mode = VA_RC_NONE;
670 else if (!strncmp(str, "CBR", 3))
672 else if (!strncmp(str, "VBR", 3))
674 else if (!strncmp(str, "VCM", 3))
676 else if (!strncmp(str, "CQP", 3))
678 else if (!strncmp(str, "VBR_CONSTRAINED", 15))
679 rc_mode = VA_RC_VBR_CONSTRAINED;
681 printf("Unknown RC mode\n");
688 static int print_help(void)
690 printf("./h264encode <options>\n");
691 printf(" -w <width> -h <height>\n");
692 printf(" -n <frame number>\n");
693 printf(" -o <coded file>\n");
694 printf(" -f <frame rate>\n");
695 printf(" --intra_period <number>\n");
696 printf(" --idr_period <number>\n");
697 printf(" --ip_period <number>\n");
698 printf(" --bitrate <bitrate>\n");
699 printf(" --initialqp <number>\n");
700 printf(" --minqp <number>\n");
701 printf(" --rcmode <NONE|CBR|VBR|VCM|CQP|VBR_CONTRAINED>\n");
702 printf(" --syncmode: sequentially upload source, encoding, save result, no multi-thread\n");
703 printf(" --srcyuv <filename> load YUV from a file\n");
704 printf(" --fourcc <NV12|IYUV|I420|YV12> source YUV fourcc\n");
709 static int process_cmdline(int argc, char *argv[])
712 const struct option long_opts[] = {
713 {"bitrate", required_argument, NULL, 1 },
714 {"minqp", required_argument, NULL, 2 },
715 {"initialqp", required_argument, NULL, 3 },
716 {"intra_period", required_argument, NULL, 4 },
717 {"idr_period", required_argument, NULL, 5 },
718 {"ip_period", required_argument, NULL, 6 },
719 {"rcmode", required_argument, NULL, 7 },
720 {"srcyuv", required_argument, NULL, 9 },
721 {"fourcc", required_argument, NULL, 10 },
722 {"syncmode", no_argument, NULL, 11 },
723 {NULL, no_argument, NULL, 0 }};
726 while ((c =getopt_long_only(argc,argv,"w:h:n:f:o:?",long_opts,&long_index)) != EOF) {
729 frame_width = atoi(optarg);
732 frame_height = atoi(optarg);
735 frame_count = atoi(optarg);
738 frame_rate = atoi(optarg);
741 coded_fn = strdup(optarg);
744 frame_bitrate = atoi(optarg);
747 minimal_qp = atoi(optarg);
750 initial_qp = atoi(optarg);
753 intra_period = atoi(optarg);
756 intra_idr_period = atoi(optarg);
759 ip_period = atoi(optarg);
762 rc_mode = string_to_rc(optarg);
769 srcyuv_fn = strdup(optarg);
772 srcyuv_fourcc = string_to_fourcc(optarg);
773 if (srcyuv_fourcc <= 0) {
789 printf(" ip_period must be greater than 0\n");
792 if (intra_period != 1 && intra_period % ip_period != 0) {
793 printf(" intra_period must be a multiplier of ip_period\n");
796 if (intra_period != 0 && intra_idr_period % intra_period != 0) {
797 printf(" intra_idr_period must be a multiplier of intra_period\n");
801 if (frame_bitrate == 0)
802 frame_bitrate = frame_width * frame_height * 12 * frame_rate / 50;
804 /* open source file */
806 srcyuv_fp = fopen(srcyuv_fn,"r");
808 if (srcyuv_fp == NULL)
809 printf("Open source YUV file %s failed, use auto-generated YUV data\n", srcyuv_fn);
811 fseek(srcyuv_fp, 0L, SEEK_END);
812 srcyuv_frames = ftell(srcyuv_fp) / (frame_width * frame_height * 1.5);
813 printf("Source YUV file %s with %llu frames\n", srcyuv_fn, srcyuv_frames);
817 if (coded_fn == NULL) {
819 if (stat("/tmp", &buf) == 0)
820 coded_fn = strdup("/tmp/test.264");
821 else if (stat("/sdcard", &buf) == 0)
822 coded_fn = strdup("/sdcard/test.264");
824 coded_fn = strdup("./test.264");
827 /* store coded data into a file */
828 coded_fp = fopen(coded_fn,"w+");
829 if (coded_fp == NULL) {
830 printf("Open file %s failed, exit\n", coded_fn);
838 static int init_va(void)
840 VAProfile profile_list[]={VAProfileH264High,VAProfileH264Main,VAProfileH264Baseline,VAProfileH264ConstrainedBaseline};
841 VAEntrypoint entrypoints[VAEntrypointMax]={0};
842 int num_entrypoints,slice_entrypoint;
843 int support_encode = 0;
844 int major_ver, minor_ver;
848 va_dpy = va_open_display();
849 va_status = vaInitialize(va_dpy, &major_ver, &minor_ver);
850 CHECK_VASTATUS(va_status, "vaInitialize");
852 /* use the highest profile */
853 for (i = 0; i < sizeof(profile_list)/sizeof(profile_list[0]); i++) {
854 h264_profile = profile_list[i];
855 vaQueryConfigEntrypoints(va_dpy, h264_profile, entrypoints, &num_entrypoints);
856 for (slice_entrypoint = 0; slice_entrypoint < num_entrypoints; slice_entrypoint++) {
857 if (entrypoints[slice_entrypoint] == VAEntrypointEncSlice) {
862 if (support_encode == 1)
866 if (support_encode == 0) {
867 printf("Can't find VAEntrypointEncSlice for H264 profiles\n");
870 switch (h264_profile) {
871 case VAProfileH264Baseline:
872 printf("Use profile VAProfileH264Baseline\n");
874 constraint_set_flag |= (1 << 0); /* Annex A.2.1 */
876 case VAProfileH264ConstrainedBaseline:
877 printf("Use profile VAProfileH264ConstrainedBaseline\n");
878 constraint_set_flag |= (1 << 0 | 1 << 1); /* Annex A.2.2 */
882 case VAProfileH264Main:
883 printf("Use profile VAProfileH264Main\n");
884 constraint_set_flag |= (1 << 1); /* Annex A.2.2 */
887 case VAProfileH264High:
888 constraint_set_flag |= (1 << 3); /* Annex A.2.4 */
889 printf("Use profile VAProfileH264High\n");
892 printf("unknow profile. Set to Baseline");
893 h264_profile = VAProfileH264Baseline;
895 constraint_set_flag |= (1 << 0); /* Annex A.2.1 */
900 /* find out the format for the render target, and rate control mode */
901 for (i = 0; i < VAConfigAttribTypeMax; i++)
904 va_status = vaGetConfigAttributes(va_dpy, h264_profile, VAEntrypointEncSlice,
905 &attrib[0], VAConfigAttribTypeMax);
906 CHECK_VASTATUS(va_status, "vaGetConfigAttributes");
907 /* check the interested configattrib */
908 if ((attrib[VAConfigAttribRTFormat].value & VA_RT_FORMAT_YUV420) == 0) {
909 printf("Not find desired YUV420 RT format\n");
912 config_attrib[config_attrib_num].type = VAConfigAttribRTFormat;
913 config_attrib[config_attrib_num].value = VA_RT_FORMAT_YUV420;
917 if (attrib[VAConfigAttribRateControl].value != VA_ATTRIB_NOT_SUPPORTED) {
918 int tmp = attrib[VAConfigAttribRateControl].value;
920 printf("Supported rate control mode (0x%x):", tmp);
922 if (tmp & VA_RC_NONE)
932 if (tmp & VA_RC_VBR_CONSTRAINED)
933 printf("VBR_CONSTRAINED ");
937 /* need to check if support rc_mode */
938 config_attrib[config_attrib_num].type = VAConfigAttribRateControl;
939 config_attrib[config_attrib_num].value = rc_mode;
944 if (attrib[VAConfigAttribEncPackedHeaders].value != VA_ATTRIB_NOT_SUPPORTED) {
945 int tmp = attrib[VAConfigAttribEncPackedHeaders].value;
947 printf("Support VAConfigAttribEncPackedHeaders\n");
949 h264_packedheader = 1;
950 config_attrib[config_attrib_num].type = VAConfigAttribEncPackedHeaders;
951 config_attrib[config_attrib_num].value = VA_ENC_PACKED_HEADER_NONE;
953 if (tmp & VA_ENC_PACKED_HEADER_SEQUENCE) {
954 printf("Support packed sequence headers\n");
955 config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_SEQUENCE;
958 if (tmp & VA_ENC_PACKED_HEADER_PICTURE) {
959 printf("Support packed picture headers\n");
960 config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_PICTURE;
963 if (tmp & VA_ENC_PACKED_HEADER_SLICE) {
964 printf("Support packed slice headers\n");
965 config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_SLICE;
968 if (tmp & VA_ENC_PACKED_HEADER_MISC) {
969 printf("Support packed misc headers\n");
970 config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_MISC;
976 if (attrib[VAConfigAttribEncInterlaced].value != VA_ATTRIB_NOT_SUPPORTED) {
977 int tmp = attrib[VAConfigAttribEncInterlaced].value;
979 printf("Support VAConfigAttribEncInterlaced\n");
981 if (tmp & VA_ENC_INTERLACED_FRAME)
982 printf("support VA_ENC_INTERLACED_FRAME\n");
983 if (tmp & VA_ENC_INTERLACED_FIELD)
984 printf("Support VA_ENC_INTERLACED_FIELD\n");
985 if (tmp & VA_ENC_INTERLACED_MBAFF)
986 printf("Support VA_ENC_INTERLACED_MBAFF\n");
987 if (tmp & VA_ENC_INTERLACED_PAFF)
988 printf("Support VA_ENC_INTERLACED_PAFF\n");
990 config_attrib[config_attrib_num].type = VAConfigAttribEncInterlaced;
991 config_attrib[config_attrib_num].value = VA_ENC_PACKED_HEADER_NONE;
995 if (attrib[VAConfigAttribEncMaxRefFrames].value != VA_ATTRIB_NOT_SUPPORTED) {
996 h264_maxref = attrib[VAConfigAttribEncMaxRefFrames].value;
998 printf("Support %d reference frames\n", h264_maxref);
1001 if (attrib[VAConfigAttribEncMaxSlices].value != VA_ATTRIB_NOT_SUPPORTED)
1002 printf("Support %d slices\n", attrib[VAConfigAttribEncMaxSlices].value);
1004 if (attrib[VAConfigAttribEncSliceStructure].value != VA_ATTRIB_NOT_SUPPORTED) {
1005 int tmp = attrib[VAConfigAttribEncSliceStructure].value;
1007 printf("Support VAConfigAttribEncSliceStructure\n");
1009 if (tmp & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS)
1010 printf("Support VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS\n");
1011 if (tmp & VA_ENC_SLICE_STRUCTURE_POWER_OF_TWO_ROWS)
1012 printf("Support VA_ENC_SLICE_STRUCTURE_POWER_OF_TWO_ROWS\n");
1013 if (tmp & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS)
1014 printf("Support VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS\n");
1016 if (attrib[VAConfigAttribEncMacroblockInfo].value != VA_ATTRIB_NOT_SUPPORTED) {
1017 printf("Support VAConfigAttribEncMacroblockInfo\n");
1023 static int setup_encode()
1026 VASurfaceID *tmp_surfaceid;
1027 int codedbuf_size, i;
1029 va_status = vaCreateConfig(va_dpy, h264_profile, VAEntrypointEncSlice,
1030 &config_attrib[0], config_attrib_num, &config_id);
1031 CHECK_VASTATUS(va_status, "vaCreateConfig");
1033 /* create source surfaces */
1034 va_status = vaCreateSurfaces(va_dpy,
1035 VA_RT_FORMAT_YUV420, frame_width, frame_height,
1036 &src_surface[0], SURFACE_NUM,
1038 CHECK_VASTATUS(va_status, "vaCreateSurfaces");
1040 /* create reference surfaces */
1041 va_status = vaCreateSurfaces(
1043 VA_RT_FORMAT_YUV420, frame_width, frame_height,
1044 &ref_surface[0], SURFACE_NUM,
1047 CHECK_VASTATUS(va_status, "vaCreateSurfaces");
1049 tmp_surfaceid = calloc(2 * SURFACE_NUM, sizeof(VASurfaceID));
1050 memcpy(tmp_surfaceid, src_surface, SURFACE_NUM * sizeof(VASurfaceID));
1051 memcpy(tmp_surfaceid + SURFACE_NUM, ref_surface, SURFACE_NUM * sizeof(VASurfaceID));
1053 /* Create a context for this encode pipe */
1054 va_status = vaCreateContext(va_dpy, config_id,
1055 frame_width, frame_height,
1057 tmp_surfaceid, 2 * SURFACE_NUM,
1059 CHECK_VASTATUS(va_status, "vaCreateContext");
1060 free(tmp_surfaceid);
1062 codedbuf_size = (frame_width * frame_height * 400) / (16*16);
1064 for (i = 0; i < SURFACE_NUM; i++) {
1065 /* create coded buffer once for all
1066 * other VA buffers which won't be used again after vaRenderPicture.
1067 * so APP can always vaCreateBuffer for every frame
1068 * but coded buffer need to be mapped and accessed after vaRenderPicture/vaEndPicture
1069 * so VA won't maintain the coded buffer
1071 va_status = vaCreateBuffer(va_dpy,context_id,VAEncCodedBufferType,
1072 codedbuf_size, 1, NULL, &coded_buf[i]);
1073 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1081 #define partition(ref, field, key, ascending) \
1084 while (ref[i].field < key) \
1086 while (ref[j].field > key) \
1089 while (ref[i].field > key) \
1091 while (ref[j].field < key) \
1103 static void sort_one(VAPictureH264 ref[], int left, int right,
1104 int ascending, int frame_idx)
1106 int i = left, j = right;
1111 key = ref[(left + right) / 2].frame_idx;
1112 partition(ref, frame_idx, key, ascending);
1114 key = ref[(left + right) / 2].TopFieldOrderCnt;
1115 partition(ref, TopFieldOrderCnt, key, ascending);
1120 sort_one(ref, left, j, ascending, frame_idx);
1123 sort_one(ref, i, right, ascending, frame_idx);
1126 static void sort_two(VAPictureH264 ref[], int left, int right, int key, int frame_idx,
1127 int divide_ascending, int list0_ascending, int list1_ascending)
1129 int i = left, j = right;
1133 partition(ref, frame_idx, key, divide_ascending);
1135 partition(ref, TopFieldOrderCnt, key, divide_ascending);
1139 sort_one(ref, left, i-1, list0_ascending, frame_idx);
1140 sort_one(ref, j+1, right, list1_ascending, frame_idx);
1143 static int update_ReferenceFrames(void)
1147 if (current_frame_type == FRAME_B)
1150 CurrentCurrPic.flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
1152 if (numShortTerm > num_ref_frames)
1153 numShortTerm = num_ref_frames;
1154 for (i=numShortTerm-1; i>0; i--)
1155 ReferenceFrames[i] = ReferenceFrames[i-1];
1156 ReferenceFrames[0] = CurrentCurrPic;
1158 if (current_frame_type != FRAME_B)
1159 current_frame_num++;
1160 if (current_frame_num > MaxFrameNum)
1161 current_frame_num = 0;
1166 static int update_RefPicList(void)
1168 unsigned int current_poc = CurrentCurrPic.TopFieldOrderCnt;
1170 if (current_frame_type == FRAME_IDR) {
1172 current_frame_num = 0;
1176 if (current_frame_type == FRAME_P) {
1177 memcpy(RefPicList0_P, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
1178 sort_one(RefPicList0_P, 0, numShortTerm-1, 0, 1);
1181 if (current_frame_type == FRAME_B) {
1182 memcpy(RefPicList0_B, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
1183 sort_two(RefPicList0_B, 0, numShortTerm-1, current_poc, 0,
1186 memcpy(RefPicList1_B, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
1187 sort_two(RefPicList1_B, 0, numShortTerm-1, current_poc, 0,
1195 static int render_sequence(void)
1197 VABufferID seq_param_buf, rc_param_buf, render_id[2];
1199 VAEncMiscParameterBuffer *misc_param;
1200 VAEncMiscParameterRateControl *misc_rate_ctrl;
1202 seq_param.level_idc = 41 /*SH_LEVEL_3*/;
1203 seq_param.picture_width_in_mbs = frame_width / 16;
1204 seq_param.picture_height_in_mbs = frame_height / 16;
1205 seq_param.bits_per_second = frame_bitrate;
1207 seq_param.intra_period = intra_period;
1208 seq_param.intra_idr_period = intra_idr_period;
1209 seq_param.ip_period = ip_period;
1211 seq_param.max_num_ref_frames = num_ref_frames;
1212 seq_param.seq_fields.bits.frame_mbs_only_flag = 1;
1213 seq_param.time_scale = 900;
1214 seq_param.num_units_in_tick = 15; /* Tc = num_units_in_tick / time_sacle */
1215 seq_param.seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 = Log2MaxPicOrderCntLsb - 4;
1216 seq_param.seq_fields.bits.log2_max_frame_num_minus4 = Log2MaxFrameNum - 4;;
1217 seq_param.seq_fields.bits.frame_mbs_only_flag = 1;
1219 va_status = vaCreateBuffer(va_dpy, context_id,
1220 VAEncSequenceParameterBufferType,
1221 sizeof(seq_param),1,&seq_param,&seq_param_buf);
1222 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1224 va_status = vaCreateBuffer(va_dpy, context_id,
1225 VAEncMiscParameterBufferType,
1226 sizeof(VAEncMiscParameterBuffer) + sizeof(VAEncMiscParameterRateControl),
1227 1,NULL,&rc_param_buf);
1228 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1230 vaMapBuffer(va_dpy, rc_param_buf,(void **)&misc_param);
1231 misc_param->type = VAEncMiscParameterTypeRateControl;
1232 misc_rate_ctrl = (VAEncMiscParameterRateControl *)misc_param->data;
1233 memset(misc_rate_ctrl, 0, sizeof(*misc_rate_ctrl));
1234 misc_rate_ctrl->bits_per_second = frame_bitrate;
1235 misc_rate_ctrl->initial_qp = initial_qp;
1236 misc_rate_ctrl->min_qp = minimal_qp;
1237 misc_rate_ctrl->basic_unit_size = 0;
1238 vaUnmapBuffer(va_dpy, rc_param_buf);
1240 render_id[0] = seq_param_buf;
1241 render_id[1] = rc_param_buf;
1243 va_status = vaRenderPicture(va_dpy,context_id, &render_id[0], 2);
1244 CHECK_VASTATUS(va_status,"vaRenderPicture");;
1249 static unsigned int calc_poc(unsigned int pic_order_cnt_lsb)
1251 static unsigned int prevPicOrderCntMsb = 0, prevPicOrderCntLsb = 0;
1252 static unsigned int PicOrderCntMsb = 0;
1253 unsigned int TopFieldOrderCnt;
1255 if (current_frame_type == FRAME_IDR)
1256 prevPicOrderCntMsb = prevPicOrderCntLsb = 0;
1258 prevPicOrderCntMsb = PicOrderCntMsb;
1259 if ((pic_order_cnt_lsb < prevPicOrderCntLsb) &&
1260 ((prevPicOrderCntLsb - pic_order_cnt_lsb) >= (MaxPicOrderCntLsb / 2)))
1261 PicOrderCntMsb = prevPicOrderCntMsb + MaxPicOrderCntLsb;
1262 else if ((pic_order_cnt_lsb > prevPicOrderCntLsb) &&
1263 ((pic_order_cnt_lsb - prevPicOrderCntLsb) > (MaxPicOrderCntLsb / 2)))
1264 PicOrderCntMsb = prevPicOrderCntMsb - MaxPicOrderCntLsb;
1266 PicOrderCntMsb = prevPicOrderCntMsb;
1268 TopFieldOrderCnt = PicOrderCntMsb + pic_order_cnt_lsb;
1270 return TopFieldOrderCnt;
1273 static int render_picture(void)
1275 VABufferID pic_param_buf;
1279 pic_param.CurrPic.picture_id = ref_surface[current_slot];
1280 pic_param.CurrPic.frame_idx = current_frame_num;
1281 pic_param.CurrPic.flags = 0;
1282 pic_param.CurrPic.TopFieldOrderCnt = calc_poc(current_frame_display % MaxPicOrderCntLsb);
1283 pic_param.CurrPic.BottomFieldOrderCnt = pic_param.CurrPic.TopFieldOrderCnt;
1284 CurrentCurrPic = pic_param.CurrPic;
1286 if (getenv("TO_DEL")) { /* set RefPicList into ReferenceFrames */
1287 update_RefPicList(); /* calc RefPicList */
1288 memset(pic_param.ReferenceFrames, 0xff, 16 * sizeof(VAPictureH264)); /* invalid all */
1289 if (current_frame_type == FRAME_P) {
1290 pic_param.ReferenceFrames[0] = RefPicList0_P[0];
1291 } else if (current_frame_type == FRAME_B) {
1292 pic_param.ReferenceFrames[0] = RefPicList0_B[0];
1293 pic_param.ReferenceFrames[1] = RefPicList1_B[0];
1296 memcpy(pic_param.ReferenceFrames, ReferenceFrames, numShortTerm*sizeof(VAPictureH264));
1297 for (i = numShortTerm; i < SURFACE_NUM; i++) {
1298 pic_param.ReferenceFrames[i].picture_id = VA_INVALID_SURFACE;
1299 pic_param.ReferenceFrames[i].flags = VA_PICTURE_H264_INVALID;
1303 pic_param.pic_fields.bits.idr_pic_flag = (current_frame_type == FRAME_IDR);
1304 pic_param.pic_fields.bits.reference_pic_flag = (current_frame_type != FRAME_B);
1305 pic_param.pic_fields.bits.entropy_coding_mode_flag = 1;
1306 pic_param.pic_fields.bits.deblocking_filter_control_present_flag = 1;
1307 pic_param.frame_num = current_frame_num;
1308 pic_param.coded_buf = coded_buf[current_slot];
1309 pic_param.last_picture = (current_frame_encoding == frame_count);
1310 pic_param.pic_init_qp = initial_qp;
1312 va_status = vaCreateBuffer(va_dpy, context_id,VAEncPictureParameterBufferType,
1313 sizeof(pic_param),1,&pic_param, &pic_param_buf);
1314 CHECK_VASTATUS(va_status,"vaCreateBuffer");;
1316 va_status = vaRenderPicture(va_dpy,context_id, &pic_param_buf, 1);
1317 CHECK_VASTATUS(va_status,"vaRenderPicture");
1322 static int render_packedsequence(void)
1324 VAEncPackedHeaderParameterBuffer packedheader_param_buffer={0};
1325 VABufferID packedseq_para_bufid, packedseq_data_bufid, render_id[2];
1326 unsigned int length_in_bits;
1327 unsigned char *packedseq_buffer = NULL;
1330 length_in_bits = build_packed_seq_buffer(&packedseq_buffer);
1332 packedheader_param_buffer.type = VAEncPackedHeaderSequence;
1334 packedheader_param_buffer.bit_length = length_in_bits; /*length_in_bits*/
1335 packedheader_param_buffer.has_emulation_bytes = 0;
1336 va_status = vaCreateBuffer(va_dpy,
1338 VAEncPackedHeaderParameterBufferType,
1339 sizeof(packedheader_param_buffer), 1, &packedheader_param_buffer,
1340 &packedseq_para_bufid);
1341 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1343 va_status = vaCreateBuffer(va_dpy,
1345 VAEncPackedHeaderDataBufferType,
1346 (length_in_bits + 7) / 8, 1, packedseq_buffer,
1347 &packedseq_data_bufid);
1348 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1350 render_id[0] = packedseq_para_bufid;
1351 render_id[1] = packedseq_data_bufid;
1352 va_status = vaRenderPicture(va_dpy,context_id, render_id, 2);
1353 CHECK_VASTATUS(va_status,"vaRenderPicture");
1355 free(packedseq_buffer);
1361 static int render_packedpicture(void)
1363 VAEncPackedHeaderParameterBuffer packedheader_param_buffer={0};
1364 VABufferID packedpic_para_bufid, packedpic_data_bufid, render_id[2];
1365 unsigned int length_in_bits;
1366 unsigned char *packedpic_buffer = NULL;
1369 length_in_bits = build_packed_pic_buffer(&packedpic_buffer);
1370 packedheader_param_buffer.type = VAEncPackedHeaderPicture;
1371 packedheader_param_buffer.bit_length = length_in_bits;
1372 packedheader_param_buffer.has_emulation_bytes = 0;
1374 va_status = vaCreateBuffer(va_dpy,
1376 VAEncPackedHeaderParameterBufferType,
1377 sizeof(packedheader_param_buffer), 1, &packedheader_param_buffer,
1378 &packedpic_para_bufid);
1379 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1381 va_status = vaCreateBuffer(va_dpy,
1383 VAEncPackedHeaderDataBufferType,
1384 (length_in_bits + 7) / 8, 1, packedpic_buffer,
1385 &packedpic_data_bufid);
1386 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1388 render_id[0] = packedpic_para_bufid;
1389 render_id[1] = packedpic_data_bufid;
1390 va_status = vaRenderPicture(va_dpy,context_id, render_id, 2);
1391 CHECK_VASTATUS(va_status,"vaRenderPicture");
1393 free(packedpic_buffer);
1398 static void render_packedsei(void)
1400 VAEncPackedHeaderParameterBuffer packed_header_param_buffer;
1401 VABufferID packed_sei_header_param_buf_id, packed_sei_buf_id, render_id[2];
1402 unsigned int length_in_bits, offset_in_bytes;
1403 unsigned char *packed_sei_buffer = NULL;
1405 int init_cpb_size, target_bit_rate, i_initial_cpb_removal_delay_length, i_initial_cpb_removal_delay;
1406 int i_cpb_removal_delay, i_dpb_output_delay_length, i_cpb_removal_delay_length;
1408 /* it comes for the bps defined in SPS */
1409 target_bit_rate = frame_bitrate;
1410 init_cpb_size = (target_bit_rate * 8) >> 10;
1411 i_initial_cpb_removal_delay = init_cpb_size * 0.5 * 1024 / target_bit_rate * 90000;
1413 i_cpb_removal_delay = 2;
1414 i_initial_cpb_removal_delay_length = 24;
1415 i_cpb_removal_delay_length = 24;
1416 i_dpb_output_delay_length = 24;
1419 length_in_bits = build_packed_sei_buffer_timing(
1420 i_initial_cpb_removal_delay_length,
1421 i_initial_cpb_removal_delay,
1423 i_cpb_removal_delay_length,
1424 i_cpb_removal_delay * current_frame_encoding,
1425 i_dpb_output_delay_length,
1427 &packed_sei_buffer);
1429 offset_in_bytes = 0;
1430 packed_header_param_buffer.type = VAEncPackedHeaderH264_SEI;
1431 packed_header_param_buffer.bit_length = length_in_bits;
1432 packed_header_param_buffer.has_emulation_bytes = 0;
1434 va_status = vaCreateBuffer(va_dpy,
1436 VAEncPackedHeaderParameterBufferType,
1437 sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
1438 &packed_sei_header_param_buf_id);
1439 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1441 va_status = vaCreateBuffer(va_dpy,
1443 VAEncPackedHeaderDataBufferType,
1444 (length_in_bits + 7) / 8, 1, packed_sei_buffer,
1445 &packed_sei_buf_id);
1446 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1449 render_id[0] = packed_sei_header_param_buf_id;
1450 render_id[1] = packed_sei_buf_id;
1451 va_status = vaRenderPicture(va_dpy,context_id, render_id, 2);
1452 CHECK_VASTATUS(va_status,"vaRenderPicture");
1455 free(packed_sei_buffer);
1461 static int render_hrd(void)
1463 VABufferID misc_parameter_hrd_buf_id;
1465 VAEncMiscParameterBuffer *misc_param;
1466 VAEncMiscParameterHRD *misc_hrd_param;
1468 va_status = vaCreateBuffer(va_dpy, context_id,
1469 VAEncMiscParameterBufferType,
1470 sizeof(VAEncMiscParameterBuffer) + sizeof(VAEncMiscParameterHRD),
1473 &misc_parameter_hrd_buf_id);
1474 CHECK_VASTATUS(va_status, "vaCreateBuffer");
1477 misc_parameter_hrd_buf_id,
1478 (void **)&misc_param);
1479 misc_param->type = VAEncMiscParameterTypeHRD;
1480 misc_hrd_param = (VAEncMiscParameterHRD *)misc_param->data;
1482 if (frame_bitrate > 0) {
1483 misc_hrd_param->initial_buffer_fullness = frame_bitrate * 1024 * 4;
1484 misc_hrd_param->buffer_size = frame_bitrate * 1024 * 8;
1486 misc_hrd_param->initial_buffer_fullness = 0;
1487 misc_hrd_param->buffer_size = 0;
1489 vaUnmapBuffer(va_dpy, misc_parameter_hrd_buf_id);
1491 va_status = vaRenderPicture(va_dpy,context_id, &misc_parameter_hrd_buf_id, 1);
1492 CHECK_VASTATUS(va_status,"vaRenderPicture");;
1497 static int render_slice(void)
1499 VABufferID slice_param_buf;
1503 update_RefPicList();
1505 /* one frame, one slice */
1506 slice_param.macroblock_address = 0;
1507 slice_param.num_macroblocks = frame_width*frame_height/(16*16); /* Measured by MB */
1508 slice_param.slice_type = (current_frame_type == FRAME_IDR)?2:current_frame_type;
1509 if (current_frame_type == FRAME_I || current_frame_type == FRAME_IDR) {
1511 } else if (current_frame_type == FRAME_P) {
1512 int refpiclist0_max = h264_maxref & 0xffff;
1513 memcpy(slice_param.RefPicList0, RefPicList0_P, refpiclist0_max*sizeof(VAPictureH264));
1515 for (i = refpiclist0_max; i < 32; i++) {
1516 slice_param.RefPicList0[i].picture_id = VA_INVALID_SURFACE;
1517 slice_param.RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
1519 } else if (current_frame_type == FRAME_B) {
1520 int refpiclist0_max = h264_maxref & 0xffff;
1521 int refpiclist1_max = (h264_maxref >> 16) & 0xffff;
1523 memcpy(slice_param.RefPicList0, RefPicList0_B, refpiclist0_max*sizeof(VAPictureH264));
1524 for (i = refpiclist0_max; i < 32; i++) {
1525 slice_param.RefPicList0[i].picture_id = VA_INVALID_SURFACE;
1526 slice_param.RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
1529 memcpy(slice_param.RefPicList1, RefPicList1_B, refpiclist1_max*sizeof(VAPictureH264));
1530 for (i = refpiclist1_max; i < 32; i++) {
1531 slice_param.RefPicList1[i].picture_id = VA_INVALID_SURFACE;
1532 slice_param.RefPicList1[i].flags = VA_PICTURE_H264_INVALID;
1536 slice_param.slice_alpha_c0_offset_div2 = 2;
1537 slice_param.slice_beta_offset_div2 = 2;
1538 slice_param.pic_order_cnt_lsb = current_frame_display % MaxPicOrderCntLsb;
1540 va_status = vaCreateBuffer(va_dpy,context_id,VAEncSliceParameterBufferType,
1541 sizeof(slice_param),1,&slice_param,&slice_param_buf);
1542 CHECK_VASTATUS(va_status,"vaCreateBuffer");;
1544 va_status = vaRenderPicture(va_dpy,context_id, &slice_param_buf, 1);
1545 CHECK_VASTATUS(va_status,"vaRenderPicture");
1551 static int upload_source_YUV_once_for_all()
1557 for (i = 0; i < SURFACE_NUM; i++) {
1558 printf("\rLoading data into surface %d.....", i);
1559 upload_surface(va_dpy, src_surface[i], box_width, row_shift, 0);
1562 if (row_shift==(2*box_width)) row_shift= 0;
1564 printf("Completed surface loading\n");
1570 static int load_surface(VASurfaceID surface_id, unsigned long long display_order)
1572 VAImage surface_image;
1573 unsigned char *surface_p=NULL, *Y_start=NULL, *U_start=NULL,*V_start=NULL, *uv_ptr;
1574 int Y_pitch=0, U_pitch=0, row, V_pitch, uv_size;
1577 if (srcyuv_fp == NULL)
1580 /* rewind the file pointer if encoding more than srcyuv_frames */
1581 display_order = display_order % srcyuv_frames;
1583 fseek(srcyuv_fp, display_order * frame_width * frame_height * 1.5, SEEK_SET);
1585 va_status = vaDeriveImage(va_dpy,surface_id, &surface_image);
1586 CHECK_VASTATUS(va_status,"vaDeriveImage");
1588 vaMapBuffer(va_dpy,surface_image.buf,(void **)&surface_p);
1589 assert(VA_STATUS_SUCCESS == va_status);
1591 Y_start = surface_p;
1592 Y_pitch = surface_image.pitches[0];
1593 switch (surface_image.format.fourcc) {
1594 case VA_FOURCC_NV12:
1595 U_start = (unsigned char *)surface_p + surface_image.offsets[1];
1596 V_start = U_start + 1;
1597 U_pitch = surface_image.pitches[1];
1598 V_pitch = surface_image.pitches[1];
1600 case VA_FOURCC_IYUV:
1601 U_start = (unsigned char *)surface_p + surface_image.offsets[1];
1602 V_start = (unsigned char *)surface_p + surface_image.offsets[2];
1603 U_pitch = surface_image.pitches[1];
1604 V_pitch = surface_image.pitches[2];
1606 case VA_FOURCC_YV12:
1607 U_start = (unsigned char *)surface_p + surface_image.offsets[2];
1608 V_start = (unsigned char *)surface_p + surface_image.offsets[1];
1609 U_pitch = surface_image.pitches[2];
1610 V_pitch = surface_image.pitches[1];
1612 case VA_FOURCC_YUY2:
1613 U_start = surface_p + 1;
1614 V_start = surface_p + 3;
1615 U_pitch = surface_image.pitches[0];
1616 V_pitch = surface_image.pitches[0];
1623 for (row=0;row<surface_image.height;row++) {
1624 unsigned char *Y_row = Y_start + row * Y_pitch;
1625 (void)fread(Y_row, 1, surface_image.width, srcyuv_fp);
1628 /* copy UV data, reset file pointer,
1629 * surface_image.height may not be equal to source YUV height/frame_height
1632 display_order * frame_width * frame_height * 1.5 + frame_width * frame_height,
1635 uv_size = 2 * (frame_width/2) * (frame_height/2);
1636 uv_ptr = malloc(uv_size);
1637 fread(uv_ptr, uv_size, 1, srcyuv_fp);
1639 for (row =0; row < surface_image.height/2; row++) {
1640 unsigned char *U_row = U_start + row * U_pitch;
1641 unsigned char *u_ptr, *v_ptr;
1643 switch (surface_image.format.fourcc) {
1644 case VA_FOURCC_NV12:
1645 if (srcyuv_fourcc == VA_FOURCC_NV12) {
1646 memcpy(U_row, uv_ptr + row * frame_width, frame_width);
1648 } else if (srcyuv_fourcc == VA_FOURCC_IYUV) {
1649 u_ptr = uv_ptr + row * (frame_width/2);
1650 v_ptr = uv_ptr + (frame_width/2) * (frame_height/2) + row * (frame_width/2);
1651 } else if (srcyuv_fourcc == VA_FOURCC_YV12) {
1652 v_ptr = uv_ptr + row * (frame_height/2);
1653 u_ptr = uv_ptr + (frame_width/2) * (frame_height/2) + row * (frame_width/2);
1655 for(j = 0; j < frame_width/2; j++) {
1656 U_row[2*j] = u_ptr[j];
1657 U_row[2*j+1] = v_ptr[j];
1660 case VA_FOURCC_IYUV:
1661 case VA_FOURCC_YV12:
1662 case VA_FOURCC_YUY2:
1664 printf("unsupported fourcc in load_surface\n");
1670 vaUnmapBuffer(va_dpy,surface_image.buf);
1672 vaDestroyImage(va_dpy,surface_image.image_id);
1678 static int save_codeddata(unsigned long long display_order, unsigned long long encode_order)
1680 VACodedBufferSegment *buf_list = NULL;
1682 unsigned int coded_size = 0;
1684 va_status = vaMapBuffer(va_dpy,coded_buf[display_order % SURFACE_NUM],(void **)(&buf_list));
1685 CHECK_VASTATUS(va_status,"vaMapBuffer");
1686 while (buf_list != NULL) {
1687 coded_size += fwrite(buf_list->buf, 1, buf_list->size, coded_fp);
1688 buf_list = (VACodedBufferSegment *) buf_list->next;
1690 vaUnmapBuffer(va_dpy,coded_buf[display_order % SURFACE_NUM]);
1692 printf("\r "); /* return back to startpoint */
1693 switch (encode_order % 4) {
1707 printf("%08lld", encode_order);
1709 if (current_frame_encoding % intra_count == 0)
1714 printf("(%06d bytes coded)",coded_size);
1715 /* skipped frame ? */
1722 static struct storage_task_t * storage_task_dequque(void)
1724 struct storage_task_t *header;
1726 pthread_mutex_lock(&encode_mutex);
1728 header = storage_task_header;
1729 if (storage_task_header != NULL) {
1730 if (storage_task_tail == storage_task_header)
1731 storage_task_tail = NULL;
1732 storage_task_header = header->next;
1735 pthread_mutex_unlock(&encode_mutex);
1740 static int storage_task_queue(unsigned long long display_order, unsigned long long encode_order)
1742 struct storage_task_t *tmp;
1744 tmp = calloc(1, sizeof(struct storage_task_t));
1745 tmp->display_order = display_order;
1746 tmp->encode_order = encode_order;
1748 pthread_mutex_lock(&encode_mutex);
1750 if (storage_task_header == NULL) {
1751 storage_task_header = tmp;
1752 storage_task_tail = tmp;
1754 storage_task_tail->next = tmp;
1755 storage_task_tail = tmp;
1758 srcsurface_status[display_order % SURFACE_NUM] = SRC_SURFACE_IN_STORAGE;
1759 pthread_cond_signal(&encode_cond);
1761 pthread_mutex_unlock(&encode_mutex);
1766 static void storage_task(unsigned long long display_order, unsigned long encode_order)
1771 tmp = GetTickCount();
1772 va_status = vaSyncSurface(va_dpy, src_surface[display_order % SURFACE_NUM]);
1773 CHECK_VASTATUS(va_status,"vaSyncSurface");
1774 SyncPictureTicks += GetTickCount() - tmp;
1775 tmp = GetTickCount();
1776 save_codeddata(display_order, encode_order);
1777 SavePictureTicks += GetTickCount() - tmp;
1778 /* tbd: save reconstructed frame */
1780 /* reload a new frame data */
1781 tmp = GetTickCount();
1782 if (srcyuv_fp != NULL)
1783 load_surface(src_surface[display_order % SURFACE_NUM], display_order + SURFACE_NUM);
1784 UploadPictureTicks += GetTickCount() - tmp;
1786 pthread_mutex_lock(&encode_mutex);
1787 srcsurface_status[display_order % SURFACE_NUM] = SRC_SURFACE_IN_ENCODING;
1788 pthread_mutex_unlock(&encode_mutex);
1792 static void * storage_task_thread(void *t)
1795 struct storage_task_t *current;
1797 current = storage_task_dequque();
1798 if (current == NULL) {
1799 pthread_mutex_lock(&encode_mutex);
1800 pthread_cond_wait(&encode_cond, &encode_mutex);
1801 pthread_mutex_unlock(&encode_mutex);
1805 storage_task(current->display_order, current->encode_order);
1809 /* all frames are saved, exit the thread */
1810 if (++frame_coded >= frame_count)
1818 static int encode_frames(void)
1820 unsigned int i, tmp;
1822 //VASurfaceStatus surface_status;
1824 /* upload RAW YUV data into all surfaces */
1825 tmp = GetTickCount();
1826 if (srcyuv_fp != NULL) {
1827 for (i = 0; i < SURFACE_NUM; i++)
1828 load_surface(src_surface[i], i);
1830 upload_source_YUV_once_for_all();
1831 UploadPictureTicks += GetTickCount() - tmp;
1833 /* ready for encoding */
1834 memset(srcsurface_status, SRC_SURFACE_IN_ENCODING, sizeof(srcsurface_status));
1836 memset(&seq_param, 0, sizeof(seq_param));
1837 memset(&pic_param, 0, sizeof(pic_param));
1838 memset(&slice_param, 0, sizeof(slice_param));
1840 if (encode_syncmode == 0)
1841 pthread_create(&encode_thread, NULL, storage_task_thread, NULL);
1843 for (current_frame_encoding = 0; current_frame_encoding < frame_count; current_frame_encoding++) {
1844 encoding2display_order(current_frame_encoding, intra_period, intra_idr_period, ip_period,
1845 ¤t_frame_display, ¤t_frame_type);
1847 /* check if the source frame is ready */
1848 while (srcsurface_status[current_slot] != SRC_SURFACE_IN_ENCODING);
1850 tmp = GetTickCount();
1851 va_status = vaBeginPicture(va_dpy, context_id, src_surface[current_slot]);
1852 CHECK_VASTATUS(va_status,"vaBeginPicture");
1853 BeginPictureTicks += GetTickCount() - tmp;
1855 tmp = GetTickCount();
1856 if (current_frame_encoding == 0) {
1859 if (h264_packedheader) {
1860 render_packedsequence();
1861 render_packedpicture();
1863 //if (rc_mode == VA_RC_CBR)
1864 // render_packedsei();
1867 //render_sequence();
1869 //if (rc_mode == VA_RC_CBR)
1870 // render_packedsei();
1874 RenderPictureTicks += GetTickCount() - tmp;
1876 tmp = GetTickCount();
1877 va_status = vaEndPicture(va_dpy,context_id);
1878 CHECK_VASTATUS(va_status,"vaEndPicture");;
1879 EndPictureTicks += GetTickCount() - tmp;
1881 if (encode_syncmode)
1882 storage_task(current_frame_display, current_frame_encoding);
1883 else /* queue the storage task queue */
1884 storage_task_queue(current_frame_display, current_frame_encoding);
1886 /* how to process skipped frames
1887 surface_status = (VASurfaceStatus) 0;
1888 va_status = vaQuerySurfaceStatus(va_dpy, src_surface[i%SURFACE_NUM],&surface_status);
1889 frame_skipped = (surface_status & VASurfaceSkipped);
1891 update_ReferenceFrames();
1894 if (encode_syncmode == 0) {
1896 pthread_join(encode_thread, (void **)&ret);
1903 static int release_encode()
1907 vaDestroySurfaces(va_dpy,&src_surface[0],SURFACE_NUM);
1908 vaDestroySurfaces(va_dpy,&ref_surface[0],SURFACE_NUM);
1910 for (i = 0; i < SURFACE_NUM; i++)
1911 vaDestroyBuffer(va_dpy,coded_buf[i]);
1913 vaDestroyContext(va_dpy,context_id);
1914 vaDestroyConfig(va_dpy,config_id);
1919 static int deinit_va()
1921 vaTerminate(va_dpy);
1923 va_close_display(va_dpy);
1929 static int print_input()
1931 printf("\n\nINPUT:Try to encode H264...\n");
1932 printf("INPUT: RateControl : %s\n", rc_to_string(rc_mode));
1933 printf("INPUT: Resolution : %dx%d, %d frames\n",
1934 frame_width, frame_height, frame_count);
1935 printf("INPUT: FrameRate : %d\n", frame_rate);
1936 printf("INPUT: Bitrate : %d\n", frame_bitrate);
1937 printf("INPUT: Slieces : %d\n", frame_slices);
1938 printf("INPUT: IntraPeriod : %d\n", intra_period);
1939 printf("INPUT: IDRPeriod : %d\n", intra_idr_period);
1940 printf("INPUT: IpPeriod : %d\n", ip_period);
1941 printf("INPUT: Initial QP : %d\n", initial_qp);
1942 printf("INPUT: Min QP : %d\n", minimal_qp);
1943 printf("INPUT: Source YUV : %s", srcyuv_fp?"FILE":"AUTO generated");
1945 printf(":%s (fourcc %s)\n", srcyuv_fn, fourcc_to_string(srcyuv_fourcc));
1948 printf("INPUT: Coded Clip : %s\n", coded_fn);
1949 if (recyuv_fp == NULL)
1950 printf("INPUT: Rec Clip : %s\n", "Not save reconstructed frame");
1952 printf("INPUT: Rec Clip : Save reconstructed frame into %s (fourcc %s)\n", recyuv_fn,
1953 fourcc_to_string(srcyuv_fourcc));
1955 printf("\n\n"); /* return back to startpoint */
1961 static int print_performance(unsigned int PictureCount)
1963 unsigned int others = 0;
1965 others = TotalTicks - UploadPictureTicks - BeginPictureTicks
1966 - RenderPictureTicks - EndPictureTicks - SyncPictureTicks - SavePictureTicks;
1970 printf("PERFORMANCE: Frame Rate : %.2f fps (%d frames, %d ms (%.2f ms per frame))\n",
1971 (double) 1000*PictureCount / TotalTicks, PictureCount,
1972 TotalTicks, ((double) TotalTicks) / (double) PictureCount);
1974 printf("PERFORMANCE: UploadPicture : %d ms (%.2f, %.2f%% percent)\n",
1975 (int) UploadPictureTicks, ((double) UploadPictureTicks) / (double) PictureCount,
1976 UploadPictureTicks/(double) TotalTicks/0.01);
1977 printf("PERFORMANCE: vaBeginPicture : %d ms (%.2f, %.2f%% percent)\n",
1978 (int) BeginPictureTicks, ((double) BeginPictureTicks) / (double) PictureCount,
1979 BeginPictureTicks/(double) TotalTicks/0.01);
1980 printf("PERFORMANCE: vaRenderHeader : %d ms (%.2f, %.2f%% percent)\n",
1981 (int) RenderPictureTicks, ((double) RenderPictureTicks) / (double) PictureCount,
1982 RenderPictureTicks/(double) TotalTicks/0.01);
1983 printf("PERFORMANCE: vaEndPicture : %d ms (%.2f, %.2f%% percent)\n",
1984 (int) EndPictureTicks, ((double) EndPictureTicks) / (double) PictureCount,
1985 EndPictureTicks/(double) TotalTicks/0.01);
1986 printf("PERFORMANCE: vaSyncSurface : %d ms (%.2f, %.2f%% percent)\n",
1987 (int) SyncPictureTicks, ((double) SyncPictureTicks) / (double) PictureCount,
1988 SyncPictureTicks/(double) TotalTicks/0.01);
1989 printf("PERFORMANCE: SavePicture : %d ms (%.2f, %.2f%% percent)\n",
1990 (int) SavePictureTicks, ((double) SavePictureTicks) / (double) PictureCount,
1991 SavePictureTicks/(double) TotalTicks/0.01);
1992 printf("PERFORMANCE: Others : %d ms (%.2f, %.2f%% percent)\n",
1993 (int) others, ((double) others) / (double) PictureCount,
1994 others/(double) TotalTicks/0.01);
1996 if (encode_syncmode == 0)
1997 printf("(Multithread enabled, the profiling is only for reference)\n");
2003 int main(int argc,char **argv)
2007 process_cmdline(argc, argv);
2011 start = GetTickCount();
2021 TotalTicks += GetTickCount() - start;
2022 print_performance(frame_count);