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