2 * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
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.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "rangecoder.h"
26 #include "mpegvideo.h"
31 #define MAX_DECOMPOSITIONS 8
35 #define QROOT (1<<QSHIFT)
36 #define LOSSLESS_QLOG -128
39 static const int8_t quant3
[256]={
40 0, 0, 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, 1, 1, 1, 1,
46 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
47 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
48 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
49 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
50 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
51 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-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0,
189 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,
190 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,
191 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,
192 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,
193 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,
194 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,
195 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,
196 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,
197 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,
198 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,
199 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,
200 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,
201 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,
202 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,
203 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,
204 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,
205 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,
206 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,
207 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,
208 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,
209 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,
210 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,
211 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,
212 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,
213 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,
214 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,
215 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,
216 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,
217 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,
218 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,
219 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,
222 static const uint8_t obmc16
[256]={
223 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
224 0, 1, 1, 2, 4, 5, 5, 6, 6, 5, 5, 4, 2, 1, 1, 0,
225 0, 1, 4, 6, 9,11,13,15,15,13,11, 9, 6, 4, 1, 0,
226 0, 2, 6,11,15,20,24,26,26,24,20,15,11, 6, 2, 0,
227 0, 4, 9,15,23,29,34,38,38,34,29,23,15, 9, 4, 0,
228 0, 5,11,20,29,38,45,49,49,45,38,29,20,11, 5, 0,
229 1, 5,13,24,34,45,53,57,57,53,45,34,24,13, 5, 1,
230 1, 6,15,26,38,49,57,62,62,57,49,38,26,15, 6, 1,
231 1, 6,15,26,38,49,57,62,62,57,49,38,26,15, 6, 1,
232 1, 5,13,24,34,45,53,57,57,53,45,34,24,13, 5, 1,
233 0, 5,11,20,29,38,45,49,49,45,38,29,20,11, 5, 0,
234 0, 4, 9,15,23,29,34,38,38,34,29,23,15, 9, 4, 0,
235 0, 2, 6,11,15,20,24,26,26,24,20,15,11, 6, 2, 0,
236 0, 1, 4, 6, 9,11,13,15,15,13,11, 9, 6, 4, 1, 0,
237 0, 1, 1, 2, 4, 5, 5, 6, 6, 5, 5, 4, 2, 1, 1, 0,
238 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
242 static const uint8_t obmc32
[1024]={
243 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
244 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 5, 5, 5, 4, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0,
245 0, 1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 8, 9,10,10, 9, 8, 8, 7, 7, 6, 5, 5, 4, 3, 3, 2, 2, 1, 0,
246 0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9,10,11,12,13,14,14,13,12,11,10, 9, 8, 7, 7, 6, 5, 4, 3, 2, 1, 0,
247 1, 2, 3, 4, 5, 6, 7, 8,10,11,12,13,14,15,16,17,17,16,15,14,13,12,11,10, 8, 7, 6, 5, 4, 3, 2, 1,
248 1, 2, 3, 5, 6, 8, 9,10,12,13,14,16,17,19,20,21,21,20,19,17,16,14,13,12,10, 9, 8, 6, 5, 3, 2, 1,
249 1, 2, 4, 6, 7, 9,11,12,14,15,17,19,20,22,24,25,25,24,22,20,19,17,15,14,12,11, 9, 7, 6, 4, 2, 1,
250 1, 3, 5, 7, 8,10,12,14,16,18,20,22,23,25,27,29,29,27,25,23,22,20,18,16,14,12,10, 8, 7, 5, 3, 1,
251 1, 3, 5, 7,10,12,14,16,18,20,22,24,27,29,31,33,33,31,29,27,24,22,20,18,16,14,12,10, 7, 5, 3, 1,
252 1, 4, 6, 8,11,13,15,18,20,23,25,27,30,32,34,37,37,34,32,30,27,25,23,20,18,15,13,11, 8, 6, 4, 1,
253 1, 4, 7, 9,12,14,17,20,22,25,28,30,33,35,38,41,41,38,35,33,30,28,25,22,20,17,14,12, 9, 7, 4, 1,
254 1, 4, 7,10,13,16,19,22,24,27,30,33,36,39,42,45,45,42,39,36,33,30,27,24,22,19,16,13,10, 7, 4, 1,
255 2, 5, 8,11,14,17,20,23,27,30,33,36,39,42,45,48,48,45,42,39,36,33,30,27,23,20,17,14,11, 8, 5, 2,
256 2, 5, 8,12,15,19,22,25,29,32,35,39,42,46,49,52,52,49,46,42,39,35,32,29,25,22,19,15,12, 8, 5, 2,
257 2, 5, 9,13,16,20,24,27,31,34,38,42,45,49,53,56,56,53,49,45,42,38,34,31,27,24,20,16,13, 9, 5, 2,
258 2, 6,10,14,17,21,25,29,33,37,41,45,48,52,56,60,60,56,52,48,45,41,37,33,29,25,21,17,14,10, 6, 2,
259 2, 6,10,14,17,21,25,29,33,37,41,45,48,52,56,60,60,56,52,48,45,41,37,33,29,25,21,17,14,10, 6, 2,
260 2, 5, 9,13,16,20,24,27,31,34,38,42,45,49,53,56,56,53,49,45,42,38,34,31,27,24,20,16,13, 9, 5, 2,
261 2, 5, 8,12,15,19,22,25,29,32,35,39,42,46,49,52,52,49,46,42,39,35,32,29,25,22,19,15,12, 8, 5, 2,
262 2, 5, 8,11,14,17,20,23,27,30,33,36,39,42,45,48,48,45,42,39,36,33,30,27,23,20,17,14,11, 8, 5, 2,
263 1, 4, 7,10,13,16,19,22,24,27,30,33,36,39,42,45,45,42,39,36,33,30,27,24,22,19,16,13,10, 7, 4, 1,
264 1, 4, 7, 9,12,14,17,20,22,25,28,30,33,35,38,41,41,38,35,33,30,28,25,22,20,17,14,12, 9, 7, 4, 1,
265 1, 4, 6, 8,11,13,15,18,20,23,25,27,30,32,34,37,37,34,32,30,27,25,23,20,18,15,13,11, 8, 6, 4, 1,
266 1, 3, 5, 7,10,12,14,16,18,20,22,24,27,29,31,33,33,31,29,27,24,22,20,18,16,14,12,10, 7, 5, 3, 1,
267 1, 3, 5, 7, 8,10,12,14,16,18,20,22,23,25,27,29,29,27,25,23,22,20,18,16,14,12,10, 8, 7, 5, 3, 1,
268 1, 2, 4, 6, 7, 9,11,12,14,15,17,19,20,22,24,25,25,24,22,20,19,17,15,14,12,11, 9, 7, 6, 4, 2, 1,
269 1, 2, 3, 5, 6, 8, 9,10,12,13,14,16,17,19,20,21,21,20,19,17,16,14,13,12,10, 9, 8, 6, 5, 3, 2, 1,
270 1, 2, 3, 4, 5, 6, 7, 8,10,11,12,13,14,15,16,17,17,16,15,14,13,12,11,10, 8, 7, 6, 5, 4, 3, 2, 1,
271 0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9,10,11,12,13,14,14,13,12,11,10, 9, 8, 7, 7, 6, 5, 4, 3, 2, 1, 0,
272 0, 1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 8, 9,10,10, 9, 8, 8, 7, 7, 6, 5, 5, 4, 3, 3, 2, 2, 1, 0,
273 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 5, 5, 5, 4, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0,
274 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
277 static const uint8_t obmc16
[256]={
278 0, 1, 1, 2, 2, 3, 3, 4, 4, 3, 3, 2, 2, 1, 1, 0,
279 1, 2, 4, 5, 7, 8,10,11,11,10, 8, 7, 5, 4, 2, 1,
280 1, 4, 6, 9,11,14,16,19,19,16,14,11, 9, 6, 4, 1,
281 2, 5, 9,12,16,19,23,26,26,23,19,16,12, 9, 5, 2,
282 2, 7,11,16,20,25,29,34,34,29,25,20,16,11, 7, 2,
283 3, 8,14,19,25,30,36,41,41,36,30,25,19,14, 8, 3,
284 3,10,16,23,29,36,42,49,49,42,36,29,23,16,10, 3,
285 4,11,19,26,34,41,49,56,56,49,41,34,26,19,11, 4,
286 4,11,19,26,34,41,49,56,56,49,41,34,26,19,11, 4,
287 3,10,16,23,29,36,42,49,49,42,36,29,23,16,10, 3,
288 3, 8,14,19,25,30,36,41,41,36,30,25,19,14, 8, 3,
289 2, 7,11,16,20,25,29,34,34,29,25,20,16,11, 7, 2,
290 2, 5, 9,12,16,19,23,26,26,23,19,16,12, 9, 5, 2,
291 1, 4, 6, 9,11,14,16,19,19,16,14,11, 9, 6, 4, 1,
292 1, 2, 4, 5, 7, 8,10,11,11,10, 8, 7, 5, 4, 2, 1,
293 0, 1, 1, 2, 2, 3, 3, 4, 4, 3, 3, 2, 2, 1, 1, 0,
297 static const uint8_t obmc32
[1024]={
298 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,
299 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,
300 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,
301 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,
302 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,
303 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,
304 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,
305 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,
306 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,
307 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,
308 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,
309 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,
310 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,
311 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,
312 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,
313 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,
314 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,
315 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,
316 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,
317 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,
318 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,
319 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,
320 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,
321 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,
322 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,
323 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,
324 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,
325 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,
326 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,
327 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,
328 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,
329 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,
332 static const uint8_t obmc16
[256]={
333 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
334 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0,
335 0, 1, 3, 6, 8,11,13,14,14,13,11, 8, 6, 3, 1, 0,
336 0, 2, 6,10,15,20,24,26,26,24,20,15,10, 6, 2, 0,
337 0, 3, 8,16,23,30,35,38,38,35,30,23,16, 8, 3, 0,
338 1, 4,11,20,30,39,46,49,49,46,39,30,20,11, 4, 1,
339 1, 5,13,24,35,46,54,58,58,54,46,35,24,13, 5, 1,
340 0, 5,14,26,38,49,58,63,63,58,49,38,26,14, 5, 0,
341 0, 5,14,26,38,49,58,63,63,58,49,38,26,14, 5, 0,
342 1, 5,13,24,35,46,54,58,58,54,46,35,24,13, 5, 1,
343 1, 4,11,20,30,39,46,49,49,46,39,30,20,11, 4, 1,
344 0, 3, 8,16,23,30,35,38,38,35,30,23,16, 8, 3, 0,
345 0, 2, 6,10,15,20,24,26,26,24,20,15,10, 6, 2, 0,
346 0, 1, 3, 6, 8,11,13,14,14,13,11, 8, 6, 3, 1, 0,
347 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0,
348 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
354 static const uint8_t obmc8
[64]={
355 1, 3, 5, 7, 7, 5, 3, 1,
356 3, 9,15,21,21,15, 9, 3,
357 5,15,25,35,35,25,15, 5,
358 7,21,35,49,49,35,21, 7,
359 7,21,35,49,49,35,21, 7,
360 5,15,25,35,35,25,15, 5,
361 3, 9,15,21,21,15, 9, 3,
362 1, 3, 5, 7, 7, 5, 3, 1,
367 static const uint8_t obmc4
[16]={
375 static const uint8_t *obmc_tab
[4]={
376 obmc32
, obmc16
, obmc8
, obmc4
379 typedef struct BlockNode
{
384 //#define TYPE_SPLIT 1
385 #define BLOCK_INTRA 1
386 //#define TYPE_NOCOLOR 4
387 uint8_t level
; //FIXME merge into type?
390 #define LOG2_MB_SIZE 4
391 #define MB_SIZE (1<<LOG2_MB_SIZE)
393 typedef struct x_and_coeff
{
398 typedef struct SubBand
{
403 int qlog
; ///< log(qscale)/log[2^(1/6)]
407 int stride_line
; ///< Stride measured in lines, not pixels.
408 x_and_coeff
* x_coeff
;
409 struct SubBand
*parent
;
410 uint8_t state
[/*7*2*/ 7 + 512][32];
413 typedef struct Plane
{
416 SubBand band
[MAX_DECOMPOSITIONS
][4];
419 /** Used to minimize the amount of memory used in order to optimize cache performance. **/
421 DWTELEM
* * line
; ///< For use by idwt and predict_slices.
422 DWTELEM
* * data_stack
; ///< Used for internal purposes.
427 DWTELEM
* base_buffer
; ///< Buffer that this structure is caching.
430 typedef struct SnowContext
{
431 // 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)
433 AVCodecContext
*avctx
;
436 AVFrame input_picture
;
437 AVFrame current_picture
;
438 AVFrame last_picture
;
439 AVFrame mconly_picture
;
440 // uint8_t q_context[16];
441 uint8_t header_state
[32];
442 uint8_t block_state
[128 + 32*128];
446 int spatial_decomposition_type
;
447 int temporal_decomposition_type
;
448 int spatial_decomposition_count
;
449 int temporal_decomposition_count
;
450 DWTELEM
*spatial_dwt_buffer
;
454 int spatial_scalability
;
460 #define QBIAS_SHIFT 3
464 Plane plane
[MAX_PLANES
];
468 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)
479 #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)))
480 //#define slice_buffer_get_line(slice_buf, line_num) (slice_buffer_load_line((slice_buf), (line_num)))
482 static void slice_buffer_init(slice_buffer
* buf
, int line_count
, int max_allocated_lines
, int line_width
, DWTELEM
* base_buffer
)
486 buf
->base_buffer
= base_buffer
;
487 buf
->line_count
= line_count
;
488 buf
->line_width
= line_width
;
489 buf
->data_count
= max_allocated_lines
;
490 buf
->line
= (DWTELEM
* *) av_mallocz (sizeof(DWTELEM
*) * line_count
);
491 buf
->data_stack
= (DWTELEM
* *) av_malloc (sizeof(DWTELEM
*) * max_allocated_lines
);
493 for (i
= 0; i
< max_allocated_lines
; i
++)
495 buf
->data_stack
[i
] = (DWTELEM
*) av_malloc (sizeof(DWTELEM
) * line_width
);
498 buf
->data_stack_top
= max_allocated_lines
- 1;
501 static DWTELEM
* slice_buffer_load_line(slice_buffer
* buf
, int line
)
507 // av_log(NULL, AV_LOG_DEBUG, "Cache hit: %d\n", line);
509 assert(buf
->data_stack_top
>= 0);
510 // assert(!buf->line[line]);
512 return buf
->line
[line
];
514 offset
= buf
->line_width
* line
;
515 buffer
= buf
->data_stack
[buf
->data_stack_top
];
516 buf
->data_stack_top
--;
517 buf
->line
[line
] = buffer
;
519 // av_log(NULL, AV_LOG_DEBUG, "slice_buffer_load_line: line: %d remaining: %d\n", line, buf->data_stack_top + 1);
524 static void slice_buffer_release(slice_buffer
* buf
, int line
)
530 assert(line
>= 0 && line
< buf
->line_count
);
531 assert(buf
->line
[line
]);
533 offset
= buf
->line_width
* line
;
534 buffer
= buf
->line
[line
];
535 buf
->data_stack_top
++;
536 buf
->data_stack
[buf
->data_stack_top
] = buffer
;
537 buf
->line
[line
] = NULL
;
539 // av_log(NULL, AV_LOG_DEBUG, "slice_buffer_release: line: %d remaining: %d\n", line, buf->data_stack_top + 1);
542 static void slice_buffer_flush(slice_buffer
* buf
)
545 for (i
= 0; i
< buf
->line_count
; i
++)
549 // av_log(NULL, AV_LOG_DEBUG, "slice_buffer_flush: line: %d \n", i);
550 slice_buffer_release(buf
, i
);
555 static void slice_buffer_destroy(slice_buffer
* buf
)
558 slice_buffer_flush(buf
);
560 for (i
= buf
->data_count
- 1; i
>= 0; i
--)
562 assert(buf
->data_stack
[i
]);
563 av_free(buf
->data_stack
[i
]);
565 assert(buf
->data_stack
);
566 av_free(buf
->data_stack
);
572 // Avoid a name clash on SGI IRIX
575 #define QEXPSHIFT (7-FRAC_BITS+8) //FIXME try to change this to 0
576 static uint8_t qexp
[QROOT
];
578 static inline int mirror(int v
, int m
){
580 else if(v
>m
) return 2*m
-v
;
584 static inline void put_symbol(RangeCoder
*c
, uint8_t *state
, int v
, int is_signed
){
589 const int e
= av_log2(a
);
591 const int el
= FFMIN(e
, 10);
592 put_rac(c
, state
+0, 0);
595 put_rac(c
, state
+1+i
, 1); //1..10
598 put_rac(c
, state
+1+9, 1); //1..10
600 put_rac(c
, state
+1+FFMIN(i
,9), 0);
602 for(i
=e
-1; i
>=el
; i
--){
603 put_rac(c
, state
+22+9, (a
>>i
)&1); //22..31
606 put_rac(c
, state
+22+i
, (a
>>i
)&1); //22..31
610 put_rac(c
, state
+11 + el
, v
< 0); //11..21
613 put_rac(c
, state
+0, 0);
616 put_rac(c
, state
+1+i
, 1); //1..10
618 put_rac(c
, state
+1+i
, 0);
620 for(i
=e
-1; i
>=0; i
--){
621 put_rac(c
, state
+22+i
, (a
>>i
)&1); //22..31
625 put_rac(c
, state
+11 + e
, v
< 0); //11..21
628 put_rac(c
, state
+1+FFMIN(i
,9), 1); //1..10
630 put_rac(c
, state
+1+FFMIN(i
,9), 0);
632 for(i
=e
-1; i
>=0; i
--){
633 put_rac(c
, state
+22+FFMIN(i
,9), (a
>>i
)&1); //22..31
637 put_rac(c
, state
+11 + FFMIN(e
,10), v
< 0); //11..21
641 put_rac(c
, state
+0, 1);
645 static inline int get_symbol(RangeCoder
*c
, uint8_t *state
, int is_signed
){
646 if(get_rac(c
, state
+0))
651 while(get_rac(c
, state
+1 + FFMIN(e
,9))){ //1..10
656 for(i
=e
-1; i
>=0; i
--){
657 a
+= a
+ get_rac(c
, state
+22 + FFMIN(i
,9)); //22..31
660 if(is_signed
&& get_rac(c
, state
+11 + FFMIN(e
,10))) //11..21
667 static inline void put_symbol2(RangeCoder
*c
, uint8_t *state
, int v
, int log2
){
669 int r
= log2
>=0 ?
1<<log2
: 1;
675 put_rac(c
, state
+4+log2
, 1);
680 put_rac(c
, state
+4+log2
, 0);
682 for(i
=log2
-1; i
>=0; i
--){
683 put_rac(c
, state
+31-i
, (v
>>i
)&1);
687 static inline int get_symbol2(RangeCoder
*c
, uint8_t *state
, int log2
){
689 int r
= log2
>=0 ?
1<<log2
: 1;
694 while(get_rac(c
, state
+4+log2
)){
700 for(i
=log2
-1; i
>=0; i
--){
701 v
+= get_rac(c
, state
+31-i
)<<i
;
707 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
){
708 const int mirror_left
= !highpass
;
709 const int mirror_right
= (width
&1) ^ highpass
;
710 const int w
= (width
>>1) - 1 + (highpass
& width
);
713 #define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
715 dst
[0] = LIFT(src
[0], ((mul
*2*ref
[0]+add
)>>shift
), inverse
);
721 dst
[i
*dst_step
] = LIFT(src
[i
*src_step
], ((mul
*(ref
[i
*ref_step
] + ref
[(i
+1)*ref_step
])+add
)>>shift
), inverse
);
725 dst
[w
*dst_step
] = LIFT(src
[w
*src_step
], ((mul
*2*ref
[w
*ref_step
]+add
)>>shift
), inverse
);
729 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
){
730 const int mirror_left
= !highpass
;
731 const int mirror_right
= (width
&1) ^ highpass
;
732 const int w
= (width
>>1) - 1 + (highpass
& width
);
739 dst
[0] = LIFT(src
[0], ((r
+add
)>>shift
), inverse
);
745 int r
= 3*(ref
[i
*ref_step
] + ref
[(i
+1)*ref_step
]);
748 dst
[i
*dst_step
] = LIFT(src
[i
*src_step
], ((r
+add
)>>shift
), inverse
);
752 int r
= 3*2*ref
[w
*ref_step
];
755 dst
[w
*dst_step
] = LIFT(src
[w
*src_step
], ((r
+add
)>>shift
), inverse
);
760 static void inplace_lift(DWTELEM
*dst
, int width
, int *coeffs
, int n
, int shift
, int start
, int inverse
){
763 for(x
=start
; x
<width
; x
+=2){
767 int x2
= x
+ 2*i
- n
+ 1;
769 else if(x2
>=width
) x2
= 2*width
-x2
-2;
770 sum
+= coeffs
[i
]*(int64_t)dst
[x2
];
772 if(inverse
) dst
[x
] -= (sum
+ (1<<shift
)/2)>>shift
;
773 else dst
[x
] += (sum
+ (1<<shift
)/2)>>shift
;
777 static void inplace_liftV(DWTELEM
*dst
, int width
, int height
, int stride
, int *coeffs
, int n
, int shift
, int start
, int inverse
){
779 for(y
=start
; y
<height
; y
+=2){
780 for(x
=0; x
<width
; x
++){
784 int y2
= y
+ 2*i
- n
+ 1;
786 else if(y2
>=height
) y2
= 2*height
-y2
-2;
787 sum
+= coeffs
[i
]*(int64_t)dst
[x
+ y2
*stride
];
789 if(inverse
) dst
[x
+ y
*stride
] -= (sum
+ (1<<shift
)/2)>>shift
;
790 else dst
[x
+ y
*stride
] += (sum
+ (1<<shift
)/2)>>shift
;
799 #if 0 // more accurate 9/7
802 #define COEFFS1 (int[]){-25987,-25987}
805 #define COEFFS2 (int[]){-27777,-27777}
808 #define COEFFS3 (int[]){28931,28931}
811 #define COEFFS4 (int[]){14533,14533}
815 #define COEFFS1 (int[]){1,-9,-9,1}
818 #define COEFFS2 (int[]){-1,5,5,-1}
831 #define COEFFS1 (int[]){1,1}
834 #define COEFFS2 (int[]){-1,-1}
847 #define COEFFS2 (int[]){-1,-1}
850 #define COEFFS3 (int[]){-1,-1}
853 #define COEFFS4 (int[]){-5,29,29,-5}
858 #define COEFFS1 (int[]){-203,-203}
861 #define COEFFS2 (int[]){-217,-217}
864 #define COEFFS3 (int[]){113,113}
867 #define COEFFS4 (int[]){227,227}
875 #define COEFFS2 (int[]){-1,-1}
878 #define COEFFS3 (int[]){-1,-1}
881 #define COEFFS4 (int[]){3,3}
885 #define COEFFS1 (int[]){1,-9,-9,1}
888 #define COEFFS2 (int[]){1,1}
898 #define COEFFS1 (int[]){1,-9,-9,1}
901 #define COEFFS2 (int[]){-1,5,5,-1}
909 static void horizontal_decomposeX(DWTELEM
*b
, int width
){
911 const int width2
= width
>>1;
912 const int w2
= (width
+1)>>1;
915 inplace_lift(b
, width
, COEFFS1
, N1
, SHIFT1
, LX1
, 0);
916 inplace_lift(b
, width
, COEFFS2
, N2
, SHIFT2
, LX0
, 0);
917 inplace_lift(b
, width
, COEFFS3
, N3
, SHIFT3
, LX1
, 0);
918 inplace_lift(b
, width
, COEFFS4
, N4
, SHIFT4
, LX0
, 0);
920 for(x
=0; x
<width2
; x
++){
922 temp
[x
+w2
]= b
[2*x
+ 1];
926 memcpy(b
, temp
, width
*sizeof(int));
929 static void horizontal_composeX(DWTELEM
*b
, int width
){
931 const int width2
= width
>>1;
933 const int w2
= (width
+1)>>1;
935 memcpy(temp
, b
, width
*sizeof(int));
936 for(x
=0; x
<width2
; x
++){
938 b
[2*x
+ 1]= temp
[x
+w2
];
943 inplace_lift(b
, width
, COEFFS4
, N4
, SHIFT4
, LX0
, 1);
944 inplace_lift(b
, width
, COEFFS3
, N3
, SHIFT3
, LX1
, 1);
945 inplace_lift(b
, width
, COEFFS2
, N2
, SHIFT2
, LX0
, 1);
946 inplace_lift(b
, width
, COEFFS1
, N1
, SHIFT1
, LX1
, 1);
949 static void spatial_decomposeX(DWTELEM
*buffer
, int width
, int height
, int stride
){
952 for(y
=0; y
<height
; y
++){
953 for(x
=0; x
<width
; x
++){
954 buffer
[y
*stride
+ x
] *= SCALEX
;
958 for(y
=0; y
<height
; y
++){
959 horizontal_decomposeX(buffer
+ y
*stride
, width
);
962 inplace_liftV(buffer
, width
, height
, stride
, COEFFS1
, N1
, SHIFT1
, LX1
, 0);
963 inplace_liftV(buffer
, width
, height
, stride
, COEFFS2
, N2
, SHIFT2
, LX0
, 0);
964 inplace_liftV(buffer
, width
, height
, stride
, COEFFS3
, N3
, SHIFT3
, LX1
, 0);
965 inplace_liftV(buffer
, width
, height
, stride
, COEFFS4
, N4
, SHIFT4
, LX0
, 0);
968 static void spatial_composeX(DWTELEM
*buffer
, int width
, int height
, int stride
){
971 inplace_liftV(buffer
, width
, height
, stride
, COEFFS4
, N4
, SHIFT4
, LX0
, 1);
972 inplace_liftV(buffer
, width
, height
, stride
, COEFFS3
, N3
, SHIFT3
, LX1
, 1);
973 inplace_liftV(buffer
, width
, height
, stride
, COEFFS2
, N2
, SHIFT2
, LX0
, 1);
974 inplace_liftV(buffer
, width
, height
, stride
, COEFFS1
, N1
, SHIFT1
, LX1
, 1);
976 for(y
=0; y
<height
; y
++){
977 horizontal_composeX(buffer
+ y
*stride
, width
);
980 for(y
=0; y
<height
; y
++){
981 for(x
=0; x
<width
; x
++){
982 buffer
[y
*stride
+ x
] /= SCALEX
;
987 static void horizontal_decompose53i(DWTELEM
*b
, int width
){
989 const int width2
= width
>>1;
991 const int w2
= (width
+1)>>1;
993 for(x
=0; x
<width2
; x
++){
995 temp
[x
+w2
]= b
[2*x
+ 1];
1007 for(x
=1; x
+1<width2
; x
+=2){
1011 A2
+= (A1
+ A3
+ 2)>>2;
1015 A1
= temp
[x
+1+width2
];
1018 A4
+= (A1
+ A3
+ 2)>>2;
1024 A2
+= (A1
+ A3
+ 2)>>2;
1028 lift(b
+w2
, temp
+w2
, temp
, 1, 1, 1, width
, -1, 0, 1, 1, 0);
1029 lift(b
, temp
, b
+w2
, 1, 1, 1, width
, 1, 2, 2, 0, 0);
1033 static void vertical_decompose53iH0(DWTELEM
*b0
, DWTELEM
*b1
, DWTELEM
*b2
, int width
){
1036 for(i
=0; i
<width
; i
++){
1037 b1
[i
] -= (b0
[i
] + b2
[i
])>>1;
1041 static void vertical_decompose53iL0(DWTELEM
*b0
, DWTELEM
*b1
, DWTELEM
*b2
, int width
){
1044 for(i
=0; i
<width
; i
++){
1045 b1
[i
] += (b0
[i
] + b2
[i
] + 2)>>2;
1049 static void spatial_decompose53i(DWTELEM
*buffer
, int width
, int height
, int stride
){
1051 DWTELEM
*b0
= buffer
+ mirror(-2-1, height
-1)*stride
;
1052 DWTELEM
*b1
= buffer
+ mirror(-2 , height
-1)*stride
;
1054 for(y
=-2; y
<height
; y
+=2){
1055 DWTELEM
*b2
= buffer
+ mirror(y
+1, height
-1)*stride
;
1056 DWTELEM
*b3
= buffer
+ mirror(y
+2, height
-1)*stride
;
1059 if(b1
<= b3
) horizontal_decompose53i(b2
, width
);
1060 if(y
+2 < height
) horizontal_decompose53i(b3
, width
);
1061 STOP_TIMER("horizontal_decompose53i")}
1064 if(b1
<= b3
) vertical_decompose53iH0(b1
, b2
, b3
, width
);
1065 if(b0
<= b2
) vertical_decompose53iL0(b0
, b1
, b2
, width
);
1066 STOP_TIMER("vertical_decompose53i*")}
1142 static void horizontal_decompose97i(DWTELEM
*b
, int width
){
1143 DWTELEM temp
[width
];
1144 const int w2
= (width
+1)>>1;
1146 lift (temp
+w2
, b
+1, b
, 1, 2, 2, width
, -W_AM
, W_AO
, W_AS
, 1, 0);
1147 lift (temp
, b
, temp
+w2
, 1, 2, 1, width
, -W_BM
, W_BO
, W_BS
, 0, 0);
1148 lift5(b
+w2
, temp
+w2
, temp
, 1, 1, 1, width
, W_CM
, W_CO
, W_CS
, 1, 0);
1149 lift (b
, temp
, b
+w2
, 1, 1, 1, width
, W_DM
, W_DO
, W_DS
, 0, 0);
1153 static void vertical_decompose97iH0(DWTELEM
*b0
, DWTELEM
*b1
, DWTELEM
*b2
, int width
){
1156 for(i
=0; i
<width
; i
++){
1157 b1
[i
] -= (W_AM
*(b0
[i
] + b2
[i
])+W_AO
)>>W_AS
;
1161 static void vertical_decompose97iH1(DWTELEM
*b0
, DWTELEM
*b1
, DWTELEM
*b2
, int width
){
1164 for(i
=0; i
<width
; i
++){
1166 b1
[i
] += (W_CM
*(b0
[i
] + b2
[i
])+W_CO
)>>W_CS
;
1168 int r
= 3*(b0
[i
] + b2
[i
]);
1171 b1
[i
] += (r
+W_CO
)>>W_CS
;
1176 static void vertical_decompose97iL0(DWTELEM
*b0
, DWTELEM
*b1
, DWTELEM
*b2
, int width
){
1179 for(i
=0; i
<width
; i
++){
1180 b1
[i
] -= (W_BM
*(b0
[i
] + b2
[i
])+W_BO
)>>W_BS
;
1184 static void vertical_decompose97iL1(DWTELEM
*b0
, DWTELEM
*b1
, DWTELEM
*b2
, int width
){
1187 for(i
=0; i
<width
; i
++){
1188 b1
[i
] += (W_DM
*(b0
[i
] + b2
[i
])+W_DO
)>>W_DS
;
1192 static void spatial_decompose97i(DWTELEM
*buffer
, int width
, int height
, int stride
){
1194 DWTELEM
*b0
= buffer
+ mirror(-4-1, height
-1)*stride
;
1195 DWTELEM
*b1
= buffer
+ mirror(-4 , height
-1)*stride
;
1196 DWTELEM
*b2
= buffer
+ mirror(-4+1, height
-1)*stride
;
1197 DWTELEM
*b3
= buffer
+ mirror(-4+2, height
-1)*stride
;
1199 for(y
=-4; y
<height
; y
+=2){
1200 DWTELEM
*b4
= buffer
+ mirror(y
+3, height
-1)*stride
;
1201 DWTELEM
*b5
= buffer
+ mirror(y
+4, height
-1)*stride
;
1204 if(b3
<= b5
) horizontal_decompose97i(b4
, width
);
1205 if(y
+4 < height
) horizontal_decompose97i(b5
, width
);
1207 STOP_TIMER("horizontal_decompose97i")
1211 if(b3
<= b5
) vertical_decompose97iH0(b3
, b4
, b5
, width
);
1212 if(b2
<= b4
) vertical_decompose97iL0(b2
, b3
, b4
, width
);
1213 if(b1
<= b3
) vertical_decompose97iH1(b1
, b2
, b3
, width
);
1214 if(b0
<= b2
) vertical_decompose97iL1(b0
, b1
, b2
, width
);
1217 STOP_TIMER("vertical_decompose97i")
1227 void ff_spatial_dwt(DWTELEM
*buffer
, int width
, int height
, int stride
, int type
, int decomposition_count
){
1230 for(level
=0; level
<decomposition_count
; level
++){
1232 case 0: spatial_decompose97i(buffer
, width
>>level
, height
>>level
, stride
<<level
); break;
1233 case 1: spatial_decompose53i(buffer
, width
>>level
, height
>>level
, stride
<<level
); break;
1234 case 2: spatial_decomposeX (buffer
, width
>>level
, height
>>level
, stride
<<level
); break;
1239 static void horizontal_compose53i(DWTELEM
*b
, int width
){
1240 DWTELEM temp
[width
];
1241 const int width2
= width
>>1;
1242 const int w2
= (width
+1)>>1;
1253 for(x
=1; x
+1<width2
; x
+=2){
1257 A2
+= (A1
+ A3
+ 2)>>2;
1261 A1
= temp
[x
+1+width2
];
1264 A4
+= (A1
+ A3
+ 2)>>2;
1270 A2
+= (A1
+ A3
+ 2)>>2;
1274 lift(temp
, b
, b
+w2
, 1, 1, 1, width
, 1, 2, 2, 0, 1);
1275 lift(temp
+w2
, b
+w2
, temp
, 1, 1, 1, width
, -1, 0, 1, 1, 1);
1277 for(x
=0; x
<width2
; x
++){
1279 b
[2*x
+ 1]= temp
[x
+w2
];
1285 static void vertical_compose53iH0(DWTELEM
*b0
, DWTELEM
*b1
, DWTELEM
*b2
, int width
){
1288 for(i
=0; i
<width
; i
++){
1289 b1
[i
] += (b0
[i
] + b2
[i
])>>1;
1293 static void vertical_compose53iL0(DWTELEM
*b0
, DWTELEM
*b1
, DWTELEM
*b2
, int width
){
1296 for(i
=0; i
<width
; i
++){
1297 b1
[i
] -= (b0
[i
] + b2
[i
] + 2)>>2;
1301 static void spatial_compose53i_buffered_init(dwt_compose_t
*cs
, slice_buffer
* sb
, int height
, int stride_line
){
1302 cs
->b0
= slice_buffer_get_line(sb
, mirror(-1-1, height
-1) * stride_line
);
1303 cs
->b1
= slice_buffer_get_line(sb
, mirror(-1 , height
-1) * stride_line
);
1307 static void spatial_compose53i_init(dwt_compose_t
*cs
, DWTELEM
*buffer
, int height
, int stride
){
1308 cs
->b0
= buffer
+ mirror(-1-1, height
-1)*stride
;
1309 cs
->b1
= buffer
+ mirror(-1 , height
-1)*stride
;
1313 static void spatial_compose53i_dy_buffered(dwt_compose_t
*cs
, slice_buffer
* sb
, int width
, int height
, int stride_line
){
1315 int mirror0
= mirror(y
-1, height
-1);
1316 int mirror1
= mirror(y
, height
-1);
1317 int mirror2
= mirror(y
+1, height
-1);
1318 int mirror3
= mirror(y
+2, height
-1);
1320 DWTELEM
*b0
= cs
->b0
;
1321 DWTELEM
*b1
= cs
->b1
;
1322 DWTELEM
*b2
= slice_buffer_get_line(sb
, mirror2
* stride_line
);
1323 DWTELEM
*b3
= slice_buffer_get_line(sb
, mirror3
* stride_line
);
1326 if(mirror1
<= mirror3
) vertical_compose53iL0(b1
, b2
, b3
, width
);
1327 if(mirror0
<= mirror2
) vertical_compose53iH0(b0
, b1
, b2
, width
);
1328 STOP_TIMER("vertical_compose53i*")}
1331 if(y
-1 >= 0) horizontal_compose53i(b0
, width
);
1332 if(mirror0
<= mirror2
) horizontal_compose53i(b1
, width
);
1333 STOP_TIMER("horizontal_compose53i")}
1340 static void spatial_compose53i_dy(dwt_compose_t
*cs
, DWTELEM
*buffer
, int width
, int height
, int stride
){
1342 DWTELEM
*b0
= cs
->b0
;
1343 DWTELEM
*b1
= cs
->b1
;
1344 DWTELEM
*b2
= buffer
+ mirror(y
+1, height
-1)*stride
;
1345 DWTELEM
*b3
= buffer
+ mirror(y
+2, height
-1)*stride
;
1348 if(b1
<= b3
) vertical_compose53iL0(b1
, b2
, b3
, width
);
1349 if(b0
<= b2
) vertical_compose53iH0(b0
, b1
, b2
, width
);
1350 STOP_TIMER("vertical_compose53i*")}
1353 if(y
-1 >= 0) horizontal_compose53i(b0
, width
);
1354 if(b0
<= b2
) horizontal_compose53i(b1
, width
);
1355 STOP_TIMER("horizontal_compose53i")}
1362 static void spatial_compose53i(DWTELEM
*buffer
, int width
, int height
, int stride
){
1364 spatial_compose53i_init(&cs
, buffer
, height
, stride
);
1365 while(cs
.y
<= height
)
1366 spatial_compose53i_dy(&cs
, buffer
, width
, height
, stride
);
1370 static void horizontal_compose97i(DWTELEM
*b
, int width
){
1371 DWTELEM temp
[width
];
1372 const int w2
= (width
+1)>>1;
1374 lift (temp
, b
, b
+w2
, 1, 1, 1, width
, W_DM
, W_DO
, W_DS
, 0, 1);
1375 lift5(temp
+w2
, b
+w2
, temp
, 1, 1, 1, width
, W_CM
, W_CO
, W_CS
, 1, 1);
1376 lift (b
, temp
, temp
+w2
, 2, 1, 1, width
, -W_BM
, W_BO
, W_BS
, 0, 1);
1377 lift (b
+1 , temp
+w2
, b
, 2, 1, 2, width
, -W_AM
, W_AO
, W_AS
, 1, 1);
1380 static void vertical_compose97iH0(DWTELEM
*b0
, DWTELEM
*b1
, DWTELEM
*b2
, int width
){
1383 for(i
=0; i
<width
; i
++){
1384 b1
[i
] += (W_AM
*(b0
[i
] + b2
[i
])+W_AO
)>>W_AS
;
1388 static void vertical_compose97iH1(DWTELEM
*b0
, DWTELEM
*b1
, DWTELEM
*b2
, int width
){
1391 for(i
=0; i
<width
; i
++){
1393 b1
[i
] -= (W_CM
*(b0
[i
] + b2
[i
])+W_CO
)>>W_CS
;
1395 int r
= 3*(b0
[i
] + b2
[i
]);
1398 b1
[i
] -= (r
+W_CO
)>>W_CS
;
1403 static void vertical_compose97iL0(DWTELEM
*b0
, DWTELEM
*b1
, DWTELEM
*b2
, int width
){
1406 for(i
=0; i
<width
; i
++){
1407 b1
[i
] += (W_BM
*(b0
[i
] + b2
[i
])+W_BO
)>>W_BS
;
1411 static void vertical_compose97iL1(DWTELEM
*b0
, DWTELEM
*b1
, DWTELEM
*b2
, int width
){
1414 for(i
=0; i
<width
; i
++){
1415 b1
[i
] -= (W_DM
*(b0
[i
] + b2
[i
])+W_DO
)>>W_DS
;
1419 static void vertical_compose97i(DWTELEM
*b0
, DWTELEM
*b1
, DWTELEM
*b2
, DWTELEM
*b3
, DWTELEM
*b4
, DWTELEM
*b5
, int width
){
1422 for(i
=0; i
<width
; i
++){
1424 b4
[i
] -= (W_DM
*(b3
[i
] + b5
[i
])+W_DO
)>>W_DS
;
1426 b3
[i
] -= (W_CM
*(b2
[i
] + b4
[i
])+W_CO
)>>W_CS
;
1428 r
= 3*(b2
[i
] + b4
[i
]);
1431 b3
[i
] -= (r
+W_CO
)>>W_CS
;
1433 b2
[i
] += (W_BM
*(b1
[i
] + b3
[i
])+W_BO
)>>W_BS
;
1434 b1
[i
] += (W_AM
*(b0
[i
] + b2
[i
])+W_AO
)>>W_AS
;
1438 static void spatial_compose97i_buffered_init(dwt_compose_t
*cs
, slice_buffer
* sb
, int height
, int stride_line
){
1439 cs
->b0
= slice_buffer_get_line(sb
, mirror(-3-1, height
-1) * stride_line
);
1440 cs
->b1
= slice_buffer_get_line(sb
, mirror(-3 , height
-1) * stride_line
);
1441 cs
->b2
= slice_buffer_get_line(sb
, mirror(-3+1, height
-1) * stride_line
);
1442 cs
->b3
= slice_buffer_get_line(sb
, mirror(-3+2, height
-1) * stride_line
);
1446 static void spatial_compose97i_init(dwt_compose_t
*cs
, DWTELEM
*buffer
, int height
, int stride
){
1447 cs
->b0
= buffer
+ mirror(-3-1, height
-1)*stride
;
1448 cs
->b1
= buffer
+ mirror(-3 , height
-1)*stride
;
1449 cs
->b2
= buffer
+ mirror(-3+1, height
-1)*stride
;
1450 cs
->b3
= buffer
+ mirror(-3+2, height
-1)*stride
;
1454 static void spatial_compose97i_dy_buffered(dwt_compose_t
*cs
, slice_buffer
* sb
, int width
, int height
, int stride_line
){
1457 int mirror0
= mirror(y
- 1, height
- 1);
1458 int mirror1
= mirror(y
+ 0, height
- 1);
1459 int mirror2
= mirror(y
+ 1, height
- 1);
1460 int mirror3
= mirror(y
+ 2, height
- 1);
1461 int mirror4
= mirror(y
+ 3, height
- 1);
1462 int mirror5
= mirror(y
+ 4, height
- 1);
1463 DWTELEM
*b0
= cs
->b0
;
1464 DWTELEM
*b1
= cs
->b1
;
1465 DWTELEM
*b2
= cs
->b2
;
1466 DWTELEM
*b3
= cs
->b3
;
1467 DWTELEM
*b4
= slice_buffer_get_line(sb
, mirror4
* stride_line
);
1468 DWTELEM
*b5
= slice_buffer_get_line(sb
, mirror5
* stride_line
);
1471 if(y
>0 && y
+4<height
){
1472 vertical_compose97i(b0
, b1
, b2
, b3
, b4
, b5
, width
);
1474 if(mirror3
<= mirror5
) vertical_compose97iL1(b3
, b4
, b5
, width
);
1475 if(mirror2
<= mirror4
) vertical_compose97iH1(b2
, b3
, b4
, width
);
1476 if(mirror1
<= mirror3
) vertical_compose97iL0(b1
, b2
, b3
, width
);
1477 if(mirror0
<= mirror2
) vertical_compose97iH0(b0
, b1
, b2
, width
);
1480 STOP_TIMER("vertical_compose97i")}}
1483 if(y
-1>= 0) horizontal_compose97i(b0
, width
);
1484 if(mirror0
<= mirror2
) horizontal_compose97i(b1
, width
);
1485 if(width
>400 && mirror0
<= mirror2
){
1486 STOP_TIMER("horizontal_compose97i")}}
1495 static void spatial_compose97i_dy(dwt_compose_t
*cs
, DWTELEM
*buffer
, int width
, int height
, int stride
){
1497 DWTELEM
*b0
= cs
->b0
;
1498 DWTELEM
*b1
= cs
->b1
;
1499 DWTELEM
*b2
= cs
->b2
;
1500 DWTELEM
*b3
= cs
->b3
;
1501 DWTELEM
*b4
= buffer
+ mirror(y
+3, height
-1)*stride
;
1502 DWTELEM
*b5
= buffer
+ mirror(y
+4, height
-1)*stride
;
1504 if(stride
== width
&& y
+4 < height
&& 0){
1506 for(x
=0; x
<width
/2; x
++)
1513 if(b3
<= b5
) vertical_compose97iL1(b3
, b4
, b5
, width
);
1514 if(b2
<= b4
) vertical_compose97iH1(b2
, b3
, b4
, width
);
1515 if(b1
<= b3
) vertical_compose97iL0(b1
, b2
, b3
, width
);
1516 if(b0
<= b2
) vertical_compose97iH0(b0
, b1
, b2
, width
);
1518 STOP_TIMER("vertical_compose97i")}}
1521 if(y
-1>= 0) horizontal_compose97i(b0
, width
);
1522 if(b0
<= b2
) horizontal_compose97i(b1
, width
);
1523 if(width
>400 && b0
<= b2
){
1524 STOP_TIMER("horizontal_compose97i")}}
1533 static void spatial_compose97i(DWTELEM
*buffer
, int width
, int height
, int stride
){
1535 spatial_compose97i_init(&cs
, buffer
, height
, stride
);
1536 while(cs
.y
<= height
)
1537 spatial_compose97i_dy(&cs
, buffer
, width
, height
, stride
);
1540 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
){
1542 for(level
=decomposition_count
-1; level
>=0; level
--){
1544 case 0: spatial_compose97i_buffered_init(cs
+level
, sb
, height
>>level
, stride_line
<<level
); break;
1545 case 1: spatial_compose53i_buffered_init(cs
+level
, sb
, height
>>level
, stride_line
<<level
); break;
1546 /* not slicified yet */
1547 case 2: /*spatial_composeX(buffer, width>>level, height>>level, stride<<level); break;*/
1548 av_log(NULL
, AV_LOG_ERROR
, "spatial_composeX neither buffered nor slicified yet.\n"); break;
1553 void ff_spatial_idwt_init(dwt_compose_t
*cs
, DWTELEM
*buffer
, int width
, int height
, int stride
, int type
, int decomposition_count
){
1555 for(level
=decomposition_count
-1; level
>=0; level
--){
1557 case 0: spatial_compose97i_init(cs
+level
, buffer
, height
>>level
, stride
<<level
); break;
1558 case 1: spatial_compose53i_init(cs
+level
, buffer
, height
>>level
, stride
<<level
); break;
1559 /* not slicified yet */
1560 case 2: spatial_composeX(buffer
, width
>>level
, height
>>level
, stride
<<level
); break;
1565 void ff_spatial_idwt_slice(dwt_compose_t
*cs
, DWTELEM
*buffer
, int width
, int height
, int stride
, int type
, int decomposition_count
, int y
){
1566 const int support
= type
==1 ?
3 : 5;
1570 for(level
=decomposition_count
-1; level
>=0; level
--){
1571 while(cs
[level
].y
<= FFMIN((y
>>level
)+support
, height
>>level
)){
1573 case 0: spatial_compose97i_dy(cs
+level
, buffer
, width
>>level
, height
>>level
, stride
<<level
);
1575 case 1: spatial_compose53i_dy(cs
+level
, buffer
, width
>>level
, height
>>level
, stride
<<level
);
1583 void ff_spatial_idwt_buffered_slice(dwt_compose_t
*cs
, slice_buffer
* slice_buf
, int width
, int height
, int stride_line
, int type
, int decomposition_count
, int y
){
1584 const int support
= type
==1 ?
3 : 5;
1588 for(level
=decomposition_count
-1; level
>=0; level
--){
1589 while(cs
[level
].y
<= FFMIN((y
>>level
)+support
, height
>>level
)){
1591 case 0: spatial_compose97i_dy_buffered(cs
+level
, slice_buf
, width
>>level
, height
>>level
, stride_line
<<level
);
1593 case 1: spatial_compose53i_dy_buffered(cs
+level
, slice_buf
, width
>>level
, height
>>level
, stride_line
<<level
);
1601 void ff_spatial_idwt(DWTELEM
*buffer
, int width
, int height
, int stride
, int type
, int decomposition_count
){
1604 for(level
=decomposition_count
-1; level
>=0; level
--)
1605 spatial_composeX (buffer
, width
>>level
, height
>>level
, stride
<<level
);
1607 dwt_compose_t cs
[MAX_DECOMPOSITIONS
];
1609 ff_spatial_idwt_init(cs
, buffer
, width
, height
, stride
, type
, decomposition_count
);
1610 for(y
=0; y
<height
; y
+=4)
1611 ff_spatial_idwt_slice(cs
, buffer
, width
, height
, stride
, type
, decomposition_count
, y
);
1615 static int encode_subband_c0run(SnowContext
*s
, SubBand
*b
, DWTELEM
*src
, DWTELEM
*parent
, int stride
, int orientation
){
1616 const int w
= b
->width
;
1617 const int h
= b
->height
;
1628 int /*ll=0, */l
=0, lt
=0, t
=0, rt
=0;
1629 v
= src
[x
+ y
*stride
];
1632 t
= src
[x
+ (y
-1)*stride
];
1634 lt
= src
[x
- 1 + (y
-1)*stride
];
1637 rt
= src
[x
+ 1 + (y
-1)*stride
];
1641 l
= src
[x
- 1 + y
*stride
];
1643 if(orientation==1) ll= src[y + (x-2)*stride];
1644 else ll= src[x - 2 + y*stride];
1650 if(px
<b
->parent
->width
&& py
<b
->parent
->height
)
1651 p
= parent
[px
+ py
*2*stride
];
1653 if(!(/*ll|*/l
|lt
|t
|rt
|p
)){
1655 runs
[run_index
++]= run
;
1663 runs
[run_index
++]= run
;
1665 run
= runs
[run_index
++];
1667 put_symbol2(&s
->c
, b
->state
[1], run
, 3);
1670 if(s
->c
.bytestream_end
- s
->c
.bytestream
< w
*40){
1671 av_log(s
->avctx
, AV_LOG_ERROR
, "encoded frame too large\n");
1676 int /*ll=0, */l
=0, lt
=0, t
=0, rt
=0;
1677 v
= src
[x
+ y
*stride
];
1680 t
= src
[x
+ (y
-1)*stride
];
1682 lt
= src
[x
- 1 + (y
-1)*stride
];
1685 rt
= src
[x
+ 1 + (y
-1)*stride
];
1689 l
= src
[x
- 1 + y
*stride
];
1691 if(orientation==1) ll= src[y + (x-2)*stride];
1692 else ll= src[x - 2 + y*stride];
1698 if(px
<b
->parent
->width
&& py
<b
->parent
->height
)
1699 p
= parent
[px
+ py
*2*stride
];
1701 if(/*ll|*/l
|lt
|t
|rt
|p
){
1702 int context
= av_log2(/*ABS(ll) + */3*ABS(l
) + ABS(lt
) + 2*ABS(t
) + ABS(rt
) + ABS(p
));
1704 put_rac(&s
->c
, &b
->state
[0][context
], !!v
);
1707 run
= runs
[run_index
++];
1709 put_symbol2(&s
->c
, b
->state
[1], run
, 3);
1717 int context
= av_log2(/*ABS(ll) + */3*ABS(l
) + ABS(lt
) + 2*ABS(t
) + ABS(rt
) + ABS(p
));
1718 int l2
= 2*ABS(l
) + (l
<0);
1719 int t2
= 2*ABS(t
) + (t
<0);
1721 put_symbol2(&s
->c
, b
->state
[context
+ 2], ABS(v
)-1, context
-4);
1722 put_rac(&s
->c
, &b
->state
[0][16 + 1 + 3 + quant3bA
[l2
&0xFF] + 3*quant3bA
[t2
&0xFF]], v
<0);
1730 static int encode_subband(SnowContext
*s
, SubBand
*b
, DWTELEM
*src
, DWTELEM
*parent
, int stride
, int orientation
){
1731 // encode_subband_qtree(s, b, src, parent, stride, orientation);
1732 // encode_subband_z0run(s, b, src, parent, stride, orientation);
1733 return encode_subband_c0run(s
, b
, src
, parent
, stride
, orientation
);
1734 // encode_subband_dzr(s, b, src, parent, stride, orientation);
1737 static inline void unpack_coeffs(SnowContext
*s
, SubBand
*b
, SubBand
* parent
, int orientation
){
1738 const int w
= b
->width
;
1739 const int h
= b
->height
;
1747 int parent_index
= 0;
1748 int prev_parent_index
= 0;
1750 run
= get_symbol2(&s
->c
, b
->state
[1], 3);
1753 int lt
=0, t
=0, rt
=0;
1755 if(y
&& b
->x_coeff
[prev_index
].x
== 0){
1756 rt
= b
->x_coeff
[prev_index
].coeff
;
1765 if(b
->x_coeff
[prev_index
].x
<= x
)
1767 if(b
->x_coeff
[prev_index
].x
== x
+ 1)
1768 rt
= b
->x_coeff
[prev_index
].coeff
;
1773 if(x
>>1 > parent
->x_coeff
[parent_index
].x
){
1776 if(x
>>1 == parent
->x_coeff
[parent_index
].x
){
1777 p
= parent
->x_coeff
[parent_index
].coeff
;
1780 if(/*ll|*/l
|lt
|t
|rt
|p
){
1781 int context
= av_log2(/*ABS(ll) + */3*(l
>>1) + (lt
>>1) + (t
&~1) + (rt
>>1) + (p
>>1));
1783 v
=get_rac(&s
->c
, &b
->state
[0][context
]);
1786 run
= get_symbol2(&s
->c
, b
->state
[1], 3);
1795 max_run
= FFMIN(run
, b
->x_coeff
[prev_index
].x
- x
- 2);
1796 max_run
= FFMIN(max_run
, 2*parent
->x_coeff
[parent_index
].x
- x
- 1);
1803 int context
= av_log2(/*ABS(ll) + */3*(l
>>1) + (lt
>>1) + (t
&~1) + (rt
>>1) + (p
>>1));
1804 v
= 2*(get_symbol2(&s
->c
, b
->state
[context
+ 2], context
-4) + 1);
1805 v
+=get_rac(&s
->c
, &b
->state
[0][16 + 1 + 3 + quant3bA
[l
&0xFF] + 3*quant3bA
[t
&0xFF]]);
1807 b
->x_coeff
[index
].x
=x
;
1808 b
->x_coeff
[index
++].coeff
= v
;
1811 b
->x_coeff
[index
++].x
= w
+1; //end marker
1812 prev_index
= prev2_index
;
1817 while(parent
->x_coeff
[parent_index
].x
!= parent
->width
+1)
1820 prev_parent_index
= parent_index
;
1822 parent_index
= prev_parent_index
;
1827 b
->x_coeff
[index
++].x
= w
+1; //end marker
1831 static inline void decode_subband_slice_buffered(SnowContext
*s
, SubBand
*b
, slice_buffer
* sb
, int start_y
, int h
, int save_state
[1]){
1832 const int w
= b
->width
;
1834 const int qlog
= clip(s
->qlog
+ b
->qlog
, 0, QROOT
*16);
1835 int qmul
= qexp
[qlog
&(QROOT
-1)]<<(qlog
>>QSHIFT
);
1836 int qadd
= (s
->qbias
*qmul
)>>QBIAS_SHIFT
;
1841 if(b
->buf
== s
->spatial_dwt_buffer
|| s
->qlog
== LOSSLESS_QLOG
){
1846 /* If we are on the second or later slice, restore our index. */
1848 new_index
= save_state
[0];
1851 for(y
=start_y
; y
<h
; y
++){
1854 DWTELEM
* line
= slice_buffer_get_line(sb
, y
* b
->stride_line
+ b
->buf_y_offset
) + b
->buf_x_offset
;
1855 memset(line
, 0, b
->width
*sizeof(DWTELEM
));
1856 v
= b
->x_coeff
[new_index
].coeff
;
1857 x
= b
->x_coeff
[new_index
++].x
;
1860 register int t
= ( (v
>>1)*qmul
+ qadd
)>>QEXPSHIFT
;
1861 register int u
= -(v
&1);
1862 line
[x
] = (t
^u
) - u
;
1864 v
= b
->x_coeff
[new_index
].coeff
;
1865 x
= b
->x_coeff
[new_index
++].x
;
1868 if(w
> 200 && start_y
!= 0/*level+1 == s->spatial_decomposition_count*/){
1869 STOP_TIMER("decode_subband")
1872 /* Save our variables for the next slice. */
1873 save_state
[0] = new_index
;
1878 static void reset_contexts(SnowContext
*s
){
1879 int plane_index
, level
, orientation
;
1881 for(plane_index
=0; plane_index
<3; plane_index
++){
1882 for(level
=0; level
<s
->spatial_decomposition_count
; level
++){
1883 for(orientation
=level ?
1:0; orientation
<4; orientation
++){
1884 memset(s
->plane
[plane_index
].band
[level
][orientation
].state
, MID_STATE
, sizeof(s
->plane
[plane_index
].band
[level
][orientation
].state
));
1888 memset(s
->header_state
, MID_STATE
, sizeof(s
->header_state
));
1889 memset(s
->block_state
, MID_STATE
, sizeof(s
->block_state
));
1892 static int alloc_blocks(SnowContext
*s
){
1893 int w
= -((-s
->avctx
->width
)>>LOG2_MB_SIZE
);
1894 int h
= -((-s
->avctx
->height
)>>LOG2_MB_SIZE
);
1899 s
->block
= av_mallocz(w
* h
* sizeof(BlockNode
) << (s
->block_max_depth
*2));
1903 static inline void copy_rac_state(RangeCoder
*d
, RangeCoder
*s
){
1904 uint8_t *bytestream
= d
->bytestream
;
1905 uint8_t *bytestream_start
= d
->bytestream_start
;
1907 d
->bytestream
= bytestream
;
1908 d
->bytestream_start
= bytestream_start
;
1911 //near copy & paste from dsputil, FIXME
1912 static int pix_sum(uint8_t * pix
, int line_size
, int w
)
1917 for (i
= 0; i
< w
; i
++) {
1918 for (j
= 0; j
< w
; j
++) {
1922 pix
+= line_size
- w
;
1927 //near copy & paste from dsputil, FIXME
1928 static int pix_norm1(uint8_t * pix
, int line_size
, int w
)
1931 uint32_t *sq
= squareTbl
+ 256;
1934 for (i
= 0; i
< w
; i
++) {
1935 for (j
= 0; j
< w
; j
++) {
1939 pix
+= line_size
- w
;
1944 static inline void set_blocks(SnowContext
*s
, int level
, int x
, int y
, int l
, int cb
, int cr
, int mx
, int my
, int type
){
1945 const int w
= s
->b_width
<< s
->block_max_depth
;
1946 const int rem_depth
= s
->block_max_depth
- level
;
1947 const int index
= (x
+ y
*w
) << rem_depth
;
1948 const int block_w
= 1<<rem_depth
;
1960 for(j
=0; j
<block_w
; j
++){
1961 for(i
=0; i
<block_w
; i
++){
1962 s
->block
[index
+ i
+ j
*w
]= block
;
1967 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
){
1968 const int offset
[3]= {
1970 ((y
*c
->uvstride
+ x
)>>1),
1971 ((y
*c
->uvstride
+ x
)>>1),
1975 c
->src
[0][i
]= src
[i
];
1976 c
->ref
[0][i
]= ref
[i
] + offset
[i
];
1984 #define P_TOPRIGHT P[3]
1985 #define P_MEDIAN P[4]
1987 #define FLAG_QPEL 1 //must be 1
1989 static int encode_q_branch(SnowContext
*s
, int level
, int x
, int y
){
1990 uint8_t p_buffer
[1024];
1991 uint8_t i_buffer
[1024];
1992 uint8_t p_state
[sizeof(s
->block_state
)];
1993 uint8_t i_state
[sizeof(s
->block_state
)];
1995 uint8_t *pbbak
= s
->c
.bytestream
;
1996 uint8_t *pbbak_start
= s
->c
.bytestream_start
;
1997 int score
, score2
, iscore
, i_len
, p_len
, block_s
, sum
;
1998 const int w
= s
->b_width
<< s
->block_max_depth
;
1999 const int h
= s
->b_height
<< s
->block_max_depth
;
2000 const int rem_depth
= s
->block_max_depth
- level
;
2001 const int index
= (x
+ y
*w
) << rem_depth
;
2002 const int block_w
= 1<<(LOG2_MB_SIZE
- level
);
2003 static BlockNode null_block
= { //FIXME add border maybe
2004 .color
= {128,128,128},
2010 int trx
= (x
+1)<<rem_depth
;
2011 int try= (y
+1)<<rem_depth
;
2012 BlockNode
*left
= x ?
&s
->block
[index
-1] : &null_block
;
2013 BlockNode
*top
= y ?
&s
->block
[index
-w
] : &null_block
;
2014 BlockNode
*right
= trx
<w ?
&s
->block
[index
+1] : &null_block
;
2015 BlockNode
*bottom
= try<h ?
&s
->block
[index
+w
] : &null_block
;
2016 BlockNode
*tl
= y
&& x ?
&s
->block
[index
-w
-1] : left
;
2017 BlockNode
*tr
= y
&& trx
<w
&& ((x
&1)==0 || level
==0) ?
&s
->block
[index
-w
+(1<<rem_depth
)] : tl
; //FIXME use lt
2018 int pl
= left
->color
[0];
2019 int pcb
= left
->color
[1];
2020 int pcr
= left
->color
[2];
2021 int pmx
= mid_pred(left
->mx
, top
->mx
, tr
->mx
);
2022 int pmy
= mid_pred(left
->my
, top
->my
, tr
->my
);
2025 const int stride
= s
->current_picture
.linesize
[0];
2026 const int uvstride
= s
->current_picture
.linesize
[1];
2027 const int instride
= s
->input_picture
.linesize
[0];
2028 const int uvinstride
= s
->input_picture
.linesize
[1];
2029 uint8_t *new_l
= s
->input_picture
.data
[0] + (x
+ y
* instride
)*block_w
;
2030 uint8_t *new_cb
= s
->input_picture
.data
[1] + (x
+ y
*uvinstride
)*block_w
/2;
2031 uint8_t *new_cr
= s
->input_picture
.data
[2] + (x
+ y
*uvinstride
)*block_w
/2;
2032 uint8_t current_mb
[3][stride
*block_w
];
2033 uint8_t *current_data
[3]= {¤t_mb
[0][0], ¤t_mb
[1][0], ¤t_mb
[2][0]};
2035 int16_t last_mv
[3][2];
2036 int qpel
= !!(s
->avctx
->flags
& CODEC_FLAG_QPEL
); //unused
2037 const int shift
= 1+qpel
;
2038 MotionEstContext
*c
= &s
->m
.me
;
2039 int mx_context
= av_log2(2*ABS(left
->mx
- top
->mx
));
2040 int my_context
= av_log2(2*ABS(left
->my
- top
->my
));
2041 int s_context
= 2*left
->level
+ 2*top
->level
+ tl
->level
+ tr
->level
;
2043 assert(sizeof(s
->block_state
) >= 256);
2045 set_blocks(s
, level
, x
, y
, pl
, pcb
, pcr
, pmx
, pmy
, BLOCK_INTRA
);
2050 for(i
=0; i
<block_w
; i
++)
2051 memcpy(¤t_mb
[0][0] + stride
*i
, new_l
+ instride
*i
, block_w
);
2052 for(i
=0; i
<block_w
>>1; i
++)
2053 memcpy(¤t_mb
[1][0] + uvstride
*i
, new_cb
+ uvinstride
*i
, block_w
>>1);
2054 for(i
=0; i
<block_w
>>1; i
++)
2055 memcpy(¤t_mb
[2][0] + uvstride
*i
, new_cr
+ uvinstride
*i
, block_w
>>1);
2057 // clip predictors / edge ?
2059 P_LEFT
[0]= left
->mx
;
2060 P_LEFT
[1]= left
->my
;
2063 P_TOPRIGHT
[0]= tr
->mx
;
2064 P_TOPRIGHT
[1]= tr
->my
;
2066 last_mv
[0][0]= s
->block
[index
].mx
;
2067 last_mv
[0][1]= s
->block
[index
].my
;
2068 last_mv
[1][0]= right
->mx
;
2069 last_mv
[1][1]= right
->my
;
2070 last_mv
[2][0]= bottom
->mx
;
2071 last_mv
[2][1]= bottom
->my
;
2078 init_ref(c
, current_data
, s
->last_picture
.data
, NULL
, block_w
*x
, block_w
*y
, 0);
2080 assert(s
->m
.me
. stride
== stride
);
2081 assert(s
->m
.me
.uvstride
== uvstride
);
2083 c
->penalty_factor
= get_penalty_factor(s
->lambda
, s
->lambda2
, c
->avctx
->me_cmp
);
2084 c
->sub_penalty_factor
= get_penalty_factor(s
->lambda
, s
->lambda2
, c
->avctx
->me_sub_cmp
);
2085 c
->mb_penalty_factor
= get_penalty_factor(s
->lambda
, s
->lambda2
, c
->avctx
->mb_cmp
);
2086 c
->current_mv_penalty
= c
->mv_penalty
[s
->m
.f_code
=1] + MAX_MV
;
2088 c
->xmin
= - x
*block_w
- 16+2;
2089 c
->ymin
= - y
*block_w
- 16+2;
2090 c
->xmax
= - (x
+1)*block_w
+ (w
<<(LOG2_MB_SIZE
- s
->block_max_depth
)) + 16-2;