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