| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | /** | ||
| 24 | * @file | ||
| 25 | * Discrete wavelet transform | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include "libavutil/error.h" | ||
| 29 | #include "libavutil/macros.h" | ||
| 30 | #include "libavutil/mem.h" | ||
| 31 | #include "jpeg2000dwt.h" | ||
| 32 | |||
| 33 | /* Defines for 9/7 DWT lifting parameters. | ||
| 34 | * Parameters are in float. */ | ||
| 35 | #define F_LFTG_ALPHA 1.586134342059924f | ||
| 36 | #define F_LFTG_BETA 0.052980118572961f | ||
| 37 | #define F_LFTG_GAMMA 0.882911075530934f | ||
| 38 | #define F_LFTG_DELTA 0.443506852043971f | ||
| 39 | |||
| 40 | /* Lifting parameters in integer format. | ||
| 41 | * Computed as param = (float param) * (1 << 16) */ | ||
| 42 | #define I_LFTG_ALPHA_PRIME 38413ll // = 103949 - 65536, (= alpha - 1.0) | ||
| 43 | #define I_LFTG_BETA 3472ll | ||
| 44 | #define I_LFTG_GAMMA 57862ll | ||
| 45 | #define I_LFTG_DELTA 29066ll | ||
| 46 | #define I_LFTG_K 80621ll | ||
| 47 | #define I_LFTG_X 53274ll | ||
| 48 | |||
| 49 | 8041689 | static inline void extend53(int *p, int i0, int i1) | |
| 50 | { | ||
| 51 | 8041689 | p[i0 - 1] = p[i0 + 1]; | |
| 52 | 8041689 | p[i1] = p[i1 - 2]; | |
| 53 | 8041689 | p[i0 - 2] = p[i0 + 2]; | |
| 54 | 8041689 | p[i1 + 1] = p[i1 - 3]; | |
| 55 | 8041689 | } | |
| 56 | |||
| 57 | 12364 | static inline void extend97_float(float *p, int i0, int i1) | |
| 58 | { | ||
| 59 | int i; | ||
| 60 | |||
| 61 |
2/2✓ Branch 0 taken 49456 times.
✓ Branch 1 taken 12364 times.
|
61820 | for (i = 1; i <= 4; i++) { |
| 62 | 49456 | p[i0 - i] = p[i0 + i]; | |
| 63 | 49456 | p[i1 + i - 1] = p[i1 - i - 1]; | |
| 64 | } | ||
| 65 | 12364 | } | |
| 66 | |||
| 67 | 2363699 | static inline void extend97_int(int32_t *p, int i0, int i1) | |
| 68 | { | ||
| 69 | int i; | ||
| 70 | |||
| 71 |
2/2✓ Branch 0 taken 9454796 times.
✓ Branch 1 taken 2363699 times.
|
11818495 | for (i = 1; i <= 4; i++) { |
| 72 | 9454796 | p[i0 - i] = p[i0 + i]; | |
| 73 | 9454796 | p[i1 + i - 1] = p[i1 - i - 1]; | |
| 74 | } | ||
| 75 | 2363699 | } | |
| 76 | |||
| 77 | 3856290 | static void sd_1d53(int *p, int i0, int i1) | |
| 78 | { | ||
| 79 | int i; | ||
| 80 | |||
| 81 |
2/2✓ Branch 0 taken 16608 times.
✓ Branch 1 taken 3839682 times.
|
3856290 | if (i1 <= i0 + 1) { |
| 82 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 16544 times.
|
16608 | if (i0 == 1) |
| 83 | 64 | p[1] *= 2; | |
| 84 | 16608 | return; | |
| 85 | } | ||
| 86 | |||
| 87 | 3839682 | extend53(p, i0, i1); | |
| 88 | |||
| 89 |
2/2✓ Branch 0 taken 207535651 times.
✓ Branch 1 taken 3839682 times.
|
211375333 | for (i = ((i0+1)>>1) - 1; i < (i1+1)>>1; i++) |
| 90 | 207535651 | p[2*i+1] -= (p[2*i] + p[2*i+2]) >> 1; | |
| 91 |
2/2✓ Branch 0 taken 203695969 times.
✓ Branch 1 taken 3839682 times.
|
207535651 | for (i = ((i0+1)>>1); i < (i1+1)>>1; i++) |
| 92 | 203695969 | p[2*i] += (p[2*i-1] + p[2*i+1] + 2) >> 2; | |
| 93 | } | ||
| 94 | |||
| 95 | 6520 | static void dwt_encode53(DWTContext *s, int *t) | |
| 96 | { | ||
| 97 | int lev, | ||
| 98 | 6520 | w = s->linelen[s->ndeclevels-1][0]; | |
| 99 | 6520 | int *line = s->i_linebuf; | |
| 100 | 6520 | line += 3; | |
| 101 | |||
| 102 |
2/2✓ Branch 0 taken 39338 times.
✓ Branch 1 taken 6520 times.
|
45858 | for (lev = s->ndeclevels-1; lev >= 0; lev--){ |
| 103 | 39338 | int lh = s->linelen[lev][0], | |
| 104 | 39338 | lv = s->linelen[lev][1], | |
| 105 | 39338 | mh = s->mod[lev][0], | |
| 106 | 39338 | mv = s->mod[lev][1], | |
| 107 | lp; | ||
| 108 | int *l; | ||
| 109 | |||
| 110 | // VER_SD | ||
| 111 | 39338 | l = line + mv; | |
| 112 |
2/2✓ Branch 0 taken 2116676 times.
✓ Branch 1 taken 39338 times.
|
2156014 | for (lp = 0; lp < lh; lp++) { |
| 113 | 2116676 | int i, j = 0; | |
| 114 | |||
| 115 |
2/2✓ Branch 0 taken 203680547 times.
✓ Branch 1 taken 2116676 times.
|
205797223 | for (i = 0; i < lv; i++) |
| 116 | 203680547 | l[i] = t[w*i + lp]; | |
| 117 | |||
| 118 | 2116676 | sd_1d53(line, mv, mv + lv); | |
| 119 | |||
| 120 | // copy back and deinterleave | ||
| 121 |
2/2✓ Branch 0 taken 101857016 times.
✓ Branch 1 taken 2116676 times.
|
103973692 | for (i = mv; i < lv; i+=2, j++) |
| 122 | 101857016 | t[w*j + lp] = l[i]; | |
| 123 |
2/2✓ Branch 0 taken 101823531 times.
✓ Branch 1 taken 2116676 times.
|
103940207 | for (i = 1-mv; i < lv; i+=2, j++) |
| 124 | 101823531 | t[w*j + lp] = l[i]; | |
| 125 | } | ||
| 126 | |||
| 127 | // HOR_SD | ||
| 128 | 39338 | l = line + mh; | |
| 129 |
2/2✓ Branch 0 taken 1739614 times.
✓ Branch 1 taken 39338 times.
|
1778952 | for (lp = 0; lp < lv; lp++){ |
| 130 | 1739614 | int i, j = 0; | |
| 131 | |||
| 132 |
2/2✓ Branch 0 taken 203680547 times.
✓ Branch 1 taken 1739614 times.
|
205420161 | for (i = 0; i < lh; i++) |
| 133 | 203680547 | l[i] = t[w*lp + i]; | |
| 134 | |||
| 135 | 1739614 | sd_1d53(line, mh, mh + lh); | |
| 136 | |||
| 137 | // copy back and deinterleave | ||
| 138 |
2/2✓ Branch 0 taken 101855479 times.
✓ Branch 1 taken 1739614 times.
|
103595093 | for (i = mh; i < lh; i+=2, j++) |
| 139 | 101855479 | t[w*lp + j] = l[i]; | |
| 140 |
2/2✓ Branch 0 taken 101825068 times.
✓ Branch 1 taken 1739614 times.
|
103564682 | for (i = 1-mh; i < lh; i+=2, j++) |
| 141 | 101825068 | t[w*lp + j] = l[i]; | |
| 142 | } | ||
| 143 | } | ||
| 144 | 6520 | } | |
| 145 | 6290 | static void sd_1d97_float(float *p, int i0, int i1) | |
| 146 | { | ||
| 147 | int i; | ||
| 148 | |||
| 149 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 6182 times.
|
6290 | if (i1 <= i0 + 1) { |
| 150 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 44 times.
|
108 | if (i0 == 1) |
| 151 | 64 | p[1] *= F_LFTG_X * 2; | |
| 152 | else | ||
| 153 | 44 | p[0] *= F_LFTG_K; | |
| 154 | 108 | return; | |
| 155 | } | ||
| 156 | |||
| 157 | 6182 | extend97_float(p, i0, i1); | |
| 158 | 6182 | i0++; i1++; | |
| 159 | |||
| 160 |
2/2✓ Branch 0 taken 214515 times.
✓ Branch 1 taken 6182 times.
|
220697 | for (i = (i0>>1) - 2; i < (i1>>1) + 1; i++) |
| 161 | 214515 | p[2*i+1] -= 1.586134 * (p[2*i] + p[2*i+2]); | |
| 162 |
2/2✓ Branch 0 taken 208333 times.
✓ Branch 1 taken 6182 times.
|
214515 | for (i = (i0>>1) - 1; i < (i1>>1) + 1; i++) |
| 163 | 208333 | p[2*i] -= 0.052980 * (p[2*i-1] + p[2*i+1]); | |
| 164 |
2/2✓ Branch 0 taken 202151 times.
✓ Branch 1 taken 6182 times.
|
208333 | for (i = (i0>>1) - 1; i < (i1>>1); i++) |
| 165 | 202151 | p[2*i+1] += 0.882911 * (p[2*i] + p[2*i+2]); | |
| 166 |
2/2✓ Branch 0 taken 195969 times.
✓ Branch 1 taken 6182 times.
|
202151 | for (i = (i0>>1); i < (i1>>1); i++) |
| 167 | 195969 | p[2*i] += 0.443506 * (p[2*i-1] + p[2*i+1]); | |
| 168 | } | ||
| 169 | |||
| 170 | 20 | static void dwt_encode97_float(DWTContext *s, float *t) | |
| 171 | { | ||
| 172 | int lev, | ||
| 173 | 20 | w = s->linelen[s->ndeclevels-1][0]; | |
| 174 | 20 | float *line = s->f_linebuf; | |
| 175 | 20 | line += 5; | |
| 176 | |||
| 177 |
2/2✓ Branch 0 taken 338 times.
✓ Branch 1 taken 20 times.
|
358 | for (lev = s->ndeclevels-1; lev >= 0; lev--){ |
| 178 | 338 | int lh = s->linelen[lev][0], | |
| 179 | 338 | lv = s->linelen[lev][1], | |
| 180 | 338 | mh = s->mod[lev][0], | |
| 181 | 338 | mv = s->mod[lev][1], | |
| 182 | lp; | ||
| 183 | float *l; | ||
| 184 | |||
| 185 | // HOR_SD | ||
| 186 | 338 | l = line + mh; | |
| 187 |
2/2✓ Branch 0 taken 3614 times.
✓ Branch 1 taken 338 times.
|
3952 | for (lp = 0; lp < lv; lp++){ |
| 188 | 3614 | int i, j = 0; | |
| 189 | |||
| 190 |
2/2✓ Branch 0 taken 196047 times.
✓ Branch 1 taken 3614 times.
|
199661 | for (i = 0; i < lh; i++) |
| 191 | 196047 | l[i] = t[w*lp + i]; | |
| 192 | |||
| 193 | 3614 | sd_1d97_float(line, mh, mh + lh); | |
| 194 | |||
| 195 | // copy back and deinterleave | ||
| 196 |
2/2✓ Branch 0 taken 97979 times.
✓ Branch 1 taken 3614 times.
|
101593 | for (i = mh; i < lh; i+=2, j++) |
| 197 | 97979 | t[w*lp + j] = l[i]; | |
| 198 |
2/2✓ Branch 0 taken 98068 times.
✓ Branch 1 taken 3614 times.
|
101682 | for (i = 1-mh; i < lh; i+=2, j++) |
| 199 | 98068 | t[w*lp + j] = l[i]; | |
| 200 | } | ||
| 201 | |||
| 202 | // VER_SD | ||
| 203 | 338 | l = line + mv; | |
| 204 |
2/2✓ Branch 0 taken 2676 times.
✓ Branch 1 taken 338 times.
|
3014 | for (lp = 0; lp < lh; lp++) { |
| 205 | 2676 | int i, j = 0; | |
| 206 | |||
| 207 |
2/2✓ Branch 0 taken 196047 times.
✓ Branch 1 taken 2676 times.
|
198723 | for (i = 0; i < lv; i++) |
| 208 | 196047 | l[i] = t[w*i + lp]; | |
| 209 | |||
| 210 | 2676 | sd_1d97_float(line, mv, mv + lv); | |
| 211 | |||
| 212 | // copy back and deinterleave | ||
| 213 |
2/2✓ Branch 0 taken 98016 times.
✓ Branch 1 taken 2676 times.
|
100692 | for (i = mv; i < lv; i+=2, j++) |
| 214 | 98016 | t[w*j + lp] = l[i]; | |
| 215 |
2/2✓ Branch 0 taken 98031 times.
✓ Branch 1 taken 2676 times.
|
100707 | for (i = 1-mv; i < lv; i+=2, j++) |
| 216 | 98031 | t[w*j + lp] = l[i]; | |
| 217 | } | ||
| 218 | } | ||
| 219 | 20 | } | |
| 220 | |||
| 221 | 1161290 | static void sd_1d97_int(int *p, int i0, int i1) | |
| 222 | { | ||
| 223 | int i; | ||
| 224 | |||
| 225 |
2/2✓ Branch 0 taken 5058 times.
✓ Branch 1 taken 1156232 times.
|
1161290 | if (i1 <= i0 + 1) { |
| 226 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 4994 times.
|
5058 | if (i0 == 1) |
| 227 | 64 | p[1] = (p[1] * I_LFTG_X + (1<<14)) >> 15; | |
| 228 | else | ||
| 229 | 4994 | p[0] = (p[0] * I_LFTG_K + (1<<15)) >> 16; | |
| 230 | 5058 | return; | |
| 231 | } | ||
| 232 | |||
| 233 | 1156232 | extend97_int(p, i0, i1); | |
| 234 | 1156232 | i0++; i1++; | |
| 235 | |||
| 236 |
2/2✓ Branch 0 taken 64714665 times.
✓ Branch 1 taken 1156232 times.
|
65870897 | for (i = (i0>>1) - 2; i < (i1>>1) + 1; i++) { |
| 237 | 64714665 | const int64_t sum = p[2 * i] + p[2 * i + 2]; | |
| 238 | 64714665 | p[2 * i + 1] -= sum; | |
| 239 | 64714665 | p[2 * i + 1] -= (I_LFTG_ALPHA_PRIME * sum + (1 << 15)) >> 16; | |
| 240 | } | ||
| 241 |
2/2✓ Branch 0 taken 63558433 times.
✓ Branch 1 taken 1156232 times.
|
64714665 | for (i = (i0>>1) - 1; i < (i1>>1) + 1; i++) |
| 242 | 63558433 | p[2 * i] -= (I_LFTG_BETA * (p[2 * i - 1] + p[2 * i + 1]) + (1 << 15)) >> 16; | |
| 243 |
2/2✓ Branch 0 taken 62402201 times.
✓ Branch 1 taken 1156232 times.
|
63558433 | for (i = (i0>>1) - 1; i < (i1>>1); i++) |
| 244 | 62402201 | p[2 * i + 1] += (I_LFTG_GAMMA * (p[2 * i] + p[2 * i + 2]) + (1 << 15)) >> 16; | |
| 245 |
2/2✓ Branch 0 taken 61245969 times.
✓ Branch 1 taken 1156232 times.
|
62402201 | for (i = (i0>>1); i < (i1>>1); i++) |
| 246 | 61245969 | p[2 * i] += (I_LFTG_DELTA * (p[2 * i - 1] + p[2 * i + 1]) + (1 << 15)) >> 16; | |
| 247 | } | ||
| 248 | |||
| 249 | 1970 | static void dwt_encode97_int(DWTContext *s, int *t) | |
| 250 | { | ||
| 251 | int lev; | ||
| 252 | 1970 | int w = s->linelen[s->ndeclevels-1][0]; | |
| 253 | 1970 | int h = s->linelen[s->ndeclevels-1][1]; | |
| 254 | int i; | ||
| 255 | 1970 | int *line = s->i_linebuf; | |
| 256 | 1970 | line += 5; | |
| 257 | |||
| 258 |
2/2✓ Branch 0 taken 45940533 times.
✓ Branch 1 taken 1970 times.
|
45942503 | for (i = 0; i < w * h; i++) |
| 259 | 45940533 | t[i] *= 1 << I_PRESHIFT; | |
| 260 | |||
| 261 |
2/2✓ Branch 0 taken 12038 times.
✓ Branch 1 taken 1970 times.
|
14008 | for (lev = s->ndeclevels-1; lev >= 0; lev--){ |
| 262 | 12038 | int lh = s->linelen[lev][0], | |
| 263 | 12038 | lv = s->linelen[lev][1], | |
| 264 | 12038 | mh = s->mod[lev][0], | |
| 265 | 12038 | mv = s->mod[lev][1], | |
| 266 | lp; | ||
| 267 | int *l; | ||
| 268 | |||
| 269 | // VER_SD | ||
| 270 | 12038 | l = line + mv; | |
| 271 |
2/2✓ Branch 0 taken 636876 times.
✓ Branch 1 taken 12038 times.
|
648914 | for (lp = 0; lp < lh; lp++) { |
| 272 | 636876 | int i, j = 0; | |
| 273 | |||
| 274 |
2/2✓ Branch 0 taken 61241397 times.
✓ Branch 1 taken 636876 times.
|
61878273 | for (i = 0; i < lv; i++) |
| 275 | 61241397 | l[i] = t[w*i + lp]; | |
| 276 | |||
| 277 | 636876 | sd_1d97_int(line, mv, mv + lv); | |
| 278 | |||
| 279 | // copy back and deinterleave | ||
| 280 |
2/2✓ Branch 0 taken 30625716 times.
✓ Branch 1 taken 636876 times.
|
31262592 | for (i = mv; i < lv; i+=2, j++) |
| 281 | 30625716 | t[w*j + lp] = l[i]; | |
| 282 |
2/2✓ Branch 0 taken 30615681 times.
✓ Branch 1 taken 636876 times.
|
31252557 | for (i = 1-mv; i < lv; i+=2, j++) |
| 283 | 30615681 | t[w*j + lp] = l[i]; | |
| 284 | } | ||
| 285 | |||
| 286 | // HOR_SD | ||
| 287 | 12038 | l = line + mh; | |
| 288 |
2/2✓ Branch 0 taken 524414 times.
✓ Branch 1 taken 12038 times.
|
536452 | for (lp = 0; lp < lv; lp++){ |
| 289 | 524414 | int i, j = 0; | |
| 290 | |||
| 291 |
2/2✓ Branch 0 taken 61241397 times.
✓ Branch 1 taken 524414 times.
|
61765811 | for (i = 0; i < lh; i++) |
| 292 | 61241397 | l[i] = t[w*lp + i]; | |
| 293 | |||
| 294 | 524414 | sd_1d97_int(line, mh, mh + lh); | |
| 295 | |||
| 296 | // copy back and deinterleave | ||
| 297 |
2/2✓ Branch 0 taken 30625229 times.
✓ Branch 1 taken 524414 times.
|
31149643 | for (i = mh; i < lh; i+=2, j++) |
| 298 | 30625229 | t[w*lp + j] = l[i]; | |
| 299 |
2/2✓ Branch 0 taken 30616168 times.
✓ Branch 1 taken 524414 times.
|
31140582 | for (i = 1-mh; i < lh; i+=2, j++) |
| 300 | 30616168 | t[w*lp + j] = l[i]; | |
| 301 | } | ||
| 302 | |||
| 303 | } | ||
| 304 | |||
| 305 |
2/2✓ Branch 0 taken 45940533 times.
✓ Branch 1 taken 1970 times.
|
45942503 | for (i = 0; i < w * h; i++) |
| 306 | 45940533 | t[i] = (t[i] + ((1<<(I_PRESHIFT))>>1)) >> (I_PRESHIFT); | |
| 307 | 1970 | } | |
| 308 | |||
| 309 | 4218617 | static void sr_1d53(unsigned *p, int i0, int i1) | |
| 310 | { | ||
| 311 | int i; | ||
| 312 | |||
| 313 |
2/2✓ Branch 0 taken 16610 times.
✓ Branch 1 taken 4202007 times.
|
4218617 | if (i1 <= i0 + 1) { |
| 314 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 16546 times.
|
16610 | if (i0 == 1) |
| 315 | 64 | p[1] = (int)p[1] >> 1; | |
| 316 | 16610 | return; | |
| 317 | } | ||
| 318 | |||
| 319 | 4202007 | extend53(p, i0, i1); | |
| 320 | |||
| 321 |
2/2✓ Branch 0 taken 226523583 times.
✓ Branch 1 taken 4202007 times.
|
230725590 | for (i = (i0 >> 1); i < (i1 >> 1) + 1; i++) |
| 322 | 226523583 | p[2 * i] -= (int)(p[2 * i - 1] + p[2 * i + 1] + 2) >> 2; | |
| 323 |
2/2✓ Branch 0 taken 222321576 times.
✓ Branch 1 taken 4202007 times.
|
226523583 | for (i = (i0 >> 1); i < (i1 >> 1); i++) |
| 324 | 222321576 | p[2 * i + 1] += (int)(p[2 * i] + p[2 * i + 2]) >> 1; | |
| 325 | } | ||
| 326 | |||
| 327 | 7320 | static void dwt_decode53(DWTContext *s, int *t) | |
| 328 | { | ||
| 329 | int lev; | ||
| 330 | 7320 | int w = s->linelen[s->ndeclevels - 1][0]; | |
| 331 | 7320 | int32_t *line = s->i_linebuf; | |
| 332 | 7320 | line += 3; | |
| 333 | |||
| 334 |
2/2✓ Branch 0 taken 41740 times.
✓ Branch 1 taken 7320 times.
|
49060 | for (lev = 0; lev < s->ndeclevels; lev++) { |
| 335 | 41740 | int lh = s->linelen[lev][0], | |
| 336 | 41740 | lv = s->linelen[lev][1], | |
| 337 | 41740 | mh = s->mod[lev][0], | |
| 338 | 41740 | mv = s->mod[lev][1], | |
| 339 | lp; | ||
| 340 | int *l; | ||
| 341 | |||
| 342 | // HOR_SD | ||
| 343 | 41740 | l = line + mh; | |
| 344 |
2/2✓ Branch 0 taken 1924595 times.
✓ Branch 1 taken 41740 times.
|
1966335 | for (lp = 0; lp < lv; lp++) { |
| 345 | 1924595 | int i, j = 0; | |
| 346 | // copy with interleaving | ||
| 347 |
2/2✓ Branch 0 taken 111199077 times.
✓ Branch 1 taken 1924595 times.
|
113123672 | for (i = mh; i < lh; i += 2, j++) |
| 348 | 111199077 | l[i] = t[w * lp + j]; | |
| 349 |
2/2✓ Branch 0 taken 111159329 times.
✓ Branch 1 taken 1924595 times.
|
113083924 | for (i = 1 - mh; i < lh; i += 2, j++) |
| 350 | 111159329 | l[i] = t[w * lp + j]; | |
| 351 | |||
| 352 | 1924595 | sr_1d53(line, mh, mh + lh); | |
| 353 | |||
| 354 |
2/2✓ Branch 0 taken 222358406 times.
✓ Branch 1 taken 1924595 times.
|
224283001 | for (i = 0; i < lh; i++) |
| 355 | 222358406 | t[w * lp + i] = l[i]; | |
| 356 | } | ||
| 357 | |||
| 358 | // VER_SD | ||
| 359 | 41740 | l = line + mv; | |
| 360 |
2/2✓ Branch 0 taken 2294022 times.
✓ Branch 1 taken 41740 times.
|
2335762 | for (lp = 0; lp < lh; lp++) { |
| 361 | 2294022 | int i, j = 0; | |
| 362 | // copy with interleaving | ||
| 363 |
2/2✓ Branch 0 taken 111196105 times.
✓ Branch 1 taken 2294022 times.
|
113490127 | for (i = mv; i < lv; i += 2, j++) |
| 364 | 111196105 | l[i] = t[w * j + lp]; | |
| 365 |
2/2✓ Branch 0 taken 111162301 times.
✓ Branch 1 taken 2294022 times.
|
113456323 | for (i = 1 - mv; i < lv; i += 2, j++) |
| 366 | 111162301 | l[i] = t[w * j + lp]; | |
| 367 | |||
| 368 | 2294022 | sr_1d53(line, mv, mv + lv); | |
| 369 | |||
| 370 |
2/2✓ Branch 0 taken 222358406 times.
✓ Branch 1 taken 2294022 times.
|
224652428 | for (i = 0; i < lv; i++) |
| 371 | 222358406 | t[w * i + lp] = l[i]; | |
| 372 | } | ||
| 373 | } | ||
| 374 | 7320 | } | |
| 375 | |||
| 376 | 6290 | static void sr_1d97_float(float *p, int i0, int i1) | |
| 377 | { | ||
| 378 | int i; | ||
| 379 | |||
| 380 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 6182 times.
|
6290 | if (i1 <= i0 + 1) { |
| 381 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 44 times.
|
108 | if (i0 == 1) |
| 382 | 64 | p[1] *= F_LFTG_K/2; | |
| 383 | else | ||
| 384 | 44 | p[0] *= F_LFTG_X; | |
| 385 | 108 | return; | |
| 386 | } | ||
| 387 | |||
| 388 | 6182 | extend97_float(p, i0, i1); | |
| 389 | |||
| 390 |
2/2✓ Branch 0 taken 214591 times.
✓ Branch 1 taken 6182 times.
|
220773 | for (i = (i0 >> 1) - 1; i < (i1 >> 1) + 2; i++) |
| 391 | 214591 | p[2 * i] -= F_LFTG_DELTA * (p[2 * i - 1] + p[2 * i + 1]); | |
| 392 | /* step 4 */ | ||
| 393 |
2/2✓ Branch 0 taken 208409 times.
✓ Branch 1 taken 6182 times.
|
214591 | for (i = (i0 >> 1) - 1; i < (i1 >> 1) + 1; i++) |
| 394 | 208409 | p[2 * i + 1] -= F_LFTG_GAMMA * (p[2 * i] + p[2 * i + 2]); | |
| 395 | /*step 5*/ | ||
| 396 |
2/2✓ Branch 0 taken 202227 times.
✓ Branch 1 taken 6182 times.
|
208409 | for (i = (i0 >> 1); i < (i1 >> 1) + 1; i++) |
| 397 | 202227 | p[2 * i] += F_LFTG_BETA * (p[2 * i - 1] + p[2 * i + 1]); | |
| 398 | /* step 6 */ | ||
| 399 |
2/2✓ Branch 0 taken 196045 times.
✓ Branch 1 taken 6182 times.
|
202227 | for (i = (i0 >> 1); i < (i1 >> 1); i++) |
| 400 | 196045 | p[2 * i + 1] += F_LFTG_ALPHA * (p[2 * i] + p[2 * i + 2]); | |
| 401 | } | ||
| 402 | |||
| 403 | 20 | static void dwt_decode97_float(DWTContext *s, float *t) | |
| 404 | { | ||
| 405 | int lev; | ||
| 406 | 20 | int w = s->linelen[s->ndeclevels - 1][0]; | |
| 407 | 20 | float *line = s->f_linebuf; | |
| 408 | 20 | float *data = t; | |
| 409 | /* position at index O of line range [0-5,w+5] cf. extend function */ | ||
| 410 | 20 | line += 5; | |
| 411 | |||
| 412 |
2/2✓ Branch 0 taken 338 times.
✓ Branch 1 taken 20 times.
|
358 | for (lev = 0; lev < s->ndeclevels; lev++) { |
| 413 | 338 | int lh = s->linelen[lev][0], | |
| 414 | 338 | lv = s->linelen[lev][1], | |
| 415 | 338 | mh = s->mod[lev][0], | |
| 416 | 338 | mv = s->mod[lev][1], | |
| 417 | lp; | ||
| 418 | float *l; | ||
| 419 | // HOR_SD | ||
| 420 | 338 | l = line + mh; | |
| 421 |
2/2✓ Branch 0 taken 3614 times.
✓ Branch 1 taken 338 times.
|
3952 | for (lp = 0; lp < lv; lp++) { |
| 422 | 3614 | int i, j = 0; | |
| 423 | // copy with interleaving | ||
| 424 |
2/2✓ Branch 0 taken 97979 times.
✓ Branch 1 taken 3614 times.
|
101593 | for (i = mh; i < lh; i += 2, j++) |
| 425 | 97979 | l[i] = data[w * lp + j]; | |
| 426 |
2/2✓ Branch 0 taken 98068 times.
✓ Branch 1 taken 3614 times.
|
101682 | for (i = 1 - mh; i < lh; i += 2, j++) |
| 427 | 98068 | l[i] = data[w * lp + j]; | |
| 428 | |||
| 429 | 3614 | sr_1d97_float(line, mh, mh + lh); | |
| 430 | |||
| 431 |
2/2✓ Branch 0 taken 196047 times.
✓ Branch 1 taken 3614 times.
|
199661 | for (i = 0; i < lh; i++) |
| 432 | 196047 | data[w * lp + i] = l[i]; | |
| 433 | } | ||
| 434 | |||
| 435 | // VER_SD | ||
| 436 | 338 | l = line + mv; | |
| 437 |
2/2✓ Branch 0 taken 2676 times.
✓ Branch 1 taken 338 times.
|
3014 | for (lp = 0; lp < lh; lp++) { |
| 438 | 2676 | int i, j = 0; | |
| 439 | // copy with interleaving | ||
| 440 |
2/2✓ Branch 0 taken 98016 times.
✓ Branch 1 taken 2676 times.
|
100692 | for (i = mv; i < lv; i += 2, j++) |
| 441 | 98016 | l[i] = data[w * j + lp]; | |
| 442 |
2/2✓ Branch 0 taken 98031 times.
✓ Branch 1 taken 2676 times.
|
100707 | for (i = 1 - mv; i < lv; i += 2, j++) |
| 443 | 98031 | l[i] = data[w * j + lp]; | |
| 444 | |||
| 445 | 2676 | sr_1d97_float(line, mv, mv + lv); | |
| 446 | |||
| 447 |
2/2✓ Branch 0 taken 196047 times.
✓ Branch 1 taken 2676 times.
|
198723 | for (i = 0; i < lv; i++) |
| 448 | 196047 | data[w * i + lp] = l[i]; | |
| 449 | } | ||
| 450 | } | ||
| 451 | 20 | } | |
| 452 | |||
| 453 | 1212525 | static void sr_1d97_int(int32_t *p, int i0, int i1) | |
| 454 | { | ||
| 455 | int i; | ||
| 456 | |||
| 457 |
2/2✓ Branch 0 taken 5058 times.
✓ Branch 1 taken 1207467 times.
|
1212525 | if (i1 <= i0 + 1) { |
| 458 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 4994 times.
|
5058 | if (i0 == 1) |
| 459 | 64 | p[1] = (p[1] * I_LFTG_K + (1<<16)) >> 17; | |
| 460 | else | ||
| 461 | 4994 | p[0] = (p[0] * I_LFTG_X + (1<<15)) >> 16; | |
| 462 | 5058 | return; | |
| 463 | } | ||
| 464 | |||
| 465 | 1207467 | extend97_int(p, i0, i1); | |
| 466 | |||
| 467 |
2/2✓ Branch 0 taken 85779182 times.
✓ Branch 1 taken 1207467 times.
|
86986649 | for (i = (i0 >> 1) - 1; i < (i1 >> 1) + 2; i++) |
| 468 | 85779182 | p[2 * i] -= (I_LFTG_DELTA * (p[2 * i - 1] + (int64_t)p[2 * i + 1]) + (1 << 15)) >> 16; | |
| 469 | /* step 4 */ | ||
| 470 |
2/2✓ Branch 0 taken 84571715 times.
✓ Branch 1 taken 1207467 times.
|
85779182 | for (i = (i0 >> 1) - 1; i < (i1 >> 1) + 1; i++) |
| 471 | 84571715 | p[2 * i + 1] -= (I_LFTG_GAMMA * (p[2 * i] + (int64_t)p[2 * i + 2]) + (1 << 15)) >> 16; | |
| 472 | /*step 5*/ | ||
| 473 |
2/2✓ Branch 0 taken 83364248 times.
✓ Branch 1 taken 1207467 times.
|
84571715 | for (i = (i0 >> 1); i < (i1 >> 1) + 1; i++) |
| 474 | 83364248 | p[2 * i] += (I_LFTG_BETA * (p[2 * i - 1] + (int64_t)p[2 * i + 1]) + (1 << 15)) >> 16; | |
| 475 | /* step 6 */ | ||
| 476 |
2/2✓ Branch 0 taken 82156781 times.
✓ Branch 1 taken 1207467 times.
|
83364248 | for (i = (i0 >> 1); i < (i1 >> 1); i++) { |
| 477 | 82156781 | const int64_t sum = p[2 * i] + (int64_t) p[2 * i + 2]; | |
| 478 | 82156781 | p[2 * i + 1] += sum; | |
| 479 | 82156781 | p[2 * i + 1] += (I_LFTG_ALPHA_PRIME * sum + (1 << 15)) >> 16; | |
| 480 | } | ||
| 481 | } | ||
| 482 | |||
| 483 | 1983 | static void dwt_decode97_int(DWTContext *s, int32_t *t) | |
| 484 | { | ||
| 485 | int lev; | ||
| 486 | 1983 | int w = s->linelen[s->ndeclevels - 1][0]; | |
| 487 | 1983 | int h = s->linelen[s->ndeclevels - 1][1]; | |
| 488 | int i; | ||
| 489 | 1983 | int32_t *line = s->i_linebuf; | |
| 490 | 1983 | int32_t *data = t; | |
| 491 | /* position at index O of line range [0-5,w+5] cf. extend function */ | ||
| 492 | 1983 | line += 5; | |
| 493 | |||
| 494 |
2/2✓ Branch 0 taken 12106 times.
✓ Branch 1 taken 1983 times.
|
14089 | for (lev = 0; lev < s->ndeclevels; lev++) { |
| 495 | 12106 | int lh = s->linelen[lev][0], | |
| 496 | 12106 | lv = s->linelen[lev][1], | |
| 497 | 12106 | mh = s->mod[lev][0], | |
| 498 | 12106 | mv = s->mod[lev][1], | |
| 499 | lp; | ||
| 500 | int32_t *l; | ||
| 501 | // HOR_SD | ||
| 502 | 12106 | l = line + mh; | |
| 503 |
2/2✓ Branch 0 taken 544697 times.
✓ Branch 1 taken 12106 times.
|
556803 | for (lp = 0; lp < lv; lp++) { |
| 504 | 544697 | int i, j = 0; | |
| 505 | // interleaving | ||
| 506 |
2/2✓ Branch 0 taken 41088158 times.
✓ Branch 1 taken 544697 times.
|
41632855 | for (i = mh; i < lh; i += 2, j++) |
| 507 | 41088158 | l[i] = data[w * lp + j]; | |
| 508 |
2/2✓ Branch 0 taken 41079026 times.
✓ Branch 1 taken 544697 times.
|
41623723 | for (i = 1 - mh; i < lh; i += 2, j++) |
| 509 | 41079026 | l[i] = data[w * lp + j]; | |
| 510 | |||
| 511 | 544697 | sr_1d97_int(line, mh, mh + lh); | |
| 512 | |||
| 513 |
2/2✓ Branch 0 taken 82167184 times.
✓ Branch 1 taken 544697 times.
|
82711881 | for (i = 0; i < lh; i++) |
| 514 | 82167184 | data[w * lp + i] = l[i]; | |
| 515 | } | ||
| 516 | |||
| 517 | // VER_SD | ||
| 518 | 12106 | l = line + mv; | |
| 519 |
2/2✓ Branch 0 taken 667828 times.
✓ Branch 1 taken 12106 times.
|
679934 | for (lp = 0; lp < lh; lp++) { |
| 520 | 667828 | int i, j = 0; | |
| 521 | // interleaving | ||
| 522 |
2/2✓ Branch 0 taken 41089375 times.
✓ Branch 1 taken 667828 times.
|
41757203 | for (i = mv; i < lv; i += 2, j++) |
| 523 | 41089375 | l[i] = data[w * j + lp]; | |
| 524 |
2/2✓ Branch 0 taken 41077809 times.
✓ Branch 1 taken 667828 times.
|
41745637 | for (i = 1 - mv; i < lv; i += 2, j++) |
| 525 | 41077809 | l[i] = data[w * j + lp]; | |
| 526 | |||
| 527 | 667828 | sr_1d97_int(line, mv, mv + lv); | |
| 528 | |||
| 529 |
2/2✓ Branch 0 taken 82167184 times.
✓ Branch 1 taken 667828 times.
|
82835012 | for (i = 0; i < lv; i++) |
| 530 | 82167184 | data[w * i + lp] = l[i]; | |
| 531 | } | ||
| 532 | } | ||
| 533 | |||
| 534 |
2/2✓ Branch 0 taken 61663658 times.
✓ Branch 1 taken 1983 times.
|
61665641 | for (i = 0; i < w * h; i++) |
| 535 | // We shift down by `I_PRESHIFT` because the input coefficients `datap[]` were shifted up by `I_PRESHIFT` to secure the precision | ||
| 536 | 61663658 | data[i] = (int32_t)(data[i] + ((1LL<<(I_PRESHIFT))>>1)) >> (I_PRESHIFT); | |
| 537 | 1983 | } | |
| 538 | |||
| 539 | 9493 | int ff_jpeg2000_dwt_init(DWTContext *s, int border[2][2], | |
| 540 | int decomp_levels, int type) | ||
| 541 | { | ||
| 542 | 9493 | int i, j, lev = decomp_levels, maxlen, | |
| 543 | b[2][2]; | ||
| 544 | |||
| 545 | 9493 | s->ndeclevels = decomp_levels; | |
| 546 | 9493 | s->type = type; | |
| 547 | |||
| 548 |
2/2✓ Branch 0 taken 18986 times.
✓ Branch 1 taken 9493 times.
|
28479 | for (i = 0; i < 2; i++) |
| 549 |
2/2✓ Branch 0 taken 37972 times.
✓ Branch 1 taken 18986 times.
|
56958 | for (j = 0; j < 2; j++) |
| 550 | 37972 | b[i][j] = border[i][j]; | |
| 551 | |||
| 552 | 9493 | maxlen = FFMAX(b[0][1] - b[0][0], | |
| 553 | b[1][1] - b[1][0]); | ||
| 554 |
2/2✓ Branch 0 taken 55198 times.
✓ Branch 1 taken 9493 times.
|
64691 | while (--lev >= 0) |
| 555 |
2/2✓ Branch 0 taken 110396 times.
✓ Branch 1 taken 55198 times.
|
165594 | for (i = 0; i < 2; i++) { |
| 556 | 110396 | s->linelen[lev][i] = b[i][1] - b[i][0]; | |
| 557 | 110396 | s->mod[lev][i] = b[i][0] & 1; | |
| 558 |
2/2✓ Branch 0 taken 220792 times.
✓ Branch 1 taken 110396 times.
|
331188 | for (j = 0; j < 2; j++) |
| 559 | 220792 | b[i][j] = (b[i][j] + 1) >> 1; | |
| 560 | } | ||
| 561 |
3/4✓ Branch 0 taken 20 times.
✓ Branch 1 taken 2022 times.
✓ Branch 2 taken 7451 times.
✗ Branch 3 not taken.
|
9493 | switch (type) { |
| 562 | 20 | case FF_DWT97: | |
| 563 | 20 | s->f_linebuf = av_malloc_array((maxlen + 12), sizeof(*s->f_linebuf)); | |
| 564 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | if (!s->f_linebuf) |
| 565 | ✗ | return AVERROR(ENOMEM); | |
| 566 | 20 | break; | |
| 567 | 2022 | case FF_DWT97_INT: | |
| 568 | 2022 | s->i_linebuf = av_malloc_array((maxlen + 12), sizeof(*s->i_linebuf)); | |
| 569 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2022 times.
|
2022 | if (!s->i_linebuf) |
| 570 | ✗ | return AVERROR(ENOMEM); | |
| 571 | 2022 | break; | |
| 572 | 7451 | case FF_DWT53: | |
| 573 | 7451 | s->i_linebuf = av_malloc_array((maxlen + 6), sizeof(*s->i_linebuf)); | |
| 574 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7451 times.
|
7451 | if (!s->i_linebuf) |
| 575 | ✗ | return AVERROR(ENOMEM); | |
| 576 | 7451 | break; | |
| 577 | ✗ | default: | |
| 578 | ✗ | return -1; | |
| 579 | } | ||
| 580 | 9493 | return 0; | |
| 581 | } | ||
| 582 | |||
| 583 | 8510 | int ff_dwt_encode(DWTContext *s, void *t) | |
| 584 | { | ||
| 585 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8510 times.
|
8510 | if (s->ndeclevels == 0) |
| 586 | ✗ | return 0; | |
| 587 | |||
| 588 |
3/4✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1970 times.
✓ Branch 2 taken 6520 times.
✗ Branch 3 not taken.
|
8510 | switch(s->type){ |
| 589 | 20 | case FF_DWT97: | |
| 590 | 20 | dwt_encode97_float(s, t); break; | |
| 591 | 1970 | case FF_DWT97_INT: | |
| 592 | 1970 | dwt_encode97_int(s, t); break; | |
| 593 | 6520 | case FF_DWT53: | |
| 594 | 6520 | dwt_encode53(s, t); break; | |
| 595 | ✗ | default: | |
| 596 | ✗ | return -1; | |
| 597 | } | ||
| 598 | 8510 | return 0; | |
| 599 | } | ||
| 600 | |||
| 601 | 9324 | int ff_dwt_decode(DWTContext *s, void *t) | |
| 602 | { | ||
| 603 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9323 times.
|
9324 | if (s->ndeclevels == 0) |
| 604 | 1 | return 0; | |
| 605 | |||
| 606 |
3/4✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1983 times.
✓ Branch 2 taken 7320 times.
✗ Branch 3 not taken.
|
9323 | switch (s->type) { |
| 607 | 20 | case FF_DWT97: | |
| 608 | 20 | dwt_decode97_float(s, t); | |
| 609 | 20 | break; | |
| 610 | 1983 | case FF_DWT97_INT: | |
| 611 | 1983 | dwt_decode97_int(s, t); | |
| 612 | 1983 | break; | |
| 613 | 7320 | case FF_DWT53: | |
| 614 | 7320 | dwt_decode53(s, t); | |
| 615 | 7320 | break; | |
| 616 | ✗ | default: | |
| 617 | ✗ | return -1; | |
| 618 | } | ||
| 619 | 9323 | return 0; | |
| 620 | } | ||
| 621 | |||
| 622 | 10484 | void ff_dwt_destroy(DWTContext *s) | |
| 623 | { | ||
| 624 | 10484 | av_freep(&s->f_linebuf); | |
| 625 | 10484 | av_freep(&s->i_linebuf); | |
| 626 | 10484 | } | |
| 627 |