OSDN Git Service

h264encode: coding 1920x1080 with crop
[android-x86/hardware-intel-common-libva.git] / test / encode / h264encode.c
1 /*
2  * Copyright (c) 2007-2013 Intel Corporation. All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  * 
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  * 
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #include "sysdeps.h"
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <getopt.h>
29 #include <unistd.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/time.h>
33 #include <sys/mman.h>
34 #include <fcntl.h>
35 #include <assert.h>
36 #include <pthread.h>
37 #include <errno.h>
38 #include <math.h>
39 #include <va/va.h>
40 #include <va/va_enc_h264.h>
41 #include "va_display.h"
42
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__); \
46         exit(1);                                                        \
47     }
48
49 #include "../loadsurface.h"
50
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
55
56 #define NAL_NON_IDR             1
57 #define NAL_IDR                 5
58 #define NAL_SPS                 7
59 #define NAL_PPS                 8
60 #define NAL_SEI                 6
61
62 #define SLICE_TYPE_P            0
63 #define SLICE_TYPE_B            1
64 #define SLICE_TYPE_I            2
65
66 #define ENTROPY_MODE_CAVLC      0
67 #define ENTROPY_MODE_CABAC      1
68
69 #define PROFILE_IDC_BASELINE    66
70 #define PROFILE_IDC_MAIN        77
71 #define PROFILE_IDC_HIGH        100
72    
73 #define BITSTREAM_ALLOCATE_STEPPING     4096
74
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];
92
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;
97
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;
108
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)
131
132 static  int misc_priv_type = 0;
133 static  int misc_priv_value = 0;
134
135 #define MIN(a, b) ((a)>(b)?(b):(a))
136 #define MAX(a, b) ((a)>(b)?(a):(b))
137
138 /* thread to save coded data/upload source YUV */
139 struct storage_task_t {
140     void *next;
141     unsigned long long display_order;
142     unsigned long long encode_order;
143 };
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;
152     
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;
161
162 struct __bitstream {
163     unsigned int *buffer;
164     int bit_offset;
165     int max_size_in_dword;
166 };
167 typedef struct __bitstream bitstream;
168
169
170 static unsigned int 
171 va_swap32(unsigned int val)
172 {
173     unsigned char *pval = (unsigned char *)&val;
174
175     return ((pval[0] << 24)     |
176             (pval[1] << 16)     |
177             (pval[2] << 8)      |
178             (pval[3] << 0));
179 }
180
181 static void
182 bitstream_start(bitstream *bs)
183 {
184     bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
185     bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
186     bs->bit_offset = 0;
187 }
188
189 static void
190 bitstream_end(bitstream *bs)
191 {
192     int pos = (bs->bit_offset >> 5);
193     int bit_offset = (bs->bit_offset & 0x1f);
194     int bit_left = 32 - bit_offset;
195
196     if (bit_offset) {
197         bs->buffer[pos] = va_swap32((bs->buffer[pos] << bit_left));
198     }
199 }
200  
201 static void
202 bitstream_put_ui(bitstream *bs, unsigned int val, int size_in_bits)
203 {
204     int pos = (bs->bit_offset >> 5);
205     int bit_offset = (bs->bit_offset & 0x1f);
206     int bit_left = 32 - bit_offset;
207
208     if (!size_in_bits)
209         return;
210
211     bs->bit_offset += size_in_bits;
212
213     if (bit_left > size_in_bits) {
214         bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
215     } else {
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]);
219
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));
223         }
224
225         bs->buffer[pos + 1] = val;
226     }
227 }
228
229 static void
230 bitstream_put_ue(bitstream *bs, unsigned int val)
231 {
232     int size_in_bits = 0;
233     int tmp_val = ++val;
234
235     while (tmp_val) {
236         tmp_val >>= 1;
237         size_in_bits++;
238     }
239
240     bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
241     bitstream_put_ui(bs, val, size_in_bits);
242 }
243
244 static void
245 bitstream_put_se(bitstream *bs, int val)
246 {
247     unsigned int new_val;
248
249     if (val <= 0)
250         new_val = -2 * val;
251     else
252         new_val = 2 * val - 1;
253
254     bitstream_put_ue(bs, new_val);
255 }
256
257 static void
258 bitstream_byte_aligning(bitstream *bs, int bit)
259 {
260     int bit_offset = (bs->bit_offset & 0x7);
261     int bit_left = 8 - bit_offset;
262     int new_val;
263
264     if (!bit_offset)
265         return;
266
267     assert(bit == 0 || bit == 1);
268
269     if (bit)
270         new_val = (1 << bit_left) - 1;
271     else
272         new_val = 0;
273
274     bitstream_put_ui(bs, new_val, bit_left);
275 }
276
277 static void 
278 rbsp_trailing_bits(bitstream *bs)
279 {
280     bitstream_put_ui(bs, 1, 1);
281     bitstream_byte_aligning(bs, 0);
282 }
283
284 static void nal_start_code_prefix(bitstream *bs)
285 {
286     bitstream_put_ui(bs, 0x00000001, 32);
287 }
288
289 static void nal_header(bitstream *bs, int nal_ref_idc, int nal_unit_type)
290 {
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);
294 }
295
296 static void sps_rbsp(bitstream *bs)
297 {
298     int profile_idc = PROFILE_IDC_BASELINE;
299
300     if (h264_profile  == VAProfileH264High)
301         profile_idc = PROFILE_IDC_HIGH;
302     else if (h264_profile  == VAProfileH264Main)
303         profile_idc = PROFILE_IDC_MAIN;
304
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 */
313
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 */
320     }
321
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 */
324
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 */
327     else {
328         assert(0);
329     }
330
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 */
333
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 */
337
338     if (!seq_param.seq_fields.bits.frame_mbs_only_flag) {
339         assert(0);
340     }
341
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 */
344
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 */
350     }
351     
352     //if ( frame_bit_rate < 0 ) { //TODO EW: the vui header isn't correct
353     if ( 1 ) {
354         bitstream_put_ui(bs, 0, 1); /* vui_parameters_present_flag */
355     } else {
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 */
362         {
363             bitstream_put_ui(bs, 15, 32);
364             bitstream_put_ui(bs, 900, 32);
365             bitstream_put_ui(bs, 1, 1);
366         }
367         bitstream_put_ui(bs, 1, 1); /* nal_hrd_parameters_present_flag */
368         {
369             // hrd_parameters 
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 */
373            
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] */
377
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  */
382         }
383         bitstream_put_ui(bs, 0, 1);   /* vcl_hrd_parameters_present_flag */
384         bitstream_put_ui(bs, 0, 1);   /* low_delay_hrd_flag */ 
385
386         bitstream_put_ui(bs, 0, 1); /* pic_struct_present_flag */
387         bitstream_put_ui(bs, 0, 1); /* bitstream_restriction_flag */
388     }
389
390     rbsp_trailing_bits(bs);     /* rbsp_trailing_bits */
391 }
392
393
394 static void pps_rbsp(bitstream *bs)
395 {
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 */
398
399     bitstream_put_ui(bs, pic_param.pic_fields.bits.entropy_coding_mode_flag, 1);  /* entropy_coding_mode_flag */
400
401     bitstream_put_ui(bs, 0, 1);                         /* pic_order_present_flag: 0 */
402
403     bitstream_put_ue(bs, 0);                            /* num_slice_groups_minus1 */
404
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 */
407
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 */
410
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 */
414
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 */
418     
419     /* more_rbsp_data */
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 */
423
424     rbsp_trailing_bits(bs);
425 }
426
427
428 static int
429 build_packed_pic_buffer(unsigned char **header_buffer)
430 {
431     bitstream bs;
432
433     bitstream_start(&bs);
434     nal_start_code_prefix(&bs);
435     nal_header(&bs, NAL_REF_IDC_HIGH, NAL_PPS);
436     pps_rbsp(&bs);
437     bitstream_end(&bs);
438
439     *header_buffer = (unsigned char *)bs.buffer;
440     return bs.bit_offset;
441 }
442
443 static int
444 build_packed_seq_buffer(unsigned char **header_buffer)
445 {
446     bitstream bs;
447
448     bitstream_start(&bs);
449     nal_start_code_prefix(&bs);
450     nal_header(&bs, NAL_REF_IDC_HIGH, NAL_SPS);
451     sps_rbsp(&bs);
452     bitstream_end(&bs);
453
454     *header_buffer = (unsigned char *)bs.buffer;
455     return bs.bit_offset;
456 }
457
458 static int 
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)
467 {
468     unsigned char *byte_buf;
469     int bp_byte_size, i, pic_byte_size;
470
471     bitstream nal_bs;
472     bitstream sei_bp_bs, sei_pic_bs;
473
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);
480     }
481     bitstream_end(&sei_bp_bs);
482     bp_byte_size = (sei_bp_bs.bit_offset + 7) / 8;
483     
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);
489     }
490     bitstream_end(&sei_pic_bs);
491     pic_byte_size = (sei_pic_bs.bit_offset + 7) / 8;
492     
493     bitstream_start(&nal_bs);
494     nal_start_code_prefix(&nal_bs);
495     nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
496
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);
500     
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);
504     }
505     free(byte_buf);
506         /* write the SEI timing data */
507     bitstream_put_ui(&nal_bs, 0x01, 8);
508     bitstream_put_ui(&nal_bs, pic_byte_size, 8);
509     
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);
513     }
514     free(byte_buf);
515
516     rbsp_trailing_bits(&nal_bs);
517     bitstream_end(&nal_bs);
518
519     *sei_buffer = (unsigned char *)nal_bs.buffer; 
520    
521     return nal_bs.bit_offset;
522 }
523
524
525
526 /*
527  * Helper function for profiling purposes
528  */
529 static unsigned int GetTickCount()
530 {
531     struct timeval tv;
532     if (gettimeofday(&tv, NULL))
533         return 0;
534     return tv.tv_usec/1000+tv.tv_sec*1000;
535 }
536
537 /*
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)
562                                            {IDR(PBB)(PBB)}.
563 */
564
565 /*
566  * Return displaying order with specified periods and encoding order
567  * displaying_order: displaying order
568  * frame_type: frame type 
569  */
570 #define FRAME_P 0
571 #define FRAME_B 1
572 #define FRAME_I 2
573 #define FRAME_IDR 7
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,
578     int *frame_type)
579 {
580     int encoding_order_gop = 0;
581
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;
586         else
587             *frame_type = (encoding_order % intra_idr_period == 0)?FRAME_IDR:FRAME_I;
588         return;
589     }
590
591     if (intra_period == 0)
592         intra_idr_period = 0;
593
594     /* new sequence like
595      * IDR PPPPP IPPPPP
596      * IDR (PBB)(PBB)(IBB)(PBB)
597      */
598     encoding_order_gop = (intra_idr_period == 0)? encoding_order:
599         (encoding_order % (intra_idr_period + ((ip_period == 1)?0:1)));
600          
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;
614     } else {
615         *frame_type = FRAME_P;
616         *displaying_order = encoding_order + ip_period - 1;
617     }
618 }
619
620
621 static char *fourcc_to_string(int fourcc)
622 {
623     switch (fourcc) {
624     case VA_FOURCC_NV12:
625         return "NV12";
626     case VA_FOURCC_IYUV:
627         return "IYUV";
628     case VA_FOURCC_YV12:
629         return "YV12";
630     case VA_FOURCC_UYVY:
631         return "UYVY";
632     default:
633         return "Unknown";
634     }
635 }
636
637 static int string_to_fourcc(char *str)
638 {
639     int fourcc;
640     
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;
649     else {
650         printf("Unknow FOURCC\n");
651         fourcc = -1;
652     }
653     return fourcc;
654 }
655
656
657 static char *rc_to_string(int rcmode)
658 {
659     switch (rc_mode) {
660     case VA_RC_NONE:
661         return "NONE";
662     case VA_RC_CBR:
663         return "CBR";
664     case VA_RC_VBR:
665         return "VBR";
666     case VA_RC_VCM:
667         return "VCM";
668     case VA_RC_CQP:
669         return "CQP";
670     case VA_RC_VBR_CONSTRAINED:
671         return "VBR_CONSTRAINED";
672     default:
673         return "Unknown";
674     }
675 }
676
677 static int string_to_rc(char *str)
678 {
679     int rc_mode;
680     
681     if (!strncmp(str, "NONE", 4))
682         rc_mode = VA_RC_NONE;
683     else if (!strncmp(str, "CBR", 3))
684         rc_mode = VA_RC_CBR;
685     else if (!strncmp(str, "VBR", 3))
686         rc_mode = VA_RC_VBR;
687     else if (!strncmp(str, "VCM", 3))
688         rc_mode = VA_RC_VCM;
689     else if (!strncmp(str, "CQP", 3))
690         rc_mode = VA_RC_CQP;
691     else if (!strncmp(str, "VBR_CONSTRAINED", 15))
692         rc_mode = VA_RC_VBR_CONSTRAINED;
693     else {
694         printf("Unknown RC mode\n");
695         rc_mode = -1;
696     }
697     return rc_mode;
698 }
699
700
701 static int print_help(void)
702 {
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");
720
721     return 0;
722 }
723
724 static int process_cmdline(int argc, char *argv[])
725 {
726     char c;
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 }};
744     int long_index;
745     
746     while ((c =getopt_long_only(argc,argv,"w:h:n:f:o:?",long_opts,&long_index)) != EOF) {
747         switch (c) {
748         case 'w':
749             frame_width = atoi(optarg);
750             break;
751         case 'h':
752             frame_height = atoi(optarg);
753             break;
754         case 'n':
755             frame_count = atoi(optarg);
756             break;
757         case 'f':
758             frame_rate = atoi(optarg);
759             break;
760         case 'o':
761             coded_fn = strdup(optarg);
762             break;
763         case 0:
764             print_help();
765             exit(0);
766         case 1:
767             frame_bitrate = atoi(optarg);
768             break;
769         case 2:
770             minimal_qp = atoi(optarg);
771             break;
772         case 3:
773             initial_qp = atoi(optarg);
774             break;
775         case 4:
776             intra_period = atoi(optarg);
777             break;
778         case 5:
779             intra_idr_period = atoi(optarg);
780             break;
781         case 6:
782             ip_period = atoi(optarg);
783             break;
784         case 7:
785             rc_mode = string_to_rc(optarg);
786             if (rc_mode < 0) {
787                 print_help();
788                 exit(1);
789             }
790             break;
791         case 9:
792             srcyuv_fn = strdup(optarg);
793             break;
794         case 10:
795             recyuv_fn = strdup(optarg);
796             break;
797         case 11:
798             srcyuv_fourcc = string_to_fourcc(optarg);
799             if (srcyuv_fourcc <= 0) {
800                 print_help();
801                 exit(1);
802             }
803             break;
804         case 12:
805             encode_syncmode = 1;
806             break;
807         case 13:
808             calc_psnr = 1;
809             break;
810         case 14:
811             misc_priv_type = strtol(optarg, NULL, 0);
812             break;
813         case 15:
814             misc_priv_value = strtol(optarg, NULL, 0);
815             break;
816         case ':':
817         case '?':
818             print_help();
819             exit(0);
820         }
821     }
822
823     if (ip_period < 1) {
824         printf(" ip_period must be greater than 0\n");
825         exit(0);
826     }
827     if (intra_period != 1 && intra_period % ip_period != 0) {
828         printf(" intra_period must be a multiplier of ip_period\n");
829         exit(0);        
830     }
831     if (intra_period != 0 && intra_idr_period % intra_period != 0) {
832         printf(" intra_idr_period must be a multiplier of intra_period\n");
833         exit(0);        
834     }
835
836     if (frame_bitrate == 0)
837         frame_bitrate = frame_width * frame_height * 12 * frame_rate / 50;
838         
839     /* open source file */
840     if (srcyuv_fn) {
841         srcyuv_fp = fopen(srcyuv_fn,"r");
842     
843         if (srcyuv_fp == NULL)
844             printf("Open source YUV file %s failed, use auto-generated YUV data\n", srcyuv_fn);
845         else {
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);
849         }
850     }
851
852     /* open source file */
853     if (recyuv_fn) {
854         recyuv_fp = fopen(recyuv_fn,"w+");
855     
856         if (recyuv_fp == NULL)
857             printf("Open reconstructed YUV file %s failed\n", recyuv_fn);
858     }
859     
860     if (coded_fn == NULL) {
861         struct stat buf;
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");
866         else
867             coded_fn = strdup("./test.264");
868     }
869     
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);
874         exit(1);
875     }
876
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
884                );
885     }
886     
887     return 0;
888 }
889
890 static int init_va(void)
891 {
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;
897     VAStatus va_status;
898     unsigned int i;
899
900     va_dpy = va_open_display();
901     va_status = vaInitialize(va_dpy, &major_ver, &minor_ver);
902     CHECK_VASTATUS(va_status, "vaInitialize");
903
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) {
910                 support_encode = 1;
911                 break;
912             }
913         }
914         if (support_encode == 1)
915             break;
916     }
917     
918     if (support_encode == 0) {
919         printf("Can't find VAEntrypointEncSlice for H264 profiles\n");
920         exit(1);
921     } else {
922         switch (h264_profile) {
923             case VAProfileH264Baseline:
924                 printf("Use profile VAProfileH264Baseline\n");
925                 ip_period = 1;
926                 constraint_set_flag |= (1 << 0); /* Annex A.2.1 */
927                 break;
928             case VAProfileH264ConstrainedBaseline:
929                 printf("Use profile VAProfileH264ConstrainedBaseline\n");
930                 constraint_set_flag |= (1 << 0 | 1 << 1); /* Annex A.2.2 */
931                 ip_period = 1;
932                 break;
933
934             case VAProfileH264Main:
935                 printf("Use profile VAProfileH264Main\n");
936                 constraint_set_flag |= (1 << 1); /* Annex A.2.2 */
937                 break;
938
939             case VAProfileH264High:
940                 constraint_set_flag |= (1 << 3); /* Annex A.2.4 */
941                 printf("Use profile VAProfileH264High\n");
942                 break;
943             default:
944                 printf("unknow profile. Set to Baseline");
945                 h264_profile = VAProfileH264Baseline;
946                 ip_period = 1;
947                 constraint_set_flag |= (1 << 0); /* Annex A.2.1 */
948                 break;
949         }
950     }
951
952     /* find out the format for the render target, and rate control mode */
953     for (i = 0; i < VAConfigAttribTypeMax; i++)
954         attrib[i].type = i;
955
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");
962         exit(1);
963     } else {
964         config_attrib[config_attrib_num].type = VAConfigAttribRTFormat;
965         config_attrib[config_attrib_num].value = VA_RT_FORMAT_YUV420;
966         config_attrib_num++;
967     }
968     
969     if (attrib[VAConfigAttribRateControl].value != VA_ATTRIB_NOT_SUPPORTED) {
970         int tmp = attrib[VAConfigAttribRateControl].value;
971
972         printf("Support rate control mode (0x%x):", tmp);
973         
974         if (tmp & VA_RC_NONE)
975             printf("NONE ");
976         if (tmp & VA_RC_CBR)
977             printf("CBR ");
978         if (tmp & VA_RC_VBR)
979             printf("VBR ");
980         if (tmp & VA_RC_VCM)
981             printf("VCM ");
982         if (tmp & VA_RC_CQP)
983             printf("CQP ");
984         if (tmp & VA_RC_VBR_CONSTRAINED)
985             printf("VBR_CONSTRAINED ");
986
987         printf("\n");
988
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;
992         config_attrib_num++;
993     }
994     
995
996     if (attrib[VAConfigAttribEncPackedHeaders].value != VA_ATTRIB_NOT_SUPPORTED) {
997         int tmp = attrib[VAConfigAttribEncPackedHeaders].value;
998
999         printf("Support VAConfigAttribEncPackedHeaders\n");
1000         
1001         h264_packedheader = 1;
1002         config_attrib[config_attrib_num].type = VAConfigAttribEncPackedHeaders;
1003         config_attrib[config_attrib_num].value = VA_ENC_PACKED_HEADER_NONE;
1004         
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;
1008         }
1009         
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;
1013         }
1014         
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;
1018         }
1019         
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;
1023         }
1024         
1025         config_attrib_num++;
1026     }
1027
1028     if (attrib[VAConfigAttribEncInterlaced].value != VA_ATTRIB_NOT_SUPPORTED) {
1029         int tmp = attrib[VAConfigAttribEncInterlaced].value;
1030         
1031         printf("Support VAConfigAttribEncInterlaced\n");
1032
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");
1041         
1042         config_attrib[config_attrib_num].type = VAConfigAttribEncInterlaced;
1043         config_attrib[config_attrib_num].value = VA_ENC_PACKED_HEADER_NONE;
1044         config_attrib_num++;
1045     }
1046     
1047     if (attrib[VAConfigAttribEncMaxRefFrames].value != VA_ATTRIB_NOT_SUPPORTED) {
1048         h264_maxref = attrib[VAConfigAttribEncMaxRefFrames].value;
1049         
1050         printf("Support %d RefPicList0 and %d RefPicList1\n",
1051                h264_maxref & 0xffff, (h264_maxref >> 16) & 0xffff );
1052     }
1053
1054     if (attrib[VAConfigAttribEncMaxSlices].value != VA_ATTRIB_NOT_SUPPORTED)
1055         printf("Support %d slices\n", attrib[VAConfigAttribEncMaxSlices].value);
1056
1057     if (attrib[VAConfigAttribEncSliceStructure].value != VA_ATTRIB_NOT_SUPPORTED) {
1058         int tmp = attrib[VAConfigAttribEncSliceStructure].value;
1059         
1060         printf("Support VAConfigAttribEncSliceStructure\n");
1061
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");
1068     }
1069     if (attrib[VAConfigAttribEncMacroblockInfo].value != VA_ATTRIB_NOT_SUPPORTED) {
1070         printf("Support VAConfigAttribEncMacroblockInfo\n");
1071     }
1072
1073     return 0;
1074 }
1075
1076 static int setup_encode()
1077 {
1078     VAStatus va_status;
1079     VASurfaceID *tmp_surfaceid;
1080     int codedbuf_size, i;
1081     
1082     va_status = vaCreateConfig(va_dpy, h264_profile, VAEntrypointEncSlice,
1083             &config_attrib[0], config_attrib_num, &config_id);
1084     CHECK_VASTATUS(va_status, "vaCreateConfig");
1085
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,
1090                                  NULL, 0);
1091     CHECK_VASTATUS(va_status, "vaCreateSurfaces");
1092
1093     /* create reference surfaces */
1094     va_status = vaCreateSurfaces(
1095         va_dpy,
1096         VA_RT_FORMAT_YUV420, frame_width_mbaligned, frame_height_mbaligned,
1097         &ref_surface[0], SURFACE_NUM,
1098         NULL, 0
1099         );
1100     CHECK_VASTATUS(va_status, "vaCreateSurfaces");
1101
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));
1105     
1106     /* Create a context for this encode pipe */
1107     va_status = vaCreateContext(va_dpy, config_id,
1108                                 frame_width_mbaligned, frame_height_mbaligned,
1109                                 VA_PROGRESSIVE,
1110                                 tmp_surfaceid, 2 * SURFACE_NUM,
1111                                 &context_id);
1112     CHECK_VASTATUS(va_status, "vaCreateContext");
1113     free(tmp_surfaceid);
1114
1115     codedbuf_size = (frame_width_mbaligned * frame_height_mbaligned * 400) / (16*16);
1116
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
1123          */
1124         va_status = vaCreateBuffer(va_dpy,context_id,VAEncCodedBufferType,
1125                 codedbuf_size, 1, NULL, &coded_buf[i]);
1126         CHECK_VASTATUS(va_status,"vaCreateBuffer");
1127     }
1128     
1129     return 0;
1130 }
1131
1132
1133
1134 #define partition(ref, field, key, ascending)   \
1135     while (i <= j) {                            \
1136         if (ascending) {                        \
1137             while (ref[i].field < key)          \
1138                 i++;                            \
1139             while (ref[j].field > key)          \
1140                 j--;                            \
1141         } else {                                \
1142             while (ref[i].field > key)          \
1143                 i++;                            \
1144             while (ref[j].field < key)          \
1145                 j--;                            \
1146         }                                       \
1147         if (i <= j) {                           \
1148             tmp = ref[i];                       \
1149             ref[i] = ref[j];                    \
1150             ref[j] = tmp;                       \
1151             i++;                                \
1152             j--;                                \
1153         }                                       \
1154     }                                           \
1155
1156 static void sort_one(VAPictureH264 ref[], int left, int right,
1157                      int ascending, int frame_idx)
1158 {
1159     int i = left, j = right;
1160     unsigned int key;
1161     VAPictureH264 tmp;
1162
1163     if (frame_idx) {
1164         key = ref[(left + right) / 2].frame_idx;
1165         partition(ref, frame_idx, key, ascending);
1166     } else {
1167         key = ref[(left + right) / 2].TopFieldOrderCnt;
1168         partition(ref, TopFieldOrderCnt, (signed int)key, ascending);
1169     }
1170     
1171     /* recursion */
1172     if (left < j)
1173         sort_one(ref, left, j, ascending, frame_idx);
1174     
1175     if (i < right)
1176         sort_one(ref, i, right, ascending, frame_idx);
1177 }
1178
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)
1181 {
1182     int i = left, j = right;
1183     VAPictureH264 tmp;
1184
1185     if (frame_idx) {
1186         partition(ref, frame_idx, key, partition_ascending);
1187     } else {
1188         partition(ref, TopFieldOrderCnt, (signed int)key, partition_ascending);
1189     }
1190     
1191
1192     sort_one(ref, left, i-1, list0_ascending, frame_idx);
1193     sort_one(ref, j+1, right, list1_ascending, frame_idx);
1194 }
1195
1196 static int update_ReferenceFrames(void)
1197 {
1198     int i;
1199     
1200     if (current_frame_type == FRAME_B)
1201         return 0;
1202
1203     CurrentCurrPic.flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
1204     numShortTerm++;
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;
1210     
1211     if (current_frame_type != FRAME_B)
1212         current_frame_num++;
1213     if (current_frame_num > MaxFrameNum)
1214         current_frame_num = 0;
1215     
1216     return 0;
1217 }
1218
1219
1220 static int update_RefPicList(void)
1221 {
1222     unsigned int current_poc = CurrentCurrPic.TopFieldOrderCnt;
1223     
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);
1227     }
1228     
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,
1232                  1, 0, 1);
1233
1234         memcpy(RefPicList1_B, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
1235         sort_two(RefPicList1_B, 0, numShortTerm-1, current_poc, 0,
1236                  0, 1, 0);
1237     }
1238     
1239     return 0;
1240 }
1241
1242
1243 static int render_sequence(void)
1244 {
1245     VABufferID seq_param_buf, rc_param_buf, misc_param_tmpbuf, render_id[2];
1246     VAStatus va_status;
1247     VAEncMiscParameterBuffer *misc_param, *misc_param_tmp;
1248     VAEncMiscParameterRateControl *misc_rate_ctrl;
1249     
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;
1254
1255     seq_param.intra_period = intra_period;
1256     seq_param.intra_idr_period = intra_idr_period;
1257     seq_param.ip_period = ip_period;
1258
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;
1274     }
1275     
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");
1280     
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");
1286     
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);
1296
1297     render_id[0] = seq_param_buf;
1298     render_id[1] = rc_param_buf;
1299     
1300     va_status = vaRenderPicture(va_dpy,context_id, &render_id[0], 2);
1301     CHECK_VASTATUS(va_status,"vaRenderPicture");;
1302
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);
1313     
1314         va_status = vaRenderPicture(va_dpy,context_id, &misc_param_tmpbuf, 1);
1315     }
1316     
1317     return 0;
1318 }
1319
1320 static int calc_poc(int pic_order_cnt_lsb)
1321 {
1322     static int PicOrderCntMsb_ref = 0, pic_order_cnt_lsb_ref = 0;
1323     int prevPicOrderCntMsb, prevPicOrderCntLsb;
1324     int PicOrderCntMsb, TopFieldOrderCnt;
1325     
1326     if (current_frame_type == FRAME_IDR)
1327         prevPicOrderCntMsb = prevPicOrderCntLsb = 0;
1328     else {
1329         prevPicOrderCntMsb = PicOrderCntMsb_ref;
1330         prevPicOrderCntLsb = pic_order_cnt_lsb_ref;
1331     }
1332     
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;
1339     else
1340         PicOrderCntMsb = prevPicOrderCntMsb;
1341     
1342     TopFieldOrderCnt = PicOrderCntMsb + pic_order_cnt_lsb;
1343
1344     if (current_frame_type != FRAME_B) {
1345         PicOrderCntMsb_ref = PicOrderCntMsb;
1346         pic_order_cnt_lsb_ref = pic_order_cnt_lsb;
1347     }
1348     
1349     return TopFieldOrderCnt;
1350 }
1351
1352 static int render_picture(void)
1353 {
1354     VABufferID pic_param_buf;
1355     VAStatus va_status;
1356     int i = 0;
1357
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;
1364
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];
1373         }
1374     } else {
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;
1379         }
1380     }
1381     
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;
1390
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");;
1394
1395     va_status = vaRenderPicture(va_dpy,context_id, &pic_param_buf, 1);
1396     CHECK_VASTATUS(va_status,"vaRenderPicture");
1397
1398     return 0;
1399 }
1400
1401 static int render_packedsequence(void)
1402 {
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;
1407     VAStatus va_status;
1408
1409     length_in_bits = build_packed_seq_buffer(&packedseq_buffer); 
1410     
1411     packedheader_param_buffer.type = VAEncPackedHeaderSequence;
1412     
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,
1416                                context_id,
1417                                VAEncPackedHeaderParameterBufferType,
1418                                sizeof(packedheader_param_buffer), 1, &packedheader_param_buffer,
1419                                &packedseq_para_bufid);
1420     CHECK_VASTATUS(va_status,"vaCreateBuffer");
1421
1422     va_status = vaCreateBuffer(va_dpy,
1423                                context_id,
1424                                VAEncPackedHeaderDataBufferType,
1425                                (length_in_bits + 7) / 8, 1, packedseq_buffer,
1426                                &packedseq_data_bufid);
1427     CHECK_VASTATUS(va_status,"vaCreateBuffer");
1428
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");
1433
1434     free(packedseq_buffer);
1435     
1436     return 0;
1437 }
1438
1439
1440 static int render_packedpicture(void)
1441 {
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;
1446     VAStatus va_status;
1447
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;
1452
1453     va_status = vaCreateBuffer(va_dpy,
1454                                context_id,
1455                                VAEncPackedHeaderParameterBufferType,
1456                                sizeof(packedheader_param_buffer), 1, &packedheader_param_buffer,
1457                                &packedpic_para_bufid);
1458     CHECK_VASTATUS(va_status,"vaCreateBuffer");
1459
1460     va_status = vaCreateBuffer(va_dpy,
1461                                context_id,
1462                                VAEncPackedHeaderDataBufferType,
1463                                (length_in_bits + 7) / 8, 1, packedpic_buffer,
1464                                &packedpic_data_bufid);
1465     CHECK_VASTATUS(va_status,"vaCreateBuffer");
1466
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");
1471
1472     free(packedpic_buffer);
1473     
1474     return 0;
1475 }
1476
1477 static void render_packedsei(void)
1478 {
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;
1483     VAStatus va_status;
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;
1486
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;
1491
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;
1496     
1497
1498     length_in_bits = build_packed_sei_buffer_timing(
1499         i_initial_cpb_removal_delay_length,
1500         i_initial_cpb_removal_delay,
1501         0,
1502         i_cpb_removal_delay_length,
1503         i_cpb_removal_delay * current_frame_encoding,
1504         i_dpb_output_delay_length,
1505         0,
1506         &packed_sei_buffer);
1507
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;
1512
1513     va_status = vaCreateBuffer(va_dpy,
1514                                context_id,
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");
1519
1520     va_status = vaCreateBuffer(va_dpy,
1521                                context_id,
1522                                VAEncPackedHeaderDataBufferType,
1523                                (length_in_bits + 7) / 8, 1, packed_sei_buffer,
1524                                &packed_sei_buf_id);
1525     CHECK_VASTATUS(va_status,"vaCreateBuffer");
1526
1527
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");
1532
1533     
1534     free(packed_sei_buffer);
1535         
1536     return;
1537 }
1538
1539
1540 static int render_hrd(void)
1541 {
1542     VABufferID misc_parameter_hrd_buf_id;
1543     VAStatus va_status;
1544     VAEncMiscParameterBuffer *misc_param;
1545     VAEncMiscParameterHRD *misc_hrd_param;
1546     
1547     va_status = vaCreateBuffer(va_dpy, context_id,
1548                    VAEncMiscParameterBufferType,
1549                    sizeof(VAEncMiscParameterBuffer) + sizeof(VAEncMiscParameterHRD),
1550                    1,
1551                    NULL, 
1552                    &misc_parameter_hrd_buf_id);
1553     CHECK_VASTATUS(va_status, "vaCreateBuffer");
1554
1555     vaMapBuffer(va_dpy,
1556                 misc_parameter_hrd_buf_id,
1557                 (void **)&misc_param);
1558     misc_param->type = VAEncMiscParameterTypeHRD;
1559     misc_hrd_param = (VAEncMiscParameterHRD *)misc_param->data;
1560
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;
1564     } else {
1565         misc_hrd_param->initial_buffer_fullness = 0;
1566         misc_hrd_param->buffer_size = 0;
1567     }
1568     vaUnmapBuffer(va_dpy, misc_parameter_hrd_buf_id);
1569
1570     va_status = vaRenderPicture(va_dpy,context_id, &misc_parameter_hrd_buf_id, 1);
1571     CHECK_VASTATUS(va_status,"vaRenderPicture");;
1572
1573     return 0;
1574 }
1575
1576 static int render_slice(void)
1577 {
1578     VABufferID slice_param_buf;
1579     VAStatus va_status;
1580     int i;
1581
1582     update_RefPicList();
1583     
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));
1593
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;
1597         }
1598     } else if (current_frame_type == FRAME_B) {
1599         int refpiclist0_max = h264_maxref & 0xffff;
1600         int refpiclist1_max = (h264_maxref >> 16) & 0xffff;
1601
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;
1606         }
1607
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;
1612         }
1613     }
1614
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;
1618     
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");;
1622
1623     va_status = vaRenderPicture(va_dpy,context_id, &slice_param_buf, 1);
1624     CHECK_VASTATUS(va_status,"vaRenderPicture");
1625
1626     return 0;
1627 }
1628
1629
1630 static int upload_source_YUV_once_for_all()
1631 {
1632     int box_width=8;
1633     int row_shift=0;
1634     int i;
1635
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);
1639
1640         row_shift++;
1641         if (row_shift==(2*box_width)) row_shift= 0;
1642     }
1643     printf("Complete surface loading\n");
1644
1645     return 0;
1646 }
1647
1648 #define check_ret(ret)                                  \
1649 if (ret != 1) {                                         \
1650     printf("fread doesn't return enough data\n");       \
1651     exit(1);                                            \
1652 }
1653 static int load_surface(VASurfaceID surface_id, unsigned long long display_order)
1654 {
1655     unsigned char *src_Y = NULL, *src_U = NULL, *src_V = NULL;
1656     int ret = 0;
1657     
1658     if (srcyuv_fp == NULL)
1659         return 0;
1660     
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);
1664     
1665
1666     if (srcyuv_fourcc == VA_FOURCC_NV12) {
1667         int uv_size = 2 * (frame_width/2) * (frame_height/2);
1668
1669         src_Y = malloc(2 * uv_size);
1670         src_U = malloc(uv_size);
1671         
1672         ret = fread(src_Y, frame_width * frame_height, 1, srcyuv_fp);
1673         check_ret(ret);
1674         ret = fread(src_U, uv_size, 1, srcyuv_fp);
1675         check_ret(ret);
1676     } else if (srcyuv_fourcc == VA_FOURCC_IYUV ||
1677         srcyuv_fourcc == VA_FOURCC_YV12) {
1678         int uv_size = (frame_width/2) * (frame_height/2);
1679
1680         src_Y = malloc(4 * uv_size);
1681         src_U = malloc(uv_size);
1682         src_V = malloc(uv_size);
1683
1684         ret = fread(src_Y, frame_width * frame_height, 1, srcyuv_fp);
1685         check_ret(ret);
1686         if (srcyuv_fourcc == VA_FOURCC_IYUV) {
1687             ret = fread(src_U, uv_size, 1, srcyuv_fp);
1688             check_ret(ret);
1689             ret = fread(src_V, uv_size, 1, srcyuv_fp);
1690             check_ret(ret);
1691         } else { /* YV12 */
1692             ret = fread(src_V, uv_size, 1, srcyuv_fp);
1693             check_ret(ret);
1694             ret = fread(src_U, uv_size, 1, srcyuv_fp);
1695             check_ret(ret);
1696         }
1697     } else {
1698         printf("Unsupported source YUV format\n");
1699         exit(1);
1700     }
1701     
1702     upload_surface_yuv(va_dpy, surface_id,
1703                        srcyuv_fourcc, frame_width, frame_height,
1704                        src_Y, src_U, src_V);
1705     if (src_Y)
1706         free(src_Y);
1707     if (src_U)
1708         free(src_U);
1709     if (src_V)
1710         free(src_V);
1711
1712     return 0;
1713 }
1714
1715
1716 static int save_recyuv(VASurfaceID surface_id,
1717                        unsigned long long display_order,
1718                        unsigned long long encode_order)
1719 {
1720     unsigned char *dst_Y = NULL, *dst_U = NULL, *dst_V = NULL;
1721
1722     if (recyuv_fp == NULL)
1723         return 0;
1724
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);
1735     } else {
1736         printf("Unsupported source YUV format\n");
1737         exit(1);
1738     }
1739     
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);
1744
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);
1753         
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);
1757         } else {
1758             fwrite(dst_V, uv_size, 1, recyuv_fp);
1759             fwrite(dst_U, uv_size, 1, recyuv_fp);
1760         }
1761     } else {
1762         printf("Unsupported YUV format\n");
1763         exit(1);
1764     }
1765     
1766     if (dst_Y)
1767         free(dst_Y);
1768     if (dst_U)
1769         free(dst_U);
1770     if (dst_V)
1771         free(dst_V);
1772
1773     return 0;
1774 }
1775
1776
1777 static int save_codeddata(unsigned long long display_order, unsigned long long encode_order)
1778 {    
1779     VACodedBufferSegment *buf_list = NULL;
1780     VAStatus va_status;
1781     unsigned int coded_size = 0;
1782
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;
1788
1789         frame_size += coded_size;
1790     }
1791     vaUnmapBuffer(va_dpy,coded_buf[display_order % SURFACE_NUM]);
1792
1793     printf("\r      "); /* return back to startpoint */
1794     switch (encode_order % 4) {
1795         case 0:
1796             printf("|");
1797             break;
1798         case 1:
1799             printf("/");
1800             break;
1801         case 2:
1802             printf("-");
1803             break;
1804         case 3:
1805             printf("\\");
1806             break;
1807     }
1808     printf("%08lld", encode_order);
1809     /*
1810     if (current_frame_encoding % intra_count == 0)
1811         printf("(I)");
1812     else
1813         printf("(P)");
1814     */
1815     printf("(%06d bytes coded)",coded_size);
1816     /* skipped frame ? */
1817     printf("                                    ");
1818
1819     return 0;
1820 }
1821
1822
1823 static struct storage_task_t * storage_task_dequeue(void)
1824 {
1825     struct storage_task_t *header;
1826
1827     pthread_mutex_lock(&encode_mutex);
1828
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;
1834     }
1835     
1836     pthread_mutex_unlock(&encode_mutex);
1837     
1838     return header;
1839 }
1840
1841 static int storage_task_queue(unsigned long long display_order, unsigned long long encode_order)
1842 {
1843     struct storage_task_t *tmp;
1844
1845     tmp = calloc(1, sizeof(struct storage_task_t));
1846     tmp->display_order = display_order;
1847     tmp->encode_order = encode_order;
1848
1849     pthread_mutex_lock(&encode_mutex);
1850     
1851     if (storage_task_header == NULL) {
1852         storage_task_header = tmp;
1853         storage_task_tail = tmp;
1854     } else {
1855         storage_task_tail->next = tmp;
1856         storage_task_tail = tmp;
1857     }
1858
1859     srcsurface_status[display_order % SURFACE_NUM] = SRC_SURFACE_IN_STORAGE;
1860     pthread_cond_signal(&encode_cond);
1861     
1862     pthread_mutex_unlock(&encode_mutex);
1863     
1864     return 0;
1865 }
1866
1867 static void storage_task(unsigned long long display_order, unsigned long long encode_order)
1868 {
1869     unsigned int tmp;
1870     VAStatus va_status;
1871     
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;
1879
1880     save_recyuv(ref_surface[display_order % SURFACE_NUM], display_order, encode_order);
1881
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;
1887
1888     pthread_mutex_lock(&encode_mutex);
1889     srcsurface_status[display_order % SURFACE_NUM] = SRC_SURFACE_IN_ENCODING;
1890     pthread_mutex_unlock(&encode_mutex);
1891 }
1892
1893         
1894 static void * storage_task_thread(void *t)
1895 {
1896     while (1) {
1897         struct storage_task_t *current;
1898         
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);
1904             continue;
1905         }
1906         
1907         storage_task(current->display_order, current->encode_order);
1908         
1909         free(current);
1910
1911         /* all frames are saved, exit the thread */
1912         if (++frame_coded >= frame_count)
1913             break;
1914     }
1915
1916     return 0;
1917 }
1918
1919
1920 static int encode_frames(void)
1921 {
1922     unsigned int i, tmp;
1923     VAStatus va_status;
1924     //VASurfaceStatus surface_status;
1925
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);
1931     } else
1932         upload_source_YUV_once_for_all();
1933     UploadPictureTicks += GetTickCount() - tmp;
1934     
1935     /* ready for encoding */
1936     memset(srcsurface_status, SRC_SURFACE_IN_ENCODING, sizeof(srcsurface_status));
1937     
1938     memset(&seq_param, 0, sizeof(seq_param));
1939     memset(&pic_param, 0, sizeof(pic_param));
1940     memset(&slice_param, 0, sizeof(slice_param));
1941
1942     if (encode_syncmode == 0)
1943         pthread_create(&encode_thread, NULL, storage_task_thread, NULL);
1944     
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                                &current_frame_display, &current_frame_type);
1948         if (current_frame_type == FRAME_IDR) {
1949             numShortTerm = 0;
1950             current_frame_num = 0;
1951             current_IDR_display = current_frame_display;
1952         }
1953
1954         /* check if the source frame is ready */
1955         while (srcsurface_status[current_slot] != SRC_SURFACE_IN_ENCODING) {
1956             usleep(1);
1957         }
1958         
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;
1963         
1964         tmp = GetTickCount();
1965         if (current_frame_type == FRAME_IDR) {
1966             render_sequence();
1967             render_picture();            
1968             if (h264_packedheader) {
1969                 render_packedsequence();
1970                 render_packedpicture();
1971             }
1972             //if (rc_mode == VA_RC_CBR)
1973             //    render_packedsei();
1974             //render_hrd();
1975         } else {
1976             //render_sequence();
1977             render_picture();
1978             //if (rc_mode == VA_RC_CBR)
1979             //    render_packedsei();
1980             //render_hrd();
1981         }
1982         render_slice();
1983         RenderPictureTicks += GetTickCount() - tmp;
1984         
1985         tmp = GetTickCount();
1986         va_status = vaEndPicture(va_dpy,context_id);
1987         CHECK_VASTATUS(va_status,"vaEndPicture");;
1988         EndPictureTicks += GetTickCount() - tmp;
1989
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);
1994
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);
1999         */
2000         update_ReferenceFrames();        
2001     }
2002
2003     if (encode_syncmode == 0) {
2004         int ret;
2005         pthread_join(encode_thread, (void **)&ret);
2006     }
2007     
2008     return 0;
2009 }
2010
2011
2012 static int release_encode()
2013 {
2014     int i;
2015     
2016     vaDestroySurfaces(va_dpy,&src_surface[0],SURFACE_NUM);
2017     vaDestroySurfaces(va_dpy,&ref_surface[0],SURFACE_NUM);
2018
2019     for (i = 0; i < SURFACE_NUM; i++)
2020         vaDestroyBuffer(va_dpy,coded_buf[i]);
2021     
2022     vaDestroyContext(va_dpy,context_id);
2023     vaDestroyConfig(va_dpy,config_id);
2024
2025     return 0;
2026 }
2027
2028 static int deinit_va()
2029
2030     vaTerminate(va_dpy);
2031
2032     va_close_display(va_dpy);
2033
2034     return 0;
2035 }
2036
2037
2038 static int print_input()
2039 {
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");
2053     if (srcyuv_fp) 
2054         printf(":%s (fourcc %s)\n", srcyuv_fn, fourcc_to_string(srcyuv_fourcc));
2055     else
2056         printf("\n");
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");
2060     else
2061         printf("INPUT: Rec   Clip   : Save reconstructed frame into %s (fourcc %s)\n", recyuv_fn,
2062                fourcc_to_string(srcyuv_fourcc));
2063     
2064     printf("\n\n"); /* return back to startpoint */
2065     
2066     return 0;
2067 }
2068
2069 static int calc_PSNR(double *psnr)
2070 {
2071     unsigned long srcyuv_size, recyuv_size, min_size;
2072     char *srcyuv_ptr, *recyuv_ptr;
2073     unsigned long i, sse=0;
2074     double ssemean;
2075     
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);
2080     
2081     fseek(srcyuv_fp, 0L, SEEK_SET);
2082     fseek(recyuv_fp, 0L, SEEK_SET);
2083
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");
2089         return 1;
2090     }
2091     
2092     for (i=0; i<min_size; i++) {
2093         char tmp = srcyuv_ptr[i] - recyuv_ptr[i];
2094         sse += tmp * tmp;
2095     }
2096     ssemean = (double)sse/(double)min_size;
2097     *psnr = 20.0*log10(255) - 10.0*log10(ssemean);
2098            
2099     munmap(srcyuv_ptr, min_size);
2100     munmap(recyuv_ptr, min_size);
2101     
2102     return 0;
2103 }
2104
2105 static int print_performance(unsigned int PictureCount)
2106 {
2107     unsigned int psnr_ret = 1, others = 0;
2108     double psnr = 0, total_size = frame_width * frame_height * 1.5 * frame_count;
2109
2110     if (calc_psnr && srcyuv_fp && recyuv_fp)
2111         psnr_ret = calc_PSNR(&psnr);
2112     
2113     others = TotalTicks - UploadPictureTicks - BeginPictureTicks
2114         - RenderPictureTicks - EndPictureTicks - SyncPictureTicks - SavePictureTicks;
2115
2116     printf("\n\n");
2117
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));
2122     if (psnr_ret == 0)
2123         printf("PERFORMANCE:   PSNR                 : %.2f (%lld frames calculated)\n",
2124                psnr, MIN(frame_count, srcyuv_frames));
2125
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);
2147
2148     if (encode_syncmode == 0)
2149         printf("(Multithread enabled, the timing is only for reference)\n");
2150     
2151     return 0;
2152 }
2153
2154
2155 int main(int argc,char **argv)
2156 {
2157     unsigned int start;
2158     
2159     process_cmdline(argc, argv);
2160
2161     print_input();
2162     
2163     start = GetTickCount();
2164     
2165     init_va();
2166     setup_encode();
2167     
2168     encode_frames();
2169
2170     release_encode();
2171     deinit_va();
2172
2173     TotalTicks += GetTickCount() - start;
2174     print_performance(frame_count);
2175     
2176     return 0;
2177 }