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 pic_param->picture_type == VAEncPictureTypeBidirectional) {
334 bitstream_put_ui(bs, 0, 1); /* full_pel_forward_vector, always 0 for MPEG-2 */
335 bitstream_put_ui(bs, 7, 3); /* forward_f_code, always 7 for MPEG-2 */
338 if (pic_param->picture_type == VAEncPictureTypeBidirectional) {
339 bitstream_put_ui(bs, 0, 1); /* full_pel_backward_vector, always 0 for MPEG-2 */
340 bitstream_put_ui(bs, 7, 3); /* backward_f_code, always 7 for MPEG-2 */
343 bitstream_put_ui(bs, 0, 1); /* extra_bit_picture, 0 */
345 bitstream_byte_aligning(bs, 0);
347 bitstream_put_ui(bs, START_CODE_EXT, 32);
348 bitstream_put_ui(bs, 8, 4); /* Picture Coding Extension ID: 8 */
349 bitstream_put_ui(bs, pic_param->f_code[0][0], 4);
350 bitstream_put_ui(bs, pic_param->f_code[0][1], 4);
351 bitstream_put_ui(bs, pic_param->f_code[1][0], 4);
352 bitstream_put_ui(bs, pic_param->f_code[1][1], 4);
354 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.intra_dc_precision, 2);
355 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.picture_structure, 2);
356 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.top_field_first, 1);
357 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.frame_pred_frame_dct, 1);
358 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.concealment_motion_vectors, 1);
359 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.q_scale_type, 1);
360 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.intra_vlc_format, 1);
361 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.alternate_scan, 1);
362 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.repeat_first_field, 1);
363 bitstream_put_ui(bs, chroma_420_type, 1);
364 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.progressive_frame, 1);
365 bitstream_put_ui(bs, pic_param->picture_coding_extension.bits.composite_display_flag, 1);
367 bitstream_byte_aligning(bs, 0);
369 if (pic_param->user_data_length) {
370 bitstream_put_ui(bs, START_CODE_USER, 32);
372 for (i = 0; i < pic_param->user_data_length; i++) {
373 bitstream_put_ui(bs, pic_param->user_data[i], 8);
376 bitstream_byte_aligning(bs, 0);
381 build_packed_pic_buffer(const VAEncSequenceParameterBufferMPEG2 *seq_param,
382 const VAEncPictureParameterBufferMPEG2 *pic_param,
383 unsigned char **header_buffer)
387 bitstream_start(&bs);
388 pps_rbsp(seq_param, pic_param, &bs);
391 *header_buffer = (unsigned char *)bs.buffer;
392 return bs.bit_offset;
396 build_packed_seq_buffer(struct mpeg2enc_context *ctx,
397 const VAEncSequenceParameterBufferMPEG2 *seq_param,
398 unsigned char **header_buffer)
402 bitstream_start(&bs);
403 sps_rbsp(ctx, seq_param, &bs);
406 *header_buffer = (unsigned char *)bs.buffer;
407 return bs.bit_offset;
413 #define SID_INPUT_PICTURE_0 0
414 #define SID_INPUT_PICTURE_1 1
415 #define SID_REFERENCE_PICTURE_L0 2
416 #define SID_REFERENCE_PICTURE_L1 3
417 #define SID_RECON_PICTURE 4
418 #define SID_NUMBER SID_RECON_PICTURE + 1
420 static VASurfaceID surface_ids[SID_NUMBER];
423 * upload thread function
426 upload_yuv_to_surface(void *data)
428 struct mpeg2enc_context *ctx = data;
429 VAImage surface_image;
431 void *surface_p = NULL;
432 unsigned char *y_src, *u_src, *v_src;
433 unsigned char *y_dst, *u_dst, *v_dst;
434 int y_size = ctx->width * ctx->height;
435 int u_size = (ctx->width >> 1) * (ctx->height >> 1);
440 n_items = fread(ctx->frame_data_buffer, ctx->frame_size, 1, ctx->ifp);
441 } while (n_items != 1);
443 va_status = vaDeriveImage(ctx->va_dpy, surface_ids[ctx->current_upload_surface], &surface_image);
444 CHECK_VASTATUS(va_status,"vaDeriveImage");
446 vaMapBuffer(ctx->va_dpy, surface_image.buf, &surface_p);
447 assert(VA_STATUS_SUCCESS == va_status);
449 y_src = ctx->frame_data_buffer;
450 u_src = ctx->frame_data_buffer + y_size; /* UV offset for NV12 */
451 v_src = ctx->frame_data_buffer + y_size + u_size;
453 y_dst = surface_p + surface_image.offsets[0];
454 u_dst = surface_p + surface_image.offsets[1]; /* UV offset for NV12 */
455 v_dst = surface_p + surface_image.offsets[2];
458 for (row = 0; row < surface_image.height; row++) {
459 memcpy(y_dst, y_src, surface_image.width);
460 y_dst += surface_image.pitches[0];
464 if (surface_image.format.fourcc == VA_FOURCC_NV12) { /* UV plane */
465 for (row = 0; row < surface_image.height / 2; row++) {
466 for (col = 0; col < surface_image.width / 2; col++) {
467 u_dst[col * 2] = u_src[col];
468 u_dst[col * 2 + 1] = v_src[col];
471 u_dst += surface_image.pitches[1];
472 u_src += (ctx->width / 2);
473 v_src += (ctx->width / 2);
476 for (row = 0; row < surface_image.height / 2; row++) {
477 for (col = 0; col < surface_image.width / 2; col++) {
478 u_dst[col] = u_src[col];
479 v_dst[col] = v_src[col];
482 u_dst += surface_image.pitches[1];
483 v_dst += surface_image.pitches[2];
484 u_src += (ctx->width / 2);
485 v_src += (ctx->width / 2);
489 vaUnmapBuffer(ctx->va_dpy, surface_image.buf);
490 vaDestroyImage(ctx->va_dpy, surface_image.image_id);
496 mpeg2enc_exit(struct mpeg2enc_context *ctx, int exit_code)
498 if (ctx->frame_data_buffer) {
499 free(ctx->frame_data_buffer);
500 ctx->frame_data_buffer = NULL;
519 fprintf(stderr, "Usage: %s --help\n", program);
520 fprintf(stderr, "\t--help print this message\n");
521 fprintf(stderr, "Usage: %s <width> <height> <ifile> <ofile> [options]\n", program);
522 fprintf(stderr, "\t<width> specifies the frame width\n");
523 fprintf(stderr, "\t<height> specifies the frame height\n");
524 fprintf(stderr, "\t<ifile> specifies the I420/IYUV YUV file\n");
525 fprintf(stderr, "\t<ofile> specifies the encoded MPEG-2 file\n");
526 fprintf(stderr, "where options include:\n");
527 fprintf(stderr, "\t--cqp <QP> const qp mode with specified <QP>\n");
528 fprintf(stderr, "\t--fps <FPS> specify the frame rate\n");
529 fprintf(stderr, "\t--mode <MODE> specify the mode 0 (I), 1 (I/P) and 2 (I/P/B)\n");
530 fprintf(stderr, "\t--profile <PROFILE> specify the profile 0(Simple), or 1(Main, default)\n");
531 fprintf(stderr, "\t--level <LEVEL> specify the level 0(Low), 1(Main, default) or 2(High)\n");
535 parse_args(struct mpeg2enc_context *ctx, int argc, char **argv)
538 int option_index = 0;
540 static struct option long_options[] = {
541 {"help", no_argument, 0, 'h'},
542 {"cqp", required_argument, 0, 'c'},
543 {"fps", required_argument, 0, 'f'},
544 {"mode", required_argument, 0, 'm'},
545 {"profile", required_argument, 0, 'p'},
546 {"level", required_argument, 0, 'l'},
550 if ((argc == 2 && strcmp(argv[1], "--help") == 0) ||
554 ctx->width = atoi(argv[1]);
555 ctx->height = atoi(argv[2]);
557 if (ctx->width <= 0 || ctx->height <= 0) {
558 fprintf(stderr, "<width> and <height> must be greater than 0\n");
562 ctx->ifp = fopen(argv[3], "rb");
564 if (ctx->ifp == NULL) {
565 fprintf(stderr, "Can't open the input file\n");
569 fseek(ctx->ifp, 0l, SEEK_END);
570 file_size = ftell(ctx->ifp);
571 ctx->frame_size = ctx->width * ctx->height * 3 / 2;
573 if ((file_size < ctx->frame_size) ||
574 (file_size % ctx->frame_size)) {
575 fprintf(stderr, "The input file size %ld isn't a multiple of the frame size %d\n", file_size, ctx->frame_size);
579 ctx->num_pictures = file_size / ctx->frame_size;
580 fseek(ctx->ifp, 0l, SEEK_SET);
582 ctx->ofp = fopen(argv[4], "wb");
584 if (ctx->ofp == NULL) {
585 fprintf(stderr, "Can't create the output file\n");
592 ctx->rate_control_mode = VA_RC_CQP;
593 ctx->mode = MPEG2_MODE_IP;
594 ctx->profile = VAProfileMPEG2Main;
595 ctx->level = MPEG2_LEVEL_MAIN;
599 while((c = getopt_long(argc, argv,
602 &option_index)) != -1) {
607 /* only support q_scale_type = 0 */
608 if (tmp > 62 || tmp < 2) {
609 fprintf(stderr, "Warning: QP must be in [2, 62]\n");
618 ctx->qp = tmp & 0xFE;
619 ctx->rate_control_mode = VA_RC_CQP;
627 fprintf(stderr, "Warning: FPS must be greater than 0\n");
631 ctx->rate_control_mode = VA_RC_CBR;
638 if (tmp < MPEG2_MODE_I || tmp > MPEG2_MODE_IPB)
639 fprintf(stderr, "Waning: MODE must be 0, 1, or 2\n");
648 if (tmp < 0 || tmp > 1)
649 fprintf(stderr, "Waning: PROFILE must be 0 or 1\n");
651 ctx->profile = mpeg2_va_profiles[tmp];
658 if (tmp < MPEG2_LEVEL_LOW || tmp > MPEG2_LEVEL_HIGH)
659 fprintf(stderr, "Waning: LEVEL must be 0, 1, or 2\n");
666 fprintf(stderr, "Error: unkown command options\n");
678 mpeg2enc_exit(ctx, 1);
685 mpeg2enc_init_sequence_parameter(struct mpeg2enc_context *ctx,
686 VAEncSequenceParameterBufferMPEG2 *seq_param)
688 int profile = 4, level = 8;
690 switch (ctx->profile) {
691 case VAProfileMPEG2Simple:
695 case VAProfileMPEG2Main:
704 switch (ctx->level) {
705 case MPEG2_LEVEL_LOW:
709 case MPEG2_LEVEL_MAIN:
713 case MPEG2_LEVEL_HIGH:
722 seq_param->intra_period = ctx->intra_period;
723 seq_param->ip_period = ctx->ip_period; /* FIXME: ??? */
724 seq_param->picture_width = ctx->width;
725 seq_param->picture_height = ctx->height;
727 if (ctx->bit_rate > 0)
728 seq_param->bits_per_second = 1024 * ctx->bit_rate; /* use kbps as input */
730 seq_param->bits_per_second = 0x3FFFF * 400;
732 seq_param->frame_rate = ctx->fps;
733 seq_param->aspect_ratio_information = 1;
734 seq_param->vbv_buffer_size = 3; /* B = 16 * 1024 * vbv_buffer_size */
736 seq_param->sequence_extension.bits.profile_and_level_indication = profile << 4 | level;
737 seq_param->sequence_extension.bits.progressive_sequence = 1; /* progressive frame-pictures */
738 seq_param->sequence_extension.bits.chroma_format = CHROMA_FORMAT_420; /* 4:2:0 */
739 seq_param->sequence_extension.bits.low_delay = 0; /* FIXME */
740 seq_param->sequence_extension.bits.frame_rate_extension_n = 0;
741 seq_param->sequence_extension.bits.frame_rate_extension_d = 0;
743 seq_param->gop_header.bits.time_code = (1 << 12); /* bit12: marker_bit */
744 seq_param->gop_header.bits.closed_gop = 0;
745 seq_param->gop_header.bits.broken_link = 0;
749 mpeg2enc_init_picture_parameter(struct mpeg2enc_context *ctx,
750 VAEncPictureParameterBufferMPEG2 *pic_param)
752 pic_param->forward_reference_picture = VA_INVALID_ID;
753 pic_param->backward_reference_picture = VA_INVALID_ID;
754 pic_param->reconstructed_picture = VA_INVALID_ID;
755 pic_param->coded_buf = VA_INVALID_ID;
756 pic_param->picture_type = VAEncPictureTypeIntra;
758 pic_param->temporal_reference = 0;
759 pic_param->f_code[0][0] = 0xf;
760 pic_param->f_code[0][1] = 0xf;
761 pic_param->f_code[1][0] = 0xf;
762 pic_param->f_code[1][1] = 0xf;
764 pic_param->picture_coding_extension.bits.intra_dc_precision = 0; /* 8bits */
765 pic_param->picture_coding_extension.bits.picture_structure = 3; /* frame picture */
766 pic_param->picture_coding_extension.bits.top_field_first = 0;
767 pic_param->picture_coding_extension.bits.frame_pred_frame_dct = 1; /* FIXME */
768 pic_param->picture_coding_extension.bits.concealment_motion_vectors = 0;
769 pic_param->picture_coding_extension.bits.q_scale_type = 0;
770 pic_param->picture_coding_extension.bits.intra_vlc_format = 0;
771 pic_param->picture_coding_extension.bits.alternate_scan = 0;
772 pic_param->picture_coding_extension.bits.repeat_first_field = 0;
773 pic_param->picture_coding_extension.bits.progressive_frame = 1;
774 pic_param->picture_coding_extension.bits.composite_display_flag = 0;
776 pic_param->user_data_length = 0;
780 mpeg2enc_alloc_va_resources(struct mpeg2enc_context *ctx)
782 VAEntrypoint *entrypoint_list;
783 VAConfigAttrib attrib_list[2];
785 int max_entrypoints, num_entrypoints, entrypoint;
786 int major_ver, minor_ver;
788 ctx->va_dpy = va_open_display();
789 va_status = vaInitialize(ctx->va_dpy,
792 CHECK_VASTATUS(va_status, "vaInitialize");
794 max_entrypoints = vaMaxNumEntrypoints(ctx->va_dpy);
795 entrypoint_list = malloc(max_entrypoints * sizeof(VAEntrypoint));
796 vaQueryConfigEntrypoints(ctx->va_dpy,
801 for (entrypoint = 0; entrypoint < num_entrypoints; entrypoint++) {
802 if (entrypoint_list[entrypoint] == VAEntrypointEncSlice)
806 free(entrypoint_list);
808 if (entrypoint == num_entrypoints) {
809 /* not find Slice entry point */
813 /* find out the format for the render target, and rate control mode */
814 attrib_list[0].type = VAConfigAttribRTFormat;
815 attrib_list[1].type = VAConfigAttribRateControl;
816 vaGetConfigAttributes(ctx->va_dpy,
818 VAEntrypointEncSlice,
822 if ((attrib_list[0].value & VA_RT_FORMAT_YUV420) == 0) {
823 /* not find desired YUV420 RT format */
827 if ((attrib_list[1].value & ctx->rate_control_mode) == 0) {
828 /* Can't find matched RC mode */
829 fprintf(stderr, "RC mode %d isn't found, exit\n", ctx->rate_control_mode);
833 attrib_list[0].value = VA_RT_FORMAT_YUV420; /* set to desired RT format */
834 attrib_list[1].value = ctx->rate_control_mode; /* set to desired RC mode */
836 va_status = vaCreateConfig(ctx->va_dpy,
838 VAEntrypointEncSlice,
842 CHECK_VASTATUS(va_status, "vaCreateConfig");
844 /* Create a context for this decode pipe */
845 va_status = vaCreateContext(ctx->va_dpy,
853 CHECK_VASTATUS(va_status, "vaCreateContext");
855 va_status = vaCreateSurfaces(ctx->va_dpy,
863 CHECK_VASTATUS(va_status, "vaCreateSurfaces");
867 mpeg2enc_init(struct mpeg2enc_context *ctx)
871 ctx->frame_data_buffer = (unsigned char *)malloc(ctx->frame_size);
872 ctx->seq_param_buf_id = VA_INVALID_ID;
873 ctx->pic_param_buf_id = VA_INVALID_ID;
874 ctx->packed_seq_header_param_buf_id = VA_INVALID_ID;
875 ctx->packed_seq_buf_id = VA_INVALID_ID;
876 ctx->packed_pic_header_param_buf_id = VA_INVALID_ID;
877 ctx->packed_pic_buf_id = VA_INVALID_ID;
878 ctx->codedbuf_buf_id = VA_INVALID_ID;
879 ctx->codedbuf_i_size = ctx->frame_size;
880 ctx->codedbuf_pb_size = 0;
881 ctx->next_display_order = 0;
882 ctx->next_type = VAEncPictureTypeIntra;
884 if (ctx->mode == MPEG2_MODE_I) {
885 ctx->intra_period = 1;
887 } else if (ctx->mode == MPEG2_MODE_IP) {
888 ctx->intra_period = 16;
891 ctx->intra_period = 16;
895 ctx->next_bframes = ctx->ip_period;
897 ctx->new_sequence = 1;
898 ctx->new_gop_header = 1;
899 ctx->gop_header_in_display_order = 0;
903 for (i = 0; i < MAX_SLICES; i++) {
904 ctx->slice_param_buf_id[i] = VA_INVALID_ID;
907 mpeg2enc_init_sequence_parameter(ctx, &ctx->seq_param);
908 mpeg2enc_init_picture_parameter(ctx, &ctx->pic_param);
909 mpeg2enc_alloc_va_resources(ctx);
912 ctx->current_input_surface = SID_INPUT_PICTURE_0;
913 ctx->current_upload_surface = SID_INPUT_PICTURE_1;
914 ctx->upload_thread_value = pthread_create(&ctx->upload_thread_id,
916 upload_yuv_to_surface,
921 mpeg2enc_time_code(VAEncSequenceParameterBufferMPEG2 *seq_param,
924 int fps = (int)(seq_param->frame_rate + 0.5);
926 int time_code_pictures, time_code_seconds, time_code_minutes, time_code_hours;
927 int drop_frame_flag = 0;
931 time_code_seconds = num_frames / fps;
932 time_code_pictures = num_frames % fps;
933 time_code |= time_code_pictures;
935 time_code_minutes = time_code_seconds / 60;
936 time_code_seconds = time_code_seconds % 60;
937 time_code |= (time_code_seconds << 6);
939 time_code_hours = time_code_minutes / 60;
940 time_code_minutes = time_code_minutes % 60;
942 time_code |= (1 << 12); /* marker_bit */
943 time_code |= (time_code_minutes << 13);
945 time_code_hours = time_code_hours % 24;
946 time_code |= (time_code_hours << 19);
948 time_code |= (drop_frame_flag << 24);
957 mpeg2enc_update_sequence_parameter(struct mpeg2enc_context *ctx,
958 VAEncPictureType picture_type,
962 VAEncSequenceParameterBufferMPEG2 *seq_param = &ctx->seq_param;
964 /* update the time_code info for the new GOP */
965 if (ctx->new_gop_header) {
966 seq_param->gop_header.bits.time_code = mpeg2enc_time_code(seq_param, display_order);
971 mpeg2enc_update_picture_parameter(struct mpeg2enc_context *ctx,
972 VAEncPictureType picture_type,
976 VAEncPictureParameterBufferMPEG2 *pic_param = &ctx->pic_param;
978 pic_param->picture_type = picture_type;
979 pic_param->temporal_reference = (display_order - ctx->gop_header_in_display_order) & 0x3FF;
980 pic_param->reconstructed_picture = surface_ids[SID_RECON_PICTURE];
981 pic_param->forward_reference_picture = surface_ids[SID_REFERENCE_PICTURE_L0];
982 pic_param->backward_reference_picture = surface_ids[SID_REFERENCE_PICTURE_L1];
984 if (pic_param->picture_type == VAEncPictureTypeIntra) {
985 pic_param->f_code[0][0] = 0xf;
986 pic_param->f_code[0][1] = 0xf;
987 pic_param->f_code[1][0] = 0xf;
988 pic_param->f_code[1][1] = 0xf;
989 pic_param->forward_reference_picture = VA_INVALID_SURFACE;
990 pic_param->backward_reference_picture = VA_INVALID_SURFACE;
992 } else if (pic_param->picture_type == VAEncPictureTypePredictive) {
993 pic_param->f_code[0][0] = 0x1;
994 pic_param->f_code[0][1] = 0x1;
995 pic_param->f_code[1][0] = 0xf;
996 pic_param->f_code[1][1] = 0xf;
997 pic_param->forward_reference_picture = surface_ids[SID_REFERENCE_PICTURE_L0];
998 pic_param->backward_reference_picture = VA_INVALID_SURFACE;
999 } else if (pic_param->picture_type == VAEncPictureTypeBidirectional) {
1000 pic_param->f_code[0][0] = 0x1;
1001 pic_param->f_code[0][1] = 0x1;
1002 pic_param->f_code[1][0] = 0x1;
1003 pic_param->f_code[1][1] = 0x1;
1004 pic_param->forward_reference_picture = surface_ids[SID_REFERENCE_PICTURE_L0];
1005 pic_param->backward_reference_picture = surface_ids[SID_REFERENCE_PICTURE_L1];
1012 mpeg2enc_update_picture_parameter_buffer(struct mpeg2enc_context *ctx,
1013 VAEncPictureType picture_type,
1017 VAEncPictureParameterBufferMPEG2 *pic_param = &ctx->pic_param;
1020 /* update the coded buffer id */
1021 pic_param->coded_buf = ctx->codedbuf_buf_id;
1022 va_status = vaCreateBuffer(ctx->va_dpy,
1024 VAEncPictureParameterBufferType,
1028 &ctx->pic_param_buf_id);
1029 CHECK_VASTATUS(va_status, "vaCreateBuffer");
1033 mpeg2enc_update_slice_parameter(struct mpeg2enc_context *ctx, VAEncPictureType picture_type)
1035 VAEncSequenceParameterBufferMPEG2 *seq_param;
1036 VAEncPictureParameterBufferMPEG2 *pic_param;
1037 VAEncSliceParameterBufferMPEG2 *slice_param;
1039 int i, width_in_mbs, height_in_mbs;
1041 pic_param = &ctx->pic_param;
1042 assert(pic_param->picture_coding_extension.bits.q_scale_type == 0);
1044 seq_param = &ctx->seq_param;
1045 width_in_mbs = (seq_param->picture_width + 15) / 16;
1046 height_in_mbs = (seq_param->picture_height + 15) / 16;
1047 ctx->num_slice_groups = 1;
1049 for (i = 0; i < height_in_mbs; i++) {
1050 slice_param = &ctx->slice_param[i];
1051 slice_param->macroblock_address = i * width_in_mbs;
1052 slice_param->num_macroblocks = width_in_mbs;
1053 slice_param->is_intra_slice = (picture_type == VAEncPictureTypeIntra);
1054 slice_param->quantiser_scale_code = ctx->qp / 2;
1057 va_status = vaCreateBuffer(ctx->va_dpy,
1059 VAEncSliceParameterBufferType,
1060 sizeof(*slice_param),
1063 ctx->slice_param_buf_id);
1064 CHECK_VASTATUS(va_status, "vaCreateBuffer");;
1068 begin_picture(struct mpeg2enc_context *ctx,
1071 VAEncPictureType picture_type)
1075 VAEncPackedHeaderParameterBuffer packed_header_param_buffer;
1076 unsigned int length_in_bits;
1077 unsigned char *packed_seq_buffer = NULL, *packed_pic_buffer = NULL;
1079 if (ctx->upload_thread_value != 0) {
1080 fprintf(stderr, "FATAL error!!!\n");
1084 pthread_join(ctx->upload_thread_id, NULL);
1086 ctx->upload_thread_value = -1;
1087 tmp = ctx->current_input_surface;
1088 ctx->current_input_surface = ctx->current_upload_surface;
1089 ctx->current_upload_surface = tmp;
1091 mpeg2enc_update_sequence_parameter(ctx, picture_type, coded_order, display_order);
1092 mpeg2enc_update_picture_parameter(ctx, picture_type, coded_order, display_order);
1094 if (ctx->new_sequence || ctx->new_gop_header) {
1095 assert(picture_type == VAEncPictureTypeIntra);
1096 length_in_bits = build_packed_seq_buffer(ctx, &ctx->seq_param, &packed_seq_buffer);
1097 packed_header_param_buffer.type = VAEncPackedHeaderMPEG2_SPS;
1098 packed_header_param_buffer.has_emulation_bytes = 0;
1099 packed_header_param_buffer.bit_length = length_in_bits;
1100 va_status = vaCreateBuffer(ctx->va_dpy,
1102 VAEncPackedHeaderParameterBufferType,
1103 sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
1104 &ctx->packed_seq_header_param_buf_id);
1105 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1107 va_status = vaCreateBuffer(ctx->va_dpy,
1109 VAEncPackedHeaderDataBufferType,
1110 (length_in_bits + 7) / 8, 1, packed_seq_buffer,
1111 &ctx->packed_seq_buf_id);
1112 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1114 free(packed_seq_buffer);
1117 length_in_bits = build_packed_pic_buffer(&ctx->seq_param, &ctx->pic_param, &packed_pic_buffer);
1118 packed_header_param_buffer.type = VAEncPackedHeaderMPEG2_PPS;
1119 packed_header_param_buffer.has_emulation_bytes = 0;
1120 packed_header_param_buffer.bit_length = length_in_bits;
1122 va_status = vaCreateBuffer(ctx->va_dpy,
1124 VAEncPackedHeaderParameterBufferType,
1125 sizeof(packed_header_param_buffer), 1, &packed_header_param_buffer,
1126 &ctx->packed_pic_header_param_buf_id);
1127 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1129 va_status = vaCreateBuffer(ctx->va_dpy,
1131 VAEncPackedHeaderDataBufferType,
1132 (length_in_bits + 7) / 8, 1, packed_pic_buffer,
1133 &ctx->packed_pic_buf_id);
1134 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1136 free(packed_pic_buffer);
1138 /* sequence parameter set */
1139 VAEncSequenceParameterBufferMPEG2 *seq_param = &ctx->seq_param;
1140 va_status = vaCreateBuffer(ctx->va_dpy,
1142 VAEncSequenceParameterBufferType,
1146 &ctx->seq_param_buf_id);
1147 CHECK_VASTATUS(va_status,"vaCreateBuffer");;
1149 /* slice parameter */
1150 mpeg2enc_update_slice_parameter(ctx, picture_type);
1156 mpeg2enc_render_picture(struct mpeg2enc_context *ctx)
1159 VABufferID va_buffers[16];
1160 unsigned int num_va_buffers = 0;
1162 va_buffers[num_va_buffers++] = ctx->seq_param_buf_id;
1163 va_buffers[num_va_buffers++] = ctx->pic_param_buf_id;
1165 if (ctx->packed_seq_header_param_buf_id != VA_INVALID_ID)
1166 va_buffers[num_va_buffers++] = ctx->packed_seq_header_param_buf_id;
1168 if (ctx->packed_seq_buf_id != VA_INVALID_ID)
1169 va_buffers[num_va_buffers++] = ctx->packed_seq_buf_id;
1171 if (ctx->packed_pic_header_param_buf_id != VA_INVALID_ID)
1172 va_buffers[num_va_buffers++] = ctx->packed_pic_header_param_buf_id;
1174 if (ctx->packed_pic_buf_id != VA_INVALID_ID)
1175 va_buffers[num_va_buffers++] = ctx->packed_pic_buf_id;
1177 va_status = vaBeginPicture(ctx->va_dpy,
1179 surface_ids[ctx->current_input_surface]);
1180 CHECK_VASTATUS(va_status,"vaBeginPicture");
1182 va_status = vaRenderPicture(ctx->va_dpy,
1186 CHECK_VASTATUS(va_status,"vaRenderPicture");
1188 va_status = vaRenderPicture(ctx->va_dpy,
1190 &ctx->slice_param_buf_id[0],
1191 ctx->num_slice_groups);
1192 CHECK_VASTATUS(va_status,"vaRenderPicture");
1194 va_status = vaEndPicture(ctx->va_dpy, ctx->context_id);
1195 CHECK_VASTATUS(va_status,"vaEndPicture");
1201 mpeg2enc_destroy_buffers(struct mpeg2enc_context *ctx, VABufferID *va_buffers, unsigned int num_va_buffers)
1206 for (i = 0; i < num_va_buffers; i++) {
1207 if (va_buffers[i] != VA_INVALID_ID) {
1208 va_status = vaDestroyBuffer(ctx->va_dpy, va_buffers[i]);
1209 CHECK_VASTATUS(va_status,"vaDestroyBuffer");
1210 va_buffers[i] = VA_INVALID_ID;
1218 end_picture(struct mpeg2enc_context *ctx, VAEncPictureType picture_type, int next_is_bpic)
1222 /* Prepare for next picture */
1223 tempID = surface_ids[SID_RECON_PICTURE];
1225 if (picture_type != VAEncPictureTypeBidirectional) {
1227 surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L1];
1228 surface_ids[SID_REFERENCE_PICTURE_L1] = tempID;
1230 surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L0];
1231 surface_ids[SID_REFERENCE_PICTURE_L0] = tempID;
1234 if (!next_is_bpic) {
1235 surface_ids[SID_RECON_PICTURE] = surface_ids[SID_REFERENCE_PICTURE_L0];
1236 surface_ids[SID_REFERENCE_PICTURE_L0] = surface_ids[SID_REFERENCE_PICTURE_L1];
1237 surface_ids[SID_REFERENCE_PICTURE_L1] = tempID;
1241 mpeg2enc_destroy_buffers(ctx, &ctx->seq_param_buf_id, 1);
1242 mpeg2enc_destroy_buffers(ctx, &ctx->pic_param_buf_id, 1);
1243 mpeg2enc_destroy_buffers(ctx, &ctx->packed_seq_header_param_buf_id, 1);
1244 mpeg2enc_destroy_buffers(ctx, &ctx->packed_seq_buf_id, 1);
1245 mpeg2enc_destroy_buffers(ctx, &ctx->packed_pic_header_param_buf_id, 1);
1246 mpeg2enc_destroy_buffers(ctx, &ctx->packed_pic_buf_id, 1);
1247 mpeg2enc_destroy_buffers(ctx, &ctx->slice_param_buf_id[0], ctx->num_slice_groups);
1248 mpeg2enc_destroy_buffers(ctx, &ctx->codedbuf_buf_id, 1);
1249 memset(ctx->slice_param, 0, sizeof(ctx->slice_param));
1250 ctx->num_slice_groups = 0;
1254 store_coded_buffer(struct mpeg2enc_context *ctx, VAEncPictureType picture_type)
1256 VACodedBufferSegment *coded_buffer_segment;
1257 unsigned char *coded_mem;
1258 int slice_data_length;
1260 VASurfaceStatus surface_status;
1263 va_status = vaSyncSurface(ctx->va_dpy, surface_ids[ctx->current_input_surface]);
1264 CHECK_VASTATUS(va_status,"vaSyncSurface");
1267 va_status = vaQuerySurfaceStatus(ctx->va_dpy, surface_ids[ctx->current_input_surface], &surface_status);
1268 CHECK_VASTATUS(va_status,"vaQuerySurfaceStatus");
1270 va_status = vaMapBuffer(ctx->va_dpy, ctx->codedbuf_buf_id, (void **)(&coded_buffer_segment));
1271 CHECK_VASTATUS(va_status,"vaMapBuffer");
1272 coded_mem = coded_buffer_segment->buf;
1274 if (coded_buffer_segment->status & VA_CODED_BUF_STATUS_SLICE_OVERFLOW_MASK) {
1275 if (picture_type == VAEncPictureTypeIntra)
1276 ctx->codedbuf_i_size *= 2;
1278 ctx->codedbuf_pb_size *= 2;
1280 vaUnmapBuffer(ctx->va_dpy, ctx->codedbuf_buf_id);
1284 slice_data_length = coded_buffer_segment->size;
1287 w_items = fwrite(coded_mem, slice_data_length, 1, ctx->ofp);
1288 } while (w_items != 1);
1290 if (picture_type == VAEncPictureTypeIntra) {
1291 if (ctx->codedbuf_i_size > slice_data_length * 3 / 2) {
1292 ctx->codedbuf_i_size = slice_data_length * 3 / 2;
1295 if (ctx->codedbuf_pb_size < slice_data_length) {
1296 ctx->codedbuf_pb_size = slice_data_length;
1299 if (ctx->codedbuf_pb_size > slice_data_length * 3 / 2) {
1300 ctx->codedbuf_pb_size = slice_data_length * 3 / 2;
1304 vaUnmapBuffer(ctx->va_dpy, ctx->codedbuf_buf_id);
1310 encode_picture(struct mpeg2enc_context *ctx,
1313 VAEncPictureType picture_type,
1315 int next_display_order)
1318 int ret = 0, codedbuf_size;
1320 begin_picture(ctx, coded_order, display_order, picture_type);
1323 /* upload YUV data to VA surface for next frame */
1324 if (next_display_order >= ctx->num_pictures)
1325 next_display_order = ctx->num_pictures - 1;
1327 fseek(ctx->ifp, ctx->frame_size * next_display_order, SEEK_SET);
1328 ctx->upload_thread_value = pthread_create(&ctx->upload_thread_id,
1330 upload_yuv_to_surface,
1335 mpeg2enc_destroy_buffers(ctx, &ctx->codedbuf_buf_id, 1);
1336 mpeg2enc_destroy_buffers(ctx, &ctx->pic_param_buf_id, 1);
1339 if (VAEncPictureTypeIntra == picture_type) {
1340 codedbuf_size = ctx->codedbuf_i_size;
1342 codedbuf_size = ctx->codedbuf_pb_size;
1346 va_status = vaCreateBuffer(ctx->va_dpy,
1348 VAEncCodedBufferType,
1349 codedbuf_size, 1, NULL,
1350 &ctx->codedbuf_buf_id);
1351 CHECK_VASTATUS(va_status,"vaCreateBuffer");
1353 /* picture parameter set */
1354 mpeg2enc_update_picture_parameter_buffer(ctx, picture_type, coded_order, display_order);
1356 mpeg2enc_render_picture(ctx);
1358 ret = store_coded_buffer(ctx, picture_type);
1361 end_picture(ctx, picture_type, next_is_bpic);
1365 update_next_frame_info(struct mpeg2enc_context *ctx,
1366 VAEncPictureType curr_type,
1367 int curr_coded_order,
1368 int curr_display_order)
1370 if (((curr_coded_order + 1) % ctx->intra_period) == 0) {
1371 ctx->next_type = VAEncPictureTypeIntra;
1372 ctx->next_display_order = curr_coded_order + 1;
1377 if (curr_type == VAEncPictureTypeIntra) {
1378 assert(curr_display_order == curr_coded_order);
1379 ctx->next_type = VAEncPictureTypePredictive;
1380 ctx->next_bframes = ctx->ip_period;
1381 ctx->next_display_order = curr_display_order + ctx->next_bframes + 1;
1382 } else if (curr_type == VAEncPictureTypePredictive) {
1383 if (ctx->ip_period == 0) {
1384 assert(curr_display_order == curr_coded_order);
1385 ctx->next_type = VAEncPictureTypePredictive;
1386 ctx->next_display_order = curr_display_order + 1;
1388 ctx->next_type = VAEncPictureTypeBidirectional;
1389 ctx->next_display_order = curr_display_order - ctx->next_bframes;
1390 ctx->next_bframes--;
1392 } else if (curr_type == VAEncPictureTypeBidirectional) {
1393 if (ctx->next_bframes == 0) {
1394 ctx->next_type = VAEncPictureTypePredictive;
1395 ctx->next_bframes = ctx->ip_period;
1396 ctx->next_display_order = curr_display_order + ctx->next_bframes + 2;
1398 ctx->next_type = VAEncPictureTypeBidirectional;
1399 ctx->next_display_order = curr_display_order + 1;
1400 ctx->next_bframes--;
1404 if (ctx->next_display_order >= ctx->num_pictures) {
1405 int rtmp = ctx->next_display_order - (ctx->num_pictures - 1);
1406 ctx->next_display_order = ctx->num_pictures - 1;
1407 ctx->next_bframes -= rtmp;
1412 mpeg2enc_run(struct mpeg2enc_context *ctx)
1414 int display_order = 0, coded_order = 0;
1415 VAEncPictureType type;
1417 ctx->new_sequence = 1;
1418 ctx->new_gop_header = 1;
1419 ctx->gop_header_in_display_order = display_order;
1421 while (coded_order < ctx->num_pictures) {
1422 type = ctx->next_type;
1423 display_order = ctx->next_display_order;
1424 /* follow the IPBxxBPBxxB mode */
1425 update_next_frame_info(ctx, type, coded_order, display_order);
1430 ctx->next_type == VAEncPictureTypeBidirectional,
1431 ctx->next_display_order);
1433 /* update gop_header */
1434 ctx->new_sequence = 0;
1435 ctx->new_gop_header = ctx->next_type == VAEncPictureTypeIntra;
1437 if (ctx->new_gop_header)
1438 ctx->gop_header_in_display_order += ctx->intra_period;
1442 fprintf(stderr, "\r %d/%d ...", coded_order, ctx->num_pictures);
1451 mpeg2enc_release_va_resources(struct mpeg2enc_context *ctx)
1453 vaDestroySurfaces(ctx->va_dpy, surface_ids, SID_NUMBER);
1454 vaDestroyContext(ctx->va_dpy, ctx->context_id);
1455 vaDestroyConfig(ctx->va_dpy, ctx->config_id);
1456 vaTerminate(ctx->va_dpy);
1457 va_close_display(ctx->va_dpy);
1461 mpeg2enc_end(struct mpeg2enc_context *ctx)
1463 pthread_join(ctx->upload_thread_id, NULL);
1464 mpeg2enc_release_va_resources(ctx);
1468 main(int argc, char *argv[])
1470 struct mpeg2enc_context ctx;
1471 struct timeval tpstart, tpend;
1474 gettimeofday(&tpstart, NULL);
1476 memset(&ctx, 0, sizeof(ctx));
1477 parse_args(&ctx, argc, argv);
1478 mpeg2enc_init(&ctx);
1482 gettimeofday(&tpend, NULL);
1483 timeuse = 1000000 * (tpend.tv_sec - tpstart.tv_sec) + tpend.tv_usec - tpstart.tv_usec;
1485 fprintf(stderr, "\ndone!\n");
1486 fprintf(stderr, "encode %d frames in %f secondes, FPS is %.1f\n", ctx.num_pictures, timeuse, ctx.num_pictures / timeuse);
1488 mpeg2enc_exit(&ctx, 0);