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 MPEG-2 encoder based on libVA.
35 #include <sys/types.h>
41 #include <va/va_enc_mpeg2.h>
43 #include "va_display.h"
45 #define START_CODE_PICUTRE 0x00000100
46 #define START_CODE_SLICE 0x00000101
47 #define START_CODE_USER 0x000001B2
48 #define START_CODE_SEQ 0x000001B3
49 #define START_CODE_EXT 0x000001B5
50 #define START_CODE_GOP 0x000001B8
52 #define CHROMA_FORMAT_RESERVED 0
53 #define CHROMA_FORMAT_420 1
54 #define CHROMA_FORMAT_422 2
55 #define CHROMA_FORMAT_444 3
57 #define MAX_SLICES 128
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 int const picture_type_pattern[][2] = {{VAEncPictureTypeIntra, 1},
78 {VAEncPictureTypePredictive, 3}, {VAEncPictureTypePredictive, 3},{VAEncPictureTypePredictive, 3},
79 {VAEncPictureTypePredictive, 3}, {VAEncPictureTypePredictive, 3},{VAEncPictureTypePredictive, 3},
80 {VAEncPictureTypePredictive, 3}, {VAEncPictureTypePredictive, 3},{VAEncPictureTypePredictive, 3},
81 {VAEncPictureTypePredictive, 2}};
83 static VAProfile mpeg2_va_profiles[] = {
88 struct mpeg2enc_context {
90 int rate_control_mode;
92 int mode; /* 0:I, 1:I/P, 2:I/P/B */
102 unsigned char *frame_data_buffer;
104 int bit_rate; /* in kbps */
108 VAEncSequenceParameterBufferMPEG2 seq_param;
109 VAEncPictureParameterBufferMPEG2 pic_param;
110 VAEncSliceParameterBufferMPEG2 slice_param[MAX_SLICES];
111 VAContextID context_id;
112 VAConfigID config_id;
113 VABufferID seq_param_buf_id; /* Sequence level parameter */
114 VABufferID pic_param_buf_id; /* Picture level parameter */
115 VABufferID slice_param_buf_id[MAX_SLICES]; /* Slice level parameter, multil slices */
116 VABufferID codedbuf_buf_id; /* Output buffer, compressed data */
117 VABufferID packed_seq_header_param_buf_id;
118 VABufferID packed_seq_buf_id;
119 VABufferID packed_pic_header_param_buf_id;
120 VABufferID packed_pic_buf_id;
121 int num_slice_groups;
123 int codedbuf_pb_size;
126 pthread_t upload_thread_id;
127 int upload_thread_value;
128 int current_input_surface;
129 int current_upload_surface;
135 #define BITSTREAM_ALLOCATE_STEPPING 4096
138 unsigned int *buffer;
140 int max_size_in_dword;
143 typedef struct __bitstream bitstream;
146 swap32(unsigned int val)
148 unsigned char *pval = (unsigned char *)&val;
150 return ((pval[0] << 24) |
157 bitstream_start(bitstream *bs)
159 bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
160 bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
165 bitstream_end(bitstream *bs)
167 int pos = (bs->bit_offset >> 5);
168 int bit_offset = (bs->bit_offset & 0x1f);
169 int bit_left = 32 - bit_offset;
172 bs->buffer[pos] = swap32((bs->buffer[pos] << bit_left));
177 bitstream_put_ui(bitstream *bs, unsigned int val, int size_in_bits)
179 int pos = (bs->bit_offset >> 5);
180 int bit_offset = (bs->bit_offset & 0x1f);
181 int bit_left = 32 - bit_offset;
186 if (size_in_bits < 32)
187 val &= ((1 << size_in_bits) - 1);
189 bs->bit_offset += size_in_bits;
191 if (bit_left > size_in_bits) {
192 bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
194 size_in_bits -= bit_left;
195 bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
196 bs->buffer[pos] = swap32(bs->buffer[pos]);
198 if (pos + 1 == bs->max_size_in_dword) {
199 bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
200 bs->buffer = realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
203 bs->buffer[pos + 1] = val;
208 bitstream_byte_aligning(bitstream *bs, int bit)
210 int bit_offset = (bs->bit_offset & 0x7);
211 int bit_left = 8 - bit_offset;
217 assert(bit == 0 || bit == 1);
220 new_val = (1 << bit_left) - 1;
224 bitstream_put_ui(bs, new_val, bit_left);
227 static struct mpeg2_frame_rate {
230 } frame_rate_tab[] = {
242 find_frame_rate_code(const VAEncSequenceParameterBufferMPEG2 *seq_param)
244 unsigned int delta = -1;
246 float frame_rate_value = seq_param->frame_rate *
247 (seq_param->sequence_extension.bits.frame_rate_extension_d + 1) /
248 (seq_param->sequence_extension.bits.frame_rate_extension_n + 1);
250 for (i = 0; i < sizeof(frame_rate_tab) / sizeof(frame_rate_tab[0]); i++) {
252 if (abs(1000 * frame_rate_tab[i].value - 1000 * frame_rate_value) < delta) {
253 code = frame_rate_tab[i].code;
254 delta = abs(1000 * frame_rate_tab[i].value - 1000 * frame_rate_value);
262 sps_rbsp(const VAEncSequenceParameterBufferMPEG2 *seq_param,
265 int frame_rate_code = find_frame_rate_code(seq_param);
267 bitstream_put_ui(bs, START_CODE_SEQ, 32);
268 bitstream_put_ui(bs, seq_param->picture_width, 12);
269 bitstream_put_ui(bs, seq_param->picture_height, 12);
270 bitstream_put_ui(bs, seq_param->aspect_ratio_information, 4);
271 bitstream_put_ui(bs, frame_rate_code, 4); /* frame_rate_code */
272 bitstream_put_ui(bs, (seq_param->bits_per_second + 399) / 400, 18); /* the low 18 bits of bit_rate */
273 bitstream_put_ui(bs, 1, 1); /* marker_bit */
274 bitstream_put_ui(bs, seq_param->vbv_buffer_size, 10);
275 bitstream_put_ui(bs, 0, 1); /* constraint_parameter_flag, always 0 for MPEG-2 */
276 bitstream_put_ui(bs, 0, 1); /* load_intra_quantiser_matrix */
277 bitstream_put_ui(bs, 0, 1); /* load_non_intra_quantiser_matrix */
279 bitstream_byte_aligning(bs, 0);
281 bitstream_put_ui(bs, START_CODE_EXT, 32);
282 bitstream_put_ui(bs, 1, 4); /* sequence_extension id */
283 bitstream_put_ui(bs, seq_param->sequence_extension.bits.profile_and_level_indication, 8);
284 bitstream_put_ui(bs, seq_param->sequence_extension.bits.progressive_sequence, 1);
285 bitstream_put_ui(bs, seq_param->sequence_extension.bits.chroma_format, 2);
286 bitstream_put_ui(bs, seq_param->picture_width >> 12, 2);
287 bitstream_put_ui(bs, seq_param->picture_height >> 12, 2);
288 bitstream_put_ui(bs, ((seq_param->bits_per_second + 399) / 400) >> 18, 12); /* bit_rate_extension */
289 bitstream_put_ui(bs, 1, 1); /* marker_bit */
290 bitstream_put_ui(bs, seq_param->vbv_buffer_size >> 10, 8);
291 bitstream_put_ui(bs, seq_param->sequence_extension.bits.low_delay, 1);
292 bitstream_put_ui(bs, seq_param->sequence_extension.bits.frame_rate_extension_n, 2);
293 bitstream_put_ui(bs, seq_param->sequence_extension.bits.frame_rate_extension_d, 5);
295 bitstream_byte_aligning(bs, 0);
299 pps_rbsp(const VAEncSequenceParameterBufferMPEG2 *seq_param,
300 const VAEncPictureParameterBufferMPEG2 *pic_param,
306 if (pic_param->temporal_reference == 0) {
307 bitstream_put_ui(bs, START_CODE_GOP, 32);
308 bitstream_put_ui(bs, seq_param->gop_header.bits.time_code, 25);
309 bitstream_put_ui(bs, seq_param->gop_header.bits.closed_gop, 1);
310 bitstream_put_ui(bs, seq_param->gop_header.bits.broken_link, 1);
312 bitstream_byte_aligning(bs, 0);
315 if (seq_param->sequence_extension.bits.chroma_format == CHROMA_FORMAT_420)
316 chroma_420_type = pic_param->picture_coding_extension.bits.progressive_frame;
320 bitstream_put_ui(bs, START_CODE_PICUTRE, 32);
321 bitstream_put_ui(bs, pic_param->temporal_reference, 10);
323 pic_param->picture_type == VAEncPictureTypeIntra ? 1 :
324 pic_param->picture_type == VAEncPictureTypePredictive ? 2 : 3,
326 bitstream_put_ui(bs, 0xFFFF, 16); /* vbv_delay, always 0xFFFF */
328 if (pic_param->picture_type == VAEncPictureTypePredictive) {
329 bitstream_put_ui(bs, 0, 1); /* full_pel_forward_vector, always 0 for MPEG-2 */
330 bitstream_put_ui(bs, 7, 3); /* forward_f_code, always 7 for MPEG-2 */
333 if (pic_param->picture_type == VAEncPictureTypeBidirectional) {
334 bitstream_put_ui(bs, 0, 1); /* full_pel_backward_vector, always 0 for MPEG-2 */
335 bitstream_put_ui(bs, 7, 3); /* backward_f_code, always 7 for MPEG-2 */
338 bitstream_put_ui(bs, 0, 1); /* extra_bit_picture, 0 */
340 bitstream_byte_aligning(bs, 0);
342 bitstream_put_ui(bs, START_CODE_EXT, 32);
343 bitstream_put_ui(bs, 8, 4); /* Picture Coding Extension ID: 8 */
344 bitstream_put_ui(bs, pic_param->f_code[0][0], 4);
345 bitstream_put_ui(bs, pic_param->f_code[0][1], 4);
346 bitstream_put_ui(bs, pic_param->f_code[1][0], 4);
347 bitstream_put_ui(bs, pic_param->f_code[1][1], 4);
349 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.intra_dc_precision, 2);
350 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.picture_structure, 2);
351 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.top_field_first, 1);
352 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.frame_pred_frame_dct, 1);
353 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.concealment_motion_vectors, 1);
354 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.q_scale_type, 1);
355 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.intra_vlc_format, 1);
356 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.alternate_scan, 1);
357 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.repeat_first_field, 1);
358 bitstream_put_ui(bs, chroma_420_type, 1);
359 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.progressive_frame, 1);
360 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.composite_display_flag, 1);
362 bitstream_byte_aligning(bs, 0);
364 if (pic_param->user_data_length) {
365 bitstream_put_ui(bs, START_CODE_USER, 32);
367 for (i = 0; i < pic_param->user_data_length; i++) {
368 bitstream_put_ui(bs, pic_param->user_data[i], 8);
371 bitstream_byte_aligning(bs, 0);
376 build_packed_pic_buffer(const VAEncSequenceParameterBufferMPEG2 *seq_param,
377 const VAEncPictureParameterBufferMPEG2 *pic_param,
378 unsigned char **header_buffer)
382 bitstream_start(&bs);
383 pps_rbsp(seq_param, pic_param, &bs);
386 *header_buffer = (unsigned char *)bs.buffer;
387 return bs.bit_offset;
391 build_packed_seq_buffer(const VAEncSequenceParameterBufferMPEG2 *seq_param,
392 unsigned char **header_buffer)
396 bitstream_start(&bs);
397 sps_rbsp(seq_param, &bs);
400 *header_buffer = (unsigned char *)bs.buffer;
401 return bs.bit_offset;
407 #define SID_INPUT_PICTURE_0 0
408 #define SID_INPUT_PICTURE_1 1
409 #define SID_REFERENCE_PICTURE_L0 2
410 #define SID_REFERENCE_PICTURE_L1 3
411 #define SID_RECON_PICTURE 4
412 #define SID_NUMBER SID_RECON_PICTURE + 1
414 static VASurfaceID surface_ids[SID_NUMBER];
417 * upload thread function
420 upload_yuv_to_surface(void *data)
422 struct mpeg2enc_context *ctx = data;
423 VAImage surface_image;
425 void *surface_p = NULL;
426 unsigned char *y_src, *u_src, *v_src;
427 unsigned char *y_dst, *u_dst, *v_dst;
428 int y_size = ctx->width * ctx->height;
429 int u_size = (ctx->width >> 1) * (ctx->height >> 1);
434 n_items = fread(ctx->frame_data_buffer, ctx->frame_size, 1, ctx->ifp);
435 } while (n_items != 1);
437 va_status = vaDeriveImage(ctx->va_dpy, surface_ids[ctx->current_upload_surface], &surface_image);
438 CHECK_VASTATUS(va_status,"vaDeriveImage");
440 vaMapBuffer(ctx->va_dpy, surface_image.buf, &surface_p);
441 assert(VA_STATUS_SUCCESS == va_status);
443 y_src = ctx->frame_data_buffer;
444 u_src = ctx->frame_data_buffer + y_size; /* UV offset for NV12 */
445 v_src = ctx->frame_data_buffer + y_size + u_size;
447 y_dst = surface_p + surface_image.offsets[0];
448 u_dst = surface_p + surface_image.offsets[1]; /* UV offset for NV12 */
449 v_dst = surface_p + surface_image.offsets[2];
452 for (row = 0; row < surface_image.height; row++) {
453 memcpy(y_dst, y_src, surface_image.width);
454 y_dst += surface_image.pitches[0];
458 if (surface_image.format.fourcc == VA_FOURCC_NV12) { /* UV plane */
459 for (row = 0; row < surface_image.height / 2; row++) {
460 for (col = 0; col < surface_image.width / 2; col++) {
461 u_dst[col * 2] = u_src[col];
462 u_dst[col * 2 + 1] = v_src[col];
465 u_dst += surface_image.pitches[1];
466 u_src += (ctx->width / 2);
467 v_src += (ctx->width / 2);
470 for (row = 0; row < surface_image.height / 2; row++) {
471 for (col = 0; col < surface_image.width / 2; col++) {
472 u_dst[col] = u_src[col];
473 v_dst[col] = v_src[col];
476 u_dst += surface_image.pitches[1];
477 v_dst += surface_image.pitches[2];
478 u_src += (ctx->width / 2);
479 v_src += (ctx->width / 2);
483 vaUnmapBuffer(ctx->va_dpy, surface_image.buf);
484 vaDestroyImage(ctx->va_dpy, surface_image.image_id);
490 mpeg2enc_exit(struct mpeg2enc_context *ctx, int exit_code)
492 if (ctx->frame_data_buffer) {
493 free(ctx->frame_data_buffer);
494 ctx->frame_data_buffer = NULL;
513 fprintf(stderr, "Usage: %s --help\n", program);
514 fprintf(stderr, "\t--help print this message\n");
515 fprintf(stderr, "Usage: %s <width> <height> <ifile> <ofile> [options]\n", program);
516 fprintf(stderr, "\t<width> specifies the frame width\n");
517 fprintf(stderr, "\t<height> specifies the frame height\n");
518 fprintf(stderr, "\t<ifile> specifies the I420/IYUV YUV file\n");
519 fprintf(stderr, "\t<ofile> specifies the encoded MPEG-2 file\n");
520 fprintf(stderr, "where options include:\n");
521 fprintf(stderr, "\t--cqp <QP> const qp mode with specified <QP>\n");
522 fprintf(stderr, "\t--fps <FPS> specify the frame rate\n");
523 fprintf(stderr, "\t--mode <MODE> specify the mode 0 (I), 1 (I/P) and 2 (I/P/B)\n");
524 fprintf(stderr, "\t--profile <PROFILE> specify the profile 0(Simple), or 1(Main, default)\n");
525 fprintf(stderr, "\t--level <LEVEL> specify the level 0(Low), 1(Main, default) or 2(High)\n");
529 parse_args(struct mpeg2enc_context *ctx, int argc, char **argv)
532 int option_index = 0;
534 static struct option long_options[] = {
535 {"help", no_argument, 0, 'h'},
536 {"cqp", required_argument, 0, 'c'},
537 {"fps", required_argument, 0, 'f'},
538 {"mode", required_argument, 0, 'm'},
539 {"profile", required_argument, 0, 'p'},
540 {"level", required_argument, 0, 'l'},
544 if ((argc == 2 && strcmp(argv[1], "--help") == 0) ||
548 ctx->width = atoi(argv[1]);
549 ctx->height = atoi(argv[2]);
551 if (ctx->width <= 0 || ctx->height <= 0) {
552 fprintf(stderr, "<width> and <height> must be greater than 0\n");
556 ctx->ifp = fopen(argv[3], "rb");
558 if (ctx->ifp == NULL) {
559 fprintf(stderr, "Can't open the input file\n");
563 fseek(ctx->ifp, 0l, SEEK_END);
564 file_size = ftell(ctx->ifp);
565 ctx->frame_size = ctx->width * ctx->height * 3 / 2;
567 if ((file_size < ctx->frame_size) ||
568 (file_size % ctx->frame_size)) {
569 fprintf(stderr, "The input file size %ld isn't a multiple of the frame size %d\n", file_size, ctx->frame_size);
573 ctx->num_pictures = file_size / ctx->frame_size;
574 fseek(ctx->ifp, 0l, SEEK_SET);
576 ctx->ofp = fopen(argv[4], "wb");
578 if (ctx->ofp == NULL) {
579 fprintf(stderr, "Can't create the output file\n");
586 ctx->rate_control_mode = VA_RC_CQP;
587 ctx->mode = MPEG2_MODE_IP;
588 ctx->profile = VAProfileMPEG2Main;
589 ctx->level = MPEG2_LEVEL_MAIN;
593 while((c = getopt_long(argc, argv,
596 &option_index)) != -1) {
601 /* only support q_scale_type = 0 */
602 if (tmp > 62 || tmp < 2) {
603 fprintf(stderr, "Warning: QP must be in [2, 62]\n");
612 ctx->qp = tmp & 0xFE;
613 ctx->rate_control_mode = VA_RC_CQP;
621 fprintf(stderr, "Warning: FPS must be greater than 0\n");
625 ctx->rate_control_mode = VA_RC_CBR;
632 if (tmp < MPEG2_MODE_I || tmp > MPEG2_MODE_IPB)
633 fprintf(stderr, "Waning: MODE must be 0, 1, or 2\n");
642 if (tmp < 0 || tmp > 1)
643 fprintf(stderr, "Waning: PROFILE must be 0 or 1\n");
645 ctx->profile = mpeg2_va_profiles[tmp];
652 if (tmp < MPEG2_LEVEL_LOW || tmp > MPEG2_LEVEL_HIGH)
653 fprintf(stderr, "Waning: LEVEL must be 0, 1, or 2\n");
660 fprintf(stderr, "Error: unkown command options\n");
672 mpeg2enc_exit(ctx, 1);
679 mpeg2enc_init_sequence_parameter(struct mpeg2enc_context *ctx,
680 VAEncSequenceParameterBufferMPEG2 *seq_param)
682 seq_param->intra_period = ctx->intra_period;
683 seq_param->ip_period = 0; /* FIXME: ??? */
684 seq_param->picture_width = ctx->width;
685 seq_param->picture_height = ctx->height;
687 if (ctx->bit_rate > 0)
688 seq_param->bits_per_second = 1024 * ctx->bit_rate; /* use kbps as input */
690 seq_param->bits_per_second = 0x3FFFF * 400;
692 seq_param->frame_rate = ctx->fps;
693 seq_param->aspect_ratio_information = 1;
694 seq_param->vbv_buffer_size = 3; /* B = 16 * 1024 * vbv_buffer_size */
696 seq_param->sequence_extension.bits.profile_and_level_indication = 4 << 4 | 0x8; /* FIXME: Main/Main ??? */
697 seq_param->sequence_extension.bits.progressive_sequence = 1; /* progressive frame-pictures */
698 seq_param->sequence_extension.bits.chroma_format = CHROMA_FORMAT_420; /* 4:2:0 */
699 seq_param->sequence_extension.bits.low_delay = 0; /* FIXME */
700 seq_param->sequence_extension.bits.frame_rate_extension_n = 0;
701 seq_param->sequence_extension.bits.frame_rate_extension_d = 0;
703 seq_param->gop_header.bits.time_code = (1 << 12); /* bit12: marker_bit */
704 seq_param->gop_header.bits.closed_gop = 0;
705 seq_param->gop_header.bits.broken_link = 0;
709 mpeg2enc_init_picture_parameter(struct mpeg2enc_context *ctx,
710 VAEncPictureParameterBufferMPEG2 *pic_param)
712 pic_param->forward_reference_picture = VA_INVALID_ID;
713 pic_param->backward_reference_picture = VA_INVALID_ID;
714 pic_param->reconstructed_picture = VA_INVALID_ID;
715 pic_param->coded_buf = VA_INVALID_ID;
716 pic_param->picture_type = VAEncPictureTypeIntra;
718 pic_param->temporal_reference = 0;
719 pic_param->f_code[0][0] = 0xf;
720 pic_param->f_code[0][1] = 0xf;
721 pic_param->f_code[1][0] = 0xf;
722 pic_param->f_code[1][1] = 0xf;
724 pic_param->picture_coding_extension.bits.intra_dc_precision = 0; /* 8bits */
725 pic_param->picture_coding_extension.bits.picture_structure = 3; /* frame picture */
726 pic_param->picture_coding_extension.bits.top_field_first = 0;
727 pic_param->picture_coding_extension.bits.frame_pred_frame_dct = 1; /* FIXME */
728 pic_param->picture_coding_extension.bits.concealment_motion_vectors = 0;
729 pic_param->picture_coding_extension.bits.q_scale_type = 0;
730 pic_param->picture_coding_extension.bits.intra_vlc_format = 0;
731 pic_param->picture_coding_extension.bits.alternate_scan = 0;
732 pic_param->picture_coding_extension.bits.repeat_first_field = 0;
733 pic_param->picture_coding_extension.bits.progressive_frame = 1;
734 pic_param->picture_coding_extension.bits.composite_display_flag = 0;
736 pic_param->user_data_length = 0;
740 mpeg2enc_alloc_va_resources(struct mpeg2enc_context *ctx)
742 VAEntrypoint *entrypoint_list;
743 VAConfigAttrib attrib_list[2];
745 int max_entrypoints, num_entrypoints, entrypoint;
746 int major_ver, minor_ver;
748 ctx->va_dpy = va_open_display();
749 va_status = vaInitialize(ctx->va_dpy,
752 CHECK_VASTATUS(va_status, "vaInitialize");
754 max_entrypoints = vaMaxNumEntrypoints(ctx->va_dpy);
755 entrypoint_list = malloc(max_entrypoints * sizeof(VAEntrypoint));
756 vaQueryConfigEntrypoints(ctx->va_dpy,
757 VAProfileMPEG2Simple,
761 for (entrypoint = 0; entrypoint < num_entrypoints; entrypoint++) {
762 if (entrypoint_list[entrypoint] == VAEntrypointEncSlice)
766 free(entrypoint_list);
768 if (entrypoint == num_entrypoints) {
769 /* not find Slice entry point */
773 /* find out the format for the render target, and rate control mode */
774 attrib_list[0].type = VAConfigAttribRTFormat;
775 attrib_list[1].type = VAConfigAttribRateControl;
776 vaGetConfigAttributes(ctx->va_dpy,
777 VAProfileMPEG2Simple,
778 VAEntrypointEncSlice,
782 if ((attrib_list[0].value & VA_RT_FORMAT_YUV420) == 0) {
783 /* not find desired YUV420 RT format */
787 if ((attrib_list[1].value & ctx->rate_control_mode) == 0) {
788 /* Can't find matched RC mode */
789 fprintf(stderr, "RC mode %d isn't found, exit\n", ctx->rate_control_mode);
793 attrib_list[0].value = VA_RT_FORMAT_YUV420; /* set to desired RT format */
794 attrib_list[1].value = ctx->rate_control_mode; /* set to desired RC mode */
796 va_status = vaCreateConfig(ctx->va_dpy,
797 VAProfileMPEG2Simple,
798 VAEntrypointEncSlice,
802 CHECK_VASTATUS(va_status, "vaCreateConfig");
804 /* Create a context for this decode pipe */
805 va_status = vaCreateContext(ctx->va_dpy,
813 CHECK_VASTATUS(va_status, "vaCreateContext");
815 va_status = vaCreateSurfaces(ctx->va_dpy,
823 CHECK_VASTATUS(va_status, "vaCreateSurfaces");
827 mpeg2enc_init(struct mpeg2enc_context *ctx)
831 ctx->frame_data_buffer = (unsigned char *)malloc(ctx->frame_size);
832 ctx->seq_param_buf_id = VA_INVALID_ID;
833 ctx->pic_param_buf_id = VA_INVALID_ID;
834 ctx->packed_seq_header_param_buf_id = VA_INVALID_ID;
835 ctx->packed_seq_buf_id = VA_INVALID_ID;
836 ctx->packed_pic_header_param_buf_id = VA_INVALID_ID;
837 ctx->packed_pic_buf_id = VA_INVALID_ID;
838 ctx->codedbuf_buf_id = VA_INVALID_ID;
839 ctx->codedbuf_i_size = ctx->frame_size;
840 ctx->codedbuf_pb_size = 0;
842 if (ctx->mode == MPEG2_MODE_I)
843 ctx->intra_period = 1;
845 ctx->intra_period = 16;
849 for (i = 0; i < MAX_SLICES; i++) {
850 ctx->slice_param_buf_id[i] = VA_INVALID_ID;
853 mpeg2enc_init_sequence_parameter(ctx, &ctx->seq_param);
854 mpeg2enc_init_picture_parameter(ctx, &ctx->pic_param);
855 mpeg2enc_alloc_va_resources(ctx);
858 ctx->current_input_surface = SID_INPUT_PICTURE_0;
859 ctx->current_upload_surface = SID_INPUT_PICTURE_1;
860 ctx->upload_thread_value = pthread_create(&ctx->upload_thread_id,
862 upload_yuv_to_surface,
867 mpeg2enc_time_code(VAEncSequenceParameterBufferMPEG2 *seq_param,
870 int fps = (int)(seq_param->frame_rate + 0.5);
872 int time_code_pictures, time_code_seconds, time_code_minutes, time_code_hours;
873 int drop_frame_flag = 0;
877 time_code_seconds = num_frames / fps;
878 time_code_pictures = num_frames % fps;
879 time_code |= time_code_pictures;
881 time_code_minutes = time_code_minutes / 60;
882 time_code_seconds = time_code_minutes % 60;
883 time_code |= (time_code_seconds << 6);
885 time_code_hours = time_code_minutes / 60;
886 time_code_minutes = time_code_minutes % 60;
888 time_code |= (1 << 12); /* marker_bit */
889 time_code |= (time_code_minutes << 13);
891 time_code_hours = time_code_hours % 24;
892 time_code |= (time_code_hours << 19);
894 time_code |= (drop_frame_flag << 24);
903 mpeg2enc_update_sequence_parameter(struct mpeg2enc_context *ctx,
904 VAEncPictureType picture_type,
908 VAEncSequenceParameterBufferMPEG2 *seq_param = &ctx->seq_param;
910 /* update the GOP info for the new GOP */
911 if (display_order % ctx->intra_period == 0) {
912 seq_param->gop_header.bits.time_code = mpeg2enc_time_code(seq_param, display_order);
917 mpeg2enc_update_picture_parameter(struct mpeg2enc_context *ctx,
918 VAEncPictureType picture_type,
922 VAEncPictureParameterBufferMPEG2 *pic_param = &ctx->pic_param;
924 pic_param->picture_type = picture_type;
925 pic_param->temporal_reference = display_order % ctx->intra_period;
926 pic_param->reconstructed_picture = surface_ids[SID_RECON_PICTURE];
927 pic_param->forward_reference_picture = surface_ids[SID_REFERENCE_PICTURE_L0];
928 pic_param->backward_reference_picture = surface_ids[SID_REFERENCE_PICTURE_L1];
932 mpeg2enc_update_picture_parameter_buffer(struct mpeg2enc_context *ctx,
933 VAEncPictureType picture_type,
937 VAEncPictureParameterBufferMPEG2 *pic_param = &ctx->pic_param;
940 /* update the coded buffer id */
941 pic_param->coded_buf = ctx->codedbuf_buf_id;
942 va_status = vaCreateBuffer(ctx->va_dpy,
944 VAEncPictureParameterBufferType,
948 &ctx->pic_param_buf_id);
949 CHECK_VASTATUS(va_status, "vaCreateBuffer");
953 mpeg2enc_update_slice_parameter(struct mpeg2enc_context *ctx, VAEncPictureType picture_type)
955 VAEncSequenceParameterBufferMPEG2 *seq_param;
956 VAEncPictureParameterBufferMPEG2 *pic_param;
957 VAEncSliceParameterBufferMPEG2 *slice_param;
959 int i, width_in_mbs, height_in_mbs;
961 pic_param = &ctx->pic_param;
962 assert(pic_param->picture_coding_extension.bits.q_scale_type == 0);
964 seq_param = &ctx->seq_param;
965 width_in_mbs = (seq_param->picture_width + 15) / 16;
966 height_in_mbs = (seq_param->picture_height + 15) / 16;
967 ctx->num_slice_groups = 1;
969 for (i = 0; i < height_in_mbs; i++) {
970 slice_param = &ctx->slice_param[i];
971 slice_param->macroblock_address = i * width_in_mbs;
972 slice_param->num_macroblocks = width_in_mbs;
973 slice_param->is_intra_slice = (picture_type == VAEncPictureTypeIntra);
974 slice_param->quantiser_scale_code = ctx->qp / 2;
977 va_status = vaCreateBuffer(ctx->va_dpy,
979 VAEncSliceParameterBufferType,
980 sizeof(*slice_param),
983 ctx->slice_param_buf_id);
984 CHECK_VASTATUS(va_status, "vaCreateBuffer");;
988 begin_picture(struct mpeg2enc_context *ctx,
991 VAEncPictureType picture_type)
995 VAEncPackedHeaderParameterBuffer packed_header_param_buffer;
996 unsigned int length_in_bits;
997 unsigned char *packed_seq_buffer = NULL, *packed_pic_buffer = NULL;
999 if (ctx->upload_thread_value != 0) {
1000 fprintf(stderr, "FATAL error!!!\n");
1004 pthread_join(ctx->upload_thread_id, NULL);
1006 ctx->upload_thread_value = -1;
1007 tmp = ctx->current_input_surface;
1008 ctx->current_input_surface = ctx->current_upload_surface;
1009 ctx->current_upload_surface = tmp;
1011 mpeg2enc_update_sequence_parameter(ctx, picture_type, coded_order, display_order);
1012 mpeg2enc_update_picture_parameter(ctx, picture_type, coded_order, display_order);
1014 if (coded_order == 0) {
1015 assert(picture_type == VAEncPictureTypeIntra);
1016 length_in_bits = build_packed_seq_buffer(&ctx->seq_param, &packed_seq_buffer);
1017 packed_header_param_buffer.type = VAEncPackedHeaderMPEG2_SPS;
1018 packed_header_param_buffer.has_emulation_bytes = 0;
1019 packed_header_param_buffer.bit_length = length_in_bits;
1020 va_status = vaCreateBuffer(ctx->va_dpy,
1022 VAEncPackedHeaderParameterBufferType,
1023 sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
1024 &ctx->packed_seq_header_param_buf_id);
1025 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1027 va_status = vaCreateBuffer(ctx->va_dpy,
1029 VAEncPackedHeaderDataBufferType,
1030 (length_in_bits + 7) / 8, 1, packed_seq_buffer,
1031 &ctx->packed_seq_buf_id);
1032 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1034 free(packed_seq_buffer);
1037 length_in_bits = build_packed_pic_buffer(&ctx->seq_param, &ctx->pic_param, &packed_pic_buffer);
1038 packed_header_param_buffer.type = VAEncPackedHeaderMPEG2_PPS;
1039 packed_header_param_buffer.has_emulation_bytes = 0;
1040 packed_header_param_buffer.bit_length = length_in_bits;
1042 va_status = vaCreateBuffer(ctx->va_dpy,
1044 VAEncPackedHeaderParameterBufferType,
1045 sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
1046 &ctx->packed_pic_header_param_buf_id);
1047 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1049 va_status = vaCreateBuffer(ctx->va_dpy,
1051 VAEncPackedHeaderDataBufferType,
1052 (length_in_bits + 7) / 8, 1, packed_pic_buffer,
1053 &ctx->packed_pic_buf_id);
1054 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1056 free(packed_pic_buffer);
1058 /* sequence parameter set */
1059 VAEncSequenceParameterBufferMPEG2 *seq_param = &ctx->seq_param;
1060 va_status = vaCreateBuffer(ctx->va_dpy,
1062 VAEncSequenceParameterBufferType,
1066 &ctx->seq_param_buf_id);
1067 CHECK_VASTATUS(va_status,"vaCreateBuffer");;
1069 /* slice parameter */
1070 mpeg2enc_update_slice_parameter(ctx, picture_type);
1076 mpeg2enc_render_picture(struct mpeg2enc_context *ctx)
1079 VABufferID va_buffers[16];
1080 unsigned int num_va_buffers = 0;
1082 va_buffers[num_va_buffers++] = ctx->seq_param_buf_id;
1083 va_buffers[num_va_buffers++] = ctx->pic_param_buf_id;
1085 if (ctx->packed_seq_header_param_buf_id != VA_INVALID_ID)
1086 va_buffers[num_va_buffers++] = ctx->packed_seq_header_param_buf_id;
1088 if (ctx->packed_seq_buf_id != VA_INVALID_ID)
1089 va_buffers[num_va_buffers++] = ctx->packed_seq_buf_id;
1091 if (ctx->packed_pic_header_param_buf_id != VA_INVALID_ID)
1092 va_buffers[num_va_buffers++] = ctx->packed_pic_header_param_buf_id;
1094 if (ctx->packed_pic_buf_id != VA_INVALID_ID)
1095 va_buffers[num_va_buffers++] = ctx->packed_pic_buf_id;
1097 va_status = vaBeginPicture(ctx->va_dpy,
1099 surface_ids[ctx->current_input_surface]);
1100 CHECK_VASTATUS(va_status,"vaBeginPicture");
1102 va_status = vaRenderPicture(ctx->va_dpy,
1106 CHECK_VASTATUS(va_status,"vaRenderPicture");
1108 va_status = vaRenderPicture(ctx->va_dpy,
1110 &ctx->slice_param_buf_id[0],
1111 ctx->num_slice_groups);
1112 CHECK_VASTATUS(va_status,"vaRenderPicture");
1114 va_status = vaEndPicture(ctx->va_dpy, ctx->context_id);
1115 CHECK_VASTATUS(va_status,"vaEndPicture");
1121 mpeg2enc_destroy_buffers(struct mpeg2enc_context *ctx, VABufferID *va_buffers, unsigned int num_va_buffers)
1126 for (i = 0; i < num_va_buffers; i++) {
1127 if (va_buffers[i] != VA_INVALID_ID) {
1128 va_status = vaDestroyBuffer(ctx->va_dpy, va_buffers[i]);
1129 CHECK_VASTATUS(va_status,"vaDestroyBuffer");
1130 va_buffers[i] = VA_INVALID_ID;
1138 end_picture(struct mpeg2enc_context *ctx, VAEncPictureType picture_type, int next_is_bpic)
1142 /* Prepare for next picture */
1143 tempID = surface_ids[SID_RECON_PICTURE];
1145 if (picture_type != VAEncPictureTypeBidirectional) {
1147 surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L1];
1148 surface_ids[SID_REFERENCE_PICTURE_L1] = tempID;
1150 surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L0];
1151 surface_ids[SID_REFERENCE_PICTURE_L0] = tempID;
1154 if (!next_is_bpic) {
1155 surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L0];
1156 surface_ids[SID_REFERENCE_PICTURE_L0] = surface_ids[SID_REFERENCE_PICTURE_L1];
1157 surface_ids[SID_REFERENCE_PICTURE_L1] = tempID;
1161 mpeg2enc_destroy_buffers(ctx, &ctx->seq_param_buf_id, 1);
1162 mpeg2enc_destroy_buffers(ctx, &ctx->pic_param_buf_id, 1);
1163 mpeg2enc_destroy_buffers(ctx, &ctx->packed_seq_header_param_buf_id, 1);
1164 mpeg2enc_destroy_buffers(ctx, &ctx->packed_seq_buf_id, 1);
1165 mpeg2enc_destroy_buffers(ctx, &ctx->packed_pic_header_param_buf_id, 1);
1166 mpeg2enc_destroy_buffers(ctx, &ctx->packed_pic_buf_id, 1);
1167 mpeg2enc_destroy_buffers(ctx, &ctx->slice_param_buf_id[0], ctx->num_slice_groups);
1168 mpeg2enc_destroy_buffers(ctx, &ctx->codedbuf_buf_id, 1);
1169 memset(ctx->slice_param, 0, sizeof(ctx->slice_param));
1170 ctx->num_slice_groups = 0;
1174 store_coded_buffer(struct mpeg2enc_context *ctx, VAEncPictureType picture_type)
1176 VACodedBufferSegment *coded_buffer_segment;
1177 unsigned char *coded_mem;
1178 int slice_data_length;
1180 VASurfaceStatus surface_status;
1183 va_status = vaSyncSurface(ctx->va_dpy, surface_ids[ctx->current_input_surface]);
1184 CHECK_VASTATUS(va_status,"vaSyncSurface");
1187 va_status = vaQuerySurfaceStatus(ctx->va_dpy, surface_ids[ctx->current_input_surface], &surface_status);
1188 CHECK_VASTATUS(va_status,"vaQuerySurfaceStatus");
1190 va_status = vaMapBuffer(ctx->va_dpy, ctx->codedbuf_buf_id, (void **)(&coded_buffer_segment));
1191 CHECK_VASTATUS(va_status,"vaMapBuffer");
1192 coded_mem = coded_buffer_segment->buf;
1194 if (coded_buffer_segment->status & VA_CODED_BUF_STATUS_SLICE_OVERFLOW_MASK) {
1195 if (picture_type == VAEncPictureTypeIntra)
1196 ctx->codedbuf_i_size *= 2;
1198 ctx->codedbuf_pb_size *= 2;
1200 vaUnmapBuffer(ctx->va_dpy, ctx->codedbuf_buf_id);
1204 slice_data_length = coded_buffer_segment->size;
1207 w_items = fwrite(coded_mem, slice_data_length, 1, ctx->ofp);
1208 } while (w_items != 1);
1210 if (picture_type == VAEncPictureTypeIntra) {
1211 if (ctx->codedbuf_i_size > slice_data_length * 3 / 2) {
1212 ctx->codedbuf_i_size = slice_data_length * 3 / 2;
1215 if (ctx->codedbuf_pb_size < slice_data_length) {
1216 ctx->codedbuf_pb_size = slice_data_length;
1219 if (ctx->codedbuf_pb_size > slice_data_length * 3 / 2) {
1220 ctx->codedbuf_pb_size = slice_data_length * 3 / 2;
1224 vaUnmapBuffer(ctx->va_dpy, ctx->codedbuf_buf_id);
1230 encode_picture(struct mpeg2enc_context *ctx,
1233 VAEncPictureType picture_type,
1235 int next_display_order)
1238 int ret = 0, codedbuf_size;
1240 begin_picture(ctx, coded_order, display_order, picture_type);
1243 /* upload YUV data to VA surface for next frame */
1244 if (next_display_order >= ctx->num_pictures)
1245 next_display_order = ctx->num_pictures - 1;
1247 fseek(ctx->ifp, ctx->frame_size * next_display_order, SEEK_SET);
1248 ctx->upload_thread_value = pthread_create(&ctx->upload_thread_id,
1250 upload_yuv_to_surface,
1255 mpeg2enc_destroy_buffers(ctx, &ctx->codedbuf_buf_id, 1);
1256 mpeg2enc_destroy_buffers(ctx, &ctx->pic_param_buf_id, 1);
1259 if (VAEncPictureTypeIntra == picture_type) {
1260 codedbuf_size = ctx->codedbuf_i_size;
1262 codedbuf_size = ctx->codedbuf_pb_size;
1266 va_status = vaCreateBuffer(ctx->va_dpy,
1268 VAEncCodedBufferType,
1269 codedbuf_size, 1, NULL,
1270 &ctx->codedbuf_buf_id);
1271 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1273 /* picture parameter set */
1274 mpeg2enc_update_picture_parameter_buffer(ctx, picture_type, coded_order, display_order);
1276 mpeg2enc_render_picture(ctx);
1278 ret = store_coded_buffer(ctx, picture_type);
1281 end_picture(ctx, picture_type, next_is_bpic);
1285 encode_pb_pictures(struct mpeg2enc_context *ctx,
1296 VAEncPictureTypePredictive,
1300 for( i = 0; i < nbframes - 1; i++) {
1304 VAEncPictureTypeBidirectional,
1312 VAEncPictureTypeBidirectional,
1318 mpeg2enc_run(struct mpeg2enc_context *ctx)
1320 int display_order = 0, coded_order = 0;
1322 for (display_order = 0; display_order < ctx->num_pictures;) {
1323 if (ctx->mode == MPEG2_MODE_I) {
1327 VAEncPictureTypeIntra,
1332 } else if (ctx->mode == MPEG2_MODE_IP) {
1334 if ((display_order % ctx->intra_period) == 0) {
1338 VAEncPictureTypeIntra,
1347 VAEncPictureTypePredictive,
1353 } else { // follow the i,p,b pattern
1354 static int fcurrent = 0;
1359 fcurrent = fcurrent % (sizeof(picture_type_pattern)/sizeof(int[2]));
1360 fnext = (fcurrent+1) % (sizeof(picture_type_pattern)/sizeof(int[2]));
1362 if ( picture_type_pattern[fcurrent][0] == VAEncPictureTypeIntra ) {
1366 VAEncPictureTypeIntra,
1368 display_order + picture_type_pattern[fnext][1]);
1372 encode_pb_pictures(ctx,
1375 picture_type_pattern[fcurrent][1] - 1,
1376 display_order + picture_type_pattern[fcurrent][1] + picture_type_pattern[fnext][1] - 1);
1377 display_order += picture_type_pattern[fcurrent][1];
1384 fprintf(stderr, "\r %d/%d ...", display_order + 1, ctx->num_pictures);
1393 mpeg2enc_release_va_resources(struct mpeg2enc_context *ctx)
1395 vaDestroySurfaces(ctx->va_dpy, surface_ids, SID_NUMBER);
1396 vaDestroyContext(ctx->va_dpy, ctx->context_id);
1397 vaDestroyConfig(ctx->va_dpy, ctx->config_id);
1398 vaTerminate(ctx->va_dpy);
1399 va_close_display(ctx->va_dpy);
1403 mpeg2enc_end(struct mpeg2enc_context *ctx)
1405 pthread_join(ctx->upload_thread_id, NULL);
1406 mpeg2enc_release_va_resources(ctx);
1410 main(int argc, char *argv[])
1412 struct mpeg2enc_context ctx;
1413 struct timeval tpstart, tpend;
1416 gettimeofday(&tpstart, NULL);
1418 memset(&ctx, 0, sizeof(ctx));
1419 parse_args(&ctx, argc, argv);
1420 mpeg2enc_init(&ctx);
1424 gettimeofday(&tpend, NULL);
1425 timeuse = 1000000 * (tpend.tv_sec - tpstart.tv_sec) + tpend.tv_usec - tpstart.tv_usec;
1427 fprintf(stderr, "\ndone!\n");
1428 fprintf(stderr, "encode %d frames in %f secondes, FPS is %.1f\n", ctx.num_pictures, timeuse, ctx.num_pictures / timeuse);
1430 mpeg2enc_exit(&ctx, 0);