1 |
/************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC - Example for encoding and decoding |
* XVID MPEG-4 VIDEO CODEC |
4 |
|
* - Console based test application - |
5 |
|
* |
6 |
|
* 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 |
17 |
* |
* |
18 |
* You should have received a copy of the GNU General Public License |
* You should have received a copy of the GNU General Public License |
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., 675 Mass Ave, Cambridge, MA 02139, USA. |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
* |
* |
22 |
*************************************************************************/ |
* $Id: xvid_encraw.c,v 1.12 2003-02-22 21:36:27 chl Exp $ |
|
|
|
|
/************************************************************************ |
|
23 |
* |
* |
24 |
* Speed test routine for XviD using the XviD-API |
****************************************************************************/ |
25 |
* (C) Christoph Lampert, 2002/08/17 |
|
26 |
|
/***************************************************************************** |
27 |
|
* Application notes : |
28 |
* |
* |
29 |
* A sequence of YUV pics in PGM or RAW file format is encoded and the |
* A sequence of YUV pics in PGM file format is encoded and decoded |
30 |
* raw MPEG-4 stream is written to stdout. |
* The speed is measured and PSNR of decoded picture is calculated. |
|
* The encoding speed of this is measured, too. |
|
31 |
* |
* |
32 |
* The program is plain C and needs no libraries except for libxvidcore, |
* The program is plain C and needs no libraries except for libxvidcore, |
33 |
* and maths-lib, so with UN*X you simply compile by |
* and maths-lib. |
|
* |
|
|
* gcc xvid_encraw.c -lxvidcore -lm -o xvid_encraw |
|
|
* |
|
|
* Run without or with illegal parameters, then PGM input input is read |
|
|
* from stdin. |
|
|
* |
|
|
* Parameters are: xvid_stat XDIM YDIM QUALITY BITRATE/QUANTIZER FRAMERATE |
|
|
* |
|
|
* if XDIM or YDIM are illegal (e.g. 0), they are ignored and input is |
|
|
* considered to be PGM. Otherwise (X and Y both greater than 0) raw YUV |
|
|
* is expected, as e.g. the standard MPEG test-files, like "foreman" |
|
|
* |
|
|
* 0 <= QUALITY <= 6 (default 5) |
|
|
* |
|
|
* BITRATE is in kbps (default 900), |
|
|
* if BITRATE<32, then value is taken is fixed QUANTIZER |
|
|
* |
|
|
* FRAMERATE is a float (with or without decimal dot), default is 25.00 |
|
|
* |
|
|
* input/output and m4v-output is saved, if corresponding flags are set |
|
|
* |
|
|
* PGM input must in a very specific format, see read_pgmheader |
|
|
* it can be generated e.g. from MPEG2 by mpeg2dec -o pgmpipe |
|
|
* |
|
|
************************************************************************/ |
|
|
|
|
|
/************************************************************************ |
|
|
* |
|
|
* For EXAMPLES how to use this, see the seperate file xvid_stat.examples |
|
34 |
* |
* |
35 |
************************************************************************/ |
************************************************************************/ |
36 |
|
|
37 |
#include <stdio.h> |
#include <stdio.h> |
38 |
#include <stdlib.h> |
#include <stdlib.h> |
39 |
#include <math.h> // needed for log10 |
#include <string.h> |
40 |
#include <sys/time.h> // only needed for gettimeofday |
#include <math.h> |
41 |
|
#ifndef WIN32 |
42 |
|
#include <sys/time.h> |
43 |
|
#else |
44 |
|
#include <time.h> |
45 |
|
#endif |
46 |
|
|
47 |
#include "../src/xvid.h" /* comes with XviD */ |
#include "xvid.h" |
48 |
|
|
49 |
int motion_presets[7] = { |
/***************************************************************************** |
50 |
0, // Q 0 |
* Quality presets |
51 |
PMV_EARLYSTOP16, // Q 1 |
****************************************************************************/ |
52 |
PMV_EARLYSTOP16, // Q 2 |
|
53 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, // Q 3 |
static int const motion_presets[7] = { |
54 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, // Q 4 |
0, /* Q 0 */ |
55 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 // Q 5 |
PMV_EARLYSTOP16, /* Q 1 */ |
56 |
| PMV_HALFPELREFINE8, |
PMV_EARLYSTOP16, /* Q 2 */ |
57 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 // Q 6 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 3 */ |
58 |
| PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 4 */ |
59 |
|
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | /* Q 5 */ |
60 |
|
PMV_HALFPELREFINE8, |
61 |
|
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | /* Q 6 */ |
62 |
|
PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 |
63 |
}; |
}; |
64 |
|
|
65 |
int general_presets[7] = { |
static int const general_presets[7] = { |
66 |
XVID_H263QUANT, /* or use XVID_MPEGQUANT */ // Q 0 |
XVID_H263QUANT, /* Q 0 */ |
67 |
XVID_MPEGQUANT, // Q 1 |
XVID_MPEGQUANT, /* Q 1 */ |
68 |
XVID_H263QUANT, // Q 2 |
XVID_H263QUANT, /* Q 2 */ |
69 |
XVID_H263QUANT | XVID_HALFPEL, // Q 3 |
XVID_H263QUANT | XVID_HALFPEL, /* Q 3 */ |
70 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, // Q 4 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 4 */ |
71 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, // Q 5 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 5 */ |
72 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V }; // Q 6 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V /* Q 6 */ |
73 |
|
}; |
74 |
|
|
|
/* my default values for encoding */ |
|
75 |
|
|
76 |
#define ABS_MAXFRAMENR 9999 // max number of frames |
/***************************************************************************** |
77 |
|
* Command line global variables |
78 |
|
****************************************************************************/ |
79 |
|
|
80 |
|
/* Maximum number of frames to encode */ |
81 |
|
#define ABS_MAXFRAMENR 9999 |
82 |
|
|
83 |
|
/* HINTMODEs */ |
84 |
|
#define HINT_MODE_NONE 0 |
85 |
|
#define HINT_MODE_GET 1 |
86 |
|
#define HINT_MODE_SET 2 |
87 |
|
#define HINT_FILE "hints.mv" |
88 |
|
|
89 |
|
static int ARG_BITRATE = 900; |
90 |
|
static int ARG_QUANTI = 0; |
91 |
|
static int ARG_QUALITY = 6; |
92 |
|
static int ARG_MINQUANT = 1; |
93 |
|
static int ARG_MAXQUANT = 31; |
94 |
|
static float ARG_FRAMERATE = 25.00f; |
95 |
|
static int ARG_MAXFRAMENR = ABS_MAXFRAMENR; |
96 |
|
static char *ARG_INPUTFILE = NULL; |
97 |
|
static int ARG_INPUTTYPE = 0; |
98 |
|
static int ARG_SAVEMPEGSTREAM = 0; |
99 |
|
static char *ARG_OUTPUTFILE = NULL; |
100 |
|
static int ARG_HINTMODE = HINT_MODE_NONE; |
101 |
|
static int XDIM = 0; |
102 |
|
static int YDIM = 0; |
103 |
|
static int ARG_BQRATIO = 120; |
104 |
|
static int ARG_BQOFFSET = 0; |
105 |
|
static int ARG_MAXBFRAMES = 0; |
106 |
|
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
107 |
|
|
108 |
int ARG_BITRATE=900; |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
109 |
int ARG_QUANTI=0; |
#define SMALL_EPS 1e-10 |
110 |
|
|
111 |
int ARG_QUALITY =6; |
#define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ |
112 |
int ARG_MINQUANT=1; |
(((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) |
|
int ARG_MAXQUANT=31; |
|
|
float ARG_FRAMERATE=25.00; |
|
113 |
|
|
114 |
int ARG_MAXFRAMENR=ABS_MAXFRAMENR; |
/**************************************************************************** |
115 |
|
* Nasty global vars ;-) |
116 |
|
***************************************************************************/ |
117 |
|
|
118 |
|
static int i,filenr = 0; |
119 |
|
|
120 |
|
/* the path where to save output */ |
121 |
|
static char filepath[256] = "./"; |
122 |
|
|
123 |
|
/* Internal structures (handles) for encoding and decoding */ |
124 |
|
static void *enc_handle = NULL; |
125 |
|
|
126 |
|
/***************************************************************************** |
127 |
|
* Local prototypes |
128 |
|
****************************************************************************/ |
129 |
|
|
130 |
|
/* Prints program usage message */ |
131 |
|
static void usage(); |
132 |
|
|
133 |
|
/* Statistical functions */ |
134 |
|
static double msecond(); |
135 |
|
|
136 |
|
/* PGM related functions */ |
137 |
|
static int read_pgmheader(FILE* handle); |
138 |
|
static int read_pgmdata(FILE* handle, unsigned char *image); |
139 |
|
static int read_yuvdata(FILE* handle, unsigned char *image); |
140 |
|
|
141 |
|
/* Encoder related functions */ |
142 |
|
static int enc_init(int use_assembler); |
143 |
|
static int enc_stop(); |
144 |
|
static int enc_main(unsigned char* image, unsigned char* bitstream, |
145 |
|
unsigned char *hints_buffer, |
146 |
|
long *streamlength, long* frametype, long *hints_size); |
147 |
|
|
148 |
|
/***************************************************************************** |
149 |
|
* Main function |
150 |
|
****************************************************************************/ |
151 |
|
|
152 |
#ifdef BFRAMES |
int main(int argc, char *argv[]) |
153 |
|
{ |
154 |
|
|
155 |
int ARG_MAXBFRAMES=1; |
unsigned char *mp4_buffer = NULL; |
156 |
int ARG_BQUANTRATIO=200; |
unsigned char *in_buffer = NULL; |
157 |
|
unsigned char *out_buffer = NULL; |
158 |
|
unsigned char *hints_buffer = NULL; |
159 |
|
|
160 |
#endif |
double enctime; |
161 |
|
double totalenctime=0.; |
162 |
|
|
163 |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
long totalsize; |
164 |
#define SMALL_EPS 1e-10 |
long hints_size; |
165 |
|
int status; |
166 |
|
long frame_type; |
167 |
|
long bigendian; |
168 |
|
|
169 |
/* these are global variables. Not very elegant, but easy, and this is an easy program */ |
long m4v_size; |
170 |
|
int use_assembler=0; |
171 |
|
|
172 |
int XDIM=0; |
char filename[256]; |
|
int YDIM=0; // will be set when reading first image |
|
|
int i,filenr = 0; |
|
173 |
|
|
174 |
int save_m4v_flag = 1; // output MPEG4-bytestream? |
FILE *in_file = stdin; |
175 |
int save_ref_flag = 0; // save input image |
FILE *out_file = NULL; |
176 |
|
FILE *hints_file = NULL; |
177 |
|
|
178 |
int pgmflag = 0; // a flag, if input is in PGM format, overwritten in init-phase |
printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
179 |
char filepath[256] = "./"; // the path where to save output |
printf("written by Christoph Lampert 2002-2003\n\n"); |
180 |
|
|
181 |
void *enc_handle = NULL; // internal structures (handles) for encoding |
/***************************************************************************** |
182 |
|
* Command line parsing |
183 |
|
****************************************************************************/ |
184 |
|
|
185 |
|
for (i=1; i< argc; i++) { |
186 |
|
|
187 |
/*********************************************************************/ |
if (strcmp("-asm", argv[i]) == 0 ) { |
188 |
/* "statistical" functions */ |
use_assembler = 1; |
189 |
/* */ |
} |
190 |
/* these are not needed for encoding or decoding, but for measuring */ |
else if (strcmp("-w", argv[i]) == 0 && i < argc - 1 ) { |
191 |
/* time and quality, there in nothing specific to XviD in these */ |
i++; |
192 |
/* */ |
XDIM = atoi(argv[i]); |
193 |
/*********************************************************************/ |
} |
194 |
|
else if (strcmp("-h", argv[i]) == 0 && i < argc - 1 ) { |
195 |
|
i++; |
196 |
|
YDIM = atoi(argv[i]); |
197 |
|
} |
198 |
|
else if (strcmp("-b", argv[i]) == 0 && i < argc - 1 ) { |
199 |
|
i++; |
200 |
|
ARG_BITRATE = atoi(argv[i]); |
201 |
|
} |
202 |
|
else if (strcmp("-bn", argv[i]) == 0 && i < argc - 1 ) { |
203 |
|
i++; |
204 |
|
ARG_MAXBFRAMES = atoi(argv[i]); |
205 |
|
} |
206 |
|
else if (strcmp("-bqr", argv[i]) == 0 && i < argc - 1 ) { |
207 |
|
i++; |
208 |
|
ARG_BQRATIO = atoi(argv[i]); |
209 |
|
} |
210 |
|
else if (strcmp("-bqo", argv[i]) == 0 && i < argc - 1 ) { |
211 |
|
i++; |
212 |
|
ARG_BQOFFSET = atoi(argv[i]); |
213 |
|
} |
214 |
|
else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) { |
215 |
|
i++; |
216 |
|
ARG_QUALITY = atoi(argv[i]); |
217 |
|
} |
218 |
|
else if (strcmp("-f", argv[i]) == 0 && i < argc - 1 ) { |
219 |
|
i++; |
220 |
|
ARG_FRAMERATE = (float)atof(argv[i]); |
221 |
|
} |
222 |
|
else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) { |
223 |
|
i++; |
224 |
|
ARG_INPUTFILE = argv[i]; |
225 |
|
} |
226 |
|
else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) { |
227 |
|
i++; |
228 |
|
ARG_INPUTTYPE = atoi(argv[i]); |
229 |
|
} |
230 |
|
else if(strcmp("-n", argv[i]) == 0 && i < argc - 1 ) { |
231 |
|
i++; |
232 |
|
ARG_MAXFRAMENR = atoi(argv[i]); |
233 |
|
} |
234 |
|
else if (strcmp("-quant", argv[i]) == 0 && i < argc - 1 ) { |
235 |
|
i++; |
236 |
|
ARG_QUANTI = atoi(argv[i]); |
237 |
|
} |
238 |
|
else if (strcmp("-m", argv[i]) == 0 && i < argc - 1 ) { |
239 |
|
i++; |
240 |
|
ARG_SAVEMPEGSTREAM = atoi(argv[i]); |
241 |
|
} |
242 |
|
else if (strcmp("-mv", argv[i]) == 0 && i < argc - 1 ) { |
243 |
|
i++; |
244 |
|
ARG_HINTMODE = atoi(argv[i]); |
245 |
|
} |
246 |
|
else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { |
247 |
|
i++; |
248 |
|
ARG_OUTPUTFILE = argv[i]; |
249 |
|
} |
250 |
|
else if (strcmp("-help", argv[i])) { |
251 |
|
usage(); |
252 |
|
return(0); |
253 |
|
} |
254 |
|
else { |
255 |
|
usage(); |
256 |
|
exit(-1); |
257 |
|
} |
258 |
|
|
|
double msecond() |
|
|
/* return the current time in seconds(!) */ |
|
|
{ |
|
|
struct timeval tv; |
|
|
gettimeofday(&tv, 0); |
|
|
return tv.tv_sec + tv.tv_usec * 1.0e-6; |
|
259 |
} |
} |
260 |
|
|
261 |
/*********************************************************************/ |
/***************************************************************************** |
262 |
/* input and output functions */ |
* Arguments checking |
263 |
/* */ |
****************************************************************************/ |
|
/* the are small and simple routines to read and write PGM and YUV */ |
|
|
/* image. It's just for convenience, again nothing specific to XviD */ |
|
|
/* */ |
|
|
/*********************************************************************/ |
|
264 |
|
|
265 |
int read_pgmheader(FILE* handle) |
if (XDIM <= 0 || XDIM >= 2048 || YDIM <=0 || YDIM >= 2048 ) { |
266 |
{ |
fprintf(stderr, "Trying to retreive width and height from PGM header\n"); |
267 |
int bytes,xsize,ysize,depth; |
ARG_INPUTTYPE = 1; /* pgm */ |
268 |
char dummy[2]; |
} |
269 |
|
|
270 |
bytes = fread(dummy,1,2,handle); |
if ( ARG_QUALITY < 0 || ARG_QUALITY > 6) { |
271 |
|
fprintf(stderr,"Wrong Quality\n"); |
272 |
|
return -1; |
273 |
|
} |
274 |
|
|
275 |
if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) |
if ( ARG_BITRATE <= 0 && ARG_QUANTI == 0) { |
276 |
return 1; |
fprintf(stderr,"Wrong Bitrate\n"); |
277 |
fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
return -1; |
|
if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) |
|
|
{ |
|
|
return 2; |
|
278 |
} |
} |
279 |
if ( (XDIM==0) || (YDIM==0) ) |
|
280 |
{ XDIM=xsize; |
if ( ARG_FRAMERATE <= 0) { |
281 |
YDIM=ysize; |
fprintf(stderr,"Wrong Framerate %s \n",argv[5]); |
282 |
|
return -1; |
283 |
} |
} |
284 |
|
|
285 |
return 0; |
if ( ARG_MAXFRAMENR <= 0) { |
286 |
|
fprintf(stderr,"Wrong number of frames\n"); |
287 |
|
return -1; |
288 |
} |
} |
289 |
|
|
290 |
int read_pgmdata(FILE* handle, unsigned char *image) |
if ( ARG_HINTMODE != HINT_MODE_NONE && |
291 |
{ |
ARG_HINTMODE != HINT_MODE_GET && |
292 |
int i,status; |
ARG_HINTMODE != HINT_MODE_SET) |
293 |
char dummy; |
ARG_HINTMODE = HINT_MODE_NONE; |
294 |
|
|
295 |
unsigned char* buff1_ptr2 = image + XDIM*YDIM; |
if( ARG_HINTMODE != HINT_MODE_NONE) { |
296 |
unsigned char* buff1_ptr3 = image + XDIM*YDIM + XDIM/2*YDIM/2; |
char *rights = "rb"; |
297 |
|
|
298 |
fread(image,XDIM*YDIM,1,stdin); // read Y component of picture |
/* |
299 |
|
* If we are getting hints from core, we will have to write them to |
300 |
|
* hint file |
301 |
|
*/ |
302 |
|
if(ARG_HINTMODE == HINT_MODE_GET) |
303 |
|
rights = "w+b"; |
304 |
|
|
305 |
for (i=0;i<YDIM/2;i++) |
/* Open the hint file */ |
306 |
{ |
hints_file = fopen(HINT_FILE, rights); |
307 |
fread(buff1_ptr2,XDIM/2,1,stdin); // read U |
if(hints_file == NULL) { |
308 |
buff1_ptr2 += XDIM/2; |
fprintf(stderr, "Error opening input file %s\n", HINT_FILE); |
309 |
fread(buff1_ptr3,XDIM/2,1,stdin); // read V |
return -1; |
|
buff1_ptr3 += XDIM/2; |
|
|
} |
|
|
fread(&dummy,1,1,handle); // I don't know why, but this seems needed |
|
|
return 0; |
|
310 |
} |
} |
311 |
|
|
312 |
int read_yuvdata(FILE* handle, unsigned char *image) |
/* Allocate hint memory space, we will be using rawhints */ |
313 |
{ int i; |
/* NB : Hope 1Mb is enough */ |
314 |
char dummy; |
if((hints_buffer = malloc(1024*1024)) == NULL) { |
315 |
|
fprintf(stderr, "Memory allocation error\n"); |
316 |
|
return -1; |
317 |
|
} |
318 |
|
|
319 |
unsigned char* buff1_ptr2 = image + XDIM*YDIM; |
} |
|
unsigned char* buff1_ptr3 = image + XDIM*YDIM + XDIM/2*YDIM/2; |
|
320 |
|
|
321 |
if (fread(image,XDIM,YDIM*3/2,stdin) != YDIM*3/2) |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
322 |
return 1; |
in_file = stdin; |
|
else |
|
|
return 0; |
|
323 |
} |
} |
324 |
|
else { |
325 |
|
|
326 |
int write_pgm(char *filename, unsigned char *image) |
in_file = fopen(ARG_INPUTFILE, "rb"); |
327 |
{ |
if (in_file == NULL) { |
328 |
FILE *filehandle; |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
329 |
filehandle=fopen(filename,"wb"); |
return -1; |
|
if (filehandle) |
|
|
{ |
|
|
fprintf(filehandle,"P5\n\n"); // |
|
|
fprintf(filehandle,"%d %d 255\n",XDIM,YDIM*3/2); |
|
|
fwrite(image,XDIM,YDIM*3/2,filehandle); |
|
|
fclose(filehandle); |
|
|
return 0; |
|
330 |
} |
} |
|
else |
|
|
return 1; |
|
331 |
} |
} |
332 |
|
|
333 |
/*********************************************************************/ |
if (ARG_INPUTTYPE) { |
334 |
/* Routines for encoding: init encoder, frame step, release encoder */ |
if (read_pgmheader(in_file)) { |
335 |
/*********************************************************************/ |
fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); |
336 |
|
return -1; |
337 |
|
} |
338 |
|
} |
339 |
|
|
340 |
#define FRAMERATE_INCR 1001 |
/* now we know the sizes, so allocate memory */ |
341 |
|
in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)); |
342 |
|
if (!in_buffer) |
343 |
|
goto free_all_memory; |
344 |
|
|
345 |
|
/* this should really be enough memory ! */ |
346 |
|
mp4_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)*2); |
347 |
|
if (!mp4_buffer) |
348 |
|
goto free_all_memory; |
349 |
|
|
350 |
int enc_init(int use_assembler) |
/***************************************************************************** |
351 |
{ /* initialize encoder for first use, pass all needed parameters to the codec */ |
* XviD PART Start |
352 |
int xerr; |
****************************************************************************/ |
353 |
|
|
|
XVID_INIT_PARAM xinit; |
|
|
XVID_ENC_PARAM xparam; |
|
354 |
|
|
355 |
if(use_assembler) |
status = enc_init(use_assembler); |
356 |
|
if (status) |
357 |
|
{ |
358 |
|
fprintf(stderr, "Encore INIT problem, return value %d\n", status); |
359 |
|
goto release_all; |
360 |
|
} |
361 |
|
|
362 |
#ifdef ARCH_IA64 |
/***************************************************************************** |
363 |
xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
* Main loop |
364 |
#else |
****************************************************************************/ |
|
xinit.cpu_flags = 0; |
|
|
#endif |
|
365 |
|
|
366 |
else |
if (ARG_SAVEMPEGSTREAM && ARG_OUTPUTFILE) { |
|
xinit.cpu_flags = XVID_CPU_FORCE; |
|
367 |
|
|
368 |
xvid_init(NULL, 0, &xinit, NULL); |
if((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) { |
369 |
|
fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE); |
370 |
|
goto release_all; |
371 |
|
} |
372 |
|
|
|
xparam.width = XDIM; |
|
|
xparam.height = YDIM; |
|
|
if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS) |
|
|
{ |
|
|
xparam.fincr = 1; |
|
|
xparam.fbase = (int)ARG_FRAMERATE; |
|
373 |
} |
} |
374 |
else |
else { |
375 |
{ |
out_file = NULL; |
|
xparam.fincr = FRAMERATE_INCR; |
|
|
xparam.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE); |
|
376 |
} |
} |
|
xparam.rc_reaction_delay_factor = 16; |
|
|
xparam.rc_averaging_period = 100; |
|
|
xparam.rc_buffer = 10; |
|
|
xparam.rc_bitrate = ARG_BITRATE*1000; |
|
|
xparam.min_quantizer = 1; |
|
|
xparam.max_quantizer = 31; |
|
|
xparam.max_key_interval = (int)ARG_FRAMERATE*10; |
|
377 |
|
|
378 |
#ifdef BFRAMES |
/***************************************************************************** |
379 |
xparam.global = XVID_GLOBAL_DX50BVOP; |
* Encoding loop |
380 |
xparam.max_bframes = ARG_MAXBFRAMES; |
****************************************************************************/ |
|
xparam.bquant_ratio = ARG_BQUANTRATIO; |
|
|
xparam.frame_drop_ratio=0; |
|
|
#endif |
|
381 |
|
|
382 |
/* I use a small value here, since will not encode whole movies, |
totalsize = 0; |
|
but short clips */ |
|
383 |
|
|
384 |
xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xparam, NULL); |
do { |
|
enc_handle=xparam.handle; |
|
385 |
|
|
386 |
return xerr; |
if (ARG_INPUTTYPE) |
387 |
|
status = read_pgmdata(in_file, in_buffer); /* read PGM data (YUV-format) */ |
388 |
|
else |
389 |
|
status = read_yuvdata(in_file, in_buffer); /* read raw data (YUV-format) */ |
390 |
|
|
391 |
|
if (status) |
392 |
|
{ |
393 |
|
/* Couldn't read image, most likely end-of-file */ |
394 |
|
continue; |
395 |
} |
} |
396 |
|
|
397 |
int enc_stop() |
/***************************************************************************** |
398 |
{ int xerr; |
* Read hints from file |
399 |
|
****************************************************************************/ |
400 |
|
|
401 |
xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); |
if(ARG_HINTMODE == HINT_MODE_SET) { |
402 |
return xerr; |
fread(&hints_size, 1, sizeof(long), hints_file); |
403 |
|
hints_size = (!bigendian)?SWAP(hints_size):hints_size; |
404 |
|
fread(hints_buffer, 1, hints_size, hints_file); |
405 |
} |
} |
406 |
|
|
407 |
int enc_main(unsigned char* image, unsigned char* bitstream, int *streamlength, int* frametype) |
/***************************************************************************** |
408 |
{ int xerr; |
* Encode and decode this frame |
409 |
|
****************************************************************************/ |
410 |
|
|
411 |
XVID_ENC_FRAME xframe; |
enctime = msecond(); |
412 |
XVID_ENC_STATS xstats; |
status = enc_main(in_buffer, mp4_buffer, hints_buffer, |
413 |
|
&m4v_size, &frame_type, &hints_size); |
414 |
|
enctime = msecond() - enctime; |
415 |
|
|
416 |
xframe.bitstream = bitstream; |
/* if it's a not coded VOP (aka NVOP) then we write nothing */ |
417 |
xframe.length = -1; // this is written by the routine |
if(frame_type == 5) goto next_frame; |
418 |
|
|
419 |
xframe.image = image; |
{ |
420 |
xframe.colorspace = XVID_CSP_YV12; // defined in <xvid.h> |
char *type[] = {"P", "I", "B", "S", "Packed", "N", "Unknown"}; |
421 |
|
|
422 |
xframe.intra = -1; // let the codec decide between I-frame (1) and P-frame (0) |
if(frame_type<0 || frame_type>5) frame_type = 6; |
423 |
|
|
424 |
xframe.quant = ARG_QUANTI; // is quant != 0, use a fixed quant (and ignore bitrate) |
printf("Frame %5d: type = %s, enctime(ms) =%6.1f, length(bytes) =%7d\n", |
425 |
|
(int)filenr, type[frame_type], (float)enctime, (int)m4v_size); |
426 |
|
|
427 |
xframe.motion = motion_presets[ARG_QUALITY]; |
} |
|
xframe.general = general_presets[ARG_QUALITY]; |
|
|
xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL; |
|
428 |
|
|
429 |
#ifdef BFRAMES |
/* Update encoding time stats */ |
430 |
xframe.bquant = 0; |
totalenctime += enctime; |
431 |
#endif |
totalsize += m4v_size; |
432 |
|
|
433 |
xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats); |
/***************************************************************************** |
434 |
|
* Save hints to file |
435 |
|
****************************************************************************/ |
436 |
|
|
437 |
/* enc_result->is_key_frame = xframe.intra; |
if(ARG_HINTMODE == HINT_MODE_GET) { |
438 |
enc_result->quantizer = xframe.quant; |
hints_size = (!bigendian)?SWAP(hints_size):hints_size; |
439 |
enc_result->total_bits = xframe.length * 8; |
fwrite(&hints_size, 1, sizeof(long), hints_file); |
440 |
enc_result->motion_bits = xstats.hlength * 8; |
hints_size = (!bigendian)?SWAP(hints_size):hints_size; |
441 |
enc_result->texture_bits = enc_result->total_bits - enc_result->motion_bits; |
fwrite(hints_buffer, 1, hints_size, hints_file); |
442 |
*/ |
} |
443 |
|
|
444 |
/* This is statictical data, e.g. for 2-pass. |
/***************************************************************************** |
445 |
If you are not interested in any of this, you can use |
* Save stream to file |
446 |
NULL instead of &xstats |
****************************************************************************/ |
|
*/ |
|
|
*frametype = xframe.intra; |
|
|
*streamlength = xframe.length; |
|
447 |
|
|
448 |
return xerr; |
if (ARG_SAVEMPEGSTREAM) |
449 |
|
{ |
450 |
|
/* Save single files */ |
451 |
|
if (out_file == NULL) { |
452 |
|
sprintf(filename, "%sframe%05d.m4v", filepath, filenr); |
453 |
|
out_file = fopen(filename, "wb"); |
454 |
|
fwrite(mp4_buffer, m4v_size, 1, out_file); |
455 |
|
fclose(out_file); |
456 |
|
out_file = NULL; |
457 |
} |
} |
458 |
|
else { |
459 |
|
|
460 |
/*********************************************************************/ |
/* Write mp4 data */ |
461 |
/* Main program */ |
fwrite(mp4_buffer, 1, m4v_size, out_file); |
|
/*********************************************************************/ |
|
462 |
|
|
463 |
int main(int argc, char *argv[]) |
} |
464 |
{ |
} |
|
unsigned char *divx_buffer = NULL; |
|
|
unsigned char *in_buffer = NULL; |
|
465 |
|
|
466 |
double enctime; |
next_frame: |
467 |
double totalenctime=0.; |
/* Read the header if it's pgm stream */ |
468 |
|
if (ARG_INPUTTYPE) |
469 |
|
status = read_pgmheader(in_file); |
470 |
|
|
471 |
long totalsize=0; |
if(frame_type != 5) filenr++; |
|
int status; |
|
472 |
|
|
473 |
int m4v_size; |
} while ( (!status) && (filenr<ARG_MAXFRAMENR) ); |
|
int frame_type[ABS_MAXFRAMENR]; |
|
|
int Iframes=0, Pframes=0, Bframes=0; |
|
|
int use_assembler=1; |
|
474 |
|
|
|
char filename[256]; |
|
475 |
|
|
|
FILE *filehandle; |
|
476 |
|
|
477 |
/* read YUV in pgm format from stdin */ |
/***************************************************************************** |
478 |
if (!pgmflag) |
* Calculate totals and averages for output, print results |
479 |
|
****************************************************************************/ |
480 |
|
|
481 |
|
totalsize /= filenr; |
482 |
|
totalenctime /= filenr; |
483 |
|
|
484 |
|
printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d\n", |
485 |
|
totalenctime, 1000/totalenctime, (int)totalsize); |
486 |
|
|
487 |
|
/***************************************************************************** |
488 |
|
* XviD PART Stop |
489 |
|
****************************************************************************/ |
490 |
|
|
491 |
|
release_all: |
492 |
|
|
493 |
|
if (enc_handle) |
494 |
{ |
{ |
495 |
pgmflag = 1; |
status = enc_stop(); |
496 |
|
if (status) |
497 |
|
fprintf(stderr, "Encore RELEASE problem return value %d\n", status); |
498 |
|
} |
499 |
|
|
500 |
// if (argc==2 && !strcmp(argv[1],"-noasm")) |
if(in_file) |
501 |
// use_assembler = 0; |
fclose(in_file); |
502 |
|
if(out_file) |
503 |
|
fclose(out_file); |
504 |
|
if(hints_file) |
505 |
|
fclose(hints_file); |
506 |
|
|
507 |
|
free_all_memory: |
508 |
|
free(out_buffer); |
509 |
|
free(mp4_buffer); |
510 |
|
free(in_buffer); |
511 |
|
if(hints_buffer) free(hints_buffer); |
512 |
|
|
513 |
|
return 0; |
514 |
|
|
|
if (argc>=3) |
|
|
{ XDIM = atoi(argv[1]); |
|
|
YDIM = atoi(argv[2]); |
|
|
if ( (XDIM <= 0) || (XDIM >= 2048) || (YDIM <=0) || (YDIM >= 2048) ) |
|
|
{ fprintf(stderr,"Wrong frames size %d %d, trying PGM \n",XDIM, YDIM); |
|
515 |
} |
} |
516 |
else |
|
517 |
|
|
518 |
|
/***************************************************************************** |
519 |
|
* "statistical" functions |
520 |
|
* |
521 |
|
* these are not needed for encoding or decoding, but for measuring |
522 |
|
* time and quality, there in nothing specific to XviD in these |
523 |
|
* |
524 |
|
*****************************************************************************/ |
525 |
|
|
526 |
|
/* Return time elapsed time in miliseconds since the program started */ |
527 |
|
static double msecond() |
528 |
{ |
{ |
529 |
YDIM = YDIM*3/2; /* for YUV */ |
#ifndef WIN32 |
530 |
pgmflag = 0; |
struct timeval tv; |
531 |
} |
gettimeofday(&tv, 0); |
532 |
|
return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3; |
533 |
|
#else |
534 |
|
clock_t clk; |
535 |
|
clk = clock(); |
536 |
|
return clk * 1000 / CLOCKS_PER_SEC; |
537 |
|
#endif |
538 |
} |
} |
539 |
|
|
540 |
|
/***************************************************************************** |
541 |
|
* Usage message |
542 |
|
*****************************************************************************/ |
543 |
|
|
544 |
|
static void usage() |
545 |
|
{ |
546 |
|
|
547 |
|
fprintf(stderr, "Usage : xvid_stat [OPTIONS]\n"); |
548 |
|
fprintf(stderr, "Options :\n"); |
549 |
|
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
550 |
|
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
551 |
|
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
552 |
|
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
553 |
|
fprintf(stderr, " -bn integer : max bframes (default=0)\n"); |
554 |
|
fprintf(stderr, " -bqr integer : bframe quantizer ratio (default=150)\n"); |
555 |
|
fprintf(stderr, " -bqo integer : bframe quantizer offset (default=100)\n"); |
556 |
|
fprintf(stderr, " -f float : target framerate (>0)\n"); |
557 |
|
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
558 |
|
fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n"); |
559 |
|
fprintf(stderr, " -n integer : number of frames to encode\n"); |
560 |
|
fprintf(stderr, " -q integer : quality ([0..5])\n"); |
561 |
|
fprintf(stderr, " -d boolean : save decoder output (0 False*, !=0 True)\n"); |
562 |
|
fprintf(stderr, " -m boolean : save mpeg4 raw stream (0 False*, !=0 True)\n"); |
563 |
|
fprintf(stderr, " -o string : output container filename (only usefull when -m 1 is used) :\n"); |
564 |
|
fprintf(stderr, " When this option is not used : one file per encoded frame\n"); |
565 |
|
fprintf(stderr, " When this option is used : save to 'string' (default=stream.m4v)\n"); |
566 |
|
fprintf(stderr, " -mt integer : output type (m4v=0, mp4u=1)\n"); |
567 |
|
fprintf(stderr, " -mv integer : Use motion vector hints (no hints=0, get hints=1, set hints=2)\n"); |
568 |
|
fprintf(stderr, " -help : prints this help message\n"); |
569 |
|
fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); |
570 |
|
fprintf(stderr, " (* means default)\n"); |
571 |
|
|
572 |
} |
} |
573 |
|
|
574 |
if (pgmflag) |
/***************************************************************************** |
575 |
{ if (read_pgmheader(stdin)) |
* Input and output functions |
576 |
|
* |
577 |
|
* the are small and simple routines to read and write PGM and YUV |
578 |
|
* image. It's just for convenience, again nothing specific to XviD |
579 |
|
* |
580 |
|
*****************************************************************************/ |
581 |
|
|
582 |
|
static int read_pgmheader(FILE* handle) |
583 |
{ |
{ |
584 |
fprintf(stderr,"Wrong input format, I want YUV encapsulated in PGM\n"); |
int bytes,xsize,ysize,depth; |
585 |
|
char dummy[2]; |
586 |
|
|
587 |
|
bytes = fread(dummy,1,2,handle); |
588 |
|
|
589 |
|
if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) |
590 |
return 1; |
return 1; |
591 |
|
|
592 |
|
fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
593 |
|
if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) |
594 |
|
{ |
595 |
|
fprintf(stderr,"%d %d %d\n",xsize,ysize,depth); |
596 |
|
return 2; |
597 |
} |
} |
598 |
|
if ( (XDIM==0) || (YDIM==0) ) |
599 |
|
{ |
600 |
|
XDIM=xsize; |
601 |
|
YDIM=ysize*2/3; |
602 |
} |
} |
603 |
if (argc>=4) |
|
604 |
{ ARG_QUALITY = atoi(argv[3]); |
return 0; |
|
if ( (ARG_QUALITY < 0) || (ARG_QUALITY > 6) ) |
|
|
{ fprintf(stderr,"Wrong Quality\n"); return -1; } |
|
|
else |
|
|
fprintf(stderr,"Quality %d\n",ARG_QUALITY); |
|
|
} |
|
|
if (argc>=5) |
|
|
{ ARG_BITRATE = atoi(argv[4]); |
|
|
if ( (ARG_BITRATE <= 0) ) |
|
|
{ fprintf(stderr,"Wrong Bitrate\n"); return -1; } |
|
|
if ( (ARG_BITRATE < 32) ) |
|
|
{ ARG_QUANTI = ARG_BITRATE; |
|
|
ARG_BITRATE=0; |
|
|
fprintf(stderr,"Quantizer %d\n",ARG_QUANTI); |
|
|
} |
|
|
else |
|
|
fprintf(stderr,"Bitrate %d kbps\n",ARG_BITRATE); |
|
|
} |
|
|
if (argc>=6) |
|
|
{ ARG_FRAMERATE = (float)atof(argv[5]); |
|
|
if ( (ARG_FRAMERATE <= 0) ) |
|
|
{ fprintf(stderr,"Wrong Fraterate %s \n",argv[5]); return -1; } |
|
|
fprintf(stderr,"Framerate %6.3f fps\n",ARG_FRAMERATE); |
|
|
} |
|
|
|
|
|
if (argc>=7) |
|
|
{ ARG_MAXFRAMENR = atoi(argv[6]); |
|
|
if ( (ARG_MAXFRAMENR <= 0) ) |
|
|
{ fprintf(stderr,"Wrong number of frames\n"); return -1; } |
|
|
fprintf(stderr,"max. Framenr. %d\n",ARG_MAXFRAMENR); |
|
|
} |
|
|
|
|
|
#ifdef BFRAMES |
|
|
if (argc>=8) |
|
|
{ ARG_MAXBFRAMES = atoi(argv[7]); |
|
|
if ( (ARG_MAXBFRAMES < -1) || ( ARG_MAXBFRAMES > ARG_FRAMERATE) ) |
|
|
{ fprintf(stderr,"Wrong maximumnumber of bframes\n"); return -1; } |
|
|
fprintf(stderr,"max. B-frames %d\n",ARG_MAXBFRAMES); |
|
|
} |
|
|
|
|
|
if (argc>=9) |
|
|
{ ARG_MAXFRAMENR = atoi(argv[8]); |
|
|
if ( (ARG_BQUANTRATIO <= 0) ) |
|
|
{ fprintf(stderr,"Wrong B-frames Quantizer ratio \n"); return -1; } |
|
|
fprintf(stderr,"B-frames quant-ratio %d\n",ARG_BQUANTRATIO); |
|
605 |
} |
} |
|
#endif |
|
606 |
|
|
607 |
/* now we know the sizes, so allocate memory */ |
static int read_pgmdata(FILE* handle, unsigned char *image) |
608 |
|
{ |
609 |
|
int i; |
610 |
|
char dummy; |
611 |
|
|
612 |
in_buffer = (unsigned char *) malloc(XDIM*YDIM); |
unsigned char *y = image; |
613 |
if (!in_buffer) |
unsigned char *u = image + XDIM*YDIM; |
614 |
goto free_all_memory; |
unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2; |
615 |
|
|
616 |
divx_buffer = (unsigned char *) malloc(XDIM*YDIM*2); |
/* read Y component of picture */ |
617 |
if (!divx_buffer) |
fread(y, 1, XDIM*YDIM, handle); |
|
goto free_all_memory; |
|
618 |
|
|
619 |
YDIM = YDIM*2/3; // PGM is YUV 4:2:0 format, so real image height is *2/3 of PGM picture |
for (i=0;i<YDIM/2;i++) |
620 |
|
{ |
621 |
|
/* read U */ |
622 |
|
fread(u, 1, XDIM/2, handle); |
623 |
|
|
624 |
/*********************************************************************/ |
/* read V */ |
625 |
/* XviD PART Start */ |
fread(v, 1, XDIM/2, handle); |
|
/*********************************************************************/ |
|
626 |
|
|
627 |
status = enc_init(use_assembler); |
/* Update pointers */ |
628 |
if (status) |
u += XDIM/2; |
629 |
{ |
v += XDIM/2; |
|
fprintf(stderr,"Encore INIT problem, return value %d\n", status); |
|
|
goto release_all; |
|
630 |
} |
} |
631 |
|
|
632 |
/*********************************************************************/ |
/* I don't know why, but this seems needed */ |
633 |
/* Main loop */ |
fread(&dummy, 1, 1, handle); |
634 |
/*********************************************************************/ |
|
635 |
|
return 0; |
636 |
|
} |
637 |
|
|
638 |
do |
static int read_yuvdata(FILE* handle, unsigned char *image) |
639 |
{ |
{ |
640 |
if (pgmflag) |
|
641 |
status = read_pgmdata(stdin, in_buffer); // read PGM data (YUV-format) |
if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != (unsigned int)IMAGE_SIZE(XDIM, YDIM)) |
642 |
|
return 1; |
643 |
else |
else |
644 |
status = read_yuvdata(stdin, in_buffer); // read raw data (YUV-format) |
return 0; |
645 |
|
} |
646 |
|
|
647 |
if (status) |
/***************************************************************************** |
648 |
|
* Routines for encoding: init encoder, frame step, release encoder |
649 |
|
****************************************************************************/ |
650 |
|
|
651 |
|
#define FRAMERATE_INCR 1001 |
652 |
|
|
653 |
|
/* Initialize encoder for first use, pass all needed parameters to the codec */ |
654 |
|
static int enc_init(int use_assembler) |
655 |
{ |
{ |
656 |
// Couldn't read image, most likely end-of-file |
int xerr; |
657 |
continue; |
|
658 |
|
XVID_INIT_PARAM xinit; |
659 |
|
XVID_ENC_PARAM xparam; |
660 |
|
|
661 |
|
if(use_assembler) { |
662 |
|
|
663 |
|
#ifdef ARCH_IS_IA64 |
664 |
|
xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
665 |
|
#else |
666 |
|
xinit.cpu_flags = 0; |
667 |
|
#endif |
668 |
|
} |
669 |
|
else { |
670 |
|
xinit.cpu_flags = XVID_CPU_FORCE; |
671 |
} |
} |
672 |
|
|
673 |
|
xvid_init(NULL, 0, &xinit, NULL); |
674 |
|
|
675 |
if (save_ref_flag) |
xparam.width = XDIM; |
676 |
|
xparam.height = YDIM; |
677 |
|
if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS) |
678 |
{ |
{ |
679 |
sprintf(filename, "%s%05d.pgm", filepath, filenr); |
xparam.fincr = 1; |
680 |
write_pgm(filename,in_buffer); |
xparam.fbase = (int)ARG_FRAMERATE; |
681 |
} |
} |
682 |
|
else |
683 |
|
{ |
684 |
|
xparam.fincr = FRAMERATE_INCR; |
685 |
|
xparam.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE); |
686 |
|
} |
687 |
|
xparam.rc_reaction_delay_factor = 16; |
688 |
|
xparam.rc_averaging_period = 100; |
689 |
|
xparam.rc_buffer = 10; |
690 |
|
xparam.rc_bitrate = ARG_BITRATE*1000; |
691 |
|
xparam.min_quantizer = ARG_MINQUANT; |
692 |
|
xparam.max_quantizer = ARG_MAXQUANT; |
693 |
|
xparam.max_key_interval = (int)ARG_FRAMERATE*10; |
694 |
|
xparam.bquant_ratio = ARG_BQRATIO; |
695 |
|
xparam.bquant_offset = ARG_BQOFFSET; |
696 |
|
xparam.max_bframes = ARG_MAXBFRAMES; |
697 |
|
xparam.frame_drop_ratio = 0; |
698 |
|
xparam.global = 0; |
699 |
|
|
700 |
|
/* I use a small value here, since will not encode whole movies, but short clips */ |
701 |
|
|
702 |
/*********************************************************************/ |
xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xparam, NULL); |
703 |
/* analyse this frame before encoding */ |
enc_handle=xparam.handle; |
|
/*********************************************************************/ |
|
|
|
|
|
// nothing is done here at the moment, but you could e.g. create |
|
|
// histograms or measure entropy or apply preprocessing filters... |
|
704 |
|
|
705 |
/*********************************************************************/ |
return xerr; |
706 |
/* encode and decode this frame */ |
} |
|
/*********************************************************************/ |
|
707 |
|
|
708 |
enctime = -msecond(); |
static int enc_stop() |
709 |
status = enc_main(in_buffer, divx_buffer, &m4v_size, &frame_type[filenr]); |
{ |
710 |
enctime += msecond(); |
int xerr; |
711 |
|
|
712 |
totalenctime += enctime; |
xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); |
713 |
totalsize += m4v_size; |
return xerr; |
714 |
|
|
715 |
fprintf(stderr,"Frame %5d: intra %d, enctime =%6.1f ms length=%7d bytes\n", |
} |
|
filenr, frame_type[filenr], enctime*1000, m4v_size); |
|
716 |
|
|
717 |
if (save_m4v_flag) |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
718 |
|
unsigned char* hints_buffer, |
719 |
|
long *streamlength, long *frametype, long *hints_size) |
720 |
{ |
{ |
721 |
fwrite(divx_buffer, m4v_size, 1, stdout); |
int xerr; |
|
} |
|
722 |
|
|
723 |
if (pgmflag) |
XVID_ENC_FRAME xframe; |
724 |
status = read_pgmheader(stdin); |
XVID_ENC_STATS xstats; |
|
// because if this was the last PGM, stop now |
|
725 |
|
|
726 |
filenr++; |
xframe.bitstream = bitstream; |
727 |
|
xframe.length = -1; /* this is written by the routine */ |
728 |
|
|
729 |
} while ( (!status) && (filenr<ARG_MAXFRAMENR) ); |
xframe.image = image; |
730 |
|
xframe.colorspace = XVID_CSP_I420; /* defined in <xvid.h> */ |
731 |
|
|
732 |
|
xframe.intra = -1; /* let the codec decide between I-frame (1) and P-frame (0) */ |
733 |
|
|
734 |
|
xframe.quant = ARG_QUANTI; /* is quant != 0, use a fixed quant (and ignore bitrate) */ |
735 |
|
xframe.bquant = 0; |
736 |
|
|
737 |
/*********************************************************************/ |
xframe.motion = motion_presets[ARG_QUALITY]; |
738 |
/* calculate totals and averages for output, print results */ |
xframe.general = general_presets[ARG_QUALITY]; |
739 |
/*********************************************************************/ |
xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL; |
740 |
|
xframe.stride = XDIM; |
741 |
|
|
742 |
totalsize /= filenr; |
xframe.hint.hintstream = hints_buffer; |
|
totalenctime /= filenr; |
|
743 |
|
|
744 |
for (i=0;i<filenr;i++) |
if(ARG_HINTMODE == HINT_MODE_SET) { |
745 |
{ |
xframe.hint.hintlength = *hints_size; |
746 |
switch (frame_type[i]) |
xframe.hint.rawhints = 0; |
747 |
{ |
xframe.general |= XVID_HINTEDME_SET; |
|
case 0: |
|
|
Pframes++; |
|
|
break; |
|
|
case 1: |
|
|
Iframes++; |
|
|
break; |
|
|
case 2: |
|
|
default: |
|
|
Bframes++; |
|
|
break; |
|
|
} |
|
748 |
} |
} |
749 |
|
|
750 |
fprintf(stderr,"Avg: enctime %5.2f ms, %5.2f fps, filesize =%d\n", |
if(ARG_HINTMODE == HINT_MODE_GET) { |
751 |
1000*totalenctime, 1./totalenctime, totalsize); |
xframe.hint.rawhints = 0; |
752 |
|
xframe.general |= XVID_HINTEDME_GET; |
753 |
/*********************************************************************/ |
} |
|
/* XviD PART Stop */ |
|
|
/*********************************************************************/ |
|
754 |
|
|
755 |
release_all: |
xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats); |
756 |
|
|
757 |
if (enc_handle) |
if(ARG_HINTMODE == HINT_MODE_GET) |
758 |
{ |
*hints_size = xframe.hint.hintlength; |
|
status = enc_stop(); |
|
|
if (status) |
|
|
fprintf(stderr,"Encore RELEASE problem return value %d\n", status); |
|
|
} |
|
759 |
|
|
760 |
free_all_memory: |
/* |
761 |
free(divx_buffer); |
* This is statictical data, e.g. for 2-pass. If you are not |
762 |
free(in_buffer); |
* interested in any of this, you can use NULL instead of &xstats |
763 |
|
*/ |
764 |
|
*frametype = xframe.intra; |
765 |
|
*streamlength = xframe.length; |
766 |
|
|
767 |
return 0; |
return xerr; |
768 |
} |
} |