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 VAProfile mpeg2_va_profiles[] = {
82 struct mpeg2enc_context {
84 int rate_control_mode;
86 int mode; /* 0:I, 1:I/P, 2:I/P/B */
96 unsigned char *frame_data_buffer;
99 int bit_rate; /* in kbps */
100 VAEncPictureType next_type;
101 int next_display_order;
105 int gop_header_in_display_order;
109 VAEncSequenceParameterBufferMPEG2 seq_param;
110 VAEncPictureParameterBufferMPEG2 pic_param;
111 VAEncSliceParameterBufferMPEG2 slice_param[MAX_SLICES];
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 codedbuf_buf_id; /* Output buffer, compressed data */
118 VABufferID packed_seq_header_param_buf_id;
119 VABufferID packed_seq_buf_id;
120 VABufferID packed_pic_header_param_buf_id;
121 VABufferID packed_pic_buf_id;
122 int num_slice_groups;
124 int codedbuf_pb_size;
127 pthread_t upload_thread_id;
128 int upload_thread_value;
129 int current_input_surface;
130 int current_upload_surface;
136 #define BITSTREAM_ALLOCATE_STEPPING 4096
139 unsigned int *buffer;
141 int max_size_in_dword;
144 typedef struct __bitstream bitstream;
147 swap32(unsigned int val)
149 unsigned char *pval = (unsigned char *)&val;
151 return ((pval[0] << 24) |
158 bitstream_start(bitstream *bs)
160 bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
161 bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
166 bitstream_end(bitstream *bs)
168 int pos = (bs->bit_offset >> 5);
169 int bit_offset = (bs->bit_offset & 0x1f);
170 int bit_left = 32 - bit_offset;
173 bs->buffer[pos] = swap32((bs->buffer[pos] << bit_left));
178 bitstream_put_ui(bitstream *bs, unsigned int val, int size_in_bits)
180 int pos = (bs->bit_offset >> 5);
181 int bit_offset = (bs->bit_offset & 0x1f);
182 int bit_left = 32 - bit_offset;
187 if (size_in_bits < 32)
188 val &= ((1 << size_in_bits) - 1);
190 bs->bit_offset += size_in_bits;
192 if (bit_left > size_in_bits) {
193 bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
195 size_in_bits -= bit_left;
196 bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
197 bs->buffer[pos] = swap32(bs->buffer[pos]);
199 if (pos + 1 == bs->max_size_in_dword) {
200 bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
201 bs->buffer = realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
204 bs->buffer[pos + 1] = val;
209 bitstream_byte_aligning(bitstream *bs, int bit)
211 int bit_offset = (bs->bit_offset & 0x7);
212 int bit_left = 8 - bit_offset;
218 assert(bit == 0 || bit == 1);
221 new_val = (1 << bit_left) - 1;
225 bitstream_put_ui(bs, new_val, bit_left);
228 static struct mpeg2_frame_rate {
231 } frame_rate_tab[] = {
243 find_frame_rate_code(const VAEncSequenceParameterBufferMPEG2 *seq_param)
245 unsigned int delta = -1;
247 float frame_rate_value = seq_param->frame_rate *
248 (seq_param->sequence_extension.bits.frame_rate_extension_d + 1) /
249 (seq_param->sequence_extension.bits.frame_rate_extension_n + 1);
251 for (i = 0; i < sizeof(frame_rate_tab) / sizeof(frame_rate_tab[0]); i++) {
253 if (abs(1000 * frame_rate_tab[i].value - 1000 * frame_rate_value) < delta) {
254 code = frame_rate_tab[i].code;
255 delta = abs(1000 * frame_rate_tab[i].value - 1000 * frame_rate_value);
263 sps_rbsp(struct mpeg2enc_context *ctx,
264 const VAEncSequenceParameterBufferMPEG2 *seq_param,
267 int frame_rate_code = find_frame_rate_code(seq_param);
269 if (ctx->new_sequence) {
270 bitstream_put_ui(bs, START_CODE_SEQ, 32);
271 bitstream_put_ui(bs, seq_param->picture_width, 12);
272 bitstream_put_ui(bs, seq_param->picture_height, 12);
273 bitstream_put_ui(bs, seq_param->aspect_ratio_information, 4);
274 bitstream_put_ui(bs, frame_rate_code, 4); /* frame_rate_code */
275 bitstream_put_ui(bs, (seq_param->bits_per_second + 399) / 400, 18); /* the low 18 bits of bit_rate */
276 bitstream_put_ui(bs, 1, 1); /* marker_bit */
277 bitstream_put_ui(bs, seq_param->vbv_buffer_size, 10);
278 bitstream_put_ui(bs, 0, 1); /* constraint_parameter_flag, always 0 for MPEG-2 */
279 bitstream_put_ui(bs, 0, 1); /* load_intra_quantiser_matrix */
280 bitstream_put_ui(bs, 0, 1); /* load_non_intra_quantiser_matrix */
282 bitstream_byte_aligning(bs, 0);
284 bitstream_put_ui(bs, START_CODE_EXT, 32);
285 bitstream_put_ui(bs, 1, 4); /* sequence_extension id */
286 bitstream_put_ui(bs, seq_param->sequence_extension.bits.profile_and_level_indication, 8);
287 bitstream_put_ui(bs, seq_param->sequence_extension.bits.progressive_sequence, 1);
288 bitstream_put_ui(bs, seq_param->sequence_extension.bits.chroma_format, 2);
289 bitstream_put_ui(bs, seq_param->picture_width >> 12, 2);
290 bitstream_put_ui(bs, seq_param->picture_height >> 12, 2);
291 bitstream_put_ui(bs, ((seq_param->bits_per_second + 399) / 400) >> 18, 12); /* bit_rate_extension */
292 bitstream_put_ui(bs, 1, 1); /* marker_bit */
293 bitstream_put_ui(bs, seq_param->vbv_buffer_size >> 10, 8);
294 bitstream_put_ui(bs, seq_param->sequence_extension.bits.low_delay, 1);
295 bitstream_put_ui(bs, seq_param->sequence_extension.bits.frame_rate_extension_n, 2);
296 bitstream_put_ui(bs, seq_param->sequence_extension.bits.frame_rate_extension_d, 5);
298 bitstream_byte_aligning(bs, 0);
301 if (ctx->new_gop_header) {
302 bitstream_put_ui(bs, START_CODE_GOP, 32);
303 bitstream_put_ui(bs, seq_param->gop_header.bits.time_code, 25);
304 bitstream_put_ui(bs, seq_param->gop_header.bits.closed_gop, 1);
305 bitstream_put_ui(bs, seq_param->gop_header.bits.broken_link, 1);
307 bitstream_byte_aligning(bs, 0);
312 pps_rbsp(const VAEncSequenceParameterBufferMPEG2 *seq_param,
313 const VAEncPictureParameterBufferMPEG2 *pic_param,
319 if (seq_param->sequence_extension.bits.chroma_format == CHROMA_FORMAT_420)
320 chroma_420_type = pic_param->picture_coding_extension.bits.progressive_frame;
324 bitstream_put_ui(bs, START_CODE_PICUTRE, 32);
325 bitstream_put_ui(bs, pic_param->temporal_reference, 10);
327 pic_param->picture_type == VAEncPictureTypeIntra ? 1 :
328 pic_param->picture_type == VAEncPictureTypePredictive ? 2 : 3,
330 bitstream_put_ui(bs, 0xFFFF, 16); /* vbv_delay, always 0xFFFF */
332 if (pic_param->picture_type == VAEncPictureTypePredictive) {
333 bitstream_put_ui(bs, 0, 1); /* full_pel_forward_vector, always 0 for MPEG-2 */
334 bitstream_put_ui(bs, 7, 3); /* forward_f_code, always 7 for MPEG-2 */
337 if (pic_param->picture_type == VAEncPictureTypeBidirectional) {
338 bitstream_put_ui(bs, 0, 1); /* full_pel_backward_vector, always 0 for MPEG-2 */
339 bitstream_put_ui(bs, 7, 3); /* backward_f_code, always 7 for MPEG-2 */
342 bitstream_put_ui(bs, 0, 1); /* extra_bit_picture, 0 */
344 bitstream_byte_aligning(bs, 0);
346 bitstream_put_ui(bs, START_CODE_EXT, 32);
347 bitstream_put_ui(bs, 8, 4); /* Picture Coding Extension ID: 8 */
348 bitstream_put_ui(bs, pic_param->f_code[0][0], 4);
349 bitstream_put_ui(bs, pic_param->f_code[0][1], 4);
350 bitstream_put_ui(bs, pic_param->f_code[1][0], 4);
351 bitstream_put_ui(bs, pic_param->f_code[1][1], 4);
353 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.intra_dc_precision, 2);
354 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.picture_structure, 2);
355 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.top_field_first, 1);
356 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.frame_pred_frame_dct, 1);
357 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.concealment_motion_vectors, 1);
358 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.q_scale_type, 1);
359 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.intra_vlc_format, 1);
360 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.alternate_scan, 1);
361 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.repeat_first_field, 1);
362 bitstream_put_ui(bs, chroma_420_type, 1);
363 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.progressive_frame, 1);
364 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.composite_display_flag, 1);
366 bitstream_byte_aligning(bs, 0);
368 if (pic_param->user_data_length) {
369 bitstream_put_ui(bs, START_CODE_USER, 32);
371 for (i = 0; i < pic_param->user_data_length; i++) {
372 bitstream_put_ui(bs, pic_param->user_data[i], 8);
375 bitstream_byte_aligning(bs, 0);
380 build_packed_pic_buffer(const VAEncSequenceParameterBufferMPEG2 *seq_param,
381 const VAEncPictureParameterBufferMPEG2 *pic_param,
382 unsigned char **header_buffer)
386 bitstream_start(&bs);
387 pps_rbsp(seq_param, pic_param, &bs);
390 *header_buffer = (unsigned char *)bs.buffer;
391 return bs.bit_offset;
395 build_packed_seq_buffer(struct mpeg2enc_context *ctx,
396 const VAEncSequenceParameterBufferMPEG2 *seq_param,
397 unsigned char **header_buffer)
401 bitstream_start(&bs);
402 sps_rbsp(ctx, seq_param, &bs);
405 *header_buffer = (unsigned char *)bs.buffer;
406 return bs.bit_offset;
412 #define SID_INPUT_PICTURE_0 0
413 #define SID_INPUT_PICTURE_1 1
414 #define SID_REFERENCE_PICTURE_L0 2
415 #define SID_REFERENCE_PICTURE_L1 3
416 #define SID_RECON_PICTURE 4
417 #define SID_NUMBER SID_RECON_PICTURE + 1
419 static VASurfaceID surface_ids[SID_NUMBER];
422 * upload thread function
425 upload_yuv_to_surface(void *data)
427 struct mpeg2enc_context *ctx = data;
428 VAImage surface_image;
430 void *surface_p = NULL;
431 unsigned char *y_src, *u_src, *v_src;
432 unsigned char *y_dst, *u_dst, *v_dst;
433 int y_size = ctx->width * ctx->height;
434 int u_size = (ctx->width >> 1) * (ctx->height >> 1);
439 n_items = fread(ctx->frame_data_buffer, ctx->frame_size, 1, ctx->ifp);
440 } while (n_items != 1);
442 va_status = vaDeriveImage(ctx->va_dpy, surface_ids[ctx->current_upload_surface], &surface_image);
443 CHECK_VASTATUS(va_status,"vaDeriveImage");
445 vaMapBuffer(ctx->va_dpy, surface_image.buf, &surface_p);
446 assert(VA_STATUS_SUCCESS == va_status);
448 y_src = ctx->frame_data_buffer;
449 u_src = ctx->frame_data_buffer + y_size; /* UV offset for NV12 */
450 v_src = ctx->frame_data_buffer + y_size + u_size;
452 y_dst = surface_p + surface_image.offsets[0];
453 u_dst = surface_p + surface_image.offsets[1]; /* UV offset for NV12 */
454 v_dst = surface_p + surface_image.offsets[2];
457 for (row = 0; row < surface_image.height; row++) {
458 memcpy(y_dst, y_src, surface_image.width);
459 y_dst += surface_image.pitches[0];
463 if (surface_image.format.fourcc == VA_FOURCC_NV12) { /* UV plane */
464 for (row = 0; row < surface_image.height / 2; row++) {
465 for (col = 0; col < surface_image.width / 2; col++) {
466 u_dst[col * 2] = u_src[col];
467 u_dst[col * 2 + 1] = v_src[col];
470 u_dst += surface_image.pitches[1];
471 u_src += (ctx->width / 2);
472 v_src += (ctx->width / 2);
475 for (row = 0; row < surface_image.height / 2; row++) {
476 for (col = 0; col < surface_image.width / 2; col++) {
477 u_dst[col] = u_src[col];
478 v_dst[col] = v_src[col];
481 u_dst += surface_image.pitches[1];
482 v_dst += surface_image.pitches[2];
483 u_src += (ctx->width / 2);
484 v_src += (ctx->width / 2);
488 vaUnmapBuffer(ctx->va_dpy, surface_image.buf);
489 vaDestroyImage(ctx->va_dpy, surface_image.image_id);
495 mpeg2enc_exit(struct mpeg2enc_context *ctx, int exit_code)
497 if (ctx->frame_data_buffer) {
498 free(ctx->frame_data_buffer);
499 ctx->frame_data_buffer = NULL;
518 fprintf(stderr, "Usage: %s --help\n", program);
519 fprintf(stderr, "\t--help print this message\n");
520 fprintf(stderr, "Usage: %s <width> <height> <ifile> <ofile> [options]\n", program);
521 fprintf(stderr, "\t<width> specifies the frame width\n");
522 fprintf(stderr, "\t<height> specifies the frame height\n");
523 fprintf(stderr, "\t<ifile> specifies the I420/IYUV YUV file\n");
524 fprintf(stderr, "\t<ofile> specifies the encoded MPEG-2 file\n");
525 fprintf(stderr, "where options include:\n");
526 fprintf(stderr, "\t--cqp <QP> const qp mode with specified <QP>\n");
527 fprintf(stderr, "\t--fps <FPS> specify the frame rate\n");
528 fprintf(stderr, "\t--mode <MODE> specify the mode 0 (I), 1 (I/P) and 2 (I/P/B)\n");
529 fprintf(stderr, "\t--profile <PROFILE> specify the profile 0(Simple), or 1(Main, default)\n");
530 fprintf(stderr, "\t--level <LEVEL> specify the level 0(Low), 1(Main, default) or 2(High)\n");
534 parse_args(struct mpeg2enc_context *ctx, int argc, char **argv)
537 int option_index = 0;
539 static struct option long_options[] = {
540 {"help", no_argument, 0, 'h'},
541 {"cqp", required_argument, 0, 'c'},
542 {"fps", required_argument, 0, 'f'},
543 {"mode", required_argument, 0, 'm'},
544 {"profile", required_argument, 0, 'p'},
545 {"level", required_argument, 0, 'l'},
549 if ((argc == 2 && strcmp(argv[1], "--help") == 0) ||
553 ctx->width = atoi(argv[1]);
554 ctx->height = atoi(argv[2]);
556 if (ctx->width <= 0 || ctx->height <= 0) {
557 fprintf(stderr, "<width> and <height> must be greater than 0\n");
561 ctx->ifp = fopen(argv[3], "rb");
563 if (ctx->ifp == NULL) {
564 fprintf(stderr, "Can't open the input file\n");
568 fseek(ctx->ifp, 0l, SEEK_END);
569 file_size = ftell(ctx->ifp);
570 ctx->frame_size = ctx->width * ctx->height * 3 / 2;
572 if ((file_size < ctx->frame_size) ||
573 (file_size % ctx->frame_size)) {
574 fprintf(stderr, "The input file size %ld isn't a multiple of the frame size %d\n", file_size, ctx->frame_size);
578 ctx->num_pictures = file_size / ctx->frame_size;
579 fseek(ctx->ifp, 0l, SEEK_SET);
581 ctx->ofp = fopen(argv[4], "wb");
583 if (ctx->ofp == NULL) {
584 fprintf(stderr, "Can't create the output file\n");
591 ctx->rate_control_mode = VA_RC_CQP;
592 ctx->mode = MPEG2_MODE_IP;
593 ctx->profile = VAProfileMPEG2Main;
594 ctx->level = MPEG2_LEVEL_MAIN;
598 while((c = getopt_long(argc, argv,
601 &option_index)) != -1) {
606 /* only support q_scale_type = 0 */
607 if (tmp > 62 || tmp < 2) {
608 fprintf(stderr, "Warning: QP must be in [2, 62]\n");
617 ctx->qp = tmp & 0xFE;
618 ctx->rate_control_mode = VA_RC_CQP;
626 fprintf(stderr, "Warning: FPS must be greater than 0\n");
630 ctx->rate_control_mode = VA_RC_CBR;
637 if (tmp < MPEG2_MODE_I || tmp > MPEG2_MODE_IPB)
638 fprintf(stderr, "Waning: MODE must be 0, 1, or 2\n");
647 if (tmp < 0 || tmp > 1)
648 fprintf(stderr, "Waning: PROFILE must be 0 or 1\n");
650 ctx->profile = mpeg2_va_profiles[tmp];
657 if (tmp < MPEG2_LEVEL_LOW || tmp > MPEG2_LEVEL_HIGH)
658 fprintf(stderr, "Waning: LEVEL must be 0, 1, or 2\n");
665 fprintf(stderr, "Error: unkown command options\n");
677 mpeg2enc_exit(ctx, 1);
684 mpeg2enc_init_sequence_parameter(struct mpeg2enc_context *ctx,
685 VAEncSequenceParameterBufferMPEG2 *seq_param)
687 int profile = 4, level = 8;
689 switch (ctx->profile) {
690 case VAProfileMPEG2Simple:
694 case VAProfileMPEG2Main:
703 switch (ctx->level) {
704 case MPEG2_LEVEL_LOW:
708 case MPEG2_LEVEL_MAIN:
712 case MPEG2_LEVEL_HIGH:
721 seq_param->intra_period = ctx->intra_period;
722 seq_param->ip_period = ctx->ip_period; /* FIXME: ??? */
723 seq_param->picture_width = ctx->width;
724 seq_param->picture_height = ctx->height;
726 if (ctx->bit_rate > 0)
727 seq_param->bits_per_second = 1024 * ctx->bit_rate; /* use kbps as input */
729 seq_param->bits_per_second = 0x3FFFF * 400;
731 seq_param->frame_rate = ctx->fps;
732 seq_param->aspect_ratio_information = 1;
733 seq_param->vbv_buffer_size = 3; /* B = 16 * 1024 * vbv_buffer_size */
735 seq_param->sequence_extension.bits.profile_and_level_indication = profile << 4 | level;
736 seq_param->sequence_extension.bits.progressive_sequence = 1; /* progressive frame-pictures */
737 seq_param->sequence_extension.bits.chroma_format = CHROMA_FORMAT_420; /* 4:2:0 */
738 seq_param->sequence_extension.bits.low_delay = 0; /* FIXME */
739 seq_param->sequence_extension.bits.frame_rate_extension_n = 0;
740 seq_param->sequence_extension.bits.frame_rate_extension_d = 0;
742 seq_param->gop_header.bits.time_code = (1 << 12); /* bit12: marker_bit */
743 seq_param->gop_header.bits.closed_gop = 0;
744 seq_param->gop_header.bits.broken_link = 0;
748 mpeg2enc_init_picture_parameter(struct mpeg2enc_context *ctx,
749 VAEncPictureParameterBufferMPEG2 *pic_param)
751 pic_param->forward_reference_picture = VA_INVALID_ID;
752 pic_param->backward_reference_picture = VA_INVALID_ID;
753 pic_param->reconstructed_picture = VA_INVALID_ID;
754 pic_param->coded_buf = VA_INVALID_ID;
755 pic_param->picture_type = VAEncPictureTypeIntra;
757 pic_param->temporal_reference = 0;
758 pic_param->f_code[0][0] = 0xf;
759 pic_param->f_code[0][1] = 0xf;
760 pic_param->f_code[1][0] = 0xf;
761 pic_param->f_code[1][1] = 0xf;
763 pic_param->picture_coding_extension.bits.intra_dc_precision = 0; /* 8bits */
764 pic_param->picture_coding_extension.bits.picture_structure = 3; /* frame picture */
765 pic_param->picture_coding_extension.bits.top_field_first = 0;
766 pic_param->picture_coding_extension.bits.frame_pred_frame_dct = 1; /* FIXME */
767 pic_param->picture_coding_extension.bits.concealment_motion_vectors = 0;
768 pic_param->picture_coding_extension.bits.q_scale_type = 0;
769 pic_param->picture_coding_extension.bits.intra_vlc_format = 0;
770 pic_param->picture_coding_extension.bits.alternate_scan = 0;
771 pic_param->picture_coding_extension.bits.repeat_first_field = 0;
772 pic_param->picture_coding_extension.bits.progressive_frame = 1;
773 pic_param->picture_coding_extension.bits.composite_display_flag = 0;
775 pic_param->user_data_length = 0;
779 mpeg2enc_alloc_va_resources(struct mpeg2enc_context *ctx)
781 VAEntrypoint *entrypoint_list;
782 VAConfigAttrib attrib_list[2];
784 int max_entrypoints, num_entrypoints, entrypoint;
785 int major_ver, minor_ver;
787 ctx->va_dpy = va_open_display();
788 va_status = vaInitialize(ctx->va_dpy,
791 CHECK_VASTATUS(va_status, "vaInitialize");
793 max_entrypoints = vaMaxNumEntrypoints(ctx->va_dpy);
794 entrypoint_list = malloc(max_entrypoints * sizeof(VAEntrypoint));
795 vaQueryConfigEntrypoints(ctx->va_dpy,
800 for (entrypoint = 0; entrypoint < num_entrypoints; entrypoint++) {
801 if (entrypoint_list[entrypoint] == VAEntrypointEncSlice)
805 free(entrypoint_list);
807 if (entrypoint == num_entrypoints) {
808 /* not find Slice entry point */
812 /* find out the format for the render target, and rate control mode */
813 attrib_list[0].type = VAConfigAttribRTFormat;
814 attrib_list[1].type = VAConfigAttribRateControl;
815 vaGetConfigAttributes(ctx->va_dpy,
817 VAEntrypointEncSlice,
821 if ((attrib_list[0].value & VA_RT_FORMAT_YUV420) == 0) {
822 /* not find desired YUV420 RT format */
826 if ((attrib_list[1].value & ctx->rate_control_mode) == 0) {
827 /* Can't find matched RC mode */
828 fprintf(stderr, "RC mode %d isn't found, exit\n", ctx->rate_control_mode);
832 attrib_list[0].value = VA_RT_FORMAT_YUV420; /* set to desired RT format */
833 attrib_list[1].value = ctx->rate_control_mode; /* set to desired RC mode */
835 va_status = vaCreateConfig(ctx->va_dpy,
837 VAEntrypointEncSlice,
841 CHECK_VASTATUS(va_status, "vaCreateConfig");
843 /* Create a context for this decode pipe */
844 va_status = vaCreateContext(ctx->va_dpy,
852 CHECK_VASTATUS(va_status, "vaCreateContext");
854 va_status = vaCreateSurfaces(ctx->va_dpy,
860 CHECK_VASTATUS(va_status, "vaCreateSurfaces");
864 mpeg2enc_init(struct mpeg2enc_context *ctx)
868 ctx->frame_data_buffer = (unsigned char *)malloc(ctx->frame_size);
869 ctx->seq_param_buf_id = VA_INVALID_ID;
870 ctx->pic_param_buf_id = VA_INVALID_ID;
871 ctx->packed_seq_header_param_buf_id = VA_INVALID_ID;
872 ctx->packed_seq_buf_id = VA_INVALID_ID;
873 ctx->packed_pic_header_param_buf_id = VA_INVALID_ID;
874 ctx->packed_pic_buf_id = VA_INVALID_ID;
875 ctx->codedbuf_buf_id = VA_INVALID_ID;
876 ctx->codedbuf_i_size = ctx->frame_size;
877 ctx->codedbuf_pb_size = 0;
878 ctx->next_display_order = 0;
879 ctx->next_type = VAEncPictureTypeIntra;
881 if (ctx->mode == MPEG2_MODE_I) {
882 ctx->intra_period = 1;
884 } else if (ctx->mode == MPEG2_MODE_IP) {
885 ctx->intra_period = 16;
888 ctx->intra_period = 16;
892 ctx->next_bframes = ctx->ip_period;
894 ctx->new_sequence = 1;
895 ctx->new_gop_header = 1;
896 ctx->gop_header_in_display_order = 0;
900 for (i = 0; i < MAX_SLICES; i++) {
901 ctx->slice_param_buf_id[i] = VA_INVALID_ID;
904 mpeg2enc_init_sequence_parameter(ctx, &ctx->seq_param);
905 mpeg2enc_init_picture_parameter(ctx, &ctx->pic_param);
906 mpeg2enc_alloc_va_resources(ctx);
909 ctx->current_input_surface = SID_INPUT_PICTURE_0;
910 ctx->current_upload_surface = SID_INPUT_PICTURE_1;
911 ctx->upload_thread_value = pthread_create(&ctx->upload_thread_id,
913 upload_yuv_to_surface,
918 mpeg2enc_time_code(VAEncSequenceParameterBufferMPEG2 *seq_param,
921 int fps = (int)(seq_param->frame_rate + 0.5);
923 int time_code_pictures, time_code_seconds, time_code_minutes, time_code_hours;
924 int drop_frame_flag = 0;
928 time_code_seconds = num_frames / fps;
929 time_code_pictures = num_frames % fps;
930 time_code |= time_code_pictures;
932 time_code_minutes = time_code_seconds / 60;
933 time_code_seconds = time_code_minutes % 60;
934 time_code |= (time_code_seconds << 6);
936 time_code_hours = time_code_minutes / 60;
937 time_code_minutes = time_code_minutes % 60;
939 time_code |= (1 << 12); /* marker_bit */
940 time_code |= (time_code_minutes << 13);
942 time_code_hours = time_code_hours % 24;
943 time_code |= (time_code_hours << 19);
945 time_code |= (drop_frame_flag << 24);
954 mpeg2enc_update_sequence_parameter(struct mpeg2enc_context *ctx,
955 VAEncPictureType picture_type,
959 VAEncSequenceParameterBufferMPEG2 *seq_param = &ctx->seq_param;
961 /* update the time_code info for the new GOP */
962 if (ctx->new_gop_header) {
963 seq_param->gop_header.bits.time_code = mpeg2enc_time_code(seq_param, display_order);
968 mpeg2enc_update_picture_parameter(struct mpeg2enc_context *ctx,
969 VAEncPictureType picture_type,
973 VAEncPictureParameterBufferMPEG2 *pic_param = &ctx->pic_param;
975 pic_param->picture_type = picture_type;
976 pic_param->temporal_reference = (display_order - ctx->gop_header_in_display_order) & 0x3FF;
977 pic_param->reconstructed_picture = surface_ids[SID_RECON_PICTURE];
978 pic_param->forward_reference_picture = surface_ids[SID_REFERENCE_PICTURE_L0];
979 pic_param->backward_reference_picture = surface_ids[SID_REFERENCE_PICTURE_L1];
981 if (pic_param->picture_type == VAEncPictureTypeIntra) {
982 pic_param->f_code[0][0] = 0xf;
983 pic_param->f_code[0][1] = 0xf;
984 pic_param->f_code[1][0] = 0xf;
985 pic_param->f_code[1][1] = 0xf;
986 pic_param->forward_reference_picture = VA_INVALID_SURFACE;
987 pic_param->backward_reference_picture = VA_INVALID_SURFACE;
989 } else if (pic_param->picture_type == VAEncPictureTypePredictive) {
990 pic_param->f_code[0][0] = 0x1;
991 pic_param->f_code[0][1] = 0x1;
992 pic_param->f_code[1][0] = 0xf;
993 pic_param->f_code[1][1] = 0xf;
994 pic_param->forward_reference_picture = surface_ids[SID_REFERENCE_PICTURE_L0];
995 pic_param->backward_reference_picture = VA_INVALID_SURFACE;
996 } else if (pic_param->picture_type == VAEncPictureTypeBidirectional) {
997 pic_param->f_code[0][0] = 0x1;
998 pic_param->f_code[0][1] = 0x1;
999 pic_param->f_code[1][0] = 0x1;
1000 pic_param->f_code[1][1] = 0x1;
1001 pic_param->forward_reference_picture = surface_ids[SID_REFERENCE_PICTURE_L0];
1002 pic_param->backward_reference_picture = surface_ids[SID_REFERENCE_PICTURE_L1];
1009 mpeg2enc_update_picture_parameter_buffer(struct mpeg2enc_context *ctx,
1010 VAEncPictureType picture_type,
1014 VAEncPictureParameterBufferMPEG2 *pic_param = &ctx->pic_param;
1017 /* update the coded buffer id */
1018 pic_param->coded_buf = ctx->codedbuf_buf_id;
1019 va_status = vaCreateBuffer(ctx->va_dpy,
1021 VAEncPictureParameterBufferType,
1025 &ctx->pic_param_buf_id);
1026 CHECK_VASTATUS(va_status, "vaCreateBuffer");
1030 mpeg2enc_update_slice_parameter(struct mpeg2enc_context *ctx, VAEncPictureType picture_type)
1032 VAEncSequenceParameterBufferMPEG2 *seq_param;
1033 VAEncPictureParameterBufferMPEG2 *pic_param;
1034 VAEncSliceParameterBufferMPEG2 *slice_param;
1036 int i, width_in_mbs, height_in_mbs;
1038 pic_param = &ctx->pic_param;
1039 assert(pic_param->picture_coding_extension.bits.q_scale_type == 0);
1041 seq_param = &ctx->seq_param;
1042 width_in_mbs = (seq_param->picture_width + 15) / 16;
1043 height_in_mbs = (seq_param->picture_height + 15) / 16;
1044 ctx->num_slice_groups = 1;
1046 for (i = 0; i < height_in_mbs; i++) {
1047 slice_param = &ctx->slice_param[i];
1048 slice_param->macroblock_address = i * width_in_mbs;
1049 slice_param->num_macroblocks = width_in_mbs;
1050 slice_param->is_intra_slice = (picture_type == VAEncPictureTypeIntra);
1051 slice_param->quantiser_scale_code = ctx->qp / 2;
1054 va_status = vaCreateBuffer(ctx->va_dpy,
1056 VAEncSliceParameterBufferType,
1057 sizeof(*slice_param),
1060 ctx->slice_param_buf_id);
1061 CHECK_VASTATUS(va_status, "vaCreateBuffer");;
1065 begin_picture(struct mpeg2enc_context *ctx,
1068 VAEncPictureType picture_type)
1072 VAEncPackedHeaderParameterBuffer packed_header_param_buffer;
1073 unsigned int length_in_bits;
1074 unsigned char *packed_seq_buffer = NULL, *packed_pic_buffer = NULL;
1076 if (ctx->upload_thread_value != 0) {
1077 fprintf(stderr, "FATAL error!!!\n");
1081 pthread_join(ctx->upload_thread_id, NULL);
1083 ctx->upload_thread_value = -1;
1084 tmp = ctx->current_input_surface;
1085 ctx->current_input_surface = ctx->current_upload_surface;
1086 ctx->current_upload_surface = tmp;
1088 mpeg2enc_update_sequence_parameter(ctx, picture_type, coded_order, display_order);
1089 mpeg2enc_update_picture_parameter(ctx, picture_type, coded_order, display_order);
1091 if (ctx->new_sequence || ctx->new_gop_header) {
1092 assert(picture_type == VAEncPictureTypeIntra);
1093 length_in_bits = build_packed_seq_buffer(ctx, &ctx->seq_param, &packed_seq_buffer);
1094 packed_header_param_buffer.type = VAEncPackedHeaderMPEG2_SPS;
1095 packed_header_param_buffer.has_emulation_bytes = 0;
1096 packed_header_param_buffer.bit_length = length_in_bits;
1097 va_status = vaCreateBuffer(ctx->va_dpy,
1099 VAEncPackedHeaderParameterBufferType,
1100 sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
1101 &ctx->packed_seq_header_param_buf_id);
1102 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1104 va_status = vaCreateBuffer(ctx->va_dpy,
1106 VAEncPackedHeaderDataBufferType,
1107 (length_in_bits + 7) / 8, 1, packed_seq_buffer,
1108 &ctx->packed_seq_buf_id);
1109 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1111 free(packed_seq_buffer);
1114 length_in_bits = build_packed_pic_buffer(&ctx->seq_param, &ctx->pic_param, &packed_pic_buffer);
1115 packed_header_param_buffer.type = VAEncPackedHeaderMPEG2_PPS;
1116 packed_header_param_buffer.has_emulation_bytes = 0;
1117 packed_header_param_buffer.bit_length = length_in_bits;
1119 va_status = vaCreateBuffer(ctx->va_dpy,
1121 VAEncPackedHeaderParameterBufferType,
1122 sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
1123 &ctx->packed_pic_header_param_buf_id);
1124 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1126 va_status = vaCreateBuffer(ctx->va_dpy,
1128 VAEncPackedHeaderDataBufferType,
1129 (length_in_bits + 7) / 8, 1, packed_pic_buffer,
1130 &ctx->packed_pic_buf_id);
1131 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1133 free(packed_pic_buffer);
1135 /* sequence parameter set */
1136 VAEncSequenceParameterBufferMPEG2 *seq_param = &ctx->seq_param;
1137 va_status = vaCreateBuffer(ctx->va_dpy,
1139 VAEncSequenceParameterBufferType,
1143 &ctx->seq_param_buf_id);
1144 CHECK_VASTATUS(va_status,"vaCreateBuffer");;
1146 /* slice parameter */
1147 mpeg2enc_update_slice_parameter(ctx, picture_type);
1153 mpeg2enc_render_picture(struct mpeg2enc_context *ctx)
1156 VABufferID va_buffers[16];
1157 unsigned int num_va_buffers = 0;
1159 va_buffers[num_va_buffers++] = ctx->seq_param_buf_id;
1160 va_buffers[num_va_buffers++] = ctx->pic_param_buf_id;
1162 if (ctx->packed_seq_header_param_buf_id != VA_INVALID_ID)
1163 va_buffers[num_va_buffers++] = ctx->packed_seq_header_param_buf_id;
1165 if (ctx->packed_seq_buf_id != VA_INVALID_ID)
1166 va_buffers[num_va_buffers++] = ctx->packed_seq_buf_id;
1168 if (ctx->packed_pic_header_param_buf_id != VA_INVALID_ID)
1169 va_buffers[num_va_buffers++] = ctx->packed_pic_header_param_buf_id;
1171 if (ctx->packed_pic_buf_id != VA_INVALID_ID)
1172 va_buffers[num_va_buffers++] = ctx->packed_pic_buf_id;
1174 va_status = vaBeginPicture(ctx->va_dpy,
1176 surface_ids[ctx->current_input_surface]);
1177 CHECK_VASTATUS(va_status,"vaBeginPicture");
1179 va_status = vaRenderPicture(ctx->va_dpy,
1183 CHECK_VASTATUS(va_status,"vaRenderPicture");
1185 va_status = vaRenderPicture(ctx->va_dpy,
1187 &ctx->slice_param_buf_id[0],
1188 ctx->num_slice_groups);
1189 CHECK_VASTATUS(va_status,"vaRenderPicture");
1191 va_status = vaEndPicture(ctx->va_dpy, ctx->context_id);
1192 CHECK_VASTATUS(va_status,"vaEndPicture");
1198 mpeg2enc_destroy_buffers(struct mpeg2enc_context *ctx, VABufferID *va_buffers, unsigned int num_va_buffers)
1203 for (i = 0; i < num_va_buffers; i++) {
1204 if (va_buffers[i] != VA_INVALID_ID) {
1205 va_status = vaDestroyBuffer(ctx->va_dpy, va_buffers[i]);
1206 CHECK_VASTATUS(va_status,"vaDestroyBuffer");
1207 va_buffers[i] = VA_INVALID_ID;
1215 end_picture(struct mpeg2enc_context *ctx, VAEncPictureType picture_type, int next_is_bpic)
1219 /* Prepare for next picture */
1220 tempID = surface_ids[SID_RECON_PICTURE];
1222 if (picture_type != VAEncPictureTypeBidirectional) {
1224 surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L1];
1225 surface_ids[SID_REFERENCE_PICTURE_L1] = tempID;
1227 surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L0];
1228 surface_ids[SID_REFERENCE_PICTURE_L0] = tempID;
1231 if (!next_is_bpic) {
1232 surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L0];
1233 surface_ids[SID_REFERENCE_PICTURE_L0] = surface_ids[SID_REFERENCE_PICTURE_L1];
1234 surface_ids[SID_REFERENCE_PICTURE_L1] = tempID;
1238 mpeg2enc_destroy_buffers(ctx, &ctx->seq_param_buf_id, 1);
1239 mpeg2enc_destroy_buffers(ctx, &ctx->pic_param_buf_id, 1);
1240 mpeg2enc_destroy_buffers(ctx, &ctx->packed_seq_header_param_buf_id, 1);
1241 mpeg2enc_destroy_buffers(ctx, &ctx->packed_seq_buf_id, 1);
1242 mpeg2enc_destroy_buffers(ctx, &ctx->packed_pic_header_param_buf_id, 1);
1243 mpeg2enc_destroy_buffers(ctx, &ctx->packed_pic_buf_id, 1);
1244 mpeg2enc_destroy_buffers(ctx, &ctx->slice_param_buf_id[0], ctx->num_slice_groups);
1245 mpeg2enc_destroy_buffers(ctx, &ctx->codedbuf_buf_id, 1);
1246 memset(ctx->slice_param, 0, sizeof(ctx->slice_param));
1247 ctx->num_slice_groups = 0;
1251 store_coded_buffer(struct mpeg2enc_context *ctx, VAEncPictureType picture_type)
1253 VACodedBufferSegment *coded_buffer_segment;
1254 unsigned char *coded_mem;
1255 int slice_data_length;
1257 VASurfaceStatus surface_status;
1260 va_status = vaSyncSurface(ctx->va_dpy, surface_ids[ctx->current_input_surface]);
1261 CHECK_VASTATUS(va_status,"vaSyncSurface");
1264 va_status = vaQuerySurfaceStatus(ctx->va_dpy, surface_ids[ctx->current_input_surface], &surface_status);
1265 CHECK_VASTATUS(va_status,"vaQuerySurfaceStatus");
1267 va_status = vaMapBuffer(ctx->va_dpy, ctx->codedbuf_buf_id, (void **)(&coded_buffer_segment));
1268 CHECK_VASTATUS(va_status,"vaMapBuffer");
1269 coded_mem = coded_buffer_segment->buf;
1271 if (coded_buffer_segment->status & VA_CODED_BUF_STATUS_SLICE_OVERFLOW_MASK) {
1272 if (picture_type == VAEncPictureTypeIntra)
1273 ctx->codedbuf_i_size *= 2;
1275 ctx->codedbuf_pb_size *= 2;
1277 vaUnmapBuffer(ctx->va_dpy, ctx->codedbuf_buf_id);
1281 slice_data_length = coded_buffer_segment->size;
1284 w_items = fwrite(coded_mem, slice_data_length, 1, ctx->ofp);
1285 } while (w_items != 1);
1287 if (picture_type == VAEncPictureTypeIntra) {
1288 if (ctx->codedbuf_i_size > slice_data_length * 3 / 2) {
1289 ctx->codedbuf_i_size = slice_data_length * 3 / 2;
1292 if (ctx->codedbuf_pb_size < slice_data_length) {
1293 ctx->codedbuf_pb_size = slice_data_length;
1296 if (ctx->codedbuf_pb_size > slice_data_length * 3 / 2) {
1297 ctx->codedbuf_pb_size = slice_data_length * 3 / 2;
1301 vaUnmapBuffer(ctx->va_dpy, ctx->codedbuf_buf_id);
1307 encode_picture(struct mpeg2enc_context *ctx,
1310 VAEncPictureType picture_type,
1312 int next_display_order)
1315 int ret = 0, codedbuf_size;
1317 begin_picture(ctx, coded_order, display_order, picture_type);
1320 /* upload YUV data to VA surface for next frame */
1321 if (next_display_order >= ctx->num_pictures)
1322 next_display_order = ctx->num_pictures - 1;
1324 fseek(ctx->ifp, ctx->frame_size * next_display_order, SEEK_SET);
1325 ctx->upload_thread_value = pthread_create(&ctx->upload_thread_id,
1327 upload_yuv_to_surface,
1332 mpeg2enc_destroy_buffers(ctx, &ctx->codedbuf_buf_id, 1);
1333 mpeg2enc_destroy_buffers(ctx, &ctx->pic_param_buf_id, 1);
1336 if (VAEncPictureTypeIntra == picture_type) {
1337 codedbuf_size = ctx->codedbuf_i_size;
1339 codedbuf_size = ctx->codedbuf_pb_size;
1343 va_status = vaCreateBuffer(ctx->va_dpy,
1345 VAEncCodedBufferType,
1346 codedbuf_size, 1, NULL,
1347 &ctx->codedbuf_buf_id);
1348 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1350 /* picture parameter set */
1351 mpeg2enc_update_picture_parameter_buffer(ctx, picture_type, coded_order, display_order);
1353 mpeg2enc_render_picture(ctx);
1355 ret = store_coded_buffer(ctx, picture_type);
1358 end_picture(ctx, picture_type, next_is_bpic);
1362 update_next_frame_info(struct mpeg2enc_context *ctx,
1363 VAEncPictureType curr_type,
1364 int curr_coded_order,
1365 int curr_display_order)
1367 if (((curr_coded_order + 1) % ctx->intra_period) == 0) {
1368 ctx->next_type = VAEncPictureTypeIntra;
1369 ctx->next_display_order = curr_coded_order + 1;
1374 if (curr_type == VAEncPictureTypeIntra) {
1375 assert(curr_display_order == curr_coded_order);
1376 ctx->next_type = VAEncPictureTypePredictive;
1377 ctx->next_bframes = ctx->ip_period;
1378 ctx->next_display_order = curr_display_order + ctx->next_bframes + 1;
1379 } else if (curr_type == VAEncPictureTypePredictive) {
1380 if (ctx->ip_period == 0) {
1381 assert(curr_display_order == curr_coded_order);
1382 ctx->next_type = VAEncPictureTypePredictive;
1383 ctx->next_display_order = curr_display_order + 1;
1385 ctx->next_type = VAEncPictureTypeBidirectional;
1386 ctx->next_display_order = curr_display_order - ctx->next_bframes;
1387 ctx->next_bframes--;
1389 } else if (curr_type == VAEncPictureTypeBidirectional) {
1390 if (ctx->next_bframes == 0) {
1391 ctx->next_type = VAEncPictureTypePredictive;
1392 ctx->next_bframes = ctx->ip_period;
1393 ctx->next_display_order = curr_display_order + ctx->next_bframes + 2;
1395 ctx->next_type = VAEncPictureTypeBidirectional;
1396 ctx->next_display_order = curr_display_order + 1;
1397 ctx->next_bframes--;
1401 if (ctx->next_display_order >= ctx->num_pictures) {
1402 int rtmp = ctx->next_display_order - (ctx->num_pictures - 1);
1403 ctx->next_display_order = ctx->num_pictures - 1;
1404 ctx->next_bframes -= rtmp;
1409 mpeg2enc_run(struct mpeg2enc_context *ctx)
1411 int display_order = 0, coded_order = 0;
1412 VAEncPictureType type;
1414 ctx->new_sequence = 1;
1415 ctx->new_gop_header = 1;
1416 ctx->gop_header_in_display_order = display_order;
1418 while (coded_order < ctx->num_pictures) {
1419 type = ctx->next_type;
1420 display_order = ctx->next_display_order;
1421 /* follow the IPBxxBPBxxB mode */
1422 update_next_frame_info(ctx, type, coded_order, display_order);
1427 ctx->next_type == VAEncPictureTypeBidirectional,
1428 ctx->next_display_order);
1430 /* update gop_header */
1431 ctx->new_sequence = 0;
1432 ctx->new_gop_header = ctx->next_type == VAEncPictureTypeIntra;
1434 if (ctx->new_gop_header)
1435 ctx->gop_header_in_display_order += ctx->intra_period;
1439 fprintf(stderr, "\r %d/%d ...", coded_order, ctx->num_pictures);
1448 mpeg2enc_release_va_resources(struct mpeg2enc_context *ctx)
1450 vaDestroySurfaces(ctx->va_dpy, surface_ids, SID_NUMBER);
1451 vaDestroyContext(ctx->va_dpy, ctx->context_id);
1452 vaDestroyConfig(ctx->va_dpy, ctx->config_id);
1453 vaTerminate(ctx->va_dpy);
1454 va_close_display(ctx->va_dpy);
1458 mpeg2enc_end(struct mpeg2enc_context *ctx)
1460 pthread_join(ctx->upload_thread_id, NULL);
1461 mpeg2enc_release_va_resources(ctx);
1465 main(int argc, char *argv[])
1467 struct mpeg2enc_context ctx;
1468 struct timeval tpstart, tpend;
1471 gettimeofday(&tpstart, NULL);
1473 memset(&ctx, 0, sizeof(ctx));
1474 parse_args(&ctx, argc, argv);
1475 mpeg2enc_init(&ctx);
1479 gettimeofday(&tpend, NULL);
1480 timeuse = 1000000 * (tpend.tv_sec - tpstart.tv_sec) + tpend.tv_usec - tpstart.tv_usec;
1482 fprintf(stderr, "\ndone!\n");
1483 fprintf(stderr, "encode %d frames in %f secondes, FPS is %.1f\n", ctx.num_pictures, timeuse, ctx.num_pictures / timeuse);
1485 mpeg2enc_exit(&ctx, 0);