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>
40 #include <va/va_enc_h264.h>
41 #include "va_display.h"
43 #define CHECK_VASTATUS(va_status,func) \
44 if (va_status != VA_STATUS_SUCCESS) { \
45 fprintf(stderr,"%s:%s (%d) failed,exit\n", __func__, func, __LINE__); \
49 #include "../loadsurface.h"
51 #define NAL_REF_IDC_NONE 0
52 #define NAL_REF_IDC_LOW 1
53 #define NAL_REF_IDC_MEDIUM 2
54 #define NAL_REF_IDC_HIGH 3
62 #define SLICE_TYPE_P 0
63 #define SLICE_TYPE_B 1
64 #define SLICE_TYPE_I 2
66 #define ENTROPY_MODE_CAVLC 0
67 #define ENTROPY_MODE_CABAC 1
69 #define PROFILE_IDC_BASELINE 66
70 #define PROFILE_IDC_MAIN 77
71 #define PROFILE_IDC_HIGH 100
73 #define BITSTREAM_ALLOCATE_STEPPING 4096
75 #define SURFACE_NUM 16 /* 16 surfaces for source YUV */
76 #define SURFACE_NUM 16 /* 16 surfaces for reference */
77 static VADisplay va_dpy;
78 static VAProfile h264_profile;
79 static VAConfigAttrib attrib[VAConfigAttribTypeMax];
80 static VAConfigAttrib config_attrib[VAConfigAttribTypeMax];
81 static int config_attrib_num = 0;
82 static VASurfaceID src_surface[SURFACE_NUM];
83 static VABufferID coded_buf[SURFACE_NUM];
84 static VASurfaceID ref_surface[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 char *coded_fn = NULL, *srcyuv_fn = NULL, *recyuv_fn = NULL;
104 static FILE *coded_fp = NULL, *srcyuv_fp = NULL, *recyuv_fp = NULL;
105 static unsigned long long srcyuv_frames = 0;
106 static int srcyuv_fourcc = VA_FOURCC_NV12;
107 static int calc_psnr = 0;
109 static int frame_width = 176;
110 static int frame_height = 144;
111 static int frame_width_mbaligned;
112 static int frame_height_mbaligned;
113 static int frame_rate = 30;
114 static unsigned int frame_count = 60;
115 static unsigned int frame_coded = 0;
116 static unsigned int frame_bitrate = 0;
117 static unsigned int frame_slices = 1;
118 static double frame_size = 0;
119 static int initial_qp = 28;
120 static int minimal_qp = 0;
121 static int intra_period = 30;
122 static int intra_idr_period = 60;
123 static int ip_period = 1;
124 static int rc_mode = VA_RC_VBR;
125 static unsigned long long current_frame_encoding = 0;
126 static unsigned long long current_frame_display = 0;
127 static unsigned long long current_IDR_display = 0;
128 static unsigned int current_frame_num = 0;
129 static int current_frame_type;
130 #define current_slot (current_frame_display % SURFACE_NUM)
132 static int misc_priv_type = 0;
133 static int misc_priv_value = 0;
135 #define MIN(a, b) ((a)>(b)?(b):(a))
136 #define MAX(a, b) ((a)>(b)?(a):(b))
138 /* thread to save coded data/upload source YUV */
139 struct storage_task_t {
141 unsigned long long display_order;
142 unsigned long long encode_order;
144 static struct storage_task_t *storage_task_header = NULL, *storage_task_tail = NULL;
145 #define SRC_SURFACE_IN_ENCODING 0
146 #define SRC_SURFACE_IN_STORAGE 1
147 static int srcsurface_status[SURFACE_NUM];
148 static int encode_syncmode = 0;
149 static pthread_mutex_t encode_mutex = PTHREAD_MUTEX_INITIALIZER;
150 static pthread_cond_t encode_cond = PTHREAD_COND_INITIALIZER;
151 static pthread_t encode_thread;
153 /* for performance profiling */
154 static unsigned int UploadPictureTicks=0;
155 static unsigned int BeginPictureTicks=0;
156 static unsigned int RenderPictureTicks=0;
157 static unsigned int EndPictureTicks=0;
158 static unsigned int SyncPictureTicks=0;
159 static unsigned int SavePictureTicks=0;
160 static unsigned int TotalTicks=0;
163 unsigned int *buffer;
165 int max_size_in_dword;
167 typedef struct __bitstream bitstream;
171 va_swap32(unsigned int val)
173 unsigned char *pval = (unsigned char *)&val;
175 return ((pval[0] << 24) |
182 bitstream_start(bitstream *bs)
184 bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
185 bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
190 bitstream_end(bitstream *bs)
192 int pos = (bs->bit_offset >> 5);
193 int bit_offset = (bs->bit_offset & 0x1f);
194 int bit_left = 32 - bit_offset;
197 bs->buffer[pos] = va_swap32((bs->buffer[pos] << bit_left));
202 bitstream_put_ui(bitstream *bs, unsigned int val, int size_in_bits)
204 int pos = (bs->bit_offset >> 5);
205 int bit_offset = (bs->bit_offset & 0x1f);
206 int bit_left = 32 - bit_offset;
211 bs->bit_offset += size_in_bits;
213 if (bit_left > size_in_bits) {
214 bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
216 size_in_bits -= bit_left;
217 bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
218 bs->buffer[pos] = va_swap32(bs->buffer[pos]);
220 if (pos + 1 == bs->max_size_in_dword) {
221 bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
222 bs->buffer = realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
225 bs->buffer[pos + 1] = val;
230 bitstream_put_ue(bitstream *bs, unsigned int val)
232 int size_in_bits = 0;
240 bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
241 bitstream_put_ui(bs, val, size_in_bits);
245 bitstream_put_se(bitstream *bs, int val)
247 unsigned int new_val;
252 new_val = 2 * val - 1;
254 bitstream_put_ue(bs, new_val);
258 bitstream_byte_aligning(bitstream *bs, int bit)
260 int bit_offset = (bs->bit_offset & 0x7);
261 int bit_left = 8 - bit_offset;
267 assert(bit == 0 || bit == 1);
270 new_val = (1 << bit_left) - 1;
274 bitstream_put_ui(bs, new_val, bit_left);
278 rbsp_trailing_bits(bitstream *bs)
280 bitstream_put_ui(bs, 1, 1);
281 bitstream_byte_aligning(bs, 0);
284 static void nal_start_code_prefix(bitstream *bs)
286 bitstream_put_ui(bs, 0x00000001, 32);
289 static void nal_header(bitstream *bs, int nal_ref_idc, int nal_unit_type)
291 bitstream_put_ui(bs, 0, 1); /* forbidden_zero_bit: 0 */
292 bitstream_put_ui(bs, nal_ref_idc, 2);
293 bitstream_put_ui(bs, nal_unit_type, 5);
296 static void sps_rbsp(bitstream *bs)
298 int profile_idc = PROFILE_IDC_BASELINE;
300 if (h264_profile == VAProfileH264High)
301 profile_idc = PROFILE_IDC_HIGH;
302 else if (h264_profile == VAProfileH264Main)
303 profile_idc = PROFILE_IDC_MAIN;
305 bitstream_put_ui(bs, profile_idc, 8); /* profile_idc */
306 bitstream_put_ui(bs, !!(constraint_set_flag & 1), 1); /* constraint_set0_flag */
307 bitstream_put_ui(bs, !!(constraint_set_flag & 2), 1); /* constraint_set1_flag */
308 bitstream_put_ui(bs, !!(constraint_set_flag & 4), 1); /* constraint_set2_flag */
309 bitstream_put_ui(bs, !!(constraint_set_flag & 8), 1); /* constraint_set3_flag */
310 bitstream_put_ui(bs, 0, 4); /* reserved_zero_4bits */
311 bitstream_put_ui(bs, seq_param.level_idc, 8); /* level_idc */
312 bitstream_put_ue(bs, seq_param.seq_parameter_set_id); /* seq_parameter_set_id */
314 if ( profile_idc == PROFILE_IDC_HIGH) {
315 bitstream_put_ue(bs, 1); /* chroma_format_idc = 1, 4:2:0 */
316 bitstream_put_ue(bs, 0); /* bit_depth_luma_minus8 */
317 bitstream_put_ue(bs, 0); /* bit_depth_chroma_minus8 */
318 bitstream_put_ui(bs, 0, 1); /* qpprime_y_zero_transform_bypass_flag */
319 bitstream_put_ui(bs, 0, 1); /* seq_scaling_matrix_present_flag */
322 bitstream_put_ue(bs, seq_param.seq_fields.bits.log2_max_frame_num_minus4); /* log2_max_frame_num_minus4 */
323 bitstream_put_ue(bs, seq_param.seq_fields.bits.pic_order_cnt_type); /* pic_order_cnt_type */
325 if (seq_param.seq_fields.bits.pic_order_cnt_type == 0)
326 bitstream_put_ue(bs, seq_param.seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4); /* log2_max_pic_order_cnt_lsb_minus4 */
331 bitstream_put_ue(bs, seq_param.max_num_ref_frames); /* num_ref_frames */
332 bitstream_put_ui(bs, 0, 1); /* gaps_in_frame_num_value_allowed_flag */
334 bitstream_put_ue(bs, seq_param.picture_width_in_mbs - 1); /* pic_width_in_mbs_minus1 */
335 bitstream_put_ue(bs, seq_param.picture_height_in_mbs - 1); /* pic_height_in_map_units_minus1 */
336 bitstream_put_ui(bs, seq_param.seq_fields.bits.frame_mbs_only_flag, 1); /* frame_mbs_only_flag */
338 if (!seq_param.seq_fields.bits.frame_mbs_only_flag) {
342 bitstream_put_ui(bs, seq_param.seq_fields.bits.direct_8x8_inference_flag, 1); /* direct_8x8_inference_flag */
343 bitstream_put_ui(bs, seq_param.frame_cropping_flag, 1); /* frame_cropping_flag */
345 if (seq_param.frame_cropping_flag) {
346 bitstream_put_ue(bs, seq_param.frame_crop_left_offset); /* frame_crop_left_offset */
347 bitstream_put_ue(bs, seq_param.frame_crop_right_offset); /* frame_crop_right_offset */
348 bitstream_put_ue(bs, seq_param.frame_crop_top_offset); /* frame_crop_top_offset */
349 bitstream_put_ue(bs, seq_param.frame_crop_bottom_offset); /* frame_crop_bottom_offset */
352 //if ( frame_bit_rate < 0 ) { //TODO EW: the vui header isn't correct
354 bitstream_put_ui(bs, 0, 1); /* vui_parameters_present_flag */
356 bitstream_put_ui(bs, 1, 1); /* vui_parameters_present_flag */
357 bitstream_put_ui(bs, 0, 1); /* aspect_ratio_info_present_flag */
358 bitstream_put_ui(bs, 0, 1); /* overscan_info_present_flag */
359 bitstream_put_ui(bs, 0, 1); /* video_signal_type_present_flag */
360 bitstream_put_ui(bs, 0, 1); /* chroma_loc_info_present_flag */
361 bitstream_put_ui(bs, 1, 1); /* timing_info_present_flag */
363 bitstream_put_ui(bs, 15, 32);
364 bitstream_put_ui(bs, 900, 32);
365 bitstream_put_ui(bs, 1, 1);
367 bitstream_put_ui(bs, 1, 1); /* nal_hrd_parameters_present_flag */
370 bitstream_put_ue(bs, 0); /* cpb_cnt_minus1 */
371 bitstream_put_ui(bs, 4, 4); /* bit_rate_scale */
372 bitstream_put_ui(bs, 6, 4); /* cpb_size_scale */
374 bitstream_put_ue(bs, frame_bitrate - 1); /* bit_rate_value_minus1[0] */
375 bitstream_put_ue(bs, frame_bitrate*8 - 1); /* cpb_size_value_minus1[0] */
376 bitstream_put_ui(bs, 1, 1); /* cbr_flag[0] */
378 bitstream_put_ui(bs, 23, 5); /* initial_cpb_removal_delay_length_minus1 */
379 bitstream_put_ui(bs, 23, 5); /* cpb_removal_delay_length_minus1 */
380 bitstream_put_ui(bs, 23, 5); /* dpb_output_delay_length_minus1 */
381 bitstream_put_ui(bs, 23, 5); /* time_offset_length */
383 bitstream_put_ui(bs, 0, 1); /* vcl_hrd_parameters_present_flag */
384 bitstream_put_ui(bs, 0, 1); /* low_delay_hrd_flag */
386 bitstream_put_ui(bs, 0, 1); /* pic_struct_present_flag */
387 bitstream_put_ui(bs, 0, 1); /* bitstream_restriction_flag */
390 rbsp_trailing_bits(bs); /* rbsp_trailing_bits */
394 static void pps_rbsp(bitstream *bs)
396 bitstream_put_ue(bs, pic_param.pic_parameter_set_id); /* pic_parameter_set_id */
397 bitstream_put_ue(bs, pic_param.seq_parameter_set_id); /* seq_parameter_set_id */
399 bitstream_put_ui(bs, pic_param.pic_fields.bits.entropy_coding_mode_flag, 1); /* entropy_coding_mode_flag */
401 bitstream_put_ui(bs, 0, 1); /* pic_order_present_flag: 0 */
403 bitstream_put_ue(bs, 0); /* num_slice_groups_minus1 */
405 bitstream_put_ue(bs, pic_param.num_ref_idx_l0_active_minus1); /* num_ref_idx_l0_active_minus1 */
406 bitstream_put_ue(bs, pic_param.num_ref_idx_l1_active_minus1); /* num_ref_idx_l1_active_minus1 1 */
408 bitstream_put_ui(bs, pic_param.pic_fields.bits.weighted_pred_flag, 1); /* weighted_pred_flag: 0 */
409 bitstream_put_ui(bs, pic_param.pic_fields.bits.weighted_bipred_idc, 2); /* weighted_bipred_idc: 0 */
411 bitstream_put_se(bs, pic_param.pic_init_qp - 26); /* pic_init_qp_minus26 */
412 bitstream_put_se(bs, 0); /* pic_init_qs_minus26 */
413 bitstream_put_se(bs, 0); /* chroma_qp_index_offset */
415 bitstream_put_ui(bs, pic_param.pic_fields.bits.deblocking_filter_control_present_flag, 1); /* deblocking_filter_control_present_flag */
416 bitstream_put_ui(bs, 0, 1); /* constrained_intra_pred_flag */
417 bitstream_put_ui(bs, 0, 1); /* redundant_pic_cnt_present_flag */
420 bitstream_put_ui(bs, pic_param.pic_fields.bits.transform_8x8_mode_flag, 1); /*transform_8x8_mode_flag */
421 bitstream_put_ui(bs, 0, 1); /* pic_scaling_matrix_present_flag */
422 bitstream_put_se(bs, pic_param.second_chroma_qp_index_offset ); /*second_chroma_qp_index_offset */
424 rbsp_trailing_bits(bs);
429 build_packed_pic_buffer(unsigned char **header_buffer)
433 bitstream_start(&bs);
434 nal_start_code_prefix(&bs);
435 nal_header(&bs, NAL_REF_IDC_HIGH, NAL_PPS);
439 *header_buffer = (unsigned char *)bs.buffer;
440 return bs.bit_offset;
444 build_packed_seq_buffer(unsigned char **header_buffer)
448 bitstream_start(&bs);
449 nal_start_code_prefix(&bs);
450 nal_header(&bs, NAL_REF_IDC_HIGH, NAL_SPS);
454 *header_buffer = (unsigned char *)bs.buffer;
455 return bs.bit_offset;
459 build_packed_sei_buffer_timing(unsigned int init_cpb_removal_length,
460 unsigned int init_cpb_removal_delay,
461 unsigned int init_cpb_removal_delay_offset,
462 unsigned int cpb_removal_length,
463 unsigned int cpb_removal_delay,
464 unsigned int dpb_output_length,
465 unsigned int dpb_output_delay,
466 unsigned char **sei_buffer)
468 unsigned char *byte_buf;
469 int bp_byte_size, i, pic_byte_size;
472 bitstream sei_bp_bs, sei_pic_bs;
474 bitstream_start(&sei_bp_bs);
475 bitstream_put_ue(&sei_bp_bs, 0); /*seq_parameter_set_id*/
476 bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay, cpb_removal_length);
477 bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay_offset, cpb_removal_length);
478 if ( sei_bp_bs.bit_offset & 0x7) {
479 bitstream_put_ui(&sei_bp_bs, 1, 1);
481 bitstream_end(&sei_bp_bs);
482 bp_byte_size = (sei_bp_bs.bit_offset + 7) / 8;
484 bitstream_start(&sei_pic_bs);
485 bitstream_put_ui(&sei_pic_bs, cpb_removal_delay, cpb_removal_length);
486 bitstream_put_ui(&sei_pic_bs, dpb_output_delay, dpb_output_length);
487 if ( sei_pic_bs.bit_offset & 0x7) {
488 bitstream_put_ui(&sei_pic_bs, 1, 1);
490 bitstream_end(&sei_pic_bs);
491 pic_byte_size = (sei_pic_bs.bit_offset + 7) / 8;
493 bitstream_start(&nal_bs);
494 nal_start_code_prefix(&nal_bs);
495 nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
497 /* Write the SEI buffer period data */
498 bitstream_put_ui(&nal_bs, 0, 8);
499 bitstream_put_ui(&nal_bs, bp_byte_size, 8);
501 byte_buf = (unsigned char *)sei_bp_bs.buffer;
502 for(i = 0; i < bp_byte_size; i++) {
503 bitstream_put_ui(&nal_bs, byte_buf[i], 8);
506 /* write the SEI timing data */
507 bitstream_put_ui(&nal_bs, 0x01, 8);
508 bitstream_put_ui(&nal_bs, pic_byte_size, 8);
510 byte_buf = (unsigned char *)sei_pic_bs.buffer;
511 for(i = 0; i < pic_byte_size; i++) {
512 bitstream_put_ui(&nal_bs, byte_buf[i], 8);
516 rbsp_trailing_bits(&nal_bs);
517 bitstream_end(&nal_bs);
519 *sei_buffer = (unsigned char *)nal_bs.buffer;
521 return nal_bs.bit_offset;
527 * Helper function for profiling purposes
529 static unsigned int GetTickCount()
532 if (gettimeofday(&tv, NULL))
534 return tv.tv_usec/1000+tv.tv_sec*1000;
538 Assume frame sequence is: Frame#0,#1,#2,...,#M,...,#X,... (encoding order)
539 1) period between Frame #X and Frame #N = #X - #N
540 2) 0 means infinite for intra_period/intra_idr_period, and 0 is invalid for ip_period
541 3) intra_idr_period % intra_period (intra_period > 0) and intra_period % ip_period must be 0
542 4) intra_period and intra_idr_period take precedence over ip_period
543 5) if ip_period > 1, intra_period and intra_idr_period are not the strict periods
544 of I/IDR frames, see bellow examples
545 -------------------------------------------------------------------
546 intra_period intra_idr_period ip_period frame sequence (intra_period/intra_idr_period/ip_period)
547 0 ignored 1 IDRPPPPPPP ... (No IDR/I any more)
548 0 ignored >=2 IDR(PBB)(PBB)... (No IDR/I any more)
549 1 0 ignored IDRIIIIIII... (No IDR any more)
550 1 1 ignored IDR IDR IDR IDR...
551 1 >=2 ignored IDRII IDRII IDR... (1/3/ignore)
552 >=2 0 1 IDRPPP IPPP I... (3/0/1)
553 >=2 0 >=2 IDR(PBB)(PBB)(IBB) (6/0/3)
554 (PBB)(IBB)(PBB)(IBB)...
555 >=2 >=2 1 IDRPPPPP IPPPPP IPPPPP (6/18/1)
556 IDRPPPPP IPPPPP IPPPPP...
557 >=2 >=2 >=2 {IDR(PBB)(PBB)(IBB)(PBB)(IBB)(PBB)} (6/18/3)
558 {IDR(PBB)(PBB)(IBB)(PBB)(IBB)(PBB)}...
559 {IDR(PBB)(PBB)(IBB)(PBB)} (6/12/3)
560 {IDR(PBB)(PBB)(IBB)(PBB)}...
561 {IDR(PBB)(PBB)} (6/6/3)
566 * Return displaying order with specified periods and encoding order
567 * displaying_order: displaying order
568 * frame_type: frame type
574 void encoding2display_order(
575 unsigned long long encoding_order,int intra_period,
576 int intra_idr_period,int ip_period,
577 unsigned long long *displaying_order,
580 int encoding_order_gop = 0;
582 if (intra_period == 1) { /* all are I/IDR frames */
583 *displaying_order = encoding_order;
584 if (intra_idr_period == 0)
585 *frame_type = (encoding_order == 0)?FRAME_IDR:FRAME_I;
587 *frame_type = (encoding_order % intra_idr_period == 0)?FRAME_IDR:FRAME_I;
591 if (intra_period == 0)
592 intra_idr_period = 0;
596 * IDR (PBB)(PBB)(IBB)(PBB)
598 encoding_order_gop = (intra_idr_period == 0)? encoding_order:
599 (encoding_order % (intra_idr_period + ((ip_period == 1)?0:1)));
601 if (encoding_order_gop == 0) { /* the first frame */
602 *frame_type = FRAME_IDR;
603 *displaying_order = encoding_order;
604 } else if (((encoding_order_gop - 1) % ip_period) != 0) { /* B frames */
605 *frame_type = FRAME_B;
606 *displaying_order = encoding_order - 1;
607 } else if ((intra_period != 0) && /* have I frames */
608 (encoding_order_gop >= 2) &&
609 ((ip_period == 1 && encoding_order_gop % intra_period == 0) || /* for IDR PPPPP IPPPP */
610 /* for IDR (PBB)(PBB)(IBB) */
611 (ip_period >= 2 && ((encoding_order_gop - 1) / ip_period % (intra_period / ip_period)) == 0))) {
612 *frame_type = FRAME_I;
613 *displaying_order = encoding_order + ip_period - 1;
615 *frame_type = FRAME_P;
616 *displaying_order = encoding_order + ip_period - 1;
621 static char *fourcc_to_string(int fourcc)
637 static int string_to_fourcc(char *str)
641 if (!strncmp(str, "NV12", 4))
642 fourcc = VA_FOURCC_NV12;
643 else if (!strncmp(str, "IYUV", 4))
644 fourcc = VA_FOURCC_IYUV;
645 else if (!strncmp(str, "YV12", 4))
646 fourcc = VA_FOURCC_YV12;
647 else if (!strncmp(str, "UYVY", 4))
648 fourcc = VA_FOURCC_UYVY;
650 printf("Unknow FOURCC\n");
657 static char *rc_to_string(int rcmode)
670 case VA_RC_VBR_CONSTRAINED:
671 return "VBR_CONSTRAINED";
677 static int string_to_rc(char *str)
681 if (!strncmp(str, "NONE", 4))
682 rc_mode = VA_RC_NONE;
683 else if (!strncmp(str, "CBR", 3))
685 else if (!strncmp(str, "VBR", 3))
687 else if (!strncmp(str, "VCM", 3))
689 else if (!strncmp(str, "CQP", 3))
691 else if (!strncmp(str, "VBR_CONSTRAINED", 15))
692 rc_mode = VA_RC_VBR_CONSTRAINED;
694 printf("Unknown RC mode\n");
701 static int print_help(void)
703 printf("./h264encode <options>\n");
704 printf(" -w <width> -h <height>\n");
705 printf(" -n <frame number>\n");
706 printf(" -o <coded file>\n");
707 printf(" -f <frame rate>\n");
708 printf(" --intra_period <number>\n");
709 printf(" --idr_period <number>\n");
710 printf(" --ip_period <number>\n");
711 printf(" --bitrate <bitrate>\n");
712 printf(" --initialqp <number>\n");
713 printf(" --minqp <number>\n");
714 printf(" --rcmode <NONE|CBR|VBR|VCM|CQP|VBR_CONTRAINED>\n");
715 printf(" --syncmode: sequentially upload source, encoding, save result, no multi-thread\n");
716 printf(" --srcyuv <filename> load YUV from a file\n");
717 printf(" --fourcc <NV12|IYUV|YV12> source YUV fourcc\n");
718 printf(" --recyuv <filename> save reconstructed YUV into a file\n");
719 printf(" --enablePSNR calculate PSNR of recyuv vs. srcyuv\n");
724 static int process_cmdline(int argc, char *argv[])
727 const struct option long_opts[] = {
728 {"help", no_argument, NULL, 0 },
729 {"bitrate", required_argument, NULL, 1 },
730 {"minqp", required_argument, NULL, 2 },
731 {"initialqp", required_argument, NULL, 3 },
732 {"intra_period", required_argument, NULL, 4 },
733 {"idr_period", required_argument, NULL, 5 },
734 {"ip_period", required_argument, NULL, 6 },
735 {"rcmode", required_argument, NULL, 7 },
736 {"srcyuv", required_argument, NULL, 9 },
737 {"recyuv", required_argument, NULL, 10 },
738 {"fourcc", required_argument, NULL, 11 },
739 {"syncmode", no_argument, NULL, 12 },
740 {"enablePSNR", no_argument, NULL, 13 },
741 {"privt", required_argument, NULL, 14 },
742 {"privv", required_argument, NULL, 15 },
743 {NULL, no_argument, NULL, 0 }};
746 while ((c =getopt_long_only(argc,argv,"w:h:n:f:o:?",long_opts,&long_index)) != EOF) {
749 frame_width = atoi(optarg);
752 frame_height = atoi(optarg);
755 frame_count = atoi(optarg);
758 frame_rate = atoi(optarg);
761 coded_fn = strdup(optarg);
767 frame_bitrate = atoi(optarg);
770 minimal_qp = atoi(optarg);
773 initial_qp = atoi(optarg);
776 intra_period = atoi(optarg);
779 intra_idr_period = atoi(optarg);
782 ip_period = atoi(optarg);
785 rc_mode = string_to_rc(optarg);
792 srcyuv_fn = strdup(optarg);
795 recyuv_fn = strdup(optarg);
798 srcyuv_fourcc = string_to_fourcc(optarg);
799 if (srcyuv_fourcc <= 0) {
811 misc_priv_type = strtol(optarg, NULL, 0);
814 misc_priv_value = strtol(optarg, NULL, 0);
824 printf(" ip_period must be greater than 0\n");
827 if (intra_period != 1 && intra_period % ip_period != 0) {
828 printf(" intra_period must be a multiplier of ip_period\n");
831 if (intra_period != 0 && intra_idr_period % intra_period != 0) {
832 printf(" intra_idr_period must be a multiplier of intra_period\n");
836 if (frame_bitrate == 0)
837 frame_bitrate = frame_width * frame_height * 12 * frame_rate / 50;
839 /* open source file */
841 srcyuv_fp = fopen(srcyuv_fn,"r");
843 if (srcyuv_fp == NULL)
844 printf("Open source YUV file %s failed, use auto-generated YUV data\n", srcyuv_fn);
846 fseek(srcyuv_fp, 0L, SEEK_END);
847 srcyuv_frames = ftell(srcyuv_fp) / (frame_width * frame_height * 1.5);
848 printf("Source YUV file %s with %llu frames\n", srcyuv_fn, srcyuv_frames);
852 /* open source file */
854 recyuv_fp = fopen(recyuv_fn,"w+");
856 if (recyuv_fp == NULL)
857 printf("Open reconstructed YUV file %s failed\n", recyuv_fn);
860 if (coded_fn == NULL) {
862 if (stat("/tmp", &buf) == 0)
863 coded_fn = strdup("/tmp/test.264");
864 else if (stat("/sdcard", &buf) == 0)
865 coded_fn = strdup("/sdcard/test.264");
867 coded_fn = strdup("./test.264");
870 /* store coded data into a file */
871 coded_fp = fopen(coded_fn,"w+");
872 if (coded_fp == NULL) {
873 printf("Open file %s failed, exit\n", coded_fn);
877 frame_width_mbaligned = (frame_width + 15) & (~15);
878 frame_height_mbaligned = (frame_height + 15) & (~15);
879 if (frame_width != frame_width_mbaligned ||
880 frame_height != frame_height_mbaligned) {
881 printf("Source frame is %dx%d and will code clip to %dx%d with crop\n",
882 frame_width, frame_height,
883 frame_width_mbaligned, frame_height_mbaligned
890 static int init_va(void)
892 VAProfile profile_list[]={VAProfileH264High,VAProfileH264Main,VAProfileH264Baseline,VAProfileH264ConstrainedBaseline};
893 VAEntrypoint entrypoints[VAEntrypointMax]={0};
894 int num_entrypoints,slice_entrypoint;
895 int support_encode = 0;
896 int major_ver, minor_ver;
900 va_dpy = va_open_display();
901 va_status = vaInitialize(va_dpy, &major_ver, &minor_ver);
902 CHECK_VASTATUS(va_status, "vaInitialize");
904 /* use the highest profile */
905 for (i = 0; i < sizeof(profile_list)/sizeof(profile_list[0]); i++) {
906 h264_profile = profile_list[i];
907 vaQueryConfigEntrypoints(va_dpy, h264_profile, entrypoints, &num_entrypoints);
908 for (slice_entrypoint = 0; slice_entrypoint < num_entrypoints; slice_entrypoint++) {
909 if (entrypoints[slice_entrypoint] == VAEntrypointEncSlice) {
914 if (support_encode == 1)
918 if (support_encode == 0) {
919 printf("Can't find VAEntrypointEncSlice for H264 profiles\n");
922 switch (h264_profile) {
923 case VAProfileH264Baseline:
924 printf("Use profile VAProfileH264Baseline\n");
926 constraint_set_flag |= (1 << 0); /* Annex A.2.1 */
928 case VAProfileH264ConstrainedBaseline:
929 printf("Use profile VAProfileH264ConstrainedBaseline\n");
930 constraint_set_flag |= (1 << 0 | 1 << 1); /* Annex A.2.2 */
934 case VAProfileH264Main:
935 printf("Use profile VAProfileH264Main\n");
936 constraint_set_flag |= (1 << 1); /* Annex A.2.2 */
939 case VAProfileH264High:
940 constraint_set_flag |= (1 << 3); /* Annex A.2.4 */
941 printf("Use profile VAProfileH264High\n");
944 printf("unknow profile. Set to Baseline");
945 h264_profile = VAProfileH264Baseline;
947 constraint_set_flag |= (1 << 0); /* Annex A.2.1 */
952 /* find out the format for the render target, and rate control mode */
953 for (i = 0; i < VAConfigAttribTypeMax; i++)
956 va_status = vaGetConfigAttributes(va_dpy, h264_profile, VAEntrypointEncSlice,
957 &attrib[0], VAConfigAttribTypeMax);
958 CHECK_VASTATUS(va_status, "vaGetConfigAttributes");
959 /* check the interested configattrib */
960 if ((attrib[VAConfigAttribRTFormat].value & VA_RT_FORMAT_YUV420) == 0) {
961 printf("Not find desired YUV420 RT format\n");
964 config_attrib[config_attrib_num].type = VAConfigAttribRTFormat;
965 config_attrib[config_attrib_num].value = VA_RT_FORMAT_YUV420;
969 if (attrib[VAConfigAttribRateControl].value != VA_ATTRIB_NOT_SUPPORTED) {
970 int tmp = attrib[VAConfigAttribRateControl].value;
972 printf("Support rate control mode (0x%x):", tmp);
974 if (tmp & VA_RC_NONE)
984 if (tmp & VA_RC_VBR_CONSTRAINED)
985 printf("VBR_CONSTRAINED ");
989 /* need to check if support rc_mode */
990 config_attrib[config_attrib_num].type = VAConfigAttribRateControl;
991 config_attrib[config_attrib_num].value = rc_mode;
996 if (attrib[VAConfigAttribEncPackedHeaders].value != VA_ATTRIB_NOT_SUPPORTED) {
997 int tmp = attrib[VAConfigAttribEncPackedHeaders].value;
999 printf("Support VAConfigAttribEncPackedHeaders\n");
1001 h264_packedheader = 1;
1002 config_attrib[config_attrib_num].type = VAConfigAttribEncPackedHeaders;
1003 config_attrib[config_attrib_num].value = VA_ENC_PACKED_HEADER_NONE;
1005 if (tmp & VA_ENC_PACKED_HEADER_SEQUENCE) {
1006 printf("Support packed sequence headers\n");
1007 config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_SEQUENCE;
1010 if (tmp & VA_ENC_PACKED_HEADER_PICTURE) {
1011 printf("Support packed picture headers\n");
1012 config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_PICTURE;
1015 if (tmp & VA_ENC_PACKED_HEADER_SLICE) {
1016 printf("Support packed slice headers\n");
1017 config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_SLICE;
1020 if (tmp & VA_ENC_PACKED_HEADER_MISC) {
1021 printf("Support packed misc headers\n");
1022 config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_MISC;
1025 config_attrib_num++;
1028 if (attrib[VAConfigAttribEncInterlaced].value != VA_ATTRIB_NOT_SUPPORTED) {
1029 int tmp = attrib[VAConfigAttribEncInterlaced].value;
1031 printf("Support VAConfigAttribEncInterlaced\n");
1033 if (tmp & VA_ENC_INTERLACED_FRAME)
1034 printf("support VA_ENC_INTERLACED_FRAME\n");
1035 if (tmp & VA_ENC_INTERLACED_FIELD)
1036 printf("Support VA_ENC_INTERLACED_FIELD\n");
1037 if (tmp & VA_ENC_INTERLACED_MBAFF)
1038 printf("Support VA_ENC_INTERLACED_MBAFF\n");
1039 if (tmp & VA_ENC_INTERLACED_PAFF)
1040 printf("Support VA_ENC_INTERLACED_PAFF\n");
1042 config_attrib[config_attrib_num].type = VAConfigAttribEncInterlaced;
1043 config_attrib[config_attrib_num].value = VA_ENC_PACKED_HEADER_NONE;
1044 config_attrib_num++;
1047 if (attrib[VAConfigAttribEncMaxRefFrames].value != VA_ATTRIB_NOT_SUPPORTED) {
1048 h264_maxref = attrib[VAConfigAttribEncMaxRefFrames].value;
1050 printf("Support %d RefPicList0 and %d RefPicList1\n",
1051 h264_maxref & 0xffff, (h264_maxref >> 16) & 0xffff );
1054 if (attrib[VAConfigAttribEncMaxSlices].value != VA_ATTRIB_NOT_SUPPORTED)
1055 printf("Support %d slices\n", attrib[VAConfigAttribEncMaxSlices].value);
1057 if (attrib[VAConfigAttribEncSliceStructure].value != VA_ATTRIB_NOT_SUPPORTED) {
1058 int tmp = attrib[VAConfigAttribEncSliceStructure].value;
1060 printf("Support VAConfigAttribEncSliceStructure\n");
1062 if (tmp & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS)
1063 printf("Support VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS\n");
1064 if (tmp & VA_ENC_SLICE_STRUCTURE_POWER_OF_TWO_ROWS)
1065 printf("Support VA_ENC_SLICE_STRUCTURE_POWER_OF_TWO_ROWS\n");
1066 if (tmp & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS)
1067 printf("Support VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS\n");
1069 if (attrib[VAConfigAttribEncMacroblockInfo].value != VA_ATTRIB_NOT_SUPPORTED) {
1070 printf("Support VAConfigAttribEncMacroblockInfo\n");
1076 static int setup_encode()
1079 VASurfaceID *tmp_surfaceid;
1080 int codedbuf_size, i;
1082 va_status = vaCreateConfig(va_dpy, h264_profile, VAEntrypointEncSlice,
1083 &config_attrib[0], config_attrib_num, &config_id);
1084 CHECK_VASTATUS(va_status, "vaCreateConfig");
1086 /* create source surfaces */
1087 va_status = vaCreateSurfaces(va_dpy,
1088 VA_RT_FORMAT_YUV420, frame_width_mbaligned, frame_height_mbaligned,
1089 &src_surface[0], SURFACE_NUM,
1091 CHECK_VASTATUS(va_status, "vaCreateSurfaces");
1093 /* create reference surfaces */
1094 va_status = vaCreateSurfaces(
1096 VA_RT_FORMAT_YUV420, frame_width_mbaligned, frame_height_mbaligned,
1097 &ref_surface[0], SURFACE_NUM,
1100 CHECK_VASTATUS(va_status, "vaCreateSurfaces");
1102 tmp_surfaceid = calloc(2 * SURFACE_NUM, sizeof(VASurfaceID));
1103 memcpy(tmp_surfaceid, src_surface, SURFACE_NUM * sizeof(VASurfaceID));
1104 memcpy(tmp_surfaceid + SURFACE_NUM, ref_surface, SURFACE_NUM * sizeof(VASurfaceID));
1106 /* Create a context for this encode pipe */
1107 va_status = vaCreateContext(va_dpy, config_id,
1108 frame_width_mbaligned, frame_height_mbaligned,
1110 tmp_surfaceid, 2 * SURFACE_NUM,
1112 CHECK_VASTATUS(va_status, "vaCreateContext");
1113 free(tmp_surfaceid);
1115 codedbuf_size = (frame_width_mbaligned * frame_height_mbaligned * 400) / (16*16);
1117 for (i = 0; i < SURFACE_NUM; i++) {
1118 /* create coded buffer once for all
1119 * other VA buffers which won't be used again after vaRenderPicture.
1120 * so APP can always vaCreateBuffer for every frame
1121 * but coded buffer need to be mapped and accessed after vaRenderPicture/vaEndPicture
1122 * so VA won't maintain the coded buffer
1124 va_status = vaCreateBuffer(va_dpy,context_id,VAEncCodedBufferType,
1125 codedbuf_size, 1, NULL, &coded_buf[i]);
1126 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1134 #define partition(ref, field, key, ascending) \
1137 while (ref[i].field < key) \
1139 while (ref[j].field > key) \
1142 while (ref[i].field > key) \
1144 while (ref[j].field < key) \
1156 static void sort_one(VAPictureH264 ref[], int left, int right,
1157 int ascending, int frame_idx)
1159 int i = left, j = right;
1164 key = ref[(left + right) / 2].frame_idx;
1165 partition(ref, frame_idx, key, ascending);
1167 key = ref[(left + right) / 2].TopFieldOrderCnt;
1168 partition(ref, TopFieldOrderCnt, (signed int)key, ascending);
1173 sort_one(ref, left, j, ascending, frame_idx);
1176 sort_one(ref, i, right, ascending, frame_idx);
1179 static void sort_two(VAPictureH264 ref[], int left, int right, unsigned int key, unsigned int frame_idx,
1180 int partition_ascending, int list0_ascending, int list1_ascending)
1182 int i = left, j = right;
1186 partition(ref, frame_idx, key, partition_ascending);
1188 partition(ref, TopFieldOrderCnt, (signed int)key, partition_ascending);
1192 sort_one(ref, left, i-1, list0_ascending, frame_idx);
1193 sort_one(ref, j+1, right, list1_ascending, frame_idx);
1196 static int update_ReferenceFrames(void)
1200 if (current_frame_type == FRAME_B)
1203 CurrentCurrPic.flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
1205 if (numShortTerm > num_ref_frames)
1206 numShortTerm = num_ref_frames;
1207 for (i=numShortTerm-1; i>0; i--)
1208 ReferenceFrames[i] = ReferenceFrames[i-1];
1209 ReferenceFrames[0] = CurrentCurrPic;
1211 if (current_frame_type != FRAME_B)
1212 current_frame_num++;
1213 if (current_frame_num > MaxFrameNum)
1214 current_frame_num = 0;
1220 static int update_RefPicList(void)
1222 unsigned int current_poc = CurrentCurrPic.TopFieldOrderCnt;
1224 if (current_frame_type == FRAME_P) {
1225 memcpy(RefPicList0_P, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
1226 sort_one(RefPicList0_P, 0, numShortTerm-1, 0, 1);
1229 if (current_frame_type == FRAME_B) {
1230 memcpy(RefPicList0_B, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
1231 sort_two(RefPicList0_B, 0, numShortTerm-1, current_poc, 0,
1234 memcpy(RefPicList1_B, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
1235 sort_two(RefPicList1_B, 0, numShortTerm-1, current_poc, 0,
1243 static int render_sequence(void)
1245 VABufferID seq_param_buf, rc_param_buf, misc_param_tmpbuf, render_id[2];
1247 VAEncMiscParameterBuffer *misc_param, *misc_param_tmp;
1248 VAEncMiscParameterRateControl *misc_rate_ctrl;
1250 seq_param.level_idc = 41 /*SH_LEVEL_3*/;
1251 seq_param.picture_width_in_mbs = frame_width_mbaligned / 16;
1252 seq_param.picture_height_in_mbs = frame_height_mbaligned / 16;
1253 seq_param.bits_per_second = frame_bitrate;
1255 seq_param.intra_period = intra_period;
1256 seq_param.intra_idr_period = intra_idr_period;
1257 seq_param.ip_period = ip_period;
1259 seq_param.max_num_ref_frames = num_ref_frames;
1260 seq_param.seq_fields.bits.frame_mbs_only_flag = 1;
1261 seq_param.time_scale = 900;
1262 seq_param.num_units_in_tick = 15; /* Tc = num_units_in_tick / time_sacle */
1263 seq_param.seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 = Log2MaxPicOrderCntLsb - 4;
1264 seq_param.seq_fields.bits.log2_max_frame_num_minus4 = Log2MaxFrameNum - 4;;
1265 seq_param.seq_fields.bits.frame_mbs_only_flag = 1;
1266 seq_param.seq_fields.bits.chroma_format_idc = 1;
1267 if (frame_width != frame_width_mbaligned ||
1268 frame_height != frame_height_mbaligned) {
1269 seq_param.frame_cropping_flag = 1;
1270 seq_param.frame_crop_left_offset = 0;
1271 seq_param.frame_crop_right_offset = (frame_width_mbaligned - frame_width)/2;
1272 seq_param.frame_crop_top_offset = 0;
1273 seq_param.frame_crop_bottom_offset = (frame_height_mbaligned - frame_height)/2;
1276 va_status = vaCreateBuffer(va_dpy, context_id,
1277 VAEncSequenceParameterBufferType,
1278 sizeof(seq_param),1,&seq_param,&seq_param_buf);
1279 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1281 va_status = vaCreateBuffer(va_dpy, context_id,
1282 VAEncMiscParameterBufferType,
1283 sizeof(VAEncMiscParameterBuffer) + sizeof(VAEncMiscParameterRateControl),
1284 1,NULL,&rc_param_buf);
1285 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1287 vaMapBuffer(va_dpy, rc_param_buf,(void **)&misc_param);
1288 misc_param->type = VAEncMiscParameterTypeRateControl;
1289 misc_rate_ctrl = (VAEncMiscParameterRateControl *)misc_param->data;
1290 memset(misc_rate_ctrl, 0, sizeof(*misc_rate_ctrl));
1291 misc_rate_ctrl->bits_per_second = frame_bitrate;
1292 misc_rate_ctrl->initial_qp = initial_qp;
1293 misc_rate_ctrl->min_qp = minimal_qp;
1294 misc_rate_ctrl->basic_unit_size = 0;
1295 vaUnmapBuffer(va_dpy, rc_param_buf);
1297 render_id[0] = seq_param_buf;
1298 render_id[1] = rc_param_buf;
1300 va_status = vaRenderPicture(va_dpy,context_id, &render_id[0], 2);
1301 CHECK_VASTATUS(va_status,"vaRenderPicture");;
1303 if (misc_priv_type != 0) {
1304 va_status = vaCreateBuffer(va_dpy, context_id,
1305 VAEncMiscParameterBufferType,
1306 sizeof(VAEncMiscParameterBuffer),
1307 1, NULL, &misc_param_tmpbuf);
1308 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1309 vaMapBuffer(va_dpy, misc_param_tmpbuf,(void **)&misc_param_tmp);
1310 misc_param_tmp->type = misc_priv_type;
1311 misc_param_tmp->data[0] = misc_priv_value;
1312 vaUnmapBuffer(va_dpy, misc_param_tmpbuf);
1314 va_status = vaRenderPicture(va_dpy,context_id, &misc_param_tmpbuf, 1);
1320 static int calc_poc(int pic_order_cnt_lsb)
1322 static int PicOrderCntMsb_ref = 0, pic_order_cnt_lsb_ref = 0;
1323 int prevPicOrderCntMsb, prevPicOrderCntLsb;
1324 int PicOrderCntMsb, TopFieldOrderCnt;
1326 if (current_frame_type == FRAME_IDR)
1327 prevPicOrderCntMsb = prevPicOrderCntLsb = 0;
1329 prevPicOrderCntMsb = PicOrderCntMsb_ref;
1330 prevPicOrderCntLsb = pic_order_cnt_lsb_ref;
1333 if ((pic_order_cnt_lsb < prevPicOrderCntLsb) &&
1334 ((prevPicOrderCntLsb - pic_order_cnt_lsb) >= (int)(MaxPicOrderCntLsb / 2)))
1335 PicOrderCntMsb = prevPicOrderCntMsb + MaxPicOrderCntLsb;
1336 else if ((pic_order_cnt_lsb > prevPicOrderCntLsb) &&
1337 ((pic_order_cnt_lsb - prevPicOrderCntLsb) > (int)(MaxPicOrderCntLsb / 2)))
1338 PicOrderCntMsb = prevPicOrderCntMsb - MaxPicOrderCntLsb;
1340 PicOrderCntMsb = prevPicOrderCntMsb;
1342 TopFieldOrderCnt = PicOrderCntMsb + pic_order_cnt_lsb;
1344 if (current_frame_type != FRAME_B) {
1345 PicOrderCntMsb_ref = PicOrderCntMsb;
1346 pic_order_cnt_lsb_ref = pic_order_cnt_lsb;
1349 return TopFieldOrderCnt;
1352 static int render_picture(void)
1354 VABufferID pic_param_buf;
1358 pic_param.CurrPic.picture_id = ref_surface[current_slot];
1359 pic_param.CurrPic.frame_idx = current_frame_num;
1360 pic_param.CurrPic.flags = 0;
1361 pic_param.CurrPic.TopFieldOrderCnt = calc_poc((current_frame_display - current_IDR_display) % MaxPicOrderCntLsb);
1362 pic_param.CurrPic.BottomFieldOrderCnt = pic_param.CurrPic.TopFieldOrderCnt;
1363 CurrentCurrPic = pic_param.CurrPic;
1365 if (getenv("TO_DEL")) { /* set RefPicList into ReferenceFrames */
1366 update_RefPicList(); /* calc RefPicList */
1367 memset(pic_param.ReferenceFrames, 0xff, 16 * sizeof(VAPictureH264)); /* invalid all */
1368 if (current_frame_type == FRAME_P) {
1369 pic_param.ReferenceFrames[0] = RefPicList0_P[0];
1370 } else if (current_frame_type == FRAME_B) {
1371 pic_param.ReferenceFrames[0] = RefPicList0_B[0];
1372 pic_param.ReferenceFrames[1] = RefPicList1_B[0];
1375 memcpy(pic_param.ReferenceFrames, ReferenceFrames, numShortTerm*sizeof(VAPictureH264));
1376 for (i = numShortTerm; i < SURFACE_NUM; i++) {
1377 pic_param.ReferenceFrames[i].picture_id = VA_INVALID_SURFACE;
1378 pic_param.ReferenceFrames[i].flags = VA_PICTURE_H264_INVALID;
1382 pic_param.pic_fields.bits.idr_pic_flag = (current_frame_type == FRAME_IDR);
1383 pic_param.pic_fields.bits.reference_pic_flag = (current_frame_type != FRAME_B);
1384 pic_param.pic_fields.bits.entropy_coding_mode_flag = 1;
1385 pic_param.pic_fields.bits.deblocking_filter_control_present_flag = 1;
1386 pic_param.frame_num = current_frame_num;
1387 pic_param.coded_buf = coded_buf[current_slot];
1388 pic_param.last_picture = (current_frame_encoding == frame_count);
1389 pic_param.pic_init_qp = initial_qp;
1391 va_status = vaCreateBuffer(va_dpy, context_id,VAEncPictureParameterBufferType,
1392 sizeof(pic_param),1,&pic_param, &pic_param_buf);
1393 CHECK_VASTATUS(va_status,"vaCreateBuffer");;
1395 va_status = vaRenderPicture(va_dpy,context_id, &pic_param_buf, 1);
1396 CHECK_VASTATUS(va_status,"vaRenderPicture");
1401 static int render_packedsequence(void)
1403 VAEncPackedHeaderParameterBuffer packedheader_param_buffer;
1404 VABufferID packedseq_para_bufid, packedseq_data_bufid, render_id[2];
1405 unsigned int length_in_bits;
1406 unsigned char *packedseq_buffer = NULL;
1409 length_in_bits = build_packed_seq_buffer(&packedseq_buffer);
1411 packedheader_param_buffer.type = VAEncPackedHeaderSequence;
1413 packedheader_param_buffer.bit_length = length_in_bits; /*length_in_bits*/
1414 packedheader_param_buffer.has_emulation_bytes = 0;
1415 va_status = vaCreateBuffer(va_dpy,
1417 VAEncPackedHeaderParameterBufferType,
1418 sizeof(packedheader_param_buffer), 1, &packedheader_param_buffer,
1419 &packedseq_para_bufid);
1420 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1422 va_status = vaCreateBuffer(va_dpy,
1424 VAEncPackedHeaderDataBufferType,
1425 (length_in_bits + 7) / 8, 1, packedseq_buffer,
1426 &packedseq_data_bufid);
1427 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1429 render_id[0] = packedseq_para_bufid;
1430 render_id[1] = packedseq_data_bufid;
1431 va_status = vaRenderPicture(va_dpy,context_id, render_id, 2);
1432 CHECK_VASTATUS(va_status,"vaRenderPicture");
1434 free(packedseq_buffer);
1440 static int render_packedpicture(void)
1442 VAEncPackedHeaderParameterBuffer packedheader_param_buffer;
1443 VABufferID packedpic_para_bufid, packedpic_data_bufid, render_id[2];
1444 unsigned int length_in_bits;
1445 unsigned char *packedpic_buffer = NULL;
1448 length_in_bits = build_packed_pic_buffer(&packedpic_buffer);
1449 packedheader_param_buffer.type = VAEncPackedHeaderPicture;
1450 packedheader_param_buffer.bit_length = length_in_bits;
1451 packedheader_param_buffer.has_emulation_bytes = 0;
1453 va_status = vaCreateBuffer(va_dpy,
1455 VAEncPackedHeaderParameterBufferType,
1456 sizeof(packedheader_param_buffer), 1, &packedheader_param_buffer,
1457 &packedpic_para_bufid);
1458 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1460 va_status = vaCreateBuffer(va_dpy,
1462 VAEncPackedHeaderDataBufferType,
1463 (length_in_bits + 7) / 8, 1, packedpic_buffer,
1464 &packedpic_data_bufid);
1465 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1467 render_id[0] = packedpic_para_bufid;
1468 render_id[1] = packedpic_data_bufid;
1469 va_status = vaRenderPicture(va_dpy,context_id, render_id, 2);
1470 CHECK_VASTATUS(va_status,"vaRenderPicture");
1472 free(packedpic_buffer);
1477 static void render_packedsei(void)
1479 VAEncPackedHeaderParameterBuffer packed_header_param_buffer;
1480 VABufferID packed_sei_header_param_buf_id, packed_sei_buf_id, render_id[2];
1481 unsigned int length_in_bits /*offset_in_bytes*/;
1482 unsigned char *packed_sei_buffer = NULL;
1484 int init_cpb_size, target_bit_rate, i_initial_cpb_removal_delay_length, i_initial_cpb_removal_delay;
1485 int i_cpb_removal_delay, i_dpb_output_delay_length, i_cpb_removal_delay_length;
1487 /* it comes for the bps defined in SPS */
1488 target_bit_rate = frame_bitrate;
1489 init_cpb_size = (target_bit_rate * 8) >> 10;
1490 i_initial_cpb_removal_delay = init_cpb_size * 0.5 * 1024 / target_bit_rate * 90000;
1492 i_cpb_removal_delay = 2;
1493 i_initial_cpb_removal_delay_length = 24;
1494 i_cpb_removal_delay_length = 24;
1495 i_dpb_output_delay_length = 24;
1498 length_in_bits = build_packed_sei_buffer_timing(
1499 i_initial_cpb_removal_delay_length,
1500 i_initial_cpb_removal_delay,
1502 i_cpb_removal_delay_length,
1503 i_cpb_removal_delay * current_frame_encoding,
1504 i_dpb_output_delay_length,
1506 &packed_sei_buffer);
1508 //offset_in_bytes = 0;
1509 packed_header_param_buffer.type = VAEncPackedHeaderH264_SEI;
1510 packed_header_param_buffer.bit_length = length_in_bits;
1511 packed_header_param_buffer.has_emulation_bytes = 0;
1513 va_status = vaCreateBuffer(va_dpy,
1515 VAEncPackedHeaderParameterBufferType,
1516 sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
1517 &packed_sei_header_param_buf_id);
1518 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1520 va_status = vaCreateBuffer(va_dpy,
1522 VAEncPackedHeaderDataBufferType,
1523 (length_in_bits + 7) / 8, 1, packed_sei_buffer,
1524 &packed_sei_buf_id);
1525 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1528 render_id[0] = packed_sei_header_param_buf_id;
1529 render_id[1] = packed_sei_buf_id;
1530 va_status = vaRenderPicture(va_dpy,context_id, render_id, 2);
1531 CHECK_VASTATUS(va_status,"vaRenderPicture");
1534 free(packed_sei_buffer);
1540 static int render_hrd(void)
1542 VABufferID misc_parameter_hrd_buf_id;
1544 VAEncMiscParameterBuffer *misc_param;
1545 VAEncMiscParameterHRD *misc_hrd_param;
1547 va_status = vaCreateBuffer(va_dpy, context_id,
1548 VAEncMiscParameterBufferType,
1549 sizeof(VAEncMiscParameterBuffer) + sizeof(VAEncMiscParameterHRD),
1552 &misc_parameter_hrd_buf_id);
1553 CHECK_VASTATUS(va_status, "vaCreateBuffer");
1556 misc_parameter_hrd_buf_id,
1557 (void **)&misc_param);
1558 misc_param->type = VAEncMiscParameterTypeHRD;
1559 misc_hrd_param = (VAEncMiscParameterHRD *)misc_param->data;
1561 if (frame_bitrate > 0) {
1562 misc_hrd_param->initial_buffer_fullness = frame_bitrate * 1024 * 4;
1563 misc_hrd_param->buffer_size = frame_bitrate * 1024 * 8;
1565 misc_hrd_param->initial_buffer_fullness = 0;
1566 misc_hrd_param->buffer_size = 0;
1568 vaUnmapBuffer(va_dpy, misc_parameter_hrd_buf_id);
1570 va_status = vaRenderPicture(va_dpy,context_id, &misc_parameter_hrd_buf_id, 1);
1571 CHECK_VASTATUS(va_status,"vaRenderPicture");;
1576 static int render_slice(void)
1578 VABufferID slice_param_buf;
1582 update_RefPicList();
1584 /* one frame, one slice */
1585 slice_param.macroblock_address = 0;
1586 slice_param.num_macroblocks = frame_width_mbaligned * frame_height_mbaligned/(16*16); /* Measured by MB */
1587 slice_param.slice_type = (current_frame_type == FRAME_IDR)?2:current_frame_type;
1588 if (current_frame_type == FRAME_IDR) {
1589 ++slice_param.idr_pic_id;
1590 } else if (current_frame_type == FRAME_P) {
1591 int refpiclist0_max = h264_maxref & 0xffff;
1592 memcpy(slice_param.RefPicList0, RefPicList0_P, refpiclist0_max*sizeof(VAPictureH264));
1594 for (i = refpiclist0_max; i < 32; i++) {
1595 slice_param.RefPicList0[i].picture_id = VA_INVALID_SURFACE;
1596 slice_param.RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
1598 } else if (current_frame_type == FRAME_B) {
1599 int refpiclist0_max = h264_maxref & 0xffff;
1600 int refpiclist1_max = (h264_maxref >> 16) & 0xffff;
1602 memcpy(slice_param.RefPicList0, RefPicList0_B, refpiclist0_max*sizeof(VAPictureH264));
1603 for (i = refpiclist0_max; i < 32; i++) {
1604 slice_param.RefPicList0[i].picture_id = VA_INVALID_SURFACE;
1605 slice_param.RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
1608 memcpy(slice_param.RefPicList1, RefPicList1_B, refpiclist1_max*sizeof(VAPictureH264));
1609 for (i = refpiclist1_max; i < 32; i++) {
1610 slice_param.RefPicList1[i].picture_id = VA_INVALID_SURFACE;
1611 slice_param.RefPicList1[i].flags = VA_PICTURE_H264_INVALID;
1615 slice_param.slice_alpha_c0_offset_div2 = 2;
1616 slice_param.slice_beta_offset_div2 = 2;
1617 slice_param.pic_order_cnt_lsb = (current_frame_display - current_IDR_display) % MaxPicOrderCntLsb;
1619 va_status = vaCreateBuffer(va_dpy,context_id,VAEncSliceParameterBufferType,
1620 sizeof(slice_param),1,&slice_param,&slice_param_buf);
1621 CHECK_VASTATUS(va_status,"vaCreateBuffer");;
1623 va_status = vaRenderPicture(va_dpy,context_id, &slice_param_buf, 1);
1624 CHECK_VASTATUS(va_status,"vaRenderPicture");
1630 static int upload_source_YUV_once_for_all()
1636 for (i = 0; i < SURFACE_NUM; i++) {
1637 printf("\rLoading data into surface %d.....", i);
1638 upload_surface(va_dpy, src_surface[i], box_width, row_shift, 0);
1641 if (row_shift==(2*box_width)) row_shift= 0;
1643 printf("Complete surface loading\n");
1648 #define check_ret(ret) \
1650 printf("fread doesn't return enough data\n"); \
1653 static int load_surface(VASurfaceID surface_id, unsigned long long display_order)
1655 unsigned char *src_Y = NULL, *src_U = NULL, *src_V = NULL;
1658 if (srcyuv_fp == NULL)
1661 /* rewind the file pointer if encoding more than srcyuv_frames */
1662 display_order = display_order % srcyuv_frames;
1663 fseek(srcyuv_fp, display_order * frame_width * frame_height * 1.5, SEEK_SET);
1666 if (srcyuv_fourcc == VA_FOURCC_NV12) {
1667 int uv_size = 2 * (frame_width/2) * (frame_height/2);
1669 src_Y = malloc(2 * uv_size);
1670 src_U = malloc(uv_size);
1672 ret = fread(src_Y, frame_width * frame_height, 1, srcyuv_fp);
1674 ret = fread(src_U, uv_size, 1, srcyuv_fp);
1676 } else if (srcyuv_fourcc == VA_FOURCC_IYUV ||
1677 srcyuv_fourcc == VA_FOURCC_YV12) {
1678 int uv_size = (frame_width/2) * (frame_height/2);
1680 src_Y = malloc(4 * uv_size);
1681 src_U = malloc(uv_size);
1682 src_V = malloc(uv_size);
1684 ret = fread(src_Y, frame_width * frame_height, 1, srcyuv_fp);
1686 if (srcyuv_fourcc == VA_FOURCC_IYUV) {
1687 ret = fread(src_U, uv_size, 1, srcyuv_fp);
1689 ret = fread(src_V, uv_size, 1, srcyuv_fp);
1692 ret = fread(src_V, uv_size, 1, srcyuv_fp);
1694 ret = fread(src_U, uv_size, 1, srcyuv_fp);
1698 printf("Unsupported source YUV format\n");
1702 upload_surface_yuv(va_dpy, surface_id,
1703 srcyuv_fourcc, frame_width, frame_height,
1704 src_Y, src_U, src_V);
1716 static int save_recyuv(VASurfaceID surface_id,
1717 unsigned long long display_order,
1718 unsigned long long encode_order)
1720 unsigned char *dst_Y = NULL, *dst_U = NULL, *dst_V = NULL;
1722 if (recyuv_fp == NULL)
1725 if (srcyuv_fourcc == VA_FOURCC_NV12) {
1726 int uv_size = 2 * (frame_width/2) * (frame_height/2);
1727 dst_Y = malloc(2*uv_size);
1728 dst_U = malloc(uv_size);
1729 } else if (srcyuv_fourcc == VA_FOURCC_IYUV ||
1730 srcyuv_fourcc == VA_FOURCC_YV12) {
1731 int uv_size = (frame_width/2) * (frame_height/2);
1732 dst_Y = malloc(4*uv_size);
1733 dst_U = malloc(uv_size);
1734 dst_V = malloc(uv_size);
1736 printf("Unsupported source YUV format\n");
1740 download_surface_yuv(va_dpy, surface_id,
1741 srcyuv_fourcc, frame_width, frame_height,
1742 dst_Y, dst_U, dst_V);
1743 fseek(recyuv_fp, display_order * frame_width * frame_height * 1.5, SEEK_SET);
1745 if (srcyuv_fourcc == VA_FOURCC_NV12) {
1746 int uv_size = 2 * (frame_width/2) * (frame_height/2);
1747 fwrite(dst_Y, uv_size * 2, 1, recyuv_fp);
1748 fwrite(dst_U, uv_size, 1, recyuv_fp);
1749 } else if (srcyuv_fourcc == VA_FOURCC_IYUV ||
1750 srcyuv_fourcc == VA_FOURCC_YV12) {
1751 int uv_size = (frame_width/2) * (frame_height/2);
1752 fwrite(dst_Y, uv_size * 4, 1, recyuv_fp);
1754 if (srcyuv_fourcc == VA_FOURCC_IYUV) {
1755 fwrite(dst_U, uv_size, 1, recyuv_fp);
1756 fwrite(dst_V, uv_size, 1, recyuv_fp);
1758 fwrite(dst_V, uv_size, 1, recyuv_fp);
1759 fwrite(dst_U, uv_size, 1, recyuv_fp);
1762 printf("Unsupported YUV format\n");
1777 static int save_codeddata(unsigned long long display_order, unsigned long long encode_order)
1779 VACodedBufferSegment *buf_list = NULL;
1781 unsigned int coded_size = 0;
1783 va_status = vaMapBuffer(va_dpy,coded_buf[display_order % SURFACE_NUM],(void **)(&buf_list));
1784 CHECK_VASTATUS(va_status,"vaMapBuffer");
1785 while (buf_list != NULL) {
1786 coded_size += fwrite(buf_list->buf, 1, buf_list->size, coded_fp);
1787 buf_list = (VACodedBufferSegment *) buf_list->next;
1789 frame_size += coded_size;
1791 vaUnmapBuffer(va_dpy,coded_buf[display_order % SURFACE_NUM]);
1793 printf("\r "); /* return back to startpoint */
1794 switch (encode_order % 4) {
1808 printf("%08lld", encode_order);
1810 if (current_frame_encoding % intra_count == 0)
1815 printf("(%06d bytes coded)",coded_size);
1816 /* skipped frame ? */
1823 static struct storage_task_t * storage_task_dequeue(void)
1825 struct storage_task_t *header;
1827 pthread_mutex_lock(&encode_mutex);
1829 header = storage_task_header;
1830 if (storage_task_header != NULL) {
1831 if (storage_task_tail == storage_task_header)
1832 storage_task_tail = NULL;
1833 storage_task_header = header->next;
1836 pthread_mutex_unlock(&encode_mutex);
1841 static int storage_task_queue(unsigned long long display_order, unsigned long long encode_order)
1843 struct storage_task_t *tmp;
1845 tmp = calloc(1, sizeof(struct storage_task_t));
1846 tmp->display_order = display_order;
1847 tmp->encode_order = encode_order;
1849 pthread_mutex_lock(&encode_mutex);
1851 if (storage_task_header == NULL) {
1852 storage_task_header = tmp;
1853 storage_task_tail = tmp;
1855 storage_task_tail->next = tmp;
1856 storage_task_tail = tmp;
1859 srcsurface_status[display_order % SURFACE_NUM] = SRC_SURFACE_IN_STORAGE;
1860 pthread_cond_signal(&encode_cond);
1862 pthread_mutex_unlock(&encode_mutex);
1867 static void storage_task(unsigned long long display_order, unsigned long long encode_order)
1872 tmp = GetTickCount();
1873 va_status = vaSyncSurface(va_dpy, src_surface[display_order % SURFACE_NUM]);
1874 CHECK_VASTATUS(va_status,"vaSyncSurface");
1875 SyncPictureTicks += GetTickCount() - tmp;
1876 tmp = GetTickCount();
1877 save_codeddata(display_order, encode_order);
1878 SavePictureTicks += GetTickCount() - tmp;
1880 save_recyuv(ref_surface[display_order % SURFACE_NUM], display_order, encode_order);
1882 /* reload a new frame data */
1883 tmp = GetTickCount();
1884 if (srcyuv_fp != NULL)
1885 load_surface(src_surface[display_order % SURFACE_NUM], display_order + SURFACE_NUM);
1886 UploadPictureTicks += GetTickCount() - tmp;
1888 pthread_mutex_lock(&encode_mutex);
1889 srcsurface_status[display_order % SURFACE_NUM] = SRC_SURFACE_IN_ENCODING;
1890 pthread_mutex_unlock(&encode_mutex);
1894 static void * storage_task_thread(void *t)
1897 struct storage_task_t *current;
1899 current = storage_task_dequeue();
1900 if (current == NULL) {
1901 pthread_mutex_lock(&encode_mutex);
1902 pthread_cond_wait(&encode_cond, &encode_mutex);
1903 pthread_mutex_unlock(&encode_mutex);
1907 storage_task(current->display_order, current->encode_order);
1911 /* all frames are saved, exit the thread */
1912 if (++frame_coded >= frame_count)
1920 static int encode_frames(void)
1922 unsigned int i, tmp;
1924 //VASurfaceStatus surface_status;
1926 /* upload RAW YUV data into all surfaces */
1927 tmp = GetTickCount();
1928 if (srcyuv_fp != NULL) {
1929 for (i = 0; i < SURFACE_NUM; i++)
1930 load_surface(src_surface[i], i);
1932 upload_source_YUV_once_for_all();
1933 UploadPictureTicks += GetTickCount() - tmp;
1935 /* ready for encoding */
1936 memset(srcsurface_status, SRC_SURFACE_IN_ENCODING, sizeof(srcsurface_status));
1938 memset(&seq_param, 0, sizeof(seq_param));
1939 memset(&pic_param, 0, sizeof(pic_param));
1940 memset(&slice_param, 0, sizeof(slice_param));
1942 if (encode_syncmode == 0)
1943 pthread_create(&encode_thread, NULL, storage_task_thread, NULL);
1945 for (current_frame_encoding = 0; current_frame_encoding < frame_count; current_frame_encoding++) {
1946 encoding2display_order(current_frame_encoding, intra_period, intra_idr_period, ip_period,
1947 ¤t_frame_display, ¤t_frame_type);
1948 if (current_frame_type == FRAME_IDR) {
1950 current_frame_num = 0;
1951 current_IDR_display = current_frame_display;
1954 /* check if the source frame is ready */
1955 while (srcsurface_status[current_slot] != SRC_SURFACE_IN_ENCODING) {
1959 tmp = GetTickCount();
1960 va_status = vaBeginPicture(va_dpy, context_id, src_surface[current_slot]);
1961 CHECK_VASTATUS(va_status,"vaBeginPicture");
1962 BeginPictureTicks += GetTickCount() - tmp;
1964 tmp = GetTickCount();
1965 if (current_frame_type == FRAME_IDR) {
1968 if (h264_packedheader) {
1969 render_packedsequence();
1970 render_packedpicture();
1972 //if (rc_mode == VA_RC_CBR)
1973 // render_packedsei();
1976 //render_sequence();
1978 //if (rc_mode == VA_RC_CBR)
1979 // render_packedsei();
1983 RenderPictureTicks += GetTickCount() - tmp;
1985 tmp = GetTickCount();
1986 va_status = vaEndPicture(va_dpy,context_id);
1987 CHECK_VASTATUS(va_status,"vaEndPicture");;
1988 EndPictureTicks += GetTickCount() - tmp;
1990 if (encode_syncmode)
1991 storage_task(current_frame_display, current_frame_encoding);
1992 else /* queue the storage task queue */
1993 storage_task_queue(current_frame_display, current_frame_encoding);
1995 /* how to process skipped frames
1996 surface_status = (VASurfaceStatus) 0;
1997 va_status = vaQuerySurfaceStatus(va_dpy, src_surface[i%SURFACE_NUM],&surface_status);
1998 frame_skipped = (surface_status & VASurfaceSkipped);
2000 update_ReferenceFrames();
2003 if (encode_syncmode == 0) {
2005 pthread_join(encode_thread, (void **)&ret);
2012 static int release_encode()
2016 vaDestroySurfaces(va_dpy,&src_surface[0],SURFACE_NUM);
2017 vaDestroySurfaces(va_dpy,&ref_surface[0],SURFACE_NUM);
2019 for (i = 0; i < SURFACE_NUM; i++)
2020 vaDestroyBuffer(va_dpy,coded_buf[i]);
2022 vaDestroyContext(va_dpy,context_id);
2023 vaDestroyConfig(va_dpy,config_id);
2028 static int deinit_va()
2030 vaTerminate(va_dpy);
2032 va_close_display(va_dpy);
2038 static int print_input()
2040 printf("\n\nINPUT:Try to encode H264...\n");
2041 printf("INPUT: RateControl : %s\n", rc_to_string(rc_mode));
2042 printf("INPUT: Resolution : %dx%d, %d frames\n",
2043 frame_width, frame_height, frame_count);
2044 printf("INPUT: FrameRate : %d\n", frame_rate);
2045 printf("INPUT: Bitrate : %d\n", frame_bitrate);
2046 printf("INPUT: Slieces : %d\n", frame_slices);
2047 printf("INPUT: IntraPeriod : %d\n", intra_period);
2048 printf("INPUT: IDRPeriod : %d\n", intra_idr_period);
2049 printf("INPUT: IpPeriod : %d\n", ip_period);
2050 printf("INPUT: Initial QP : %d\n", initial_qp);
2051 printf("INPUT: Min QP : %d\n", minimal_qp);
2052 printf("INPUT: Source YUV : %s", srcyuv_fp?"FILE":"AUTO generated");
2054 printf(":%s (fourcc %s)\n", srcyuv_fn, fourcc_to_string(srcyuv_fourcc));
2057 printf("INPUT: Coded Clip : %s\n", coded_fn);
2058 if (recyuv_fp == NULL)
2059 printf("INPUT: Rec Clip : %s\n", "Not save reconstructed frame");
2061 printf("INPUT: Rec Clip : Save reconstructed frame into %s (fourcc %s)\n", recyuv_fn,
2062 fourcc_to_string(srcyuv_fourcc));
2064 printf("\n\n"); /* return back to startpoint */
2069 static int calc_PSNR(double *psnr)
2071 unsigned long srcyuv_size, recyuv_size, min_size;
2072 char *srcyuv_ptr, *recyuv_ptr;
2073 unsigned long i, sse=0;
2076 fseek(srcyuv_fp, 0L, SEEK_END);
2077 srcyuv_size = ftell(srcyuv_fp);
2078 fseek(recyuv_fp, 0L, SEEK_END);
2079 recyuv_size = ftell(recyuv_fp);
2081 fseek(srcyuv_fp, 0L, SEEK_SET);
2082 fseek(recyuv_fp, 0L, SEEK_SET);
2084 min_size = MIN(srcyuv_size,recyuv_size);
2085 srcyuv_ptr = mmap (0, min_size, PROT_READ, MAP_SHARED, fileno(srcyuv_fp), 0);
2086 recyuv_ptr = mmap (0, min_size, PROT_READ, MAP_SHARED, fileno(recyuv_fp), 0);
2087 if ((srcyuv_ptr == MAP_FAILED) || (recyuv_ptr == MAP_FAILED)) {
2088 printf("Failed to mmap YUV files\n");
2092 for (i=0; i<min_size; i++) {
2093 char tmp = srcyuv_ptr[i] - recyuv_ptr[i];
2096 ssemean = (double)sse/(double)min_size;
2097 *psnr = 20.0*log10(255) - 10.0*log10(ssemean);
2099 munmap(srcyuv_ptr, min_size);
2100 munmap(recyuv_ptr, min_size);
2105 static int print_performance(unsigned int PictureCount)
2107 unsigned int psnr_ret = 1, others = 0;
2108 double psnr = 0, total_size = frame_width * frame_height * 1.5 * frame_count;
2110 if (calc_psnr && srcyuv_fp && recyuv_fp)
2111 psnr_ret = calc_PSNR(&psnr);
2113 others = TotalTicks - UploadPictureTicks - BeginPictureTicks
2114 - RenderPictureTicks - EndPictureTicks - SyncPictureTicks - SavePictureTicks;
2118 printf("PERFORMANCE: Frame Rate : %.2f fps (%d frames, %d ms (%.2f ms per frame))\n",
2119 (double) 1000*PictureCount / TotalTicks, PictureCount,
2120 TotalTicks, ((double) TotalTicks) / (double) PictureCount);
2121 printf("PERFORMANCE: Compression ratio : %d:1\n", (unsigned int)(total_size / frame_size));
2123 printf("PERFORMANCE: PSNR : %.2f (%lld frames calculated)\n",
2124 psnr, MIN(frame_count, srcyuv_frames));
2126 printf("PERFORMANCE: UploadPicture : %d ms (%.2f, %.2f%% percent)\n",
2127 (int) UploadPictureTicks, ((double) UploadPictureTicks) / (double) PictureCount,
2128 UploadPictureTicks/(double) TotalTicks/0.01);
2129 printf("PERFORMANCE: vaBeginPicture : %d ms (%.2f, %.2f%% percent)\n",
2130 (int) BeginPictureTicks, ((double) BeginPictureTicks) / (double) PictureCount,
2131 BeginPictureTicks/(double) TotalTicks/0.01);
2132 printf("PERFORMANCE: vaRenderHeader : %d ms (%.2f, %.2f%% percent)\n",
2133 (int) RenderPictureTicks, ((double) RenderPictureTicks) / (double) PictureCount,
2134 RenderPictureTicks/(double) TotalTicks/0.01);
2135 printf("PERFORMANCE: vaEndPicture : %d ms (%.2f, %.2f%% percent)\n",
2136 (int) EndPictureTicks, ((double) EndPictureTicks) / (double) PictureCount,
2137 EndPictureTicks/(double) TotalTicks/0.01);
2138 printf("PERFORMANCE: vaSyncSurface : %d ms (%.2f, %.2f%% percent)\n",
2139 (int) SyncPictureTicks, ((double) SyncPictureTicks) / (double) PictureCount,
2140 SyncPictureTicks/(double) TotalTicks/0.01);
2141 printf("PERFORMANCE: SavePicture : %d ms (%.2f, %.2f%% percent)\n",
2142 (int) SavePictureTicks, ((double) SavePictureTicks) / (double) PictureCount,
2143 SavePictureTicks/(double) TotalTicks/0.01);
2144 printf("PERFORMANCE: Others : %d ms (%.2f, %.2f%% percent)\n",
2145 (int) others, ((double) others) / (double) PictureCount,
2146 others/(double) TotalTicks/0.01);
2148 if (encode_syncmode == 0)
2149 printf("(Multithread enabled, the timing is only for reference)\n");
2155 int main(int argc,char **argv)
2159 process_cmdline(argc, argv);
2163 start = GetTickCount();
2173 TotalTicks += GetTickCount() - start;
2174 print_performance(frame_count);