AVCENC: Pass the H264 RefPicList0/1 by using slice_param instead of hacked DPB reference
[platform/upstream/libva.git] / test / encode / avcenc.c
1 /*
2  * Copyright (c) 2012 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 /*
25  * Simple AVC encoder based on libVA.
26  *
27  * Usage:
28  * ./avcenc <width> <height> <input file> <output file> [qp]
29  */  
30
31 #include "sysdeps.h"
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <getopt.h>
36 #include <unistd.h>
37
38 #include <sys/time.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42 #include <assert.h>
43 #include <time.h>
44
45 #include <pthread.h>
46
47 #include <va/va.h>
48 #include <va/va_enc_h264.h>
49 #include "va_display.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 CHECK_VASTATUS(va_status,func)                                  \
74     if (va_status != VA_STATUS_SUCCESS) {                               \
75         fprintf(stderr,"%s:%s (%d) failed,exit\n", __func__, func, __LINE__); \
76         exit(1);                                                        \
77     }
78
79 static VADisplay va_dpy;
80
81 static int picture_width, picture_width_in_mbs;
82 static int picture_height, picture_height_in_mbs;
83 static int frame_size;
84 static unsigned char *newImageBuffer = 0;
85
86 static int qp_value = 26;
87
88 static int intra_period = 30;
89 static int pb_period = 5;
90 static int frame_bit_rate = -1;
91
92 #define MAX_SLICES      32
93
94 static int
95 build_packed_pic_buffer(unsigned char **header_buffer);
96
97 static int
98 build_packed_seq_buffer(unsigned char **header_buffer);
99
100 static int 
101 build_packed_sei_buffer_timing(unsigned int init_cpb_removal_length,
102                                 unsigned int init_cpb_removal_delay,
103                                 unsigned int init_cpb_removal_delay_offset,
104                                 unsigned int cpb_removal_length,
105                                 unsigned int cpb_removal_delay,
106                                 unsigned int dpb_output_length,
107                                 unsigned int dpb_output_delay,
108                                 unsigned char **sei_buffer);
109
110 struct upload_thread_param
111 {
112     FILE *yuv_fp;
113     VASurfaceID surface_id;
114 };
115
116 static void 
117 upload_yuv_to_surface(FILE *yuv_fp, VASurfaceID surface_id);
118
119 static struct {
120     VAProfile profile;
121     int constraint_set_flag;
122     VAEncSequenceParameterBufferH264 seq_param;
123     VAEncPictureParameterBufferH264 pic_param;
124     VAEncSliceParameterBufferH264 slice_param[MAX_SLICES];
125     VAContextID context_id;
126     VAConfigID config_id;
127     VABufferID seq_param_buf_id;                /* Sequence level parameter */
128     VABufferID pic_param_buf_id;                /* Picture level parameter */
129     VABufferID slice_param_buf_id[MAX_SLICES];  /* Slice level parameter, multil slices */
130     VABufferID codedbuf_buf_id;                 /* Output buffer, compressed data */
131     VABufferID packed_seq_header_param_buf_id;
132     VABufferID packed_seq_buf_id;
133     VABufferID packed_pic_header_param_buf_id;
134     VABufferID packed_pic_buf_id;
135     VABufferID packed_sei_header_param_buf_id;   /* the SEI buffer */
136     VABufferID packed_sei_buf_id;
137     VABufferID misc_parameter_hrd_buf_id;
138
139     int num_slices;
140     int codedbuf_i_size;
141     int codedbuf_pb_size;
142     int current_input_surface;
143     int rate_control_method;
144     struct upload_thread_param upload_thread_param;
145     pthread_t upload_thread_id;
146     int upload_thread_value;
147     int i_initial_cpb_removal_delay;
148     int i_initial_cpb_removal_delay_length;
149     int i_cpb_removal_delay;
150     int i_cpb_removal_delay_length;
151     int i_dpb_output_delay_length;
152 } avcenc_context;
153
154 static  VAPictureH264 ReferenceFrames[16], RefPicList0[32], RefPicList1[32];
155
156 static void create_encode_pipe()
157 {
158     VAEntrypoint entrypoints[5];
159     int num_entrypoints,slice_entrypoint;
160     VAConfigAttrib attrib[2];
161     int major_ver, minor_ver;
162     VAStatus va_status;
163
164     va_dpy = va_open_display();
165     va_status = vaInitialize(va_dpy, &major_ver, &minor_ver);
166     CHECK_VASTATUS(va_status, "vaInitialize");
167
168     vaQueryConfigEntrypoints(va_dpy, avcenc_context.profile, entrypoints, 
169                              &num_entrypoints);
170
171     for (slice_entrypoint = 0; slice_entrypoint < num_entrypoints; slice_entrypoint++) {
172         if (entrypoints[slice_entrypoint] == VAEntrypointEncSlice)
173             break;
174     }
175
176     if (slice_entrypoint == num_entrypoints) {
177         /* not find Slice entry point */
178         assert(0);
179     }
180
181     /* find out the format for the render target, and rate control mode */
182     attrib[0].type = VAConfigAttribRTFormat;
183     attrib[1].type = VAConfigAttribRateControl;
184     vaGetConfigAttributes(va_dpy, avcenc_context.profile, VAEntrypointEncSlice,
185                           &attrib[0], 2);
186
187     if ((attrib[0].value & VA_RT_FORMAT_YUV420) == 0) {
188         /* not find desired YUV420 RT format */
189         assert(0);
190     }
191
192     if ((attrib[1].value & avcenc_context.rate_control_method) == 0) {
193         /* Can't find matched RC mode */
194         printf("Can't find the desired RC mode, exit\n");
195         assert(0);
196     }
197
198     attrib[0].value = VA_RT_FORMAT_YUV420; /* set to desired RT format */
199     attrib[1].value = avcenc_context.rate_control_method; /* set to desired RC mode */
200
201     va_status = vaCreateConfig(va_dpy, avcenc_context.profile, VAEntrypointEncSlice,
202                                &attrib[0], 2,&avcenc_context.config_id);
203     CHECK_VASTATUS(va_status, "vaCreateConfig");
204
205     /* Create a context for this decode pipe */
206     va_status = vaCreateContext(va_dpy, avcenc_context.config_id,
207                                 picture_width, picture_height,
208                                 VA_PROGRESSIVE, 
209                                 0, 0,
210                                 &avcenc_context.context_id);
211     CHECK_VASTATUS(va_status, "vaCreateContext");
212 }
213
214 static void destory_encode_pipe()
215 {
216     vaDestroyContext(va_dpy,avcenc_context.context_id);
217     vaDestroyConfig(va_dpy,avcenc_context.config_id);
218     vaTerminate(va_dpy);
219     va_close_display(va_dpy);
220 }
221
222 /***************************************************
223  *
224  *  The encode pipe resource define 
225  *
226  ***************************************************/
227 #define SID_INPUT_PICTURE_0                     0
228 #define SID_INPUT_PICTURE_1                     1
229 #define SID_REFERENCE_PICTURE_L0                2
230 #define SID_REFERENCE_PICTURE_L1                3
231 #define SID_RECON_PICTURE                       4
232 #define SID_NUMBER                              SID_RECON_PICTURE + 1
233 static  VASurfaceID surface_ids[SID_NUMBER];
234
235 static int frame_number;
236 static int enc_frame_number;
237
238 /***************************************************/
239
240 static void *
241 upload_thread_function(void *data)
242 {
243     struct upload_thread_param *param = data;
244
245     upload_yuv_to_surface(param->yuv_fp, param->surface_id);
246
247     return NULL;
248 }
249
250 static void alloc_encode_resource(FILE *yuv_fp)
251 {
252     VAStatus va_status;
253
254     // Create surface
255     va_status = vaCreateSurfaces(
256             va_dpy,
257             VA_RT_FORMAT_YUV420, picture_width, picture_height,
258             &surface_ids[0], SID_NUMBER,
259             NULL, 0
260         );
261     CHECK_VASTATUS(va_status, "vaCreateSurfaces");
262
263     newImageBuffer = (unsigned char *)malloc(frame_size);
264
265     /* firstly upload YUV data to SID_INPUT_PICTURE_1 */
266     avcenc_context.upload_thread_param.yuv_fp = yuv_fp;
267     avcenc_context.upload_thread_param.surface_id = surface_ids[SID_INPUT_PICTURE_1];
268
269     avcenc_context.upload_thread_value = pthread_create(&avcenc_context.upload_thread_id,
270                                                         NULL,
271                                                         upload_thread_function, 
272                                                         (void*)&avcenc_context.upload_thread_param);
273 }
274
275 static void release_encode_resource()
276 {
277     pthread_join(avcenc_context.upload_thread_id, NULL);
278     free(newImageBuffer);
279
280     // Release all the surfaces resource
281     vaDestroySurfaces(va_dpy, &surface_ids[0], SID_NUMBER);     
282 }
283
284 static void avcenc_update_sei_param(int frame_num)
285 {
286         VAEncPackedHeaderParameterBuffer packed_header_param_buffer;
287         unsigned int length_in_bits, offset_in_bytes;
288         unsigned char *packed_sei_buffer = NULL;
289         VAStatus va_status;
290
291         length_in_bits = build_packed_sei_buffer_timing(
292                                 avcenc_context.i_initial_cpb_removal_delay_length,
293                                 avcenc_context.i_initial_cpb_removal_delay,
294                                 0,
295                                 avcenc_context.i_cpb_removal_delay_length,
296                                 avcenc_context.i_cpb_removal_delay * frame_num,
297                                 avcenc_context.i_dpb_output_delay_length,
298                                 0,
299                                 &packed_sei_buffer);
300
301         offset_in_bytes = 0;
302         packed_header_param_buffer.type = VAEncPackedHeaderH264_SEI;
303         packed_header_param_buffer.bit_length = length_in_bits;
304         packed_header_param_buffer.has_emulation_bytes = 0;
305
306         va_status = vaCreateBuffer(va_dpy,
307                                 avcenc_context.context_id,
308                                 VAEncPackedHeaderParameterBufferType,
309                                 sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
310                                 &avcenc_context.packed_sei_header_param_buf_id);
311         CHECK_VASTATUS(va_status,"vaCreateBuffer");
312
313         va_status = vaCreateBuffer(va_dpy,
314                                 avcenc_context.context_id,
315                                 VAEncPackedHeaderDataBufferType,
316                                 (length_in_bits + 7) / 8, 1, packed_sei_buffer,
317                                 &avcenc_context.packed_sei_buf_id);
318         CHECK_VASTATUS(va_status,"vaCreateBuffer");
319         free(packed_sei_buffer);
320         return;
321 }
322
323 static void avcenc_update_picture_parameter(int slice_type, int frame_num, int display_num, int is_idr)
324 {
325     VAEncPictureParameterBufferH264 *pic_param;
326     VAStatus va_status;
327
328     // Picture level
329     pic_param = &avcenc_context.pic_param;
330     pic_param->CurrPic.picture_id = surface_ids[SID_RECON_PICTURE];
331     pic_param->CurrPic.TopFieldOrderCnt = display_num * 2;
332     pic_param->ReferenceFrames[0].picture_id = surface_ids[SID_REFERENCE_PICTURE_L0];
333     pic_param->ReferenceFrames[1].picture_id = surface_ids[SID_REFERENCE_PICTURE_L1];
334     pic_param->ReferenceFrames[2].picture_id = VA_INVALID_ID;
335     assert(avcenc_context.codedbuf_buf_id != VA_INVALID_ID);
336     pic_param->coded_buf = avcenc_context.codedbuf_buf_id;
337     pic_param->frame_num = frame_num;
338     pic_param->pic_fields.bits.idr_pic_flag = !!is_idr;
339     pic_param->pic_fields.bits.reference_pic_flag = (slice_type != SLICE_TYPE_B);
340
341     va_status = vaCreateBuffer(va_dpy,
342                                avcenc_context.context_id,
343                                VAEncPictureParameterBufferType,
344                                sizeof(*pic_param), 1, pic_param,
345                                &avcenc_context.pic_param_buf_id);
346     CHECK_VASTATUS(va_status,"vaCreateBuffer");
347 }
348
349 #ifndef VA_FOURCC_I420
350 #define VA_FOURCC_I420          0x30323449
351 #endif
352
353 static void upload_yuv_to_surface(FILE *yuv_fp, VASurfaceID surface_id)
354 {
355     VAImage surface_image;
356     VAStatus va_status;
357     void *surface_p = NULL;
358     unsigned char *y_src, *u_src, *v_src;
359     unsigned char *y_dst, *u_dst, *v_dst;
360     int y_size = picture_width * picture_height;
361     int u_size = (picture_width >> 1) * (picture_height >> 1);
362     int row, col;
363     size_t n_items;
364
365     do {
366         n_items = fread(newImageBuffer, frame_size, 1, yuv_fp);
367     } while (n_items != 1);
368
369     va_status = vaDeriveImage(va_dpy, surface_id, &surface_image);
370     CHECK_VASTATUS(va_status,"vaDeriveImage");
371
372     vaMapBuffer(va_dpy, surface_image.buf, &surface_p);
373     assert(VA_STATUS_SUCCESS == va_status);
374         
375     y_src = newImageBuffer;
376     u_src = newImageBuffer + y_size; /* UV offset for NV12 */
377     v_src = newImageBuffer + y_size + u_size;
378
379     y_dst = surface_p + surface_image.offsets[0];
380     u_dst = surface_p + surface_image.offsets[1]; /* UV offset for NV12 */
381     v_dst = surface_p + surface_image.offsets[2];
382
383     /* Y plane */
384     for (row = 0; row < surface_image.height; row++) {
385         memcpy(y_dst, y_src, surface_image.width);
386         y_dst += surface_image.pitches[0];
387         y_src += picture_width;
388     }
389
390     if (surface_image.format.fourcc == VA_FOURCC_NV12) { /* UV plane */
391         for (row = 0; row < surface_image.height / 2; row++) {
392             for (col = 0; col < surface_image.width / 2; col++) {
393                 u_dst[col * 2] = u_src[col];
394                 u_dst[col * 2 + 1] = v_src[col];
395             }
396
397             u_dst += surface_image.pitches[1];
398             u_src += (picture_width / 2);
399             v_src += (picture_width / 2);
400         }
401     } else if (surface_image.format.fourcc == VA_FOURCC_YV12 ||
402                surface_image.format.fourcc == VA_FOURCC_I420) {
403         const int U = surface_image.format.fourcc == VA_FOURCC_I420 ? 1 : 2;
404         const int V = surface_image.format.fourcc == VA_FOURCC_I420 ? 2 : 1;
405
406         u_dst = surface_p + surface_image.offsets[U];
407         v_dst = surface_p + surface_image.offsets[V];
408
409         for (row = 0; row < surface_image.height / 2; row++) {
410             memcpy(u_dst, u_src, surface_image.width / 2);
411             memcpy(v_dst, v_src, surface_image.width / 2);
412             u_dst += surface_image.pitches[U];
413             v_dst += surface_image.pitches[V];
414             u_src += (picture_width / 2);
415             v_src += (picture_width / 2);
416         }
417     }
418
419     vaUnmapBuffer(va_dpy, surface_image.buf);
420     vaDestroyImage(va_dpy, surface_image.image_id);
421 }
422
423 static void avcenc_update_slice_parameter(int slice_type)
424 {
425     VAEncSliceParameterBufferH264 *slice_param;
426     VAStatus va_status;
427     int i;
428
429     // Slice level
430     i = 0;
431     slice_param = &avcenc_context.slice_param[i];
432     slice_param->macroblock_address = 0;
433     slice_param->num_macroblocks = picture_height_in_mbs * picture_width_in_mbs; 
434     slice_param->pic_parameter_set_id = 0;
435     slice_param->slice_type = slice_type;
436     slice_param->direct_spatial_mv_pred_flag = 0;
437     slice_param->num_ref_idx_l0_active_minus1 = 0;      /* FIXME: ??? */
438     slice_param->num_ref_idx_l1_active_minus1 = 0;
439     slice_param->cabac_init_idc = 0;
440     slice_param->slice_qp_delta = 0;
441     slice_param->disable_deblocking_filter_idc = 0;
442     slice_param->slice_alpha_c0_offset_div2 = 2;
443     slice_param->slice_beta_offset_div2 = 2;
444     slice_param->idr_pic_id = 0;
445
446     /* FIXME: fill other fields */
447     if ((slice_type == SLICE_TYPE_P) || (slice_type == SLICE_TYPE_B)) {
448         int j;
449         slice_param->RefPicList0[0].picture_id = surface_ids[SID_REFERENCE_PICTURE_L0];
450         for (j = 1; j < 32; j++) {
451             slice_param->RefPicList0[j].picture_id = VA_INVALID_SURFACE;
452             slice_param->RefPicList0[j].flags = VA_PICTURE_H264_INVALID;
453         }
454     }
455
456     if ((slice_type == SLICE_TYPE_B)) {
457         int j;
458         slice_param->RefPicList1[0].picture_id = surface_ids[SID_REFERENCE_PICTURE_L1];
459         for (j = 1; j < 32; j++) {
460             slice_param->RefPicList1[j].picture_id = VA_INVALID_SURFACE;
461             slice_param->RefPicList1[j].flags = VA_PICTURE_H264_INVALID;
462         }
463     }
464
465     va_status = vaCreateBuffer(va_dpy,
466                                avcenc_context.context_id,
467                                VAEncSliceParameterBufferType,
468                                sizeof(*slice_param), 1, slice_param,
469                                &avcenc_context.slice_param_buf_id[i]);
470     CHECK_VASTATUS(va_status,"vaCreateBuffer");;
471     i++;
472
473 #if 0
474     slice_param = &avcenc_context.slice_param[i];
475     slice_param->macroblock_address = picture_height_in_mbs * picture_width_in_mbs / 2;
476     slice_param->num_macroblocks = picture_height_in_mbs * picture_width_in_mbs / 2;
477     slice_param->pic_parameter_set_id = 0;
478     slice_param->slice_type = slice_type;
479     slice_param->direct_spatial_mv_pred_flag = 0;
480     slice_param->num_ref_idx_l0_active_minus1 = 0;      /* FIXME: ??? */
481     slice_param->num_ref_idx_l1_active_minus1 = 0;
482     slice_param->cabac_init_idc = 0;
483     slice_param->slice_qp_delta = 0;
484     slice_param->disable_deblocking_filter_idc = 0;
485     slice_param->slice_alpha_c0_offset_div2 = 2;
486     slice_param->slice_beta_offset_div2 = 2;
487     slice_param->idr_pic_id = 0;
488
489     /* FIXME: fill other fields */
490
491     va_status = vaCreateBuffer(va_dpy,
492                                avcenc_context.context_id,
493                                VAEncSliceParameterBufferType,
494                                sizeof(*slice_param), 1, slice_param,
495                                &avcenc_context.slice_param_buf_id[i]);
496     CHECK_VASTATUS(va_status,"vaCreateBuffer");;
497     i++;
498 #endif
499
500     avcenc_context.num_slices = i;
501 }
502
503 static int begin_picture(FILE *yuv_fp, int frame_num, int display_num, int slice_type, int is_idr)
504 {
505     VAStatus va_status;
506
507     if (avcenc_context.upload_thread_value != 0) {
508         fprintf(stderr, "FATAL error!!!\n");
509         exit(1);
510     }
511     
512     pthread_join(avcenc_context.upload_thread_id, NULL);
513
514     avcenc_context.upload_thread_value = -1;
515
516     if (avcenc_context.current_input_surface == SID_INPUT_PICTURE_0)
517         avcenc_context.current_input_surface = SID_INPUT_PICTURE_1;
518     else
519         avcenc_context.current_input_surface = SID_INPUT_PICTURE_0;
520
521     if (frame_num == 0) {
522         VAEncPackedHeaderParameterBuffer packed_header_param_buffer;
523         unsigned int length_in_bits, offset_in_bytes;
524         unsigned char *packed_seq_buffer = NULL, *packed_pic_buffer = NULL;
525
526         assert(slice_type == SLICE_TYPE_I);
527         length_in_bits = build_packed_seq_buffer(&packed_seq_buffer);
528         offset_in_bytes = 0;
529         packed_header_param_buffer.type = VAEncPackedHeaderSequence;
530         packed_header_param_buffer.bit_length = length_in_bits;
531         packed_header_param_buffer.has_emulation_bytes = 0;
532         va_status = vaCreateBuffer(va_dpy,
533                                    avcenc_context.context_id,
534                                    VAEncPackedHeaderParameterBufferType,
535                                    sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
536                                    &avcenc_context.packed_seq_header_param_buf_id);
537         CHECK_VASTATUS(va_status,"vaCreateBuffer");
538
539         va_status = vaCreateBuffer(va_dpy,
540                                    avcenc_context.context_id,
541                                    VAEncPackedHeaderDataBufferType,
542                                    (length_in_bits + 7) / 8, 1, packed_seq_buffer,
543                                    &avcenc_context.packed_seq_buf_id);
544         CHECK_VASTATUS(va_status,"vaCreateBuffer");
545
546         length_in_bits = build_packed_pic_buffer(&packed_pic_buffer);
547         offset_in_bytes = 0;
548         packed_header_param_buffer.type = VAEncPackedHeaderPicture;
549         packed_header_param_buffer.bit_length = length_in_bits;
550         packed_header_param_buffer.has_emulation_bytes = 0;
551
552         va_status = vaCreateBuffer(va_dpy,
553                                    avcenc_context.context_id,
554                                    VAEncPackedHeaderParameterBufferType,
555                                    sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
556                                    &avcenc_context.packed_pic_header_param_buf_id);
557         CHECK_VASTATUS(va_status,"vaCreateBuffer");
558
559         va_status = vaCreateBuffer(va_dpy,
560                                    avcenc_context.context_id,
561                                    VAEncPackedHeaderDataBufferType,
562                                    (length_in_bits + 7) / 8, 1, packed_pic_buffer,
563                                    &avcenc_context.packed_pic_buf_id);
564         CHECK_VASTATUS(va_status,"vaCreateBuffer");
565
566         free(packed_seq_buffer);
567         free(packed_pic_buffer);
568     }
569
570     /* sequence parameter set */
571     VAEncSequenceParameterBufferH264 *seq_param = &avcenc_context.seq_param;
572     va_status = vaCreateBuffer(va_dpy,
573                                avcenc_context.context_id,
574                                VAEncSequenceParameterBufferType,
575                                sizeof(*seq_param), 1, seq_param,
576                                &avcenc_context.seq_param_buf_id);
577     CHECK_VASTATUS(va_status,"vaCreateBuffer");
578
579
580     /* hrd parameter */
581     VAEncMiscParameterBuffer *misc_param;
582     VAEncMiscParameterHRD *misc_hrd_param;
583     vaCreateBuffer(va_dpy,
584                    avcenc_context.context_id,
585                    VAEncMiscParameterBufferType,
586                    sizeof(VAEncMiscParameterBuffer) + sizeof(VAEncMiscParameterRateControl),
587                    1,
588                    NULL, 
589                    &avcenc_context.misc_parameter_hrd_buf_id);
590     CHECK_VASTATUS(va_status, "vaCreateBuffer");
591
592     vaMapBuffer(va_dpy,
593                 avcenc_context.misc_parameter_hrd_buf_id,
594                 (void **)&misc_param);
595     misc_param->type = VAEncMiscParameterTypeHRD;
596     misc_hrd_param = (VAEncMiscParameterHRD *)misc_param->data;
597
598     if (frame_bit_rate > 0) {
599         misc_hrd_param->initial_buffer_fullness = frame_bit_rate * 1024 * 4;
600         misc_hrd_param->buffer_size = frame_bit_rate * 1024 * 8;
601     } else {
602         misc_hrd_param->initial_buffer_fullness = 0;
603         misc_hrd_param->buffer_size = 0;
604     }
605
606     vaUnmapBuffer(va_dpy, avcenc_context.misc_parameter_hrd_buf_id);
607
608     /* slice parameter */
609     avcenc_update_slice_parameter(slice_type);
610
611     return 0;
612 }
613
614 int avcenc_render_picture()
615 {
616     VAStatus va_status;
617     VABufferID va_buffers[10];
618     unsigned int num_va_buffers = 0;
619     int i;
620
621     va_buffers[num_va_buffers++] = avcenc_context.seq_param_buf_id;
622     va_buffers[num_va_buffers++] = avcenc_context.pic_param_buf_id;
623
624     if (avcenc_context.packed_seq_header_param_buf_id != VA_INVALID_ID)
625         va_buffers[num_va_buffers++] = avcenc_context.packed_seq_header_param_buf_id;
626
627     if (avcenc_context.packed_seq_buf_id != VA_INVALID_ID)
628         va_buffers[num_va_buffers++] = avcenc_context.packed_seq_buf_id;
629
630     if (avcenc_context.packed_pic_header_param_buf_id != VA_INVALID_ID)
631         va_buffers[num_va_buffers++] = avcenc_context.packed_pic_header_param_buf_id;
632
633     if (avcenc_context.packed_pic_buf_id != VA_INVALID_ID)
634         va_buffers[num_va_buffers++] = avcenc_context.packed_pic_buf_id;
635
636     if (avcenc_context.packed_sei_header_param_buf_id != VA_INVALID_ID)
637         va_buffers[num_va_buffers++] = avcenc_context.packed_sei_header_param_buf_id;
638
639     if (avcenc_context.packed_sei_buf_id != VA_INVALID_ID)
640         va_buffers[num_va_buffers++] = avcenc_context.packed_sei_buf_id;
641
642     if (avcenc_context.misc_parameter_hrd_buf_id != VA_INVALID_ID)
643         va_buffers[num_va_buffers++] =  avcenc_context.misc_parameter_hrd_buf_id;
644
645     va_status = vaBeginPicture(va_dpy,
646                                avcenc_context.context_id,
647                                surface_ids[avcenc_context.current_input_surface]);
648     CHECK_VASTATUS(va_status,"vaBeginPicture");
649     
650     va_status = vaRenderPicture(va_dpy,
651                                 avcenc_context.context_id,
652                                 va_buffers,
653                                 num_va_buffers);
654     CHECK_VASTATUS(va_status,"vaRenderPicture");
655     
656     for(i = 0; i < avcenc_context.num_slices; i++) {
657         va_status = vaRenderPicture(va_dpy,
658                                 avcenc_context.context_id,
659                                 &avcenc_context.slice_param_buf_id[i],
660                                 1);
661         CHECK_VASTATUS(va_status,"vaRenderPicture");
662     }
663
664     va_status = vaEndPicture(va_dpy, avcenc_context.context_id);
665     CHECK_VASTATUS(va_status,"vaEndPicture");
666
667     return 0;
668 }
669
670 static int avcenc_destroy_buffers(VABufferID *va_buffers, unsigned int num_va_buffers)
671 {
672     VAStatus va_status;
673     unsigned int i;
674
675     for (i = 0; i < num_va_buffers; i++) {
676         if (va_buffers[i] != VA_INVALID_ID) {
677             va_status = vaDestroyBuffer(va_dpy, va_buffers[i]);
678             CHECK_VASTATUS(va_status,"vaDestroyBuffer");
679             va_buffers[i] = VA_INVALID_ID;
680         }
681     }
682
683     return 0;
684 }
685
686 static void end_picture(int slice_type, int next_is_bpic)
687 {
688     VABufferID tempID;
689
690     /* Prepare for next picture */
691     tempID = surface_ids[SID_RECON_PICTURE];  
692
693     if (slice_type != SLICE_TYPE_B) {
694         if (next_is_bpic) {
695             surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L1]; 
696             surface_ids[SID_REFERENCE_PICTURE_L1] = tempID;     
697         } else {
698             surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L0]; 
699             surface_ids[SID_REFERENCE_PICTURE_L0] = tempID;
700         }
701     } else {
702         if (!next_is_bpic) {
703             surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L0]; 
704             surface_ids[SID_REFERENCE_PICTURE_L0] = surface_ids[SID_REFERENCE_PICTURE_L1];
705             surface_ids[SID_REFERENCE_PICTURE_L1] = tempID;
706         }
707     }
708
709     avcenc_destroy_buffers(&avcenc_context.seq_param_buf_id, 1);
710     avcenc_destroy_buffers(&avcenc_context.pic_param_buf_id, 1);
711     avcenc_destroy_buffers(&avcenc_context.packed_seq_header_param_buf_id, 1);
712     avcenc_destroy_buffers(&avcenc_context.packed_seq_buf_id, 1);
713     avcenc_destroy_buffers(&avcenc_context.packed_pic_header_param_buf_id, 1);
714     avcenc_destroy_buffers(&avcenc_context.packed_pic_buf_id, 1);
715     avcenc_destroy_buffers(&avcenc_context.packed_sei_header_param_buf_id, 1);
716     avcenc_destroy_buffers(&avcenc_context.packed_sei_buf_id, 1);
717     avcenc_destroy_buffers(&avcenc_context.slice_param_buf_id[0], avcenc_context.num_slices);
718     avcenc_destroy_buffers(&avcenc_context.codedbuf_buf_id, 1);
719     avcenc_destroy_buffers(&avcenc_context.misc_parameter_hrd_buf_id, 1);
720
721     memset(avcenc_context.slice_param, 0, sizeof(avcenc_context.slice_param));
722     avcenc_context.num_slices = 0;
723 }
724
725 #define BITSTREAM_ALLOCATE_STEPPING     4096
726
727 struct __bitstream {
728     unsigned int *buffer;
729     int bit_offset;
730     int max_size_in_dword;
731 };
732
733 typedef struct __bitstream bitstream;
734
735 #if 0
736 static int 
737 get_coded_bitsteam_length(unsigned char *buffer, int buffer_length)
738 {
739     int i;
740
741     for (i = 0; i < buffer_length - 3; i++) {
742         if (!buffer[i] &&
743             !buffer[i + 1] &&
744             !buffer[i + 2] &&
745             !buffer[i + 3])
746             break;
747     }
748
749     return i;
750 }
751 #endif
752
753 static unsigned int 
754 va_swap32(unsigned int val)
755 {
756     unsigned char *pval = (unsigned char *)&val;
757
758     return ((pval[0] << 24)     |
759             (pval[1] << 16)     |
760             (pval[2] << 8)      |
761             (pval[3] << 0));
762 }
763
764 static void
765 bitstream_start(bitstream *bs)
766 {
767     bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
768     bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
769     bs->bit_offset = 0;
770 }
771
772 static void
773 bitstream_end(bitstream *bs)
774 {
775     int pos = (bs->bit_offset >> 5);
776     int bit_offset = (bs->bit_offset & 0x1f);
777     int bit_left = 32 - bit_offset;
778
779     if (bit_offset) {
780         bs->buffer[pos] = va_swap32((bs->buffer[pos] << bit_left));
781     }
782 }
783  
784 static void
785 bitstream_put_ui(bitstream *bs, unsigned int val, int size_in_bits)
786 {
787     int pos = (bs->bit_offset >> 5);
788     int bit_offset = (bs->bit_offset & 0x1f);
789     int bit_left = 32 - bit_offset;
790
791     if (!size_in_bits)
792         return;
793
794     bs->bit_offset += size_in_bits;
795
796     if (bit_left > size_in_bits) {
797         bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
798     } else {
799         size_in_bits -= bit_left;
800         bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
801         bs->buffer[pos] = va_swap32(bs->buffer[pos]);
802
803         if (pos + 1 == bs->max_size_in_dword) {
804             bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
805             bs->buffer = realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
806         }
807
808         bs->buffer[pos + 1] = val;
809     }
810 }
811
812 static void
813 bitstream_put_ue(bitstream *bs, unsigned int val)
814 {
815     int size_in_bits = 0;
816     int tmp_val = ++val;
817
818     while (tmp_val) {
819         tmp_val >>= 1;
820         size_in_bits++;
821     }
822
823     bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
824     bitstream_put_ui(bs, val, size_in_bits);
825 }
826
827 static void
828 bitstream_put_se(bitstream *bs, int val)
829 {
830     unsigned int new_val;
831
832     if (val <= 0)
833         new_val = -2 * val;
834     else
835         new_val = 2 * val - 1;
836
837     bitstream_put_ue(bs, new_val);
838 }
839
840 static void
841 bitstream_byte_aligning(bitstream *bs, int bit)
842 {
843     int bit_offset = (bs->bit_offset & 0x7);
844     int bit_left = 8 - bit_offset;
845     int new_val;
846
847     if (!bit_offset)
848         return;
849
850     assert(bit == 0 || bit == 1);
851
852     if (bit)
853         new_val = (1 << bit_left) - 1;
854     else
855         new_val = 0;
856
857     bitstream_put_ui(bs, new_val, bit_left);
858 }
859
860 static void 
861 rbsp_trailing_bits(bitstream *bs)
862 {
863     bitstream_put_ui(bs, 1, 1);
864     bitstream_byte_aligning(bs, 0);
865 }
866
867 static void nal_start_code_prefix(bitstream *bs)
868 {
869     bitstream_put_ui(bs, 0x00000001, 32);
870 }
871
872 static void nal_header(bitstream *bs, int nal_ref_idc, int nal_unit_type)
873 {
874     bitstream_put_ui(bs, 0, 1);                /* forbidden_zero_bit: 0 */
875     bitstream_put_ui(bs, nal_ref_idc, 2);
876     bitstream_put_ui(bs, nal_unit_type, 5);
877 }
878
879 static void sps_rbsp(bitstream *bs)
880 {
881     VAEncSequenceParameterBufferH264 *seq_param = &avcenc_context.seq_param;
882     int profile_idc = PROFILE_IDC_BASELINE;
883
884     if (avcenc_context.profile == VAProfileH264High)
885         profile_idc = PROFILE_IDC_HIGH;
886     else if (avcenc_context.profile == VAProfileH264Main)
887         profile_idc = PROFILE_IDC_MAIN;
888
889     bitstream_put_ui(bs, profile_idc, 8);               /* profile_idc */
890     bitstream_put_ui(bs, !!(avcenc_context.constraint_set_flag & 1), 1);                         /* constraint_set0_flag */
891     bitstream_put_ui(bs, !!(avcenc_context.constraint_set_flag & 2), 1);                         /* constraint_set1_flag */
892     bitstream_put_ui(bs, !!(avcenc_context.constraint_set_flag & 4), 1);                         /* constraint_set2_flag */
893     bitstream_put_ui(bs, !!(avcenc_context.constraint_set_flag & 8), 1);                         /* constraint_set3_flag */
894     bitstream_put_ui(bs, 0, 4);                         /* reserved_zero_4bits */
895     bitstream_put_ui(bs, seq_param->level_idc, 8);      /* level_idc */
896     bitstream_put_ue(bs, seq_param->seq_parameter_set_id);      /* seq_parameter_set_id */
897
898     if ( profile_idc == PROFILE_IDC_HIGH) {
899         bitstream_put_ue(bs, 1);        /* chroma_format_idc = 1, 4:2:0 */ 
900         bitstream_put_ue(bs, 0);        /* bit_depth_luma_minus8 */
901         bitstream_put_ue(bs, 0);        /* bit_depth_chroma_minus8 */
902         bitstream_put_ui(bs, 0, 1);     /* qpprime_y_zero_transform_bypass_flag */
903         bitstream_put_ui(bs, 0, 1);     /* seq_scaling_matrix_present_flag */
904     }
905
906     bitstream_put_ue(bs, seq_param->seq_fields.bits.log2_max_frame_num_minus4); /* log2_max_frame_num_minus4 */
907     bitstream_put_ue(bs, seq_param->seq_fields.bits.pic_order_cnt_type);        /* pic_order_cnt_type */
908
909     if (seq_param->seq_fields.bits.pic_order_cnt_type == 0)
910         bitstream_put_ue(bs, seq_param->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4);     /* log2_max_pic_order_cnt_lsb_minus4 */
911     else {
912         assert(0);
913     }
914
915     bitstream_put_ue(bs, seq_param->max_num_ref_frames);        /* num_ref_frames */
916     bitstream_put_ui(bs, 0, 1);                                 /* gaps_in_frame_num_value_allowed_flag */
917
918     bitstream_put_ue(bs, seq_param->picture_width_in_mbs - 1);  /* pic_width_in_mbs_minus1 */
919     bitstream_put_ue(bs, seq_param->picture_height_in_mbs - 1); /* pic_height_in_map_units_minus1 */
920     bitstream_put_ui(bs, seq_param->seq_fields.bits.frame_mbs_only_flag, 1);    /* frame_mbs_only_flag */
921
922     if (!seq_param->seq_fields.bits.frame_mbs_only_flag) {
923         assert(0);
924     }
925
926     bitstream_put_ui(bs, seq_param->seq_fields.bits.direct_8x8_inference_flag, 1);      /* direct_8x8_inference_flag */
927     bitstream_put_ui(bs, seq_param->frame_cropping_flag, 1);            /* frame_cropping_flag */
928
929     if (seq_param->frame_cropping_flag) {
930         bitstream_put_ue(bs, seq_param->frame_crop_left_offset);        /* frame_crop_left_offset */
931         bitstream_put_ue(bs, seq_param->frame_crop_right_offset);       /* frame_crop_right_offset */
932         bitstream_put_ue(bs, seq_param->frame_crop_top_offset);         /* frame_crop_top_offset */
933         bitstream_put_ue(bs, seq_param->frame_crop_bottom_offset);      /* frame_crop_bottom_offset */
934     }
935     
936     if ( frame_bit_rate < 0 ) {
937         bitstream_put_ui(bs, 0, 1); /* vui_parameters_present_flag */
938     } else {
939         bitstream_put_ui(bs, 1, 1); /* vui_parameters_present_flag */
940         bitstream_put_ui(bs, 0, 1); /* aspect_ratio_info_present_flag */
941         bitstream_put_ui(bs, 0, 1); /* overscan_info_present_flag */
942         bitstream_put_ui(bs, 0, 1); /* video_signal_type_present_flag */
943         bitstream_put_ui(bs, 0, 1); /* chroma_loc_info_present_flag */
944         bitstream_put_ui(bs, 1, 1); /* timing_info_present_flag */
945         {
946             bitstream_put_ui(bs, 15, 32);
947             bitstream_put_ui(bs, 900, 32);
948             bitstream_put_ui(bs, 1, 1);
949         }
950         bitstream_put_ui(bs, 1, 1); /* nal_hrd_parameters_present_flag */
951         {
952             // hrd_parameters 
953             bitstream_put_ue(bs, 0);    /* cpb_cnt_minus1 */
954             bitstream_put_ui(bs, 4, 4); /* bit_rate_scale */
955             bitstream_put_ui(bs, 6, 4); /* cpb_size_scale */
956            
957             bitstream_put_ue(bs, frame_bit_rate - 1); /* bit_rate_value_minus1[0] */
958             bitstream_put_ue(bs, frame_bit_rate*8 - 1); /* cpb_size_value_minus1[0] */
959             bitstream_put_ui(bs, 1, 1);  /* cbr_flag[0] */
960
961             bitstream_put_ui(bs, 23, 5);   /* initial_cpb_removal_delay_length_minus1 */
962             bitstream_put_ui(bs, 23, 5);   /* cpb_removal_delay_length_minus1 */
963             bitstream_put_ui(bs, 23, 5);   /* dpb_output_delay_length_minus1 */
964             bitstream_put_ui(bs, 23, 5);   /* time_offset_length  */
965         }
966         bitstream_put_ui(bs, 0, 1);   /* vcl_hrd_parameters_present_flag */
967         bitstream_put_ui(bs, 0, 1);   /* low_delay_hrd_flag */ 
968
969         bitstream_put_ui(bs, 0, 1); /* pic_struct_present_flag */
970         bitstream_put_ui(bs, 0, 1); /* bitstream_restriction_flag */
971     }
972
973     rbsp_trailing_bits(bs);     /* rbsp_trailing_bits */
974 }
975
976 #if 0
977 static void build_nal_sps(FILE *avc_fp)
978 {
979     bitstream bs;
980
981     bitstream_start(&bs);
982     nal_start_code_prefix(&bs);
983     nal_header(&bs, NAL_REF_IDC_HIGH, NAL_SPS);
984     sps_rbsp(&bs);
985     bitstream_end(&bs, avc_fp);
986 }
987 #endif
988
989 static void pps_rbsp(bitstream *bs)
990 {
991     VAEncPictureParameterBufferH264 *pic_param = &avcenc_context.pic_param;
992
993     bitstream_put_ue(bs, pic_param->pic_parameter_set_id);      /* pic_parameter_set_id */
994     bitstream_put_ue(bs, pic_param->seq_parameter_set_id);      /* seq_parameter_set_id */
995
996     bitstream_put_ui(bs, pic_param->pic_fields.bits.entropy_coding_mode_flag, 1);  /* entropy_coding_mode_flag */
997
998     bitstream_put_ui(bs, 0, 1);                         /* pic_order_present_flag: 0 */
999
1000     bitstream_put_ue(bs, 0);                            /* num_slice_groups_minus1 */
1001
1002     bitstream_put_ue(bs, pic_param->num_ref_idx_l0_active_minus1);      /* num_ref_idx_l0_active_minus1 */
1003     bitstream_put_ue(bs, pic_param->num_ref_idx_l1_active_minus1);      /* num_ref_idx_l1_active_minus1 1 */
1004
1005     bitstream_put_ui(bs, pic_param->pic_fields.bits.weighted_pred_flag, 1);     /* weighted_pred_flag: 0 */
1006     bitstream_put_ui(bs, pic_param->pic_fields.bits.weighted_bipred_idc, 2);    /* weighted_bipred_idc: 0 */
1007
1008     bitstream_put_se(bs, pic_param->pic_init_qp - 26);  /* pic_init_qp_minus26 */
1009     bitstream_put_se(bs, 0);                            /* pic_init_qs_minus26 */
1010     bitstream_put_se(bs, 0);                            /* chroma_qp_index_offset */
1011
1012     bitstream_put_ui(bs, pic_param->pic_fields.bits.deblocking_filter_control_present_flag, 1); /* deblocking_filter_control_present_flag */
1013     bitstream_put_ui(bs, 0, 1);                         /* constrained_intra_pred_flag */
1014     bitstream_put_ui(bs, 0, 1);                         /* redundant_pic_cnt_present_flag */
1015     
1016     /* more_rbsp_data */
1017     bitstream_put_ui(bs, pic_param->pic_fields.bits.transform_8x8_mode_flag, 1);    /*transform_8x8_mode_flag */
1018     bitstream_put_ui(bs, 0, 1);                         /* pic_scaling_matrix_present_flag */
1019     bitstream_put_se(bs, pic_param->second_chroma_qp_index_offset );    /*second_chroma_qp_index_offset */
1020
1021     rbsp_trailing_bits(bs);
1022 }
1023
1024 #if 0
1025 static void build_nal_pps(FILE *avc_fp)
1026 {
1027     bitstream bs;
1028
1029     bitstream_start(&bs);
1030     nal_start_code_prefix(&bs);
1031     nal_header(&bs, NAL_REF_IDC_HIGH, NAL_PPS);
1032     pps_rbsp(&bs);
1033     bitstream_end(&bs, avc_fp);
1034 }
1035
1036 static void 
1037 build_header(FILE *avc_fp)
1038 {
1039     build_nal_sps(avc_fp);
1040     build_nal_pps(avc_fp);
1041 }
1042 #endif
1043
1044 static int
1045 build_packed_pic_buffer(unsigned char **header_buffer)
1046 {
1047     bitstream bs;
1048
1049     bitstream_start(&bs);
1050     nal_start_code_prefix(&bs);
1051     nal_header(&bs, NAL_REF_IDC_HIGH, NAL_PPS);
1052     pps_rbsp(&bs);
1053     bitstream_end(&bs);
1054
1055     *header_buffer = (unsigned char *)bs.buffer;
1056     return bs.bit_offset;
1057 }
1058
1059 static int
1060 build_packed_seq_buffer(unsigned char **header_buffer)
1061 {
1062     bitstream bs;
1063
1064     bitstream_start(&bs);
1065     nal_start_code_prefix(&bs);
1066     nal_header(&bs, NAL_REF_IDC_HIGH, NAL_SPS);
1067     sps_rbsp(&bs);
1068     bitstream_end(&bs);
1069
1070     *header_buffer = (unsigned char *)bs.buffer;
1071     return bs.bit_offset;
1072 }
1073
1074 static int 
1075 build_packed_sei_buffer_timing(unsigned int init_cpb_removal_length,
1076                                 unsigned int init_cpb_removal_delay,
1077                                 unsigned int init_cpb_removal_delay_offset,
1078                                 unsigned int cpb_removal_length,
1079                                 unsigned int cpb_removal_delay,
1080                                 unsigned int dpb_output_length,
1081                                 unsigned int dpb_output_delay,
1082                                 unsigned char **sei_buffer)
1083 {
1084     unsigned char *byte_buf;
1085     int bp_byte_size, i, pic_byte_size;
1086
1087     bitstream nal_bs;
1088     bitstream sei_bp_bs, sei_pic_bs;
1089
1090     bitstream_start(&sei_bp_bs);
1091     bitstream_put_ue(&sei_bp_bs, 0);       /*seq_parameter_set_id*/
1092     bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay, cpb_removal_length); 
1093     bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay_offset, cpb_removal_length); 
1094     if ( sei_bp_bs.bit_offset & 0x7) {
1095         bitstream_put_ui(&sei_bp_bs, 1, 1);
1096     }
1097     bitstream_end(&sei_bp_bs);
1098     bp_byte_size = (sei_bp_bs.bit_offset + 7) / 8;
1099     
1100     bitstream_start(&sei_pic_bs);
1101     bitstream_put_ui(&sei_pic_bs, cpb_removal_delay, cpb_removal_length); 
1102     bitstream_put_ui(&sei_pic_bs, dpb_output_delay, dpb_output_length); 
1103     if ( sei_pic_bs.bit_offset & 0x7) {
1104         bitstream_put_ui(&sei_pic_bs, 1, 1);
1105     }
1106     bitstream_end(&sei_pic_bs);
1107     pic_byte_size = (sei_pic_bs.bit_offset + 7) / 8;
1108     
1109     bitstream_start(&nal_bs);
1110     nal_start_code_prefix(&nal_bs);
1111     nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
1112
1113         /* Write the SEI buffer period data */    
1114     bitstream_put_ui(&nal_bs, 0, 8);
1115     bitstream_put_ui(&nal_bs, bp_byte_size, 8);
1116     
1117     byte_buf = (unsigned char *)sei_bp_bs.buffer;
1118     for(i = 0; i < bp_byte_size; i++) {
1119         bitstream_put_ui(&nal_bs, byte_buf[i], 8);
1120     }
1121     free(byte_buf);
1122         /* write the SEI timing data */
1123     bitstream_put_ui(&nal_bs, 0x01, 8);
1124     bitstream_put_ui(&nal_bs, pic_byte_size, 8);
1125     
1126     byte_buf = (unsigned char *)sei_pic_bs.buffer;
1127     for(i = 0; i < pic_byte_size; i++) {
1128         bitstream_put_ui(&nal_bs, byte_buf[i], 8);
1129     }
1130     free(byte_buf);
1131
1132     rbsp_trailing_bits(&nal_bs);
1133     bitstream_end(&nal_bs);
1134
1135     *sei_buffer = (unsigned char *)nal_bs.buffer; 
1136    
1137     return nal_bs.bit_offset;
1138 }
1139
1140 #if 0
1141 static void 
1142 slice_header(bitstream *bs, int frame_num, int display_frame, int slice_type, int nal_ref_idc, int is_idr)
1143 {
1144     VAEncSequenceParameterBufferH264 *seq_param = &avcenc_context.seq_param;
1145     VAEncPictureParameterBufferH264 *pic_param = &avcenc_context.pic_param;
1146     int is_cabac = (pic_param->pic_fields.bits.entropy_coding_mode_flag == ENTROPY_MODE_CABAC);
1147
1148     bitstream_put_ue(bs, 0);                   /* first_mb_in_slice: 0 */
1149     bitstream_put_ue(bs, slice_type);          /* slice_type */
1150     bitstream_put_ue(bs, 0);                   /* pic_parameter_set_id: 0 */
1151     bitstream_put_ui(bs, frame_num & 0x0F, seq_param->seq_fields.bits.log2_max_frame_num_minus4 + 4);    /* frame_num */
1152
1153     /* frame_mbs_only_flag == 1 */
1154     if (!seq_param->seq_fields.bits.frame_mbs_only_flag) {
1155         /* FIXME: */
1156         assert(0);
1157     }
1158
1159     if (is_idr)
1160         bitstream_put_ue(bs, 0);                /* idr_pic_id: 0 */
1161
1162     if (seq_param->seq_fields.bits.pic_order_cnt_type == 0) {
1163         bitstream_put_ui(bs, (display_frame*2) & 0x3F, seq_param->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 + 4);
1164         /* only support frame */
1165     } else {
1166         /* FIXME: */
1167         assert(0);
1168     }
1169
1170     /* redundant_pic_cnt_present_flag == 0 */
1171     
1172     /* slice type */
1173     if (slice_type == SLICE_TYPE_P) {
1174         bitstream_put_ui(bs, 0, 1);            /* num_ref_idx_active_override_flag: 0 */
1175         /* ref_pic_list_reordering */
1176         bitstream_put_ui(bs, 0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
1177     } else if (slice_type == SLICE_TYPE_B) {
1178         bitstream_put_ui(bs, 1, 1);            /* direct_spatial_mv_pred: 1 */
1179         bitstream_put_ui(bs, 0, 1);            /* num_ref_idx_active_override_flag: 0 */
1180         /* ref_pic_list_reordering */
1181         bitstream_put_ui(bs, 0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
1182         bitstream_put_ui(bs, 0, 1);            /* ref_pic_list_reordering_flag_l1: 0 */
1183     } 
1184
1185     /* weighted_pred_flag == 0 */
1186
1187     /* dec_ref_pic_marking */
1188     if (nal_ref_idc != 0) {
1189         if ( is_idr) {
1190             bitstream_put_ui(bs, 0, 1);            /* no_output_of_prior_pics_flag: 0 */
1191             bitstream_put_ui(bs, 0, 1);            /* long_term_reference_flag: 0 */
1192         } else {
1193             bitstream_put_ui(bs, 0, 1);            /* adaptive_ref_pic_marking_mode_flag: 0 */
1194         }
1195     }
1196
1197     if (is_cabac && (slice_type != SLICE_TYPE_I))
1198         bitstream_put_ue(bs, 0);               /* cabac_init_idc: 0 */
1199
1200     bitstream_put_se(bs, 0);                   /* slice_qp_delta: 0 */
1201
1202     if (pic_param->pic_fields.bits.deblocking_filter_control_present_flag == 1) {
1203         bitstream_put_ue(bs, 0);               /* disable_deblocking_filter_idc: 0 */
1204         bitstream_put_se(bs, 2);               /* slice_alpha_c0_offset_div2: 2 */
1205         bitstream_put_se(bs, 2);               /* slice_beta_offset_div2: 2 */
1206     }
1207 }
1208
1209 static void 
1210 slice_data(bitstream *bs)
1211 {
1212     VACodedBufferSegment *coded_buffer_segment;
1213     unsigned char *coded_mem;
1214     int i, slice_data_length;
1215     VAStatus va_status;
1216     VASurfaceStatus surface_status;
1217
1218     va_status = vaSyncSurface(va_dpy, surface_ids[avcenc_context.current_input_surface]);
1219     CHECK_VASTATUS(va_status,"vaSyncSurface");
1220
1221     surface_status = 0;
1222     va_status = vaQuerySurfaceStatus(va_dpy, surface_ids[avcenc_context.current_input_surface], &surface_status);
1223     CHECK_VASTATUS(va_status,"vaQuerySurfaceStatus");
1224
1225     va_status = vaMapBuffer(va_dpy, avcenc_context.codedbuf_buf_id, (void **)(&coded_buffer_segment));
1226     CHECK_VASTATUS(va_status,"vaMapBuffer");
1227     coded_mem = coded_buffer_segment->buf;
1228
1229     slice_data_length = get_coded_bitsteam_length(coded_mem, codedbuf_size);
1230
1231     for (i = 0; i < slice_data_length; i++) {
1232         bitstream_put_ui(bs, *coded_mem, 8);
1233         coded_mem++;
1234     }
1235
1236     vaUnmapBuffer(va_dpy, avcenc_context.codedbuf_buf_id);
1237 }
1238
1239 static void 
1240 build_nal_slice(FILE *avc_fp, int frame_num, int display_frame, int slice_type, int is_idr)
1241 {
1242     bitstream bs;
1243
1244     bitstream_start(&bs);
1245     slice_data(&bs);
1246     bitstream_end(&bs, avc_fp);
1247 }
1248
1249 #endif
1250
1251 static int
1252 store_coded_buffer(FILE *avc_fp, int slice_type)
1253 {
1254     VACodedBufferSegment *coded_buffer_segment;
1255     unsigned char *coded_mem;
1256     int slice_data_length;
1257     VAStatus va_status;
1258     VASurfaceStatus surface_status;
1259     size_t w_items;
1260
1261     va_status = vaSyncSurface(va_dpy, surface_ids[avcenc_context.current_input_surface]);
1262     CHECK_VASTATUS(va_status,"vaSyncSurface");
1263
1264     surface_status = 0;
1265     va_status = vaQuerySurfaceStatus(va_dpy, surface_ids[avcenc_context.current_input_surface], &surface_status);
1266     CHECK_VASTATUS(va_status,"vaQuerySurfaceStatus");
1267
1268     va_status = vaMapBuffer(va_dpy, avcenc_context.codedbuf_buf_id, (void **)(&coded_buffer_segment));
1269     CHECK_VASTATUS(va_status,"vaMapBuffer");
1270     coded_mem = coded_buffer_segment->buf;
1271
1272     if (coded_buffer_segment->status & VA_CODED_BUF_STATUS_SLICE_OVERFLOW_MASK) {
1273         if (slice_type == SLICE_TYPE_I)
1274             avcenc_context.codedbuf_i_size *= 2;
1275         else
1276             avcenc_context.codedbuf_pb_size *= 2;
1277
1278         vaUnmapBuffer(va_dpy, avcenc_context.codedbuf_buf_id);
1279         return -1;
1280     }
1281
1282     slice_data_length = coded_buffer_segment->size;
1283
1284     do {
1285         w_items = fwrite(coded_mem, slice_data_length, 1, avc_fp);
1286     } while (w_items != 1);
1287
1288     if (slice_type == SLICE_TYPE_I) {
1289         if (avcenc_context.codedbuf_i_size > slice_data_length * 3 / 2) {
1290             avcenc_context.codedbuf_i_size = slice_data_length * 3 / 2;
1291         }
1292         
1293         if (avcenc_context.codedbuf_pb_size < slice_data_length) {
1294             avcenc_context.codedbuf_pb_size = slice_data_length;
1295         }
1296     } else {
1297         if (avcenc_context.codedbuf_pb_size > slice_data_length * 3 / 2) {
1298             avcenc_context.codedbuf_pb_size = slice_data_length * 3 / 2;
1299         }
1300     }
1301
1302     vaUnmapBuffer(va_dpy, avcenc_context.codedbuf_buf_id);
1303
1304     return 0;
1305 }
1306
1307 static void
1308 encode_picture(FILE *yuv_fp, FILE *avc_fp,
1309                int frame_num, int display_num,
1310                int is_idr,
1311                int slice_type, int next_is_bpic,
1312                int next_display_num)
1313 {
1314     VAStatus va_status;
1315     int ret = 0, codedbuf_size;
1316     
1317     begin_picture(yuv_fp, frame_num, display_num, slice_type, is_idr);
1318
1319     //if (next_display_num < frame_number) {
1320     if (1) {
1321         int index;
1322
1323         /* prepare for next frame */
1324         if (avcenc_context.current_input_surface == SID_INPUT_PICTURE_0)
1325             index = SID_INPUT_PICTURE_1;
1326         else
1327             index = SID_INPUT_PICTURE_0;
1328         if ( next_display_num >= frame_number )
1329             next_display_num = frame_number - 1;
1330         fseek(yuv_fp, frame_size * next_display_num, SEEK_SET);
1331
1332         avcenc_context.upload_thread_param.yuv_fp = yuv_fp;
1333         avcenc_context.upload_thread_param.surface_id = surface_ids[index];
1334
1335         avcenc_context.upload_thread_value = pthread_create(&avcenc_context.upload_thread_id,
1336                                                             NULL,
1337                                                             upload_thread_function, 
1338                                                             (void*)&avcenc_context.upload_thread_param);
1339     }
1340
1341     do {
1342         avcenc_destroy_buffers(&avcenc_context.codedbuf_buf_id, 1);
1343         avcenc_destroy_buffers(&avcenc_context.pic_param_buf_id, 1);
1344
1345
1346         if (SLICE_TYPE_I == slice_type) {
1347             codedbuf_size = avcenc_context.codedbuf_i_size;
1348         } else {
1349             codedbuf_size = avcenc_context.codedbuf_pb_size;
1350         }
1351
1352         /* coded buffer */
1353         va_status = vaCreateBuffer(va_dpy,
1354                                    avcenc_context.context_id,
1355                                    VAEncCodedBufferType,
1356                                    codedbuf_size, 1, NULL,
1357                                    &avcenc_context.codedbuf_buf_id);
1358         CHECK_VASTATUS(va_status,"vaCreateBuffer");
1359
1360         /* picture parameter set */
1361         avcenc_update_picture_parameter(slice_type, frame_num, display_num, is_idr);
1362
1363         if (avcenc_context.rate_control_method == VA_RC_CBR)
1364                 avcenc_update_sei_param(frame_num);
1365
1366         avcenc_render_picture();
1367
1368         ret = store_coded_buffer(avc_fp, slice_type);
1369     } while (ret);
1370
1371     end_picture(slice_type, next_is_bpic);
1372 }
1373
1374 static void encode_pb_pictures(FILE *yuv_fp, FILE *avc_fp, int f, int nbframes, int next_f)
1375 {
1376     int i;
1377     encode_picture(yuv_fp, avc_fp,
1378                    enc_frame_number, f + nbframes,
1379                    0,
1380                    SLICE_TYPE_P, 1, f);
1381
1382     for( i = 0; i < nbframes - 1; i++) {
1383         encode_picture(yuv_fp, avc_fp,
1384                        enc_frame_number + 1, f + i,
1385                        0,
1386                        SLICE_TYPE_B, 1, f + i + 1);
1387     }
1388     
1389     encode_picture(yuv_fp, avc_fp,
1390                    enc_frame_number + 1, f + nbframes - 1,
1391                    0,
1392                    SLICE_TYPE_B, 0, next_f);
1393 }
1394
1395 static void show_help()
1396 {
1397     printf("Usage: avnenc <width> <height> <input_yuvfile> <output_avcfile> [qp=qpvalue|fb=framebitrate] [mode=0(I frames only)/1(I and P frames)/2(I, P and B frames)\n");
1398 }
1399
1400 static void avcenc_context_seq_param_init(VAEncSequenceParameterBufferH264 *seq_param,
1401                                           int width, int height)
1402
1403 {
1404     int width_in_mbs = (width + 15) / 16;
1405     int height_in_mbs = (height + 15) / 16;
1406     int frame_cropping_flag = 0;
1407     int frame_crop_bottom_offset = 0;
1408
1409     seq_param->seq_parameter_set_id = 0;
1410     seq_param->level_idc = 41;
1411     seq_param->intra_period = intra_period;
1412     seq_param->ip_period = 0;   /* FIXME: ??? */
1413     seq_param->max_num_ref_frames = 4;
1414     seq_param->picture_width_in_mbs = width_in_mbs;
1415     seq_param->picture_height_in_mbs = height_in_mbs;
1416     seq_param->seq_fields.bits.frame_mbs_only_flag = 1;
1417     
1418     if (frame_bit_rate > 0)
1419         seq_param->bits_per_second = 1024 * frame_bit_rate; /* use kbps as input */
1420     else
1421         seq_param->bits_per_second = 0;
1422     
1423     seq_param->time_scale = 900;
1424     seq_param->num_units_in_tick = 15;                  /* Tc = num_units_in_tick / time_sacle */
1425
1426     if (height_in_mbs * 16 - height) {
1427         frame_cropping_flag = 1;
1428         frame_crop_bottom_offset = 
1429             (height_in_mbs * 16 - height) / (2 * (!seq_param->seq_fields.bits.frame_mbs_only_flag + 1));
1430     }
1431
1432     seq_param->frame_cropping_flag = frame_cropping_flag;
1433     seq_param->frame_crop_left_offset = 0;
1434     seq_param->frame_crop_right_offset = 0;
1435     seq_param->frame_crop_top_offset = 0;
1436     seq_param->frame_crop_bottom_offset = frame_crop_bottom_offset;
1437
1438     seq_param->seq_fields.bits.pic_order_cnt_type = 0;
1439     seq_param->seq_fields.bits.direct_8x8_inference_flag = 0;
1440     
1441     seq_param->seq_fields.bits.log2_max_frame_num_minus4 = 0;
1442     seq_param->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 = 2;
1443         
1444     if (frame_bit_rate > 0)
1445         seq_param->vui_parameters_present_flag = 1;     //HRD info located in vui
1446     else
1447         seq_param->vui_parameters_present_flag = 0;
1448 }
1449
1450 static void avcenc_context_pic_param_init(VAEncPictureParameterBufferH264 *pic_param)
1451 {
1452     pic_param->seq_parameter_set_id = 0;
1453     pic_param->pic_parameter_set_id = 0;
1454
1455     pic_param->last_picture = 0;
1456     pic_param->frame_num = 0;
1457     
1458     pic_param->pic_init_qp = (qp_value >= 0 ?  qp_value : 26);
1459     pic_param->num_ref_idx_l0_active_minus1 = 0;
1460     pic_param->num_ref_idx_l1_active_minus1 = 0;
1461
1462     pic_param->pic_fields.bits.idr_pic_flag = 0;
1463     pic_param->pic_fields.bits.reference_pic_flag = 0;
1464     pic_param->pic_fields.bits.entropy_coding_mode_flag = ENTROPY_MODE_CABAC;
1465     pic_param->pic_fields.bits.weighted_pred_flag = 0;
1466     pic_param->pic_fields.bits.weighted_bipred_idc = 0;
1467     
1468     if (avcenc_context.constraint_set_flag & 0x7)
1469         pic_param->pic_fields.bits.transform_8x8_mode_flag = 0;
1470     else
1471         pic_param->pic_fields.bits.transform_8x8_mode_flag = 1;
1472
1473     pic_param->pic_fields.bits.deblocking_filter_control_present_flag = 1;
1474 }
1475
1476 static void avcenc_context_sei_init()
1477 {
1478         int init_cpb_size;
1479         int target_bit_rate;
1480
1481         /* it comes for the bps defined in SPS */
1482         target_bit_rate = avcenc_context.seq_param.bits_per_second;
1483         init_cpb_size = (target_bit_rate * 8) >> 10;
1484         avcenc_context.i_initial_cpb_removal_delay = init_cpb_size * 0.5 * 1024 / target_bit_rate * 90000;
1485
1486         avcenc_context.i_cpb_removal_delay = 2;
1487         avcenc_context.i_initial_cpb_removal_delay_length = 24;
1488         avcenc_context.i_cpb_removal_delay_length = 24;
1489         avcenc_context.i_dpb_output_delay_length = 24;
1490 }
1491
1492 static void avcenc_context_init(int width, int height)
1493 {
1494     int i;
1495     memset(&avcenc_context, 0, sizeof(avcenc_context));
1496     avcenc_context.profile = VAProfileH264Main;
1497
1498     switch (avcenc_context.profile) {
1499     case VAProfileH264Baseline:
1500         avcenc_context.constraint_set_flag |= (1 << 0); /* Annex A.2.1 */
1501         break;
1502
1503     case VAProfileH264Main:
1504         avcenc_context.constraint_set_flag |= (1 << 1); /* Annex A.2.2 */
1505         break;
1506
1507     case VAProfileH264High:
1508         avcenc_context.constraint_set_flag |= (1 << 3); /* Annex A.2.4 */
1509         break;
1510         
1511     default:
1512         break;
1513     }
1514         
1515     avcenc_context.seq_param_buf_id = VA_INVALID_ID;
1516     avcenc_context.pic_param_buf_id = VA_INVALID_ID;
1517     avcenc_context.packed_seq_header_param_buf_id = VA_INVALID_ID;
1518     avcenc_context.packed_seq_buf_id = VA_INVALID_ID;
1519     avcenc_context.packed_pic_header_param_buf_id = VA_INVALID_ID;
1520     avcenc_context.packed_pic_buf_id = VA_INVALID_ID;
1521     avcenc_context.codedbuf_buf_id = VA_INVALID_ID;
1522     avcenc_context.misc_parameter_hrd_buf_id = VA_INVALID_ID;
1523     avcenc_context.codedbuf_i_size = width * height;
1524     avcenc_context.codedbuf_pb_size = 0;
1525     avcenc_context.current_input_surface = SID_INPUT_PICTURE_0;
1526     avcenc_context.upload_thread_value = -1;
1527     avcenc_context.packed_sei_header_param_buf_id = VA_INVALID_ID;
1528     avcenc_context.packed_sei_buf_id = VA_INVALID_ID;
1529
1530     if (qp_value == -1)
1531         avcenc_context.rate_control_method = VA_RC_CBR;
1532     else if (qp_value == -2)
1533         avcenc_context.rate_control_method = VA_RC_VBR;
1534     else {
1535         assert(qp_value >= 0 && qp_value <= 51);
1536         avcenc_context.rate_control_method = VA_RC_CQP;
1537     }
1538
1539     for (i = 0; i < MAX_SLICES; i++) {
1540         avcenc_context.slice_param_buf_id[i] = VA_INVALID_ID;
1541     }
1542
1543     avcenc_context_seq_param_init(&avcenc_context.seq_param, width, height);
1544     avcenc_context_pic_param_init(&avcenc_context.pic_param);
1545     if (avcenc_context.rate_control_method == VA_RC_CBR)
1546         avcenc_context_sei_init();
1547 }
1548
1549 int main(int argc, char *argv[])
1550 {
1551     int f;
1552     FILE *yuv_fp;
1553     FILE *avc_fp;
1554     long file_size;
1555     int i_frame_only=0,i_p_frame_only=1;
1556     int mode_value;
1557     struct timeval tpstart,tpend; 
1558     float  timeuse;
1559
1560     va_init_display_args(&argc, argv);
1561
1562     //TODO may be we should using option analytics library
1563     if(argc != 5 && argc != 6 && argc != 7) {
1564         show_help();
1565         return -1;
1566     }
1567
1568     picture_width = atoi(argv[1]);
1569     picture_height = atoi(argv[2]);
1570     picture_width_in_mbs = (picture_width + 15) / 16;
1571     picture_height_in_mbs = (picture_height + 15) / 16;
1572
1573     if (argc == 6 || argc == 7) {
1574         qp_value = -1;
1575         sscanf(argv[5], "qp=%d", &qp_value);
1576         if ( qp_value == -1 ) {
1577             frame_bit_rate = -1;
1578             sscanf(argv[5], "fb=%d", &frame_bit_rate);
1579             if (  frame_bit_rate == -1 ) {
1580                 show_help();
1581                 return -1;
1582             }
1583         } else if (qp_value > 51) {
1584             qp_value = 51;
1585         } else if (qp_value < 0) {
1586             qp_value = 0;
1587         }
1588     } else
1589         qp_value = 28;                          //default const QP mode
1590
1591     if (argc == 7) {
1592         sscanf(argv[6], "mode=%d", &mode_value);
1593         if ( mode_value == 0 ) {
1594                 i_frame_only = 1;
1595                 i_p_frame_only = 0;
1596         }
1597         else if ( mode_value == 1) {
1598                 i_frame_only = 0;
1599                 i_p_frame_only = 1;
1600         }
1601         else if ( mode_value == 2 ) {
1602                 i_frame_only = 0;
1603                 i_p_frame_only = 0;
1604         }
1605         else {
1606                 printf("mode_value=%d\n",mode_value);
1607                 show_help();
1608                 return -1;
1609         }
1610     }
1611
1612     yuv_fp = fopen(argv[3],"rb");
1613     if ( yuv_fp == NULL){
1614         printf("Can't open input YUV file\n");
1615         return -1;
1616     }
1617     fseek(yuv_fp,0l, SEEK_END);
1618     file_size = ftell(yuv_fp);
1619     frame_size = picture_width * picture_height +  ((picture_width * picture_height) >> 1) ;
1620
1621     if ( (file_size < frame_size) || (file_size % frame_size) ) {
1622         fclose(yuv_fp);
1623         printf("The YUV file's size is not correct\n");
1624         return -1;
1625     }
1626     frame_number = file_size / frame_size;
1627     fseek(yuv_fp, 0l, SEEK_SET);
1628
1629     avc_fp = fopen(argv[4], "wb");      
1630     if ( avc_fp == NULL) {
1631         fclose(yuv_fp);
1632         printf("Can't open output avc file\n");
1633         return -1;
1634     }   
1635     gettimeofday(&tpstart,NULL);        
1636     avcenc_context_init(picture_width, picture_height);
1637     create_encode_pipe();
1638     alloc_encode_resource(yuv_fp);
1639
1640     enc_frame_number = 0;
1641     for ( f = 0; f < frame_number; ) {          //picture level loop
1642         static int const frame_type_pattern[][2] = { {SLICE_TYPE_I,1}, 
1643                                                      {SLICE_TYPE_P,3}, {SLICE_TYPE_P,3},{SLICE_TYPE_P,3},
1644                                                      {SLICE_TYPE_P,3}, {SLICE_TYPE_P,3},{SLICE_TYPE_P,3},
1645                                                      {SLICE_TYPE_P,3}, {SLICE_TYPE_P,3},{SLICE_TYPE_P,3},
1646                                                      {SLICE_TYPE_P,2} };
1647
1648         if ( i_frame_only ) {
1649             encode_picture(yuv_fp, avc_fp,enc_frame_number, f, f==0, SLICE_TYPE_I, 0, f+1);
1650             f++;
1651             enc_frame_number++;
1652         } else if ( i_p_frame_only ) {
1653             if ( (f % intra_period) == 0 ) {
1654                 encode_picture(yuv_fp, avc_fp,enc_frame_number, f, f==0, SLICE_TYPE_I, 0, f+1);
1655                 f++;
1656                 enc_frame_number++;
1657             } else {
1658                 encode_picture(yuv_fp, avc_fp,enc_frame_number, f, f==0, SLICE_TYPE_P, 0, f+1);
1659                 f++;
1660                 enc_frame_number++;
1661             }
1662         } else { // follow the i,p,b pattern
1663             static int fcurrent = 0;
1664             int fnext;
1665             
1666             fcurrent = fcurrent % (sizeof(frame_type_pattern)/sizeof(int[2]));
1667             fnext = (fcurrent+1) % (sizeof(frame_type_pattern)/sizeof(int[2]));
1668             
1669             if ( frame_type_pattern[fcurrent][0] == SLICE_TYPE_I ) {
1670                 encode_picture(yuv_fp, avc_fp,enc_frame_number, f, f==0, SLICE_TYPE_I, 0, 
1671                         f+frame_type_pattern[fnext][1]);
1672                 f++;
1673                 enc_frame_number++;
1674             } else {
1675                 encode_pb_pictures(yuv_fp, avc_fp, f, frame_type_pattern[fcurrent][1]-1, 
1676                         f + frame_type_pattern[fcurrent][1] + frame_type_pattern[fnext][1] -1 );
1677                 f += frame_type_pattern[fcurrent][1];
1678                 enc_frame_number++;
1679             }
1680  
1681             fcurrent++;
1682         }
1683         printf("\r %d/%d ...", f+1, frame_number);
1684         fflush(stdout);
1685     }
1686
1687     gettimeofday(&tpend,NULL);
1688     timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+ tpend.tv_usec-tpstart.tv_usec;
1689     timeuse/=1000000;
1690     printf("\ndone!\n");
1691     printf("encode %d frames in %f secondes, FPS is %.1f\n",frame_number, timeuse, frame_number/timeuse);
1692     release_encode_resource();
1693     destory_encode_pipe();
1694
1695     fclose(yuv_fp);
1696     fclose(avc_fp);
1697
1698     return 0;
1699 }