3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Console based test application - |
* - Console based test application - |
5 |
* |
* |
6 |
* Copyright(C) 2002 Christoph Lampert |
* Copyright(C) 2002-2003 Christoph Lampert |
7 |
* |
* |
8 |
* This program is free software; you can redistribute it and/or modify |
* This program is free software; you can redistribute it and/or modify |
9 |
* 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 |
19 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
* |
* |
22 |
* $Id: xvid_encraw.c,v 1.8 2003-02-09 19:32:52 edgomez Exp $ |
* $Id: xvid_encraw.c,v 1.11.2.2 2003-03-09 16:45:24 edgomez Exp $ |
23 |
* |
* |
24 |
****************************************************************************/ |
****************************************************************************/ |
25 |
|
|
38 |
#include <stdlib.h> |
#include <stdlib.h> |
39 |
#include <string.h> |
#include <string.h> |
40 |
#include <math.h> |
#include <math.h> |
41 |
#ifndef _MSC_VER |
#ifndef WIN32 |
42 |
#include <sys/time.h> |
#include <sys/time.h> |
43 |
#else |
#else |
44 |
#include <time.h> |
#include <time.h> |
50 |
* Quality presets |
* Quality presets |
51 |
****************************************************************************/ |
****************************************************************************/ |
52 |
|
|
53 |
static int const motion_presets[7] = { |
static xvid_motion_t const motion_presets[] = { |
54 |
0, /* Q 0 */ |
0, |
55 |
PMV_EARLYSTOP16, /* Q 1 */ |
PMV_HALFPELREFINE16, |
56 |
PMV_EARLYSTOP16, /* Q 2 */ |
PMV_HALFPELREFINE16, |
57 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 3 */ |
PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8, |
58 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 4 */ |
PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8 | PMV_EXTSEARCH16 | PMV_USESQUARES16, |
59 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | /* Q 5 */ |
PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8 | PMV_EXTSEARCH16 | PMV_USESQUARES16 | PMV_CHROMA16 | PMV_CHROMA8, |
|
PMV_HALFPELREFINE8, |
|
|
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | /* Q 6 */ |
|
|
PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 |
|
60 |
}; |
}; |
61 |
|
|
62 |
static int const general_presets[7] = { |
static xvid_vol_t const vol_presets[] = { |
63 |
XVID_H263QUANT, /* Q 0 */ |
XVID_MPEGQUANT, |
64 |
XVID_MPEGQUANT, /* Q 1 */ |
0, |
65 |
XVID_H263QUANT, /* Q 2 */ |
0, |
66 |
XVID_H263QUANT | XVID_HALFPEL, /* Q 3 */ |
XVID_QUARTERPEL, |
67 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 4 */ |
XVID_QUARTERPEL | XVID_GMC, |
68 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 5 */ |
XVID_QUARTERPEL | XVID_GMC |
|
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V /* Q 6 */ |
|
69 |
}; |
}; |
70 |
|
|
71 |
|
static xvid_vop_t const vop_presets[] = { |
72 |
|
XVID_DYNAMIC_BFRAMES, |
73 |
|
XVID_DYNAMIC_BFRAMES, |
74 |
|
XVID_DYNAMIC_BFRAMES | XVID_HALFPEL, |
75 |
|
XVID_DYNAMIC_BFRAMES | XVID_HALFPEL | XVID_INTER4V, |
76 |
|
XVID_DYNAMIC_BFRAMES | XVID_HALFPEL | XVID_INTER4V | XVID_HQACPRED, |
77 |
|
XVID_DYNAMIC_BFRAMES | XVID_HALFPEL | XVID_INTER4V | XVID_HQACPRED | XVID_MODEDECISION_BITS |
78 |
|
}; |
79 |
|
|
80 |
/***************************************************************************** |
/***************************************************************************** |
81 |
* Command line global variables |
* Command line global variables |
84 |
/* Maximum number of frames to encode */ |
/* Maximum number of frames to encode */ |
85 |
#define ABS_MAXFRAMENR 9999 |
#define ABS_MAXFRAMENR 9999 |
86 |
|
|
|
/* HINTMODEs */ |
|
|
#define HINT_MODE_NONE 0 |
|
|
#define HINT_MODE_GET 1 |
|
|
#define HINT_MODE_SET 2 |
|
|
#define HINT_FILE "hints.mv" |
|
|
|
|
87 |
static int ARG_BITRATE = 900; |
static int ARG_BITRATE = 900; |
88 |
static int ARG_QUANTI = 0; |
static int ARG_QUANTI = 0; |
89 |
static int ARG_QUALITY = 6; |
static int ARG_QUALITY = 5; |
90 |
static int ARG_MINQUANT = 1; |
static int ARG_MINQUANT = 1; |
91 |
static int ARG_MAXQUANT = 31; |
static int ARG_MAXQUANT = 31; |
92 |
static float ARG_FRAMERATE = 25.00f; |
static float ARG_FRAMERATE = 25.00f; |
94 |
static char *ARG_INPUTFILE = NULL; |
static char *ARG_INPUTFILE = NULL; |
95 |
static int ARG_INPUTTYPE = 0; |
static int ARG_INPUTTYPE = 0; |
96 |
static int ARG_SAVEMPEGSTREAM = 0; |
static int ARG_SAVEMPEGSTREAM = 0; |
|
static int ARG_OUTPUTTYPE = 0; |
|
97 |
static char *ARG_OUTPUTFILE = NULL; |
static char *ARG_OUTPUTFILE = NULL; |
|
static int ARG_HINTMODE = HINT_MODE_NONE; |
|
98 |
static int XDIM = 0; |
static int XDIM = 0; |
99 |
static int YDIM = 0; |
static int YDIM = 0; |
100 |
|
static int ARG_BQRATIO = 150; |
101 |
|
static int ARG_BQOFFSET = 100; |
102 |
|
static int ARG_MAXBFRAMES = 0; |
103 |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
104 |
|
|
105 |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
106 |
#define SMALL_EPS 1e-10 |
#define SMALL_EPS (1e-10) |
|
|
|
|
#define LONG_PACK(a,b,c,d) ((long) (((long)(a))<<24) | (((long)(b))<<16) | \ |
|
|
(((long)(c))<<8) |((long)(d))) |
|
107 |
|
|
108 |
#define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ |
#define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ |
109 |
(((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) |
(((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) |
138 |
/* Encoder related functions */ |
/* Encoder related functions */ |
139 |
static int enc_init(int use_assembler); |
static int enc_init(int use_assembler); |
140 |
static int enc_stop(); |
static int enc_stop(); |
141 |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
static int enc_main(unsigned char* image, |
142 |
unsigned char *hints_buffer, |
unsigned char* bitstream, |
143 |
long *streamlength, long* frametype, long *hints_size); |
long *frametype, |
144 |
|
int stats[3]); |
145 |
|
|
146 |
/***************************************************************************** |
/***************************************************************************** |
147 |
* Main function |
* Main function |
153 |
unsigned char *mp4_buffer = NULL; |
unsigned char *mp4_buffer = NULL; |
154 |
unsigned char *in_buffer = NULL; |
unsigned char *in_buffer = NULL; |
155 |
unsigned char *out_buffer = NULL; |
unsigned char *out_buffer = NULL; |
|
unsigned char *hints_buffer = NULL; |
|
156 |
|
|
157 |
double enctime; |
double enctime; |
158 |
double totalenctime=0.; |
double totalenctime=0.; |
159 |
|
|
160 |
long totalsize; |
long totalsize; |
|
long hints_size; |
|
161 |
int status; |
int status; |
162 |
long frame_type; |
long frame_type; |
|
long bigendian; |
|
163 |
|
|
164 |
long m4v_size; |
long m4v_size; |
165 |
int use_assembler=0; |
int use_assembler=0; |
168 |
|
|
169 |
FILE *in_file = stdin; |
FILE *in_file = stdin; |
170 |
FILE *out_file = NULL; |
FILE *out_file = NULL; |
|
FILE *hints_file = NULL; |
|
171 |
|
|
172 |
printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
173 |
printf("written by Christoph Lampert 2002\n\n"); |
printf("written by Christoph Lampert 2002-2003\n\n"); |
174 |
|
|
175 |
/***************************************************************************** |
/***************************************************************************** |
176 |
* Command line parsing |
* Command line parsing |
193 |
i++; |
i++; |
194 |
ARG_BITRATE = atoi(argv[i]); |
ARG_BITRATE = atoi(argv[i]); |
195 |
} |
} |
196 |
|
else if (strcmp("-bn", argv[i]) == 0 && i < argc - 1 ) { |
197 |
|
i++; |
198 |
|
ARG_MAXBFRAMES = atoi(argv[i]); |
199 |
|
} |
200 |
|
else if (strcmp("-bqr", argv[i]) == 0 && i < argc - 1 ) { |
201 |
|
i++; |
202 |
|
ARG_BQRATIO = atoi(argv[i]); |
203 |
|
} |
204 |
|
else if (strcmp("-bqo", argv[i]) == 0 && i < argc - 1 ) { |
205 |
|
i++; |
206 |
|
ARG_BQOFFSET = atoi(argv[i]); |
207 |
|
} |
208 |
else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) { |
209 |
i++; |
i++; |
210 |
ARG_QUALITY = atoi(argv[i]); |
ARG_QUALITY = atoi(argv[i]); |
233 |
i++; |
i++; |
234 |
ARG_SAVEMPEGSTREAM = atoi(argv[i]); |
ARG_SAVEMPEGSTREAM = atoi(argv[i]); |
235 |
} |
} |
|
else if (strcmp("-mt", argv[i]) == 0 && i < argc - 1 ) { |
|
|
i++; |
|
|
ARG_OUTPUTTYPE = atoi(argv[i]); |
|
|
} |
|
|
else if (strcmp("-mv", argv[i]) == 0 && i < argc - 1 ) { |
|
|
i++; |
|
|
ARG_HINTMODE = atoi(argv[i]); |
|
|
} |
|
236 |
else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { |
237 |
i++; |
i++; |
238 |
ARG_OUTPUTFILE = argv[i]; |
ARG_OUTPUTFILE = argv[i]; |
257 |
ARG_INPUTTYPE = 1; /* pgm */ |
ARG_INPUTTYPE = 1; /* pgm */ |
258 |
} |
} |
259 |
|
|
260 |
if ( ARG_QUALITY < 0 || ARG_QUALITY > 6) { |
if ( ARG_QUALITY < 0 || ARG_QUALITY > 5) { |
261 |
fprintf(stderr,"Wrong Quality\n"); |
fprintf(stderr,"Wrong Quality\n"); |
262 |
return -1; |
return(-1); |
263 |
} |
} |
264 |
|
|
265 |
if ( ARG_BITRATE <= 0 && ARG_QUANTI == 0) { |
if ( ARG_BITRATE <= 0 && ARG_QUANTI == 0) { |
266 |
fprintf(stderr,"Wrong Bitrate\n"); |
fprintf(stderr,"Wrong Bitrate\n"); |
267 |
return -1; |
return(-1); |
268 |
} |
} |
269 |
|
|
270 |
if ( ARG_FRAMERATE <= 0) { |
if ( ARG_FRAMERATE <= 0) { |
271 |
fprintf(stderr,"Wrong Framerate %s \n",argv[5]); |
fprintf(stderr,"Wrong Framerate %s \n",argv[5]); |
272 |
return -1; |
return(-1); |
273 |
} |
} |
274 |
|
|
275 |
if ( ARG_MAXFRAMENR <= 0) { |
if ( ARG_MAXFRAMENR <= 0) { |
276 |
fprintf(stderr,"Wrong number of frames\n"); |
fprintf(stderr,"Wrong number of frames\n"); |
277 |
return -1; |
return(-1); |
|
} |
|
|
|
|
|
if ( ARG_HINTMODE != HINT_MODE_NONE && |
|
|
ARG_HINTMODE != HINT_MODE_GET && |
|
|
ARG_HINTMODE != HINT_MODE_SET) |
|
|
ARG_HINTMODE = HINT_MODE_NONE; |
|
|
|
|
|
if( ARG_HINTMODE != HINT_MODE_NONE) { |
|
|
char *rights = "rb"; |
|
|
|
|
|
/* |
|
|
* If we are getting hints from core, we will have to write them to |
|
|
* hint file |
|
|
*/ |
|
|
if(ARG_HINTMODE == HINT_MODE_GET) |
|
|
rights = "w+b"; |
|
|
|
|
|
/* Open the hint file */ |
|
|
hints_file = fopen(HINT_FILE, rights); |
|
|
if(hints_file == NULL) { |
|
|
fprintf(stderr, "Error opening input file %s\n", HINT_FILE); |
|
|
return -1; |
|
|
} |
|
|
|
|
|
/* Allocate hint memory space, we will be using rawhints */ |
|
|
/* NB : Hope 1Mb is enough */ |
|
|
if((hints_buffer = malloc(1024*1024)) == NULL) { |
|
|
fprintf(stderr, "Memory allocation error\n"); |
|
|
return -1; |
|
|
} |
|
|
|
|
278 |
} |
} |
279 |
|
|
280 |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
285 |
in_file = fopen(ARG_INPUTFILE, "rb"); |
in_file = fopen(ARG_INPUTFILE, "rb"); |
286 |
if (in_file == NULL) { |
if (in_file == NULL) { |
287 |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
288 |
return -1; |
return(-1); |
289 |
} |
} |
290 |
} |
} |
291 |
|
|
292 |
if (ARG_INPUTTYPE) { |
if (ARG_INPUTTYPE) { |
293 |
if (read_pgmheader(in_file)) { |
if (read_pgmheader(in_file)) { |
294 |
fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); |
fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); |
295 |
return -1; |
return(-1); |
296 |
} |
} |
297 |
} |
} |
298 |
|
|
299 |
/* now we know the sizes, so allocate memory */ |
/* now we know the sizes, so allocate memory */ |
|
|
|
300 |
in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)); |
in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)); |
301 |
if (!in_buffer) |
if (!in_buffer) |
302 |
goto free_all_memory; |
goto free_all_memory; |
322 |
* Main loop |
* Main loop |
323 |
****************************************************************************/ |
****************************************************************************/ |
324 |
|
|
325 |
totalsize = LONG_PACK('M','P','4','U'); |
if (ARG_SAVEMPEGSTREAM && ARG_OUTPUTFILE) { |
|
if(*((char *)(&totalsize)) == 'M') |
|
|
bigendian = 1; |
|
|
else |
|
|
bigendian = 0; |
|
|
|
|
|
if (ARG_SAVEMPEGSTREAM && (ARG_OUTPUTTYPE || ARG_OUTPUTFILE)) { |
|
|
|
|
|
if (ARG_OUTPUTFILE == NULL && ARG_OUTPUTTYPE) |
|
|
ARG_OUTPUTFILE = "stream.mp4u"; |
|
|
else if(ARG_OUTPUTFILE == NULL && !ARG_OUTPUTTYPE) |
|
|
ARG_OUTPUTFILE = "stream.m4v"; |
|
326 |
|
|
327 |
if((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) { |
if((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) { |
328 |
fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE); |
fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE); |
329 |
goto release_all; |
goto release_all; |
330 |
} |
} |
331 |
|
|
|
/* Write header */ |
|
|
if (ARG_OUTPUTTYPE) { |
|
|
|
|
|
long test = LONG_PACK('M','P','4','U'); |
|
|
|
|
|
test = (!bigendian)?SWAP(test):test; |
|
|
|
|
|
fwrite(&test, sizeof(test), 1, out_file); |
|
|
|
|
|
} |
|
|
|
|
332 |
} |
} |
333 |
else { |
else { |
334 |
out_file = NULL; |
out_file = NULL; |
342 |
|
|
343 |
do { |
do { |
344 |
|
|
345 |
if (ARG_INPUTTYPE) |
char *type; |
346 |
status = read_pgmdata(in_file, in_buffer); /* read PGM data (YUV-format) */ |
int stats[3]; |
|
else |
|
|
status = read_yuvdata(in_file, in_buffer); /* read raw data (YUV-format) */ |
|
347 |
|
|
348 |
if (status) |
if(ARG_INPUTTYPE) { |
349 |
{ |
/* read PGM data (YUV-format) */ |
350 |
/* Couldn't read image, most likely end-of-file */ |
status = read_pgmdata(in_file, in_buffer); |
351 |
continue; |
} else { |
352 |
|
/* read raw data (YUV-format) */ |
353 |
|
status = read_yuvdata(in_file, in_buffer); |
354 |
} |
} |
355 |
|
|
356 |
/***************************************************************************** |
if(status) { |
357 |
* Read hints from file |
/* Couldn't read image, most likely end-of-file */ |
358 |
****************************************************************************/ |
continue; |
|
|
|
|
if(ARG_HINTMODE == HINT_MODE_SET) { |
|
|
fread(&hints_size, 1, sizeof(long), hints_file); |
|
|
hints_size = (!bigendian)?SWAP(hints_size):hints_size; |
|
|
fread(hints_buffer, 1, hints_size, hints_file); |
|
359 |
} |
} |
360 |
|
|
361 |
/***************************************************************************** |
/***************************************************************************** |
363 |
****************************************************************************/ |
****************************************************************************/ |
364 |
|
|
365 |
enctime = msecond(); |
enctime = msecond(); |
366 |
status = enc_main(in_buffer, mp4_buffer, hints_buffer, |
m4v_size = enc_main(in_buffer, mp4_buffer, &frame_type, stats); |
|
&m4v_size, &frame_type, &hints_size); |
|
367 |
enctime = msecond() - enctime; |
enctime = msecond() - enctime; |
368 |
|
|
369 |
totalenctime += enctime; |
/* Not coded frames return 0 */ |
370 |
totalsize += m4v_size; |
if(m4v_size == 0) goto next_frame; |
371 |
|
|
372 |
printf("Frame %5d: intra %1d, enctime=%6.1f ms, size=%6dbytes\n", |
/* Write the Frame statistics */ |
373 |
(int)filenr, (int)frame_type, (float)enctime, (int)m4v_size); |
switch(frame_type) { |
374 |
|
case XVID_TYPE_IVOP: |
375 |
|
type = "I"; |
376 |
|
break; |
377 |
|
case XVID_TYPE_PVOP: |
378 |
|
type = "P"; |
379 |
|
break; |
380 |
|
case XVID_TYPE_BVOP: |
381 |
|
type = "B"; |
382 |
|
break; |
383 |
|
case XVID_TYPE_SVOP: |
384 |
|
type = "S"; |
385 |
|
break; |
386 |
|
case XVID_TYPE_NOTHING: |
387 |
|
type = "N"; |
388 |
|
break; |
389 |
|
default: |
390 |
|
type = "U"; |
391 |
|
break; |
392 |
|
} |
393 |
|
|
394 |
|
printf("Frame %5d: type = %s, enctime(ms) =%6.1f, length(bytes) =%7d, " |
395 |
|
"psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f\n", |
396 |
|
(int)filenr, |
397 |
|
type, |
398 |
|
(float)enctime, |
399 |
|
(int)m4v_size, |
400 |
|
(stats[0] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[0]/((float)(XDIM)*(YDIM))), |
401 |
|
(stats[1] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[1]/((float)(XDIM)*(YDIM)/4)), |
402 |
|
(stats[2] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[2]/((float)(XDIM)*(YDIM)/4))); |
403 |
|
|
404 |
/***************************************************************************** |
/* Update encoding time stats */ |
405 |
* Save hints to file |
totalenctime += enctime; |
406 |
****************************************************************************/ |
totalsize += m4v_size; |
|
|
|
|
if(ARG_HINTMODE == HINT_MODE_GET) { |
|
|
hints_size = (!bigendian)?SWAP(hints_size):hints_size; |
|
|
fwrite(&hints_size, 1, sizeof(long), hints_file); |
|
|
hints_size = (!bigendian)?SWAP(hints_size):hints_size; |
|
|
fwrite(hints_buffer, 1, hints_size, hints_file); |
|
|
} |
|
407 |
|
|
408 |
/***************************************************************************** |
/***************************************************************************** |
409 |
* Save stream to file |
* Save stream to file |
420 |
out_file = NULL; |
out_file = NULL; |
421 |
} |
} |
422 |
else { |
else { |
|
/* Using mp4u container */ |
|
|
if (ARG_OUTPUTTYPE) { |
|
|
long size = m4v_size; |
|
|
size = (!bigendian)?SWAP(size):size; |
|
|
fwrite(&size, sizeof(size), 1, out_file); |
|
|
} |
|
423 |
|
|
424 |
/* Write mp4 data */ |
/* Write mp4 data */ |
425 |
fwrite(mp4_buffer, m4v_size, 1, out_file); |
fwrite(mp4_buffer, 1, m4v_size, out_file); |
426 |
|
|
427 |
} |
} |
428 |
} |
} |
429 |
|
|
430 |
|
next_frame: |
431 |
/* Read the header if it's pgm stream */ |
/* Read the header if it's pgm stream */ |
432 |
if (ARG_INPUTTYPE) |
if (ARG_INPUTTYPE) |
433 |
status = read_pgmheader(in_file); |
status = read_pgmheader(in_file); |
445 |
totalsize /= filenr; |
totalsize /= filenr; |
446 |
totalenctime /= filenr; |
totalenctime /= filenr; |
447 |
|
|
448 |
printf("Avg: enctime %5.2f ms, %5.2f fps, filesize %7d bytes\n", |
printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d\n", |
449 |
totalenctime, 1000/totalenctime, (int)totalsize); |
totalenctime, 1000/totalenctime, (int)totalsize); |
450 |
|
|
451 |
|
|
452 |
/***************************************************************************** |
/***************************************************************************** |
453 |
* XviD PART Stop |
* XviD PART Stop |
454 |
****************************************************************************/ |
****************************************************************************/ |
466 |
fclose(in_file); |
fclose(in_file); |
467 |
if(out_file) |
if(out_file) |
468 |
fclose(out_file); |
fclose(out_file); |
|
if(hints_file) |
|
|
fclose(hints_file); |
|
469 |
|
|
470 |
free_all_memory: |
free_all_memory: |
471 |
free(out_buffer); |
free(out_buffer); |
472 |
free(mp4_buffer); |
free(mp4_buffer); |
473 |
free(in_buffer); |
free(in_buffer); |
|
if(hints_buffer) free(hints_buffer); |
|
474 |
|
|
475 |
return 0; |
return(0); |
476 |
|
|
477 |
} |
} |
478 |
|
|
491 |
#ifndef WIN32 |
#ifndef WIN32 |
492 |
struct timeval tv; |
struct timeval tv; |
493 |
gettimeofday(&tv, 0); |
gettimeofday(&tv, 0); |
494 |
return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3; |
return(tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3); |
495 |
#else |
#else |
496 |
clock_t clk; |
clock_t clk; |
497 |
clk = clock(); |
clk = clock(); |
498 |
return clk * 1000 / CLOCKS_PER_SEC; |
return(clk * 1000 / CLOCKS_PER_SEC); |
499 |
#endif |
#endif |
500 |
} |
} |
501 |
|
|
511 |
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
512 |
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
513 |
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
514 |
|
fprintf(stderr, " -bn integer : max bframes (default=0)\n"); |
515 |
|
fprintf(stderr, " -bqr integer : bframe quantizer ratio (default=150)\n"); |
516 |
|
fprintf(stderr, " -bqo integer : bframe quantizer offset (default=100)\n"); |
517 |
fprintf(stderr, " -f float : target framerate (>0)\n"); |
fprintf(stderr, " -f float : target framerate (>0)\n"); |
518 |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
519 |
fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n"); |
fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n"); |
523 |
fprintf(stderr, " -m boolean : save mpeg4 raw stream (0 False*, !=0 True)\n"); |
fprintf(stderr, " -m boolean : save mpeg4 raw stream (0 False*, !=0 True)\n"); |
524 |
fprintf(stderr, " -o string : output container filename (only usefull when -m 1 is used) :\n"); |
fprintf(stderr, " -o string : output container filename (only usefull when -m 1 is used) :\n"); |
525 |
fprintf(stderr, " When this option is not used : one file per encoded frame\n"); |
fprintf(stderr, " When this option is not used : one file per encoded frame\n"); |
526 |
fprintf(stderr, " When this option is used :\n"); |
fprintf(stderr, " When this option is used : save to 'string' file\n"); |
|
fprintf(stderr, " + stream.m4v with -mt 0\n"); |
|
|
fprintf(stderr, " + stream.mp4u with -mt 1\n"); |
|
|
fprintf(stderr, " -mt integer : output type (m4v=0, mp4u=1)\n"); |
|
|
fprintf(stderr, " -mv integer : Use motion vector hints (no hints=0, get hints=1, set hints=2)\n"); |
|
527 |
fprintf(stderr, " -help : prints this help message\n"); |
fprintf(stderr, " -help : prints this help message\n"); |
528 |
fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); |
fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); |
529 |
fprintf(stderr, " (* means default)\n"); |
fprintf(stderr, " (* means default)\n"); |
546 |
bytes = fread(dummy,1,2,handle); |
bytes = fread(dummy,1,2,handle); |
547 |
|
|
548 |
if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) |
if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) |
549 |
return 1; |
return(1); |
550 |
|
|
551 |
fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
552 |
if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) |
if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) |
553 |
{ |
{ |
554 |
fprintf(stderr,"%d %d %d\n",xsize,ysize,depth); |
fprintf(stderr,"%d %d %d\n",xsize,ysize,depth); |
555 |
return 2; |
return(2); |
556 |
} |
} |
557 |
if ( (XDIM==0) || (YDIM==0) ) |
if ( (XDIM==0) || (YDIM==0) ) |
558 |
{ |
{ |
560 |
YDIM=ysize*2/3; |
YDIM=ysize*2/3; |
561 |
} |
} |
562 |
|
|
563 |
return 0; |
return(0); |
564 |
} |
} |
565 |
|
|
566 |
static int read_pgmdata(FILE* handle, unsigned char *image) |
static int read_pgmdata(FILE* handle, unsigned char *image) |
591 |
/* I don't know why, but this seems needed */ |
/* I don't know why, but this seems needed */ |
592 |
fread(&dummy, 1, 1, handle); |
fread(&dummy, 1, 1, handle); |
593 |
|
|
594 |
return 0; |
return(0); |
595 |
} |
} |
596 |
|
|
597 |
static int read_yuvdata(FILE* handle, unsigned char *image) |
static int read_yuvdata(FILE* handle, unsigned char *image) |
598 |
{ |
{ |
599 |
|
|
600 |
if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != (unsigned int)IMAGE_SIZE(XDIM, YDIM)) |
if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != (unsigned int)IMAGE_SIZE(XDIM, YDIM)) |
601 |
return 1; |
return(1); |
602 |
else |
else |
603 |
return 0; |
return(0); |
604 |
} |
} |
605 |
|
|
606 |
/***************************************************************************** |
/***************************************************************************** |
614 |
{ |
{ |
615 |
int xerr; |
int xerr; |
616 |
|
|
617 |
XVID_INIT_PARAM xinit; |
xvid_gbl_init_t xvid_gbl_init; |
618 |
XVID_ENC_PARAM xparam; |
xvid_enc_create_t xvid_enc_create; |
619 |
|
|
620 |
|
/*------------------------------------------------------------------------ |
621 |
|
* XviD core initialization |
622 |
|
*----------------------------------------------------------------------*/ |
623 |
|
|
624 |
|
/* Set version -- version checking will done by xvidcore*/ |
625 |
|
xvid_gbl_init.version = XVID_VERSION; |
626 |
|
|
627 |
|
/* Do we have to enable ASM optimizations ? */ |
628 |
if(use_assembler) { |
if(use_assembler) { |
629 |
|
|
630 |
#ifdef ARCH_IA64 |
#ifdef ARCH_IS_IA64 |
631 |
xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
632 |
#else |
#else |
633 |
xinit.cpu_flags = 0; |
xvid_gbl_init.cpu_flags = 0; |
634 |
#endif |
#endif |
635 |
} |
} |
636 |
else { |
else { |
637 |
xinit.cpu_flags = XVID_CPU_FORCE; |
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; |
638 |
} |
} |
639 |
|
|
640 |
xvid_init(NULL, 0, &xinit, NULL); |
/* Initialize XviD core -- Should be done once per __process__ */ |
641 |
|
xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL); |
642 |
|
|
643 |
xparam.width = XDIM; |
/*------------------------------------------------------------------------ |
644 |
xparam.height = YDIM; |
* XviD encoder initialization |
645 |
if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS) |
*----------------------------------------------------------------------*/ |
646 |
{ |
|
647 |
xparam.fincr = 1; |
/* Version again */ |
648 |
xparam.fbase = (int)ARG_FRAMERATE; |
xvid_enc_create.version = XVID_VERSION; |
649 |
} |
|
650 |
else |
/* Width and Height of input frames */ |
651 |
{ |
xvid_enc_create.width = XDIM; |
652 |
xparam.fincr = FRAMERATE_INCR; |
xvid_enc_create.height = YDIM; |
653 |
xparam.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE); |
|
654 |
|
/* No fancy thread tests */ |
655 |
|
xvid_enc_create.num_threads = 0; |
656 |
|
|
657 |
|
/* Frame rate - Do some quick float fps = fincr/fbase hack */ |
658 |
|
if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS) { |
659 |
|
xvid_enc_create.fincr = 1; |
660 |
|
xvid_enc_create.fbase = (int)ARG_FRAMERATE; |
661 |
|
} else { |
662 |
|
xvid_enc_create.fincr = FRAMERATE_INCR; |
663 |
|
xvid_enc_create.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE); |
664 |
} |
} |
665 |
xparam.rc_reaction_delay_factor = 16; |
|
666 |
xparam.rc_averaging_period = 100; |
/* Maximum key frame interval */ |
667 |
xparam.rc_buffer = 10; |
xvid_enc_create.max_key_interval = (int)ARG_FRAMERATE*10; |
668 |
xparam.rc_bitrate = ARG_BITRATE*1000; |
|
669 |
xparam.min_quantizer = ARG_MINQUANT; |
/* Bframes settings */ |
670 |
xparam.max_quantizer = ARG_MAXQUANT; |
xvid_enc_create.max_bframes = ARG_MAXBFRAMES; |
671 |
xparam.max_key_interval = (int)ARG_FRAMERATE*10; |
xvid_enc_create.bquant_ratio = ARG_BQRATIO; |
672 |
|
xvid_enc_create.bquant_offset = ARG_BQOFFSET; |
673 |
|
|
674 |
|
/* Dropping ratio frame -- we don't need that */ |
675 |
|
xvid_enc_create.frame_drop_ratio = 0; |
676 |
|
|
677 |
|
/* Global encoder options */ |
678 |
|
xvid_enc_create.global = XVID_EXTRASTATS_ENABLE; |
679 |
|
|
680 |
/* I use a small value here, since will not encode whole movies, but short clips */ |
/* I use a small value here, since will not encode whole movies, but short clips */ |
681 |
|
xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); |
682 |
|
|
683 |
xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xparam, NULL); |
/* Retrieve the encoder instance from the structure */ |
684 |
enc_handle=xparam.handle; |
enc_handle = xvid_enc_create.handle; |
685 |
|
|
686 |
return xerr; |
return(xerr); |
687 |
} |
} |
688 |
|
|
689 |
static int enc_stop() |
static int enc_stop() |
690 |
{ |
{ |
691 |
int xerr; |
int xerr; |
692 |
|
|
693 |
|
/* Destroy the encoder instance */ |
694 |
xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); |
xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); |
|
return xerr; |
|
695 |
|
|
696 |
|
return(xerr); |
697 |
} |
} |
698 |
|
|
699 |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
static int enc_main(unsigned char* image, |
700 |
unsigned char* hints_buffer, |
unsigned char* bitstream, |
701 |
long *streamlength, long *frametype, long *hints_size) |
long *frametype, |
702 |
|
int stats[3]) |
703 |
{ |
{ |
704 |
int xerr; |
int ret; |
705 |
|
|
706 |
XVID_ENC_FRAME xframe; |
xvid_enc_frame_t xvid_enc_frame; |
707 |
XVID_ENC_STATS xstats; |
xvid_enc_stats_t xvid_enc_stats[2]; |
708 |
|
|
709 |
xframe.bitstream = bitstream; |
/* Version for the frame and the stats */ |
710 |
xframe.length = -1; /* this is written by the routine */ |
xvid_enc_frame.version = XVID_VERSION; |
711 |
|
xvid_enc_stats[0].version = XVID_VERSION; |
712 |
|
xvid_enc_stats[1].version = XVID_VERSION; |
713 |
|
|
714 |
xframe.image = image; |
/* Bind output buffer */ |
715 |
xframe.colorspace = XVID_CSP_YV12; /* defined in <xvid.h> */ |
xvid_enc_frame.bitstream = bitstream; |
716 |
|
xvid_enc_frame.length = -1; |
717 |
|
|
718 |
xframe.intra = -1; /* let the codec decide between I-frame (1) and P-frame (0) */ |
/* Initialize input image fields */ |
719 |
|
xvid_enc_frame.input.plane[0] = image; |
720 |
|
xvid_enc_frame.input.csp = XVID_CSP_I420; |
721 |
|
xvid_enc_frame.input.stride[0] = XDIM; |
722 |
|
|
723 |
xframe.quant = ARG_QUANTI; /* is quant != 0, use a fixed quant (and ignore bitrate) */ |
/* Set up core's general features */ |
724 |
|
xvid_enc_frame.vol_flags = vol_presets[ARG_QUALITY]; |
725 |
|
|
726 |
xframe.motion = motion_presets[ARG_QUALITY]; |
/* Set up core's general features */ |
727 |
xframe.general = general_presets[ARG_QUALITY]; |
xvid_enc_frame.vop_flags = vop_presets[ARG_QUALITY]; |
|
xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL; |
|
728 |
|
|
729 |
xframe.hint.hintstream = hints_buffer; |
/* Frame type -- let core decide for us */ |
730 |
|
xvid_enc_frame.type = XVID_TYPE_AUTO; |
731 |
|
|
732 |
if(ARG_HINTMODE == HINT_MODE_SET) { |
/* Force the right quantizer */ |
733 |
xframe.hint.hintlength = *hints_size; |
xvid_enc_frame.quant = ARG_QUANTI; |
734 |
xframe.hint.rawhints = 0; |
xvid_enc_frame.bquant = 0; |
|
xframe.general |= XVID_HINTEDME_SET; |
|
|
} |
|
735 |
|
|
736 |
if(ARG_HINTMODE == HINT_MODE_GET) { |
/* Set up motion estimation flags */ |
737 |
xframe.hint.rawhints = 0; |
xvid_enc_frame.motion = motion_presets[ARG_QUALITY]; |
738 |
xframe.general |= XVID_HINTEDME_GET; |
|
739 |
} |
/* We don't use special matrices */ |
740 |
|
xvid_enc_frame.quant_intra_matrix = NULL; |
741 |
|
xvid_enc_frame.quant_inter_matrix = NULL; |
742 |
|
|
743 |
xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats); |
/* Foll proof */ |
744 |
|
xvid_enc_stats[0].sse_y = 0; |
745 |
|
xvid_enc_stats[0].sse_v = 0; |
746 |
|
xvid_enc_stats[0].sse_u = 0; |
747 |
|
|
748 |
if(ARG_HINTMODE == HINT_MODE_GET) |
/* Encode the frame */ |
749 |
*hints_size = xframe.hint.hintlength; |
xvid_enc_frame.vop_flags |= XVID_EXTRASTATS; |
750 |
|
ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame, &xvid_enc_stats); |
751 |
|
|
752 |
/* |
*frametype = xvid_enc_stats[0].type; |
753 |
* This is statictical data, e.g. for 2-pass. If you are not |
stats[0] = xvid_enc_stats[0].sse_y; |
754 |
* interested in any of this, you can use NULL instead of &xstats |
stats[1] = xvid_enc_stats[0].sse_u; |
755 |
*/ |
stats[2] = xvid_enc_stats[0].sse_v; |
|
*frametype = xframe.intra; |
|
|
*streamlength = xframe.length; |
|
756 |
|
|
757 |
return xerr; |
return(ret); |
758 |
} |
} |