Define AVMediaType enum, and use it instead of enum CodecType, which
[libav.git] / libavcodec / snow.c
1 /*
2 * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "libavutil/intmath.h"
22 #include "avcodec.h"
23 #include "dsputil.h"
24 #include "dwt.h"
25 #include "snow.h"
26
27 #include "rangecoder.h"
28 #include "mathops.h"
29
30 #include "mpegvideo.h"
31 #include "h263.h"
32
33 #undef NDEBUG
34 #include <assert.h>
35
36 static const int8_t quant3[256]={
37 0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
45 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-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-3,-3,-2,-2,-1,
179 };
180
181 #if 0 //64*cubic
182 static const uint8_t obmc32[1024]={
183 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,
184 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0,
185 0, 0, 0, 4, 4, 4, 4, 8, 8, 12, 12, 12, 16, 16, 16, 16, 16, 16, 16, 16, 12, 12, 12, 8, 8, 4, 4, 4, 4, 0, 0, 0,
186 0, 0, 4, 4, 8, 8, 12, 16, 16, 20, 24, 24, 28, 28, 32, 32, 32, 32, 28, 28, 24, 24, 20, 16, 16, 12, 8, 8, 4, 4, 0, 0,
187 0, 0, 4, 8, 8, 12, 16, 24, 28, 32, 36, 40, 44, 48, 48, 48, 48, 48, 48, 44, 40, 36, 32, 28, 24, 16, 12, 8, 8, 4, 0, 0,
188 0, 4, 4, 8, 12, 20, 24, 32, 40, 44, 52, 56, 60, 64, 68, 72, 72, 68, 64, 60, 56, 52, 44, 40, 32, 24, 20, 12, 8, 4, 4, 0,
189 0, 4, 4, 12, 16, 24, 32, 40, 52, 60, 68, 76, 80, 88, 88, 92, 92, 88, 88, 80, 76, 68, 60, 52, 40, 32, 24, 16, 12, 4, 4, 0,
190 0, 4, 8, 16, 24, 32, 40, 52, 64, 76, 84, 92,100,108,112,116,116,112,108,100, 92, 84, 76, 64, 52, 40, 32, 24, 16, 8, 4, 0,
191 0, 4, 8, 16, 28, 40, 52, 64, 76, 88,100,112,124,132,136,140,140,136,132,124,112,100, 88, 76, 64, 52, 40, 28, 16, 8, 4, 0,
192 0, 4, 12, 20, 32, 44, 60, 76, 88,104,120,132,144,152,160,164,164,160,152,144,132,120,104, 88, 76, 60, 44, 32, 20, 12, 4, 0,
193 0, 4, 12, 24, 36, 48, 68, 84,100,120,136,152,164,176,180,184,184,180,176,164,152,136,120,100, 84, 68, 48, 36, 24, 12, 4, 0,
194 0, 4, 12, 24, 40, 56, 76, 92,112,132,152,168,180,192,204,208,208,204,192,180,168,152,132,112, 92, 76, 56, 40, 24, 12, 4, 0,
195 0, 4, 16, 28, 44, 60, 80,100,124,144,164,180,196,208,220,224,224,220,208,196,180,164,144,124,100, 80, 60, 44, 28, 16, 4, 0,
196 0, 8, 16, 28, 48, 64, 88,108,132,152,176,192,208,224,232,240,240,232,224,208,192,176,152,132,108, 88, 64, 48, 28, 16, 8, 0,
197 0, 4, 16, 32, 48, 68, 88,112,136,160,180,204,220,232,244,248,248,244,232,220,204,180,160,136,112, 88, 68, 48, 32, 16, 4, 0,
198 1, 8, 16, 32, 48, 72, 92,116,140,164,184,208,224,240,248,255,255,248,240,224,208,184,164,140,116, 92, 72, 48, 32, 16, 8, 1,
199 1, 8, 16, 32, 48, 72, 92,116,140,164,184,208,224,240,248,255,255,248,240,224,208,184,164,140,116, 92, 72, 48, 32, 16, 8, 1,
200 0, 4, 16, 32, 48, 68, 88,112,136,160,180,204,220,232,244,248,248,244,232,220,204,180,160,136,112, 88, 68, 48, 32, 16, 4, 0,
201 0, 8, 16, 28, 48, 64, 88,108,132,152,176,192,208,224,232,240,240,232,224,208,192,176,152,132,108, 88, 64, 48, 28, 16, 8, 0,
202 0, 4, 16, 28, 44, 60, 80,100,124,144,164,180,196,208,220,224,224,220,208,196,180,164,144,124,100, 80, 60, 44, 28, 16, 4, 0,
203 0, 4, 12, 24, 40, 56, 76, 92,112,132,152,168,180,192,204,208,208,204,192,180,168,152,132,112, 92, 76, 56, 40, 24, 12, 4, 0,
204 0, 4, 12, 24, 36, 48, 68, 84,100,120,136,152,164,176,180,184,184,180,176,164,152,136,120,100, 84, 68, 48, 36, 24, 12, 4, 0,
205 0, 4, 12, 20, 32, 44, 60, 76, 88,104,120,132,144,152,160,164,164,160,152,144,132,120,104, 88, 76, 60, 44, 32, 20, 12, 4, 0,
206 0, 4, 8, 16, 28, 40, 52, 64, 76, 88,100,112,124,132,136,140,140,136,132,124,112,100, 88, 76, 64, 52, 40, 28, 16, 8, 4, 0,
207 0, 4, 8, 16, 24, 32, 40, 52, 64, 76, 84, 92,100,108,112,116,116,112,108,100, 92, 84, 76, 64, 52, 40, 32, 24, 16, 8, 4, 0,
208 0, 4, 4, 12, 16, 24, 32, 40, 52, 60, 68, 76, 80, 88, 88, 92, 92, 88, 88, 80, 76, 68, 60, 52, 40, 32, 24, 16, 12, 4, 4, 0,
209 0, 4, 4, 8, 12, 20, 24, 32, 40, 44, 52, 56, 60, 64, 68, 72, 72, 68, 64, 60, 56, 52, 44, 40, 32, 24, 20, 12, 8, 4, 4, 0,
210 0, 0, 4, 8, 8, 12, 16, 24, 28, 32, 36, 40, 44, 48, 48, 48, 48, 48, 48, 44, 40, 36, 32, 28, 24, 16, 12, 8, 8, 4, 0, 0,
211 0, 0, 4, 4, 8, 8, 12, 16, 16, 20, 24, 24, 28, 28, 32, 32, 32, 32, 28, 28, 24, 24, 20, 16, 16, 12, 8, 8, 4, 4, 0, 0,
212 0, 0, 0, 4, 4, 4, 4, 8, 8, 12, 12, 12, 16, 16, 16, 16, 16, 16, 16, 16, 12, 12, 12, 8, 8, 4, 4, 4, 4, 0, 0, 0,
213 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0,
214 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,
215 //error:0.000022
216 };
217 static const uint8_t obmc16[256]={
218 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0,
219 0, 4, 4, 8, 16, 20, 20, 24, 24, 20, 20, 16, 8, 4, 4, 0,
220 0, 4, 16, 24, 36, 44, 52, 60, 60, 52, 44, 36, 24, 16, 4, 0,
221 0, 8, 24, 44, 60, 80, 96,104,104, 96, 80, 60, 44, 24, 8, 0,
222 0, 16, 36, 60, 92,116,136,152,152,136,116, 92, 60, 36, 16, 0,
223 0, 20, 44, 80,116,152,180,196,196,180,152,116, 80, 44, 20, 0,
224 4, 20, 52, 96,136,180,212,228,228,212,180,136, 96, 52, 20, 4,
225 4, 24, 60,104,152,196,228,248,248,228,196,152,104, 60, 24, 4,
226 4, 24, 60,104,152,196,228,248,248,228,196,152,104, 60, 24, 4,
227 4, 20, 52, 96,136,180,212,228,228,212,180,136, 96, 52, 20, 4,
228 0, 20, 44, 80,116,152,180,196,196,180,152,116, 80, 44, 20, 0,
229 0, 16, 36, 60, 92,116,136,152,152,136,116, 92, 60, 36, 16, 0,
230 0, 8, 24, 44, 60, 80, 96,104,104, 96, 80, 60, 44, 24, 8, 0,
231 0, 4, 16, 24, 36, 44, 52, 60, 60, 52, 44, 36, 24, 16, 4, 0,
232 0, 4, 4, 8, 16, 20, 20, 24, 24, 20, 20, 16, 8, 4, 4, 0,
233 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0,
234 //error:0.000033
235 };
236 #elif 1 // 64*linear
237 static const uint8_t obmc32[1024]={
238 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0,
239 0, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12, 8, 8, 8, 4, 4, 4, 0,
240 0, 4, 8, 8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12, 8, 8, 4, 0,
241 0, 4, 8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12, 8, 4, 0,
242 4, 8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12, 8, 4,
243 4, 8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12, 8, 4,
244 4, 8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16, 8, 4,
245 4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12, 4,
246 4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12, 4,
247 4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16, 4,
248 4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16, 4,
249 4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16, 4,
250 8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20, 8,
251 8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20, 8,
252 8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20, 8,
253 8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24, 8,
254 8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24, 8,
255 8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20, 8,
256 8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20, 8,
257 8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20, 8,
258 4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16, 4,
259 4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16, 4,
260 4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16, 4,
261 4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12, 4,
262 4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12, 4,
263 4, 8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16, 8, 4,
264 4, 8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12, 8, 4,
265 4, 8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12, 8, 4,
266 0, 4, 8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12, 8, 4, 0,
267 0, 4, 8, 8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12, 8, 8, 4, 0,
268 0, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12, 8, 8, 8, 4, 4, 4, 0,
269 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0,
270 //error:0.000020
271 };
272 static const uint8_t obmc16[256]={
273 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0,
274 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4,
275 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4,
276 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8,
277 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8,
278 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
279 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
280 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
281 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
282 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
283 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
284 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8,
285 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8,
286 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4,
287 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4,
288 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0,
289 //error:0.000015
290 };
291 #else //64*cos
292 static const uint8_t obmc32[1024]={
293 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,
294 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 4, 4, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0,
295 0, 0, 0, 4, 4, 4, 4, 8, 8, 12, 12, 12, 12, 16, 16, 16, 16, 16, 16, 12, 12, 12, 12, 8, 8, 4, 4, 4, 4, 0, 0, 0,
296 0, 0, 4, 4, 4, 8, 8, 12, 16, 20, 20, 24, 28, 28, 28, 28, 28, 28, 28, 28, 24, 20, 20, 16, 12, 8, 8, 4, 4, 4, 0, 0,
297 0, 0, 4, 4, 8, 12, 16, 20, 24, 28, 36, 40, 44, 44, 48, 48, 48, 48, 44, 44, 40, 36, 28, 24, 20, 16, 12, 8, 4, 4, 0, 0,
298 0, 0, 4, 8, 12, 20, 24, 32, 36, 44, 48, 56, 60, 64, 68, 68, 68, 68, 64, 60, 56, 48, 44, 36, 32, 24, 20, 12, 8, 4, 0, 0,
299 0, 4, 4, 8, 16, 24, 32, 40, 48, 60, 68, 76, 80, 84, 88, 92, 92, 88, 84, 80, 76, 68, 60, 48, 40, 32, 24, 16, 8, 4, 4, 0,
300 0, 4, 8, 12, 20, 32, 40, 52, 64, 76, 84, 96,104,108,112,116,116,112,108,104, 96, 84, 76, 64, 52, 40, 32, 20, 12, 8, 4, 0,
301 0, 4, 8, 16, 24, 36, 48, 64, 76, 92,104,116,124,132,136,140,140,136,132,124,116,104, 92, 76, 64, 48, 36, 24, 16, 8, 4, 0,
302 0, 4, 12, 20, 28, 44, 60, 76, 92,104,120,136,148,156,160,164,164,160,156,148,136,120,104, 92, 76, 60, 44, 28, 20, 12, 4, 0,
303 0, 4, 12, 20, 36, 48, 68, 84,104,120,140,152,168,176,184,188,188,184,176,168,152,140,120,104, 84, 68, 48, 36, 20, 12, 4, 0,
304 0, 4, 12, 24, 36, 56, 76, 96,116,136,152,172,184,196,204,208,208,204,196,184,172,152,136,116, 96, 76, 56, 36, 24, 12, 4, 0,
305 0, 4, 12, 24, 44, 60, 80,104,124,148,168,184,200,212,224,228,228,224,212,200,184,168,148,124,104, 80, 60, 44, 24, 12, 4, 0,
306 0, 4, 12, 28, 44, 64, 84,108,132,156,176,196,212,228,236,240,240,236,228,212,196,176,156,132,108, 84, 64, 44, 28, 12, 4, 0,
307 0, 4, 16, 28, 48, 68, 88,112,136,160,184,204,224,236,244,252,252,244,236,224,204,184,160,136,112, 88, 68, 48, 28, 16, 4, 0,
308 1, 4, 16, 28, 48, 68, 92,116,140,164,188,208,228,240,252,255,255,252,240,228,208,188,164,140,116, 92, 68, 48, 28, 16, 4, 1,
309 1, 4, 16, 28, 48, 68, 92,116,140,164,188,208,228,240,252,255,255,252,240,228,208,188,164,140,116, 92, 68, 48, 28, 16, 4, 1,
310 0, 4, 16, 28, 48, 68, 88,112,136,160,184,204,224,236,244,252,252,244,236,224,204,184,160,136,112, 88, 68, 48, 28, 16, 4, 0,
311 0, 4, 12, 28, 44, 64, 84,108,132,156,176,196,212,228,236,240,240,236,228,212,196,176,156,132,108, 84, 64, 44, 28, 12, 4, 0,
312 0, 4, 12, 24, 44, 60, 80,104,124,148,168,184,200,212,224,228,228,224,212,200,184,168,148,124,104, 80, 60, 44, 24, 12, 4, 0,
313 0, 4, 12, 24, 36, 56, 76, 96,116,136,152,172,184,196,204,208,208,204,196,184,172,152,136,116, 96, 76, 56, 36, 24, 12, 4, 0,
314 0, 4, 12, 20, 36, 48, 68, 84,104,120,140,152,168,176,184,188,188,184,176,168,152,140,120,104, 84, 68, 48, 36, 20, 12, 4, 0,
315 0, 4, 12, 20, 28, 44, 60, 76, 92,104,120,136,148,156,160,164,164,160,156,148,136,120,104, 92, 76, 60, 44, 28, 20, 12, 4, 0,
316 0, 4, 8, 16, 24, 36, 48, 64, 76, 92,104,116,124,132,136,140,140,136,132,124,116,104, 92, 76, 64, 48, 36, 24, 16, 8, 4, 0,
317 0, 4, 8, 12, 20, 32, 40, 52, 64, 76, 84, 96,104,108,112,116,116,112,108,104, 96, 84, 76, 64, 52, 40, 32, 20, 12, 8, 4, 0,
318 0, 4, 4, 8, 16, 24, 32, 40, 48, 60, 68, 76, 80, 84, 88, 92, 92, 88, 84, 80, 76, 68, 60, 48, 40, 32, 24, 16, 8, 4, 4, 0,
319 0, 0, 4, 8, 12, 20, 24, 32, 36, 44, 48, 56, 60, 64, 68, 68, 68, 68, 64, 60, 56, 48, 44, 36, 32, 24, 20, 12, 8, 4, 0, 0,
320 0, 0, 4, 4, 8, 12, 16, 20, 24, 28, 36, 40, 44, 44, 48, 48, 48, 48, 44, 44, 40, 36, 28, 24, 20, 16, 12, 8, 4, 4, 0, 0,
321 0, 0, 4, 4, 4, 8, 8, 12, 16, 20, 20, 24, 28, 28, 28, 28, 28, 28, 28, 28, 24, 20, 20, 16, 12, 8, 8, 4, 4, 4, 0, 0,
322 0, 0, 0, 4, 4, 4, 4, 8, 8, 12, 12, 12, 12, 16, 16, 16, 16, 16, 16, 12, 12, 12, 12, 8, 8, 4, 4, 4, 4, 0, 0, 0,
323 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 8, 4, 4, 8, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0,
324 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,
325 //error:0.000022
326 };
327 static const uint8_t obmc16[256]={
328 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0,
329 0, 0, 4, 8, 12, 16, 20, 20, 20, 20, 16, 12, 8, 4, 0, 0,
330 0, 4, 12, 24, 32, 44, 52, 56, 56, 52, 44, 32, 24, 12, 4, 0,
331 0, 8, 24, 40, 60, 80, 96,104,104, 96, 80, 60, 40, 24, 8, 0,
332 0, 12, 32, 64, 92,120,140,152,152,140,120, 92, 64, 32, 12, 0,
333 4, 16, 44, 80,120,156,184,196,196,184,156,120, 80, 44, 16, 4,
334 4, 20, 52, 96,140,184,216,232,232,216,184,140, 96, 52, 20, 4,
335 0, 20, 56,104,152,196,232,252,252,232,196,152,104, 56, 20, 0,
336 0, 20, 56,104,152,196,232,252,252,232,196,152,104, 56, 20, 0,
337 4, 20, 52, 96,140,184,216,232,232,216,184,140, 96, 52, 20, 4,
338 4, 16, 44, 80,120,156,184,196,196,184,156,120, 80, 44, 16, 4,
339 0, 12, 32, 64, 92,120,140,152,152,140,120, 92, 64, 32, 12, 0,
340 0, 8, 24, 40, 60, 80, 96,104,104, 96, 80, 60, 40, 24, 8, 0,
341 0, 4, 12, 24, 32, 44, 52, 56, 56, 52, 44, 32, 24, 12, 4, 0,
342 0, 0, 4, 8, 12, 16, 20, 20, 20, 20, 16, 12, 8, 4, 0, 0,
343 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0,
344 //error:0.000022
345 };
346 #endif /* 0 */
347
348 //linear *64
349 static const uint8_t obmc8[64]={
350 4, 12, 20, 28, 28, 20, 12, 4,
351 12, 36, 60, 84, 84, 60, 36, 12,
352 20, 60,100,140,140,100, 60, 20,
353 28, 84,140,196,196,140, 84, 28,
354 28, 84,140,196,196,140, 84, 28,
355 20, 60,100,140,140,100, 60, 20,
356 12, 36, 60, 84, 84, 60, 36, 12,
357 4, 12, 20, 28, 28, 20, 12, 4,
358 //error:0.000000
359 };
360
361 //linear *64
362 static const uint8_t obmc4[16]={
363 16, 48, 48, 16,
364 48,144,144, 48,
365 48,144,144, 48,
366 16, 48, 48, 16,
367 //error:0.000000
368 };
369
370 static const uint8_t * const obmc_tab[4]={
371 obmc32, obmc16, obmc8, obmc4
372 };
373
374 static int scale_mv_ref[MAX_REF_FRAMES][MAX_REF_FRAMES];
375
376 typedef struct BlockNode{
377 int16_t mx;
378 int16_t my;
379 uint8_t ref;
380 uint8_t color[3];
381 uint8_t type;
382 //#define TYPE_SPLIT 1
383 #define BLOCK_INTRA 1
384 #define BLOCK_OPT 2
385 //#define TYPE_NOCOLOR 4
386 uint8_t level; //FIXME merge into type?
387 }BlockNode;
388
389 static const BlockNode null_block= { //FIXME add border maybe
390 .color= {128,128,128},
391 .mx= 0,
392 .my= 0,
393 .ref= 0,
394 .type= 0,
395 .level= 0,
396 };
397
398 #define LOG2_MB_SIZE 4
399 #define MB_SIZE (1<<LOG2_MB_SIZE)
400 #define ENCODER_EXTRA_BITS 4
401 #define HTAPS_MAX 8
402
403 typedef struct x_and_coeff{
404 int16_t x;
405 uint16_t coeff;
406 } x_and_coeff;
407
408 typedef struct SubBand{
409 int level;
410 int stride;
411 int width;
412 int height;
413 int qlog; ///< log(qscale)/log[2^(1/6)]
414 DWTELEM *buf;
415 IDWTELEM *ibuf;
416 int buf_x_offset;
417 int buf_y_offset;
418 int stride_line; ///< Stride measured in lines, not pixels.
419 x_and_coeff * x_coeff;
420 struct SubBand *parent;
421 uint8_t state[/*7*2*/ 7 + 512][32];
422 }SubBand;
423
424 typedef struct Plane{
425 int width;
426 int height;
427 SubBand band[MAX_DECOMPOSITIONS][4];
428
429 int htaps;
430 int8_t hcoeff[HTAPS_MAX/2];
431 int diag_mc;
432 int fast_mc;
433
434 int last_htaps;
435 int8_t last_hcoeff[HTAPS_MAX/2];
436 int last_diag_mc;
437 }Plane;
438
439 typedef struct SnowContext{
440
441 AVCodecContext *avctx;
442 RangeCoder c;
443 DSPContext dsp;
444 DWTContext dwt;
445 AVFrame new_picture;
446 AVFrame input_picture; ///< new_picture with the internal linesizes
447 AVFrame current_picture;
448 AVFrame last_picture[MAX_REF_FRAMES];
449 uint8_t *halfpel_plane[MAX_REF_FRAMES][4][4];
450 AVFrame mconly_picture;
451 // uint8_t q_context[16];
452 uint8_t header_state[32];
453 uint8_t block_state[128 + 32*128];
454 int keyframe;
455 int always_reset;
456 int version;
457 int spatial_decomposition_type;
458 int last_spatial_decomposition_type;
459 int temporal_decomposition_type;
460 int spatial_decomposition_count;
461 int last_spatial_decomposition_count;
462 int temporal_decomposition_count;
463 int max_ref_frames;
464 int ref_frames;
465 int16_t (*ref_mvs[MAX_REF_FRAMES])[2];
466 uint32_t *ref_scores[MAX_REF_FRAMES];
467 DWTELEM *spatial_dwt_buffer;
468 IDWTELEM *spatial_idwt_buffer;
469 int colorspace_type;
470 int chroma_h_shift;
471 int chroma_v_shift;
472 int spatial_scalability;
473 int qlog;
474 int last_qlog;
475 int lambda;
476 int lambda2;
477 int pass1_rc;
478 int mv_scale;
479 int last_mv_scale;
480 int qbias;
481 int last_qbias;
482 #define QBIAS_SHIFT 3
483 int b_width;
484 int b_height;
485 int block_max_depth;
486 int last_block_max_depth;
487 Plane plane[MAX_PLANES];
488 BlockNode *block;
489 #define ME_CACHE_SIZE 1024
490 int me_cache[ME_CACHE_SIZE];
491 int me_cache_generation;
492 slice_buffer sb;
493
494 MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to eventually make the motion estimation independent of MpegEncContext, so this will be removed then (FIXME/XXX)
495
496 uint8_t *scratchbuf;
497 }SnowContext;
498
499 #ifdef __sgi
500 // Avoid a name clash on SGI IRIX
501 #undef qexp
502 #endif
503 #define QEXPSHIFT (7-FRAC_BITS+8) //FIXME try to change this to 0
504 static uint8_t qexp[QROOT];
505
506 static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
507 int i;
508
509 if(v){
510 const int a= FFABS(v);
511 const int e= av_log2(a);
512 #if 1
513 const int el= FFMIN(e, 10);
514 put_rac(c, state+0, 0);
515
516 for(i=0; i<el; i++){
517 put_rac(c, state+1+i, 1); //1..10
518 }
519 for(; i<e; i++){
520 put_rac(c, state+1+9, 1); //1..10
521 }
522 put_rac(c, state+1+FFMIN(i,9), 0);
523
524 for(i=e-1; i>=el; i--){
525 put_rac(c, state+22+9, (a>>i)&1); //22..31
526 }
527 for(; i>=0; i--){
528 put_rac(c, state+22+i, (a>>i)&1); //22..31
529 }
530
531 if(is_signed)
532 put_rac(c, state+11 + el, v < 0); //11..21
533 #else
534
535 put_rac(c, state+0, 0);
536 if(e<=9){
537 for(i=0; i<e; i++){
538 put_rac(c, state+1+i, 1); //1..10
539 }
540 put_rac(c, state+1+i, 0);
541
542 for(i=e-1; i>=0; i--){
543 put_rac(c, state+22+i, (a>>i)&1); //22..31
544 }
545
546 if(is_signed)
547 put_rac(c, state+11 + e, v < 0); //11..21
548 }else{
549 for(i=0; i<e; i++){
550 put_rac(c, state+1+FFMIN(i,9), 1); //1..10
551 }
552 put_rac(c, state+1+9, 0);
553
554 for(i=e-1; i>=0; i--){
555 put_rac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31
556 }
557
558 if(is_signed)
559 put_rac(c, state+11 + 10, v < 0); //11..21
560 }
561 #endif /* 1 */
562 }else{
563 put_rac(c, state+0, 1);
564 }
565 }
566
567 static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
568 if(get_rac(c, state+0))
569 return 0;
570 else{
571 int i, e, a;
572 e= 0;
573 while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
574 e++;
575 }
576
577 a= 1;
578 for(i=e-1; i>=0; i--){
579 a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
580 }
581
582 e= -(is_signed && get_rac(c, state+11 + FFMIN(e,10))); //11..21
583 return (a^e)-e;
584 }
585 }
586
587 static inline void put_symbol2(RangeCoder *c, uint8_t *state, int v, int log2){
588 int i;
589 int r= log2>=0 ? 1<<log2 : 1;
590
591 assert(v>=0);
592 assert(log2>=-4);
593
594 while(v >= r){
595 put_rac(c, state+4+log2, 1);
596 v -= r;
597 log2++;
598 if(log2>0) r+=r;
599 }
600 put_rac(c, state+4+log2, 0);
601
602 for(i=log2-1; i>=0; i--){
603 put_rac(c, state+31-i, (v>>i)&1);
604 }
605 }
606
607 static inline int get_symbol2(RangeCoder *c, uint8_t *state, int log2){
608 int i;
609 int r= log2>=0 ? 1<<log2 : 1;
610 int v=0;
611
612 assert(log2>=-4);
613
614 while(get_rac(c, state+4+log2)){
615 v+= r;
616 log2++;
617 if(log2>0) r+=r;
618 }
619
620 for(i=log2-1; i>=0; i--){
621 v+= get_rac(c, state+31-i)<<i;
622 }
623
624 return v;
625 }
626
627 static inline void unpack_coeffs(SnowContext *s, SubBand *b, SubBand * parent, int orientation){
628 const int w= b->width;
629 const int h= b->height;
630 int x,y;
631
632 int run, runs;
633 x_and_coeff *xc= b->x_coeff;
634 x_and_coeff *prev_xc= NULL;
635 x_and_coeff *prev2_xc= xc;
636 x_and_coeff *parent_xc= parent ? parent->x_coeff : NULL;
637 x_and_coeff *prev_parent_xc= parent_xc;
638
639 runs= get_symbol2(&s->c, b->state[30], 0);
640 if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
641 else run= INT_MAX;
642
643 for(y=0; y<h; y++){
644 int v=0;
645 int lt=0, t=0, rt=0;
646
647 if(y && prev_xc->x == 0){
648 rt= prev_xc->coeff;
649 }
650 for(x=0; x<w; x++){
651 int p=0;
652 const int l= v;
653
654 lt= t; t= rt;
655
656 if(y){
657 if(prev_xc->x <= x)
658 prev_xc++;
659 if(prev_xc->x == x + 1)
660 rt= prev_xc->coeff;
661 else
662 rt=0;
663 }
664 if(parent_xc){
665 if(x>>1 > parent_xc->x){
666 parent_xc++;
667 }
668 if(x>>1 == parent_xc->x){
669 p= parent_xc->coeff;
670 }
671 }
672 if(/*ll|*/l|lt|t|rt|p){
673 int context= av_log2(/*FFABS(ll) + */3*(l>>1) + (lt>>1) + (t&~1) + (rt>>1) + (p>>1));
674
675 v=get_rac(&s->c, &b->state[0][context]);
676 if(v){
677 v= 2*(get_symbol2(&s->c, b->state[context + 2], context-4) + 1);
678 v+=get_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3bA[l&0xFF] + 3*quant3bA[t&0xFF]]);
679
680 xc->x=x;
681 (xc++)->coeff= v;
682 }
683 }else{
684 if(!run){
685 if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
686 else run= INT_MAX;
687 v= 2*(get_symbol2(&s->c, b->state[0 + 2], 0-4) + 1);
688 v+=get_rac(&s->c, &b->state[0][16 + 1 + 3]);
689
690 xc->x=x;
691 (xc++)->coeff= v;
692 }else{
693 int max_run;
694 run--;
695 v=0;
696
697 if(y) max_run= FFMIN(run, prev_xc->x - x - 2);
698 else max_run= FFMIN(run, w-x-1);
699 if(parent_xc)
700 max_run= FFMIN(max_run, 2*parent_xc->x - x - 1);
701 x+= max_run;
702 run-= max_run;
703 }
704 }
705 }
706 (xc++)->x= w+1; //end marker
707 prev_xc= prev2_xc;
708 prev2_xc= xc;
709
710 if(parent_xc){
711 if(y&1){
712 while(parent_xc->x != parent->width+1)
713 parent_xc++;
714 parent_xc++;
715 prev_parent_xc= parent_xc;
716 }else{
717 parent_xc= prev_parent_xc;
718 }
719 }
720 }
721
722 (xc++)->x= w+1; //end marker
723 }
724
725 static inline void decode_subband_slice_buffered(SnowContext *s, SubBand *b, slice_buffer * sb, int start_y, int h, int save_state[1]){
726 const int w= b->width;
727 int y;
728 const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
729 int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
730 int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
731 int new_index = 0;
732
733 if(b->ibuf == s->spatial_idwt_buffer || s->qlog == LOSSLESS_QLOG){
734 qadd= 0;
735 qmul= 1<<QEXPSHIFT;
736 }
737
738 /* If we are on the second or later slice, restore our index. */
739 if (start_y != 0)
740 new_index = save_state[0];
741
742
743 for(y=start_y; y<h; y++){
744 int x = 0;
745 int v;
746 IDWTELEM * line = slice_buffer_get_line(sb, y * b->stride_line + b->buf_y_offset) + b->buf_x_offset;
747 memset(line, 0, b->width*sizeof(IDWTELEM));
748 v = b->x_coeff[new_index].coeff;
749 x = b->x_coeff[new_index++].x;
750 while(x < w){
751 register int t= ( (v>>1)*qmul + qadd)>>QEXPSHIFT;
752 register int u= -(v&1);
753 line[x] = (t^u) - u;
754
755 v = b->x_coeff[new_index].coeff;
756 x = b->x_coeff[new_index++].x;
757 }
758 }
759
760 /* Save our variables for the next slice. */
761 save_state[0] = new_index;
762
763 return;
764 }
765
766 static void reset_contexts(SnowContext *s){ //FIXME better initial contexts
767 int plane_index, level, orientation;
768
769 for(plane_index=0; plane_index<3; plane_index++){
770 for(level=0; level<MAX_DECOMPOSITIONS; level++){
771 for(orientation=level ? 1:0; orientation<4; orientation++){
772 memset(s->plane[plane_index].band[level][orientation].state, MID_STATE, sizeof(s->plane[plane_index].band[level][orientation].state));
773 }
774 }
775 }
776 memset(s->header_state, MID_STATE, sizeof(s->header_state));
777 memset(s->block_state, MID_STATE, sizeof(s->block_state));
778 }
779
780 static int alloc_blocks(SnowContext *s){
781 int w= -((-s->avctx->width )>>LOG2_MB_SIZE);
782 int h= -((-s->avctx->height)>>LOG2_MB_SIZE);
783
784 s->b_width = w;
785 s->b_height= h;
786
787 av_free(s->block);
788 s->block= av_mallocz(w * h * sizeof(BlockNode) << (s->block_max_depth*2));
789 return 0;
790 }
791
792 static inline void copy_rac_state(RangeCoder *d, RangeCoder *s){
793 uint8_t *bytestream= d->bytestream;
794 uint8_t *bytestream_start= d->bytestream_start;
795 *d= *s;
796 d->bytestream= bytestream;
797 d->bytestream_start= bytestream_start;
798 }
799
800 static inline void set_blocks(SnowContext *s, int level, int x, int y, int l, int cb, int cr, int mx, int my, int ref, int type){
801 const int w= s->b_width << s->block_max_depth;
802 const int rem_depth= s->block_max_depth - level;
803 const int index= (x + y*w) << rem_depth;
804 const int block_w= 1<<rem_depth;
805 BlockNode block;
806 int i,j;
807
808 block.color[0]= l;
809 block.color[1]= cb;
810 block.color[2]= cr;
811 block.mx= mx;
812 block.my= my;
813 block.ref= ref;
814 block.type= type;
815 block.level= level;
816
817 for(j=0; j<block_w; j++){
818 for(i=0; i<block_w; i++){
819 s->block[index + i + j*w]= block;
820 }
821 }
822 }
823
824 static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){
825 const int offset[3]= {
826 y*c-> stride + x,
827 ((y*c->uvstride + x)>>1),
828 ((y*c->uvstride + x)>>1),
829 };
830 int i;
831 for(i=0; i<3; i++){
832 c->src[0][i]= src [i];
833 c->ref[0][i]= ref [i] + offset[i];
834 }
835 assert(!ref_index);
836 }
837
838 static inline void pred_mv(SnowContext *s, int *mx, int *my, int ref,
839 const BlockNode *left, const BlockNode *top, const BlockNode *tr){
840 if(s->ref_frames == 1){
841 *mx = mid_pred(left->mx, top->mx, tr->mx);
842 *my = mid_pred(left->my, top->my, tr->my);
843 }else{
844 const int *scale = scale_mv_ref[ref];
845 *mx = mid_pred((left->mx * scale[left->ref] + 128) >>8,
846 (top ->mx * scale[top ->ref] + 128) >>8,
847 (tr ->mx * scale[tr ->ref] + 128) >>8);
848 *my = mid_pred((left->my * scale[left->ref] + 128) >>8,
849 (top ->my * scale[top ->ref] + 128) >>8,
850 (tr ->my * scale[tr ->ref] + 128) >>8);
851 }
852 }
853
854 static av_always_inline int same_block(BlockNode *a, BlockNode *b){
855 if((a->type&BLOCK_INTRA) && (b->type&BLOCK_INTRA)){
856 return !((a->color[0] - b->color[0]) | (a->color[1] - b->color[1]) | (a->color[2] - b->color[2]));
857 }else{
858 return !((a->mx - b->mx) | (a->my - b->my) | (a->ref - b->ref) | ((a->type ^ b->type)&BLOCK_INTRA));
859 }
860 }
861
862 static void decode_q_branch(SnowContext *s, int level, int x, int y){
863 const int w= s->b_width << s->block_max_depth;
864 const int rem_depth= s->block_max_depth - level;
865 const int index= (x + y*w) << rem_depth;
866 int trx= (x+1)<<rem_depth;
867 const BlockNode *left = x ? &s->block[index-1] : &null_block;
868 const BlockNode *top = y ? &s->block[index-w] : &null_block;
869 const BlockNode *tl = y && x ? &s->block[index-w-1] : left;
870 const BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
871 int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
872
873 if(s->keyframe){
874 set_blocks(s, level, x, y, null_block.color[0], null_block.color[1], null_block.color[2], null_block.mx, null_block.my, null_block.ref, BLOCK_INTRA);
875 return;
876 }
877
878 if(level==s->block_max_depth || get_rac(&s->c, &s->block_state[4 + s_context])){
879 int type, mx, my;
880 int l = left->color[0];
881 int cb= left->color[1];
882 int cr= left->color[2];
883 int ref = 0;
884 int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
885 int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 0*av_log2(2*FFABS(tr->mx - top->mx));
886 int my_context= av_log2(2*FFABS(left->my - top->my)) + 0*av_log2(2*FFABS(tr->my - top->my));
887
888 type= get_rac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0;
889
890 if(type){
891 pred_mv(s, &mx, &my, 0, left, top, tr);
892 l += get_symbol(&s->c, &s->block_state[32], 1);
893 cb+= get_symbol(&s->c, &s->block_state[64], 1);
894 cr+= get_symbol(&s->c, &s->block_state[96], 1);
895 }else{
896 if(s->ref_frames > 1)
897 ref= get_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], 0);
898 pred_mv(s, &mx, &my, ref, left, top, tr);
899 mx+= get_symbol(&s->c, &s->block_state[128 + 32*(mx_context + 16*!!ref)], 1);
900 my+= get_symbol(&s->c, &s->block_state[128 + 32*(my_context + 16*!!ref)], 1);
901 }
902 set_blocks(s, level, x, y, l, cb, cr, mx, my, ref, type);
903 }else{
904 decode_q_branch(s, level+1, 2*x+0, 2*y+0);
905 decode_q_branch(s, level+1, 2*x+1, 2*y+0);
906 decode_q_branch(s, level+1, 2*x+0, 2*y+1);
907 decode_q_branch(s, level+1, 2*x+1, 2*y+1);
908 }
909 }
910
911 static void decode_blocks(SnowContext *s){
912 int x, y;
913 int w= s->b_width;
914 int h= s->b_height;
915
916 for(y=0; y<h; y++){
917 for(x=0; x<w; x++){
918 decode_q_branch(s, 0, x, y);
919 }
920 }
921 }
922
923 static void mc_block(Plane *p, uint8_t *dst, const uint8_t *src, uint8_t *tmp, int stride, int b_w, int b_h, int dx, int dy){
924 static const uint8_t weight[64]={
925 8,7,6,5,4,3,2,1,
926 7,7,0,0,0,0,0,1,
927 6,0,6,0,0,0,2,0,
928 5,0,0,5,0,3,0,0,
929 4,0,0,0,4,0,0,0,
930 3,0,0,5,0,3,0,0,
931 2,0,6,0,0,0,2,0,
932 1,7,0,0,0,0,0,1,
933 };
934
935 static const uint8_t brane[256]={
936 0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x11,0x12,0x12,0x12,0x12,0x12,0x12,0x12,
937 0x04,0x05,0xcc,0xcc,0xcc,0xcc,0xcc,0x41,0x15,0x16,0xcc,0xcc,0xcc,0xcc,0xcc,0x52,
938 0x04,0xcc,0x05,0xcc,0xcc,0xcc,0x41,0xcc,0x15,0xcc,0x16,0xcc,0xcc,0xcc,0x52,0xcc,
939 0x04,0xcc,0xcc,0x05,0xcc,0x41,0xcc,0xcc,0x15,0xcc,0xcc,0x16,0xcc,0x52,0xcc,0xcc,
940 0x04,0xcc,0xcc,0xcc,0x41,0xcc,0xcc,0xcc,0x15,0xcc,0xcc,0xcc,0x16,0xcc,0xcc,0xcc,
941 0x04,0xcc,0xcc,0x41,0xcc,0x05,0xcc,0xcc,0x15,0xcc,0xcc,0x52,0xcc,0x16,0xcc,0xcc,
942 0x04,0xcc,0x41,0xcc,0xcc,0xcc,0x05,0xcc,0x15,0xcc,0x52,0xcc,0xcc,0xcc,0x16,0xcc,
943 0x04,0x41,0xcc,0xcc,0xcc,0xcc,0xcc,0x05,0x15,0x52,0xcc,0xcc,0xcc,0xcc,0xcc,0x16,
944 0x44,0x45,0x45,0x45,0x45,0x45,0x45,0x45,0x55,0x56,0x56,0x56,0x56,0x56,0x56,0x56,
945 0x48,0x49,0xcc,0xcc,0xcc,0xcc,0xcc,0x85,0x59,0x5A,0xcc,0xcc,0xcc,0xcc,0xcc,0x96,
946 0x48,0xcc,0x49,0xcc,0xcc,0xcc,0x85,0xcc,0x59,0xcc,0x5A,0xcc,0xcc,0xcc,0x96,0xcc,
947 0x48,0xcc,0xcc,0x49,0xcc,0x85,0xcc,0xcc,0x59,0xcc,0xcc,0x5A,0xcc,0x96,0xcc,0xcc,
948 0x48,0xcc,0xcc,0xcc,0x49,0xcc,0xcc,0xcc,0x59,0xcc,0xcc,0xcc,0x96,0xcc,0xcc,0xcc,
949 0x48,0xcc,0xcc,0x85,0xcc,0x49,0xcc,0xcc,0x59,0xcc,0xcc,0x96,0xcc,0x5A,0xcc,0xcc,
950 0x48,0xcc,0x85,0xcc,0xcc,0xcc,0x49,0xcc,0x59,0xcc,0x96,0xcc,0xcc,0xcc,0x5A,0xcc,
951 0x48,0x85,0xcc,0xcc,0xcc,0xcc,0xcc,0x49,0x59,0x96,0xcc,0xcc,0xcc,0xcc,0xcc,0x5A,
952 };
953
954 static const uint8_t needs[16]={
955 0,1,0,0,
956 2,4,2,0,
957 0,1,0,0,
958 15
959 };
960
961 int x, y, b, r, l;
962 int16_t tmpIt [64*(32+HTAPS_MAX)];
963 uint8_t tmp2t[3][stride*(32+HTAPS_MAX)];
964 int16_t *tmpI= tmpIt;
965 uint8_t *tmp2= tmp2t[0];
966 const uint8_t *hpel[11];
967 assert(dx<16 && dy<16);
968 r= brane[dx + 16*dy]&15;
969 l= brane[dx + 16*dy]>>4;
970
971 b= needs[l] | needs[r];
972 if(p && !p->diag_mc)
973 b= 15;
974
975 if(b&5){
976 for(y=0; y < b_h+HTAPS_MAX-1; y++){
977 for(x=0; x < b_w; x++){
978 int a_1=src[x + HTAPS_MAX/2-4];
979 int a0= src[x + HTAPS_MAX/2-3];
980 int a1= src[x + HTAPS_MAX/2-2];
981 int a2= src[x + HTAPS_MAX/2-1];
982 int a3= src[x + HTAPS_MAX/2+0];
983 int a4= src[x + HTAPS_MAX/2+1];
984 int a5= src[x + HTAPS_MAX/2+2];
985 int a6= src[x + HTAPS_MAX/2+3];
986 int am=0;
987 if(!p || p->fast_mc){
988 am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
989 tmpI[x]= am;
990 am= (am+16)>>5;
991 }else{
992 am= p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6);
993 tmpI[x]= am;
994 am= (am+32)>>6;
995 }
996
997 if(am&(~255)) am= ~(am>>31);
998 tmp2[x]= am;
999 }
1000 tmpI+= 64;
1001 tmp2+= stride;
1002 src += stride;
1003 }
1004 src -= stride*y;
1005 }
1006 src += HTAPS_MAX/2 - 1;
1007 tmp2= tmp2t[1];
1008
1009 if(b&2){
1010 for(y=0; y < b_h; y++){
1011 for(x=0; x < b_w+1; x++){
1012 int a_1=src[x + (HTAPS_MAX/2-4)*stride];
1013 int a0= src[x + (HTAPS_MAX/2-3)*stride];
1014 int a1= src[x + (HTAPS_MAX/2-2)*stride];
1015 int a2= src[x + (HTAPS_MAX/2-1)*stride];
1016 int a3= src[x + (HTAPS_MAX/2+0)*stride];
1017 int a4= src[x + (HTAPS_MAX/2+1)*stride];
1018 int a5= src[x + (HTAPS_MAX/2+2)*stride];
1019 int a6= src[x + (HTAPS_MAX/2+3)*stride];
1020 int am=0;
1021 if(!p || p->fast_mc)
1022 am= (20*(a2+a3) - 5*(a1+a4) + (a0+a5) + 16)>>5;
1023 else
1024 am= (p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6) + 32)>>6;
1025
1026 if(am&(~255)) am= ~(am>>31);
1027 tmp2[x]= am;
1028 }
1029 src += stride;
1030 tmp2+= stride;
1031 }
1032 src -= stride*y;
1033 }
1034 src += stride*(HTAPS_MAX/2 - 1);
1035 tmp2= tmp2t[2];
1036 tmpI= tmpIt;
1037 if(b&4){
1038 for(y=0; y < b_h; y++){
1039 for(x=0; x < b_w; x++){
1040 int a_1=tmpI[x + (HTAPS_MAX/2-4)*64];
1041 int a0= tmpI[x + (HTAPS_MAX/2-3)*64];
1042 int a1= tmpI[x + (HTAPS_MAX/2-2)*64];
1043 int a2= tmpI[x + (HTAPS_MAX/2-1)*64];
1044 int a3= tmpI[x + (HTAPS_MAX/2+0)*64];
1045 int a4= tmpI[x + (HTAPS_MAX/2+1)*64];
1046 int a5= tmpI[x + (HTAPS_MAX/2+2)*64];
1047 int a6= tmpI[x + (HTAPS_MAX/2+3)*64];
1048 int am=0;
1049 if(!p || p->fast_mc)
1050 am= (20*(a2+a3) - 5*(a1+a4) + (a0+a5) + 512)>>10;
1051 else
1052 am= (p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6) + 2048)>>12;
1053 if(am&(~255)) am= ~(am>>31);
1054 tmp2[x]= am;
1055 }
1056 tmpI+= 64;
1057 tmp2+= stride;
1058 }
1059 }
1060
1061 hpel[ 0]= src;
1062 hpel[ 1]= tmp2t[0] + stride*(HTAPS_MAX/2-1);
1063 hpel[ 2]= src + 1;
1064
1065 hpel[ 4]= tmp2t[1];
1066 hpel[ 5]= tmp2t[2];
1067 hpel[ 6]= tmp2t[1] + 1;
1068
1069 hpel[ 8]= src + stride;
1070 hpel[ 9]= hpel[1] + stride;
1071 hpel[10]= hpel[8] + 1;
1072
1073 if(b==15){
1074 const uint8_t *src1= hpel[dx/8 + dy/8*4 ];
1075 const uint8_t *src2= hpel[dx/8 + dy/8*4+1];
1076 const uint8_t *src3= hpel[dx/8 + dy/8*4+4];
1077 const uint8_t *src4= hpel[dx/8 + dy/8*4+5];
1078 dx&=7;
1079 dy&=7;
1080 for(y=0; y < b_h; y++){
1081 for(x=0; x < b_w; x++){
1082 dst[x]= ((8-dx)*(8-dy)*src1[x] + dx*(8-dy)*src2[x]+
1083 (8-dx)* dy *src3[x] + dx* dy *src4[x]+32)>>6;
1084 }
1085 src1+=stride;
1086 src2+=stride;
1087 src3+=stride;
1088 src4+=stride;
1089 dst +=stride;
1090 }
1091 }else{
1092 const uint8_t *src1= hpel[l];
1093 const uint8_t *src2= hpel[r];
1094 int a= weight[((dx&7) + (8*(dy&7)))];
1095 int b= 8-a;
1096 for(y=0; y < b_h; y++){
1097 for(x=0; x < b_w; x++){
1098 dst[x]= (a*src1[x] + b*src2[x] + 4)>>3;
1099 }
1100 src1+=stride;
1101 src2+=stride;
1102 dst +=stride;
1103 }
1104 }
1105 }
1106
1107 #define mca(dx,dy,b_w)\
1108 static void mc_block_hpel ## dx ## dy ## b_w(uint8_t *dst, const uint8_t *src, int stride, int h){\
1109 uint8_t tmp[stride*(b_w+HTAPS_MAX-1)];\
1110 assert(h==b_w);\
1111 mc_block(NULL, dst, src-(HTAPS_MAX/2-1)-(HTAPS_MAX/2-1)*stride, tmp, stride, b_w, b_w, dx, dy);\
1112 }
1113
1114 mca( 0, 0,16)
1115 mca( 8, 0,16)
1116 mca( 0, 8,16)
1117 mca( 8, 8,16)
1118 mca( 0, 0,8)
1119 mca( 8, 0,8)
1120 mca( 0, 8,8)
1121 mca( 8, 8,8)
1122
1123 static void pred_block(SnowContext *s, uint8_t *dst, uint8_t *tmp, int stride, int sx, int sy, int b_w, int b_h, BlockNode *block, int plane_index, int w, int h){
1124 if(block->type & BLOCK_INTRA){
1125 int x, y;
1126 const int color = block->color[plane_index];
1127 const int color4= color*0x01010101;
1128 if(b_w==32){
1129 for(y=0; y < b_h; y++){
1130 *(uint32_t*)&dst[0 + y*stride]= color4;
1131 *(uint32_t*)&dst[4 + y*stride]= color4;
1132 *(uint32_t*)&dst[8 + y*stride]= color4;
1133 *(uint32_t*)&dst[12+ y*stride]= color4;
1134 *(uint32_t*)&dst[16+ y*stride]= color4;
1135 *(uint32_t*)&dst[20+ y*stride]= color4;
1136 *(uint32_t*)&dst[24+ y*stride]= color4;
1137 *(uint32_t*)&dst[28+ y*stride]= color4;
1138 }
1139 }else if(b_w==16){
1140 for(y=0; y < b_h; y++){
1141 *(uint32_t*)&dst[0 + y*stride]= color4;
1142 *(uint32_t*)&dst[4 + y*stride]= color4;
1143 *(uint32_t*)&dst[8 + y*stride]= color4;
1144 *(uint32_t*)&dst[12+ y*stride]= color4;
1145 }
1146 }else if(b_w==8){
1147 for(y=0; y < b_h; y++){
1148 *(uint32_t*)&dst[0 + y*stride]= color4;
1149 *(uint32_t*)&dst[4 + y*stride]= color4;
1150 }
1151 }else if(b_w==4){
1152 for(y=0; y < b_h; y++){
1153 *(uint32_t*)&dst[0 + y*stride]= color4;
1154 }
1155 }else{
1156 for(y=0; y < b_h; y++){
1157 for(x=0; x < b_w; x++){
1158 dst[x + y*stride]= color;
1159 }
1160 }
1161 }
1162 }else{
1163 uint8_t *src= s->last_picture[block->ref].data[plane_index];
1164 const int scale= plane_index ? s->mv_scale : 2*s->mv_scale;
1165 int mx= block->mx*scale;
1166 int my= block->my*scale;
1167 const int dx= mx&15;
1168 const int dy= my&15;
1169 const int tab_index= 3 - (b_w>>2) + (b_w>>4);
1170 sx += (mx>>4) - (HTAPS_MAX/2-1);
1171 sy += (my>>4) - (HTAPS_MAX/2-1);
1172 src += sx + sy*stride;
1173 if( (unsigned)sx >= w - b_w - (HTAPS_MAX-2)
1174 || (unsigned)sy >= h - b_h - (HTAPS_MAX-2)){
1175 ff_emulated_edge_mc(tmp + MB_SIZE, src, stride, b_w+HTAPS_MAX-1, b_h+HTAPS_MAX-1, sx, sy, w, h);
1176 src= tmp + MB_SIZE;
1177 }
1178 // assert(b_w == b_h || 2*b_w == b_h || b_w == 2*b_h);
1179 // assert(!(b_w&(b_w-1)));
1180 assert(b_w>1 && b_h>1);
1181 assert((tab_index>=0 && tab_index<4) || b_w==32);
1182 if((dx&3) || (dy&3) || !(b_w == b_h || 2*b_w == b_h || b_w == 2*b_h) || (b_w&(b_w-1)) || !s->plane[plane_index].fast_mc )
1183 mc_block(&s->plane[plane_index], dst, src, tmp, stride, b_w, b_h, dx, dy);
1184 else if(b_w==32){
1185 int y;
1186 for(y=0; y<b_h; y+=16){
1187 s->dsp.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + y*stride, src + 3 + (y+3)*stride,stride);
1188 s->dsp.put_h264_qpel_pixels_tab[0][dy+(dx>>2)](dst + 16 + y*stride, src + 19 + (y+3)*stride,stride);
1189 }
1190 }else if(b_w==b_h)
1191 s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst,src + 3 + 3*stride,stride);
1192 else if(b_w==2*b_h){
1193 s->dsp.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst ,src + 3 + 3*stride,stride);
1194 s->dsp.put_h264_qpel_pixels_tab[tab_index+1][dy+(dx>>2)](dst+b_h,src + 3 + b_h + 3*stride,stride);
1195 }else{
1196 assert(2*b_w==b_h);
1197 s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst ,src + 3 + 3*stride ,stride);
1198 s->dsp.put_h264_qpel_pixels_tab[tab_index ][dy+(dx>>2)](dst+b_w*stride,src + 3 + 3*stride+b_w*stride,stride);
1199 }
1200 }
1201 }
1202
1203 void ff_snow_inner_add_yblock(const uint8_t *obmc, const int obmc_stride, uint8_t * * block, int b_w, int b_h,
1204 int src_x, int src_y, int src_stride, slice_buffer * sb, int add, uint8_t * dst8){
1205 int y, x;
1206 IDWTELEM * dst;
1207 for(y=0; y<b_h; y++){
1208 //FIXME ugly misuse of obmc_stride
1209 const uint8_t *obmc1= obmc + y*obmc_stride;
1210 const uint8_t *obmc2= obmc1+ (obmc_stride>>1);
1211 const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
1212 const uint8_t *obmc4= obmc3+ (obmc_stride>>1);
1213 dst = slice_buffer_get_line(sb, src_y + y);
1214 for(x=0; x<b_w; x++){
1215 int v= obmc1[x] * block[3][x + y*src_stride]
1216 +obmc2[x] * block[2][x + y*src_stride]
1217 +obmc3[x] * block[1][x + y*src_stride]
1218 +obmc4[x] * block[0][x + y*src_stride];
1219
1220 v <<= 8 - LOG2_OBMC_MAX;
1221 if(FRAC_BITS != 8){
1222 v >>= 8 - FRAC_BITS;
1223 }
1224 if(add){
1225 v += dst[x + src_x];
1226 v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
1227 if(v&(~255)) v= ~(v>>31);
1228 dst8[x + y*src_stride] = v;
1229 }else{
1230 dst[x + src_x] -= v;
1231 }
1232 }
1233 }
1234 }
1235
1236 //FIXME name cleanup (b_w, block_w, b_width stuff)
1237 static av_always_inline void add_yblock(SnowContext *s, int sliced, slice_buffer *sb, IDWTELEM *dst, uint8_t *dst8, const uint8_t *obmc, int src_x, int src_y, int b_w, int b_h, int w, int h, int dst_stride, int src_stride, int obmc_stride, int b_x, int b_y, int add, int offset_dst, int plane_index){
1238 const int b_width = s->b_width << s->block_max_depth;
1239 const int b_height= s->b_height << s->block_max_depth;
1240 const int b_stride= b_width;
1241 BlockNode *lt= &s->block[b_x + b_y*b_stride];
1242 BlockNode *rt= lt+1;
1243 BlockNode *lb= lt+b_stride;
1244 BlockNode *rb= lb+1;
1245 uint8_t *block[4];
1246 int tmp_step= src_stride >= 7*MB_SIZE ? MB_SIZE : MB_SIZE*src_stride;
1247 uint8_t *tmp = s->scratchbuf;
1248 uint8_t *ptmp;
1249 int x,y;
1250
1251 if(b_x<0){
1252 lt= rt;
1253 lb= rb;
1254 }else if(b_x + 1 >= b_width){
1255 rt= lt;
1256 rb= lb;
1257 }
1258 if(b_y<0){
1259 lt= lb;
1260 rt= rb;
1261 }else if(b_y + 1 >= b_height){
1262 lb= lt;
1263 rb= rt;
1264 }
1265
1266 if(src_x<0){ //FIXME merge with prev & always round internal width up to *16
1267 obmc -= src_x;
1268 b_w += src_x;
1269 if(!sliced && !offset_dst)
1270 dst -= src_x;
1271 src_x=0;
1272 }else if(src_x + b_w > w){
1273 b_w = w - src_x;
1274 }
1275 if(src_y<0){
1276 obmc -= src_y*obmc_stride;
1277 b_h += src_y;
1278 if(!sliced && !offset_dst)
1279 dst -= src_y*dst_stride;
1280 src_y=0;
1281 }else if(src_y + b_h> h){
1282 b_h = h - src_y;
1283 }
1284
1285 if(b_w<=0 || b_h<=0) return;
1286
1287 assert(src_stride > 2*MB_SIZE + 5);
1288
1289 if(!sliced && offset_dst)
1290 dst += src_x + src_y*dst_stride;
1291 dst8+= src_x + src_y*src_stride;
1292 // src += src_x + src_y*src_stride;
1293
1294 ptmp= tmp + 3*tmp_step;
1295 block[0]= ptmp;
1296 ptmp+=tmp_step;
1297 pred_block(s, block[0], tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
1298
1299 if(same_block(lt, rt)){
1300 block[1]= block[0];
1301 }else{
1302 block[1]= ptmp;
1303 ptmp+=tmp_step;
1304 pred_block(s, block[1], tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
1305 }
1306
1307 if(same_block(lt, lb)){
1308 block[2]= block[0];
1309 }else if(same_block(rt, lb)){
1310 block[2]= block[1];
1311 }else{
1312 block[2]= ptmp;
1313 ptmp+=tmp_step;
1314 pred_block(s, block[2], tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
1315 }
1316
1317 if(same_block(lt, rb) ){
1318 block[3]= block[0];
1319 }else if(same_block(rt, rb)){
1320 block[3]= block[1];
1321 }else if(same_block(lb, rb)){
1322 block[3]= block[2];
1323 }else{
1324 block[3]= ptmp;
1325 pred_block(s, block[3], tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
1326 }
1327 #if 0
1328 for(y=0; y<b_h; y++){
1329 for(x=0; x<b_w; x++){
1330 int v= obmc [x + y*obmc_stride] * block[3][x + y*src_stride] * (256/OBMC_MAX);
1331 if(add) dst[x + y*dst_stride] += v;
1332 else dst[x + y*dst_stride] -= v;
1333 }
1334 }
1335 for(y=0; y<b_h; y++){
1336 uint8_t *obmc2= obmc + (obmc_stride>>1);
1337 for(x=0; x<b_w; x++){
1338 int v= obmc2[x + y*obmc_stride] * block[2][x + y*src_stride] * (256/OBMC_MAX);
1339 if(add) dst[x + y*dst_stride] += v;
1340 else dst[x + y*dst_stride] -= v;
1341 }
1342 }
1343 for(y=0; y<b_h; y++){
1344 uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
1345 for(x=0; x<b_w; x++){
1346 int v= obmc3[x + y*obmc_stride] * block[1][x + y*src_stride] * (256/OBMC_MAX);
1347 if(add) dst[x + y*dst_stride] += v;
1348 else dst[x + y*dst_stride] -= v;
1349 }
1350 }
1351 for(y=0; y<b_h; y++){
1352 uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
1353 uint8_t *obmc4= obmc3+ (obmc_stride>>1);
1354 for(x=0; x<b_w; x++){
1355 int v= obmc4[x + y*obmc_stride] * block[0][x + y*src_stride] * (256/OBMC_MAX);
1356 if(add) dst[x + y*dst_stride] += v;
1357 else dst[x + y*dst_stride] -= v;
1358 }
1359 }
1360 #else
1361 if(sliced){
1362 s->dwt.inner_add_yblock(obmc, obmc_stride, block, b_w, b_h, src_x,src_y, src_stride, sb, add, dst8);
1363 }else{
1364 for(y=0; y<b_h; y++){
1365 //FIXME ugly misuse of obmc_stride
1366 const uint8_t *obmc1= obmc + y*obmc_stride;
1367 const uint8_t *obmc2= obmc1+ (obmc_stride>>1);
1368 const uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
1369 const uint8_t *obmc4= obmc3+ (obmc_stride>>1);
1370 for(x=0; x<b_w; x++){
1371 int v= obmc1[x] * block[3][x + y*src_stride]
1372 +obmc2[x] * block[2][x + y*src_stride]
1373 +obmc3[x] * block[1][x + y*src_stride]
1374 +obmc4[x] * block[0][x + y*src_stride];
1375
1376 v <<= 8 - LOG2_OBMC_MAX;
1377 if(FRAC_BITS != 8){
1378 v >>= 8 - FRAC_BITS;
1379 }
1380 if(add){
1381 v += dst[x + y*dst_stride];
1382 v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
1383 if(v&(~255)) v= ~(v>>31);
1384 dst8[x + y*src_stride] = v;
1385 }else{
1386 dst[x + y*dst_stride] -= v;
1387 }
1388 }
1389 }
1390 }
1391 #endif /* 0 */
1392 }
1393
1394 static av_always_inline void predict_slice_buffered(SnowContext *s, slice_buffer * sb, IDWTELEM * old_buffer, int plane_index, int add, int mb_y){
1395 Plane *p= &s->plane[plane_index];
1396 const int mb_w= s->b_width << s->block_max_depth;
1397 const int mb_h= s->b_height << s->block_max_depth;
1398 int x, y, mb_x;
1399 int block_size = MB_SIZE >> s->block_max_depth;
1400 int block_w = plane_index ? block_size/2 : block_size;
1401 const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
1402 int obmc_stride= plane_index ? block_size : 2*block_size;
1403 int ref_stride= s->current_picture.linesize[plane_index];
1404 uint8_t *dst8= s->current_picture.data[plane_index];
1405 int w= p->width;
1406 int h= p->height;
1407
1408 if(s->keyframe || (s->avctx->debug&512)){
1409 if(mb_y==mb_h)
1410 return;
1411
1412 if(add){
1413 for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
1414 // DWTELEM * line = slice_buffer_get_line(sb, y);
1415 IDWTELEM * line = sb->line[y];
1416 for(x=0; x<w; x++){
1417 // int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
1418 int v= line[x] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
1419 v >>= FRAC_BITS;
1420 if(v&(~255)) v= ~(v>>31);
1421 dst8[x + y*ref_stride]= v;
1422 }
1423 }
1424 }else{
1425 for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
1426 // DWTELEM * line = slice_buffer_get_line(sb, y);
1427 IDWTELEM * line = sb->line[y];
1428 for(x=0; x<w; x++){
1429 line[x] -= 128 << FRAC_BITS;
1430 // buf[x + y*w]-= 128<<FRAC_BITS;
1431 }
1432 }
1433 }
1434
1435 return;
1436 }
1437
1438 for(mb_x=0; mb_x<=mb_w; mb_x++){
1439 add_yblock(s, 1, sb, old_buffer, dst8, obmc,
1440 block_w*mb_x - block_w/2,
1441 block_w*mb_y - block_w/2,
1442 block_w, block_w,
1443 w, h,
1444 w, ref_stride, obmc_stride,
1445 mb_x - 1, mb_y - 1,
1446 add, 0, plane_index);
1447 }
1448 }
1449
1450 static av_always_inline void predict_slice(SnowContext *s, IDWTELEM *buf, int plane_index, int add, int mb_y){
1451 Plane *p= &s->plane[plane_index];
1452 const int mb_w= s->b_width << s->block_max_depth;
1453 const int mb_h= s->b_height << s->block_max_depth;
1454 int x, y, mb_x;
1455 int block_size = MB_SIZE >> s->block_max_depth;
1456 int block_w = plane_index ? block_size/2 : block_size;
1457 const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
1458 const int obmc_stride= plane_index ? block_size : 2*block_size;
1459 int ref_stride= s->current_picture.linesize[plane_index];
1460 uint8_t *dst8= s->current_picture.data[plane_index];
1461 int w= p->width;
1462 int h= p->height;
1463
1464 if(s->keyframe || (s->avctx->debug&512)){
1465 if(mb_y==mb_h)
1466 return;
1467
1468 if(add){
1469 for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
1470 for(x=0; x<w; x++){
1471 int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
1472 v >>= FRAC_BITS;
1473 if(v&(~255)) v= ~(v>>31);
1474 dst8[x + y*ref_stride]= v;
1475 }
1476 }
1477 }else{
1478 for(y=block_w*mb_y; y<FFMIN(h,block_w*(mb_y+1)); y++){
1479 for(x=0; x<w; x++){
1480 buf[x + y*w]-= 128<<FRAC_BITS;
1481 }
1482 }
1483 }
1484
1485 return;
1486 }
1487
1488 for(mb_x=0; mb_x<=mb_w; mb_x++){
1489 add_yblock(s, 0, NULL, buf, dst8, obmc,
1490 block_w*mb_x - block_w/2,
1491 block_w*mb_y - block_w/2,
1492 block_w, block_w,
1493 w, h,
1494 w, ref_stride, obmc_stride,
1495 mb_x - 1, mb_y - 1,
1496 add, 1, plane_index);
1497 }
1498 }
1499
1500 static av_always_inline void predict_plane(SnowContext *s, IDWTELEM *buf, int plane_index, int add){
1501 const int mb_h= s->b_height << s->block_max_depth;
1502 int mb_y;
1503 for(mb_y=0; mb_y<=mb_h; mb_y++)
1504 predict_slice(s, buf, plane_index, add, mb_y);
1505 }
1506
1507 static void dequantize_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, IDWTELEM *src, int stride, int start_y, int end_y){
1508 const int w= b->width;
1509 const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
1510 const int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
1511 const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
1512 int x,y;
1513
1514 if(s->qlog == LOSSLESS_QLOG) return;
1515
1516 for(y=start_y; y<end_y; y++){
1517 // DWTELEM * line = slice_buffer_get_line_from_address(sb, src + (y * stride));
1518 IDWTELEM * line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
1519 for(x=0; x<w; x++){
1520 int i= line[x];
1521 if(i<0){
1522 line[x]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
1523 }else if(i>0){
1524 line[x]= (( i*qmul + qadd)>>(QEXPSHIFT));
1525 }
1526 }
1527 }
1528 }
1529
1530 static void correlate_slice_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, IDWTELEM *src, int stride, int inverse, int use_median, int start_y, int end_y){
1531 const int w= b->width;
1532 int x,y;
1533
1534 IDWTELEM * line=0; // silence silly "could be used without having been initialized" warning
1535 IDWTELEM * prev;
1536
1537 if (start_y != 0)
1538 line = slice_buffer_get_line(sb, ((start_y - 1) * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
1539
1540 for(y=start_y; y<end_y; y++){
1541 prev = line;
1542 // line = slice_buffer_get_line_from_address(sb, src + (y * stride));
1543 line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
1544 for(x=0; x<w; x++){
1545 if(x){
1546 if(use_median){
1547 if(y && x+1<w) line[x] += mid_pred(line[x - 1], prev[x], prev[x + 1]);
1548 else line[x] += line[x - 1];
1549 }else{
1550 if(y) line[x] += mid_pred(line[x - 1], prev[x], line[x - 1] + prev[x] - prev[x - 1]);
1551 else line[x] += line[x - 1];
1552 }
1553 }else{
1554 if(y) line[x] += prev[x];
1555 }
1556 }
1557 }
1558 }
1559
1560 static void decode_qlogs(SnowContext *s){
1561 int plane_index, level, orientation;
1562
1563 for(plane_index=0; plane_index<3; plane_index++){
1564 for(level=0; level<s->spatial_decomposition_count; level++){
1565 for(orientation=level ? 1:0; orientation<4; orientation++){
1566 int q;
1567 if (plane_index==2) q= s->plane[1].band[level][orientation].qlog;
1568 else if(orientation==2) q= s->plane[plane_index].band[level][1].qlog;
1569 else q= get_symbol(&s->c, s->header_state, 1);
1570 s->plane[plane_index].band[level][orientation].qlog= q;
1571 }
1572 }
1573 }
1574 }
1575
1576 #define GET_S(dst, check) \
1577 tmp= get_symbol(&s->c, s->header_state, 0);\
1578 if(!(check)){\
1579 av_log(s->avctx, AV_LOG_ERROR, "Error " #dst " is %d\n", tmp);\
1580 return -1;\
1581 }\
1582 dst= tmp;
1583
1584 static int decode_header(SnowContext *s){
1585 int plane_index, tmp;
1586 uint8_t kstate[32];
1587
1588 memset(kstate, MID_STATE, sizeof(kstate));
1589
1590 s->keyframe= get_rac(&s->c, kstate);
1591 if(s->keyframe || s->always_reset){
1592 reset_contexts(s);
1593 s->spatial_decomposition_type=
1594 s->qlog=
1595 s->qbias=
1596 s->mv_scale=
1597 s->block_max_depth= 0;
1598 }
1599 if(s->keyframe){
1600 GET_S(s->version, tmp <= 0U)
1601 s->always_reset= get_rac(&s->c, s->header_state);
1602 s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0);
1603 s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0);
1604 GET_S(s->spatial_decomposition_count, 0 < tmp && tmp <= MAX_DECOMPOSITIONS)
1605 s->colorspace_type= get_symbol(&s->c, s->header_state, 0);
1606 s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0);
1607 s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0);
1608 s->spatial_scalability= get_rac(&s->c, s->header_state);
1609 // s->rate_scalability= get_rac(&s->c, s->header_state);
1610 GET_S(s->max_ref_frames, tmp < (unsigned)MAX_REF_FRAMES)
1611 s->max_ref_frames++;
1612
1613 decode_qlogs(s);
1614 }
1615
1616 if(!s->keyframe){
1617 if(get_rac(&s->c, s->header_state)){
1618 for(plane_index=0; plane_index<2; plane_index++){
1619 int htaps, i, sum=0;
1620 Plane *p= &s->plane[plane_index];
1621 p->diag_mc= get_rac(&s->c, s->header_state);
1622 htaps= get_symbol(&s->c, s->header_state, 0)*2 + 2;
1623 if((unsigned)htaps > HTAPS_MAX || htaps==0)
1624 return -1;
1625 p->htaps= htaps;
1626 for(i= htaps/2; i; i--){
1627 p->hcoeff[i]= get_symbol(&s->c, s->header_state, 0) * (1-2*(i&1));
1628 sum += p->hcoeff[i];
1629 }
1630 p->hcoeff[0]= 32-sum;
1631 }
1632 s->plane[2].diag_mc= s->plane[1].diag_mc;
1633 s->plane[2].htaps = s->plane[1].htaps;
1634 memcpy(s->plane[2].hcoeff, s->plane[1].hcoeff, sizeof(s->plane[1].hcoeff));
1635 }
1636 if(get_rac(&s->c, s->header_state)){
1637 GET_S(s->spatial_decomposition_count, 0 < tmp && tmp <= MAX_DECOMPOSITIONS)
1638 decode_qlogs(s);
1639 }
1640 }
1641
1642 s->spatial_decomposition_type+= get_symbol(&s->c, s->header_state, 1);
1643 if(s->spatial_decomposition_type > 1U){
1644 av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported", s->spatial_decomposition_type);
1645 return -1;
1646 }
1647 if(FFMIN(s->avctx-> width>>s->chroma_h_shift,
1648 s->avctx->height>>s->chroma_v_shift) >> (s->spatial_decomposition_count-1) <= 0){
1649 av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_count %d too large for size", s->spatial_decomposition_count);
1650 return -1;
1651 }
1652
1653 s->qlog += get_symbol(&s->c, s->header_state, 1);
1654 s->mv_scale += get_symbol(&s->c, s->header_state, 1);
1655 s->qbias += get_symbol(&s->c, s->header_state, 1);
1656 s->block_max_depth+= get_symbol(&s->c, s->header_state, 1);
1657 if(s->block_max_depth > 1 || s->block_max_depth < 0){
1658 av_log(s->avctx, AV_LOG_ERROR, "block_max_depth= %d is too large", s->block_max_depth);
1659 s->block_max_depth= 0;
1660 return -1;
1661 }
1662
1663 return 0;
1664 }
1665
1666 static void init_qexp(void){
1667 int i;
1668 double v=128;
1669
1670 for(i=0; i<QROOT; i++){
1671 qexp[i]= lrintf(v);
1672 v *= pow(2, 1.0 / QROOT);
1673 }
1674 }
1675
1676 static av_cold int common_init(AVCodecContext *avctx){
1677 SnowContext *s = avctx->priv_data;
1678 int width, height;
1679 int i, j;
1680
1681 s->avctx= avctx;
1682 s->max_ref_frames=1; //just make sure its not an invalid value in case of no initial keyframe
1683
1684 dsputil_init(&s->dsp, avctx);
1685 ff_dwt_init(&s->dwt);
1686
1687 #define mcf(dx,dy)\
1688 s->dsp.put_qpel_pixels_tab [0][dy+dx/4]=\
1689 s->dsp.put_no_rnd_qpel_pixels_tab[0][dy+dx/4]=\
1690 s->dsp.put_h264_qpel_pixels_tab[0][dy+dx/4];\
1691 s->dsp.put_qpel_pixels_tab [1][dy+dx/4]=\
1692 s->dsp.put_no_rnd_qpel_pixels_tab[1][dy+dx/4]=\
1693 s->dsp.put_h264_qpel_pixels_tab[1][dy+dx/4];
1694
1695 mcf( 0, 0)
1696 mcf( 4, 0)
1697 mcf( 8, 0)
1698 mcf(12, 0)
1699 mcf( 0, 4)
1700 mcf( 4, 4)
1701 mcf( 8, 4)
1702 mcf(12, 4)
1703 mcf( 0, 8)
1704 mcf( 4, 8)
1705 mcf( 8, 8)
1706 mcf(12, 8)
1707 mcf( 0,12)
1708 mcf( 4,12)
1709 mcf( 8,12)
1710 mcf(12,12)
1711
1712 #define mcfh(dx,dy)\
1713 s->dsp.put_pixels_tab [0][dy/4+dx/8]=\
1714 s->dsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
1715 mc_block_hpel ## dx ## dy ## 16;\
1716 s->dsp.put_pixels_tab [1][dy/4+dx/8]=\
1717 s->dsp.put_no_rnd_pixels_tab[1][dy/4+dx/8]=\
1718 mc_block_hpel ## dx ## dy ## 8;
1719
1720 mcfh(0, 0)
1721 mcfh(8, 0)
1722 mcfh(0, 8)
1723 mcfh(8, 8)
1724
1725 if(!qexp[0])
1726 init_qexp();
1727
1728 // dec += FFMAX(s->chroma_h_shift, s->chroma_v_shift);
1729
1730 width= s->avctx->width;
1731 height= s->avctx->height;
1732
1733 s->spatial_idwt_buffer= av_mallocz(width*height*sizeof(IDWTELEM));
1734 s->spatial_dwt_buffer= av_mallocz(width*height*sizeof(DWTELEM)); //FIXME this does not belong here
1735
1736 for(i=0; i<MAX_REF_FRAMES; i++)
1737 for(j=0; j<MAX_REF_FRAMES; j++)
1738 scale_mv_ref[i][j] = 256*(i+1)/(j+1);
1739
1740 s->avctx->get_buffer(s->avctx, &s->mconly_picture);
1741 s->scratchbuf = av_malloc(s->mconly_picture.linesize[0]*7*MB_SIZE);
1742
1743 return 0;
1744 }
1745
1746 static int common_init_after_header(AVCodecContext *avctx){
1747 SnowContext *s = avctx->priv_data;
1748 int plane_index, level, orientation;
1749
1750 for(plane_index=0; plane_index<3; plane_index++){
1751 int w= s->avctx->width;
1752 int h= s->avctx->height;
1753
1754 if(plane_index){
1755 w>>= s->chroma_h_shift;
1756 h>>= s->chroma_v_shift;
1757 }
1758 s->plane[plane_index].width = w;
1759 s->plane[plane_index].height= h;
1760
1761 for(level=s->spatial_decomposition_count-1; level>=0; level--){
1762 for(orientation=level ? 1 : 0; orientation<4; orientation++){
1763 SubBand *b= &s->plane[plane_index].band[level][orientation];
1764
1765 b->buf= s->spatial_dwt_buffer;
1766 b->level= level;
1767 b->stride= s->plane[plane_index].width << (s->spatial_decomposition_count - level);
1768 b->width = (w + !(orientation&1))>>1;
1769 b->height= (h + !(orientation>1))>>1;
1770
1771 b->stride_line = 1 << (s->spatial_decomposition_count - level);
1772 b->buf_x_offset = 0;
1773 b->buf_y_offset = 0;
1774
1775 if(orientation&1){
1776 b->buf += (w+1)>>1;
1777 b->buf_x_offset = (w+1)>>1;
1778 }
1779 if(orientation>1){
1780 b->buf += b->stride>>1;
1781 b->buf_y_offset = b->stride_line >> 1;
1782 }
1783 b->ibuf= s->spatial_idwt_buffer + (b->buf - s->spatial_dwt_buffer);
1784
1785 if(level)
1786 b->parent= &s->plane[plane_index].band[level-1][orientation];
1787 //FIXME avoid this realloc
1788 av_freep(&b->x_coeff);
1789 b->x_coeff=av_mallocz(((b->width+1) * b->height+1)*sizeof(x_and_coeff));
1790 }
1791 w= (w+1)>>1;
1792 h= (h+1)>>1;
1793 }
1794 }
1795
1796 return 0;
1797 }
1798
1799 #define QUANTIZE2 0
1800
1801 #if QUANTIZE2==1
1802 #define Q2_STEP 8
1803
1804 static void find_sse(SnowContext *s, Plane *p, int *score, int score_stride, IDWTELEM *r0, IDWTELEM *r1, int level, int orientation){
1805 SubBand *b= &p->band[level][orientation];
1806 int x, y;
1807 int xo=0;
1808 int yo=0;
1809 int step= 1 << (s->spatial_decomposition_count - level);
1810
1811 if(orientation&1)
1812 xo= step>>1;
1813 if(orientation&2)
1814 yo= step>>1;
1815
1816 //FIXME bias for nonzero ?
1817 //FIXME optimize
1818 memset(score, 0, sizeof(*score)*score_stride*((p->height + Q2_STEP-1)/Q2_STEP));
1819 for(y=0; y<p->height; y++){
1820 for(x=0; x<p->width; x++){
1821 int sx= (x-xo + step/2) / step / Q2_STEP;
1822 int sy= (y-yo + step/2) / step / Q2_STEP;
1823 int v= r0[x + y*p->width] - r1[x + y*p->width];
1824 assert(sx>=0 && sy>=0 && sx < score_stride);
1825 v= ((v+8)>>4)<<4;
1826 score[sx + sy*score_stride] += v*v;
1827 assert(score[sx + sy*score_stride] >= 0);
1828 }
1829 }
1830 }
1831
1832 static void dequantize_all(SnowContext *s, Plane *p, IDWTELEM *buffer, int width, int height){
1833 int level, orientation;
1834
1835 for(level=0; level<s->spatial_decomposition_count; level++){
1836 for(orientation=level ? 1 : 0; orientation<4; orientation++){
1837 SubBand *b= &p->band[level][orientation];
1838 IDWTELEM *dst= buffer + (b->ibuf - s->spatial_idwt_buffer);
1839
1840 dequantize(s, b, dst, b->stride);
1841 }
1842 }
1843 }
1844
1845 static void dwt_quantize(SnowContext *s, Plane *p, DWTELEM *buffer, int width, int height, int stride, int type){
1846 int level, orientation, ys, xs, x, y, pass;
1847 IDWTELEM best_dequant[height * stride];
1848 IDWTELEM idwt2_buffer[height * stride];
1849 const int score_stride= (width + 10)/Q2_STEP;
1850 int best_score[(width + 10)/Q2_STEP * (height + 10)/Q2_STEP]; //FIXME size
1851 int score[(width + 10)/Q2_STEP * (height + 10)/Q2_STEP]; //FIXME size
1852 int threshold= (s->m.lambda * s->m.lambda) >> 6;
1853
1854 //FIXME pass the copy cleanly ?
1855
1856 // memcpy(dwt_buffer, buffer, height * stride * sizeof(DWTELEM));
1857 ff_spatial_dwt(buffer, width, height, stride, type, s->spatial_decomposition_count);
1858
1859 for(level=0; level<s->spatial_decomposition_count; level++){
1860 for(orientation=level ? 1 : 0; orientation<4; orientation++){
1861 SubBand *b= &p->band[level][orientation];
1862 IDWTELEM *dst= best_dequant + (b->ibuf - s->spatial_idwt_buffer);
1863 DWTELEM *src= buffer + (b-> buf - s->spatial_dwt_buffer);
1864 assert(src == b->buf); // code does not depend on this but it is true currently
1865
1866 quantize(s, b, dst, src, b->stride, s->qbias);
1867 }
1868 }
1869 for(pass=0; pass<1; pass++){
1870 if(s->qbias == 0) //keyframe
1871 continue;
1872 for(level=0; level<s->spatial_decomposition_count; level++){
1873 for(orientation=level ? 1 : 0; orientation<4; orientation++){
1874 SubBand *b= &p->band[level][orientation];
1875 IDWTELEM *dst= idwt2_buffer + (b->ibuf - s->spatial_idwt_buffer);
1876 IDWTELEM *best_dst= best_dequant + (b->ibuf - s->spatial_idwt_buffer);
1877
1878 for(ys= 0; ys<Q2_STEP; ys++){
1879 for(xs= 0; xs<Q2_STEP; xs++){
1880 memcpy(idwt2_buffer, best_dequant, height * stride * sizeof(IDWTELEM));
1881 dequantize_all(s, p, idwt2_buffer, width, height);
1882 ff_spatial_idwt(idwt2_buffer, width, height, stride, type, s->spatial_decomposition_count);
1883 find_sse(s, p, best_score, score_stride, idwt2_buffer, s->spatial_idwt_buffer, level, orientation);
1884 memcpy(idwt2_buffer, best_dequant, height * stride * sizeof(IDWTELEM));
1885 for(y=ys; y<b->height; y+= Q2_STEP){
1886 for(x=xs; x<b->width; x+= Q2_STEP){
1887 if(dst[x + y*b->stride]<0) dst[x + y*b->stride]++;
1888 if(dst[x + y*b->stride]>0) dst[x + y*b->stride]--;
1889 //FIXME try more than just --
1890 }
1891 }
1892 dequantize_all(s, p, idwt2_buffer, width, height);
1893 ff_spatial_idwt(idwt2_buffer, width, height, stride, type, s->spatial_decomposition_count);
1894 find_sse(s, p, score, score_stride, idwt2_buffer, s->spatial_idwt_buffer, level, orientation);
1895 for(y=ys; y<b->height; y+= Q2_STEP){
1896 for(x=xs; x<b->width; x+= Q2_STEP){
1897 int score_idx= x/Q2_STEP + (y/Q2_STEP)*score_stride;
1898 if(score[score_idx] <= best_score[score_idx] + threshold){
1899 best_score[score_idx]= score[score_idx];
1900 if(best_dst[x + y*b->stride]<0) best_dst[x + y*b->stride]++;
1901 if(best_dst[x + y*b->stride]>0) best_dst[x + y*b->stride]--;
1902 //FIXME copy instead
1903 }
1904 }
1905 }
1906 }
1907 }
1908 }
1909 }
1910 }
1911 memcpy(s->spatial_idwt_buffer, best_dequant, height * stride * sizeof(IDWTELEM)); //FIXME work with that directly instead of copy at the end
1912 }
1913
1914 #endif /* QUANTIZE2==1 */
1915
1916 #define USE_HALFPEL_PLANE 0
1917
1918 static void halfpel_interpol(SnowContext *s, uint8_t *halfpel[4][4], AVFrame *frame){
1919 int p,x,y;
1920
1921 assert(!(s->avctx->flags & CODEC_FLAG_EMU_EDGE));
1922
1923 for(p=0; p<3; p++){
1924 int is_chroma= !!p;
1925 int w= s->avctx->width >>is_chroma;
1926 int h= s->avctx->height >>is_chroma;
1927 int ls= frame->linesize[p];
1928 uint8_t *src= frame->data[p];
1929
1930 halfpel[1][p]= (uint8_t*)av_malloc(ls * (h+2*EDGE_WIDTH)) + EDGE_WIDTH*(1+ls);
1931 halfpel[2][p]= (uint8_t*)av_malloc(ls * (h+2*EDGE_WIDTH)) + EDGE_WIDTH*(1+ls