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-2003 Christoph Lampert |
* 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 |
96 |
static char *ARG_INPUTFILE = NULL; |
static char *ARG_INPUTFILE = NULL; |
97 |
static int ARG_INPUTTYPE = 0; |
static int ARG_INPUTTYPE = 0; |
98 |
static int ARG_SAVEMPEGSTREAM = 0; |
static int ARG_SAVEMPEGSTREAM = 0; |
99 |
|
static int ARG_OUTPUTTYPE = 0; |
100 |
static char *ARG_OUTPUTFILE = NULL; |
static char *ARG_OUTPUTFILE = NULL; |
101 |
static int ARG_HINTMODE = HINT_MODE_NONE; |
static int ARG_HINTMODE = HINT_MODE_NONE; |
102 |
static int XDIM = 0; |
static int XDIM = 0; |
103 |
static int YDIM = 0; |
static int YDIM = 0; |
|
static int ARG_BQRATIO = 120; |
|
|
static int ARG_BQOFFSET = 0; |
|
|
static int ARG_MAXBFRAMES = 0; |
|
104 |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
105 |
|
|
106 |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
107 |
#define SMALL_EPS 1e-10 |
#define SMALL_EPS 1e-10 |
108 |
|
|
109 |
|
#define LONG_PACK(a,b,c,d) ((long) (((long)(a))<<24) | (((long)(b))<<16) | \ |
110 |
|
(((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) ) |
114 |
|
|
177 |
FILE *hints_file = NULL; |
FILE *hints_file = NULL; |
178 |
|
|
179 |
printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
180 |
printf("written by Christoph Lampert 2002-2003\n\n"); |
printf("written by Christoph Lampert 2002\n\n"); |
181 |
|
|
182 |
/***************************************************************************** |
/***************************************************************************** |
183 |
* Command line parsing |
* Command line parsing |
200 |
i++; |
i++; |
201 |
ARG_BITRATE = atoi(argv[i]); |
ARG_BITRATE = atoi(argv[i]); |
202 |
} |
} |
|
else if (strcmp("-bn", argv[i]) == 0 && i < argc - 1 ) { |
|
|
i++; |
|
|
ARG_MAXBFRAMES = atoi(argv[i]); |
|
|
} |
|
|
else if (strcmp("-bqr", argv[i]) == 0 && i < argc - 1 ) { |
|
|
i++; |
|
|
ARG_BQRATIO = atoi(argv[i]); |
|
|
} |
|
|
else if (strcmp("-bqo", argv[i]) == 0 && i < argc - 1 ) { |
|
|
i++; |
|
|
ARG_BQOFFSET = atoi(argv[i]); |
|
|
} |
|
203 |
else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) { |
204 |
i++; |
i++; |
205 |
ARG_QUALITY = atoi(argv[i]); |
ARG_QUALITY = atoi(argv[i]); |
228 |
i++; |
i++; |
229 |
ARG_SAVEMPEGSTREAM = atoi(argv[i]); |
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 ) { |
else if (strcmp("-mv", argv[i]) == 0 && i < argc - 1 ) { |
236 |
i++; |
i++; |
237 |
ARG_HINTMODE = atoi(argv[i]); |
ARG_HINTMODE = atoi(argv[i]); |
331 |
} |
} |
332 |
|
|
333 |
/* now we know the sizes, so allocate memory */ |
/* now we know the sizes, so allocate memory */ |
334 |
|
|
335 |
in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)); |
in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)); |
336 |
if (!in_buffer) |
if (!in_buffer) |
337 |
goto free_all_memory; |
goto free_all_memory; |
357 |
* Main loop |
* Main loop |
358 |
****************************************************************************/ |
****************************************************************************/ |
359 |
|
|
360 |
if (ARG_SAVEMPEGSTREAM && ARG_OUTPUTFILE) { |
totalsize = LONG_PACK('M','P','4','U'); |
361 |
|
if(*((char *)(&totalsize)) == 'M') |
362 |
|
bigendian = 1; |
363 |
|
else |
364 |
|
bigendian = 0; |
365 |
|
|
366 |
|
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) { |
if((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) { |
374 |
fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE); |
fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE); |
375 |
goto release_all; |
goto release_all; |
376 |
} |
} |
377 |
|
|
378 |
|
/* Write header */ |
379 |
|
if (ARG_OUTPUTTYPE) { |
380 |
|
|
381 |
|
long test = LONG_PACK('M','P','4','U'); |
382 |
|
|
383 |
|
test = (!bigendian)?SWAP(test):test; |
384 |
|
|
385 |
|
fwrite(&test, sizeof(test), 1, out_file); |
386 |
|
|
387 |
|
} |
388 |
|
|
389 |
} |
} |
390 |
else { |
else { |
391 |
out_file = NULL; |
out_file = NULL; |
429 |
&m4v_size, &frame_type, &hints_size); |
&m4v_size, &frame_type, &hints_size); |
430 |
enctime = msecond() - enctime; |
enctime = msecond() - enctime; |
431 |
|
|
|
/* if it's a not coded VOP (aka NVOP) then we write nothing */ |
|
|
if(frame_type == 5) goto next_frame; |
|
|
|
|
|
{ |
|
|
char *type[] = {"P", "I", "B", "S", "Packed", "N", "Unknown"}; |
|
|
|
|
|
if(frame_type<0 || frame_type>5) frame_type = 6; |
|
|
|
|
|
printf("Frame %5d: type = %s, enctime(ms) =%6.1f, length(bytes) =%7d\n", |
|
|
(int)filenr, type[frame_type], (float)enctime, (int)m4v_size); |
|
|
|
|
|
} |
|
|
|
|
|
/* Update encoding time stats */ |
|
432 |
totalenctime += enctime; |
totalenctime += enctime; |
433 |
totalsize += m4v_size; |
totalsize += m4v_size; |
434 |
|
|
435 |
|
printf("Frame %5d: intra %1d, enctime=%6.1f ms, size=%6dbytes\n", |
436 |
|
(int)filenr, (int)frame_type, (float)enctime, (int)m4v_size); |
437 |
|
|
438 |
/***************************************************************************** |
/***************************************************************************** |
439 |
* Save hints to file |
* Save hints to file |
440 |
****************************************************************************/ |
****************************************************************************/ |
461 |
out_file = NULL; |
out_file = NULL; |
462 |
} |
} |
463 |
else { |
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 */ |
/* Write mp4 data */ |
472 |
fwrite(mp4_buffer, 1, m4v_size, out_file); |
fwrite(mp4_buffer, m4v_size, 1, out_file); |
473 |
|
|
474 |
} |
} |
475 |
} |
} |
476 |
|
|
|
next_frame: |
|
477 |
/* Read the header if it's pgm stream */ |
/* Read the header if it's pgm stream */ |
478 |
if (ARG_INPUTTYPE) |
if (ARG_INPUTTYPE) |
479 |
status = read_pgmheader(in_file); |
status = read_pgmheader(in_file); |
480 |
|
|
481 |
if(frame_type != 5) filenr++; |
filenr++; |
482 |
|
|
483 |
} while ( (!status) && (filenr<ARG_MAXFRAMENR) ); |
} while ( (!status) && (filenr<ARG_MAXFRAMENR) ); |
484 |
|
|
491 |
totalsize /= filenr; |
totalsize /= filenr; |
492 |
totalenctime /= filenr; |
totalenctime /= filenr; |
493 |
|
|
494 |
printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d\n", |
printf("Avg: enctime %5.2f ms, %5.2f fps, filesize %7d bytes\n", |
495 |
totalenctime, 1000/totalenctime, (int)totalsize); |
totalenctime, 1000/totalenctime, (int)totalsize); |
496 |
|
|
497 |
/***************************************************************************** |
/***************************************************************************** |
536 |
/* Return time elapsed time in miliseconds since the program started */ |
/* Return time elapsed time in miliseconds since the program started */ |
537 |
static double msecond() |
static double msecond() |
538 |
{ |
{ |
539 |
#ifndef _MSC_VER |
#ifndef WIN32 |
540 |
struct timeval tv; |
struct timeval tv; |
541 |
gettimeofday(&tv, 0); |
gettimeofday(&tv, 0); |
542 |
return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3; |
return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3; |
559 |
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
560 |
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
561 |
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
|
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
|
|
fprintf(stderr, " -bn integer : max bframes (default=0)\n"); |
|
|
fprintf(stderr, " -bqr integer : bframe quantizer ratio (default=150)\n"); |
|
|
fprintf(stderr, " -bqo integer : bframe quantizer offset (default=100)\n"); |
|
562 |
fprintf(stderr, " -f float : target framerate (>0)\n"); |
fprintf(stderr, " -f float : target framerate (>0)\n"); |
563 |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
fprintf(stderr, " -i string : input filename (default=stdin)\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"); |
568 |
fprintf(stderr, " -m boolean : save mpeg4 raw stream (0 False*, !=0 True)\n"); |
fprintf(stderr, " -m boolean : save mpeg4 raw stream (0 False*, !=0 True)\n"); |
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 : save to 'string' (default=stream.m4v)\n"); |
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"); |
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"); |
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"); |
fprintf(stderr, " -help : prints this help message\n"); |
699 |
xparam.min_quantizer = ARG_MINQUANT; |
xparam.min_quantizer = ARG_MINQUANT; |
700 |
xparam.max_quantizer = ARG_MAXQUANT; |
xparam.max_quantizer = ARG_MAXQUANT; |
701 |
xparam.max_key_interval = (int)ARG_FRAMERATE*10; |
xparam.max_key_interval = (int)ARG_FRAMERATE*10; |
|
xparam.bquant_ratio = ARG_BQRATIO; |
|
|
xparam.bquant_offset = ARG_BQOFFSET; |
|
|
xparam.max_bframes = ARG_MAXBFRAMES; |
|
|
xparam.frame_drop_ratio = 0; |
|
|
xparam.global = 0; |
|
702 |
|
|
703 |
/* 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 */ |
704 |
|
|
735 |
xframe.intra = -1; /* let the codec decide between I-frame (1) and P-frame (0) */ |
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) */ |
xframe.quant = ARG_QUANTI; /* is quant != 0, use a fixed quant (and ignore bitrate) */ |
|
xframe.bquant = 0; |
|
738 |
|
|
739 |
xframe.motion = motion_presets[ARG_QUALITY]; |
xframe.motion = motion_presets[ARG_QUALITY]; |
740 |
xframe.general = general_presets[ARG_QUALITY]; |
xframe.general = general_presets[ARG_QUALITY]; |
741 |
xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL; |
xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL; |
|
xframe.stride = XDIM; |
|
742 |
|
|
743 |
xframe.hint.hintstream = hints_buffer; |
xframe.hint.hintstream = hints_buffer; |
744 |
|
|