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