comparison libmpcodecs/ve_lavc.c @ 8347:03ee1d7deba5

PSNR
author michael
date Wed, 04 Dec 2002 21:46:53 +0000
parents fd670708f87f
children 345a539683da
comparison
equal deleted inserted replaced
8346:368019e0153a 8347:03ee1d7deba5
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <stdlib.h> 2 #include <stdlib.h>
3 #include <string.h> 3 #include <string.h>
4 #include <math.h> 4 #include <math.h>
5 #include <time.h>
5 6
6 #include "../config.h" 7 #include "../config.h"
7 8
8 #ifdef USE_LIBAVCODEC 9 #ifdef USE_LIBAVCODEC
9 10
92 static int lavc_param_normalize_aqp= 0; 93 static int lavc_param_normalize_aqp= 0;
93 static int lavc_param_interlaced_dct= 0; 94 static int lavc_param_interlaced_dct= 0;
94 static int lavc_param_prediction_method= FF_PRED_LEFT; 95 static int lavc_param_prediction_method= FF_PRED_LEFT;
95 static char *lavc_param_format="YV12"; 96 static char *lavc_param_format="YV12";
96 static int lavc_param_debug= 0; 97 static int lavc_param_debug= 0;
98 static int lavc_param_psnr= 0;
97 99
98 #include "cfgparser.h" 100 #include "cfgparser.h"
99 101
100 #ifdef USE_LIBAVCODEC 102 #ifdef USE_LIBAVCODEC
101 struct config lavcopts_conf[]={ 103 struct config lavcopts_conf[]={
150 {"pred", &lavc_param_prediction_method, CONF_TYPE_INT, CONF_RANGE, 0, 20, NULL}, 152 {"pred", &lavc_param_prediction_method, CONF_TYPE_INT, CONF_RANGE, 0, 20, NULL},
151 {"format", &lavc_param_format, CONF_TYPE_STRING, 0, 0, 0, NULL}, 153 {"format", &lavc_param_format, CONF_TYPE_STRING, 0, 0, 0, NULL},
152 #if LIBAVCODEC_BUILD >= 4642 154 #if LIBAVCODEC_BUILD >= 4642
153 {"debug", &lavc_param_debug, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL}, 155 {"debug", &lavc_param_debug, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL},
154 #endif 156 #endif
157 #if LIBAVCODEC_BUILD >= 4643
158 {"psnr", &lavc_param_psnr, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PSNR, NULL},
159 #endif
155 {NULL, NULL, 0, 0, 0, 0, NULL} 160 {NULL, NULL, 0, 0, 0, 0, NULL}
156 }; 161 };
157 #endif 162 #endif
158 163
159 struct vf_priv_s { 164 struct vf_priv_s {
304 lavc_venc_context->flags|= lavc_param_data_partitioning; 309 lavc_venc_context->flags|= lavc_param_data_partitioning;
305 if(lavc_param_gray) lavc_venc_context->flags|= CODEC_FLAG_GRAY; 310 if(lavc_param_gray) lavc_venc_context->flags|= CODEC_FLAG_GRAY;
306 311
307 if(lavc_param_normalize_aqp) lavc_venc_context->flags|= CODEC_FLAG_NORMALIZE_AQP; 312 if(lavc_param_normalize_aqp) lavc_venc_context->flags|= CODEC_FLAG_NORMALIZE_AQP;
308 if(lavc_param_interlaced_dct) lavc_venc_context->flags|= CODEC_FLAG_INTERLACED_DCT; 313 if(lavc_param_interlaced_dct) lavc_venc_context->flags|= CODEC_FLAG_INTERLACED_DCT;
314 #if LIBAVCODEC_BUILD >= 4643
315 lavc_venc_context->flags|= lavc_param_psnr;
316 #endif
309 lavc_venc_context->prediction_method= lavc_param_prediction_method; 317 lavc_venc_context->prediction_method= lavc_param_prediction_method;
310 if(!strcasecmp(lavc_param_format, "YV12")) 318 if(!strcasecmp(lavc_param_format, "YV12"))
311 lavc_venc_context->pix_fmt= PIX_FMT_YUV420P; 319 lavc_venc_context->pix_fmt= PIX_FMT_YUV420P;
312 else if(!strcasecmp(lavc_param_format, "422P")) 320 else if(!strcasecmp(lavc_param_format, "422P"))
313 lavc_venc_context->pix_fmt= PIX_FMT_YUV422P; 321 lavc_venc_context->pix_fmt= PIX_FMT_YUV422P;
398 break; 406 break;
399 } 407 }
400 return 0; 408 return 0;
401 } 409 }
402 410
411 static double psnr(double d){
412 if(d==0) return INFINITY;
413 return -10.0*log(d)/log(10);
414 }
415
403 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){ 416 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){
417 const char pict_type_char[5]= {'?', 'I', 'P', 'B', 'S'};
404 int out_size; 418 int out_size;
405 AVVideoFrame *pic= vf->priv->pic; 419 AVVideoFrame *pic= vf->priv->pic;
406 420
407 pic->data[0]=mpi->planes[0]; 421 pic->data[0]=mpi->planes[0];
408 pic->data[1]=mpi->planes[1]; 422 pic->data[1]=mpi->planes[1];
414 out_size = avcodec_encode_video(lavc_venc_context, mux_v->buffer, mux_v->buffer_size, 428 out_size = avcodec_encode_video(lavc_venc_context, mux_v->buffer, mux_v->buffer_size,
415 pic); 429 pic);
416 430
417 mencoder_write_chunk(mux_v,out_size,lavc_venc_context->coded_picture->key_frame?0x10:0); 431 mencoder_write_chunk(mux_v,out_size,lavc_venc_context->coded_picture->key_frame?0x10:0);
418 432
433 #if LIBAVCODEC_BUILD >= 4643
434 /* store psnr / pict size / type / qscale */
435 if(lavc_param_psnr){
436 static FILE *fvstats=NULL;
437 char filename[20];
438 static long long int all_len=0;
439 static int frame_number=0;
440 static double all_frametime=0.0;
441 AVVideoFrame *pic= lavc_venc_context->coded_picture;
442 double f= lavc_venc_context->width*lavc_venc_context->height*255.0*255.0;
443
444 if(!fvstats) {
445 time_t today2;
446 struct tm *today;
447 today2 = time(NULL);
448 today = localtime(&today2);
449 sprintf(filename, "psnr_%02d%02d%02d.log", today->tm_hour,
450 today->tm_min, today->tm_sec);
451 fvstats = fopen(filename,"w");
452 if(!fvstats) {
453 perror("fopen");
454 lavc_param_psnr=0; // disable block
455 /*exit(1);*/
456 }
457 }
458
459 fprintf(fvstats, "%6d, %2.2f, %6d, %2.2f, %2.2f, %2.2f, %2.2f %c\n",
460 lavc_venc_context->coded_picture->coded_picture_number,
461 lavc_venc_context->coded_picture->quality,
462 out_size,
463 psnr(lavc_venc_context->coded_picture->error[0]/f),
464 psnr(lavc_venc_context->coded_picture->error[1]*4/f),
465 psnr(lavc_venc_context->coded_picture->error[2]*4/f),
466 psnr((lavc_venc_context->coded_picture->error[0]+lavc_venc_context->coded_picture->error[1]+lavc_venc_context->coded_picture->error[2])/(f*1.5)),
467 pict_type_char[lavc_venc_context->coded_picture->pict_type]
468 );
469 }
470 #endif
419 /* store stats if there are any */ 471 /* store stats if there are any */
420 if(lavc_venc_context->stats_out && stats_file) 472 if(lavc_venc_context->stats_out && stats_file)
421 fprintf(stats_file, "%s", lavc_venc_context->stats_out); 473 fprintf(stats_file, "%s", lavc_venc_context->stats_out);
422 return 1; 474 return 1;
423 } 475 }
424 476
425 static void uninit(struct vf_instance_s* vf){ 477 static void uninit(struct vf_instance_s* vf){
478 const char pict_type_char[5]= {'?', 'I', 'P', 'B', 'S'};
479
480 #if LIBAVCODEC_BUILD >= 4643
481 if(lavc_param_psnr){
482 double f= lavc_venc_context->width*lavc_venc_context->height*255.0*255.0;
483
484 f*= lavc_venc_context->coded_picture->coded_picture_number;
485
486 printf("PSNR: Y:%2.2f, Cb:%2.2f, Cr:%2.2f, All:%2.2f\n",
487 psnr(lavc_venc_context->error[0]/f),
488 psnr(lavc_venc_context->error[1]*4/f),
489 psnr(lavc_venc_context->error[2]*4/f),
490 psnr((lavc_venc_context->error[0]+lavc_venc_context->error[1]+lavc_venc_context->error[2])/(f*1.5))
491 );
492 }
493 #endif
494
426 avcodec_close(lavc_venc_context); 495 avcodec_close(lavc_venc_context);
427 496
428 if(stats_file) fclose(stats_file); 497 if(stats_file) fclose(stats_file);
429 498
430 /* free rc_override */ 499 /* free rc_override */