6 |
* Copyright(C) 2002-2003 Christoph Lampert <gruel@web.de> |
* Copyright(C) 2002-2003 Christoph Lampert <gruel@web.de> |
7 |
* 2002-2003 Edouard Gomez <ed.gomez@free.fr> |
* 2002-2003 Edouard Gomez <ed.gomez@free.fr> |
8 |
* 2003 Peter Ross <pross@xvid.org> |
* 2003 Peter Ross <pross@xvid.org> |
9 |
|
* 2003-2010 Michael Militzer <isibaar@xvid.org> |
10 |
* |
* |
11 |
* This program is free software; you can redistribute it and/or modify |
* This program is free software; you can redistribute it and/or modify |
12 |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
22 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
23 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
24 |
* |
* |
25 |
* $Id: xvid_encraw.c,v 1.34 2007-01-09 20:08:53 Isibaar Exp $ |
* $Id$ |
26 |
* |
* |
27 |
****************************************************************************/ |
****************************************************************************/ |
28 |
|
|
55 |
#endif |
#endif |
56 |
|
|
57 |
#include "xvid.h" |
#include "xvid.h" |
58 |
|
#include "portab.h" /* for pthread */ |
59 |
|
|
60 |
#ifdef XVID_MKV_OUTPUT |
#ifdef XVID_MKV_OUTPUT |
61 |
#include "matroska.cpp" |
#include "matroska.cpp" |
63 |
|
|
64 |
#undef READ_PNM |
#undef READ_PNM |
65 |
|
|
66 |
|
//#define USE_APP_LEVEL_THREADING /* Should xvid_encraw app use multi-threading? */ |
67 |
|
|
68 |
/***************************************************************************** |
/***************************************************************************** |
69 |
* Quality presets |
* Quality presets |
70 |
****************************************************************************/ |
****************************************************************************/ |
125 |
****************************************************************************/ |
****************************************************************************/ |
126 |
|
|
127 |
#define MAX_ZONES 64 |
#define MAX_ZONES 64 |
128 |
|
#define MAX_ENC_INSTANCES 4 |
129 |
|
#define MAX_XDIM 4096 |
130 |
|
#define MAX_YDIM 4096 |
131 |
#define DEFAULT_QUANT 400 |
#define DEFAULT_QUANT 400 |
132 |
|
#define DEFAULT_BITRATE 700000 /* bitrate expressed in bps, not kbps */ |
133 |
|
#define DEFAULT_INTERLACING 1 /* 1:BFF, 2:TFF */ |
134 |
|
#define DEFAULT_SSIM 2 |
135 |
|
#define DEFAULT_PROGRESS 10 /* show progress every 10 frames by default */ |
136 |
|
|
137 |
typedef struct |
typedef struct |
138 |
{ |
{ |
155 |
int quants[32]; |
int quants[32]; |
156 |
} frame_stats_t; |
} frame_stats_t; |
157 |
|
|
158 |
|
typedef struct |
159 |
|
{ |
160 |
|
pthread_t handle; /* thread's handle */ |
161 |
|
|
162 |
|
int start_num; /* begin/end of sequence */ |
163 |
|
int stop_num; |
164 |
|
|
165 |
|
char *outfilename; /* output filename */ |
166 |
|
char *statsfilename1; /* pass1 statsfile */ |
167 |
|
|
168 |
|
int input_num; |
169 |
|
|
170 |
|
int totalsize; /* encoder stats */ |
171 |
|
double totalenctime; |
172 |
|
float totalPSNR[3]; |
173 |
|
frame_stats_t framestats[7]; |
174 |
|
} enc_sequence_data_t; |
175 |
|
|
176 |
/* Maximum number of frames to encode */ |
/* Maximum number of frames to encode */ |
177 |
#define ABS_MAXFRAMENR -1 /* no limit */ |
#define ABS_MAXFRAMENR -1 /* no limit */ |
178 |
|
|
190 |
|
|
191 |
static zone_t ZONES[MAX_ZONES]; |
static zone_t ZONES[MAX_ZONES]; |
192 |
static int NUM_ZONES = 0; |
static int NUM_ZONES = 0; |
|
static frame_stats_t framestats[7]; |
|
193 |
|
|
194 |
static int ARG_STATS = 0; |
static int ARG_NUM_APP_THREADS = 1; |
195 |
|
static int ARG_CPU_FLAGS = 0; |
196 |
static int ARG_SSIM = -1; |
static int ARG_SSIM = -1; |
197 |
static char* ARG_SSIM_PATH = NULL; |
static char* ARG_SSIM_PATH = NULL; |
198 |
static int ARG_DUMP = 0; |
static int ARG_DUMP = 0; |
204 |
static char *ARG_PASS2 = 0; |
static char *ARG_PASS2 = 0; |
205 |
//static int ARG_QUALITY = ME_ELEMENTS - 1; |
//static int ARG_QUALITY = ME_ELEMENTS - 1; |
206 |
static int ARG_QUALITY = 6; |
static int ARG_QUALITY = 6; |
207 |
static float ARG_FRAMERATE = 25.00f; |
static float ARG_FRAMERATE = 0.f; |
208 |
static int ARG_DWRATE = 25; |
static int ARG_DWRATE = 25; |
209 |
static int ARG_DWSCALE = 1; |
static int ARG_DWSCALE = 1; |
210 |
static int ARG_MAXFRAMENR = ABS_MAXFRAMENR; |
static int ARG_MAXFRAMENR = ABS_MAXFRAMENR; |
215 |
static int ARG_SAVEMPEGSTREAM = 0; |
static int ARG_SAVEMPEGSTREAM = 0; |
216 |
static int ARG_SAVEINDIVIDUAL = 0; |
static int ARG_SAVEINDIVIDUAL = 0; |
217 |
static char *ARG_OUTPUTFILE = NULL; |
static char *ARG_OUTPUTFILE = NULL; |
|
#ifdef XVID_AVI_OUTPUT |
|
218 |
static char *ARG_AVIOUTPUTFILE = NULL; |
static char *ARG_AVIOUTPUTFILE = NULL; |
|
#endif |
|
|
#ifdef XVID_MKV_OUTPUT |
|
219 |
static char *ARG_MKVOUTPUTFILE = NULL; |
static char *ARG_MKVOUTPUTFILE = NULL; |
|
#endif |
|
|
#ifdef XVID_AVI_INPUT |
|
|
static PAVISTREAM avi_stream = NULL; |
|
|
static PAVIFILE avi_file = NULL; |
|
|
static LPBITMAPINFOHEADER info_header = NULL; |
|
|
static PGETFRAME get_frame = NULL; |
|
|
#else |
|
|
#define get_frame NULL |
|
|
#endif |
|
220 |
static char *ARG_TIMECODEFILE = NULL; |
static char *ARG_TIMECODEFILE = NULL; |
221 |
static int XDIM = 0; |
static int XDIM = 0; |
222 |
static int YDIM = 0; |
static int YDIM = 0; |
223 |
static int ARG_BQRATIO = 150; |
static int ARG_BQRATIO = 150; |
224 |
static int ARG_BQOFFSET = 100; |
static int ARG_BQOFFSET = 100; |
225 |
static int ARG_MAXBFRAMES = 2; |
static int ARG_MAXBFRAMES = 2; |
|
static int ARG_PACKED = 1; |
|
226 |
static int ARG_DEBUG = 0; |
static int ARG_DEBUG = 0; |
|
static int ARG_VOPDEBUG = 0; |
|
|
static int ARG_TRELLIS = 1; |
|
227 |
static int ARG_QTYPE = 0; |
static int ARG_QTYPE = 0; |
228 |
static int ARG_QMATRIX = 0; |
static int ARG_QMATRIX = 0; |
|
static int ARG_GMC = 0; |
|
229 |
static int ARG_INTERLACING = 0; |
static int ARG_INTERLACING = 0; |
|
static int ARG_QPEL = 0; |
|
|
static int ARG_TURBO = 0; |
|
230 |
static int ARG_VHQMODE = 1; |
static int ARG_VHQMODE = 1; |
231 |
static int ARG_BVHQ = 0; |
static int ARG_QMETRIC = 0; |
|
static int ARG_CLOSED_GOP = 1; |
|
|
static int ARG_CHROMAME = 1; |
|
232 |
static int ARG_PAR = 1; |
static int ARG_PAR = 1; |
233 |
static int ARG_PARHEIGHT; |
static int ARG_PARHEIGHT; |
234 |
static int ARG_PARWIDTH; |
static int ARG_PARWIDTH; |
252 |
static int ARG_VBVMAXRATE = 0; |
static int ARG_VBVMAXRATE = 0; |
253 |
static int ARG_VBVPEAKRATE = 0; |
static int ARG_VBVPEAKRATE = 0; |
254 |
static int ARG_THREADS = 0; |
static int ARG_THREADS = 0; |
255 |
|
static int ARG_SLICES = 1; |
256 |
static int ARG_VFR = 0; |
static int ARG_VFR = 0; |
257 |
static int ARG_PROGRESS = 0; |
static int ARG_PROGRESS = 0; |
258 |
static int ARG_COLORSPACE = XVID_CSP_YV12; |
static int ARG_COLORSPACE = XVID_CSP_YV12; |
259 |
/* the path where to save output */ |
/* the path where to save output */ |
260 |
static char filepath[256] = "./"; |
static char filepath[256] = "./"; |
261 |
/* Internal structures (handles) for encoding and decoding */ |
|
262 |
static void *enc_handle = NULL; |
/* on/off options */ |
263 |
|
static int ARG_USE_ASSEMBLER = 1; |
264 |
|
static int ARG_TURBO = 0; |
265 |
|
static int ARG_BVHQ = 0; |
266 |
|
static int ARG_QPEL = 0; |
267 |
|
static int ARG_GMC = 0; |
268 |
|
static int ARG_PACKED = 1; |
269 |
|
static int ARG_CLOSED_GOP = 1; |
270 |
|
static int ARG_STATS = 0; |
271 |
|
static int ARG_PSNRHVSM = 0; |
272 |
|
static int ARG_VOPDEBUG = 0; |
273 |
|
static int ARG_CHROMAME = 1; |
274 |
|
static int ARG_TRELLIS = 1; |
275 |
|
|
276 |
static unsigned char qmatrix_intra[64]; |
static unsigned char qmatrix_intra[64]; |
277 |
static unsigned char qmatrix_inter[64]; |
static unsigned char qmatrix_inter[64]; |
278 |
|
|
314 |
unsigned char *image); |
unsigned char *image); |
315 |
|
|
316 |
/* Encoder related functions */ |
/* Encoder related functions */ |
317 |
static int enc_init(int use_assembler); |
static void enc_gbl(int use_assembler); |
318 |
|
static int enc_init(void **enc_handle, char *stats_pass1, int start_num); |
319 |
static int enc_info(); |
static int enc_info(); |
320 |
static int enc_stop(); |
static int enc_stop(void *enc_handle); |
321 |
static int enc_main(unsigned char *image, |
static int enc_main(void *enc_handle, |
322 |
|
unsigned char *image, |
323 |
unsigned char *bitstream, |
unsigned char *bitstream, |
324 |
int *key, |
int *key, |
325 |
int *stats_type, |
int *stats_type, |
327 |
int *stats_length, |
int *stats_length, |
328 |
int stats[3], |
int stats[3], |
329 |
int framenum); |
int framenum); |
330 |
|
static void encode_sequence(enc_sequence_data_t *h); |
331 |
|
|
332 |
/* Zone Related Functions */ |
/* Zone Related Functions */ |
333 |
static void apply_zone_modifiers(xvid_enc_frame_t * frame, int framenum); |
static void apply_zone_modifiers(xvid_enc_frame_t * frame, int framenum); |
346 |
main(int argc, |
main(int argc, |
347 |
char *argv[]) |
char *argv[]) |
348 |
{ |
{ |
|
|
|
|
unsigned char *mp4_buffer = NULL; |
|
|
unsigned char *in_buffer = NULL; |
|
|
unsigned char *out_buffer = NULL; |
|
|
|
|
|
double enctime; |
|
349 |
double totalenctime = 0.; |
double totalenctime = 0.; |
350 |
float totalPSNR[3] = {0., 0., 0.}; |
float totalPSNR[3] = {0., 0., 0.}; |
351 |
|
|
352 |
FILE *statsfile; |
FILE *statsfile; |
353 |
|
frame_stats_t framestats[7]; |
354 |
|
|
355 |
int totalsize; |
int input_num = 0; |
356 |
int result; |
int totalsize = 0; |
357 |
int m4v_size; |
int use_assembler = ARG_USE_ASSEMBLER; |
|
int key; |
|
|
int stats_type; |
|
|
int stats_quant; |
|
|
int stats_length; |
|
|
int use_assembler = 1; |
|
|
int fakenvop = 0; |
|
358 |
int i; |
int i; |
|
int nvop_counter; |
|
|
|
|
|
int input_num; |
|
|
int output_num; |
|
|
|
|
|
char filename[256]; |
|
|
|
|
|
FILE *in_file = stdin; |
|
|
FILE *out_file = NULL; |
|
|
FILE *time_file = NULL; |
|
|
|
|
|
#ifdef XVID_AVI_OUTPUT |
|
|
int avierr; |
|
|
PAVIFILE myAVIFile=NULL; |
|
|
PAVISTREAM myAVIStream=NULL; |
|
|
AVISTREAMINFO myAVIStreamInfo; |
|
|
BITMAPINFOHEADER myBitmapInfoHeader; |
|
|
AVIFileInit(); |
|
|
#endif |
|
|
#ifdef XVID_MKV_OUTPUT |
|
|
PMKVFILE myMKVFile=NULL; |
|
|
PMKVSTREAM myMKVStream=NULL; |
|
|
MKVSTREAMINFO myMKVStreamInfo; |
|
|
#endif |
|
359 |
|
|
360 |
printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
361 |
printf("written by Christoph Lampert 2002-2003\n\n"); |
printf("written by Christoph Lampert\n\n"); |
362 |
|
|
363 |
/* Is there a dumb XviD coder ? */ |
/* Is there a dumb Xvid coder ? */ |
364 |
if(ME_ELEMENTS != VOP_ELEMENTS) { |
if(ME_ELEMENTS != VOP_ELEMENTS) { |
365 |
fprintf(stderr, "Presets' arrays should have the same number of elements -- Please fill a bug to xvid-devel@xvid.org\n"); |
fprintf(stderr, "Presets' arrays should have the same number of elements -- Please file a bug to xvid-devel@xvid.org\n"); |
366 |
return(-1); |
return(-1); |
367 |
} |
} |
368 |
|
|
369 |
|
/* Clear framestats */ |
370 |
|
memset(framestats, 0, sizeof(framestats)); |
371 |
|
|
372 |
/***************************************************************************** |
/***************************************************************************** |
373 |
* Command line parsing |
* Command line parsing |
374 |
****************************************************************************/ |
****************************************************************************/ |
400 |
ARG_BITRATE = atoi(argv[i+1]); |
ARG_BITRATE = atoi(argv[i+1]); |
401 |
if (ARG_BITRATE) { |
if (ARG_BITRATE) { |
402 |
i++; |
i++; |
403 |
if (ARG_BITRATE <= 10000) |
if (ARG_BITRATE <= 20000) |
404 |
/* if given parameter is <= 10000, assume it means kbps */ |
/* if given parameter is <= 20000, assume it means kbps */ |
405 |
ARG_BITRATE *= 1000; |
ARG_BITRATE *= 1000; |
406 |
} |
} |
407 |
else |
else |
408 |
ARG_BITRATE = 700000; |
ARG_BITRATE = DEFAULT_BITRATE; |
409 |
} else if (strcmp("-size", argv[i]) == 0 && i < argc - 1) { |
} else if (strcmp("-size", argv[i]) == 0 && i < argc - 1) { |
410 |
i++; |
i++; |
411 |
ARG_TARGETSIZE = atoi(argv[i]); |
ARG_TARGETSIZE = atoi(argv[i]); |
454 |
ARG_PACKED = 2; |
ARG_PACKED = 2; |
455 |
} else if (strcmp("-nochromame", argv[i]) == 0) { |
} else if (strcmp("-nochromame", argv[i]) == 0) { |
456 |
ARG_CHROMAME = 0; |
ARG_CHROMAME = 0; |
457 |
|
} else if (strcmp("-chromame", argv[i]) == 0) { |
458 |
|
ARG_CHROMAME = 1; |
459 |
} else if (strcmp("-threads", argv[i]) == 0 && i < argc -1) { |
} else if (strcmp("-threads", argv[i]) == 0 && i < argc -1) { |
460 |
i++; |
i++; |
461 |
ARG_THREADS = atoi(argv[i]); |
ARG_THREADS = atoi(argv[i]); |
462 |
|
} else if (strcmp("-slices", argv[i]) == 0 && i < argc -1) { |
463 |
|
i++; |
464 |
|
ARG_SLICES = atoi(argv[i]); |
465 |
} else if (strcmp("-bquant_ratio", argv[i]) == 0 && i < argc - 1) { |
} else if (strcmp("-bquant_ratio", argv[i]) == 0 && i < argc - 1) { |
466 |
i++; |
i++; |
467 |
ARG_BQRATIO = atoi(argv[i]); |
ARG_BQRATIO = atoi(argv[i]); |
496 |
memset(&ZONES[NUM_ZONES], 0, sizeof(zone_t)); |
memset(&ZONES[NUM_ZONES], 0, sizeof(zone_t)); |
497 |
|
|
498 |
ZONES[NUM_ZONES].frame = startframe; |
ZONES[NUM_ZONES].frame = startframe; |
499 |
ZONES[NUM_ZONES].modifier = atof(options)*100; |
ZONES[NUM_ZONES].modifier = (int)(atof(options)*100); |
500 |
if (toupper(c)=='Q') |
if (toupper(c)=='Q') |
501 |
ZONES[NUM_ZONES].mode = XVID_ZONE_QUANT; |
ZONES[NUM_ZONES].mode = XVID_ZONE_QUANT; |
502 |
else if (toupper(c)=='W') |
else if (toupper(c)=='W') |
509 |
if ((frameoptions=strchr(options, ','))!=NULL) { |
if ((frameoptions=strchr(options, ','))!=NULL) { |
510 |
int readchar=0, count; |
int readchar=0, count; |
511 |
frameoptions++; |
frameoptions++; |
512 |
while (readchar<strlen(frameoptions)) { |
while (readchar<(int)strlen(frameoptions)) { |
513 |
if (sscanf(frameoptions+readchar, "%d%n", &(ZONES[NUM_ZONES].bvop_threshold), &count)==1) { |
if (sscanf(frameoptions+readchar, "%d%n", &(ZONES[NUM_ZONES].bvop_threshold), &count)==1) { |
514 |
readchar += count; |
readchar += count; |
515 |
} |
} |
547 |
else { |
else { |
548 |
ZONES[NUM_ZONES].mode = XVID_ZONE_WEIGHT; |
ZONES[NUM_ZONES].mode = XVID_ZONE_WEIGHT; |
549 |
} |
} |
550 |
ZONES[NUM_ZONES].modifier = atof(argv[i+2])*100; |
ZONES[NUM_ZONES].modifier = (int)(atof(argv[i+2])*100); |
551 |
i++; |
i++; |
552 |
ZONES[NUM_ZONES].frame = atoi(argv[i]); |
ZONES[NUM_ZONES].frame = atoi(argv[i]); |
553 |
i++; |
i++; |
567 |
} else if (strcmp("-vhqmode", argv[i]) == 0 && i < argc - 1) { |
} else if (strcmp("-vhqmode", argv[i]) == 0 && i < argc - 1) { |
568 |
i++; |
i++; |
569 |
ARG_VHQMODE = atoi(argv[i]); |
ARG_VHQMODE = atoi(argv[i]); |
570 |
|
} else if (strcmp("-metric", argv[i]) == 0 && i < argc - 1) { |
571 |
|
i++; |
572 |
|
ARG_QMETRIC = atoi(argv[i]); |
573 |
} else if (strcmp("-framerate", argv[i]) == 0 && i < argc - 1) { |
} else if (strcmp("-framerate", argv[i]) == 0 && i < argc - 1) { |
574 |
int exponent; |
int exponent; |
575 |
i++; |
i++; |
576 |
ARG_FRAMERATE = (float) atof(argv[i]); |
ARG_FRAMERATE = (float) atof(argv[i]); |
577 |
exponent = strcspn(argv[i], "."); |
exponent = (int)strcspn(argv[i], "."); |
578 |
if (exponent<strlen(argv[i])) |
if (exponent<(int)strlen(argv[i])) |
579 |
exponent=pow(10.0, (int)(strlen(argv[i])-1-exponent)); |
exponent=(int)pow(10.0, (int)(strlen(argv[i])-1-exponent)); |
580 |
else |
else |
581 |
exponent=1; |
exponent=1; |
582 |
ARG_DWRATE = atof(argv[i])*exponent; |
ARG_DWRATE = (int)(atof(argv[i])*exponent); |
583 |
ARG_DWSCALE = exponent; |
ARG_DWSCALE = exponent; |
584 |
exponent = gcd(ARG_DWRATE, ARG_DWSCALE); |
exponent = gcd(ARG_DWRATE, ARG_DWSCALE); |
585 |
ARG_DWRATE /= exponent; |
ARG_DWRATE /= exponent; |
592 |
ARG_INPUTFILE = argv[i]; |
ARG_INPUTFILE = argv[i]; |
593 |
} else if (strcmp("-stats", argv[i]) == 0) { |
} else if (strcmp("-stats", argv[i]) == 0) { |
594 |
ARG_STATS = 1; |
ARG_STATS = 1; |
595 |
|
} else if (strcmp("-nostats", argv[i]) == 0) { |
596 |
|
ARG_STATS = 0; |
597 |
} else if (strcmp("-ssim", argv[i]) == 0) { |
} else if (strcmp("-ssim", argv[i]) == 0) { |
598 |
ARG_SSIM = 2; |
ARG_SSIM = DEFAULT_SSIM; |
599 |
if ((i < argc - 1) && (*argv[i+1] != '-')) { |
if ((i < argc - 1) && (*argv[i+1] != '-')) { |
600 |
i++; |
i++; |
601 |
ARG_SSIM = atoi(argv[i]); |
ARG_SSIM = atoi(argv[i]); |
602 |
} |
} |
603 |
|
} else if (strcmp("-psnrhvsm", argv[i]) == 0) { |
604 |
|
ARG_PSNRHVSM = 1; |
605 |
|
} else if (strcmp("-nopsnrhvsm", argv[i]) == 0) { |
606 |
|
ARG_PSNRHVSM = 0; |
607 |
} else if (strcmp("-ssim_file", argv[i]) == 0 && i < argc -1) { |
} else if (strcmp("-ssim_file", argv[i]) == 0 && i < argc -1) { |
608 |
i++; |
i++; |
609 |
ARG_SSIM_PATH = argv[i]; |
ARG_SSIM_PATH = argv[i]; |
612 |
ARG_TIMECODEFILE = argv[i]; |
ARG_TIMECODEFILE = argv[i]; |
613 |
} else if (strcmp("-dump", argv[i]) == 0) { |
} else if (strcmp("-dump", argv[i]) == 0) { |
614 |
ARG_DUMP = 1; |
ARG_DUMP = 1; |
615 |
} else if (strcmp("-lumimasking", argv[i]) == 0) { |
} else if (strcmp("-masking", argv[i]) == 0 && i < argc -1) { |
616 |
ARG_LUMIMASKING = 1; |
i++; |
617 |
|
ARG_LUMIMASKING = atoi(argv[i]); |
618 |
} else if (strcmp("-type", argv[i]) == 0 && i < argc - 1) { |
} else if (strcmp("-type", argv[i]) == 0 && i < argc - 1) { |
619 |
i++; |
i++; |
620 |
ARG_INPUTTYPE = atoi(argv[i]); |
ARG_INPUTTYPE = atoi(argv[i]); |
694 |
#endif |
#endif |
695 |
} else if (strcmp("-vop_debug", argv[i]) == 0) { |
} else if (strcmp("-vop_debug", argv[i]) == 0) { |
696 |
ARG_VOPDEBUG = 1; |
ARG_VOPDEBUG = 1; |
697 |
|
} else if (strcmp("-novop_debug", argv[i]) == 0) { |
698 |
|
ARG_VOPDEBUG = 0; |
699 |
|
} else if (strcmp("-trellis", argv[i]) == 0) { |
700 |
|
ARG_TRELLIS = 1; |
701 |
} else if (strcmp("-notrellis", argv[i]) == 0) { |
} else if (strcmp("-notrellis", argv[i]) == 0) { |
702 |
ARG_TRELLIS = 0; |
ARG_TRELLIS = 0; |
703 |
} else if (strcmp("-bvhq", argv[i]) == 0) { |
} else if (strcmp("-bvhq", argv[i]) == 0) { |
704 |
ARG_BVHQ = 1; |
ARG_BVHQ = 1; |
705 |
|
} else if (strcmp("-nobvhq", argv[i]) == 0) { |
706 |
|
ARG_BVHQ = 0; |
707 |
} else if (strcmp("-qpel", argv[i]) == 0) { |
} else if (strcmp("-qpel", argv[i]) == 0) { |
708 |
ARG_QPEL = 1; |
ARG_QPEL = 1; |
709 |
|
} else if (strcmp("-noqpel", argv[i]) == 0) { |
710 |
|
ARG_QPEL = 0; |
711 |
} else if (strcmp("-turbo", argv[i]) == 0) { |
} else if (strcmp("-turbo", argv[i]) == 0) { |
712 |
ARG_TURBO = 1; |
ARG_TURBO = 1; |
713 |
|
} else if (strcmp("-noturbo", argv[i]) == 0) { |
714 |
|
ARG_TURBO = 0; |
715 |
} else if (strcmp("-gmc", argv[i]) == 0) { |
} else if (strcmp("-gmc", argv[i]) == 0) { |
716 |
ARG_GMC = 1; |
ARG_GMC = 1; |
717 |
|
} else if (strcmp("-nogmc", argv[i]) == 0) { |
718 |
|
ARG_GMC = 0; |
719 |
} else if (strcmp("-interlaced", argv[i]) == 0) { |
} else if (strcmp("-interlaced", argv[i]) == 0) { |
720 |
if ((i < argc - 1) && (*argv[i+1] != '-')) { |
if ((i < argc - 1) && (*argv[i+1] != '-')) { |
721 |
i++; |
i++; |
722 |
ARG_INTERLACING = atoi(argv[i]); |
ARG_INTERLACING = atoi(argv[i]); |
723 |
} else { |
} else { |
724 |
ARG_INTERLACING = 1; |
ARG_INTERLACING = DEFAULT_INTERLACING; |
725 |
} |
} |
726 |
} else if (strcmp("-noclosed_gop", argv[i]) == 0) { |
} else if (strcmp("-noclosed_gop", argv[i]) == 0) { |
727 |
ARG_CLOSED_GOP = 0; |
ARG_CLOSED_GOP = 0; |
735 |
ARG_VBVMAXRATE = atoi(argv[i]); |
ARG_VBVMAXRATE = atoi(argv[i]); |
736 |
} else if (strcmp("-vbvpeak", argv[i]) == 0 && i < argc -1) { |
} else if (strcmp("-vbvpeak", argv[i]) == 0 && i < argc -1) { |
737 |
i++; |
i++; |
738 |
ARG_VBVPEAKRATE = atoi(argv[i])*3; |
ARG_VBVPEAKRATE = atoi(argv[i]); |
739 |
} else if (strcmp("-reaction", argv[i]) == 0 && i < argc -1) { |
} else if (strcmp("-reaction", argv[i]) == 0 && i < argc -1) { |
740 |
i++; |
i++; |
741 |
ARG_REACTION = atoi(argv[i]); |
ARG_REACTION = atoi(argv[i]); |
779 |
if (ARG_PROGRESS > 0) |
if (ARG_PROGRESS > 0) |
780 |
i++; |
i++; |
781 |
else |
else |
782 |
ARG_PROGRESS = 10; |
ARG_PROGRESS = DEFAULT_PROGRESS; |
783 |
} else if (strcmp("-help", argv[i])) { |
} else if (strcmp("-help", argv[i]) == 0) { |
784 |
usage(); |
usage(); |
785 |
return (0); |
return (0); |
786 |
} else { |
} else { |
794 |
* Arguments checking |
* Arguments checking |
795 |
****************************************************************************/ |
****************************************************************************/ |
796 |
|
|
797 |
if (XDIM <= 0 || XDIM >= 4096 || YDIM <= 0 || YDIM >= 4096) { |
if (XDIM <= 0 || XDIM >= MAX_XDIM || YDIM <= 0 || YDIM >= MAX_YDIM) { |
798 |
fprintf(stderr, |
fprintf(stderr, |
799 |
"Trying to retrieve width and height from input header\n"); |
"Trying to retrieve width and height from input header\n"); |
800 |
if (!ARG_INPUTTYPE) |
if (!ARG_INPUTTYPE) |
850 |
return (-1); |
return (-1); |
851 |
} |
} |
852 |
|
|
853 |
if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
if (ARG_INPUTFILE != NULL) { |
854 |
in_file = stdin; |
#if defined(XVID_AVI_INPUT) |
|
} else { |
|
|
#ifdef XVID_AVI_INPUT |
|
855 |
if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 || |
if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 || |
856 |
strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 || |
strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 || |
857 |
ARG_INPUTTYPE==2) |
ARG_INPUTTYPE==2) |
858 |
{ |
{ |
859 |
|
PAVIFILE avi_in = NULL; |
860 |
|
PAVISTREAM avi_in_stream = NULL; |
861 |
|
PGETFRAME get_frame = NULL; |
862 |
|
BITMAPINFOHEADER myBitmapInfoHeader; |
863 |
AVISTREAMINFO avi_info; |
AVISTREAMINFO avi_info; |
|
#ifndef XVID_AVI_OUTPUT |
|
|
AVIFileInit(); |
|
|
#endif |
|
864 |
FILE *avi_fp = fopen(ARG_INPUTFILE, "rb"); |
FILE *avi_fp = fopen(ARG_INPUTFILE, "rb"); |
865 |
|
|
866 |
|
AVIFileInit(); |
867 |
|
|
868 |
if (avi_fp == NULL) { |
if (avi_fp == NULL) { |
869 |
fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE); |
fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE); |
870 |
return (-1); |
return (-1); |
871 |
} |
} |
872 |
fclose(avi_fp); |
fclose(avi_fp); |
873 |
|
|
874 |
if (AVIFileOpen(&avi_file, ARG_INPUTFILE, OF_READ, NULL) != AVIERR_OK) { |
if (AVIFileOpen(&avi_in, ARG_INPUTFILE, OF_READ, NULL) != AVIERR_OK) { |
875 |
fprintf(stderr, "Can't open avi/avs file %s\n", ARG_INPUTFILE); |
fprintf(stderr, "Can't open avi/avs file %s\n", ARG_INPUTFILE); |
876 |
AVIFileExit(); |
AVIFileExit(); |
877 |
return(-1); |
return(-1); |
878 |
} |
} |
879 |
|
|
880 |
if (AVIFileGetStream(avi_file, &avi_stream, streamtypeVIDEO, 0) != AVIERR_OK) { |
if (AVIFileGetStream(avi_in, &avi_in_stream, streamtypeVIDEO, 0) != AVIERR_OK) { |
881 |
fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE); |
fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE); |
882 |
AVIFileRelease(avi_file); |
AVIFileRelease(avi_in); |
883 |
AVIFileExit(); |
AVIFileExit(); |
884 |
return (-1); |
return (-1); |
885 |
} |
} |
886 |
|
|
887 |
AVIFileRelease(avi_file); |
AVIFileRelease(avi_in); |
888 |
|
|
889 |
if(AVIStreamInfo(avi_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) { |
if(AVIStreamInfo(avi_in_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) { |
890 |
fprintf(stderr, "Can't get stream info from file '%s'!\n", ARG_INPUTFILE); |
fprintf(stderr, "Can't get stream info from file '%s'!\n", ARG_INPUTFILE); |
891 |
AVIStreamRelease(avi_stream); |
AVIStreamRelease(avi_in_stream); |
892 |
AVIFileExit(); |
AVIFileExit(); |
893 |
return (-1); |
return (-1); |
894 |
} |
} |
899 |
avi_info.fccHandler%256, (avi_info.fccHandler>>8)%256, (avi_info.fccHandler>>16)%256, |
avi_info.fccHandler%256, (avi_info.fccHandler>>8)%256, (avi_info.fccHandler>>16)%256, |
900 |
(avi_info.fccHandler>>24)%256); |
(avi_info.fccHandler>>24)%256); |
901 |
size = sizeof(myBitmapInfoHeader); |
size = sizeof(myBitmapInfoHeader); |
902 |
AVIStreamReadFormat(avi_stream, 0, &myBitmapInfoHeader, &size); |
AVIStreamReadFormat(avi_in_stream, 0, &myBitmapInfoHeader, &size); |
903 |
if (size==0) |
if (size==0) |
904 |
fprintf(stderr, "AVIStreamReadFormat read 0 bytes.\n"); |
fprintf(stderr, "AVIStreamReadFormat read 0 bytes.\n"); |
905 |
else { |
else { |
914 |
myBitmapInfoHeader.biCompression = MAKEFOURCC('Y', 'V', '1', '2'); |
myBitmapInfoHeader.biCompression = MAKEFOURCC('Y', 'V', '1', '2'); |
915 |
myBitmapInfoHeader.biBitCount = 12; |
myBitmapInfoHeader.biBitCount = 12; |
916 |
myBitmapInfoHeader.biSizeImage = (myBitmapInfoHeader.biWidth*myBitmapInfoHeader.biHeight)*3/2; |
myBitmapInfoHeader.biSizeImage = (myBitmapInfoHeader.biWidth*myBitmapInfoHeader.biHeight)*3/2; |
917 |
get_frame = AVIStreamGetFrameOpen(avi_stream, &myBitmapInfoHeader); |
get_frame = AVIStreamGetFrameOpen(avi_in_stream, &myBitmapInfoHeader); |
918 |
} |
} |
919 |
if (get_frame == NULL) { |
if (get_frame == NULL) { |
920 |
AVIStreamRelease(avi_stream); |
AVIStreamRelease(avi_in_stream); |
921 |
AVIFileExit(); |
AVIFileExit(); |
922 |
return (-1); |
return (-1); |
923 |
} |
} |
927 |
temp = (unsigned char*)AVIStreamGetFrame(get_frame, 0); |
temp = (unsigned char*)AVIStreamGetFrame(get_frame, 0); |
928 |
if (temp != NULL) { |
if (temp != NULL) { |
929 |
int i; |
int i; |
930 |
for (i = 0; i < ((DWORD*)temp)[0]; i++) { |
for (i = 0; i < (int)((DWORD*)temp)[0]; i++) { |
931 |
fprintf(stderr, "%2d ", temp[i]); |
fprintf(stderr, "%2d ", temp[i]); |
932 |
} |
} |
933 |
fprintf(stderr, "\n"); |
fprintf(stderr, "\n"); |
943 |
if (ARG_MAXFRAMENR<0) |
if (ARG_MAXFRAMENR<0) |
944 |
ARG_MAXFRAMENR = avi_info.dwLength-ARG_STARTFRAMENR; |
ARG_MAXFRAMENR = avi_info.dwLength-ARG_STARTFRAMENR; |
945 |
else |
else |
946 |
ARG_MAXFRAMENR = min(ARG_MAXFRAMENR, avi_info.dwLength-ARG_STARTFRAMENR); |
ARG_MAXFRAMENR = min(ARG_MAXFRAMENR, (int)(avi_info.dwLength-ARG_STARTFRAMENR)); |
947 |
|
|
948 |
XDIM = avi_info.rcFrame.right - avi_info.rcFrame.left; |
XDIM = avi_info.rcFrame.right - avi_info.rcFrame.left; |
949 |
YDIM = avi_info.rcFrame.bottom - avi_info.rcFrame.top; |
YDIM = avi_info.rcFrame.bottom - avi_info.rcFrame.top; |
954 |
} |
} |
955 |
|
|
956 |
ARG_INPUTTYPE = 2; |
ARG_INPUTTYPE = 2; |
957 |
|
|
958 |
|
if (get_frame) AVIStreamGetFrameClose(get_frame); |
959 |
|
if (avi_in_stream) AVIStreamRelease(avi_in_stream); |
960 |
|
AVIFileExit(); |
961 |
} |
} |
962 |
else |
else |
963 |
#endif |
#endif |
964 |
{ |
{ |
965 |
in_file = fopen(ARG_INPUTFILE, "rb"); |
FILE *in_file = fopen(ARG_INPUTFILE, "rb"); |
966 |
|
int pos = 0; |
967 |
if (in_file == NULL) { |
if (in_file == NULL) { |
968 |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
969 |
return (-1); |
return (-1); |
970 |
} |
} |
971 |
} |
#ifdef USE_APP_LEVEL_THREADING |
972 |
} |
fseek(in_file, 0, SEEK_END); /* Determine input size */ |
973 |
|
pos = ftell(in_file); |
974 |
// This should be after the avi input opening stuff |
ARG_MAXFRAMENR = pos / IMAGE_SIZE(XDIM, YDIM); /* PGM, header size ?? */ |
975 |
if (ARG_TIMECODEFILE != NULL) { |
#endif |
976 |
time_file = fopen(ARG_TIMECODEFILE, "r"); |
fclose(in_file); |
|
if (time_file==NULL) { |
|
|
fprintf(stderr, "Couldn't open timecode file '%s'!\n", ARG_TIMECODEFILE); |
|
|
return(-1); |
|
|
} |
|
|
else { |
|
|
fscanf(time_file, "# timecode format v2\n"); |
|
977 |
} |
} |
978 |
} |
} |
979 |
|
|
980 |
if (ARG_FRAMERATE <= 0) { |
if (ARG_FRAMERATE <= 0) { |
981 |
fprintf(stderr, "Wrong Framerate %f\n", ARG_FRAMERATE); |
ARG_FRAMERATE = 25.00f; /* default value */ |
|
return (-1); |
|
982 |
} |
} |
983 |
|
|
984 |
if (ARG_TARGETSIZE) { |
if (ARG_TARGETSIZE) { |
989 |
fprintf(stderr, "Parameter conflict: Do not specify both -bitrate and -size\n"); |
fprintf(stderr, "Parameter conflict: Do not specify both -bitrate and -size\n"); |
990 |
goto release_all; |
goto release_all; |
991 |
} else |
} else |
992 |
ARG_BITRATE = ((ARG_TARGETSIZE * 8) / (ARG_MAXFRAMENR / ARG_FRAMERATE)) * 1024; |
ARG_BITRATE = (int)(((ARG_TARGETSIZE * 8) / (ARG_MAXFRAMENR / ARG_FRAMERATE)) * 1024); |
993 |
} |
} |
994 |
|
|
995 |
/* Set constant quant to default if no bitrate given for single pass */ |
/* Set constant quant to default if no bitrate given for single pass */ |
996 |
if (ARG_SINGLE && (!ARG_BITRATE) && (!ARG_CQ)) |
if (ARG_SINGLE && (!ARG_BITRATE) && (!ARG_CQ)) |
997 |
ARG_CQ = DEFAULT_QUANT; |
ARG_CQ = DEFAULT_QUANT; |
998 |
|
|
999 |
|
/* Init xvidcore */ |
1000 |
|
enc_gbl(use_assembler); |
1001 |
|
|
1002 |
|
#ifdef USE_APP_LEVEL_THREADING |
1003 |
|
if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0 || |
1004 |
|
ARG_NUM_APP_THREADS <= 1 || ARG_THREADS != 0 || |
1005 |
|
ARG_TIMECODEFILE != NULL || ARG_AVIOUTPUTFILE != NULL || |
1006 |
|
ARG_INPUTTYPE == 1 || ARG_MKVOUTPUTFILE != NULL) /* TODO: PGM input */ |
1007 |
|
#endif /* Spawn just one encoder instance */ |
1008 |
|
{ |
1009 |
|
enc_sequence_data_t enc_data; |
1010 |
|
memset(&enc_data, 0, sizeof(enc_sequence_data_t)); |
1011 |
|
|
1012 |
|
if (!ARG_THREADS) ARG_THREADS = ARG_NUM_APP_THREADS; |
1013 |
|
ARG_NUM_APP_THREADS = 1; |
1014 |
|
|
1015 |
|
enc_data.outfilename = ARG_OUTPUTFILE; |
1016 |
|
enc_data.statsfilename1 = ARG_PASS1; |
1017 |
|
enc_data.start_num = ARG_STARTFRAMENR; |
1018 |
|
enc_data.stop_num = ARG_MAXFRAMENR; |
1019 |
|
|
1020 |
|
/* Encode input */ |
1021 |
|
encode_sequence(&enc_data); |
1022 |
|
|
1023 |
|
/* Copy back stats */ |
1024 |
|
input_num = enc_data.input_num; |
1025 |
|
totalsize = enc_data.totalsize; |
1026 |
|
totalenctime = enc_data.totalenctime; |
1027 |
|
for (i=0; i < 3; i++) totalPSNR[i] = enc_data.totalPSNR[i]; |
1028 |
|
memcpy(framestats, enc_data.framestats, sizeof(framestats)); |
1029 |
|
} |
1030 |
|
#ifdef USE_APP_LEVEL_THREADING |
1031 |
|
else { /* Split input into sequences and create multiple encoder instances */ |
1032 |
|
int k; |
1033 |
|
void *status; |
1034 |
|
FILE *f_out = NULL, *f_stats = NULL; |
1035 |
|
|
1036 |
|
enc_sequence_data_t enc_data[MAX_ENC_INSTANCES]; |
1037 |
|
char outfile[MAX_ENC_INSTANCES][256]; |
1038 |
|
char statsfilename[MAX_ENC_INSTANCES][256]; |
1039 |
|
|
1040 |
|
for (k = 0; k < MAX_ENC_INSTANCES; k++) |
1041 |
|
memset(&enc_data[k], 0, sizeof(enc_sequence_data_t)); |
1042 |
|
|
1043 |
|
/* Overwrite internal encoder threading */ |
1044 |
|
if (ARG_NUM_APP_THREADS > MAX_ENC_INSTANCES) { |
1045 |
|
ARG_THREADS = (int) (ARG_NUM_APP_THREADS / MAX_ENC_INSTANCES); |
1046 |
|
ARG_NUM_APP_THREADS = MAX_ENC_INSTANCES; |
1047 |
|
} |
1048 |
|
else |
1049 |
|
ARG_THREADS = -1; |
1050 |
|
|
1051 |
|
enc_data[0].outfilename = ARG_OUTPUTFILE; |
1052 |
|
enc_data[0].statsfilename1 = ARG_PASS1; |
1053 |
|
enc_data[0].start_num = ARG_STARTFRAMENR; |
1054 |
|
enc_data[0].stop_num = (ARG_MAXFRAMENR-ARG_STARTFRAMENR)/ARG_NUM_APP_THREADS; |
1055 |
|
|
1056 |
|
for (k = 1; k < ARG_NUM_APP_THREADS; k++) { |
1057 |
|
sprintf(outfile[k], "%s.%03d", ARG_OUTPUTFILE, k); |
1058 |
|
enc_data[k].outfilename = outfile[k]; |
1059 |
|
if (ARG_PASS1) { |
1060 |
|
sprintf(statsfilename[k], "%s.%03d", ARG_PASS1, k); |
1061 |
|
enc_data[k].statsfilename1 = statsfilename[k]; |
1062 |
|
} |
1063 |
|
enc_data[k].start_num = (k*(ARG_MAXFRAMENR-ARG_STARTFRAMENR))/ARG_NUM_APP_THREADS; |
1064 |
|
enc_data[k].stop_num = ((k+1)*(ARG_MAXFRAMENR-ARG_STARTFRAMENR))/ARG_NUM_APP_THREADS; |
1065 |
|
} |
1066 |
|
|
1067 |
|
/* Start multiple encoder threads in parallel */ |
1068 |
|
for (k = 1; k < ARG_NUM_APP_THREADS; k++) { |
1069 |
|
pthread_create(&enc_data[k].handle, NULL, (void*)encode_sequence, (void*)&enc_data[k]); |
1070 |
|
} |
1071 |
|
|
1072 |
|
/* Encode first sequence in this thread */ |
1073 |
|
encode_sequence(&enc_data[0]); |
1074 |
|
|
1075 |
|
/* Wait until encoder threads have finished */ |
1076 |
|
for (k = 1; k < ARG_NUM_APP_THREADS; k++) { |
1077 |
|
pthread_join(enc_data[k].handle, &status); |
1078 |
|
} |
1079 |
|
|
1080 |
|
/* Join encoder stats and encoder output files */ |
1081 |
|
if (ARG_OUTPUTFILE) |
1082 |
|
f_out = fopen(enc_data[0].outfilename, "ab+"); |
1083 |
|
if (ARG_PASS1) |
1084 |
|
f_stats = fopen(enc_data[0].statsfilename1, "ab+"); |
1085 |
|
|
1086 |
|
for (k = 0; k < ARG_NUM_APP_THREADS; k++) { |
1087 |
|
/* Join stats */ |
1088 |
|
input_num += enc_data[k].input_num; |
1089 |
|
totalsize += enc_data[k].totalsize; |
1090 |
|
totalenctime = MAX(totalenctime, enc_data[k].totalenctime); |
1091 |
|
|
1092 |
|
for (i=0; i < 3; i++) totalPSNR[i] += enc_data[k].totalPSNR[i]; |
1093 |
|
for (i=0; i < 7; i++) { |
1094 |
|
int l; |
1095 |
|
framestats[i].count += enc_data[k].framestats[i].count; |
1096 |
|
framestats[i].size += enc_data[k].framestats[i].size; |
1097 |
|
for (l=0; l < 32; l++) |
1098 |
|
framestats[i].quants[l] += enc_data[k].framestats[i].quants[l]; |
1099 |
|
} |
1100 |
|
/* Join output files */ |
1101 |
|
if ((k > 0) && (f_out != NULL)) { |
1102 |
|
int ch; |
1103 |
|
FILE *f = fopen(enc_data[k].outfilename, "rb"); |
1104 |
|
while((ch = fgetc(f)) != EOF) { fputc(ch, f_out); } |
1105 |
|
fclose(f); |
1106 |
|
remove(enc_data[k].outfilename); |
1107 |
|
} |
1108 |
|
/* Join first pass stats files */ |
1109 |
|
if ((k > 0) && (f_stats != NULL)) { |
1110 |
|
char str[256]; |
1111 |
|
FILE *f = fopen(enc_data[k].statsfilename1, "r"); |
1112 |
|
while(fgets(str, sizeof(str), f) != NULL) { |
1113 |
|
if (str[0] != '#' && strlen(str) > 3) |
1114 |
|
fputs(str, f_stats); |
1115 |
|
} |
1116 |
|
fclose(f); |
1117 |
|
remove(enc_data[k].statsfilename1); |
1118 |
|
} |
1119 |
|
} |
1120 |
|
if (f_out) fclose(f_out); |
1121 |
|
if (f_stats) fclose(f_stats); |
1122 |
|
} |
1123 |
|
#endif |
1124 |
|
|
1125 |
|
/***************************************************************************** |
1126 |
|
* Calculate totals and averages for output, print results |
1127 |
|
****************************************************************************/ |
1128 |
|
|
1129 |
|
printf("\n"); |
1130 |
|
printf("Tot: enctime(ms) =%7.2f, length(bytes) = %7d\n", |
1131 |
|
totalenctime, (int) totalsize); |
1132 |
|
|
1133 |
|
if (input_num > 0) { |
1134 |
|
totalsize /= input_num; |
1135 |
|
totalenctime /= input_num; |
1136 |
|
totalPSNR[0] /= input_num; |
1137 |
|
totalPSNR[1] /= input_num; |
1138 |
|
totalPSNR[2] /= input_num; |
1139 |
|
} else { |
1140 |
|
totalsize = -1; |
1141 |
|
totalenctime = -1; |
1142 |
|
} |
1143 |
|
|
1144 |
|
printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d", |
1145 |
|
totalenctime, 1000 / totalenctime, (int) totalsize); |
1146 |
|
if (ARG_STATS) { |
1147 |
|
printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f", |
1148 |
|
totalPSNR[0],totalPSNR[1],totalPSNR[2]); |
1149 |
|
} |
1150 |
|
printf("\n"); |
1151 |
|
if (framestats[XVID_TYPE_IVOP].count) { |
1152 |
|
printf("I frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \ |
1153 |
|
framestats[XVID_TYPE_IVOP].count, framestats[XVID_TYPE_IVOP].size/framestats[XVID_TYPE_IVOP].count, \ |
1154 |
|
framestats[XVID_TYPE_IVOP].size, minquant(framestats[XVID_TYPE_IVOP].quants), \ |
1155 |
|
avgquant(framestats[XVID_TYPE_IVOP]), maxquant(framestats[XVID_TYPE_IVOP].quants)); |
1156 |
|
} |
1157 |
|
if (framestats[XVID_TYPE_PVOP].count) { |
1158 |
|
printf("P frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \ |
1159 |
|
framestats[XVID_TYPE_PVOP].count, framestats[XVID_TYPE_PVOP].size/framestats[XVID_TYPE_PVOP].count, \ |
1160 |
|
framestats[XVID_TYPE_PVOP].size, minquant(framestats[XVID_TYPE_PVOP].quants), \ |
1161 |
|
avgquant(framestats[XVID_TYPE_PVOP]), maxquant(framestats[XVID_TYPE_PVOP].quants)); |
1162 |
|
} |
1163 |
|
if (framestats[XVID_TYPE_BVOP].count) { |
1164 |
|
printf("B frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \ |
1165 |
|
framestats[XVID_TYPE_BVOP].count, framestats[XVID_TYPE_BVOP].size/framestats[XVID_TYPE_BVOP].count, \ |
1166 |
|
framestats[XVID_TYPE_BVOP].size, minquant(framestats[XVID_TYPE_BVOP].quants), \ |
1167 |
|
avgquant(framestats[XVID_TYPE_BVOP]), maxquant(framestats[XVID_TYPE_BVOP].quants)); |
1168 |
|
} |
1169 |
|
if (framestats[XVID_TYPE_SVOP].count) { |
1170 |
|
printf("S frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \ |
1171 |
|
framestats[XVID_TYPE_SVOP].count, framestats[XVID_TYPE_SVOP].size/framestats[XVID_TYPE_SVOP].count, \ |
1172 |
|
framestats[XVID_TYPE_SVOP].size, minquant(framestats[XVID_TYPE_SVOP].quants), \ |
1173 |
|
avgquant(framestats[XVID_TYPE_SVOP]), maxquant(framestats[XVID_TYPE_SVOP].quants)); |
1174 |
|
} |
1175 |
|
if (framestats[5].count) { |
1176 |
|
printf("N frames: %6d frames, size = %7d/%7d\n", \ |
1177 |
|
framestats[5].count, framestats[5].size/framestats[5].count, \ |
1178 |
|
framestats[5].size); |
1179 |
|
} |
1180 |
|
|
1181 |
|
|
1182 |
|
/***************************************************************************** |
1183 |
|
* Xvid PART Stop |
1184 |
|
****************************************************************************/ |
1185 |
|
|
1186 |
|
release_all: |
1187 |
|
|
1188 |
|
return (0); |
1189 |
|
} |
1190 |
|
|
1191 |
|
/***************************************************************************** |
1192 |
|
* Encode a sequence |
1193 |
|
****************************************************************************/ |
1194 |
|
|
1195 |
|
void encode_sequence(enc_sequence_data_t *h) { |
1196 |
|
|
1197 |
|
/* Internal structures (handles) for encoding */ |
1198 |
|
void *enc_handle = NULL; |
1199 |
|
|
1200 |
|
int start_num = h->start_num; |
1201 |
|
int stop_num = h->stop_num; |
1202 |
|
char *outfilename = h->outfilename; |
1203 |
|
float *totalPSNR = h->totalPSNR; |
1204 |
|
|
1205 |
|
int input_num; |
1206 |
|
int totalsize; |
1207 |
|
double totalenctime = 0.; |
1208 |
|
|
1209 |
|
unsigned char *mp4_buffer = NULL; |
1210 |
|
unsigned char *in_buffer = NULL; |
1211 |
|
unsigned char *out_buffer = NULL; |
1212 |
|
|
1213 |
|
double enctime; |
1214 |
|
|
1215 |
|
int result; |
1216 |
|
int output_num; |
1217 |
|
int nvop_counter; |
1218 |
|
int m4v_size; |
1219 |
|
int key; |
1220 |
|
int stats_type; |
1221 |
|
int stats_quant; |
1222 |
|
int stats_length; |
1223 |
|
int fakenvop = 0; |
1224 |
|
|
1225 |
|
FILE *in_file = stdin; |
1226 |
|
FILE *out_file = NULL; |
1227 |
|
FILE *time_file = NULL; |
1228 |
|
|
1229 |
|
char filename[256]; |
1230 |
|
|
1231 |
|
#ifdef XVID_MKV_OUTPUT |
1232 |
|
PMKVFILE myMKVFile = NULL; |
1233 |
|
PMKVSTREAM myMKVStream = NULL; |
1234 |
|
MKVSTREAMINFO myMKVStreamInfo; |
1235 |
|
#endif |
1236 |
|
#if defined(XVID_AVI_INPUT) |
1237 |
|
PAVIFILE avi_in = NULL; |
1238 |
|
PAVISTREAM avi_in_stream = NULL; |
1239 |
|
PGETFRAME get_frame = NULL; |
1240 |
|
#else |
1241 |
|
#define get_frame NULL |
1242 |
|
#endif |
1243 |
|
#if defined(XVID_AVI_OUTPUT) |
1244 |
|
int avierr; |
1245 |
|
PAVIFILE myAVIFile = NULL; |
1246 |
|
PAVISTREAM myAVIStream = NULL; |
1247 |
|
AVISTREAMINFO myAVIStreamInfo; |
1248 |
|
#endif |
1249 |
|
#if defined(XVID_AVI_INPUT) || defined(XVID_AVI_OUTPUT) |
1250 |
|
BITMAPINFOHEADER myBitmapInfoHeader; |
1251 |
|
if (ARG_NUM_APP_THREADS > 1) |
1252 |
|
CoInitializeEx(0, COINIT_MULTITHREADED); |
1253 |
|
AVIFileInit(); |
1254 |
|
#endif |
1255 |
|
|
1256 |
|
if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
1257 |
|
in_file = stdin; |
1258 |
|
} else { |
1259 |
|
#ifdef XVID_AVI_INPUT |
1260 |
|
if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 || |
1261 |
|
strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 || |
1262 |
|
ARG_INPUTTYPE==2) |
1263 |
|
{ |
1264 |
|
AVISTREAMINFO avi_info; |
1265 |
|
FILE *avi_fp = fopen(ARG_INPUTFILE, "rb"); |
1266 |
|
|
1267 |
|
if (avi_fp == NULL) { |
1268 |
|
fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE); |
1269 |
|
return; |
1270 |
|
} |
1271 |
|
fclose(avi_fp); |
1272 |
|
|
1273 |
|
if (AVIFileOpen(&avi_in, ARG_INPUTFILE, OF_READ, NULL) != AVIERR_OK) { |
1274 |
|
fprintf(stderr, "Can't open avi/avs file %s\n", ARG_INPUTFILE); |
1275 |
|
AVIFileExit(); |
1276 |
|
return; |
1277 |
|
} |
1278 |
|
|
1279 |
|
if (AVIFileGetStream(avi_in, &avi_in_stream, streamtypeVIDEO, 0) != AVIERR_OK) { |
1280 |
|
fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE); |
1281 |
|
AVIFileRelease(avi_in); |
1282 |
|
AVIFileExit(); |
1283 |
|
return; |
1284 |
|
} |
1285 |
|
|
1286 |
|
AVIFileRelease(avi_in); |
1287 |
|
|
1288 |
|
if(AVIStreamInfo(avi_in_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) { |
1289 |
|
fprintf(stderr, "Can't get stream info from file '%s'!\n", ARG_INPUTFILE); |
1290 |
|
AVIStreamRelease(avi_in_stream); |
1291 |
|
AVIFileExit(); |
1292 |
|
return; |
1293 |
|
} |
1294 |
|
|
1295 |
|
if (avi_info.fccHandler != MAKEFOURCC('Y', 'V', '1', '2')) { |
1296 |
|
LONG size; |
1297 |
|
fprintf(stderr, "Non YV12 input colorspace %c%c%c%c! Attempting conversion...\n", |
1298 |
|
avi_info.fccHandler%256, (avi_info.fccHandler>>8)%256, (avi_info.fccHandler>>16)%256, |
1299 |
|
(avi_info.fccHandler>>24)%256); |
1300 |
|
size = sizeof(myBitmapInfoHeader); |
1301 |
|
AVIStreamReadFormat(avi_in_stream, 0, &myBitmapInfoHeader, &size); |
1302 |
|
if (size==0) |
1303 |
|
fprintf(stderr, "AVIStreamReadFormat read 0 bytes.\n"); |
1304 |
|
else { |
1305 |
|
fprintf(stderr, "AVIStreamReadFormat read %d bytes.\n", size); |
1306 |
|
fprintf(stderr, "width = %d, height = %d, planes = %d\n", myBitmapInfoHeader.biWidth, |
1307 |
|
myBitmapInfoHeader.biHeight, myBitmapInfoHeader.biPlanes); |
1308 |
|
fprintf(stderr, "Compression = %c%c%c%c, %d\n", |
1309 |
|
myBitmapInfoHeader.biCompression%256, (myBitmapInfoHeader.biCompression>>8)%256, |
1310 |
|
(myBitmapInfoHeader.biCompression>>16)%256, (myBitmapInfoHeader.biCompression>>24)%256, |
1311 |
|
myBitmapInfoHeader.biCompression); |
1312 |
|
fprintf(stderr, "Bits Per Pixel = %d\n", myBitmapInfoHeader.biBitCount); |
1313 |
|
myBitmapInfoHeader.biCompression = MAKEFOURCC('Y', 'V', '1', '2'); |
1314 |
|
myBitmapInfoHeader.biBitCount = 12; |
1315 |
|
myBitmapInfoHeader.biSizeImage = (myBitmapInfoHeader.biWidth*myBitmapInfoHeader.biHeight)*3/2; |
1316 |
|
get_frame = AVIStreamGetFrameOpen(avi_in_stream, &myBitmapInfoHeader); |
1317 |
|
} |
1318 |
|
if (get_frame == NULL) { |
1319 |
|
AVIStreamRelease(avi_in_stream); |
1320 |
|
AVIFileExit(); |
1321 |
|
return; |
1322 |
|
} |
1323 |
|
else { |
1324 |
|
unsigned char *temp; |
1325 |
|
fprintf(stderr, "AVIStreamGetFrameOpen successful.\n"); |
1326 |
|
temp = (unsigned char*)AVIStreamGetFrame(get_frame, 0); |
1327 |
|
if (temp != NULL) { |
1328 |
|
int i; |
1329 |
|
for (i = 0; i < (int)((DWORD*)temp)[0]; i++) { |
1330 |
|
fprintf(stderr, "%2d ", temp[i]); |
1331 |
|
} |
1332 |
|
fprintf(stderr, "\n"); |
1333 |
|
} |
1334 |
|
} |
1335 |
|
if (avi_info.fccHandler == MAKEFOURCC('D', 'I', 'B', ' ')) { |
1336 |
|
AVIStreamGetFrameClose(get_frame); |
1337 |
|
get_frame = NULL; |
1338 |
|
ARG_COLORSPACE = XVID_CSP_BGR | XVID_CSP_VFLIP; |
1339 |
|
} |
1340 |
|
} |
1341 |
|
} |
1342 |
|
else |
1343 |
|
#endif |
1344 |
|
{ |
1345 |
|
in_file = fopen(ARG_INPUTFILE, "rb"); |
1346 |
|
if (in_file == NULL) { |
1347 |
|
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
1348 |
|
return; |
1349 |
|
} |
1350 |
|
} |
1351 |
|
} |
1352 |
|
|
1353 |
|
// This should be after the avi input opening stuff |
1354 |
|
if (ARG_TIMECODEFILE != NULL) { |
1355 |
|
time_file = fopen(ARG_TIMECODEFILE, "r"); |
1356 |
|
if (time_file==NULL) { |
1357 |
|
fprintf(stderr, "Couldn't open timecode file '%s'!\n", ARG_TIMECODEFILE); |
1358 |
|
return; |
1359 |
|
} |
1360 |
|
else { |
1361 |
|
fscanf(time_file, "# timecode format v2\n"); |
1362 |
|
} |
1363 |
|
} |
1364 |
|
|
1365 |
if (ARG_INPUTTYPE==1) { |
if (ARG_INPUTTYPE==1) { |
1366 |
#ifndef READ_PNM |
#ifndef READ_PNM |
1367 |
if (read_pgmheader(in_file)) { |
if (read_pgmheader(in_file)) { |
1370 |
#endif |
#endif |
1371 |
fprintf(stderr, |
fprintf(stderr, |
1372 |
"Wrong input format, I want YUV encapsulated in PGM\n"); |
"Wrong input format, I want YUV encapsulated in PGM\n"); |
1373 |
return (-1); |
return; |
1374 |
} |
} |
1375 |
} |
} |
1376 |
|
|
1377 |
/* Jump to the starting frame */ |
/* Jump to the starting frame */ |
1378 |
if (ARG_INPUTTYPE == 0) |
if (ARG_INPUTTYPE == 0) /* TODO: Other input formats ??? */ |
1379 |
fseek(in_file, ARG_STARTFRAMENR*IMAGE_SIZE(XDIM, YDIM), SEEK_SET); |
fseek(in_file, start_num*IMAGE_SIZE(XDIM, YDIM), SEEK_SET); |
1380 |
|
|
1381 |
|
|
1382 |
/* now we know the sizes, so allocate memory */ |
/* now we know the sizes, so allocate memory */ |
1383 |
if (get_frame == NULL) |
if (get_frame == NULL) |
1393 |
goto free_all_memory; |
goto free_all_memory; |
1394 |
|
|
1395 |
/***************************************************************************** |
/***************************************************************************** |
1396 |
* XviD PART Start |
* Xvid PART Start |
1397 |
****************************************************************************/ |
****************************************************************************/ |
1398 |
|
|
1399 |
|
|
1400 |
result = enc_init(use_assembler); |
result = enc_init(&enc_handle, h->statsfilename1, h->start_num); |
1401 |
if (result) { |
if (result) { |
1402 |
fprintf(stderr, "Encore INIT problem, return value %d\n", result); |
fprintf(stderr, "Encore INIT problem, return value %d\n", result); |
1403 |
goto release_all; |
goto release_all; |
1409 |
|
|
1410 |
if (ARG_SAVEMPEGSTREAM) { |
if (ARG_SAVEMPEGSTREAM) { |
1411 |
|
|
1412 |
if (ARG_OUTPUTFILE) { |
if (outfilename) { |
1413 |
if ((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) { |
if ((out_file = fopen(outfilename, "w+b")) == NULL) { |
1414 |
fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE); |
fprintf(stderr, "Error opening output file %s\n", outfilename); |
1415 |
goto release_all; |
goto release_all; |
1416 |
} |
} |
1417 |
} |
} |
1436 |
myAVIStreamInfo.dwRate = ARG_DWRATE; |
myAVIStreamInfo.dwRate = ARG_DWRATE; |
1437 |
myAVIStreamInfo.dwLength = ARG_MAXFRAMENR; |
myAVIStreamInfo.dwLength = ARG_MAXFRAMENR; |
1438 |
myAVIStreamInfo.dwQuality = 10000; |
myAVIStreamInfo.dwQuality = 10000; |
1439 |
SetRect(&myAVIStreamInfo.rcFrame, 0, 0, YDIM, XDIM); |
SetRect(&myAVIStreamInfo.rcFrame, 0, 0, XDIM, YDIM); |
1440 |
|
|
1441 |
if (avierr=AVIFileOpen(&myAVIFile, ARG_AVIOUTPUTFILE, OF_CREATE|OF_WRITE, NULL)) { |
if (avierr=AVIFileOpen(&myAVIFile, ARG_AVIOUTPUTFILE, OF_CREATE|OF_WRITE, NULL)) { |
1442 |
fprintf(stderr, "AVIFileOpen failed opening output file %s, error code %d\n", ARG_AVIOUTPUTFILE, avierr); |
fprintf(stderr, "AVIFileOpen failed opening output file %s, error code %d\n", ARG_AVIOUTPUTFILE, avierr); |
1507 |
result = 0; |
result = 0; |
1508 |
|
|
1509 |
input_num = 0; /* input frame counter */ |
input_num = 0; /* input frame counter */ |
1510 |
output_num = 0; /* output frame counter */ |
output_num = start_num; /* output frame counter */ |
1511 |
|
|
1512 |
nvop_counter = 0; |
nvop_counter = 0; |
1513 |
|
|
1516 |
char *type; |
char *type; |
1517 |
int sse[3]; |
int sse[3]; |
1518 |
|
|
1519 |
if (input_num >= ARG_MAXFRAMENR && ARG_MAXFRAMENR > 0) { |
if ((input_num+start_num) >= stop_num && stop_num > 0) { |
1520 |
result = 1; |
result = 1; |
1521 |
} |
} |
1522 |
|
|
1525 |
if (ARG_INPUTTYPE==2) { |
if (ARG_INPUTTYPE==2) { |
1526 |
/* read avs/avi data (YUV-format) */ |
/* read avs/avi data (YUV-format) */ |
1527 |
if (get_frame != NULL) { |
if (get_frame != NULL) { |
1528 |
in_buffer = (unsigned char*)AVIStreamGetFrame(get_frame, input_num+ARG_STARTFRAMENR); |
in_buffer = (unsigned char*)AVIStreamGetFrame(get_frame, input_num+start_num); |
1529 |
if (in_buffer == NULL) |
if (in_buffer == NULL) |
1530 |
result = 1; |
result = 1; |
1531 |
else |
else |
1532 |
in_buffer += ((DWORD*)in_buffer)[0]; |
in_buffer += ((DWORD*)in_buffer)[0]; |
1533 |
} else { |
} else { |
1534 |
if(AVIStreamRead(avi_stream, input_num+ARG_STARTFRAMENR, 1, in_buffer, 4*XDIM*YDIM, NULL, NULL ) != AVIERR_OK) |
if(AVIStreamRead(avi_in_stream, input_num+start_num, 1, in_buffer, 4*XDIM*YDIM, NULL, NULL ) != AVIERR_OK) |
1535 |
result = 1; |
result = 1; |
1536 |
} |
} |
1537 |
} else |
} else |
1553 |
* Encode and decode this frame |
* Encode and decode this frame |
1554 |
****************************************************************************/ |
****************************************************************************/ |
1555 |
|
|
1556 |
if (input_num >= (unsigned int)ARG_MAXFRAMENR-1 && ARG_MAXBFRAMES) { |
if ((unsigned int)(input_num+start_num) >= (unsigned int)(stop_num-1) && ARG_MAXBFRAMES) { |
1557 |
stats_type = XVID_TYPE_PVOP; |
stats_type = XVID_TYPE_PVOP; |
1558 |
} |
} |
1559 |
else |
else |
1561 |
|
|
1562 |
enctime = msecond(); |
enctime = msecond(); |
1563 |
m4v_size = |
m4v_size = |
1564 |
enc_main(!result ? in_buffer : 0, mp4_buffer, &key, &stats_type, |
enc_main(enc_handle, !result ? in_buffer : 0, mp4_buffer, &key, &stats_type, |
1565 |
&stats_quant, &stats_length, sse, input_num); |
&stats_quant, &stats_length, sse, input_num); |
1566 |
enctime = msecond() - enctime; |
enctime = msecond() - enctime; |
1567 |
|
|
1589 |
} |
} |
1590 |
|
|
1591 |
if (stats_length > 8) { |
if (stats_length > 8) { |
1592 |
framestats[stats_type].count++; |
h->framestats[stats_type].count++; |
1593 |
framestats[stats_type].quants[stats_quant]++; |
h->framestats[stats_type].quants[stats_quant]++; |
1594 |
framestats[stats_type].size += stats_length; |
h->framestats[stats_type].size += stats_length; |
1595 |
} |
} |
1596 |
else { |
else { |
1597 |
framestats[5].count++; |
h->framestats[5].count++; |
1598 |
framestats[5].quants[stats_quant]++; |
h->framestats[5].quants[stats_quant]++; |
1599 |
framestats[5].size += stats_length; |
h->framestats[5].size += stats_length; |
1600 |
} |
} |
1601 |
|
|
1602 |
#define SSE2PSNR(sse, width, height) ((!(sse))?0.0f : 48.131f - 10*(float)log10((float)(sse)/((float)((width)*(height))))) |
#define SSE2PSNR(sse, width, height) ((!(sse))?0.0f : 48.131f - 10*(float)log10((float)(sse)/((float)((width)*(height))))) |
1603 |
|
|
1604 |
if (ARG_PROGRESS == 0) { |
if (ARG_PROGRESS == 0) { |
1605 |
printf("%5d: key=%i, time= %6.0f, len= %7d", !result ? input_num : -1, |
printf("%5d: key=%i, time= %6.0f, len= %7d", !result ? (input_num+start_num) : -1, |
1606 |
key, (float) enctime, (int) m4v_size); |
key, (float) enctime, (int) m4v_size); |
1607 |
printf(" | type=%s, quant= %2d, len= %7d", type, stats_quant, |
printf(" | type=%s, quant= %2d, len= %7d", type, stats_quant, |
1608 |
stats_length); |
stats_length); |
1615 |
} |
} |
1616 |
printf("\n"); |
printf("\n"); |
1617 |
} else { |
} else { |
1618 |
if (input_num % ARG_PROGRESS == 1) { |
if ((input_num) % ARG_PROGRESS == 1) { |
1619 |
if (ARG_MAXFRAMENR > 0) { |
if (stop_num > 0) { |
1620 |
fprintf(stderr, "\r%7d frames(%3d%%) encoded, %6.2f fps, Average Bitrate = %5.0fkbps", \ |
fprintf(stderr, "\r%7d frames(%3d%%) encoded, %6.2f fps, Average Bitrate = %5.0fkbps", \ |
1621 |
input_num, input_num*100/ARG_MAXFRAMENR, input_num*1000/totalenctime, \ |
(ARG_NUM_APP_THREADS*input_num), (input_num)*100/(stop_num-start_num), (ARG_NUM_APP_THREADS*input_num)*1000/(totalenctime), \ |
1622 |
(((totalsize/1000)*ARG_FRAMERATE)*8)/input_num); |
((((totalsize)/1000)*ARG_FRAMERATE)*8)/(input_num)); |
1623 |
} else { |
} else { |
1624 |
fprintf(stderr, "\r%7d frames encoded, %6.2f fps, Average Bitrate = %5.0fkbps", \ |
fprintf(stderr, "\r%7d frames encoded, %6.2f fps, Average Bitrate = %5.0fkbps", \ |
1625 |
input_num, input_num*1000/totalenctime, \ |
(ARG_NUM_APP_THREADS*input_num), (ARG_NUM_APP_THREADS*input_num)*1000/(totalenctime), \ |
1626 |
(((totalsize/1000)*ARG_FRAMERATE)*8)/input_num); |
((((totalsize)/1000)*ARG_FRAMERATE)*8)/(input_num)); |
1627 |
} |
} |
1628 |
} |
} |
1629 |
} |
} |
1687 |
removedivxp((char*)mp4_buffer, m4v_size); |
removedivxp((char*)mp4_buffer, m4v_size); |
1688 |
|
|
1689 |
/* Save ES stream */ |
/* Save ES stream */ |
1690 |
if (ARG_OUTPUTFILE && out_file && !(fakenvop && m4v_size <= 8)) { |
if (outfilename && out_file && !(fakenvop && m4v_size <= 8)) { |
1691 |
fwrite(mp4_buffer, 1, m4v_size, out_file); |
fwrite(mp4_buffer, 1, m4v_size, out_file); |
1692 |
} |
} |
1693 |
#ifdef XVID_MKV_OUTPUT |
#ifdef XVID_MKV_OUTPUT |
1701 |
fakenvop=0; |
fakenvop=0; |
1702 |
} |
} |
1703 |
|
|
1704 |
input_num++; |
if (!result) |
1705 |
|
(input_num)++; |
1706 |
|
|
1707 |
/* Read the header if it's pgm stream */ |
/* Read the header if it's pgm stream */ |
1708 |
if (!result && (ARG_INPUTTYPE==1)) |
if (!result && (ARG_INPUTTYPE==1)) |
1714 |
} while (1); |
} while (1); |
1715 |
|
|
1716 |
|
|
|
|
|
|
/***************************************************************************** |
|
|
* Calculate totals and averages for output, print results |
|
|
****************************************************************************/ |
|
|
|
|
|
printf("\n"); |
|
|
printf("Tot: enctime(ms) =%7.2f, length(bytes) = %7d\n", |
|
|
totalenctime, (int) totalsize); |
|
|
|
|
|
if (input_num > 0) { |
|
|
totalsize /= input_num; |
|
|
totalenctime /= input_num; |
|
|
totalPSNR[0] /= input_num; |
|
|
totalPSNR[1] /= input_num; |
|
|
totalPSNR[2] /= input_num; |
|
|
} else { |
|
|
totalsize = -1; |
|
|
totalenctime = -1; |
|
|
} |
|
|
|
|
|
printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d", |
|
|
totalenctime, 1000 / totalenctime, (int) totalsize); |
|
|
if (ARG_STATS) { |
|
|
printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f", |
|
|
totalPSNR[0],totalPSNR[1],totalPSNR[2]); |
|
|
} |
|
|
printf("\n"); |
|
|
if (framestats[XVID_TYPE_IVOP].count) { |
|
|
printf("I frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \ |
|
|
framestats[XVID_TYPE_IVOP].count, framestats[XVID_TYPE_IVOP].size/framestats[XVID_TYPE_IVOP].count, \ |
|
|
framestats[XVID_TYPE_IVOP].size, minquant(framestats[XVID_TYPE_IVOP].quants), \ |
|
|
avgquant(framestats[XVID_TYPE_IVOP]), maxquant(framestats[XVID_TYPE_IVOP].quants)); |
|
|
} |
|
|
if (framestats[XVID_TYPE_PVOP].count) { |
|
|
printf("P frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \ |
|
|
framestats[XVID_TYPE_PVOP].count, framestats[XVID_TYPE_PVOP].size/framestats[XVID_TYPE_PVOP].count, \ |
|
|
framestats[XVID_TYPE_PVOP].size, minquant(framestats[XVID_TYPE_PVOP].quants), \ |
|
|
avgquant(framestats[XVID_TYPE_PVOP]), maxquant(framestats[XVID_TYPE_PVOP].quants)); |
|
|
} |
|
|
if (framestats[XVID_TYPE_BVOP].count) { |
|
|
printf("B frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \ |
|
|
framestats[XVID_TYPE_BVOP].count, framestats[XVID_TYPE_BVOP].size/framestats[XVID_TYPE_BVOP].count, \ |
|
|
framestats[XVID_TYPE_BVOP].size, minquant(framestats[XVID_TYPE_BVOP].quants), \ |
|
|
avgquant(framestats[XVID_TYPE_BVOP]), maxquant(framestats[XVID_TYPE_BVOP].quants)); |
|
|
} |
|
|
if (framestats[XVID_TYPE_SVOP].count) { |
|
|
printf("S frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \ |
|
|
framestats[XVID_TYPE_SVOP].count, framestats[XVID_TYPE_SVOP].size/framestats[XVID_TYPE_SVOP].count, \ |
|
|
framestats[XVID_TYPE_SVOP].size, minquant(framestats[XVID_TYPE_SVOP].quants), \ |
|
|
avgquant(framestats[XVID_TYPE_SVOP]), maxquant(framestats[XVID_TYPE_SVOP].quants)); |
|
|
} |
|
|
if (framestats[5].count) { |
|
|
printf("N frames: %6d frames, size = %7d/%7d\n", \ |
|
|
framestats[5].count, framestats[5].size/framestats[5].count, \ |
|
|
framestats[5].size); |
|
|
} |
|
|
|
|
|
|
|
|
/***************************************************************************** |
|
|
* XviD PART Stop |
|
|
****************************************************************************/ |
|
|
|
|
1717 |
release_all: |
release_all: |
1718 |
|
|
1719 |
|
h->input_num = input_num; |
1720 |
|
h->totalenctime = totalenctime; |
1721 |
|
h->totalsize = totalsize; |
1722 |
|
|
1723 |
#ifdef XVID_AVI_INPUT |
#ifdef XVID_AVI_INPUT |
1724 |
if (get_frame) AVIStreamGetFrameClose(get_frame); |
if (get_frame) AVIStreamGetFrameClose(get_frame); |
1725 |
if (avi_stream) AVIStreamRelease(avi_stream); |
if (avi_in_stream) AVIStreamRelease(avi_in_stream); |
|
#ifndef XVID_AVI_OUTPUT |
|
|
AVIFileExit(); |
|
|
#endif |
|
1726 |
#endif |
#endif |
1727 |
|
|
1728 |
if (enc_handle) { |
if (enc_handle) { |
1729 |
result = enc_stop(); |
result = enc_stop(enc_handle); |
1730 |
if (result) |
if (result) |
1731 |
fprintf(stderr, "Encore RELEASE problem return value %d\n", |
fprintf(stderr, "Encore RELEASE problem return value %d\n", |
1732 |
result); |
result); |
1736 |
fclose(in_file); |
fclose(in_file); |
1737 |
if (out_file) |
if (out_file) |
1738 |
fclose(out_file); |
fclose(out_file); |
1739 |
|
if (time_file) |
1740 |
|
fclose(time_file); |
1741 |
|
|
1742 |
#ifdef XVID_AVI_OUTPUT |
#ifdef XVID_AVI_OUTPUT |
1743 |
if (myAVIStream) AVIStreamRelease(myAVIStream); |
if (myAVIStream) AVIStreamRelease(myAVIStream); |
1744 |
if (myAVIFile) AVIFileRelease(myAVIFile); |
if (myAVIFile) AVIFileRelease(myAVIFile); |
|
AVIFileExit(); |
|
1745 |
#endif |
#endif |
1746 |
#ifdef XVID_MKV_OUTPUT |
#ifdef XVID_MKV_OUTPUT |
1747 |
if (myMKVStream) MKVStreamRelease(myMKVStream); |
if (myMKVStream) MKVStreamRelease(myMKVStream); |
1748 |
if (myMKVFile) MKVFileRelease(myMKVFile); |
if (myMKVFile) MKVFileRelease(myMKVFile); |
1749 |
#endif |
#endif |
1750 |
|
#if defined(XVID_AVI_INPUT) || defined(XVID_AVI_OUTPUT) |
1751 |
|
AVIFileExit(); |
1752 |
|
#endif |
1753 |
|
|
1754 |
free_all_memory: |
free_all_memory: |
1755 |
free(out_buffer); |
free(out_buffer); |
1756 |
free(mp4_buffer); |
free(mp4_buffer); |
1757 |
free(in_buffer); |
free(in_buffer); |
|
|
|
|
return (0); |
|
|
|
|
1758 |
} |
} |
1759 |
|
|
|
|
|
1760 |
/***************************************************************************** |
/***************************************************************************** |
1761 |
* "statistical" functions |
* "statistical" functions |
1762 |
* |
* |
1763 |
* these are not needed for encoding or decoding, but for measuring |
* these are not needed for encoding or decoding, but for measuring |
1764 |
* time and quality, there in nothing specific to XviD in these |
* time and quality, there in nothing specific to Xvid in these |
1765 |
* |
* |
1766 |
*****************************************************************************/ |
*****************************************************************************/ |
1767 |
|
|
1841 |
fprintf(stderr, "Input options:\n"); |
fprintf(stderr, "Input options:\n"); |
1842 |
fprintf(stderr, " -i string : input filename (stdin)\n"); |
fprintf(stderr, " -i string : input filename (stdin)\n"); |
1843 |
#ifdef XVID_AVI_INPUT |
#ifdef XVID_AVI_INPUT |
1844 |
fprintf(stderr, " -type integer: input data type (yuv=0, pgm=1, avi/avs=2)\n"); |
fprintf(stderr, " -type integer : input data type (yuv=0, pgm=1, avi/avs=2) ("); |
1845 |
#else |
#else |
1846 |
fprintf(stderr, " -type integer: input data type (yuv=0, pgm=1)\n"); |
fprintf(stderr, " -type integer : input data type (yuv=0, pgm=1) ("); |
1847 |
#endif |
#endif |
1848 |
fprintf(stderr, " -w integer: frame width ([1.2048])\n"); |
fprintf(stderr, "%d)\n", ARG_INPUTTYPE); |
1849 |
fprintf(stderr, " -h integer: frame height ([1.2048])\n"); |
fprintf(stderr, " -w integer : frame width ([1.%d])\n", MAX_XDIM); |
1850 |
fprintf(stderr, " -csp string : colorspace of raw input file i420, yv12 (default)\n"); |
fprintf(stderr, " -h integer : frame height ([1.%d])\n", MAX_YDIM); |
1851 |
fprintf(stderr, " -frames integer: number of frames to encode\n"); |
fprintf(stderr, " -csp string : colorspace of raw input file i420%s, yv12%s\n", (ARG_COLORSPACE == XVID_CSP_I420)?" (default)":"", (ARG_COLORSPACE == XVID_CSP_YV12)?" (default)":""); |
1852 |
fprintf(stderr, "\n"); |
fprintf(stderr, " -frames integer : number of frames to encode ("); |
1853 |
|
if (ARG_MAXFRAMENR==-1) |
1854 |
|
fprintf(stderr, "all)"); |
1855 |
|
else |
1856 |
|
fprintf(stderr, "%d)", ARG_MAXFRAMENR); |
1857 |
|
fprintf(stderr, "\n\n"); |
1858 |
fprintf(stderr, "Output options:\n"); |
fprintf(stderr, "Output options:\n"); |
1859 |
fprintf(stderr, " -dump : save decoder output\n"); |
fprintf(stderr, " -dump : save decoder output\n"); |
1860 |
fprintf(stderr, " -save : save an Elementary Stream file per frame\n"); |
fprintf(stderr, " -save : save an Elementary Stream file per frame\n"); |
1862 |
#ifdef XVID_AVI_OUTPUT |
#ifdef XVID_AVI_OUTPUT |
1863 |
fprintf(stderr, " -avi string: save an AVI file for the complete sequence\n"); |
fprintf(stderr, " -avi string: save an AVI file for the complete sequence\n"); |
1864 |
#endif |
#endif |
1865 |
|
#ifdef XVID_MKV_OUTPUT |
1866 |
fprintf(stderr, " -mkv string: save a MKV file for the complete sequence\n"); |
fprintf(stderr, " -mkv string: save a MKV file for the complete sequence\n"); |
1867 |
fprintf(stderr, "\n"); |
#endif |
|
fprintf(stderr, "BFrames options:\n"); |
|
|
fprintf(stderr, " -max_bframes integer: max bframes (2)\n"); |
|
|
fprintf(stderr, " -bquant_ratio integer: bframe quantizer ratio (150)\n"); |
|
|
fprintf(stderr, " -bquant_offset integer: bframe quantizer offset (100)\n"); |
|
1868 |
fprintf(stderr, "\n"); |
fprintf(stderr, "\n"); |
1869 |
fprintf(stderr, "Rate control options:\n"); |
fprintf(stderr, "Rate control options:\n"); |
1870 |
fprintf(stderr, " -framerate float : target framerate (25.0)\n"); |
fprintf(stderr, " -framerate float : target framerate (auto)\n"); |
1871 |
fprintf(stderr, " -bitrate [integer] : target bitrate in kbps (700)\n"); |
fprintf(stderr, " -bitrate [integer] : target bitrate in kbps (%d)\n", DEFAULT_BITRATE/1000); |
1872 |
fprintf(stderr, " -size integer : target size in kilobytes\n"); |
fprintf(stderr, " -size integer : target size in kilobytes\n"); |
1873 |
fprintf(stderr, " -single : single pass mode (default)\n"); |
fprintf(stderr, " -single : single pass mode%s\n", (ARG_SINGLE)?" (default)":""); |
1874 |
fprintf(stderr, " -cq float : single pass constant quantizer\n"); |
fprintf(stderr, " -cq float : single pass constant quantizer\n"); |
1875 |
fprintf(stderr, " -pass1 [filename] : twopass mode (first pass)\n"); |
fprintf(stderr, " -pass1 [filename] : twopass mode (first pass)\n"); |
1876 |
fprintf(stderr, " -full1pass : perform full first pass\n"); |
fprintf(stderr, " -full1pass : perform full quality first pass (disabled)\n"); |
1877 |
fprintf(stderr, " -pass2 [filename] : twopass mode (2nd pass)\n"); |
fprintf(stderr, " -pass2 [filename] : twopass mode (2nd pass)\n"); |
1878 |
|
fprintf(stderr, " -max_key_interval integer : maximum keyframe interval (%d)\n", ARG_MAXKEYINTERVAL); |
1879 |
fprintf(stderr, " -zq starting_frame float : bitrate zone; quant\n"); |
fprintf(stderr, " -zq starting_frame float : bitrate zone; quant\n"); |
1880 |
fprintf(stderr, " -zw starting_frame float : bitrate zone; weight\n"); |
fprintf(stderr, " -zw starting_frame float : bitrate zone; weight\n"); |
|
fprintf(stderr, " -max_key_interval integer : maximum keyframe interval (300)\n"); |
|
1881 |
fprintf(stderr, "\n"); |
fprintf(stderr, "\n"); |
1882 |
fprintf(stderr, "Single Pass options:\n"); |
fprintf(stderr, "Single Pass options:\n"); |
1883 |
fprintf(stderr, "-reaction integer : reaction delay factor (16)\n"); |
fprintf(stderr, " -reaction integer : reaction delay factor (%d)\n", ARG_REACTION); |
1884 |
fprintf(stderr, "-averaging integer : averaging period (100)\n"); |
fprintf(stderr, " -averaging integer : averaging period (%d)\n", ARG_AVERAGING); |
1885 |
fprintf(stderr, "-smoother integer : smoothing buffer (100)\n"); |
fprintf(stderr, " -smoother integer : smoothing buffer (%d)\n", ARG_SMOOTHER); |
1886 |
fprintf(stderr, "\n"); |
fprintf(stderr, "\n"); |
1887 |
fprintf(stderr, "Second Pass options:\n"); |
fprintf(stderr, "Second Pass options:\n"); |
1888 |
fprintf(stderr, "-kboost integer : I frame boost (10)\n"); |
fprintf(stderr, " -kboost integer : I frame boost (%d)\n", ARG_KBOOST); |
1889 |
fprintf(stderr, "-kthresh integer : I frame reduction threshold (1)\n"); |
fprintf(stderr, " -kthresh integer : I frame reduction threshold (%d)\n", ARG_KTHRESH); |
1890 |
fprintf(stderr, "-kreduction integer : I frame reduction amount (20)\n"); |
fprintf(stderr, " -kreduction integer : I frame reduction amount (%d)\n", ARG_KREDUCTION); |
1891 |
fprintf(stderr, "-ostrength integer : overflow control strength (5)\n"); |
fprintf(stderr, " -ostrength integer : overflow control strength (%d)\n", ARG_OVERSTRENGTH); |
1892 |
fprintf(stderr, "-oimprove integer : max overflow improvement (5)\n"); |
fprintf(stderr, " -oimprove integer : max overflow improvement (%d)\n", ARG_OVERIMPROVE); |
1893 |
fprintf(stderr, "-odegrade integer : max overflow degradation (5)\n"); |
fprintf(stderr, " -odegrade integer : max overflow degradation (%d)\n", ARG_OVERDEGRADE); |
1894 |
fprintf(stderr, "-chigh integer : high bitrate scenes degradation (0)\n"); |
fprintf(stderr, " -chigh integer : high bitrate scenes degradation (%d)\n", ARG_CHIGH); |
1895 |
fprintf(stderr, "-clow integer : low bitrate scenes improvement (0)\n"); |
fprintf(stderr, " -clow integer : low bitrate scenes improvement (%d)\n", ARG_CLOW); |
1896 |
fprintf(stderr, "-overhead integer : container frame overhead (24)\n"); |
fprintf(stderr, " -overhead integer : container frame overhead (%d)\n", ARG_OVERHEAD); |
1897 |
fprintf(stderr, "-vbvsize integer : use vbv buffer size\n"); |
fprintf(stderr, "-vbvsize integer : use vbv buffer size\n"); |
1898 |
fprintf(stderr, "-vbvmax integer : vbv max bitrate\n"); |
fprintf(stderr, "-vbvmax integer : vbv max bitrate\n"); |
1899 |
fprintf(stderr, "-vbvpeak integer : vbv peak bitrate over 1 second\n"); |
fprintf(stderr, "-vbvpeak integer : vbv peak bitrate over 1 second\n"); |
1900 |
fprintf(stderr, "\n"); |
fprintf(stderr, "\n"); |
1901 |
|
fprintf(stderr, "BFrames options:\n"); |
1902 |
|
fprintf(stderr, " -max_bframes integer : max bframes (%d)\n", ARG_MAXBFRAMES); |
1903 |
|
fprintf(stderr, " -bquant_ratio integer : bframe quantizer ratio (%d)\n", ARG_BQRATIO); |
1904 |
|
fprintf(stderr, " -bquant_offset integer : bframe quantizer offset (%d)\n", ARG_BQOFFSET); |
1905 |
|
fprintf(stderr, "\n"); |
1906 |
fprintf(stderr, "Other options\n"); |
fprintf(stderr, "Other options\n"); |
1907 |
fprintf(stderr, " -noasm : do not use assembly optmized code\n"); |
if (ARG_USE_ASSEMBLER) |
1908 |
fprintf(stderr, " -turbo : use turbo presets for higher encoding speed\n"); |
fprintf(stderr, " -noasm : do not use assembly optimized code (use)\n"); |
1909 |
fprintf(stderr, " -quality integer : quality ([0..%d]) (6)\n", ME_ELEMENTS - 1); |
else |
1910 |
fprintf(stderr, " -vhqmode integer : level of R-D optimizations ([0..4]) (1)\n"); |
fprintf(stderr, " -asm : use assembly optimized code (don't use)\n"); |
1911 |
fprintf(stderr, " -bvhq : use R-D optimizations for B-frames\n"); |
if (ARG_TURBO) |
1912 |
fprintf(stderr, " -qpel : use quarter pixel ME\n"); |
fprintf(stderr, " -noturbo : do not use turbo presets for higher encoding speed (use)\n"); |
1913 |
fprintf(stderr, " -gmc : use global motion compensation\n"); |
else |
1914 |
fprintf(stderr, " -qtype integer : quantization type (H263:0, MPEG4:1) (0)\n"); |
fprintf(stderr, " -turbo : use turbo presets for higher encoding speed (don't use)\n"); |
1915 |
|
fprintf(stderr, " -quality integer : quality ([0..%d]) (%d)\n", ME_ELEMENTS - 1, ARG_QUALITY); |
1916 |
|
fprintf(stderr, " -vhqmode integer : level of R-D optimizations ([0..4]) (%d)\n", ARG_VHQMODE); |
1917 |
|
if (ARG_BVHQ) |
1918 |
|
fprintf(stderr, " -nobvhq : do not use R-D optimizations for B-frames (use)\n"); |
1919 |
|
else |
1920 |
|
fprintf(stderr, " -bvhq : use R-D optimizations for B-frames (don't use)\n"); |
1921 |
|
fprintf(stderr, " -metric integer : distortion metric for R-D opt (PSNR:0, PSNRHVSM:1) (%d)\n", ARG_QMETRIC); |
1922 |
|
if (ARG_QPEL) |
1923 |
|
fprintf(stderr, " -noqpel : do not use quarter pixel ME (use)\n"); |
1924 |
|
else |
1925 |
|
fprintf(stderr, " -qpel : use quarter pixel ME (don't use)\n"); |
1926 |
|
if (ARG_GMC) |
1927 |
|
fprintf(stderr, " -nogmc : do not use global motion compensation (use)\n"); |
1928 |
|
else |
1929 |
|
fprintf(stderr, " -gmc : use global motion compensation (don't use)\n"); |
1930 |
|
fprintf(stderr, " -qtype integer : quantization type (H263:0, MPEG4:1) (%d)\n", ARG_QTYPE); |
1931 |
fprintf(stderr, " -qmatrix filename : use custom MPEG4 quantization matrix\n"); |
fprintf(stderr, " -qmatrix filename : use custom MPEG4 quantization matrix\n"); |
1932 |
fprintf(stderr, " -interlaced [integer] : interlaced encoding (BFF:1, TFF:2) (1)\n"); |
fprintf(stderr, " -interlaced [integer] : interlaced encoding (BFF:1, TFF:2) (%d)\n", DEFAULT_INTERLACING); |
1933 |
fprintf(stderr, " -nopacked : Disable packed mode\n"); |
if (ARG_PACKED) |
1934 |
fprintf(stderr, " -noclosed_gop : Disable closed GOP mode\n"); |
fprintf(stderr, " -nopacked : Disable packed B-frames mode (enabled)\n"); |
1935 |
fprintf(stderr, " -lumimasking : use lumimasking algorithm\n"); |
else |
1936 |
fprintf(stderr, " -stats : print stats about encoded frames\n"); |
fprintf(stderr, " -packed : Enable packed B-frames mode (disabled)\n"); |
1937 |
fprintf(stderr, " -ssim [integer] : prints ssim for every frame (accurate: 0 fast: 4) (2)\n"); |
if (ARG_CLOSED_GOP) |
1938 |
|
fprintf(stderr, " -noclosed_gop : Disable closed GOP mode (enabled)\n"); |
1939 |
|
else |
1940 |
|
fprintf(stderr, " -closed_gop : Enable closed GOP mode (disabled)\n"); |
1941 |
|
fprintf(stderr, " -masking [integer] : HVS masking mode (None:0, Lumi:1, Variance:2) (%d)\n", ARG_LUMIMASKING); |
1942 |
|
if (ARG_STATS) |
1943 |
|
fprintf(stderr, " -nostats : do not print stats about encoded frames (print)\n"); |
1944 |
|
else |
1945 |
|
fprintf(stderr, " -stats : print stats about encoded frames (don't print)\n"); |
1946 |
|
fprintf(stderr, " -ssim [integer] : prints ssim for every frame (accurate: 0 fast: 4) (%d)\n", DEFAULT_SSIM); |
1947 |
fprintf(stderr, " -ssim_file filename : outputs the ssim stats into a file\n"); |
fprintf(stderr, " -ssim_file filename : outputs the ssim stats into a file\n"); |
1948 |
fprintf(stderr, " -debug : activates xvidcore internal debugging output\n"); |
if (ARG_PSNRHVSM) |
1949 |
fprintf(stderr, " -vop_debug : print some info directly into encoded frames\n"); |
fprintf(stderr, " -nopsnrhvsm : do not print PSNRHVSM metric for every frame (print)\n"); |
1950 |
fprintf(stderr, " -nochromame : Disable chroma motion estimation\n"); |
else |
1951 |
fprintf(stderr, " -notrellis : Disable trellis quantization\n"); |
fprintf(stderr, " -psnrhvsm : print PSNRHVSM metric for every frame (don't print)\n"); |
1952 |
fprintf(stderr, " -imin integer : Minimum I Quantizer (1..31) (2)\n"); |
fprintf(stderr, " -debug integer : activates xvidcore internal debugging output (don't activate)\n"); |
1953 |
fprintf(stderr, " -imax integer : Maximum I quantizer (1..31) (31)\n"); |
if (ARG_VOPDEBUG) |
1954 |
fprintf(stderr, " -bmin integer : Minimum B Quantizer (1..31) (2)\n"); |
fprintf(stderr, " -novop_debug : do not print debug info directly into encoded frames (print)\n"); |
1955 |
fprintf(stderr, " -bmax integer : Maximum B quantizer (1..31) (31)\n"); |
else |
1956 |
fprintf(stderr, " -pmin integer : Minimum P Quantizer (1..31) (2)\n"); |
fprintf(stderr, " -vop_debug : print some info directly into encoded frames (don't print)\n"); |
1957 |
fprintf(stderr, " -pmax integer : Maximum P quantizer (1..31) (31)\n"); |
if (ARG_CHROMAME) |
1958 |
fprintf(stderr, " -drop integer : Frame Drop Ratio (0..100) (0)\n"); |
fprintf(stderr, " -nochromame : Disable chroma motion estimation (enabled)\n"); |
1959 |
fprintf(stderr, " -start integer : Starting frame number\n"); |
else |
1960 |
fprintf(stderr, " -threads integer : Number of threads\n"); |
fprintf(stderr, " -chromame : Enable chroma motion estimation (disabled)\n"); |
1961 |
fprintf(stderr, " -progress [integer] : Show progress updates every n frames (10)\n"); |
if (ARG_TRELLIS) |
1962 |
fprintf(stderr, " -par integer[:integer] : Set Pixel Aspect Ratio.\n"); |
fprintf(stderr, " -notrellis : Disable trellis quantization (enabled)\n"); |
1963 |
|
else |
1964 |
|
fprintf(stderr, " -trellis : Enable trellis quantization (disabled)\n"); |
1965 |
|
fprintf(stderr, " -imin integer : Minimum I Quantizer (1..31) (%d)\n", ARG_QUANTS[0]); |
1966 |
|
fprintf(stderr, " -imax integer : Maximum I quantizer (1..31) (%d)\n", ARG_QUANTS[1]); |
1967 |
|
fprintf(stderr, " -bmin integer : Minimum B Quantizer (1..31) (%d)\n", ARG_QUANTS[4]); |
1968 |
|
fprintf(stderr, " -bmax integer : Maximum B quantizer (1..31) (%d)\n", ARG_QUANTS[5]); |
1969 |
|
fprintf(stderr, " -pmin integer : Minimum P Quantizer (1..31) (%d)\n", ARG_QUANTS[2]); |
1970 |
|
fprintf(stderr, " -pmax integer : Maximum P quantizer (1..31) (%d)\n", ARG_QUANTS[3]); |
1971 |
|
fprintf(stderr, " -drop integer : Frame Drop Ratio (0..100) (%d)\n", ARG_FRAMEDROP); |
1972 |
|
fprintf(stderr, " -start integer : Starting frame number (%d)\n", ARG_STARTFRAMENR); |
1973 |
|
fprintf(stderr, " -threads integer : Number of threads (auto)\n"); |
1974 |
|
fprintf(stderr, " -slices integer : Number of slices (%d)\n", ARG_SLICES); |
1975 |
|
fprintf(stderr, " -progress [integer] : Show progress updates every n frames (%d)\n", DEFAULT_PROGRESS); |
1976 |
|
fprintf(stderr, " -par integer[:integer] : Set Pixel Aspect Ratio (%d)\n", ARG_PAR); |
1977 |
fprintf(stderr, " 1 = 1:1\n"); |
fprintf(stderr, " 1 = 1:1\n"); |
1978 |
fprintf(stderr, " 2 = 12:11 (4:3 PAL)\n"); |
fprintf(stderr, " 2 = 12:11 (4:3 PAL)\n"); |
1979 |
fprintf(stderr, " 3 = 10:11 (4:3 NTSC)\n"); |
fprintf(stderr, " 3 = 10:11 (4:3 NTSC)\n"); |
1989 |
* Input and output functions |
* Input and output functions |
1990 |
* |
* |
1991 |
* the are small and simple routines to read and write PGM and YUV |
* the are small and simple routines to read and write PGM and YUV |
1992 |
* image. It's just for convenience, again nothing specific to XviD |
* image. It's just for convenience, again nothing specific to Xvid |
1993 |
* |
* |
1994 |
*****************************************************************************/ |
*****************************************************************************/ |
1995 |
|
|
2000 |
int bytes, xsize, ysize, depth; |
int bytes, xsize, ysize, depth; |
2001 |
char dummy[2]; |
char dummy[2]; |
2002 |
|
|
2003 |
bytes = fread(dummy, 1, 2, handle); |
bytes = (int) fread(dummy, 1, 2, handle); |
2004 |
|
|
2005 |
if ((bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5')) |
if ((bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5')) |
2006 |
return (1); |
return (1); |
2007 |
|
|
2008 |
fscanf(handle, "%d %d %d", &xsize, &ysize, &depth); |
fscanf(handle, "%d %d %d", &xsize, &ysize, &depth); |
2009 |
|
fread(&dummy, 1, 1, handle); |
2010 |
if ((xsize > 4096) || (ysize > 4096*3/2) || (depth != 255)) { |
if ((xsize > 4096) || (ysize > 4096*3/2) || (depth != 255)) { |
2011 |
fprintf(stderr, "%d %d %d\n", xsize, ysize, depth); |
fprintf(stderr, "%d %d %d\n", xsize, ysize, depth); |
2012 |
return (2); |
return (2); |
2024 |
unsigned char *image) |
unsigned char *image) |
2025 |
{ |
{ |
2026 |
int i; |
int i; |
|
char dummy; |
|
2027 |
|
|
2028 |
unsigned char *y = image; |
unsigned char *y = image; |
2029 |
unsigned char *u = image + XDIM * YDIM; |
unsigned char *u = image + XDIM * YDIM; |
2044 |
v += XDIM / 2; |
v += XDIM / 2; |
2045 |
} |
} |
2046 |
|
|
|
/* I don't know why, but this seems needed */ |
|
|
fread(&dummy, 1, 1, handle); |
|
|
|
|
2047 |
return (0); |
return (0); |
2048 |
} |
} |
2049 |
#else |
#else |
2148 |
|
|
2149 |
#define FRAMERATE_INCR 1001 |
#define FRAMERATE_INCR 1001 |
2150 |
|
|
2151 |
|
/* Gobal encoder init, once per process */ |
2152 |
/* Initialize encoder for first use, pass all needed parameters to the codec */ |
void |
2153 |
static int |
enc_gbl(int use_assembler) |
|
enc_init(int use_assembler) |
|
2154 |
{ |
{ |
|
int xerr; |
|
|
//xvid_plugin_cbr_t cbr; |
|
|
xvid_plugin_single_t single; |
|
|
xvid_plugin_2pass1_t rc2pass1; |
|
|
xvid_plugin_2pass2_t rc2pass2; |
|
|
xvid_plugin_ssim_t ssim; |
|
|
//xvid_plugin_fixed_t rcfixed; |
|
|
xvid_enc_plugin_t plugins[8]; |
|
2155 |
xvid_gbl_init_t xvid_gbl_init; |
xvid_gbl_init_t xvid_gbl_init; |
|
xvid_enc_create_t xvid_enc_create; |
|
|
int i; |
|
2156 |
|
|
2157 |
/*------------------------------------------------------------------------ |
/*------------------------------------------------------------------------ |
2158 |
* XviD core initialization |
* Xvid core initialization |
2159 |
*----------------------------------------------------------------------*/ |
*----------------------------------------------------------------------*/ |
2160 |
|
|
2161 |
/* Set version -- version checking will done by xvidcore */ |
/* Set version -- version checking will done by xvidcore */ |
2176 |
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; |
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; |
2177 |
} |
} |
2178 |
|
|
2179 |
/* Initialize XviD core -- Should be done once per __process__ */ |
/* Initialize Xvid core -- Should be done once per __process__ */ |
2180 |
xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL); |
xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL); |
2181 |
|
ARG_CPU_FLAGS = xvid_gbl_init.cpu_flags; |
2182 |
enc_info(); |
enc_info(); |
2183 |
|
} |
2184 |
|
|
2185 |
|
/* Initialize encoder for first use, pass all needed parameters to the codec */ |
2186 |
|
static int |
2187 |
|
enc_init(void **enc_handle, char *stats_pass1, int start_num) |
2188 |
|
{ |
2189 |
|
int xerr; |
2190 |
|
//xvid_plugin_cbr_t cbr; |
2191 |
|
xvid_plugin_single_t single; |
2192 |
|
xvid_plugin_2pass1_t rc2pass1; |
2193 |
|
xvid_plugin_2pass2_t rc2pass2; |
2194 |
|
xvid_plugin_ssim_t ssim; |
2195 |
|
xvid_plugin_lumimasking_t masking; |
2196 |
|
//xvid_plugin_fixed_t rcfixed; |
2197 |
|
xvid_enc_plugin_t plugins[8]; |
2198 |
|
xvid_enc_create_t xvid_enc_create; |
2199 |
|
int i; |
2200 |
|
|
2201 |
/*------------------------------------------------------------------------ |
/*------------------------------------------------------------------------ |
2202 |
* XviD encoder initialization |
* Xvid encoder initialization |
2203 |
*----------------------------------------------------------------------*/ |
*----------------------------------------------------------------------*/ |
2204 |
|
|
2205 |
/* Version again */ |
/* Version again */ |
2254 |
rc2pass2.vbv_size = ARG_VBVSIZE; |
rc2pass2.vbv_size = ARG_VBVSIZE; |
2255 |
rc2pass2.vbv_initial = (ARG_VBVSIZE*3)/4; |
rc2pass2.vbv_initial = (ARG_VBVSIZE*3)/4; |
2256 |
rc2pass2.vbv_maxrate = ARG_VBVMAXRATE; |
rc2pass2.vbv_maxrate = ARG_VBVMAXRATE; |
2257 |
rc2pass2.vbv_peakrate = ARG_VBVPEAKRATE*3; |
rc2pass2.vbv_peakrate = ARG_VBVPEAKRATE; |
2258 |
|
|
2259 |
|
|
2260 |
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2; |
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2; |
2262 |
xvid_enc_create.num_plugins++; |
xvid_enc_create.num_plugins++; |
2263 |
} |
} |
2264 |
|
|
2265 |
if (ARG_PASS1) { |
if (stats_pass1) { |
2266 |
memset(&rc2pass1, 0, sizeof(xvid_plugin_2pass1_t)); |
memset(&rc2pass1, 0, sizeof(xvid_plugin_2pass1_t)); |
2267 |
rc2pass1.version = XVID_VERSION; |
rc2pass1.version = XVID_VERSION; |
2268 |
rc2pass1.filename = ARG_PASS1; |
rc2pass1.filename = stats_pass1; |
2269 |
if (ARG_FULL1PASS) |
if (ARG_FULL1PASS) |
2270 |
prepare_full1pass_zones(); |
prepare_full1pass_zones(); |
2271 |
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1; |
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1; |
2285 |
|
|
2286 |
|
|
2287 |
if (ARG_LUMIMASKING) { |
if (ARG_LUMIMASKING) { |
2288 |
|
memset(&masking, 0, sizeof(xvid_plugin_lumimasking_t)); |
2289 |
|
masking.method = (ARG_LUMIMASKING==2); |
2290 |
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking; |
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking; |
2291 |
plugins[xvid_enc_create.num_plugins].param = NULL; |
plugins[xvid_enc_create.num_plugins].param = &masking; |
2292 |
xvid_enc_create.num_plugins++; |
xvid_enc_create.num_plugins++; |
2293 |
} |
} |
2294 |
|
|
2299 |
} |
} |
2300 |
|
|
2301 |
if (ARG_SSIM>=0 || ARG_SSIM_PATH != NULL) { |
if (ARG_SSIM>=0 || ARG_SSIM_PATH != NULL) { |
2302 |
|
memset(&ssim, 0, sizeof(xvid_plugin_ssim_t)); |
2303 |
|
|
2304 |
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_ssim; |
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_ssim; |
2305 |
|
|
2306 |
if( ARG_SSIM >=0){ |
if( ARG_SSIM >=0){ |
2315 |
ssim.stat_path = ARG_SSIM_PATH; |
ssim.stat_path = ARG_SSIM_PATH; |
2316 |
} |
} |
2317 |
|
|
2318 |
|
ssim.cpu_flags = ARG_CPU_FLAGS; |
2319 |
ssim.b_visualize = 0; |
ssim.b_visualize = 0; |
2320 |
plugins[xvid_enc_create.num_plugins].param = &ssim; |
plugins[xvid_enc_create.num_plugins].param = &ssim; |
2321 |
xvid_enc_create.num_plugins++; |
xvid_enc_create.num_plugins++; |
2322 |
} |
} |
2323 |
|
|
2324 |
|
if (ARG_PSNRHVSM>0) { |
2325 |
|
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_psnrhvsm; |
2326 |
|
plugins[xvid_enc_create.num_plugins].param = NULL; |
2327 |
|
xvid_enc_create.num_plugins++; |
2328 |
|
} |
2329 |
|
|
2330 |
#if 0 |
#if 0 |
2331 |
if (ARG_DEBUG) { |
if (ARG_DEBUG) { |
2332 |
plugins[xvid_enc_create.num_plugins].func = rawenc_debug; |
plugins[xvid_enc_create.num_plugins].func = rawenc_debug; |
2336 |
#endif |
#endif |
2337 |
|
|
2338 |
xvid_enc_create.num_threads = ARG_THREADS; |
xvid_enc_create.num_threads = ARG_THREADS; |
2339 |
|
xvid_enc_create.num_slices = ARG_SLICES; |
2340 |
|
|
2341 |
/* Frame rate */ |
/* Frame rate */ |
2342 |
xvid_enc_create.fincr = ARG_DWSCALE; |
xvid_enc_create.fincr = ARG_DWSCALE; |
2364 |
/* Frame drop ratio */ |
/* Frame drop ratio */ |
2365 |
xvid_enc_create.frame_drop_ratio = ARG_FRAMEDROP; |
xvid_enc_create.frame_drop_ratio = ARG_FRAMEDROP; |
2366 |
|
|
2367 |
|
/* Start frame number */ |
2368 |
|
xvid_enc_create.start_frame_num = start_num; |
2369 |
|
|
2370 |
/* Global encoder options */ |
/* Global encoder options */ |
2371 |
xvid_enc_create.global = 0; |
xvid_enc_create.global = 0; |
2372 |
|
|
2383 |
xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); |
xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); |
2384 |
|
|
2385 |
/* Retrieve the encoder instance from the structure */ |
/* Retrieve the encoder instance from the structure */ |
2386 |
enc_handle = xvid_enc_create.handle; |
*enc_handle = xvid_enc_create.handle; |
2387 |
|
|
2388 |
free(xvid_enc_create.zones); |
free(xvid_enc_create.zones); |
2389 |
|
|
2414 |
fprintf(stderr, "SSE "); |
fprintf(stderr, "SSE "); |
2415 |
if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE2) |
if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE2) |
2416 |
fprintf(stderr, "SSE2 "); |
fprintf(stderr, "SSE2 "); |
2417 |
|
if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE3) |
2418 |
|
fprintf(stderr, "SSE3 "); |
2419 |
|
if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE41) |
2420 |
|
fprintf(stderr, "SSE41 "); |
2421 |
if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOW) |
if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOW) |
2422 |
fprintf(stderr, "3DNOW "); |
fprintf(stderr, "3DNOW "); |
2423 |
if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOWEXT) |
if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOWEXT) |
2426 |
fprintf(stderr, "TSC "); |
fprintf(stderr, "TSC "); |
2427 |
fprintf(stderr, "\n"); |
fprintf(stderr, "\n"); |
2428 |
fprintf(stderr, "Detected %d cpus,", xvid_gbl_info.num_threads); |
fprintf(stderr, "Detected %d cpus,", xvid_gbl_info.num_threads); |
2429 |
if (!ARG_THREADS) |
ARG_NUM_APP_THREADS = xvid_gbl_info.num_threads; |
2430 |
ARG_THREADS = xvid_gbl_info.num_threads; |
fprintf(stderr, " using %d threads.\n", (!ARG_THREADS) ? ARG_NUM_APP_THREADS : ARG_THREADS); |
|
fprintf(stderr, " using %d threads.\n", ARG_THREADS); |
|
2431 |
return ret; |
return ret; |
2432 |
} |
} |
2433 |
|
|
2434 |
static int |
static int |
2435 |
enc_stop() |
enc_stop(void *enc_handle) |
2436 |
{ |
{ |
2437 |
int xerr; |
int xerr; |
2438 |
|
|
2443 |
} |
} |
2444 |
|
|
2445 |
static int |
static int |
2446 |
enc_main(unsigned char *image, |
enc_main(void *enc_handle, |
2447 |
|
unsigned char *image, |
2448 |
unsigned char *bitstream, |
unsigned char *bitstream, |
2449 |
int *key, |
int *key, |
2450 |
int *stats_type, |
int *stats_type, |
2559 |
if (ARG_BVHQ) |
if (ARG_BVHQ) |
2560 |
xvid_enc_frame.vop_flags |= XVID_VOP_RD_BVOP; |
xvid_enc_frame.vop_flags |= XVID_VOP_RD_BVOP; |
2561 |
|
|
2562 |
|
if (ARG_QMETRIC == 1) |
2563 |
|
xvid_enc_frame.vop_flags |= XVID_VOP_RD_PSNRHVSM; |
2564 |
|
|
2565 |
switch (ARG_VHQMODE) /* this is the same code as for vfw */ |
switch (ARG_VHQMODE) /* this is the same code as for vfw */ |
2566 |
{ |
{ |
2567 |
case 1: /* VHQ_MODE_DECISION */ |
case 1: /* VHQ_MODE_DECISION */ |
2657 |
|
|
2658 |
ZONES[NUM_ZONES].frame = 0; |
ZONES[NUM_ZONES].frame = 0; |
2659 |
ZONES[NUM_ZONES].mode = XVID_ZONE_QUANT; |
ZONES[NUM_ZONES].mode = XVID_ZONE_QUANT; |
2660 |
ZONES[NUM_ZONES].modifier = ARG_CQ; |
ZONES[NUM_ZONES].modifier = (int)ARG_CQ; |
2661 |
ZONES[NUM_ZONES].type = XVID_TYPE_AUTO; |
ZONES[NUM_ZONES].type = XVID_TYPE_AUTO; |
2662 |
ZONES[NUM_ZONES].greyscale = 0; |
ZONES[NUM_ZONES].greyscale = 0; |
2663 |
ZONES[NUM_ZONES].chroma_opt = 0; |
ZONES[NUM_ZONES].chroma_opt = 0; |
2673 |
for(i = 0; i < NUM_ZONES; i++) |
for(i = 0; i < NUM_ZONES; i++) |
2674 |
if (ZONES[i].mode == XVID_ZONE_WEIGHT) { |
if (ZONES[i].mode == XVID_ZONE_WEIGHT) { |
2675 |
ZONES[i].mode = XVID_ZONE_QUANT; |
ZONES[i].mode = XVID_ZONE_QUANT; |
2676 |
ZONES[i].modifier = (100*ARG_CQ) / ZONES[i].modifier; |
ZONES[i].modifier = (int) ((100*ARG_CQ) / ZONES[i].modifier); |
2677 |
} |
} |
2678 |
} |
} |
2679 |
|
|
2742 |
int i; |
int i; |
2743 |
char* userdata; |
char* userdata; |
2744 |
|
|
2745 |
for (i=0; i <= (bufsize-sizeof(userdata_start_code)); i++) { |
for (i=0; i <= (int)(bufsize-sizeof(userdata_start_code)); i++) { |
2746 |
if (memcmp((void*)userdata_start_code, (void*)(buf+i), strlen(userdata_start_code))==0) { |
if (memcmp((void*)userdata_start_code, (void*)(buf+i), strlen(userdata_start_code))==0) { |
2747 |
if ((userdata = strstr(buf+i+4, "DivX"))!=NULL) { |
if ((userdata = strstr(buf+i+4, "DivX"))!=NULL) { |
2748 |
userdata[strlen(userdata)-1] = '\0'; |
userdata[strlen(userdata)-1] = '\0'; |