Remove unused variables and the corresponding warnings along with them.
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "avcodec.h"
20 #include "common.h"
21 #include "dsputil.h"
22 #include "snow.h"
23
24 #include "rangecoder.h"
25
26 #include "mpegvideo.h"
27
28 #undef NDEBUG
29 #include <assert.h>
30
31 static const int8_t quant3[256]={
32 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
33 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
34 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
35 1, 1, 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,-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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179 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,
180 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,
181 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,
182 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,
183 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,
184 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,
185 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,
186 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,
187 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,
188 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,
189 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,
190 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,
191 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,
192 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,
193 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,
194 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,
195 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,
196 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,
197 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,
198 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,
199 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,
200 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,
201 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,
202 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,
203 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,
204 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,
205 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,
206 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,
207 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,
208 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,
209 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,
210 //error:0.000022
211 };
212 static const uint8_t obmc16[256]={
213 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
214 0, 1, 1, 2, 4, 5, 5, 6, 6, 5, 5, 4, 2, 1, 1, 0,
215 0, 1, 4, 6, 9,11,13,15,15,13,11, 9, 6, 4, 1, 0,
216 0, 2, 6,11,15,20,24,26,26,24,20,15,11, 6, 2, 0,
217 0, 4, 9,15,23,29,34,38,38,34,29,23,15, 9, 4, 0,
218 0, 5,11,20,29,38,45,49,49,45,38,29,20,11, 5, 0,
219 1, 5,13,24,34,45,53,57,57,53,45,34,24,13, 5, 1,
220 1, 6,15,26,38,49,57,62,62,57,49,38,26,15, 6, 1,
221 1, 6,15,26,38,49,57,62,62,57,49,38,26,15, 6, 1,
222 1, 5,13,24,34,45,53,57,57,53,45,34,24,13, 5, 1,
223 0, 5,11,20,29,38,45,49,49,45,38,29,20,11, 5, 0,
224 0, 4, 9,15,23,29,34,38,38,34,29,23,15, 9, 4, 0,
225 0, 2, 6,11,15,20,24,26,26,24,20,15,11, 6, 2, 0,
226 0, 1, 4, 6, 9,11,13,15,15,13,11, 9, 6, 4, 1, 0,
227 0, 1, 1, 2, 4, 5, 5, 6, 6, 5, 5, 4, 2, 1, 1, 0,
228 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
229 //error:0.000033
230 };
231 #elif 1 // 64*linear
232 static const uint8_t obmc32[1024]={
233 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,
234 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,
235 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,
236 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,
237 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,
238 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,
239 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,
240 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,
241 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,
242 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,
243 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,
244 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,
245 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,
246 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,
247 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,
248 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,
249 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,
250 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,
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, 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,
253 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,
254 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,
255 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,
256 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,
257 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,
258 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,
259 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,
260 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,
261 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,
262 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,
263 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,
264 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,
265 //error:0.000020
266 };
267 static const uint8_t obmc16[256]={
268 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0,
269 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4,
270 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4,
271 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8,
272 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8,
273 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
274 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
275 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
276 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
277 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
278 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
279 8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28, 8,
280 8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20, 8,
281 4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16, 4,
282 4, 8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16, 8, 4,
283 0, 4, 4, 8, 8, 12, 12, 16, 16, 12, 12, 8, 8, 4, 4, 0,
284 //error:0.000015
285 };
286 #else //64*cos
287 static const uint8_t obmc32[1024]={
288 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,
289 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,
290 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,
291 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,
292 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,
293 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,
294 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,
295 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,
296 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,
297 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,
298 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,
299 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,
300 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,
301 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,
302 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,
303 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,
304 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,
305 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,
306 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,
307 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,
308 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,
309 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,
310 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,
311 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,
312 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,
313 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,
314 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,
315 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,
316 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,
317 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,
318 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,
319 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,
320 //error:0.000022
321 };
322 static const uint8_t obmc16[256]={
323 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
324 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0,
325 0, 1, 3, 6, 8,11,13,14,14,13,11, 8, 6, 3, 1, 0,
326 0, 2, 6,10,15,20,24,26,26,24,20,15,10, 6, 2, 0,
327 0, 3, 8,16,23,30,35,38,38,35,30,23,16, 8, 3, 0,
328 1, 4,11,20,30,39,46,49,49,46,39,30,20,11, 4, 1,
329 1, 5,13,24,35,46,54,58,58,54,46,35,24,13, 5, 1,
330 0, 5,14,26,38,49,58,63,63,58,49,38,26,14, 5, 0,
331 0, 5,14,26,38,49,58,63,63,58,49,38,26,14, 5, 0,
332 1, 5,13,24,35,46,54,58,58,54,46,35,24,13, 5, 1,
333 1, 4,11,20,30,39,46,49,49,46,39,30,20,11, 4, 1,
334 0, 3, 8,16,23,30,35,38,38,35,30,23,16, 8, 3, 0,
335 0, 2, 6,10,15,20,24,26,26,24,20,15,10, 6, 2, 0,
336 0, 1, 3, 6, 8,11,13,14,14,13,11, 8, 6, 3, 1, 0,
337 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0,
338 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
339 //error:0.000022
340 };
341 #endif
342
343 //linear *64
344 static const uint8_t obmc8[64]={
345 4, 12, 20, 28, 28, 20, 12, 4,
346 12, 36, 60, 84, 84, 60, 36, 12,
347 20, 60,100,140,140,100, 60, 20,
348 28, 84,140,196,196,140, 84, 28,
349 28, 84,140,196,196,140, 84, 28,
350 20, 60,100,140,140,100, 60, 20,
351 12, 36, 60, 84, 84, 60, 36, 12,
352 4, 12, 20, 28, 28, 20, 12, 4,
353 //error:0.000000
354 };
355
356 //linear *64
357 static const uint8_t obmc4[16]={
358 16, 48, 48, 16,
359 48,144,144, 48,
360 48,144,144, 48,
361 16, 48, 48, 16,
362 //error:0.000000
363 };
364
365 static const uint8_t *obmc_tab[4]={
366 obmc32, obmc16, obmc8, obmc4
367 };
368
369 static int scale_mv_ref[MAX_REF_FRAMES][MAX_REF_FRAMES];
370
371 typedef struct BlockNode{
372 int16_t mx;
373 int16_t my;
374 uint8_t ref;
375 uint8_t color[3];
376 uint8_t type;
377 //#define TYPE_SPLIT 1
378 #define BLOCK_INTRA 1
379 #define BLOCK_OPT 2
380 //#define TYPE_NOCOLOR 4
381 uint8_t level; //FIXME merge into type?
382 }BlockNode;
383
384 static const BlockNode null_block= { //FIXME add border maybe
385 .color= {128,128,128},
386 .mx= 0,
387 .my= 0,
388 .ref= 0,
389 .type= 0,
390 .level= 0,
391 };
392
393 #define LOG2_MB_SIZE 4
394 #define MB_SIZE (1<<LOG2_MB_SIZE)
395
396 typedef struct x_and_coeff{
397 int16_t x;
398 uint16_t coeff;
399 } x_and_coeff;
400
401 typedef struct SubBand{
402 int level;
403 int stride;
404 int width;
405 int height;
406 int qlog; ///< log(qscale)/log[2^(1/6)]
407 DWTELEM *buf;
408 int buf_x_offset;
409 int buf_y_offset;
410 int stride_line; ///< Stride measured in lines, not pixels.
411 x_and_coeff * x_coeff;
412 struct SubBand *parent;
413 uint8_t state[/*7*2*/ 7 + 512][32];
414 }SubBand;
415
416 typedef struct Plane{
417 int width;
418 int height;
419 SubBand band[MAX_DECOMPOSITIONS][4];
420 }Plane;
421
422 typedef struct SnowContext{
423 // 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)
424
425 AVCodecContext *avctx;
426 RangeCoder c;
427 DSPContext dsp;
428 AVFrame new_picture;
429 AVFrame input_picture; ///< new_picture with the internal linesizes
430 AVFrame current_picture;
431 AVFrame last_picture[MAX_REF_FRAMES];
432 AVFrame mconly_picture;
433 // uint8_t q_context[16];
434 uint8_t header_state[32];
435 uint8_t block_state[128 + 32*128];
436 int keyframe;
437 int always_reset;
438 int version;
439 int spatial_decomposition_type;
440 int temporal_decomposition_type;
441 int spatial_decomposition_count;
442 int temporal_decomposition_count;
443 int max_ref_frames;
444 int ref_frames;
445 int16_t (*ref_mvs[MAX_REF_FRAMES])[2];
446 uint32_t *ref_scores[MAX_REF_FRAMES];
447 DWTELEM *spatial_dwt_buffer;
448 int colorspace_type;
449 int chroma_h_shift;
450 int chroma_v_shift;
451 int spatial_scalability;
452 int qlog;
453 int lambda;
454 int lambda2;
455 int pass1_rc;
456 int mv_scale;
457 int qbias;
458 #define QBIAS_SHIFT 3
459 int b_width;
460 int b_height;
461 int block_max_depth;
462 Plane plane[MAX_PLANES];
463 BlockNode *block;
464 #define ME_CACHE_SIZE 1024
465 int me_cache[ME_CACHE_SIZE];
466 int me_cache_generation;
467 slice_buffer sb;
468
469 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)
470 }SnowContext;
471
472 typedef struct {
473 DWTELEM *b0;
474 DWTELEM *b1;
475 DWTELEM *b2;
476 DWTELEM *b3;
477 int y;
478 } dwt_compose_t;
479
480 #define slice_buffer_get_line(slice_buf, line_num) ((slice_buf)->line[line_num] ? (slice_buf)->line[line_num] : slice_buffer_load_line((slice_buf), (line_num)))
481 //#define slice_buffer_get_line(slice_buf, line_num) (slice_buffer_load_line((slice_buf), (line_num)))
482
483 static void iterative_me(SnowContext *s);
484
485 static void slice_buffer_init(slice_buffer * buf, int line_count, int max_allocated_lines, int line_width, DWTELEM * base_buffer)
486 {
487 int i;
488
489 buf->base_buffer = base_buffer;
490 buf->line_count = line_count;
491 buf->line_width = line_width;
492 buf->data_count = max_allocated_lines;
493 buf->line = (DWTELEM * *) av_mallocz (sizeof(DWTELEM *) * line_count);
494 buf->data_stack = (DWTELEM * *) av_malloc (sizeof(DWTELEM *) * max_allocated_lines);
495
496 for (i = 0; i < max_allocated_lines; i++)
497 {
498 buf->data_stack[i] = (DWTELEM *) av_malloc (sizeof(DWTELEM) * line_width);
499 }
500
501 buf->data_stack_top = max_allocated_lines - 1;
502 }
503
504 static DWTELEM * slice_buffer_load_line(slice_buffer * buf, int line)
505 {
506 int offset;
507 DWTELEM * buffer;
508
509 // av_log(NULL, AV_LOG_DEBUG, "Cache hit: %d\n", line);
510
511 assert(buf->data_stack_top >= 0);
512 // assert(!buf->line[line]);
513 if (buf->line[line])
514 return buf->line[line];
515
516 offset = buf->line_width * line;
517 buffer = buf->data_stack[buf->data_stack_top];
518 buf->data_stack_top--;
519 buf->line[line] = buffer;
520
521 // av_log(NULL, AV_LOG_DEBUG, "slice_buffer_load_line: line: %d remaining: %d\n", line, buf->data_stack_top + 1);
522
523 return buffer;
524 }
525
526 static void slice_buffer_release(slice_buffer * buf, int line)
527 {
528 int offset;
529 DWTELEM * buffer;
530
531 assert(line >= 0 && line < buf->line_count);
532 assert(buf->line[line]);
533
534 offset = buf->line_width * line;
535 buffer = buf->line[line];
536 buf->data_stack_top++;
537 buf->data_stack[buf->data_stack_top] = buffer;
538 buf->line[line] = NULL;
539
540 // av_log(NULL, AV_LOG_DEBUG, "slice_buffer_release: line: %d remaining: %d\n", line, buf->data_stack_top + 1);
541 }
542
543 static void slice_buffer_flush(slice_buffer * buf)
544 {
545 int i;
546 for (i = 0; i < buf->line_count; i++)
547 {
548 if (buf->line[i])
549 {
550 // av_log(NULL, AV_LOG_DEBUG, "slice_buffer_flush: line: %d \n", i);
551 slice_buffer_release(buf, i);
552 }
553 }
554 }
555
556 static void slice_buffer_destroy(slice_buffer * buf)
557 {
558 int i;
559 slice_buffer_flush(buf);
560
561 for (i = buf->data_count - 1; i >= 0; i--)
562 {
563 assert(buf->data_stack[i]);
564 av_freep(&buf->data_stack[i]);
565 }
566 assert(buf->data_stack);
567 av_freep(&buf->data_stack);
568 assert(buf->line);
569 av_freep(&buf->line);
570 }
571
572 #ifdef __sgi
573 // Avoid a name clash on SGI IRIX
574 #undef qexp
575 #endif
576 #define QEXPSHIFT (7-FRAC_BITS+8) //FIXME try to change this to 0
577 static uint8_t qexp[QROOT];
578
579 static inline int mirror(int v, int m){
580 while((unsigned)v > (unsigned)m){
581 v=-v;
582 if(v<0) v+= 2*m;
583 }
584 return v;
585 }
586
587 static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
588 int i;
589
590 if(v){
591 const int a= ABS(v);
592 const int e= av_log2(a);
593 #if 1
594 const int el= FFMIN(e, 10);
595 put_rac(c, state+0, 0);
596
597 for(i=0; i<el; i++){
598 put_rac(c, state+1+i, 1); //1..10
599 }
600 for(; i<e; i++){
601 put_rac(c, state+1+9, 1); //1..10
602 }
603 put_rac(c, state+1+FFMIN(i,9), 0);
604
605 for(i=e-1; i>=el; i--){
606 put_rac(c, state+22+9, (a>>i)&1); //22..31
607 }
608 for(; i>=0; i--){
609 put_rac(c, state+22+i, (a>>i)&1); //22..31
610 }
611
612 if(is_signed)
613 put_rac(c, state+11 + el, v < 0); //11..21
614 #else
615
616 put_rac(c, state+0, 0);
617 if(e<=9){
618 for(i=0; i<e; i++){
619 put_rac(c, state+1+i, 1); //1..10
620 }
621 put_rac(c, state+1+i, 0);
622
623 for(i=e-1; i>=0; i--){
624 put_rac(c, state+22+i, (a>>i)&1); //22..31
625 }
626
627 if(is_signed)
628 put_rac(c, state+11 + e, v < 0); //11..21
629 }else{
630 for(i=0; i<e; i++){
631 put_rac(c, state+1+FFMIN(i,9), 1); //1..10
632 }
633 put_rac(c, state+1+FFMIN(i,9), 0);
634
635 for(i=e-1; i>=0; i--){
636 put_rac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31
637 }
638
639 if(is_signed)
640 put_rac(c, state+11 + FFMIN(e,10), v < 0); //11..21
641 }
642 #endif
643 }else{
644 put_rac(c, state+0, 1);
645 }
646 }
647
648 static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
649 if(get_rac(c, state+0))
650 return 0;
651 else{
652 int i, e, a;
653 e= 0;
654 while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
655 e++;
656 }
657
658 a= 1;
659 for(i=e-1; i>=0; i--){
660 a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
661 }
662
663 if(is_signed && get_rac(c, state+11 + FFMIN(e,10))) //11..21
664 return -a;
665 else
666 return a;
667 }
668 }
669
670 static inline void put_symbol2(RangeCoder *c, uint8_t *state, int v, int log2){
671 int i;
672 int r= log2>=0 ? 1<<log2 : 1;
673
674 assert(v>=0);
675 assert(log2>=-4);
676
677 while(v >= r){
678 put_rac(c, state+4+log2, 1);
679 v -= r;
680 log2++;
681 if(log2>0) r+=r;
682 }
683 put_rac(c, state+4+log2, 0);
684
685 for(i=log2-1; i>=0; i--){
686 put_rac(c, state+31-i, (v>>i)&1);
687 }
688 }
689
690 static inline int get_symbol2(RangeCoder *c, uint8_t *state, int log2){
691 int i;
692 int r= log2>=0 ? 1<<log2 : 1;
693 int v=0;
694
695 assert(log2>=-4);
696
697 while(get_rac(c, state+4+log2)){
698 v+= r;
699 log2++;
700 if(log2>0) r+=r;
701 }
702
703 for(i=log2-1; i>=0; i--){
704 v+= get_rac(c, state+31-i)<<i;
705 }
706
707 return v;
708 }
709
710 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){
711 const int mirror_left= !highpass;
712 const int mirror_right= (width&1) ^ highpass;
713 const int w= (width>>1) - 1 + (highpass & width);
714 int i;
715
716 #define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
717 if(mirror_left){
718 dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
719 dst += dst_step;
720 src += src_step;
721 }
722
723 for(i=0; i<w; i++){
724 dst[i*dst_step] = LIFT(src[i*src_step], ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift), inverse);
725 }
726
727 if(mirror_right){
728 dst[w*dst_step] = LIFT(src[w*src_step], ((mul*2*ref[w*ref_step]+add)>>shift), inverse);
729 }
730 }
731
732 #ifndef lift5
733 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){
734 const int mirror_left= !highpass;
735 const int mirror_right= (width&1) ^ highpass;
736 const int w= (width>>1) - 1 + (highpass & width);
737 int i;
738
739 if(mirror_left){
740 int r= 3*2*ref[0];
741 r += r>>4;
742 r += r>>8;
743 dst[0] = LIFT(src[0], ((r+add)>>shift), inverse);
744 dst += dst_step;
745 src += src_step;
746 }
747
748 for(i=0; i<w; i++){
749 int r= 3*(ref[i*ref_step] + ref[(i+1)*ref_step]);
750 r += r>>4;
751 r += r>>8;
752 dst[i*dst_step] = LIFT(src[i*src_step], ((r+add)>>shift), inverse);
753 }
754
755 if(mirror_right){
756 int r= 3*2*ref[w*ref_step];
757 r += r>>4;
758 r += r>>8;
759 dst[w*dst_step] = LIFT(src[w*src_step], ((r+add)>>shift), inverse);
760 }
761 }
762 #endif
763
764 #ifndef liftS
765 static always_inline void liftS(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){
766 const int mirror_left= !highpass;
767 const int mirror_right= (width&1) ^ highpass;
768 const int w= (width>>1) - 1 + (highpass & width);
769 int i;
770
771 assert(shift == 4);
772 #define LIFTS(src, ref, inv) ((inv) ? (src) - (((ref) - 4*(src))>>shift): (16*4*(src) + 4*(ref) + 8 + (5<<27))/(5*16) - (1<<23))
773 if(mirror_left){
774 dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse);
775 dst += dst_step;
776 src += src_step;
777 }
778
779 for(i=0; i<w; i++){
780 dst[i*dst_step] = LIFTS(src[i*src_step], mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add, inverse);
781 }
782
783 if(mirror_right){
784 dst[w*dst_step] = LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse);
785 }
786 }
787 #endif
788
789
790 static void inplace_lift(DWTELEM *dst, int width, int *coeffs, int n, int shift, int start, int inverse){
791 int x, i;
792
793 for(x=start; x<width; x+=2){
794 int64_t sum=0;
795
796 for(i=0; i<n; i++){
797 int x2= x + 2*i - n + 1;
798 if (x2< 0) x2= -x2;
799 else if(x2>=width) x2= 2*width-x2-2;
800 sum += coeffs[i]*(int64_t)dst[x2];
801 }
802 if(inverse) dst[x] -= (sum + (1<<shift)/2)>>shift;
803 else dst[x] += (sum + (1<<shift)/2)>>shift;
804 }
805 }
806
807 static void inplace_liftV(DWTELEM *dst, int width, int height, int stride, int *coeffs, int n, int shift, int start, int inverse){
808 int x, y, i;
809 for(y=start; y<height; y+=2){
810 for(x=0; x<width; x++){
811 int64_t sum=0;
812
813 for(i=0; i<n; i++){
814 int y2= y + 2*i - n + 1;
815 if (y2< 0) y2= -y2;
816 else if(y2>=height) y2= 2*height-y2-2;
817 sum += coeffs[i]*(int64_t)dst[x + y2*stride];
818 }
819 if(inverse) dst[x + y*stride] -= (sum + (1<<shift)/2)>>shift;
820 else dst[x + y*stride] += (sum + (1<<shift)/2)>>shift;
821 }
822 }
823 }
824
825 #define SCALEX 1
826 #define LX0 0
827 #define LX1 1
828
829 #if 0 // more accurate 9/7
830 #define N1 2
831 #define SHIFT1 14
832 #define COEFFS1 (int[]){-25987,-25987}
833 #define N2 2
834 #define SHIFT2 19
835 #define COEFFS2 (int[]){-27777,-27777}
836 #define N3 2
837 #define SHIFT3 15
838 #define COEFFS3 (int[]){28931,28931}
839 #define N4 2
840 #define SHIFT4 15
841 #define COEFFS4 (int[]){14533,14533}
842 #elif 1 // 13/7 CRF
843 #define N1 4
844 #define SHIFT1 4
845 #define COEFFS1 (int[]){1,-9,-9,1}
846 #define N2 4
847 #define SHIFT2 4
848 #define COEFFS2 (int[]){-1,5,5,-1}
849 #define N3 0
850 #define SHIFT3 1
851 #define COEFFS3 NULL
852 #define N4 0
853 #define SHIFT4 1
854 #define COEFFS4 NULL
855 #elif 1 // 3/5
856 #define LX0 1
857 #define LX1 0
858 #define SCALEX 0.5
859 #define N1 2
860 #define SHIFT1 1
861 #define COEFFS1 (int[]){1,1}
862 #define N2 2
863 #define SHIFT2 2
864 #define COEFFS2 (int[]){-1,-1}
865 #define N3 0
866 #define SHIFT3 0
867 #define COEFFS3 NULL
868 #define N4 0
869 #define SHIFT4 0
870 #define COEFFS4 NULL
871 #elif 1 // 11/5
872 #define N1 0
873 #define SHIFT1 1
874 #define COEFFS1 NULL
875 #define N2 2
876 #define SHIFT2 2
877 #define COEFFS2 (int[]){-1,-1}
878 #define N3 2
879 #define SHIFT3 0
880 #define COEFFS3 (int[]){-1,-1}
881 #define N4 4
882 #define SHIFT4 7
883 #define COEFFS4 (int[]){-5,29,29,-5}
884 #define SCALEX 4
885 #elif 1 // 9/7 CDF
886 #define N1 2
887 #define SHIFT1 7
888 #define COEFFS1 (int[]){-203,-203}
889 #define N2 2
890 #define SHIFT2 12
891 #define COEFFS2 (int[]){-217,-217}
892 #define N3 2
893 #define SHIFT3 7
894 #define COEFFS3 (int[]){113,113}
895 #define N4 2
896 #define SHIFT4 9
897 #define COEFFS4 (int[]){227,227}
898 #define SCALEX 1
899 #elif 1 // 7/5 CDF
900 #define N1 0
901 #define SHIFT1 1
902 #define COEFFS1 NULL
903 #define N2 2
904 #define SHIFT2 2
905 #define COEFFS2 (int[]){-1,-1}
906 #define N3 2
907 #define SHIFT3 0
908 #define COEFFS3 (int[]){-1,-1}
909 #define N4 2
910 #define SHIFT4 4
911 #define COEFFS4 (int[]){3,3}
912 #elif 1 // 9/7 MN
913 #define N1 4
914 #define SHIFT1 4
915 #define COEFFS1 (int[]){1,-9,-9,1}
916 #define N2 2
917 #define SHIFT2 2
918 #define COEFFS2 (int[]){1,1}
919 #define N3 0
920 #define SHIFT3 1
921 #define COEFFS3 NULL
922 #define N4 0
923 #define SHIFT4 1
924 #define COEFFS4 NULL
925 #else // 13/7 CRF
926 #define N1 4
927 #define SHIFT1 4
928 #define COEFFS1 (int[]){1,-9,-9,1}
929 #define N2 4
930 #define SHIFT2 4
931 #define COEFFS2 (int[]){-1,5,5,-1}
932 #define N3 0
933 #define SHIFT3 1
934 #define COEFFS3 NULL
935 #define N4 0
936 #define SHIFT4 1
937 #define COEFFS4 NULL
938 #endif
939 static void horizontal_decomposeX(DWTELEM *b, int width){
940 DWTELEM temp[width];
941 const int width2= width>>1;
942 const int w2= (width+1)>>1;
943 int x;
944
945 inplace_lift(b, width, COEFFS1, N1, SHIFT1, LX1, 0);
946 inplace_lift(b, width, COEFFS2, N2, SHIFT2, LX0, 0);
947 inplace_lift(b, width, COEFFS3, N3, SHIFT3, LX1, 0);
948 inplace_lift(b, width, COEFFS4, N4, SHIFT4, LX0, 0);
949
950 for(x=0; x<width2; x++){
951 temp[x ]= b[2*x ];
952 temp[x+w2]= b[2*x + 1];
953 }
954 if(width&1)
955 temp[x ]= b[2*x ];
956 memcpy(b, temp, width*sizeof(int));
957 }
958
959 static void horizontal_composeX(DWTELEM *b, int width){
960 DWTELEM temp[width];
961 const int width2= width>>1;
962 int x;
963 const int w2= (width+1)>>1;
964
965 memcpy(temp, b, width*sizeof(int));
966 for(x=0; x<width2; x++){
967 b[2*x ]= temp[x ];
968 b[2*x + 1]= temp[x+w2];
969 }
970 if(width&1)
971 b[2*x ]= temp[x ];
972
973 inplace_lift(b, width, COEFFS4, N4, SHIFT4, LX0, 1);
974 inplace_lift(b, width, COEFFS3, N3, SHIFT3, LX1, 1);
975 inplace_lift(b, width, COEFFS2, N2, SHIFT2, LX0, 1);
976 inplace_lift(b, width, COEFFS1, N1, SHIFT1, LX1, 1);
977 }
978
979 static void spatial_decomposeX(DWTELEM *buffer, int width, int height, int stride){
980 int x, y;
981
982 for(y=0; y<height; y++){
983 for(x=0; x<width; x++){
984 buffer[y*stride + x] *= SCALEX;
985 }
986 }
987
988 for(y=0; y<height; y++){
989 horizontal_decomposeX(buffer + y*stride, width);
990 }
991
992 inplace_liftV(buffer, width, height, stride, COEFFS1, N1, SHIFT1, LX1, 0);
993 inplace_liftV(buffer, width, height, stride, COEFFS2, N2, SHIFT2, LX0, 0);
994 inplace_liftV(buffer, width, height, stride, COEFFS3, N3, SHIFT3, LX1, 0);
995 inplace_liftV(buffer, width, height, stride, COEFFS4, N4, SHIFT4, LX0, 0);
996 }
997
998 static void spatial_composeX(DWTELEM *buffer, int width, int height, int stride){
999 int x, y;
1000
1001 inplace_liftV(buffer, width, height, stride, COEFFS4, N4, SHIFT4, LX0, 1);
1002 inplace_liftV(buffer, width, height, stride, COEFFS3, N3, SHIFT3, LX1, 1);
1003 inplace_liftV(buffer, width, height, stride, COEFFS2, N2, SHIFT2, LX0, 1);
1004 inplace_liftV(buffer, width, height, stride, COEFFS1, N1, SHIFT1, LX1, 1);
1005
1006 for(y=0; y<height; y++){
1007 horizontal_composeX(buffer + y*stride, width);
1008 }
1009
1010 for(y=0; y<height; y++){
1011 for(x=0; x<width; x++){
1012 buffer[y*stride + x] /= SCALEX;
1013 }
1014 }
1015 }
1016
1017 static void horizontal_decompose53i(DWTELEM *b, int width){
1018 DWTELEM temp[width];
1019 const int width2= width>>1;
1020 int x;
1021 const int w2= (width+1)>>1;
1022
1023 for(x=0; x<width2; x++){
1024 temp[x ]= b[2*x ];
1025 temp[x+w2]= b[2*x + 1];
1026 }
1027 if(width&1)
1028 temp[x ]= b[2*x ];
1029 #if 0
1030 {
1031 int A1,A2,A3,A4;
1032 A2= temp[1 ];
1033 A4= temp[0 ];
1034 A1= temp[0+width2];
1035 A1 -= (A2 + A4)>>1;
1036 A4 += (A1 + 1)>>1;
1037 b[0+width2] = A1;
1038 b[0 ] = A4;
1039 for(x=1; x+1<width2; x+=2){
1040 A3= temp[x+width2];
1041 A4= temp[x+1 ];
1042 A3 -= (A2 + A4)>>1;
1043 A2 += (A1 + A3 + 2)>>2;
1044 b[x+width2] = A3;
1045 b[x ] = A2;
1046
1047 A1= temp[x+1+width2];
1048 A2= temp[x+2 ];
1049 A1 -= (A2 + A4)>>1;
1050 A4 += (A1 + A3 + 2)>>2;
1051 b[x+1+width2] = A1;
1052 b[x+1 ] = A4;
1053 }
1054 A3= temp[width-1];
1055 A3 -= A2;
1056 A2 += (A1 + A3 + 2)>>2;
1057 b[width -1] = A3;
1058 b[width2-1] = A2;
1059 }
1060 #else
1061 lift(b+w2, temp+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
1062 lift(b , temp , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 0);
1063 #endif
1064 }
1065
1066 static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1067 int i;
1068
1069 for(i=0; i<width; i++){
1070 b1[i] -= (b0[i] + b2[i])>>1;
1071 }
1072 }
1073
1074 static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1075 int i;
1076
1077 for(i=0; i<width; i++){
1078 b1[i] += (b0[i] + b2[i] + 2)>>2;
1079 }
1080 }
1081
1082 static void spatial_decompose53i(DWTELEM *buffer, int width, int height, int stride){
1083 int y;
1084 DWTELEM *b0= buffer + mirror(-2-1, height-1)*stride;
1085 DWTELEM *b1= buffer + mirror(-2 , height-1)*stride;
1086
1087 for(y=-2; y<height; y+=2){
1088 DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
1089 DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
1090
1091 {START_TIMER
1092 if(y+1<(unsigned)height) horizontal_decompose53i(b2, width);
1093 if(y+2<(unsigned)height) horizontal_decompose53i(b3, width);
1094 STOP_TIMER("horizontal_decompose53i")}
1095
1096 {START_TIMER
1097 if(y+1<(unsigned)height) vertical_decompose53iH0(b1, b2, b3, width);
1098 if(y+0<(unsigned)height) vertical_decompose53iL0(b0, b1, b2, width);
1099 STOP_TIMER("vertical_decompose53i*")}
1100
1101 b0=b2;
1102 b1=b3;
1103 }
1104 }
1105
1106 static void horizontal_decompose97i(DWTELEM *b, int width){
1107 DWTELEM temp[width];
1108 const int w2= (width+1)>>1;
1109
1110 lift (temp+w2, b +1, b , 1, 2, 2, width, -W_AM, W_AO, W_AS, 1, 0);
1111 liftS(temp , b , temp+w2, 1, 2, 1, width, -W_BM, W_BO, W_BS, 0, 0);
1112 lift5(b +w2, temp+w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 0);
1113 lift (b , temp , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 0);
1114 }
1115
1116
1117 static void vertical_decompose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1118 int i;
1119
1120 for(i=0; i<width; i++){
1121 b1[i] -= (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
1122 }
1123 }
1124
1125 static void vertical_decompose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1126 int i;
1127
1128 for(i=0; i<width; i++){
1129 #ifdef lift5
1130 b1[i] += (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
1131 #else
1132 int r= 3*(b0[i] + b2[i]);
1133 r+= r>>4;
1134 r+= r>>8;
1135 b1[i] += (r+W_CO)>>W_CS;
1136 #endif
1137 }
1138 }
1139
1140 static void vertical_decompose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1141 int i;
1142
1143 for(i=0; i<width; i++){
1144 #ifdef liftS
1145 b1[i] -= (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
1146 #else
1147 b1[i] = (16*4*b1[i] - 4*(b0[i] + b2[i]) + 8*5 + (5<<27)) / (5*16) - (1<<23);
1148 #endif
1149 }
1150 }
1151
1152 static void vertical_decompose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1153 int i;
1154
1155 for(i=0; i<width; i++){
1156 b1[i] += (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
1157 }
1158 }
1159
1160 static void spatial_decompose97i(DWTELEM *buffer, int width, int height, int stride){
1161 int y;
1162 DWTELEM *b0= buffer + mirror(-4-1, height-1)*stride;
1163 DWTELEM *b1= buffer + mirror(-4 , height-1)*stride;
1164 DWTELEM *b2= buffer + mirror(-4+1, height-1)*stride;
1165 DWTELEM *b3= buffer + mirror(-4+2, height-1)*stride;
1166
1167 for(y=-4; y<height; y+=2){
1168 DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
1169 DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
1170
1171 {START_TIMER
1172 if(y+3<(unsigned)height) horizontal_decompose97i(b4, width);
1173 if(y+4<(unsigned)height) horizontal_decompose97i(b5, width);
1174 if(width>400){
1175 STOP_TIMER("horizontal_decompose97i")
1176 }}
1177
1178 {START_TIMER
1179 if(y+3<(unsigned)height) vertical_decompose97iH0(b3, b4, b5, width);
1180 if(y+2<(unsigned)height) vertical_decompose97iL0(b2, b3, b4, width);
1181 if(y+1<(unsigned)height) vertical_decompose97iH1(b1, b2, b3, width);
1182 if(y+0<(unsigned)height) vertical_decompose97iL1(b0, b1, b2, width);
1183
1184 if(width>400){
1185 STOP_TIMER("vertical_decompose97i")
1186 }}
1187
1188 b0=b2;
1189 b1=b3;
1190 b2=b4;
1191 b3=b5;
1192 }
1193 }
1194
1195 void ff_spatial_dwt(DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
1196 int level;
1197
1198 for(level=0; level<decomposition_count; level++){
1199 switch(type){
1200 case DWT_97: spatial_decompose97i(buffer, width>>level, height>>level, stride<<level); break;
1201 case DWT_53: spatial_decompose53i(buffer, width>>level, height>>level, stride<<level); break;
1202 case DWT_X: spatial_decomposeX (buffer, width>>level, height>>level, stride<<level); break;
1203 }
1204 }
1205 }
1206
1207 static void horizontal_compose53i(DWTELEM *b, int width){
1208 DWTELEM temp[width];
1209 const int width2= width>>1;
1210 const int w2= (width+1)>>1;
1211 int x;
1212
1213 #if 0
1214 int A1,A2,A3,A4;
1215 A2= temp[1 ];
1216 A4= temp[0 ];
1217 A1= temp[0+width2];
1218 A1 -= (A2 + A4)>>1;
1219 A4 += (A1 + 1)>>1;
1220 b[0+width2] = A1;
1221 b[0 ] = A4;
1222 for(x=1; x+1<width2; x+=2){
1223 A3= temp[x+width2];
1224 A4= temp[x+1 ];
1225 A3 -= (A2 + A4)>>1;
1226 A2 += (A1 + A3 + 2)>>2;
1227 b[x+width2] = A3;
1228 b[x ] = A2;
1229
1230 A1= temp[x+1+width2];
1231 A2= temp[x+2 ];
1232 A1 -= (A2 + A4)>>1;
1233 A4 += (A1 + A3 + 2)>>2;
1234 b[x+1+width2] = A1;
1235 b[x+1 ] = A4;
1236 }
1237 A3= temp[width-1];
1238 A3 -= A2;
1239 A2 += (A1 + A3 + 2)>>2;
1240 b[width -1] = A3;
1241 b[width2-1] = A2;
1242 #else
1243 lift(temp , b , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 1);
1244 lift(temp+w2, b+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 1);
1245 #endif
1246 for(x=0; x<width2; x++){
1247 b[2*x ]= temp[x ];
1248 b[2*x + 1]= temp[x+w2];
1249 }
1250 if(width&1)
1251 b[2*x ]= temp[x ];
1252 }
1253
1254 static void vertical_compose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1255 int i;
1256
1257 for(i=0; i<width; i++){
1258 b1[i] += (b0[i] + b2[i])>>1;
1259 }
1260 }
1261
1262 static void vertical_compose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1263 int i;
1264
1265 for(i=0; i<width; i++){
1266 b1[i] -= (b0[i] + b2[i] + 2)>>2;
1267 }
1268 }
1269
1270 static void spatial_compose53i_buffered_init(dwt_compose_t *cs, slice_buffer * sb, int height, int stride_line){
1271 cs->b0 = slice_buffer_get_line(sb, mirror(-1-1, height-1) * stride_line);
1272 cs->b1 = slice_buffer_get_line(sb, mirror(-1 , height-1) * stride_line);
1273 cs->y = -1;
1274 }
1275
1276 static void spatial_compose53i_init(dwt_compose_t *cs, DWTELEM *buffer, int height, int stride){
1277 cs->b0 = buffer + mirror(-1-1, height-1)*stride;
1278 cs->b1 = buffer + mirror(-1 , height-1)*stride;
1279 cs->y = -1;
1280 }
1281
1282 static void spatial_compose53i_dy_buffered(dwt_compose_t *cs, slice_buffer * sb, int width, int height, int stride_line){
1283 int y= cs->y;
1284
1285 DWTELEM *b0= cs->b0;
1286 DWTELEM *b1= cs->b1;
1287 DWTELEM *b2= slice_buffer_get_line(sb, mirror(y+1, height-1) * stride_line);
1288 DWTELEM *b3= slice_buffer_get_line(sb, mirror(y+2, height-1) * stride_line);
1289
1290 {START_TIMER
1291 if(y+1<(unsigned)height) vertical_compose53iL0(b1, b2, b3, width);
1292 if(y+0<(unsigned)height) vertical_compose53iH0(b0, b1, b2, width);
1293 STOP_TIMER("vertical_compose53i*")}
1294
1295 {START_TIMER
1296 if(y-1<(unsigned)height) horizontal_compose53i(b0, width);
1297 if(y+0<(unsigned)height) horizontal_compose53i(b1, width);
1298 STOP_TIMER("horizontal_compose53i")}
1299
1300 cs->b0 = b2;
1301 cs->b1 = b3;
1302 cs->y += 2;
1303 }
1304
1305 static void spatial_compose53i_dy(dwt_compose_t *cs, DWTELEM *buffer, int width, int height, int stride){
1306 int y= cs->y;
1307 DWTELEM *b0= cs->b0;
1308 DWTELEM *b1= cs->b1;
1309 DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
1310 DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
1311
1312 {START_TIMER
1313 if(y+1<(unsigned)height) vertical_compose53iL0(b1, b2, b3, width);
1314 if(y+0<(unsigned)height) vertical_compose53iH0(b0, b1, b2, width);
1315 STOP_TIMER("vertical_compose53i*")}
1316
1317 {START_TIMER
1318 if(y-1<(unsigned)height) horizontal_compose53i(b0, width);
1319 if(y+0<(unsigned)height) horizontal_compose53i(b1, width);
1320 STOP_TIMER("horizontal_compose53i")}
1321
1322 cs->b0 = b2;
1323 cs->b1 = b3;
1324 cs->y += 2;
1325 }
1326
1327 static void spatial_compose53i(DWTELEM *buffer, int width, int height, int stride){
1328 dwt_compose_t cs;
1329 spatial_compose53i_init(&cs, buffer, height, stride);
1330 while(cs.y <= height)
1331 spatial_compose53i_dy(&cs, buffer, width, height, stride);
1332 }
1333
1334
1335 void ff_snow_horizontal_compose97i(DWTELEM *b, int width){
1336 DWTELEM temp[width];
1337 const int w2= (width+1)>>1;
1338
1339 lift (temp , b , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 1);
1340 lift5(temp+w2, b +w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 1);
1341 liftS(b , temp , temp+w2, 2, 1, 1, width, -W_BM, W_BO, W_BS, 0, 1);
1342 lift (b+1 , temp+w2, b , 2, 1, 2, width, -W_AM, W_AO, W_AS, 1, 1);
1343 }
1344
1345 static void vertical_compose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1346 int i;
1347
1348 for(i=0; i<width; i++){
1349 b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
1350 }
1351 }
1352
1353 static void vertical_compose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1354 int i;
1355
1356 for(i=0; i<width; i++){
1357 #ifdef lift5
1358 b1[i] -= (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
1359 #else
1360 int r= 3*(b0[i] + b2[i]);
1361 r+= r>>4;
1362 r+= r>>8;
1363 b1[i] -= (r+W_CO)>>W_CS;
1364 #endif
1365 }
1366 }
1367
1368 static void vertical_compose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1369 int i;
1370
1371 for(i=0; i<width; i++){
1372 #ifdef liftS
1373 b1[i] += (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
1374 #else
1375 b1[i] += (W_BM*(b0[i] + b2[i])+4*b1[i]+W_BO)>>W_BS;
1376 #endif
1377 }
1378 }
1379
1380 static void vertical_compose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1381 int i;
1382
1383 for(i=0; i<width; i++){
1384 b1[i] -= (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
1385 }
1386 }
1387
1388 void ff_snow_vertical_compose97i(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, DWTELEM *b3, DWTELEM *b4, DWTELEM *b5, int width){
1389 int i;
1390
1391 for(i=0; i<width; i++){
1392 #ifndef lift5
1393 int r;
1394 #endif
1395 b4[i] -= (W_DM*(b3[i] + b5[i])+W_DO)>>W_DS;
1396 #ifdef lift5
1397 b3[i] -= (W_CM*(b2[i] + b4[i])+W_CO)>>W_CS;
1398 #else
1399 r= 3*(b2[i] + b4[i]);
1400 r+= r>>4;
1401 r+= r>>8;
1402 b3[i] -= (r+W_CO)>>W_CS;
1403 #endif
1404 #ifdef liftS
1405 b2[i] += (W_BM*(b1[i] + b3[i])+W_BO)>>W_BS;
1406 #else
1407 b2[i] += (W_BM*(b1[i] + b3[i])+4*b2[i]+W_BO)>>W_BS;
1408 #endif
1409 b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
1410 }
1411 }
1412
1413 static void spatial_compose97i_buffered_init(dwt_compose_t *cs, slice_buffer * sb, int height, int stride_line){
1414 cs->b0 = slice_buffer_get_line(sb, mirror(-3-1, height-1) * stride_line);
1415 cs->b1 = slice_buffer_get_line(sb, mirror(-3 , height-1) * stride_line);
1416 cs->b2 = slice_buffer_get_line(sb, mirror(-3+1, height-1) * stride_line);
1417 cs->b3 = slice_buffer_get_line(sb, mirror(-3+2, height-1) * stride_line);
1418 cs->y = -3;
1419 }
1420
1421 static void spatial_compose97i_init(dwt_compose_t *cs, DWTELEM *buffer, int height, int stride){
1422 cs->b0 = buffer + mirror(-3-1, height-1)*stride;
1423 cs->b1 = buffer + mirror(-3 , height-1)*stride;
1424 cs->b2 = buffer + mirror(-3+1, height-1)*stride;
1425 cs->b3 = buffer + mirror(-3+2, height-1)*stride;
1426 cs->y = -3;
1427 }
1428
1429 static void spatial_compose97i_dy_buffered(DSPContext *dsp, dwt_compose_t *cs, slice_buffer * sb, int width, int height, int stride_line){
1430 int y = cs->y;
1431
1432 DWTELEM *b0= cs->b0;
1433 DWTELEM *b1= cs->b1;
1434 DWTELEM *b2= cs->b2;
1435 DWTELEM *b3= cs->b3;
1436 DWTELEM *b4= slice_buffer_get_line(sb, mirror(y + 3, height - 1) * stride_line);
1437 DWTELEM *b5= slice_buffer_get_line(sb, mirror(y + 4, height - 1) * stride_line);
1438
1439 {START_TIMER
1440 if(y>0 && y+4<height){
1441 dsp->vertical_compose97i(b0, b1, b2, b3, b4, b5, width);
1442 }else{
1443 if(y+3<(unsigned)height) vertical_compose97iL1(b3, b4, b5, width);
1444 if(y+2<(unsigned)height) vertical_compose97iH1(b2, b3, b4, width);
1445 if(y+1<(unsigned)height) vertical_compose97iL0(b1, b2, b3, width);
1446 if(y+0<(unsigned)height) vertical_compose97iH0(b0, b1, b2, width);
1447 }
1448 if(width>400){
1449 STOP_TIMER("vertical_compose97i")}}
1450
1451 {START_TIMER
1452 if(y-1<(unsigned)height) dsp->horizontal_compose97i(b0, width);
1453 if(y+0<(unsigned)height) dsp->horizontal_compose97i(b1, width);
1454 if(width>400 && y+0<(unsigned)height){
1455 STOP_TIMER("horizontal_compose97i")}}
1456
1457 cs->b0=b2;
1458 cs->b1=b3;
1459 cs->b2=b4;
1460 cs->b3=b5;
1461 cs->y += 2;
1462 }
1463
1464 static void spatial_compose97i_dy(dwt_compose_t *cs, DWTELEM *buffer, int width, int height, int stride){
1465 int y = cs->y;
1466 DWTELEM *b0= cs->b0;
1467 DWTELEM *b1= cs->b1;
1468 DWTELEM *b2= cs->b2;
1469 DWTELEM *b3= cs->b3;
1470 DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
1471 DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
1472
1473 {START_TIMER
1474 if(y+3<(unsigned)height) vertical_compose97iL1(b3, b4, b5, width);
1475 if(y+2<(unsigned)height) vertical_compose97iH1(b2, b3, b4, width);
1476 if(y+1<(unsigned)height) vertical_compose97iL0(b1, b2, b3, width);
1477 if(y+0<(unsigned)height) vertical_compose97iH0(b0, b1, b2, width);
1478 if(width>400){
1479 STOP_TIMER("vertical_compose97i")}}
1480
1481 {START_TIMER
1482 if(y-1<(unsigned)height) ff_snow_horizontal_compose97i(b0, width);
1483 if(y+0<(unsigned)height) ff_snow_horizontal_compose97i(b1, width);
1484 if(width>400 && b0 <= b2){
1485 STOP_TIMER("horizontal_compose97i")}}
1486
1487 cs->b0=b2;
1488 cs->b1=b3;
1489 cs->b2=b4;
1490 cs->b3=b5;
1491 cs->y += 2;
1492 }
1493
1494 static void spatial_compose97i(DWTELEM *buffer, int width, int height, int stride){
1495 dwt_compose_t cs;
1496 spatial_compose97i_init(&cs, buffer, height, stride);
1497 while(cs.y <= height)
1498 spatial_compose97i_dy(&cs, buffer, width, height, stride);
1499 }
1500
1501 static void ff_spatial_idwt_buffered_init(dwt_compose_t *cs, slice_buffer * sb, int width, int height, int stride_line, int type, int decomposition_count){
1502 int level;
1503 for(level=decomposition_count-1; level>=0; level--){
1504 switch(type){
1505 case DWT_97: spatial_compose97i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
1506 case DWT_53: spatial_compose53i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
1507 /* not slicified yet */
1508 case DWT_X: /*spatial_composeX(buffer, width>>level, height>>level, stride<<level); break;*/
1509 av_log(NULL, AV_LOG_ERROR, "spatial_composeX neither buffered nor slicified yet.\n"); break;
1510 }
1511 }
1512 }
1513
1514 static void ff_spatial_idwt_init(dwt_compose_t *cs, DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
1515 int level;
1516 for(level=decomposition_count-1; level>=0; level--){
1517 switch(type){
1518 case DWT_97: spatial_compose97i_init(cs+level, buffer, height>>level, stride<<level); break;
1519 case DWT_53: spatial_compose53i_init(cs+level, buffer, height>>level, stride<<level); break;
1520 /* not slicified yet */
1521 case DWT_X: spatial_composeX(buffer, width>>level, height>>level, stride<<level); break;
1522 }
1523 }
1524 }
1525
1526 static void ff_spatial_idwt_slice(dwt_compose_t *cs, DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count, int y){
1527 const int support = type==1 ? 3 : 5;
1528 int level;
1529 if(type==2) return;
1530
1531 for(level=decomposition_count-1; level>=0; level--){
1532 while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
1533 switch(type){
1534 case DWT_97: spatial_compose97i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
1535 break;
1536 case DWT_53: spatial_compose53i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
1537 break;
1538 case DWT_X: break;
1539 }
1540 }
1541 }
1542 }
1543
1544 static void ff_spatial_idwt_buffered_slice(DSPContext *dsp, dwt_compose_t *cs, slice_buffer * slice_buf, int width, int height, int stride_line, int type, int decomposition_count, int y){
1545 const int support = type==1 ? 3 : 5;
1546 int level;
1547 if(type==2) return;
1548
1549 for(level=decomposition_count-1; level>=0; level--){
1550 while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
1551 switch(type){
1552 case DWT_97: spatial_compose97i_dy_buffered(dsp, cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
1553 break;
1554 case DWT_53: spatial_compose53i_dy_buffered(cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
1555 break;
1556 case DWT_X: break;
1557 }
1558 }
1559 }
1560 }
1561
1562 static void ff_spatial_idwt(DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
1563 if(type==2){
1564 int level;
1565 for(level=decomposition_count-1; level>=0; level--)
1566 spatial_composeX (buffer, width>>level, height>>level, stride<<level);
1567 }else{
1568 dwt_compose_t cs[MAX_DECOMPOSITIONS];
1569 int y;
1570 ff_spatial_idwt_init(cs, buffer, width, height, stride, type, decomposition_count);
1571 for(y=0; y<height; y+=4)
1572 ff_spatial_idwt_slice(cs, buffer, width, height, stride, type, decomposition_count, y);
1573 }
1574 }
1575
1576 static int encode_subband_c0run(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
1577 const int w= b->width;
1578 const int h= b->height;
1579 int x, y;
1580
1581 if(1){
1582 int run=0;
1583 int runs[w*h];
1584 int run_index=0;
1585 int max_index;
1586
1587 for(y=0; y<h; y++){
1588 for(x=0; x<w; x++){
1589 int v, p=0;
1590 int /*ll=0, */l=0, lt=0, t=0, rt=0;
1591 v= src[x + y*stride];
1592
1593 if(y){
1594 t= src[x + (y-1)*stride];
1595 if(x){
1596 lt= src[x - 1 + (y-1)*stride];
1597 }
1598 if(x + 1 < w){
1599 rt= src[x + 1 + (y-1)*stride];
1600 }
1601 }
1602 if(x){
1603 l= src[x - 1 + y*stride];
1604 /*if(x > 1){
1605 if(orientation==1) ll= src[y + (x-2)*stride];
1606 else ll= src[x - 2 + y*stride];
1607 }*/
1608 }
1609 if(parent){
1610 int px= x>>1;
1611 int py= y>>1;
1612 if(px<b->parent->width && py<b->parent->height)
1613 p= parent[px + py*2*stride];
1614 }
1615 if(!(/*ll|*/l|lt|t|rt|p)){
1616 if(v){
1617 runs[run_index++]= run;
1618 run=0;
1619 }else{
1620 run++;
1621 }
1622 }
1623 }
1624 }
1625 max_index= run_index;
1626 runs[run_index++]= run;
1627 run_index=0;
1628 run= runs[run_index++];
1629
1630 put_symbol2(&s->c, b->state[30], max_index, 0);
1631 if(run_index <= max_index)
1632 put_symbol2(&s->c, b->state[1], run, 3);
1633
1634 for(y=0; y<h; y++){
1635 if(s->c.bytestream_end - s->c.bytestream < w*40){
1636 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
1637 return -1;
1638 }
1639 for(x=0; x<w; x++){
1640 int v, p=0;
1641 int /*ll=0, */l=0, lt=0, t=0, rt=0;
1642 v= src[x + y*stride];
1643
1644 if(y){
1645 t= src[x + (y-1)*stride];
1646 if(x){
1647 lt= src[x - 1 + (y-1)*stride];
1648 }
1649 if(x + 1 < w){
1650 rt= src[x + 1 + (y-1)*stride];
1651 }
1652 }
1653 if(x){
1654 l= src[x - 1 + y*stride];
1655 /*if(x > 1){
1656 if(orientation==1) ll= src[y + (x-2)*stride];
1657 else ll= src[x - 2 + y*stride];
1658 }*/
1659 }
1660 if(parent){
1661 int px= x>>1;
1662 int py= y>>1;
1663 if(px<b->parent->width && py<b->parent->height)
1664 p= parent[px + py*2*stride];
1665 }
1666 if(/*ll|*/l|lt|t|rt|p){
1667 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
1668
1669 put_rac(&s->c, &b->state[0][context], !!v);
1670 }else{
1671 if(!run){
1672 run= runs[run_index++];
1673
1674 if(run_index <= max_index)
1675 put_symbol2(&s->c, b->state[1], run, 3);
1676 assert(v);
1677 }else{
1678 run--;
1679 assert(!v);
1680 }
1681 }
1682 if(v){
1683 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
1684 int l2= 2*ABS(l) + (l<0);
1685 int t2= 2*ABS(t) + (t<0);
1686
1687 put_symbol2(&s->c, b->state[context + 2], ABS(v)-1, context-4);
1688 put_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3bA[l2&0xFF] + 3*quant3bA[t2&0xFF]], v<0);
1689 }
1690 }
1691 }
1692 }
1693 return 0;
1694 }
1695
1696 static int encode_subband(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
1697 // encode_subband_qtree(s, b, src, parent, stride, orientation);
1698 // encode_subband_z0run(s, b, src, parent, stride, orientation);
1699 return encode_subband_c0run(s, b, src, parent, stride, orientation);
1700 // encode_subband_dzr(s, b, src, parent, stride, orientation);
1701 }
1702
1703 static inline void unpack_coeffs(SnowContext *s, SubBand *b, SubBand * parent, int orientation){
1704 const int w= b->width;
1705 const int h= b->height;
1706 int x,y;
1707
1708 if(1){
1709 int run, runs;
1710 x_and_coeff *xc= b->x_coeff;
1711 x_and_coeff *prev_xc= NULL;
1712 x_and_coeff *prev2_xc= xc;
1713 x_and_coeff *parent_xc= parent ? parent->x_coeff : NULL;
1714 x_and_coeff *prev_parent_xc= parent_xc;
1715
1716 runs= get_symbol2(&s->c, b->state[30], 0);
1717 if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
1718 else run= INT_MAX;
1719
1720 for(y=0; y<h; y++){
1721 int v=0;
1722 int lt=0, t=0, rt=0;
1723
1724 if(y && prev_xc->x == 0){
1725 rt= prev_xc->coeff;
1726 }
1727 for(x=0; x<w; x++){
1728 int p=0;
1729 const int l= v;
1730
1731 lt= t; t= rt;
1732
1733 if(y){
1734 if(prev_xc->x <= x)
1735 prev_xc++;
1736 if(prev_xc->x == x + 1)
1737 rt= prev_xc->coeff;
1738 else
1739 rt=0;
1740 }
1741 if(parent_xc){
1742 if(x>>1 > parent_xc->x){
1743 parent_xc++;
1744 }
1745 if(x>>1 == parent_xc->x){
1746 p= parent_xc->coeff;
1747 }
1748 }
1749 if(/*ll|*/l|lt|t|rt|p){
1750 int context= av_log2(/*ABS(ll) + */3*(l>>1) + (lt>>1) + (t&~1) + (rt>>1) + (p>>1));
1751
1752 v=get_rac(&s->c, &b->state[0][context]);
1753 if(v){
1754 v= 2*(get_symbol2(&s->c, b->state[context + 2], context-4) + 1);
1755 v+=get_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3bA[l&0xFF] + 3*quant3bA[t&0xFF]]);
1756
1757 xc->x=x;
1758 (xc++)->coeff= v;
1759 }
1760 }else{
1761 if(!run){
1762 if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
1763 else run= INT_MAX;
1764 v= 2*(get_symbol2(&s->c, b->state[0 + 2], 0-4) + 1);
1765 v+=get_rac(&s->c, &b->state[0][16 + 1 + 3]);
1766
1767 xc->x=x;
1768 (xc++)->coeff= v;
1769 }else{
1770 int max_run;
1771 run--;
1772 v=0;
1773
1774 if(y) max_run= FFMIN(run, prev_xc->x - x - 2);
1775 else max_run= FFMIN(run, w-x-1);
1776 if(parent_xc)
1777 max_run= FFMIN(max_run, 2*parent_xc->x - x - 1);
1778 x+= max_run;
1779 run-= max_run;
1780 }
1781 }
1782 }
1783 (xc++)->x= w+1; //end marker
1784 prev_xc= prev2_xc;
1785 prev2_xc= xc;
1786
1787 if(parent_xc){
1788 if(y&1){
1789 while(parent_xc->x != parent->width+1)
1790 parent_xc++;
1791 parent_xc++;
1792 prev_parent_xc= parent_xc;
1793 }else{
1794 parent_xc= prev_parent_xc;
1795 }
1796 }
1797 }
1798
1799 (xc++)->x= w+1; //end marker
1800 }
1801 }
1802
1803 static inline void decode_subband_slice_buffered(SnowContext *s, SubBand *b, slice_buffer * sb, int start_y, int h, int save_state[1]){
1804 const int w= b->width;
1805 int y;
1806 const int qlog= clip(s->qlog + b->qlog, 0, QROOT*16);
1807 int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
1808 int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
1809 int new_index = 0;
1810
1811 START_TIMER
1812
1813 if(b->buf == s->spatial_dwt_buffer || s->qlog == LOSSLESS_QLOG){
1814 qadd= 0;
1815 qmul= 1<<QEXPSHIFT;
1816 }
1817
1818 /* If we are on the second or later slice, restore our index. */
1819 if (start_y != 0)
1820 new_index = save_state[0];
1821
1822
1823 for(y=start_y; y<h; y++){
1824 int x = 0;
1825 int v;
1826 DWTELEM * line = slice_buffer_get_line(sb, y * b->stride_line + b->buf_y_offset) + b->buf_x_offset;
1827 memset(line, 0, b->width*sizeof(DWTELEM));
1828 v = b->x_coeff[new_index].coeff;
1829 x = b->x_coeff[new_index++].x;
1830 while(x < w)
1831 {
1832 register int t= ( (v>>1)*qmul + qadd)>>QEXPSHIFT;
1833 register int u= -(v&1);
1834 line[x] = (t^u) - u;
1835
1836 v = b->x_coeff[new_index].coeff;
1837 x = b->x_coeff[new_index++].x;
1838 }
1839 }
1840 if(w > 200 && start_y != 0/*level+1 == s->spatial_decomposition_count*/){
1841 STOP_TIMER("decode_subband")
1842 }
1843
1844 /* Save our variables for the next slice. */
1845 save_state[0] = new_index;
1846
1847 return;
1848 }
1849
1850 static void reset_contexts(SnowContext *s){
1851 int plane_index, level, orientation;
1852
1853 for(plane_index=0; plane_index<3; plane_index++){
1854 for(level=0; level<s->spatial_decomposition_count; level++){
1855 for(orientation=level ? 1:0; orientation<4; orientation++){
1856 memset(s->plane[plane_index].band[level][orientation].state, MID_STATE, sizeof(s->plane[plane_index].band[level][orientation].state));
1857 }
1858 }
1859 }
1860 memset(s->header_state, MID_STATE, sizeof(s->header_state));
1861 memset(s->block_state, MID_STATE, sizeof(s->block_state));
1862 }
1863
1864 static int alloc_blocks(SnowContext *s){
1865 int w= -((-s->avctx->width )>>LOG2_MB_SIZE);
1866 int h= -((-s->avctx->height)>>LOG2_MB_SIZE);
1867
1868 s->b_width = w;
1869 s->b_height= h;
1870
1871 s->block= av_mallocz(w * h * sizeof(BlockNode) << (s->block_max_depth*2));
1872 return 0;
1873 }
1874
1875 static inline void copy_rac_state(RangeCoder *d, RangeCoder *s){
1876 uint8_t *bytestream= d->bytestream;
1877 uint8_t *bytestream_start= d->bytestream_start;
1878 *d= *s;
1879 d->bytestream= bytestream;
1880 d->bytestream_start= bytestream_start;
1881 }
1882
1883 //near copy & paste from dsputil, FIXME
1884 static int pix_sum(uint8_t * pix, int line_size, int w)
1885 {
1886 int s, i, j;
1887
1888 s = 0;
1889 for (i = 0; i < w; i++) {
1890 for (j = 0; j < w; j++) {
1891 s += pix[0];
1892 pix ++;
1893 }
1894 pix += line_size - w;
1895 }
1896 return s;
1897 }
1898
1899 //near copy & paste from dsputil, FIXME
1900 static int pix_norm1(uint8_t * pix, int line_size, int w)
1901 {
1902 int s, i, j;
1903 uint32_t *sq = squareTbl + 256;
1904
1905 s = 0;
1906 for (i = 0; i < w; i++) {
1907 for (j = 0; j < w; j ++) {
1908 s += sq[pix[0]];
1909 pix ++;
1910 }
1911 pix += line_size - w;
1912 }
1913 return s;
1914 }
1915
1916 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){
1917 const int w= s->b_width << s->block_max_depth;
1918 const int rem_depth= s->block_max_depth - level;
1919 const int index= (x + y*w) << rem_depth;
1920 const int block_w= 1<<rem_depth;
1921 BlockNode block;
1922 int i,j;
1923
1924 block.color[0]= l;
1925 block.color[1]= cb;
1926 block.color[2]= cr;
1927 block.mx= mx;
1928 block.my= my;
1929 block.ref= ref;
1930 block.type= type;
1931 block.level= level;
1932
1933 for(j=0; j<block_w; j++){
1934 for(i=0; i<block_w; i++){
1935 s->block[index + i + j*w]= block;
1936 }
1937 }
1938 }
1939
1940 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){
1941 const int offset[3]= {
1942 y*c-> stride + x,
1943 ((y*c->uvstride + x)>>1),
1944 ((y*c->uvstride + x)>>1),
1945 };
1946 int i;
1947 for(i=0; i<3; i++){
1948 c->src[0][i]= src [i];
1949 c->ref[0][i]= ref [i] + offset[i];
1950 }
1951 assert(!ref_index);
1952 }
1953
1954 static inline void pred_mv(SnowContext *s, int *mx, int *my, int ref,
1955 BlockNode *left, BlockNode *top, BlockNode *tr){
1956 if(s->ref_frames == 1){
1957 *mx = mid_pred(left->mx, top->mx, tr->mx);
1958 *my = mid_pred(left->my, top->my, tr->my);
1959 }else{
1960 const int *scale = scale_mv_ref[ref];
1961 *mx = mid_pred(left->mx * scale[left->ref] + 128 >>8,
1962 top ->mx * scale[top ->ref] + 128 >>8,
1963 tr ->mx * scale[tr ->ref] + 128 >>8);
1964 *my = mid_pred(left->my * scale[left->ref] + 128 >>8,
1965 top ->my * scale[top ->ref] + 128 >>8,
1966 tr ->my * scale[tr ->ref] + 128 >>8);
1967 }
1968 }
1969
1970 //FIXME copy&paste
1971 #define P_LEFT P[1]
1972 #define P_TOP P[2]
1973 #define P_TOPRIGHT P[3]
1974 #define P_MEDIAN P[4]
1975 #define P_MV1 P[9]
1976 #define FLAG_QPEL 1 //must be 1
1977
1978 static int encode_q_branch(SnowContext *s, int level, int x, int y){
1979 uint8_t p_buffer[1024];
1980 uint8_t i_buffer[1024];
1981 uint8_t p_state[sizeof(s->block_state)];
1982 uint8_t i_state[sizeof(s->block_state)];
1983 RangeCoder pc, ic;
1984 uint8_t *pbbak= s->c.bytestream;
1985 uint8_t *pbbak_start= s->c.bytestream_start;
1986 int score, score2, iscore, i_len, p_len, block_s, sum;
1987 const int w= s->b_width << s->block_max_depth;
1988 const int h= s->b_height << s->block_max_depth;
1989 const int rem_depth= s->block_max_depth - level;
1990 const int index= (x + y*w) << rem_depth;
1991 const int block_w= 1<<(LOG2_MB_SIZE - level);
1992 int trx= (x+1)<<rem_depth;
1993 int try= (y+1)<<rem_depth;
1994 BlockNode *left = x ? &s->block[index-1] : &null_block;
1995 BlockNode *top = y ? &s->block[index-w] : &null_block;
1996 BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
1997 BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
1998 BlockNode *tl = y && x ? &s->block[index-w-1] : left;
1999 BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
2000 int pl = left->color[0];
2001 int pcb= left->color[1];
2002 int pcr= left->color[2];
2003 int pmx, pmy;
2004 int mx=0, my=0;
2005 int l,cr,cb;
2006 const int stride= s->current_picture.linesize[0];
2007 const int uvstride= s->current_picture.linesize[1];
2008 uint8_t *current_data[3]= { s->input_picture.data[0] + (x + y* stride)*block_w,
2009 s->input_picture.data[1] + (x + y*uvstride)*block_w/2,
2010 s->input_picture.data[2] + (x + y*uvstride)*block_w/2};
2011 int P[10][2];
2012 int16_t last_mv[3][2];
2013 int qpel= !!(s->avctx->flags & CODEC_FLAG_QPEL); //unused
2014 const int shift= 1+qpel;
2015 MotionEstContext *c= &s->m.me;
2016 int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
2017 int mx_context= av_log2(2*ABS(left->mx - top->mx));
2018 int my_context= av_log2(2*ABS(left->my - top->my));
2019 int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
2020 int ref, best_ref, ref_score, ref_mx, ref_my;
2021
2022 assert(sizeof(s->block_state) >= 256);
2023 if(s->keyframe){
2024 set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
2025 return 0;
2026 }
2027
2028 // clip predictors / edge ?
2029
2030 P_LEFT[0]= left->mx;
2031 P_LEFT[1]= left->my;
2032 P_TOP [0]= top->mx;
2033 P_TOP [1]= top->my;
2034 P_TOPRIGHT[0]= tr->mx;
2035 P_TOPRIGHT[1]= tr->my;
2036
2037 last_mv[0][0]= s->block[index].mx;
2038 last_mv[0][1]= s->block[index].my;
2039 last_mv[1][0]= right->mx;
2040 last_mv[1][1]= right->my;
2041 last_mv[2][0]= bottom->mx;
2042 last_mv[2][1]= bottom->my;
2043
2044 s->m.mb_stride=2;
2045 s->m.mb_x=
2046 s->m.mb_y= 0;
2047 s->m.me.skip= 0;
2048
2049 assert(s->m.me. stride == stride);
2050 assert(s->m.me.uvstride == uvstride);
2051
2052 c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
2053 c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
2054 c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
2055 c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_MV;
2056
2057 c->xmin = - x*block_w - 16+2;
2058 c->ymin = - y*block_w - 16+2;
2059 c->xmax = - (x+