📄 xvid_encraw.c
字号:
/***************************************************************************** * * XVID MPEG-4 VIDEO CODEC * - Console based test application - * * Copyright(C) 2002-2003 Christoph Lampert <gruel@web.de> * 2002-2003 Edouard Gomez <ed.gomez@free.fr> * 2003 Peter Ross <pross@xvid.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: xvid_encraw.c,v 1.1 2006/02/23 15:13:25 kevin-fu Exp $ * ****************************************************************************//***************************************************************************** * Application notes : * * A sequence of raw YUV I420 pics or YUV I420 PGM file format is encoded * The speed is measured and frames' PSNR are taken from core. * * The program is plain C and needs no libraries except for libxvidcore, * and maths-lib. * * Use ./xvid_encraw -help for a list of options * ************************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#ifndef WIN32#include <sys/time.h>#else#include <windows.h>#include <vfw.h>#include <time.h>#define XVID_AVI_INPUT#endif#include "xvid.h"#undef READ_PNM/***************************************************************************** * Quality presets ****************************************************************************/static const int motion_presets[] = { /* quality 0 */ 0, /* quality 1 */ XVID_ME_ADVANCEDDIAMOND16, /* quality 2 */ XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16, /* quality 3 */ XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8, /* quality 4 */ XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | XVID_ME_CHROMA_PVOP | XVID_ME_CHROMA_BVOP, /* quality 5 */ XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | XVID_ME_CHROMA_PVOP | XVID_ME_CHROMA_BVOP, /* quality 6 */ XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | XVID_ME_EXTSEARCH16 | XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | XVID_ME_EXTSEARCH8 | XVID_ME_CHROMA_PVOP | XVID_ME_CHROMA_BVOP,};#define ME_ELEMENTS (sizeof(motion_presets)/sizeof(motion_presets[0]))static const int vop_presets[] = { /* quality 0 */ 0, /* quality 1 */ 0, /* quality 2 */ XVID_VOP_HALFPEL, /* quality 3 */ XVID_VOP_HALFPEL | XVID_VOP_INTER4V, /* quality 4 */ XVID_VOP_HALFPEL | XVID_VOP_INTER4V, /* quality 5 */ XVID_VOP_HALFPEL | XVID_VOP_INTER4V | XVID_VOP_TRELLISQUANT, /* quality 6 */ XVID_VOP_HALFPEL | XVID_VOP_INTER4V | XVID_VOP_TRELLISQUANT | XVID_VOP_HQACPRED,};#define VOP_ELEMENTS (sizeof(vop_presets)/sizeof(vop_presets[0]))/***************************************************************************** * Command line global variables ****************************************************************************/#define MAX_ZONES 64static xvid_enc_zone_t ZONES[MAX_ZONES];static int NUM_ZONES = 0;/* Maximum number of frames to encode */#define ABS_MAXFRAMENR 9999static int ARG_STATS = 0;static int ARG_DUMP = 0;static int ARG_LUMIMASKING = 0;static int ARG_BITRATE = 0;static int ARG_SINGLE = 0;static char *ARG_PASS1 = 0;static char *ARG_PASS2 = 0;static int ARG_QUALITY = ME_ELEMENTS - 1;static float ARG_FRAMERATE = 25.00f;static int ARG_MAXFRAMENR = ABS_MAXFRAMENR;static int ARG_MAXKEYINTERVAL = 0;static char *ARG_INPUTFILE = NULL;static int ARG_INPUTTYPE = 0;static int ARG_SAVEMPEGSTREAM = 0;static int ARG_SAVEINDIVIDUAL = 0;static char *ARG_OUTPUTFILE = NULL;static int XDIM = 0;static int YDIM = 0;static int ARG_BQRATIO = 150;static int ARG_BQOFFSET = 100;static int ARG_MAXBFRAMES = 0;static int ARG_PACKED = 0;static int ARG_DEBUG = 0;static int ARG_VOPDEBUG = 0;static int ARG_GREYSCALE = 0;static int ARG_QTYPE = 0;static int ARG_QMATRIX = 0;static int ARG_GMC = 0;static int ARG_INTERLACING = 0;static int ARG_QPEL = 0;static int ARG_TURBO = 0;static int ARG_VHQMODE = 0;static int ARG_BVHQ = 0;static int ARG_CLOSED_GOP = 0;#ifndef READ_PNM#define IMAGE_SIZE(x,y) ((x)*(y)*3/2)#else#define IMAGE_SIZE(x,y) ((x)*(y)*3)#endif#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) )#define SMALL_EPS (1e-10)#define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ (((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) )/**************************************************************************** * Nasty global vars ;-) ***************************************************************************/static int i;/* the path where to save output */static char filepath[256] = "./";/* Internal structures (handles) for encoding and decoding */static void *enc_handle = NULL;static unsigned char qmatrix_intra[64];static unsigned char qmatrix_inter[64];#ifdef XVID_AVI_INPUTstatic PAVISTREAM avi_stream = NULL;#endif/***************************************************************************** * Local prototypes ****************************************************************************//* Prints program usage message */static void usage();/* Statistical functions */static double msecond();/* PGM related functions */#ifndef READ_PNMstatic int read_pgmheader(FILE * handle);static int read_pgmdata(FILE * handle, unsigned char *image);#elsestatic int read_pnmheader(FILE * handle);static int read_pnmdata(FILE * handle, unsigned char *image);#endifstatic int read_yuvdata(FILE * handle, unsigned char *image);/* Encoder related functions */static int enc_init(int use_assembler);static int enc_stop();static int enc_main(unsigned char *image, unsigned char *bitstream, int *key, int *stats_type, int *stats_quant, int *stats_length, int stats[3]);/***************************************************************************** * Main function ****************************************************************************/intmain(int argc, char *argv[]){ unsigned char *mp4_buffer = NULL; unsigned char *in_buffer = NULL; unsigned char *out_buffer = NULL; double enctime; double totalenctime = 0.; float totalPSNR[3] = {0., 0., 0.}; int totalsize; int result; int m4v_size; int key; int stats_type; int stats_quant; int stats_length; int use_assembler = 1; int input_num; int output_num; char filename[256]; FILE *in_file = stdin; FILE *out_file = NULL; printf("xvid_encraw - raw mpeg4 bitstream encoder "); printf("written by Christoph Lampert 2002-2003\n\n"); /* Is there a dumb XviD coder ? */ if(ME_ELEMENTS != VOP_ELEMENTS) { fprintf(stderr, "Presets' arrays should have the same number of elements -- Please fill a bug to xvid-devel@xvid.org\n"); return(-1); }/***************************************************************************** * Command line parsing ****************************************************************************/ for (i = 1; i < argc; i++) { if (strcmp("-asm", argv[i]) == 0) { use_assembler = 1; } else if (strcmp("-noasm", argv[i]) == 0) { use_assembler = 0; } else if (strcmp("-w", argv[i]) == 0 && i < argc - 1) { i++; XDIM = atoi(argv[i]); } else if (strcmp("-h", argv[i]) == 0 && i < argc - 1) { i++; YDIM = atoi(argv[i]); } else if (strcmp("-bitrate", argv[i]) == 0 && i < argc - 1) { i++; ARG_BITRATE = atoi(argv[i]); } else if (strcmp("-single", argv[i]) == 0) { ARG_SINGLE = 1; } else if (strcmp("-pass1", argv[i]) == 0 && i < argc - 1) { i++; ARG_PASS1 = argv[i]; } else if (strcmp("-pass2", argv[i]) == 0 && i < argc - 1) { i++; ARG_PASS2 = argv[i]; } else if (strcmp("-max_bframes", argv[i]) == 0 && i < argc - 1) { i++; ARG_MAXBFRAMES = atoi(argv[i]); } else if (strcmp("-packed", argv[i]) == 0) { ARG_PACKED = 1; } else if (strcmp("-bquant_ratio", argv[i]) == 0 && i < argc - 1) { i++; ARG_BQRATIO = atoi(argv[i]); } else if (strcmp("-bquant_offset", argv[i]) == 0 && i < argc - 1) { i++; ARG_BQOFFSET = atoi(argv[i]); } else if ((strcmp("-zq", argv[i]) == 0 || strcmp("-zw", argv[i]) == 0) && i < argc - 2) { if (NUM_ZONES >= MAX_ZONES) { fprintf(stderr,"warning: too many zones; zone ignored\n"); continue; } ZONES[NUM_ZONES].mode = strcmp("-zq", argv[i])==0 ? XVID_ZONE_QUANT : XVID_ZONE_WEIGHT; i++; ZONES[NUM_ZONES].frame = atoi(argv[i]); i++; ZONES[NUM_ZONES].increment = (int)(atof(argv[i]) * 100); ZONES[NUM_ZONES].base = 100; NUM_ZONES++; } else if (strcmp("-quality", argv[i]) == 0 && i < argc - 1) { i++; ARG_QUALITY = atoi(argv[i]); } else if (strcmp("-vhqmode", argv[i]) == 0 && i < argc - 1) { i++; ARG_VHQMODE = atoi(argv[i]); } else if (strcmp("-framerate", argv[i]) == 0 && i < argc - 1) { i++; ARG_FRAMERATE = (float) atof(argv[i]); } else if (strcmp("-max_key_interval", argv[i]) == 0 && i < argc - 1) { i++; ARG_MAXKEYINTERVAL = atoi(argv[i]); } else if (strcmp("-i", argv[i]) == 0 && i < argc - 1) { i++; ARG_INPUTFILE = argv[i]; } else if (strcmp("-stats", argv[i]) == 0) { ARG_STATS = 1; } else if (strcmp("-dump", argv[i]) == 0) { ARG_DUMP = 1; } else if (strcmp("-lumimasking", argv[i]) == 0) { ARG_LUMIMASKING = 1; } else if (strcmp("-type", argv[i]) == 0 && i < argc - 1) { i++; ARG_INPUTTYPE = atoi(argv[i]); } else if (strcmp("-frames", argv[i]) == 0 && i < argc - 1) { i++; ARG_MAXFRAMENR = atoi(argv[i]); } else if (strcmp("-qtype", argv[i]) == 0 && i < argc - 1) { i++; ARG_QTYPE = atoi(argv[i]); } else if (strcmp("-qmatrix", argv[i]) == 0 && i < argc - 1) { FILE *fp = fopen(argv[++i], "rb"); if (fp == NULL) { fprintf(stderr, "Error opening input file %s\n", argv[i]); return (-1); } fseek(fp, 0, SEEK_END); if (ftell(fp) != 128) { fprintf(stderr, "Unexpected size of input file %s\n", argv[i]); return (-1); } fseek(fp, 0, SEEK_SET); fread(qmatrix_intra, 1, 64, fp); fread(qmatrix_inter, 1, 64, fp); ARG_QMATRIX = 1; } else if (strcmp("-save", argv[i]) == 0) { ARG_SAVEMPEGSTREAM = 1; ARG_SAVEINDIVIDUAL = 1; } else if (strcmp("-debug", argv[i]) == 0) { i++; if (sscanf(argv[i],"0x%x", &ARG_DEBUG) || sscanf(argv[i],"%d", &ARG_DEBUG)) ; } else if (strcmp("-o", argv[i]) == 0 && i < argc - 1) { ARG_SAVEMPEGSTREAM = 1; i++; ARG_OUTPUTFILE = argv[i]; } else if (strcmp("-vop_debug", argv[i]) == 0) { ARG_VOPDEBUG = 1; } else if (strcmp("-grey", argv[i]) == 0) { ARG_GREYSCALE = 1; } else if (strcmp("-bvhq", argv[i]) == 0) { ARG_BVHQ = 1; } else if (strcmp("-qpel", argv[i]) == 0) { ARG_QPEL = 1; } else if (strcmp("-turbo", argv[i]) == 0) { ARG_TURBO = 1; } else if (strcmp("-gmc", argv[i]) == 0) { ARG_GMC = 1; } else if (strcmp("-interlaced", argv[i]) == 0) { ARG_INTERLACING = 1; } else if (strcmp("-closed_gop", argv[i]) == 0) { ARG_CLOSED_GOP = 1; } else if (strcmp("-help", argv[i])) { usage(); return (0); } else { usage(); exit(-1); } }/***************************************************************************** * Arguments checking ****************************************************************************/ if (XDIM <= 0 || XDIM >= 4096 || YDIM <= 0 || YDIM >= 4096) { fprintf(stderr, "Trying to retrieve width and height from input header\n"); if (!ARG_INPUTTYPE) ARG_INPUTTYPE = 1; /* pgm */ } if (ARG_QUALITY < 0 ) { ARG_QUALITY = 0; } else if (ARG_QUALITY >= ME_ELEMENTS) { ARG_QUALITY = ME_ELEMENTS - 1; } if (ARG_FRAMERATE <= 0) { fprintf(stderr, "Wrong Framerate %s \n", argv[5]); return (-1); } if (ARG_MAXFRAMENR <= 0) { fprintf(stderr, "Wrong number of frames\n"); return (-1); } if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { in_file = stdin;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -