Merge commit 'a6a750c7ef240b72ce01e9653343a0ddf247d196'
[ffmpeg.git] / libavcodec / tests / snowenc.c
1 /*
2  * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "libavcodec/snowenc.c"
22
23 #undef malloc
24 #undef free
25 #undef printf
26
27 #include "libavutil/lfg.h"
28 #include "libavutil/mathematics.h"
29
30 int main(void){
31 #define width  256
32 #define height 256
33     int buffer[2][width*height];
34     SnowContext s;
35     int i;
36     AVLFG prng;
37     s.spatial_decomposition_count=6;
38     s.spatial_decomposition_type=1;
39
40     s.temp_dwt_buffer  = av_mallocz_array(width, sizeof(DWTELEM));
41     s.temp_idwt_buffer = av_mallocz_array(width, sizeof(IDWTELEM));
42
43     if (!s.temp_dwt_buffer || !s.temp_idwt_buffer) {
44         fprintf(stderr, "Failed to allocate memory\n");
45         return 1;
46     }
47
48     av_lfg_init(&prng, 1);
49
50     printf("testing 5/3 DWT\n");
51     for(i=0; i<width*height; i++)
52         buffer[0][i] = buffer[1][i] = av_lfg_get(&prng) % 54321 - 12345;
53
54     ff_spatial_dwt(buffer[0], s.temp_dwt_buffer, width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
55     ff_spatial_idwt((IDWTELEM*)buffer[0], s.temp_idwt_buffer, width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
56
57     for(i=0; i<width*height; i++)
58         if(buffer[0][i]!= buffer[1][i]) printf("fsck: %6d %12d %7d\n",i, buffer[0][i], buffer[1][i]);
59
60     printf("testing 9/7 DWT\n");
61     s.spatial_decomposition_type=0;
62     for(i=0; i<width*height; i++)
63         buffer[0][i] = buffer[1][i] = av_lfg_get(&prng) % 54321 - 12345;
64
65     ff_spatial_dwt(buffer[0], s.temp_dwt_buffer, width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
66     ff_spatial_idwt((IDWTELEM*)buffer[0], s.temp_idwt_buffer, width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
67
68     for(i=0; i<width*height; i++)
69         if(FFABS(buffer[0][i] - buffer[1][i])>20) printf("fsck: %6d %12d %7d\n",i, buffer[0][i], buffer[1][i]);
70
71     {
72     int level, orientation, x, y;
73     int64_t errors[8][4];
74     int64_t g=0;
75
76         memset(errors, 0, sizeof(errors));
77         s.spatial_decomposition_count=3;
78         s.spatial_decomposition_type=0;
79         for(level=0; level<s.spatial_decomposition_count; level++){
80             for(orientation=level ? 1 : 0; orientation<4; orientation++){
81                 int w= width  >> (s.spatial_decomposition_count-level);
82                 int h= height >> (s.spatial_decomposition_count-level);
83                 int stride= width  << (s.spatial_decomposition_count-level);
84                 DWTELEM *buf= buffer[0];
85                 int64_t error=0;
86
87                 if(orientation&1) buf+=w;
88                 if(orientation>1) buf+=stride>>1;
89
90                 memset(buffer[0], 0, sizeof(int)*width*height);
91                 buf[w/2 + h/2*stride]= 256*256;
92                 ff_spatial_idwt((IDWTELEM*)buffer[0], s.temp_idwt_buffer, width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
93                 for(y=0; y<height; y++){
94                     for(x=0; x<width; x++){
95                         int64_t d= buffer[0][x + y*width];
96                         error += d*d;
97                         if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9 && level==2) printf("%8"PRId64" ", d);
98                     }
99                     if(FFABS(height/2-y)<9 && level==2) printf("\n");
100                 }
101                 error= (int)(sqrt(error)+0.5);
102                 errors[level][orientation]= error;
103                 if(g) g=av_gcd(g, error);
104                 else g= error;
105             }
106         }
107         printf("static int const visual_weight[][4]={\n");
108         for(level=0; level<s.spatial_decomposition_count; level++){
109             printf("  {");
110             for(orientation=0; orientation<4; orientation++){
111                 printf("%8"PRId64",", errors[level][orientation]/g);
112             }
113             printf("},\n");
114         }
115         printf("};\n");
116         {
117             int level=2;
118             int w= width  >> (s.spatial_decomposition_count-level);
119             //int h= height >> (s.spatial_decomposition_count-level);
120             int stride= width  << (s.spatial_decomposition_count-level);
121             DWTELEM *buf= buffer[0];
122             int64_t error=0;
123
124             buf+=w;
125             buf+=stride>>1;
126
127             memset(buffer[0], 0, sizeof(int)*width*height);
128             for(y=0; y<height; y++){
129                 for(x=0; x<width; x++){
130                     int tab[4]={0,2,3,1};
131                     buffer[0][x+width*y]= 256*256*tab[(x&1) + 2*(y&1)];
132                 }
133             }
134             ff_spatial_dwt(buffer[0], s.temp_dwt_buffer, width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
135             for(y=0; y<height; y++){
136                 for(x=0; x<width; x++){
137                     int64_t d= buffer[0][x + y*width];
138                     error += d*d;
139                     if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9) printf("%8"PRId64" ", d);
140                 }
141                 if(FFABS(height/2-y)<9) printf("\n");
142             }
143         }
144
145     }
146     return 0;
147 }