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.11.2.6 2003-03-15 14:32:56 suxen_drol 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 raw YUV I420 pics or YUV I420 PGM file format is encoded |
30 |
* raw MPEG-4 stream is written to stdout. |
* The speed is measured and frames' PSNR are taken from core. |
|
* 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 |
/***************************************************************************** |
51 |
PMV_EARLYSTOP16, // Q 1 |
* Quality presets |
52 |
PMV_EARLYSTOP16, // Q 2 |
****************************************************************************/ |
53 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, // Q 3 |
|
54 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, // Q 4 |
static xvid_motion_t const motion_presets[] = { |
55 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 // Q 5 |
0, |
56 |
| PMV_HALFPELREFINE8, |
PMV_HALFPELREFINE16, |
57 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 // Q 6 |
PMV_HALFPELREFINE16, |
58 |
| PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 |
PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8, |
59 |
|
PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8 | PMV_EXTSEARCH16 | PMV_USESQUARES16, |
60 |
|
PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8 | PMV_EXTSEARCH16 | PMV_USESQUARES16 | PMV_CHROMA16 | PMV_CHROMA8, |
61 |
}; |
}; |
62 |
|
|
63 |
int general_presets[7] = { |
static xvid_vol_t const vol_presets[] = { |
64 |
XVID_H263QUANT, /* or use XVID_MPEGQUANT */ // Q 0 |
XVID_MPEGQUANT, |
65 |
XVID_MPEGQUANT, // Q 1 |
0, |
66 |
XVID_H263QUANT, // Q 2 |
0, |
67 |
XVID_H263QUANT | XVID_HALFPEL, // Q 3 |
XVID_QUARTERPEL, |
68 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, // Q 4 |
XVID_QUARTERPEL | XVID_GMC, |
69 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, // Q 5 |
XVID_QUARTERPEL | XVID_GMC |
70 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V }; // Q 6 |
}; |
71 |
|
|
72 |
|
static xvid_vop_t const vop_presets[] = { |
73 |
|
XVID_DYNAMIC_BFRAMES, |
74 |
|
XVID_DYNAMIC_BFRAMES, |
75 |
|
XVID_DYNAMIC_BFRAMES | XVID_HALFPEL, |
76 |
|
XVID_DYNAMIC_BFRAMES | XVID_HALFPEL | XVID_INTER4V, |
77 |
|
XVID_DYNAMIC_BFRAMES | XVID_HALFPEL | XVID_INTER4V | XVID_HQACPRED, |
78 |
|
XVID_DYNAMIC_BFRAMES | XVID_HALFPEL | XVID_INTER4V | XVID_HQACPRED | XVID_MODEDECISION_BITS |
79 |
|
}; |
80 |
|
|
81 |
/* my default values for encoding */ |
/***************************************************************************** |
82 |
|
* Command line global variables |
83 |
|
****************************************************************************/ |
84 |
|
|
85 |
|
/* Maximum number of frames to encode */ |
86 |
|
#define ABS_MAXFRAMENR 9999 |
87 |
|
|
88 |
|
static int ARG_STATS = 0; |
89 |
|
static int ARG_DUMP = 0; |
90 |
|
static int ARG_BITRATE = 900; |
91 |
|
static int ARG_QUANTI = 0; |
92 |
|
static int ARG_QUALITY = 5; |
93 |
|
static int ARG_MINQUANT = 1; |
94 |
|
static int ARG_MAXQUANT = 31; |
95 |
|
static float ARG_FRAMERATE = 25.00f; |
96 |
|
static int ARG_MAXFRAMENR = ABS_MAXFRAMENR; |
97 |
|
static char *ARG_INPUTFILE = NULL; |
98 |
|
static int ARG_INPUTTYPE = 0; |
99 |
|
static int ARG_SAVEMPEGSTREAM = 0; |
100 |
|
static char *ARG_OUTPUTFILE = NULL; |
101 |
|
static int XDIM = 0; |
102 |
|
static int YDIM = 0; |
103 |
|
static int ARG_BQRATIO = 150; |
104 |
|
static int ARG_BQOFFSET = 100; |
105 |
|
static int ARG_MAXBFRAMES = 0; |
106 |
|
static int ARG_PACKED = 0; |
107 |
|
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
108 |
|
|
109 |
#define ABS_MAXFRAMENR 9999 // max number of frames |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
110 |
|
#define SMALL_EPS (1e-10) |
111 |
|
|
112 |
int ARG_BITRATE=900; |
#define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ |
113 |
int ARG_QUANTI=0; |
(((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) |
114 |
|
|
115 |
int ARG_QUALITY =6; |
/**************************************************************************** |
116 |
int ARG_MINQUANT=1; |
* Nasty global vars ;-) |
117 |
int ARG_MAXQUANT=31; |
***************************************************************************/ |
118 |
float ARG_FRAMERATE=25.00; |
|
119 |
|
static int i; |
120 |
|
|
121 |
|
/* the path where to save output */ |
122 |
|
static char filepath[256] = "./"; |
123 |
|
|
124 |
|
/* Internal structures (handles) for encoding and decoding */ |
125 |
|
static void *enc_handle = NULL; |
126 |
|
|
127 |
|
/***************************************************************************** |
128 |
|
* Local prototypes |
129 |
|
****************************************************************************/ |
130 |
|
|
131 |
|
/* Prints program usage message */ |
132 |
|
static void usage(); |
133 |
|
|
134 |
|
/* Statistical functions */ |
135 |
|
static double msecond(); |
136 |
|
|
137 |
|
/* PGM related functions */ |
138 |
|
static int read_pgmheader(FILE* handle); |
139 |
|
static int read_pgmdata(FILE* handle, unsigned char *image); |
140 |
|
static int read_yuvdata(FILE* handle, unsigned char *image); |
141 |
|
|
142 |
|
/* Encoder related functions */ |
143 |
|
static int enc_init(int use_assembler); |
144 |
|
static int enc_stop(); |
145 |
|
static int enc_main(unsigned char* image, |
146 |
|
unsigned char* bitstream, |
147 |
|
int *key, |
148 |
|
int *stats_type, |
149 |
|
int *stats_quant, |
150 |
|
int *stats_length, |
151 |
|
int stats[3]); |
152 |
|
|
153 |
|
/***************************************************************************** |
154 |
|
* Main function |
155 |
|
****************************************************************************/ |
156 |
|
|
157 |
int ARG_MAXFRAMENR=ABS_MAXFRAMENR; |
int main(int argc, char *argv[]) |
158 |
|
{ |
159 |
|
|
160 |
#ifdef BFRAMES |
unsigned char *mp4_buffer = NULL; |
161 |
|
unsigned char *in_buffer = NULL; |
162 |
|
unsigned char *out_buffer = NULL; |
163 |
|
|
164 |
int ARG_MAXBFRAMES=1; |
double enctime; |
165 |
int ARG_BQUANTRATIO=200; |
double totalenctime=0.; |
166 |
|
|
167 |
#endif |
int totalsize; |
168 |
|
int result; |
169 |
|
int m4v_size; |
170 |
|
int key; |
171 |
|
int stats_type; |
172 |
|
int stats_quant; |
173 |
|
int stats_length; |
174 |
|
int use_assembler=0; |
175 |
|
|
176 |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
int input_num; |
177 |
#define SMALL_EPS 1e-10 |
int output_num; |
178 |
|
|
179 |
/* these are global variables. Not very elegant, but easy, and this is an easy program */ |
char filename[256]; |
180 |
|
|
181 |
int XDIM=0; |
FILE *in_file = stdin; |
182 |
int YDIM=0; // will be set when reading first image |
FILE *out_file = NULL; |
|
int i,filenr = 0; |
|
183 |
|
|
184 |
int save_m4v_flag = 1; // output MPEG4-bytestream? |
printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
185 |
int save_ref_flag = 0; // save input image |
printf("written by Christoph Lampert 2002-2003\n\n"); |
186 |
|
|
187 |
int pgmflag = 0; // a flag, if input is in PGM format, overwritten in init-phase |
/***************************************************************************** |
188 |
char filepath[256] = "./"; // the path where to save output |
* Command line parsing |
189 |
|
****************************************************************************/ |
190 |
|
|
191 |
void *enc_handle = NULL; // internal structures (handles) for encoding |
for (i=1; i< argc; i++) { |
192 |
|
|
193 |
|
if (strcmp("-asm", argv[i]) == 0 ) { |
194 |
|
use_assembler = 1; |
195 |
|
} |
196 |
|
else if (strcmp("-w", argv[i]) == 0 && i < argc - 1 ) { |
197 |
|
i++; |
198 |
|
XDIM = atoi(argv[i]); |
199 |
|
} |
200 |
|
else if (strcmp("-h", argv[i]) == 0 && i < argc - 1 ) { |
201 |
|
i++; |
202 |
|
YDIM = atoi(argv[i]); |
203 |
|
} |
204 |
|
else if (strcmp("-b", argv[i]) == 0 && i < argc - 1 ) { |
205 |
|
i++; |
206 |
|
ARG_BITRATE = atoi(argv[i]); |
207 |
|
} |
208 |
|
else if (strcmp("-bn", argv[i]) == 0 && i < argc - 1 ) { |
209 |
|
i++; |
210 |
|
ARG_MAXBFRAMES = atoi(argv[i]); |
211 |
|
} |
212 |
|
else if (strcmp("-p", argv[i]) == 0) { |
213 |
|
ARG_PACKED = 1; |
214 |
|
} |
215 |
|
else if (strcmp("-bqr", argv[i]) == 0 && i < argc - 1 ) { |
216 |
|
i++; |
217 |
|
ARG_BQRATIO = atoi(argv[i]); |
218 |
|
} |
219 |
|
else if (strcmp("-bqo", argv[i]) == 0 && i < argc - 1 ) { |
220 |
|
i++; |
221 |
|
ARG_BQOFFSET = atoi(argv[i]); |
222 |
|
} |
223 |
|
else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) { |
224 |
|
i++; |
225 |
|
ARG_QUALITY = atoi(argv[i]); |
226 |
|
} |
227 |
|
else if (strcmp("-f", argv[i]) == 0 && i < argc - 1 ) { |
228 |
|
i++; |
229 |
|
ARG_FRAMERATE = (float)atof(argv[i]); |
230 |
|
} |
231 |
|
else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) { |
232 |
|
i++; |
233 |
|
ARG_INPUTFILE = argv[i]; |
234 |
|
} |
235 |
|
else if (strcmp("-s", argv[i]) == 0) { |
236 |
|
ARG_STATS = 1; |
237 |
|
} |
238 |
|
else if (strcmp("-dump", argv[i]) == 0) { |
239 |
|
ARG_DUMP = 1; |
240 |
|
} |
241 |
|
else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) { |
242 |
|
i++; |
243 |
|
ARG_INPUTTYPE = atoi(argv[i]); |
244 |
|
} |
245 |
|
else if(strcmp("-n", argv[i]) == 0 && i < argc - 1 ) { |
246 |
|
i++; |
247 |
|
ARG_MAXFRAMENR = atoi(argv[i]); |
248 |
|
} |
249 |
|
else if (strcmp("-quant", argv[i]) == 0 && i < argc - 1 ) { |
250 |
|
i++; |
251 |
|
ARG_QUANTI = atoi(argv[i]); |
252 |
|
} |
253 |
|
else if (strcmp("-m", argv[i]) == 0) { |
254 |
|
ARG_SAVEMPEGSTREAM = 1; |
255 |
|
} |
256 |
|
else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { |
257 |
|
i++; |
258 |
|
ARG_OUTPUTFILE = argv[i]; |
259 |
|
} |
260 |
|
else if (strcmp("-help", argv[i])) { |
261 |
|
usage(); |
262 |
|
return(0); |
263 |
|
} |
264 |
|
else { |
265 |
|
usage(); |
266 |
|
exit(-1); |
267 |
|
} |
268 |
|
|
269 |
/*********************************************************************/ |
} |
|
/* "statistical" functions */ |
|
|
/* */ |
|
|
/* these are not needed for encoding or decoding, but for measuring */ |
|
|
/* time and quality, there in nothing specific to XviD in these */ |
|
|
/* */ |
|
|
/*********************************************************************/ |
|
270 |
|
|
271 |
double msecond() |
/***************************************************************************** |
272 |
/* return the current time in seconds(!) */ |
* Arguments checking |
273 |
{ |
****************************************************************************/ |
274 |
struct timeval tv; |
|
275 |
gettimeofday(&tv, 0); |
if (XDIM <= 0 || XDIM >= 2048 || YDIM <=0 || YDIM >= 2048 ) { |
276 |
return tv.tv_sec + tv.tv_usec * 1.0e-6; |
fprintf(stderr, "Trying to retreive width and height from PGM header\n"); |
277 |
|
ARG_INPUTTYPE = 1; /* pgm */ |
278 |
} |
} |
279 |
|
|
280 |
/*********************************************************************/ |
if ( ARG_QUALITY < 0 || ARG_QUALITY > 5) { |
281 |
/* input and output functions */ |
fprintf(stderr,"Wrong Quality\n"); |
282 |
/* */ |
return(-1); |
283 |
/* the are small and simple routines to read and write PGM and YUV */ |
} |
|
/* image. It's just for convenience, again nothing specific to XviD */ |
|
|
/* */ |
|
|
/*********************************************************************/ |
|
284 |
|
|
285 |
int read_pgmheader(FILE* handle) |
if ( ARG_BITRATE <= 0 && ARG_QUANTI == 0) { |
286 |
{ |
fprintf(stderr,"Wrong Bitrate\n"); |
287 |
int bytes,xsize,ysize,depth; |
return(-1); |
288 |
char dummy[2]; |
} |
289 |
|
|
290 |
bytes = fread(dummy,1,2,handle); |
if ( ARG_FRAMERATE <= 0) { |
291 |
|
fprintf(stderr,"Wrong Framerate %s \n",argv[5]); |
292 |
|
return(-1); |
293 |
|
} |
294 |
|
|
295 |
if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) |
if ( ARG_MAXFRAMENR <= 0) { |
296 |
return 1; |
fprintf(stderr,"Wrong number of frames\n"); |
297 |
fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
return(-1); |
|
if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) |
|
|
{ |
|
|
return 2; |
|
298 |
} |
} |
299 |
if ( (XDIM==0) || (YDIM==0) ) |
|
300 |
{ XDIM=xsize; |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
301 |
YDIM=ysize; |
in_file = stdin; |
302 |
} |
} |
303 |
|
else { |
304 |
|
|
305 |
return 0; |
in_file = fopen(ARG_INPUTFILE, "rb"); |
306 |
|
if (in_file == NULL) { |
307 |
|
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
308 |
|
return(-1); |
309 |
|
} |
310 |
} |
} |
311 |
|
|
312 |
int read_pgmdata(FILE* handle, unsigned char *image) |
if (ARG_INPUTTYPE) { |
313 |
{ |
if (read_pgmheader(in_file)) { |
314 |
int i,status; |
fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); |
315 |
char dummy; |
return(-1); |
316 |
|
} |
317 |
|
} |
318 |
|
|
319 |
unsigned char* buff1_ptr2 = image + XDIM*YDIM; |
/* now we know the sizes, so allocate memory */ |
320 |
unsigned char* buff1_ptr3 = image + XDIM*YDIM + XDIM/2*YDIM/2; |
in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)); |
321 |
|
if (!in_buffer) |
322 |
|
goto free_all_memory; |
323 |
|
|
324 |
fread(image,XDIM*YDIM,1,stdin); // read Y component of picture |
/* this should really be enough memory ! */ |
325 |
|
mp4_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)*2); |
326 |
|
if (!mp4_buffer) |
327 |
|
goto free_all_memory; |
328 |
|
|
329 |
for (i=0;i<YDIM/2;i++) |
/***************************************************************************** |
330 |
|
* XviD PART Start |
331 |
|
****************************************************************************/ |
332 |
|
|
333 |
|
|
334 |
|
result = enc_init(use_assembler); |
335 |
|
if (result) |
336 |
{ |
{ |
337 |
fread(buff1_ptr2,XDIM/2,1,stdin); // read U |
fprintf(stderr, "Encore INIT problem, return value %d\n", result); |
338 |
buff1_ptr2 += XDIM/2; |
goto release_all; |
|
fread(buff1_ptr3,XDIM/2,1,stdin); // read V |
|
|
buff1_ptr3 += XDIM/2; |
|
|
} |
|
|
fread(&dummy,1,1,handle); // I don't know why, but this seems needed |
|
|
return 0; |
|
339 |
} |
} |
340 |
|
|
341 |
int read_yuvdata(FILE* handle, unsigned char *image) |
/***************************************************************************** |
342 |
{ int i; |
* Main loop |
343 |
char dummy; |
****************************************************************************/ |
344 |
|
|
345 |
unsigned char* buff1_ptr2 = image + XDIM*YDIM; |
if (ARG_SAVEMPEGSTREAM && ARG_OUTPUTFILE) { |
|
unsigned char* buff1_ptr3 = image + XDIM*YDIM + XDIM/2*YDIM/2; |
|
346 |
|
|
347 |
if (fread(image,XDIM,YDIM*3/2,stdin) != YDIM*3/2) |
if((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) { |
348 |
return 1; |
fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE); |
349 |
else |
goto release_all; |
|
return 0; |
|
350 |
} |
} |
351 |
|
|
|
int write_pgm(char *filename, unsigned char *image) |
|
|
{ |
|
|
FILE *filehandle; |
|
|
filehandle=fopen(filename,"wb"); |
|
|
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; |
|
352 |
} |
} |
353 |
else |
else { |
354 |
return 1; |
out_file = NULL; |
355 |
} |
} |
356 |
|
|
357 |
/*********************************************************************/ |
/***************************************************************************** |
358 |
/* Routines for encoding: init encoder, frame step, release encoder */ |
* Encoding loop |
359 |
/*********************************************************************/ |
****************************************************************************/ |
360 |
|
|
361 |
#define FRAMERATE_INCR 1001 |
totalsize = 0; |
362 |
|
|
363 |
|
result = 0; |
364 |
|
|
365 |
int enc_init(int use_assembler) |
input_num = 0; /* input frame counter */ |
366 |
{ /* initialize encoder for first use, pass all needed parameters to the codec */ |
output_num = 0; /* output frame counter */ |
|
int xerr; |
|
367 |
|
|
368 |
XVID_INIT_PARAM xinit; |
do { |
|
XVID_ENC_PARAM xparam; |
|
369 |
|
|
370 |
if(use_assembler) |
char *type; |
371 |
|
int stats[3]; |
372 |
|
|
373 |
#ifdef ARCH_IA64 |
if (input_num >= ARG_MAXFRAMENR) |
374 |
xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
{ |
375 |
#else |
result = 1; |
376 |
xinit.cpu_flags = 0; |
} |
|
#endif |
|
377 |
|
|
378 |
else |
if (!result) |
379 |
xinit.cpu_flags = XVID_CPU_FORCE; |
{ |
380 |
|
if(ARG_INPUTTYPE) { |
381 |
|
/* read PGM data (YUV-format) */ |
382 |
|
result = read_pgmdata(in_file, in_buffer); |
383 |
|
} else { |
384 |
|
/* read raw data (YUV-format) */ |
385 |
|
result = read_yuvdata(in_file, in_buffer); |
386 |
|
} |
387 |
|
} |
388 |
|
|
389 |
xvid_init(NULL, 0, &xinit, NULL); |
/***************************************************************************** |
390 |
|
* Encode and decode this frame |
391 |
|
****************************************************************************/ |
392 |
|
|
393 |
xparam.width = XDIM; |
enctime = msecond(); |
394 |
xparam.height = YDIM; |
m4v_size = enc_main(!result?in_buffer:0, mp4_buffer, &key, &stats_type, &stats_quant, &stats_length, stats); |
395 |
if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS) |
enctime = msecond() - enctime; |
396 |
{ |
|
397 |
xparam.fincr = 1; |
/* Write the Frame statistics */ |
398 |
xparam.fbase = (int)ARG_FRAMERATE; |
|
399 |
|
printf("%5d: key=%i, time(ms)=%6.1f, length=%7d", |
400 |
|
!result?input_num:-1, |
401 |
|
key, |
402 |
|
(float)enctime, |
403 |
|
(int)m4v_size); |
404 |
|
|
405 |
|
if (stats_type > 0) { /* !XVID_TYPE_NOTHING */ |
406 |
|
|
407 |
|
switch(stats_type) { |
408 |
|
case XVID_TYPE_IVOP: |
409 |
|
type = "I"; |
410 |
|
break; |
411 |
|
case XVID_TYPE_PVOP: |
412 |
|
type = "P"; |
413 |
|
break; |
414 |
|
case XVID_TYPE_BVOP: |
415 |
|
type = "B"; |
416 |
|
break; |
417 |
|
case XVID_TYPE_SVOP: |
418 |
|
type = "S"; |
419 |
|
break; |
420 |
|
default: |
421 |
|
type = "U"; |
422 |
|
break; |
423 |
} |
} |
424 |
else |
|
425 |
{ |
printf(" | type=%s quant=%2d, length=%7d", type, stats_quant, stats_length); |
|
xparam.fincr = FRAMERATE_INCR; |
|
|
xparam.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE); |
|
426 |
} |
} |
|
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; |
|
|
|
|
|
#ifdef BFRAMES |
|
|
xparam.global = XVID_GLOBAL_DX50BVOP; |
|
|
xparam.max_bframes = ARG_MAXBFRAMES; |
|
|
xparam.bquant_ratio = ARG_BQUANTRATIO; |
|
|
xparam.frame_drop_ratio=0; |
|
|
#endif |
|
427 |
|
|
428 |
/* I use a small value here, since will not encode whole movies, |
printf("\n"); |
|
but short clips */ |
|
429 |
|
|
430 |
xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xparam, NULL); |
if (m4v_size < 0) { |
431 |
enc_handle=xparam.handle; |
break; |
432 |
|
} |
433 |
|
|
434 |
return xerr; |
/* Update encoding time stats */ |
435 |
|
totalenctime += enctime; |
436 |
|
totalsize += m4v_size; |
437 |
|
|
438 |
|
/***************************************************************************** |
439 |
|
* Save stream to file |
440 |
|
****************************************************************************/ |
441 |
|
|
442 |
|
if (m4v_size>0 && ARG_SAVEMPEGSTREAM) |
443 |
|
{ |
444 |
|
/* Save single files */ |
445 |
|
if (out_file == NULL) { |
446 |
|
sprintf(filename, "%sframe%05d.m4v", filepath, output_num); |
447 |
|
out_file = fopen(filename, "wb"); |
448 |
|
fwrite(mp4_buffer, m4v_size, 1, out_file); |
449 |
|
fclose(out_file); |
450 |
|
out_file = NULL; |
451 |
|
output_num++; |
452 |
} |
} |
453 |
|
else { |
454 |
|
|
455 |
int enc_stop() |
/* Write mp4 data */ |
456 |
{ int xerr; |
fwrite(mp4_buffer, 1, m4v_size, out_file); |
457 |
|
|
458 |
xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); |
} |
|
return xerr; |
|
459 |
} |
} |
460 |
|
|
461 |
int enc_main(unsigned char* image, unsigned char* bitstream, int *streamlength, int* frametype) |
input_num++; |
|
{ int xerr; |
|
462 |
|
|
463 |
XVID_ENC_FRAME xframe; |
/* Read the header if it's pgm stream */ |
464 |
XVID_ENC_STATS xstats; |
if (!result && ARG_INPUTTYPE) |
465 |
|
result = read_pgmheader(in_file); |
466 |
|
|
467 |
xframe.bitstream = bitstream; |
} while (1); |
|
xframe.length = -1; // this is written by the routine |
|
468 |
|
|
|
xframe.image = image; |
|
|
xframe.colorspace = XVID_CSP_YV12; // defined in <xvid.h> |
|
469 |
|
|
|
xframe.intra = -1; // let the codec decide between I-frame (1) and P-frame (0) |
|
470 |
|
|
471 |
xframe.quant = ARG_QUANTI; // is quant != 0, use a fixed quant (and ignore bitrate) |
/***************************************************************************** |
472 |
|
* Calculate totals and averages for output, print results |
473 |
|
****************************************************************************/ |
474 |
|
|
475 |
xframe.motion = motion_presets[ARG_QUALITY]; |
if (input_num > 0) { |
476 |
xframe.general = general_presets[ARG_QUALITY]; |
totalsize /= input_num; |
477 |
xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL; |
totalenctime /= input_num; |
478 |
|
}else{ |
479 |
|
totalsize = -1; |
480 |
|
totalenctime = -1; |
481 |
|
} |
482 |
|
|
483 |
#ifdef BFRAMES |
printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d\n", |
484 |
xframe.bquant = 0; |
totalenctime, 1000/totalenctime, (int)totalsize); |
|
#endif |
|
485 |
|
|
|
xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats); |
|
486 |
|
|
487 |
/* enc_result->is_key_frame = xframe.intra; |
/***************************************************************************** |
488 |
enc_result->quantizer = xframe.quant; |
* XviD PART Stop |
489 |
enc_result->total_bits = xframe.length * 8; |
****************************************************************************/ |
|
enc_result->motion_bits = xstats.hlength * 8; |
|
|
enc_result->texture_bits = enc_result->total_bits - enc_result->motion_bits; |
|
|
*/ |
|
|
|
|
|
/* This is statictical data, e.g. for 2-pass. |
|
|
If you are not interested in any of this, you can use |
|
|
NULL instead of &xstats |
|
|
*/ |
|
|
*frametype = xframe.intra; |
|
|
*streamlength = xframe.length; |
|
|
|
|
|
return xerr; |
|
|
} |
|
|
|
|
|
/*********************************************************************/ |
|
|
/* Main program */ |
|
|
/*********************************************************************/ |
|
490 |
|
|
491 |
int main(int argc, char *argv[]) |
release_all: |
492 |
|
|
493 |
|
if (enc_handle) |
494 |
{ |
{ |
495 |
unsigned char *divx_buffer = NULL; |
result = enc_stop(); |
496 |
unsigned char *in_buffer = NULL; |
if (result) |
497 |
|
fprintf(stderr, "Encore RELEASE problem return value %d\n", result); |
498 |
|
} |
499 |
|
|
500 |
double enctime; |
if(in_file) |
501 |
double totalenctime=0.; |
fclose(in_file); |
502 |
|
if(out_file) |
503 |
|
fclose(out_file); |
504 |
|
|
505 |
long totalsize=0; |
free_all_memory: |
506 |
int status; |
free(out_buffer); |
507 |
|
free(mp4_buffer); |
508 |
|
free(in_buffer); |
509 |
|
|
510 |
int m4v_size; |
return(0); |
|
int frame_type[ABS_MAXFRAMENR]; |
|
|
int Iframes=0, Pframes=0, Bframes=0; |
|
|
int use_assembler=1; |
|
511 |
|
|
512 |
char filename[256]; |
} |
513 |
|
|
|
FILE *filehandle; |
|
514 |
|
|
515 |
/* read YUV in pgm format from stdin */ |
/***************************************************************************** |
516 |
if (!pgmflag) |
* "statistical" functions |
517 |
|
* |
518 |
|
* these are not needed for encoding or decoding, but for measuring |
519 |
|
* time and quality, there in nothing specific to XviD in these |
520 |
|
* |
521 |
|
*****************************************************************************/ |
522 |
|
|
523 |
|
/* Return time elapsed time in miliseconds since the program started */ |
524 |
|
static double msecond() |
525 |
{ |
{ |
526 |
pgmflag = 1; |
#ifndef WIN32 |
527 |
|
struct timeval tv; |
528 |
|
gettimeofday(&tv, 0); |
529 |
|
return(tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3); |
530 |
|
#else |
531 |
|
clock_t clk; |
532 |
|
clk = clock(); |
533 |
|
return(clk * 1000 / CLOCKS_PER_SEC); |
534 |
|
#endif |
535 |
|
} |
536 |
|
|
537 |
// if (argc==2 && !strcmp(argv[1],"-noasm")) |
/***************************************************************************** |
538 |
// use_assembler = 0; |
* Usage message |
539 |
|
*****************************************************************************/ |
540 |
|
|
541 |
|
static void usage() |
542 |
|
{ |
543 |
|
|
544 |
|
fprintf(stderr, "Usage : xvid_stat [OPTIONS]\n"); |
545 |
|
fprintf(stderr, "Options :\n"); |
546 |
|
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
547 |
|
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
548 |
|
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
549 |
|
fprintf(stderr, " -bn integer : max bframes (default=0)\n"); |
550 |
|
fprintf(stderr, " -p : packed mode\n"); |
551 |
|
fprintf(stderr, " -bqr integer : bframe quantizer ratio (default=150)\n"); |
552 |
|
fprintf(stderr, " -bqo integer : bframe quantizer offset (default=100)\n"); |
553 |
|
fprintf(stderr, " -f float : target framerate (>0)\n"); |
554 |
|
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
555 |
|
fprintf(stderr, " -s : print stats about encoded frames\n"); |
556 |
|
fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n"); |
557 |
|
fprintf(stderr, " -n integer : number of frames to encode\n"); |
558 |
|
fprintf(stderr, " -q integer : quality ([0..5])\n"); |
559 |
|
fprintf(stderr, " -d boolean : save decoder output (0 False*, !=0 True)\n"); |
560 |
|
fprintf(stderr, " -m : save mpeg4 raw stream\n"); |
561 |
|
fprintf(stderr, " -o string : output container filename (only usefull when -m 1 is used) :\n"); |
562 |
|
fprintf(stderr, " When this option is not used : one file per encoded frame\n"); |
563 |
|
fprintf(stderr, " When this option is used : save to 'string' file\n"); |
564 |
|
fprintf(stderr, " -help : prints this help message\n"); |
565 |
|
fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); |
566 |
|
fprintf(stderr, " (* means default)\n"); |
567 |
|
|
|
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); |
|
568 |
} |
} |
569 |
else |
|
570 |
|
/***************************************************************************** |
571 |
|
* Input and output functions |
572 |
|
* |
573 |
|
* the are small and simple routines to read and write PGM and YUV |
574 |
|
* image. It's just for convenience, again nothing specific to XviD |
575 |
|
* |
576 |
|
*****************************************************************************/ |
577 |
|
|
578 |
|
static int read_pgmheader(FILE* handle) |
579 |
{ |
{ |
580 |
YDIM = YDIM*3/2; /* for YUV */ |
int bytes,xsize,ysize,depth; |
581 |
pgmflag = 0; |
char dummy[2]; |
582 |
|
|
583 |
|
bytes = fread(dummy,1,2,handle); |
584 |
|
|
585 |
|
if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) |
586 |
|
return(1); |
587 |
|
|
588 |
|
fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
589 |
|
if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) |
590 |
|
{ |
591 |
|
fprintf(stderr,"%d %d %d\n",xsize,ysize,depth); |
592 |
|
return(2); |
593 |
} |
} |
594 |
|
if ( (XDIM==0) || (YDIM==0) ) |
595 |
|
{ |
596 |
|
XDIM=xsize; |
597 |
|
YDIM=ysize*2/3; |
598 |
} |
} |
599 |
|
|
600 |
|
return(0); |
601 |
} |
} |
602 |
|
|
603 |
if (pgmflag) |
static int read_pgmdata(FILE* handle, unsigned char *image) |
|
{ if (read_pgmheader(stdin)) |
|
604 |
{ |
{ |
605 |
fprintf(stderr,"Wrong input format, I want YUV encapsulated in PGM\n"); |
int i; |
606 |
return 1; |
char dummy; |
607 |
|
|
608 |
|
unsigned char *y = image; |
609 |
|
unsigned char *u = image + XDIM*YDIM; |
610 |
|
unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2; |
611 |
|
|
612 |
|
/* read Y component of picture */ |
613 |
|
fread(y, 1, XDIM*YDIM, handle); |
614 |
|
|
615 |
|
for (i=0;i<YDIM/2;i++) |
616 |
|
{ |
617 |
|
/* read U */ |
618 |
|
fread(u, 1, XDIM/2, handle); |
619 |
|
|
620 |
|
/* read V */ |
621 |
|
fread(v, 1, XDIM/2, handle); |
622 |
|
|
623 |
|
/* Update pointers */ |
624 |
|
u += XDIM/2; |
625 |
|
v += XDIM/2; |
626 |
} |
} |
627 |
|
|
628 |
|
/* I don't know why, but this seems needed */ |
629 |
|
fread(&dummy, 1, 1, handle); |
630 |
|
|
631 |
|
return(0); |
632 |
} |
} |
633 |
if (argc>=4) |
|
634 |
{ ARG_QUALITY = atoi(argv[3]); |
static int read_yuvdata(FILE* handle, unsigned char *image) |
635 |
if ( (ARG_QUALITY < 0) || (ARG_QUALITY > 6) ) |
{ |
636 |
{ fprintf(stderr,"Wrong Quality\n"); return -1; } |
|
637 |
|
if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != (unsigned int)IMAGE_SIZE(XDIM, YDIM)) |
638 |
|
return(1); |
639 |
else |
else |
640 |
fprintf(stderr,"Quality %d\n",ARG_QUALITY); |
return(0); |
641 |
} |
} |
642 |
if (argc>=5) |
|
643 |
{ ARG_BITRATE = atoi(argv[4]); |
/***************************************************************************** |
644 |
if ( (ARG_BITRATE <= 0) ) |
* Routines for encoding: init encoder, frame step, release encoder |
645 |
{ fprintf(stderr,"Wrong Bitrate\n"); return -1; } |
****************************************************************************/ |
646 |
if ( (ARG_BITRATE < 32) ) |
|
647 |
{ ARG_QUANTI = ARG_BITRATE; |
/* sample plugin */ |
648 |
ARG_BITRATE=0; |
|
649 |
fprintf(stderr,"Quantizer %d\n",ARG_QUANTI); |
int rawenc_debug(void * handle, int opt, void * param1, void * param2) |
650 |
|
{ |
651 |
|
switch(opt) |
652 |
|
{ |
653 |
|
case XVID_PLG_INFO : |
654 |
|
case XVID_PLG_CREATE : |
655 |
|
case XVID_PLG_DESTROY : |
656 |
|
case XVID_PLG_BEFORE : |
657 |
|
return 0; |
658 |
|
|
659 |
|
case XVID_PLG_AFTER : |
660 |
|
{ |
661 |
|
xvid_plg_data_t * data = (xvid_plg_data_t*)param1; |
662 |
|
printf("type=%i, quant=%i, length=%i\n", data->type, data->quant, data->length); |
663 |
|
return 0; |
664 |
} |
} |
|
else |
|
|
fprintf(stderr,"Bitrate %d kbps\n",ARG_BITRATE); |
|
665 |
} |
} |
666 |
if (argc>=6) |
|
667 |
{ ARG_FRAMERATE = (float)atof(argv[5]); |
return XVID_ERR_FAIL; |
|
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); |
|
668 |
} |
} |
|
#endif |
|
669 |
|
|
|
/* now we know the sizes, so allocate memory */ |
|
670 |
|
|
671 |
in_buffer = (unsigned char *) malloc(XDIM*YDIM); |
#define FRAMERATE_INCR 1001 |
|
if (!in_buffer) |
|
|
goto free_all_memory; |
|
672 |
|
|
673 |
divx_buffer = (unsigned char *) malloc(XDIM*YDIM*2); |
/* Initialize encoder for first use, pass all needed parameters to the codec */ |
674 |
if (!divx_buffer) |
static int enc_init(int use_assembler) |
675 |
goto free_all_memory; |
{ |
676 |
|
int xerr; |
677 |
|
|
678 |
YDIM = YDIM*2/3; // PGM is YUV 4:2:0 format, so real image height is *2/3 of PGM picture |
xvid_enc_plugin_t plugins[2]; |
679 |
|
|
680 |
/*********************************************************************/ |
xvid_gbl_init_t xvid_gbl_init; |
681 |
/* XviD PART Start */ |
xvid_enc_create_t xvid_enc_create; |
|
/*********************************************************************/ |
|
682 |
|
|
683 |
status = enc_init(use_assembler); |
/*------------------------------------------------------------------------ |
684 |
if (status) |
* XviD core initialization |
685 |
{ |
*----------------------------------------------------------------------*/ |
686 |
fprintf(stderr,"Encore INIT problem, return value %d\n", status); |
|
687 |
goto release_all; |
/* Set version -- version checking will done by xvidcore*/ |
688 |
|
memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init)); |
689 |
|
xvid_gbl_init.version = XVID_VERSION; |
690 |
|
|
691 |
|
|
692 |
|
/* Do we have to enable ASM optimizations ? */ |
693 |
|
if(use_assembler) { |
694 |
|
|
695 |
|
#ifdef ARCH_IS_IA64 |
696 |
|
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
697 |
|
#else |
698 |
|
xvid_gbl_init.cpu_flags = 0; |
699 |
|
#endif |
700 |
|
} |
701 |
|
else { |
702 |
|
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; |
703 |
} |
} |
704 |
|
|
705 |
/*********************************************************************/ |
/* Initialize XviD core -- Should be done once per __process__ */ |
706 |
/* Main loop */ |
xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL); |
|
/*********************************************************************/ |
|
707 |
|
|
708 |
do |
/*------------------------------------------------------------------------ |
709 |
{ |
* XviD encoder initialization |
710 |
if (pgmflag) |
*----------------------------------------------------------------------*/ |
|
status = read_pgmdata(stdin, in_buffer); // read PGM data (YUV-format) |
|
|
else |
|
|
status = read_yuvdata(stdin, in_buffer); // read raw data (YUV-format) |
|
711 |
|
|
712 |
if (status) |
/* Version again */ |
713 |
{ |
memset(&xvid_enc_create, 0, sizeof(xvid_enc_create)); |
714 |
// Couldn't read image, most likely end-of-file |
xvid_enc_create.version = XVID_VERSION; |
715 |
continue; |
|
716 |
|
/* Width and Height of input frames */ |
717 |
|
xvid_enc_create.width = XDIM; |
718 |
|
xvid_enc_create.height = YDIM; |
719 |
|
|
720 |
|
/* init plugins */ |
721 |
|
|
722 |
|
xvid_enc_create.plugins = plugins; |
723 |
|
xvid_enc_create.num_plugins = 0; |
724 |
|
if (ARG_STATS) { |
725 |
|
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_psnr; |
726 |
|
plugins[xvid_enc_create.num_plugins].param = NULL; |
727 |
|
xvid_enc_create.num_plugins++; |
728 |
|
} |
729 |
|
if (ARG_DUMP) { |
730 |
|
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_dump; |
731 |
|
plugins[xvid_enc_create.num_plugins].param = NULL; |
732 |
|
xvid_enc_create.num_plugins++; |
733 |
} |
} |
734 |
|
|
735 |
|
/* No fancy thread tests */ |
736 |
|
xvid_enc_create.num_threads = 0; |
737 |
|
|
738 |
if (save_ref_flag) |
/* Frame rate - Do some quick float fps = fincr/fbase hack */ |
739 |
{ |
if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS) { |
740 |
sprintf(filename, "%s%05d.pgm", filepath, filenr); |
xvid_enc_create.fincr = 1; |
741 |
write_pgm(filename,in_buffer); |
xvid_enc_create.fbase = (int)ARG_FRAMERATE; |
742 |
|
} else { |
743 |
|
xvid_enc_create.fincr = FRAMERATE_INCR; |
744 |
|
xvid_enc_create.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE); |
745 |
} |
} |
746 |
|
|
747 |
|
/* Maximum key frame interval */ |
748 |
|
xvid_enc_create.max_key_interval = (int)ARG_FRAMERATE*10; |
749 |
|
|
750 |
/*********************************************************************/ |
/* Bframes settings */ |
751 |
/* analyse this frame before encoding */ |
xvid_enc_create.max_bframes = ARG_MAXBFRAMES; |
752 |
/*********************************************************************/ |
xvid_enc_create.bquant_ratio = ARG_BQRATIO; |
753 |
|
xvid_enc_create.bquant_offset = ARG_BQOFFSET; |
754 |
|
|
755 |
// nothing is done here at the moment, but you could e.g. create |
/* Dropping ratio frame -- we don't need that */ |
756 |
// histograms or measure entropy or apply preprocessing filters... |
xvid_enc_create.frame_drop_ratio = 0; |
757 |
|
|
758 |
/*********************************************************************/ |
/* Global encoder options */ |
759 |
/* encode and decode this frame */ |
xvid_enc_create.global = 0; |
760 |
/*********************************************************************/ |
if (ARG_PACKED) xvid_enc_create.global |= XVID_PACKED; |
761 |
|
|
762 |
enctime = -msecond(); |
/* I use a small value here, since will not encode whole movies, but short clips */ |
763 |
status = enc_main(in_buffer, divx_buffer, &m4v_size, &frame_type[filenr]); |
xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); |
|
enctime += msecond(); |
|
764 |
|
|
765 |
totalenctime += enctime; |
/* Retrieve the encoder instance from the structure */ |
766 |
totalsize += m4v_size; |
enc_handle = xvid_enc_create.handle; |
767 |
|
|
768 |
fprintf(stderr,"Frame %5d: intra %d, enctime =%6.1f ms length=%7d bytes\n", |
return(xerr); |
769 |
filenr, frame_type[filenr], enctime*1000, m4v_size); |
} |
770 |
|
|
771 |
if (save_m4v_flag) |
static int enc_stop() |
772 |
{ |
{ |
773 |
fwrite(divx_buffer, m4v_size, 1, stdout); |
int xerr; |
|
} |
|
774 |
|
|
775 |
if (pgmflag) |
/* Destroy the encoder instance */ |
776 |
status = read_pgmheader(stdin); |
xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); |
|
// because if this was the last PGM, stop now |
|
777 |
|
|
778 |
filenr++; |
return(xerr); |
779 |
|
} |
780 |
|
|
781 |
} while ( (!status) && (filenr<ARG_MAXFRAMENR) ); |
static int enc_main(unsigned char* image, |
782 |
|
unsigned char* bitstream, |
783 |
|
int * key, |
784 |
|
int *stats_type, |
785 |
|
int *stats_quant, |
786 |
|
int *stats_length, |
787 |
|
int stats[3]) |
788 |
|
{ |
789 |
|
int ret; |
790 |
|
|
791 |
|
xvid_enc_frame_t xvid_enc_frame; |
792 |
|
xvid_enc_stats_t xvid_enc_stats; |
793 |
|
|
794 |
|
/* Version for the frame and the stats */ |
795 |
|
memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame)); |
796 |
|
xvid_enc_frame.version = XVID_VERSION; |
797 |
|
|
798 |
/*********************************************************************/ |
memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats)); |
799 |
/* calculate totals and averages for output, print results */ |
xvid_enc_stats.version = XVID_VERSION; |
|
/*********************************************************************/ |
|
800 |
|
|
801 |
totalsize /= filenr; |
/* Bind output buffer */ |
802 |
totalenctime /= filenr; |
xvid_enc_frame.bitstream = bitstream; |
803 |
|
xvid_enc_frame.length = -1; |
804 |
|
|
805 |
for (i=0;i<filenr;i++) |
/* Initialize input image fields */ |
806 |
{ |
if (image) { |
807 |
switch (frame_type[i]) |
xvid_enc_frame.input.plane[0] = image; |
808 |
{ |
xvid_enc_frame.input.csp = XVID_CSP_I420; |
809 |
case 0: |
xvid_enc_frame.input.stride[0] = XDIM; |
810 |
Pframes++; |
}else{ |
811 |
break; |
xvid_enc_frame.input.csp = XVID_CSP_NULL; |
|
case 1: |
|
|
Iframes++; |
|
|
break; |
|
|
case 2: |
|
|
default: |
|
|
Bframes++; |
|
|
break; |
|
|
} |
|
812 |
} |
} |
813 |
|
|
814 |
fprintf(stderr,"Avg: enctime %5.2f ms, %5.2f fps, filesize =%d\n", |
/* Set up core's general features */ |
815 |
1000*totalenctime, 1./totalenctime, totalsize); |
xvid_enc_frame.vol_flags = vol_presets[ARG_QUALITY]; |
816 |
|
|
817 |
/*********************************************************************/ |
/* Set up core's general features */ |
818 |
/* XviD PART Stop */ |
xvid_enc_frame.vop_flags = vop_presets[ARG_QUALITY]; |
|
/*********************************************************************/ |
|
819 |
|
|
820 |
release_all: |
/* Frame type -- let core decide for us */ |
821 |
|
xvid_enc_frame.type = XVID_TYPE_AUTO; |
822 |
|
|
823 |
if (enc_handle) |
/* Force the right quantizer */ |
824 |
{ |
xvid_enc_frame.quant = ARG_QUANTI; |
825 |
status = enc_stop(); |
xvid_enc_frame.bquant = 0; |
|
if (status) |
|
|
fprintf(stderr,"Encore RELEASE problem return value %d\n", status); |
|
|
} |
|
826 |
|
|
827 |
free_all_memory: |
/* Set up motion estimation flags */ |
828 |
free(divx_buffer); |
xvid_enc_frame.motion = motion_presets[ARG_QUALITY]; |
|
free(in_buffer); |
|
829 |
|
|
830 |
return 0; |
/* We don't use special matrices */ |
831 |
|
xvid_enc_frame.quant_intra_matrix = NULL; |
832 |
|
xvid_enc_frame.quant_inter_matrix = NULL; |
833 |
|
|
834 |
|
/* Encode the frame */ |
835 |
|
ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame, &xvid_enc_stats); |
836 |
|
|
837 |
|
*key = (xvid_enc_frame.out_flags & XVID_KEYFRAME); |
838 |
|
*stats_type = xvid_enc_stats.type; |
839 |
|
*stats_quant = xvid_enc_stats.quant; |
840 |
|
*stats_length = xvid_enc_stats.length; |
841 |
|
stats[0] = xvid_enc_stats.sse_y; |
842 |
|
stats[1] = xvid_enc_stats.sse_u; |
843 |
|
stats[2] = xvid_enc_stats.sse_v; |
844 |
|
|
845 |
|
return(ret); |
846 |
} |
} |