2 * Copyright (c) 2012 Intel Corporation. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 * Simple AVC encoder based on libVA.
28 * ./avcenc <width> <height> <input file> <output file> [qp]
38 #include <sys/types.h>
45 #include "va_display.h"
47 #define NAL_REF_IDC_NONE 0
48 #define NAL_REF_IDC_LOW 1
49 #define NAL_REF_IDC_MEDIUM 2
50 #define NAL_REF_IDC_HIGH 3
57 #define SLICE_TYPE_P 0
58 #define SLICE_TYPE_B 1
59 #define SLICE_TYPE_I 2
61 #define ENTROPY_MODE_CAVLC 0
62 #define ENTROPY_MODE_CABAC 1
64 #define PROFILE_IDC_BASELINE 66
65 #define PROFILE_IDC_MAIN 77
66 #define PROFILE_IDC_HIGH 100
68 #define CHECK_VASTATUS(va_status,func) \
69 if (va_status != VA_STATUS_SUCCESS) { \
70 fprintf(stderr,"%s:%s (%d) failed,exit\n", __func__, func, __LINE__); \
74 static VADisplay va_dpy;
76 static int picture_width, picture_width_in_mbs;
77 static int picture_height, picture_height_in_mbs;
78 static int frame_size;
79 static unsigned char *newImageBuffer = 0;
81 static int qp_value = 26;
83 static int intra_period = 30;
84 static int pb_period = 5;
85 static int frame_bit_rate = -1;
94 build_packed_pic_buffer(unsigned char **header_buffer);
97 build_packed_seq_buffer(unsigned char **header_buffer);
99 struct packed_data_format
101 unsigned int length_in_bits;
103 unsigned char num_skip_bytes;
104 unsigned char pad[2];
108 VAEncSequenceParameterBufferH264Ext seq_param;
109 VAEncPictureParameterBufferH264Ext pic_param;
110 VAEncSliceParameterBufferH264Ext slice_param[MAX_SLICES];
111 VAEncH264DecRefPicMarkingBuffer dec_ref_pic_marking;
112 VAContextID context_id;
113 VAConfigID config_id;
114 VABufferID seq_param_buf_id; /* Sequence level parameter */
115 VABufferID pic_param_buf_id; /* Picture level parameter */
116 VABufferID slice_param_buf_id[MAX_SLICES]; /* Slice level parameter, multil slices */
117 VABufferID dec_ref_pic_marking_buf_id;
118 VABufferID codedbuf_buf_id; /* Output buffer, compressed data */
119 VABufferID packed_seq_buf_id;
120 VABufferID packed_pic_buf_id;
123 int codedbuf_pb_size;
126 static void create_encode_pipe()
128 VAEntrypoint entrypoints[5];
129 int num_entrypoints,slice_entrypoint;
130 VAConfigAttrib attrib[2];
131 int major_ver, minor_ver;
134 va_dpy = va_open_display();
135 va_status = vaInitialize(va_dpy, &major_ver, &minor_ver);
136 CHECK_VASTATUS(va_status, "vaInitialize");
138 vaQueryConfigEntrypoints(va_dpy, VAProfileH264Baseline, entrypoints,
141 for (slice_entrypoint = 0; slice_entrypoint < num_entrypoints; slice_entrypoint++) {
142 if (entrypoints[slice_entrypoint] == VAEntrypointEncSlice)
146 if (slice_entrypoint == num_entrypoints) {
147 /* not find Slice entry point */
151 /* find out the format for the render target, and rate control mode */
152 attrib[0].type = VAConfigAttribRTFormat;
153 attrib[1].type = VAConfigAttribRateControl;
154 vaGetConfigAttributes(va_dpy, VAProfileH264Baseline, VAEntrypointEncSlice,
157 if ((attrib[0].value & VA_RT_FORMAT_YUV420) == 0) {
158 /* not find desired YUV420 RT format */
162 if ((attrib[1].value & VA_RC_VBR) == 0) {
163 /* Can't find matched RC mode */
164 printf("VBR mode doesn't found, exit\n");
168 attrib[0].value = VA_RT_FORMAT_YUV420; /* set to desired RT format */
169 attrib[1].value = VA_RC_VBR; /* set to desired RC mode */
171 va_status = vaCreateConfig(va_dpy, VAProfileH264Baseline, VAEntrypointEncSlice,
172 &attrib[0], 2,&avcenc_context.config_id);
173 CHECK_VASTATUS(va_status, "vaCreateConfig");
175 /* Create a context for this decode pipe */
176 va_status = vaCreateContext(va_dpy, avcenc_context.config_id,
177 picture_width, picture_height,
180 &avcenc_context.context_id);
181 CHECK_VASTATUS(va_status, "vaCreateContext");
184 static void destory_encode_pipe()
186 vaDestroyContext(va_dpy,avcenc_context.context_id);
187 vaDestroyConfig(va_dpy,avcenc_context.config_id);
189 va_close_display(va_dpy);
192 /***************************************************
194 * The encode pipe resource define
196 ***************************************************/
197 #define SID_INPUT_PICTURE 0
198 #define SID_REFERENCE_PICTURE_L0 1
199 #define SID_REFERENCE_PICTURE_L1 2
200 #define SID_RECON_PICTURE 3
201 #define SID_NUMBER SID_RECON_PICTURE + 1
202 static VASurfaceID surface_ids[SID_NUMBER];
204 static int enc_frame_number;
206 /***************************************************/
208 static void alloc_encode_resource()
213 va_status = vaCreateSurfaces(va_dpy, picture_width, picture_height,
214 VA_RT_FORMAT_YUV420, SID_NUMBER, &surface_ids[0]);
215 CHECK_VASTATUS(va_status, "vaCreateSurfaces");
217 newImageBuffer = (unsigned char *)malloc(frame_size);
220 static void release_encode_resource()
222 free(newImageBuffer);
224 // Release all the surfaces resource
225 vaDestroySurfaces(va_dpy, &surface_ids[0], SID_NUMBER);
228 static void avcenc_update_picture_parameter(int slice_type, int frame_num, int display_num, int is_idr)
230 VAEncPictureParameterBufferH264Ext *pic_param;
234 pic_param = &avcenc_context.pic_param;
235 pic_param->CurrPic.picture_id = surface_ids[SID_RECON_PICTURE];
236 pic_param->CurrPic.TopFieldOrderCnt = display_num * 2;
237 pic_param->ReferenceFrames[0].picture_id = surface_ids[SID_REFERENCE_PICTURE_L0];
238 pic_param->ReferenceFrames[1].picture_id = surface_ids[SID_REFERENCE_PICTURE_L1];
239 pic_param->ReferenceFrames[2].picture_id = VA_INVALID_ID;
240 assert(avcenc_context.codedbuf_buf_id != VA_INVALID_ID);
241 pic_param->CodedBuf = avcenc_context.codedbuf_buf_id;
242 pic_param->frame_num = frame_num;
243 pic_param->pic_fields.bits.idr_pic_flag = !!is_idr;
244 pic_param->pic_fields.bits.reference_pic_flag = (slice_type != SLICE_TYPE_B);
246 va_status = vaCreateBuffer(va_dpy,
247 avcenc_context.context_id,
248 VAEncPictureParameterBufferExtType,
249 sizeof(*pic_param), 1, pic_param,
250 &avcenc_context.pic_param_buf_id);
251 CHECK_VASTATUS(va_status,"vaCreateBuffer");
254 static void upload_yuv_to_surface(FILE *yuv_fp, VASurfaceID surface_id)
256 VAImage surface_image;
258 void *surface_p = NULL;
259 unsigned char *y_src, *u_src, *v_src;
260 unsigned char *y_dst, *u_dst, *v_dst;
261 int y_size = picture_width * picture_height;
262 int u_size = (picture_width >> 1) * (picture_height >> 1);
267 n_items = fread(newImageBuffer, frame_size, 1, yuv_fp);
268 } while (n_items != 1);
270 va_status = vaDeriveImage(va_dpy, surface_id, &surface_image);
271 CHECK_VASTATUS(va_status,"vaDeriveImage");
273 vaMapBuffer(va_dpy, surface_image.buf, &surface_p);
274 assert(VA_STATUS_SUCCESS == va_status);
276 y_src = newImageBuffer;
277 u_src = newImageBuffer + y_size; /* UV offset for NV12 */
278 v_src = newImageBuffer + y_size + u_size;
280 y_dst = surface_p + surface_image.offsets[0];
281 u_dst = surface_p + surface_image.offsets[1]; /* UV offset for NV12 */
282 v_dst = surface_p + surface_image.offsets[2];
285 for (row = 0; row < surface_image.height; row++) {
286 memcpy(y_dst, y_src, surface_image.width);
287 y_dst += surface_image.pitches[0];
288 y_src += picture_width;
291 if (surface_image.format.fourcc == VA_FOURCC_NV12) { /* UV plane */
292 for (row = 0; row < surface_image.height / 2; row++) {
293 for (col = 0; col < surface_image.width / 2; col++) {
294 u_dst[col * 2] = u_src[col];
295 u_dst[col * 2 + 1] = v_src[col];
298 u_dst += surface_image.pitches[1];
299 u_src += (picture_width / 2);
300 v_src += (picture_width / 2);
303 /* FIXME: fix this later */
307 vaUnmapBuffer(va_dpy, surface_image.buf);
308 vaDestroyImage(va_dpy, surface_image.image_id);
311 static void avcenc_update_slice_parameter(int slice_type)
313 VAEncSliceParameterBufferH264Ext *slice_param;
319 slice_param = &avcenc_context.slice_param[i];
320 slice_param->start_row_number = 0;
321 slice_param->slice_height = picture_height_in_mbs/16; /* Measured by MB */
322 slice_param->pic_parameter_set_id = 0;
323 slice_param->slice_type = slice_type;
324 slice_param->direct_spatial_mv_pred_flag = 0;
325 slice_param->num_ref_idx_l0_active_minus1 = 0; /* FIXME: ??? */
326 slice_param->num_ref_idx_l1_active_minus1 = 0;
327 slice_param->cabac_init_idc = 0;
328 slice_param->slice_qp_delta = 0;
329 slice_param->disable_deblocking_filter_idc = 0;
330 slice_param->slice_alpha_c0_offset_div2 = 2;
331 slice_param->slice_beta_offset_div2 = 2;
332 slice_param->idr_pic_id = 0;
334 /* ref_pic_list_modification() */
335 slice_param->ref_pic_list_modification_flag_l0 = 0;
336 slice_param->ref_pic_list_modification_flag_l1 = 0;
337 /* FIXME: fill other fields */
339 va_status = vaCreateBuffer(va_dpy,
340 avcenc_context.context_id,
341 VAEncSliceParameterBufferExtType,
342 sizeof(*slice_param), 1, slice_param,
343 &avcenc_context.slice_param_buf_id[i]);
344 CHECK_VASTATUS(va_status,"vaCreateBuffer");;
348 avcenc_context.num_slices = i;
351 static int begin_picture(FILE *yuv_fp, int frame_num, int display_num, int slice_type, int is_idr)
355 if (frame_num == 0) {
356 unsigned char *packed_seq_buffer = NULL, *packed_pic_buffer = NULL;
357 int seq_length, pic_length;
359 assert(slice_type == SLICE_TYPE_I);
360 seq_length = build_packed_seq_buffer(&packed_seq_buffer);
361 va_status = vaCreateBuffer(va_dpy,
362 avcenc_context.context_id,
363 VAEncPackedSequenceParameterBufferType,
364 (seq_length + 7) / 8, 1, packed_seq_buffer,
365 &avcenc_context.packed_seq_buf_id);
366 CHECK_VASTATUS(va_status,"vaCreateBuffer");;
368 pic_length = build_packed_pic_buffer(&packed_pic_buffer);
369 va_status = vaCreateBuffer(va_dpy,
370 avcenc_context.context_id,
371 VAEncPackedPictureParameterBufferType,
372 (pic_length + 7) / 8 , 1, packed_pic_buffer,
373 &avcenc_context.packed_pic_buf_id);
374 CHECK_VASTATUS(va_status,"vaCreateBuffer");;
376 free(packed_seq_buffer);
377 free(packed_pic_buffer);
380 /* sequence parameter set */
381 VAEncSequenceParameterBufferH264Ext *seq_param = &avcenc_context.seq_param;
382 va_status = vaCreateBuffer(va_dpy,
383 avcenc_context.context_id,
384 VAEncSequenceParameterBufferExtType,
385 sizeof(*seq_param), 1, seq_param,
386 &avcenc_context.seq_param_buf_id);
387 CHECK_VASTATUS(va_status,"vaCreateBuffer");;
389 /* slice parameter */
390 avcenc_update_slice_parameter(slice_type);
392 /* Copy Image to target surface according input YUV data. */
393 fseek(yuv_fp, frame_size * display_num, SEEK_SET);
394 upload_yuv_to_surface(yuv_fp, surface_ids[SID_INPUT_PICTURE]);
399 static int avcenc_render_picture()
402 VABufferID va_buffers[8];
403 unsigned int num_va_buffers = 0;
405 va_buffers[num_va_buffers++] = avcenc_context.seq_param_buf_id;
406 va_buffers[num_va_buffers++] = avcenc_context.pic_param_buf_id;
408 if (avcenc_context.dec_ref_pic_marking_buf_id != VA_INVALID_ID)
409 va_buffers[num_va_buffers++] = avcenc_context.dec_ref_pic_marking_buf_id;
411 if (avcenc_context.packed_seq_buf_id != VA_INVALID_ID)
412 va_buffers[num_va_buffers++] = avcenc_context.packed_seq_buf_id;
414 if (avcenc_context.packed_pic_buf_id != VA_INVALID_ID)
415 va_buffers[num_va_buffers++] = avcenc_context.packed_pic_buf_id;
417 va_status = vaBeginPicture(va_dpy,
418 avcenc_context.context_id,
419 surface_ids[SID_INPUT_PICTURE]);
420 CHECK_VASTATUS(va_status,"vaBeginPicture");
422 va_status = vaRenderPicture(va_dpy,
423 avcenc_context.context_id,
426 CHECK_VASTATUS(va_status,"vaRenderPicture");
428 va_status = vaRenderPicture(va_dpy,
429 avcenc_context.context_id,
430 &avcenc_context.slice_param_buf_id[0],
431 avcenc_context.num_slices);
432 CHECK_VASTATUS(va_status,"vaRenderPicture");
434 va_status = vaEndPicture(va_dpy, avcenc_context.context_id);
435 CHECK_VASTATUS(va_status,"vaEndPicture");
440 static int avcenc_destroy_buffers(VABufferID *va_buffers, unsigned int num_va_buffers)
445 for (i = 0; i < num_va_buffers; i++) {
446 if (va_buffers[i] != VA_INVALID_ID) {
447 va_status = vaDestroyBuffer(va_dpy, va_buffers[i]);
448 CHECK_VASTATUS(va_status,"vaDestroyBuffer");
449 va_buffers[i] = VA_INVALID_ID;
456 static void end_picture(int slice_type, int next_is_bpic)
460 /* Prepare for next picture */
461 tempID = surface_ids[SID_RECON_PICTURE];
463 if (slice_type != SLICE_TYPE_B) {
465 surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L1];
466 surface_ids[SID_REFERENCE_PICTURE_L1] = tempID;
468 surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L0];
469 surface_ids[SID_REFERENCE_PICTURE_L0] = tempID;
473 surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L0];
474 surface_ids[SID_REFERENCE_PICTURE_L0] = surface_ids[SID_REFERENCE_PICTURE_L1];
475 surface_ids[SID_REFERENCE_PICTURE_L1] = tempID;
479 avcenc_destroy_buffers(&avcenc_context.seq_param_buf_id, 1);
480 avcenc_destroy_buffers(&avcenc_context.pic_param_buf_id, 1);
481 avcenc_destroy_buffers(&avcenc_context.dec_ref_pic_marking_buf_id, 1);
482 avcenc_destroy_buffers(&avcenc_context.packed_seq_buf_id, 1);
483 avcenc_destroy_buffers(&avcenc_context.packed_pic_buf_id, 1);
484 avcenc_destroy_buffers(&avcenc_context.slice_param_buf_id[0], avcenc_context.num_slices);
485 avcenc_destroy_buffers(&avcenc_context.codedbuf_buf_id, 1);
486 memset(avcenc_context.slice_param, 0, sizeof(avcenc_context.slice_param));
487 avcenc_context.num_slices = 0;
490 #define BITSTREAM_ALLOCATE_STEPPING 4096
493 unsigned int *buffer;
495 int max_size_in_dword;
498 typedef struct __bitstream bitstream;
502 get_coded_bitsteam_length(unsigned char *buffer, int buffer_length)
506 for (i = 0; i < buffer_length - 3; i++) {
519 va_swap32(unsigned int val)
521 unsigned char *pval = (unsigned char *)&val;
523 return ((pval[0] << 24) |
530 bitstream_start(bitstream *bs)
532 bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
533 bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
534 bs->bit_offset = sizeof(struct packed_data_format) * 8; /* the first 64 bits used for format */
538 bitstream_end(bitstream *bs)
540 int pos = (bs->bit_offset >> 5);
541 int bit_offset = (bs->bit_offset & 0x1f);
542 int bit_left = 32 - bit_offset;
543 struct packed_data_format *format;
546 bs->buffer[pos] = va_swap32((bs->buffer[pos] << bit_left));
549 format = (struct packed_data_format *)bs->buffer;
550 format->length_in_bits = bs->bit_offset - sizeof(struct packed_data_format) * 8;
552 format->num_skip_bytes = 5; /* ignore start code & nal type for emulation prevetion check */
556 bitstream_put_ui(bitstream *bs, unsigned int val, int size_in_bits)
558 int pos = (bs->bit_offset >> 5);
559 int bit_offset = (bs->bit_offset & 0x1f);
560 int bit_left = 32 - bit_offset;
565 bs->bit_offset += size_in_bits;
567 if (bit_left > size_in_bits) {
568 bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
570 size_in_bits -= bit_left;
571 bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
572 bs->buffer[pos] = va_swap32(bs->buffer[pos]);
574 if (pos + 1 == bs->max_size_in_dword) {
575 bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
576 bs->buffer = realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
579 bs->buffer[pos + 1] = val;
584 bitstream_put_ue(bitstream *bs, unsigned int val)
586 int size_in_bits = 0;
594 bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
595 bitstream_put_ui(bs, val, size_in_bits);
599 bitstream_put_se(bitstream *bs, int val)
601 unsigned int new_val;
606 new_val = 2 * val - 1;
608 bitstream_put_ue(bs, new_val);
612 bitstream_byte_aligning(bitstream *bs, int bit)
614 int bit_offset = (bs->bit_offset & 0x7);
615 int bit_left = 8 - bit_offset;
621 assert(bit == 0 || bit == 1);
624 new_val = (1 << bit_left) - 1;
628 bitstream_put_ui(bs, new_val, bit_left);
632 rbsp_trailing_bits(bitstream *bs)
634 bitstream_put_ui(bs, 1, 1);
635 bitstream_byte_aligning(bs, 0);
638 static void nal_start_code_prefix(bitstream *bs)
640 bitstream_put_ui(bs, 0x00000001, 32);
643 static void nal_header(bitstream *bs, int nal_ref_idc, int nal_unit_type)
645 bitstream_put_ui(bs, 0, 1); /* forbidden_zero_bit: 0 */
646 bitstream_put_ui(bs, nal_ref_idc, 2);
647 bitstream_put_ui(bs, nal_unit_type, 5);
650 static void sps_rbsp(bitstream *bs)
652 VAEncSequenceParameterBufferH264Ext *seq_param = &avcenc_context.seq_param;
654 bitstream_put_ui(bs, seq_param->profile_idc, 8); /* profile_idc */
655 bitstream_put_ui(bs, 0, 1); /* constraint_set0_flag */
656 bitstream_put_ui(bs, 1, 1); /* constraint_set1_flag */
657 bitstream_put_ui(bs, 0, 1); /* constraint_set2_flag */
658 bitstream_put_ui(bs, 0, 1); /* constraint_set3_flag */
659 bitstream_put_ui(bs, 0, 4); /* reserved_zero_4bits */
660 bitstream_put_ui(bs, seq_param->level_idc, 8); /* level_idc */
661 bitstream_put_ue(bs, seq_param->seq_parameter_set_id); /* seq_parameter_set_id */
663 if (seq_param->profile_idc >= PROFILE_IDC_HIGH) {
664 /* FIXME: fix for high profile */
668 bitstream_put_ue(bs, seq_param->log2_max_frame_num_minus4); /* log2_max_frame_num_minus4 */
669 bitstream_put_ue(bs, seq_param->pic_order_cnt_type); /* pic_order_cnt_type */
671 if (seq_param->pic_order_cnt_type == 0)
672 bitstream_put_ue(bs, seq_param->log2_max_pic_order_cnt_lsb_minus4); /* log2_max_pic_order_cnt_lsb_minus4 */
677 bitstream_put_ue(bs, seq_param->max_num_ref_frames); /* num_ref_frames */
678 bitstream_put_ui(bs, 0, 1); /* gaps_in_frame_num_value_allowed_flag */
680 bitstream_put_ue(bs, seq_param->picture_width_in_mbs - 1); /* pic_width_in_mbs_minus1 */
681 bitstream_put_ue(bs, seq_param->picture_height_in_mbs - 1); /* pic_height_in_map_units_minus1 */
682 bitstream_put_ui(bs, seq_param->frame_mbs_only_flag, 1); /* frame_mbs_only_flag */
684 if (!seq_param->frame_mbs_only_flag) {
688 bitstream_put_ui(bs, seq_param->direct_8x8_inference_flag, 1); /* direct_8x8_inference_flag */
689 bitstream_put_ui(bs, seq_param->frame_cropping_flag, 1); /* frame_cropping_flag */
691 if (seq_param->frame_cropping_flag) {
692 bitstream_put_ue(bs, seq_param->frame_crop_left_offset); /* frame_crop_left_offset */
693 bitstream_put_ue(bs, seq_param->frame_crop_right_offset); /* frame_crop_right_offset */
694 bitstream_put_ue(bs, seq_param->frame_crop_top_offset); /* frame_crop_top_offset */
695 bitstream_put_ue(bs, seq_param->frame_crop_bottom_offset); /* frame_crop_bottom_offset */
698 bitstream_put_ui(bs, 0, 1); /* vui_parameters_present_flag */
699 rbsp_trailing_bits(bs); /* rbsp_trailing_bits */
703 static void build_nal_sps(FILE *avc_fp)
707 bitstream_start(&bs);
708 nal_start_code_prefix(&bs);
709 nal_header(&bs, NAL_REF_IDC_HIGH, NAL_SPS);
711 bitstream_end(&bs, avc_fp);
715 static void pps_rbsp(bitstream *bs)
717 VAEncPictureParameterBufferH264Ext *pic_param = &avcenc_context.pic_param;
719 bitstream_put_ue(bs, pic_param->pic_parameter_set_id); /* pic_parameter_set_id */
720 bitstream_put_ue(bs, pic_param->seq_parameter_set_id); /* seq_parameter_set_id */
722 bitstream_put_ui(bs, pic_param->pic_fields.bits.entropy_coding_mode_flag, 1); /* entropy_coding_mode_flag */
724 bitstream_put_ui(bs, 0, 1); /* pic_order_present_flag: 0 */
726 bitstream_put_ue(bs, 0); /* num_slice_groups_minus1 */
728 bitstream_put_ue(bs, pic_param->num_ref_idx_l0_active_minus1); /* num_ref_idx_l0_active_minus1 */
729 bitstream_put_ue(bs, pic_param->num_ref_idx_l1_active_minus1); /* num_ref_idx_l1_active_minus1 1 */
731 bitstream_put_ui(bs, pic_param->pic_fields.bits.weighted_pred_flag, 1); /* weighted_pred_flag: 0 */
732 bitstream_put_ui(bs, pic_param->pic_fields.bits.weighted_bipred_idc, 2); /* weighted_bipred_idc: 0 */
734 bitstream_put_se(bs, pic_param->pic_init_qp - 26); /* pic_init_qp_minus26 */
735 bitstream_put_se(bs, 0); /* pic_init_qs_minus26 */
736 bitstream_put_se(bs, 0); /* chroma_qp_index_offset */
738 bitstream_put_ui(bs, pic_param->pic_fields.bits.deblocking_filter_control_present_flag, 1); /* deblocking_filter_control_present_flag */
739 bitstream_put_ui(bs, 0, 1); /* constrained_intra_pred_flag */
740 bitstream_put_ui(bs, 0, 1); /* redundant_pic_cnt_present_flag */
742 rbsp_trailing_bits(bs);
746 static void build_nal_pps(FILE *avc_fp)
750 bitstream_start(&bs);
751 nal_start_code_prefix(&bs);
752 nal_header(&bs, NAL_REF_IDC_HIGH, NAL_PPS);
754 bitstream_end(&bs, avc_fp);
758 build_header(FILE *avc_fp)
760 build_nal_sps(avc_fp);
761 build_nal_pps(avc_fp);
766 build_packed_pic_buffer(unsigned char **header_buffer)
770 bitstream_start(&bs);
771 nal_start_code_prefix(&bs);
772 nal_header(&bs, NAL_REF_IDC_HIGH, NAL_PPS);
776 *header_buffer = (unsigned char *)bs.buffer;
777 return bs.bit_offset;
781 build_packed_seq_buffer(unsigned char **header_buffer)
785 bitstream_start(&bs);
786 nal_start_code_prefix(&bs);
787 nal_header(&bs, NAL_REF_IDC_HIGH, NAL_SPS);
791 *header_buffer = (unsigned char *)bs.buffer;
792 return bs.bit_offset;
798 slice_header(bitstream *bs, int frame_num, int display_frame, int slice_type, int nal_ref_idc, int is_idr)
800 VAEncSequenceParameterBufferH264Ext *seq_param = &avcenc_context.seq_param;
801 VAEncPictureParameterBufferH264Ext *pic_param = &avcenc_context.pic_param;
802 int is_cabac = (pic_param->pic_fields.bits.entropy_coding_mode_flag == ENTROPY_MODE_CABAC);
804 bitstream_put_ue(bs, 0); /* first_mb_in_slice: 0 */
805 bitstream_put_ue(bs, slice_type); /* slice_type */
806 bitstream_put_ue(bs, 0); /* pic_parameter_set_id: 0 */
807 bitstream_put_ui(bs, frame_num & 0x0F, seq_param->log2_max_frame_num_minus4 + 4); /* frame_num */
809 /* frame_mbs_only_flag == 1 */
810 if (!seq_param->frame_mbs_only_flag) {
816 bitstream_put_ue(bs, 0); /* idr_pic_id: 0 */
818 if (seq_param->pic_order_cnt_type == 0) {
819 bitstream_put_ui(bs, (display_frame*2) & 0x3F, seq_param->log2_max_pic_order_cnt_lsb_minus4 + 4);
820 /* only support frame */
826 /* redundant_pic_cnt_present_flag == 0 */
829 if (slice_type == SLICE_TYPE_P) {
830 bitstream_put_ui(bs, 0, 1); /* num_ref_idx_active_override_flag: 0 */
831 /* ref_pic_list_reordering */
832 bitstream_put_ui(bs, 0, 1); /* ref_pic_list_reordering_flag_l0: 0 */
833 } else if (slice_type == SLICE_TYPE_B) {
834 bitstream_put_ui(bs, 1, 1); /* direct_spatial_mv_pred: 1 */
835 bitstream_put_ui(bs, 0, 1); /* num_ref_idx_active_override_flag: 0 */
836 /* ref_pic_list_reordering */
837 bitstream_put_ui(bs, 0, 1); /* ref_pic_list_reordering_flag_l0: 0 */
838 bitstream_put_ui(bs, 0, 1); /* ref_pic_list_reordering_flag_l1: 0 */
841 /* weighted_pred_flag == 0 */
843 /* dec_ref_pic_marking */
844 if (nal_ref_idc != 0) {
846 bitstream_put_ui(bs, 0, 1); /* no_output_of_prior_pics_flag: 0 */
847 bitstream_put_ui(bs, 0, 1); /* long_term_reference_flag: 0 */
849 bitstream_put_ui(bs, 0, 1); /* adaptive_ref_pic_marking_mode_flag: 0 */
853 if (is_cabac && (slice_type != SLICE_TYPE_I))
854 bitstream_put_ue(bs, 0); /* cabac_init_idc: 0 */
856 bitstream_put_se(bs, 0); /* slice_qp_delta: 0 */
858 if (pic_param->pic_fields.bits.deblocking_filter_control_present_flag == 1) {
859 bitstream_put_ue(bs, 0); /* disable_deblocking_filter_idc: 0 */
860 bitstream_put_se(bs, 2); /* slice_alpha_c0_offset_div2: 2 */
861 bitstream_put_se(bs, 2); /* slice_beta_offset_div2: 2 */
866 slice_data(bitstream *bs)
868 VACodedBufferSegment *coded_buffer_segment;
869 unsigned char *coded_mem;
870 int i, slice_data_length;
872 VASurfaceStatus surface_status;
874 va_status = vaSyncSurface(va_dpy, surface_ids[SID_INPUT_PICTURE]);
875 CHECK_VASTATUS(va_status,"vaSyncSurface");
878 va_status = vaQuerySurfaceStatus(va_dpy, surface_ids[SID_INPUT_PICTURE], &surface_status);
879 CHECK_VASTATUS(va_status,"vaQuerySurfaceStatus");
881 va_status = vaMapBuffer(va_dpy, avcenc_context.codedbuf_buf_id, (void **)(&coded_buffer_segment));
882 CHECK_VASTATUS(va_status,"vaMapBuffer");
883 coded_mem = coded_buffer_segment->buf;
885 slice_data_length = get_coded_bitsteam_length(coded_mem, codedbuf_size);
887 for (i = 0; i < slice_data_length; i++) {
888 bitstream_put_ui(bs, *coded_mem, 8);
892 vaUnmapBuffer(va_dpy, avcenc_context.codedbuf_buf_id);
896 build_nal_slice(FILE *avc_fp, int frame_num, int display_frame, int slice_type, int is_idr)
900 bitstream_start(&bs);
902 bitstream_end(&bs, avc_fp);
908 store_coded_buffer(FILE *avc_fp, int slice_type)
910 VACodedBufferSegment *coded_buffer_segment;
911 unsigned char *coded_mem;
912 int slice_data_length;
914 VASurfaceStatus surface_status;
917 va_status = vaSyncSurface(va_dpy, surface_ids[SID_INPUT_PICTURE]);
918 CHECK_VASTATUS(va_status,"vaSyncSurface");
921 va_status = vaQuerySurfaceStatus(va_dpy, surface_ids[SID_INPUT_PICTURE], &surface_status);
922 CHECK_VASTATUS(va_status,"vaQuerySurfaceStatus");
924 va_status = vaMapBuffer(va_dpy, avcenc_context.codedbuf_buf_id, (void **)(&coded_buffer_segment));
925 CHECK_VASTATUS(va_status,"vaMapBuffer");
926 coded_mem = coded_buffer_segment->buf;
928 if (coded_buffer_segment->status & VA_CODED_BUF_STATUS_SLICE_OVERFLOW_MASK) {
929 if (slice_type == SLICE_TYPE_I)
930 avcenc_context.codedbuf_i_size *= 2;
932 avcenc_context.codedbuf_pb_size *= 2;
934 vaUnmapBuffer(va_dpy, avcenc_context.codedbuf_buf_id);
938 slice_data_length = coded_buffer_segment->size;
941 w_items = fwrite(coded_mem, slice_data_length, 1, avc_fp);
942 } while (w_items != 1);
944 if (slice_type == SLICE_TYPE_I) {
945 if (avcenc_context.codedbuf_i_size > slice_data_length * 3 / 2) {
946 avcenc_context.codedbuf_i_size = slice_data_length * 3 / 2;
949 if (avcenc_context.codedbuf_pb_size < slice_data_length) {
950 avcenc_context.codedbuf_pb_size = slice_data_length;
953 if (avcenc_context.codedbuf_pb_size > slice_data_length * 3 / 2) {
954 avcenc_context.codedbuf_pb_size = slice_data_length * 3 / 2;
958 vaUnmapBuffer(va_dpy, avcenc_context.codedbuf_buf_id);
964 encode_picture(FILE *yuv_fp, FILE *avc_fp,
965 int frame_num, int display_num,
967 int slice_type, int next_is_bpic)
970 int count = 10, ret = 0, codedbuf_size;
972 begin_picture(yuv_fp, frame_num, display_num, slice_type, is_idr);
975 avcenc_destroy_buffers(&avcenc_context.codedbuf_buf_id, 1);
976 avcenc_destroy_buffers(&avcenc_context.pic_param_buf_id, 1);
979 if (SLICE_TYPE_I == slice_type) {
980 codedbuf_size = avcenc_context.codedbuf_i_size;
982 codedbuf_size = avcenc_context.codedbuf_pb_size;
986 va_status = vaCreateBuffer(va_dpy,
987 avcenc_context.context_id,
988 VAEncCodedBufferType,
989 codedbuf_size, 1, NULL,
990 &avcenc_context.codedbuf_buf_id);
991 CHECK_VASTATUS(va_status,"vaCreateBuffer");
993 /* picture parameter set */
994 avcenc_update_picture_parameter(slice_type, frame_num, display_num, is_idr);
996 avcenc_render_picture();
997 ret = store_coded_buffer(avc_fp, slice_type);
998 } while (ret && --count);
1000 end_picture(slice_type, next_is_bpic);
1003 static void encode_i_picture(FILE *yuv_fp, FILE *avc_fp, int f, int is_idr)
1005 encode_picture(yuv_fp, avc_fp,
1006 enc_frame_number, f,
1011 static void encode_p_picture(FILE *yuv_fp, FILE *avc_fp, int f)
1013 encode_picture(yuv_fp, avc_fp,
1014 enc_frame_number, f,
1019 static void encode_pb_pictures(FILE *yuv_fp, FILE *avc_fp, int f, int nbframes)
1022 encode_picture(yuv_fp, avc_fp,
1023 enc_frame_number, f + nbframes,
1027 for( i = 0; i < nbframes - 1; i++) {
1028 encode_picture(yuv_fp, avc_fp,
1029 enc_frame_number + 1, f + i,
1034 encode_picture(yuv_fp, avc_fp,
1035 enc_frame_number + 1, f + nbframes - 1,
1040 static void show_help()
1042 printf("Usage: avnenc <width> <height> <input_yuvfile> <output_avcfile> [qp=qpvalue|fb=framebitrate] [mode=1(I frames only)/2(I and P frames)/3(I, P and B frames)\n");
1045 static void avcenc_context_seq_param_init(VAEncSequenceParameterBufferH264Ext *seq_param,
1046 int width, int height)
1049 int width_in_mbs = (width + 15) / 16;
1050 int height_in_mbs = (height + 15) / 16;
1051 int frame_cropping_flag = 0;
1052 int frame_crop_bottom_offset = 0;
1054 seq_param->seq_parameter_set_id = 0;
1055 seq_param->profile_idc = PROFILE_IDC_MAIN;
1056 seq_param->level_idc = 41;
1057 seq_param->intra_period = intra_period;
1058 seq_param->ip_period = 0; /* FIXME: ??? */
1059 seq_param->max_num_ref_frames = 4;
1060 seq_param->picture_width_in_mbs = width_in_mbs;
1061 seq_param->picture_height_in_mbs = height_in_mbs;
1062 seq_param->frame_mbs_only_flag = 1;
1063 seq_param->target_usage = 1;
1065 /* 0:CBR, 1:VBR, 2:Constant QP */
1067 seq_param->rate_control_method = BR_CBR;
1068 else if (qp_value == -2)
1069 seq_param->rate_control_method = BR_VBR;
1071 assert(qp_value >= 0 && qp_value <= 51);
1072 seq_param->rate_control_method = BR_CQP;
1075 if (frame_bit_rate > 0)
1076 seq_param->bits_per_second = 30 * frame_bit_rate;
1078 seq_param->bits_per_second = 0;
1080 if (seq_param->rate_control_method == BR_VBR) {
1081 seq_param->max_bits_per_second = 0; /* FIXME: set it later */
1082 seq_param->min_bits_per_second = 0;
1085 seq_param->initial_hrd_buffer_fullness = 0; /* FIXME: ??? */
1086 seq_param->hrd_buffer_size = 0; /* FIXME: ??? */
1087 seq_param->time_scale = 900;
1088 seq_param->num_units_in_tick = 15;
1090 if (height_in_mbs * 16 - height) {
1091 frame_cropping_flag = 1;
1092 frame_crop_bottom_offset =
1093 (height_in_mbs * 16 - height) / (2 * (!seq_param->frame_mbs_only_flag + 1));
1096 seq_param->frame_cropping_flag = frame_cropping_flag;
1097 seq_param->frame_crop_left_offset = 0;
1098 seq_param->frame_crop_right_offset = 0;
1099 seq_param->frame_crop_top_offset = 0;
1100 seq_param->frame_crop_bottom_offset = frame_crop_bottom_offset;
1102 seq_param->pic_order_cnt_type = 0;
1103 seq_param->direct_8x8_inference_flag = 0;
1105 seq_param->log2_max_frame_num_minus4 = 0;
1106 seq_param->log2_max_pic_order_cnt_lsb_minus4 = 2;
1108 seq_param->vui_flag = 0;
1111 static void avcenc_context_pic_param_init(VAEncPictureParameterBufferH264Ext *pic_param)
1113 pic_param->seq_parameter_set_id = 0;
1114 pic_param->pic_parameter_set_id = 0;
1116 pic_param->last_picture = 0;
1117 pic_param->frame_num = 0;
1118 pic_param->coding_type = 0;
1120 pic_param->pic_init_qp = (qp_value >= 0 ? qp_value : 26);
1121 pic_param->num_ref_idx_l0_active_minus1 = 0;
1122 pic_param->num_ref_idx_l1_active_minus1 = 0;
1124 pic_param->pic_fields.bits.idr_pic_flag = 0;
1125 pic_param->pic_fields.bits.reference_pic_flag = 0;
1126 pic_param->pic_fields.bits.entropy_coding_mode_flag = ENTROPY_MODE_CABAC;
1127 pic_param->pic_fields.bits.weighted_pred_flag = 0;
1128 pic_param->pic_fields.bits.weighted_bipred_idc = 0;
1129 pic_param->pic_fields.bits.transform_8x8_mode_flag = 0;
1130 pic_param->pic_fields.bits.deblocking_filter_control_present_flag = 1;
1133 static void avcenc_context_init(int width, int height)
1136 memset(&avcenc_context, 0, sizeof(avcenc_context));
1137 avcenc_context.seq_param_buf_id = VA_INVALID_ID;
1138 avcenc_context.pic_param_buf_id = VA_INVALID_ID;
1139 avcenc_context.dec_ref_pic_marking_buf_id = VA_INVALID_ID;
1140 avcenc_context.packed_seq_buf_id = VA_INVALID_ID;
1141 avcenc_context.packed_pic_buf_id = VA_INVALID_ID;
1142 avcenc_context.codedbuf_buf_id = VA_INVALID_ID;
1143 avcenc_context.codedbuf_i_size = width * height;
1144 avcenc_context.codedbuf_pb_size = 0;
1146 for (i = 0; i < MAX_SLICES; i++) {
1147 avcenc_context.slice_param_buf_id[i] = VA_INVALID_ID;
1150 avcenc_context_seq_param_init(&avcenc_context.seq_param, width, height);
1151 avcenc_context_pic_param_init(&avcenc_context.pic_param);
1154 int main(int argc, char *argv[])
1161 int i_frame_only=0,i_p_frame_only=1;
1163 struct timeval tpstart,tpend;
1166 va_init_display_args(&argc, argv);
1168 //TODO may be we should using option analytics library
1169 if(argc != 5 && argc != 6 && argc != 7) {
1174 picture_width = atoi(argv[1]);
1175 picture_height = atoi(argv[2]);
1176 picture_width_in_mbs = (picture_width + 15) / 16;
1177 picture_height_in_mbs = (picture_height + 15) / 16;
1179 if (argc == 6 || argc == 7) {
1181 sscanf(argv[5], "qp=%d", &qp_value);
1182 if ( qp_value == -1 ) {
1183 frame_bit_rate = -1;
1184 sscanf(argv[5], "fb=%d", &frame_bit_rate);
1185 if ( frame_bit_rate == -1 ) {
1189 } else if (qp_value > 51) {
1191 } else if (qp_value < 0) {
1195 qp_value = 28; //default const QP mode
1198 sscanf(argv[6], "mode=%d", &mode_value);
1199 if ( mode_value == 0 ) {
1203 else if ( mode_value == 1) {
1207 else if ( mode_value == 2 ) {
1212 printf("mode_value=%d\n",mode_value);
1218 yuv_fp = fopen(argv[3],"rb");
1219 if ( yuv_fp == NULL){
1220 printf("Can't open input YUV file\n");
1223 fseek(yuv_fp,0l, SEEK_END);
1224 file_size = ftell(yuv_fp);
1225 frame_size = picture_width * picture_height + ((picture_width * picture_height) >> 1) ;
1227 if ( (file_size < frame_size) || (file_size % frame_size) ) {
1228 printf("The YUV file's size is not correct\n");
1231 frame_number = file_size / frame_size;
1232 fseek(yuv_fp, 0l, SEEK_SET);
1234 avc_fp = fopen(argv[4], "wb");
1235 if ( avc_fp == NULL) {
1236 printf("Can't open output avc file\n");
1239 gettimeofday(&tpstart,NULL);
1240 avcenc_context_init(picture_width, picture_height);
1241 create_encode_pipe();
1242 alloc_encode_resource();
1244 enc_frame_number = 0;
1245 for ( f = 0; f < frame_number; ) { //picture level loop
1246 int is_intra = i_frame_only?1:(enc_frame_number % intra_period == 0);
1247 int is_idr = (f == 0);
1250 if ( ! is_intra && pb_period > 0) {
1251 is_bslice = i_p_frame_only?0:(f % pb_period == 1) && (f < frame_number - 1);
1255 encode_i_picture(yuv_fp, avc_fp, f, is_idr);
1258 } else if ( is_bslice) {
1259 encode_pb_pictures(yuv_fp, avc_fp, f, 2); //last parameter is continue B frames number
1263 encode_p_picture(yuv_fp, avc_fp, f);
1268 printf("\r %d/%d ...", f+1, frame_number);
1272 gettimeofday(&tpend,NULL);
1273 timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+ tpend.tv_usec-tpstart.tv_usec;
1275 printf("\ndone!\n");
1276 printf("encode %d frames in %f secondes, FPS is %.1f\n",frame_number, timeuse, frame_number/timeuse);
1277 release_encode_resource();
1278 destory_encode_pipe();