📄 mp4emain.c
字号:
/******************************************************************************// INTEL CORPORATION PROPRIETARY INFORMATION// This software is supplied under the terms of a license agreement or// nondisclosure agreement with Intel Corporation and may not be copied// or disclosed except in accordance with the terms of that agreement.// Copyright (c) 2003 Intel Corporation. All Rights Reserved.//// Description: Encoder main entry function of MPEG-4 video encoder sample// code for Intel(R) Integrated Performance Primitives.// Functions List:// main()// print_help_infor()// parse_command_line_mpeg4()******************************************************************************/#include <memory.h>#include <string.h>#include <stdlib.h>#include "sampmp4.h"/*****************************************************************************// Name: print_help_infor// Description: Print help information about the encoder usage.//// Returns:// None*****************************************************************************/void print_help_infor(){ printf("Usage: MPEG4Decoder.exe infile outfile [options]\n" " infile : .yuv input filename\n" " outfile: .cmp output filename\n" " options: -vi xx vol verison id;\n" " (xx = 1 or 2, default = 2)\n" " -fw xx frame width in pixel;\n" " (default = 176)\n" " -fh xx frame height in pixel;\n" " (default = 144)\n" " -fr xx frame rate in frames per second;\n" " (default = 25)\n" " -cr xx source frame color format;\n" " (xx = 0, default = 0 for YUV4:1:1)\n" " -qt xx quantisation method;\n" " (xx = 1 or 2, default = 1)\n" " -qp xx initial quantiser step;\n" " (xx = 1 - 31, default = 16)\n" " -th xx intra dc vlc threshold;\n" " (xx = 0 - 7, default = 0)\n" " -iv xx ivop interval in number of pvops in between;\n" " (default = -1 for only the 1st frame is ivop)\n" " -sr xx search range for motion estimation in pixel;\n" " (xx = 1 - 1023, default = 15)\n" " -us xx use src data or not for motion estimation;\n" " (xx = 0 or 1, default = 0)\n" " -hp for help information;\n"); return;}/******************************************************************************// Name: parse_command_line_mpeg4// Description: Parse command line to get encoder configuration options.//// Input Arguments:// argc Number of arguments passed from main entry// argv Array of arguments passed from main entry// Output Arguments:// enc_config Pointer to structure to store encoder configuration// parameters.// fpin_ptr Pointer to the input .cmp file stream handle to be opened// fpout_ptr Pointer to the output .yuv file stream handle to be opened//// Returns:// None******************************************************************************/sample_status parse_command_line_mpeg4(int argc, char** argv, mp4_enc_params *enc_config, FILE **fpin_ptr, FILE **fpout_ptr){ FILE *fpin = NULL, *fpout = NULL; int i, tmp; /* argument check */ if (3 > argc) { print_help_infor(); return SAMPLE_STATUS_BADARG_ERR; } /* open input .yuv file handle */ fpin = fopen(argv[1], "rb"); if (!fpin) { printf("Can't open input .yuv file!\n"); return SAMPLE_STATUS_BADARG_ERR; } else { *fpin_ptr = fpin; } /* open output .cmp file handle */ fpout = fopen(argv[2], "wb"); if (!fpout) { printf("Can't open output .cmp file!\n"); return SAMPLE_STATUS_BADARG_ERR; } else { *fpout_ptr = fpout; } /* fill default configurations */ enc_config->vol_verid = 2; enc_config->vol_width = 176; enc_config->vol_height = 144; enc_config->frame_rate = 25; enc_config->intra_dc_thr = 0; enc_config->ivop_interval = -1; enc_config->quant_type = Q_MPEG4; enc_config->vop_quant = 16; enc_config->search_range = 15; enc_config->use_src_me = 0; enc_config->color_format = SAMPLE_YCbCr411; /* parse encoder configuration params */ for (i = 3; i < argc; i++) { /* vol version id */ if (!strcmp(argv[i], "-vi")) { tmp = atoi(argv[++i]); if ((1 != tmp) && (2 != tmp)) { printf("Warning: vol version id must be equal to 1 or 2!\n"); return SAMPLE_STATUS_BADARG_ERR; } else { enc_config->vol_verid = tmp; } } else if (!strcmp(argv[i], "-fw")) { /* vol width */ tmp = atoi(argv[++i]); if (0 >= tmp) { printf("Warning: vol width must be positive!\n"); return SAMPLE_STATUS_BADARG_ERR; } else { enc_config->vol_width = tmp; } } else if (!strcmp(argv[i], "-fh")) { /* vol height */ tmp = atoi(argv[++i]); if (0 >= tmp) { printf("Warning: vol height must be positive!\n"); return SAMPLE_STATUS_BADARG_ERR; } else { enc_config->vol_height = tmp; } } else if(!strcmp(argv[i], "-cr")) { /* source color format */ tmp = atoi(argv[++i]); if (0 != tmp) { printf("Warning: source pic color format must be YUV4:1:1\n"); return SAMPLE_STATUS_NOTSUPPORTED_ERR; } else { enc_config->color_format = SAMPLE_YCbCr411; } } else if (!strcmp(argv[i], "-qt")) { /* quantisation method */ tmp = atoi(argv[++i]); if ((1 != tmp) && (2 != tmp)) { printf("Warning: quantisation type must be equal to 1 or 2!\n"); return SAMPLE_STATUS_BADARG_ERR; } else { enc_config->quant_type = (tmp == 1) ? Q_MPEG4 : Q_H263; } } else if (!strcmp(argv[i], "-qp")) { /* initial quantiser */ tmp = atoi(argv[++i]); if ((31 < tmp) || (1 > tmp)) { printf("Warning: quantiser must be within the range of [1,31]!\n"); return SAMPLE_STATUS_BADARG_ERR; } else { enc_config->vop_quant = tmp; } } else if (!strcmp(argv[i], "-th")) { /* intra dc vlc threshold */ tmp = atoi(argv[++i]); if ((7 < tmp) || (0 > tmp)) { printf("Warning: intra dc vlc threshold must be within the range of [0, 7]!\n"); return SAMPLE_STATUS_BADARG_ERR; } else { enc_config->intra_dc_thr = tmp; } } else if (!strcmp(argv[i], "-iv")) { /* ivop interval */ tmp = atoi(argv[++i]); enc_config->ivop_interval = (0 > tmp) ? -1 : tmp; } else if (!strcmp(argv[i], "-sr")) { /* search range in me */ tmp = atoi(argv[++i]); if ((1023 < tmp) || (1 > tmp)) { printf("Warning: search range must be within the range of [1, 1023]!\n"); return SAMPLE_STATUS_BADARG_ERR; } else { enc_config->search_range = tmp; } } else if (!strcmp(argv[i], "-us")) { /* use src raw data in me */ tmp = atoi(argv[++i]); if ((0 != tmp) && (1 != tmp)) { printf("Warning: whether use src data or not in motion estimation must be equal to 1 or 0!\n"); return SAMPLE_STATUS_BADARG_ERR; } else { enc_config->use_src_me = tmp; } } else if (!strcmp(argv[i], "-fr")) { /* frame rate */ enc_config->frame_rate = atoi(argv[++i]); } else { print_help_infor(); return SAMPLE_STATUS_ERR; } } /* count the number of frames in source file */ fseek(fpin, 0, SEEK_END); enc_config->num_frames = ftell(fpin) / (enc_config->vol_width * enc_config->vol_height * 3 / 2); if (-1 == enc_config->ivop_interval) { enc_config->ivop_interval = enc_config->num_frames; } fseek(fpin, 0, SEEK_SET); return SAMPLE_STATUS_NOERR;}/*****************************************************************************// Name: main// Description: Entry function for MPEG4 baseline encoder//// Returns: 0: encoding okay// others: encoding fail*****************************************************************************/int main(int argc, char** argv){ mp4_enc_state enc_state; mp4_enc_params enc_config; sample_bitstream stream_buf; sample_status ret_code; int buf_size, i; FILE *fpin; FILE *fpout; /* parse command line to obtain encoder configuration options */ ret_code = parse_command_line_mpeg4(argc, argv, &enc_config, &fpin, &fpout); if (SAMPLE_STATUS_NOERR != ret_code) { return -1; } /* init the encoder state */ ret_code = encoder_init_alloc_mpeg4(&enc_config, &enc_state); if (SAMPLE_STATUS_NOERR != ret_code) { return -1; } /* init output video bitstream buffer */ buf_size = enc_state.frame_dimension.width * enc_state.frame_dimension.height * 3; ret_code = init_output_video_buffer(&stream_buf, buf_size); if (SAMPLE_STATUS_NOERR != ret_code) { encoder_free_mpeg4(&enc_state); return -1; } /* create vo and vol header */ create_voandvol_header_mpeg4(&stream_buf, &enc_state); dump_video_buffer(&stream_buf, fpout); for (i = 0; i < enc_config.num_frames; i++) { load_extend_picture(fpin, enc_state.info_pic); ret_code = encode_mpeg4(&stream_buf, &enc_state); switch (ret_code) { case SAMPLE_STATUS_NOERR: printf("Frame encoding ... ... %d\r", i); dump_video_buffer(&stream_buf, fpout); break; default: i = enc_config.num_frames; break; } } if (SAMPLE_STATUS_NOERR == ret_code) { printf("Everything is OK!\n"); } release_output_video_buffer(&stream_buf); encoder_free_mpeg4(&enc_state); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -