d3162edaada2fc2b3efac0b808cb46adc130e931
[libav.git] / libavcodec / snow.c
1 /*
2 * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 #include "avcodec.h"
20 #include "common.h"
21 #include "dsputil.h"
22 #include "cabac.h"
23
24 #include "mpegvideo.h"
25
26 #undef NDEBUG
27 #include <assert.h>
28
29 #define MAX_DECOMPOSITIONS 8
30 #define MAX_PLANES 4
31 #define DWTELEM int
32 #define QROOT 8
33
34 static const int8_t quant3[256]={
35 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
36 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
37 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
38 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
39 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
40 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
41 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
42 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
43 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
44 -1,-1,-1,-1,-1,-1,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6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5,
156 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
157 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
158 -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
159 };
160
161 #define OBMC_MAX 64
162 #if 0 //64*cubic
163 static const uint8_t obmc32[1024]={
164 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
165 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
166 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0,
167 0, 0, 1, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 8, 8, 8, 7, 7, 6, 6, 5, 4, 4, 3, 2, 2, 1, 1, 0, 0,
168 0, 0, 1, 2, 2, 3, 4, 6, 7, 8, 9,10,11,12,12,12,12,12,12,11,10, 9, 8, 7, 6, 4, 3, 2, 2, 1, 0, 0,
169 0, 1, 1, 2, 3, 5, 6, 8,10,11,13,14,15,16,17,18,18,17,16,15,14,13,11,10, 8, 6, 5, 3, 2, 1, 1, 0,
170 0, 1, 1, 3, 4, 6, 8,10,13,15,17,19,20,22,22,23,23,22,22,20,19,17,15,13,10, 8, 6, 4, 3, 1, 1, 0,
171 0, 1, 2, 4, 6, 8,10,13,16,19,21,23,25,27,28,29,29,28,27,25,23,21,19,16,13,10, 8, 6, 4, 2, 1, 0,
172 0, 1, 2, 4, 7,10,13,16,19,22,25,28,31,33,34,35,35,34,33,31,28,25,22,19,16,13,10, 7, 4, 2, 1, 0,
173 0, 1, 3, 5, 8,11,15,19,22,26,30,33,36,38,40,41,41,40,38,36,33,30,26,22,19,15,11, 8, 5, 3, 1, 0,
174 0, 1, 3, 6, 9,12,17,21,25,30,34,38,41,44,45,46,46,45,44,41,38,34,30,25,21,17,12, 9, 6, 3, 1, 0,
175 0, 1, 3, 6,10,14,19,23,28,33,38,42,45,48,51,52,52,51,48,45,42,38,33,28,23,19,14,10, 6, 3, 1, 0,
176 0, 1, 4, 7,11,15,20,25,31,36,41,45,49,52,55,56,56,55,52,49,45,41,36,31,25,20,15,11, 7, 4, 1, 0,
177 0, 2, 4, 7,12,16,22,27,33,38,44,48,52,56,58,60,60,58,56,52,48,44,38,33,27,22,16,12, 7, 4, 2, 0,
178 0, 1, 4, 8,12,17,22,28,34,40,45,51,55,58,61,62,62,61,58,55,51,45,40,34,28,22,17,12, 8, 4, 1, 0,
179 0, 2, 4, 8,12,18,23,29,35,41,46,52,56,60,62,64,64,62,60,56,52,46,41,35,29,23,18,12, 8, 4, 2, 0,
180 0, 2, 4, 8,12,18,23,29,35,41,46,52,56,60,62,64,64,62,60,56,52,46,41,35,29,23,18,12, 8, 4, 2, 0,
181 0, 1, 4, 8,12,17,22,28,34,40,45,51,55,58,61,62,62,61,58,55,51,45,40,34,28,22,17,12, 8, 4, 1, 0,
182 0, 2, 4, 7,12,16,22,27,33,38,44,48,52,56,58,60,60,58,56,52,48,44,38,33,27,22,16,12, 7, 4, 2, 0,
183 0, 1, 4, 7,11,15,20,25,31,36,41,45,49,52,55,56,56,55,52,49,45,41,36,31,25,20,15,11, 7, 4, 1, 0,
184 0, 1, 3, 6,10,14,19,23,28,33,38,42,45,48,51,52,52,51,48,45,42,38,33,28,23,19,14,10, 6, 3, 1, 0,
185 0, 1, 3, 6, 9,12,17,21,25,30,34,38,41,44,45,46,46,45,44,41,38,34,30,25,21,17,12, 9, 6, 3, 1, 0,
186 0, 1, 3, 5, 8,11,15,19,22,26,30,33,36,38,40,41,41,40,38,36,33,30,26,22,19,15,11, 8, 5, 3, 1, 0,
187 0, 1, 2, 4, 7,10,13,16,19,22,25,28,31,33,34,35,35,34,33,31,28,25,22,19,16,13,10, 7, 4, 2, 1, 0,
188 0, 1, 2, 4, 6, 8,10,13,16,19,21,23,25,27,28,29,29,28,27,25,23,21,19,16,13,10, 8, 6, 4, 2, 1, 0,
189 0, 1, 1, 3, 4, 6, 8,10,13,15,17,19,20,22,22,23,23,22,22,20,19,17,15,13,10, 8, 6, 4, 3, 1, 1, 0,
190 0, 1, 1, 2, 3, 5, 6, 8,10,11,13,14,15,16,17,18,18,17,16,15,14,13,11,10, 8, 6, 5, 3, 2, 1, 1, 0,
191 0, 0, 1, 2, 2, 3, 4, 6, 7, 8, 9,10,11,12,12,12,12,12,12,11,10, 9, 8, 7, 6, 4, 3, 2, 2, 1, 0, 0,
192 0, 0, 1, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 8, 8, 8, 7, 7, 6, 6, 5, 4, 4, 3, 2, 2, 1, 1, 0, 0,
193 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0,
194 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
195 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
196 //error:0.000022
197 };
198 static const uint8_t obmc16[256]={
199 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
200 0, 1, 1, 2, 4, 5, 5, 6, 6, 5, 5, 4, 2, 1, 1, 0,
201 0, 1, 4, 6, 9,11,13,15,15,13,11, 9, 6, 4, 1, 0,
202 0, 2, 6,11,15,20,24,26,26,24,20,15,11, 6, 2, 0,
203 0, 4, 9,15,23,29,34,38,38,34,29,23,15, 9, 4, 0,
204 0, 5,11,20,29,38,45,49,49,45,38,29,20,11, 5, 0,
205 1, 5,13,24,34,45,53,57,57,53,45,34,24,13, 5, 1,
206 1, 6,15,26,38,49,57,62,62,57,49,38,26,15, 6, 1,
207 1, 6,15,26,38,49,57,62,62,57,49,38,26,15, 6, 1,
208 1, 5,13,24,34,45,53,57,57,53,45,34,24,13, 5, 1,
209 0, 5,11,20,29,38,45,49,49,45,38,29,20,11, 5, 0,
210 0, 4, 9,15,23,29,34,38,38,34,29,23,15, 9, 4, 0,
211 0, 2, 6,11,15,20,24,26,26,24,20,15,11, 6, 2, 0,
212 0, 1, 4, 6, 9,11,13,15,15,13,11, 9, 6, 4, 1, 0,
213 0, 1, 1, 2, 4, 5, 5, 6, 6, 5, 5, 4, 2, 1, 1, 0,
214 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
215 //error:0.000033
216 };
217 #elif 1 // 64*linear
218 static const uint8_t obmc32[1024]={
219 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
220 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 5, 5, 5, 4, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0,
221 0, 1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 8, 9,10,10, 9, 8, 8, 7, 7, 6, 5, 5, 4, 3, 3, 2, 2, 1, 0,
222 0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9,10,11,12,13,14,14,13,12,11,10, 9, 8, 7, 7, 6, 5, 4, 3, 2, 1, 0,
223 1, 2, 3, 4, 5, 6, 7, 8,10,11,12,13,14,15,16,17,17,16,15,14,13,12,11,10, 8, 7, 6, 5, 4, 3, 2, 1,
224 1, 2, 3, 5, 6, 8, 9,10,12,13,14,16,17,19,20,21,21,20,19,17,16,14,13,12,10, 9, 8, 6, 5, 3, 2, 1,
225 1, 2, 4, 6, 7, 9,11,12,14,15,17,19,20,22,24,25,25,24,22,20,19,17,15,14,12,11, 9, 7, 6, 4, 2, 1,
226 1, 3, 5, 7, 8,10,12,14,16,18,20,22,23,25,27,29,29,27,25,23,22,20,18,16,14,12,10, 8, 7, 5, 3, 1,
227 1, 3, 5, 7,10,12,14,16,18,20,22,24,27,29,31,33,33,31,29,27,24,22,20,18,16,14,12,10, 7, 5, 3, 1,
228 1, 4, 6, 8,11,13,15,18,20,23,25,27,30,32,34,37,37,34,32,30,27,25,23,20,18,15,13,11, 8, 6, 4, 1,
229 1, 4, 7, 9,12,14,17,20,22,25,28,30,33,35,38,41,41,38,35,33,30,28,25,22,20,17,14,12, 9, 7, 4, 1,
230 1, 4, 7,10,13,16,19,22,24,27,30,33,36,39,42,45,45,42,39,36,33,30,27,24,22,19,16,13,10, 7, 4, 1,
231 2, 5, 8,11,14,17,20,23,27,30,33,36,39,42,45,48,48,45,42,39,36,33,30,27,23,20,17,14,11, 8, 5, 2,
232 2, 5, 8,12,15,19,22,25,29,32,35,39,42,46,49,52,52,49,46,42,39,35,32,29,25,22,19,15,12, 8, 5, 2,
233 2, 5, 9,13,16,20,24,27,31,34,38,42,45,49,53,56,56,53,49,45,42,38,34,31,27,24,20,16,13, 9, 5, 2,
234 2, 6,10,14,17,21,25,29,33,37,41,45,48,52,56,60,60,56,52,48,45,41,37,33,29,25,21,17,14,10, 6, 2,
235 2, 6,10,14,17,21,25,29,33,37,41,45,48,52,56,60,60,56,52,48,45,41,37,33,29,25,21,17,14,10, 6, 2,
236 2, 5, 9,13,16,20,24,27,31,34,38,42,45,49,53,56,56,53,49,45,42,38,34,31,27,24,20,16,13, 9, 5, 2,
237 2, 5, 8,12,15,19,22,25,29,32,35,39,42,46,49,52,52,49,46,42,39,35,32,29,25,22,19,15,12, 8, 5, 2,
238 2, 5, 8,11,14,17,20,23,27,30,33,36,39,42,45,48,48,45,42,39,36,33,30,27,23,20,17,14,11, 8, 5, 2,
239 1, 4, 7,10,13,16,19,22,24,27,30,33,36,39,42,45,45,42,39,36,33,30,27,24,22,19,16,13,10, 7, 4, 1,
240 1, 4, 7, 9,12,14,17,20,22,25,28,30,33,35,38,41,41,38,35,33,30,28,25,22,20,17,14,12, 9, 7, 4, 1,
241 1, 4, 6, 8,11,13,15,18,20,23,25,27,30,32,34,37,37,34,32,30,27,25,23,20,18,15,13,11, 8, 6, 4, 1,
242 1, 3, 5, 7,10,12,14,16,18,20,22,24,27,29,31,33,33,31,29,27,24,22,20,18,16,14,12,10, 7, 5, 3, 1,
243 1, 3, 5, 7, 8,10,12,14,16,18,20,22,23,25,27,29,29,27,25,23,22,20,18,16,14,12,10, 8, 7, 5, 3, 1,
244 1, 2, 4, 6, 7, 9,11,12,14,15,17,19,20,22,24,25,25,24,22,20,19,17,15,14,12,11, 9, 7, 6, 4, 2, 1,
245 1, 2, 3, 5, 6, 8, 9,10,12,13,14,16,17,19,20,21,21,20,19,17,16,14,13,12,10, 9, 8, 6, 5, 3, 2, 1,
246 1, 2, 3, 4, 5, 6, 7, 8,10,11,12,13,14,15,16,17,17,16,15,14,13,12,11,10, 8, 7, 6, 5, 4, 3, 2, 1,
247 0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9,10,11,12,13,14,14,13,12,11,10, 9, 8, 7, 7, 6, 5, 4, 3, 2, 1, 0,
248 0, 1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 8, 9,10,10, 9, 8, 8, 7, 7, 6, 5, 5, 4, 3, 3, 2, 2, 1, 0,
249 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 5, 5, 5, 4, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0,
250 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
251 //error:0.000020
252 };
253 static const uint8_t obmc16[256]={
254 0, 1, 1, 2, 2, 3, 3, 4, 4, 3, 3, 2, 2, 1, 1, 0,
255 1, 2, 4, 5, 7, 8,10,11,11,10, 8, 7, 5, 4, 2, 1,
256 1, 4, 6, 9,11,14,16,19,19,16,14,11, 9, 6, 4, 1,
257 2, 5, 9,12,16,19,23,26,26,23,19,16,12, 9, 5, 2,
258 2, 7,11,16,20,25,29,34,34,29,25,20,16,11, 7, 2,
259 3, 8,14,19,25,30,36,41,41,36,30,25,19,14, 8, 3,
260 3,10,16,23,29,36,42,49,49,42,36,29,23,16,10, 3,
261 4,11,19,26,34,41,49,56,56,49,41,34,26,19,11, 4,
262 4,11,19,26,34,41,49,56,56,49,41,34,26,19,11, 4,
263 3,10,16,23,29,36,42,49,49,42,36,29,23,16,10, 3,
264 3, 8,14,19,25,30,36,41,41,36,30,25,19,14, 8, 3,
265 2, 7,11,16,20,25,29,34,34,29,25,20,16,11, 7, 2,
266 2, 5, 9,12,16,19,23,26,26,23,19,16,12, 9, 5, 2,
267 1, 4, 6, 9,11,14,16,19,19,16,14,11, 9, 6, 4, 1,
268 1, 2, 4, 5, 7, 8,10,11,11,10, 8, 7, 5, 4, 2, 1,
269 0, 1, 1, 2, 2, 3, 3, 4, 4, 3, 3, 2, 2, 1, 1, 0,
270 //error:0.000015
271 };
272 #else //64*cos
273 static const uint8_t obmc32[1024]={
274 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
275 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
276 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0,
277 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0,
278 0, 0, 1, 1, 2, 3, 4, 5, 6, 7, 9,10,11,11,12,12,12,12,11,11,10, 9, 7, 6, 5, 4, 3, 2, 1, 1, 0, 0,
279 0, 0, 1, 2, 3, 5, 6, 8, 9,11,12,14,15,16,17,17,17,17,16,15,14,12,11, 9, 8, 6, 5, 3, 2, 1, 0, 0,
280 0, 1, 1, 2, 4, 6, 8,10,12,15,17,19,20,21,22,23,23,22,21,20,19,17,15,12,10, 8, 6, 4, 2, 1, 1, 0,
281 0, 1, 2, 3, 5, 8,10,13,16,19,21,24,26,27,28,29,29,28,27,26,24,21,19,16,13,10, 8, 5, 3, 2, 1, 0,
282 0, 1, 2, 4, 6, 9,12,16,19,23,26,29,31,33,34,35,35,34,33,31,29,26,23,19,16,12, 9, 6, 4, 2, 1, 0,
283 0, 1, 3, 5, 7,11,15,19,23,26,30,34,37,39,40,41,41,40,39,37,34,30,26,23,19,15,11, 7, 5, 3, 1, 0,
284 0, 1, 3, 5, 9,12,17,21,26,30,35,38,42,44,46,47,47,46,44,42,38,35,30,26,21,17,12, 9, 5, 3, 1, 0,
285 0, 1, 3, 6, 9,14,19,24,29,34,38,43,46,49,51,52,52,51,49,46,43,38,34,29,24,19,14, 9, 6, 3, 1, 0,
286 0, 1, 3, 6,11,15,20,26,31,37,42,46,50,53,56,57,57,56,53,50,46,42,37,31,26,20,15,11, 6, 3, 1, 0,
287 0, 1, 3, 7,11,16,21,27,33,39,44,49,53,57,59,60,60,59,57,53,49,44,39,33,27,21,16,11, 7, 3, 1, 0,
288 0, 1, 4, 7,12,17,22,28,34,40,46,51,56,59,61,63,63,61,59,56,51,46,40,34,28,22,17,12, 7, 4, 1, 0,
289 0, 1, 4, 7,12,17,23,29,35,41,47,52,57,60,63,64,64,63,60,57,52,47,41,35,29,23,17,12, 7, 4, 1, 0,
290 0, 1, 4, 7,12,17,23,29,35,41,47,52,57,60,63,64,64,63,60,57,52,47,41,35,29,23,17,12, 7, 4, 1, 0,
291 0, 1, 4, 7,12,17,22,28,34,40,46,51,56,59,61,63,63,61,59,56,51,46,40,34,28,22,17,12, 7, 4, 1, 0,
292 0, 1, 3, 7,11,16,21,27,33,39,44,49,53,57,59,60,60,59,57,53,49,44,39,33,27,21,16,11, 7, 3, 1, 0,
293 0, 1, 3, 6,11,15,20,26,31,37,42,46,50,53,56,57,57,56,53,50,46,42,37,31,26,20,15,11, 6, 3, 1, 0,
294 0, 1, 3, 6, 9,14,19,24,29,34,38,43,46,49,51,52,52,51,49,46,43,38,34,29,24,19,14, 9, 6, 3, 1, 0,
295 0, 1, 3, 5, 9,12,17,21,26,30,35,38,42,44,46,47,47,46,44,42,38,35,30,26,21,17,12, 9, 5, 3, 1, 0,
296 0, 1, 3, 5, 7,11,15,19,23,26,30,34,37,39,40,41,41,40,39,37,34,30,26,23,19,15,11, 7, 5, 3, 1, 0,
297 0, 1, 2, 4, 6, 9,12,16,19,23,26,29,31,33,34,35,35,34,33,31,29,26,23,19,16,12, 9, 6, 4, 2, 1, 0,
298 0, 1, 2, 3, 5, 8,10,13,16,19,21,24,26,27,28,29,29,28,27,26,24,21,19,16,13,10, 8, 5, 3, 2, 1, 0,
299 0, 1, 1, 2, 4, 6, 8,10,12,15,17,19,20,21,22,23,23,22,21,20,19,17,15,12,10, 8, 6, 4, 2, 1, 1, 0,
300 0, 0, 1, 2, 3, 5, 6, 8, 9,11,12,14,15,16,17,17,17,17,16,15,14,12,11, 9, 8, 6, 5, 3, 2, 1, 0, 0,
301 0, 0, 1, 1, 2, 3, 4, 5, 6, 7, 9,10,11,11,12,12,12,12,11,11,10, 9, 7, 6, 5, 4, 3, 2, 1, 1, 0, 0,
302 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0,
303 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0,
304 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
305 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
306 //error:0.000022
307 };
308 static const uint8_t obmc16[256]={
309 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
310 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0,
311 0, 1, 3, 6, 8,11,13,14,14,13,11, 8, 6, 3, 1, 0,
312 0, 2, 6,10,15,20,24,26,26,24,20,15,10, 6, 2, 0,
313 0, 3, 8,16,23,30,35,38,38,35,30,23,16, 8, 3, 0,
314 1, 4,11,20,30,39,46,49,49,46,39,30,20,11, 4, 1,
315 1, 5,13,24,35,46,54,58,58,54,46,35,24,13, 5, 1,
316 0, 5,14,26,38,49,58,63,63,58,49,38,26,14, 5, 0,
317 0, 5,14,26,38,49,58,63,63,58,49,38,26,14, 5, 0,
318 1, 5,13,24,35,46,54,58,58,54,46,35,24,13, 5, 1,
319 1, 4,11,20,30,39,46,49,49,46,39,30,20,11, 4, 1,
320 0, 3, 8,16,23,30,35,38,38,35,30,23,16, 8, 3, 0,
321 0, 2, 6,10,15,20,24,26,26,24,20,15,10, 6, 2, 0,
322 0, 1, 3, 6, 8,11,13,14,14,13,11, 8, 6, 3, 1, 0,
323 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0,
324 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
325 //error:0.000022
326 };
327 #endif
328
329 typedef struct SubBand{
330 int level;
331 int stride;
332 int width;
333 int height;
334 int qlog; ///< log(qscale)/log[2^(1/6)]
335 DWTELEM *buf;
336 struct SubBand *parent;
337 uint8_t state[/*7*2*/ 7 + 512][32];
338 }SubBand;
339
340 typedef struct Plane{
341 int width;
342 int height;
343 SubBand band[MAX_DECOMPOSITIONS][4];
344 }Plane;
345
346 typedef struct SnowContext{
347 // MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to make the motion estimation eventually independant of MpegEncContext, so this will be removed then (FIXME/XXX)
348
349 AVCodecContext *avctx;
350 CABACContext c;
351 DSPContext dsp;
352 AVFrame input_picture;
353 AVFrame current_picture;
354 AVFrame last_picture;
355 AVFrame mconly_picture;
356 // uint8_t q_context[16];
357 uint8_t header_state[32];
358 int keyframe;
359 int version;
360 int spatial_decomposition_type;
361 int temporal_decomposition_type;
362 int spatial_decomposition_count;
363 int temporal_decomposition_count;
364 DWTELEM *spatial_dwt_buffer;
365 DWTELEM *pred_buffer;
366 int colorspace_type;
367 int chroma_h_shift;
368 int chroma_v_shift;
369 int spatial_scalability;
370 int qlog;
371 int mv_scale;
372 int qbias;
373 #define QBIAS_SHIFT 3
374 int b_width; //FIXME remove?
375 int b_height; //FIXME remove?
376 Plane plane[MAX_PLANES];
377 SubBand mb_band;
378 SubBand mv_band[2];
379
380 uint16_t *mb_type;
381 uint8_t *mb_mean;
382 uint32_t *dummy;
383 int16_t (*motion_val8)[2];
384 int16_t (*motion_val16)[2];
385 MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to make the motion estimation eventually independant of MpegEncContext, so this will be removed then (FIXME/XXX)
386 }SnowContext;
387
388 #define QEXPSHIFT 7 //FIXME try to change this to 0
389 static const uint8_t qexp[8]={
390 128, 140, 152, 166, 181, 197, 215, 235
391 // 64, 70, 76, 83, 91, 99, 108, 117
392 // 32, 35, 38, 41, 45, 49, 54, 59
393 // 16, 17, 19, 21, 23, 25, 27, 29
394 // 8, 9, 10, 10, 11, 12, 13, 15
395 };
396
397 static inline int mirror(int v, int m){
398 if (v<0) return -v;
399 else if(v>m) return 2*m-v;
400 else return v;
401 }
402
403 static inline void put_symbol(CABACContext *c, uint8_t *state, int v, int is_signed){
404 int i;
405
406 if(v){
407 const int a= ABS(v);
408 const int e= av_log2(a);
409 #if 1
410 const int el= FFMIN(e, 10);
411 put_cabac(c, state+0, 0);
412
413 for(i=0; i<el; i++){
414 put_cabac(c, state+1+i, 1); //1..10
415 }
416 for(; i<e; i++){
417 put_cabac(c, state+1+9, 1); //1..10
418 }
419 put_cabac(c, state+1+FFMIN(i,9), 0);
420
421 for(i=e-1; i>=el; i--){
422 put_cabac(c, state+22+9, (a>>i)&1); //22..31
423 }
424 for(; i>=0; i--){
425 put_cabac(c, state+22+i, (a>>i)&1); //22..31
426 }
427
428 if(is_signed)
429 put_cabac(c, state+11 + el, v < 0); //11..21
430 #else
431
432 put_cabac(c, state+0, 0);
433 if(e<=9){
434 for(i=0; i<e; i++){
435 put_cabac(c, state+1+i, 1); //1..10
436 }
437 put_cabac(c, state+1+i, 0);
438
439 for(i=e-1; i>=0; i--){
440 put_cabac(c, state+22+i, (a>>i)&1); //22..31
441 }
442
443 if(is_signed)
444 put_cabac(c, state+11 + e, v < 0); //11..21
445 }else{
446 for(i=0; i<e; i++){
447 put_cabac(c, state+1+FFMIN(i,9), 1); //1..10
448 }
449 put_cabac(c, state+1+FFMIN(i,9), 0);
450
451 for(i=e-1; i>=0; i--){
452 put_cabac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31
453 }
454
455 if(is_signed)
456 put_cabac(c, state+11 + FFMIN(e,10), v < 0); //11..21
457 }
458 #endif
459 }else{
460 put_cabac(c, state+0, 1);
461 }
462 }
463
464 static inline int get_symbol(CABACContext *c, uint8_t *state, int is_signed){
465 if(get_cabac(c, state+0))
466 return 0;
467 else{
468 int i, e, a, el;
469 //FIXME try to merge loops with FFMIN() maybe they are equally fast and they are surly cuter
470 for(e=0; e<10; e++){
471 if(get_cabac(c, state + 1 + e)==0) // 1..10
472 break;
473 }
474 el= e;
475
476 if(e==10){
477 while(get_cabac(c, state + 1 + 9)) //10
478 e++;
479 }
480 a= 1;
481 for(i=e-1; i>=el; i--){
482 a += a + get_cabac(c, state+22+9); //31
483 }
484 for(; i>=0; i--){
485 a += a + get_cabac(c, state+22+i); //22..31
486 }
487
488 if(is_signed && get_cabac(c, state+11 + el)) //11..21
489 return -a;
490 else
491 return a;
492 }
493 }
494
495 static inline void put_symbol2(CABACContext *c, uint8_t *state, int v, int log2){
496 int i;
497 int r= log2>=0 ? 1<<log2 : 1;
498
499 assert(v>=0);
500 assert(log2>=-4);
501
502 while(v >= r){
503 put_cabac(c, state+4+log2, 1);
504 v -= r;
505 log2++;
506 if(log2>0) r+=r;
507 }
508 put_cabac(c, state+4+log2, 0);
509
510 for(i=log2-1; i>=0; i--){
511 put_cabac(c, state+31-i, (v>>i)&1);
512 }
513 }
514
515 static inline int get_symbol2(CABACContext *c, uint8_t *state, int log2){
516 int i;
517 int r= log2>=0 ? 1<<log2 : 1;
518 int v=0;
519
520 assert(log2>=-4);
521
522 while(get_cabac(c, state+4+log2)){
523 v+= r;
524 log2++;
525 if(log2>0) r+=r;
526 }
527
528 for(i=log2-1; i>=0; i--){
529 v+= get_cabac(c, state+31-i)<<i;
530 }
531
532 return v;
533 }
534
535 static always_inline void lift(DWTELEM *dst, DWTELEM *src, DWTELEM *ref, int dst_step, int src_step, int ref_step, int width, int mul, int add, int shift, int highpass, int inverse){
536 const int mirror_left= !highpass;
537 const int mirror_right= (width&1) ^ highpass;
538 const int w= (width>>1) - 1 + (highpass & width);
539 int i;
540
541 #define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
542 if(mirror_left){
543 dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
544 dst += dst_step;
545 src += src_step;
546 }
547
548 for(i=0; i<w; i++){
549 dst[i*dst_step] = LIFT(src[i*src_step], ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift), inverse);
550 }
551
552 if(mirror_right){
553 dst[w*dst_step] = LIFT(src[w*src_step], ((mul*2*ref[w*ref_step]+add)>>shift), inverse);
554 }
555 }
556
557 static always_inline void lift5(DWTELEM *dst, DWTELEM *src, DWTELEM *ref, int dst_step, int src_step, int ref_step, int width, int mul, int add, int shift, int highpass, int inverse){
558 const int mirror_left= !highpass;
559 const int mirror_right= (width&1) ^ highpass;
560 const int w= (width>>1) - 1 + (highpass & width);
561 int i;
562
563 if(mirror_left){
564 int r= 3*2*ref[0];
565 r += r>>4;
566 r += r>>8;
567 dst[0] = LIFT(src[0], ((r+add)>>shift), inverse);
568 dst += dst_step;
569 src += src_step;
570 }
571
572 for(i=0; i<w; i++){
573 int r= 3*(ref[i*ref_step] + ref[(i+1)*ref_step]);
574 r += r>>4;
575 r += r>>8;
576 dst[i*dst_step] = LIFT(src[i*src_step], ((r+add)>>shift), inverse);
577 }
578
579 if(mirror_right){
580 int r= 3*2*ref[w*ref_step];
581 r += r>>4;
582 r += r>>8;
583 dst[w*dst_step] = LIFT(src[w*src_step], ((r+add)>>shift), inverse);
584 }
585 }
586
587
588 static void inplace_lift(int *dst, int width, int *coeffs, int n, int shift, int start, int inverse){
589 int x, i;
590
591 for(x=start; x<width; x+=2){
592 int64_t sum=0;
593
594 for(i=0; i<n; i++){
595 int x2= x + 2*i - n + 1;
596 if (x2< 0) x2= -x2;
597 else if(x2>=width) x2= 2*width-x2-2;
598 sum += coeffs[i]*(int64_t)dst[x2];
599 }
600 if(inverse) dst[x] -= (sum + (1<<shift)/2)>>shift;
601 else dst[x] += (sum + (1<<shift)/2)>>shift;
602 }
603 }
604
605 static void inplace_liftV(int *dst, int width, int height, int stride, int *coeffs, int n, int shift, int start, int inverse){
606 int x, y, i;
607 for(y=start; y<height; y+=2){
608 for(x=0; x<width; x++){
609 int64_t sum=0;
610
611 for(i=0; i<n; i++){
612 int y2= y + 2*i - n + 1;
613 if (y2< 0) y2= -y2;
614 else if(y2>=height) y2= 2*height-y2-2;
615 sum += coeffs[i]*(int64_t)dst[x + y2*stride];
616 }
617 if(inverse) dst[x + y*stride] -= (sum + (1<<shift)/2)>>shift;
618 else dst[x + y*stride] += (sum + (1<<shift)/2)>>shift;
619 }
620 }
621 }
622
623 #define SCALEX 1
624 #define LX0 0
625 #define LX1 1
626
627 #if 0 // more accurate 9/7
628 #define N1 2
629 #define SHIFT1 14
630 #define COEFFS1 (int[]){-25987,-25987}
631 #define N2 2
632 #define SHIFT2 19
633 #define COEFFS2 (int[]){-27777,-27777}
634 #define N3 2
635 #define SHIFT3 15
636 #define COEFFS3 (int[]){28931,28931}
637 #define N4 2
638 #define SHIFT4 15
639 #define COEFFS4 (int[]){14533,14533}
640 #elif 1 // 13/7 CRF
641 #define N1 4
642 #define SHIFT1 4
643 #define COEFFS1 (int[]){1,-9,-9,1}
644 #define N2 4
645 #define SHIFT2 4
646 #define COEFFS2 (int[]){-1,5,5,-1}
647 #define N3 0
648 #define SHIFT3 1
649 #define COEFFS3 NULL
650 #define N4 0
651 #define SHIFT4 1
652 #define COEFFS4 NULL
653 #elif 1 // 3/5
654 #define LX0 1
655 #define LX1 0
656 #define SCALEX 0.5
657 #define N1 2
658 #define SHIFT1 1
659 #define COEFFS1 (int[]){1,1}
660 #define N2 2
661 #define SHIFT2 2
662 #define COEFFS2 (int[]){-1,-1}
663 #define N3 0
664 #define SHIFT3 0
665 #define COEFFS3 NULL
666 #define N4 0
667 #define SHIFT4 0
668 #define COEFFS4 NULL
669 #elif 1 // 11/5
670 #define N1 0
671 #define SHIFT1 1
672 #define COEFFS1 NULL
673 #define N2 2
674 #define SHIFT2 2
675 #define COEFFS2 (int[]){-1,-1}
676 #define N3 2
677 #define SHIFT3 0
678 #define COEFFS3 (int[]){-1,-1}
679 #define N4 4
680 #define SHIFT4 7
681 #define COEFFS4 (int[]){-5,29,29,-5}
682 #define SCALEX 4
683 #elif 1 // 9/7 CDF
684 #define N1 2
685 #define SHIFT1 7
686 #define COEFFS1 (int[]){-203,-203}
687 #define N2 2
688 #define SHIFT2 12
689 #define COEFFS2 (int[]){-217,-217}
690 #define N3 2
691 #define SHIFT3 7
692 #define COEFFS3 (int[]){113,113}
693 #define N4 2
694 #define SHIFT4 9
695 #define COEFFS4 (int[]){227,227}
696 #define SCALEX 1
697 #elif 1 // 7/5 CDF
698 #define N1 0
699 #define SHIFT1 1
700 #define COEFFS1 NULL
701 #define N2 2
702 #define SHIFT2 2
703 #define COEFFS2 (int[]){-1,-1}
704 #define N3 2
705 #define SHIFT3 0
706 #define COEFFS3 (int[]){-1,-1}
707 #define N4 2
708 #define SHIFT4 4
709 #define COEFFS4 (int[]){3,3}
710 #elif 1 // 9/7 MN
711 #define N1 4
712 #define SHIFT1 4
713 #define COEFFS1 (int[]){1,-9,-9,1}
714 #define N2 2
715 #define SHIFT2 2
716 #define COEFFS2 (int[]){1,1}
717 #define N3 0
718 #define SHIFT3 1
719 #define COEFFS3 NULL
720 #define N4 0
721 #define SHIFT4 1
722 #define COEFFS4 NULL
723 #else // 13/7 CRF
724 #define N1 4
725 #define SHIFT1 4
726 #define COEFFS1 (int[]){1,-9,-9,1}
727 #define N2 4
728 #define SHIFT2 4
729 #define COEFFS2 (int[]){-1,5,5,-1}
730 #define N3 0
731 #define SHIFT3 1
732 #define COEFFS3 NULL
733 #define N4 0
734 #define SHIFT4 1
735 #define COEFFS4 NULL
736 #endif
737 static void horizontal_decomposeX(int *b, int width){
738 int temp[width];
739 const int width2= width>>1;
740 const int w2= (width+1)>>1;
741 int A1,A2,A3,A4, x;
742
743 inplace_lift(b, width, COEFFS1, N1, SHIFT1, LX1, 0);
744 inplace_lift(b, width, COEFFS2, N2, SHIFT2, LX0, 0);
745 inplace_lift(b, width, COEFFS3, N3, SHIFT3, LX1, 0);
746 inplace_lift(b, width, COEFFS4, N4, SHIFT4, LX0, 0);
747
748 for(x=0; x<width2; x++){
749 temp[x ]= b[2*x ];
750 temp[x+w2]= b[2*x + 1];
751 }
752 if(width&1)
753 temp[x ]= b[2*x ];
754 memcpy(b, temp, width*sizeof(int));
755 }
756
757 static void horizontal_composeX(int *b, int width){
758 int temp[width];
759 const int width2= width>>1;
760 int A1,A2,A3,A4, x;
761 const int w2= (width+1)>>1;
762
763 memcpy(temp, b, width*sizeof(int));
764 for(x=0; x<width2; x++){
765 b[2*x ]= temp[x ];
766 b[2*x + 1]= temp[x+w2];
767 }
768 if(width&1)
769 b[2*x ]= temp[x ];
770
771 inplace_lift(b, width, COEFFS4, N4, SHIFT4, LX0, 1);
772 inplace_lift(b, width, COEFFS3, N3, SHIFT3, LX1, 1);
773 inplace_lift(b, width, COEFFS2, N2, SHIFT2, LX0, 1);
774 inplace_lift(b, width, COEFFS1, N1, SHIFT1, LX1, 1);
775 }
776
777 static void spatial_decomposeX(int *buffer, int width, int height, int stride){
778 int x, y;
779
780 for(y=0; y<height; y++){
781 for(x=0; x<width; x++){
782 buffer[y*stride + x] *= SCALEX;
783 }
784 }
785
786 for(y=0; y<height; y++){
787 horizontal_decomposeX(buffer + y*stride, width);
788 }
789
790 inplace_liftV(buffer, width, height, stride, COEFFS1, N1, SHIFT1, LX1, 0);
791 inplace_liftV(buffer, width, height, stride, COEFFS2, N2, SHIFT2, LX0, 0);
792 inplace_liftV(buffer, width, height, stride, COEFFS3, N3, SHIFT3, LX1, 0);
793 inplace_liftV(buffer, width, height, stride, COEFFS4, N4, SHIFT4, LX0, 0);
794 }
795
796 static void spatial_composeX(int *buffer, int width, int height, int stride){
797 int x, y;
798
799 inplace_liftV(buffer, width, height, stride, COEFFS4, N4, SHIFT4, LX0, 1);
800 inplace_liftV(buffer, width, height, stride, COEFFS3, N3, SHIFT3, LX1, 1);
801 inplace_liftV(buffer, width, height, stride, COEFFS2, N2, SHIFT2, LX0, 1);
802 inplace_liftV(buffer, width, height, stride, COEFFS1, N1, SHIFT1, LX1, 1);
803
804 for(y=0; y<height; y++){
805 horizontal_composeX(buffer + y*stride, width);
806 }
807
808 for(y=0; y<height; y++){
809 for(x=0; x<width; x++){
810 buffer[y*stride + x] /= SCALEX;
811 }
812 }
813 }
814
815 static void horizontal_decompose53i(int *b, int width){
816 int temp[width];
817 const int width2= width>>1;
818 int A1,A2,A3,A4, x;
819 const int w2= (width+1)>>1;
820
821 for(x=0; x<width2; x++){
822 temp[x ]= b[2*x ];
823 temp[x+w2]= b[2*x + 1];
824 }
825 if(width&1)
826 temp[x ]= b[2*x ];
827 #if 0
828 A2= temp[1 ];
829 A4= temp[0 ];
830 A1= temp[0+width2];
831 A1 -= (A2 + A4)>>1;
832 A4 += (A1 + 1)>>1;
833 b[0+width2] = A1;
834 b[0 ] = A4;
835 for(x=1; x+1<width2; x+=2){
836 A3= temp[x+width2];
837 A4= temp[x+1 ];
838 A3 -= (A2 + A4)>>1;
839 A2 += (A1 + A3 + 2)>>2;
840 b[x+width2] = A3;
841 b[x ] = A2;
842
843 A1= temp[x+1+width2];
844 A2= temp[x+2 ];
845 A1 -= (A2 + A4)>>1;
846 A4 += (A1 + A3 + 2)>>2;
847 b[x+1+width2] = A1;
848 b[x+1 ] = A4;
849 }
850 A3= temp[width-1];
851 A3 -= A2;
852 A2 += (A1 + A3 + 2)>>2;
853 b[width -1] = A3;
854 b[width2-1] = A2;
855 #else
856 lift(b+w2, temp+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
857 lift(b , temp , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 0);
858 #endif
859 }
860
861 static void vertical_decompose53iH0(int *b0, int *b1, int *b2, int width){
862 int i;
863
864 for(i=0; i<width; i++){
865 b1[i] -= (b0[i] + b2[i])>>1;
866 }
867 }
868
869 static void vertical_decompose53iL0(int *b0, int *b1, int *b2, int width){
870 int i;
871
872 for(i=0; i<width; i++){
873 b1[i] += (b0[i] + b2[i] + 2)>>2;
874 }
875 }
876
877 static void spatial_decompose53i(int *buffer, int width, int height, int stride){
878 int x, y;
879 DWTELEM *b0= buffer + mirror(-2-1, height-1)*stride;
880 DWTELEM *b1= buffer + mirror(-2 , height-1)*stride;
881
882 for(y=-2; y<height; y+=2){
883 DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
884 DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
885
886 {START_TIMER
887 if(b1 <= b3) horizontal_decompose53i(b2, width);
888 if(y+2 < height) horizontal_decompose53i(b3, width);
889 STOP_TIMER("horizontal_decompose53i")}
890
891 {START_TIMER
892 if(b1 <= b3) vertical_decompose53iH0(b1, b2, b3, width);
893 if(b0 <= b2) vertical_decompose53iL0(b0, b1, b2, width);
894 STOP_TIMER("vertical_decompose53i*")}
895
896 b0=b2;
897 b1=b3;
898 }
899 }
900
901 #define lift5 lift
902 #if 1
903 #define W_AM 3
904 #define W_AO 0
905 #define W_AS 1
906
907 #define W_BM 1
908 #define W_BO 8
909 #define W_BS 4
910
911 #undef lift5
912 #define W_CM 9999
913 #define W_CO 2
914 #define W_CS 2
915
916 #define W_DM 15
917 #define W_DO 16
918 #define W_DS 5
919 #elif 0
920 #define W_AM 55
921 #define W_AO 16
922 #define W_AS 5
923
924 #define W_BM 3
925 #define W_BO 32
926 #define W_BS 6
927
928 #define W_CM 127
929 #define W_CO 64
930 #define W_CS 7
931
932 #define W_DM 7
933 #define W_DO 8
934 #define W_DS 4
935 #elif 0
936 #define W_AM 97
937 #define W_AO 32
938 #define W_AS 6
939
940 #define W_BM 63
941 #define W_BO 512
942 #define W_BS 10
943
944 #define W_CM 13
945 #define W_CO 8
946 #define W_CS 4
947
948 #define W_DM 15
949 #define W_DO 16
950 #define W_DS 5
951
952 #else
953
954 #define W_AM 203
955 #define W_AO 64
956 #define W_AS 7
957
958 #define W_BM 217
959 #define W_BO 2048
960 #define W_BS 12
961
962 #define W_CM 113
963 #define W_CO 64
964 #define W_CS 7
965
966 #define W_DM 227
967 #define W_DO 128
968 #define W_DS 9
969 #endif
970 static void horizontal_decompose97i(int *b, int width){
971 int temp[width];
972 const int w2= (width+1)>>1;
973
974 lift (temp+w2, b +1, b , 1, 2, 2, width, -W_AM, W_AO, W_AS, 1, 0);
975 lift (temp , b , temp+w2, 1, 2, 1, width, -W_BM, W_BO, W_BS, 0, 0);
976 lift5(b +w2, temp+w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 0);
977 lift (b , temp , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 0);
978 }
979
980
981 static void vertical_decompose97iH0(int *b0, int *b1, int *b2, int width){
982 int i;
983
984 for(i=0; i<width; i++){
985 b1[i] -= (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
986 }
987 }
988
989 static void vertical_decompose97iH1(int *b0, int *b1, int *b2, int width){
990 int i;
991
992 for(i=0; i<width; i++){
993 #ifdef lift5
994 b1[i] += (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
995 #else
996 int r= 3*(b0[i] + b2[i]);
997 r+= r>>4;
998 r+= r>>8;
999 b1[i] += (r+W_CO)>>W_CS;
1000 #endif
1001 }
1002 }
1003
1004 static void vertical_decompose97iL0(int *b0, int *b1, int *b2, int width){
1005 int i;
1006
1007 for(i=0; i<width; i++){
1008 b1[i] -= (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
1009 }
1010 }
1011
1012 static void vertical_decompose97iL1(int *b0, int *b1, int *b2, int width){
1013 int i;
1014
1015 for(i=0; i<width; i++){
1016 b1[i] += (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
1017 }
1018 }
1019
1020 static void spatial_decompose97i(int *buffer, int width, int height, int stride){
1021 int x, y;
1022 DWTELEM *b0= buffer + mirror(-4-1, height-1)*stride;
1023 DWTELEM *b1= buffer + mirror(-4 , height-1)*stride;
1024 DWTELEM *b2= buffer + mirror(-4+1, height-1)*stride;
1025 DWTELEM *b3= buffer + mirror(-4+2, height-1)*stride;
1026
1027 for(y=-4; y<height; y+=2){
1028 DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
1029 DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
1030
1031 {START_TIMER
1032 if(b3 <= b5) horizontal_decompose97i(b4, width);
1033 if(y+4 < height) horizontal_decompose97i(b5, width);
1034 if(width>400){
1035 STOP_TIMER("horizontal_decompose97i")
1036 }}
1037
1038 {START_TIMER
1039 if(b3 <= b5) vertical_decompose97iH0(b3, b4, b5, width);
1040 if(b2 <= b4) vertical_decompose97iL0(b2, b3, b4, width);
1041 if(b1 <= b3) vertical_decompose97iH1(b1, b2, b3, width);
1042 if(b0 <= b2) vertical_decompose97iL1(b0, b1, b2, width);
1043
1044 if(width>400){
1045 STOP_TIMER("vertical_decompose97i")
1046 }}
1047
1048 b0=b2;
1049 b1=b3;
1050 b2=b4;
1051 b3=b5;
1052 }
1053 }
1054
1055 static void spatial_dwt(SnowContext *s, int *buffer, int width, int height, int stride){
1056 int level;
1057
1058 for(level=0; level<s->spatial_decomposition_count; level++){
1059 switch(s->spatial_decomposition_type){
1060 case 0: spatial_decompose97i(buffer, width>>level, height>>level, stride<<level); break;
1061 case 1: spatial_decompose53i(buffer, width>>level, height>>level, stride<<level); break;
1062 case 2: spatial_decomposeX (buffer, width>>level, height>>level, stride<<level); break;
1063 }
1064 }
1065 }
1066
1067 static void horizontal_compose53i(int *b, int width){
1068 int temp[width];
1069 const int width2= width>>1;
1070 const int w2= (width+1)>>1;
1071 int A1,A2,A3,A4, x;
1072
1073 #if 0
1074 A2= temp[1 ];
1075 A4= temp[0 ];
1076 A1= temp[0+width2];
1077 A1 -= (A2 + A4)>>1;
1078 A4 += (A1 + 1)>>1;
1079 b[0+width2] = A1;
1080 b[0 ] = A4;
1081 for(x=1; x+1<width2; x+=2){
1082 A3= temp[x+width2];
1083 A4= temp[x+1 ];
1084 A3 -= (A2 + A4)>>1;
1085 A2 += (A1 + A3 + 2)>>2;
1086 b[x+width2] = A3;
1087 b[x ] = A2;
1088
1089 A1= temp[x+1+width2];
1090 A2= temp[x+2 ];
1091 A1 -= (A2 + A4)>>1;
1092 A4 += (A1 + A3 + 2)>>2;
1093 b[x+1+width2] = A1;
1094 b[x+1 ] = A4;
1095 }
1096 A3= temp[width-1];
1097 A3 -= A2;
1098 A2 += (A1 + A3 + 2)>>2;
1099 b[width -1] = A3;
1100 b[width2-1] = A2;
1101 #else
1102 lift(temp , b , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 1);
1103 lift(temp+w2, b+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 1);
1104 #endif
1105 for(x=0; x<width2; x++){
1106 b[2*x ]= temp[x ];
1107 b[2*x + 1]= temp[x+w2];
1108 }
1109 if(width&1)
1110 b[2*x ]= temp[x ];
1111 }
1112
1113 static void vertical_compose53iH0(int *b0, int *b1, int *b2, int width){
1114 int i;
1115
1116 for(i=0; i<width; i++){
1117 b1[i] += (b0[i] + b2[i])>>1;
1118 }
1119 }
1120
1121 static void vertical_compose53iL0(int *b0, int *b1, int *b2, int width){
1122 int i;
1123
1124 for(i=0; i<width; i++){
1125 b1[i] -= (b0[i] + b2[i] + 2)>>2;
1126 }
1127 }
1128
1129 static void spatial_compose53i(int *buffer, int width, int height, int stride){
1130 int x, y;
1131 DWTELEM *b0= buffer + mirror(-1-1, height-1)*stride;
1132 DWTELEM *b1= buffer + mirror(-1 , height-1)*stride;
1133
1134 for(y=-1; y<=height; y+=2){
1135 DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
1136 DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
1137
1138 {START_TIMER
1139 if(b1 <= b3) vertical_compose53iL0(b1, b2, b3, width);
1140 if(b0 <= b2) vertical_compose53iH0(b0, b1, b2, width);
1141 STOP_TIMER("vertical_compose53i*")}
1142
1143 {START_TIMER
1144 if(y-1 >= 0) horizontal_compose53i(b0, width);
1145 if(b0 <= b2) horizontal_compose53i(b1, width);
1146 STOP_TIMER("horizontal_compose53i")}
1147
1148 b0=b2;
1149 b1=b3;
1150 }
1151 }
1152
1153
1154 static void horizontal_compose97i(int *b, int width){
1155 int temp[width];
1156 const int w2= (width+1)>>1;
1157
1158 lift (temp , b , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 1);
1159 lift5(temp+w2, b +w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 1);
1160 lift (b , temp , temp+w2, 2, 1, 1, width, -W_BM, W_BO, W_BS, 0, 1);
1161 lift (b+1 , temp+w2, b , 2, 1, 2, width, -W_AM, W_AO, W_AS, 1, 1);
1162 }
1163
1164 static void vertical_compose97iH0(int *b0, int *b1, int *b2, int width){
1165 int i;
1166
1167 for(i=0; i<width; i++){
1168 b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
1169 }
1170 }
1171
1172 static void vertical_compose97iH1(int *b0, int *b1, int *b2, int width){
1173 int i;
1174
1175 for(i=0; i<width; i++){
1176 #ifdef lift5
1177 b1[i] -= (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
1178 #else
1179 int r= 3*(b0[i] + b2[i]);
1180 r+= r>>4;
1181 r+= r>>8;
1182 b1[i] -= (r+W_CO)>>W_CS;
1183 #endif
1184 }
1185 }
1186
1187 static void vertical_compose97iL0(int *b0, int *b1, int *b2, int width){
1188 int i;
1189
1190 for(i=0; i<width; i++){
1191 b1[i] += (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
1192 }
1193 }
1194
1195 static void vertical_compose97iL1(int *b0, int *b1, int *b2, int width){
1196 int i;
1197
1198 for(i=0; i<width; i++){
1199 b1[i] -= (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
1200 }
1201 }
1202
1203 static void spatial_compose97i(int *buffer, int width, int height, int stride){
1204 int x, y;
1205 DWTELEM *b0= buffer + mirror(-3-1, height-1)*stride;
1206 DWTELEM *b1= buffer + mirror(-3 , height-1)*stride;
1207 DWTELEM *b2= buffer + mirror(-3+1, height-1)*stride;
1208 DWTELEM *b3= buffer + mirror(-3+2, height-1)*stride;
1209
1210 for(y=-3; y<=height; y+=2){
1211 DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
1212 DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
1213
1214 if(stride == width && y+4 < height && 0){
1215 int x;
1216 for(x=0; x<width/2; x++)
1217 b5[x] += 64*2;
1218 for(; x<width; x++)
1219 b5[x] += 169*2;
1220 }
1221
1222 {START_TIMER
1223 if(b3 <= b5) vertical_compose97iL1(b3, b4, b5, width);
1224 if(b2 <= b4) vertical_compose97iH1(b2, b3, b4, width);
1225 if(b1 <= b3) vertical_compose97iL0(b1, b2, b3, width);
1226 if(b0 <= b2) vertical_compose97iH0(b0, b1, b2, width);
1227 if(width>400){
1228 STOP_TIMER("vertical_compose97i")}}
1229
1230 {START_TIMER
1231 if(y-1>= 0) horizontal_compose97i(b0, width);
1232 if(b0 <= b2) horizontal_compose97i(b1, width);
1233 if(width>400 && b0 <= b2){
1234 STOP_TIMER("horizontal_compose97i")}}
1235
1236 b0=b2;
1237 b1=b3;
1238 b2=b4;
1239 b3=b5;
1240 }
1241 }
1242
1243 static void spatial_idwt(SnowContext *s, int *buffer, int width, int height, int stride){
1244 int level;
1245
1246 for(level=s->spatial_decomposition_count-1; level>=0; level--){
1247 switch(s->spatial_decomposition_type){
1248 case 0: spatial_compose97i(buffer, width>>level, height>>level, stride<<level); break;
1249 case 1: spatial_compose53i(buffer, width>>level, height>>level, stride<<level); break;
1250 case 2: spatial_composeX (buffer, width>>level, height>>level, stride<<level); break;
1251 }
1252 }
1253 }
1254
1255 static const int hilbert[16][2]={
1256 {0,0}, {1,0}, {1,1}, {0,1},
1257 {0,2}, {0,3}, {1,3}, {1,2},
1258 {2,2}, {2,3}, {3,3}, {3,2},
1259 {3,1}, {2,1}, {2,0}, {3,0},
1260 };
1261 #if 0
1262 -o o-
1263 | |
1264 o-o
1265
1266 -o-o o-o-
1267 | |
1268 o-o o-o
1269 | |
1270 o o-o o
1271 | | | |
1272 o-o o-o
1273
1274 0112122312232334122323342334
1275 0123456789ABCDEF0123456789AB
1276 RLLRMRRLLRRMRLLMLRRLMLLRRLLM
1277
1278 4 B F 14 1B
1279 4 11 15 20 27
1280
1281 -o o-o-o o-o-o o-
1282 | | | | | |
1283 o-o o-o o-o o-o
1284 | |
1285 o-o o-o o-o o-o
1286 | | | | | |
1287 o o-o-o o-o-o o
1288 | |
1289 o-o o-o-o-o o-o
1290 | | | |
1291 o-o o-o o-o o-o
1292 | | | |
1293 o o-o o o o-o o
1294 | | | | | | | |
1295 o-o o-o o-o o-o
1296
1297 #endif
1298
1299 #define SVI(a, i, x, y) \
1300 {\
1301 a[i][0]= x;\
1302 a[i][1]= y;\
1303 i++;\
1304 }
1305
1306 static int sig_cmp(const void *a, const void *b){
1307 const int16_t* da = (const int16_t *) a;
1308 const int16_t* db = (const int16_t *) b;
1309
1310 if(da[1] != db[1]) return da[1] - db[1];
1311 else return da[0] - db[0];
1312 }
1313
1314 static int deint(unsigned int a){
1315 a &= 0x55555555; //0 1 2 3 4 5 6 7 8 9 A B C D E F
1316 a += a & 0x11111111; // 01 23 45 67 89 AB CD EF
1317 a += 3*(a & 0x0F0F0F0F);// 0123 4567 89AB CDEF
1318 a += 15*(a & 0x00FF00FF);// 01234567 89ABCDEF
1319 a +=255*(a & 0x0000FFFF);// 0123456789ABCDEF
1320 return a>>15;
1321 }
1322
1323 static void encode_subband_z0run(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
1324 const int level= b->level;
1325 const int w= b->width;
1326 const int h= b->height;
1327 int x, y, pos;
1328
1329 if(1){
1330 int run=0;
1331 int runs[w*h];
1332 int run_index=0;
1333 int count=0;
1334
1335 for(pos=0; ; pos++){
1336 int x= deint(pos );
1337 int y= deint(pos>>1);
1338 int v, p=0, pr=0, pd=0;
1339 int /*ll=0, */l=0, lt=0, t=0/*, rt=0*/;
1340
1341 if(x>=w || y>=h){
1342 if(x>=w && y>=h)
1343 break;
1344 continue;
1345 }
1346 count++;
1347
1348 v= src[x + y*stride];
1349
1350 if(y){
1351 t= src[x + (y-1)*stride];
1352 if(x){
1353 lt= src[x - 1 + (y-1)*stride];
1354 }
1355 if(x + 1 < w){
1356 /*rt= src[x + 1 + (y-1)*stride]*/;
1357 }
1358 }
1359 if(x){
1360 l= src[x - 1 + y*stride];
1361 /*if(x > 1){
1362 if(orientation==1) ll= src[y + (x-2)*stride];
1363 else ll= src[x - 2 + y*stride];
1364 }*/
1365 }
1366 if(parent){
1367 int px= x>>1;
1368 int py= y>>1;
1369 if(px<b->parent->width && py<b->parent->height){
1370 p= parent[px + py*2*stride];
1371 /*if(px+1<b->parent->width)
1372 pr= parent[px + 1 + py*2*stride];
1373 if(py+1<b->parent->height)
1374 pd= parent[px + (py+1)*2*stride];*/
1375 }
1376 }
1377 if(!(/*ll|*/l|lt|t|/*rt|*/p)){
1378 if(v){
1379 runs[run_index++]= run;
1380 run=0;
1381 }else{
1382 run++;
1383 }
1384 }
1385 }
1386 assert(count==w*h);
1387 runs[run_index++]= run;
1388 run_index=0;
1389 run= runs[run_index++];
1390
1391 put_symbol(&s->c, b->state[1], run, 0);
1392
1393 for(pos=0; ; pos++){
1394 int x= deint(pos );
1395 int y= deint(pos>>1);
1396 int v, p=0, pr=0, pd=0;
1397 int /*ll=0, */l=0, lt=0, t=0/*, rt=0*/;
1398
1399 if(x>=w || y>=h){
1400 if(x>=w && y>=h)
1401 break;
1402 continue;
1403 }
1404 v= src[x + y*stride];
1405
1406 if(y){
1407 t= src[x + (y-1)*stride];
1408 if(x){
1409 lt= src[x - 1 + (y-1)*stride];
1410 }
1411 if(x + 1 < w){
1412 // rt= src[x + 1 + (y-1)*stride];
1413 }
1414 }
1415 if(x){
1416 l= src[x - 1 + y*stride];
1417 /*if(x > 1){
1418 if(orientation==1) ll= src[y + (x-2)*stride];
1419 else ll= src[x - 2 + y*stride];
1420 }*/
1421 }
1422
1423 if(parent){
1424 int px= x>>1;
1425 int py= y>>1;
1426 if(px<b->parent->width && py<b->parent->height){
1427 p= parent[px + py*2*stride];
1428 /* if(px+1<b->parent->width)
1429 pr= parent[px + 1 + py*2*stride];
1430 if(py+1<b->parent->height)
1431 pd= parent[px + (py+1)*2*stride];*/
1432 }
1433 }
1434 if(/*ll|*/l|lt|t|/*rt|*/p){
1435 int context= av_log2(/*ABS(ll) + */2*(3*ABS(l) + ABS(lt) + 2*ABS(t) + /*ABS(rt) +*/ ABS(p)));
1436
1437 put_cabac(&s->c, &b->state[0][context], !!v);
1438 }else{
1439 if(!run){
1440 run= runs[run_index++];
1441 put_symbol(&s->c, b->state[1], run, 0);
1442 assert(v);
1443 }else{
1444 run--;
1445 assert(!v);
1446 }
1447 }
1448 if(v){
1449 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + /*ABS(rt) +*/ ABS(p));
1450
1451 put_symbol(&s->c, b->state[context + 2], ABS(v)-1, 0);
1452 put_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]], v<0);
1453 }
1454 }
1455 }
1456 }
1457
1458 static void encode_subband_bp(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
1459 const int level= b->level;
1460 const int w= b->width;
1461 const int h= b->height;
1462 int x, y;
1463
1464 #if 0
1465 int plane;
1466 for(plane=24; plane>=0; plane--){
1467 int run=0;
1468 int runs[w*h];
1469 int run_index=0;
1470
1471 for(y=0; y<h; y++){
1472 for(x=0; x<w; x++){
1473 int v, lv, p=0;
1474 int d=0, r=0, rd=0, ld=0;
1475 int /*ll=0, */l=0, lt=0, t=0, rt=0;
1476 v= src[x + y*stride];
1477
1478 if(y){
1479 t= src[x + (y-1)*stride];
1480 if(x){
1481 lt= src[x - 1 + (y-1)*stride];
1482 }
1483 if(x + 1 < w){
1484 rt= src[x + 1 + (y-1)*stride];
1485 }
1486 }
1487 if(x){
1488 l= src[x - 1 + y*stride];
1489 /*if(x > 1){
1490 if(orientation==1) ll= src[y + (x-2)*stride];
1491 else ll= src[x - 2 + y*stride];
1492 }*/
1493 }
1494 if(y+1<h){
1495 d= src[x + (y+1)*stride];
1496 if(x) ld= src[x - 1 + (y+1)*stride];
1497 if(x + 1 < w) rd= src[x + 1 + (y+1)*stride];
1498 }
1499 if(x + 1 < w)
1500 r= src[x + 1 + y*stride];
1501 if(parent){
1502 int px= x>>1;
1503 int py= y>>1;
1504 if(px<b->parent->width && py<b->parent->height)
1505 p= parent[px + py*2*stride];
1506 }
1507 #define HIDE(c, plane) c= c>=0 ? c&((-1)<<(plane)) : -((-c)&((-1)<<(plane)));
1508 lv=v;
1509 HIDE( v, plane)
1510 HIDE(lv, plane+1)
1511 HIDE( p, plane)
1512 HIDE( l, plane)
1513 HIDE(lt, plane)
1514 HIDE( t, plane)
1515 HIDE(rt, plane)
1516 HIDE( r, plane+1)
1517 HIDE(rd, plane+1)
1518 HIDE( d, plane+1)
1519 HIDE(ld, plane+1)
1520 if(!(/*ll|*/l|lt|t|rt|r|rd|ld|d|p|lv)){
1521 if(v){
1522 runs[run_index++]= run;
1523 run=0;
1524 }else{
1525 run++;
1526 }
1527 }
1528 }
1529 }
1530 runs[run_index++]= run;
1531 run_index=0;
1532 run= runs[run_index++];
1533
1534 put_symbol(&s->c, b->state[1], run, 0);
1535
1536 for(y=0; y<h; y++){
1537 for(x=0; x<w; x++){
1538 int v, p=0, lv;
1539 int /*ll=0, */l=0, lt=0, t=0, rt=0;
1540 int d=0, r=0, rd=0, ld=0;
1541 v= src[x + y*stride];
1542
1543 if(y){
1544 t= src[x + (y-1)*stride];
1545 if(x){
1546 lt= src[x - 1 + (y-1)*stride];
1547 }
1548 if(x + 1 < w){
1549 rt= src[x + 1 + (y-1)*stride];
1550 }
1551 }
1552 if(x){
1553 l= src[x - 1 + y*stride];
1554 /*if(x > 1){
1555 if(orientation==1) ll= src[y + (x-2)*stride];
1556 else ll= src[x - 2 + y*stride];
1557 }*/
1558 }
1559 if(y+1<h){
1560 d= src[x + (y+1)*stride];
1561 if(x) ld= src[x - 1 + (y+1)*stride];
1562 if(x + 1 < w) rd= src[x + 1 + (y+1)*stride];
1563 }
1564 if(x + 1 < w)
1565 r= src[x + 1 + y*stride];
1566
1567 if(parent){
1568 int px= x>>1;
1569 int py= y>>1;
1570 if(px<b->parent->width && py<b->parent->height)
1571 p= parent[px + py*2*stride];
1572 }
1573 lv=v;
1574 HIDE( v, plane)
1575 HIDE(lv, plane+1)
1576 HIDE( p, plane)
1577 HIDE( l, plane)
1578 HIDE(lt, plane)
1579 HIDE( t, plane)
1580 HIDE(rt, plane)
1581 HIDE( r, plane+1)
1582 HIDE(rd, plane+1)
1583 HIDE( d, plane+1)
1584 HIDE(ld, plane+1)
1585 if(/*ll|*/l|lt|t|rt|r|rd|ld|d|p|lv){
1586 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p)
1587 +3*ABS(r) + ABS(rd) + 2*ABS(d) + ABS(ld));
1588
1589 if(lv) put_cabac(&s->c, &b->state[99][context + 8*(av_log2(ABS(lv))-plane)], !!(v-lv));
1590 else put_cabac(&s->c, &b->state[ 0][context], !!v);
1591 }else{
1592 assert(!lv);
1593 if(!run){
1594 run= runs[run_index++];
1595 put_symbol(&s->c, b->state[1], run, 0);
1596 assert(v);
1597 }else{
1598 run--;
1599 assert(!v);
1600 }
1601 }
1602 if(v && !lv){
1603 int context= clip(quant3b[l&0xFF] + quant3b[r&0xFF], -1,1)
1604 + 3*clip(quant3b[t&0xFF] + quant3b[d&0xFF], -1,1);
1605 put_cabac(&s->c, &b->state[0][16 + 1 + 3 + context], v<0);
1606 }
1607 }
1608 }
1609 }
1610 return;
1611 #endif
1612 }
1613
1614 static void encode_subband_X(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
1615 const int level= b->level;
1616 const int w= b->width;
1617 const int h= b->height;
1618 int x, y;
1619
1620 #if 0
1621 if(orientation==3 && parent && 0){
1622 int16_t candidate[w*h][2];
1623 uint8_t state[w*h];
1624 int16_t boarder[3][w*h*4][2];
1625 int16_t significant[w*h][2];
1626 int candidate_count=0;
1627 int boarder_count[3]={0,0,0};
1628 int significant_count=0;
1629 int rle_pos=0;
1630 int v, last_v;
1631 int primary= orientation==1;
1632
1633 memset(candidate, 0, sizeof(candidate));
1634 memset(state, 0, sizeof(state));
1635 memset(boarder, 0, sizeof(boarder));
1636
1637 for(y=0; y<h; y++){
1638 for(x=0; x<w; x++){
1639 if(parent[(x>>1) + (y>>1)*2*stride])
1640 SVI(candidate, candidate_count, x, y)
1641 }
1642 }
1643
1644 for(;;){
1645 while(candidate_count && !boarder_count[0] && !boarder_count[1] && !boarder_count[2]){
1646 candidate_count--;
1647 x= candidate[ candidate_count][0];
1648 y= candidate[ candidate_count][1];
1649 if(state[x + y*w])
1650 continue;
1651 state[x + y*w]= 1;
1652 v= !!src[x + y*stride];
1653 put_cabac(&s->c, &b->state[0][0], v);
1654 if(v){
1655 SVI(significant, significant_count, x,y)
1656 if(x && !state[x - 1 + y *w]) SVI(boarder[0],boarder_count[0],x-1,y )
1657 if(y && !state[x + (y-1)*w]) SVI(boarder[1],boarder_count[1],x ,y-1)
1658 if(x+1<w && !state[x + 1 + y *w]) SVI(boarder[0],boarder_count[0],x+1,y )
1659 if(y+1<h && !state[x + (y+1)*w]) SVI(boarder[1],boarder_count[1],x ,y+1)
1660 if(x && y && !state[x - 1 + (y-1)*w]) SVI(boarder[2],boarder_count[2],x-1,y-1)
1661 if(x && y+1<h && !state[x - 1 + (y+1)*w]) SVI(boarder[2],boarder_count[2],x-1,y+1)
1662 if(x+1<w && y+1<h && !state[x + 1 + (y+1)*w]) SVI(boarder[2],boarder_count[2],x+1,y+1)
1663 if(x+1<w && y && !state[x + 1 + (y-1)*w]) SVI(boarder[2],boarder_count[2],x+1,y-1)
1664 }
1665 }
1666 while(!boarder_count[0] && !boarder_count[1] && !boarder_count[2] && rle_pos < w*h){
1667 int run=0;
1668 for(; rle_pos < w*h;){
1669 x= rle_pos % w; //FIXME speed
1670 y= rle_pos / w;
1671 rle_pos++;
1672 if(state[x + y*w])
1673 continue;
1674 state[x + y*w]= 1;
1675 v= !!src[x + y*stride];
1676 if(v){
1677 put_symbol(&s->c, b->state[1], run, 0);
1678 SVI(significant, significant_count, x,y)
1679 if(x && !state[x - 1 + y *w]) SVI(boarder[0],boarder_count[0],x-1,y )
1680 if(y && !state[x + (y-1)*w]) SVI(boarder[1],boarder_count[1],x ,y-1)
1681 if(x+1<w && !state[x + 1 + y *w]) SVI(boarder[0],boarder_count[0],x+1,y )
1682 if(y+1<h && !state[x + (y+1)*w]) SVI(boarder[1],boarder_count[1],x ,y+1)
1683 if(x && y && !state[x - 1 + (y-1)*w]) SVI(boarder[2],boarder_count[2],x-1,y-1)
1684 if(x && y+1<h && !state[x - 1 + (y+1)*w]) SVI(boarder[2],boarder_count[2],x-1,y+1)
1685 if(x+1<w && y+1<h && !state[x + 1 + (y+1)*w]) SVI(boarder[2],boarder_count[2],x+1,y+1)
1686 if(x+1<w && y && !state[x + 1 + (y-1)*w]) SVI(boarder[2],boarder_count[2],x+1,y-1)
1687 break;
1688 //FIXME note only right & down can be boarders
1689 }
1690 run++;
1691 }
1692 }
1693 if(!boarder_count[0] && !boarder_count[1] && !boarder_count[2])
1694 break;
1695
1696 while(boarder_count[0] || boarder_count[1] || boarder_count[2]){
1697 int index;
1698
1699 if (boarder_count[ primary]) index= primary;
1700 else if(boarder_count[1-primary]) index=1-primary;
1701 else index=2;
1702
1703 boarder_count[index]--;
1704 x= boarder[index][ boarder_count[index] ][0];
1705 y= boarder[index][ boarder_count[index] ][1];
1706 if(state[x + y*w]) //FIXME maybe check earlier
1707 continue;
1708 state[x + y*w]= 1;
1709 v= !!src[x + y*stride];
1710 put_cabac(&s->c, &b->state[0][index+1], v);
1711 if(v){
1712 SVI(significant, significant_count, x,y)
1713 if(x && !state[x - 1 + y *w]) SVI(boarder[0],boarder_count[0],x-1,y )
1714 if(y && !state[x + (y-1)*w]) SVI(boarder[1],boarder_count[1],x ,y-1)
1715 if(x+1<w && !state[x + 1 + y *w]) SVI(boarder[0],boarder_count[0],x+1,y )
1716 if(y+1<h && !state[x + (y+1)*w]) SVI(boarder[1],boarder_count[1],x ,y+1)
1717 if(x && y && !state[x - 1 + (y-1)*w]) SVI(boarder[2],boarder_count[2],x-1,y-1)
1718 if(x && y+1<h && !state[x - 1 + (y+1)*w]) SVI(boarder[2],boarder_count[2],x-1,y+1)
1719 if(x+1<w && y+1<h && !state[x + 1 + (y+1)*w]) SVI(boarder[2],boarder_count[2],x+1,y+1)
1720 if(x+1<w && y && !state[x + 1 + (y-1)*w]) SVI(boarder[2],boarder_count[2],x+1,y-1)
1721 }
1722 }
1723 }
1724 //FIXME sort significant coeffs maybe
1725 if(1){
1726 qsort(significant, significant_count, sizeof(int16_t[2]), sig_cmp);
1727 }
1728
1729 last_v=1;
1730 while(significant_count){
1731 int context= 3 + quant7[last_v&0xFF]; //use significance of suroundings
1732 significant_count--;
1733 x= significant[significant_count][0];//FIXME try opposit direction
1734 y= significant[significant_count][1];
1735 v= src[x + y*stride];
1736 put_symbol(&s->c, b->state[context + 2], v, 1); //FIXME try to avoid first bit, try this with the old code too!!
1737 last_v= v;
1738 }
1739 }
1740 #endif
1741 }
1742
1743 static void encode_subband_c0run(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
1744 const int level= b->level;
1745 const int w= b->width;
1746 const int h= b->height;
1747 int x, y;
1748
1749 if(1){
1750 int run=0;
1751 int runs[w*h];
1752 int run_index=0;
1753
1754 for(y=0; y<h; y++){
1755 for(x=0; x<w; x++){
1756 int v, p=0;
1757 int /*ll=0, */l=0, lt=0, t=0, rt=0;
1758 v= src[x + y*stride];
1759
1760 if(y){
1761 t= src[x + (y-1)*stride];
1762 if(x){
1763 lt= src[x - 1 + (y-1)*stride];
1764 }
1765 if(x + 1 < w){
1766 rt= src[x + 1 + (y-1)*stride];
1767 }
1768 }
1769 if(x){
1770 l= src[x - 1 + y*stride];
1771 /*if(x > 1){
1772 if(orientation==1) ll= src[y + (x-2)*stride];
1773 else ll= src[x - 2 + y*stride];
1774 }*/
1775 }
1776 if(parent){
1777 int px= x>>1;
1778 int py= y>>1;
1779 if(px<b->parent->width && py<b->parent->height)
1780 p= parent[px + py*2*stride];
1781 }
1782 if(!(/*ll|*/l|lt|t|rt|p)){
1783 if(v){
1784 runs[run_index++]= run;
1785 run=0;
1786 }else{
1787 run++;
1788 }
1789 }
1790 }
1791 }
1792 runs[run_index++]= run;
1793 run_index=0;
1794 run= runs[run_index++];
1795
1796 put_symbol2(&s->c, b->state[1], run, 3);
1797
1798 for(y=0; y<h; y++){
1799 for(x=0; x<w; x++){
1800 int v, p=0;
1801 int /*ll=0, */l=0, lt=0, t=0, rt=0;
1802 v= src[x + y*stride];
1803
1804 if(y){
1805 t= src[x + (y-1)*stride];
1806 if(x){
1807 lt= src[x - 1 + (y-1)*stride];
1808 }
1809 if(x + 1 < w){
1810 rt= src[x + 1 + (y-1)*stride];
1811 }
1812 }
1813 if(x){
1814 l= src[x - 1 + y*stride];
1815 /*if(x > 1){
1816 if(orientation==1) ll= src[y + (x-2)*stride];
1817 else ll= src[x - 2 + y*stride];
1818 }*/
1819 }
1820 if(parent){
1821 int px= x>>1;
1822 int py= y>>1;
1823 if(px<b->parent->width && py<b->parent->height)
1824 p= parent[px + py*2*stride];
1825 }
1826 if(/*ll|*/l|lt|t|rt|p){
1827 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
1828
1829 put_cabac(&s->c, &b->state[0][context], !!v);
1830 }else{
1831 if(!run){
1832 run= runs[run_index++];
1833
1834 put_symbol2(&s->c, b->state[1], run, 3);
1835 assert(v);
1836 }else{
1837 run--;
1838 assert(!v);
1839 }
1840 }
1841 if(v){
1842 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
1843
1844 put_symbol2(&s->c, b->state[context + 2], ABS(v)-1, context-4);
1845 put_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]], v<0);
1846 }
1847 }
1848 }
1849 }
1850 }
1851
1852 static void encode_subband(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
1853 // encode_subband_qtree(s, b, src, parent, stride, orientation);
1854 // encode_subband_z0run(s, b, src, parent, stride, orientation);
1855 encode_subband_c0run(s, b, src, parent, stride, orientation);
1856 // encode_subband_dzr(s, b, src, parent, stride, orientation);
1857 }
1858
1859 static inline void decode_subband(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
1860 const int level= b->level;
1861 const int w= b->width;
1862 const int h= b->height;
1863 int x,y;
1864
1865 START_TIMER
1866 #if 0
1867 for(y=0; y<b->height; y++)
1868 memset(&src[y*stride], 0, b->width*sizeof(DWTELEM));
1869
1870 int plane;
1871 for(plane=24; plane>=0; plane--){
1872 int run;
1873
1874 run= get_symbol(&s->c, b->state[1], 0);
1875
1876 #define HIDE(c, plane) c= c>=0 ? c&((-1)<<(plane)) : -((-c)&((-1)<<(plane)));
1877
1878 for(y=0; y<h; y++){
1879 for(x=0; x<w; x++){
1880 int v, p=0, lv;
1881 int /*ll=0, */l=0, lt=0, t=0, rt=0;
1882 int d=0, r=0, rd=0, ld=0;
1883 lv= src[x + y*stride];
1884
1885 if(y){
1886 t= src[x + (y-1)*stride];
1887 if(x){
1888 lt= src[x - 1 + (y-1)*stride];
1889 }
1890 if(x + 1 < w){
1891 rt= src[x + 1 + (y-1)*stride];
1892 }
1893 }
1894 if(x){
1895 l= src[x - 1 + y*stride];
1896 /*if(x > 1){
1897 if(orientation==1) ll= src[y + (x-2)*stride];
1898 else ll= src[x - 2 + y*stride];
1899 }*/
1900 }
1901 if(y+1<h){
1902 d= src[x + (y+1)*stride];
1903 if(x) ld= src[x - 1 + (y+1)*stride];
1904 if(x + 1 < w) rd= src[x + 1 + (y+1)*stride];
1905 }
1906 if(x + 1 < w)
1907 r= src[x + 1 + y*stride];
1908
1909 if(parent){
1910 int px= x>>1;
1911 int py= y>>1;
1912 if(px<b->parent->width && py<b->parent->height)
1913 p= parent[px + py*2*stride];
1914 }
1915 HIDE( p, plane)
1916 if(/*ll|*/l|lt|t|rt|r|rd|ld|d|p|lv){
1917 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p)
1918 +3*ABS(r) + ABS(rd) + 2*ABS(d) + ABS(ld));
1919
1920 if(lv){
1921 assert(context + 8*av_log2(ABS(lv)) < 512 - 100);
1922 if(get_cabac(&s->c, &b->state[99][context + 8*(av_log2(ABS(lv))-plane)])){
1923 if(lv<0) v= lv - (1<<plane);
1924 else v= lv + (1<<plane);
1925 }else
1926 v=lv;
1927 }else{
1928 v= get_cabac(&s->c, &b->state[ 0][context]) << plane;
1929 }
1930 }else{
1931 assert(!lv);
1932 if(!run){
1933 run= get_symbol(&s->c, b->state[1], 0);
1934 v= 1<<plane;
1935 }else{
1936 run--;
1937 v=0;
1938 }
1939 }
1940 if(v && !lv){
1941 int context= clip(quant3b[l&0xFF] + quant3b[r&0xFF], -1,1)
1942 + 3*clip(quant3b[t&0xFF] + quant3b[d&0xFF], -1,1);
1943 if(get_cabac(&s->c, &b->state[0][16 + 1 + 3 + context]))
1944 v= -v;
1945 }
1946 src[x + y*stride]= v;
1947 }
1948 }
1949 }
1950 return;
1951 #endif
1952 if(1){
1953 int run;
1954
1955 for(y=0; y<b->height; y++)
1956 memset(&src[y*stride], 0, b->width*sizeof(DWTELEM));
1957
1958 run= get_symbol2(&s->c, b->state[1], 3);
1959 for(y=0; y<h; y++){
1960 for(x=0; x<w; x++){
1961 int v, p=0;
1962 int /*ll=0, */l=0, lt=0, t=0, rt=0;
1963
1964 if(y){
1965 t= src[x + (y-1)*stride];
1966 if(x){
1967 lt= src[x - 1 + (y-1)*stride];
1968 }
1969 if(x + 1 < w){
1970 rt= src[x + 1 + (y-1)*stride];
1971 }
1972 }
1973 if(x){
1974 l= src[x - 1 + y*stride];
1975 /*if(x > 1){
1976 if(orientation==1) ll= src[y + (x-2)*stride];
1977 else ll= src[x - 2 + y*stride];
1978 }*/
1979 }
1980 if(parent){
1981 int px= x>>1;
1982 int py= y>>1;
1983 if(px<b->parent->width && py<b->parent->height)
1984 p= parent[px + py*2*stride];
1985 }
1986 if(/*ll|*/l|lt|t|rt|p){
1987 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
1988
1989 v=get_cabac(&s->c, &b->state[0][context]);
1990 }else{
1991 if(!run){
1992 run= get_symbol2(&s->c, b->state[1], 3);
1993 //FIXME optimize this here
1994 //FIXME try to store a more naive run
1995 v=1;
1996 }else{
1997 run--;
1998 v=0;
1999 }
2000 }
2001 if(v){
2002 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
2003 v= get_symbol2(&s->c, b->state[context + 2], context-4) + 1;
2004 if(get_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]]))
2005 v= -v;
2006 src[x + y*stride]= v;
2007 }
2008 }
2009 }
2010 if(level+1 == s->spatial_decomposition_count){
2011 STOP_TIMER("decode_subband")
2012 }
2013
2014 return;
2015 }
2016 }
2017
2018 static void reset_contexts(SnowContext *s){
2019 int plane_index, level, orientation;
2020
2021 for(plane_index=0; plane_index<2; plane_index++){
2022 for(level=0; level<s->spatial_decomposition_count; level++){
2023 for(orientation=level ? 1:0; orientation<4; orientation++){
2024 memset(s->plane[plane_index].band[level][orientation].state, 0, sizeof(s->plane[plane_index].band[level][orientation].state));
2025 }
2026 }
2027 }
2028 memset(s->mb_band.state, 0, sizeof(s->mb_band.state));
2029 memset(s->mv_band[0].state, 0, sizeof(s->mv_band[0].state));
2030 memset(s->mv_band[1].state, 0, sizeof(s->mv_band[1].state));
2031 memset(s->header_state, 0, sizeof(s->header_state));
2032 }
2033
2034 static void mc_block(uint8_t *dst, uint8_t *src, uint8_t *tmp, int stride, int b_w, int b_h, int dx, int dy){
2035 int x, y;
2036
2037 for(y=0; y < b_h+5; y++){
2038 for(x=0; x < b_w; x++){
2039 int a0= src[x + y*stride];
2040 int a1= src[x + 1 + y*stride];
2041 int a2= src[x + 2 + y*stride];
2042 int a3= src[x + 3 + y*stride];
2043 int a4= src[x + 4 + y*stride];
2044 int a5= src[x + 5 + y*stride];
2045 // int am= 9*(a1+a2) - (a0+a3);
2046 int am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
2047 // int am= 18*(a2+a3) - 2*(a1+a4);
2048 // int aL= (-7*a0 + 105*a1 + 35*a2 - 5*a3)>>3;
2049 // int aR= (-7*a3 + 105*a2 + 35*a1 - 5*a0)>>3;
2050
2051 // if(b_w==16) am= 8*(a1+a2);
2052
2053 if(dx<8) tmp[x + y*stride]= (32*a2*( 8-dx) + am* dx + 128)>>8;
2054 else tmp[x + y*stride]= ( am*(16-dx) + 32*a3*(dx-8) + 128)>>8;
2055
2056 /* if (dx< 4) tmp[x + y*stride]= (16*a1*( 4-dx) + aL* dx + 32)>>6;
2057 else if(dx< 8) tmp[x + y*stride]= ( aL*( 8-dx) + am*(dx- 4) + 32)>>6;
2058 else if(dx<12) tmp[x + y*stride]= ( am*(12-dx) + aR*(dx- 8) + 32)>>6;
2059 else tmp[x + y*stride]= ( aR*(16-dx) + 16*a2*(dx-12) + 32)>>6;*/
2060 }
2061 }
2062 for(y=0; y < b_h; y++){
2063 for(x=0; x < b_w; x++){
2064 int a0= tmp[x + y *stride];
2065 int a1= tmp[x + (y + 1)*stride];
2066 int a2= tmp[x + (y + 2)*stride];
2067 int a3= tmp[x + (y + 3)*stride];
2068 int a4= tmp[x + (y + 4)*stride];
2069 int a5= tmp[x + (y + 5)*stride];
2070 int am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
2071 // int am= 18*(a2+a3) - 2*(a1+a4);
2072 /* int aL= (-7*a0 + 105*a1 + 35*a2 - 5*a3)>>3;
2073 int aR= (-7*a3 + 105*a2 + 35*a1 - 5*a0)>>3;*/
2074
2075 // if(b_w==16) am= 8*(a1+a2);
2076
2077 if(dy<8) dst[x + y*stride]= (32*a2*( 8-dy) + am* dy + 128)>>8;
2078 else dst[x + y*stride]= ( am*(16-dy) + 32*a3*(dy-8) + 128)>>8;
2079
2080 /* if (dy< 4) tmp[x + y*stride]= (16*a1*( 4-dy) + aL* dy + 32)>>6;
2081 else if(dy< 8) tmp[x + y*stride]= ( aL*( 8-dy) + am*(dy- 4) + 32)>>6;
2082 else if(dy<12) tmp[x + y*stride]= ( am*(12-dy) + aR*(dy- 8) + 32)>>6;
2083 else tmp[x + y*stride]= ( aR*(16-dy) + 16*a2*(dy-12) + 32)>>6;*/
2084 }
2085 }
2086 }
2087
2088 #define mcb(dx,dy,b_w)\
2089 static void mc_block ## dx ## dy(uint8_t *dst, uint8_t *src, int stride){\
2090 uint8_t tmp[stride*(b_w+5)];\
2091 mc_block(dst, src-2-2*stride, tmp, stride, b_w, b_w, dx, dy);\
2092 }
2093
2094 mcb( 0, 0,16)
2095 mcb( 4, 0,16)
2096 mcb( 8, 0,16)
2097 mcb(12, 0,16)
2098 mcb( 0, 4,16)
2099 mcb( 4, 4,16)
2100 mcb( 8, 4,16)
2101 mcb(12, 4,16)
2102 mcb( 0, 8,16)
2103 mcb( 4, 8,16)
2104 mcb( 8, 8,16)
2105 mcb(12, 8,16)
2106 mcb( 0,12,16)
2107 mcb( 4,12,16)
2108 mcb( 8,12,16)
2109 mcb(12,12,16)
2110
2111 #define mca(dx,dy,b_w)\
2112 static void mc_block_hpel ## dx ## dy(uint8_t *dst, uint8_t *src, int stride, int h){\
2113 uint8_t tmp[stride*(b_w+5)];\
2114 assert(h==b_w);\
2115 mc_block(dst, src-2-2*stride, tmp, stride, b_w, b_w, dx, dy);\
2116 }
2117
2118 mca( 0, 0,16)
2119 mca( 8, 0,16)
2120 mca( 0, 8,16)
2121 mca( 8, 8,16)
2122
2123 static void add_xblock(DWTELEM *dst, uint8_t *src, uint8_t *obmc, int s_x, int s_y, int b_w, int b_h, int mv_x, int mv_y, int w, int h, int dst_stride, int src_stride, int obmc_stride, int mb_type, int add){
2124 uint8_t tmp[src_stride*(b_h+5)]; //FIXME move to context to gurantee alignment
2125 int x,y;
2126
2127 if(s_x<0){
2128 obmc -= s_x;
2129 b_w += s_x;
2130 s_x=0;
2131 }else if(s_x + b_w > w){
2132 b_w = w - s_x;
2133 }
2134 if(s_y<0){
2135 obmc -= s_y*obmc_stride;
2136 b_h += s_y;
2137 s_y=0;
2138 }else if(s_y + b_h> h){
2139 b_h = h - s_y;
2140 }
2141
2142 if(b_w<=0 || b_h<=0) return;
2143
2144 dst += s_x + s_y*dst_stride;
2145
2146 if(mb_type==1){
2147 src += s_x + s_y*src_stride;
2148 for(y=0; y < b_h; y++){
2149 for(x=0; x < b_w; x++){
2150 if(add) dst[x + y*dst_stride] += obmc[x + y*obmc_stride] * 128 * (256/OBMC_MAX);
2151 else dst[x + y*dst_stride] -= obmc[x + y*obmc_stride] * 128 * (256/OBMC_MAX);
2152 }
2153 }
2154 }else{
2155 int dx= mv_x&15;
2156 int dy= mv_y&15;
2157 // int dxy= (mv_x&1) + 2*(mv_y&1);
2158
2159 s_x += (mv_x>>4) - 2;
2160 s_y += (mv_y>>4) - 2;
2161 src += s_x + s_y*src_stride;
2162 //use dsputil
2163
2164 if( (unsigned)s_x >= w - b_w - 4
2165 || (unsigned)s_y >= h - b_h - 4){
2166 ff_emulated_edge_mc(tmp + 32, src, src_stride, b_w+5, b_h+5, s_x, s_y, w, h);
2167 src= tmp + 32;
2168 }
2169
2170 if(mb_type==0){
2171 mc_block(tmp, src, tmp + 64+8, src_stride, b_w, b_h, dx, dy);
2172 }else{
2173 int sum=0;
2174 for(y=0; y < b_h; y++){
2175 for(x=0; x < b_w; x++){
2176 sum += src[x+ y*src_stride];
2177 }
2178 }
2179 sum= (sum + b_h*b_w/2) / (b_h*b_w);
2180 for(y=0; y < b_h; y++){
2181 for(x=0; x < b_w; x++){
2182 tmp[x + y*src_stride]= sum;
2183 }
2184 }
2185 }
2186
2187 for(y=0; y < b_h; y++){
2188 for(x=0; x < b_w; x++){
2189 if(add) dst[x + y*dst_stride] += obmc[x + y*obmc_stride] * tmp[x + y*src_stride] * (256/OBMC_MAX);
2190 else dst[x + y*dst_stride] -= obmc[x + y*obmc_stride] * tmp[x + y*src_stride] * (256/OBMC_MAX);
2191 }
2192 }
2193 }
2194 }
2195
2196 static void predict_plane(SnowContext *s, DWTELEM *buf, int plane_index, int add){
2197 Plane *p= &s->plane[plane_index];
2198 const int mb_w= s->mb_band.width;
2199 const int mb_h= s->mb_band.height;
2200 const int mb_stride= s->mb_band.stride;
2201 int x, y, mb_x, mb_y;
2202 int scale = plane_index ? s->mv_scale : 2*s->mv_scale;
2203 int block_w = plane_index ? 8 : 16;
2204 uint8_t *obmc = plane_index ? obmc16 : obmc32;
2205 int obmc_stride= plane_index ? 16 : 32;
2206 int ref_stride= s->last_picture.linesize[plane_index];
2207 uint8_t *ref = s->last_picture.data[plane_index];
2208 int w= p->width;
2209 int h= p->height;
2210
2211 if(s->avctx->debug&512){
2212 for(y=0; y<h; y++){
2213 for(x=0; x<w; x++){
2214 if(add) buf[x + y*w]+= 128*256;
2215 else buf[x + y*w]-= 128*256;
2216 }
2217 }
2218
2219 return;
2220 }
2221 for(mb_y=-1; mb_y<=mb_h; mb_y++){
2222 for(mb_x=-1; mb_x<=mb_w; mb_x++){
2223 int index= clip(mb_x, 0, mb_w-1) + clip(mb_y, 0, mb_h-1)*mb_stride;
2224
2225 add_xblock(buf, ref, obmc,
2226 block_w*mb_x - block_w/2,
2227 block_w*mb_y - block_w/2,
2228 2*block_w, 2*block_w,
2229 s->mv_band[0].buf[index]*scale, s->mv_band[1].buf[index]*scale,
2230 w, h,
2231 w, ref_stride, obmc_stride,
2232 s->mb_band.buf[index], add);
2233
2234 }
2235 }
2236 }
2237
2238 static void quantize(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int bias){
2239 const int level= b->level;
2240 const int w= b->width;
2241 const int h= b->height;
2242 const int qlog= clip(s->qlog + b->qlog, 0, 128);
2243 const int qmul= qexp[qlog&7]<<(qlog>>3);
2244 int x,y, thres1, thres2;
2245 START_TIMER
2246
2247 assert(QROOT==8);
2248
2249 bias= bias ? 0 : (3*qmul)>>3;
2250 thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
2251 thres2= 2*thres1;
2252
2253 if(!bias){
2254 for(y=0; y<h; y++){
2255 for(x=0; x<w; x++){
2256 int i= src[x + y*stride];
2257
2258 if((unsigned)(i+thres1) > thres2){
2259 if(i>=0){
2260 i<<= QEXPSHIFT;
2261 i/= qmul; //FIXME optimize
2262 src[x + y*stride]= i;
2263 }else{
2264 i= -i;
2265 i<<= QEXPSHIFT;
2266 i/= qmul; //FIXME optimize
2267 src[x + y*stride]= -i;
2268 }
2269 }else
2270 src[x + y*stride]= 0;
2271 }
2272 }
2273 }else{
2274 for(y=0; y<h; y++){
2275 for(x=0; x<w; x++){
2276 int i= src[x + y*stride];
2277
2278 if((unsigned)(i+thres1) > thres2){
2279 if(i>=0){
2280 i<<= QEXPSHIFT;
2281 i= (i + bias) / qmul; //FIXME optimize
2282 src[x + y*stride]= i;
2283 }else{
2284 i= -i;
2285 i<<= QEXPSHIFT;
2286 i= (i + bias) / qmul; //FIXME optimize
2287 src[x + y*stride]= -i;
2288 }
2289 }else
2290 src[x + y*stride]= 0;
2291 }
2292 }
2293 }
2294 if(level+1 == s->spatial_decomposition_count){
2295 // STOP_TIMER("quantize")
2296 }
2297 }
2298
2299 static void dequantize(SnowContext *s, SubBand *b, DWTELEM *src, int stride){
2300 const int level= b->level;
2301 const int w= b->width;
2302 const int h= b->height;
2303 const int qlog= clip(s->qlog + b->qlog, 0, 128);
2304 const int qmul= qexp[qlog&7]<<(qlog>>3);
2305 const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
2306 int x,y;
2307
2308 assert(QROOT==8);
2309
2310 for(y=0; y<h; y++){
2311 for(x=0; x<w; x++){
2312 int i= src[x + y*stride];
2313 if(i<0){
2314 src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
2315 }else if(i>0){
2316 src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
2317 }
2318 }
2319 }
2320 }
2321
2322 static void decorrelate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
2323 const int w= b->width;
2324 const int h= b->height;
2325 int x,y;
2326
2327 for(y=h-1; y>=0; y--){
2328 for(x=w-1; x>=0; x--){
2329 int i= x + y*stride;
2330
2331 if(x){
2332 if(use_median){
2333 if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
2334 else src[i] -= src[i - 1];
2335 }else{
2336 if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
2337 else src[i] -= src[i - 1];
2338 }
2339 }else{
2340 if(y) src[i] -= src[i - stride];
2341 }
2342 }
2343 }
2344 }
2345
2346 static void correlate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
2347 const int w= b->width;
2348 const int h= b->height;
2349 int x,y;
2350
2351 for(y=0; y<h; y++){
2352 for(x=0; x<w; x++){
2353 int i= x + y*stride;
2354
2355 if(x){
2356 if(use_median){
2357 if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
2358 else src[i] += src[i - 1];
2359 }else{