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.11.2.4 2003-03-11 23:39:47 edgomez Exp $ |
* $Id: xvid_encraw.c,v 1.11.2.5 2003-03-13 11:07:20 suxen_drol Exp $ |
23 |
* |
* |
24 |
****************************************************************************/ |
****************************************************************************/ |
25 |
|
|
101 |
static int ARG_BQRATIO = 150; |
static int ARG_BQRATIO = 150; |
102 |
static int ARG_BQOFFSET = 100; |
static int ARG_BQOFFSET = 100; |
103 |
static int ARG_MAXBFRAMES = 0; |
static int ARG_MAXBFRAMES = 0; |
104 |
|
static int ARG_PACKED = 0; |
105 |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
106 |
|
|
107 |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
142 |
static int enc_stop(); |
static int enc_stop(); |
143 |
static int enc_main(unsigned char* image, |
static int enc_main(unsigned char* image, |
144 |
unsigned char* bitstream, |
unsigned char* bitstream, |
145 |
long *frametype, |
int *key, |
146 |
|
int *stats_type, |
147 |
|
int *stats_quant, |
148 |
|
int *stats_length, |
149 |
int stats[3]); |
int stats[3]); |
150 |
|
|
151 |
/***************************************************************************** |
/***************************************************************************** |
162 |
double enctime; |
double enctime; |
163 |
double totalenctime=0.; |
double totalenctime=0.; |
164 |
|
|
165 |
long totalsize; |
int totalsize; |
166 |
int status; |
int status; |
167 |
long frame_type; |
int m4v_size; |
168 |
|
int key; |
169 |
long m4v_size; |
int stats_type; |
170 |
|
int stats_quant; |
171 |
|
int stats_length; |
172 |
int use_assembler=0; |
int use_assembler=0; |
173 |
|
|
174 |
char filename[256]; |
char filename[256]; |
204 |
i++; |
i++; |
205 |
ARG_MAXBFRAMES = atoi(argv[i]); |
ARG_MAXBFRAMES = atoi(argv[i]); |
206 |
} |
} |
207 |
|
else if (strcmp("-p", argv[i]) == 0) { |
208 |
|
ARG_PACKED = 1; |
209 |
|
} |
210 |
else if (strcmp("-bqr", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-bqr", argv[i]) == 0 && i < argc - 1 ) { |
211 |
i++; |
i++; |
212 |
ARG_BQRATIO = atoi(argv[i]); |
ARG_BQRATIO = atoi(argv[i]); |
375 |
****************************************************************************/ |
****************************************************************************/ |
376 |
|
|
377 |
enctime = msecond(); |
enctime = msecond(); |
378 |
m4v_size = enc_main(in_buffer, mp4_buffer, &frame_type, stats); |
m4v_size = enc_main(in_buffer, mp4_buffer, &key, &stats_type, &stats_quant, &stats_length, stats); |
379 |
enctime = msecond() - enctime; |
enctime = msecond() - enctime; |
380 |
|
|
|
/* Not coded frames return 0 */ |
|
|
if(m4v_size == 0) goto next_frame; |
|
|
|
|
381 |
/* Write the Frame statistics */ |
/* Write the Frame statistics */ |
382 |
switch(frame_type) { |
|
383 |
|
printf("Frame %5d: key=%i, time(ms)=%6.1f, length=%7d", |
384 |
|
(int)filenr, |
385 |
|
key, |
386 |
|
(float)enctime, |
387 |
|
(int)m4v_size); |
388 |
|
|
389 |
|
if (stats_type > 0) { /* !XVID_TYPE_NOTHING */ |
390 |
|
|
391 |
|
switch(stats_type) { |
392 |
case XVID_TYPE_IVOP: |
case XVID_TYPE_IVOP: |
393 |
type = "I"; |
type = "I"; |
394 |
break; |
break; |
401 |
case XVID_TYPE_SVOP: |
case XVID_TYPE_SVOP: |
402 |
type = "S"; |
type = "S"; |
403 |
break; |
break; |
|
case XVID_TYPE_NOTHING: |
|
|
type = "N"; |
|
|
break; |
|
404 |
default: |
default: |
405 |
type = "U"; |
type = "U"; |
406 |
break; |
break; |
407 |
} |
} |
408 |
|
|
409 |
printf("Frame %5d: type = %s, enctime(ms) =%6.1f, length(bytes) =%7d", |
printf(" | type=%s quant=%2d, length=%7d", type, stats_quant, stats_length); |
|
(int)filenr, |
|
|
type, |
|
|
(float)enctime, |
|
|
(int)m4v_size); |
|
410 |
|
|
411 |
if(ARG_STATS) { |
if(ARG_STATS) { |
412 |
printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f", |
printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f", |
414 |
(stats[1] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[1]/((float)(XDIM)*(YDIM)/4)), |
(stats[1] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[1]/((float)(XDIM)*(YDIM)/4)), |
415 |
(stats[2] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[2]/((float)(XDIM)*(YDIM)/4))); |
(stats[2] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[2]/((float)(XDIM)*(YDIM)/4))); |
416 |
} |
} |
417 |
|
} |
418 |
|
|
419 |
printf("\n"); |
printf("\n"); |
420 |
|
|
421 |
|
|
422 |
/* Update encoding time stats */ |
/* Update encoding time stats */ |
423 |
totalenctime += enctime; |
totalenctime += enctime; |
424 |
totalsize += m4v_size; |
totalsize += m4v_size; |
425 |
|
|
426 |
|
/* Not coded frames return 0 */ |
427 |
|
if(m4v_size == 0) goto next_frame; |
428 |
|
|
429 |
|
|
430 |
/***************************************************************************** |
/***************************************************************************** |
431 |
* Save stream to file |
* Save stream to file |
432 |
****************************************************************************/ |
****************************************************************************/ |
534 |
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
535 |
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
536 |
fprintf(stderr, " -bn integer : max bframes (default=0)\n"); |
fprintf(stderr, " -bn integer : max bframes (default=0)\n"); |
537 |
|
fprintf(stderr, " -p : packed mode\n"); |
538 |
fprintf(stderr, " -bqr integer : bframe quantizer ratio (default=150)\n"); |
fprintf(stderr, " -bqr integer : bframe quantizer ratio (default=150)\n"); |
539 |
fprintf(stderr, " -bqo integer : bframe quantizer offset (default=100)\n"); |
fprintf(stderr, " -bqo integer : bframe quantizer offset (default=100)\n"); |
540 |
fprintf(stderr, " -f float : target framerate (>0)\n"); |
fprintf(stderr, " -f float : target framerate (>0)\n"); |
631 |
* Routines for encoding: init encoder, frame step, release encoder |
* Routines for encoding: init encoder, frame step, release encoder |
632 |
****************************************************************************/ |
****************************************************************************/ |
633 |
|
|
634 |
|
/* sample plugin */ |
635 |
|
|
636 |
|
int rawenc_debug(void * handle, int opt, void * param1, void * param2) |
637 |
|
{ |
638 |
|
switch(opt) |
639 |
|
{ |
640 |
|
case XVID_PLG_INFO : |
641 |
|
case XVID_PLG_CREATE : |
642 |
|
case XVID_PLG_DESTROY : |
643 |
|
case XVID_PLG_BEFORE : |
644 |
|
return 0; |
645 |
|
|
646 |
|
case XVID_PLG_AFTER : |
647 |
|
{ |
648 |
|
xvid_plg_data_t * data = (xvid_plg_data_t*)param1; |
649 |
|
printf("type=%i, quant=%i, length=%i\n", data->type, data->quant, data->length); |
650 |
|
return 0; |
651 |
|
} |
652 |
|
} |
653 |
|
|
654 |
|
return XVID_ERR_FAIL; |
655 |
|
} |
656 |
|
|
657 |
|
|
658 |
#define FRAMERATE_INCR 1001 |
#define FRAMERATE_INCR 1001 |
659 |
|
|
660 |
/* Initialize encoder for first use, pass all needed parameters to the codec */ |
/* Initialize encoder for first use, pass all needed parameters to the codec */ |
662 |
{ |
{ |
663 |
int xerr; |
int xerr; |
664 |
|
|
665 |
|
/* xvid_enc_plugin_t plugins[1]; */ |
666 |
|
|
667 |
xvid_gbl_init_t xvid_gbl_init; |
xvid_gbl_init_t xvid_gbl_init; |
668 |
xvid_enc_create_t xvid_enc_create; |
xvid_enc_create_t xvid_enc_create; |
669 |
|
|
672 |
*----------------------------------------------------------------------*/ |
*----------------------------------------------------------------------*/ |
673 |
|
|
674 |
/* Set version -- version checking will done by xvidcore*/ |
/* Set version -- version checking will done by xvidcore*/ |
675 |
|
memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init)); |
676 |
xvid_gbl_init.version = XVID_VERSION; |
xvid_gbl_init.version = XVID_VERSION; |
677 |
|
|
678 |
|
|
679 |
/* Do we have to enable ASM optimizations ? */ |
/* Do we have to enable ASM optimizations ? */ |
680 |
if(use_assembler) { |
if(use_assembler) { |
681 |
|
|
697 |
*----------------------------------------------------------------------*/ |
*----------------------------------------------------------------------*/ |
698 |
|
|
699 |
/* Version again */ |
/* Version again */ |
700 |
|
memset(&xvid_enc_create, 0, sizeof(xvid_enc_create)); |
701 |
xvid_enc_create.version = XVID_VERSION; |
xvid_enc_create.version = XVID_VERSION; |
702 |
|
|
703 |
/* Width and Height of input frames */ |
/* Width and Height of input frames */ |
704 |
xvid_enc_create.width = XDIM; |
xvid_enc_create.width = XDIM; |
705 |
xvid_enc_create.height = YDIM; |
xvid_enc_create.height = YDIM; |
706 |
|
|
707 |
|
/* init plugins |
708 |
|
plugins[0].func = rawenc_debug; |
709 |
|
plugins[0].param = NULL; |
710 |
|
xvid_enc_create.num_plugins = 1; |
711 |
|
xvid_enc_create.plugins = plugins; */ |
712 |
|
|
713 |
/* No fancy thread tests */ |
/* No fancy thread tests */ |
714 |
xvid_enc_create.num_threads = 0; |
xvid_enc_create.num_threads = 0; |
715 |
|
|
734 |
xvid_enc_create.frame_drop_ratio = 0; |
xvid_enc_create.frame_drop_ratio = 0; |
735 |
|
|
736 |
/* Global encoder options */ |
/* Global encoder options */ |
737 |
xvid_enc_create.global = (ARG_STATS)?XVID_EXTRASTATS_ENABLE:0; |
xvid_enc_create.global = 0; |
738 |
|
if (ARG_STATS) xvid_enc_create.global |= XVID_EXTRASTATS_ENABLE; |
739 |
|
if (ARG_PACKED) xvid_enc_create.global |= XVID_PACKED; |
740 |
|
|
741 |
/* 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 */ |
742 |
xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); |
xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); |
759 |
|
|
760 |
static int enc_main(unsigned char* image, |
static int enc_main(unsigned char* image, |
761 |
unsigned char* bitstream, |
unsigned char* bitstream, |
762 |
long *frametype, |
int * key, |
763 |
|
int *stats_type, |
764 |
|
int *stats_quant, |
765 |
|
int *stats_length, |
766 |
int stats[3]) |
int stats[3]) |
767 |
{ |
{ |
768 |
int ret; |
int ret; |
769 |
|
|
770 |
xvid_enc_frame_t xvid_enc_frame; |
xvid_enc_frame_t xvid_enc_frame; |
771 |
xvid_enc_stats_t xvid_enc_stats[2]; |
xvid_enc_stats_t xvid_enc_stats; |
772 |
|
|
773 |
/* Version for the frame and the stats */ |
/* Version for the frame and the stats */ |
774 |
|
memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame)); |
775 |
xvid_enc_frame.version = XVID_VERSION; |
xvid_enc_frame.version = XVID_VERSION; |
776 |
xvid_enc_stats[0].version = XVID_VERSION; |
|
777 |
xvid_enc_stats[1].version = XVID_VERSION; |
memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats)); |
778 |
|
xvid_enc_stats.version = XVID_VERSION; |
779 |
|
|
780 |
/* Bind output buffer */ |
/* Bind output buffer */ |
781 |
xvid_enc_frame.bitstream = bitstream; |
xvid_enc_frame.bitstream = bitstream; |
806 |
xvid_enc_frame.quant_intra_matrix = NULL; |
xvid_enc_frame.quant_intra_matrix = NULL; |
807 |
xvid_enc_frame.quant_inter_matrix = NULL; |
xvid_enc_frame.quant_inter_matrix = NULL; |
808 |
|
|
|
/* Foll proof */ |
|
|
xvid_enc_stats[0].sse_y = 0; |
|
|
xvid_enc_stats[0].sse_v = 0; |
|
|
xvid_enc_stats[0].sse_u = 0; |
|
|
|
|
809 |
/* Encode the frame */ |
/* Encode the frame */ |
810 |
xvid_enc_frame.vop_flags |= (ARG_STATS)?XVID_EXTRASTATS:0; |
xvid_enc_frame.vop_flags |= (ARG_STATS)?XVID_EXTRASTATS:0; |
811 |
ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame, &xvid_enc_stats); |
ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame, &xvid_enc_stats); |
812 |
|
|
813 |
*frametype = xvid_enc_stats[0].type; |
*key = (xvid_enc_frame.out_flags & XVID_KEYFRAME); |
814 |
stats[0] = xvid_enc_stats[0].sse_y; |
*stats_type = xvid_enc_stats.type; |
815 |
stats[1] = xvid_enc_stats[0].sse_u; |
*stats_quant = xvid_enc_stats.quant; |
816 |
stats[2] = xvid_enc_stats[0].sse_v; |
*stats_length = xvid_enc_stats.length; |
817 |
|
stats[0] = xvid_enc_stats.sse_y; |
818 |
|
stats[1] = xvid_enc_stats.sse_u; |
819 |
|
stats[2] = xvid_enc_stats.sse_v; |
820 |
|
|
821 |
return(ret); |
return(ret); |
822 |
} |
} |