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

« back to all changes in this revision

Viewing changes to avidemux/ADM_videoFilter/ADM_vidFastConvolution.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_vidFastConvolution.cpp  -  description
 
3
                             -------------------
 
4
    begin                : Sat Nov 23 2002
 
5
    copyright            : (C) 2002 by mean
 
6
    email                : fixounet@free.fr
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
11
 *   This program is free software; you can redistribute it and/or modify  *
 
12
 *   it under the terms of the GNU General Public License as published by  *
 
13
 *   the Free Software Foundation; either version 2 of the License, or     *
 
14
 *   (at your option) any later version.                                   *
 
15
 *                                                                         *
 
16
 ***************************************************************************/
 
17
#include "config.h"
 
18
    
 
19
#include <stdio.h>
 
20
#include <stdlib.h>
 
21
#include <string.h>
 
22
#include "default.h"
 
23
 
 
24
#include "ADM_toolkit/toolkit.hxx"
 
25
#include "ADM_editor/ADM_edit.hxx"
 
26
#include "ADM_video/ADM_genvideo.hxx"
 
27
#include "ADM_video/ADM_genvideo.hxx"
 
28
 
 
29
#include"ADM_filter/video_filters.h"
 
30
#include "ADM_userInterfaces/ADM_commonUI/DIA_factory.h"
 
31
#include "ADM_vidConvolution.hxx"
 
32
#include <ADM_assert.h>
 
33
 
 
34
 
 
35
BUILD_CREATE(mean_create,AVDMFastVideoMean);
 
36
BUILD_CREATE(sharpen_create,AVDMFastVideoSharpen);
 
37
BUILD_CREATE(Gaussian_create,AVDMFastVideoGauss);
 
38
BUILD_CREATE(median_create,AVDMFastVideoMedian);
 
39
 
 
40
static FILTER_PARAM convParam={2,{"luma","chroma"}};
 
41
 
 
42
SCRIPT_CREATE(mean_script,AVDMFastVideoMean,convParam);
 
43
SCRIPT_CREATE(sharpen_script,AVDMFastVideoSharpen,convParam);
 
44
SCRIPT_CREATE(gaussian_script,AVDMFastVideoGauss,convParam);
 
45
SCRIPT_CREATE(median_script,AVDMFastVideoMedian,convParam);
 
46
 
 
47
 
 
48
uint8_t AVDMFastVideoConvolution::getCoupledConf( CONFcouple **couples)
 
49
{
 
50
 
 
51
        ADM_assert(_param);
 
52
        
 
53
        *couples=new CONFcouple(2);
 
54
 
 
55
#define CSET(x)  (*couples)->setCouple((char *)#x,(_param->x))
 
56
        CSET(chroma);
 
57
        CSET(luma);
 
58
        return 1;
 
59
 
 
60
}
 
61
 
 
62
//_______________________________________________________________
 
63
 
 
64
AVDMFastVideoConvolution::AVDMFastVideoConvolution(
 
65
                        AVDMGenericVideoStream *in,CONFcouple *couples)
 
66
{
 
67
 
 
68
 
 
69
        _in=in;         
 
70
        memcpy(&_info,_in->getInfo(),sizeof(_info));
 
71
        _uncompressed=new ADMImage(_in->getInfo()->width,_in->getInfo()->height);       
 
72
        ADM_assert(_uncompressed); 
 
73
        _info.encoding=1;
 
74
        if(couples==NULL)
 
75
        {
 
76
                _param=NEW( CONV_PARAM);
 
77
                _param->chroma=1;
 
78
                _param->luma=1; 
 
79
                printf("\n Creating from null\n");                              
 
80
        }
 
81
        else
 
82
        {
 
83
                _param=NEW(CONV_PARAM);
 
84
                GET(luma);
 
85
                GET(chroma);
 
86
        }
 
87
 
 
88
                
 
89
}
 
90
AVDMFastVideoConvolution::~AVDMFastVideoConvolution()
 
