FFmpeg  4.4.7
snowenc.c
Go to the documentation of this file.
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  short obuffer[width*height];
35  SnowContext s;
36  int i;
37  AVLFG prng;
38  s.spatial_decomposition_count=6;
39  s.spatial_decomposition_type=1;
40  int ret = 0;
41 
42  s.temp_dwt_buffer = av_mallocz_array(width, sizeof(DWTELEM));
43  s.temp_idwt_buffer = av_mallocz_array(width, sizeof(IDWTELEM));
44 
45  if (!s.temp_dwt_buffer || !s.temp_idwt_buffer) {
46  fprintf(stderr, "Failed to allocate memory\n");
47  ret = 1;
48  goto end;
49  }
50 
51  av_lfg_init(&prng, 1);
52 
53  printf("testing 5/3 DWT\n");
54  for(i=0; i<width*height; i++)
55  buffer[0][i] = buffer[1][i] = av_lfg_get(&prng) % 19000 - 9000;
56 
57  ff_spatial_dwt(buffer[0], s.temp_dwt_buffer, width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
58  for(i=0; i<width*height; i++)
59  obuffer[i] = buffer[0][i];
60  ff_spatial_idwt(obuffer, s.temp_idwt_buffer, width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
61 
62  for(i=0; i<width*height; i++)
63  if(buffer[1][i]!= obuffer[i]) {
64  printf("fsck: %4dx%4dx %12d %7d\n",i%width, i/width, buffer[1][i], obuffer[i]);
65  ret = 1;
66  }
67 
68  printf("testing 9/7 DWT\n");
69  s.spatial_decomposition_type=0;
70  for(i=0; i<width*height; i++)
71  buffer[0][i] = buffer[1][i] = av_lfg_get(&prng) % 11000 - 5000;
72 
73  ff_spatial_dwt(buffer[0], s.temp_dwt_buffer, width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
74  for(i=0; i<width*height; i++)
75  obuffer[i] = buffer[0][i];
76  ff_spatial_idwt(obuffer, s.temp_idwt_buffer, width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
77 
78  for(i=0; i<width*height; i++)
79  if(FFABS(buffer[1][i] - obuffer[i])>20) {
80  printf("fsck: %4dx%4d %12d %7d\n",i%width, i/width, buffer[1][i], obuffer[i]);
81  ret = 1;
82  }
83 
84  {
85  int level, orientation, x, y;
86  int64_t errors[8][4];
87  int64_t g=0;
88 
89  memset(errors, 0, sizeof(errors));
90  s.spatial_decomposition_count=3;
91  s.spatial_decomposition_type=0;
92  for(level=0; level<s.spatial_decomposition_count; level++){
93  for(orientation=level ? 1 : 0; orientation<4; orientation++){
94  int w= width >> (s.spatial_decomposition_count-level);
95  int h= height >> (s.spatial_decomposition_count-level);
96  int stride= width << (s.spatial_decomposition_count-level);
97  IDWTELEM *buf= obuffer;
98  int64_t error=0;
99 
100  if(orientation&1) buf+=w;
101  if(orientation>1) buf+=stride>>1;
102 
103  memset(obuffer, 0, sizeof(short)*width*height);
104  buf[w/2 + h/2*stride]= 8*256;
105  ff_spatial_idwt(obuffer, s.temp_idwt_buffer, width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
106  for(y=0; y<height; y++){
107  for(x=0; x<width; x++){
108  int64_t d= obuffer[x + y*width];
109  error += d*d;
110  if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9 && level==2) printf("%8"PRId64" ", d);
111  }
112  if(FFABS(height/2-y)<9 && level==2) printf("\n");
113  }
114  error= (int)(sqrt(error)+0.5);
115  errors[level][orientation]= error;
116  if(g) g=av_gcd(g, error);
117  else g= error;
118  }
119  }
120  printf("static int const visual_weight[][4]={\n");
121  for(level=0; level<s.spatial_decomposition_count; level++){
122  printf(" {");
123  for(orientation=0; orientation<4; orientation++){
124  printf("%8"PRId64",", errors[level][orientation]/g);
125  }
126  printf("},\n");
127  }
128  printf("};\n");
129  {
130  int level=2;
131  int w= width >> (s.spatial_decomposition_count-level);
132  //int h= height >> (s.spatial_decomposition_count-level);
133  int stride= width << (s.spatial_decomposition_count-level);
134  DWTELEM *buf= buffer[0];
135  int64_t error=0;
136 
137  buf+=w;
138  buf+=stride>>1;
139 
140  memset(buffer[0], 0, sizeof(int)*width*height);
141  for(y=0; y<height; y++){
142  for(x=0; x<width; x++){
143  int tab[4]={0,2,3,1};
144  buffer[0][x+width*y]= 256*256*tab[(x&1) + 2*(y&1)];
145  }
146  }
147  ff_spatial_dwt(buffer[0], s.temp_dwt_buffer, width, height, width, s.spatial_decomposition_type, s.spatial_decomposition_count);
148  for(y=0; y<height; y++){
149  for(x=0; x<width; x++){
150  int64_t d= buffer[0][x + y*width];
151  error += d*d;
152  if(FFABS(width/2-x)<9 && FFABS(height/2-y)<9) printf("%8"PRId64" ", d);
153  }
154  if(FFABS(height/2-y)<9) printf("\n");
155  }
156  }
157 
158  }
159 
160 end:
161  av_free(s.temp_dwt_buffer);
162  av_free(s.temp_idwt_buffer);
163  return ret;
164 }
#define s(width, name)
Definition: cbs_vp9.c:257
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
long long int64_t
Definition: coverity.c:34
int DWTELEM
Definition: dirac_dwt.h:26
short IDWTELEM
Definition: dirac_dwt.h:27
int
int64_t av_gcd(int64_t a, int64_t b)
Compute the greatest common divisor of two integer operands.
Definition: mathematics.c:37
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:190
int i
Definition: input.c:407
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:53
uint8_t w
Definition: llviddspenc.c:39
int stride
Definition: mace.c:144
static char buffer[20]
Definition: seek.c:32
void ff_spatial_idwt(IDWTELEM *buffer, IDWTELEM *temp, int width, int height, int stride, int type, int decomposition_count)
Definition: snow_dwt.c:731
void ff_spatial_dwt(DWTELEM *buffer, DWTELEM *temp, int width, int height, int stride, int type, int decomposition_count)
Definition: snow_dwt.c:319
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
uint8_t level
Definition: svq3.c:206
#define av_free(p)
static void error(const char *err)
#define height
#define width
int main(void)
Definition: snowenc.c:30
static const struct twinvq_data tab
const char * g
Definition: vf_curves.c:117