OSDN Git Service

h264encode: add some comments
[android-x86/hardware-intel-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 <fcntl.h>
34 #include <assert.h>
35 #include <pthread.h>
36 #include <va/va.h>
37 #include <va/va_enc_h264.h>
38 #include "va_display.h"
39
40 #define CHECK_VASTATUS(va_status,func)                                  \
41     if (va_status != VA_STATUS_SUCCESS) {                               \
42         fprintf(stderr,"%s:%s (%d) failed,exit\n", __func__, func, __LINE__); \
43         exit(1);                                                        \
44     }
45
46 #include "../loadsurface.h"
47
48 #define NAL_REF_IDC_NONE        0
49 #define NAL_REF_IDC_LOW         1
50 #define NAL_REF_IDC_MEDIUM      2
51 #define NAL_REF_IDC_HIGH        3
52
53 #define NAL_NON_IDR             1
54 #define NAL_IDR                 5
55 #define NAL_SPS                 7
56 #define NAL_PPS                 8
57 #define NAL_SEI                 6
58
59 #define SLICE_TYPE_P            0
60 #define SLICE_TYPE_B            1
61 #define SLICE_TYPE_I            2
62
63 #define ENTROPY_MODE_CAVLC      0
64 #define ENTROPY_MODE_CABAC      1
65
66 #define PROFILE_IDC_BASELINE    66
67 #define PROFILE_IDC_MAIN        77
68 #define PROFILE_IDC_HIGH        100
69    
70 #define BITSTREAM_ALLOCATE_STEPPING     4096
71
72
73 #define SURFACE_NUM 16 /* 16 surfaces for source YUV */
74 #define SURFACE_NUM 16 /* 16 surfaces for reference */
75 static  VADisplay va_dpy;
76 static  VAProfile h264_profile;
77 static  VAConfigAttrib attrib[VAConfigAttribTypeMax];
78 static  VAConfigAttrib config_attrib[VAConfigAttribTypeMax];
79 static  int config_attrib_num = 0;
80 static  VASurfaceID src_surface[SURFACE_NUM];
81 static  VABufferID  coded_buf[SURFACE_NUM];
82 static  VASurfaceID ref_surface[SURFACE_NUM];
83 static  VAConfigID config_id;
84 static  VAContextID context_id;
85 static  VAEncSequenceParameterBufferH264 seq_param;
86 static  VAEncPictureParameterBufferH264 pic_param;
87 static  VAEncSliceParameterBufferH264 slice_param;
88 static  VAPictureH264 CurrentCurrPic;
89 static  VAPictureH264 ReferenceFrames[16], RefPicList0_P[32], RefPicList0_B[32], RefPicList1_B[32];
90
91 static  unsigned int MaxFrameNum = (2<<16);
92 static  unsigned int MaxPicOrderCntLsb = (2<<8);
93 static  unsigned int Log2MaxFrameNum = 16;
94 static  unsigned int Log2MaxPicOrderCntLsb = 8;
95
96 static  unsigned int num_ref_frames = 2;
97 static  unsigned int numShortTerm = 0;
98 static  int constraint_set_flag = 0;
99 static  int h264_packedheader = 0; /* support pack header? */
100 static  int h264_maxref = (1<<16|1);
101 static  char *coded_fn = NULL, *srcyuv_fn = NULL, *recyuv_fn = NULL;
102 static  FILE *coded_fp = NULL, *srcyuv_fp = NULL, *recyuv_fp = NULL;
103 static  unsigned long long srcyuv_frames = 0;
104 static  int srcyuv_fourcc = VA_FOURCC_NV12;
105
106 static  int frame_width = 176;
107 static  int frame_height = 144;
108 static  int frame_rate = 30;
109 static  unsigned int frame_count = 60;
110 static  unsigned int frame_coded = 0;
111 static  unsigned int frame_bitrate = 0;
112 static  unsigned int frame_slices = 1;
113 static  int initial_qp = 28;
114 static  int minimal_qp = 0;
115 static  int intra_period = 30;
116 static  int intra_idr_period = 60;
117 static  int ip_period = 1;
118 static  int rc_mode = VA_RC_VBR;
119 static  unsigned long long current_frame_encoding = 0;
120 static  unsigned long long current_frame_display = 0;
121 static  int current_frame_num = 0;
122 static  int current_frame_type;
123 #define current_slot (current_frame_display % SURFACE_NUM)
124
125 /* thread to save coded data/upload source YUV */
126 struct storage_task_t {
127     void *next;
128     unsigned long long display_order;
129     unsigned long long encode_order;
130 };
131 static  struct storage_task_t *storage_task_header = NULL, *storage_task_tail = NULL;
132 #define SRC_SURFACE_IN_ENCODING 0
133 #define SRC_SURFACE_IN_STORAGE  1
134 static  int srcsurface_status[SURFACE_NUM];
135 static  int encode_syncmode = 0;
136 static  pthread_mutex_t encode_mutex = PTHREAD_MUTEX_INITIALIZER;
137 static  pthread_cond_t  encode_cond = PTHREAD_COND_INITIALIZER;
138 static  pthread_t encode_thread;
139     
140 /* for performance profiling */
141 static unsigned int UploadPictureTicks=0;
142 static unsigned int BeginPictureTicks=0;
143 static unsigned int RenderPictureTicks=0;
144 static unsigned int EndPictureTicks=0;
145 static unsigned int SyncPictureTicks=0;
146 static unsigned int SavePictureTicks=0;
147 static unsigned int TotalTicks=0;
148
149 struct __bitstream {
150     unsigned int *buffer;
151     int bit_offset;
152     int max_size_in_dword;
153 };
154 typedef struct __bitstream bitstream;
155
156
157 static unsigned int 
158 va_swap32(unsigned int val)
159 {
160     unsigned char *pval = (unsigned char *)&val;
161
162     return ((pval[0] << 24)     |
163             (pval[1] << 16)     |
164             (pval[2] << 8)      |
165             (pval[3] << 0));
166 }
167
168 static void
169 bitstream_start(bitstream *bs)
170 {
171     bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
172     bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
173     bs->bit_offset = 0;
174 }
175
176 static void
177 bitstream_end(bitstream *bs)
178 {
179     int pos = (bs->bit_offset >> 5);
180     int bit_offset = (bs->bit_offset & 0x1f);
181     int bit_left = 32 - bit_offset;
182
183     if (bit_offset) {
184         bs->buffer[pos] = va_swap32((bs->buffer[pos] << bit_left));
185     }
186 }
187  
188 static void
189 bitstream_put_ui(bitstream *bs, unsigned int val, int size_in_bits)
190 {
191     int pos = (bs->bit_offset >> 5);
192     int bit_offset = (bs->bit_offset & 0x1f);
193     int bit_left = 32 - bit_offset;
194
195     if (!size_in_bits)
196         return;
197
198     bs->bit_offset += size_in_bits;
199
200     if (bit_left > size_in_bits) {
201         bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
202     } else {
203         size_in_bits -= bit_left;
204         bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
205         bs->buffer[pos] = va_swap32(bs->buffer[pos]);
206
207         if (pos + 1 == bs->max_size_in_dword) {
208             bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
209             bs->buffer = realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
210         }
211
212         bs->buffer[pos + 1] = val;
213     }
214 }
215
216 static void
217 bitstream_put_ue(bitstream *bs, unsigned int val)
218 {
219     int size_in_bits = 0;
220     int tmp_val = ++val;
221
222     while (tmp_val) {
223         tmp_val >>= 1;
224         size_in_bits++;
225     }
226
227     bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
228     bitstream_put_ui(bs, val, size_in_bits);
229 }
230
231 static void
232 bitstream_put_se(bitstream *bs, int val)
233 {
234     unsigned int new_val;
235
236     if (val <= 0)
237         new_val = -2 * val;
238     else
239         new_val = 2 * val - 1;
240
241     bitstream_put_ue(bs, new_val);
242 }
243
244 static void
245 bitstream_byte_aligning(bitstream *bs, int bit)
246 {
247     int bit_offset = (bs->bit_offset & 0x7);
248     int bit_left = 8 - bit_offset;
249     int new_val;
250
251     if (!bit_offset)
252         return;
253
254     assert(bit == 0 || bit == 1);
255
256     if (bit)
257         new_val = (1 << bit_left) - 1;
258     else
259         new_val = 0;
260
261     bitstream_put_ui(bs, new_val, bit_left);
262 }
263
264 static void 
265 rbsp_trailing_bits(bitstream *bs)
266 {
267     bitstream_put_ui(bs, 1, 1);
268     bitstream_byte_aligning(bs, 0);
269 }
270
271 static void nal_start_code_prefix(bitstream *bs)
272 {
273     bitstream_put_ui(bs, 0x00000001, 32);
274 }
275
276 static void nal_header(bitstream *bs, int nal_ref_idc, int nal_unit_type)
277 {
278     bitstream_put_ui(bs, 0, 1);                /* forbidden_zero_bit: 0 */
279     bitstream_put_ui(bs, nal_ref_idc, 2);
280     bitstream_put_ui(bs, nal_unit_type, 5);
281 }
282
283 static void sps_rbsp(bitstream *bs)
284 {
285     int profile_idc = PROFILE_IDC_BASELINE;
286
287     if (h264_profile  == VAProfileH264High)
288         profile_idc = PROFILE_IDC_HIGH;
289     else if (h264_profile  == VAProfileH264Main)
290         profile_idc = PROFILE_IDC_MAIN;
291
292     bitstream_put_ui(bs, profile_idc, 8);               /* profile_idc */
293     bitstream_put_ui(bs, !!(constraint_set_flag & 1), 1);                         /* constraint_set0_flag */
294     bitstream_put_ui(bs, !!(constraint_set_flag & 2), 1);                         /* constraint_set1_flag */
295     bitstream_put_ui(bs, !!(constraint_set_flag & 4), 1);                         /* constraint_set2_flag */
296     bitstream_put_ui(bs, !!(constraint_set_flag & 8), 1);                         /* constraint_set3_flag */
297     bitstream_put_ui(bs, 0, 4);                         /* reserved_zero_4bits */
298     bitstream_put_ui(bs, seq_param.level_idc, 8);      /* level_idc */
299     bitstream_put_ue(bs, seq_param.seq_parameter_set_id);      /* seq_parameter_set_id */
300
301     if ( profile_idc == PROFILE_IDC_HIGH) {
302         bitstream_put_ue(bs, 1);        /* chroma_format_idc = 1, 4:2:0 */ 
303         bitstream_put_ue(bs, 0);        /* bit_depth_luma_minus8 */
304         bitstream_put_ue(bs, 0);        /* bit_depth_chroma_minus8 */
305         bitstream_put_ui(bs, 0, 1);     /* qpprime_y_zero_transform_bypass_flag */
306         bitstream_put_ui(bs, 0, 1);     /* seq_scaling_matrix_present_flag */
307     }
308
309     bitstream_put_ue(bs, seq_param.seq_fields.bits.log2_max_frame_num_minus4); /* log2_max_frame_num_minus4 */
310     bitstream_put_ue(bs, seq_param.seq_fields.bits.pic_order_cnt_type);        /* pic_order_cnt_type */
311
312     if (seq_param.seq_fields.bits.pic_order_cnt_type == 0)
313         bitstream_put_ue(bs, seq_param.seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4);     /* log2_max_pic_order_cnt_lsb_minus4 */
314     else {
315         assert(0);
316     }
317
318     bitstream_put_ue(bs, seq_param.max_num_ref_frames);        /* num_ref_frames */
319     bitstream_put_ui(bs, 0, 1);                                 /* gaps_in_frame_num_value_allowed_flag */
320
321     bitstream_put_ue(bs, seq_param.picture_width_in_mbs - 1);  /* pic_width_in_mbs_minus1 */
322     bitstream_put_ue(bs, seq_param.picture_height_in_mbs - 1); /* pic_height_in_map_units_minus1 */
323     bitstream_put_ui(bs, seq_param.seq_fields.bits.frame_mbs_only_flag, 1);    /* frame_mbs_only_flag */
324
325     if (!seq_param.seq_fields.bits.frame_mbs_only_flag) {
326         assert(0);
327     }
328
329     bitstream_put_ui(bs, seq_param.seq_fields.bits.direct_8x8_inference_flag, 1);      /* direct_8x8_inference_flag */
330     bitstream_put_ui(bs, seq_param.frame_cropping_flag, 1);            /* frame_cropping_flag */
331
332     if (seq_param.frame_cropping_flag) {
333         bitstream_put_ue(bs, seq_param.frame_crop_left_offset);        /* frame_crop_left_offset */
334         bitstream_put_ue(bs, seq_param.frame_crop_right_offset);       /* frame_crop_right_offset */
335         bitstream_put_ue(bs, seq_param.frame_crop_top_offset);         /* frame_crop_top_offset */
336         bitstream_put_ue(bs, seq_param.frame_crop_bottom_offset);      /* frame_crop_bottom_offset */
337     }
338     
339     //if ( frame_bit_rate < 0 ) { //TODO EW: the vui header isn't correct
340     if ( 1 ) {
341         bitstream_put_ui(bs, 0, 1); /* vui_parameters_present_flag */
342     } else {
343         bitstream_put_ui(bs, 1, 1); /* vui_parameters_present_flag */
344         bitstream_put_ui(bs, 0, 1); /* aspect_ratio_info_present_flag */
345         bitstream_put_ui(bs, 0, 1); /* overscan_info_present_flag */
346         bitstream_put_ui(bs, 0, 1); /* video_signal_type_present_flag */
347         bitstream_put_ui(bs, 0, 1); /* chroma_loc_info_present_flag */
348         bitstream_put_ui(bs, 1, 1); /* timing_info_present_flag */
349         {
350             bitstream_put_ui(bs, 15, 32);
351             bitstream_put_ui(bs, 900, 32);
352             bitstream_put_ui(bs, 1, 1);
353         }
354         bitstream_put_ui(bs, 1, 1); /* nal_hrd_parameters_present_flag */
355         {
356             // hrd_parameters 
357             bitstream_put_ue(bs, 0);    /* cpb_cnt_minus1 */
358             bitstream_put_ui(bs, 4, 4); /* bit_rate_scale */
359             bitstream_put_ui(bs, 6, 4); /* cpb_size_scale */
360            
361             bitstream_put_ue(bs, frame_bitrate - 1); /* bit_rate_value_minus1[0] */
362             bitstream_put_ue(bs, frame_bitrate*8 - 1); /* cpb_size_value_minus1[0] */
363             bitstream_put_ui(bs, 1, 1);  /* cbr_flag[0] */
364
365             bitstream_put_ui(bs, 23, 5);   /* initial_cpb_removal_delay_length_minus1 */
366             bitstream_put_ui(bs, 23, 5);   /* cpb_removal_delay_length_minus1 */
367             bitstream_put_ui(bs, 23, 5);   /* dpb_output_delay_length_minus1 */
368             bitstream_put_ui(bs, 23, 5);   /* time_offset_length  */
369         }
370         bitstream_put_ui(bs, 0, 1);   /* vcl_hrd_parameters_present_flag */
371         bitstream_put_ui(bs, 0, 1);   /* low_delay_hrd_flag */ 
372
373         bitstream_put_ui(bs, 0, 1); /* pic_struct_present_flag */
374         bitstream_put_ui(bs, 0, 1); /* bitstream_restriction_flag */
375     }
376
377     rbsp_trailing_bits(bs);     /* rbsp_trailing_bits */
378 }
379
380
381 static void pps_rbsp(bitstream *bs)
382 {
383     bitstream_put_ue(bs, pic_param.pic_parameter_set_id);      /* pic_parameter_set_id */
384     bitstream_put_ue(bs, pic_param.seq_parameter_set_id);      /* seq_parameter_set_id */
385
386     bitstream_put_ui(bs, pic_param.pic_fields.bits.entropy_coding_mode_flag, 1);  /* entropy_coding_mode_flag */
387
388     bitstream_put_ui(bs, 0, 1);                         /* pic_order_present_flag: 0 */
389
390     bitstream_put_ue(bs, 0);                            /* num_slice_groups_minus1 */
391
392     bitstream_put_ue(bs, pic_param.num_ref_idx_l0_active_minus1);      /* num_ref_idx_l0_active_minus1 */
393     bitstream_put_ue(bs, pic_param.num_ref_idx_l1_active_minus1);      /* num_ref_idx_l1_active_minus1 1 */
394
395     bitstream_put_ui(bs, pic_param.pic_fields.bits.weighted_pred_flag, 1);     /* weighted_pred_flag: 0 */
396     bitstream_put_ui(bs, pic_param.pic_fields.bits.weighted_bipred_idc, 2);     /* weighted_bipred_idc: 0 */
397
398     bitstream_put_se(bs, pic_param.pic_init_qp - 26);  /* pic_init_qp_minus26 */
399     bitstream_put_se(bs, 0);                            /* pic_init_qs_minus26 */
400     bitstream_put_se(bs, 0);                            /* chroma_qp_index_offset */
401
402     bitstream_put_ui(bs, pic_param.pic_fields.bits.deblocking_filter_control_present_flag, 1); /* deblocking_filter_control_present_flag */
403     bitstream_put_ui(bs, 0, 1);                         /* constrained_intra_pred_flag */
404     bitstream_put_ui(bs, 0, 1);                         /* redundant_pic_cnt_present_flag */
405     
406     /* more_rbsp_data */
407     bitstream_put_ui(bs, pic_param.pic_fields.bits.transform_8x8_mode_flag, 1);    /*transform_8x8_mode_flag */
408     bitstream_put_ui(bs, 0, 1);                         /* pic_scaling_matrix_present_flag */
409     bitstream_put_se(bs, pic_param.second_chroma_qp_index_offset );    /*second_chroma_qp_index_offset */
410
411     rbsp_trailing_bits(bs);
412 }
413
414
415 static int
416 build_packed_pic_buffer(unsigned char **header_buffer)
417 {
418     bitstream bs;
419
420     bitstream_start(&bs);
421     nal_start_code_prefix(&bs);
422     nal_header(&bs, NAL_REF_IDC_HIGH, NAL_PPS);
423     pps_rbsp(&bs);
424     bitstream_end(&bs);
425
426     *header_buffer = (unsigned char *)bs.buffer;
427     return bs.bit_offset;
428 }
429
430 static int
431 build_packed_seq_buffer(unsigned char **header_buffer)
432 {
433     bitstream bs;
434
435     bitstream_start(&bs);
436     nal_start_code_prefix(&bs);
437     nal_header(&bs, NAL_REF_IDC_HIGH, NAL_SPS);
438     sps_rbsp(&bs);
439     bitstream_end(&bs);
440
441     *header_buffer = (unsigned char *)bs.buffer;
442     return bs.bit_offset;
443 }
444
445 static int 
446 build_packed_sei_buffer_timing(unsigned int init_cpb_removal_length,
447                                 unsigned int init_cpb_removal_delay,
448                                 unsigned int init_cpb_removal_delay_offset,
449                                 unsigned int cpb_removal_length,
450                                 unsigned int cpb_removal_delay,
451                                 unsigned int dpb_output_length,
452                                 unsigned int dpb_output_delay,
453                                 unsigned char **sei_buffer)
454 {
455     unsigned char *byte_buf;
456     int bp_byte_size, i, pic_byte_size;
457
458     bitstream nal_bs;
459     bitstream sei_bp_bs, sei_pic_bs;
460
461     bitstream_start(&sei_bp_bs);
462     bitstream_put_ue(&sei_bp_bs, 0);       /*seq_parameter_set_id*/
463     bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay, cpb_removal_length); 
464     bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay_offset, cpb_removal_length); 
465     if ( sei_bp_bs.bit_offset & 0x7) {
466         bitstream_put_ui(&sei_bp_bs, 1, 1);
467     }
468     bitstream_end(&sei_bp_bs);
469     bp_byte_size = (sei_bp_bs.bit_offset + 7) / 8;
470     
471     bitstream_start(&sei_pic_bs);
472     bitstream_put_ui(&sei_pic_bs, cpb_removal_delay, cpb_removal_length); 
473     bitstream_put_ui(&sei_pic_bs, dpb_output_delay, dpb_output_length); 
474     if ( sei_pic_bs.bit_offset & 0x7) {
475         bitstream_put_ui(&sei_pic_bs, 1, 1);
476     }
477     bitstream_end(&sei_pic_bs);
478     pic_byte_size = (sei_pic_bs.bit_offset + 7) / 8;
479     
480     bitstream_start(&nal_bs);
481     nal_start_code_prefix(&nal_bs);
482     nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
483
484         /* Write the SEI buffer period data */    
485     bitstream_put_ui(&nal_bs, 0, 8);
486     bitstream_put_ui(&nal_bs, bp_byte_size, 8);
487     
488     byte_buf = (unsigned char *)sei_bp_bs.buffer;
489     for(i = 0; i < bp_byte_size; i++) {
490         bitstream_put_ui(&nal_bs, byte_buf[i], 8);
491     }
492     free(byte_buf);
493         /* write the SEI timing data */
494     bitstream_put_ui(&nal_bs, 0x01, 8);
495     bitstream_put_ui(&nal_bs, pic_byte_size, 8);
496     
497     byte_buf = (unsigned char *)sei_pic_bs.buffer;
498     for(i = 0; i < pic_byte_size; i++) {
499         bitstream_put_ui(&nal_bs, byte_buf[i], 8);
500     }
501     free(byte_buf);
502
503     rbsp_trailing_bits(&nal_bs);
504     bitstream_end(&nal_bs);
505
506     *sei_buffer = (unsigned char *)nal_bs.buffer; 
507    
508     return nal_bs.bit_offset;
509 }
510
511
512
513 /*
514  * Helper function for profiling purposes
515  */
516 static unsigned int GetTickCount()
517 {
518     struct timeval tv;
519     if (gettimeofday(&tv, NULL))
520         return 0;
521     return tv.tv_usec/1000+tv.tv_sec*1000;
522 }
523
524 /*
525   Assume frame sequence is: Frame#0,#1,#2,...,#M,...,#X,... (encoding order)
526   1) period between Frame #X and Frame #N = #X - #N
527   2) 0 means infinite for intra_period/intra_idr_period, and 0 is invalid for ip_period
528   3) intra_idr_period % intra_period (intra_period > 0) and intra_period % ip_period must be 0
529   4) intra_period and intra_idr_period take precedence over ip_period
530   5) if ip_period > 1, intra_period and intra_idr_period are not  the strict periods 
531      of I/IDR frames, see bellow examples
532   -------------------------------------------------------------------
533   intra_period intra_idr_period ip_period frame sequence (intra_period/intra_idr_period/ip_period)
534   0            ignored          1          IDRPPPPPPP ...     (No IDR/I any more)
535   0            ignored        >=2          IDR(PBB)(PBB)...   (No IDR/I any more)
536   1            0                ignored    IDRIIIIIII...      (No IDR any more)
537   1            1                ignored    IDR IDR IDR IDR...
538   1            >=2              ignored    IDRII IDRII IDR... (1/3/ignore)
539   >=2          0                1          IDRPPP IPPP I...   (3/0/1)
540   >=2          0              >=2          IDR(PBB)(PBB)(IBB) (6/0/3)
541                                               (PBB)(IBB)(PBB)(IBB)... 
542   >=2          >=2              1          IDRPPPPP IPPPPP IPPPPP (6/18/1)
543                                            IDRPPPPP IPPPPP IPPPPP...
544   >=2          >=2              >=2        {IDR(PBB)(PBB)(IBB)(PBB)(IBB)(PBB)} (6/18/3)
545                                            {IDR(PBB)(PBB)(IBB)(PBB)(IBB)(PBB)}...
546                                            {IDR(PBB)(PBB)(IBB)(PBB)}           (6/12/3)
547                                            {IDR(PBB)(PBB)(IBB)(PBB)}...
548                                            {IDR(PBB)(PBB)}                     (6/6/3)
549                                            {IDR(PBB)(PBB)}.
550 */
551
552 /*
553  * Return displaying order with specified periods and encoding order
554  * displaying_order: displaying order
555  * frame_type: frame type 
556  */
557 #define FRAME_P 0
558 #define FRAME_B 1
559 #define FRAME_I 2
560 #define FRAME_IDR 7
561 void encoding2display_order(
562     unsigned long long encoding_order,int intra_period,
563     int intra_idr_period,int ip_period,
564     unsigned long long *displaying_order,
565     int *frame_type)
566 {
567     int encoding_order_gop = 0;
568
569     if (intra_period == 1) { /* all are I/IDR frames */
570         *displaying_order = encoding_order;
571         if (intra_idr_period == 0)
572             *frame_type = (encoding_order == 0)?FRAME_IDR:FRAME_I;
573         else
574             *frame_type = (encoding_order % intra_idr_period == 0)?FRAME_IDR:FRAME_I;
575         return;
576     }
577
578     if (intra_period == 0)
579         intra_idr_period = 0;
580
581     /* new sequence like
582      * IDR PPPPP IPPPPP
583      * IDR (PBB)(PBB)(IBB)(PBB)
584      */
585     encoding_order_gop = (intra_idr_period == 0)? encoding_order:
586         (encoding_order % (intra_idr_period + ((ip_period == 1)?0:1)));
587          
588     if (encoding_order_gop == 0) { /* the first frame */
589         *frame_type = FRAME_IDR;
590         *displaying_order = encoding_order;
591     } else if (((encoding_order_gop - 1) % ip_period) != 0) { /* B frames */
592         *frame_type = FRAME_B;
593         *displaying_order = encoding_order - 1;
594     } else if ((intra_period != 0) && /* have I frames */
595                (encoding_order_gop >= 2) &&
596                ((ip_period == 1 && encoding_order_gop % intra_period == 0) || /* for IDR PPPPP IPPPP */
597                 /* for IDR (PBB)(PBB)(IBB) */
598                 (ip_period >= 2 && ((encoding_order_gop - 1) / ip_period % (intra_period / ip_period)) == 0))) {
599         *frame_type = FRAME_I;
600         *displaying_order = encoding_order + ip_period - 1;
601     } else {
602         *frame_type = FRAME_P;
603         *displaying_order = encoding_order + ip_period - 1;
604     }
605 }
606
607
608 static char *fourcc_to_string(int fourcc)
609 {
610     switch (fourcc) {
611     case VA_FOURCC_NV12:
612         return "NV12";
613     case VA_FOURCC_IYUV:
614         return "IYUV";
615     case VA_FOURCC_YV12:
616         return "YV12";
617     case VA_FOURCC_UYVY:
618         return "UYVY";
619     default:
620         return "Unknown";
621     }
622 }
623
624 static int string_to_fourcc(char *str)
625 {
626     int fourcc;
627     
628     if (!strncmp(str, "NV12", 4))
629         fourcc = VA_FOURCC_NV12;
630     else if (!strncmp(str, "IYUV", 4))
631         fourcc = VA_FOURCC_IYUV;
632     else if (!strncmp(str, "YV12", 4))
633         fourcc = VA_FOURCC_YV12;
634     else if (!strncmp(str, "UYVY", 4))
635         fourcc = VA_FOURCC_UYVY;
636     else {
637         printf("Unknow FOURCC\n");
638         fourcc = -1;
639     }
640     return fourcc;
641 }
642
643
644 static char *rc_to_string(int rcmode)
645 {
646     switch (rc_mode) {
647     case VA_RC_NONE:
648         return "NONE";
649     case VA_RC_CBR:
650         return "CBR";
651     case VA_RC_VBR:
652         return "VBR";
653     case VA_RC_VCM:
654         return "VCM";
655     case VA_RC_CQP:
656         return "CQP";
657     case VA_RC_VBR_CONSTRAINED:
658         return "VBR_CONSTRAINED";
659     default:
660         return "Unknown";
661     }
662 }
663
664 static int string_to_rc(char *str)
665 {
666     int rc_mode;
667     
668     if (!strncmp(str, "NONE", 4))
669         rc_mode = VA_RC_NONE;
670     else if (!strncmp(str, "CBR", 3))
671         rc_mode = VA_RC_CBR;
672     else if (!strncmp(str, "VBR", 3))
673         rc_mode = VA_RC_VBR;
674     else if (!strncmp(str, "VCM", 3))
675         rc_mode = VA_RC_VCM;
676     else if (!strncmp(str, "CQP", 3))
677         rc_mode = VA_RC_CQP;
678     else if (!strncmp(str, "VBR_CONSTRAINED", 15))
679         rc_mode = VA_RC_VBR_CONSTRAINED;
680     else {
681         printf("Unknown RC mode\n");
682         rc_mode = -1;
683     }
684     return rc_mode;
685 }
686
687
688 static int print_help(void)
689 {
690     printf("./h264encode <options>\n");
691     printf("   -w <width> -h <height>\n");
692     printf("   -n <frame number>\n");
693     printf("   -o <coded file>\n");
694     printf("   -f <frame rate>\n");
695     printf("   --intra_period <number>\n");
696     printf("   --idr_period <number>\n");
697     printf("   --ip_period <number>\n");
698     printf("   --bitrate <bitrate>\n");
699     printf("   --initialqp <number>\n");
700     printf("   --minqp <number>\n");
701     printf("   --rcmode <NONE|CBR|VBR|VCM|CQP|VBR_CONTRAINED>\n");
702     printf("   --syncmode: sequentially upload source, encoding, save result, no multi-thread\n");
703     printf("   --srcyuv <filename> load YUV from a file\n");
704     printf("   --fourcc <NV12|IYUV|I420|YV12> source YUV fourcc\n");
705
706     return 0;
707 }
708
709 static int process_cmdline(int argc, char *argv[])
710 {
711     char c;
712     const struct option long_opts[] = {
713         {"bitrate", required_argument, NULL, 1 },
714         {"minqp", required_argument, NULL, 2 },
715         {"initialqp", required_argument, NULL, 3 },
716         {"intra_period", required_argument, NULL, 4 },
717         {"idr_period", required_argument, NULL, 5 },
718         {"ip_period", required_argument, NULL, 6 },
719         {"rcmode", required_argument, NULL, 7 },
720         {"srcyuv", required_argument, NULL, 9 },
721         {"fourcc", required_argument, NULL, 10 },
722         {"syncmode", no_argument, NULL, 11 },
723         {NULL, no_argument, NULL, 0 }};
724     int long_index;
725     
726     while ((c =getopt_long_only(argc,argv,"w:h:n:f:o:?",long_opts,&long_index)) != EOF) {
727         switch (c) {
728         case 'w':
729             frame_width = atoi(optarg);
730             break;
731         case 'h':
732             frame_height = atoi(optarg);
733             break;
734         case 'n':
735             frame_count = atoi(optarg);
736             break;
737         case 'f':
738             frame_rate = atoi(optarg);
739             break;
740         case 'o':
741             coded_fn = strdup(optarg);
742             break;
743         case 1:
744             frame_bitrate = atoi(optarg);
745             break;
746         case 2:
747             minimal_qp = atoi(optarg);
748             break;
749         case 3:
750             initial_qp = atoi(optarg);
751             break;
752         case 4:
753             intra_period = atoi(optarg);
754             break;
755         case 5:
756             intra_idr_period = atoi(optarg);
757             break;
758         case 6:
759             ip_period = atoi(optarg);
760             break;
761         case 7:
762             rc_mode = string_to_rc(optarg);
763             if (rc_mode < 0) {
764                 print_help();
765                 exit(1);
766             }
767             break;
768         case 9:
769             srcyuv_fn = strdup(optarg);
770             break;
771         case 10:
772             srcyuv_fourcc = string_to_fourcc(optarg);
773             if (srcyuv_fourcc <= 0) {
774                 print_help();
775                 exit(1);
776             }
777             break;
778         case 11:
779             encode_syncmode = 1;
780             break;
781         case ':':
782         case '?':
783             print_help();
784             exit(0);
785         }
786     }
787
788     if (ip_period < 1) {
789         printf(" ip_period must be greater than 0\n");
790         exit(0);
791     }
792     if (intra_period != 1 && intra_period % ip_period != 0) {
793         printf(" intra_period must be a multiplier of ip_period\n");
794         exit(0);        
795     }
796     if (intra_period != 0 && intra_idr_period % intra_period != 0) {
797         printf(" intra_idr_period must be a multiplier of intra_period\n");
798         exit(0);        
799     }
800
801     if (frame_bitrate == 0)
802         frame_bitrate = frame_width * frame_height * 12 * frame_rate / 50;
803         
804     /* open source file */
805     if (srcyuv_fn) {
806         srcyuv_fp = fopen(srcyuv_fn,"r");
807     
808         if (srcyuv_fp == NULL)
809             printf("Open source YUV file %s failed, use auto-generated YUV data\n", srcyuv_fn);
810         else {
811             fseek(srcyuv_fp, 0L, SEEK_END);
812             srcyuv_frames = ftell(srcyuv_fp) / (frame_width * frame_height * 1.5);
813             printf("Source YUV file %s with %llu frames\n", srcyuv_fn, srcyuv_frames);
814         }
815     }
816     
817     if (coded_fn == NULL) {
818         struct stat buf;
819         if (stat("/tmp", &buf) == 0)
820             coded_fn = strdup("/tmp/test.264");
821         else if (stat("/sdcard", &buf) == 0)
822             coded_fn = strdup("/sdcard/test.264");
823         else
824             coded_fn = strdup("./test.264");
825     }
826     
827     /* store coded data into a file */
828     coded_fp = fopen(coded_fn,"w+");
829     if (coded_fp == NULL) {
830         printf("Open file %s failed, exit\n", coded_fn);
831         exit(1);
832     }
833
834     
835     return 0;
836 }
837
838 static int init_va(void)
839 {
840     VAProfile profile_list[]={VAProfileH264High,VAProfileH264Main,VAProfileH264Baseline,VAProfileH264ConstrainedBaseline};
841     VAEntrypoint entrypoints[VAEntrypointMax]={0};
842     int num_entrypoints,slice_entrypoint;
843     int support_encode = 0;    
844     int major_ver, minor_ver;
845     VAStatus va_status;
846     unsigned int i;
847
848     va_dpy = va_open_display();
849     va_status = vaInitialize(va_dpy, &major_ver, &minor_ver);
850     CHECK_VASTATUS(va_status, "vaInitialize");
851
852     /* use the highest profile */
853     for (i = 0; i < sizeof(profile_list)/sizeof(profile_list[0]); i++) {
854         h264_profile = profile_list[i];
855         vaQueryConfigEntrypoints(va_dpy, h264_profile, entrypoints, &num_entrypoints);
856         for (slice_entrypoint = 0; slice_entrypoint < num_entrypoints; slice_entrypoint++) {
857             if (entrypoints[slice_entrypoint] == VAEntrypointEncSlice) {
858                 support_encode = 1;
859                 break;
860             }
861         }
862         if (support_encode == 1)
863             break;
864     }
865     
866     if (support_encode == 0) {
867         printf("Can't find VAEntrypointEncSlice for H264 profiles\n");
868         exit(1);
869     } else {
870         switch (h264_profile) {
871             case VAProfileH264Baseline:
872                 printf("Use profile VAProfileH264Baseline\n");
873                 ip_period = 1;
874                 constraint_set_flag |= (1 << 0); /* Annex A.2.1 */
875                 break;
876             case VAProfileH264ConstrainedBaseline:
877                 printf("Use profile VAProfileH264ConstrainedBaseline\n");
878                 constraint_set_flag |= (1 << 0 | 1 << 1); /* Annex A.2.2 */
879                 ip_period = 1;
880                 break;
881
882             case VAProfileH264Main:
883                 printf("Use profile VAProfileH264Main\n");
884                 constraint_set_flag |= (1 << 1); /* Annex A.2.2 */
885                 break;
886
887             case VAProfileH264High:
888                 constraint_set_flag |= (1 << 3); /* Annex A.2.4 */
889                 printf("Use profile VAProfileH264High\n");
890                 break;
891             default:
892                 printf("unknow profile. Set to Baseline");
893                 h264_profile = VAProfileH264Baseline;
894                 ip_period = 1;
895                 constraint_set_flag |= (1 << 0); /* Annex A.2.1 */
896                 break;
897         }
898     }
899
900     /* find out the format for the render target, and rate control mode */
901     for (i = 0; i < VAConfigAttribTypeMax; i++)
902         attrib[i].type = i;
903
904     va_status = vaGetConfigAttributes(va_dpy, h264_profile, VAEntrypointEncSlice,
905                                       &attrib[0], VAConfigAttribTypeMax);
906     CHECK_VASTATUS(va_status, "vaGetConfigAttributes");
907     /* check the interested configattrib */
908     if ((attrib[VAConfigAttribRTFormat].value & VA_RT_FORMAT_YUV420) == 0) {
909         printf("Not find desired YUV420 RT format\n");
910         exit(1);
911     } else {
912         config_attrib[config_attrib_num].type = VAConfigAttribRTFormat;
913         config_attrib[config_attrib_num].value = VA_RT_FORMAT_YUV420;
914         config_attrib_num++;
915     }
916     
917     if (attrib[VAConfigAttribRateControl].value != VA_ATTRIB_NOT_SUPPORTED) {
918         int tmp = attrib[VAConfigAttribRateControl].value;
919
920         printf("Supported rate control mode (0x%x):", tmp);
921         
922         if (tmp & VA_RC_NONE)
923             printf("NONE ");
924         if (tmp & VA_RC_CBR)
925             printf("CBR ");
926         if (tmp & VA_RC_VBR)
927             printf("VBR ");
928         if (tmp & VA_RC_VCM)
929             printf("VCM ");
930         if (tmp & VA_RC_CQP)
931             printf("CQP ");
932         if (tmp & VA_RC_VBR_CONSTRAINED)
933             printf("VBR_CONSTRAINED ");
934
935         printf("\n");
936
937         /* need to check if support rc_mode */
938         config_attrib[config_attrib_num].type = VAConfigAttribRateControl;
939         config_attrib[config_attrib_num].value = rc_mode;
940         config_attrib_num++;
941     }
942     
943
944     if (attrib[VAConfigAttribEncPackedHeaders].value != VA_ATTRIB_NOT_SUPPORTED) {
945         int tmp = attrib[VAConfigAttribEncPackedHeaders].value;
946
947         printf("Support VAConfigAttribEncPackedHeaders\n");
948         
949         h264_packedheader = 1;
950         config_attrib[config_attrib_num].type = VAConfigAttribEncPackedHeaders;
951         config_attrib[config_attrib_num].value = VA_ENC_PACKED_HEADER_NONE;
952         
953         if (tmp & VA_ENC_PACKED_HEADER_SEQUENCE) {
954             printf("Support packed sequence headers\n");
955             config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_SEQUENCE;
956         }
957         
958         if (tmp & VA_ENC_PACKED_HEADER_PICTURE) {
959             printf("Support packed picture headers\n");
960             config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_PICTURE;
961         }
962         
963         if (tmp & VA_ENC_PACKED_HEADER_SLICE) {
964             printf("Support packed slice headers\n");
965             config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_SLICE;
966         }
967         
968         if (tmp & VA_ENC_PACKED_HEADER_MISC) {
969             printf("Support packed misc headers\n");
970             config_attrib[config_attrib_num].value |= VA_ENC_PACKED_HEADER_MISC;
971         }
972         
973         config_attrib_num++;
974     }
975
976     if (attrib[VAConfigAttribEncInterlaced].value != VA_ATTRIB_NOT_SUPPORTED) {
977         int tmp = attrib[VAConfigAttribEncInterlaced].value;
978         
979         printf("Support VAConfigAttribEncInterlaced\n");
980
981         if (tmp & VA_ENC_INTERLACED_FRAME)
982             printf("support VA_ENC_INTERLACED_FRAME\n");
983         if (tmp & VA_ENC_INTERLACED_FIELD)
984             printf("Support VA_ENC_INTERLACED_FIELD\n");
985         if (tmp & VA_ENC_INTERLACED_MBAFF)
986             printf("Support VA_ENC_INTERLACED_MBAFF\n");
987         if (tmp & VA_ENC_INTERLACED_PAFF)
988             printf("Support VA_ENC_INTERLACED_PAFF\n");
989         
990         config_attrib[config_attrib_num].type = VAConfigAttribEncInterlaced;
991         config_attrib[config_attrib_num].value = VA_ENC_PACKED_HEADER_NONE;
992         config_attrib_num++;
993     }
994     
995     if (attrib[VAConfigAttribEncMaxRefFrames].value != VA_ATTRIB_NOT_SUPPORTED) {
996         h264_maxref = attrib[VAConfigAttribEncMaxRefFrames].value;
997         
998         printf("Support %d reference frames\n", h264_maxref);
999     }
1000
1001     if (attrib[VAConfigAttribEncMaxSlices].value != VA_ATTRIB_NOT_SUPPORTED)
1002         printf("Support %d slices\n", attrib[VAConfigAttribEncMaxSlices].value);
1003
1004     if (attrib[VAConfigAttribEncSliceStructure].value != VA_ATTRIB_NOT_SUPPORTED) {
1005         int tmp = attrib[VAConfigAttribEncSliceStructure].value;
1006         
1007         printf("Support VAConfigAttribEncSliceStructure\n");
1008
1009         if (tmp & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS)
1010             printf("Support VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS\n");
1011         if (tmp & VA_ENC_SLICE_STRUCTURE_POWER_OF_TWO_ROWS)
1012             printf("Support VA_ENC_SLICE_STRUCTURE_POWER_OF_TWO_ROWS\n");
1013         if (tmp & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS)
1014             printf("Support VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS\n");
1015     }
1016     if (attrib[VAConfigAttribEncMacroblockInfo].value != VA_ATTRIB_NOT_SUPPORTED) {
1017         printf("Support VAConfigAttribEncMacroblockInfo\n");
1018     }
1019
1020     return 0;
1021 }
1022
1023 static int setup_encode()
1024 {
1025     VAStatus va_status;
1026     VASurfaceID *tmp_surfaceid;
1027     int codedbuf_size, i;
1028     
1029     va_status = vaCreateConfig(va_dpy, h264_profile, VAEntrypointEncSlice,
1030             &config_attrib[0], config_attrib_num, &config_id);
1031     CHECK_VASTATUS(va_status, "vaCreateConfig");
1032
1033     /* create source surfaces */
1034     va_status = vaCreateSurfaces(va_dpy,
1035                                  VA_RT_FORMAT_YUV420, frame_width, frame_height,
1036                                  &src_surface[0], SURFACE_NUM,
1037                                  NULL, 0);
1038     CHECK_VASTATUS(va_status, "vaCreateSurfaces");
1039
1040     /* create reference surfaces */
1041     va_status = vaCreateSurfaces(
1042         va_dpy,
1043         VA_RT_FORMAT_YUV420, frame_width, frame_height,
1044         &ref_surface[0], SURFACE_NUM,
1045         NULL, 0
1046         );
1047     CHECK_VASTATUS(va_status, "vaCreateSurfaces");
1048
1049     tmp_surfaceid = calloc(2 * SURFACE_NUM, sizeof(VASurfaceID));
1050     memcpy(tmp_surfaceid, src_surface, SURFACE_NUM * sizeof(VASurfaceID));
1051     memcpy(tmp_surfaceid + SURFACE_NUM, ref_surface, SURFACE_NUM * sizeof(VASurfaceID));
1052     
1053     /* Create a context for this encode pipe */
1054     va_status = vaCreateContext(va_dpy, config_id,
1055                                 frame_width, frame_height,
1056                                 VA_PROGRESSIVE,
1057                                 tmp_surfaceid, 2 * SURFACE_NUM,
1058                                 &context_id);
1059     CHECK_VASTATUS(va_status, "vaCreateContext");
1060     free(tmp_surfaceid);
1061
1062     codedbuf_size = (frame_width * frame_height * 400) / (16*16);
1063
1064     for (i = 0; i < SURFACE_NUM; i++) {
1065         /* create coded buffer once for all
1066          * other VA buffers which won't be used again after vaRenderPicture.
1067          * so APP can always vaCreateBuffer for every frame
1068          * but coded buffer need to be mapped and accessed after vaRenderPicture/vaEndPicture
1069          * so VA won't maintain the coded buffer
1070          */
1071         va_status = vaCreateBuffer(va_dpy,context_id,VAEncCodedBufferType,
1072                 codedbuf_size, 1, NULL, &coded_buf[i]);
1073         CHECK_VASTATUS(va_status,"vaCreateBuffer");
1074     }
1075     
1076     return 0;
1077 }
1078
1079
1080
1081 #define partition(ref, field, key, ascending)   \
1082     while (i <= j) {                            \
1083         if (ascending) {                        \
1084             while (ref[i].field < key)          \
1085                 i++;                            \
1086             while (ref[j].field > key)          \
1087                 j--;                            \
1088         } else {                                \
1089             while (ref[i].field > key)          \
1090                 i++;                            \
1091             while (ref[j].field < key)          \
1092                 j--;                            \
1093         }                                       \
1094         if (i <= j) {                           \
1095             tmp = ref[i];                       \
1096             ref[i] = ref[j];                    \
1097             ref[j] = tmp;                       \
1098             i++;                                \
1099             j--;                                \
1100         }                                       \
1101     }                                           \
1102
1103 static void sort_one(VAPictureH264 ref[], int left, int right,
1104                      int ascending, int frame_idx)
1105 {
1106     int i = left, j = right;
1107     unsigned int key;
1108     VAPictureH264 tmp;
1109
1110     if (frame_idx) {
1111         key = ref[(left + right) / 2].frame_idx;
1112         partition(ref, frame_idx, key, ascending);
1113     } else {
1114         key = ref[(left + right) / 2].TopFieldOrderCnt;
1115         partition(ref, TopFieldOrderCnt, key, ascending);
1116     }
1117     
1118     /* recursion */
1119     if (left < j)
1120         sort_one(ref, left, j, ascending, frame_idx);
1121     
1122     if (i < right)
1123         sort_one(ref, i, right, ascending, frame_idx);
1124 }
1125
1126 static void sort_two(VAPictureH264 ref[], int left, int right, int key, int frame_idx,
1127                        int divide_ascending, int list0_ascending, int list1_ascending)
1128 {
1129     int i = left, j = right;
1130     VAPictureH264 tmp;
1131
1132     if (frame_idx) {
1133         partition(ref, frame_idx, key, divide_ascending);
1134     } else {
1135         partition(ref, TopFieldOrderCnt, key, divide_ascending);
1136     }
1137     
1138
1139     sort_one(ref, left, i-1, list0_ascending, frame_idx);
1140     sort_one(ref, j+1, right, list1_ascending, frame_idx);
1141 }
1142
1143 static int update_ReferenceFrames(void)
1144 {
1145     int i;
1146     
1147     if (current_frame_type == FRAME_B)
1148         return 0;
1149
1150     CurrentCurrPic.flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
1151     numShortTerm++;
1152     if (numShortTerm > num_ref_frames)
1153         numShortTerm = num_ref_frames;
1154     for (i=numShortTerm-1; i>0; i--)
1155         ReferenceFrames[i] = ReferenceFrames[i-1];
1156     ReferenceFrames[0] = CurrentCurrPic;
1157     
1158     if (current_frame_type != FRAME_B)
1159         current_frame_num++;
1160     if (current_frame_num > MaxFrameNum)
1161         current_frame_num = 0;
1162     
1163     return 0;
1164 }
1165
1166 static int update_RefPicList(void)
1167 {
1168     unsigned int current_poc = CurrentCurrPic.TopFieldOrderCnt;
1169
1170     if (current_frame_type == FRAME_IDR) {
1171         numShortTerm = 0;
1172         current_frame_num = 0;
1173         return 0;
1174     }
1175     
1176     if (current_frame_type == FRAME_P) {
1177         memcpy(RefPicList0_P, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
1178         sort_one(RefPicList0_P, 0, numShortTerm-1, 0, 1);
1179     }
1180     
1181     if (current_frame_type == FRAME_B) {
1182         memcpy(RefPicList0_B, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
1183         sort_two(RefPicList0_B, 0, numShortTerm-1, current_poc, 0,
1184                  1, 0, 1);
1185
1186         memcpy(RefPicList1_B, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
1187         sort_two(RefPicList1_B, 0, numShortTerm-1, current_poc, 0,
1188                  0, 1, 0);
1189     }
1190     
1191     return 0;
1192 }
1193
1194
1195 static int render_sequence(void)
1196 {
1197     VABufferID seq_param_buf, rc_param_buf, render_id[2];
1198     VAStatus va_status;
1199     VAEncMiscParameterBuffer *misc_param;
1200     VAEncMiscParameterRateControl *misc_rate_ctrl;
1201     
1202     seq_param.level_idc = 41 /*SH_LEVEL_3*/;
1203     seq_param.picture_width_in_mbs = frame_width / 16;
1204     seq_param.picture_height_in_mbs = frame_height / 16;
1205     seq_param.bits_per_second = frame_bitrate;
1206
1207     seq_param.intra_period = intra_period;
1208     seq_param.intra_idr_period = intra_idr_period;
1209     seq_param.ip_period = ip_period;
1210
1211     seq_param.max_num_ref_frames = num_ref_frames;
1212     seq_param.seq_fields.bits.frame_mbs_only_flag = 1;
1213     seq_param.time_scale = 900;
1214     seq_param.num_units_in_tick = 15; /* Tc = num_units_in_tick / time_sacle */
1215     seq_param.seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 = Log2MaxPicOrderCntLsb - 4;
1216     seq_param.seq_fields.bits.log2_max_frame_num_minus4 = Log2MaxFrameNum - 4;;
1217     seq_param.seq_fields.bits.frame_mbs_only_flag = 1;
1218     
1219     va_status = vaCreateBuffer(va_dpy, context_id,
1220                                VAEncSequenceParameterBufferType,
1221                                sizeof(seq_param),1,&seq_param,&seq_param_buf);
1222     CHECK_VASTATUS(va_status,"vaCreateBuffer");
1223     
1224     va_status = vaCreateBuffer(va_dpy, context_id,
1225                                VAEncMiscParameterBufferType,
1226                                sizeof(VAEncMiscParameterBuffer) + sizeof(VAEncMiscParameterRateControl),
1227                                1,NULL,&rc_param_buf);
1228     CHECK_VASTATUS(va_status,"vaCreateBuffer");
1229     
1230     vaMapBuffer(va_dpy, rc_param_buf,(void **)&misc_param);
1231     misc_param->type = VAEncMiscParameterTypeRateControl;
1232     misc_rate_ctrl = (VAEncMiscParameterRateControl *)misc_param->data;
1233     memset(misc_rate_ctrl, 0, sizeof(*misc_rate_ctrl));
1234     misc_rate_ctrl->bits_per_second = frame_bitrate;
1235     misc_rate_ctrl->initial_qp = initial_qp;
1236     misc_rate_ctrl->min_qp = minimal_qp;
1237     misc_rate_ctrl->basic_unit_size = 0;
1238     vaUnmapBuffer(va_dpy, rc_param_buf);
1239
1240     render_id[0] = seq_param_buf;
1241     render_id[1] = rc_param_buf;
1242     
1243     va_status = vaRenderPicture(va_dpy,context_id, &render_id[0], 2);
1244     CHECK_VASTATUS(va_status,"vaRenderPicture");;
1245
1246     return 0;
1247 }
1248
1249 static unsigned int calc_poc(unsigned int pic_order_cnt_lsb)
1250 {
1251     static unsigned int prevPicOrderCntMsb = 0, prevPicOrderCntLsb = 0;
1252     static unsigned int PicOrderCntMsb = 0;
1253     unsigned int TopFieldOrderCnt;
1254     
1255     if (current_frame_type == FRAME_IDR)
1256         prevPicOrderCntMsb = prevPicOrderCntLsb = 0;
1257     
1258     prevPicOrderCntMsb = PicOrderCntMsb;
1259     if ((pic_order_cnt_lsb < prevPicOrderCntLsb) &&
1260         ((prevPicOrderCntLsb - pic_order_cnt_lsb) >= (MaxPicOrderCntLsb / 2)))
1261         PicOrderCntMsb = prevPicOrderCntMsb + MaxPicOrderCntLsb;
1262     else if ((pic_order_cnt_lsb > prevPicOrderCntLsb) &&
1263              ((pic_order_cnt_lsb - prevPicOrderCntLsb) > (MaxPicOrderCntLsb / 2)))
1264         PicOrderCntMsb = prevPicOrderCntMsb - MaxPicOrderCntLsb;
1265     else
1266         PicOrderCntMsb = prevPicOrderCntMsb;
1267     
1268     TopFieldOrderCnt = PicOrderCntMsb + pic_order_cnt_lsb;
1269
1270     return TopFieldOrderCnt;
1271 }
1272
1273 static int render_picture(void)
1274 {
1275     VABufferID pic_param_buf;
1276     VAStatus va_status;
1277     int i = 0;
1278
1279     pic_param.CurrPic.picture_id = ref_surface[current_slot];
1280     pic_param.CurrPic.frame_idx = current_frame_num;
1281     pic_param.CurrPic.flags = 0;
1282     pic_param.CurrPic.TopFieldOrderCnt = calc_poc(current_frame_display % MaxPicOrderCntLsb);
1283     pic_param.CurrPic.BottomFieldOrderCnt = pic_param.CurrPic.TopFieldOrderCnt;
1284     CurrentCurrPic = pic_param.CurrPic;
1285
1286     if (getenv("TO_DEL")) { /* set RefPicList into ReferenceFrames */
1287         update_RefPicList(); /* calc RefPicList */
1288         memset(pic_param.ReferenceFrames, 0xff, 16 * sizeof(VAPictureH264)); /* invalid all */
1289         if (current_frame_type == FRAME_P) {
1290             pic_param.ReferenceFrames[0] = RefPicList0_P[0];
1291         } else if (current_frame_type == FRAME_B) {
1292             pic_param.ReferenceFrames[0] = RefPicList0_B[0];
1293             pic_param.ReferenceFrames[1] = RefPicList1_B[0];
1294         }
1295     } else {
1296         memcpy(pic_param.ReferenceFrames, ReferenceFrames, numShortTerm*sizeof(VAPictureH264));
1297         for (i = numShortTerm; i < SURFACE_NUM; i++) {
1298             pic_param.ReferenceFrames[i].picture_id = VA_INVALID_SURFACE;
1299             pic_param.ReferenceFrames[i].flags = VA_PICTURE_H264_INVALID;
1300         }
1301     }
1302     
1303     pic_param.pic_fields.bits.idr_pic_flag = (current_frame_type == FRAME_IDR);
1304     pic_param.pic_fields.bits.reference_pic_flag = (current_frame_type != FRAME_B);
1305     pic_param.pic_fields.bits.entropy_coding_mode_flag = 1;
1306     pic_param.pic_fields.bits.deblocking_filter_control_present_flag = 1;
1307     pic_param.frame_num = current_frame_num;
1308     pic_param.coded_buf = coded_buf[current_slot];
1309     pic_param.last_picture = (current_frame_encoding == frame_count);
1310     pic_param.pic_init_qp = initial_qp;
1311
1312     va_status = vaCreateBuffer(va_dpy, context_id,VAEncPictureParameterBufferType,
1313                                sizeof(pic_param),1,&pic_param, &pic_param_buf);
1314     CHECK_VASTATUS(va_status,"vaCreateBuffer");;
1315
1316     va_status = vaRenderPicture(va_dpy,context_id, &pic_param_buf, 1);
1317     CHECK_VASTATUS(va_status,"vaRenderPicture");
1318
1319     return 0;
1320 }
1321
1322 static int render_packedsequence(void)
1323 {
1324     VAEncPackedHeaderParameterBuffer packedheader_param_buffer={0};
1325     VABufferID packedseq_para_bufid, packedseq_data_bufid, render_id[2];
1326     unsigned int length_in_bits;
1327     unsigned char *packedseq_buffer = NULL;
1328     VAStatus va_status;
1329
1330     length_in_bits = build_packed_seq_buffer(&packedseq_buffer); 
1331     
1332     packedheader_param_buffer.type = VAEncPackedHeaderSequence;
1333     
1334     packedheader_param_buffer.bit_length = length_in_bits; /*length_in_bits*/
1335     packedheader_param_buffer.has_emulation_bytes = 0;
1336     va_status = vaCreateBuffer(va_dpy,
1337                                context_id,
1338                                VAEncPackedHeaderParameterBufferType,
1339                                sizeof(packedheader_param_buffer), 1, &packedheader_param_buffer,
1340                                &packedseq_para_bufid);
1341     CHECK_VASTATUS(va_status,"vaCreateBuffer");
1342
1343     va_status = vaCreateBuffer(va_dpy,
1344                                context_id,
1345                                VAEncPackedHeaderDataBufferType,
1346                                (length_in_bits + 7) / 8, 1, packedseq_buffer,
1347                                &packedseq_data_bufid);
1348     CHECK_VASTATUS(va_status,"vaCreateBuffer");
1349
1350     render_id[0] = packedseq_para_bufid;
1351     render_id[1] = packedseq_data_bufid;
1352     va_status = vaRenderPicture(va_dpy,context_id, render_id, 2);
1353     CHECK_VASTATUS(va_status,"vaRenderPicture");
1354
1355     free(packedseq_buffer);
1356     
1357     return 0;
1358 }
1359
1360
1361 static int render_packedpicture(void)
1362 {
1363     VAEncPackedHeaderParameterBuffer packedheader_param_buffer={0};
1364     VABufferID packedpic_para_bufid, packedpic_data_bufid, render_id[2];
1365     unsigned int length_in_bits;
1366     unsigned char *packedpic_buffer = NULL;
1367     VAStatus va_status;
1368
1369     length_in_bits = build_packed_pic_buffer(&packedpic_buffer); 
1370     packedheader_param_buffer.type = VAEncPackedHeaderPicture;
1371     packedheader_param_buffer.bit_length = length_in_bits;
1372     packedheader_param_buffer.has_emulation_bytes = 0;
1373
1374     va_status = vaCreateBuffer(va_dpy,
1375                                context_id,
1376                                VAEncPackedHeaderParameterBufferType,
1377                                sizeof(packedheader_param_buffer), 1, &packedheader_param_buffer,
1378                                &packedpic_para_bufid);
1379     CHECK_VASTATUS(va_status,"vaCreateBuffer");
1380
1381     va_status = vaCreateBuffer(va_dpy,
1382                                context_id,
1383                                VAEncPackedHeaderDataBufferType,
1384                                (length_in_bits + 7) / 8, 1, packedpic_buffer,
1385                                &packedpic_data_bufid);
1386     CHECK_VASTATUS(va_status,"vaCreateBuffer");
1387
1388     render_id[0] = packedpic_para_bufid;
1389     render_id[1] = packedpic_data_bufid;
1390     va_status = vaRenderPicture(va_dpy,context_id, render_id, 2);
1391     CHECK_VASTATUS(va_status,"vaRenderPicture");
1392
1393     free(packedpic_buffer);
1394     
1395     return 0;
1396 }
1397
1398 static void render_packedsei(void)
1399 {
1400     VAEncPackedHeaderParameterBuffer packed_header_param_buffer;
1401     VABufferID packed_sei_header_param_buf_id, packed_sei_buf_id, render_id[2];
1402     unsigned int length_in_bits, offset_in_bytes;
1403     unsigned char *packed_sei_buffer = NULL;
1404     VAStatus va_status;
1405     int init_cpb_size, target_bit_rate, i_initial_cpb_removal_delay_length, i_initial_cpb_removal_delay;
1406     int i_cpb_removal_delay, i_dpb_output_delay_length, i_cpb_removal_delay_length;
1407
1408     /* it comes for the bps defined in SPS */
1409     target_bit_rate = frame_bitrate;
1410     init_cpb_size = (target_bit_rate * 8) >> 10;
1411     i_initial_cpb_removal_delay = init_cpb_size * 0.5 * 1024 / target_bit_rate * 90000;
1412
1413     i_cpb_removal_delay = 2;
1414     i_initial_cpb_removal_delay_length = 24;
1415     i_cpb_removal_delay_length = 24;
1416     i_dpb_output_delay_length = 24;
1417     
1418
1419     length_in_bits = build_packed_sei_buffer_timing(
1420         i_initial_cpb_removal_delay_length,
1421         i_initial_cpb_removal_delay,
1422         0,
1423         i_cpb_removal_delay_length,
1424         i_cpb_removal_delay * current_frame_encoding,
1425         i_dpb_output_delay_length,
1426         0,
1427         &packed_sei_buffer);
1428
1429     offset_in_bytes = 0;
1430     packed_header_param_buffer.type = VAEncPackedHeaderH264_SEI;
1431     packed_header_param_buffer.bit_length = length_in_bits;
1432     packed_header_param_buffer.has_emulation_bytes = 0;
1433
1434     va_status = vaCreateBuffer(va_dpy,
1435                                context_id,
1436                                VAEncPackedHeaderParameterBufferType,
1437                                sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
1438                                &packed_sei_header_param_buf_id);
1439     CHECK_VASTATUS(va_status,"vaCreateBuffer");
1440
1441     va_status = vaCreateBuffer(va_dpy,
1442                                context_id,
1443                                VAEncPackedHeaderDataBufferType,
1444                                (length_in_bits + 7) / 8, 1, packed_sei_buffer,
1445                                &packed_sei_buf_id);
1446     CHECK_VASTATUS(va_status,"vaCreateBuffer");
1447
1448
1449     render_id[0] = packed_sei_header_param_buf_id;
1450     render_id[1] = packed_sei_buf_id;
1451     va_status = vaRenderPicture(va_dpy,context_id, render_id, 2);
1452     CHECK_VASTATUS(va_status,"vaRenderPicture");
1453
1454     
1455     free(packed_sei_buffer);
1456         
1457     return;
1458 }
1459
1460
1461 static int render_hrd(void)
1462 {
1463     VABufferID misc_parameter_hrd_buf_id;
1464     VAStatus va_status;
1465     VAEncMiscParameterBuffer *misc_param;
1466     VAEncMiscParameterHRD *misc_hrd_param;
1467     
1468     va_status = vaCreateBuffer(va_dpy, context_id,
1469                    VAEncMiscParameterBufferType,
1470                    sizeof(VAEncMiscParameterBuffer) + sizeof(VAEncMiscParameterHRD),
1471                    1,
1472                    NULL, 
1473                    &misc_parameter_hrd_buf_id);
1474     CHECK_VASTATUS(va_status, "vaCreateBuffer");
1475
1476     vaMapBuffer(va_dpy,
1477                 misc_parameter_hrd_buf_id,
1478                 (void **)&misc_param);
1479     misc_param->type = VAEncMiscParameterTypeHRD;
1480     misc_hrd_param = (VAEncMiscParameterHRD *)misc_param->data;
1481
1482     if (frame_bitrate > 0) {
1483         misc_hrd_param->initial_buffer_fullness = frame_bitrate * 1024 * 4;
1484         misc_hrd_param->buffer_size = frame_bitrate * 1024 * 8;
1485     } else {
1486         misc_hrd_param->initial_buffer_fullness = 0;
1487         misc_hrd_param->buffer_size = 0;
1488     }
1489     vaUnmapBuffer(va_dpy, misc_parameter_hrd_buf_id);
1490
1491     va_status = vaRenderPicture(va_dpy,context_id, &misc_parameter_hrd_buf_id, 1);
1492     CHECK_VASTATUS(va_status,"vaRenderPicture");;
1493
1494     return 0;
1495 }
1496
1497 static int render_slice(void)
1498 {
1499     VABufferID slice_param_buf;
1500     VAStatus va_status;
1501     int i;
1502
1503     update_RefPicList();
1504     
1505     /* one frame, one slice */
1506     slice_param.macroblock_address = 0;
1507     slice_param.num_macroblocks = frame_width*frame_height/(16*16); /* Measured by MB */
1508     slice_param.slice_type = (current_frame_type == FRAME_IDR)?2:current_frame_type;
1509     if (current_frame_type == FRAME_I || current_frame_type == FRAME_IDR) {
1510         ;
1511     } else if (current_frame_type == FRAME_P) {
1512         int refpiclist0_max = h264_maxref & 0xffff;
1513         memcpy(slice_param.RefPicList0, RefPicList0_P, refpiclist0_max*sizeof(VAPictureH264));
1514
1515         for (i = refpiclist0_max; i < 32; i++) {
1516             slice_param.RefPicList0[i].picture_id = VA_INVALID_SURFACE;
1517             slice_param.RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
1518         }
1519     } else if (current_frame_type == FRAME_B) {
1520         int refpiclist0_max = h264_maxref & 0xffff;
1521         int refpiclist1_max = (h264_maxref >> 16) & 0xffff;
1522
1523         memcpy(slice_param.RefPicList0, RefPicList0_B, refpiclist0_max*sizeof(VAPictureH264));
1524         for (i = refpiclist0_max; i < 32; i++) {
1525             slice_param.RefPicList0[i].picture_id = VA_INVALID_SURFACE;
1526             slice_param.RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
1527         }
1528
1529         memcpy(slice_param.RefPicList1, RefPicList1_B, refpiclist1_max*sizeof(VAPictureH264));
1530         for (i = refpiclist1_max; i < 32; i++) {
1531             slice_param.RefPicList1[i].picture_id = VA_INVALID_SURFACE;
1532             slice_param.RefPicList1[i].flags = VA_PICTURE_H264_INVALID;
1533         }
1534     }
1535
1536     slice_param.slice_alpha_c0_offset_div2 = 2;
1537     slice_param.slice_beta_offset_div2 = 2;
1538     slice_param.pic_order_cnt_lsb = current_frame_display % MaxPicOrderCntLsb;
1539     
1540     va_status = vaCreateBuffer(va_dpy,context_id,VAEncSliceParameterBufferType,
1541                                sizeof(slice_param),1,&slice_param,&slice_param_buf);
1542     CHECK_VASTATUS(va_status,"vaCreateBuffer");;
1543
1544     va_status = vaRenderPicture(va_dpy,context_id, &slice_param_buf, 1);
1545     CHECK_VASTATUS(va_status,"vaRenderPicture");
1546
1547     return 0;
1548 }
1549
1550
1551 static int upload_source_YUV_once_for_all()
1552 {
1553     int box_width=8;
1554     int row_shift=0;
1555     int i;
1556
1557     for (i = 0; i < SURFACE_NUM; i++) {
1558         printf("\rLoading data into surface %d.....", i);
1559         upload_surface(va_dpy, src_surface[i], box_width, row_shift, 0);
1560
1561         row_shift++;
1562         if (row_shift==(2*box_width)) row_shift= 0;
1563     }
1564     printf("Completed surface loading\n");
1565
1566     return 0;
1567 }
1568
1569
1570 static int load_surface(VASurfaceID surface_id, unsigned long long display_order)
1571 {
1572     VAImage surface_image;
1573     unsigned char *surface_p=NULL, *Y_start=NULL, *U_start=NULL,*V_start=NULL, *uv_ptr;
1574     int Y_pitch=0, U_pitch=0, row, V_pitch, uv_size;
1575     VAStatus va_status;
1576
1577     if (srcyuv_fp == NULL)
1578         return 0;
1579     
1580     /* rewind the file pointer if encoding more than srcyuv_frames */
1581     display_order = display_order % srcyuv_frames;
1582     
1583     fseek(srcyuv_fp, display_order * frame_width * frame_height * 1.5, SEEK_SET);
1584     
1585     va_status = vaDeriveImage(va_dpy,surface_id, &surface_image);
1586     CHECK_VASTATUS(va_status,"vaDeriveImage");
1587
1588     vaMapBuffer(va_dpy,surface_image.buf,(void **)&surface_p);
1589     assert(VA_STATUS_SUCCESS == va_status);
1590
1591     Y_start = surface_p;
1592     Y_pitch = surface_image.pitches[0];
1593     switch (surface_image.format.fourcc) {
1594     case VA_FOURCC_NV12:
1595         U_start = (unsigned char *)surface_p + surface_image.offsets[1];
1596         V_start = U_start + 1;
1597         U_pitch = surface_image.pitches[1];
1598         V_pitch = surface_image.pitches[1];
1599         break;
1600     case VA_FOURCC_IYUV:
1601         U_start = (unsigned char *)surface_p + surface_image.offsets[1];
1602         V_start = (unsigned char *)surface_p + surface_image.offsets[2];
1603         U_pitch = surface_image.pitches[1];
1604         V_pitch = surface_image.pitches[2];
1605         break;
1606     case VA_FOURCC_YV12:
1607         U_start = (unsigned char *)surface_p + surface_image.offsets[2];
1608         V_start = (unsigned char *)surface_p + surface_image.offsets[1];
1609         U_pitch = surface_image.pitches[2];
1610         V_pitch = surface_image.pitches[1];
1611         break;
1612     case VA_FOURCC_YUY2:
1613         U_start = surface_p + 1;
1614         V_start = surface_p + 3;
1615         U_pitch = surface_image.pitches[0];
1616         V_pitch = surface_image.pitches[0];
1617         break;
1618     default:
1619         assert(0);
1620     }
1621
1622     /* copy Y plane */
1623     for (row=0;row<surface_image.height;row++) {
1624         unsigned char *Y_row = Y_start + row * Y_pitch;
1625         (void)fread(Y_row, 1, surface_image.width, srcyuv_fp);
1626     }
1627   
1628     /* copy UV data, reset file pointer,
1629      * surface_image.height may not be equal to source YUV height/frame_height
1630      */
1631     fseek(srcyuv_fp,
1632           display_order * frame_width * frame_height * 1.5 + frame_width * frame_height,
1633           SEEK_SET);
1634
1635     uv_size = 2 * (frame_width/2) * (frame_height/2);
1636     uv_ptr = malloc(uv_size);
1637     fread(uv_ptr, uv_size, 1, srcyuv_fp);
1638     
1639     for (row =0; row < surface_image.height/2; row++) {
1640         unsigned char *U_row = U_start + row * U_pitch;
1641         unsigned char *u_ptr, *v_ptr;
1642         int j;
1643         switch (surface_image.format.fourcc) {
1644         case VA_FOURCC_NV12:
1645             if (srcyuv_fourcc == VA_FOURCC_NV12) {
1646                 memcpy(U_row, uv_ptr + row * frame_width, frame_width);
1647                 break;
1648             } else if (srcyuv_fourcc == VA_FOURCC_IYUV) {
1649                 u_ptr = uv_ptr + row * (frame_width/2);
1650                 v_ptr = uv_ptr + (frame_width/2) * (frame_height/2) + row * (frame_width/2);
1651             } else if (srcyuv_fourcc == VA_FOURCC_YV12) {
1652                 v_ptr = uv_ptr + row * (frame_height/2);
1653                 u_ptr = uv_ptr + (frame_width/2) * (frame_height/2) + row * (frame_width/2);
1654             }
1655             for(j = 0; j < frame_width/2; j++) {
1656                 U_row[2*j] = u_ptr[j];
1657                 U_row[2*j+1] = v_ptr[j];
1658             }
1659             break;
1660         case VA_FOURCC_IYUV:
1661         case VA_FOURCC_YV12:
1662         case VA_FOURCC_YUY2:
1663         default:
1664             printf("unsupported fourcc in load_surface\n");
1665             assert(0);
1666         }
1667     }
1668     free(uv_ptr);
1669     
1670     vaUnmapBuffer(va_dpy,surface_image.buf);
1671
1672     vaDestroyImage(va_dpy,surface_image.image_id);
1673
1674     return 0;
1675 }
1676
1677
1678 static int save_codeddata(unsigned long long display_order, unsigned long long encode_order)
1679 {    
1680     VACodedBufferSegment *buf_list = NULL;
1681     VAStatus va_status;
1682     unsigned int coded_size = 0;
1683
1684     va_status = vaMapBuffer(va_dpy,coded_buf[display_order % SURFACE_NUM],(void **)(&buf_list));
1685     CHECK_VASTATUS(va_status,"vaMapBuffer");
1686     while (buf_list != NULL) {
1687         coded_size += fwrite(buf_list->buf, 1, buf_list->size, coded_fp);
1688         buf_list = (VACodedBufferSegment *) buf_list->next;
1689     }
1690     vaUnmapBuffer(va_dpy,coded_buf[display_order % SURFACE_NUM]);
1691
1692     printf("\r      "); /* return back to startpoint */
1693     switch (encode_order % 4) {
1694         case 0:
1695             printf("|");
1696             break;
1697         case 1:
1698             printf("/");
1699             break;
1700         case 2:
1701             printf("-");
1702             break;
1703         case 3:
1704             printf("\\");
1705             break;
1706     }
1707     printf("%08lld", encode_order);
1708     /*
1709     if (current_frame_encoding % intra_count == 0)
1710         printf("(I)");
1711     else
1712         printf("(P)");
1713     */
1714     printf("(%06d bytes coded)",coded_size);
1715     /* skipped frame ? */
1716     printf("                                    ");
1717
1718     return 0;
1719 }
1720
1721
1722 static struct storage_task_t * storage_task_dequque(void)
1723 {
1724     struct storage_task_t *header;
1725
1726     pthread_mutex_lock(&encode_mutex);
1727
1728     header = storage_task_header;    
1729     if (storage_task_header != NULL) {
1730         if (storage_task_tail == storage_task_header)
1731             storage_task_tail = NULL;
1732         storage_task_header = header->next;
1733     }
1734     
1735     pthread_mutex_unlock(&encode_mutex);
1736     
1737     return header;
1738 }
1739
1740 static int storage_task_queue(unsigned long long display_order, unsigned long long encode_order)
1741 {
1742     struct storage_task_t *tmp;
1743
1744     tmp = calloc(1, sizeof(struct storage_task_t));
1745     tmp->display_order = display_order;
1746     tmp->encode_order = encode_order;
1747
1748     pthread_mutex_lock(&encode_mutex);
1749     
1750     if (storage_task_header == NULL) {
1751         storage_task_header = tmp;
1752         storage_task_tail = tmp;
1753     } else {
1754         storage_task_tail->next = tmp;
1755         storage_task_tail = tmp;
1756     }
1757
1758     srcsurface_status[display_order % SURFACE_NUM] = SRC_SURFACE_IN_STORAGE;
1759     pthread_cond_signal(&encode_cond);
1760     
1761     pthread_mutex_unlock(&encode_mutex);
1762     
1763     return 0;
1764 }
1765
1766 static void storage_task(unsigned long long display_order, unsigned long encode_order)
1767 {
1768     unsigned int tmp;
1769     VAStatus va_status;
1770     
1771     tmp = GetTickCount();
1772     va_status = vaSyncSurface(va_dpy, src_surface[display_order % SURFACE_NUM]);
1773     CHECK_VASTATUS(va_status,"vaSyncSurface");
1774     SyncPictureTicks += GetTickCount() - tmp;
1775     tmp = GetTickCount();
1776     save_codeddata(display_order, encode_order);
1777     SavePictureTicks += GetTickCount() - tmp;
1778     /* tbd: save reconstructed frame */
1779         
1780     /* reload a new frame data */
1781     tmp = GetTickCount();
1782     if (srcyuv_fp != NULL)
1783         load_surface(src_surface[display_order % SURFACE_NUM], display_order + SURFACE_NUM);
1784     UploadPictureTicks += GetTickCount() - tmp;
1785
1786     pthread_mutex_lock(&encode_mutex);
1787     srcsurface_status[display_order % SURFACE_NUM] = SRC_SURFACE_IN_ENCODING;
1788     pthread_mutex_unlock(&encode_mutex);
1789 }
1790
1791         
1792 static void * storage_task_thread(void *t)
1793 {
1794     while (1) {
1795         struct storage_task_t *current;
1796         
1797         current = storage_task_dequque();
1798         if (current == NULL) {
1799             pthread_mutex_lock(&encode_mutex);
1800             pthread_cond_wait(&encode_cond, &encode_mutex);
1801             pthread_mutex_unlock(&encode_mutex);
1802             continue;
1803         }
1804         
1805         storage_task(current->display_order, current->encode_order);
1806         
1807         free(current);
1808
1809         /* all frames are saved, exit the thread */
1810         if (++frame_coded >= frame_count)
1811             break;
1812     }
1813
1814     return 0;
1815 }
1816
1817
1818 static int encode_frames(void)
1819 {
1820     unsigned int i, tmp;
1821     VAStatus va_status;
1822     //VASurfaceStatus surface_status;
1823
1824     /* upload RAW YUV data into all surfaces */
1825     tmp = GetTickCount();
1826     if (srcyuv_fp != NULL) {
1827         for (i = 0; i < SURFACE_NUM; i++)
1828             load_surface(src_surface[i], i);
1829     } else
1830         upload_source_YUV_once_for_all();
1831     UploadPictureTicks += GetTickCount() - tmp;
1832     
1833     /* ready for encoding */
1834     memset(srcsurface_status, SRC_SURFACE_IN_ENCODING, sizeof(srcsurface_status));
1835     
1836     memset(&seq_param, 0, sizeof(seq_param));
1837     memset(&pic_param, 0, sizeof(pic_param));
1838     memset(&slice_param, 0, sizeof(slice_param));
1839
1840     if (encode_syncmode == 0)
1841         pthread_create(&encode_thread, NULL, storage_task_thread, NULL);
1842     
1843     for (current_frame_encoding = 0; current_frame_encoding < frame_count; current_frame_encoding++) {
1844         encoding2display_order(current_frame_encoding, intra_period, intra_idr_period, ip_period,
1845                                &current_frame_display, &current_frame_type);
1846
1847         /* check if the source frame is ready */
1848         while (srcsurface_status[current_slot] != SRC_SURFACE_IN_ENCODING);
1849
1850         tmp = GetTickCount();
1851         va_status = vaBeginPicture(va_dpy, context_id, src_surface[current_slot]);
1852         CHECK_VASTATUS(va_status,"vaBeginPicture");
1853         BeginPictureTicks += GetTickCount() - tmp;
1854         
1855         tmp = GetTickCount();
1856         if (current_frame_encoding  == 0) {
1857             render_sequence();
1858             render_picture();            
1859             if (h264_packedheader) {
1860                 render_packedsequence();
1861                 render_packedpicture();
1862             }
1863             //if (rc_mode == VA_RC_CBR)
1864             //    render_packedsei();
1865             //render_hrd();
1866         } else {
1867             //render_sequence();
1868             render_picture();
1869             //if (rc_mode == VA_RC_CBR)
1870             //    render_packedsei();
1871             //render_hrd();
1872         }
1873         render_slice();
1874         RenderPictureTicks += GetTickCount() - tmp;
1875         
1876         tmp = GetTickCount();
1877         va_status = vaEndPicture(va_dpy,context_id);
1878         CHECK_VASTATUS(va_status,"vaEndPicture");;
1879         EndPictureTicks += GetTickCount() - tmp;
1880
1881         if (encode_syncmode)
1882             storage_task(current_frame_display, current_frame_encoding);
1883         else /* queue the storage task queue */
1884             storage_task_queue(current_frame_display, current_frame_encoding);
1885
1886         /* how to process skipped frames
1887            surface_status = (VASurfaceStatus) 0;
1888            va_status = vaQuerySurfaceStatus(va_dpy, src_surface[i%SURFACE_NUM],&surface_status);
1889            frame_skipped = (surface_status & VASurfaceSkipped);
1890         */
1891         update_ReferenceFrames();        
1892     }
1893
1894     if (encode_syncmode == 0) {
1895         int ret;
1896         pthread_join(encode_thread, (void **)&ret);
1897     }
1898     
1899     return 0;
1900 }
1901
1902
1903 static int release_encode()
1904 {
1905     int i;
1906     
1907     vaDestroySurfaces(va_dpy,&src_surface[0],SURFACE_NUM);
1908     vaDestroySurfaces(va_dpy,&ref_surface[0],SURFACE_NUM);
1909
1910     for (i = 0; i < SURFACE_NUM; i++)
1911         vaDestroyBuffer(va_dpy,coded_buf[i]);
1912     
1913     vaDestroyContext(va_dpy,context_id);
1914     vaDestroyConfig(va_dpy,config_id);
1915
1916     return 0;
1917 }
1918
1919 static int deinit_va()
1920
1921     vaTerminate(va_dpy);
1922
1923     va_close_display(va_dpy);
1924
1925     return 0;
1926 }
1927
1928
1929 static int print_input()
1930 {
1931     printf("\n\nINPUT:Try to encode H264...\n");
1932     printf("INPUT: RateControl  : %s\n", rc_to_string(rc_mode));
1933     printf("INPUT: Resolution   : %dx%d, %d frames\n",
1934            frame_width, frame_height, frame_count);
1935     printf("INPUT: FrameRate    : %d\n", frame_rate);
1936     printf("INPUT: Bitrate      : %d\n", frame_bitrate);
1937     printf("INPUT: Slieces      : %d\n", frame_slices);
1938     printf("INPUT: IntraPeriod  : %d\n", intra_period);
1939     printf("INPUT: IDRPeriod    : %d\n", intra_idr_period);
1940     printf("INPUT: IpPeriod     : %d\n", ip_period);
1941     printf("INPUT: Initial QP   : %d\n", initial_qp);
1942     printf("INPUT: Min QP       : %d\n", minimal_qp);
1943     printf("INPUT: Source YUV   : %s", srcyuv_fp?"FILE":"AUTO generated");
1944     if (srcyuv_fp) 
1945         printf(":%s (fourcc %s)\n", srcyuv_fn, fourcc_to_string(srcyuv_fourcc));
1946     else
1947         printf("\n");
1948     printf("INPUT: Coded Clip   : %s\n", coded_fn);
1949     if (recyuv_fp == NULL)
1950         printf("INPUT: Rec   Clip   : %s\n", "Not save reconstructed frame");
1951     else
1952         printf("INPUT: Rec   Clip   : Save reconstructed frame into %s (fourcc %s)\n", recyuv_fn,
1953                fourcc_to_string(srcyuv_fourcc));
1954     
1955     printf("\n\n"); /* return back to startpoint */
1956     
1957     return 0;
1958 }
1959
1960
1961 static int print_performance(unsigned int PictureCount)
1962 {
1963     unsigned int others = 0;
1964
1965     others = TotalTicks - UploadPictureTicks - BeginPictureTicks
1966         - RenderPictureTicks - EndPictureTicks - SyncPictureTicks - SavePictureTicks;
1967     
1968     printf("\n\n");
1969
1970     printf("PERFORMANCE:   Frame Rate           : %.2f fps (%d frames, %d ms (%.2f ms per frame))\n",
1971            (double) 1000*PictureCount / TotalTicks, PictureCount,
1972            TotalTicks, ((double)  TotalTicks) / (double) PictureCount);
1973
1974     printf("PERFORMANCE:     UploadPicture      : %d ms (%.2f, %.2f%% percent)\n",
1975            (int) UploadPictureTicks, ((double)  UploadPictureTicks) / (double) PictureCount,
1976            UploadPictureTicks/(double) TotalTicks/0.01);
1977     printf("PERFORMANCE:     vaBeginPicture     : %d ms (%.2f, %.2f%% percent)\n",
1978            (int) BeginPictureTicks, ((double)  BeginPictureTicks) / (double) PictureCount,
1979            BeginPictureTicks/(double) TotalTicks/0.01);
1980     printf("PERFORMANCE:     vaRenderHeader     : %d ms (%.2f, %.2f%% percent)\n",
1981            (int) RenderPictureTicks, ((double)  RenderPictureTicks) / (double) PictureCount,
1982            RenderPictureTicks/(double) TotalTicks/0.01);
1983     printf("PERFORMANCE:     vaEndPicture       : %d ms (%.2f, %.2f%% percent)\n",
1984            (int) EndPictureTicks, ((double)  EndPictureTicks) / (double) PictureCount,
1985            EndPictureTicks/(double) TotalTicks/0.01);
1986     printf("PERFORMANCE:     vaSyncSurface      : %d ms (%.2f, %.2f%% percent)\n",
1987            (int) SyncPictureTicks, ((double) SyncPictureTicks) / (double) PictureCount,
1988            SyncPictureTicks/(double) TotalTicks/0.01);
1989     printf("PERFORMANCE:     SavePicture        : %d ms (%.2f, %.2f%% percent)\n",
1990            (int) SavePictureTicks, ((double)  SavePictureTicks) / (double) PictureCount,
1991            SavePictureTicks/(double) TotalTicks/0.01);
1992     printf("PERFORMANCE:     Others             : %d ms (%.2f, %.2f%% percent)\n",
1993            (int) others, ((double) others) / (double) PictureCount,
1994            others/(double) TotalTicks/0.01);
1995
1996     if (encode_syncmode == 0)
1997         printf("(Multithread enabled, the profiling is only for reference)\n");
1998         
1999     return 0;
2000 }
2001
2002
2003 int main(int argc,char **argv)
2004 {
2005     unsigned int start;
2006     
2007     process_cmdline(argc, argv);
2008
2009     print_input();
2010     
2011     start = GetTickCount();
2012     
2013     init_va();
2014     setup_encode();
2015     
2016     encode_frames();
2017
2018     release_encode();
2019     deinit_va();
2020
2021     TotalTicks += GetTickCount() - start;
2022     print_performance(frame_count);
2023     
2024     return 0;
2025 }