91
{
 
92
        if(_uncompressed)
 
93
                delete _uncompressed;
 
94
        _uncompressed=NULL;
 
95
        DELETE(_param);
 
96
}
 
97
 
 
98
//
 
99
//      Basically ask a uncompressed frame from editor and ask
 
100
//              GUI to decompress it .
 
101
//
 
102
 
 
103
uint8_t AVDMFastVideoConvolution::getFrameNumberNoAlloc(uint32_t frame,
 
104
                                uint32_t *len,
 
105
                                ADMImage *data,
 
106
                                uint32_t *flags)
 
107
{
 
108
//uint8_t *dst,*dstu,*dstv,*srcu,*srcv;
 
109
uint8_t *x1,*x2,*x3,*o1;
 
110
uint32_t stride,page;
 
111
 
 
112
        if(frame>= _info.nb_frames) return 0;
 
113
        ADM_assert(_uncompressed);                                      
 
114
        stride=_info.width;
 
115
        page=(stride*_info.height)>>2;
 
116
        
 
117
 
 
118
        
 
119
        // read uncompressed frame
 
120
        if(!_in->getFrameNumberNoAlloc(frame, len,_uncompressed,flags)) return 0;               
 
121
         
 
122
        if(!_param->luma)
 
123
        {
 
124
                memcpy(YPLANE(data),YPLANE(_uncompressed),page*4);
 
125
        }
 
126
        else
 
127
        {
 
128
                o1=YPLANE(data)+stride;
 
129
                x1=YPLANE(_uncompressed);
 
130
                x2=x1+stride;
 
131
                x3=x2+stride;
 
132
 
 
133
                // first and last line
 
134
                memcpy(YPLANE(data),YPLANE(_uncompressed),stride);
 
135
                memcpy(YPLANE(data)+page*4-stride,YPLANE(_uncompressed)+page*4-stride,stride);          
 
136
                // Luma
 
137
                for(int32_t y=1;y<(int32_t)_info.height-1;y++)
 
138
                {
 
139
                        doLine(x1,x2,x3,o1,stride);
 
140
                        x1=x2;
 
141
                        x2=x3;
 
142
                        x3+=stride; 
 
143
                        o1+=stride;                 
 
144
                }
 
145
        }
 
146
        // chroma u & v
 
147
        if(!_param->chroma)
 
148
        {
 
149
                memcpy(UPLANE(data),UPLANE(_uncompressed),page);
 
150
                memcpy(VPLANE(data),VPLANE(_uncompressed),page);
 
151
        }
 
152
        else
 
153
        {
 
154
                stride>>=1;
 
155
                // chroma u
 
156
                o1=UPLANE(data)+stride;
 
157
                x1=UPLANE(_uncompressed);
 
158
                x2=x1+stride;
 
159
                x3=x2+stride;
 
160
                // first and last line
 
161
                memcpy(UPLANE(data),UPLANE(_uncompressed),stride);
 
162
                memcpy(UPLANE(data)+page-stride,UPLANE(_uncompressed)+page-stride,stride);          
 
163
                // Luma
 
164
                for(int32_t y=1;y<(int32_t)(_info.height>>1)-1;y++)
 
165
                {
 
166
                        doLine(x1,x2,x3,o1,stride);
 
167
                        x1=x2;
 
168
                        x2=x3;
 
169
                        x3+=stride; 
 
170
                        o1+=stride;                 
 
171
                }
 
172
                
 
173
                // chroma V
 
174
                o1=VPLANE(data)+stride;
 
175
                x1=VPLANE(_uncompressed);
 
176
                x2=x1+stride;
 
177
                x3=x2+stride;
 
178
                // first and last line
 
179
                memcpy(VPLANE(data),VPLANE(_uncompressed),stride);
 
180
                memcpy(VPLANE(data)+page-stride,VPLANE(_uncompressed)+page-stride,stride);          
 
181
                // Luma
 
182
                for(int32_t y=1;y<(int32_t)(_info.height>>1)-1;y++)
 
183
                {
 
184
                        doLine(x1,x2,x3,o1,stride);
 
185
                        x1=x2;
 
186
                        x2=x3;
 
187
                        x3+=stride; 
 
188
                        o1+=stride;                 
 
189
                }
 
190
        }
 
191
        data->copyInfo(_uncompressed);
 
192
      return 1;
 
193
}
 
