3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Console based test application - |
* - Console based test application - |
5 |
* |
* |
6 |
* Copyright(C) 2002 Christoph Lampert |
* Copyright(C) 2002-2003 Christoph Lampert |
7 |
* |
* |
8 |
* This program is free software; you can redistribute it and/or modify |
* This program is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
19 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
* |
* |
22 |
* $Id: xvid_encraw.c,v 1.2 2002-09-28 14:26:53 edgomez Exp $ |
* $Id: xvid_encraw.c,v 1.11.2.7 2003-03-15 16:41:32 suxen_drol Exp $ |
23 |
* |
* |
24 |
****************************************************************************/ |
****************************************************************************/ |
25 |
|
|
26 |
/***************************************************************************** |
/***************************************************************************** |
27 |
* Application notes : |
* Application notes : |
28 |
* |
* |
29 |
* A sequence of YUV pics in PGM file format is encoded and decoded |
* A sequence of raw YUV I420 pics or YUV I420 PGM file format is encoded |
30 |
* The speed is measured and PSNR of decoded picture is calculated. |
* The speed is measured and frames' PSNR are taken from core. |
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. |
* and maths-lib. |
36 |
|
|
37 |
#include <stdio.h> |
#include <stdio.h> |
38 |
#include <stdlib.h> |
#include <stdlib.h> |
39 |
|
#include <string.h> |
40 |
#include <math.h> |
#include <math.h> |
41 |
#ifndef _MSC_VER |
#ifndef WIN32 |
42 |
#include <sys/time.h> |
#include <sys/time.h> |
43 |
#else |
#else |
44 |
#include <time.h> |
#include <time.h> |
46 |
|
|
47 |
#include "xvid.h" |
#include "xvid.h" |
48 |
|
|
49 |
|
|
50 |
/***************************************************************************** |
/***************************************************************************** |
51 |
* Quality presets |
* Quality presets |
52 |
****************************************************************************/ |
****************************************************************************/ |
53 |
|
|
54 |
static int const motion_presets[7] = { |
static xvid_motion_t const motion_presets[] = { |
55 |
0, // Q 0 |
0, |
56 |
PMV_EARLYSTOP16, // Q 1 |
PMV_HALFPELREFINE16, |
57 |
PMV_EARLYSTOP16, // Q 2 |
PMV_HALFPELREFINE16, |
58 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, // Q 3 |
PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8, |
59 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, // Q 4 |
PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8 | PMV_EXTSEARCH16 | PMV_USESQUARES16, |
60 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | // Q 5 |
PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8 | PMV_EXTSEARCH16 | PMV_USESQUARES16 | PMV_CHROMA16 | PMV_CHROMA8, |
|
PMV_HALFPELREFINE8, |
|
|
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | // Q 6 |
|
|
PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 |
|
61 |
}; |
}; |
62 |
|
|
63 |
static int const general_presets[7] = { |
static xvid_vol_t const vol_presets[] = { |
64 |
XVID_H263QUANT, // 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 |
|
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V // Q 6 |
|
70 |
}; |
}; |
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 |
/***************************************************************************** |
/***************************************************************************** |
82 |
* Command line global variables |
* Command line global variables |
85 |
/* Maximum number of frames to encode */ |
/* Maximum number of frames to encode */ |
86 |
#define ABS_MAXFRAMENR 9999 |
#define ABS_MAXFRAMENR 9999 |
87 |
|
|
88 |
|
static int ARG_STATS = 0; |
89 |
|
static int ARG_DUMP = 0; |
90 |
static int ARG_BITRATE = 900; |
static int ARG_BITRATE = 900; |
91 |
static int ARG_QUANTI = 0; |
static int ARG_QUANTI = 0; |
92 |
static int ARG_QUALITY = 6; |
static int ARG_QUALITY = 5; |
93 |
static int ARG_MINQUANT = 1; |
static int ARG_MINQUANT = 1; |
94 |
static int ARG_MAXQUANT = 31; |
static int ARG_MAXQUANT = 31; |
95 |
static float ARG_FRAMERATE = 25.00f; |
static float ARG_FRAMERATE = 25.00f; |
97 |
static char *ARG_INPUTFILE = NULL; |
static char *ARG_INPUTFILE = NULL; |
98 |
static int ARG_INPUTTYPE = 0; |
static int ARG_INPUTTYPE = 0; |
99 |
static int ARG_SAVEMPEGSTREAM = 0; |
static int ARG_SAVEMPEGSTREAM = 0; |
|
static int ARG_OUTPUTTYPE = 0; |
|
100 |
static char *ARG_OUTPUTFILE = NULL; |
static char *ARG_OUTPUTFILE = NULL; |
101 |
static int XDIM = 0; |
static int XDIM = 0; |
102 |
static int YDIM = 0; |
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) |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
108 |
|
|
109 |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
110 |
#define SMALL_EPS 1e-10 |
#define SMALL_EPS (1e-10) |
|
|
|
|
#define LONG_PACK(a,b,c,d) ((long) (((long)(a))<<24) | (((long)(b))<<16) | \ |
|
|
(((long)(c))<<8) |((long)(d))) |
|
111 |
|
|
112 |
#define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ |
#define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ |
113 |
(((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) |
(((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) |
116 |
* Nasty global vars ;-) |
* Nasty global vars ;-) |
117 |
***************************************************************************/ |
***************************************************************************/ |
118 |
|
|
119 |
static int i,filenr = 0; |
static int i; |
120 |
|
|
121 |
/* the path where to save output */ |
/* the path where to save output */ |
122 |
static char filepath[256] = "./"; |
static char filepath[256] = "./"; |
142 |
/* Encoder related functions */ |
/* Encoder related functions */ |
143 |
static int enc_init(int use_assembler); |
static int enc_init(int use_assembler); |
144 |
static int enc_stop(); |
static int enc_stop(); |
145 |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
static int enc_main(unsigned char* image, |
146 |
int *streamlength, int* frametype); |
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 |
* Main function |
164 |
double enctime; |
double enctime; |
165 |
double totalenctime=0.; |
double totalenctime=0.; |
166 |
|
|
167 |
long totalsize; |
int totalsize; |
168 |
int status; |
int result; |
|
int frame_type; |
|
|
int bigendian; |
|
|
|
|
169 |
int m4v_size; |
int m4v_size; |
170 |
|
int key; |
171 |
|
int stats_type; |
172 |
|
int stats_quant; |
173 |
|
int stats_length; |
174 |
int use_assembler=0; |
int use_assembler=0; |
175 |
|
|
176 |
|
int input_num; |
177 |
|
int output_num; |
178 |
|
|
179 |
char filename[256]; |
char filename[256]; |
180 |
|
|
181 |
FILE *in_file = stdin; |
FILE *in_file = stdin; |
182 |
FILE *out_file = NULL; |
FILE *out_file = NULL; |
183 |
|
|
184 |
printf("xvid_decraw - raw mpeg4 bitstream encoder "); |
printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
185 |
printf("written by Christoph Lampert 2002\n\n"); |
printf("written by Christoph Lampert 2002-2003\n\n"); |
186 |
|
|
187 |
/***************************************************************************** |
/***************************************************************************** |
188 |
* Command line parsing |
* Command line parsing |
205 |
i++; |
i++; |
206 |
ARG_BITRATE = atoi(argv[i]); |
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 ) { |
else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) { |
224 |
i++; |
i++; |
225 |
ARG_QUALITY = atoi(argv[i]); |
ARG_QUALITY = atoi(argv[i]); |
232 |
i++; |
i++; |
233 |
ARG_INPUTFILE = argv[i]; |
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 ) { |
else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) { |
242 |
i++; |
i++; |
243 |
ARG_INPUTTYPE = atoi(argv[i]); |
ARG_INPUTTYPE = atoi(argv[i]); |
250 |
i++; |
i++; |
251 |
ARG_QUANTI = atoi(argv[i]); |
ARG_QUANTI = atoi(argv[i]); |
252 |
} |
} |
253 |
else if (strcmp("-m", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-m", argv[i]) == 0) { |
254 |
i++; |
ARG_SAVEMPEGSTREAM = 1; |
|
ARG_SAVEMPEGSTREAM = atoi(argv[i]); |
|
|
} |
|
|
else if (strcmp("-mt", argv[i]) == 0 && i < argc - 1 ) { |
|
|
i++; |
|
|
ARG_OUTPUTTYPE = atoi(argv[i]); |
|
255 |
} |
} |
256 |
else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { |
257 |
i++; |
i++; |
277 |
ARG_INPUTTYPE = 1; /* pgm */ |
ARG_INPUTTYPE = 1; /* pgm */ |
278 |
} |
} |
279 |
|
|
280 |
if ( ARG_QUALITY < 0 || ARG_QUALITY > 6) { |
if ( ARG_QUALITY < 0 || ARG_QUALITY > 5) { |
281 |
fprintf(stderr,"Wrong Quality\n"); |
fprintf(stderr,"Wrong Quality\n"); |
282 |
return -1; |
return(-1); |
283 |
} |
} |
284 |
|
|
285 |
if ( ARG_BITRATE <= 0 && ARG_QUANTI == 0) { |
if ( ARG_BITRATE <= 0 && ARG_QUANTI == 0) { |
286 |
fprintf(stderr,"Wrong Bitrate\n"); |
fprintf(stderr,"Wrong Bitrate\n"); |
287 |
return -1; |
return(-1); |
288 |
} |
} |
289 |
|
|
290 |
if ( ARG_FRAMERATE <= 0) { |
if ( ARG_FRAMERATE <= 0) { |
291 |
fprintf(stderr,"Wrong Framerate %s \n",argv[5]); |
fprintf(stderr,"Wrong Framerate %s \n",argv[5]); |
292 |
return -1; |
return(-1); |
293 |
} |
} |
294 |
|
|
295 |
if ( ARG_MAXFRAMENR <= 0) { |
if ( ARG_MAXFRAMENR <= 0) { |
296 |
fprintf(stderr,"Wrong number of frames\n"); |
fprintf(stderr,"Wrong number of frames\n"); |
297 |
return -1; |
return(-1); |
298 |
} |
} |
299 |
|
|
300 |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
305 |
in_file = fopen(ARG_INPUTFILE, "rb"); |
in_file = fopen(ARG_INPUTFILE, "rb"); |
306 |
if (in_file == NULL) { |
if (in_file == NULL) { |
307 |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
308 |
return -1; |
return(-1); |
309 |
} |
} |
310 |
} |
} |
311 |
|
|
312 |
if (ARG_INPUTTYPE) { |
if (ARG_INPUTTYPE) { |
313 |
if (read_pgmheader(in_file)) { |
if (read_pgmheader(in_file)) { |
314 |
fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); |
fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); |
315 |
return -1; |
return(-1); |
316 |
} |
} |
317 |
} |
} |
318 |
|
|
319 |
/* now we know the sizes, so allocate memory */ |
/* now we know the sizes, so allocate memory */ |
|
|
|
320 |
in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)); |
in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)); |
321 |
if (!in_buffer) |
if (!in_buffer) |
322 |
goto free_all_memory; |
goto free_all_memory; |
331 |
****************************************************************************/ |
****************************************************************************/ |
332 |
|
|
333 |
|
|
334 |
status = enc_init(use_assembler); |
result = enc_init(use_assembler); |
335 |
if (status) |
if (result) |
336 |
{ |
{ |
337 |
fprintf(stderr, "Encore INIT problem, return value %d\n", status); |
fprintf(stderr, "Encore INIT problem, return value %d\n", result); |
338 |
goto release_all; |
goto release_all; |
339 |
} |
} |
340 |
|
|
342 |
* Main loop |
* Main loop |
343 |
****************************************************************************/ |
****************************************************************************/ |
344 |
|
|
345 |
if (ARG_SAVEMPEGSTREAM && (ARG_OUTPUTTYPE || ARG_OUTPUTFILE)) { |
if (ARG_SAVEMPEGSTREAM && ARG_OUTPUTFILE) { |
|
|
|
|
if (ARG_OUTPUTFILE == NULL && ARG_OUTPUTTYPE) |
|
|
ARG_OUTPUTFILE = "stream.mp4u"; |
|
|
else if(ARG_OUTPUTFILE == NULL && !ARG_OUTPUTTYPE) |
|
|
ARG_OUTPUTFILE = "stream.m4v"; |
|
346 |
|
|
347 |
if((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) { |
if((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) { |
348 |
fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE); |
fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE); |
349 |
goto release_all; |
goto release_all; |
350 |
} |
} |
351 |
|
|
|
/* Write header */ |
|
|
if (ARG_OUTPUTTYPE) { |
|
|
char *ptr; |
|
|
long test; |
|
|
|
|
|
test = LONG_PACK('M','P','4','U'); |
|
|
ptr = (unsigned char *)&test; |
|
|
if(*ptr == 'M') |
|
|
bigendian = 1; |
|
|
else |
|
|
bigendian = 0; |
|
|
|
|
|
test = (!bigendian)?SWAP(test):test; |
|
|
|
|
|
fwrite(&test, sizeof(test), 1, out_file); |
|
|
|
|
|
} |
|
|
|
|
352 |
} |
} |
353 |
else { |
else { |
354 |
out_file = NULL; |
out_file = NULL; |
360 |
|
|
361 |
totalsize = 0; |
totalsize = 0; |
362 |
|
|
363 |
|
result = 0; |
364 |
|
|
365 |
|
input_num = 0; /* input frame counter */ |
366 |
|
output_num = 0; /* output frame counter */ |
367 |
|
|
368 |
do { |
do { |
369 |
|
|
370 |
if (ARG_INPUTTYPE) |
char *type; |
371 |
status = read_pgmdata(in_file, in_buffer); // read PGM data (YUV-format) |
int stats[3]; |
|
else |
|
|
status = read_yuvdata(in_file, in_buffer); // read raw data (YUV-format) |
|
372 |
|
|
373 |
if (status) |
if (input_num >= ARG_MAXFRAMENR) |
374 |
{ |
{ |
375 |
/* Couldn't read image, most likely end-of-file */ |
result = 1; |
376 |
continue; |
} |
377 |
|
|
378 |
|
if (!result) |
379 |
|
{ |
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 |
/***************************************************************************** |
/***************************************************************************** |
391 |
****************************************************************************/ |
****************************************************************************/ |
392 |
|
|
393 |
enctime = msecond(); |
enctime = msecond(); |
394 |
status = enc_main(in_buffer, mp4_buffer, &m4v_size, &frame_type); |
m4v_size = enc_main(!result?in_buffer:0, mp4_buffer, &key, &stats_type, &stats_quant, &stats_length, stats); |
395 |
enctime = msecond() - enctime; |
enctime = msecond() - enctime; |
396 |
|
|
397 |
|
/* Write the Frame statistics */ |
398 |
|
|
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 |
|
|
425 |
|
printf(" | type=%s quant=%2d, length=%7d", type, stats_quant, stats_length); |
426 |
|
|
427 |
|
if(ARG_STATS) { |
428 |
|
printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f", |
429 |
|
(stats[0] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[0]/((float)(XDIM)*(YDIM))), |
430 |
|
(stats[1] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[1]/((float)(XDIM)*(YDIM)/4)), |
431 |
|
(stats[2] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[2]/((float)(XDIM)*(YDIM)/4))); |
432 |
|
} |
433 |
|
|
434 |
|
} |
435 |
|
|
436 |
|
printf("\n"); |
437 |
|
|
438 |
|
if (m4v_size < 0) { |
439 |
|
break; |
440 |
|
} |
441 |
|
|
442 |
|
/* Update encoding time stats */ |
443 |
totalenctime += enctime; |
totalenctime += enctime; |
444 |
totalsize += m4v_size; |
totalsize += m4v_size; |
445 |
|
|
446 |
printf("Frame %5d: intra %1d, enctime=%6.1f ms, size=%6dbytes\n", |
/***************************************************************************** |
447 |
(int)filenr, (int)frame_type, (float)enctime, (int)m4v_size); |
* Save stream to file |
448 |
|
****************************************************************************/ |
449 |
|
|
450 |
if (ARG_SAVEMPEGSTREAM) |
if (m4v_size>0 && ARG_SAVEMPEGSTREAM) |
451 |
{ |
{ |
452 |
/* Save single files */ |
/* Save single files */ |
453 |
if (out_file == NULL) { |
if (out_file == NULL) { |
454 |
sprintf(filename, "%sframe%05d.m4v", filepath, filenr); |
sprintf(filename, "%sframe%05d.m4v", filepath, output_num); |
455 |
out_file = fopen(filename, "wb"); |
out_file = fopen(filename, "wb"); |
456 |
fwrite(mp4_buffer, m4v_size, 1, out_file); |
fwrite(mp4_buffer, m4v_size, 1, out_file); |
457 |
fclose(out_file); |
fclose(out_file); |
458 |
out_file = NULL; |
out_file = NULL; |
459 |
|
output_num++; |
460 |
} |
} |
461 |
else { |
else { |
|
/* Using mp4u container */ |
|
|
if (ARG_OUTPUTTYPE) { |
|
|
long size = m4v_size; |
|
|
size = (!bigendian)?SWAP(size):size; |
|
|
fwrite(&size, sizeof(size), 1, out_file); |
|
|
} |
|
462 |
|
|
463 |
/* Write mp4 data */ |
/* Write mp4 data */ |
464 |
fwrite(mp4_buffer, m4v_size, 1, out_file); |
fwrite(mp4_buffer, 1, m4v_size, out_file); |
465 |
|
|
466 |
} |
} |
467 |
} |
} |
468 |
|
|
469 |
/* Read the header if it's pgm stream */ |
input_num++; |
|
if (ARG_INPUTTYPE) |
|
|
status = read_pgmheader(in_file); |
|
470 |
|
|
471 |
filenr++; |
/* Read the header if it's pgm stream */ |
472 |
|
if (!result && ARG_INPUTTYPE) |
473 |
|
result = read_pgmheader(in_file); |
474 |
|
|
475 |
} while ( (!status) && (filenr<ARG_MAXFRAMENR) ); |
} while (1); |
476 |
|
|
477 |
|
|
478 |
|
|
480 |
* Calculate totals and averages for output, print results |
* Calculate totals and averages for output, print results |
481 |
****************************************************************************/ |
****************************************************************************/ |
482 |
|
|
483 |
totalsize /= filenr; |
if (input_num > 0) { |
484 |
totalenctime /= filenr; |
totalsize /= input_num; |
485 |
|
totalenctime /= input_num; |
486 |
|
}else{ |
487 |
|
totalsize = -1; |
488 |
|
totalenctime = -1; |
489 |
|
} |
490 |
|
|
491 |
printf("Avg: enctime %5.2f ms, %5.2f fps, filesize %7d bytes\n", |
printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d\n", |
492 |
totalenctime, 1000/totalenctime, (int)totalsize); |
totalenctime, 1000/totalenctime, (int)totalsize); |
493 |
|
|
494 |
|
|
495 |
/***************************************************************************** |
/***************************************************************************** |
496 |
* XviD PART Stop |
* XviD PART Stop |
497 |
****************************************************************************/ |
****************************************************************************/ |
500 |
|
|
501 |
if (enc_handle) |
if (enc_handle) |
502 |
{ |
{ |
503 |
status = enc_stop(); |
result = enc_stop(); |
504 |
if (status) |
if (result) |
505 |
fprintf(stderr, "Encore RELEASE problem return value %d\n", status); |
fprintf(stderr, "Encore RELEASE problem return value %d\n", result); |
506 |
} |
} |
507 |
|
|
508 |
|
if(in_file) |
509 |
fclose(in_file); |
fclose(in_file); |
510 |
if(out_file) |
if(out_file) |
511 |
fclose(out_file); |
fclose(out_file); |
515 |
free(mp4_buffer); |
free(mp4_buffer); |
516 |
free(in_buffer); |
free(in_buffer); |
517 |
|
|
518 |
return 0; |
return(0); |
519 |
|
|
520 |
} |
} |
521 |
|
|
531 |
/* Return time elapsed time in miliseconds since the program started */ |
/* Return time elapsed time in miliseconds since the program started */ |
532 |
static double msecond() |
static double msecond() |
533 |
{ |
{ |
534 |
#ifndef _MSC_VER |
#ifndef WIN32 |
535 |
struct timeval tv; |
struct timeval tv; |
536 |
gettimeofday(&tv, 0); |
gettimeofday(&tv, 0); |
537 |
return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3; |
return(tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3); |
538 |
#else |
#else |
539 |
clock_t clk; |
clock_t clk; |
540 |
clk = clock(); |
clk = clock(); |
541 |
return clk * 1000 / CLOCKS_PER_SEC; |
return(clk * 1000 / CLOCKS_PER_SEC); |
542 |
#endif |
#endif |
543 |
} |
} |
544 |
|
|
554 |
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
555 |
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
556 |
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
557 |
|
fprintf(stderr, " -bn integer : max bframes (default=0)\n"); |
558 |
|
fprintf(stderr, " -p : packed mode\n"); |
559 |
|
fprintf(stderr, " -bqr integer : bframe quantizer ratio (default=150)\n"); |
560 |
|
fprintf(stderr, " -bqo integer : bframe quantizer offset (default=100)\n"); |
561 |
fprintf(stderr, " -f float : target framerate (>0)\n"); |
fprintf(stderr, " -f float : target framerate (>0)\n"); |
562 |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
563 |
|
fprintf(stderr, " -s : print stats about encoded frames\n"); |
564 |
fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n"); |
fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n"); |
565 |
fprintf(stderr, " -n integer : number of frames to encode\n"); |
fprintf(stderr, " -n integer : number of frames to encode\n"); |
566 |
fprintf(stderr, " -q integer : quality ([0..5])\n"); |
fprintf(stderr, " -q integer : quality ([0..5])\n"); |
567 |
fprintf(stderr, " -d boolean : save decoder output (0 False*, !=0 True)\n"); |
fprintf(stderr, " -dump : save decoder output\n"); |
568 |
fprintf(stderr, " -m boolean : save mpeg4 raw stream (0 False*, !=0 True)\n"); |
fprintf(stderr, " -m : save mpeg4 raw stream\n"); |
569 |
fprintf(stderr, " -o string : output container filename (only usefull when -m 1 is used) :\n"); |
fprintf(stderr, " -o string : output container filename (only usefull when -m 1 is used) :\n"); |
570 |
fprintf(stderr, " When this option is not used : one file per encoded frame\n"); |
fprintf(stderr, " When this option is not used : one file per encoded frame\n"); |
571 |
fprintf(stderr, " When this option is used :\n"); |
fprintf(stderr, " When this option is used : save to 'string' file\n"); |
|
fprintf(stderr, " + stream.m4v with -mt 0\n"); |
|
|
fprintf(stderr, " + stream.mp4u with -mt 1\n"); |
|
|
fprintf(stderr, " -mt integer : output type (m4v=0, mp4u=1)\n"); |
|
572 |
fprintf(stderr, " -help : prints this help message\n"); |
fprintf(stderr, " -help : prints this help message\n"); |
573 |
fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); |
fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); |
574 |
fprintf(stderr, " (* means default)\n"); |
fprintf(stderr, " (* means default)\n"); |
591 |
bytes = fread(dummy,1,2,handle); |
bytes = fread(dummy,1,2,handle); |
592 |
|
|
593 |
if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) |
if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) |
594 |
return 1; |
return(1); |
595 |
|
|
596 |
fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
597 |
if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) |
if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) |
598 |
{ |
{ |
599 |
fprintf(stderr,"%d %d %d\n",xsize,ysize,depth); |
fprintf(stderr,"%d %d %d\n",xsize,ysize,depth); |
600 |
return 2; |
return(2); |
601 |
} |
} |
602 |
if ( (XDIM==0) || (YDIM==0) ) |
if ( (XDIM==0) || (YDIM==0) ) |
603 |
{ |
{ |
605 |
YDIM=ysize*2/3; |
YDIM=ysize*2/3; |
606 |
} |
} |
607 |
|
|
608 |
return 0; |
return(0); |
609 |
} |
} |
610 |
|
|
611 |
static int read_pgmdata(FILE* handle, unsigned char *image) |
static int read_pgmdata(FILE* handle, unsigned char *image) |
636 |
/* I don't know why, but this seems needed */ |
/* I don't know why, but this seems needed */ |
637 |
fread(&dummy, 1, 1, handle); |
fread(&dummy, 1, 1, handle); |
638 |
|
|
639 |
return 0; |
return(0); |
640 |
} |
} |
641 |
|
|
642 |
static int read_yuvdata(FILE* handle, unsigned char *image) |
static int read_yuvdata(FILE* handle, unsigned char *image) |
643 |
{ |
{ |
644 |
|
|
645 |
if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != (unsigned int)IMAGE_SIZE(XDIM, YDIM)) |
if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != (unsigned int)IMAGE_SIZE(XDIM, YDIM)) |
646 |
return 1; |
return(1); |
647 |
else |
else |
648 |
return 0; |
return(0); |
649 |
} |
} |
650 |
|
|
651 |
/***************************************************************************** |
/***************************************************************************** |
652 |
* Routines for encoding: init encoder, frame step, release encoder |
* Routines for encoding: init encoder, frame step, release encoder |
653 |
****************************************************************************/ |
****************************************************************************/ |
654 |
|
|
655 |
|
/* sample plugin */ |
656 |
|
|
657 |
|
int rawenc_debug(void * handle, int opt, void * param1, void * param2) |
658 |
|
{ |
659 |
|
switch(opt) |
660 |
|
{ |
661 |
|
case XVID_PLG_INFO : |
662 |
|
case XVID_PLG_CREATE : |
663 |
|
case XVID_PLG_DESTROY : |
664 |
|
case XVID_PLG_BEFORE : |
665 |
|
return 0; |
666 |
|
|
667 |
|
case XVID_PLG_AFTER : |
668 |
|
{ |
669 |
|
xvid_plg_data_t * data = (xvid_plg_data_t*)param1; |
670 |
|
printf("type=%i, quant=%i, length=%i\n", data->type, data->quant, data->length); |
671 |
|
return 0; |
672 |
|
} |
673 |
|
} |
674 |
|
|
675 |
|
return XVID_ERR_FAIL; |
676 |
|
} |
677 |
|
|
678 |
|
|
679 |
#define FRAMERATE_INCR 1001 |
#define FRAMERATE_INCR 1001 |
680 |
|
|
681 |
/* Initialize encoder for first use, pass all needed parameters to the codec */ |
/* Initialize encoder for first use, pass all needed parameters to the codec */ |
683 |
{ |
{ |
684 |
int xerr; |
int xerr; |
685 |
|
|
686 |
XVID_INIT_PARAM xinit; |
xvid_enc_plugin_t plugins[1]; |
|
XVID_ENC_PARAM xparam; |
|
687 |
|
|
688 |
|
xvid_gbl_init_t xvid_gbl_init; |
689 |
|
xvid_enc_create_t xvid_enc_create; |
690 |
|
|
691 |
|
/*------------------------------------------------------------------------ |
692 |
|
* XviD core initialization |
693 |
|
*----------------------------------------------------------------------*/ |
694 |
|
|
695 |
|
/* Set version -- version checking will done by xvidcore*/ |
696 |
|
memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init)); |
697 |
|
xvid_gbl_init.version = XVID_VERSION; |
698 |
|
|
699 |
|
|
700 |
|
/* Do we have to enable ASM optimizations ? */ |
701 |
if(use_assembler) { |
if(use_assembler) { |
702 |
|
|
703 |
#ifdef ARCH_IA64 |
#ifdef ARCH_IS_IA64 |
704 |
xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
705 |
#else |
#else |
706 |
xinit.cpu_flags = 0; |
xvid_gbl_init.cpu_flags = 0; |
707 |
#endif |
#endif |
708 |
} |
} |
709 |
else { |
else { |
710 |
xinit.cpu_flags = XVID_CPU_FORCE; |
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; |
711 |
} |
} |
712 |
|
|
713 |
xvid_init(NULL, 0, &xinit, NULL); |
/* Initialize XviD core -- Should be done once per __process__ */ |
714 |
|
xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL); |
715 |
|
|
716 |
xparam.width = XDIM; |
/*------------------------------------------------------------------------ |
717 |
xparam.height = YDIM; |
* XviD encoder initialization |
718 |
if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS) |
*----------------------------------------------------------------------*/ |
719 |
{ |
|
720 |
xparam.fincr = 1; |
/* Version again */ |
721 |
xparam.fbase = (int)ARG_FRAMERATE; |
memset(&xvid_enc_create, 0, sizeof(xvid_enc_create)); |
722 |
|
xvid_enc_create.version = XVID_VERSION; |
723 |
|
|
724 |
|
/* Width and Height of input frames */ |
725 |
|
xvid_enc_create.width = XDIM; |
726 |
|
xvid_enc_create.height = YDIM; |
727 |
|
|
728 |
|
/* init plugins */ |
729 |
|
|
730 |
|
xvid_enc_create.plugins = plugins; |
731 |
|
xvid_enc_create.num_plugins = 0; |
732 |
|
if (ARG_DUMP) { |
733 |
|
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_dump; |
734 |
|
plugins[xvid_enc_create.num_plugins].param = NULL; |
735 |
|
xvid_enc_create.num_plugins++; |
736 |
} |
} |
737 |
else |
|
738 |
{ |
/* No fancy thread tests */ |
739 |
xparam.fincr = FRAMERATE_INCR; |
xvid_enc_create.num_threads = 0; |
740 |
xparam.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE); |
|
741 |
|
/* Frame rate - Do some quick float fps = fincr/fbase hack */ |
742 |
|
if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS) { |
743 |
|
xvid_enc_create.fincr = 1; |
744 |
|
xvid_enc_create.fbase = (int)ARG_FRAMERATE; |
745 |
|
} else { |
746 |
|
xvid_enc_create.fincr = FRAMERATE_INCR; |
747 |
|
xvid_enc_create.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE); |
748 |
} |
} |
749 |
xparam.rc_reaction_delay_factor = 16; |
|
750 |
xparam.rc_averaging_period = 100; |
/* Maximum key frame interval */ |
751 |
xparam.rc_buffer = 10; |
xvid_enc_create.max_key_interval = (int)ARG_FRAMERATE*10; |
752 |
xparam.rc_bitrate = ARG_BITRATE*1000; |
|
753 |
xparam.min_quantizer = ARG_MINQUANT; |
/* Bframes settings */ |
754 |
xparam.max_quantizer = ARG_MAXQUANT; |
xvid_enc_create.max_bframes = ARG_MAXBFRAMES; |
755 |
xparam.max_key_interval = (int)ARG_FRAMERATE*10; |
xvid_enc_create.bquant_ratio = ARG_BQRATIO; |
756 |
|
xvid_enc_create.bquant_offset = ARG_BQOFFSET; |
757 |
|
|
758 |
|
/* Dropping ratio frame -- we don't need that */ |
759 |
|
xvid_enc_create.frame_drop_ratio = 0; |
760 |
|
|
761 |
|
/* Global encoder options */ |
762 |
|
xvid_enc_create.global = 0; |
763 |
|
if (ARG_PACKED) xvid_enc_create.global |= XVID_PACKED; |
764 |
|
if (ARG_STATS) xvid_enc_create.global |= XVID_EXTRASTATS_ENABLE; |
765 |
|
|
766 |
/* I use a small value here, since will not encode whole movies, but short clips */ |
/* I use a small value here, since will not encode whole movies, but short clips */ |
767 |
|
xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); |
768 |
|
|
769 |
xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xparam, NULL); |
/* Retrieve the encoder instance from the structure */ |
770 |
enc_handle=xparam.handle; |
enc_handle = xvid_enc_create.handle; |
771 |
|
|
772 |
return xerr; |
return(xerr); |
773 |
} |
} |
774 |
|
|
775 |
static int enc_stop() |
static int enc_stop() |
776 |
{ |
{ |
777 |
int xerr; |
int xerr; |
778 |
|
|
779 |
|
/* Destroy the encoder instance */ |
780 |
xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); |
xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); |
|
return xerr; |
|
781 |
|
|
782 |
|
return(xerr); |
783 |
} |
} |
784 |
|
|
785 |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
static int enc_main(unsigned char* image, |
786 |
int *streamlength, int* frametype) |
unsigned char* bitstream, |
787 |
|
int * key, |
788 |
|
int *stats_type, |
789 |
|
int *stats_quant, |
790 |
|
int *stats_length, |
791 |
|
int stats[3]) |
792 |
{ |
{ |
793 |
int xerr; |
int ret; |
794 |
|
|
795 |
|
xvid_enc_frame_t xvid_enc_frame; |
796 |
|
xvid_enc_stats_t xvid_enc_stats; |
797 |
|
|
798 |
|
/* Version for the frame and the stats */ |
799 |
|
memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame)); |
800 |
|
xvid_enc_frame.version = XVID_VERSION; |
801 |
|
|
802 |
|
memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats)); |
803 |
|
xvid_enc_stats.version = XVID_VERSION; |
804 |
|
|
805 |
|
/* Bind output buffer */ |
806 |
|
xvid_enc_frame.bitstream = bitstream; |
807 |
|
xvid_enc_frame.length = -1; |
808 |
|
|
809 |
|
/* Initialize input image fields */ |
810 |
|
if (image) { |
811 |
|
xvid_enc_frame.input.plane[0] = image; |
812 |
|
xvid_enc_frame.input.csp = XVID_CSP_I420; |
813 |
|
xvid_enc_frame.input.stride[0] = XDIM; |
814 |
|
}else{ |
815 |
|
xvid_enc_frame.input.csp = XVID_CSP_NULL; |
816 |
|
} |
817 |
|
|
818 |
XVID_ENC_FRAME xframe; |
/* Set up core's general features */ |
819 |
XVID_ENC_STATS xstats; |
xvid_enc_frame.vol_flags = vol_presets[ARG_QUALITY]; |
820 |
|
if (ARG_STATS) xvid_enc_frame.vol_flags |= XVID_EXTRASTATS; |
821 |
|
|
822 |
xframe.bitstream = bitstream; |
/* Set up core's general features */ |
823 |
xframe.length = -1; // this is written by the routine |
xvid_enc_frame.vop_flags = vop_presets[ARG_QUALITY]; |
824 |
|
|
825 |
xframe.image = image; |
/* Frame type -- let core decide for us */ |
826 |
xframe.colorspace = XVID_CSP_YV12; // defined in <xvid.h> |
xvid_enc_frame.type = XVID_TYPE_AUTO; |
827 |
|
|
828 |
xframe.intra = -1; // let the codec decide between I-frame (1) and P-frame (0) |
/* Force the right quantizer */ |
829 |
|
xvid_enc_frame.quant = ARG_QUANTI; |
830 |
|
xvid_enc_frame.bquant = 0; |
831 |
|
|
832 |
xframe.quant = ARG_QUANTI; // is quant != 0, use a fixed quant (and ignore bitrate) |
/* Set up motion estimation flags */ |
833 |
|
xvid_enc_frame.motion = motion_presets[ARG_QUALITY]; |
834 |
|
|
835 |
xframe.motion = motion_presets[ARG_QUALITY]; |
/* We don't use special matrices */ |
836 |
xframe.general = general_presets[ARG_QUALITY]; |
xvid_enc_frame.quant_intra_matrix = NULL; |
837 |
xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL; |
xvid_enc_frame.quant_inter_matrix = NULL; |
838 |
|
|
839 |
xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats); |
/* Encode the frame */ |
840 |
|
ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame, &xvid_enc_stats); |
841 |
|
|
842 |
/* |
*key = (xvid_enc_frame.out_flags & XVID_KEYFRAME); |
843 |
* This is statictical data, e.g. for 2-pass. If you are not |
*stats_type = xvid_enc_stats.type; |
844 |
* interested in any of this, you can use NULL instead of &xstats |
*stats_quant = xvid_enc_stats.quant; |
845 |
*/ |
*stats_length = xvid_enc_stats.length; |
846 |
*frametype = xframe.intra; |
stats[0] = xvid_enc_stats.sse_y; |
847 |
*streamlength = xframe.length; |
stats[1] = xvid_enc_stats.sse_u; |
848 |
|
stats[2] = xvid_enc_stats.sse_v; |
849 |
|
|
850 |
return xerr; |
return(ret); |
851 |
} |
} |