21 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
22 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
* |
* |
24 |
* $Id: xvid_encraw.c,v 1.20 2005-08-01 10:53:46 Isibaar Exp $ |
* $Id: xvid_encraw.c,v 1.22 2005-10-07 15:02:28 suxen_drol Exp $ |
25 |
* |
* |
26 |
****************************************************************************/ |
****************************************************************************/ |
27 |
|
|
45 |
#ifndef WIN32 |
#ifndef WIN32 |
46 |
#include <sys/time.h> |
#include <sys/time.h> |
47 |
#else |
#else |
48 |
|
#include <windows.h> |
49 |
|
#include <vfw.h> |
50 |
#include <time.h> |
#include <time.h> |
51 |
|
#define XVID_AVI_INPUT |
52 |
#endif |
#endif |
53 |
|
|
54 |
#include "xvid.h" |
#include "xvid.h" |
155 |
static int ARG_DEBUG = 0; |
static int ARG_DEBUG = 0; |
156 |
static int ARG_VOPDEBUG = 0; |
static int ARG_VOPDEBUG = 0; |
157 |
static int ARG_GREYSCALE = 0; |
static int ARG_GREYSCALE = 0; |
158 |
|
static int ARG_QTYPE = 0; |
159 |
|
static int ARG_QMATRIX = 0; |
160 |
static int ARG_GMC = 0; |
static int ARG_GMC = 0; |
161 |
static int ARG_INTERLACING = 0; |
static int ARG_INTERLACING = 0; |
162 |
static int ARG_QPEL = 0; |
static int ARG_QPEL = 0; |
163 |
|
static int ARG_TURBO = 0; |
164 |
static int ARG_VHQMODE = 0; |
static int ARG_VHQMODE = 0; |
165 |
|
static int ARG_BVHQ = 0; |
166 |
static int ARG_CLOSED_GOP = 0; |
static int ARG_CLOSED_GOP = 0; |
167 |
|
|
168 |
#ifndef READ_PNM |
#ifndef READ_PNM |
189 |
/* Internal structures (handles) for encoding and decoding */ |
/* Internal structures (handles) for encoding and decoding */ |
190 |
static void *enc_handle = NULL; |
static void *enc_handle = NULL; |
191 |
|
|
192 |
|
static unsigned char qmatrix_intra[64]; |
193 |
|
static unsigned char qmatrix_inter[64]; |
194 |
|
|
195 |
|
#ifdef XVID_AVI_INPUT |
196 |
|
static PAVISTREAM avi_stream = NULL; |
197 |
|
#endif |
198 |
|
|
199 |
/***************************************************************************** |
/***************************************************************************** |
200 |
* Local prototypes |
* Local prototypes |
201 |
****************************************************************************/ |
****************************************************************************/ |
353 |
} else if (strcmp("-frames", argv[i]) == 0 && i < argc - 1) { |
} else if (strcmp("-frames", argv[i]) == 0 && i < argc - 1) { |
354 |
i++; |
i++; |
355 |
ARG_MAXFRAMENR = atoi(argv[i]); |
ARG_MAXFRAMENR = atoi(argv[i]); |
356 |
|
} else if (strcmp("-qtype", argv[i]) == 0 && i < argc - 1) { |
357 |
|
i++; |
358 |
|
ARG_QTYPE = atoi(argv[i]); |
359 |
|
} else if (strcmp("-qmatrix", argv[i]) == 0 && i < argc - 1) { |
360 |
|
FILE *fp = fopen(argv[++i], "rb"); |
361 |
|
if (fp == NULL) { |
362 |
|
fprintf(stderr, "Error opening input file %s\n", argv[i]); |
363 |
|
return (-1); |
364 |
|
} |
365 |
|
fseek(fp, 0, SEEK_END); |
366 |
|
if (ftell(fp) != 128) { |
367 |
|
fprintf(stderr, "Unexpected size of input file %s\n", argv[i]); |
368 |
|
return (-1); |
369 |
|
} |
370 |
|
|
371 |
|
fseek(fp, 0, SEEK_SET); |
372 |
|
fread(qmatrix_intra, 1, 64, fp); |
373 |
|
fread(qmatrix_inter, 1, 64, fp); |
374 |
|
|
375 |
|
ARG_QMATRIX = 1; |
376 |
} else if (strcmp("-save", argv[i]) == 0) { |
} else if (strcmp("-save", argv[i]) == 0) { |
377 |
ARG_SAVEMPEGSTREAM = 1; |
ARG_SAVEMPEGSTREAM = 1; |
378 |
ARG_SAVEINDIVIDUAL = 1; |
ARG_SAVEINDIVIDUAL = 1; |
387 |
ARG_VOPDEBUG = 1; |
ARG_VOPDEBUG = 1; |
388 |
} else if (strcmp("-grey", argv[i]) == 0) { |
} else if (strcmp("-grey", argv[i]) == 0) { |
389 |
ARG_GREYSCALE = 1; |
ARG_GREYSCALE = 1; |
390 |
|
} else if (strcmp("-bvhq", argv[i]) == 0) { |
391 |
|
ARG_BVHQ = 1; |
392 |
} else if (strcmp("-qpel", argv[i]) == 0) { |
} else if (strcmp("-qpel", argv[i]) == 0) { |
393 |
ARG_QPEL = 1; |
ARG_QPEL = 1; |
394 |
|
} else if (strcmp("-turbo", argv[i]) == 0) { |
395 |
|
ARG_TURBO = 1; |
396 |
} else if (strcmp("-gmc", argv[i]) == 0) { |
} else if (strcmp("-gmc", argv[i]) == 0) { |
397 |
ARG_GMC = 1; |
ARG_GMC = 1; |
398 |
} else if (strcmp("-interlaced", argv[i]) == 0) { |
} else if (strcmp("-interlaced", argv[i]) == 0) { |
415 |
|
|
416 |
if (XDIM <= 0 || XDIM >= 4096 || YDIM <= 0 || YDIM >= 4096) { |
if (XDIM <= 0 || XDIM >= 4096 || YDIM <= 0 || YDIM >= 4096) { |
417 |
fprintf(stderr, |
fprintf(stderr, |
418 |
"Trying to retreive width and height from PGM header\n"); |
"Trying to retrieve width and height from input header\n"); |
419 |
|
if (!ARG_INPUTTYPE) |
420 |
ARG_INPUTTYPE = 1; /* pgm */ |
ARG_INPUTTYPE = 1; /* pgm */ |
421 |
} |
} |
422 |
|
|
439 |
if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
440 |
in_file = stdin; |
in_file = stdin; |
441 |
} else { |
} else { |
442 |
|
#ifdef XVID_AVI_INPUT |
443 |
|
if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 || |
444 |
|
strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 || |
445 |
|
ARG_INPUTTYPE==2) |
446 |
|
{ |
447 |
|
AVISTREAMINFO avi_info; |
448 |
|
|
449 |
|
AVIFileInit(); |
450 |
|
if (AVIStreamOpenFromFile(&avi_stream, ARG_INPUTFILE, streamtypeVIDEO, 0, OF_READ, NULL) != AVIERR_OK) { |
451 |
|
fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE); |
452 |
|
AVIFileExit(); |
453 |
|
return (-1); |
454 |
|
} |
455 |
|
|
456 |
|
if(AVIStreamInfo(avi_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) { |
457 |
|
fprintf(stderr, "Can't get stream info from file '%s'!\n", ARG_INPUTFILE); |
458 |
|
AVIStreamRelease(avi_stream); |
459 |
|
AVIFileExit(); |
460 |
|
return (-1); |
461 |
|
} |
462 |
|
|
463 |
|
if (avi_info.fccHandler != MAKEFOURCC('Y', 'V', '1', '2')) { |
464 |
|
fprintf(stderr, "Unsupported input colorspace! Only YV12 is supported!\n"); |
465 |
|
AVIStreamRelease(avi_stream); |
466 |
|
AVIFileExit(); |
467 |
|
return (-1); |
468 |
|
} |
469 |
|
|
470 |
|
ARG_MAXFRAMENR = min(ARG_MAXFRAMENR, avi_info.dwLength); |
471 |
|
|
472 |
|
XDIM = avi_info.rcFrame.right - avi_info.rcFrame.left; |
473 |
|
YDIM = avi_info.rcFrame.bottom - avi_info.rcFrame.top; |
474 |
|
ARG_FRAMERATE = (float) avi_info.dwRate / (float) avi_info.dwScale; |
475 |
|
|
476 |
|
ARG_INPUTTYPE = 2; |
477 |
|
} |
478 |
|
else |
479 |
|
#endif |
480 |
|
{ |
481 |
in_file = fopen(ARG_INPUTFILE, "rb"); |
in_file = fopen(ARG_INPUTFILE, "rb"); |
482 |
if (in_file == NULL) { |
if (in_file == NULL) { |
483 |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
484 |
return (-1); |
return (-1); |
485 |
} |
} |
486 |
} |
} |
487 |
|
} |
488 |
|
|
489 |
if (ARG_INPUTTYPE) { |
if (ARG_INPUTTYPE==1) { |
490 |
#ifndef READ_PNM |
#ifndef READ_PNM |
491 |
if (read_pgmheader(in_file)) { |
if (read_pgmheader(in_file)) { |
492 |
#else |
#else |
555 |
} |
} |
556 |
|
|
557 |
if (!result) { |
if (!result) { |
558 |
if (ARG_INPUTTYPE) { |
#ifdef XVID_AVI_INPUT |
559 |
|
if (ARG_INPUTTYPE==2) { |
560 |
|
/* read avs/avi data (YUV-format) */ |
561 |
|
if(AVIStreamRead(avi_stream, input_num, 1, in_buffer, IMAGE_SIZE(XDIM, YDIM), NULL, NULL ) != AVIERR_OK) |
562 |
|
result = 1; |
563 |
|
} else |
564 |
|
#endif |
565 |
|
if (ARG_INPUTTYPE==1) { |
566 |
/* read PGM data (YUV-format) */ |
/* read PGM data (YUV-format) */ |
567 |
#ifndef READ_PNM |
#ifndef READ_PNM |
568 |
result = read_pgmdata(in_file, in_buffer); |
result = read_pgmdata(in_file, in_buffer); |
663 |
input_num++; |
input_num++; |
664 |
|
|
665 |
/* Read the header if it's pgm stream */ |
/* Read the header if it's pgm stream */ |
666 |
if (!result && ARG_INPUTTYPE) |
if (!result && (ARG_INPUTTYPE==1)) |
667 |
#ifndef READ_PNM |
#ifndef READ_PNM |
668 |
result = read_pgmheader(in_file); |
result = read_pgmheader(in_file); |
669 |
#else |
#else |
706 |
|
|
707 |
release_all: |
release_all: |
708 |
|
|
709 |
|
#ifdef XVID_AVI_INPUT |
710 |
|
if (avi_stream) { |
711 |
|
AVIStreamRelease(avi_stream); |
712 |
|
AVIFileExit(); |
713 |
|
} |
714 |
|
#endif |
715 |
|
|
716 |
if (enc_handle) { |
if (enc_handle) { |
717 |
result = enc_stop(); |
result = enc_stop(); |
718 |
if (result) |
if (result) |
756 |
clock_t clk; |
clock_t clk; |
757 |
|
|
758 |
clk = clock(); |
clk = clock(); |
759 |
return (clk * 1000 / CLOCKS_PER_SEC); |
return (clk * 1000.0 / CLOCKS_PER_SEC); |
760 |
#endif |
#endif |
761 |
} |
} |
762 |
|
|
770 |
fprintf(stderr, "Usage : xvid_stat [OPTIONS]\n\n"); |
fprintf(stderr, "Usage : xvid_stat [OPTIONS]\n\n"); |
771 |
fprintf(stderr, "Input options:\n"); |
fprintf(stderr, "Input options:\n"); |
772 |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
773 |
|
#ifdef XVID_AVI_INPUT |
774 |
|
fprintf(stderr, " -type integer: input data type (yuv=0, pgm=1, avi/avs=2)\n"); |
775 |
|
#else |
776 |
fprintf(stderr, " -type integer: input data type (yuv=0, pgm=1)\n"); |
fprintf(stderr, " -type integer: input data type (yuv=0, pgm=1)\n"); |
777 |
|
#endif |
778 |
fprintf(stderr, " -w integer: frame width ([1.2048])\n"); |
fprintf(stderr, " -w integer: frame width ([1.2048])\n"); |
779 |
fprintf(stderr, " -h integer: frame height ([1.2048])\n"); |
fprintf(stderr, " -h integer: frame height ([1.2048])\n"); |
780 |
fprintf(stderr, " -frames integer: number of frames to encode\n"); |
fprintf(stderr, " -frames integer: number of frames to encode\n"); |
800 |
fprintf(stderr, " -max_key_interval integer : maximum keyframe interval\n"); |
fprintf(stderr, " -max_key_interval integer : maximum keyframe interval\n"); |
801 |
fprintf(stderr, "\n"); |
fprintf(stderr, "\n"); |
802 |
fprintf(stderr, "Other options\n"); |
fprintf(stderr, "Other options\n"); |
|
fprintf(stderr, " -asm : use assembly optmized code\n"); |
|
803 |
fprintf(stderr, " -noasm : do not use assembly optmized code\n"); |
fprintf(stderr, " -noasm : do not use assembly optmized code\n"); |
804 |
|
fprintf(stderr, " -turbo : use turbo presets for higher encoding speed\n"); |
805 |
fprintf(stderr, " -quality integer: quality ([0..%d])\n", ME_ELEMENTS - 1); |
fprintf(stderr, " -quality integer: quality ([0..%d])\n", ME_ELEMENTS - 1); |
806 |
fprintf(stderr, " -vhqmode integer: level of Rate-Distorsion optimizations ([0..4]) (default=0)\n"); |
fprintf(stderr, " -vhqmode integer : level of Rate-Distortion optimizations ([0..4]) (default=0)\n"); |
807 |
|
fprintf(stderr, " -bvhq : use Rate-Distortion optimizations for B-frames too\n"); |
808 |
fprintf(stderr, " -qpel : use quarter pixel ME\n"); |
fprintf(stderr, " -qpel : use quarter pixel ME\n"); |
809 |
fprintf(stderr, " -gmc : use global motion compensation\n"); |
fprintf(stderr, " -gmc : use global motion compensation\n"); |
810 |
|
fprintf(stderr, " -qtype integer : quantization type (H263:0, MPEG4:1) (default=0)\n"); |
811 |
|
fprintf(stderr, " -qmatrix filename: use custom MPEG4 quantization matrix\n"); |
812 |
fprintf(stderr, " -interlaced : use interlaced encoding (this is NOT a deinterlacer!)\n"); |
fprintf(stderr, " -interlaced : use interlaced encoding (this is NOT a deinterlacer!)\n"); |
813 |
fprintf(stderr, " -packed : packed mode\n"); |
fprintf(stderr, " -packed : packed mode\n"); |
814 |
fprintf(stderr, " -closed_gop : closed GOP mode\n"); |
fprintf(stderr, " -closed_gop : closed GOP mode\n"); |
1195 |
if (image) { |
if (image) { |
1196 |
xvid_enc_frame.input.plane[0] = image; |
xvid_enc_frame.input.plane[0] = image; |
1197 |
#ifndef READ_PNM |
#ifndef READ_PNM |
1198 |
|
if (ARG_INPUTTYPE==2) |
1199 |
|
xvid_enc_frame.input.csp = XVID_CSP_YV12; |
1200 |
|
else |
1201 |
xvid_enc_frame.input.csp = XVID_CSP_I420; |
xvid_enc_frame.input.csp = XVID_CSP_I420; |
1202 |
xvid_enc_frame.input.stride[0] = XDIM; |
xvid_enc_frame.input.stride[0] = XDIM; |
1203 |
#else |
#else |
1212 |
xvid_enc_frame.vol_flags = 0; |
xvid_enc_frame.vol_flags = 0; |
1213 |
if (ARG_STATS) |
if (ARG_STATS) |
1214 |
xvid_enc_frame.vol_flags |= XVID_VOL_EXTRASTATS; |
xvid_enc_frame.vol_flags |= XVID_VOL_EXTRASTATS; |
1215 |
|
if (ARG_QTYPE) |
1216 |
|
xvid_enc_frame.vol_flags |= XVID_VOL_MPEGQUANT; |
1217 |
if (ARG_QPEL) |
if (ARG_QPEL) |
1218 |
xvid_enc_frame.vol_flags |= XVID_VOL_QUARTERPEL; |
xvid_enc_frame.vol_flags |= XVID_VOL_QUARTERPEL; |
1219 |
if (ARG_GMC) |
if (ARG_GMC) |
1249 |
if (ARG_QPEL && (xvid_enc_frame.vop_flags & XVID_VOP_INTER4V)) |
if (ARG_QPEL && (xvid_enc_frame.vop_flags & XVID_VOP_INTER4V)) |
1250 |
xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE8; |
xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE8; |
1251 |
|
|
1252 |
|
if (ARG_TURBO) |
1253 |
|
xvid_enc_frame.motion |= XVID_ME_FASTREFINE16 | XVID_ME_FASTREFINE8 | |
1254 |
|
XVID_ME_SKIP_DELTASEARCH | XVID_ME_FAST_MODEINTERPOLATE | |
1255 |
|
XVID_ME_BFRAME_EARLYSTOP; |
1256 |
|
|
1257 |
|
if (ARG_BVHQ) |
1258 |
|
xvid_enc_frame.vop_flags |= XVID_VOP_RD_BVOP; |
1259 |
|
|
1260 |
switch (ARG_VHQMODE) /* this is the same code as for vfw */ |
switch (ARG_VHQMODE) /* this is the same code as for vfw */ |
1261 |
{ |
{ |
1262 |
case 1: /* VHQ_MODE_DECISION */ |
case 1: /* VHQ_MODE_DECISION */ |
1292 |
break; |
break; |
1293 |
} |
} |
1294 |
|
|
1295 |
|
if (ARG_QMATRIX) { |
1296 |
|
/* We don't use special matrices */ |
1297 |
|
xvid_enc_frame.quant_intra_matrix = qmatrix_intra; |
1298 |
|
xvid_enc_frame.quant_inter_matrix = qmatrix_inter; |
1299 |
|
} |
1300 |
|
else { |
1301 |
/* We don't use special matrices */ |
/* We don't use special matrices */ |
1302 |
xvid_enc_frame.quant_intra_matrix = NULL; |
xvid_enc_frame.quant_intra_matrix = NULL; |
1303 |
xvid_enc_frame.quant_inter_matrix = NULL; |
xvid_enc_frame.quant_inter_matrix = NULL; |
1304 |
|
} |
1305 |
|
|
1306 |
/* Encode the frame */ |
/* Encode the frame */ |
1307 |
ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame, |
ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame, |