194
//
 
195
//      Run the convolution kernel on a whole line
 
196
//      to speed up things (cache effect + locality of datas)
 
197
//
 
198
//----------------------------------------------------------------
 
199
//----------------------------------------------------------------
 
200
//-----------------------MEAN---------------------
 
201
//----------------------------------------------------------------
 
202
//----------------------------------------------------------------
 
203
 
 
204
char    *AVDMFastVideoMean::printConf(void)
 
205
{
 
206
                static char str[]="Mean(fast)";
 
207
                return (char *)str;
 
208
        
 
209
}
 
210
uint8_t AVDMFastVideoMean::doLine(uint8_t  *pred,
 
211
                                                                                                                                                                        uint8_t *cur,
 
212
                                                                                                                                                                uint8_t *next,
 
213
                                                                                                                                                                uint8_t *out,
 
214
                                                                                                uint32_t w)
 
215
                                 
 
216
{
 
217
        uint8_t a1,a2,a3;
 
218
        uint8_t b1,b2,b3;
 
219
        uint8_t c1,c2,c3;
 
220
        int32_t o;
 
221
        
 
222
        a2=*pred++;a3=*pred++;
 
223
        b2=*cur++;b3=*cur++;
 
224
        c2=*next++;c3=*next++;
 
225
        
 
226
        *out++=b2;
 
227
        w--;
 
228
        
 
229
        while(w>1)
 
230
        {
 
231
                        a1=a2;
 
232
                        a2=a3;
 
233
                        a3=*pred++;
 
234
                        b1=b2;
 
235
                        b2=b3;
 
236
                        b3=*cur++;
 
237
                        c1=c2;
 
238
                        c2=c3;
 
239
                        c3=*next++;
 
240
                
 
241
                  //
 
242
                  o=a1+a2+a3+b1+b2+b3+c1+c2+c3;
 
243
                  o/=9;
 
244
                  
 
245
                  *out++=o;
 
246
                  w--;
 
247
        }       
 
248
                *out++=b3;
 
249
                return 1;
 
250
}
 
251
 
 
252
//----------------------------------------------------------------
 
253
//----------------------------------------------------------------
 
254
//-----------------------GAUSS---------------------
 
255
//----------------------------------------------------------------
 
256
//----------------------------------------------------------------
 
257
 
 
258
//          6 10 6
 
259
//         10 16 10 *1/80
 
260
//                  6 10 6
 
261
char                                                    *AVDMFastVideoGauss::printConf(void)
 
262
{
 
263
                static char str[]="Gauss(fast)";
 
264
                return (char *)str;
 
265
        
 
266
}
 
267
 uint8_t AVDMFastVideoGauss::doLine(uint8_t  *pred,
 
268
                                                                                                                                                                        uint8_t *cur,
 
269
                                                                                                                                                                uint8_t *next,
 
270
                                                                                                                                                                uint8_t *out,
 
271
                                                                                                uint32_t w)
 
272
                                 
 
273
{
 
274
        uint8_t a1,a2,a3;
 
275
        uint8_t b1,b2,b3;
 
276
        uint8_t c1,c2,c3;
 
277
        int32_t o;
 
278
//#define MASKED__      
 
279
#define threshold 80
 
280
        int v,r;
 
281
        
 
282
        a2=*pred++;a3=*pred++;
 
283
        b2=*cur++;b3=*cur++;
 
284
        c2=*next++;c3=*next++;
 
285
        
 
286
        *out++=b2;
 
287
        w--;
 
288
        
 
289
        while(w>1)
 
290
        {
 
291
                        a1=a2;
 
292
                        a2=a3;
 
293
                        a3=*pred++;
 
294
                        b1=b2;
 
295
                        b2=b3;
 
296
                        b3=*cur++;
 
297
                        c1=c2;
 
298
                        c2=c3;
 
299
                        c3=*next++;
 
300
                
 
301
                  //
 
302
#ifdef MASKED__
 
303
                v=b2;
 
304
                r=16;
 
305
                o=b2*16;
 
306
                #define MORE(x,coef) if(abs(x-v)<=threshold) {o+=x*coef;r+=coef;}
 
307
                MORE(a1,6);
 
308
                MORE(a2,10);
 
309
                MORE(a3,6);
 
310
                
 
311
                MORE(b1,10);
 
312
                MORE(b3,10);
 
313
                
 
314
                MORE(c1,6);
 
315
                MORE(c2,10);
 
316
                MORE(c3,6);
 
317
                
 
318
                o=(o+r-1)/r;            
 
319
                
 
320
#else             
 
321
                  o=6*a1+10*a2+6*a3+10*b1+16*b2+10*b3+6*c1+10*c2+6*c3;            
 
322
                  o/=80;
 
323
#endif            
 
324
                  
 
325
                  *out++=o;
 
326
                  w--;
 
327
        }       
 
328
        *out++=b3;
 
329
                return 1;
 
330
}
 
331
//----------------------------------------------------------------
 
332
//----------------------------------------------------------------
 
333
//-----------------------SHARPEN---------------------
 
334
//----------------------------------------------------------------
 
335
//----------------------------------------------------------------
 
336
 
 
337
//         -1 -2 -1
 
338
        //         -2 16 -2 *1/16
 
339
        //                  -1 -2 -1
 
340
char                                                    *AVDMFastVideoSharpen::printConf(void)
 
341
{
 
342
                static char str[]="Sharpen(fast)";
 
343
                return (char *)str;
 
344
        
 
345
}
 
346
 uint8_t AVDMFastVideoSharpen::doLine(uint8_t  *pred,
 
347
                                                                                                                                                                        uint8_t *cur,
 
348
                                                                                                                                                                uint8_t *next,
 
349
                                                                                                                                                                uint8_t *out,
 
350
                                                                                                uint32_t w)
 
351
                                 
 
352
{
 
353
        uint8_t a1,a2,a3;
 
354
        uint8_t b1,b2,b3;
 
355
        uint8_t c1,c2,c3;
 
356
        int32_t o;
 
357
        
 
358
        a2=*pred++;a3=*pred++;
 
359
        b2=*cur++;b3=*cur++;
 
360
        c2=*next++;c3=*next++;
 
361
        
 
362
        *out++=b2;
 
363
        w--;
 
364
        
 
365
        while(w>1)
 
366
        {
 
367
                        a1=a2;
 
368
                        a2=a3;
 
369
                        a3=*pred++;
 
370
                        b1=b2;
 
371
                        b2=b3;
 
372
                        b3=*cur++;
 
373
                        c1=c2;
 
374
                        c2=c3;
 
375
                        c3=*next++;
 
376
                
 
377
                  //
 
378
                  //         -1 -2 -1
 
379
        //         -2 16 -2 *1/16
 
380
        //                  -1 -2 -1
 
381
                  //o=-a1+-2*a2+-a3+-2*b1+16*b2+-2*b3+-1*c1+-2*c2+-1*c3;
 
382
                  //o/=16;
 
383
                  o=4*b2-a2-c2-b1-b3;
 
384
                  o>>=2;
 
385
                  o+=3*a2;
 
386
                  o=o/3;
 
387
                  if(o<0) o=0;
 
388
                  if(o>255) o=255;
 
389
                  
 
390
                  *out++=o;
 
391
                  w--;
 
392
        }       
 
393
        *out++=b3;
 
394
                return 1;
 
395
}
 
396
//----------------------------------------------------------------
 
397
//----------------------------------------------------------------
 
398
//-----------------------Median---------------------
 
399
//----------------------------------------------------------------
 
400
//----------------------------------------------------------------
 
