[Libva] [PATCH Intel-FEI 2/2] Add two FEI samples to test/encode/...
Hu, Kelvin
kelvin.hu at intel.com
Mon May 5 00:10:19 PDT 2014
>From c20c45b5a7a798d5727323a0b87c3f9b4e453394 Mon Sep 17 00:00:00 2001
From: Kelvin Hu <kelvin.hu at intel.com>
Date: Mon, 5 May 2014 22:35:32 +0800
Subject: [PATCH 2/2] Add two FEI samples to test/encode/...
The new preenc.c sample for statistics case and feiavcenc.c sample
for fei case
---
test/encode/Makefile.am | 16 +-
test/encode/feiavcenc.c | 2209 +++++++++++++++++++++++++++++++++++++++++++++++
test/encode/preenc.c | 895 +++++++++++++++++++
3 files changed, 3119 insertions(+), 1 deletion(-)
create mode 100644 test/encode/feiavcenc.c
create mode 100644 test/encode/preenc.c
diff --git a/test/encode/Makefile.am b/test/encode/Makefile.am
index 5ddabd1..a5c05d8 100644
--- a/test/encode/Makefile.am
+++ b/test/encode/Makefile.am
@@ -20,7 +20,7 @@
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-bin_PROGRAMS = avcenc mpeg2vaenc h264encode
+bin_PROGRAMS = avcenc feiavcenc preenc mpeg2vaenc h264encode
INCLUDES = \
-Wall \
@@ -42,6 +42,20 @@ avcenc_LDADD = \
$(top_builddir)/test/common/libva-display.la \
-lpthread
+feiavcenc_SOURCES = feiavcenc.c
+feiavcenc_CFLAGS = -I$(top_srcdir)/test/common -g
+feiavcenc_LDADD = \
+ $(top_builddir)/va/libva.la \
+ $(top_builddir)/test/common/libva-display.la \
+ -lpthread
+
+preenc_SOURCES = preenc.c
+preenc_CFLAGS = -I$(top_srcdir)/test/common -g
+preenc_LDADD = \
+ $(top_builddir)/va/libva.la \
+ $(top_builddir)/test/common/libva-display.la \
+ -lpthread
+
mpeg2vaenc_SOURCES = mpeg2vaenc.c
mpeg2vaenc_CFLAGS = -I$(top_srcdir)/test/common
mpeg2vaenc_LDADD = \
diff --git a/test/encode/feiavcenc.c b/test/encode/feiavcenc.c
new file mode 100644
index 0000000..3edb0bd
--- /dev/null
+++ b/test/encode/feiavcenc.c
@@ -0,0 +1,2209 @@
+/*
+ * Simple AVC encoder based on libVA.
+ *
+ * Usage:
+ * ./avcenc <width> <height> <input file> <output file> [qp]
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include <X11/Xlib.h>
+
+#include <unistd.h>
+
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <assert.h>
+#include <time.h>
+
+#include <pthread.h>
+
+#include <va/va.h>
+#include <va/va_enc_h264.h>
+#include <va/vendor/intel/va_intel_fei.h>
+#include <va/vendor/intel/va_intel_statistics.h>
+#include <va/va_x11.h>
+# include "va/drm/va_drm.h"
+
+#define NAL_REF_IDC_NONE 0
+#define NAL_REF_IDC_LOW 1
+#define NAL_REF_IDC_MEDIUM 2
+#define NAL_REF_IDC_HIGH 3
+
+#define NAL_NON_IDR 1
+#define NAL_IDR 5
+#define NAL_SPS 7
+#define NAL_PPS 8
+
+
+#define SLICE_TYPE_P 0
+#define SLICE_TYPE_B 1
+#define SLICE_TYPE_I 2
+
+#define ENTROPY_MODE_CAVLC 0
+#define ENTROPY_MODE_CABAC 1
+
+#define PROFILE_IDC_BASELINE 66
+#define PROFILE_IDC_MAIN 77
+#define PROFILE_IDC_HIGH 100
+
+#define CHECK_VASTATUS(va_status,func) \
+ if (va_status != VA_STATUS_SUCCESS) { \
+ fprintf(stderr,"%s:%s (%d) failed,exit\n", __func__, func, __LINE__); \
+ exit(1); \
+ }
+
+static Display *x11_display;
+static VADisplay va_dpy;
+
+static int picture_width, picture_width_in_mbs;
+static int picture_height, picture_height_in_mbs;
+static int frame_size;
+static unsigned char *newImageBuffer = 0;
+static VAEncFEIMBControlH264Intel *feiMbCtrlBuffer = 0;
+static VAEncMVPredictorH264Intel *feiMvPredBuffer = 0;
+
+static int qp_value = 26;
+static Bool stats_en = 0;
+
+static int intra_period = 30;
+static int ip_period = 1;
+static int pb_period = 5;
+static int frame_bit_rate = -1;
+
+static int next_input_id = -1;
+#define MAX_SLICES 32
+
+signed int Cur_POC[2];
+signed int ref0_POC[2];
+signed int ref1_POC[2];
+static int
+build_packed_pic_buffer(unsigned char **header_buffer);
+
+static int
+build_packed_seq_buffer(unsigned char **header_buffer);
+
+int
+build_packed_slc_buffer(unsigned char **header_buffer, int frame_num, int display_frame, int slice_type, int is_idr);
+
+
+static int avcenc_destroy_buffers(VABufferID *va_buffers, unsigned int num_va_buffers);
+
+struct upload_thread_param
+{
+ FILE *yuv_fp;
+ VASurfaceID surface_id;
+};
+
+static void
+upload_yuv_to_surface(FILE *yuv_fp, VASurfaceID surface_id);
+
+static struct {
+ VAProfile profile;
+ VAEncSequenceParameterBufferH264 seq_param;
+ VAEncPictureParameterBufferH264 pic_param;
+ VAEncSliceParameterBufferH264 slice_param[MAX_SLICES];
+ VAContextID context_id;
+ VAConfigID config_id;
+ VABufferID seq_param_buf_id; /* Sequence level parameter */
+ VABufferID pic_param_buf_id; /* Picture level parameter */
+ VABufferID slice_param_buf_id[MAX_SLICES]; /* Slice level parameter, multil slices */
+ VABufferID codedbuf_buf_id; /* Output buffer, compressed data */
+ VABufferID fei_distortion_buf_id; /* Output buffer, distortion data */
+ VABufferID fei_mv_buf_id; /* Output buffer, mv data */
+ VABufferID fei_mbcode_buf_id; /* Output buffer, mb code data */
+ VABufferID packed_seq_header_param_buf_id;
+ VABufferID packed_seq_buf_id;
+ VABufferID packed_pic_header_param_buf_id;
+ VABufferID packed_pic_buf_id;
+ VABufferID packed_slc_header_param_buf_id;
+ VABufferID packed_slc_buf_id;
+
+ VABufferID misc_parameter_hrd_buf_id;
+ VABufferID misc_parameter_rc_buf_id;
+ VABufferID misc_parameter_fr_buf_id;
+ // FEI buffer
+ VAEncMiscParameterFEIFrameControlH264Intel fei_pic_param;
+ VABufferID misc_parameter_fei_pic_control_buf_id;
+ VABufferID fei_mbctrl_buf_id;
+ VABufferID fei_mvpred_buf_id;
+ int fei_mbctrl_buf_size;
+ int fei_mvpred_buf_size;
+
+ int num_slices;
+ int codedbuf_i_size;
+ int codedbuf_pb_size;
+ int current_input_surface;
+ int rate_control_method;
+ struct upload_thread_param upload_thread_param;
+ pthread_t upload_thread_id;
+ int upload_thread_value;
+} avcenc_context;
+
+static int drm_fd = -1;
+
+static void create_encode_pipe()
+{
+ VAEntrypoint entrypoints[5];
+ int num_entrypoints,slice_entrypoint;
+ VAConfigAttrib attrib[2];
+ int major_ver, minor_ver;
+ VAStatus va_status;
+
+ drm_fd = open("/dev/dri/card0", O_RDWR);
+ if (drm_fd < 0)
+ {
+ fprintf(stderr, "error: can't open DRM connection!\n");
+ return NULL;
+ }
+ va_dpy = vaGetDisplayDRM(drm_fd);
+ //x11_display = XOpenDisplay(":0.0");
+ //assert(x11_display);
+
+ //va_dpy = vaGetDisplay(x11_display);
+ va_status = vaInitialize(va_dpy, &major_ver, &minor_ver);
+ CHECK_VASTATUS(va_status, "vaInitialize");
+
+ vaQueryConfigEntrypoints(va_dpy, avcenc_context.profile, entrypoints,
+ &num_entrypoints);
+
+ for (slice_entrypoint = 0; slice_entrypoint < num_entrypoints; slice_entrypoint++) {
+ if (entrypoints[slice_entrypoint] == VAEntrypointEncFEIIntel)
+ break;
+ }
+
+ if (slice_entrypoint == num_entrypoints) {
+ /* not find Slice entry point */
+ assert(0);
+ }
+
+ /* find out the format for the render target, and rate control mode */
+ attrib[0].type = VAConfigAttribRTFormat;
+ attrib[1].type = VAConfigAttribRateControl;
+ attrib[2].type = VAConfigAttribEncFunctionTypeIntel;
+ attrib[3].type = VAConfigAttribEncMVPredictorsIntel;
+ vaGetConfigAttributes(va_dpy, avcenc_context.profile, VAEntrypointEncFEIIntel,
+ &attrib[0], 4);
+
+ if ((attrib[0].value & VA_RT_FORMAT_YUV420) == 0) {
+ /* not find desired YUV420 RT format */
+ assert(0);
+ }
+
+ if ((attrib[1].value & avcenc_context.rate_control_method) == 0) {
+ /* Can't find matched RC mode */
+ printf("Can't find the desired RC mode, exit\n");
+ assert(0);
+ }
+
+ attrib[0].value = VA_RT_FORMAT_YUV420; /* set to desired RT format */
+ attrib[1].value = avcenc_context.rate_control_method; /* set to desired RC mode */
+ attrib[2].value = VA_ENC_FUNCTION_ENC_PAK_INTEL; /* set FEI function to ENC_PAK */
+ attrib[3].value = 1; /* set 0 MV Predictor */
+
+ va_status = vaCreateConfig(va_dpy, avcenc_context.profile, VAEntrypointEncFEIIntel,
+ &attrib[0], 4, &avcenc_context.config_id);
+ CHECK_VASTATUS(va_status, "vaCreateConfig");
+
+
+}
+
+static void destory_encode_pipe()
+{
+ vaDestroyContext(va_dpy,avcenc_context.context_id);
+ vaDestroyConfig(va_dpy,avcenc_context.config_id);
+ vaTerminate(va_dpy);
+ if (drm_fd < 0)
+ return;
+ close(drm_fd);
+ drm_fd = -1;
+ //XCloseDisplay(x11_display);
+}
+
+/***************************************************
+ *
+ * The encode pipe resource define
+ *
+ ***************************************************/
+#define SID_INPUT_PICTURE_0 0
+#define SID_INPUT_PICTURE_1 1
+#define SID_INPUT_PICTURE_2 5 //Bo
+#define SID_INPUT_PICTURE_3 6
+#define SID_INPUT_PICTURE_4 7
+
+#define SID_REFERENCE_PICTURE_L0 2
+#define SID_REFERENCE_PICTURE_L1 3
+#define SID_RECON_PICTURE 4
+#define SID_NUMBER SID_RECON_PICTURE + 1 + 3
+static VASurfaceID surface_ids[SID_NUMBER];
+
+static int frame_number;
+static int enc_frame_number;
+
+/***************************************************/
+
+static void *
+upload_thread_function(void *data)
+{
+ struct upload_thread_param *param = data;
+
+ upload_yuv_to_surface(param->yuv_fp, param->surface_id);
+
+ return NULL;
+}
+
+static void alloc_encode_resource(FILE *yuv_fp)
+{
+ VAStatus va_status;
+
+ // Create surface
+ va_status = vaCreateSurfaces(
+ va_dpy,
+ VA_RT_FORMAT_YUV420, picture_width, picture_height,
+ &surface_ids[0], SID_NUMBER,
+ NULL, 0
+ );
+
+ CHECK_VASTATUS(va_status, "vaCreateSurfaces");
+
+ /* Create a context for this decode pipe */
+ va_status = vaCreateContext(va_dpy, avcenc_context.config_id,
+ picture_width, picture_height,
+ VA_PROGRESSIVE,
+ &surface_ids[0],
+ SID_NUMBER,
+ &avcenc_context.context_id);
+ CHECK_VASTATUS(va_status, "vaCreateContext");
+ newImageBuffer = (unsigned char *)malloc(frame_size);
+ feiMbCtrlBuffer = (VAEncFEIMBControlH264Intel *)malloc(avcenc_context.fei_mbctrl_buf_size);
+ feiMvPredBuffer = (VAEncMVPredictorH264Intel *)malloc(avcenc_context.fei_mvpred_buf_size);
+
+ /* firstly upload YUV data to SID_INPUT_PICTURE_1 */
+ avcenc_context.upload_thread_param.yuv_fp = yuv_fp;
+ avcenc_context.upload_thread_param.surface_id = surface_ids[SID_INPUT_PICTURE_1];
+ next_input_id = SID_INPUT_PICTURE_1;
+#if 0
+ avcenc_context.upload_thread_value = pthread_create(&avcenc_context.upload_thread_id,
+ NULL,
+ upload_thread_function,
+ (void*)&avcenc_context.upload_thread_param);
+//#else
+ upload_thread_function((void *)(&avcenc_context.upload_thread_param));
+#endif
+}
+
+static void release_encode_resource()
+{
+#if 0
+ pthread_join(avcenc_context.upload_thread_id, NULL);
+#endif
+ avcenc_destroy_buffers(&avcenc_context.packed_seq_header_param_buf_id, 1);
+ avcenc_destroy_buffers(&avcenc_context.packed_seq_buf_id, 1);
+ //avcenc_destroy_buffers(&avcenc_context.packed_pic_header_param_buf_id, 1);
+ //avcenc_destroy_buffers(&avcenc_context.packed_pic_buf_id, 1);
+ //avcenc_destroy_buffers(&avcenc_context.packed_slc_header_param_buf_id, 1);
+ //avcenc_destroy_buffers(&avcenc_context.packed_slc_buf_id, 1);
+
+ free(newImageBuffer);
+ free(feiMbCtrlBuffer);
+ free(feiMvPredBuffer);
+
+ // Release all the surfaces resource
+ vaDestroySurfaces(va_dpy, &surface_ids[0], SID_NUMBER);
+}
+
+static void avcenc_update_picture_parameter(int slice_type, int frame_num, int display_num, int is_idr)
+{
+ VAEncPictureParameterBufferH264 *pic_param;
+ VAStatus va_status;
+ int i;
+ // Picture level
+ unsigned int length_in_bits, offset_in_bytes;
+ VAEncPackedHeaderParameterBuffer packed_header_param_buffer;
+ unsigned char *packed_pic_buffer = NULL;
+ pic_param = &avcenc_context.pic_param;
+ pic_param->CurrPic.picture_id = surface_ids[SID_RECON_PICTURE];
+ pic_param->CurrPic.flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
+ pic_param->CurrPic.TopFieldOrderCnt = display_num * 2;
+ pic_param->CurrPic.BottomFieldOrderCnt = display_num * 2+1;
+ Cur_POC[0] = display_num * 2;
+ Cur_POC[1] = display_num * 2 + 1;
+ if (slice_type == SLICE_TYPE_I)
+ {
+ for (i=0; i<16; i++)
+ {
+ pic_param->ReferenceFrames[i].picture_id = VA_INVALID_ID;
+ pic_param->ReferenceFrames[i].flags = VA_PICTURE_H264_INVALID;
+ }
+ }
+ else
+ {
+ pic_param->ReferenceFrames[0].picture_id = surface_ids[SID_REFERENCE_PICTURE_L0];
+ pic_param->ReferenceFrames[0].flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
+ pic_param->ReferenceFrames[0].TopFieldOrderCnt = ref0_POC[0];
+ pic_param->ReferenceFrames[0].BottomFieldOrderCnt = ref0_POC[1];
+ if (slice_type != SLICE_TYPE_B)
+ {
+ pic_param->ReferenceFrames[1].picture_id = VA_INVALID_ID;//surface_ids[SID_REFERENCE_PICTURE_L0];
+ pic_param->ReferenceFrames[1].flags = VA_PICTURE_H264_INVALID;
+ }
+ else
+ {
+ pic_param->ReferenceFrames[1].picture_id = surface_ids[SID_REFERENCE_PICTURE_L1];
+ pic_param->ReferenceFrames[1].flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
+ pic_param->ReferenceFrames[1].TopFieldOrderCnt = ref1_POC[0];
+ pic_param->ReferenceFrames[1].BottomFieldOrderCnt = ref1_POC[1];
+ }
+ for (i=2; i<16; i++)
+ {
+ pic_param->ReferenceFrames[i].picture_id = VA_INVALID_ID;
+ pic_param->ReferenceFrames[i].flags = VA_PICTURE_H264_INVALID;
+ }
+ }
+ assert(avcenc_context.codedbuf_buf_id != VA_INVALID_ID);
+ pic_param->coded_buf = avcenc_context.codedbuf_buf_id;
+ pic_param->frame_num = frame_num;
+ pic_param->pic_fields.bits.idr_pic_flag = !!is_idr;
+ pic_param->pic_fields.bits.reference_pic_flag = (slice_type != SLICE_TYPE_B);
+
+ va_status = vaCreateBuffer(va_dpy,
+ avcenc_context.context_id,
+ VAEncPictureParameterBufferType,
+ sizeof(*pic_param), 1, pic_param,
+ &avcenc_context.pic_param_buf_id);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");
+
+ length_in_bits = build_packed_pic_buffer(&packed_pic_buffer);
+ offset_in_bytes = 0;
+ packed_header_param_buffer.type = VAEncPackedHeaderPicture;
+ packed_header_param_buffer.bit_length = length_in_bits;
+ packed_header_param_buffer.has_emulation_bytes = 0;
+
+ va_status = vaCreateBuffer(va_dpy,
+ avcenc_context.context_id,
+ VAEncPackedHeaderParameterBufferType,
+ sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
+ &avcenc_context.packed_pic_header_param_buf_id);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");
+
+ va_status = vaCreateBuffer(va_dpy,
+ avcenc_context.context_id,
+ VAEncPackedHeaderDataBufferType,
+ (length_in_bits + 7) / 8, 1, packed_pic_buffer,
+ &avcenc_context.packed_pic_buf_id);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");
+
+
+}
+
+#ifndef VA_FOURCC_I420
+#define VA_FOURCC_I420 0x30323449
+#endif
+
+static void upload_yuv_to_surface(FILE *yuv_fp, VASurfaceID surface_id)
+{
+ VAImage surface_image;
+ VAStatus va_status;
+ void *surface_p = NULL;
+ unsigned char *y_src, *u_src, *v_src;
+ unsigned char *y_dst, *u_dst, *v_dst;
+ int y_size = picture_width * picture_height;
+ int u_size = (picture_width >> 1) * (picture_height >> 1);
+ int row, col;
+ size_t n_items;
+
+ do {
+ n_items = fread(newImageBuffer, frame_size, 1, yuv_fp);
+ } while (n_items != 1);
+
+ va_status = vaDeriveImage(va_dpy, surface_id, &surface_image);
+ CHECK_VASTATUS(va_status,"vaDeriveImage");
+
+ vaMapBuffer(va_dpy, surface_image.buf, &surface_p);
+ assert(VA_STATUS_SUCCESS == va_status);
+
+ y_src = newImageBuffer;
+ u_src = newImageBuffer + y_size; /* UV offset for NV12 */
+ v_src = newImageBuffer + y_size + u_size;
+
+ y_dst = surface_p + surface_image.offsets[0];
+ u_dst = surface_p + surface_image.offsets[1]; /* UV offset for NV12 */
+ v_dst = surface_p + surface_image.offsets[2];
+
+ /* Y plane */
+ for (row = 0; row < surface_image.height; row++) {
+ memcpy(y_dst, y_src, surface_image.width);
+ y_dst += surface_image.pitches[0];
+ y_src += picture_width;
+ }
+
+ if (surface_image.format.fourcc == VA_FOURCC_NV12) { /* UV plane */
+ for (row = 0; row < surface_image.height / 2; row++) {
+ for (col = 0; col < surface_image.width / 2; col++) {
+ u_dst[col * 2] = u_src[col];
+ u_dst[col * 2 + 1] = v_src[col];
+ }
+
+ u_dst += surface_image.pitches[1];
+ u_src += (picture_width / 2);
+ v_src += (picture_width / 2);
+ }
+ } else if (surface_image.format.fourcc == VA_FOURCC_YV12 ||
+ surface_image.format.fourcc == VA_FOURCC_I420) {
+ const int U = surface_image.format.fourcc == VA_FOURCC_I420 ? 1 : 2;
+ const int V = surface_image.format.fourcc == VA_FOURCC_I420 ? 2 : 1;
+
+ u_dst = surface_p + surface_image.offsets[U];
+ v_dst = surface_p + surface_image.offsets[V];
+
+ for (row = 0; row < surface_image.height / 2; row++) {
+ memcpy(u_dst, u_src, surface_image.width / 2);
+ memcpy(v_dst, v_src, surface_image.width / 2);
+ u_dst += surface_image.pitches[U];
+ v_dst += surface_image.pitches[V];
+ u_src += (picture_width / 2);
+ v_src += (picture_width / 2);
+ }
+ }
+
+ vaUnmapBuffer(va_dpy, surface_image.buf);
+ vaDestroyImage(va_dpy, surface_image.image_id);
+}
+
+static void avcenc_update_slice_parameter(int slice_type, int display_num)
+{
+ VAEncSliceParameterBufferH264 *slice_param;
+ VAStatus va_status;
+ int i,j;
+
+ // Slice level
+ i = 0;
+ slice_param = &avcenc_context.slice_param[i];
+ slice_param->macroblock_address = 0;
+ slice_param->num_macroblocks = picture_height_in_mbs * picture_width_in_mbs;
+ slice_param->pic_parameter_set_id = 0;
+ slice_param->slice_type = slice_type;
+ slice_param->pic_order_cnt_lsb = display_num * 2;
+ slice_param->direct_spatial_mv_pred_flag = 1; // to match header NALU
+
+ slice_param->num_ref_idx_l0_active_minus1 = 0;
+ slice_param->num_ref_idx_l1_active_minus1 = 0;
+
+ if ( slice_type == SLICE_TYPE_I ) {
+ slice_param->num_ref_idx_l0_active_minus1 = -1;
+ slice_param->num_ref_idx_l1_active_minus1 = -1;
+ } else if ( slice_type == SLICE_TYPE_P ) {
+ slice_param->num_ref_idx_l0_active_minus1 = 0;
+ slice_param->num_ref_idx_l1_active_minus1 = -1;
+ } else if ( slice_type == SLICE_TYPE_B ) {
+ slice_param->num_ref_idx_l0_active_minus1 = 0;
+ slice_param->num_ref_idx_l1_active_minus1 = 0;
+ }
+
+ slice_param->cabac_init_idc = 0;
+ slice_param->slice_qp_delta = 0;
+ slice_param->disable_deblocking_filter_idc = 0;
+ slice_param->slice_alpha_c0_offset_div2 = 2;
+ slice_param->slice_beta_offset_div2 = 2;
+ slice_param->idr_pic_id = 0;
+ /* */
+ if ( slice_type == SLICE_TYPE_I )
+ {
+ slice_param->RefPicList0[0].picture_id = VA_INVALID_ID;
+ slice_param->RefPicList0[0].flags = VA_PICTURE_H264_INVALID;
+ }
+ else
+ {
+ slice_param->RefPicList0[0].picture_id = surface_ids[SID_REFERENCE_PICTURE_L0];
+ slice_param->RefPicList0[0].flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
+ }
+ for (j=1; j<32; j++)
+ {
+ slice_param->RefPicList0[j].picture_id = VA_INVALID_ID;
+ slice_param->RefPicList0[j].flags = VA_PICTURE_H264_INVALID;
+ }
+ for (j=0; j<32; j++)
+ {
+ slice_param->RefPicList1[j].picture_id = VA_INVALID_ID;
+ slice_param->RefPicList1[j].flags = VA_PICTURE_H264_INVALID;
+ }
+ if ( slice_type == SLICE_TYPE_B )
+ {
+ slice_param->RefPicList1[0].picture_id = surface_ids[SID_REFERENCE_PICTURE_L1];
+ slice_param->RefPicList1[0].flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE;
+
+ }
+ //slice_param->RefPicList1[0].picture_id = surface_ids[SID_REFERENCE_PICTURE_L1];
+ /* FIXME: fill other fields */
+
+ va_status = vaCreateBuffer(va_dpy,
+ avcenc_context.context_id,
+ VAEncSliceParameterBufferType,
+ sizeof(*slice_param), 1, slice_param,
+ &avcenc_context.slice_param_buf_id[i]);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");;
+ i++;
+
+#if 0
+ slice_param = &avcenc_context.slice_param[i];
+ slice_param->macroblock_address = picture_height_in_mbs * picture_width_in_mbs / 2;
+ slice_param->num_macroblocks = picture_height_in_mbs * picture_width_in_mbs / 2;
+ slice_param->pic_parameter_set_id = 0;
+ slice_param->slice_type = slice_type;
+ slice_param->direct_spatial_mv_pred_flag = 0;
+ slice_param->num_ref_idx_l0_active_minus1 = 0; /* FIXME: ??? */
+ slice_param->num_ref_idx_l1_active_minus1 = 0;
+ slice_param->cabac_init_idc = 0;
+ slice_param->slice_qp_delta = 0;
+ slice_param->disable_deblocking_filter_idc = 0;
+ slice_param->slice_alpha_c0_offset_div2 = 2;
+ slice_param->slice_beta_offset_div2 = 2;
+ slice_param->idr_pic_id = 0;
+
+ /* FIXME: fill other fields */
+
+ va_status = vaCreateBuffer(va_dpy,
+ avcenc_context.context_id,
+ VAEncSliceParameterBufferType,
+ sizeof(*slice_param), 1, slice_param,
+ &avcenc_context.slice_param_buf_id[i]);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");;
+ i++;
+#endif
+
+ avcenc_context.num_slices = i;
+}
+
+static int begin_picture(FILE *yuv_fp, int frame_num, int display_num, int slice_type, int is_idr)
+{
+ VAStatus va_status;
+ VAEncPackedHeaderParameterBuffer packed_header_param_buffer;
+ unsigned int length_in_bits, offset_in_bytes;
+ unsigned char *packed_seq_buffer = NULL, *packed_slc_buffer=NULL;
+
+#if 0
+ if (avcenc_context.upload_thread_value != 0) {
+ fprintf(stderr, "FATAL error!!!\n");
+ exit(1);
+ }
+
+ pthread_join(avcenc_context.upload_thread_id, NULL);
+#endif
+ avcenc_context.upload_thread_value = -1;
+/*
+ if (avcenc_context.current_input_surface == SID_INPUT_PICTURE_0)
+ avcenc_context.current_input_surface = SID_INPUT_PICTURE_1;
+ else if (avcenc_context.current_input_surface == SID_INPUT_PICTURE_1)
+ avcenc_context.current_input_surface = SID_INPUT_PICTURE_2;
+ else if (avcenc_context.current_input_surface == SID_INPUT_PICTURE_2)
+ avcenc_context.current_input_surface = SID_INPUT_PICTURE_3;
+ else if (avcenc_context.current_input_surface == SID_INPUT_PICTURE_3)
+ avcenc_context.current_input_surface = SID_INPUT_PICTURE_4;
+ else
+ avcenc_context.current_input_surface = SID_INPUT_PICTURE_0;
+*/
+ avcenc_context.current_input_surface = next_input_id;
+ if (frame_num == 0) {
+
+ assert(slice_type == SLICE_TYPE_I);
+ length_in_bits = build_packed_seq_buffer(&packed_seq_buffer);
+ offset_in_bytes = 0;
+ packed_header_param_buffer.type = VAEncPackedHeaderSequence;
+ packed_header_param_buffer.bit_length = length_in_bits;
+ packed_header_param_buffer.has_emulation_bytes = 0;
+ va_status = vaCreateBuffer(va_dpy,
+ avcenc_context.context_id,
+ VAEncPackedHeaderParameterBufferType,
+ sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
+ &avcenc_context.packed_seq_header_param_buf_id);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");
+
+ va_status = vaCreateBuffer(va_dpy,
+ avcenc_context.context_id,
+ VAEncPackedHeaderDataBufferType,
+ (length_in_bits + 7) / 8, 1, packed_seq_buffer,
+ &avcenc_context.packed_seq_buf_id);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");
+
+
+ free(packed_seq_buffer);
+ }
+
+ /* sequence parameter set */
+ VAEncSequenceParameterBufferH264 *seq_param = &avcenc_context.seq_param;
+ va_status = vaCreateBuffer(va_dpy,
+ avcenc_context.context_id,
+ VAEncSequenceParameterBufferType,
+ sizeof(*seq_param), 1, seq_param,
+ &avcenc_context.seq_param_buf_id);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");
+
+ /* hrd parameter */
+ VAEncMiscParameterBuffer *misc_param;
+ VAEncMiscParameterHRD *misc_hrd_param;
+ vaCreateBuffer(va_dpy,
+ avcenc_context.context_id,
+ VAEncMiscParameterBufferType,
+ sizeof(VAEncMiscParameterBuffer) + sizeof(VAEncMiscParameterHRD),
+ 1,
+ NULL,
+ &avcenc_context.misc_parameter_hrd_buf_id);
+ CHECK_VASTATUS(va_status, "vaCreateBuffer");
+
+ vaMapBuffer(va_dpy,
+ avcenc_context.misc_parameter_hrd_buf_id,
+ (void **)&misc_param);
+ misc_param->type = VAEncMiscParameterTypeHRD;
+ misc_hrd_param = (VAEncMiscParameterHRD *)misc_param->data;
+
+ if (frame_bit_rate > 0) {
+ misc_hrd_param->initial_buffer_fullness = frame_bit_rate * 1000 * 4;
+ misc_hrd_param->buffer_size = frame_bit_rate * 1000 * 8;
+ } else {
+ misc_hrd_param->initial_buffer_fullness = 0;
+ misc_hrd_param->buffer_size = 0;
+ }
+
+ vaUnmapBuffer(va_dpy, avcenc_context.misc_parameter_hrd_buf_id);
+#if 0
+ /* rate control parameter */
+ VAEncMiscParameterRateControl *misc_rc_param;
+ vaCreateBuffer(va_dpy,
+ avcenc_context.context_id,
+ VAEncMiscParameterBufferType,
+ sizeof(VAEncMiscParameterBuffer) + sizeof(VAEncMiscParameterRateControl),
+ 1,
+ NULL,
+ &avcenc_context.misc_parameter_rc_buf_id);
+ CHECK_VASTATUS(va_status, "vaCreateBuffer");
+
+ vaMapBuffer(va_dpy,
+ avcenc_context.misc_parameter_rc_buf_id,
+ (void **)&misc_param);
+ misc_param->type = VAEncMiscParameterTypeRateControl;
+ misc_rc_param = (VAEncMiscParameterRateControl *)misc_param->data;
+
+ if (frame_bit_rate > 0) {
+ misc_rc_param->bits_per_second = frame_bit_rate * 1000;
+ misc_rc_param->target_percentage = 95;
+ misc_rc_param->window_size = 4000;
+ misc_rc_param->initial_qp = 26;
+ misc_rc_param->min_qp = 4;
+ } else {
+ misc_rc_param->bits_per_second = 0;
+ misc_rc_param->target_percentage = 0;
+ misc_rc_param->window_size = 0;
+ misc_rc_param->initial_qp = 0;
+ misc_rc_param->min_qp = 0;
+ }
+
+ vaUnmapBuffer(va_dpy, avcenc_context.misc_parameter_rc_buf_id);
+#endif
+ /* frame rate parameter */
+ VAEncMiscParameterFrameRate *misc_fr_param;
+ vaCreateBuffer(va_dpy,
+ avcenc_context.context_id,
+ VAEncMiscParameterBufferType,
+ sizeof(VAEncMiscParameterBuffer) + sizeof(VAEncMiscParameterFrameRate),
+ 1,
+ NULL,
+ &avcenc_context.misc_parameter_fr_buf_id);
+ CHECK_VASTATUS(va_status, "vaCreateBuffer");
+
+ vaMapBuffer(va_dpy,
+ avcenc_context.misc_parameter_fr_buf_id,
+ (void **)&misc_param);
+ misc_param->type = VAEncMiscParameterTypeFrameRate;
+ misc_fr_param = (VAEncMiscParameterRateControl*)misc_param->data;
+ misc_fr_param->framerate = 30*100;
+
+ vaUnmapBuffer(va_dpy, avcenc_context.misc_parameter_fr_buf_id);
+
+ /* FEI picture control parameter */
+ VAEncMiscParameterFEIFrameControlH264Intel *misc_fei_pic_control_param;
+ vaCreateBuffer(va_dpy,
+ avcenc_context.context_id,
+ VAEncMiscParameterBufferType,
+ sizeof(VAEncMiscParameterBuffer) + sizeof(VAEncMiscParameterFEIFrameControlH264Intel),
+ 1,
+ NULL,
+ &avcenc_context.misc_parameter_fei_pic_control_buf_id);
+ CHECK_VASTATUS(va_status, "vaCreateBuffer");
+
+ vaMapBuffer(va_dpy,
+ avcenc_context.misc_parameter_fei_pic_control_buf_id,
+ (void **)&misc_param);
+ misc_param->type = VAEncMiscParameterTypeFEIFrameControlIntel;
+ misc_fei_pic_control_param = (VAEncMiscParameterFEIFrameControlH264Intel*)misc_param->data;
+ if(avcenc_context.fei_pic_param.mb_input)
+ {
+#if 0
+ FILE *mbctrl_fp;
+ int data_amount;
+ int read_amount;
+ mbctrl_fp = fopen( "mbctrlin.bin", "rb" );
+ if ( ! mbctrl_fp ) {
+ printf( "Error: No input MB control file was found\n" );
+ exit(-1);
+ }
+ data_amount = avcenc_context.fei_mbctrl_buf_size;
+ read_amount = fread( feiMbCtrlBuffer, 1, data_amount, mbctrl_fp );
+ if( data_amount != read_amount )
+ {
+ printf( "Error: read input MVs file failed\n" );
+ exit(-1);
+ }
+#else
+ VAEncFEIMBControlH264Intel *pFeiMbCtrl = feiMbCtrlBuffer;
+ int i = 0;
+ memset(pFeiMbCtrl, 0, avcenc_context.fei_mbctrl_buf_size);
+
+ for(i = 0; i<(picture_width_in_mbs*picture_height_in_mbs); i++)
+ {
+ pFeiMbCtrl->force_to_intra = 0;
+ pFeiMbCtrl->force_to_skip = 0;
+
+ pFeiMbCtrl->target_size_in_word = 0xff;
+ pFeiMbCtrl->max_size_in_word = 80;
+ pFeiMbCtrl++;
+ }
+ if(0)
+ {
+ FILE *out;
+ int ret = 0;
+ out = fopen("mbctrlin.bin", "wb");
+ ret = fwrite(feiMbCtrlBuffer, 1, avcenc_context.fei_mbctrl_buf_size, out);
+ fclose(out);
+ }
+#endif
+ va_status = vaCreateBuffer(va_dpy,
+ avcenc_context.context_id,
+ VAEncFEIMBControlBufferTypeIntel,
+ sizeof(VAEncFEIMBControlH264Intel)*picture_width_in_mbs*picture_height_in_mbs, 1, feiMbCtrlBuffer,
+ &avcenc_context.fei_mbctrl_buf_id);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");
+ avcenc_context.fei_pic_param.mb_ctrl = avcenc_context.fei_mbctrl_buf_id;
+ }
+ if(avcenc_context.fei_pic_param.mv_predictor_enable)
+ {
+#if 0
+ // FIXME get FEI MV Predictor from file input to feiMvPredBuffer first
+ FILE *mvpred_fp;
+ int data_amount;
+ int read_amount;
+ mvpred_fp = fopen( "mvpindata0.bin", "rb" );
+ if ( ! mvpred_fp ) {
+ printf( "Error: No input MVs file was found\n" );
+ exit(-1);
+ }
+ data_amount = avcenc_context.fei_mvpred_buf_size;
+ read_amount = fread( feiMvPredBuffer, 1, data_amount, mvpred_fp );
+ if( data_amount != read_amount )
+ {
+ printf( "Error: read input MVs file failed\n" );
+ exit(-1);
+ }
+#else
+ VAEncMVPredictorH264Intel *pMvPred = feiMvPredBuffer;
+ int i = 0;
+ memset(pMvPred, 0, avcenc_context.fei_mvpred_buf_size);
+
+ for(i = 0; i<(picture_width_in_mbs*picture_height_in_mbs); i++)
+ {
+ pMvPred->mv[0].mv0[0] = 1;
+ pMvPred->mv[1].mv0[0] = 1;
+ pMvPred++;
+ }
+ if(0)
+ {
+ FILE *out;
+ int ret = 0;
+ out = fopen("mvpindata.bin", "wb");
+ ret = fwrite(feiMvPredBuffer, 1, avcenc_context.fei_mvpred_buf_size, out);
+ fclose(out);
+ }
+#endif
+ va_status = vaCreateBuffer(va_dpy,
+ avcenc_context.context_id,
+ VAEncFEIMVPredictorBufferTypeIntel,
+ sizeof(VAEncMVPredictorH264Intel)*picture_width_in_mbs*picture_height_in_mbs, 1, feiMvPredBuffer,
+ &avcenc_context.fei_mvpred_buf_id);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");
+ avcenc_context.fei_pic_param.mv_predictor = avcenc_context.fei_mvpred_buf_id;
+ }
+ /* distortion buffer */
+ if(stats_en)
+ {
+ va_status = vaCreateBuffer(va_dpy,
+ avcenc_context.context_id,
+ VAEncFEIDistortionBufferTypeIntel,
+ sizeof(VAEncFEIDistortionBufferH264Intel)*picture_width_in_mbs*picture_height_in_mbs, 1, NULL,
+ &avcenc_context.fei_distortion_buf_id);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");
+ avcenc_context.fei_pic_param.distortion = avcenc_context.fei_distortion_buf_id;
+
+ va_status = vaCreateBuffer(va_dpy,
+ avcenc_context.context_id,
+ VAEncFEIMVBufferTypeIntel,
+ sizeof(VAMotionVectorIntel)*16*picture_width_in_mbs*picture_height_in_mbs, 1, NULL,
+ &avcenc_context.fei_mv_buf_id);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");
+ avcenc_context.fei_pic_param.mv_data = avcenc_context.fei_mv_buf_id;
+
+ va_status = vaCreateBuffer(va_dpy,
+ avcenc_context.context_id,
+ VAEncFEIModeBufferTypeIntel,
+ sizeof(VAEncFEIModeBufferH264Intel)*picture_width_in_mbs*picture_height_in_mbs, 1, NULL,
+ &avcenc_context.fei_mbcode_buf_id);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");
+ avcenc_context.fei_pic_param.mb_code_data = avcenc_context.fei_mbcode_buf_id;
+
+ }
+ *misc_fei_pic_control_param = avcenc_context.fei_pic_param;
+
+ vaUnmapBuffer(va_dpy, avcenc_context.misc_parameter_fei_pic_control_buf_id);
+
+ /* slice parameter */
+ avcenc_update_slice_parameter(slice_type,display_num);
+
+ length_in_bits = build_packed_slc_buffer(&packed_slc_buffer,frame_num, display_num, slice_type, is_idr);
+ offset_in_bytes = 0;
+ packed_header_param_buffer.type = VAEncPackedHeaderSlice;
+ packed_header_param_buffer.bit_length = length_in_bits;
+ packed_header_param_buffer.has_emulation_bytes = 0;
+
+ va_status = vaCreateBuffer(va_dpy,
+ avcenc_context.context_id,
+ VAEncPackedHeaderParameterBufferType,
+ sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
+ &avcenc_context.packed_slc_header_param_buf_id);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");
+
+ va_status = vaCreateBuffer(va_dpy,
+ avcenc_context.context_id,
+ VAEncPackedHeaderDataBufferType,
+ (length_in_bits + 7) / 8, 1, packed_slc_buffer,
+ &avcenc_context.packed_slc_buf_id);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");
+
+ free(packed_slc_buffer);
+
+ return 0;
+}
+
+int avcenc_render_picture()
+{
+ VAStatus va_status;
+ VABufferID va_buffers[10];
+ unsigned int num_va_buffers = 0;
+ int i;
+
+ va_buffers[num_va_buffers++] = avcenc_context.seq_param_buf_id;
+ va_buffers[num_va_buffers++] = avcenc_context.pic_param_buf_id;
+
+ if (avcenc_context.packed_seq_header_param_buf_id != VA_INVALID_ID)
+ va_buffers[num_va_buffers++] = avcenc_context.packed_seq_header_param_buf_id;
+
+ if (avcenc_context.packed_seq_buf_id != VA_INVALID_ID)
+ va_buffers[num_va_buffers++] = avcenc_context.packed_seq_buf_id;
+
+ if (avcenc_context.packed_pic_header_param_buf_id != VA_INVALID_ID)
+ va_buffers[num_va_buffers++] = avcenc_context.packed_pic_header_param_buf_id;
+
+ if (avcenc_context.packed_pic_buf_id != VA_INVALID_ID)
+ va_buffers[num_va_buffers++] = avcenc_context.packed_pic_buf_id;
+
+ if (avcenc_context.packed_slc_header_param_buf_id != VA_INVALID_ID)
+ va_buffers[num_va_buffers++] = avcenc_context.packed_slc_header_param_buf_id;
+
+ if (avcenc_context.packed_slc_buf_id != VA_INVALID_ID)
+ va_buffers[num_va_buffers++] = avcenc_context.packed_slc_buf_id;
+
+ if (avcenc_context.misc_parameter_hrd_buf_id != VA_INVALID_ID)
+ va_buffers[num_va_buffers++] = avcenc_context.misc_parameter_hrd_buf_id;
+
+ if (avcenc_context.misc_parameter_rc_buf_id != VA_INVALID_ID)
+ va_buffers[num_va_buffers++] = avcenc_context.misc_parameter_rc_buf_id;
+
+ if (avcenc_context.misc_parameter_fr_buf_id != VA_INVALID_ID)
+ va_buffers[num_va_buffers++] = avcenc_context.misc_parameter_fr_buf_id;
+
+ if (avcenc_context.misc_parameter_fei_pic_control_buf_id != VA_INVALID_ID)
+ va_buffers[num_va_buffers++] = avcenc_context.misc_parameter_fei_pic_control_buf_id;
+#if 0
+ if (stats_en)
+ {
+ va_buffers[num_va_buffers++] = avcenc_context.fei_distortion_buf_id;
+ va_buffers[num_va_buffers++] = avcenc_context.fei_mv_buf_id;
+ va_buffers[num_va_buffers++] = avcenc_context.fei_mbcode_buf_id;
+ }
+#endif
+#if 1
+ // there have been sent by avcenc_context.misc_parameter_fei_pic_control_buf_id, so no needed to render again.
+ if(avcenc_context.fei_pic_param.mb_input)
+ va_buffers[num_va_buffers++] = avcenc_context.fei_mbctrl_buf_id;
+
+ if(avcenc_context.fei_pic_param.mv_predictor_enable)
+ va_buffers[num_va_buffers++] = avcenc_context.fei_mvpred_buf_id;
+#endif
+ va_status = vaBeginPicture(va_dpy,
+ avcenc_context.context_id,
+ surface_ids[avcenc_context.current_input_surface]);
+ CHECK_VASTATUS(va_status,"vaBeginPicture");
+
+ va_status = vaRenderPicture(va_dpy,
+ avcenc_context.context_id,
+ va_buffers,
+ num_va_buffers);
+ CHECK_VASTATUS(va_status,"vaRenderPicture");
+
+ for(i = 0; i < avcenc_context.num_slices; i++) {
+ va_status = vaRenderPicture(va_dpy,
+ avcenc_context.context_id,
+ &avcenc_context.slice_param_buf_id[i],
+ 1);
+ CHECK_VASTATUS(va_status,"vaRenderPicture");
+ }
+
+ va_status = vaEndPicture(va_dpy, avcenc_context.context_id);
+ CHECK_VASTATUS(va_status,"vaEndPicture");
+
+ return 0;
+}
+
+static int avcenc_destroy_buffers(VABufferID *va_buffers, unsigned int num_va_buffers)
+{
+ VAStatus va_status;
+ unsigned int i;
+
+ for (i = 0; i < num_va_buffers; i++) {
+ if (va_buffers[i] != VA_INVALID_ID) {
+ va_status = vaDestroyBuffer(va_dpy, va_buffers[i]);
+ CHECK_VASTATUS(va_status,"vaDestroyBuffer");
+ va_buffers[i] = VA_INVALID_ID;
+ }
+ }
+
+ return 0;
+}
+
+static GetNextInput (int current_input)
+{
+ if (current_input == SID_INPUT_PICTURE_0)
+ return SID_INPUT_PICTURE_1;
+ else if (current_input == SID_INPUT_PICTURE_1)
+ return SID_INPUT_PICTURE_2;
+ else if (current_input == SID_INPUT_PICTURE_2)
+ return SID_INPUT_PICTURE_3;
+ else if (current_input == SID_INPUT_PICTURE_3)
+ return SID_INPUT_PICTURE_4;
+ else
+ return SID_INPUT_PICTURE_0;
+}
+static void end_picture(int slice_type, int next_is_bpic)
+{
+ VABufferID tempID;
+
+ /* Prepare for next picture */
+ tempID = surface_ids[SID_RECON_PICTURE];
+
+ if (slice_type != SLICE_TYPE_B) {
+ if (next_is_bpic) {
+ surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L1];
+ surface_ids[SID_REFERENCE_PICTURE_L1] = tempID;
+ ref1_POC[0] = Cur_POC[0];
+ ref1_POC[1] = Cur_POC[1];
+ next_input_id = GetNextInput(next_input_id );
+ } else {
+ surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L0];
+ surface_ids[SID_REFERENCE_PICTURE_L0] = tempID;
+ ref0_POC[0] = Cur_POC[0];
+ ref0_POC[1] = Cur_POC[1];
+ next_input_id = GetNextInput(next_input_id );
+ }
+ } else {
+ if (!next_is_bpic) {
+ surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L0];
+ surface_ids[SID_REFERENCE_PICTURE_L0] = surface_ids[SID_REFERENCE_PICTURE_L1];
+ surface_ids[SID_REFERENCE_PICTURE_L1] = tempID;
+ ref0_POC[0] = ref1_POC[0];
+ ref0_POC[1] = ref1_POC[1];
+ ref1_POC[0] = Cur_POC[0];
+ ref1_POC[1] = Cur_POC[1];
+ next_input_id = GetNextInput(next_input_id );
+ }
+ }
+
+ avcenc_destroy_buffers(&avcenc_context.seq_param_buf_id, 1);
+ avcenc_destroy_buffers(&avcenc_context.pic_param_buf_id, 1);
+ /*
+ avcenc_destroy_buffers(&avcenc_context.packed_seq_header_param_buf_id, 1);
+ avcenc_destroy_buffers(&avcenc_context.packed_seq_buf_id, 1);
+ */
+ avcenc_destroy_buffers(&avcenc_context.packed_pic_header_param_buf_id, 1);
+ avcenc_destroy_buffers(&avcenc_context.packed_pic_buf_id, 1);
+
+ avcenc_destroy_buffers(&avcenc_context.packed_slc_header_param_buf_id, 1);
+ avcenc_destroy_buffers(&avcenc_context.packed_slc_buf_id, 1);
+
+
+ avcenc_destroy_buffers(&avcenc_context.slice_param_buf_id[0], avcenc_context.num_slices);
+ avcenc_destroy_buffers(&avcenc_context.codedbuf_buf_id, 1);
+ avcenc_destroy_buffers(&avcenc_context.misc_parameter_hrd_buf_id, 1);
+ avcenc_destroy_buffers(&avcenc_context.misc_parameter_rc_buf_id, 1);
+ avcenc_destroy_buffers(&avcenc_context.misc_parameter_fr_buf_id, 1);
+ if(avcenc_context.fei_pic_param.mb_input)
+ avcenc_destroy_buffers(&avcenc_context.fei_mbctrl_buf_id, 1);
+ if(avcenc_context.fei_pic_param.mv_predictor_enable)
+ avcenc_destroy_buffers(&avcenc_context.fei_mvpred_buf_id, 1);
+ if (stats_en)
+ {
+ avcenc_destroy_buffers(&avcenc_context.fei_distortion_buf_id, 1);
+ avcenc_destroy_buffers(&avcenc_context.fei_mv_buf_id, 1);
+ avcenc_destroy_buffers(&avcenc_context.fei_mbcode_buf_id, 1);
+ }
+
+ memset(avcenc_context.slice_param, 0, sizeof(avcenc_context.slice_param));
+ avcenc_context.num_slices = 0;
+}
+
+#define BITSTREAM_ALLOCATE_STEPPING 4096
+
+struct __bitstream {
+ unsigned int *buffer;
+ int bit_offset;
+ int max_size_in_dword;
+};
+
+typedef struct __bitstream bitstream;
+
+#if 0
+static int
+get_coded_bitsteam_length(unsigned char *buffer, int buffer_length)
+{
+ int i;
+
+ for (i = 0; i < buffer_length - 3; i++) {
+ if (!buffer[i] &&
+ !buffer[i + 1] &&
+ !buffer[i + 2] &&
+ !buffer[i + 3])
+ break;
+ }
+
+ return i;
+}
+#endif
+
+static unsigned int
+swap32(unsigned int val)
+{
+ unsigned char *pval = (unsigned char *)&val;
+
+ return ((pval[0] << 24) |
+ (pval[1] << 16) |
+ (pval[2] << 8) |
+ (pval[3] << 0));
+}
+
+static void
+bitstream_start(bitstream *bs)
+{
+ bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
+ bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
+ bs->bit_offset = 0;
+}
+
+static void
+bitstream_end(bitstream *bs)
+{
+ int pos = (bs->bit_offset >> 5);
+ int bit_offset = (bs->bit_offset & 0x1f);
+ int bit_left = 32 - bit_offset;
+
+ if (bit_offset) {
+ bs->buffer[pos] = swap32((bs->buffer[pos] << bit_left));
+ }
+}
+
+static void
+bitstream_put_ui(bitstream *bs, unsigned int val, int size_in_bits)
+{
+ int pos = (bs->bit_offset >> 5);
+ int bit_offset = (bs->bit_offset & 0x1f);
+ int bit_left = 32 - bit_offset;
+
+ if (!size_in_bits)
+ return;
+
+ bs->bit_offset += size_in_bits;
+
+ if (bit_left > size_in_bits) {
+ bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
+ } else {
+ size_in_bits -= bit_left;
+ bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
+ bs->buffer[pos] = swap32(bs->buffer[pos]);
+
+ if (pos + 1 == bs->max_size_in_dword) {
+ bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
+ bs->buffer = realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
+ }
+
+ bs->buffer[pos + 1] = val;
+ }
+}
+
+static void
+bitstream_put_ue(bitstream *bs, unsigned int val)
+{
+ int size_in_bits = 0;
+ int tmp_val = ++val;
+
+ while (tmp_val) {
+ tmp_val >>= 1;
+ size_in_bits++;
+ }
+
+ bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
+ bitstream_put_ui(bs, val, size_in_bits);
+}
+
+static void
+bitstream_put_se(bitstream *bs, int val)
+{
+ unsigned int new_val;
+
+ if (val <= 0)
+ new_val = -2 * val;
+ else
+ new_val = 2 * val - 1;
+
+ bitstream_put_ue(bs, new_val);
+}
+
+static void
+bitstream_byte_aligning(bitstream *bs, int bit)
+{
+ int bit_offset = (bs->bit_offset & 0x7);
+ int bit_left = 8 - bit_offset;
+ int new_val;
+
+ if (!bit_offset)
+ return;
+
+ assert(bit == 0 || bit == 1);
+
+ if (bit)
+ new_val = (1 << bit_left) - 1;
+ else
+ new_val = 0;
+
+ bitstream_put_ui(bs, new_val, bit_left);
+}
+
+static void
+rbsp_trailing_bits(bitstream *bs)
+{
+ bitstream_put_ui(bs, 1, 1);
+ bitstream_byte_aligning(bs, 0);
+}
+
+static void nal_start_code_prefix(bitstream *bs)
+{
+ bitstream_put_ui(bs, 0x00000001, 32);
+}
+
+static void nal_header(bitstream *bs, int nal_ref_idc, int nal_unit_type)
+{
+ bitstream_put_ui(bs, 0, 1); /* forbidden_zero_bit: 0 */
+ bitstream_put_ui(bs, nal_ref_idc, 2);
+ bitstream_put_ui(bs, nal_unit_type, 5);
+}
+
+static void sps_rbsp(bitstream *bs)
+{
+ VAEncSequenceParameterBufferH264 *seq_param = &avcenc_context.seq_param;
+ int profile_idc = PROFILE_IDC_BASELINE;
+
+ if (avcenc_context.profile == VAProfileH264High)
+ profile_idc = PROFILE_IDC_HIGH;
+ else if (avcenc_context.profile == VAProfileH264Main)
+ profile_idc = PROFILE_IDC_MAIN;
+
+ bitstream_put_ui(bs, profile_idc, 8); /* profile_idc */
+ bitstream_put_ui(bs, 0, 1); /* constraint_set0_flag */
+ bitstream_put_ui(bs, 1, 1); /* constraint_set1_flag */
+ bitstream_put_ui(bs, 0, 1); /* constraint_set2_flag */
+ bitstream_put_ui(bs, 0, 1); /* constraint_set3_flag */
+ bitstream_put_ui(bs, 0, 4); /* reserved_zero_4bits */
+ bitstream_put_ui(bs, seq_param->level_idc, 8); /* level_idc */
+ bitstream_put_ue(bs, seq_param->seq_parameter_set_id); /* seq_parameter_set_id */
+
+ if (avcenc_context.profile == VAProfileH264High)
+ {
+ bitstream_put_ue(bs, 1); /* chroma_format_idc */
+ bitstream_put_ue(bs, 0); /* bit_depth_luma_minus8 */
+ bitstream_put_ue(bs, 0); /* bit_depth_chroma_minus8 */
+ bitstream_put_ui(bs, 0, 1); /* qpprime_y_zero_transform_bypass_flag */
+ bitstream_put_ui(bs, 0, 1); /* seq_scaling_matrix_present_flag */
+ }
+
+ bitstream_put_ue(bs, seq_param->seq_fields.bits.log2_max_frame_num_minus4); /* log2_max_frame_num_minus4 */
+
+ bitstream_put_ue(bs, seq_param->seq_fields.bits.pic_order_cnt_type); /* pic_order_cnt_type */
+
+ if (seq_param->seq_fields.bits.pic_order_cnt_type == 0)
+ bitstream_put_ue(bs, seq_param->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4); /* log2_max_pic_order_cnt_lsb_minus4 */
+ else {
+ assert(0);
+ }
+
+ bitstream_put_ue(bs, seq_param->max_num_ref_frames); /* num_ref_frames */
+ bitstream_put_ui(bs, 0, 1); /* gaps_in_frame_num_value_allowed_flag */
+
+ bitstream_put_ue(bs, seq_param->picture_width_in_mbs - 1); /* pic_width_in_mbs_minus1 */
+ bitstream_put_ue(bs, seq_param->picture_height_in_mbs - 1); /* pic_height_in_map_units_minus1 */
+ bitstream_put_ui(bs, seq_param->seq_fields.bits.frame_mbs_only_flag, 1); /* frame_mbs_only_flag */
+
+ if (!seq_param->seq_fields.bits.frame_mbs_only_flag) {
+ assert(0);
+ }
+
+ bitstream_put_ui(bs, seq_param->seq_fields.bits.direct_8x8_inference_flag, 1); /* direct_8x8_inference_flag */
+ bitstream_put_ui(bs, seq_param->frame_cropping_flag, 1); /* frame_cropping_flag */
+
+ if (seq_param->frame_cropping_flag) {
+ bitstream_put_ue(bs, seq_param->frame_crop_left_offset); /* frame_crop_left_offset */
+ bitstream_put_ue(bs, seq_param->frame_crop_right_offset); /* frame_crop_right_offset */
+ bitstream_put_ue(bs, seq_param->frame_crop_top_offset); /* frame_crop_top_offset */
+ bitstream_put_ue(bs, seq_param->frame_crop_bottom_offset); /* frame_crop_bottom_offset */
+ }
+
+ if ( frame_bit_rate < 0 ) {
+ bitstream_put_ui(bs, 0, 1); /* vui_parameters_present_flag */
+ } else {
+ bitstream_put_ui(bs, 1, 1); /* vui_parameters_present_flag */
+ bitstream_put_ui(bs, 0, 1); /* aspect_ratio_info_present_flag */
+ bitstream_put_ui(bs, 0, 1); /* overscan_info_present_flag */
+ bitstream_put_ui(bs, 0, 1); /* video_signal_type_present_flag */
+ bitstream_put_ui(bs, 0, 1); /* chroma_loc_info_present_flag */
+ bitstream_put_ui(bs, 1, 1); /* timing_info_present_flag */
+ {
+ bitstream_put_ui(bs, 15, 32);
+ bitstream_put_ui(bs, 900, 32);
+ bitstream_put_ui(bs, 1, 1);
+ }
+ bitstream_put_ui(bs, 1, 1); /* nal_hrd_parameters_present_flag */
+ {
+ // hrd_parameters
+ bitstream_put_ue(bs, 0); /* cpb_cnt_minus1 */
+ bitstream_put_ui(bs, 4, 4); /* bit_rate_scale */
+ bitstream_put_ui(bs, 6, 4); /* cpb_size_scale */
+
+ bitstream_put_ue(bs, frame_bit_rate - 1); /* bit_rate_value_minus1[0] */
+ bitstream_put_ue(bs, frame_bit_rate*8 - 1); /* cpb_size_value_minus1[0] */
+ bitstream_put_ui(bs, 1, 1); /* cbr_flag[0] */
+
+ bitstream_put_ui(bs, 23, 5); /* initial_cpb_removal_delay_length_minus1 */
+ bitstream_put_ui(bs, 23, 5); /* cpb_removal_delay_length_minus1 */
+ bitstream_put_ui(bs, 23, 5); /* dpb_output_delay_length_minus1 */
+ bitstream_put_ui(bs, 23, 5); /* time_offset_length */
+ }
+ bitstream_put_ui(bs, 0, 1); /* vcl_hrd_parameters_present_flag */
+ bitstream_put_ui(bs, 0, 1); /* low_delay_hrd_flag */
+
+ bitstream_put_ui(bs, 0, 1); /* pic_struct_present_flag */
+ bitstream_put_ui(bs, 0, 1); /* bitstream_restriction_flag */
+ }
+
+ rbsp_trailing_bits(bs); /* rbsp_trailing_bits */
+}
+
+#if 0
+static void build_nal_sps(FILE *avc_fp)
+{
+ bitstream bs;
+
+ bitstream_start(&bs);
+ nal_start_code_prefix(&bs);
+ nal_header(&bs, NAL_REF_IDC_HIGH, NAL_SPS);
+ sps_rbsp(&bs);
+ bitstream_end(&bs, avc_fp);
+}
+#endif
+
+static void pps_rbsp(bitstream *bs)
+{
+ VAEncPictureParameterBufferH264 *pic_param = &avcenc_context.pic_param;
+
+ bitstream_put_ue(bs, pic_param->pic_parameter_set_id); /* pic_parameter_set_id */
+ bitstream_put_ue(bs, pic_param->seq_parameter_set_id); /* seq_parameter_set_id */
+
+ bitstream_put_ui(bs, pic_param->pic_fields.bits.entropy_coding_mode_flag, 1); /* entropy_coding_mode_flag */
+
+ bitstream_put_ui(bs, 0, 1); /* pic_order_present_flag: 0 */
+
+ bitstream_put_ue(bs, 0); /* num_slice_groups_minus1 */
+
+ bitstream_put_ue(bs, pic_param->num_ref_idx_l0_active_minus1); /* num_ref_idx_l0_active_minus1 */
+ bitstream_put_ue(bs, pic_param->num_ref_idx_l1_active_minus1); /* num_ref_idx_l1_active_minus1 1 */
+
+ bitstream_put_ui(bs, pic_param->pic_fields.bits.weighted_pred_flag, 1); /* weighted_pred_flag: 0 */
+ bitstream_put_ui(bs, pic_param->pic_fields.bits.weighted_bipred_idc, 2); /* weighted_bipred_idc: 0 */
+
+ bitstream_put_se(bs, pic_param->pic_init_qp - 26); /* pic_init_qp_minus26 */
+ bitstream_put_se(bs, 0); /* pic_init_qs_minus26 */
+ bitstream_put_se(bs, 0); /* chroma_qp_index_offset */
+
+ bitstream_put_ui(bs, pic_param->pic_fields.bits.deblocking_filter_control_present_flag, 1); /* deblocking_filter_control_present_flag */
+ bitstream_put_ui(bs, 0, 1); /* constrained_intra_pred_flag */
+ bitstream_put_ui(bs, 0, 1); /* redundant_pic_cnt_present_flag */
+
+ /* more_rbsp_data */
+ if (avcenc_context.profile == VAProfileH264High)
+ {
+ bitstream_put_ui(bs, pic_param->pic_fields.bits.transform_8x8_mode_flag, 1); /*transform_8x8_mode_flag */
+ bitstream_put_ui(bs, 0, 1); /* pic_scaling_matrix_present_flag */
+ bitstream_put_se(bs, pic_param->second_chroma_qp_index_offset ); /*second_chroma_qp_index_offset */
+ }
+ rbsp_trailing_bits(bs);
+}
+
+#if 0
+static void build_nal_pps(FILE *avc_fp)
+{
+ bitstream bs;
+
+ bitstream_start(&bs);
+ nal_start_code_prefix(&bs);
+ nal_header(&bs, NAL_REF_IDC_HIGH, NAL_PPS);
+ pps_rbsp(&bs);
+ bitstream_end(&bs, avc_fp);
+}
+
+static void
+build_header(FILE *avc_fp)
+{
+ build_nal_sps(avc_fp);
+ build_nal_pps(avc_fp);
+}
+#endif
+
+static int
+build_packed_pic_buffer(unsigned char **header_buffer)
+{
+ bitstream bs;
+
+ bitstream_start(&bs);
+ nal_start_code_prefix(&bs);
+ nal_header(&bs, NAL_REF_IDC_HIGH, NAL_PPS);
+ pps_rbsp(&bs);
+ bitstream_end(&bs);
+
+ *header_buffer = (unsigned char *)bs.buffer;
+ return bs.bit_offset;
+}
+
+static int
+build_packed_seq_buffer(unsigned char **header_buffer)
+{
+ bitstream bs;
+
+ bitstream_start(&bs);
+ nal_start_code_prefix(&bs);
+ nal_header(&bs, NAL_REF_IDC_HIGH, NAL_SPS);
+ sps_rbsp(&bs);
+ bitstream_end(&bs);
+
+ *header_buffer = (unsigned char *)bs.buffer;
+ return bs.bit_offset;
+}
+
+
+#if 1
+static void
+slice_header(bitstream *bs, int frame_num, int display_frame, int slice_type, int nal_ref_idc, int is_idr)
+{
+ VAEncSequenceParameterBufferH264 *seq_param = &avcenc_context.seq_param;
+ VAEncPictureParameterBufferH264 *pic_param = &avcenc_context.pic_param;
+ int is_cabac = (pic_param->pic_fields.bits.entropy_coding_mode_flag == ENTROPY_MODE_CABAC);
+
+ bitstream_put_ue(bs, 0); /* first_mb_in_slice: 0 */
+ bitstream_put_ue(bs, slice_type); /* slice_type */
+ bitstream_put_ue(bs, 0); /* pic_parameter_set_id: 0 */
+
+ if(is_idr) {
+ frame_num = 0; // !! IDR pic need to reset frame_num
+ }
+
+ bitstream_put_ui(bs, frame_num & 0x0F, seq_param->seq_fields.bits.log2_max_frame_num_minus4 + 4); /* frame_num */
+
+ /* frame_mbs_only_flag == 1 */
+ if (!seq_param->seq_fields.bits.frame_mbs_only_flag) {
+ /* FIXME: */
+ assert(0);
+ }
+
+ if (is_idr)
+ bitstream_put_ue(bs, 0); /* idr_pic_id: 0 */
+
+ if (seq_param->seq_fields.bits.pic_order_cnt_type == 0) {
+ bitstream_put_ui(bs, (display_frame*2) & 0x3F, seq_param->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 + 4);
+ /* only support frame */
+ } else {
+ /* FIXME: */
+ assert(0);
+ }
+
+ /* redundant_pic_cnt_present_flag == 0 */
+
+ /* slice type */
+ if (slice_type == SLICE_TYPE_P) {
+ bitstream_put_ui(bs, 0, 1); /* num_ref_idx_active_override_flag: 0 */
+ /* ref_pic_list_reordering */
+ bitstream_put_ui(bs, 0, 1); /* ref_pic_list_reordering_flag_l0: 0 */
+ } else if (slice_type == SLICE_TYPE_B) {
+ bitstream_put_ui(bs, 1, 1); /* direct_spatial_mv_pred: 1 */
+ bitstream_put_ui(bs, 0, 1); /* num_ref_idx_active_override_flag: 0 */
+ /* ref_pic_list_reordering */
+ bitstream_put_ui(bs, 0, 1); /* ref_pic_list_reordering_flag_l0: 0 */
+ bitstream_put_ui(bs, 0, 1); /* ref_pic_list_reordering_flag_l1: 0 */
+ }
+
+ /* weighted_pred_flag == 0 */
+
+ /* dec_ref_pic_marking */
+ if (nal_ref_idc != 0) {
+ if ( is_idr) {
+ bitstream_put_ui(bs, 0, 1); /* no_output_of_prior_pics_flag: 0 */
+ bitstream_put_ui(bs, 0, 1); /* long_term_reference_flag: 0 */
+ } else {
+ bitstream_put_ui(bs, 0, 1); /* adaptive_ref_pic_marking_mode_flag: 0 */
+ }
+ }
+
+ if (is_cabac && (slice_type != SLICE_TYPE_I))
+ bitstream_put_ue(bs, 0); /* cabac_init_idc: 0 */
+
+ bitstream_put_se(bs, 0); /* slice_qp_delta: 0 */
+
+ if (pic_param->pic_fields.bits.deblocking_filter_control_present_flag == 1) {
+ bitstream_put_ue(bs, 0); /* disable_deblocking_filter_idc: 0 */
+ bitstream_put_se(bs, 2); /* slice_alpha_c0_offset_div2: 2 */
+ bitstream_put_se(bs, 2); /* slice_beta_offset_div2: 2 */
+ }
+
+ if(is_cabac) {
+ while(bs->bit_offset % 8) {
+ bitstream_put_ui(bs, 1, 1); // cabac_alignment_one_bit
+ }
+ }
+
+}
+#endif
+
+
+int
+build_packed_slc_buffer(unsigned char **header_buffer, int frame_num, int display_frame, int slice_type, int is_idr)
+{
+ int nal_ref_idc; // NAL_REF_IDC_HIGH or zero
+ int nal_unit_type; // NAL_IDR or NAL_NON_IDR
+ bitstream bs;
+ bitstream_start(&bs);
+ nal_start_code_prefix(&bs);
+
+ if(is_idr) {
+ nal_ref_idc = NAL_REF_IDC_HIGH;
+ nal_unit_type = NAL_IDR;
+ } else if(slice_type != SLICE_TYPE_B) {
+ nal_ref_idc = NAL_REF_IDC_HIGH;
+ nal_unit_type = NAL_NON_IDR;
+ } else {
+ nal_ref_idc = 0;
+ nal_unit_type = NAL_NON_IDR;
+ }
+
+ nal_header(&bs, nal_ref_idc, nal_unit_type);
+
+ slice_header(&bs, frame_num, display_frame, slice_type, nal_ref_idc, is_idr);
+ bitstream_end(&bs);
+ *header_buffer = (unsigned char *)bs.buffer;
+ return bs.bit_offset;
+}
+
+
+#if 0
+static void
+slice_data(bitstream *bs)
+{
+ VACodedBufferSegment *coded_buffer_segment;
+ unsigned char *coded_mem;
+ int i, slice_data_length;
+ VAStatus va_status;
+ VASurfaceStatus surface_status;
+
+ va_status = vaSyncSurface(va_dpy, surface_ids[avcenc_context.current_input_surface]);
+ CHECK_VASTATUS(va_status,"vaSyncSurface");
+
+ surface_status = 0;
+ va_status = vaQuerySurfaceStatus(va_dpy, surface_ids[avcenc_context.current_input_surface], &surface_status);
+ CHECK_VASTATUS(va_status,"vaQuerySurfaceStatus");
+
+ va_status = vaMapBuffer(va_dpy, avcenc_context.codedbuf_buf_id, (void **)(&coded_buffer_segment));
+ CHECK_VASTATUS(va_status,"vaMapBuffer");
+ coded_mem = coded_buffer_segment->buf;
+
+ slice_data_length = get_coded_bitsteam_length(coded_mem, codedbuf_size);
+
+ for (i = 0; i < slice_data_length; i++) {
+ bitstream_put_ui(bs, *coded_mem, 8);
+ coded_mem++;
+ }
+
+ vaUnmapBuffer(va_dpy, avcenc_context.codedbuf_buf_id);
+}
+
+static void
+build_nal_slice(FILE *avc_fp, int frame_num, int display_frame, int slice_type, int is_idr)
+{
+ bitstream bs;
+
+ bitstream_start(&bs);
+ slice_data(&bs);
+ bitstream_end(&bs, avc_fp);
+}
+
+#endif
+
+void
+store_distortion_buffer()
+{
+ FILE *dist_fp;
+ unsigned char *pbuf;
+ VAStatus va_status;
+ size_t n_items;
+
+ // distoriton output
+ va_status = vaMapBuffer(va_dpy, avcenc_context.fei_distortion_buf_id, (void **)(&pbuf));
+ CHECK_VASTATUS(va_status,"vaMapBuffer");
+
+ dist_fp = fopen("feidistortionout.bin", "wb");
+
+ do {
+ n_items = fwrite(pbuf, sizeof(VAEncFEIDistortionBufferH264Intel)*picture_width_in_mbs*picture_height_in_mbs, 1, dist_fp);
+ }
+ while (n_items != 1);
+
+ fclose(dist_fp);
+
+ va_status = vaUnmapBuffer(va_dpy, avcenc_context.fei_distortion_buf_id);
+ CHECK_VASTATUS(va_status,"vaUnmapBuffer");
+
+ // mv output
+ va_status = vaMapBuffer(va_dpy, avcenc_context.fei_mv_buf_id, (void **)(&pbuf));
+ CHECK_VASTATUS(va_status,"vaMapBuffer");
+
+ dist_fp = fopen("feimvout.bin", "wb");
+
+ do {
+ n_items = fwrite(pbuf, sizeof(VAMotionVectorIntel)*16*picture_width_in_mbs*picture_height_in_mbs, 1, dist_fp);
+ }
+ while (n_items != 1);
+
+ fclose(dist_fp);
+
+ va_status = vaUnmapBuffer(va_dpy, avcenc_context.fei_mv_buf_id);
+ CHECK_VASTATUS(va_status,"vaUnmapBuffer");
+
+ // mv output
+ va_status = vaMapBuffer(va_dpy, avcenc_context.fei_mbcode_buf_id, (void **)(&pbuf));
+ CHECK_VASTATUS(va_status,"vaMapBuffer");
+
+ dist_fp = fopen("feimbcodeout.bin", "wb");
+
+ do {
+ n_items = fwrite(pbuf, sizeof(VAEncFEIModeBufferH264Intel)*picture_width_in_mbs*picture_height_in_mbs, 1, dist_fp);
+ }
+ while (n_items != 1);
+
+ fclose(dist_fp);
+
+ va_status = vaUnmapBuffer(va_dpy, avcenc_context.fei_mbcode_buf_id);
+ CHECK_VASTATUS(va_status,"vaUnmapBuffer");
+
+ return;
+}
+
+static int
+store_coded_buffer(FILE *avc_fp, int slice_type)
+{
+ VACodedBufferSegment *coded_buffer_segment;
+ unsigned char *coded_mem;
+ int slice_data_length;
+ VAStatus va_status;
+ VASurfaceStatus surface_status;
+ size_t w_items;
+
+ va_status = vaSyncSurface(va_dpy, surface_ids[avcenc_context.current_input_surface]);
+ CHECK_VASTATUS(va_status,"vaSyncSurface");
+
+ surface_status = 0;
+ va_status = vaQuerySurfaceStatus(va_dpy, surface_ids[avcenc_context.current_input_surface], &surface_status);
+ CHECK_VASTATUS(va_status,"vaQuerySurfaceStatus");
+
+ va_status = vaMapBuffer(va_dpy, avcenc_context.codedbuf_buf_id, (void **)(&coded_buffer_segment));
+ CHECK_VASTATUS(va_status,"vaMapBuffer");
+ coded_mem = coded_buffer_segment->buf;
+
+
+
+ if (coded_buffer_segment->status & VA_CODED_BUF_STATUS_SLICE_OVERFLOW_MASK) {
+ if (slice_type == SLICE_TYPE_I)
+ avcenc_context.codedbuf_i_size *= 2;
+ else
+ avcenc_context.codedbuf_pb_size *= 2;
+
+ vaUnmapBuffer(va_dpy, avcenc_context.codedbuf_buf_id);
+ return -1;
+ }
+
+ slice_data_length = coded_buffer_segment->size;
+ printf("SIZE=%d\n",slice_data_length);
+ do {
+ w_items = fwrite(coded_mem, slice_data_length, 1, avc_fp);
+ } while (w_items != 1);
+#if 0
+ if (slice_type == SLICE_TYPE_I) {
+ if (avcenc_context.codedbuf_i_size > slice_data_length * 3 / 2) {
+ avcenc_context.codedbuf_i_size = slice_data_length * 3 / 2;
+ }
+
+ if (avcenc_context.codedbuf_pb_size < slice_data_length) {
+ avcenc_context.codedbuf_pb_size = slice_data_length;
+ }
+ } else {
+ if (avcenc_context.codedbuf_pb_size > slice_data_length * 3 / 2) {
+ avcenc_context.codedbuf_pb_size = slice_data_length * 3 / 2;
+ }
+ }
+#endif
+ vaUnmapBuffer(va_dpy, avcenc_context.codedbuf_buf_id);
+
+ return 0;
+}
+
+static void
+encode_picture(FILE *yuv_fp, FILE *avc_fp,
+ int frame_num, int display_num,
+ int is_idr,
+ int slice_type, int next_is_bpic,
+ int next_display_num)
+{
+ VAStatus va_status;
+ int ret = 0, codedbuf_size;
+
+ printf("\r ---> encoded frame %d, type=%d ...\n", frame_num, slice_type);
+ begin_picture(yuv_fp, frame_num, display_num, slice_type, is_idr);
+
+ //if (next_display_num < frame_number) {
+ if (1) {
+ int index;
+
+ /* prepare for next frame */
+ /*
+ if (avcenc_context.current_input_surface == SID_INPUT_PICTURE_0)
+ index = SID_INPUT_PICTURE_1;
+ else if (avcenc_context.current_input_surface == SID_INPUT_PICTURE_1)
+ index = SID_INPUT_PICTURE_2;
+ else if (avcenc_context.current_input_surface == SID_INPUT_PICTURE_2)
+ index = SID_INPUT_PICTURE_3;
+ else if (avcenc_context.current_input_surface == SID_INPUT_PICTURE_3)
+ index = SID_INPUT_PICTURE_4;
+ else
+ index = SID_INPUT_PICTURE_0;
+ */
+ index=next_input_id;
+ if ( next_display_num >= frame_number )
+ next_display_num = frame_number - 1;
+ fseek(yuv_fp, frame_size * display_num, SEEK_SET);
+
+ avcenc_context.upload_thread_param.yuv_fp = yuv_fp;
+ avcenc_context.upload_thread_param.surface_id = surface_ids[index];
+#if 0
+ avcenc_context.upload_thread_value = pthread_create(&avcenc_context.upload_thread_id,
+ NULL,
+ upload_thread_function,
+ (void*)&avcenc_context.upload_thread_param);
+#else
+ upload_thread_function((void *)(&avcenc_context.upload_thread_param));
+#endif
+ }
+ usleep(10000);
+ do {
+ avcenc_destroy_buffers(&avcenc_context.pic_param_buf_id, 1);
+ avcenc_destroy_buffers(&avcenc_context.packed_pic_buf_id, 1);
+
+ if (SLICE_TYPE_I == slice_type) {
+ codedbuf_size = avcenc_context.codedbuf_i_size;
+ } else {
+ codedbuf_size = avcenc_context.codedbuf_pb_size;
+ }
+
+ /* coded buffer */
+ va_status = vaCreateBuffer(va_dpy,
+ avcenc_context.context_id,
+ VAEncCodedBufferType,
+ codedbuf_size, 1, NULL,
+ &avcenc_context.codedbuf_buf_id);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");
+
+ /* picture parameter set */
+ avcenc_update_picture_parameter(slice_type, frame_num, display_num, is_idr);
+
+ avcenc_render_picture();
+ usleep(10000);
+
+ if(stats_en)
+ store_distortion_buffer();
+ ret = store_coded_buffer(avc_fp, slice_type);
+ } while (ret);
+
+ end_picture(slice_type, next_is_bpic);
+}
+
+static void encode_pb_pictures(FILE *yuv_fp, FILE *avc_fp, int f, int nbframes, int next_f)
+{
+ int i;
+ encode_picture(yuv_fp, avc_fp,
+ enc_frame_number, f + nbframes,
+ 0,
+ SLICE_TYPE_P, 1, f);
+
+ for( i = 0; i < nbframes - 1; i++) {
+ encode_picture(yuv_fp, avc_fp,
+ enc_frame_number + 1, f + i,
+ 0,
+ SLICE_TYPE_B, 1, f + i + 1);
+ }
+
+ encode_picture(yuv_fp, avc_fp,
+ enc_frame_number + 1, f + nbframes - 1,
+ 0,
+ SLICE_TYPE_B, 0, next_f);
+}
+
+static void show_help()
+{
+ //printf("Usage: avnenc <width> <height> <input_yuvfile> <output_avcfile> [qp=qp_value|fb=framebitrate] [mode=0(I frames only)/1(I and P frames)/2(I, P and B frames)\n");
+ printf("Usage: feiavnenc -size <width> <height> -i <input_yuvfile> -o <output_avcfile> -qp [QP value] -fb [frame_bit_rate] -mode [mode=0(I frames only)/1(I and P frames)/2(I, P and B frames) -frames [0 means encoded all frames, or encoded real frames] -stats <enable output statistics or not>\n");
+}
+
+static void avcenc_context_seq_param_init(VAEncSequenceParameterBufferH264 *seq_param,
+ int width, int height)
+
+{
+ int width_in_mbs = (width + 15) / 16;
+ int height_in_mbs = (height + 15) / 16;
+ int frame_cropping_flag = 0;
+ int frame_crop_bottom_offset = 0;
+
+ seq_param->seq_parameter_set_id = 0;
+ seq_param->level_idc = 41;
+ seq_param->intra_period = intra_period;
+ seq_param->ip_period = ip_period; /* FIXME: ??? */
+ seq_param->max_num_ref_frames = 4;
+ seq_param->picture_width_in_mbs = width_in_mbs;
+ seq_param->picture_height_in_mbs = height_in_mbs;
+ seq_param->seq_fields.bits.frame_mbs_only_flag = 1;
+
+ if (frame_bit_rate > 0)
+ seq_param->bits_per_second = 1024 * frame_bit_rate; /* use kbps as input */
+ else
+ seq_param->bits_per_second = 0;
+
+ seq_param->time_scale = 900;
+ seq_param->num_units_in_tick = 15; /* Tc = num_units_in_tick / time_sacle */
+
+ if (height_in_mbs * 16 - height) {
+ frame_cropping_flag = 1;
+ frame_crop_bottom_offset =
+ (height_in_mbs * 16 - height) / (2 * (!seq_param->seq_fields.bits.frame_mbs_only_flag + 1));
+ }
+
+ seq_param->frame_cropping_flag = frame_cropping_flag;
+ seq_param->frame_crop_left_offset = 0;
+ seq_param->frame_crop_right_offset = 0;
+ seq_param->frame_crop_top_offset = 0;
+ seq_param->frame_crop_bottom_offset = frame_crop_bottom_offset;
+
+ seq_param->seq_fields.bits.pic_order_cnt_type = 0;
+ seq_param->seq_fields.bits.direct_8x8_inference_flag = 1;
+
+ seq_param->seq_fields.bits.log2_max_frame_num_minus4 = 0;
+
+ seq_param->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 = 2;
+
+ if (frame_bit_rate > 0)
+ seq_param->vui_parameters_present_flag = 1; //HRD info located in vui
+ else
+ seq_param->vui_parameters_present_flag = 0;
+}
+
+static void avcenc_context_pic_param_init(VAEncPictureParameterBufferH264 *pic_param)
+{
+ pic_param->seq_parameter_set_id = 0;
+ pic_param->pic_parameter_set_id = 0;
+
+ pic_param->last_picture = 0;
+ pic_param->frame_num = 0;
+
+ pic_param->pic_init_qp = (qp_value >= 0 ? qp_value : 26);
+ pic_param->num_ref_idx_l0_active_minus1 = 0;
+ pic_param->num_ref_idx_l1_active_minus1 = 0;
+
+ pic_param->pic_fields.bits.idr_pic_flag = 0;
+ pic_param->pic_fields.bits.reference_pic_flag = 0;
+ pic_param->pic_fields.bits.entropy_coding_mode_flag = ENTROPY_MODE_CABAC;
+ pic_param->pic_fields.bits.weighted_pred_flag = 0;
+ pic_param->pic_fields.bits.weighted_bipred_idc = 0;
+ pic_param->pic_fields.bits.transform_8x8_mode_flag = 1;
+ pic_param->pic_fields.bits.deblocking_filter_control_present_flag = 1;
+}
+
+static void avcenc_context_init(int width, int height)
+{
+ int i;
+ memset(&avcenc_context, 0, sizeof(avcenc_context));
+ avcenc_context.profile = VAProfileH264High;
+ avcenc_context.seq_param_buf_id = VA_INVALID_ID;
+ avcenc_context.pic_param_buf_id = VA_INVALID_ID;
+ avcenc_context.packed_seq_header_param_buf_id = VA_INVALID_ID;
+ avcenc_context.packed_seq_buf_id = VA_INVALID_ID;
+ avcenc_context.packed_pic_header_param_buf_id = VA_INVALID_ID;
+ avcenc_context.packed_pic_buf_id = VA_INVALID_ID;
+ avcenc_context.packed_slc_header_param_buf_id = VA_INVALID_ID;
+ avcenc_context.packed_slc_buf_id = VA_INVALID_ID;
+ avcenc_context.codedbuf_buf_id = VA_INVALID_ID;
+ avcenc_context.fei_distortion_buf_id = VA_INVALID_ID;
+ avcenc_context.fei_mv_buf_id = VA_INVALID_ID;
+ avcenc_context.fei_mbcode_buf_id = VA_INVALID_ID;
+ avcenc_context.misc_parameter_fei_pic_control_buf_id = VA_INVALID_ID;
+ avcenc_context.fei_mbctrl_buf_id = VA_INVALID_ID;
+ avcenc_context.fei_mvpred_buf_id = VA_INVALID_ID;
+ avcenc_context.misc_parameter_hrd_buf_id = VA_INVALID_ID;
+ avcenc_context.misc_parameter_rc_buf_id = VA_INVALID_ID;
+ avcenc_context.misc_parameter_fr_buf_id = VA_INVALID_ID;
+ avcenc_context.codedbuf_i_size = width * height * 3 / 2;
+ avcenc_context.codedbuf_pb_size = width * height * 3 / 2;
+ avcenc_context.fei_mbctrl_buf_size = 0;
+ avcenc_context.fei_mvpred_buf_size = 0;
+ avcenc_context.current_input_surface = SID_INPUT_PICTURE_0;
+ avcenc_context.upload_thread_value = -1;
+
+ if (qp_value == -1)
+ avcenc_context.rate_control_method = VA_RC_CBR;
+ else if (qp_value == -2)
+ avcenc_context.rate_control_method = VA_RC_VBR;
+ else {
+ assert(qp_value >= 0 && qp_value <= 51);
+ avcenc_context.rate_control_method = VA_RC_CQP;
+ }
+
+ for (i = 0; i < MAX_SLICES; i++) {
+ avcenc_context.slice_param_buf_id[i] = VA_INVALID_ID;
+ }
+
+ avcenc_context_seq_param_init(&avcenc_context.seq_param, width, height);
+ avcenc_context_pic_param_init(&avcenc_context.pic_param);
+
+ // init FEI pic level parameters
+ avcenc_context.fei_pic_param.function = VA_ENC_FUNCTION_ENC_PAK_INTEL;
+ avcenc_context.fei_pic_param.num_mv_predictors = 1;
+ avcenc_context.fei_pic_param.max_len_sp = 57;
+ avcenc_context.fei_pic_param.len_sp = 57;
+ avcenc_context.fei_pic_param.sub_mb_part_mask = 0x77; //1110111b
+ avcenc_context.fei_pic_param.intra_part_mask = 0;
+ avcenc_context.fei_pic_param.multi_pred_l0 = 0;
+ avcenc_context.fei_pic_param.multi_pred_l1 = 0;
+ avcenc_context.fei_pic_param.sub_pel_mode = 3;
+ avcenc_context.fei_pic_param.inter_sad = 2;
+ avcenc_context.fei_pic_param.intra_sad = 2;
+ avcenc_context.fei_pic_param.distortion_type = 0;
+ avcenc_context.fei_pic_param.repartition_check_enable = 0;
+ avcenc_context.fei_pic_param.adaptive_search = 1;
+ avcenc_context.fei_pic_param.mv_predictor_enable = 1;
+ avcenc_context.fei_pic_param.mb_qp = 0;
+ avcenc_context.fei_pic_param.mb_input = 1;
+ avcenc_context.fei_pic_param.mb_size_ctrl = 0;
+ avcenc_context.fei_pic_param.ref_width = 48;
+ avcenc_context.fei_pic_param.ref_height = 40;
+ avcenc_context.fei_pic_param.search_window = 0;
+
+ avcenc_context.fei_pic_param.distortion = avcenc_context.fei_distortion_buf_id;
+ avcenc_context.fei_pic_param.mv_data = avcenc_context.fei_mv_buf_id;
+ avcenc_context.fei_pic_param.mb_code_data = avcenc_context.fei_mbcode_buf_id;
+
+ if(avcenc_context.fei_pic_param.mb_input)
+ avcenc_context.fei_mbctrl_buf_size = picture_width_in_mbs * picture_height_in_mbs * sizeof(VAEncFEIMBControlH264Intel);
+ if(avcenc_context.fei_pic_param.mv_predictor_enable)
+ avcenc_context.fei_mvpred_buf_size = picture_width_in_mbs * picture_height_in_mbs * sizeof(VAEncMVPredictorH264Intel);
+}
+
+int main(int argc, char *argv[])
+{
+ unsigned char inputyuv[256];
+ unsigned char outputstream[256];
+ int i = 0;
+ int f;
+ FILE *yuv_fp;
+ FILE *avc_fp;
+ long file_size;
+ int enc_frames = 0;
+ int i_frame_only=0,i_p_frame_only=1;
+ int mode_value;
+ struct timeval tpstart,tpend;
+ float timeuse;
+
+ picture_width = 352;
+ picture_height = 288;
+ qp_value = 28; //default const QP mode
+ for (i=1; i<argc; i++)
+ {
+ //printf("---> i=%d, argv[i]=%s \n", i, argv[i]);
+ if(strcmp(argv[i],"-size")==0)
+ {
+ if(i+2<argc)
+ {
+ picture_width = atoi(argv[i+1]);
+ picture_height = atoi(argv[i+2]);
+ i += 2;
+ } else
+ {
+ show_help();
+ return -1;
+ }
+ } else if(strcmp(argv[i],"-i")==0)
+ {
+ if(i+1<argc)
+ {
+ strcpy(inputyuv, argv[i+1]);
+ i++;
+ //printf("---> argv[i]=%s, inputyuv=%s \n", argv[i+1], inputyuv);
+ } else
+ {
+ show_help();
+ return -1;
+ }
+ } else if(strcmp(argv[i],"-o")==0)
+ {
+ if(i+1<argc)
+ {
+ strcpy(outputstream, argv[i+1]);
+ i++;
+ //printf("---> argv[i]=%s, outputstream=%s \n", argv[i+1], outputstream);
+ } else
+ {
+ show_help();
+ return -1;
+ }
+
+ } else if(strcmp(argv[i],"-qp")==0)
+ {
+ if(i+1<argc)
+ {
+ qp_value = atoi(argv[i+1]);
+ i++;
+ } else
+ {
+ show_help();
+ return -1;
+ }
+ } else if(strcmp(argv[i],"-fb")==0)
+ {
+ if(i+1<argc)
+ {
+ frame_bit_rate = atoi(argv[i+1]);
+ printf("frame_bit_rate: disregarded, FEI should have no notion for bitrate!\n");
+ frame_bit_rate = -1; // force to -1 to match correct logic
+ i++;
+ } else
+ {
+ show_help();
+ return -1;
+ }
+ } else if(strcmp(argv[i],"-mode")==0)
+ {
+ if(i+1<argc)
+ {
+ mode_value = atoi(argv[i+1]);
+ i++;
+ if ( mode_value == 0 )
+ {
+ i_frame_only = 1;
+ i_p_frame_only = 0;
+ intra_period = 1;
+ ip_period = 1;
+ } else if ( mode_value == 1 )
+ {
+ i_frame_only = 0;
+ i_p_frame_only = 1;
+ intra_period = 30;
+ ip_period = 1;
+ }
+ else if ( mode_value == 2 )
+ {
+ i_frame_only = 0;
+ i_p_frame_only = 0;
+ intra_period = 30;
+ ip_period = 3;
+ }
+ else {
+ printf("mode_value=%d\n",mode_value);
+ show_help();
+ return -1;
+ }
+ }
+ } else
+ if(strcmp(argv[i],"-frames")==0)
+ {
+ if(i+1<argc)
+ {
+ enc_frames = atoi(argv[i+1]);
+ i++;
+ } else
+ {
+ show_help();
+ return -1;
+ }
+ } else
+ if(strcmp(argv[i],"-stats")==0)
+ {
+ if(i+1<argc)
+ {
+ stats_en = (atoi(argv[i+1]) != 0);
+ i++;
+ } else
+ {
+ show_help();
+ return -1;
+ }
+ } else
+ {
+ show_help();
+ return -1;
+ }
+ }
+
+ picture_width_in_mbs = (picture_width + 15) / 16;
+ picture_height_in_mbs = (picture_height + 15) / 16;
+
+ if((qp_value == -1) && (frame_bit_rate = -1))
+ {
+ show_help();
+ return -1;
+ } else if (qp_value > 51)
+ {
+ qp_value = 51;
+ } else if (qp_value < 0) {
+ qp_value = 0;
+ }
+
+ yuv_fp = fopen(inputyuv,"rb");
+ if ( yuv_fp == NULL){
+ printf("Can't open input YUV file\n");
+ show_help();
+ return -1;
+ }
+ fseek(yuv_fp,0l, SEEK_END);
+ file_size = ftell(yuv_fp);
+ frame_size = picture_width * picture_height + ((picture_width * picture_height) >> 1) ;
+
+ if ( (file_size < frame_size) || (file_size % frame_size) ) {
+ fclose(yuv_fp);
+ printf("The YUV file's size is not correct\n");
+ show_help();
+ return -1;
+ }
+ if(enc_frames > 0 )
+ file_size = enc_frames * frame_size; //default 0 run all frames, or run enc_frames frames.
+ frame_number = file_size / frame_size;
+ fseek(yuv_fp, 0l, SEEK_SET);
+
+ avc_fp = fopen(outputstream, "wb");
+ if ( avc_fp == NULL) {
+ fclose(yuv_fp);
+ printf("Can't open output avc file\n");
+ show_help();
+ return -1;
+ }
+ gettimeofday(&tpstart,NULL);
+ avcenc_context_init(picture_width, picture_height);
+ create_encode_pipe();
+ alloc_encode_resource(yuv_fp);
+
+ enc_frame_number = 0;
+ for ( f = 0; f < frame_number; ) { //picture level loop
+ static int const frame_type_pattern[][2] = { {SLICE_TYPE_I,1},
+ {SLICE_TYPE_P,3}, {SLICE_TYPE_P,3},{SLICE_TYPE_P,3},
+ {SLICE_TYPE_P,3}, {SLICE_TYPE_P,3},{SLICE_TYPE_P,3},
+ {SLICE_TYPE_P,3}, {SLICE_TYPE_P,3},{SLICE_TYPE_P,3},
+ {SLICE_TYPE_P,2} };
+
+ if ( i_frame_only ) {
+ encode_picture(yuv_fp, avc_fp,enc_frame_number, f, f==0, SLICE_TYPE_I, 0, f+1);
+ f++;
+ enc_frame_number++;
+ } else if ( i_p_frame_only ) {
+ if ( (f % intra_period) == 0 ) {
+ encode_picture(yuv_fp, avc_fp,enc_frame_number, f, 1, SLICE_TYPE_I, 0, f+1);
+ f++;
+ enc_frame_number++;
+ } else {
+ encode_picture(yuv_fp, avc_fp,enc_frame_number, f, 0, SLICE_TYPE_P, 0, f+1);
+ f++;
+ enc_frame_number++;
+ }
+ } else { // follow the i,p,b pattern
+ static int fcurrent = 0;
+ int fnext;
+
+ fcurrent = fcurrent % (sizeof(frame_type_pattern)/sizeof(int[2]));
+ fnext = (fcurrent+1) % (sizeof(frame_type_pattern)/sizeof(int[2]));
+
+ if ( frame_type_pattern[fcurrent][0] == SLICE_TYPE_I ) {
+ encode_picture(yuv_fp, avc_fp,enc_frame_number, f, 1, SLICE_TYPE_I, 0,
+ f+frame_type_pattern[fnext][1]);
+
+ f++;
+ enc_frame_number++;
+ } else {
+ encode_pb_pictures(yuv_fp, avc_fp, f, frame_type_pattern[fcurrent][1]-1,
+ f + frame_type_pattern[fcurrent][1] + frame_type_pattern[fnext][1] -1 );
+ f += frame_type_pattern[fcurrent][1];
+ enc_frame_number++;
+ }
+
+ fcurrent++;
+ }
+ //printf("\r ---> encoded %d/%d ...", f+1, frame_number);
+ fflush(stdout);
+ }
+
+ gettimeofday(&tpend,NULL);
+ timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+ tpend.tv_usec-tpstart.tv_usec;
+ timeuse/=1000000;
+ printf("\ndone!\n");
+ printf("encode %d frames in %f secondes, FPS is %.1f\n",frame_number, timeuse, frame_number/timeuse);
+ release_encode_resource();
+ destory_encode_pipe();
+
+ fclose(yuv_fp);
+ fclose(avc_fp);
+
+ return 0;
+}
diff --git a/test/encode/preenc.c b/test/encode/preenc.c
new file mode 100644
index 0000000..a605e49
--- /dev/null
+++ b/test/encode/preenc.c
@@ -0,0 +1,895 @@
+/*
+ * Simple AVC encoder based on libVA.
+ *
+ * Usage:
+ * ./preenc -size <width> <height> -i <input_yuvfile> -mode [mode=0(P frames only)/mode=1(B frames)]
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include <X11/Xlib.h>
+
+#include <unistd.h>
+
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <assert.h>
+#include <time.h>
+
+#include <pthread.h>
+
+#include <va/va.h>
+#include <va/va_enc_h264.h>
+#include <va/vendor/intel/va_intel_statistics.h>
+#include <va/vendor/intel/va_intel_fei.h>
+#include <va/va_x11.h>
+#include "va/drm/va_drm.h"
+
+#define SLICE_TYPE_P 0
+#define SLICE_TYPE_B 1
+#define SLICE_TYPE_I 2
+
+#define CHECK_VASTATUS(va_status,func) \
+ if (va_status != VA_STATUS_SUCCESS) { \
+ fprintf(stderr,"%s:%s (%d) failed,exit\n", __func__, func, __LINE__); \
+ exit(1); \
+ }
+
+#define MIN(a, b) ((a)>(b)?(b):(a))
+
+static VADisplay va_dpy;
+
+static int picture_width, picture_width_in_mbs;
+static int picture_height, picture_height_in_mbs;
+static int frame_size;
+static unsigned char *newImageBuffer = 0;
+static unsigned char *pMvPredInBuffer = 0;
+static unsigned char *pMbQpInBuffer = 0;
+
+static VAStatsStatisticsParameter16x16Intel pic_param_back;
+
+struct upload_thread_param
+{
+ FILE *yuv_fp;
+ VASurfaceID surface_id;
+};
+
+// mv_predictor or qp
+struct upload_input_thread_param
+{
+ FILE *input_fp;
+ int size;
+ VASurfaceID surface_id;
+};
+
+static struct {
+ VAProfile profile;
+ VAStatsStatisticsParameter16x16Intel pic_param;
+ VAContextID context_id;
+ VAConfigID config_id;
+ VABufferID pic_param_buf_id;
+ VABufferID mvpred_buf_id;
+ VABufferID mbqp_buf_id;
+ VABufferID out_buf_id[2]; // 0 for mv out and 1 for stats out buffer if both mv and stats out enabled
+
+ int mvpred_buf_size;
+ int mbqp_buf_size;
+ int mvout_buf_size;
+ int statsout_buf_size;
+ int current_input_surface;
+ struct upload_thread_param upload_thread_param;
+ struct upload_input_thread_param upload_input_thread_param;
+ pthread_t upload_thread_id;
+ int upload_thread_value;
+} preenc_context;
+
+
+static FILE *yuv_fp = 0;
+static FILE *mvpredin_fp = 0;
+static FILE *qpin_fp = 0;
+
+static FILE *mvout_fp = 0;
+static FILE *statsout_fp = 0;
+
+static void show_help()
+{
+ printf("Usage: preenc -size <width> <height> -i <input_yuvfile> -frames <#frames to process, >=2, default runs all frames> -bdistance <distance between I/P and P, 1 means no B> -gop_lenght <I period>\n");
+}
+
+static void preenc_context_init()
+{
+ memset(&preenc_context, 0, sizeof(preenc_context));
+ preenc_context.profile = VAProfileNone;
+ preenc_context.pic_param_buf_id = VA_INVALID_ID;
+ preenc_context.mvpred_buf_id = VA_INVALID_ID;
+ preenc_context.mbqp_buf_id = VA_INVALID_ID;
+ preenc_context.out_buf_id[0] = VA_INVALID_ID;
+ preenc_context.out_buf_id[1] = VA_INVALID_ID;
+ preenc_context.mvpred_buf_size = 0;
+ preenc_context.mbqp_buf_size = 0;
+ preenc_context.mvout_buf_size = picture_width_in_mbs * picture_height_in_mbs * 16 * sizeof(VAMotionVectorIntel);
+ preenc_context.statsout_buf_size = picture_width_in_mbs * picture_height_in_mbs * sizeof(VAStatsStatistics16x16Intel);
+
+ preenc_context.current_input_surface = 2;//SID_REFERENCE_PICTURE_L0;
+ preenc_context.upload_thread_value = -1;
+
+
+ // init picture level params
+ preenc_context.pic_param.num_past_references = 1;
+ preenc_context.pic_param.num_future_references = 0;
+ preenc_context.pic_param.frame_qp = 27;
+ preenc_context.pic_param.len_sp = 57;
+ preenc_context.pic_param.max_len_sp = 57;
+ preenc_context.pic_param.sub_mb_part_mask = 0x77; //1110111b
+ preenc_context.pic_param.sub_pel_mode = 3;
+ preenc_context.pic_param.inter_sad = 2;
+ preenc_context.pic_param.intra_sad = 2;
+ preenc_context.pic_param.adaptive_search = 1;
+ preenc_context.pic_param.mv_predictor_ctrl = 1;
+ preenc_context.pic_param.mb_qp = 1;
+ preenc_context.pic_param.ft_enable = 0;
+ preenc_context.pic_param.ref_width = 48;
+ preenc_context.pic_param.ref_height = 40;
+ preenc_context.pic_param.search_window = 0;
+ preenc_context.pic_param.disable_mv_output = 0;
+ preenc_context.pic_param.disable_statistics_output = 0;
+ preenc_context.pic_param.interlaced = 0;
+
+ if(preenc_context.pic_param.mv_predictor_ctrl != 0)
+ preenc_context.mvpred_buf_size = picture_width_in_mbs * picture_height_in_mbs * sizeof(VAMotionVectorIntel);
+ if(preenc_context.pic_param.mb_qp != 0)
+ preenc_context.mbqp_buf_size = picture_width_in_mbs * picture_height_in_mbs * sizeof(VAEncQpBufferH264);
+}
+
+static int drm_fd = -1;
+static void create_preenc_pipe()
+{
+ VAEntrypoint entrypoints[5];
+ int num_entrypoints = 1;
+ VAConfigAttrib attrib[2];
+ int major_ver, minor_ver;
+ VAStatus va_status;
+
+ drm_fd = open("/dev/dri/card0", O_RDWR);
+ if (drm_fd < 0)
+ {
+ fprintf(stderr, "error: can't open DRM connection!\n");
+ return;
+ }
+ va_dpy = vaGetDisplayDRM(drm_fd);
+ //x11_display = XOpenDisplay(":0.0");
+ //assert(x11_display);
+
+ //va_dpy = vaGetDisplay(x11_display);
+ va_status = vaInitialize(va_dpy, &major_ver, &minor_ver);
+ CHECK_VASTATUS(va_status, "vaInitialize");
+
+ vaQueryConfigEntrypoints(va_dpy, preenc_context.profile, entrypoints,
+ &num_entrypoints);
+
+ /* find out the format for the render target, and rate control mode */
+ attrib[0].type = VAConfigAttribStatisticsIntel;
+ vaGetConfigAttributes(va_dpy, preenc_context.profile, VAEntrypointStatisticsIntel,
+ &attrib[0], 1);
+
+ if ((attrib[0].value & 0xff) == 0) {
+ /* not support past/future reference */
+ assert(0);
+ }
+
+ /* set to default mode */
+ attrib[0].value = preenc_context.pic_param.num_past_references |
+ (preenc_context.pic_param.num_future_references << 4) |
+ ((2 - preenc_context.pic_param.disable_mv_output - preenc_context.pic_param.disable_statistics_output) << 8) |
+ (preenc_context.pic_param.interlaced << 11);
+
+ va_status = vaCreateConfig(va_dpy, preenc_context.profile, VAEntrypointStatisticsIntel,
+ &attrib[0], 1, &preenc_context.config_id);
+ CHECK_VASTATUS(va_status, "vaCreateConfig");
+
+}
+
+static void destory_preenc_pipe()
+{
+ vaDestroyContext(va_dpy,preenc_context.context_id);
+ vaDestroyConfig(va_dpy,preenc_context.config_id);
+ vaTerminate(va_dpy);
+ if (drm_fd < 0)
+ return;
+ close(drm_fd);
+ drm_fd = -1;
+ //XCloseDisplay(x11_display);
+}
+
+/***************************************************
+ *
+ * The preenc pipe resource define
+ *
+ ***************************************************/
+#define SID_INPUT_PICTURE_0 0
+#define SID_INPUT_PICTURE_1 1
+#define SID_REFERENCE_PICTURE_L0 2
+#define SID_REFERENCE_PICTURE_L1 3
+
+
+#define SID_NUMBER 4
+static VASurfaceID surface_ids[SID_NUMBER];
+
+static int frame_number;
+
+/***************************************************/
+#ifndef VA_FOURCC_I420
+#define VA_FOURCC_I420 0x30323449
+#endif
+
+static void upload_yuv_to_surface(FILE *yuv_fp, VASurfaceID surface_id)
+{
+ VAImage surface_image;
+ VAStatus va_status;
+ void *surface_p = NULL;
+ unsigned char *y_src, *u_src, *v_src;
+ unsigned char *y_dst, *u_dst, *v_dst;
+ int y_size = picture_width * picture_height;
+ int u_size = (picture_width >> 1) * (picture_height >> 1);
+ int row, col;
+ size_t n_items;
+
+ do {
+ n_items = fread(newImageBuffer, frame_size, 1, yuv_fp);
+ } while (n_items != 1);
+
+ va_status = vaDeriveImage(va_dpy, surface_id, &surface_image);
+ CHECK_VASTATUS(va_status,"vaDeriveImage");
+
+ vaMapBuffer(va_dpy, surface_image.buf, &surface_p);
+ assert(VA_STATUS_SUCCESS == va_status);
+
+ y_src = newImageBuffer;
+ u_src = newImageBuffer + y_size; /* UV offset for NV12 */
+ v_src = newImageBuffer + y_size + u_size;
+
+ y_dst = surface_p + surface_image.offsets[0];
+ u_dst = surface_p + surface_image.offsets[1]; /* UV offset for NV12 */
+ v_dst = surface_p + surface_image.offsets[2];
+
+ /* Y plane */
+ memset(y_dst, 0, surface_image.pitches[0]*surface_image.height);
+ for (row = 0; row < picture_height; row++) {
+ memcpy(y_dst, y_src, picture_width);
+ y_dst += surface_image.pitches[0];
+ y_src += picture_width;
+ }
+
+ if (surface_image.format.fourcc == VA_FOURCC_NV12) { /* UV plane */
+ memset(u_dst, 0, surface_image.pitches[1]*(surface_image.height/2));
+ for (row = 0; row < picture_height / 2; row++) {
+ for (col = 0; col < picture_width / 2; col++) {
+ u_dst[col * 2] = u_src[col];
+ u_dst[col * 2 + 1] = v_src[col];
+ }
+
+ u_dst += surface_image.pitches[1];
+ u_src += (picture_width / 2);
+ v_src += (picture_width / 2);
+ }
+ } else if (surface_image.format.fourcc == VA_FOURCC_YV12 ||
+ surface_image.format.fourcc == VA_FOURCC_I420) {
+ const int U = surface_image.format.fourcc == VA_FOURCC_I420 ? 1 : 2;
+ const int V = surface_image.format.fourcc == VA_FOURCC_I420 ? 2 : 1;
+
+ u_dst = surface_p + surface_image.offsets[U];
+ v_dst = surface_p + surface_image.offsets[V];
+
+ for (row = 0; row < picture_height / 2; row++) {
+ memcpy(u_dst, u_src, picture_width / 2);
+ memcpy(v_dst, v_src, picture_width / 2);
+ u_dst += surface_image.pitches[U];
+ v_dst += surface_image.pitches[V];
+ u_src += (picture_width / 2);
+ v_src += (picture_width / 2);
+ }
+ }
+
+ vaUnmapBuffer (va_dpy, surface_image.buf);
+ vaDestroyImage(va_dpy, surface_image.image_id);
+}
+
+static void *
+upload_thread_function(void *data)
+{
+ struct upload_thread_param *param = data;
+
+ upload_yuv_to_surface(param->yuv_fp, param->surface_id);
+
+ return NULL;
+}
+
+static void *
+upload_input_thread_function(void *data)
+{
+// struct upload_inpyut_thread_param *param = data;
+
+// upload_input_to_surface(param->input_fp, param->surface_id, param->size);
+
+ return NULL;
+}
+
+static void alloc_preenc_resource()
+{
+ VAStatus va_status;
+
+ // Create surface
+ va_status = vaCreateSurfaces(
+ va_dpy,
+ VA_RT_FORMAT_YUV420, picture_width_in_mbs*16, picture_height_in_mbs*16,
+ &surface_ids[0], SID_NUMBER,
+ NULL, 0
+ );
+
+ CHECK_VASTATUS(va_status, "vaCreateSurfaces");
+
+ /* Create a context for this decode pipe */
+ va_status = vaCreateContext(va_dpy, preenc_context.config_id,
+ picture_width_in_mbs*16, picture_height_in_mbs*16,
+ VA_PROGRESSIVE,
+ &surface_ids[0],
+ SID_NUMBER,
+ &preenc_context.context_id);
+ CHECK_VASTATUS(va_status, "vaCreateContext");
+ newImageBuffer = (unsigned char *)malloc(frame_size);
+
+ if(preenc_context.pic_param.mv_predictor_ctrl != 0)
+ {
+ pMvPredInBuffer = (unsigned char *)malloc(preenc_context.mvpred_buf_size);
+ memset(pMvPredInBuffer, 0, preenc_context.mvpred_buf_size);
+ }
+
+ if(preenc_context.pic_param.mb_qp != 0)
+ {
+ pMbQpInBuffer = (unsigned char *)malloc(preenc_context.mbqp_buf_size);
+ memset(pMbQpInBuffer, 0, preenc_context.mbqp_buf_size);
+ }
+
+}
+
+static int preenc_destroy_buffers(VABufferID *va_buffers, unsigned int num_va_buffers)
+{
+ VAStatus va_status;
+ unsigned int i;
+
+ for (i = 0; i < num_va_buffers; i++) {
+ if (va_buffers[i] != VA_INVALID_ID) {
+ va_status = vaDestroyBuffer(va_dpy, va_buffers[i]);
+ CHECK_VASTATUS(va_status,"vaDestroyBuffer");
+ va_buffers[i] = VA_INVALID_ID;
+ }
+ }
+
+ return 0;
+}
+
+static void release_preenc_resource()
+{
+ //preenc_destroy_buffers(&preenc_context.pic_param_buf_id, 1);
+ free(newImageBuffer);
+ if(pMvPredInBuffer)
+ free(pMvPredInBuffer);
+ if(pMbQpInBuffer)
+ free(pMbQpInBuffer);
+
+ // Release all the surfaces resource
+ vaDestroySurfaces(va_dpy, &surface_ids[0], SID_NUMBER);
+}
+
+static int begin_picture(FILE *yuv_fp, int frame_num, int slice_type)
+{
+ VAStatus va_status;
+
+ VAStatsStatisticsParameter16x16Intel *pic_param = &preenc_context.pic_param;
+
+ preenc_context.current_input_surface = SID_INPUT_PICTURE_0;
+ if(frame_num == 0)
+ {
+ assert(slice_type == SLICE_TYPE_I);
+ return 0;
+ }
+
+ if(pic_param->mv_predictor_ctrl != 0)
+ {
+ // init input MV prediction buffer, FIXME reading from files
+ VAMotionVectorIntel *pMvPred = pMvPredInBuffer;
+ int i = 0;
+ memset(pMvPred, 0, preenc_context.mvpred_buf_size);
+
+ for(i = 0; i<(picture_width_in_mbs*picture_height_in_mbs); i++)
+ {
+ pMvPred->mv0[0] = 1;
+ pMvPred->mv0[1] = 1;
+ pMvPred++;
+ }
+#if 1
+ {
+ FILE* out = NULL;
+
+ out = fopen("mvpindata.bin", "wb");
+ fwrite(pMvPred, 1, preenc_context.mvpred_buf_size, out);
+ fclose(out);
+ }
+#endif
+ }
+
+ if(pic_param->mb_qp != 0)
+ {
+ // init input MB QP buffer, FIXME reading from files
+ VAEncQpBufferH264 *pQpPred = pMbQpInBuffer;
+ memset(pQpPred, 20, preenc_context.mbqp_buf_size);
+#if 1
+ {
+ FILE* out = NULL;
+
+ out = fopen("qpindata.bin", "wb");
+ fwrite(pQpPred, 1, preenc_context.mbqp_buf_size, out);
+ fclose(out);
+ }
+#endif
+ }
+
+ return 0;
+}
+
+static void preenc_update_picture_parameter(int slice_type, int frame_num)
+{
+ VAStatsStatisticsParameter16x16Intel *pic_param;
+ VAStatus va_status;
+
+ // Picture level
+ pic_param = &preenc_context.pic_param;
+ pic_param->input = surface_ids[SID_INPUT_PICTURE_0];
+ if(slice_type == SLICE_TYPE_I)
+ {
+ pic_param->num_past_references = 0;
+ pic_param->num_future_references = 0;
+ }
+ else if(slice_type == SLICE_TYPE_P)
+ {
+ pic_param->num_past_references = 1;
+ pic_param->num_future_references = 0;
+ pic_param->past_references = &surface_ids[SID_REFERENCE_PICTURE_L0];
+ }
+ else if(slice_type == SLICE_TYPE_B)
+ {
+ pic_param->num_past_references = 1;
+ pic_param->num_future_references = 1;
+ pic_param->past_references = &surface_ids[SID_REFERENCE_PICTURE_L0];
+ pic_param->future_references = &surface_ids[SID_REFERENCE_PICTURE_L1];
+ }
+
+
+ if(!pic_param->disable_mv_output || !pic_param->disable_statistics_output)
+ pic_param->outputs = preenc_context.out_buf_id;
+ else
+ pic_param->outputs = 0;
+
+
+ if(pic_param->mv_predictor_ctrl != 0)
+ {
+ va_status = vaCreateBuffer(va_dpy,
+ preenc_context.context_id,
+ VAStatsMVPredictorBufferTypeIntel,
+ sizeof(VAMotionVectorIntel), picture_width_in_mbs*picture_height_in_mbs, pMvPredInBuffer,
+ &preenc_context.mvpred_buf_id);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");
+ pic_param->mv_predictor = preenc_context.mvpred_buf_id;
+ }
+
+ if(pic_param->mb_qp != 0)
+ {
+ va_status = vaCreateBuffer(va_dpy,
+ preenc_context.context_id,
+ VAEncQpBufferType,
+ sizeof(VAEncQpBufferH264), picture_width_in_mbs*picture_height_in_mbs, pMbQpInBuffer,
+ &preenc_context.mbqp_buf_id);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");
+ pic_param->qp = preenc_context.mbqp_buf_id;
+ }
+
+ va_status = vaCreateBuffer(va_dpy,
+ preenc_context.context_id,
+ VAStatsStatisticsParameterBufferTypeIntel,
+ sizeof(*pic_param), 1, pic_param,
+ &preenc_context.pic_param_buf_id);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");
+
+}
+
+static int preenc_render_picture()
+{
+ VAStatus va_status;
+ VABufferID va_buffers[10];
+ unsigned int num_va_buffers = 0;
+
+
+ va_buffers[num_va_buffers++] = preenc_context.pic_param_buf_id;
+ if(preenc_context.pic_param.mv_predictor_ctrl != 0)
+ va_buffers[num_va_buffers++] = preenc_context.mvpred_buf_id;
+ if(preenc_context.pic_param.mb_qp != 0)
+ va_buffers[num_va_buffers++] = preenc_context.mbqp_buf_id;
+
+ va_status = vaBeginPicture(va_dpy,
+ preenc_context.context_id,
+ surface_ids[preenc_context.current_input_surface]);
+ CHECK_VASTATUS(va_status,"vaBeginPicture");
+
+ va_status = vaRenderPicture(va_dpy,
+ preenc_context.context_id,
+ va_buffers,
+ num_va_buffers);
+ CHECK_VASTATUS(va_status,"vaRenderPicture");
+
+ va_status = vaEndPicture(va_dpy, preenc_context.context_id);
+ CHECK_VASTATUS(va_status,"vaEndPicture");
+
+ return 0;
+}
+
+
+static void download_buffer_to_file(FILE *out_fp, VABufferID buf_id, int size)
+{
+ unsigned char *pbuf;
+ VAStatus va_status;
+ size_t n_items;
+
+
+ va_status = vaMapBuffer(va_dpy, buf_id, (void **)(&pbuf));
+ CHECK_VASTATUS(va_status,"vaMapBuffer");
+
+ do {
+ n_items = fwrite(pbuf, size, 1, out_fp);
+ //printf("--> n_items = %d\n", n_items);
+ }
+ while (n_items != 1);
+
+ va_status = vaUnmapBuffer(va_dpy, buf_id);
+ CHECK_VASTATUS(va_status,"vaUnmapBuffer");
+}
+
+static int store_output_buffer()
+{
+ VAStatus va_status;
+ VASurfaceStatus surface_status;
+ int i = 0;
+
+ va_status = vaSyncSurface(va_dpy, surface_ids[preenc_context.current_input_surface]);
+ CHECK_VASTATUS(va_status,"vaSyncSurface");
+ surface_status = 0;
+ va_status = vaQuerySurfaceStatus(va_dpy, surface_ids[preenc_context.current_input_surface], &surface_status);
+ CHECK_VASTATUS(va_status,"vaQuerySurfaceStatus");
+
+ // get MV output surface data
+ if(!preenc_context.pic_param.disable_mv_output)
+ {
+ download_buffer_to_file(mvout_fp, preenc_context.out_buf_id[i++], preenc_context.mvout_buf_size);
+ }
+
+ // get Statistics output surface data
+ if(!preenc_context.pic_param.disable_statistics_output)
+ {
+ download_buffer_to_file(statsout_fp, preenc_context.out_buf_id[i++], preenc_context.statsout_buf_size);
+ }
+
+ return 0;
+}
+
+static int GetNextInput(int current_input)
+{
+// if (current_input == SID_REFERENCE_PICTURE_L0)
+// return SID_INPUT_PICTURE_0;
+// if (current_input == SID_INPUT_PICTURE_0)
+ return SID_INPUT_PICTURE_0;
+}
+
+static void end_picture(int slice_type, int next_bpic_num)
+{
+ VABufferID tempID;
+ int i = 0;
+
+ /* Prepare for next picture */
+ tempID = surface_ids[SID_INPUT_PICTURE_0];
+
+ if (slice_type == SLICE_TYPE_I) {
+ surface_ids[SID_INPUT_PICTURE_0] = surface_ids[SID_REFERENCE_PICTURE_L0];
+ surface_ids[SID_REFERENCE_PICTURE_L0] = tempID;
+ } else
+ if (slice_type != SLICE_TYPE_B) {
+ if (next_bpic_num) {
+ surface_ids[SID_INPUT_PICTURE_0] = surface_ids[SID_REFERENCE_PICTURE_L1];
+ surface_ids[SID_REFERENCE_PICTURE_L1] = tempID;
+ } else {
+ surface_ids[SID_INPUT_PICTURE_0] = surface_ids[SID_REFERENCE_PICTURE_L0];
+ surface_ids[SID_REFERENCE_PICTURE_L0] = tempID;
+ }
+ } else {
+ if (next_bpic_num == 0) {
+ surface_ids[SID_INPUT_PICTURE_0] = surface_ids[SID_REFERENCE_PICTURE_L0];
+ surface_ids[SID_REFERENCE_PICTURE_L0] = surface_ids[SID_REFERENCE_PICTURE_L1];
+ surface_ids[SID_REFERENCE_PICTURE_L1] = tempID;
+ }
+ }
+
+ preenc_destroy_buffers(&preenc_context.pic_param_buf_id, 1);
+ if(preenc_context.pic_param.mv_predictor_ctrl != 0)
+ preenc_destroy_buffers(&preenc_context.mvpred_buf_id, 1);
+ if(preenc_context.pic_param.mb_qp != 0)
+ preenc_destroy_buffers(&preenc_context.mbqp_buf_id, 1);
+ if(!preenc_context.pic_param.disable_mv_output)
+ preenc_destroy_buffers(&preenc_context.out_buf_id[i++], 1);
+ if(!preenc_context.pic_param.disable_statistics_output)
+ preenc_destroy_buffers(&preenc_context.out_buf_id[i++], 1);
+}
+
+static void
+preenc_picture(int frame_num, int slice_type, int next_bpic_num)
+{
+ VAStatus va_status;
+ int ret = 0;
+ int index;
+
+ printf("---> preenc picture %d, type=%d, next_bpic_num=%d \n", frame_num, slice_type, next_bpic_num);
+
+ pic_param_back = preenc_context.pic_param;
+ if(slice_type == SLICE_TYPE_I)
+ {
+ preenc_context.pic_param.disable_mv_output = 1;
+ }
+ begin_picture(yuv_fp, frame_num, slice_type);
+
+ index = SID_INPUT_PICTURE_0;
+ if(slice_type == SLICE_TYPE_P)
+ fseek(yuv_fp, frame_size * (frame_num + next_bpic_num), SEEK_SET); // skip B frame to P frame
+ else if(slice_type == SLICE_TYPE_B)
+ fseek(yuv_fp, frame_size * (frame_num - 1), SEEK_SET); // inserted one P frame before B frames, so minus 1.
+ else
+ fseek(yuv_fp, frame_size * frame_num, SEEK_SET);
+ preenc_context.upload_thread_param.yuv_fp = yuv_fp;
+ preenc_context.upload_thread_param.surface_id = surface_ids[index];
+ upload_thread_function((void *)(&preenc_context.upload_thread_param));
+ usleep(10000);
+
+ do {
+ int i = 0;
+ preenc_destroy_buffers(&preenc_context.pic_param_buf_id, 1);
+ if(preenc_context.pic_param.mv_predictor_ctrl != 0)
+ preenc_destroy_buffers(&preenc_context.mvpred_buf_id, 1);
+ if(preenc_context.pic_param.mb_qp != 0)
+ preenc_destroy_buffers(&preenc_context.mbqp_buf_id, 1);
+
+ if(!preenc_context.pic_param.disable_mv_output)
+ {
+ preenc_destroy_buffers(&preenc_context.out_buf_id[i], 1);
+ va_status = vaCreateBuffer(va_dpy,
+ preenc_context.context_id,
+ VAStatsMotionVectorBufferTypeIntel,
+ sizeof(VAMotionVectorIntel), picture_width_in_mbs*picture_height_in_mbs*16, NULL,
+ &preenc_context.out_buf_id[i++]);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");
+ }
+ if(!preenc_context.pic_param.disable_statistics_output)
+ {
+ preenc_destroy_buffers(&preenc_context.out_buf_id[i], 1);
+ va_status = vaCreateBuffer(va_dpy,
+ preenc_context.context_id,
+ VAStatsStatisticsBufferTypeIntel,
+ sizeof(VAStatsStatistics16x16Intel), picture_width_in_mbs*picture_height_in_mbs, NULL,
+ &preenc_context.out_buf_id[i++]);
+ CHECK_VASTATUS(va_status,"vaCreateBuffer");
+ }
+
+ /* picture parameter set */
+ preenc_update_picture_parameter(slice_type, frame_num);
+
+ preenc_render_picture();
+ usleep(10000);
+ ret = store_output_buffer();
+ } while (ret);
+
+ end_picture(slice_type, next_bpic_num);
+ preenc_context.pic_param = pic_param_back;
+}
+
+int main(int argc, char *argv[])
+{
+ unsigned char inputyuv[256];
+ int i = 0;
+ int f;
+ long file_size;
+ int mode_value;
+ int enc_frames = 0;
+ int bdistance = 1; //Distance between I- or P- key frames (1 means no B-frames) (0 - by default(I frames) not allowed in this test)
+ int gop_length = -1;
+ struct timeval tpstart,tpend;
+ float timeuse;
+
+ picture_width = 352;
+ picture_height = 288;
+ for (i=1; i<argc; i++)
+ {
+ //printf("---> i=%d, argv[i]=%s \n", i, argv[i]);
+ if(strcmp(argv[i],"-size")==0)
+ {
+ if(i+2<argc)
+ {
+ picture_width = atoi(argv[i+1]);
+ picture_height = atoi(argv[i+2]);
+ i += 2;
+ } else
+ {
+ show_help();
+ return -1;
+ }
+ } else
+ if(strcmp(argv[i],"-i")==0)
+ {
+ if(i+1<argc)
+ {
+ //strcat(inputyuv, argv[i+1]);
+ strcpy(inputyuv, argv[i+1]);
+ //printf("---> argv[i+1]=%s, inputyuv=%s \n", argv[i+1], inputyuv);
+ i++;
+ } else
+ {
+ show_help();
+ return -1;
+ }
+ } else
+ if(strcmp(argv[i],"-frames")==0)
+ {
+ if(i+1<argc)
+ {
+ enc_frames = atoi(argv[i+1]);
+ i++;
+ } else
+ {
+ show_help();
+ return -1;
+ }
+ } else
+ if(strcmp(argv[i],"-bdistance")==0)
+ {
+ if(i+1<argc)
+ {
+ bdistance = atoi(argv[i+1]);
+ i++;
+ } else
+ {
+ show_help();
+ return -1;
+ }
+ } else
+ if(strcmp(argv[i],"-gop_length")==0)
+ {
+ if(i+1<argc)
+ {
+ gop_length = atoi(argv[i+1]);
+ i++;
+ } else
+ {
+ show_help();
+ return -1;
+ }
+ } else
+ {
+ show_help();
+ return -1;
+ }
+ }
+
+ picture_width_in_mbs = (picture_width + 15) / 16;
+ picture_height_in_mbs = (picture_height + 15) / 16;
+
+ yuv_fp = fopen(inputyuv,"rb");
+ if ( yuv_fp == NULL){
+ printf("Can't open input YUV file\n");
+ show_help();
+ return -1;
+ }
+ fseek(yuv_fp,0l, SEEK_END);
+ file_size = ftell(yuv_fp);
+ frame_size = picture_width * picture_height + ((picture_width * picture_height) >> 1) ;
+
+ if(enc_frames == 1 )
+ file_size = 2 * frame_size; //FIXME default 0 run all frames, 1 only test first P frame first, first is I frame
+ else if(enc_frames > 1)
+ file_size = enc_frames * frame_size; //FIXME only test (enc_frames - 1) P frames
+
+ if ( (file_size < frame_size) || (file_size % frame_size) ) {
+ fclose(yuv_fp);
+ printf("The YUV file's size is not correct\n");
+ show_help();
+ return -1;
+ }
+
+ frame_number = file_size / frame_size;
+ fseek(yuv_fp, 0l, SEEK_SET);
+
+ if(bdistance < 1)
+ bdistance = 1;
+ if(gop_length < 0)
+ gop_length = frame_number;
+
+ if(preenc_context.pic_param.disable_mv_output)
+ {
+ mvout_fp = 0;
+ } else
+ {
+ mvout_fp = fopen("mvoutdata.bin", "wb");
+ if ( mvout_fp == NULL) {
+ fclose(mvout_fp);
+ printf("Can't open output MV data file\n");
+ show_help();
+ return -1;
+ }
+ }
+ if(preenc_context.pic_param.disable_statistics_output)
+ {
+ statsout_fp = 0;
+ } else
+ {
+ statsout_fp = fopen("statsoutdata.bin", "wb");
+ if ( statsout_fp == NULL) {
+ fclose(statsout_fp);
+ printf("Can't open output Statistics data file\n");
+ show_help();
+ return -1;
+ }
+ }
+
+ gettimeofday(&tpstart,NULL);
+ preenc_context_init();
+ create_preenc_pipe();
+ alloc_preenc_resource();
+
+ for ( f = 0; f < frame_number; f++) { //picture level loop, start from second picture
+ if(bdistance==1)
+ {
+ if((f % gop_length) == 0)
+ preenc_picture(f, SLICE_TYPE_I, 0);
+ else
+ preenc_picture(f, SLICE_TYPE_P, 0);
+ } else
+ {
+ if((f % gop_length) == 0)
+ preenc_picture(f, SLICE_TYPE_I, 0);
+ else if (((f-1)%bdistance) == 0)
+ preenc_picture(f, SLICE_TYPE_P, MIN((bdistance-1), (gop_length-1-(f%gop_length))));
+ else
+ preenc_picture(f, SLICE_TYPE_B, MIN((((f+bdistance-1)/bdistance)*bdistance-f), (gop_length-1-(f%gop_length))));
+
+ }
+ }
+
+ gettimeofday(&tpend,NULL);
+ timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+ tpend.tv_usec-tpstart.tv_usec;
+ timeuse/=1000000;
+ printf("\ndone!\n");
+ printf("encode %d frames in %f secondes, FPS is %.1f\n",frame_number, timeuse, frame_number/timeuse);
+ release_preenc_resource();
+ destory_preenc_pipe();
+
+ if(yuv_fp)
+ fclose(yuv_fp);
+ if(mvpredin_fp)
+ fclose(mvpredin_fp);
+ if(qpin_fp)
+ fclose(qpin_fp);
+ if(mvout_fp)
+ fclose(mvout_fp);
+ if(statsout_fp)
+ fclose(statsout_fp);
+
+ return 0;
+}
+
--
1.7.9.5
Hi,
We add two new FEI samples into test/encode/ folder for test sample fei/statistics cases, feiavcenc.c is for fei case and preenc.c is for statistics case.
Thanks,
Kelvin
More information about the Libva
mailing list