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.6 2002-12-18 20:48:25 edgomez Exp $ |
* $Id: xvid_encraw.c,v 1.11 2003-02-16 05:11:39 suxen_drol Exp $ |
23 |
* |
* |
24 |
****************************************************************************/ |
****************************************************************************/ |
25 |
|
|
38 |
#include <stdlib.h> |
#include <stdlib.h> |
39 |
#include <string.h> |
#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> |
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; |
|
static int ARG_OUTPUTTYPE = 0; |
|
99 |
static char *ARG_OUTPUTFILE = NULL; |
static char *ARG_OUTPUTFILE = NULL; |
100 |
static int ARG_HINTMODE = HINT_MODE_NONE; |
static int ARG_HINTMODE = HINT_MODE_NONE; |
101 |
static int XDIM = 0; |
static int XDIM = 0; |
102 |
static int YDIM = 0; |
static int YDIM = 0; |
103 |
|
static int ARG_BQRATIO = 120; |
104 |
|
static int ARG_BQOFFSET = 0; |
105 |
|
static int ARG_MAXBFRAMES = 0; |
106 |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
107 |
|
|
108 |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
109 |
#define SMALL_EPS 1e-10 |
#define SMALL_EPS 1e-10 |
110 |
|
|
|
#define LONG_PACK(a,b,c,d) ((long) (((long)(a))<<24) | (((long)(b))<<16) | \ |
|
|
(((long)(c))<<8) |((long)(d))) |
|
|
|
|
111 |
#define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ |
#define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ |
112 |
(((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) |
(((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) |
113 |
|
|
175 |
FILE *out_file = NULL; |
FILE *out_file = NULL; |
176 |
FILE *hints_file = NULL; |
FILE *hints_file = NULL; |
177 |
|
|
178 |
printf("xvid_decraw - raw mpeg4 bitstream encoder "); |
printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
179 |
printf("written by Christoph Lampert 2002\n\n"); |
printf("written by Christoph Lampert 2002-2003\n\n"); |
180 |
|
|
181 |
/***************************************************************************** |
/***************************************************************************** |
182 |
* Command line parsing |
* Command line parsing |
199 |
i++; |
i++; |
200 |
ARG_BITRATE = atoi(argv[i]); |
ARG_BITRATE = atoi(argv[i]); |
201 |
} |
} |
202 |
|
else if (strcmp("-bn", argv[i]) == 0 && i < argc - 1 ) { |
203 |
|
i++; |
204 |
|
ARG_MAXBFRAMES = atoi(argv[i]); |
205 |
|
} |
206 |
|
else if (strcmp("-bqr", argv[i]) == 0 && i < argc - 1 ) { |
207 |
|
i++; |
208 |
|
ARG_BQRATIO = atoi(argv[i]); |
209 |
|
} |
210 |
|
else if (strcmp("-bqo", argv[i]) == 0 && i < argc - 1 ) { |
211 |
|
i++; |
212 |
|
ARG_BQOFFSET = atoi(argv[i]); |
213 |
|
} |
214 |
else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) { |
215 |
i++; |
i++; |
216 |
ARG_QUALITY = atoi(argv[i]); |
ARG_QUALITY = atoi(argv[i]); |
239 |
i++; |
i++; |
240 |
ARG_SAVEMPEGSTREAM = atoi(argv[i]); |
ARG_SAVEMPEGSTREAM = atoi(argv[i]); |
241 |
} |
} |
|
else if (strcmp("-mt", argv[i]) == 0 && i < argc - 1 ) { |
|
|
i++; |
|
|
ARG_OUTPUTTYPE = atoi(argv[i]); |
|
|
} |
|
242 |
else if (strcmp("-mv", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-mv", argv[i]) == 0 && i < argc - 1 ) { |
243 |
i++; |
i++; |
244 |
ARG_HINTMODE = atoi(argv[i]); |
ARG_HINTMODE = atoi(argv[i]); |
338 |
} |
} |
339 |
|
|
340 |
/* now we know the sizes, so allocate memory */ |
/* now we know the sizes, so allocate memory */ |
|
|
|
341 |
in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)); |
in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)); |
342 |
if (!in_buffer) |
if (!in_buffer) |
343 |
goto free_all_memory; |
goto free_all_memory; |
363 |
* Main loop |
* Main loop |
364 |
****************************************************************************/ |
****************************************************************************/ |
365 |
|
|
366 |
totalsize = LONG_PACK('M','P','4','U'); |
if (ARG_SAVEMPEGSTREAM && ARG_OUTPUTFILE) { |
|
if(*((char *)(&totalsize)) == 'M') |
|
|
bigendian = 1; |
|
|
else |
|
|
bigendian = 0; |
|
|
|
|
|
if (ARG_SAVEMPEGSTREAM && (ARG_OUTPUTTYPE || ARG_OUTPUTFILE)) { |
|
|
|
|
|
if (ARG_OUTPUTFILE == NULL && ARG_OUTPUTTYPE) |
|
|
ARG_OUTPUTFILE = "stream.mp4u"; |
|
|
else if(ARG_OUTPUTFILE == NULL && !ARG_OUTPUTTYPE) |
|
|
ARG_OUTPUTFILE = "stream.m4v"; |
|
367 |
|
|
368 |
if((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) { |
if((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) { |
369 |
fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE); |
fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE); |
370 |
goto release_all; |
goto release_all; |
371 |
} |
} |
372 |
|
|
|
/* Write header */ |
|
|
if (ARG_OUTPUTTYPE) { |
|
|
|
|
|
long test = LONG_PACK('M','P','4','U'); |
|
|
|
|
|
test = (!bigendian)?SWAP(test):test; |
|
|
|
|
|
fwrite(&test, sizeof(test), 1, out_file); |
|
|
|
|
|
} |
|
|
|
|
373 |
} |
} |
374 |
else { |
else { |
375 |
out_file = NULL; |
out_file = NULL; |
413 |
&m4v_size, &frame_type, &hints_size); |
&m4v_size, &frame_type, &hints_size); |
414 |
enctime = msecond() - enctime; |
enctime = msecond() - enctime; |
415 |
|
|
416 |
|
/* if it's a not coded VOP (aka NVOP) then we write nothing */ |
417 |
|
if(frame_type == 5) goto next_frame; |
418 |
|
|
419 |
|
{ |
420 |
|
char *type[] = {"P", "I", "B", "S", "Packed", "N", "Unknown"}; |
421 |
|
|
422 |
|
if(frame_type<0 || frame_type>5) frame_type = 6; |
423 |
|
|
424 |
|
printf("Frame %5d: type = %s, enctime(ms) =%6.1f, length(bytes) =%7d\n", |
425 |
|
(int)filenr, type[frame_type], (float)enctime, (int)m4v_size); |
426 |
|
|
427 |
|
} |
428 |
|
|
429 |
|
/* Update encoding time stats */ |
430 |
totalenctime += enctime; |
totalenctime += enctime; |
431 |
totalsize += m4v_size; |
totalsize += m4v_size; |
432 |
|
|
|
printf("Frame %5d: intra %1d, enctime=%6.1f ms, size=%6dbytes\n", |
|
|
(int)filenr, (int)frame_type, (float)enctime, (int)m4v_size); |
|
|
|
|
433 |
/***************************************************************************** |
/***************************************************************************** |
434 |
* Save hints to file |
* Save hints to file |
435 |
****************************************************************************/ |
****************************************************************************/ |
456 |
out_file = NULL; |
out_file = NULL; |
457 |
} |
} |
458 |
else { |
else { |
|
/* Using mp4u container */ |
|
|
if (ARG_OUTPUTTYPE) { |
|
|
long size = m4v_size; |
|
|
size = (!bigendian)?SWAP(size):size; |
|
|
fwrite(&size, sizeof(size), 1, out_file); |
|
|
} |
|
459 |
|
|
460 |
/* Write mp4 data */ |
/* Write mp4 data */ |
461 |
fwrite(mp4_buffer, m4v_size, 1, out_file); |
fwrite(mp4_buffer, 1, m4v_size, out_file); |
462 |
|
|
463 |
} |
} |
464 |
} |
} |
465 |
|
|
466 |
|
next_frame: |
467 |
/* Read the header if it's pgm stream */ |
/* Read the header if it's pgm stream */ |
468 |
if (ARG_INPUTTYPE) |
if (ARG_INPUTTYPE) |
469 |
status = read_pgmheader(in_file); |
status = read_pgmheader(in_file); |
470 |
|
|
471 |
filenr++; |
if(frame_type != 5) filenr++; |
472 |
|
|
473 |
} while ( (!status) && (filenr<ARG_MAXFRAMENR) ); |
} while ( (!status) && (filenr<ARG_MAXFRAMENR) ); |
474 |
|
|
481 |
totalsize /= filenr; |
totalsize /= filenr; |
482 |
totalenctime /= filenr; |
totalenctime /= filenr; |
483 |
|
|
484 |
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", |
485 |
totalenctime, 1000/totalenctime, (int)totalsize); |
totalenctime, 1000/totalenctime, (int)totalsize); |
486 |
|
|
487 |
/***************************************************************************** |
/***************************************************************************** |
526 |
/* Return time elapsed time in miliseconds since the program started */ |
/* Return time elapsed time in miliseconds since the program started */ |
527 |
static double msecond() |
static double msecond() |
528 |
{ |
{ |
529 |
#ifndef _MSC_VER |
#ifndef WIN32 |
530 |
struct timeval tv; |
struct timeval tv; |
531 |
gettimeofday(&tv, 0); |
gettimeofday(&tv, 0); |
532 |
return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3; |
return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3; |
549 |
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
550 |
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
551 |
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
552 |
|
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
553 |
|
fprintf(stderr, " -bn integer : max bframes (default=0)\n"); |
554 |
|
fprintf(stderr, " -bqr integer : bframe quantizer ratio (default=150)\n"); |
555 |
|
fprintf(stderr, " -bqo integer : bframe quantizer offset (default=100)\n"); |
556 |
fprintf(stderr, " -f float : target framerate (>0)\n"); |
fprintf(stderr, " -f float : target framerate (>0)\n"); |
557 |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
558 |
fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n"); |
fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n"); |
562 |
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"); |
563 |
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"); |
564 |
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"); |
565 |
fprintf(stderr, " When this option is used :\n"); |
fprintf(stderr, " When this option is used : save to 'string' (default=stream.m4v)\n"); |
|
fprintf(stderr, " + stream.m4v with -mt 0\n"); |
|
|
fprintf(stderr, " + stream.mp4u with -mt 1\n"); |
|
566 |
fprintf(stderr, " -mt integer : output type (m4v=0, mp4u=1)\n"); |
fprintf(stderr, " -mt integer : output type (m4v=0, mp4u=1)\n"); |
567 |
fprintf(stderr, " -mv integer : Use motion vector hints (no hints=0, get hints=1, set hints=2)\n"); |
fprintf(stderr, " -mv integer : Use motion vector hints (no hints=0, get hints=1, set hints=2)\n"); |
568 |
fprintf(stderr, " -help : prints this help message\n"); |
fprintf(stderr, " -help : prints this help message\n"); |
660 |
|
|
661 |
if(use_assembler) { |
if(use_assembler) { |
662 |
|
|
663 |
#ifdef ARCH_IA64 |
#ifdef ARCH_IS_IA64 |
664 |
xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
665 |
#else |
#else |
666 |
xinit.cpu_flags = 0; |
xinit.cpu_flags = 0; |
691 |
xparam.min_quantizer = ARG_MINQUANT; |
xparam.min_quantizer = ARG_MINQUANT; |
692 |
xparam.max_quantizer = ARG_MAXQUANT; |
xparam.max_quantizer = ARG_MAXQUANT; |
693 |
xparam.max_key_interval = (int)ARG_FRAMERATE*10; |
xparam.max_key_interval = (int)ARG_FRAMERATE*10; |
694 |
|
xparam.bquant_ratio = ARG_BQRATIO; |
695 |
|
xparam.bquant_offset = ARG_BQOFFSET; |
696 |
|
xparam.max_bframes = ARG_MAXBFRAMES; |
697 |
|
xparam.frame_drop_ratio = 0; |
698 |
|
xparam.global = 0; |
699 |
|
|
700 |
/* I use a small value here, since will not encode whole movies, but short clips */ |
/* I use a small value here, since will not encode whole movies, but short clips */ |
701 |
|
|
732 |
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) */ |
733 |
|
|
734 |
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) */ |
735 |
|
xframe.bquant = 0; |
736 |
|
|
737 |
xframe.motion = motion_presets[ARG_QUALITY]; |
xframe.motion = motion_presets[ARG_QUALITY]; |
738 |
xframe.general = general_presets[ARG_QUALITY]; |
xframe.general = general_presets[ARG_QUALITY]; |
739 |
xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL; |
xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL; |
740 |
|
xframe.stride = XDIM; |
741 |
|
|
742 |
xframe.hint.hintstream = hints_buffer; |
xframe.hint.hintstream = hints_buffer; |
743 |
|
|