401
 
 
402
//         -1 -2 -1
 
403
        //         -2 16 -2 *1/16
 
404
        //                  -1 -2 -1
 
405
char                                                    *AVDMFastVideoMedian::printConf(void)
 
406
{
 
407
                static char str[]="Median(fast)";
 
408
                return (char *)str;
 
409
        
 
410
}
 
411
 uint8_t AVDMFastVideoMedian::doLine(uint8_t  *pred,
 
412
                                                                                                                                                                        uint8_t *cur,
 
413
                                                                                                                                                                uint8_t *next,
 
414
                                                                                                                                                                uint8_t *out,
 
415
                                                                                                uint32_t w)
 
416
                                 
 
417
{
 
418
        uint8_t a1,a2,a3;
 
419
        uint8_t b1,b2,b3;
 
420
        uint8_t c1,c2,c3; //,i;
 
421
        //int32_t o;
 
422
        uint8_t temp;
 
423
        
 
424
        static uint8_t tab[9];
 
425
        a2=*pred++;a3=*pred++;
 
426
        b2=*cur++;b3=*cur++;
 
427
        c2=*next++;c3=*next++;
 
428
        
 
429
        *out++=b2;
 
430
        w--;
 
431
        
 
432
        while(w>1)
 
433
        {
 
434
                        tab[0]=a1=a2;
 
435
                        tab[1]=a2=a3;
 
436
                        tab[2]=a3=*pred++;
 
437
                        tab[3]=b1=b2;
 
438
                        tab[4]=b2=b3;
 
439
                        tab[5]=b3=*cur++;
 
440
                        tab[6]=c1=c2;
 
441
                        tab[7]=c2=c3;
 
442
                        tab[8]=c3=*next++;
 
443
                
 
444
#define PIX_SORT(a,b) { if ((a)>(b)) PIX_SWAP((a),(b)); }
 
445
#define PIX_SWAP(a,b) { temp=(a);(a)=(b);(b)=temp; }
 
446
 
 
447
   uint8_t *p=(uint8_t *)tab;
 
448
                                                                
 
449
    PIX_SORT(p[1], p[2]) ; PIX_SORT(p[4], p[5]) ; PIX_SORT(p[7], p[8]) ;
 
450
    PIX_SORT(p[0], p[1]) ; PIX_SORT(p[3], p[4]) ; PIX_SORT(p[6], p[7]) ;
 
451
    PIX_SORT(p[1], p[2]) ; PIX_SORT(p[4], p[5]) ; PIX_SORT(p[7], p[8]) ;
 
452
    PIX_SORT(p[0], p[3]) ; PIX_SORT(p[5], p[8]) ; PIX_SORT(p[4], p[7]) ;
 
453
    PIX_SORT(p[3], p[6]) ; PIX_SORT(p[1], p[4]) ; PIX_SORT(p[2], p[5]) ;
 
454
    PIX_SORT(p[4], p[7]) ; PIX_SORT(p[4], p[2]) ; PIX_SORT(p[6], p[4]) ;
 
455
    PIX_SORT(p[4], p[2]) ; 
 
456
                  
 
457
                  *out++=tab[4];
 
458
                  w--;
 
459
        }       
 
460
        *out++=b3;
 
461
        return 1;
 
462
}
 
463
uint8_t AVDMFastVideoConvolution::configure(AVDMGenericVideoStream * instream)
 
464
{
 
465
  
 
466
  //return DIA_getLumaChroma(&(_param->luma),&(_param->chroma)) ; 
 
467
  diaElemToggle luma(&(_param->luma),QT_TR_NOOP("_Process luma"),QT_TR_NOOP("Process luma plane"));
 
468
  diaElemToggle chroma(&(_param->chroma),QT_TR_NOOP("P_rocess chroma"));
 
469
  
 
470
  diaElem *elems[2]={&luma,&chroma};
 
471
  
 
472
  return diaFactoryRun(QT_TR_NOOP("Fast Convolution"),2,elems);
 
473
}
 
474
 
 
475
 
 
476