~ubuntu-branches/ubuntu/hardy/avidemux/hardy

« back to all changes in this revision

Viewing changes to avidemux/ADM_video/ADM_vidDenoise.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Matvey Kozhev
  • Date: 2007-12-18 13:53:04 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20071218135304-cdqec2lg2bglyz15
Tags: 1:2.4~preview3-0.0ubuntu1
* Upload to Ubuntu. (LP: #163287, LP: #126572)
* debian/changelog: re-added Ubuntu releases.
* debian/control:
  - Require debhelper >= 5.0.51 (for dh_icons) and imagemagick.
  - Build-depend on libsdl1.2-dev instead of libsdl-dev.
  - Build against newer libx264-dev. (LP: #138854)
  - Removed libamrnb-dev, not in Ubuntu yet.
* debian/rules:
  - Install all icon sizes, using convert (upstream installs none).
  - Added missing calls to dh_installmenu, dh_installman, dh_icons and
    dh_desktop.
* debian/menu, debian/avidemux-qt.menu:
  - Corrected package and executable names.
* debian/avidemux-common.install: Install icons.
* debian/avidemux.common.manpages: Install man/avidemux.1.
* debian/links, debian/avidemux-cli.links, debian/avidemux-gtk.links:
  - Link manpages to avidemux.1.gz.
* debian/install, debian/avidemux-qt.install, debian/avidemux-gtk.desktop,
  debian/avidemux-qt.desktop: Install desktop files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
                          ADM_vidDenoise.cpp  -  description
3
 
                             -------------------
4
 
    begin                : Mon Nov 25 2002
5
 
    copyright            : (C) 2002 by mean
6
 
    email                : fixounet@free.fr
7
 
    
8
 
    Denoiser inspired from DNR in transcode
9
 
    Ported to YV12 and simplified
10
 
    
11
 
   Original code  Copyright (C) Gerhard Monzel - November 2001
12
 
 
13
 
    
14
 
 ***************************************************************************/
15
 
 
16
 
/***************************************************************************
17
 
 *                                                                         *
18
 
 *   This program is free software; you can redistribute it and/or modify  *
19
 
 *   it under the terms of the GNU General Public License as published by  *
20
 
 *   the Free Software Foundation; either version 2 of the License, or     *
21
 
 *   (at your option) any later version.                                   *
22
 
 *                                                                         *
23
 
 ***************************************************************************/
24
 
#include <stdio.h>
25
 
#include <stdlib.h>
26
 
#include <string.h>
27
 
#include <ADM_assert.h>
28
 
#include <math.h>
29
 
 
30
 
#include "config.h"
31
 
#include "fourcc.h"
32
 
#include "avio.hxx"
33
 
#include "config.h"
34
 
#include "avi_vars.h"
35
 
 
36
 
 
37
 
#include "ADM_toolkit/toolkit.hxx"
38
 
#include "ADM_editor/ADM_edit.hxx"
39
 
#include "ADM_video/ADM_genvideo.hxx"
40
 
#include "ADM_video/ADM_vidDenoise.h"
41
 
#include "ADM_filter/video_filters.h"
42
 
 
43
 
 
44
 
static FILTER_PARAM denoiseParam={5,{"lumaLock","lumaThreshold","chromaLock","chromaThreshold",
45
 
                                        "sceneChange"}};
46
 
 
47
 
 
48
 
SCRIPT_CREATE(denoise_script,ADMVideoDenoise,denoiseParam);
49
 
 
50
 
uint8_t distMatrix[256][256];
51
 
uint32_t fixMul[16];
52
 
 
53
 
//static uint8_t matrixReady=0;
54
 
//static uint8_t doOnePix(uint8_t *in,uint8_t *out,uint8_t *lock,uint8_t *nb);
55
 
 
56
 
BUILD_CREATE(denoise_create,ADMVideoDenoise);
57
 
char *ADMVideoDenoise::printConf( void )
58
 
{
59
 
        static char buf[50];
60
 
 
61
 
  ADM_assert(_param);   
62
 
        sprintf((char *)buf," Denoise : Lum :%02ld/:%02ld / Chm :%02ld/%02ld",
63
 
                                                                _param->lumaLock,
64
 
                                        _param->lumaThreshold,
65
 
                        _param->chromaLock,
66
 
                        _param->chromaThreshold);
67
 
        return buf;
68
 
}
69
 
void buildDistMatrix( void );
70
 
void buildDistMatrix( void )
71
 
{
72
 
int d;  
73
 
        for(uint32_t y=255;y>0;y--)
74
 
        for(uint32_t x=255;x>0;x--)
75
 
        {
76
 
                  d=x-y;
77
 
                  if(d<0) d=-d;
78
 
                  distMatrix[x][y]=d;
79
 
                
80
 
        }
81
 
 
82
 
         for(int i=1;i<16;i++)
83
 
                        {
84
 
                                        fixMul[i]=(1<<16)/i;
85
 
                        }
86
 
 
87
 
}
88
 
 
89
 
//_______________________________________________________________
90
 
 
91
 
ADMVideoDenoise::ADMVideoDenoise(
92
 
                                                                        AVDMGenericVideoStream *in,CONFcouple *couples)
93
 
{
94
 
 
95
 
 
96
 
        _in=in;         
97
 
        memcpy(&_info,_in->getInfo(),sizeof(_info));                            
98
 
    uint32_t page;
99
 
    
100
 
  _info.encoding=1;
101
 
  
102
 
  page= _in->getInfo()->width*_in->getInfo()->height;
103
 
  
104
 
//  _uncompressed=new uint8_t [page];
105
 
  _uncompressed=new ADMImage(_in->getInfo()->width,_in->getInfo()->height);
106
 
  ADM_assert(_uncompressed);
107
 
  
108
 
 // _locked=new uint8_t [page];
109
 
  _locked=new ADMImage(_in->getInfo()->width,_in->getInfo()->height);
110
 
  ADM_assert(_locked);
111
 
 
112
 
//      _lockcount=new uint8_t [page];
113
 
 
114
 
 
115
 
  
116
 
   _lockcount=new ADMImage(_in->getInfo()->width,_in->getInfo()->height);
117
 
  memset(YPLANE(_lockcount),0,page);  
118
 
  memset(UPLANE(_lockcount),0,page>>2);  
119
 
  memset(VPLANE(_lockcount),0,page>>2);  
120
 
        
121
 
  _param=NULL;
122
 
  
123
 
  if(couples)
124
 
        {
125
 
                        _param=NEW(NOISE_PARAM);
126
 
                        GET(lumaLock);
127
 
                        GET(lumaThreshold);
128
 
                        GET(chromaLock);
129
 
                        GET(chromaThreshold);
130
 
                        GET(sceneChange);
131
 
                 }
132
 
        else
133
 
                {
134
 
                          #define XXX 1
135
 
                          _param=NEW(NOISE_PARAM);
136
 
                          _param->lumaLock=  4*XXX;
137
 
                          _param->lumaThreshold= 10*XXX;
138
 
                          _param->chromaLock=  8*XXX;
139
 
                          _param->chromaThreshold= 16*XXX;
140
 
        _param->sceneChange=  30*XXX;
141
 
                        }
142
 
          _lastFrame=0xfffffff0;        
143
 
}
144
 
 
145
 
 
146
 
uint8_t ADMVideoDenoise::getCoupledConf( CONFcouple **couples)
147
 
{
148
 
 
149
 
                        ADM_assert(_param);
150
 
                        *couples=new CONFcouple(5);
151
 
 
152
 
#define CSET(x)  (*couples)->setCouple((char *)#x,(_param->x))
153
 
        CSET(lumaLock);
154
 
        CSET(lumaThreshold);
155
 
        CSET(chromaLock);
156
 
        CSET(chromaThreshold);
157
 
        CSET(sceneChange);
158
 
 
159
 
        return 1;
160
 
 
161
 
}
162
 
ADMVideoDenoise::~ADMVideoDenoise()
163
 
{
164
 
        
165
 
        delete  _uncompressed;
166
 
        delete  _locked;
167
 
        delete  _lockcount;
168
 
  DELETE(_param);
169
 
  
170
 
  _uncompressed=_locked=_lockcount=NULL;
171
 
}
172
 
 
173
 
//
174
 
//      Remove y and v just keep U and expand it
175
 
//
176
 
uint8_t ADMVideoDenoise::getFrameNumberNoAlloc(uint32_t frame,
177
 
                                uint32_t *len,
178
 
                                ADMImage *data,
179
 
                                uint32_t *flags)
180
 
{
181
 
   //uint32_t x,w;
182
 
        uint32_t page; 
183
 
                ADM_assert(_param);
184
 
                ADM_assert(frame<_info.nb_frames);
185
 
                                                                
186
 
                        
187
 
                if(!_in->getFrameNumberNoAlloc(frame, len,_uncompressed,flags)) return 0;
188
 
 
189
 
                
190
 
                page=_info.width*_info.height;  
191
 
                *len=(page*3)>>1;           
192
 
 
193
 
        if((_lastFrame+1)!=frame) // async jump
194
 
        {
195
 
                        // just copy it 
196
 
                        memcpy(YPLANE(data),YPLANE(_uncompressed),page);
197
 
                        memcpy(UPLANE(data),UPLANE(_uncompressed),page>>2);
198
 
                        memcpy(VPLANE(data),VPLANE(_uncompressed),page>>2);
199
 
                        
200
 
                        memcpy(YPLANE(_locked),YPLANE(_uncompressed),page);
201
 
                        memcpy(UPLANE(_locked),UPLANE(_uncompressed),page>>2);
202
 
                        memcpy(VPLANE(_locked),VPLANE(_uncompressed),page>>2);
203
 
                        
204
 
                        _lastFrame=frame;
205
 
                        return 1;
206
 
        }          
207
 
        _lastFrame=frame;
208
 
          
209
 
          // copy chroma for now
210
 
        
211
 
         
212
 
          
213
 
          //
214
 
          //uint32_t count=0;
215
 
          //uint32_t cell=page*4; // size of luma
216
 
          uint8_t *in,*out,*lock,*nb;
217
 
          uint8_t *uin,*uout,*ulock,*unb;
218
 
          uint8_t *vin,*vout,*vlock,*vnb;
219
 
          
220
 
          
221
 
          //uint32_t d;
222
 
          // init all
223
 
          
224
 
          // luma
225
 
          nb=YPLANE(_lockcount);
226
 
          lock=YPLANE(_locked);
227
 
          in=YPLANE(_uncompressed);
228
 
          out=YPLANE(data);
229
 
          // u
230
 
          unb=UPLANE(_lockcount);
231
 
          ulock=UPLANE(_locked);
232
 
          uin=UPLANE(_uncompressed);
233
 
          uout=UPLANE(data);
234
 
          // v
235
 
          vnb=VPLANE(_lockcount);
236
 
          vlock=VPLANE(_locked);
237
 
          vin=VPLANE(_uncompressed);
238
 
          vout=VPLANE(data);
239
 
          
240
 
          
241
 
          uint32_t xx,yy/*,dl*/,du,dv;
242
 
          uint32_t locked=0;
243
 
          for(yy=_info.height>>1;yy>0;yy--)
244
 
          {
245
 
                  for(xx=_info.width>>1;xx>0;xx--)          
246
 
                {
247
 
                        du=distMatrix[*uin][*ulock];    
248
 
                        dv=distMatrix[*vin][*vlock];            
249
 
                                                
250
 
                        // if chroma is locked , we try to lock luma
251
 
                        if( (du<_param->chromaLock)
252
 
                                 && (dv<_param->chromaLock))
253
 
                         {  
254
 
                                *uout=*ulock;
255
 
                                *vout=*vlock;
256
 
 
257
 
#define PIX(z)          doOnePix(in+z,out+z,lock+z,nb+z) 
258
 
                                locked+=PIX(0)+ PIX(1)+ PIX(_info.width)+PIX(_info.width+1);
259
 
                        }
260
 
                        else
261
 
                         // if chroma is blended, we blend luma
262
 
#undef PIX                                                                
263
 
#define PIX(z)          doBlend(in+z,out+z,lock+z,nb+z)                                                                  
264
 
                                if( (du<_param->chromaThreshold)
265
 
                                         && (dv<_param->chromaThreshold))
266
 
                                {
267
 
                                        PIX(0);
268
 
                                        PIX(1);
269
 
                                        PIX(_info.width);
270
 
                                        PIX(_info.width+1);     
271
 
                                      *uout=*ulock=(*uin+*uin)>>1;
272
 
                                        *vout=*vlock=(*vin+*vin)>>1;
273
 
                                }
274
 
#undef PIX                                                                                      
275
 
                                                                                
276
 
                        else
277
 
                        {
278
 
#define PIX(z) *(out+z)=*(lock+z)=*(in+z);*(nb+z)=0                     
279
 
                                                                                        
280
 
                                PIX(0);
281
 
                                PIX(1);
282
 
                                PIX(_info.width);
283
 
                                PIX(_info.width+1);             
284
 
                                *uout=*ulock=*uin;
285
 
                                *vout=*vlock=*vin;
286
 
                                
287
 
#undef PIX              
288
 
                        }
289
 
                                                                  
290
 
                                                                                                                                                                        
291
 
                        uin++;uout++;ulock++;unb++;   
292
 
                        vin++;vout++;vlock++;vnb++;   
293
 
                        in++;out++;lock++;nb++;   
294
 
                        in++;out++;lock++;nb++;   
295
 
                                                        
296
 
                }
297
 
            // 
298
 
            in+=_info.width;
299
 
            out+=_info.width;
300
 
            lock+=_info.width;
301
 
            nb+=_info.width;                                                            
302
 
        };
303
 
          
304
 
          if(locked>((page*3)>>2)) // if more than 75% pixel not locked -> scene change
305
 
          {
306
 
                        memcpy(YPLANE(data),YPLANE(_uncompressed),page);
307
 
                        memcpy(UPLANE(data),UPLANE(_uncompressed),page>>2);
308
 
                        memcpy(VPLANE(data),VPLANE(_uncompressed),page>>2);
309
 
                        
310
 
                        memcpy(YPLANE(_locked),YPLANE(_uncompressed),page);
311
 
                        memcpy(UPLANE(_locked),UPLANE(_uncompressed),page>>2);
312
 
                        memcpy(VPLANE(_locked),VPLANE(_uncompressed),page>>2);
313
 
        }
314
 
      data->copyInfo(_uncompressed);  
315
 
      return 1;
316
 
}
317
 
 
318
 
//
319
 
//      0 copy
320
 
//  1 lock
321
 
//  2 threshold
322
 
//
323
 
uint8_t ADMVideoDenoise::doOnePix(uint8_t *in,uint8_t *out,uint8_t *lock,uint8_t *nb)
324
 
{
325
 
unsigned int d;
326
 
                d=distMatrix[*(in)][*(lock)]; 
327
 
                if(d<_param->lumaLock)         
328
 
                {                                                                               
329
 
                        if(*(nb)>30)  // out of scope -> copy new                   
330
 
                        {       // too much copy ->                              
331
 
                                *(nb)=0;                       
332
 
                                *(out)=(*(in)+*(lock))>>1;
333
 
                                *(lock)=*(out);         
334
 
                                return DN_COPY;      
335
 
                        }                                 
336
 
                        else                               
337
 
                        {                                   
338
 
                                *(out)=*(lock);         
339
 
                                *nb += 1; // *(nb)++;   
340
 
                                return DN_LOCK;         
341
 
                        }                  
342
 
                }                     
343
 
                else if(d< _param->lumaThreshold) 
344
 
                        {                                  
345
 
                                 *(nb)=0;                           
346
 
                                *(out)=(*(in)+*(lock))>>1;      
347
 
                                return DN_BLEND;
348
 
                        }
349
 
                        else   // too big delta
350
 
                        {    
351
 
                                 *(nb)=0; 
352
 
                                *(out)=*(in);     
353
 
                                *(lock)=*(in);    
354
 
                                return DN_COPY;
355
 
                        }                     
356
 
                                                                   
357
 
                        ADM_assert(0);
358
 
                        return 0;
359
 
 
360
 
}
361
 
uint8_t ADMVideoDenoise::doBlend(uint8_t *in,uint8_t *out,uint8_t *lock,uint8_t *nb)
362
 
{
363
 
unsigned int d;
364
 
                   d=distMatrix[*(in)][*(lock)]; 
365
 
                   *nb=0;
366
 
                   
367
 
                        if(d<_param->lumaThreshold)         
368
 
                        {
369
 
                                        *(out)=(*(in)+*(lock))>>1;                                      
370
 
                        }
371
 
                        else
372
 
                        *out=*in;
373
 
                        return 0;
374
 
        
375
 
}
376
 
 
377
 
// EOF