Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  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 FRAME_IDR 7
  67.  
  68. #define ENTROPY_MODE_CAVLC      0
  69. #define ENTROPY_MODE_CABAC      1
  70.  
  71. #define PROFILE_IDC_BASELINE    66
  72. #define PROFILE_IDC_MAIN        77
  73. #define PROFILE_IDC_HIGH        100
  74.  
  75. #define CHECK_VASTATUS(va_status,func)                                  \
  76.     if (va_status != VA_STATUS_SUCCESS) {                               \
  77.         fprintf(stderr,"%s:%s (%d) failed,exit\n", __func__, func, __LINE__); \
  78.         exit(1);                                                        \
  79.     }
  80.  
  81. static VADisplay va_dpy;
  82.  
  83. static int picture_width, picture_width_in_mbs;
  84. static int picture_height, picture_height_in_mbs;
  85. static int frame_size;
  86. static unsigned char *newImageBuffer = 0;
  87.  
  88. static int qp_value = 26;
  89.  
  90. static int intra_period = 30;
  91. static int frame_bit_rate = -1;
  92. static int frame_rate = 30;
  93. static int ip_period = 1;
  94.  
  95. #define MAX_SLICES      32
  96.  
  97.  
  98. static  unsigned int MaxFrameNum = (1<<12);
  99. static  unsigned int Log2MaxFrameNum = 12;
  100. static  unsigned int Log2MaxPicOrderCntLsb = 8;
  101.  
  102. static int
  103. build_packed_pic_buffer(unsigned char **header_buffer);
  104.  
  105. static int
  106. build_packed_seq_buffer(unsigned char **header_buffer);
  107.  
  108. static int
  109. build_packed_sei_pic_timing(unsigned int cpb_removal_length,
  110.                                 unsigned int dpb_output_length,
  111.                                 unsigned char **sei_buffer);
  112.  
  113. static int
  114. build_packed_idr_sei_buffer_timing(unsigned int init_cpb_removal_delay_length,
  115.                                 unsigned int cpb_removal_length,
  116.                                 unsigned int dpb_output_length,
  117.                                 unsigned char **sei_buffer);
  118.  
  119. struct upload_thread_param
  120. {
  121.     FILE *yuv_fp;
  122.     VASurfaceID surface_id;
  123. };
  124.  
  125. static void
  126. upload_yuv_to_surface(FILE *yuv_fp, VASurfaceID surface_id);
  127.  
  128. static struct {
  129.     VAProfile profile;
  130.     int constraint_set_flag;
  131.     VAEncSequenceParameterBufferH264 seq_param;
  132.     VAEncPictureParameterBufferH264 pic_param;
  133.     VAEncSliceParameterBufferH264 slice_param[MAX_SLICES];
  134.     VAContextID context_id;
  135.     VAConfigID config_id;
  136.     VABufferID seq_param_buf_id;                /* Sequence level parameter */
  137.     VABufferID pic_param_buf_id;                /* Picture level parameter */
  138.     VABufferID slice_param_buf_id[MAX_SLICES];  /* Slice level parameter, multil slices */
  139.     VABufferID codedbuf_buf_id;                 /* Output buffer, compressed data */
  140.     VABufferID packed_seq_header_param_buf_id;
  141.     VABufferID packed_seq_buf_id;
  142.     VABufferID packed_pic_header_param_buf_id;
  143.     VABufferID packed_pic_buf_id;
  144.     VABufferID packed_sei_header_param_buf_id;   /* the SEI buffer */
  145.     VABufferID packed_sei_buf_id;
  146.     VABufferID misc_parameter_hrd_buf_id;
  147.  
  148.     int num_slices;
  149.     int codedbuf_i_size;
  150.     int codedbuf_pb_size;
  151.     int current_input_surface;
  152.     int rate_control_method;
  153.     struct upload_thread_param upload_thread_param;
  154.     pthread_t upload_thread_id;
  155.     int upload_thread_value;
  156.     int i_initial_cpb_removal_delay;
  157.     int i_initial_cpb_removal_delay_offset;
  158.     int i_initial_cpb_removal_delay_length;
  159.     int i_cpb_removal_delay;
  160.     int i_cpb_removal_delay_length;
  161.     int i_dpb_output_delay_length;
  162.     int time_offset_length;
  163.  
  164.     unsigned long long idr_frame_num;
  165.     unsigned long long prev_idr_cpb_removal;
  166.     unsigned long long current_idr_cpb_removal;
  167.     unsigned long long current_cpb_removal;
  168.     /* This is relative to the current_cpb_removal */
  169.     unsigned int current_dpb_removal_delta;
  170. } avcenc_context;
  171.  
  172. static  VAPictureH264 ReferenceFrames[16], RefPicList0[32], RefPicList1[32];
  173.  
  174. static void create_encode_pipe()
  175. {
  176.     VAEntrypoint entrypoints[5];
  177.     int num_entrypoints,slice_entrypoint;
  178.     VAConfigAttrib attrib[2];
  179.     int major_ver, minor_ver;
  180.     VAStatus va_status;
  181.  
  182.     va_dpy = va_open_display();
  183.     va_status = vaInitialize(va_dpy, &major_ver, &minor_ver);
  184.     CHECK_VASTATUS(va_status, "vaInitialize");
  185.  
  186.     vaQueryConfigEntrypoints(va_dpy, avcenc_context.profile, entrypoints,
  187.                              &num_entrypoints);
  188.  
  189.     for (slice_entrypoint = 0; slice_entrypoint < num_entrypoints; slice_entrypoint++) {
  190.         if (entrypoints[slice_entrypoint] == VAEntrypointEncSlice)
  191.             break;
  192.     }
  193.  
  194.     if (slice_entrypoint == num_entrypoints) {
  195.         /* not find Slice entry point */
  196.         assert(0);
  197.     }
  198.  
  199.     /* find out the format for the render target, and rate control mode */
  200.     attrib[0].type = VAConfigAttribRTFormat;
  201.     attrib[1].type = VAConfigAttribRateControl;
  202.     vaGetConfigAttributes(va_dpy, avcenc_context.profile, VAEntrypointEncSlice,
  203.                           &attrib[0], 2);
  204.  
  205.     if ((attrib[0].value & VA_RT_FORMAT_YUV420) == 0) {
  206.         /* not find desired YUV420 RT format */
  207.         assert(0);
  208.     }
  209.  
  210.     if ((attrib[1].value & avcenc_context.rate_control_method) == 0) {
  211.         /* Can't find matched RC mode */
  212.         printf("Can't find the desired RC mode, exit\n");
  213.         assert(0);
  214.     }
  215.  
  216.     attrib[0].value = VA_RT_FORMAT_YUV420; /* set to desired RT format */
  217.     attrib[1].value = avcenc_context.rate_control_method; /* set to desired RC mode */
  218.  
  219.     va_status = vaCreateConfig(va_dpy, avcenc_context.profile, VAEntrypointEncSlice,
  220.                                &attrib[0], 2,&avcenc_context.config_id);
  221.     CHECK_VASTATUS(va_status, "vaCreateConfig");
  222.  
  223.     /* Create a context for this decode pipe */
  224.     va_status = vaCreateContext(va_dpy, avcenc_context.config_id,
  225.                                 picture_width, picture_height,
  226.                                 VA_PROGRESSIVE,
  227.                                 0, 0,
  228.                                 &avcenc_context.context_id);
  229.     CHECK_VASTATUS(va_status, "vaCreateContext");
  230. }
  231.  
  232. static void destory_encode_pipe()
  233. {
  234.     vaDestroyContext(va_dpy,avcenc_context.context_id);
  235.     vaDestroyConfig(va_dpy,avcenc_context.config_id);
  236.     vaTerminate(va_dpy);
  237.     va_close_display(va_dpy);
  238. }
  239.  
  240. /***************************************************
  241.  *
  242.  *  The encode pipe resource define
  243.  *
  244.  ***************************************************/
  245. #define SID_INPUT_PICTURE_0                     0
  246. #define SID_INPUT_PICTURE_1                     1
  247. #define SID_REFERENCE_PICTURE_L0                2
  248. #define SID_REFERENCE_PICTURE_L1                3
  249. #define SID_RECON_PICTURE                       4
  250. #define SID_NUMBER                              SID_RECON_PICTURE + 1
  251.  
  252. #define SURFACE_NUM 16 /* 16 surfaces for reference */
  253.  
  254. static  VASurfaceID surface_ids[SID_NUMBER];
  255. static  VASurfaceID ref_surface[SURFACE_NUM];
  256. static  int use_slot[SURFACE_NUM];
  257.  
  258. static  unsigned long long current_frame_display = 0;
  259. static  unsigned long long current_IDR_display = 0;
  260.  
  261. static  VAPictureH264 CurrentCurrPic;
  262.  
  263. #define current_slot (current_frame_display % SURFACE_NUM)
  264.  
  265. static int frame_number;
  266. static unsigned long long enc_frame_number;
  267. static int current_frame_type;
  268. static int current_frame_num;
  269. static unsigned int current_poc;
  270.  
  271. static  unsigned int num_ref_frames = 2;
  272. static  unsigned int numShortTerm = 0;
  273. /***************************************************/
  274.  
  275. static int get_free_slot()
  276. {
  277.     int i, index = -1;
  278.  
  279.     for (i = 0; i < SURFACE_NUM; i++) {
  280.         if (use_slot[i] == 0) {
  281.             index = i;
  282.             break;
  283.         }
  284.     }
  285.     if (index < 0) {
  286.         printf("WARNING: No free slot to store the reconstructed frame \n");
  287.         index = SURFACE_NUM - 1;
  288.     }
  289.     return index;
  290. }
  291.  
  292. static void *
  293. upload_thread_function(void *data)
  294. {
  295.     struct upload_thread_param *param = data;
  296.  
  297.     upload_yuv_to_surface(param->yuv_fp, param->surface_id);
  298.  
  299.     return NULL;
  300. }
  301.  
  302. static void alloc_encode_resource(FILE *yuv_fp)
  303. {
  304.     VAStatus va_status;
  305.  
  306.     // Create surface
  307.     va_status = vaCreateSurfaces(
  308.         va_dpy,
  309.         VA_RT_FORMAT_YUV420, picture_width, picture_height,
  310.         surface_ids, SID_NUMBER,
  311.         NULL, 0
  312.     );
  313.  
  314.     CHECK_VASTATUS(va_status, "vaCreateSurfaces");
  315.  
  316.     // Create surface
  317.     va_status = vaCreateSurfaces(
  318.         va_dpy,
  319.         VA_RT_FORMAT_YUV420, picture_width, picture_height,
  320.         ref_surface, SURFACE_NUM,
  321.         NULL, 0
  322.     );
  323.  
  324.     CHECK_VASTATUS(va_status, "vaCreateSurfaces");
  325.  
  326.  
  327.     newImageBuffer = (unsigned char *)malloc(frame_size);
  328.  
  329.     /* firstly upload YUV data to SID_INPUT_PICTURE_1 */
  330.     avcenc_context.upload_thread_param.yuv_fp = yuv_fp;
  331.     avcenc_context.upload_thread_param.surface_id = surface_ids[SID_INPUT_PICTURE_1];
  332.  
  333.     avcenc_context.upload_thread_value = pthread_create(&avcenc_context.upload_thread_id,
  334.                                                         NULL,
  335.                                                         upload_thread_function,
  336.                                                         (void*)&avcenc_context.upload_thread_param);
  337. }
  338.  
  339. static void release_encode_resource()
  340. {
  341.     pthread_join(avcenc_context.upload_thread_id, NULL);
  342.     free(newImageBuffer);
  343.  
  344.     // Release all the surfaces resource
  345.     vaDestroySurfaces(va_dpy, surface_ids, SID_NUMBER);
  346.     // Release all the reference surfaces
  347.     vaDestroySurfaces(va_dpy, ref_surface, SURFACE_NUM);
  348. }
  349.  
  350. static void avcenc_update_sei_param(int is_idr)
  351. {
  352.         VAEncPackedHeaderParameterBuffer packed_header_param_buffer;
  353.         unsigned int length_in_bits;
  354.         unsigned char *packed_sei_buffer = NULL;
  355.         VAStatus va_status;
  356.  
  357.         if (is_idr)
  358.             length_in_bits = build_packed_idr_sei_buffer_timing(
  359.                                 avcenc_context.i_initial_cpb_removal_delay_length,
  360.                                 avcenc_context.i_cpb_removal_delay_length,
  361.                                 avcenc_context.i_dpb_output_delay_length,
  362.                                 &packed_sei_buffer);
  363.        else
  364.             length_in_bits = build_packed_sei_pic_timing(
  365.                                 avcenc_context.i_cpb_removal_delay_length,
  366.                                 avcenc_context.i_dpb_output_delay_length,
  367.                                 &packed_sei_buffer);
  368.  
  369.         packed_header_param_buffer.type = VAEncPackedHeaderH264_SEI;
  370.         packed_header_param_buffer.bit_length = length_in_bits;
  371.         packed_header_param_buffer.has_emulation_bytes = 0;
  372.  
  373.         va_status = vaCreateBuffer(va_dpy,
  374.                                 avcenc_context.context_id,
  375.                                 VAEncPackedHeaderParameterBufferType,
  376.                                 sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
  377.                                 &avcenc_context.packed_sei_header_param_buf_id);
  378.         CHECK_VASTATUS(va_status,"vaCreateBuffer");
  379.  
  380.         va_status = vaCreateBuffer(va_dpy,
  381.                                 avcenc_context.context_id,
  382.                                 VAEncPackedHeaderDataBufferType,
  383.                                 (length_in_bits + 7) / 8, 1, packed_sei_buffer,
  384.                                 &avcenc_context.packed_sei_buf_id);
  385.         CHECK_VASTATUS(va_status,"vaCreateBuffer");
  386.         free(packed_sei_buffer);
  387.         return;
  388. }
  389.  
  390. #define partition(ref, field, key, ascending)   \
  391.     while (i <= j) {                            \
  392.         if (ascending) {                        \
  393.             while (ref[i].field < key)          \
  394.                 i++;                            \
  395.             while (ref[j].field > key)          \
  396.                 j--;                            \
  397.         } else {                                \
  398.             while (ref[i].field > key)          \
  399.                 i++;                            \
  400.             while (ref[j].field < key)          \
  401.                 j--;                            \
  402.         }                                       \
  403.         if (i <= j) {                           \
  404.             tmp = ref[i];                       \
  405.             ref[i] = ref[j];                    \
  406.             ref[j] = tmp;                       \
  407.             i++;                                \
  408.             j--;                                \
  409.         }                                       \
  410.     }                                           \
  411.  
  412. static void sort_one(VAPictureH264 ref[], int left, int right,
  413.                      int ascending, int frame_idx)
  414. {
  415.     int i = left, j = right;
  416.     unsigned int key;
  417.     VAPictureH264 tmp;
  418.  
  419.     if (frame_idx) {
  420.         key = ref[(left + right) / 2].frame_idx;
  421.         partition(ref, frame_idx, key, ascending);
  422.     } else {
  423.         key = ref[(left + right) / 2].TopFieldOrderCnt;
  424.         partition(ref, TopFieldOrderCnt, (signed int)key, ascending);
  425.     }
  426.  
  427.     /* recursion */
  428.     if (left < j)
  429.         sort_one(ref, left, j, ascending, frame_idx);
  430.  
  431.     if (i < right)
  432.         sort_one(ref, i, right, ascending, frame_idx);
  433. }
  434.  
  435. static void sort_two(VAPictureH264 ref[], int left, int right, unsigned int key, unsigned int frame_idx,
  436.                      int partition_ascending, int list0_ascending, int list1_ascending)
  437. {
  438.     int i = left, j = right;
  439.     VAPictureH264 tmp;
  440.  
  441.     if (frame_idx) {
  442.         partition(ref, frame_idx, key, partition_ascending);
  443.     } else {
  444.         partition(ref, TopFieldOrderCnt, (signed int)key, partition_ascending);
  445.     }
  446.  
  447.     sort_one(ref, left, i-1, list0_ascending, frame_idx);
  448.     sort_one(ref, j+1, right, list1_ascending, frame_idx);
  449. }
  450.  
  451. static int update_RefPicList()
  452. {
  453.  
  454.     if (current_frame_type == SLICE_TYPE_P) {
  455.         memcpy(RefPicList0, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
  456.         sort_one(RefPicList0, 0, numShortTerm-1, 0, 1);
  457.     }
  458.  
  459.     if (current_frame_type == SLICE_TYPE_B) {
  460.         memcpy(RefPicList0, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
  461.         sort_two(RefPicList0, 0, numShortTerm-1, current_poc, 0,
  462.                  1, 0, 1);
  463.  
  464.         memcpy(RefPicList1, ReferenceFrames, numShortTerm * sizeof(VAPictureH264));
  465.         sort_two(RefPicList1, 0, numShortTerm-1, current_poc, 0,
  466.                  0, 1, 0);
  467.     }
  468.  
  469.     return 0;
  470. }
  471.  
  472. static void avcenc_update_picture_parameter(int slice_type, int is_idr)
  473. {
  474.     VAEncPictureParameterBufferH264 *pic_param;
  475.     VAStatus va_status;
  476.     int recon_index;
  477.  
  478.     recon_index = get_free_slot();
  479.     // Picture level
  480.     pic_param = &avcenc_context.pic_param;
  481.  
  482.     pic_param->CurrPic.picture_id = ref_surface[recon_index];
  483.     pic_param->CurrPic.frame_idx = current_frame_num;
  484.     pic_param->CurrPic.flags = 0;
  485.  
  486.     pic_param->CurrPic.TopFieldOrderCnt = current_poc;
  487.     pic_param->CurrPic.BottomFieldOrderCnt = pic_param->CurrPic.TopFieldOrderCnt;
  488.  
  489.     assert(avcenc_context.codedbuf_buf_id != VA_INVALID_ID);
  490.     pic_param->coded_buf = avcenc_context.codedbuf_buf_id;
  491.     pic_param->frame_num = current_frame_num;
  492.     pic_param->pic_fields.bits.idr_pic_flag = !!is_idr;
  493.     pic_param->pic_fields.bits.reference_pic_flag = (slice_type != SLICE_TYPE_B);
  494.     CurrentCurrPic = pic_param->CurrPic;
  495.  
  496.     if (slice_type == SLICE_TYPE_P || slice_type == SLICE_TYPE_B)
  497.         memset(pic_param->ReferenceFrames, 0xff, 16 * sizeof(VAPictureH264)); /* invalid all */
  498.  
  499.     if ((slice_type == SLICE_TYPE_P) || (slice_type == SLICE_TYPE_B)) {
  500.         pic_param->ReferenceFrames[0] = RefPicList0[0];
  501.     }
  502.     if (slice_type == SLICE_TYPE_B) {
  503.         pic_param->ReferenceFrames[1] = RefPicList1[0];
  504.     }
  505.  
  506.     va_status = vaCreateBuffer(va_dpy,
  507.                                avcenc_context.context_id,
  508.                                VAEncPictureParameterBufferType,
  509.                                sizeof(*pic_param), 1, pic_param,
  510.                                &avcenc_context.pic_param_buf_id);
  511.     CHECK_VASTATUS(va_status,"vaCreateBuffer");
  512.  
  513. }
  514.  
  515. #ifndef VA_FOURCC_I420
  516. #define VA_FOURCC_I420          0x30323449
  517. #endif
  518.  
  519. static void upload_yuv_to_surface(FILE *yuv_fp, VASurfaceID surface_id)
  520. {
  521.     VAImage surface_image;
  522.     VAStatus va_status;
  523.     void *surface_p = NULL;
  524.     unsigned char *y_src, *u_src, *v_src;
  525.     unsigned char *y_dst, *u_dst, *v_dst;
  526.     int y_size = picture_width * picture_height;
  527.     int u_size = (picture_width >> 1) * (picture_height >> 1);
  528.     int row, col;
  529.     size_t n_items;
  530.  
  531.     do {
  532.         n_items = fread(newImageBuffer, frame_size, 1, yuv_fp);
  533.     } while (n_items != 1);
  534.  
  535.     va_status = vaDeriveImage(va_dpy, surface_id, &surface_image);
  536.     CHECK_VASTATUS(va_status,"vaDeriveImage");
  537.  
  538.     vaMapBuffer(va_dpy, surface_image.buf, &surface_p);
  539.     assert(VA_STATUS_SUCCESS == va_status);
  540.        
  541.     y_src = newImageBuffer;
  542.     u_src = newImageBuffer + y_size; /* UV offset for NV12 */
  543.     v_src = newImageBuffer + y_size + u_size;
  544.  
  545.     y_dst = surface_p + surface_image.offsets[0];
  546.     u_dst = surface_p + surface_image.offsets[1]; /* UV offset for NV12 */
  547.     v_dst = surface_p + surface_image.offsets[2];
  548.  
  549.     /* Y plane */
  550.     for (row = 0; row < surface_image.height; row++) {
  551.         memcpy(y_dst, y_src, surface_image.width);
  552.         y_dst += surface_image.pitches[0];
  553.         y_src += picture_width;
  554.     }
  555.  
  556.     if (surface_image.format.fourcc == VA_FOURCC_NV12) { /* UV plane */
  557.         for (row = 0; row < surface_image.height / 2; row++) {
  558.             for (col = 0; col < surface_image.width / 2; col++) {
  559.                 u_dst[col * 2] = u_src[col];
  560.                 u_dst[col * 2 + 1] = v_src[col];
  561.             }
  562.  
  563.             u_dst += surface_image.pitches[1];
  564.             u_src += (picture_width / 2);
  565.             v_src += (picture_width / 2);
  566.         }
  567.     } else if (surface_image.format.fourcc == VA_FOURCC_YV12 ||
  568.                surface_image.format.fourcc == VA_FOURCC_I420) {
  569.         const int U = surface_image.format.fourcc == VA_FOURCC_I420 ? 1 : 2;
  570.         const int V = surface_image.format.fourcc == VA_FOURCC_I420 ? 2 : 1;
  571.  
  572.         u_dst = surface_p + surface_image.offsets[U];
  573.         v_dst = surface_p + surface_image.offsets[V];
  574.  
  575.         for (row = 0; row < surface_image.height / 2; row++) {
  576.             memcpy(u_dst, u_src, surface_image.width / 2);
  577.             memcpy(v_dst, v_src, surface_image.width / 2);
  578.             u_dst += surface_image.pitches[U];
  579.             v_dst += surface_image.pitches[V];
  580.             u_src += (picture_width / 2);
  581.             v_src += (picture_width / 2);
  582.         }
  583.     }
  584.  
  585.     vaUnmapBuffer(va_dpy, surface_image.buf);
  586.     vaDestroyImage(va_dpy, surface_image.image_id);
  587. }
  588.  
  589. static void avcenc_update_slice_parameter(int slice_type)
  590. {
  591.     VAEncSliceParameterBufferH264 *slice_param;
  592.     VAStatus va_status;
  593.     int i;
  594.  
  595.     // Slice level
  596.     i = 0;
  597.     slice_param = &avcenc_context.slice_param[i];
  598.     slice_param->macroblock_address = 0;
  599.     slice_param->num_macroblocks = picture_height_in_mbs * picture_width_in_mbs;
  600.     slice_param->pic_parameter_set_id = 0;
  601.     slice_param->slice_type = slice_type;
  602.     slice_param->direct_spatial_mv_pred_flag = 0;
  603.     slice_param->num_ref_idx_l0_active_minus1 = 0;      /* FIXME: ??? */
  604.     slice_param->num_ref_idx_l1_active_minus1 = 0;
  605.     slice_param->cabac_init_idc = 0;
  606.     slice_param->slice_qp_delta = 0;
  607.     slice_param->disable_deblocking_filter_idc = 0;
  608.     slice_param->slice_alpha_c0_offset_div2 = 2;
  609.     slice_param->slice_beta_offset_div2 = 2;
  610.     slice_param->idr_pic_id = 0;
  611.  
  612.     /* FIXME: fill other fields */
  613.     if ((slice_type == SLICE_TYPE_P) || (slice_type == SLICE_TYPE_B)) {
  614.         memset(slice_param->RefPicList0, 0xFF, 32 * sizeof(VAPictureH264));
  615.         slice_param->RefPicList0[0] = RefPicList0[0];
  616.     }
  617.  
  618.     if ((slice_type == SLICE_TYPE_B)) {
  619.         memset(slice_param->RefPicList1, 0xFF, 32 * sizeof(VAPictureH264));
  620.         slice_param->RefPicList1[0] = RefPicList1[0];
  621.     }
  622.  
  623.     va_status = vaCreateBuffer(va_dpy,
  624.                                avcenc_context.context_id,
  625.                                VAEncSliceParameterBufferType,
  626.                                sizeof(*slice_param), 1, slice_param,
  627.                                &avcenc_context.slice_param_buf_id[i]);
  628.     CHECK_VASTATUS(va_status,"vaCreateBuffer");;
  629.     i++;
  630.  
  631. #if 0
  632.     slice_param = &avcenc_context.slice_param[i];
  633.     slice_param->macroblock_address = picture_height_in_mbs * picture_width_in_mbs / 2;
  634.     slice_param->num_macroblocks = picture_height_in_mbs * picture_width_in_mbs / 2;
  635.     slice_param->pic_parameter_set_id = 0;
  636.     slice_param->slice_type = slice_type;
  637.     slice_param->direct_spatial_mv_pred_flag = 0;
  638.     slice_param->num_ref_idx_l0_active_minus1 = 0;      /* FIXME: ??? */
  639.     slice_param->num_ref_idx_l1_active_minus1 = 0;
  640.     slice_param->cabac_init_idc = 0;
  641.     slice_param->slice_qp_delta = 0;
  642.     slice_param->disable_deblocking_filter_idc = 0;
  643.     slice_param->slice_alpha_c0_offset_div2 = 2;
  644.     slice_param->slice_beta_offset_div2 = 2;
  645.     slice_param->idr_pic_id = 0;
  646.  
  647.     /* FIXME: fill other fields */
  648.  
  649.     va_status = vaCreateBuffer(va_dpy,
  650.                                avcenc_context.context_id,
  651.                                VAEncSliceParameterBufferType,
  652.                                sizeof(*slice_param), 1, slice_param,
  653.                                &avcenc_context.slice_param_buf_id[i]);
  654.     CHECK_VASTATUS(va_status,"vaCreateBuffer");;
  655.     i++;
  656. #endif
  657.  
  658.     avcenc_context.num_slices = i;
  659. }
  660.  
  661. static int update_ReferenceFrames(void)
  662. {
  663.     int i;
  664.     /* B-frame is not used for reference */
  665.     if (current_frame_type == SLICE_TYPE_B)
  666.         return 0;
  667.  
  668.     CurrentCurrPic.flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
  669.     numShortTerm++;
  670.     if (numShortTerm > num_ref_frames)
  671.         numShortTerm = num_ref_frames;
  672.     for (i=numShortTerm-1; i>0; i--)
  673.         ReferenceFrames[i] = ReferenceFrames[i-1];
  674.     ReferenceFrames[0] = CurrentCurrPic;
  675.  
  676.     if (current_frame_type != SLICE_TYPE_B)
  677.         current_frame_num++;
  678.     if (current_frame_num > MaxFrameNum)
  679.         current_frame_num = 0;
  680.  
  681.     /* Update the use_slot. Only when the surface is used in reference
  682.      * frame list, the use_slot[index] is set
  683.      */
  684.     for (i = 0; i < SURFACE_NUM; i++) {
  685.         int j;
  686.         bool found;
  687.  
  688.         found = false;
  689.         for (j = 0; j < numShortTerm; j++) {
  690.             if (ref_surface[i] == ReferenceFrames[j].picture_id) {
  691.                 found = true;
  692.                 break;
  693.             }
  694.         }
  695.         if (found)
  696.             use_slot[i] = 1;
  697.         else
  698.             use_slot[i] = 0;
  699.     }
  700.  
  701.     return 0;
  702. }
  703.  
  704. static int begin_picture(FILE *yuv_fp, int frame_num, int display_num, int slice_type, int is_idr)
  705. {
  706.     VAStatus va_status;
  707.  
  708.     if (avcenc_context.upload_thread_value != 0) {
  709.         fprintf(stderr, "FATAL error!!!\n");
  710.         exit(1);
  711.     }
  712.  
  713.     pthread_join(avcenc_context.upload_thread_id, NULL);
  714.  
  715.     avcenc_context.upload_thread_value = -1;
  716.  
  717.     if (avcenc_context.current_input_surface == SID_INPUT_PICTURE_0)
  718.         avcenc_context.current_input_surface = SID_INPUT_PICTURE_1;
  719.     else
  720.         avcenc_context.current_input_surface = SID_INPUT_PICTURE_0;
  721.  
  722.     if (is_idr) {
  723.         VAEncPackedHeaderParameterBuffer packed_header_param_buffer;
  724.         unsigned int length_in_bits;
  725.         unsigned char *packed_seq_buffer = NULL, *packed_pic_buffer = NULL;
  726.  
  727.         assert(slice_type == SLICE_TYPE_I);
  728.         length_in_bits = build_packed_seq_buffer(&packed_seq_buffer);
  729.         packed_header_param_buffer.type = VAEncPackedHeaderSequence;
  730.         packed_header_param_buffer.bit_length = length_in_bits;
  731.         packed_header_param_buffer.has_emulation_bytes = 0;
  732.         va_status = vaCreateBuffer(va_dpy,
  733.                                    avcenc_context.context_id,
  734.                                    VAEncPackedHeaderParameterBufferType,
  735.                                    sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
  736.                                    &avcenc_context.packed_seq_header_param_buf_id);
  737.         CHECK_VASTATUS(va_status,"vaCreateBuffer");
  738.  
  739.         va_status = vaCreateBuffer(va_dpy,
  740.                                    avcenc_context.context_id,
  741.                                    VAEncPackedHeaderDataBufferType,
  742.                                    (length_in_bits + 7) / 8, 1, packed_seq_buffer,
  743.                                    &avcenc_context.packed_seq_buf_id);
  744.         CHECK_VASTATUS(va_status,"vaCreateBuffer");
  745.  
  746.         length_in_bits = build_packed_pic_buffer(&packed_pic_buffer);
  747.         packed_header_param_buffer.type = VAEncPackedHeaderPicture;
  748.         packed_header_param_buffer.bit_length = length_in_bits;
  749.         packed_header_param_buffer.has_emulation_bytes = 0;
  750.  
  751.         va_status = vaCreateBuffer(va_dpy,
  752.                                    avcenc_context.context_id,
  753.                                    VAEncPackedHeaderParameterBufferType,
  754.                                    sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
  755.                                    &avcenc_context.packed_pic_header_param_buf_id);
  756.         CHECK_VASTATUS(va_status,"vaCreateBuffer");
  757.  
  758.         va_status = vaCreateBuffer(va_dpy,
  759.                                    avcenc_context.context_id,
  760.                                    VAEncPackedHeaderDataBufferType,
  761.                                    (length_in_bits + 7) / 8, 1, packed_pic_buffer,
  762.                                    &avcenc_context.packed_pic_buf_id);
  763.         CHECK_VASTATUS(va_status,"vaCreateBuffer");
  764.  
  765.         free(packed_seq_buffer);
  766.         free(packed_pic_buffer);
  767.     }
  768.  
  769.     /* sequence parameter set */
  770.     VAEncSequenceParameterBufferH264 *seq_param = &avcenc_context.seq_param;
  771.     va_status = vaCreateBuffer(va_dpy,
  772.                                avcenc_context.context_id,
  773.                                VAEncSequenceParameterBufferType,
  774.                                sizeof(*seq_param), 1, seq_param,
  775.                                &avcenc_context.seq_param_buf_id);
  776.     CHECK_VASTATUS(va_status,"vaCreateBuffer");
  777.  
  778.  
  779.     /* hrd parameter */
  780.     VAEncMiscParameterBuffer *misc_param;
  781.     VAEncMiscParameterHRD *misc_hrd_param;
  782.     vaCreateBuffer(va_dpy,
  783.                    avcenc_context.context_id,
  784.                    VAEncMiscParameterBufferType,
  785.                    sizeof(VAEncMiscParameterBuffer) + sizeof(VAEncMiscParameterRateControl),
  786.                    1,
  787.                    NULL,
  788.                    &avcenc_context.misc_parameter_hrd_buf_id);
  789.     CHECK_VASTATUS(va_status, "vaCreateBuffer");
  790.  
  791.     vaMapBuffer(va_dpy,
  792.                 avcenc_context.misc_parameter_hrd_buf_id,
  793.                 (void **)&misc_param);
  794.     misc_param->type = VAEncMiscParameterTypeHRD;
  795.     misc_hrd_param = (VAEncMiscParameterHRD *)misc_param->data;
  796.  
  797.     if (frame_bit_rate > 0) {
  798.         misc_hrd_param->initial_buffer_fullness = frame_bit_rate * 1000 * 4;
  799.         misc_hrd_param->buffer_size = frame_bit_rate * 1000 * 8;
  800.     } else {
  801.         misc_hrd_param->initial_buffer_fullness = 0;
  802.         misc_hrd_param->buffer_size = 0;
  803.     }
  804.  
  805.     vaUnmapBuffer(va_dpy, avcenc_context.misc_parameter_hrd_buf_id);
  806.  
  807.     return 0;
  808. }
  809.  
  810. int avcenc_render_picture()
  811. {
  812.     VAStatus va_status;
  813.     VABufferID va_buffers[10];
  814.     unsigned int num_va_buffers = 0;
  815.     int i;
  816.  
  817.     va_buffers[num_va_buffers++] = avcenc_context.seq_param_buf_id;
  818.     va_buffers[num_va_buffers++] = avcenc_context.pic_param_buf_id;
  819.  
  820.     if (avcenc_context.packed_seq_header_param_buf_id != VA_INVALID_ID)
  821.         va_buffers[num_va_buffers++] = avcenc_context.packed_seq_header_param_buf_id;
  822.  
  823.     if (avcenc_context.packed_seq_buf_id != VA_INVALID_ID)
  824.         va_buffers[num_va_buffers++] = avcenc_context.packed_seq_buf_id;
  825.  
  826.     if (avcenc_context.packed_pic_header_param_buf_id != VA_INVALID_ID)
  827.         va_buffers[num_va_buffers++] = avcenc_context.packed_pic_header_param_buf_id;
  828.  
  829.     if (avcenc_context.packed_pic_buf_id != VA_INVALID_ID)
  830.         va_buffers[num_va_buffers++] = avcenc_context.packed_pic_buf_id;
  831.  
  832.     if (avcenc_context.packed_sei_header_param_buf_id != VA_INVALID_ID)
  833.         va_buffers[num_va_buffers++] = avcenc_context.packed_sei_header_param_buf_id;
  834.  
  835.     if (avcenc_context.packed_sei_buf_id != VA_INVALID_ID)
  836.         va_buffers[num_va_buffers++] = avcenc_context.packed_sei_buf_id;
  837.  
  838.     if (avcenc_context.misc_parameter_hrd_buf_id != VA_INVALID_ID)
  839.         va_buffers[num_va_buffers++] =  avcenc_context.misc_parameter_hrd_buf_id;
  840.  
  841.     va_status = vaBeginPicture(va_dpy,
  842.                                avcenc_context.context_id,
  843.                                surface_ids[avcenc_context.current_input_surface]);
  844.     CHECK_VASTATUS(va_status,"vaBeginPicture");
  845.    
  846.     va_status = vaRenderPicture(va_dpy,
  847.                                 avcenc_context.context_id,
  848.                                 va_buffers,
  849.                                 num_va_buffers);
  850.     CHECK_VASTATUS(va_status,"vaRenderPicture");
  851.    
  852.     for(i = 0; i < avcenc_context.num_slices; i++) {
  853.         va_status = vaRenderPicture(va_dpy,
  854.                                 avcenc_context.context_id,
  855.                                 &avcenc_context.slice_param_buf_id[i],
  856.                                 1);
  857.         CHECK_VASTATUS(va_status,"vaRenderPicture");
  858.     }
  859.  
  860.     va_status = vaEndPicture(va_dpy, avcenc_context.context_id);
  861.     CHECK_VASTATUS(va_status,"vaEndPicture");
  862.  
  863.     return 0;
  864. }
  865.  
  866. static int avcenc_destroy_buffers(VABufferID *va_buffers, unsigned int num_va_buffers)
  867. {
  868.     VAStatus va_status;
  869.     unsigned int i;
  870.  
  871.     for (i = 0; i < num_va_buffers; i++) {
  872.         if (va_buffers[i] != VA_INVALID_ID) {
  873.             va_status = vaDestroyBuffer(va_dpy, va_buffers[i]);
  874.             CHECK_VASTATUS(va_status,"vaDestroyBuffer");
  875.             va_buffers[i] = VA_INVALID_ID;
  876.         }
  877.     }
  878.  
  879.     return 0;
  880. }
  881.  
  882. static void end_picture()
  883. {
  884.  
  885.     update_ReferenceFrames();
  886.     avcenc_destroy_buffers(&avcenc_context.seq_param_buf_id, 1);
  887.     avcenc_destroy_buffers(&avcenc_context.pic_param_buf_id, 1);
  888.     avcenc_destroy_buffers(&avcenc_context.packed_seq_header_param_buf_id, 1);
  889.     avcenc_destroy_buffers(&avcenc_context.packed_seq_buf_id, 1);
  890.     avcenc_destroy_buffers(&avcenc_context.packed_pic_header_param_buf_id, 1);
  891.     avcenc_destroy_buffers(&avcenc_context.packed_pic_buf_id, 1);
  892.     avcenc_destroy_buffers(&avcenc_context.packed_sei_header_param_buf_id, 1);
  893.     avcenc_destroy_buffers(&avcenc_context.packed_sei_buf_id, 1);
  894.     avcenc_destroy_buffers(&avcenc_context.slice_param_buf_id[0], avcenc_context.num_slices);
  895.     avcenc_destroy_buffers(&avcenc_context.codedbuf_buf_id, 1);
  896.     avcenc_destroy_buffers(&avcenc_context.misc_parameter_hrd_buf_id, 1);
  897.  
  898.     memset(avcenc_context.slice_param, 0, sizeof(avcenc_context.slice_param));
  899.     avcenc_context.num_slices = 0;
  900. }
  901.  
  902. #define BITSTREAM_ALLOCATE_STEPPING     4096
  903.  
  904. struct __bitstream {
  905.     unsigned int *buffer;
  906.     int bit_offset;
  907.     int max_size_in_dword;
  908. };
  909.  
  910. typedef struct __bitstream bitstream;
  911.  
  912. #if 0
  913. static int
  914. get_coded_bitsteam_length(unsigned char *buffer, int buffer_length)
  915. {
  916.     int i;
  917.  
  918.     for (i = 0; i < buffer_length - 3; i++) {
  919.         if (!buffer[i] &&
  920.             !buffer[i + 1] &&
  921.             !buffer[i + 2] &&
  922.             !buffer[i + 3])
  923.             break;
  924.     }
  925.  
  926.     return i;
  927. }
  928. #endif
  929.  
  930. static unsigned int
  931. va_swap32(unsigned int val)
  932. {
  933.     unsigned char *pval = (unsigned char *)&val;
  934.  
  935.     return ((pval[0] << 24)     |
  936.             (pval[1] << 16)     |
  937.             (pval[2] << 8)      |
  938.             (pval[3] << 0));
  939. }
  940.  
  941. static void
  942. bitstream_start(bitstream *bs)
  943. {
  944.     bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
  945.     bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
  946.     bs->bit_offset = 0;
  947. }
  948.  
  949. static void
  950. bitstream_end(bitstream *bs)
  951. {
  952.     int pos = (bs->bit_offset >> 5);
  953.     int bit_offset = (bs->bit_offset & 0x1f);
  954.     int bit_left = 32 - bit_offset;
  955.  
  956.     if (bit_offset) {
  957.         bs->buffer[pos] = va_swap32((bs->buffer[pos] << bit_left));
  958.     }
  959. }
  960.  
  961. static void
  962. bitstream_put_ui(bitstream *bs, unsigned int val, int size_in_bits)
  963. {
  964.     int pos = (bs->bit_offset >> 5);
  965.     int bit_offset = (bs->bit_offset & 0x1f);
  966.     int bit_left = 32 - bit_offset;
  967.  
  968.     if (!size_in_bits)
  969.         return;
  970.  
  971.     bs->bit_offset += size_in_bits;
  972.  
  973.     if (bit_left > size_in_bits) {
  974.         bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
  975.     } else {
  976.         size_in_bits -= bit_left;
  977.         bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
  978.         bs->buffer[pos] = va_swap32(bs->buffer[pos]);
  979.  
  980.         if (pos + 1 == bs->max_size_in_dword) {
  981.             bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
  982.             bs->buffer = realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
  983.         }
  984.  
  985.         bs->buffer[pos + 1] = val;
  986.     }
  987. }
  988.  
  989. static void
  990. bitstream_put_ue(bitstream *bs, unsigned int val)
  991. {
  992.     int size_in_bits = 0;
  993.     int tmp_val = ++val;
  994.  
  995.     while (tmp_val) {
  996.         tmp_val >>= 1;
  997.         size_in_bits++;
  998.     }
  999.  
  1000.     bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
  1001.     bitstream_put_ui(bs, val, size_in_bits);
  1002. }
  1003.  
  1004. static void
  1005. bitstream_put_se(bitstream *bs, int val)
  1006. {
  1007.     unsigned int new_val;
  1008.  
  1009.     if (val <= 0)
  1010.         new_val = -2 * val;
  1011.     else
  1012.         new_val = 2 * val - 1;
  1013.  
  1014.     bitstream_put_ue(bs, new_val);
  1015. }
  1016.  
  1017. static void
  1018. bitstream_byte_aligning(bitstream *bs, int bit)
  1019. {
  1020.     int bit_offset = (bs->bit_offset & 0x7);
  1021.     int bit_left = 8 - bit_offset;
  1022.     int new_val;
  1023.  
  1024.     if (!bit_offset)
  1025.         return;
  1026.  
  1027.     assert(bit == 0 || bit == 1);
  1028.  
  1029.     if (bit)
  1030.         new_val = (1 << bit_left) - 1;
  1031.     else
  1032.         new_val = 0;
  1033.  
  1034.     bitstream_put_ui(bs, new_val, bit_left);
  1035. }
  1036.  
  1037. static void
  1038. rbsp_trailing_bits(bitstream *bs)
  1039. {
  1040.     bitstream_put_ui(bs, 1, 1);
  1041.     bitstream_byte_aligning(bs, 0);
  1042. }
  1043.  
  1044. static void nal_start_code_prefix(bitstream *bs)
  1045. {
  1046.     bitstream_put_ui(bs, 0x00000001, 32);
  1047. }
  1048.  
  1049. static void nal_header(bitstream *bs, int nal_ref_idc, int nal_unit_type)
  1050. {
  1051.     bitstream_put_ui(bs, 0, 1);                /* forbidden_zero_bit: 0 */
  1052.     bitstream_put_ui(bs, nal_ref_idc, 2);
  1053.     bitstream_put_ui(bs, nal_unit_type, 5);
  1054. }
  1055.  
  1056. static void sps_rbsp(bitstream *bs)
  1057. {
  1058.     VAEncSequenceParameterBufferH264 *seq_param = &avcenc_context.seq_param;
  1059.     int profile_idc = PROFILE_IDC_BASELINE;
  1060.  
  1061.     if (avcenc_context.profile == VAProfileH264High)
  1062.         profile_idc = PROFILE_IDC_HIGH;
  1063.     else if (avcenc_context.profile == VAProfileH264Main)
  1064.         profile_idc = PROFILE_IDC_MAIN;
  1065.  
  1066.     bitstream_put_ui(bs, profile_idc, 8);               /* profile_idc */
  1067.     bitstream_put_ui(bs, !!(avcenc_context.constraint_set_flag & 1), 1);                         /* constraint_set0_flag */
  1068.     bitstream_put_ui(bs, !!(avcenc_context.constraint_set_flag & 2), 1);                         /* constraint_set1_flag */
  1069.     bitstream_put_ui(bs, !!(avcenc_context.constraint_set_flag & 4), 1);                         /* constraint_set2_flag */
  1070.     bitstream_put_ui(bs, !!(avcenc_context.constraint_set_flag & 8), 1);                         /* constraint_set3_flag */
  1071.     bitstream_put_ui(bs, 0, 4);                         /* reserved_zero_4bits */
  1072.     bitstream_put_ui(bs, seq_param->level_idc, 8);      /* level_idc */
  1073.     bitstream_put_ue(bs, seq_param->seq_parameter_set_id);      /* seq_parameter_set_id */
  1074.  
  1075.     if ( profile_idc == PROFILE_IDC_HIGH) {
  1076.         bitstream_put_ue(bs, 1);        /* chroma_format_idc = 1, 4:2:0 */
  1077.         bitstream_put_ue(bs, 0);        /* bit_depth_luma_minus8 */
  1078.         bitstream_put_ue(bs, 0);        /* bit_depth_chroma_minus8 */
  1079.         bitstream_put_ui(bs, 0, 1);     /* qpprime_y_zero_transform_bypass_flag */
  1080.         bitstream_put_ui(bs, 0, 1);     /* seq_scaling_matrix_present_flag */
  1081.     }
  1082.  
  1083.     bitstream_put_ue(bs, seq_param->seq_fields.bits.log2_max_frame_num_minus4); /* log2_max_frame_num_minus4 */
  1084.     bitstream_put_ue(bs, seq_param->seq_fields.bits.pic_order_cnt_type);        /* pic_order_cnt_type */
  1085.  
  1086.     if (seq_param->seq_fields.bits.pic_order_cnt_type == 0)
  1087.         bitstream_put_ue(bs, seq_param->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4);     /* log2_max_pic_order_cnt_lsb_minus4 */
  1088.     else {
  1089.         assert(0);
  1090.     }
  1091.  
  1092.     bitstream_put_ue(bs, seq_param->max_num_ref_frames);        /* num_ref_frames */
  1093.     bitstream_put_ui(bs, 0, 1);                                 /* gaps_in_frame_num_value_allowed_flag */
  1094.  
  1095.     bitstream_put_ue(bs, seq_param->picture_width_in_mbs - 1);  /* pic_width_in_mbs_minus1 */
  1096.     bitstream_put_ue(bs, seq_param->picture_height_in_mbs - 1); /* pic_height_in_map_units_minus1 */
  1097.     bitstream_put_ui(bs, seq_param->seq_fields.bits.frame_mbs_only_flag, 1);    /* frame_mbs_only_flag */
  1098.  
  1099.     if (!seq_param->seq_fields.bits.frame_mbs_only_flag) {
  1100.         assert(0);
  1101.     }
  1102.  
  1103.     bitstream_put_ui(bs, seq_param->seq_fields.bits.direct_8x8_inference_flag, 1);      /* direct_8x8_inference_flag */
  1104.     bitstream_put_ui(bs, seq_param->frame_cropping_flag, 1);            /* frame_cropping_flag */
  1105.  
  1106.     if (seq_param->frame_cropping_flag) {
  1107.         bitstream_put_ue(bs, seq_param->frame_crop_left_offset);        /* frame_crop_left_offset */
  1108.         bitstream_put_ue(bs, seq_param->frame_crop_right_offset);       /* frame_crop_right_offset */
  1109.         bitstream_put_ue(bs, seq_param->frame_crop_top_offset);         /* frame_crop_top_offset */
  1110.         bitstream_put_ue(bs, seq_param->frame_crop_bottom_offset);      /* frame_crop_bottom_offset */
  1111.     }
  1112.    
  1113.     if ( frame_bit_rate < 0 ) {
  1114.         bitstream_put_ui(bs, 0, 1); /* vui_parameters_present_flag */
  1115.     } else {
  1116.         bitstream_put_ui(bs, 1, 1); /* vui_parameters_present_flag */
  1117.         bitstream_put_ui(bs, 0, 1); /* aspect_ratio_info_present_flag */
  1118.         bitstream_put_ui(bs, 0, 1); /* overscan_info_present_flag */
  1119.         bitstream_put_ui(bs, 0, 1); /* video_signal_type_present_flag */
  1120.         bitstream_put_ui(bs, 0, 1); /* chroma_loc_info_present_flag */
  1121.         bitstream_put_ui(bs, 1, 1); /* timing_info_present_flag */
  1122.         {
  1123.             bitstream_put_ui(bs, 1, 32);
  1124.             bitstream_put_ui(bs, frame_rate * 2, 32);
  1125.             bitstream_put_ui(bs, 1, 1);
  1126.         }
  1127.         bitstream_put_ui(bs, 1, 1); /* nal_hrd_parameters_present_flag */
  1128.         {
  1129.             // hrd_parameters
  1130.             bitstream_put_ue(bs, 0);    /* cpb_cnt_minus1 */
  1131.             bitstream_put_ui(bs, 0, 4); /* bit_rate_scale */
  1132.             bitstream_put_ui(bs, 2, 4); /* cpb_size_scale */
  1133.            
  1134.             /* the frame_bit_rate is in kbps */
  1135.             bitstream_put_ue(bs, (((frame_bit_rate * 1000)>> 6) - 1)); /* bit_rate_value_minus1[0] */
  1136.             bitstream_put_ue(bs, ((frame_bit_rate * 8000) >> 6) - 1); /* cpb_size_value_minus1[0] */
  1137.             bitstream_put_ui(bs, 1, 1);  /* cbr_flag[0] */
  1138.  
  1139.             /* initial_cpb_removal_delay_length_minus1 */
  1140.             bitstream_put_ui(bs,
  1141.                 (avcenc_context.i_initial_cpb_removal_delay_length - 1), 5);
  1142.             /* cpb_removal_delay_length_minus1 */
  1143.             bitstream_put_ui(bs,
  1144.                 (avcenc_context.i_cpb_removal_delay_length - 1), 5);
  1145.             /* dpb_output_delay_length_minus1 */
  1146.             bitstream_put_ui(bs,
  1147.                 (avcenc_context.i_dpb_output_delay_length - 1), 5);
  1148.             /* time_offset_length  */
  1149.             bitstream_put_ui(bs,
  1150.                 (avcenc_context.time_offset_length - 1), 5);
  1151.         }
  1152.         bitstream_put_ui(bs, 0, 1);   /* vcl_hrd_parameters_present_flag */
  1153.         bitstream_put_ui(bs, 0, 1);   /* low_delay_hrd_flag */
  1154.  
  1155.         bitstream_put_ui(bs, 0, 1); /* pic_struct_present_flag */
  1156.         bitstream_put_ui(bs, 0, 1); /* bitstream_restriction_flag */
  1157.     }
  1158.  
  1159.     rbsp_trailing_bits(bs);     /* rbsp_trailing_bits */
  1160. }
  1161.  
  1162. #if 0
  1163. static void build_nal_sps(FILE *avc_fp)
  1164. {
  1165.     bitstream bs;
  1166.  
  1167.     bitstream_start(&bs);
  1168.     nal_start_code_prefix(&bs);
  1169.     nal_header(&bs, NAL_REF_IDC_HIGH, NAL_SPS);
  1170.     sps_rbsp(&bs);
  1171.     bitstream_end(&bs, avc_fp);
  1172. }
  1173. #endif
  1174.  
  1175. static void pps_rbsp(bitstream *bs)
  1176. {
  1177.     VAEncPictureParameterBufferH264 *pic_param = &avcenc_context.pic_param;
  1178.  
  1179.     bitstream_put_ue(bs, pic_param->pic_parameter_set_id);      /* pic_parameter_set_id */
  1180.     bitstream_put_ue(bs, pic_param->seq_parameter_set_id);      /* seq_parameter_set_id */
  1181.  
  1182.     bitstream_put_ui(bs, pic_param->pic_fields.bits.entropy_coding_mode_flag, 1);  /* entropy_coding_mode_flag */
  1183.  
  1184.     bitstream_put_ui(bs, 0, 1);                         /* pic_order_present_flag: 0 */
  1185.  
  1186.     bitstream_put_ue(bs, 0);                            /* num_slice_groups_minus1 */
  1187.  
  1188.     bitstream_put_ue(bs, pic_param->num_ref_idx_l0_active_minus1);      /* num_ref_idx_l0_active_minus1 */
  1189.     bitstream_put_ue(bs, pic_param->num_ref_idx_l1_active_minus1);      /* num_ref_idx_l1_active_minus1 1 */
  1190.  
  1191.     bitstream_put_ui(bs, pic_param->pic_fields.bits.weighted_pred_flag, 1);     /* weighted_pred_flag: 0 */
  1192.     bitstream_put_ui(bs, pic_param->pic_fields.bits.weighted_bipred_idc, 2);    /* weighted_bipred_idc: 0 */
  1193.  
  1194.     bitstream_put_se(bs, pic_param->pic_init_qp - 26);  /* pic_init_qp_minus26 */
  1195.     bitstream_put_se(bs, 0);                            /* pic_init_qs_minus26 */
  1196.     bitstream_put_se(bs, 0);                            /* chroma_qp_index_offset */
  1197.  
  1198.     bitstream_put_ui(bs, pic_param->pic_fields.bits.deblocking_filter_control_present_flag, 1); /* deblocking_filter_control_present_flag */
  1199.     bitstream_put_ui(bs, 0, 1);                         /* constrained_intra_pred_flag */
  1200.     bitstream_put_ui(bs, 0, 1);                         /* redundant_pic_cnt_present_flag */
  1201.    
  1202.     /* more_rbsp_data */
  1203.     bitstream_put_ui(bs, pic_param->pic_fields.bits.transform_8x8_mode_flag, 1);    /*transform_8x8_mode_flag */
  1204.     bitstream_put_ui(bs, 0, 1);                         /* pic_scaling_matrix_present_flag */
  1205.     bitstream_put_se(bs, pic_param->second_chroma_qp_index_offset );    /*second_chroma_qp_index_offset */
  1206.  
  1207.     rbsp_trailing_bits(bs);
  1208. }
  1209.  
  1210. #if 0
  1211. static void build_nal_pps(FILE *avc_fp)
  1212. {
  1213.     bitstream bs;
  1214.  
  1215.     bitstream_start(&bs);
  1216.     nal_start_code_prefix(&bs);
  1217.     nal_header(&bs, NAL_REF_IDC_HIGH, NAL_PPS);
  1218.     pps_rbsp(&bs);
  1219.     bitstream_end(&bs, avc_fp);
  1220. }
  1221.  
  1222. static void
  1223. build_header(FILE *avc_fp)
  1224. {
  1225.     build_nal_sps(avc_fp);
  1226.     build_nal_pps(avc_fp);
  1227. }
  1228. #endif
  1229.  
  1230. static int
  1231. build_packed_pic_buffer(unsigned char **header_buffer)
  1232. {
  1233.     bitstream bs;
  1234.  
  1235.     bitstream_start(&bs);
  1236.     nal_start_code_prefix(&bs);
  1237.     nal_header(&bs, NAL_REF_IDC_HIGH, NAL_PPS);
  1238.     pps_rbsp(&bs);
  1239.     bitstream_end(&bs);
  1240.  
  1241.     *header_buffer = (unsigned char *)bs.buffer;
  1242.     return bs.bit_offset;
  1243. }
  1244.  
  1245. static int
  1246. build_packed_seq_buffer(unsigned char **header_buffer)
  1247. {
  1248.     bitstream bs;
  1249.  
  1250.     bitstream_start(&bs);
  1251.     nal_start_code_prefix(&bs);
  1252.     nal_header(&bs, NAL_REF_IDC_HIGH, NAL_SPS);
  1253.     sps_rbsp(&bs);
  1254.     bitstream_end(&bs);
  1255.  
  1256.     *header_buffer = (unsigned char *)bs.buffer;
  1257.     return bs.bit_offset;
  1258. }
  1259.  
  1260. static int
  1261. build_packed_idr_sei_buffer_timing(unsigned int init_cpb_removal_delay_length,
  1262.                                 unsigned int cpb_removal_length,
  1263.                                 unsigned int dpb_output_length,
  1264.                                 unsigned char **sei_buffer)
  1265. {
  1266.     unsigned char *byte_buf;
  1267.     int bp_byte_size, i, pic_byte_size;
  1268.     unsigned int cpb_removal_delay;
  1269.  
  1270.     bitstream nal_bs;
  1271.     bitstream sei_bp_bs, sei_pic_bs;
  1272.  
  1273.     bitstream_start(&sei_bp_bs);
  1274.     bitstream_put_ue(&sei_bp_bs, 0);       /*seq_parameter_set_id*/
  1275.     /* SEI buffer period info */
  1276.     /* NALHrdBpPresentFlag == 1 */
  1277.     bitstream_put_ui(&sei_bp_bs, avcenc_context.i_initial_cpb_removal_delay,
  1278.                      init_cpb_removal_delay_length);
  1279.     bitstream_put_ui(&sei_bp_bs, avcenc_context.i_initial_cpb_removal_delay_offset,
  1280.                      init_cpb_removal_delay_length);
  1281.     if ( sei_bp_bs.bit_offset & 0x7) {
  1282.         bitstream_put_ui(&sei_bp_bs, 1, 1);
  1283.     }
  1284.     bitstream_end(&sei_bp_bs);
  1285.     bp_byte_size = (sei_bp_bs.bit_offset + 7) / 8;
  1286.    
  1287.     /* SEI pic timing info */
  1288.     bitstream_start(&sei_pic_bs);
  1289.     /* The info of CPB and DPB delay is controlled by CpbDpbDelaysPresentFlag,
  1290.      * which is derived as 1 if one of the following conditions is true:
  1291.      * nal_hrd_parameters_present_flag is present in the bitstream and is equal to 1,
  1292.      * vcl_hrd_parameters_present_flag is present in the bitstream and is equal to 1,
  1293.      */
  1294.     cpb_removal_delay = (avcenc_context.current_cpb_removal - avcenc_context.prev_idr_cpb_removal);
  1295.     bitstream_put_ui(&sei_pic_bs, cpb_removal_delay, cpb_removal_length);
  1296.     bitstream_put_ui(&sei_pic_bs, avcenc_context.current_dpb_removal_delta,
  1297.                      dpb_output_length);
  1298.     if ( sei_pic_bs.bit_offset & 0x7) {
  1299.         bitstream_put_ui(&sei_pic_bs, 1, 1);
  1300.     }
  1301.     /* The pic_structure_present_flag determines whether the pic_structure
  1302.      * info is written into the SEI pic timing info.
  1303.      * Currently it is set to zero.
  1304.      */
  1305.     bitstream_end(&sei_pic_bs);
  1306.     pic_byte_size = (sei_pic_bs.bit_offset + 7) / 8;
  1307.    
  1308.     bitstream_start(&nal_bs);
  1309.     nal_start_code_prefix(&nal_bs);
  1310.     nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
  1311.  
  1312.         /* Write the SEI buffer period data */    
  1313.     bitstream_put_ui(&nal_bs, 0, 8);
  1314.     bitstream_put_ui(&nal_bs, bp_byte_size, 8);
  1315.    
  1316.     byte_buf = (unsigned char *)sei_bp_bs.buffer;
  1317.     for(i = 0; i < bp_byte_size; i++) {
  1318.         bitstream_put_ui(&nal_bs, byte_buf[i], 8);
  1319.     }
  1320.     free(byte_buf);
  1321.         /* write the SEI pic timing data */
  1322.     bitstream_put_ui(&nal_bs, 0x01, 8);
  1323.     bitstream_put_ui(&nal_bs, pic_byte_size, 8);
  1324.    
  1325.     byte_buf = (unsigned char *)sei_pic_bs.buffer;
  1326.     for(i = 0; i < pic_byte_size; i++) {
  1327.         bitstream_put_ui(&nal_bs, byte_buf[i], 8);
  1328.     }
  1329.     free(byte_buf);
  1330.  
  1331.     rbsp_trailing_bits(&nal_bs);
  1332.     bitstream_end(&nal_bs);
  1333.  
  1334.     *sei_buffer = (unsigned char *)nal_bs.buffer;
  1335.    
  1336.     return nal_bs.bit_offset;
  1337. }
  1338.  
  1339. static int
  1340. build_packed_sei_pic_timing(unsigned int cpb_removal_length,
  1341.                                 unsigned int dpb_output_length,
  1342.                                 unsigned char **sei_buffer)
  1343. {
  1344.     unsigned char *byte_buf;
  1345.     int i, pic_byte_size;
  1346.     unsigned int cpb_removal_delay;
  1347.  
  1348.     bitstream nal_bs;
  1349.     bitstream sei_pic_bs;
  1350.  
  1351.     bitstream_start(&sei_pic_bs);
  1352.     /* The info of CPB and DPB delay is controlled by CpbDpbDelaysPresentFlag,
  1353.      * which is derived as 1 if one of the following conditions is true:
  1354.      * nal_hrd_parameters_present_flag is present in the bitstream and is equal to 1,
  1355.      * vcl_hrd_parameters_present_flag is present in the bitstream and is equal to 1,
  1356.      */
  1357.     cpb_removal_delay = (avcenc_context.current_cpb_removal - avcenc_context.current_idr_cpb_removal);
  1358.     bitstream_put_ui(&sei_pic_bs, cpb_removal_delay, cpb_removal_length);
  1359.     bitstream_put_ui(&sei_pic_bs, avcenc_context.current_dpb_removal_delta,
  1360.                      dpb_output_length);
  1361.     if ( sei_pic_bs.bit_offset & 0x7) {
  1362.         bitstream_put_ui(&sei_pic_bs, 1, 1);
  1363.     }
  1364.  
  1365.     /* The pic_structure_present_flag determines whether the pic_structure
  1366.      * info is written into the SEI pic timing info.
  1367.      * Currently it is set to zero.
  1368.      */
  1369.     bitstream_end(&sei_pic_bs);
  1370.     pic_byte_size = (sei_pic_bs.bit_offset + 7) / 8;
  1371.  
  1372.     bitstream_start(&nal_bs);
  1373.     nal_start_code_prefix(&nal_bs);
  1374.     nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
  1375.  
  1376.         /* write the SEI Pic timing data */
  1377.     bitstream_put_ui(&nal_bs, 0x01, 8);
  1378.     bitstream_put_ui(&nal_bs, pic_byte_size, 8);
  1379.  
  1380.     byte_buf = (unsigned char *)sei_pic_bs.buffer;
  1381.     for(i = 0; i < pic_byte_size; i++) {
  1382.         bitstream_put_ui(&nal_bs, byte_buf[i], 8);
  1383.     }
  1384.     free(byte_buf);
  1385.  
  1386.     rbsp_trailing_bits(&nal_bs);
  1387.     bitstream_end(&nal_bs);
  1388.  
  1389.     *sei_buffer = (unsigned char *)nal_bs.buffer;
  1390.  
  1391.     return nal_bs.bit_offset;
  1392. }
  1393.  
  1394. #if 0
  1395. static void
  1396. slice_header(bitstream *bs, int frame_num, int display_frame, int slice_type, int nal_ref_idc, int is_idr)
  1397. {
  1398.     VAEncSequenceParameterBufferH264 *seq_param = &avcenc_context.seq_param;
  1399.     VAEncPictureParameterBufferH264 *pic_param = &avcenc_context.pic_param;
  1400.     int is_cabac = (pic_param->pic_fields.bits.entropy_coding_mode_flag == ENTROPY_MODE_CABAC);
  1401.  
  1402.     bitstream_put_ue(bs, 0);                   /* first_mb_in_slice: 0 */
  1403.     bitstream_put_ue(bs, slice_type);          /* slice_type */
  1404.     bitstream_put_ue(bs, 0);                   /* pic_parameter_set_id: 0 */
  1405.     bitstream_put_ui(bs, frame_num & 0x0F, seq_param->seq_fields.bits.log2_max_frame_num_minus4 + 4);    /* frame_num */
  1406.  
  1407.     /* frame_mbs_only_flag == 1 */
  1408.     if (!seq_param->seq_fields.bits.frame_mbs_only_flag) {
  1409.         /* FIXME: */
  1410.         assert(0);
  1411.     }
  1412.  
  1413.     if (is_idr)
  1414.         bitstream_put_ue(bs, 0);                /* idr_pic_id: 0 */
  1415.  
  1416.     if (seq_param->seq_fields.bits.pic_order_cnt_type == 0) {
  1417.         bitstream_put_ui(bs, (display_frame*2) & 0x3F, seq_param->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 + 4);
  1418.         /* only support frame */
  1419.     } else {
  1420.         /* FIXME: */
  1421.         assert(0);
  1422.     }
  1423.  
  1424.     /* redundant_pic_cnt_present_flag == 0 */
  1425.    
  1426.     /* slice type */
  1427.     if (slice_type == SLICE_TYPE_P) {
  1428.         bitstream_put_ui(bs, 0, 1);            /* num_ref_idx_active_override_flag: 0 */
  1429.         /* ref_pic_list_reordering */
  1430.         bitstream_put_ui(bs, 0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
  1431.     } else if (slice_type == SLICE_TYPE_B) {
  1432.         bitstream_put_ui(bs, 1, 1);            /* direct_spatial_mv_pred: 1 */
  1433.         bitstream_put_ui(bs, 0, 1);            /* num_ref_idx_active_override_flag: 0 */
  1434.         /* ref_pic_list_reordering */
  1435.         bitstream_put_ui(bs, 0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
  1436.         bitstream_put_ui(bs, 0, 1);            /* ref_pic_list_reordering_flag_l1: 0 */
  1437.     }
  1438.  
  1439.     /* weighted_pred_flag == 0 */
  1440.  
  1441.     /* dec_ref_pic_marking */
  1442.     if (nal_ref_idc != 0) {
  1443.         if ( is_idr) {
  1444.             bitstream_put_ui(bs, 0, 1);            /* no_output_of_prior_pics_flag: 0 */
  1445.             bitstream_put_ui(bs, 0, 1);            /* long_term_reference_flag: 0 */
  1446.         } else {
  1447.             bitstream_put_ui(bs, 0, 1);            /* adaptive_ref_pic_marking_mode_flag: 0 */
  1448.         }
  1449.     }
  1450.  
  1451.     if (is_cabac && (slice_type != SLICE_TYPE_I))
  1452.         bitstream_put_ue(bs, 0);               /* cabac_init_idc: 0 */
  1453.  
  1454.     bitstream_put_se(bs, 0);                   /* slice_qp_delta: 0 */
  1455.  
  1456.     if (pic_param->pic_fields.bits.deblocking_filter_control_present_flag == 1) {
  1457.         bitstream_put_ue(bs, 0);               /* disable_deblocking_filter_idc: 0 */
  1458.         bitstream_put_se(bs, 2);               /* slice_alpha_c0_offset_div2: 2 */
  1459.         bitstream_put_se(bs, 2);               /* slice_beta_offset_div2: 2 */
  1460.     }
  1461. }
  1462.  
  1463. static void
  1464. slice_data(bitstream *bs)
  1465. {
  1466.     VACodedBufferSegment *coded_buffer_segment;
  1467.     unsigned char *coded_mem;
  1468.     int i, slice_data_length;
  1469.     VAStatus va_status;
  1470.     VASurfaceStatus surface_status;
  1471.  
  1472.     va_status = vaSyncSurface(va_dpy, surface_ids[avcenc_context.current_input_surface]);
  1473.     CHECK_VASTATUS(va_status,"vaSyncSurface");
  1474.  
  1475.     surface_status = 0;
  1476.     va_status = vaQuerySurfaceStatus(va_dpy, surface_ids[avcenc_context.current_input_surface], &surface_status);
  1477.     CHECK_VASTATUS(va_status,"vaQuerySurfaceStatus");
  1478.  
  1479.     va_status = vaMapBuffer(va_dpy, avcenc_context.codedbuf_buf_id, (void **)(&coded_buffer_segment));
  1480.     CHECK_VASTATUS(va_status,"vaMapBuffer");
  1481.     coded_mem = coded_buffer_segment->buf;
  1482.  
  1483.     slice_data_length = get_coded_bitsteam_length(coded_mem, codedbuf_size);
  1484.  
  1485.     for (i = 0; i < slice_data_length; i++) {
  1486.         bitstream_put_ui(bs, *coded_mem, 8);
  1487.         coded_mem++;
  1488.     }
  1489.  
  1490.     vaUnmapBuffer(va_dpy, avcenc_context.codedbuf_buf_id);
  1491. }
  1492.  
  1493. static void
  1494. build_nal_slice(FILE *avc_fp, int frame_num, int display_frame, int slice_type, int is_idr)
  1495. {
  1496.     bitstream bs;
  1497.  
  1498.     bitstream_start(&bs);
  1499.     slice_data(&bs);
  1500.     bitstream_end(&bs, avc_fp);
  1501. }
  1502.  
  1503. #endif
  1504.  
  1505. static int
  1506. store_coded_buffer(FILE *avc_fp, int slice_type)
  1507. {
  1508.     VACodedBufferSegment *coded_buffer_segment;
  1509.     unsigned char *coded_mem;
  1510.     int slice_data_length;
  1511.     VAStatus va_status;
  1512.     VASurfaceStatus surface_status;
  1513.     size_t w_items;
  1514.  
  1515.     va_status = vaSyncSurface(va_dpy, surface_ids[avcenc_context.current_input_surface]);
  1516.     CHECK_VASTATUS(va_status,"vaSyncSurface");
  1517.  
  1518.     surface_status = 0;
  1519.     va_status = vaQuerySurfaceStatus(va_dpy, surface_ids[avcenc_context.current_input_surface], &surface_status);
  1520.     CHECK_VASTATUS(va_status,"vaQuerySurfaceStatus");
  1521.  
  1522.     va_status = vaMapBuffer(va_dpy, avcenc_context.codedbuf_buf_id, (void **)(&coded_buffer_segment));
  1523.     CHECK_VASTATUS(va_status,"vaMapBuffer");
  1524.     coded_mem = coded_buffer_segment->buf;
  1525.  
  1526.     if (coded_buffer_segment->status & VA_CODED_BUF_STATUS_SLICE_OVERFLOW_MASK) {
  1527.         if (slice_type == SLICE_TYPE_I)
  1528.             avcenc_context.codedbuf_i_size *= 2;
  1529.         else
  1530.             avcenc_context.codedbuf_pb_size *= 2;
  1531.  
  1532.         vaUnmapBuffer(va_dpy, avcenc_context.codedbuf_buf_id);
  1533.         return -1;
  1534.     }
  1535.  
  1536.     slice_data_length = coded_buffer_segment->size;
  1537.  
  1538.     do {
  1539.         w_items = fwrite(coded_mem, slice_data_length, 1, avc_fp);
  1540.     } while (w_items != 1);
  1541.  
  1542.     vaUnmapBuffer(va_dpy, avcenc_context.codedbuf_buf_id);
  1543.  
  1544.     return 0;
  1545. }
  1546.  
  1547. /*
  1548.  * It is from the h264encode.c but it simplifies something.
  1549.  * For example: When one frame is encoded as I-frame under the scenario with
  1550.  * P-B frames, it will be regarded as IDR frame(key-frame) and then new GOP is
  1551.  * started. If the video clip is encoded as all I-frames, the first frame
  1552.  * is regarded as IDR and the remaining is regarded as I-frame.
  1553.  *
  1554.  */
  1555.  
  1556. static void encoding2display_order(
  1557.     unsigned long long encoding_order,int gop_size,
  1558.     int ip_period,
  1559.     unsigned long long *displaying_order,
  1560.     int *frame_type)
  1561. {
  1562.     int encoding_order_gop = 0;
  1563.  
  1564.     /* When ip_period is 0, all are I/IDR frames */
  1565.     if (ip_period == 0) { /* all are I/IDR frames */
  1566.         if (encoding_order == 0)
  1567.             *frame_type = FRAME_IDR;
  1568.         else
  1569.             *frame_type = SLICE_TYPE_I;
  1570.  
  1571.         *displaying_order = encoding_order;
  1572.         return;
  1573.     }
  1574.  
  1575.     /* new sequence like
  1576.      * IDR PPPPP IDRPPPPP
  1577.      * IDR (PBB)(PBB)(PBB)(PBB) IDR (PBB)(PBB)(PBB)(PBB)
  1578.      */
  1579.     encoding_order_gop = encoding_order % gop_size;
  1580.  
  1581.     if (encoding_order_gop == 0) { /* the first frame */
  1582.         *frame_type = FRAME_IDR;
  1583.         *displaying_order = encoding_order;
  1584.     } else {
  1585.         int gop_delta;
  1586.  
  1587.         gop_delta = 1;
  1588.  
  1589.         if ((ip_period != 1) && ((gop_size - 1) % ip_period)) {
  1590.             int ipb_size;
  1591.             ipb_size = (gop_size - 1) / ip_period * ip_period + 1;
  1592.             if (encoding_order_gop >= ipb_size) {
  1593.                 gop_delta = ipb_size;
  1594.                 ip_period = gop_size - ipb_size;
  1595.             }
  1596.         }
  1597.  
  1598.         if (((encoding_order_gop - gop_delta) % ip_period) == 0) { /* P frames */
  1599.             *frame_type = SLICE_TYPE_P;
  1600.             *displaying_order = encoding_order + ip_period - 1;
  1601.         } else {
  1602.             *frame_type = SLICE_TYPE_B;
  1603.             *displaying_order = encoding_order - 1;
  1604.         }
  1605.     }
  1606. }
  1607.  
  1608.  
  1609. static void
  1610. encode_picture(FILE *yuv_fp, FILE *avc_fp,
  1611.                int frame_num, int display_num,
  1612.                int is_idr,
  1613.                int slice_type, int next_is_bpic,
  1614.                int next_display_num)
  1615. {
  1616.     VAStatus va_status;
  1617.     int ret = 0, codedbuf_size;
  1618.    
  1619.     begin_picture(yuv_fp, frame_num, display_num, slice_type, is_idr);
  1620.  
  1621.     //if (next_display_num < frame_number) {
  1622.     if (1) {
  1623.         int index;
  1624.  
  1625.         /* prepare for next frame */
  1626.         if (avcenc_context.current_input_surface == SID_INPUT_PICTURE_0)
  1627.             index = SID_INPUT_PICTURE_1;
  1628.         else
  1629.             index = SID_INPUT_PICTURE_0;
  1630.         if ( next_display_num >= frame_number )
  1631.             next_display_num = frame_number - 1;
  1632.         fseeko(yuv_fp, (off_t)frame_size * next_display_num, SEEK_SET);
  1633.  
  1634.         avcenc_context.upload_thread_param.yuv_fp = yuv_fp;
  1635.         avcenc_context.upload_thread_param.surface_id = surface_ids[index];
  1636.  
  1637.         avcenc_context.upload_thread_value = pthread_create(&avcenc_context.upload_thread_id,
  1638.                                                             NULL,
  1639.                                                             upload_thread_function,
  1640.                                                             (void*)&avcenc_context.upload_thread_param);
  1641.     }
  1642.  
  1643.     do {
  1644.         avcenc_destroy_buffers(&avcenc_context.codedbuf_buf_id, 1);
  1645.         avcenc_destroy_buffers(&avcenc_context.pic_param_buf_id, 1);
  1646.  
  1647.  
  1648.         if (SLICE_TYPE_I == slice_type) {
  1649.             codedbuf_size = avcenc_context.codedbuf_i_size;
  1650.         } else {
  1651.             codedbuf_size = avcenc_context.codedbuf_pb_size;
  1652.         }
  1653.  
  1654.         /* coded buffer */
  1655.         va_status = vaCreateBuffer(va_dpy,
  1656.                                    avcenc_context.context_id,
  1657.                                    VAEncCodedBufferType,
  1658.                                    codedbuf_size, 1, NULL,
  1659.                                    &avcenc_context.codedbuf_buf_id);
  1660.         CHECK_VASTATUS(va_status,"vaCreateBuffer");
  1661.  
  1662.         /* Update the RefPicList */
  1663.         update_RefPicList();
  1664.  
  1665.         /* picture parameter set */
  1666.         avcenc_update_picture_parameter(slice_type, is_idr);
  1667.  
  1668.         /* slice parameter */
  1669.         avcenc_update_slice_parameter(slice_type);
  1670.  
  1671.         if (avcenc_context.rate_control_method == VA_RC_CBR)
  1672.                 avcenc_update_sei_param(is_idr);
  1673.  
  1674.         avcenc_render_picture();
  1675.  
  1676.         ret = store_coded_buffer(avc_fp, slice_type);
  1677.     } while (ret);
  1678.  
  1679.     end_picture(slice_type, next_is_bpic);
  1680. }
  1681.  
  1682. static void show_help()
  1683. {
  1684.     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");
  1685. }
  1686.  
  1687. static void avcenc_context_seq_param_init(VAEncSequenceParameterBufferH264 *seq_param,
  1688.                                           int width, int height)
  1689.  
  1690. {
  1691.     int width_in_mbs = (width + 15) / 16;
  1692.     int height_in_mbs = (height + 15) / 16;
  1693.     int frame_cropping_flag = 0;
  1694.     int frame_crop_bottom_offset = 0;
  1695.  
  1696.     seq_param->seq_parameter_set_id = 0;
  1697.     seq_param->level_idc = 41;
  1698.     seq_param->intra_period = intra_period;
  1699.     seq_param->intra_idr_period = seq_param->intra_period;
  1700.     seq_param->ip_period = ip_period;
  1701.     seq_param->max_num_ref_frames = 4;
  1702.     seq_param->picture_width_in_mbs = width_in_mbs;
  1703.     seq_param->picture_height_in_mbs = height_in_mbs;
  1704.     seq_param->seq_fields.bits.frame_mbs_only_flag = 1;
  1705.     seq_param->seq_fields.bits.chroma_format_idc = 1;
  1706.  
  1707.    
  1708.     if (frame_bit_rate > 0)
  1709.         seq_param->bits_per_second = 1000 * frame_bit_rate; /* use kbps as input */
  1710.     else
  1711.         seq_param->bits_per_second = 0;
  1712.    
  1713.     seq_param->time_scale = frame_rate * 2;
  1714.     seq_param->num_units_in_tick = 1;                   /* Tc = num_units_in_tick / time_sacle */
  1715.  
  1716.     if (height_in_mbs * 16 - height) {
  1717.         frame_cropping_flag = 1;
  1718.         frame_crop_bottom_offset =
  1719.             (height_in_mbs * 16 - height) / (2 * (!seq_param->seq_fields.bits.frame_mbs_only_flag + 1));
  1720.     }
  1721.  
  1722.     seq_param->frame_cropping_flag = frame_cropping_flag;
  1723.     seq_param->frame_crop_left_offset = 0;
  1724.     seq_param->frame_crop_right_offset = 0;
  1725.     seq_param->frame_crop_top_offset = 0;
  1726.     seq_param->frame_crop_bottom_offset = frame_crop_bottom_offset;
  1727.  
  1728.     seq_param->seq_fields.bits.pic_order_cnt_type = 0;
  1729.     seq_param->seq_fields.bits.direct_8x8_inference_flag = 0;
  1730.    
  1731.     seq_param->seq_fields.bits.log2_max_frame_num_minus4 = Log2MaxFrameNum - 4;
  1732.     seq_param->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 = Log2MaxPicOrderCntLsb - 4;
  1733.        
  1734.     if (frame_bit_rate > 0)
  1735.         seq_param->vui_parameters_present_flag = 1;     //HRD info located in vui
  1736.     else
  1737.         seq_param->vui_parameters_present_flag = 0;
  1738. }
  1739.  
  1740. static void avcenc_context_pic_param_init(VAEncPictureParameterBufferH264 *pic_param)
  1741. {
  1742.     pic_param->seq_parameter_set_id = 0;
  1743.     pic_param->pic_parameter_set_id = 0;
  1744.  
  1745.     pic_param->last_picture = 0;
  1746.     pic_param->frame_num = 0;
  1747.    
  1748.     pic_param->pic_init_qp = (qp_value >= 0 ?  qp_value : 26);
  1749.     pic_param->num_ref_idx_l0_active_minus1 = 0;
  1750.     pic_param->num_ref_idx_l1_active_minus1 = 0;
  1751.  
  1752.     pic_param->pic_fields.bits.idr_pic_flag = 0;
  1753.     pic_param->pic_fields.bits.reference_pic_flag = 0;
  1754.     pic_param->pic_fields.bits.entropy_coding_mode_flag = ENTROPY_MODE_CABAC;
  1755.     pic_param->pic_fields.bits.weighted_pred_flag = 0;
  1756.     pic_param->pic_fields.bits.weighted_bipred_idc = 0;
  1757.    
  1758.     if (avcenc_context.constraint_set_flag & 0x7)
  1759.         pic_param->pic_fields.bits.transform_8x8_mode_flag = 0;
  1760.     else
  1761.         pic_param->pic_fields.bits.transform_8x8_mode_flag = 1;
  1762.  
  1763.     pic_param->pic_fields.bits.deblocking_filter_control_present_flag = 1;
  1764.  
  1765.     memset(pic_param->ReferenceFrames, 0xff, 16 * sizeof(VAPictureH264)); /* invalid all */
  1766. }
  1767.  
  1768. static void avcenc_context_sei_init()
  1769. {
  1770.         /* it comes for the bps defined in SPS */
  1771.         avcenc_context.i_initial_cpb_removal_delay = 2 * 90000;
  1772.         avcenc_context.i_initial_cpb_removal_delay_offset = 2 * 90000;
  1773.  
  1774.         avcenc_context.i_cpb_removal_delay = 2;
  1775.         avcenc_context.i_initial_cpb_removal_delay_length = 24;
  1776.         avcenc_context.i_cpb_removal_delay_length = 24;
  1777.         avcenc_context.i_dpb_output_delay_length = 24;
  1778.         avcenc_context.time_offset_length = 24;
  1779.  
  1780.         avcenc_context.prev_idr_cpb_removal = avcenc_context.i_initial_cpb_removal_delay / 90000;
  1781.         avcenc_context.current_idr_cpb_removal = avcenc_context.prev_idr_cpb_removal;
  1782.         avcenc_context.current_cpb_removal = 0;
  1783.         avcenc_context.idr_frame_num = 0;
  1784. }
  1785.  
  1786. static void avcenc_context_init(int width, int height)
  1787. {
  1788.     int i;
  1789.     memset(&avcenc_context, 0, sizeof(avcenc_context));
  1790.     avcenc_context.profile = VAProfileH264Main;
  1791.  
  1792.     memset(&use_slot, 0, sizeof(use_slot));
  1793.     switch (avcenc_context.profile) {
  1794.     case VAProfileH264Baseline:
  1795.         avcenc_context.constraint_set_flag |= (1 << 0); /* Annex A.2.1 */
  1796.         break;
  1797.  
  1798.     case VAProfileH264Main:
  1799.         avcenc_context.constraint_set_flag |= (1 << 1); /* Annex A.2.2 */
  1800.         break;
  1801.  
  1802.     case VAProfileH264High:
  1803.         avcenc_context.constraint_set_flag |= (1 << 3); /* Annex A.2.4 */
  1804.         break;
  1805.        
  1806.     default:
  1807.         break;
  1808.     }
  1809.        
  1810.     avcenc_context.seq_param_buf_id = VA_INVALID_ID;
  1811.     avcenc_context.pic_param_buf_id = VA_INVALID_ID;
  1812.     avcenc_context.packed_seq_header_param_buf_id = VA_INVALID_ID;
  1813.     avcenc_context.packed_seq_buf_id = VA_INVALID_ID;
  1814.     avcenc_context.packed_pic_header_param_buf_id = VA_INVALID_ID;
  1815.     avcenc_context.packed_pic_buf_id = VA_INVALID_ID;
  1816.     avcenc_context.codedbuf_buf_id = VA_INVALID_ID;
  1817.     avcenc_context.misc_parameter_hrd_buf_id = VA_INVALID_ID;
  1818.     avcenc_context.codedbuf_i_size = width * height;
  1819.     avcenc_context.codedbuf_pb_size = width * height;
  1820.     avcenc_context.current_input_surface = SID_INPUT_PICTURE_0;
  1821.     avcenc_context.upload_thread_value = -1;
  1822.     avcenc_context.packed_sei_header_param_buf_id = VA_INVALID_ID;
  1823.     avcenc_context.packed_sei_buf_id = VA_INVALID_ID;
  1824.  
  1825.     if (qp_value == -1)
  1826.         avcenc_context.rate_control_method = VA_RC_CBR;
  1827.     else if (qp_value == -2)
  1828.         avcenc_context.rate_control_method = VA_RC_VBR;
  1829.     else {
  1830.         assert(qp_value >= 0 && qp_value <= 51);
  1831.         avcenc_context.rate_control_method = VA_RC_CQP;
  1832.     }
  1833.  
  1834.     for (i = 0; i < MAX_SLICES; i++) {
  1835.         avcenc_context.slice_param_buf_id[i] = VA_INVALID_ID;
  1836.     }
  1837.  
  1838.     avcenc_context_seq_param_init(&avcenc_context.seq_param, width, height);
  1839.     avcenc_context_pic_param_init(&avcenc_context.pic_param);
  1840.     if (avcenc_context.rate_control_method == VA_RC_CBR)
  1841.         avcenc_context_sei_init();
  1842. }
  1843.  
  1844. int main(int argc, char *argv[])
  1845. {
  1846.     int f;
  1847.     FILE *yuv_fp;
  1848.     FILE *avc_fp;
  1849.     off_t file_size;
  1850.     int mode_value;
  1851.     struct timeval tpstart,tpend;
  1852.     float  timeuse;
  1853.  
  1854.     va_init_display_args(&argc, argv);
  1855.  
  1856.     //TODO may be we should using option analytics library
  1857.     if(argc != 5 && argc != 6 && argc != 7) {
  1858.         show_help();
  1859.         return -1;
  1860.     }
  1861.  
  1862.     picture_width = atoi(argv[1]);
  1863.     picture_height = atoi(argv[2]);
  1864.     picture_width_in_mbs = (picture_width + 15) / 16;
  1865.     picture_height_in_mbs = (picture_height + 15) / 16;
  1866.  
  1867.     if (argc == 6 || argc == 7) {
  1868.         qp_value = -1;
  1869.         sscanf(argv[5], "qp=%d", &qp_value);
  1870.         if ( qp_value == -1 ) {
  1871.             frame_bit_rate = -1;
  1872.             sscanf(argv[5], "fb=%d", &frame_bit_rate);
  1873.             if (  frame_bit_rate == -1 ) {
  1874.                 show_help();
  1875.                 return -1;
  1876.             }
  1877.         } else if (qp_value > 51) {
  1878.             qp_value = 51;
  1879.         } else if (qp_value < 0) {
  1880.             qp_value = 0;
  1881.         }
  1882.     } else
  1883.         qp_value = 28;                          //default const QP mode
  1884.  
  1885.     if (argc == 7) {
  1886.         sscanf(argv[6], "mode=%d", &mode_value);
  1887.         if ( mode_value == 0 ) {
  1888.                 ip_period = 0;
  1889.         }
  1890.         else if ( mode_value == 1) {
  1891.                 ip_period = 1;
  1892.         }
  1893.         else if ( mode_value == 2 ) {
  1894.                 /* Hack mechanism before adding the parameter of B-frame number */
  1895.                 ip_period = 3;
  1896.         }
  1897.         else {
  1898.                 printf("mode_value=%d\n",mode_value);
  1899.                 show_help();
  1900.                 return -1;
  1901.         }
  1902.     }
  1903.  
  1904.     yuv_fp = fopen(argv[3],"rb");
  1905.     if ( yuv_fp == NULL){
  1906.         printf("Can't open input YUV file\n");
  1907.         return -1;
  1908.     }
  1909.     fseeko(yuv_fp, (off_t)0, SEEK_END);
  1910.     file_size = ftello(yuv_fp);
  1911.     frame_size = picture_width * picture_height +  ((picture_width * picture_height) >> 1) ;
  1912.  
  1913.     if ( (file_size < frame_size) || (file_size % frame_size) ) {
  1914.         fclose(yuv_fp);
  1915.         printf("The YUV file's size is not correct\n");
  1916.         return -1;
  1917.     }
  1918.     frame_number = file_size / frame_size;
  1919.     fseeko(yuv_fp, (off_t)0, SEEK_SET);
  1920.  
  1921.     avc_fp = fopen(argv[4], "wb");     
  1922.     if ( avc_fp == NULL) {
  1923.         fclose(yuv_fp);
  1924.         printf("Can't open output avc file\n");
  1925.         return -1;
  1926.     }  
  1927.     gettimeofday(&tpstart,NULL);       
  1928.     avcenc_context_init(picture_width, picture_height);
  1929.     create_encode_pipe();
  1930.     alloc_encode_resource(yuv_fp);
  1931.  
  1932.     enc_frame_number = 0;
  1933.     for ( f = 0; f < frame_number; f++) {               //picture level loop
  1934.         unsigned long long next_frame_display;
  1935.         int next_frame_type;
  1936.  
  1937.         enc_frame_number = f;
  1938.  
  1939.         encoding2display_order(enc_frame_number, intra_period, ip_period,
  1940.                                &current_frame_display, &current_frame_type);
  1941.  
  1942.         encoding2display_order(enc_frame_number + 1, intra_period, ip_period,
  1943.                                &next_frame_display, &next_frame_type);
  1944.  
  1945.         if (current_frame_type == FRAME_IDR) {
  1946.             numShortTerm = 0;
  1947.             current_frame_num = 0;
  1948.             memset(&use_slot, 0, sizeof(use_slot));
  1949.             current_IDR_display = current_frame_display;
  1950.             if (avcenc_context.rate_control_method == VA_RC_CBR) {
  1951.                 unsigned long long frame_interval;
  1952.  
  1953.                 frame_interval = enc_frame_number - avcenc_context.idr_frame_num;
  1954.  
  1955.                 /* Based on the H264 spec the removal time of the IDR access
  1956.                  * unit is derived as the following:
  1957.                  * the removal time of previous IDR unit + Tc * cpb_removal_delay(n)
  1958.                  */
  1959.                 avcenc_context.current_cpb_removal = avcenc_context.prev_idr_cpb_removal +
  1960.                                 frame_interval * 2;
  1961.                 avcenc_context.idr_frame_num = enc_frame_number;
  1962.                 avcenc_context.current_idr_cpb_removal = avcenc_context.current_cpb_removal;
  1963.                 if (ip_period)
  1964.                     avcenc_context.current_dpb_removal_delta = (ip_period + 1) * 2;
  1965.                 else
  1966.                     avcenc_context.current_dpb_removal_delta = 2;
  1967.             }
  1968.         } else {
  1969.             if (avcenc_context.rate_control_method == VA_RC_CBR) {
  1970.                 unsigned long long frame_interval;
  1971.  
  1972.                 frame_interval = enc_frame_number - avcenc_context.idr_frame_num;
  1973.  
  1974.                 /* Based on the H264 spec the removal time of the non-IDR access
  1975.                  * unit is derived as the following:
  1976.                  * the removal time of current IDR unit + Tc * cpb_removal_delay(n)
  1977.                  */
  1978.                 avcenc_context.current_cpb_removal = avcenc_context.current_idr_cpb_removal +
  1979.                                 frame_interval * 2;
  1980.                 if (current_frame_type == SLICE_TYPE_I ||
  1981.                     current_frame_type == SLICE_TYPE_P) {
  1982.                     if (ip_period)
  1983.                         avcenc_context.current_dpb_removal_delta = (ip_period + 1) * 2;
  1984.                     else
  1985.                         avcenc_context.current_dpb_removal_delta = 2;
  1986.                 } else
  1987.                    avcenc_context.current_dpb_removal_delta = 2;
  1988.             }
  1989.         }
  1990.  
  1991.         /* use the simple mechanism to calc the POC */
  1992.         current_poc = (current_frame_display - current_IDR_display) * 2;
  1993.  
  1994.         encode_picture(yuv_fp, avc_fp, frame_number, current_frame_display,
  1995.                       (current_frame_type == FRAME_IDR) ? 1 : 0,
  1996.                       (current_frame_type == FRAME_IDR) ? SLICE_TYPE_I : current_frame_type,
  1997.                       (next_frame_type == SLICE_TYPE_B) ? 1 : 0,
  1998.                 next_frame_display);
  1999.         if ((current_frame_type == FRAME_IDR) &&
  2000.             (avcenc_context.rate_control_method == VA_RC_CBR)) {
  2001.            /* after one IDR frame is written, it needs to update the
  2002.             * prev_idr_cpb_removal for next IDR
  2003.             */
  2004.            avcenc_context.prev_idr_cpb_removal = avcenc_context.current_idr_cpb_removal;
  2005.         }
  2006.         printf("\r %d/%d ...", f, frame_number);
  2007.         fflush(stdout);
  2008.     }
  2009.  
  2010.     gettimeofday(&tpend,NULL);
  2011.     timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+ tpend.tv_usec-tpstart.tv_usec;
  2012.     timeuse/=1000000;
  2013.     printf("\ndone!\n");
  2014.     printf("encode %d frames in %f secondes, FPS is %.1f\n",frame_number, timeuse, frame_number/timeuse);
  2015.     release_encode_resource();
  2016.     destory_encode_pipe();
  2017.  
  2018.     fclose(yuv_fp);
  2019.     fclose(avc_fp);
  2020.  
  2021.     return 0;
  2022. }
  2023.