comparison mpegvideo.c @ 1597:4c9165372ab3 libavcodec

noise reduction of dct coefficients
author michael
date Sun, 02 Nov 2003 23:19:47 +0000
parents 3d1d0490e5a6
children 932d306bf1dc
comparison
equal deleted inserted replaced
1596:c1d5491f144a 1597:4c9165372ab3
456 CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int)) 456 CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int))
457 CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t)) 457 CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t))
458 CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t)) 458 CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t))
459 CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*)) 459 CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
460 CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*)) 460 CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
461
462 if(s->avctx->noise_reduction){
463 CHECKED_ALLOCZ(s->dct_error_sum, 2 * 64 * sizeof(int))
464 CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t))
465 }
461 } 466 }
462 CHECKED_ALLOCZ(s->blocks, 64*6*2 * sizeof(DCTELEM)) 467 CHECKED_ALLOCZ(s->blocks, 64*6*2 * sizeof(DCTELEM))
463 468
464 CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture)) 469 CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))
465 470
586 av_freep(&s->q_intra_matrix16); 591 av_freep(&s->q_intra_matrix16);
587 av_freep(&s->q_inter_matrix16); 592 av_freep(&s->q_inter_matrix16);
588 av_freep(&s->blocks); 593 av_freep(&s->blocks);
589 av_freep(&s->input_picture); 594 av_freep(&s->input_picture);
590 av_freep(&s->reordered_input_picture); 595 av_freep(&s->reordered_input_picture);
596 av_freep(&s->dct_error_sum);
597 av_freep(&s->dct_offset);
591 598
592 if(s->picture){ 599 if(s->picture){
593 for(i=0; i<MAX_PICTURE_COUNT; i++){ 600 for(i=0; i<MAX_PICTURE_COUNT; i++){
594 free_picture(s, &s->picture[i]); 601 free_picture(s, &s->picture[i]);
595 } 602 }
1032 1039
1033 assert(0); 1040 assert(0);
1034 return -1; 1041 return -1;
1035 } 1042 }
1036 1043
1044 static void update_noise_reduction(MpegEncContext *s){
1045 int intra, i;
1046
1047 for(intra=0; intra<2; intra++){
1048 if(s->dct_count[intra] > (1<<16)){
1049 for(i=0; i<64; i++){
1050 s->dct_error_sum[intra][i] >>=1;
1051 }
1052 s->dct_count[intra] >>= 1;
1053 }
1054
1055 for(i=0; i<64; i++){
1056 s->dct_offset[intra][i]= (s->avctx->noise_reduction * s->dct_count[intra] + s->dct_error_sum[intra][i]/2) / (s->dct_error_sum[intra][i]+1);
1057 }
1058 }
1059 }
1060
1037 /** 1061 /**
1038 * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded 1062 * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded
1039 */ 1063 */
1040 int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx) 1064 int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
1041 { 1065 {
1134 else if(s->out_format == FMT_H263) 1158 else if(s->out_format == FMT_H263)
1135 s->dct_unquantize = s->dct_unquantize_h263; 1159 s->dct_unquantize = s->dct_unquantize_h263;
1136 else 1160 else
1137 s->dct_unquantize = s->dct_unquantize_mpeg1; 1161 s->dct_unquantize = s->dct_unquantize_mpeg1;
1138 1162
1163 if(s->dct_error_sum){
1164 assert(s->avctx->noise_reduction && s->encoding);
1165
1166 update_noise_reduction(s);
1167 }
1168
1139 #ifdef HAVE_XVMC 1169 #ifdef HAVE_XVMC
1140 if(s->avctx->xvmc_acceleration) 1170 if(s->avctx->xvmc_acceleration)
1141 return XVMC_field_start(s, avctx); 1171 return XVMC_field_start(s, avctx);
1142 #endif 1172 #endif
1143 return 0; 1173 return 0;
4040 s->ptr_lastgob = pbBufPtr(&s->pb); 4070 s->ptr_lastgob = pbBufPtr(&s->pb);
4041 //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif); 4071 //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
4042 } 4072 }
4043 } 4073 }
4044 4074
4075 void ff_denoise_dct(MpegEncContext *s, DCTELEM *block){
4076 const int intra= s->mb_intra;
4077 int i;
4078
4079 for(i=0; i<64; i++){
4080 int level= block[i];
4081
4082 if(level){
4083 if(level>0){
4084 s->dct_error_sum[intra][i] += level;
4085 level -= s->dct_offset[intra][i];
4086 if(level<0) level=0;
4087 }else{
4088 s->dct_error_sum[intra][i] -= level;
4089 level += s->dct_offset[intra][i];
4090 if(level>0) level=0;
4091 }
4092 block[i]= level;
4093 }
4094 }
4095 }
4096
4045 static int dct_quantize_trellis_c(MpegEncContext *s, 4097 static int dct_quantize_trellis_c(MpegEncContext *s,
4046 DCTELEM *block, int n, 4098 DCTELEM *block, int n,
4047 int qscale, int *overflow){ 4099 int qscale, int *overflow){
4048 const int *qmat; 4100 const int *qmat;
4049 const uint8_t *scantable= s->intra_scantable.scantable; 4101 const uint8_t *scantable= s->intra_scantable.scantable;
4068 int left_limit= 0; 4120 int left_limit= 0;
4069 const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6); 4121 const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
4070 const int patch_table= s->out_format == FMT_MPEG1 && !s->mb_intra; 4122 const int patch_table= s->out_format == FMT_MPEG1 && !s->mb_intra;
4071 4123
4072 s->dsp.fdct (block); 4124 s->dsp.fdct (block);
4073 4125
4126 if(s->dct_error_sum)
4127 ff_denoise_dct(s, block);
4128
4074 qmul= qscale*16; 4129 qmul= qscale*16;
4075 qadd= ((qscale-1)|1)*8; 4130 qadd= ((qscale-1)|1)*8;
4076 4131
4077 if (s->mb_intra) { 4132 if (s->mb_intra) {
4078 int q; 4133 int q;
4359 int bias; 4414 int bias;
4360 int max=0; 4415 int max=0;
4361 unsigned int threshold1, threshold2; 4416 unsigned int threshold1, threshold2;
4362 4417
4363 s->dsp.fdct (block); 4418 s->dsp.fdct (block);
4419
4420 if(s->dct_error_sum)
4421 ff_denoise_dct(s, block);
4364 4422
4365 if (s->mb_intra) { 4423 if (s->mb_intra) {
4366 if (!s->h263_aic) { 4424 if (!s->h263_aic) {
4367 if (n < 4) 4425 if (n < 4)
4368 q = s->y_dc_scale; 4426 q = s->y_dc_scale;