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]
39 #include <sys/types.h>
48 #include "va_display.h"
50 #define NAL_REF_IDC_NONE 0
51 #define NAL_REF_IDC_LOW 1
52 #define NAL_REF_IDC_MEDIUM 2
53 #define NAL_REF_IDC_HIGH 3
60 #define SLICE_TYPE_P 0
61 #define SLICE_TYPE_B 1
62 #define SLICE_TYPE_I 2
64 #define ENTROPY_MODE_CAVLC 0
65 #define ENTROPY_MODE_CABAC 1
67 #define PROFILE_IDC_BASELINE 66
68 #define PROFILE_IDC_MAIN 77
69 #define PROFILE_IDC_HIGH 100
71 #define CHECK_VASTATUS(va_status,func) \
72 if (va_status != VA_STATUS_SUCCESS) { \
73 fprintf(stderr,"%s:%s (%d) failed,exit\n", __func__, func, __LINE__); \
77 static VADisplay va_dpy;
79 static int picture_width, picture_width_in_mbs;
80 static int picture_height, picture_height_in_mbs;
81 static int frame_size;
82 static unsigned char *newImageBuffer = 0;
84 static int qp_value = 26;
86 static int intra_period = 30;
87 static int pb_period = 5;
88 static int frame_bit_rate = -1;
97 build_packed_pic_buffer(unsigned char **header_buffer);
100 build_packed_seq_buffer(unsigned char **header_buffer);
102 struct upload_thread_param
105 VASurfaceID surface_id;
109 upload_yuv_to_surface(FILE *yuv_fp, VASurfaceID surface_id);
111 struct packed_data_format
113 unsigned int length_in_bits;
115 unsigned char num_skip_bytes;
116 unsigned char pad[2];
120 VAEncSequenceParameterBufferH264Ext seq_param;
121 VAEncPictureParameterBufferH264Ext pic_param;
122 VAEncSliceParameterBufferH264Ext slice_param[MAX_SLICES];
123 VAEncH264DecRefPicMarkingBuffer dec_ref_pic_marking;
124 VAContextID context_id;
125 VAConfigID config_id;
126 VABufferID seq_param_buf_id; /* Sequence level parameter */
127 VABufferID pic_param_buf_id; /* Picture level parameter */
128 VABufferID slice_param_buf_id[MAX_SLICES]; /* Slice level parameter, multil slices */
129 VABufferID dec_ref_pic_marking_buf_id;
130 VABufferID codedbuf_buf_id; /* Output buffer, compressed data */
131 VABufferID packed_seq_buf_id;
132 VABufferID packed_pic_buf_id;
135 int codedbuf_pb_size;
136 int current_input_surface;
137 struct upload_thread_param upload_thread_param;
138 pthread_t upload_thread_id;
139 int upload_thread_value;
142 static void create_encode_pipe()
144 VAEntrypoint entrypoints[5];
145 int num_entrypoints,slice_entrypoint;
146 VAConfigAttrib attrib[2];
147 int major_ver, minor_ver;
150 va_dpy = va_open_display();
151 va_status = vaInitialize(va_dpy, &major_ver, &minor_ver);
152 CHECK_VASTATUS(va_status, "vaInitialize");
154 vaQueryConfigEntrypoints(va_dpy, VAProfileH264Baseline, entrypoints,
157 for (slice_entrypoint = 0; slice_entrypoint < num_entrypoints; slice_entrypoint++) {
158 if (entrypoints[slice_entrypoint] == VAEntrypointEncSlice)
162 if (slice_entrypoint == num_entrypoints) {
163 /* not find Slice entry point */
167 /* find out the format for the render target, and rate control mode */
168 attrib[0].type = VAConfigAttribRTFormat;
169 attrib[1].type = VAConfigAttribRateControl;
170 vaGetConfigAttributes(va_dpy, VAProfileH264Baseline, VAEntrypointEncSlice,
173 if ((attrib[0].value & VA_RT_FORMAT_YUV420) == 0) {
174 /* not find desired YUV420 RT format */
178 if ((attrib[1].value & VA_RC_VBR) == 0) {
179 /* Can't find matched RC mode */
180 printf("VBR mode doesn't found, exit\n");
184 attrib[0].value = VA_RT_FORMAT_YUV420; /* set to desired RT format */
185 attrib[1].value = VA_RC_VBR; /* set to desired RC mode */
187 va_status = vaCreateConfig(va_dpy, VAProfileH264Baseline, VAEntrypointEncSlice,
188 &attrib[0], 2,&avcenc_context.config_id);
189 CHECK_VASTATUS(va_status, "vaCreateConfig");
191 /* Create a context for this decode pipe */
192 va_status = vaCreateContext(va_dpy, avcenc_context.config_id,
193 picture_width, picture_height,
196 &avcenc_context.context_id);
197 CHECK_VASTATUS(va_status, "vaCreateContext");
200 static void destory_encode_pipe()
202 vaDestroyContext(va_dpy,avcenc_context.context_id);
203 vaDestroyConfig(va_dpy,avcenc_context.config_id);
205 va_close_display(va_dpy);
208 /***************************************************
210 * The encode pipe resource define
212 ***************************************************/
213 #define SID_INPUT_PICTURE_0 0
214 #define SID_INPUT_PICTURE_1 1
215 #define SID_REFERENCE_PICTURE_L0 2
216 #define SID_REFERENCE_PICTURE_L1 3
217 #define SID_RECON_PICTURE 4
218 #define SID_NUMBER SID_RECON_PICTURE + 1
219 static VASurfaceID surface_ids[SID_NUMBER];
221 static int frame_number;
222 static int enc_frame_number;
224 /***************************************************/
227 upload_thread_function(void *data)
229 struct upload_thread_param *param = data;
231 upload_yuv_to_surface(param->yuv_fp, param->surface_id);
236 static void alloc_encode_resource(FILE *yuv_fp)
241 va_status = vaCreateSurfaces(va_dpy, picture_width, picture_height,
242 VA_RT_FORMAT_YUV420, SID_NUMBER, &surface_ids[0]);
243 CHECK_VASTATUS(va_status, "vaCreateSurfaces");
245 newImageBuffer = (unsigned char *)malloc(frame_size);
247 /* firstly upload YUV data to SID_INPUT_PICTURE_1 */
248 avcenc_context.upload_thread_param.yuv_fp = yuv_fp;
249 avcenc_context.upload_thread_param.surface_id = surface_ids[SID_INPUT_PICTURE_1];
251 avcenc_context.upload_thread_value = pthread_create(&avcenc_context.upload_thread_id,
253 upload_thread_function,
254 (void*)&avcenc_context.upload_thread_param);
257 static void release_encode_resource()
259 free(newImageBuffer);
261 // Release all the surfaces resource
262 vaDestroySurfaces(va_dpy, &surface_ids[0], SID_NUMBER);
265 static void avcenc_update_picture_parameter(int slice_type, int frame_num, int display_num, int is_idr)
267 VAEncPictureParameterBufferH264Ext *pic_param;
271 pic_param = &avcenc_context.pic_param;
272 pic_param->CurrPic.picture_id = surface_ids[SID_RECON_PICTURE];
273 pic_param->CurrPic.TopFieldOrderCnt = display_num * 2;
274 pic_param->ReferenceFrames[0].picture_id = surface_ids[SID_REFERENCE_PICTURE_L0];
275 pic_param->ReferenceFrames[1].picture_id = surface_ids[SID_REFERENCE_PICTURE_L1];
276 pic_param->ReferenceFrames[2].picture_id = VA_INVALID_ID;
277 assert(avcenc_context.codedbuf_buf_id != VA_INVALID_ID);
278 pic_param->CodedBuf = avcenc_context.codedbuf_buf_id;
279 pic_param->frame_num = frame_num;
280 pic_param->pic_fields.bits.idr_pic_flag = !!is_idr;
281 pic_param->pic_fields.bits.reference_pic_flag = (slice_type != SLICE_TYPE_B);
283 va_status = vaCreateBuffer(va_dpy,
284 avcenc_context.context_id,
285 VAEncPictureParameterBufferExtType,
286 sizeof(*pic_param), 1, pic_param,
287 &avcenc_context.pic_param_buf_id);
288 CHECK_VASTATUS(va_status,"vaCreateBuffer");
291 static void upload_yuv_to_surface(FILE *yuv_fp, VASurfaceID surface_id)
293 VAImage surface_image;
295 void *surface_p = NULL;
296 unsigned char *y_src, *u_src, *v_src;
297 unsigned char *y_dst, *u_dst, *v_dst;
298 int y_size = picture_width * picture_height;
299 int u_size = (picture_width >> 1) * (picture_height >> 1);
304 n_items = fread(newImageBuffer, frame_size, 1, yuv_fp);
305 } while (n_items != 1);
307 va_status = vaDeriveImage(va_dpy, surface_id, &surface_image);
308 CHECK_VASTATUS(va_status,"vaDeriveImage");
310 vaMapBuffer(va_dpy, surface_image.buf, &surface_p);
311 assert(VA_STATUS_SUCCESS == va_status);
313 y_src = newImageBuffer;
314 u_src = newImageBuffer + y_size; /* UV offset for NV12 */
315 v_src = newImageBuffer + y_size + u_size;
317 y_dst = surface_p + surface_image.offsets[0];
318 u_dst = surface_p + surface_image.offsets[1]; /* UV offset for NV12 */
319 v_dst = surface_p + surface_image.offsets[2];
322 for (row = 0; row < surface_image.height; row++) {
323 memcpy(y_dst, y_src, surface_image.width);
324 y_dst += surface_image.pitches[0];
325 y_src += picture_width;
328 if (surface_image.format.fourcc == VA_FOURCC_NV12) { /* UV plane */
329 for (row = 0; row < surface_image.height / 2; row++) {
330 for (col = 0; col < surface_image.width / 2; col++) {
331 u_dst[col * 2] = u_src[col];
332 u_dst[col * 2 + 1] = v_src[col];
335 u_dst += surface_image.pitches[1];
336 u_src += (picture_width / 2);
337 v_src += (picture_width / 2);
340 /* FIXME: fix this later */
344 vaUnmapBuffer(va_dpy, surface_image.buf);
345 vaDestroyImage(va_dpy, surface_image.image_id);
348 static void avcenc_update_slice_parameter(int slice_type)
350 VAEncSliceParameterBufferH264Ext *slice_param;
356 slice_param = &avcenc_context.slice_param[i];
357 slice_param->start_row_number = 0;
358 slice_param->slice_height = picture_height_in_mbs/16; /* Measured by MB */
359 slice_param->pic_parameter_set_id = 0;
360 slice_param->slice_type = slice_type;
361 slice_param->direct_spatial_mv_pred_flag = 0;
362 slice_param->num_ref_idx_l0_active_minus1 = 0; /* FIXME: ??? */
363 slice_param->num_ref_idx_l1_active_minus1 = 0;
364 slice_param->cabac_init_idc = 0;
365 slice_param->slice_qp_delta = 0;
366 slice_param->disable_deblocking_filter_idc = 0;
367 slice_param->slice_alpha_c0_offset_div2 = 2;
368 slice_param->slice_beta_offset_div2 = 2;
369 slice_param->idr_pic_id = 0;
371 /* ref_pic_list_modification() */
372 slice_param->ref_pic_list_modification_flag_l0 = 0;
373 slice_param->ref_pic_list_modification_flag_l1 = 0;
374 /* FIXME: fill other fields */
376 va_status = vaCreateBuffer(va_dpy,
377 avcenc_context.context_id,
378 VAEncSliceParameterBufferExtType,
379 sizeof(*slice_param), 1, slice_param,
380 &avcenc_context.slice_param_buf_id[i]);
381 CHECK_VASTATUS(va_status,"vaCreateBuffer");;
385 avcenc_context.num_slices = i;
388 static int begin_picture(FILE *yuv_fp, int frame_num, int display_num, int slice_type, int is_idr)
392 if (avcenc_context.upload_thread_value != 0) {
393 fprintf(stderr, "FATAL error!!!\n");
397 pthread_join(avcenc_context.upload_thread_id, NULL);
399 avcenc_context.upload_thread_value = -1;
401 if (avcenc_context.current_input_surface == SID_INPUT_PICTURE_0)
402 avcenc_context.current_input_surface = SID_INPUT_PICTURE_1;
404 avcenc_context.current_input_surface = SID_INPUT_PICTURE_0;
406 if (frame_num == 0) {
407 unsigned char *packed_seq_buffer = NULL, *packed_pic_buffer = NULL;
408 int seq_length, pic_length;
410 assert(slice_type == SLICE_TYPE_I);
411 seq_length = build_packed_seq_buffer(&packed_seq_buffer);
412 va_status = vaCreateBuffer(va_dpy,
413 avcenc_context.context_id,
414 VAEncPackedSequenceParameterBufferType,
415 (seq_length + 7) / 8, 1, packed_seq_buffer,
416 &avcenc_context.packed_seq_buf_id);
417 CHECK_VASTATUS(va_status,"vaCreateBuffer");;
419 pic_length = build_packed_pic_buffer(&packed_pic_buffer);
420 va_status = vaCreateBuffer(va_dpy,
421 avcenc_context.context_id,
422 VAEncPackedPictureParameterBufferType,
423 (pic_length + 7) / 8 , 1, packed_pic_buffer,
424 &avcenc_context.packed_pic_buf_id);
425 CHECK_VASTATUS(va_status,"vaCreateBuffer");;
427 free(packed_seq_buffer);
428 free(packed_pic_buffer);
431 /* sequence parameter set */
432 VAEncSequenceParameterBufferH264Ext *seq_param = &avcenc_context.seq_param;
433 va_status = vaCreateBuffer(va_dpy,
434 avcenc_context.context_id,
435 VAEncSequenceParameterBufferExtType,
436 sizeof(*seq_param), 1, seq_param,
437 &avcenc_context.seq_param_buf_id);
438 CHECK_VASTATUS(va_status,"vaCreateBuffer");;
440 /* slice parameter */
441 avcenc_update_slice_parameter(slice_type);
446 int avcenc_render_picture()
449 VABufferID va_buffers[8];
450 unsigned int num_va_buffers = 0;
452 va_buffers[num_va_buffers++] = avcenc_context.seq_param_buf_id;
453 va_buffers[num_va_buffers++] = avcenc_context.pic_param_buf_id;
455 if (avcenc_context.dec_ref_pic_marking_buf_id != VA_INVALID_ID)
456 va_buffers[num_va_buffers++] = avcenc_context.dec_ref_pic_marking_buf_id;
458 if (avcenc_context.packed_seq_buf_id != VA_INVALID_ID)
459 va_buffers[num_va_buffers++] = avcenc_context.packed_seq_buf_id;
461 if (avcenc_context.packed_pic_buf_id != VA_INVALID_ID)
462 va_buffers[num_va_buffers++] = avcenc_context.packed_pic_buf_id;
464 va_status = vaBeginPicture(va_dpy,
465 avcenc_context.context_id,
466 surface_ids[avcenc_context.current_input_surface]);
467 CHECK_VASTATUS(va_status,"vaBeginPicture");
469 va_status = vaRenderPicture(va_dpy,
470 avcenc_context.context_id,
473 CHECK_VASTATUS(va_status,"vaRenderPicture");
475 va_status = vaRenderPicture(va_dpy,
476 avcenc_context.context_id,
477 &avcenc_context.slice_param_buf_id[0],
478 avcenc_context.num_slices);
479 CHECK_VASTATUS(va_status,"vaRenderPicture");
481 va_status = vaEndPicture(va_dpy, avcenc_context.context_id);
482 CHECK_VASTATUS(va_status,"vaEndPicture");
487 static int avcenc_destroy_buffers(VABufferID *va_buffers, unsigned int num_va_buffers)
492 for (i = 0; i < num_va_buffers; i++) {
493 if (va_buffers[i] != VA_INVALID_ID) {
494 va_status = vaDestroyBuffer(va_dpy, va_buffers[i]);
495 CHECK_VASTATUS(va_status,"vaDestroyBuffer");
496 va_buffers[i] = VA_INVALID_ID;
503 static void end_picture(int slice_type, int next_is_bpic)
507 /* Prepare for next picture */
508 tempID = surface_ids[SID_RECON_PICTURE];
510 if (slice_type != SLICE_TYPE_B) {
512 surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L1];
513 surface_ids[SID_REFERENCE_PICTURE_L1] = tempID;
515 surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L0];
516 surface_ids[SID_REFERENCE_PICTURE_L0] = tempID;
520 surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L0];
521 surface_ids[SID_REFERENCE_PICTURE_L0] = surface_ids[SID_REFERENCE_PICTURE_L1];
522 surface_ids[SID_REFERENCE_PICTURE_L1] = tempID;
526 avcenc_destroy_buffers(&avcenc_context.seq_param_buf_id, 1);
527 avcenc_destroy_buffers(&avcenc_context.pic_param_buf_id, 1);
528 avcenc_destroy_buffers(&avcenc_context.dec_ref_pic_marking_buf_id, 1);
529 avcenc_destroy_buffers(&avcenc_context.packed_seq_buf_id, 1);
530 avcenc_destroy_buffers(&avcenc_context.packed_pic_buf_id, 1);
531 avcenc_destroy_buffers(&avcenc_context.slice_param_buf_id[0], avcenc_context.num_slices);
532 avcenc_destroy_buffers(&avcenc_context.codedbuf_buf_id, 1);
533 memset(avcenc_context.slice_param, 0, sizeof(avcenc_context.slice_param));
534 avcenc_context.num_slices = 0;
537 #define BITSTREAM_ALLOCATE_STEPPING 4096
540 unsigned int *buffer;
542 int max_size_in_dword;
545 typedef struct __bitstream bitstream;
549 get_coded_bitsteam_length(unsigned char *buffer, int buffer_length)
553 for (i = 0; i < buffer_length - 3; i++) {
566 va_swap32(unsigned int val)
568 unsigned char *pval = (unsigned char *)&val;
570 return ((pval[0] << 24) |
577 bitstream_start(bitstream *bs)
579 bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
580 bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
581 bs->bit_offset = sizeof(struct packed_data_format) * 8; /* the first 64 bits used for format */
585 bitstream_end(bitstream *bs)
587 int pos = (bs->bit_offset >> 5);
588 int bit_offset = (bs->bit_offset & 0x1f);
589 int bit_left = 32 - bit_offset;
590 struct packed_data_format *format;
593 bs->buffer[pos] = va_swap32((bs->buffer[pos] << bit_left));
596 format = (struct packed_data_format *)bs->buffer;
597 format->length_in_bits = bs->bit_offset - sizeof(struct packed_data_format) * 8;
599 format->num_skip_bytes = 5; /* ignore start code & nal type for emulation prevetion check */
603 bitstream_put_ui(bitstream *bs, unsigned int val, int size_in_bits)
605 int pos = (bs->bit_offset >> 5);
606 int bit_offset = (bs->bit_offset & 0x1f);
607 int bit_left = 32 - bit_offset;
612 bs->bit_offset += size_in_bits;
614 if (bit_left > size_in_bits) {
615 bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
617 size_in_bits -= bit_left;
618 bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
619 bs->buffer[pos] = va_swap32(bs->buffer[pos]);
621 if (pos + 1 == bs->max_size_in_dword) {
622 bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
623 bs->buffer = realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
626 bs->buffer[pos + 1] = val;
631 bitstream_put_ue(bitstream *bs, unsigned int val)
633 int size_in_bits = 0;
641 bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
642 bitstream_put_ui(bs, val, size_in_bits);
646 bitstream_put_se(bitstream *bs, int val)
648 unsigned int new_val;
653 new_val = 2 * val - 1;
655 bitstream_put_ue(bs, new_val);
659 bitstream_byte_aligning(bitstream *bs, int bit)
661 int bit_offset = (bs->bit_offset & 0x7);
662 int bit_left = 8 - bit_offset;
668 assert(bit == 0 || bit == 1);
671 new_val = (1 << bit_left) - 1;
675 bitstream_put_ui(bs, new_val, bit_left);
679 rbsp_trailing_bits(bitstream *bs)
681 bitstream_put_ui(bs, 1, 1);
682 bitstream_byte_aligning(bs, 0);
685 static void nal_start_code_prefix(bitstream *bs)
687 bitstream_put_ui(bs, 0x00000001, 32);
690 static void nal_header(bitstream *bs, int nal_ref_idc, int nal_unit_type)
692 bitstream_put_ui(bs, 0, 1); /* forbidden_zero_bit: 0 */
693 bitstream_put_ui(bs, nal_ref_idc, 2);
694 bitstream_put_ui(bs, nal_unit_type, 5);
697 static void sps_rbsp(bitstream *bs)
699 VAEncSequenceParameterBufferH264Ext *seq_param = &avcenc_context.seq_param;
701 bitstream_put_ui(bs, seq_param->profile_idc, 8); /* profile_idc */
702 bitstream_put_ui(bs, 0, 1); /* constraint_set0_flag */
703 bitstream_put_ui(bs, 1, 1); /* constraint_set1_flag */
704 bitstream_put_ui(bs, 0, 1); /* constraint_set2_flag */
705 bitstream_put_ui(bs, 0, 1); /* constraint_set3_flag */
706 bitstream_put_ui(bs, 0, 4); /* reserved_zero_4bits */
707 bitstream_put_ui(bs, seq_param->level_idc, 8); /* level_idc */
708 bitstream_put_ue(bs, seq_param->seq_parameter_set_id); /* seq_parameter_set_id */
710 if (seq_param->profile_idc >= PROFILE_IDC_HIGH) {
711 /* FIXME: fix for high profile */
715 bitstream_put_ue(bs, seq_param->log2_max_frame_num_minus4); /* log2_max_frame_num_minus4 */
716 bitstream_put_ue(bs, seq_param->pic_order_cnt_type); /* pic_order_cnt_type */
718 if (seq_param->pic_order_cnt_type == 0)
719 bitstream_put_ue(bs, seq_param->log2_max_pic_order_cnt_lsb_minus4); /* log2_max_pic_order_cnt_lsb_minus4 */
724 bitstream_put_ue(bs, seq_param->max_num_ref_frames); /* num_ref_frames */
725 bitstream_put_ui(bs, 0, 1); /* gaps_in_frame_num_value_allowed_flag */
727 bitstream_put_ue(bs, seq_param->picture_width_in_mbs - 1); /* pic_width_in_mbs_minus1 */
728 bitstream_put_ue(bs, seq_param->picture_height_in_mbs - 1); /* pic_height_in_map_units_minus1 */
729 bitstream_put_ui(bs, seq_param->frame_mbs_only_flag, 1); /* frame_mbs_only_flag */
731 if (!seq_param->frame_mbs_only_flag) {
735 bitstream_put_ui(bs, seq_param->direct_8x8_inference_flag, 1); /* direct_8x8_inference_flag */
736 bitstream_put_ui(bs, seq_param->frame_cropping_flag, 1); /* frame_cropping_flag */
738 if (seq_param->frame_cropping_flag) {
739 bitstream_put_ue(bs, seq_param->frame_crop_left_offset); /* frame_crop_left_offset */
740 bitstream_put_ue(bs, seq_param->frame_crop_right_offset); /* frame_crop_right_offset */
741 bitstream_put_ue(bs, seq_param->frame_crop_top_offset); /* frame_crop_top_offset */
742 bitstream_put_ue(bs, seq_param->frame_crop_bottom_offset); /* frame_crop_bottom_offset */
745 bitstream_put_ui(bs, 0, 1); /* vui_parameters_present_flag */
746 rbsp_trailing_bits(bs); /* rbsp_trailing_bits */
750 static void build_nal_sps(FILE *avc_fp)
754 bitstream_start(&bs);
755 nal_start_code_prefix(&bs);
756 nal_header(&bs, NAL_REF_IDC_HIGH, NAL_SPS);
758 bitstream_end(&bs, avc_fp);
762 static void pps_rbsp(bitstream *bs)
764 VAEncPictureParameterBufferH264Ext *pic_param = &avcenc_context.pic_param;
766 bitstream_put_ue(bs, pic_param->pic_parameter_set_id); /* pic_parameter_set_id */
767 bitstream_put_ue(bs, pic_param->seq_parameter_set_id); /* seq_parameter_set_id */
769 bitstream_put_ui(bs, pic_param->pic_fields.bits.entropy_coding_mode_flag, 1); /* entropy_coding_mode_flag */
771 bitstream_put_ui(bs, 0, 1); /* pic_order_present_flag: 0 */
773 bitstream_put_ue(bs, 0); /* num_slice_groups_minus1 */
775 bitstream_put_ue(bs, pic_param->num_ref_idx_l0_active_minus1); /* num_ref_idx_l0_active_minus1 */
776 bitstream_put_ue(bs, pic_param->num_ref_idx_l1_active_minus1); /* num_ref_idx_l1_active_minus1 1 */
778 bitstream_put_ui(bs, pic_param->pic_fields.bits.weighted_pred_flag, 1); /* weighted_pred_flag: 0 */
779 bitstream_put_ui(bs, pic_param->pic_fields.bits.weighted_bipred_idc, 2); /* weighted_bipred_idc: 0 */
781 bitstream_put_se(bs, pic_param->pic_init_qp - 26); /* pic_init_qp_minus26 */
782 bitstream_put_se(bs, 0); /* pic_init_qs_minus26 */
783 bitstream_put_se(bs, 0); /* chroma_qp_index_offset */
785 bitstream_put_ui(bs, pic_param->pic_fields.bits.deblocking_filter_control_present_flag, 1); /* deblocking_filter_control_present_flag */
786 bitstream_put_ui(bs, 0, 1); /* constrained_intra_pred_flag */
787 bitstream_put_ui(bs, 0, 1); /* redundant_pic_cnt_present_flag */
789 rbsp_trailing_bits(bs);
793 static void build_nal_pps(FILE *avc_fp)
797 bitstream_start(&bs);
798 nal_start_code_prefix(&bs);
799 nal_header(&bs, NAL_REF_IDC_HIGH, NAL_PPS);
801 bitstream_end(&bs, avc_fp);
805 build_header(FILE *avc_fp)
807 build_nal_sps(avc_fp);
808 build_nal_pps(avc_fp);
813 build_packed_pic_buffer(unsigned char **header_buffer)
817 bitstream_start(&bs);
818 nal_start_code_prefix(&bs);
819 nal_header(&bs, NAL_REF_IDC_HIGH, NAL_PPS);
823 *header_buffer = (unsigned char *)bs.buffer;
824 return bs.bit_offset;
828 build_packed_seq_buffer(unsigned char **header_buffer)
832 bitstream_start(&bs);
833 nal_start_code_prefix(&bs);
834 nal_header(&bs, NAL_REF_IDC_HIGH, NAL_SPS);
838 *header_buffer = (unsigned char *)bs.buffer;
839 return bs.bit_offset;
845 slice_header(bitstream *bs, int frame_num, int display_frame, int slice_type, int nal_ref_idc, int is_idr)
847 VAEncSequenceParameterBufferH264Ext *seq_param = &avcenc_context.seq_param;
848 VAEncPictureParameterBufferH264Ext *pic_param = &avcenc_context.pic_param;
849 int is_cabac = (pic_param->pic_fields.bits.entropy_coding_mode_flag == ENTROPY_MODE_CABAC);
851 bitstream_put_ue(bs, 0); /* first_mb_in_slice: 0 */
852 bitstream_put_ue(bs, slice_type); /* slice_type */
853 bitstream_put_ue(bs, 0); /* pic_parameter_set_id: 0 */
854 bitstream_put_ui(bs, frame_num & 0x0F, seq_param->log2_max_frame_num_minus4 + 4); /* frame_num */
856 /* frame_mbs_only_flag == 1 */
857 if (!seq_param->frame_mbs_only_flag) {
863 bitstream_put_ue(bs, 0); /* idr_pic_id: 0 */
865 if (seq_param->pic_order_cnt_type == 0) {
866 bitstream_put_ui(bs, (display_frame*2) & 0x3F, seq_param->log2_max_pic_order_cnt_lsb_minus4 + 4);
867 /* only support frame */
873 /* redundant_pic_cnt_present_flag == 0 */
876 if (slice_type == SLICE_TYPE_P) {
877 bitstream_put_ui(bs, 0, 1); /* num_ref_idx_active_override_flag: 0 */
878 /* ref_pic_list_reordering */
879 bitstream_put_ui(bs, 0, 1); /* ref_pic_list_reordering_flag_l0: 0 */
880 } else if (slice_type == SLICE_TYPE_B) {
881 bitstream_put_ui(bs, 1, 1); /* direct_spatial_mv_pred: 1 */
882 bitstream_put_ui(bs, 0, 1); /* num_ref_idx_active_override_flag: 0 */
883 /* ref_pic_list_reordering */
884 bitstream_put_ui(bs, 0, 1); /* ref_pic_list_reordering_flag_l0: 0 */
885 bitstream_put_ui(bs, 0, 1); /* ref_pic_list_reordering_flag_l1: 0 */
888 /* weighted_pred_flag == 0 */
890 /* dec_ref_pic_marking */
891 if (nal_ref_idc != 0) {
893 bitstream_put_ui(bs, 0, 1); /* no_output_of_prior_pics_flag: 0 */
894 bitstream_put_ui(bs, 0, 1); /* long_term_reference_flag: 0 */
896 bitstream_put_ui(bs, 0, 1); /* adaptive_ref_pic_marking_mode_flag: 0 */
900 if (is_cabac && (slice_type != SLICE_TYPE_I))
901 bitstream_put_ue(bs, 0); /* cabac_init_idc: 0 */
903 bitstream_put_se(bs, 0); /* slice_qp_delta: 0 */
905 if (pic_param->pic_fields.bits.deblocking_filter_control_present_flag == 1) {
906 bitstream_put_ue(bs, 0); /* disable_deblocking_filter_idc: 0 */
907 bitstream_put_se(bs, 2); /* slice_alpha_c0_offset_div2: 2 */
908 bitstream_put_se(bs, 2); /* slice_beta_offset_div2: 2 */
913 slice_data(bitstream *bs)
915 VACodedBufferSegment *coded_buffer_segment;
916 unsigned char *coded_mem;
917 int i, slice_data_length;
919 VASurfaceStatus surface_status;
921 va_status = vaSyncSurface(va_dpy, surface_ids[avcenc_context.current_input_surface]);
922 CHECK_VASTATUS(va_status,"vaSyncSurface");
925 va_status = vaQuerySurfaceStatus(va_dpy, surface_ids[avcenc_context.current_input_surface], &surface_status);
926 CHECK_VASTATUS(va_status,"vaQuerySurfaceStatus");
928 va_status = vaMapBuffer(va_dpy, avcenc_context.codedbuf_buf_id, (void **)(&coded_buffer_segment));
929 CHECK_VASTATUS(va_status,"vaMapBuffer");
930 coded_mem = coded_buffer_segment->buf;
932 slice_data_length = get_coded_bitsteam_length(coded_mem, codedbuf_size);
934 for (i = 0; i < slice_data_length; i++) {
935 bitstream_put_ui(bs, *coded_mem, 8);
939 vaUnmapBuffer(va_dpy, avcenc_context.codedbuf_buf_id);
943 build_nal_slice(FILE *avc_fp, int frame_num, int display_frame, int slice_type, int is_idr)
947 bitstream_start(&bs);
949 bitstream_end(&bs, avc_fp);
955 store_coded_buffer(FILE *avc_fp, int slice_type)
957 VACodedBufferSegment *coded_buffer_segment;
958 unsigned char *coded_mem;
959 int slice_data_length;
961 VASurfaceStatus surface_status;
964 va_status = vaSyncSurface(va_dpy, surface_ids[avcenc_context.current_input_surface]);
965 CHECK_VASTATUS(va_status,"vaSyncSurface");
968 va_status = vaQuerySurfaceStatus(va_dpy, surface_ids[avcenc_context.current_input_surface], &surface_status);
969 CHECK_VASTATUS(va_status,"vaQuerySurfaceStatus");
971 va_status = vaMapBuffer(va_dpy, avcenc_context.codedbuf_buf_id, (void **)(&coded_buffer_segment));
972 CHECK_VASTATUS(va_status,"vaMapBuffer");
973 coded_mem = coded_buffer_segment->buf;
975 if (coded_buffer_segment->status & VA_CODED_BUF_STATUS_SLICE_OVERFLOW_MASK) {
976 if (slice_type == SLICE_TYPE_I)
977 avcenc_context.codedbuf_i_size *= 2;
979 avcenc_context.codedbuf_pb_size *= 2;
981 vaUnmapBuffer(va_dpy, avcenc_context.codedbuf_buf_id);
985 slice_data_length = coded_buffer_segment->size;
988 w_items = fwrite(coded_mem, slice_data_length, 1, avc_fp);
989 } while (w_items != 1);
991 if (slice_type == SLICE_TYPE_I) {
992 if (avcenc_context.codedbuf_i_size > slice_data_length * 3 / 2) {
993 avcenc_context.codedbuf_i_size = slice_data_length * 3 / 2;
996 if (avcenc_context.codedbuf_pb_size < slice_data_length) {
997 avcenc_context.codedbuf_pb_size = slice_data_length;
1000 if (avcenc_context.codedbuf_pb_size > slice_data_length * 3 / 2) {
1001 avcenc_context.codedbuf_pb_size = slice_data_length * 3 / 2;
1005 vaUnmapBuffer(va_dpy, avcenc_context.codedbuf_buf_id);
1011 encode_picture(FILE *yuv_fp, FILE *avc_fp,
1012 int frame_num, int display_num,
1014 int slice_type, int next_is_bpic,
1015 int next_display_num)
1018 int ret = 0, codedbuf_size;
1020 begin_picture(yuv_fp, frame_num, display_num, slice_type, is_idr);
1022 if (next_display_num < frame_number) {
1025 /* prepare for next frame */
1026 if (avcenc_context.current_input_surface == SID_INPUT_PICTURE_0)
1027 index = SID_INPUT_PICTURE_1;
1029 index = SID_INPUT_PICTURE_0;
1031 fseek(yuv_fp, frame_size * next_display_num, SEEK_SET);
1033 avcenc_context.upload_thread_param.yuv_fp = yuv_fp;
1034 avcenc_context.upload_thread_param.surface_id = surface_ids[index];
1036 avcenc_context.upload_thread_value = pthread_create(&avcenc_context.upload_thread_id,
1038 upload_thread_function,
1039 (void*)&avcenc_context.upload_thread_param);
1043 avcenc_destroy_buffers(&avcenc_context.codedbuf_buf_id, 1);
1044 avcenc_destroy_buffers(&avcenc_context.pic_param_buf_id, 1);
1047 if (SLICE_TYPE_I == slice_type) {
1048 codedbuf_size = avcenc_context.codedbuf_i_size;
1050 codedbuf_size = avcenc_context.codedbuf_pb_size;
1054 va_status = vaCreateBuffer(va_dpy,
1055 avcenc_context.context_id,
1056 VAEncCodedBufferType,
1057 codedbuf_size, 1, NULL,
1058 &avcenc_context.codedbuf_buf_id);
1059 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1061 /* picture parameter set */
1062 avcenc_update_picture_parameter(slice_type, frame_num, display_num, is_idr);
1064 avcenc_render_picture();
1066 ret = store_coded_buffer(avc_fp, slice_type);
1069 end_picture(slice_type, next_is_bpic);
1072 static void encode_i_picture(FILE *yuv_fp, FILE *avc_fp, int f, int is_idr)
1074 encode_picture(yuv_fp, avc_fp,
1075 enc_frame_number, f,
1077 SLICE_TYPE_I, 0, f + 1);
1080 static void encode_p_picture(FILE *yuv_fp, FILE *avc_fp, int f)
1082 encode_picture(yuv_fp, avc_fp,
1083 enc_frame_number, f,
1085 SLICE_TYPE_P, 0, f + 1);
1088 static void encode_pb_pictures(FILE *yuv_fp, FILE *avc_fp, int f, int nbframes)
1091 encode_picture(yuv_fp, avc_fp,
1092 enc_frame_number, f + nbframes,
1094 SLICE_TYPE_P, 1, f);
1096 for( i = 0; i < nbframes - 1; i++) {
1097 encode_picture(yuv_fp, avc_fp,
1098 enc_frame_number + 1, f + i,
1100 SLICE_TYPE_B, 1, f + i + 1);
1103 encode_picture(yuv_fp, avc_fp,
1104 enc_frame_number + 1, f + nbframes - 1,
1106 SLICE_TYPE_B, 0, f + nbframes + 1);
1109 static void show_help()
1111 printf("Usage: avnenc <width> <height> <input_yuvfile> <output_avcfile> [qp=qpvalue|fb=framebitrate] [mode=0(I frames only)/1(I and P frames)/2(I, P and B frames)\n");
1114 static void avcenc_context_seq_param_init(VAEncSequenceParameterBufferH264Ext *seq_param,
1115 int width, int height)
1118 int width_in_mbs = (width + 15) / 16;
1119 int height_in_mbs = (height + 15) / 16;
1120 int frame_cropping_flag = 0;
1121 int frame_crop_bottom_offset = 0;
1123 seq_param->seq_parameter_set_id = 0;
1124 seq_param->profile_idc = PROFILE_IDC_MAIN;
1125 seq_param->level_idc = 41;
1126 seq_param->intra_period = intra_period;
1127 seq_param->ip_period = 0; /* FIXME: ??? */
1128 seq_param->max_num_ref_frames = 4;
1129 seq_param->picture_width_in_mbs = width_in_mbs;
1130 seq_param->picture_height_in_mbs = height_in_mbs;
1131 seq_param->frame_mbs_only_flag = 1;
1132 seq_param->target_usage = 1;
1134 /* 0:CBR, 1:VBR, 2:Constant QP */
1136 seq_param->rate_control_method = BR_CBR;
1137 else if (qp_value == -2)
1138 seq_param->rate_control_method = BR_VBR;
1140 assert(qp_value >= 0 && qp_value <= 51);
1141 seq_param->rate_control_method = BR_CQP;
1144 if (frame_bit_rate > 0)
1145 seq_param->bits_per_second = 30 * frame_bit_rate;
1147 seq_param->bits_per_second = 0;
1149 if (seq_param->rate_control_method == BR_VBR) {
1150 seq_param->max_bits_per_second = 0; /* FIXME: set it later */
1151 seq_param->min_bits_per_second = 0;
1154 seq_param->initial_hrd_buffer_fullness = 0; /* FIXME: ??? */
1155 seq_param->hrd_buffer_size = 0; /* FIXME: ??? */
1156 seq_param->time_scale = 900;
1157 seq_param->num_units_in_tick = 15;
1159 if (height_in_mbs * 16 - height) {
1160 frame_cropping_flag = 1;
1161 frame_crop_bottom_offset =
1162 (height_in_mbs * 16 - height) / (2 * (!seq_param->frame_mbs_only_flag + 1));
1165 seq_param->frame_cropping_flag = frame_cropping_flag;
1166 seq_param->frame_crop_left_offset = 0;
1167 seq_param->frame_crop_right_offset = 0;
1168 seq_param->frame_crop_top_offset = 0;
1169 seq_param->frame_crop_bottom_offset = frame_crop_bottom_offset;
1171 seq_param->pic_order_cnt_type = 0;
1172 seq_param->direct_8x8_inference_flag = 0;
1174 seq_param->log2_max_frame_num_minus4 = 0;
1175 seq_param->log2_max_pic_order_cnt_lsb_minus4 = 2;
1177 seq_param->vui_flag = 0;
1180 static void avcenc_context_pic_param_init(VAEncPictureParameterBufferH264Ext *pic_param)
1182 pic_param->seq_parameter_set_id = 0;
1183 pic_param->pic_parameter_set_id = 0;
1185 pic_param->last_picture = 0;
1186 pic_param->frame_num = 0;
1187 pic_param->coding_type = 0;
1189 pic_param->pic_init_qp = (qp_value >= 0 ? qp_value : 26);
1190 pic_param->num_ref_idx_l0_active_minus1 = 0;
1191 pic_param->num_ref_idx_l1_active_minus1 = 0;
1193 pic_param->pic_fields.bits.idr_pic_flag = 0;
1194 pic_param->pic_fields.bits.reference_pic_flag = 0;
1195 pic_param->pic_fields.bits.entropy_coding_mode_flag = ENTROPY_MODE_CABAC;
1196 pic_param->pic_fields.bits.weighted_pred_flag = 0;
1197 pic_param->pic_fields.bits.weighted_bipred_idc = 0;
1198 pic_param->pic_fields.bits.transform_8x8_mode_flag = 0;
1199 pic_param->pic_fields.bits.deblocking_filter_control_present_flag = 1;
1202 static void avcenc_context_init(int width, int height)
1205 memset(&avcenc_context, 0, sizeof(avcenc_context));
1206 avcenc_context.seq_param_buf_id = VA_INVALID_ID;
1207 avcenc_context.pic_param_buf_id = VA_INVALID_ID;
1208 avcenc_context.dec_ref_pic_marking_buf_id = VA_INVALID_ID;
1209 avcenc_context.packed_seq_buf_id = VA_INVALID_ID;
1210 avcenc_context.packed_pic_buf_id = VA_INVALID_ID;
1211 avcenc_context.codedbuf_buf_id = VA_INVALID_ID;
1212 avcenc_context.codedbuf_i_size = width * height;
1213 avcenc_context.codedbuf_pb_size = 0;
1214 avcenc_context.current_input_surface = SID_INPUT_PICTURE_0;
1215 avcenc_context.upload_thread_value = -1;
1217 for (i = 0; i < MAX_SLICES; i++) {
1218 avcenc_context.slice_param_buf_id[i] = VA_INVALID_ID;
1221 avcenc_context_seq_param_init(&avcenc_context.seq_param, width, height);
1222 avcenc_context_pic_param_init(&avcenc_context.pic_param);
1225 int main(int argc, char *argv[])
1231 int i_frame_only=0,i_p_frame_only=1;
1233 struct timeval tpstart,tpend;
1236 va_init_display_args(&argc, argv);
1238 //TODO may be we should using option analytics library
1239 if(argc != 5 && argc != 6 && argc != 7) {
1244 picture_width = atoi(argv[1]);
1245 picture_height = atoi(argv[2]);
1246 picture_width_in_mbs = (picture_width + 15) / 16;
1247 picture_height_in_mbs = (picture_height + 15) / 16;
1249 if (argc == 6 || argc == 7) {
1251 sscanf(argv[5], "qp=%d", &qp_value);
1252 if ( qp_value == -1 ) {
1253 frame_bit_rate = -1;
1254 sscanf(argv[5], "fb=%d", &frame_bit_rate);
1255 if ( frame_bit_rate == -1 ) {
1259 } else if (qp_value > 51) {
1261 } else if (qp_value < 0) {
1265 qp_value = 28; //default const QP mode
1268 sscanf(argv[6], "mode=%d", &mode_value);
1269 if ( mode_value == 0 ) {
1273 else if ( mode_value == 1) {
1277 else if ( mode_value == 2 ) {
1282 printf("mode_value=%d\n",mode_value);
1288 yuv_fp = fopen(argv[3],"rb");
1289 if ( yuv_fp == NULL){
1290 printf("Can't open input YUV file\n");
1293 fseek(yuv_fp,0l, SEEK_END);
1294 file_size = ftell(yuv_fp);
1295 frame_size = picture_width * picture_height + ((picture_width * picture_height) >> 1) ;
1297 if ( (file_size < frame_size) || (file_size % frame_size) ) {
1299 printf("The YUV file's size is not correct\n");
1302 frame_number = file_size / frame_size;
1303 fseek(yuv_fp, 0l, SEEK_SET);
1305 avc_fp = fopen(argv[4], "wb");
1306 if ( avc_fp == NULL) {
1308 printf("Can't open output avc file\n");
1311 gettimeofday(&tpstart,NULL);
1312 avcenc_context_init(picture_width, picture_height);
1313 create_encode_pipe();
1314 alloc_encode_resource(yuv_fp);
1316 enc_frame_number = 0;
1317 for ( f = 0; f < frame_number; ) { //picture level loop
1318 int is_intra = i_frame_only?1:(enc_frame_number % intra_period == 0);
1319 int is_idr = (f == 0);
1322 if ( ! is_intra && pb_period > 0) {
1323 is_bslice = i_p_frame_only?0:(f % pb_period == 1) && (f < frame_number - 1);
1327 encode_i_picture(yuv_fp, avc_fp, f, is_idr);
1330 } else if ( is_bslice) {
1331 encode_pb_pictures(yuv_fp, avc_fp, f, 2); //last parameter is continue B frames number
1335 encode_p_picture(yuv_fp, avc_fp, f);
1340 printf("\r %d/%d ...", f+1, frame_number);
1344 gettimeofday(&tpend,NULL);
1345 timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+ tpend.tv_usec-tpstart.tv_usec;
1347 printf("\ndone!\n");
1348 printf("encode %d frames in %f secondes, FPS is %.1f\n",frame_number, timeuse, frame_number/timeuse);
1349 release_encode_resource();
1350 destory_encode_pipe();