FFmpeg
jpeg2000dwt.c
Go to the documentation of this file.
1 /*
2  * Discrete wavelet transform
3  * Copyright (c) 2007 Kamil Nowosad
4  * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include <inttypes.h>
24 #include <stdio.h>
25 
26 #include "libavutil/common.h"
27 
28 #include "libavcodec/jpeg2000dwt.c"
29 
30 #include "libavutil/lfg.h"
31 
32 #define MAX_W 256
33 
34 static int test_dwt(int *array, int *ref, int border[2][2], int decomp_levels, int type, int max_diff) {
35  int ret, j;
36  DWTContext s1={{{0}}}, *s= &s1;
37  int64_t err2 = 0;
38 
39  ret = ff_jpeg2000_dwt_init(s, border, decomp_levels, type);
40  if (ret < 0) {
41  fprintf(stderr, "ff_jpeg2000_dwt_init failed\n");
42  return 1;
43  }
45  if (ret < 0) {
46  fprintf(stderr, "ff_dwt_encode failed\n");
47  return 1;
48  }
50  if (ret < 0) {
51  fprintf(stderr, "ff_dwt_encode failed\n");
52  return 1;
53  }
54  for (j = 0; j<MAX_W * MAX_W; j++) {
55  if (FFABS(array[j] - (int64_t)ref[j]) > max_diff) {
56  fprintf(stderr, "missmatch at %d (%d != %d) decomp:%d border %d %d %d %d\n",
57  j, array[j], ref[j],decomp_levels, border[0][0], border[0][1], border[1][0], border[1][1]);
58  return 2;
59  }
60  err2 += (array[j] - ref[j]) * (int64_t)(array[j] - ref[j]);
61  array[j] = ref[j];
62  }
64 
65  printf("%s, decomp:%2d border %3d %3d %3d %3d milli-err2:%9"PRId64"\n",
66  type == FF_DWT53 ? "5/3i" : "9/7i",
67  decomp_levels, border[0][0], border[0][1], border[1][0], border[1][1],
68  1000*err2 / ((border[0][1] - border[0][0])*(border[1][1] - border[1][0])));
69 
70  return 0;
71 }
72 
73 static int test_dwtf(float *array, float *ref, int border[2][2], int decomp_levels, float max_diff) {
74  int ret, j;
75  DWTContext s1={{{0}}}, *s= &s1;
76  double err2 = 0;
77 
78  ret = ff_jpeg2000_dwt_init(s, border, decomp_levels, FF_DWT97);
79  if (ret < 0) {
80  fprintf(stderr, "ff_jpeg2000_dwt_init failed\n");
81  return 1;
82  }
84  if (ret < 0) {
85  fprintf(stderr, "ff_dwt_encode failed\n");
86  return 1;
87  }
89  if (ret < 0) {
90  fprintf(stderr, "ff_dwt_encode failed\n");
91  return 1;
92  }
93  for (j = 0; j<MAX_W * MAX_W; j++) {
94  if (FFABS(array[j] - ref[j]) > max_diff) {
95  fprintf(stderr, "missmatch at %d (%f != %f) decomp:%d border %d %d %d %d\n",
96  j, array[j], ref[j],decomp_levels, border[0][0], border[0][1], border[1][0], border[1][1]);
97  return 2;
98  }
99  err2 += (array[j] - ref[j]) * (array[j] - ref[j]);
100  array[j] = ref[j];
101  }
102  ff_dwt_destroy(s);
103 
104  printf("9/7f, decomp:%2d border %3d %3d %3d %3d err2:%20.3f\n",
105  decomp_levels, border[0][0], border[0][1], border[1][0], border[1][1],
106  err2 / ((border[0][1] - border[0][0])*(border[1][1] - border[1][0])));
107 
108  return 0;
109 }
110 
111 static int array[MAX_W * MAX_W];
112 static int ref [MAX_W * MAX_W];
113 static float arrayf[MAX_W * MAX_W];
114 static float reff [MAX_W * MAX_W];
115 
116 int main(void) {
117  AVLFG prng;
118  int i,j;
119  int border[2][2];
120  int ret, decomp_levels;
121 
122  av_lfg_init(&prng, 1);
123 
124  for (i = 0; i<MAX_W * MAX_W; i++)
125  arrayf[i] = reff[i] = array[i] = ref[i] = av_lfg_get(&prng) % 2048;
126 
127  for (i = 0; i < 100; i++) {
128  for (j=0; j<4; j++)
129  border[j>>1][j&1] = av_lfg_get(&prng) % MAX_W;
130  if (border[0][0] >= border[0][1] || border[1][0] >= border[1][1])
131  continue;
132  decomp_levels = av_lfg_get(&prng) % FF_DWT_MAX_DECLVLS;
133 
134  ret = test_dwt(array, ref, border, decomp_levels, FF_DWT53, 0);
135  if (ret)
136  return ret;
137  ret = test_dwt(array, ref, border, decomp_levels, FF_DWT97_INT, FFMIN(7+5*decomp_levels, 15+3*decomp_levels));
138  if (ret)
139  return ret;
140  ret = test_dwtf(arrayf, reff, border, decomp_levels, 0.05);
141  if (ret)
142  return ret;
143  }
144 
145  return 0;
146 }
ff_dwt_decode
int ff_dwt_decode(DWTContext *s, void *t)
Definition: jpeg2000dwt.c:598
av_lfg_init
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
main
int main(void)
Definition: jpeg2000dwt.c:116
int64_t
long long int64_t
Definition: coverity.c:34
ff_dwt_encode
int ff_dwt_encode(DWTContext *s, void *t)
Definition: jpeg2000dwt.c:580
FF_DWT97
@ FF_DWT97
Definition: jpeg2000dwt.h:37
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
reff
static float reff[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:114
arrayf
static float arrayf[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:113
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_lfg_get
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:53
test_dwtf
static int test_dwtf(float *array, float *ref, int border[2][2], int decomp_levels, float max_diff)
Definition: jpeg2000dwt.c:73
lfg.h
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:74
test_dwt
static int test_dwt(int *array, int *ref, int border[2][2], int decomp_levels, int type, int max_diff)
Definition: jpeg2000dwt.c:34
ff_jpeg2000_dwt_init
int ff_jpeg2000_dwt_init(DWTContext *s, int border[2][2], int decomp_levels, int type)
Initialize DWT.
Definition: jpeg2000dwt.c:536
AVLFG
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
ff_dwt_destroy
void ff_dwt_destroy(DWTContext *s)
Definition: jpeg2000dwt.c:619
printf
printf("static const uint8_t my_array[100] = {\n")
MAX_W
#define MAX_W
Definition: jpeg2000dwt.c:32
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
common.h
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
FF_DWT_MAX_DECLVLS
#define FF_DWT_MAX_DECLVLS
max number of decomposition levels
Definition: jpeg2000dwt.h:32
DWTContext
Definition: dirac_dwt.h:54
array
static int array[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:111
ret
ret
Definition: filter_design.txt:187
FF_DWT53
@ FF_DWT53
Definition: jpeg2000dwt.h:38
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:112
jpeg2000dwt.c
FF_DWT97_INT
@ FF_DWT97_INT
Definition: jpeg2000dwt.h:39