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 LOSSLESS_QLOG -128
38 static const int8_t quant3[256]={
39 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
40 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
41 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
42 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
43 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
44 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
45 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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,-1,
52 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
53 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
54 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0,
56 static const int8_t quant3b[256]={
57 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
58 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
59 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
60 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
61 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
62 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
63 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
64 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
65 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
66 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
67 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
68 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
69 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
70 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
71 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
72 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
74 static const int8_t quant5[256]={
75 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
76 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
77 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
78 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
79 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
80 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
81 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
82 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
83 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
84 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
85 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
86 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
87 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
88 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
89 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
90 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,-1,
92 static const int8_t quant7[256]={
93 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
94 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
95 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
96 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
97 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
98 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
99 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
100 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
101 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
102 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
103 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
104 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
105 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
106 -3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-2,-2,-2,
107 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
108 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,
110 static const int8_t quant9[256]={
111 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
112 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
113 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
114 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
115 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
116 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
117 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
118 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
119 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
120 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
121 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
122 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
123 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
124 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
125 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,
126 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-1,-1,
128 static const int8_t quant11[256]={
129 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
130 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
131 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
132 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
133 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
134 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
135 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
136 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
137 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
138 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
139 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
140 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
141 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
142 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-4,-4,
143 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
144 -4,-4,-4,-4,-4,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-1,
146 static const int8_t quant13[256]={
147 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
148 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
149 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
150 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
151 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
152 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
153 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
154 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
155 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
156 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
157 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
158 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
159 -6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5,
160 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
161 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
162 -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
165 #define LOG2_OBMC_MAX 6
166 #define OBMC_MAX (1<<(LOG2_OBMC_MAX))
168 static const uint8_t obmc32[1024]={
169 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,
170 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,
171 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,
172 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,
173 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,
174 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,
175 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,
176 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,
177 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,
178 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,
179 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,
180 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,
181 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,
182 0, 2, 4, 7,12,16,22,27,33,38,44,48,52,56,58,60,60,58,56,52,48,44,38,33,27,22,16,12, 7, 4, 2, 0,
183 0, 1, 4, 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,
184 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,
185 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,
186 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,
187 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,
188 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,
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, 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,
191 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,
192 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,
193 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,
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, 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,
196 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,
197 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,
198 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,
199 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,
200 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,
203 static const uint8_t obmc16[256]={
204 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
205 0, 1, 1, 2, 4, 5, 5, 6, 6, 5, 5, 4, 2, 1, 1, 0,
206 0, 1, 4, 6, 9,11,13,15,15,13,11, 9, 6, 4, 1, 0,
207 0, 2, 6,11,15,20,24,26,26,24,20,15,11, 6, 2, 0,
208 0, 4, 9,15,23,29,34,38,38,34,29,23,15, 9, 4, 0,
209 0, 5,11,20,29,38,45,49,49,45,38,29,20,11, 5, 0,
210 1, 5,13,24,34,45,53,57,57,53,45,34,24,13, 5, 1,
211 1, 6,15,26,38,49,57,62,62,57,49,38,26,15, 6, 1,
212 1, 6,15,26,38,49,57,62,62,57,49,38,26,15, 6, 1,
213 1, 5,13,24,34,45,53,57,57,53,45,34,24,13, 5, 1,
214 0, 5,11,20,29,38,45,49,49,45,38,29,20,11, 5, 0,
215 0, 4, 9,15,23,29,34,38,38,34,29,23,15, 9, 4, 0,
216 0, 2, 6,11,15,20,24,26,26,24,20,15,11, 6, 2, 0,
217 0, 1, 4, 6, 9,11,13,15,15,13,11, 9, 6, 4, 1, 0,
218 0, 1, 1, 2, 4, 5, 5, 6, 6, 5, 5, 4, 2, 1, 1, 0,
219 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
223 static const uint8_t obmc32[1024]={
224 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,
225 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,
226 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,
227 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,
228 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,
229 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,
230 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,
231 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,
232 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,
233 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,
234 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,
235 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,
236 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,
237 2, 5, 8,12,15,19,22,25,29,32,35,39,42,46,49,52,52,49,46,42,39,35,32,29,25,22,19,15,12, 8, 5, 2,
238 2, 5, 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,
239 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,
240 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,
241 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,
242 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,
243 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,
244 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,
245 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,
246 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,
247 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,
248 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,
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, 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,
251 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,
252 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,
253 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,
254 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,
255 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,
258 static const uint8_t obmc16[256]={
259 0, 1, 1, 2, 2, 3, 3, 4, 4, 3, 3, 2, 2, 1, 1, 0,
260 1, 2, 4, 5, 7, 8,10,11,11,10, 8, 7, 5, 4, 2, 1,
261 1, 4, 6, 9,11,14,16,19,19,16,14,11, 9, 6, 4, 1,
262 2, 5, 9,12,16,19,23,26,26,23,19,16,12, 9, 5, 2,
263 2, 7,11,16,20,25,29,34,34,29,25,20,16,11, 7, 2,
264 3, 8,14,19,25,30,36,41,41,36,30,25,19,14, 8, 3,
265 3,10,16,23,29,36,42,49,49,42,36,29,23,16,10, 3,
266 4,11,19,26,34,41,49,56,56,49,41,34,26,19,11, 4,
267 4,11,19,26,34,41,49,56,56,49,41,34,26,19,11, 4,
268 3,10,16,23,29,36,42,49,49,42,36,29,23,16,10, 3,
269 3, 8,14,19,25,30,36,41,41,36,30,25,19,14, 8, 3,
270 2, 7,11,16,20,25,29,34,34,29,25,20,16,11, 7, 2,
271 2, 5, 9,12,16,19,23,26,26,23,19,16,12, 9, 5, 2,
272 1, 4, 6, 9,11,14,16,19,19,16,14,11, 9, 6, 4, 1,
273 1, 2, 4, 5, 7, 8,10,11,11,10, 8, 7, 5, 4, 2, 1,
274 0, 1, 1, 2, 2, 3, 3, 4, 4, 3, 3, 2, 2, 1, 1, 0,
278 static const uint8_t obmc32[1024]={
279 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,
280 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,
281 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,
282 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,
283 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,
284 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,
285 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,
286 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,
287 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,
288 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,
289 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,
290 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,
291 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,
292 0, 1, 3, 7,11,16,21,27,33,39,44,49,53,57,59,60,60,59,57,53,49,44,39,33,27,21,16,11, 7, 3, 1, 0,
293 0, 1, 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,
294 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,
295 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,
296 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,
297 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,
298 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,
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, 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,
301 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,
302 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,
303 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,
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, 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,
306 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,
307 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,
308 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,
309 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,
310 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,
313 static const uint8_t obmc16[256]={
314 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
315 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0,
316 0, 1, 3, 6, 8,11,13,14,14,13,11, 8, 6, 3, 1, 0,
317 0, 2, 6,10,15,20,24,26,26,24,20,15,10, 6, 2, 0,
318 0, 3, 8,16,23,30,35,38,38,35,30,23,16, 8, 3, 0,
319 1, 4,11,20,30,39,46,49,49,46,39,30,20,11, 4, 1,
320 1, 5,13,24,35,46,54,58,58,54,46,35,24,13, 5, 1,
321 0, 5,14,26,38,49,58,63,63,58,49,38,26,14, 5, 0,
322 0, 5,14,26,38,49,58,63,63,58,49,38,26,14, 5, 0,
323 1, 5,13,24,35,46,54,58,58,54,46,35,24,13, 5, 1,
324 1, 4,11,20,30,39,46,49,49,46,39,30,20,11, 4, 1,
325 0, 3, 8,16,23,30,35,38,38,35,30,23,16, 8, 3, 0,
326 0, 2, 6,10,15,20,24,26,26,24,20,15,10, 6, 2, 0,
327 0, 1, 3, 6, 8,11,13,14,14,13,11, 8, 6, 3, 1, 0,
328 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0,
329 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
335 static const uint8_t obmc8[64]={
336 1, 3, 5, 7, 7, 5, 3, 1,
337 3, 9,15,21,21,15, 9, 3,
338 5,15,25,35,35,25,15, 5,
339 7,21,35,49,49,35,21, 7,
340 7,21,35,49,49,35,21, 7,
341 5,15,25,35,35,25,15, 5,
342 3, 9,15,21,21,15, 9, 3,
343 1, 3, 5, 7, 7, 5, 3, 1,
348 static const uint8_t obmc4[16]={
356 static const uint8_t *obmc_tab[4]={
357 obmc32, obmc16, obmc8, obmc4
360 typedef struct BlockNode{
365 //#define TYPE_SPLIT 1
366 #define BLOCK_INTRA 1
367 //#define TYPE_NOCOLOR 4
368 uint8_t level; //FIXME merge into type?
371 #define LOG2_MB_SIZE 4
372 #define MB_SIZE (1<<LOG2_MB_SIZE)
374 typedef struct x_and_coeff{
379 typedef struct SubBand{
384 int qlog; ///< log(qscale)/log[2^(1/6)]
388 int stride_line; ///< Stride measured in lines, not pixels.
389 x_and_coeff * x_coeff;
390 struct SubBand *parent;
391 uint8_t state[/*7*2*/ 7 + 512][32];
394 typedef struct Plane{
397 SubBand band[MAX_DECOMPOSITIONS][4];
400 /** Used to minimize the amount of memory used in order to optimize cache performance. **/
402 DWTELEM * * line; ///< For use by idwt and predict_slices.
403 DWTELEM * * data_stack; ///< Used for internal purposes.
408 DWTELEM * base_buffer; ///< Buffer that this structure is caching.
411 typedef struct SnowContext{
412 // 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)
414 AVCodecContext *avctx;
417 AVFrame input_picture;
418 AVFrame current_picture;
419 AVFrame last_picture;
420 AVFrame mconly_picture;
421 // uint8_t q_context[16];
422 uint8_t header_state[32];
423 uint8_t block_state[128 + 32*128];
427 int spatial_decomposition_type;
428 int temporal_decomposition_type;
429 int spatial_decomposition_count;
430 int temporal_decomposition_count;
431 DWTELEM *spatial_dwt_buffer;
435 int spatial_scalability;
441 #define QBIAS_SHIFT 3
445 Plane plane[MAX_PLANES];
449 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)
460 #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)))
461 //#define slice_buffer_get_line(slice_buf, line_num) (slice_buffer_load_line((slice_buf), (line_num)))
463 static void slice_buffer_init(slice_buffer * buf, int line_count, int max_allocated_lines, int line_width, DWTELEM * base_buffer)
467 buf->base_buffer = base_buffer;
468 buf->line_count = line_count;
469 buf->line_width = line_width;
470 buf->data_count = max_allocated_lines;
471 buf->line = (DWTELEM * *) av_mallocz (sizeof(DWTELEM *) * line_count);
472 buf->data_stack = (DWTELEM * *) av_malloc (sizeof(DWTELEM *) * max_allocated_lines);
474 for (i = 0; i < max_allocated_lines; i++)
476 buf->data_stack[i] = (DWTELEM *) av_malloc (sizeof(DWTELEM) * line_width);
479 buf->data_stack_top = max_allocated_lines - 1;
482 static DWTELEM * slice_buffer_load_line(slice_buffer * buf, int line)
488 // av_log(NULL, AV_LOG_DEBUG, "Cache hit: %d\n", line);
490 assert(buf->data_stack_top >= 0);
491 // assert(!buf->line[line]);
493 return buf->line[line];
495 offset = buf->line_width * line;
496 buffer = buf->data_stack[buf->data_stack_top];
497 buf->data_stack_top--;
498 buf->line[line] = buffer;
500 // av_log(NULL, AV_LOG_DEBUG, "slice_buffer_load_line: line: %d remaining: %d\n", line, buf->data_stack_top + 1);
505 static void slice_buffer_release(slice_buffer * buf, int line)
511 assert(line >= 0 && line < buf->line_count);
512 assert(buf->line[line]);
514 offset = buf->line_width * line;
515 buffer = buf->line[line];
516 buf->data_stack_top++;
517 buf->data_stack[buf->data_stack_top] = buffer;
518 buf->line[line] = NULL;
520 // av_log(NULL, AV_LOG_DEBUG, "slice_buffer_release: line: %d remaining: %d\n", line, buf->data_stack_top + 1);
523 static void slice_buffer_flush(slice_buffer * buf)
526 for (i = 0; i < buf->line_count; i++)
530 // av_log(NULL, AV_LOG_DEBUG, "slice_buffer_flush: line: %d \n", i);
531 slice_buffer_release(buf, i);
536 static void slice_buffer_destroy(slice_buffer * buf)
539 slice_buffer_flush(buf);
541 for (i = buf->data_count - 1; i >= 0; i--)
543 assert(buf->data_stack[i]);
544 av_free(buf->data_stack[i]);
546 assert(buf->data_stack);
547 av_free(buf->data_stack);
553 // Avoid a name clash on SGI IRIX
556 #define QEXPSHIFT (7-FRAC_BITS+8) //FIXME try to change this to 0
557 static const uint8_t qexp[8]={
558 128, 140, 152, 166, 181, 197, 215, 235
559 // 64, 70, 76, 83, 91, 99, 108, 117
560 // 32, 35, 38, 41, 45, 49, 54, 59
561 // 16, 17, 19, 21, 23, 25, 27, 29
562 // 8, 9, 10, 10, 11, 12, 13, 15
565 static inline int mirror(int v, int m){
567 else if(v>m) return 2*m-v;
571 static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
576 const int e= av_log2(a);
578 const int el= FFMIN(e, 10);
579 put_rac(c, state+0, 0);
582 put_rac(c, state+1+i, 1); //1..10
585 put_rac(c, state+1+9, 1); //1..10
587 put_rac(c, state+1+FFMIN(i,9), 0);
589 for(i=e-1; i>=el; i--){
590 put_rac(c, state+22+9, (a>>i)&1); //22..31
593 put_rac(c, state+22+i, (a>>i)&1); //22..31
597 put_rac(c, state+11 + el, v < 0); //11..21
600 put_rac(c, state+0, 0);
603 put_rac(c, state+1+i, 1); //1..10
605 put_rac(c, state+1+i, 0);
607 for(i=e-1; i>=0; i--){
608 put_rac(c, state+22+i, (a>>i)&1); //22..31
612 put_rac(c, state+11 + e, v < 0); //11..21
615 put_rac(c, state+1+FFMIN(i,9), 1); //1..10
617 put_rac(c, state+1+FFMIN(i,9), 0);
619 for(i=e-1; i>=0; i--){
620 put_rac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31
624 put_rac(c, state+11 + FFMIN(e,10), v < 0); //11..21
628 put_rac(c, state+0, 1);
632 static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
633 if(get_rac(c, state+0))
638 while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
643 for(i=e-1; i>=0; i--){
644 a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
647 if(is_signed && get_rac(c, state+11 + FFMIN(e,10))) //11..21
654 static inline void put_symbol2(RangeCoder *c, uint8_t *state, int v, int log2){
656 int r= log2>=0 ? 1<<log2 : 1;
662 put_rac(c, state+4+log2, 1);
667 put_rac(c, state+4+log2, 0);
669 for(i=log2-1; i>=0; i--){
670 put_rac(c, state+31-i, (v>>i)&1);
674 static inline int get_symbol2(RangeCoder *c, uint8_t *state, int log2){
676 int r= log2>=0 ? 1<<log2 : 1;
681 while(get_rac(c, state+4+log2)){
687 for(i=log2-1; i>=0; i--){
688 v+= get_rac(c, state+31-i)<<i;
694 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){
695 const int mirror_left= !highpass;
696 const int mirror_right= (width&1) ^ highpass;
697 const int w= (width>>1) - 1 + (highpass & width);
700 #define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
702 dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
708 dst[i*dst_step] = LIFT(src[i*src_step], ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift), inverse);
712 dst[w*dst_step] = LIFT(src[w*src_step], ((mul*2*ref[w*ref_step]+add)>>shift), inverse);
716 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){
717 const int mirror_left= !highpass;
718 const int mirror_right= (width&1) ^ highpass;
719 const int w= (width>>1) - 1 + (highpass & width);
726 dst[0] = LIFT(src[0], ((r+add)>>shift), inverse);
732 int r= 3*(ref[i*ref_step] + ref[(i+1)*ref_step]);
735 dst[i*dst_step] = LIFT(src[i*src_step], ((r+add)>>shift), inverse);
739 int r= 3*2*ref[w*ref_step];
742 dst[w*dst_step] = LIFT(src[w*src_step], ((r+add)>>shift), inverse);
747 static void inplace_lift(DWTELEM *dst, int width, int *coeffs, int n, int shift, int start, int inverse){
750 for(x=start; x<width; x+=2){
754 int x2= x + 2*i - n + 1;
756 else if(x2>=width) x2= 2*width-x2-2;
757 sum += coeffs[i]*(int64_t)dst[x2];
759 if(inverse) dst[x] -= (sum + (1<<shift)/2)>>shift;
760 else dst[x] += (sum + (1<<shift)/2)>>shift;
764 static void inplace_liftV(DWTELEM *dst, int width, int height, int stride, int *coeffs, int n, int shift, int start, int inverse){
766 for(y=start; y<height; y+=2){
767 for(x=0; x<width; x++){
771 int y2= y + 2*i - n + 1;
773 else if(y2>=height) y2= 2*height-y2-2;
774 sum += coeffs[i]*(int64_t)dst[x + y2*stride];
776 if(inverse) dst[x + y*stride] -= (sum + (1<<shift)/2)>>shift;
777 else dst[x + y*stride] += (sum + (1<<shift)/2)>>shift;
786 #if 0 // more accurate 9/7
789 #define COEFFS1 (int[]){-25987,-25987}
792 #define COEFFS2 (int[]){-27777,-27777}
795 #define COEFFS3 (int[]){28931,28931}
798 #define COEFFS4 (int[]){14533,14533}
802 #define COEFFS1 (int[]){1,-9,-9,1}
805 #define COEFFS2 (int[]){-1,5,5,-1}
818 #define COEFFS1 (int[]){1,1}
821 #define COEFFS2 (int[]){-1,-1}
834 #define COEFFS2 (int[]){-1,-1}
837 #define COEFFS3 (int[]){-1,-1}
840 #define COEFFS4 (int[]){-5,29,29,-5}
845 #define COEFFS1 (int[]){-203,-203}
848 #define COEFFS2 (int[]){-217,-217}
851 #define COEFFS3 (int[]){113,113}
854 #define COEFFS4 (int[]){227,227}
862 #define COEFFS2 (int[]){-1,-1}
865 #define COEFFS3 (int[]){-1,-1}
868 #define COEFFS4 (int[]){3,3}
872 #define COEFFS1 (int[]){1,-9,-9,1}
875 #define COEFFS2 (int[]){1,1}
885 #define COEFFS1 (int[]){1,-9,-9,1}
888 #define COEFFS2 (int[]){-1,5,5,-1}
896 static void horizontal_decomposeX(DWTELEM *b, int width){
898 const int width2= width>>1;
899 const int w2= (width+1)>>1;
902 inplace_lift(b, width, COEFFS1, N1, SHIFT1, LX1, 0);
903 inplace_lift(b, width, COEFFS2, N2, SHIFT2, LX0, 0);
904 inplace_lift(b, width, COEFFS3, N3, SHIFT3, LX1, 0);
905 inplace_lift(b, width, COEFFS4, N4, SHIFT4, LX0, 0);
907 for(x=0; x<width2; x++){
909 temp[x+w2]= b[2*x + 1];
913 memcpy(b, temp, width*sizeof(int));
916 static void horizontal_composeX(DWTELEM *b, int width){
918 const int width2= width>>1;
920 const int w2= (width+1)>>1;
922 memcpy(temp, b, width*sizeof(int));
923 for(x=0; x<width2; x++){
925 b[2*x + 1]= temp[x+w2];
930 inplace_lift(b, width, COEFFS4, N4, SHIFT4, LX0, 1);
931 inplace_lift(b, width, COEFFS3, N3, SHIFT3, LX1, 1);
932 inplace_lift(b, width, COEFFS2, N2, SHIFT2, LX0, 1);
933 inplace_lift(b, width, COEFFS1, N1, SHIFT1, LX1, 1);
936 static void spatial_decomposeX(DWTELEM *buffer, int width, int height, int stride){
939 for(y=0; y<height; y++){
940 for(x=0; x<width; x++){
941 buffer[y*stride + x] *= SCALEX;
945 for(y=0; y<height; y++){
946 horizontal_decomposeX(buffer + y*stride, width);
949 inplace_liftV(buffer, width, height, stride, COEFFS1, N1, SHIFT1, LX1, 0);
950 inplace_liftV(buffer, width, height, stride, COEFFS2, N2, SHIFT2, LX0, 0);
951 inplace_liftV(buffer, width, height, stride, COEFFS3, N3, SHIFT3, LX1, 0);
952 inplace_liftV(buffer, width, height, stride, COEFFS4, N4, SHIFT4, LX0, 0);
955 static void spatial_composeX(DWTELEM *buffer, int width, int height, int stride){
958 inplace_liftV(buffer, width, height, stride, COEFFS4, N4, SHIFT4, LX0, 1);
959 inplace_liftV(buffer, width, height, stride, COEFFS3, N3, SHIFT3, LX1, 1);
960 inplace_liftV(buffer, width, height, stride, COEFFS2, N2, SHIFT2, LX0, 1);
961 inplace_liftV(buffer, width, height, stride, COEFFS1, N1, SHIFT1, LX1, 1);
963 for(y=0; y<height; y++){
964 horizontal_composeX(buffer + y*stride, width);
967 for(y=0; y<height; y++){
968 for(x=0; x<width; x++){
969 buffer[y*stride + x] /= SCALEX;
974 static void horizontal_decompose53i(DWTELEM *b, int width){
976 const int width2= width>>1;
978 const int w2= (width+1)>>1;
980 for(x=0; x<width2; x++){
982 temp[x+w2]= b[2*x + 1];
994 for(x=1; x+1<width2; x+=2){
998 A2 += (A1 + A3 + 2)>>2;
1002 A1= temp[x+1+width2];
1005 A4 += (A1 + A3 + 2)>>2;
1011 A2 += (A1 + A3 + 2)>>2;
1015 lift(b+w2, temp+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
1016 lift(b , temp , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 0);
1020 static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1023 for(i=0; i<width; i++){
1024 b1[i] -= (b0[i] + b2[i])>>1;
1028 static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1031 for(i=0; i<width; i++){
1032 b1[i] += (b0[i] + b2[i] + 2)>>2;
1036 static void spatial_decompose53i(DWTELEM *buffer, int width, int height, int stride){
1038 DWTELEM *b0= buffer + mirror(-2-1, height-1)*stride;
1039 DWTELEM *b1= buffer + mirror(-2 , height-1)*stride;
1041 for(y=-2; y<height; y+=2){
1042 DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
1043 DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
1046 if(b1 <= b3) horizontal_decompose53i(b2, width);
1047 if(y+2 < height) horizontal_decompose53i(b3, width);
1048 STOP_TIMER("horizontal_decompose53i")}
1051 if(b1 <= b3) vertical_decompose53iH0(b1, b2, b3, width);
1052 if(b0 <= b2) vertical_decompose53iL0(b0, b1, b2, width);
1053 STOP_TIMER("vertical_decompose53i*")}
1129 static void horizontal_decompose97i(DWTELEM *b, int width){
1130 DWTELEM temp[width];
1131 const int w2= (width+1)>>1;
1133 lift (temp+w2, b +1, b , 1, 2, 2, width, -W_AM, W_AO, W_AS, 1, 0);
1134 lift (temp , b , temp+w2, 1, 2, 1, width, -W_BM, W_BO, W_BS, 0, 0);
1135 lift5(b +w2, temp+w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 0);
1136 lift (b , temp , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 0);
1140 static void vertical_decompose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1143 for(i=0; i<width; i++){
1144 b1[i] -= (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
1148 static void vertical_decompose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1151 for(i=0; i<width; i++){
1153 b1[i] += (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
1155 int r= 3*(b0[i] + b2[i]);
1158 b1[i] += (r+W_CO)>>W_CS;
1163 static void vertical_decompose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1166 for(i=0; i<width; i++){
1167 b1[i] -= (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
1171 static void vertical_decompose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1174 for(i=0; i<width; i++){
1175 b1[i] += (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
1179 static void spatial_decompose97i(DWTELEM *buffer, int width, int height, int stride){
1181 DWTELEM *b0= buffer + mirror(-4-1, height-1)*stride;
1182 DWTELEM *b1= buffer + mirror(-4 , height-1)*stride;
1183 DWTELEM *b2= buffer + mirror(-4+1, height-1)*stride;
1184 DWTELEM *b3= buffer + mirror(-4+2, height-1)*stride;
1186 for(y=-4; y<height; y+=2){
1187 DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
1188 DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
1191 if(b3 <= b5) horizontal_decompose97i(b4, width);
1192 if(y+4 < height) horizontal_decompose97i(b5, width);
1194 STOP_TIMER("horizontal_decompose97i")
1198 if(b3 <= b5) vertical_decompose97iH0(b3, b4, b5, width);
1199 if(b2 <= b4) vertical_decompose97iL0(b2, b3, b4, width);
1200 if(b1 <= b3) vertical_decompose97iH1(b1, b2, b3, width);
1201 if(b0 <= b2) vertical_decompose97iL1(b0, b1, b2, width);
1204 STOP_TIMER("vertical_decompose97i")
1214 void ff_spatial_dwt(DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
1217 for(level=0; level<decomposition_count; level++){
1219 case 0: spatial_decompose97i(buffer, width>>level, height>>level, stride<<level); break;
1220 case 1: spatial_decompose53i(buffer, width>>level, height>>level, stride<<level); break;
1221 case 2: spatial_decomposeX (buffer, width>>level, height>>level, stride<<level); break;
1226 static void horizontal_compose53i(DWTELEM *b, int width){
1227 DWTELEM temp[width];
1228 const int width2= width>>1;
1229 const int w2= (width+1)>>1;
1240 for(x=1; x+1<width2; x+=2){
1244 A2 += (A1 + A3 + 2)>>2;
1248 A1= temp[x+1+width2];
1251 A4 += (A1 + A3 + 2)>>2;
1257 A2 += (A1 + A3 + 2)>>2;
1261 lift(temp , b , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 1);
1262 lift(temp+w2, b+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 1);
1264 for(x=0; x<width2; x++){
1266 b[2*x + 1]= temp[x+w2];
1272 static void vertical_compose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1275 for(i=0; i<width; i++){
1276 b1[i] += (b0[i] + b2[i])>>1;
1280 static void vertical_compose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1283 for(i=0; i<width; i++){
1284 b1[i] -= (b0[i] + b2[i] + 2)>>2;
1288 static void spatial_compose53i_buffered_init(dwt_compose_t *cs, slice_buffer * sb, int height, int stride_line){
1289 cs->b0 = slice_buffer_get_line(sb, mirror(-1-1, height-1) * stride_line);
1290 cs->b1 = slice_buffer_get_line(sb, mirror(-1 , height-1) * stride_line);
1294 static void spatial_compose53i_init(dwt_compose_t *cs, DWTELEM *buffer, int height, int stride){
1295 cs->b0 = buffer + mirror(-1-1, height-1)*stride;
1296 cs->b1 = buffer + mirror(-1 , height-1)*stride;
1300 static void spatial_compose53i_dy_buffered(dwt_compose_t *cs, slice_buffer * sb, int width, int height, int stride_line){
1302 int mirror0 = mirror(y-1, height-1);
1303 int mirror1 = mirror(y , height-1);
1304 int mirror2 = mirror(y+1, height-1);
1305 int mirror3 = mirror(y+2, height-1);
1307 DWTELEM *b0= cs->b0;
1308 DWTELEM *b1= cs->b1;
1309 DWTELEM *b2= slice_buffer_get_line(sb, mirror2 * stride_line);
1310 DWTELEM *b3= slice_buffer_get_line(sb, mirror3 * stride_line);
1313 if(mirror1 <= mirror3) vertical_compose53iL0(b1, b2, b3, width);
1314 if(mirror0 <= mirror2) vertical_compose53iH0(b0, b1, b2, width);
1315 STOP_TIMER("vertical_compose53i*")}
1318 if(y-1 >= 0) horizontal_compose53i(b0, width);
1319 if(mirror0 <= mirror2) horizontal_compose53i(b1, width);
1320 STOP_TIMER("horizontal_compose53i")}
1327 static void spatial_compose53i_dy(dwt_compose_t *cs, DWTELEM *buffer, int width, int height, int stride){
1329 DWTELEM *b0= cs->b0;
1330 DWTELEM *b1= cs->b1;
1331 DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
1332 DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
1335 if(b1 <= b3) vertical_compose53iL0(b1, b2, b3, width);
1336 if(b0 <= b2) vertical_compose53iH0(b0, b1, b2, width);
1337 STOP_TIMER("vertical_compose53i*")}
1340 if(y-1 >= 0) horizontal_compose53i(b0, width);
1341 if(b0 <= b2) horizontal_compose53i(b1, width);
1342 STOP_TIMER("horizontal_compose53i")}
1349 static void spatial_compose53i(DWTELEM *buffer, int width, int height, int stride){
1351 spatial_compose53i_init(&cs, buffer, height, stride);
1352 while(cs.y <= height)
1353 spatial_compose53i_dy(&cs, buffer, width, height, stride);
1357 static void horizontal_compose97i(DWTELEM *b, int width){
1358 DWTELEM temp[width];
1359 const int w2= (width+1)>>1;
1361 lift (temp , b , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 1);
1362 lift5(temp+w2, b +w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 1);
1363 lift (b , temp , temp+w2, 2, 1, 1, width, -W_BM, W_BO, W_BS, 0, 1);
1364 lift (b+1 , temp+w2, b , 2, 1, 2, width, -W_AM, W_AO, W_AS, 1, 1);
1367 static void vertical_compose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1370 for(i=0; i<width; i++){
1371 b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
1375 static void vertical_compose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1378 for(i=0; i<width; i++){
1380 b1[i] -= (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
1382 int r= 3*(b0[i] + b2[i]);
1385 b1[i] -= (r+W_CO)>>W_CS;
1390 static void vertical_compose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1393 for(i=0; i<width; i++){
1394 b1[i] += (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
1398 static void vertical_compose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
1401 for(i=0; i<width; i++){
1402 b1[i] -= (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
1406 static void spatial_compose97i_buffered_init(dwt_compose_t *cs, slice_buffer * sb, int height, int stride_line){
1407 cs->b0 = slice_buffer_get_line(sb, mirror(-3-1, height-1) * stride_line);
1408 cs->b1 = slice_buffer_get_line(sb, mirror(-3 , height-1) * stride_line);
1409 cs->b2 = slice_buffer_get_line(sb, mirror(-3+1, height-1) * stride_line);
1410 cs->b3 = slice_buffer_get_line(sb, mirror(-3+2, height-1) * stride_line);
1414 static void spatial_compose97i_init(dwt_compose_t *cs, DWTELEM *buffer, int height, int stride){
1415 cs->b0 = buffer + mirror(-3-1, height-1)*stride;
1416 cs->b1 = buffer + mirror(-3 , height-1)*stride;
1417 cs->b2 = buffer + mirror(-3+1, height-1)*stride;
1418 cs->b3 = buffer + mirror(-3+2, height-1)*stride;
1422 static void spatial_compose97i_dy_buffered(dwt_compose_t *cs, slice_buffer * sb, int width, int height, int stride_line){
1425 int mirror0 = mirror(y - 1, height - 1);
1426 int mirror1 = mirror(y + 0, height - 1);
1427 int mirror2 = mirror(y + 1, height - 1);
1428 int mirror3 = mirror(y + 2, height - 1);
1429 int mirror4 = mirror(y + 3, height - 1);
1430 int mirror5 = mirror(y + 4, height - 1);
1431 DWTELEM *b0= cs->b0;
1432 DWTELEM *b1= cs->b1;
1433 DWTELEM *b2= cs->b2;
1434 DWTELEM *b3= cs->b3;
1435 DWTELEM *b4= slice_buffer_get_line(sb, mirror4 * stride_line);
1436 DWTELEM *b5= slice_buffer_get_line(sb, mirror5 * stride_line);
1438 if(stride_line == 1 && y+4 < height && 0){
1440 for(x=0; x<width/2; x++)
1446 // if(mirror3 <= mirror5 && mirror2 <= mirror4 && mirror1 <= mirror3 && mirror0 <= mirror2)
1449 // vertical_compose97_complete(b0, b1, b2, b3, b4, b5, width);
1451 //STOP_TIMER("vertical_compose97i-NEW")}}
1456 if(mirror3 <= mirror5) vertical_compose97iL1(b3, b4, b5, width);
1457 if(mirror2 <= mirror4) vertical_compose97iH1(b2, b3, b4, width);
1458 if(mirror1 <= mirror3) vertical_compose97iL0(b1, b2, b3, width);
1459 if(mirror0 <= mirror2) vertical_compose97iH0(b0, b1, b2, width);
1461 STOP_TIMER("vertical_compose97i")}}
1465 if(y-1>= 0) horizontal_compose97i(b0, width);
1466 if(mirror0 <= mirror2) horizontal_compose97i(b1, width);
1467 if(width>400 && mirror0 <= mirror2){
1468 STOP_TIMER("horizontal_compose97i")}}
1477 static void spatial_compose97i_dy(dwt_compose_t *cs, DWTELEM *buffer, int width, int height, int stride){
1479 DWTELEM *b0= cs->b0;
1480 DWTELEM *b1= cs->b1;
1481 DWTELEM *b2= cs->b2;
1482 DWTELEM *b3= cs->b3;
1483 DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
1484 DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
1486 if(stride == width && y+4 < height && 0){
1488 for(x=0; x<width/2; x++)
1495 if(b3 <= b5) vertical_compose97iL1(b3, b4, b5, width);
1496 if(b2 <= b4) vertical_compose97iH1(b2, b3, b4, width);
1497 if(b1 <= b3) vertical_compose97iL0(b1, b2, b3, width);
1498 if(b0 <= b2) vertical_compose97iH0(b0, b1, b2, width);
1500 STOP_TIMER("vertical_compose97i")}}
1503 if(y-1>= 0) horizontal_compose97i(b0, width);
1504 if(b0 <= b2) horizontal_compose97i(b1, width);
1505 if(width>400 && b0 <= b2){
1506 STOP_TIMER("horizontal_compose97i")}}
1515 static void spatial_compose97i(DWTELEM *buffer, int width, int height, int stride){
1517 spatial_compose97i_init(&cs, buffer, height, stride);
1518 while(cs.y <= height)
1519 spatial_compose97i_dy(&cs, buffer, width, height, stride);
1522 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){
1524 for(level=decomposition_count-1; level>=0; level--){
1526 case 0: spatial_compose97i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
1527 case 1: spatial_compose53i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
1528 /* not slicified yet */
1529 case 2: /*spatial_composeX(buffer, width>>level, height>>level, stride<<level); break;*/
1530 av_log(NULL, AV_LOG_ERROR, "spatial_composeX neither buffered nor slicified yet.\n"); break;
1535 void ff_spatial_idwt_init(dwt_compose_t *cs, DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
1537 for(level=decomposition_count-1; level>=0; level--){
1539 case 0: spatial_compose97i_init(cs+level, buffer, height>>level, stride<<level); break;
1540 case 1: spatial_compose53i_init(cs+level, buffer, height>>level, stride<<level); break;
1541 /* not slicified yet */
1542 case 2: spatial_composeX(buffer, width>>level, height>>level, stride<<level); break;
1547 void ff_spatial_idwt_slice(dwt_compose_t *cs, DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count, int y){
1548 const int support = type==1 ? 3 : 5;
1552 for(level=decomposition_count-1; level>=0; level--){
1553 while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
1555 case 0: spatial_compose97i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
1557 case 1: spatial_compose53i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
1565 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){
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_buffered(cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
1575 case 1: spatial_compose53i_dy_buffered(cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
1583 void ff_spatial_idwt(DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
1586 for(level=decomposition_count-1; level>=0; level--)
1587 spatial_composeX (buffer, width>>level, height>>level, stride<<level);
1589 dwt_compose_t cs[MAX_DECOMPOSITIONS];
1591 ff_spatial_idwt_init(cs, buffer, width, height, stride, type, decomposition_count);
1592 for(y=0; y<height; y+=4)
1593 ff_spatial_idwt_slice(cs, buffer, width, height, stride, type, decomposition_count, y);
1597 static int encode_subband_c0run(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
1598 const int w= b->width;
1599 const int h= b->height;
1610 int /*ll=0, */l=0, lt=0, t=0, rt=0;
1611 v= src[x + y*stride];
1614 t= src[x + (y-1)*stride];
1616 lt= src[x - 1 + (y-1)*stride];
1619 rt= src[x + 1 + (y-1)*stride];
1623 l= src[x - 1 + y*stride];
1625 if(orientation==1) ll= src[y + (x-2)*stride];
1626 else ll= src[x - 2 + y*stride];
1632 if(px<b->parent->width && py<b->parent->height)
1633 p= parent[px + py*2*stride];
1635 if(!(/*ll|*/l|lt|t|rt|p)){
1637 runs[run_index++]= run;
1645 runs[run_index++]= run;
1647 run= runs[run_index++];
1649 put_symbol2(&s->c, b->state[1], run, 3);
1652 if(s->c.bytestream_end - s->c.bytestream < w*40){
1653 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
1658 int /*ll=0, */l=0, lt=0, t=0, rt=0;
1659 v= src[x + y*stride];
1662 t= src[x + (y-1)*stride];
1664 lt= src[x - 1 + (y-1)*stride];
1667 rt= src[x + 1 + (y-1)*stride];
1671 l= src[x - 1 + y*stride];
1673 if(orientation==1) ll= src[y + (x-2)*stride];
1674 else ll= src[x - 2 + y*stride];
1680 if(px<b->parent->width && py<b->parent->height)
1681 p= parent[px + py*2*stride];
1683 if(/*ll|*/l|lt|t|rt|p){
1684 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
1686 put_rac(&s->c, &b->state[0][context], !!v);
1689 run= runs[run_index++];
1691 put_symbol2(&s->c, b->state[1], run, 3);
1699 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
1701 put_symbol2(&s->c, b->state[context + 2], ABS(v)-1, context-4);
1702 put_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]], v<0);
1710 static int encode_subband(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
1711 // encode_subband_qtree(s, b, src, parent, stride, orientation);
1712 // encode_subband_z0run(s, b, src, parent, stride, orientation);
1713 return encode_subband_c0run(s, b, src, parent, stride, orientation);
1714 // encode_subband_dzr(s, b, src, parent, stride, orientation);
1717 static inline void unpack_coeffs(SnowContext *s, SubBand *b, SubBand * parent, int orientation){
1718 const int w= b->width;
1719 const int h= b->height;
1727 int parent_index= 0;
1728 int prev_parent_index= 0;
1730 run= get_symbol2(&s->c, b->state[1], 3);
1733 int lt=0, t=0, rt=0;
1735 if(y && b->x_coeff[prev_index].x == 0){
1736 rt= b->x_coeff[prev_index].coeff;
1745 if(b->x_coeff[prev_index].x <= x)
1747 if(b->x_coeff[prev_index].x == x + 1)
1748 rt= b->x_coeff[prev_index].coeff;
1753 if(x>>1 > parent->x_coeff[parent_index].x){
1756 if(x>>1 == parent->x_coeff[parent_index].x){
1757 p= parent->x_coeff[parent_index].coeff;
1760 if(/*ll|*/l|lt|t|rt|p){
1761 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
1763 v=get_rac(&s->c, &b->state[0][context]);
1766 run= get_symbol2(&s->c, b->state[1], 3);
1775 max_run= FFMIN(run, b->x_coeff[prev_index].x - x - 2);
1776 max_run= FFMIN(max_run, 2*parent->x_coeff[parent_index].x - x - 1);
1783 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
1784 v= get_symbol2(&s->c, b->state[context + 2], context-4) + 1;
1785 if(get_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]]))
1787 b->x_coeff[index].x=x;
1788 b->x_coeff[index++].coeff= v;
1791 b->x_coeff[index++].x= w+1; //end marker
1792 prev_index= prev2_index;
1797 while(parent->x_coeff[parent_index].x != parent->width+1)
1800 prev_parent_index= parent_index;
1802 parent_index= prev_parent_index;
1807 b->x_coeff[index++].x= w+1; //end marker
1811 static inline void decode_subband_slice_buffered(SnowContext *s, SubBand *b, slice_buffer * sb, int start_y, int h, int save_state[1]){
1812 const int w= b->width;
1814 const int qlog= clip(s->qlog + b->qlog, 0, 128);
1815 int qmul= qexp[qlog&7]<<(qlog>>3);
1816 int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
1821 if(b->buf == s->spatial_dwt_buffer || s->qlog == LOSSLESS_QLOG){
1826 /* If we are on the second or later slice, restore our index. */
1828 new_index = save_state[0];
1831 for(y=start_y; y<h; y++){
1834 DWTELEM * line = slice_buffer_get_line(sb, y * b->stride_line + b->buf_y_offset) + b->buf_x_offset;
1835 memset(line, 0, b->width*sizeof(DWTELEM));
1836 v = b->x_coeff[new_index].coeff;
1837 x = b->x_coeff[new_index++].x;
1841 line[x] = -(( -v*qmul + qadd)>>(QEXPSHIFT));
1843 line[x] = (( v*qmul + qadd)>>(QEXPSHIFT));
1844 v = b->x_coeff[new_index].coeff;
1845 x = b->x_coeff[new_index++].x;
1848 if(w > 200 && start_y != 0/*level+1 == s->spatial_decomposition_count*/){
1849 STOP_TIMER("decode_subband")
1852 /* Save our variables for the next slice. */
1853 save_state[0] = new_index;
1858 static void reset_contexts(SnowContext *s){
1859 int plane_index, level, orientation;
1861 for(plane_index=0; plane_index<3; plane_index++){
1862 for(level=0; level<s->spatial_decomposition_count; level++){
1863 for(orientation=level ? 1:0; orientation<4; orientation++){
1864 memset(s->plane[plane_index].band[level][orientation].state, MID_STATE, sizeof(s->plane[plane_index].band[level][orientation].state));
1868 memset(s->header_state, MID_STATE, sizeof(s->header_state));
1869 memset(s->block_state, MID_STATE, sizeof(s->block_state));
1872 static int alloc_blocks(SnowContext *s){
1873 int w= -((-s->avctx->width )>>LOG2_MB_SIZE);
1874 int h= -((-s->avctx->height)>>LOG2_MB_SIZE);
1879 s->block= av_mallocz(w * h * sizeof(BlockNode) << (s->block_max_depth*2));
1883 static inline void copy_rac_state(RangeCoder *d, RangeCoder *s){
1884 uint8_t *bytestream= d->bytestream;
1885 uint8_t *bytestream_start= d->bytestream_start;
1887 d->bytestream= bytestream;
1888 d->bytestream_start= bytestream_start;
1891 //near copy & paste from dsputil, FIXME
1892 static int pix_sum(uint8_t * pix, int line_size, int w)
1897 for (i = 0; i < w; i++) {
1898 for (j = 0; j < w; j++) {
1902 pix += line_size - w;
1907 //near copy & paste from dsputil, FIXME
1908 static int pix_norm1(uint8_t * pix, int line_size, int w)
1911 uint32_t *sq = squareTbl + 256;
1914 for (i = 0; i < w; i++) {
1915 for (j = 0; j < w; j ++) {
1919 pix += line_size - w;
1924 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){
1925 const int w= s->b_width << s->block_max_depth;
1926 const int rem_depth= s->block_max_depth - level;
1927 const int index= (x + y*w) << rem_depth;
1928 const int block_w= 1<<rem_depth;
1940 for(j=0; j<block_w; j++){
1941 for(i=0; i<block_w; i++){
1942 s->block[index + i + j*w]= block;
1947 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){
1948 const int offset[3]= {
1950 ((y*c->uvstride + x)>>1),
1951 ((y*c->uvstride + x)>>1),
1955 c->src[0][i]= src [i];
1956 c->ref[0][i]= ref [i] + offset[i];
1964 #define P_TOPRIGHT P[3]
1965 #define P_MEDIAN P[4]
1967 #define FLAG_QPEL 1 //must be 1
1969 static int encode_q_branch(SnowContext *s, int level, int x, int y){
1970 uint8_t p_buffer[1024];
1971 uint8_t i_buffer[1024];
1972 uint8_t p_state[sizeof(s->block_state)];
1973 uint8_t i_state[sizeof(s->block_state)];
1975 uint8_t *pbbak= s->c.bytestream;
1976 uint8_t *pbbak_start= s->c.bytestream_start;
1977 int score, score2, iscore, i_len, p_len, block_s, sum;
1978 const int w= s->b_width << s->block_max_depth;
1979 const int h= s->b_height << s->block_max_depth;
1980 const int rem_depth= s->block_max_depth - level;
1981 const int index= (x + y*w) << rem_depth;
1982 const int block_w= 1<<(LOG2_MB_SIZE - level);
1983 static BlockNode null_block= { //FIXME add border maybe
1984 .color= {128,128,128},
1990 int trx= (x+1)<<rem_depth;
1991 int try= (y+1)<<rem_depth;
1992 BlockNode *left = x ? &s->block[index-1] : &null_block;
1993 BlockNode *top = y ? &s->block[index-w] : &null_block;
1994 BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
1995 BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
1996 BlockNode *tl = y && x ? &s->block[index-w-1] : left;
1997 BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
1998 int pl = left->color[0];
1999 int pcb= left->color[1];
2000 int pcr= left->color[2];
2001 int pmx= mid_pred(left->mx, top->mx, tr->mx);
2002 int pmy= mid_pred(left->my, top->my, tr->my);
2005 const int stride= s->current_picture.linesize[0];
2006 const int uvstride= s->current_picture.linesize[1];
2007 const int instride= s->input_picture.linesize[0];
2008 const int uvinstride= s->input_picture.linesize[1];
2009 uint8_t *new_l = s->input_picture.data[0] + (x + y* instride)*block_w;
2010 uint8_t *new_cb= s->input_picture.data[1] + (x + y*uvinstride)*block_w/2;
2011 uint8_t *new_cr= s->input_picture.data[2] + (x + y*uvinstride)*block_w/2;
2012 uint8_t current_mb[3][stride*block_w];
2013 uint8_t *current_data[3]= {¤t_mb[0][0], ¤t_mb[1][0], ¤t_mb[2][0]};
2015 int16_t last_mv[3][2];
2016 int qpel= !!(s->avctx->flags & CODEC_FLAG_QPEL); //unused
2017 const int shift= 1+qpel;
2018 MotionEstContext *c= &s->m.me;
2019 int mx_context= av_log2(2*ABS(left->mx - top->mx));
2020 int my_context= av_log2(2*ABS(left->my - top->my));
2021 int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
2023 assert(sizeof(s->block_state) >= 256);
2025 set_blocks(s, level, x, y, pl, pcb, pcr, pmx, pmy, BLOCK_INTRA);
2030 for(i=0; i<block_w; i++)
2031 memcpy(¤t_mb[0][0] + stride*i, new_l + instride*i, block_w);
2032 for(i=0; i<block_w>>1; i++)
2033 memcpy(¤t_mb[1][0] + uvstride*i, new_cb + uvinstride*i, block_w>>1);
2034 for(i=0; i<block_w>>1; i++)
2035 memcpy(¤t_mb[2][0] + uvstride*i, new_cr + uvinstride*i, block_w>>1);
2037 // clip predictors / edge ?
2039 P_LEFT[0]= left->mx;
2040 P_LEFT[1]= left->my;
2043 P_TOPRIGHT[0]= tr->mx;
2044 P_TOPRIGHT[1]= tr->my;
2046 last_mv[0][0]= s->block[index].mx;
2047 last_mv[0][1]= s->block[index].my;
2048 last_mv[1][0]= right->mx;
2049 last_mv[1][1]= right->my;
2050 last_mv[2][0]= bottom->mx;
2051 last_mv[2][1]= bottom->my;
2058 init_ref(c, current_data, s->last_picture.data, NULL, block_w*x, block_w*y, 0);
2060 assert(s->m.me. stride == stride);
2061 assert(s->m.me.uvstride == uvstride);
2063 c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
2064 c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
2065 c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
2066 c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_MV;
2068 c->xmin = - x*block_w - 16+2;
2069 c->ymin = - y*block_w - 16+2;
2070 c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-2;
2071 c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-2;
2073 if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
2074 if(P_LEFT[1] > (c->ymax<<shift)) P_LEFT[1] = (c->ymax<<shift);
2075 if(P_TOP[0] > (c->xmax<<shift)) P_TOP[0] = (c->xmax<<shift);
2076 if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
2077 if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
2078 if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); //due to pmx no clip
2079 if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
2081 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
2082 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
2085 c->pred_x= P_LEFT[0];
2086 c->pred_y= P_LEFT[1];
2088 c->pred_x = P_MEDIAN[0];
2089 c->pred_y = P_MEDIAN[1];
2092 score= ff_epzs_motion_search(&s->m, &mx, &my, P, 0, /*ref_index*/ 0, last_mv,
2093 (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
2095 assert(mx >= c->xmin);
2096 assert(mx <= c->xmax);
2097 assert(my >= c->ymin);
2098 assert(my <= c->ymax);
2100 score= s->m.me.sub_motion_search(&s->m, &mx, &my, score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
2101 score= ff_get_mb_score(&s->m, mx, my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
2102 //FIXME if mb_cmp != SSE then intra cant be compared currently and mb_penalty vs. lambda2
2106 pc.bytestream_start=
2107 pc.bytestream= p_buffer; //FIXME end/start? and at the other stoo
2108 memcpy(p_state, s->block_state, sizeof(s->block_state));
2110 if(level!=s->block_max_depth)
2111 put_rac(&pc, &p_state[4 + s_context], 1);
2112 put_rac(&pc, &p_state[1 + left->type + top->type], 0);
2113 put_symbol(&pc, &p_state[128 + 32*mx_context], mx - pmx, 1);
2114 put_symbol(&pc, &p_state[128 + 32*my_context], my - pmy, 1);
2115 p_len= pc.bytestream - pc.bytestream_start;
2116 score += (s->lambda2*(p_len*8
2117 + (pc.outstanding_count - s->c.outstanding_count)*8
2118 + (-av_log2(pc.range) + av_log2(s->c.range))
2119 ))>>FF_LAMBDA_SHIFT;
2121 block_s= block_w*block_w;
2122 sum = pix_sum(¤t_mb[0][0], stride, block_w);
2123 l= (sum + block_s/2)/block_s;
2124 iscore = pix_norm1(¤t_mb[0][0], stride, block_w) - 2*l*sum + l*l*block_s;
2126 block_s= block_w*block_w>>2;
2127 sum = pix_sum(¤t_mb[1][0], uvstride, block_w>>1);
2128 cb= (sum + block_s/2)/block_s;
2129 // iscore += pix_norm1(¤t_mb[1][0], uvstride, block_w>>1) - 2*cb*sum + cb*cb*block_s;
2130 sum = pix_sum(¤t_mb[2][0], uvstride, block_w>>1);
2131 cr= (sum + block_s/2)/block_s;
2132 // iscore += pix_norm1(¤t_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s;
2135 ic.bytestream_start=
2136 ic.bytestream= i_buffer; //FIXME end/start? and at the other stoo
2137 memcpy(i_state, s->block_state, sizeof(s->block_state));
2138 if(level!=s->block_max_depth)
2139 put_rac(&ic, &i_state[4 + s_context], 1);
2140 put_rac(&ic, &i_state[1 + left->type + top->type], 1);
2141 put_symbol(&ic, &i_state[32], l-pl , 1);
2142 put_symbol(&ic, &i_state[64], cb-pcb, 1);
2143 put_symbol(&ic, &i_state[96], cr-pcr, 1);
2144 i_len= ic.bytestream - ic.bytestream_start;
2145 iscore += (s->lambda2*(i_len*8
2146 + (ic.outstanding_count - s->c.outstanding_count)*8
2147 + (-av_log2(ic.range) + av_log2(s->c.range))
2148 ))>>FF_LAMBDA_SHIFT;
2150 // assert(score==256*256*256*64-1);
2151 assert(iscore < 255*255*256 + s->lambda2*10);
2152 assert(iscore >= 0);
2153 assert(l>=0 && l<=255);
2154 assert(pl>=0 && pl<=255);
2157 int varc= iscore >> 8;
2158 int vard= score >> 8;
2159 if (vard <= 64 || vard < varc)
2160 c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
2162 c->scene_change_score+= s->m.qscale;
2165 if(level!=s->block_max_depth){
2166 put_rac(&s->c, &s->block_state[4 + s_context], 0);
2167 score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0);
2168 score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0);
2169 score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1);
2170 score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1);
2171 score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead
2173 if(score2 < score && score2 < iscore)
2178 memcpy(pbbak, i_buffer, i_len);
2180 s->c.bytestream_start= pbbak_start;
2181 s->c.bytestream= pbbak + i_len;
2182 set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, BLOCK_INTRA);
2183 memcpy(s->block_state, i_state, sizeof(s->block_state));
2186 memcpy(pbbak, p_buffer, p_len);
2188 s->c.bytestream_start= pbbak_start;
2189 s->c.bytestream= pbbak + p_len;
2190 set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, 0);
2191 memcpy(s->block_state, p_state, sizeof(s->block_state));
2196 static void decode_q_branch(SnowContext *s, int level, int x, int y){
2197 const int w= s->b_width << s->block_max_depth;
2198 const int rem_depth= s->block_max_depth - level;
2199 const int index= (x + y*w) << rem_depth;
2200 static BlockNode null_block= { //FIXME add border maybe
2201 .color= {128,128,128},
2207 int trx= (x+1)<<rem_depth;
2208 BlockNode *left = x ? &s->block[index-1] : &null_block;
2209 BlockNode *top = y ? &s->block[index-w] : &null_block;
2210 BlockNode *tl = y && x ? &s->block[index-w-1] : left;
2211 BlockNode *tr = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
2212 int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
2215 set_blocks(s, level, x, y, null_block.color[0], null_block.color[1], null_block.color[2], null_block.mx, null_block.my, BLOCK_INTRA);
2219 if(level==s->block_max_depth || get_rac(&s->c, &s->block_state[4 + s_context])){
2221 int l = left->color[0];
2222 int cb= left->color[1];
2223 int cr= left->color[2];
2224 int mx= mid_pred(left->mx, top->mx, tr->mx);
2225 int my= mid_pred(left->my, top->my, tr->my);
2226 int mx_context= av_log2(2*ABS(left->mx - top->mx)) + 0*av_log2(2*ABS(tr->mx - top->mx));
2227 int my_context= av_log2(2*ABS(left->my - top->my)) + 0*av_log2(2*ABS(tr->my - top->my));
2229 type= get_rac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0;
2232 l += get_symbol(&s->c, &s->block_state[32], 1);
2233 cb+= get_symbol(&s->c, &s->block_state[64], 1);
2234 cr+= get_symbol(&s->c, &s->block_state[96], 1);
2236 mx+= get_symbol(&s->c, &s->block_state[128 + 32*mx_context], 1);
2237 my+= get_symbol(&s->c, &s->block_state[128 + 32*my_context], 1);
2239 set_blocks(s, level, x, y, l, cb, cr, mx, my, type);
2241 decode_q_branch(s, level+1, 2*x+0, 2*y+0);
2242 decode_q_branch(s, level+1, 2*x+1, 2*y+0);
2243 decode_q_branch(s, level+1, 2*x+0, 2*y+1);
2244 decode_q_branch(s, level+1, 2*x+1, 2*y+1);
2248 static void encode_blocks(SnowContext *s){
2254 if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){ //FIXME nicer limit
2255 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2259 encode_q_branch(s, 0, x, y);
2264 static void decode_blocks(SnowContext *s){
2271 decode_q_branch(s, 0, x, y);
2276 static void mc_block(uint8_t *dst, uint8_t *src, uint8_t *tmp, int stride, int b_w, int b_h, int dx, int dy){
2279 for(y=0; y < b_h+5; y++){
2280 for(x=0; x < b_w; x++){
2287 // int am= 9*(a1+a2) - (a0+a3);
2288 int am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
2289 // int am= 18*(a2+a3) - 2*(a1+a4);
2290 // int aL= (-7*a0 + 105*a1 + 35*a2 - 5*a3)>>3;
2291 // int aR= (-7*a3 + 105*a2 + 35*a1 - 5*a0)>>3;
2293 // if(b_w==16) am= 8*(a1+a2);
2295 if(dx<8) tmp[x]= (32*a2*( 8-dx) + am* dx + 128)>>8;
2296 else tmp[x]= ( am*(16-dx) + 32*a3*(dx-8) + 128)>>8;
2298 /* if (dx< 4) tmp[x + y*stride]= (16*a1*( 4-dx) + aL* dx + 32)>>6;
2299 else if(dx< 8) tmp[x + y*stride]= ( aL*( 8-dx) + am*(dx- 4) + 32)>>6;
2300 else if(dx<12) tmp[x + y*stride]= ( am*(12-dx) + aR*(dx- 8) + 32)>>6;
2301 else tmp[x + y*stride]= ( aR*(16-dx) + 16*a2*(dx-12) + 32)>>6;*/
2306 tmp -= (b_h+5)*stride;
2308 for(y=0; y < b_h; y++){
2309 for(x=0; x < b_w; x++){
2310 int a0= tmp[x + 0*stride];
2311 int a1= tmp[x + 1*stride];
2312 int a2= tmp[x + 2*stride];
2313 int a3= tmp[x + 3*stride];
2314 int a4= tmp[x + 4*stride];
2315 int a5= tmp[x + 5*stride];
2316 int am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
2317 // int am= 18*(a2+a3) - 2*(a1+a4);
2318 /* int aL= (-7*a0 + 105*a1 + 35*a2 - 5*a3)>>3;
2319 int aR= (-7*a3 + 105*a2 + 35*a1 - 5*a0)>>3;*/
2321 // if(b_w==16) am= 8*(a1+a2);
2323 if(dy<8) dst[x]= (32*a2*( 8-dy) + am* dy + 128)>>8;
2324 else dst[x]= ( am*(16-dy) + 32*a3*(dy-8) + 128)>>8;
2326 /* if (dy< 4) tmp[x + y*stride]= (16*a1*( 4-dy) + aL* dy + 32)>>6;
2327 else if(dy< 8) tmp[x + y*stride]= ( aL*( 8-dy) + am*(dy- 4) + 32)>>6;
2328 else if(dy<12) tmp[x + y*stride]= ( am*(12-dy) + aR*(dy- 8) + 32)>>6;
2329 else tmp[x + y*stride]= ( aR*(16-dy) + 16*a2*(dy-12) + 32)>>6;*/
2334 STOP_TIMER("mc_block")
2337 #define mca(dx,dy,b_w)\
2338 static void mc_block_hpel ## dx ## dy ## b_w(uint8_t *dst, uint8_t *src, int stride, int h){\
2339 uint8_t tmp[stride*(b_w+5)];\
2341 mc_block(dst, src-2-2*stride, tmp, stride, b_w, b_w, dx, dy);\
2353 static void pred_block(SnowContext *s, uint8_t *dst, uint8_t *src, uint8_t *tmp, int stride, int sx, int sy, int b_w, int b_h, BlockNode *block, int plane_index, int w, int h){
2356 const int color= block->color[plane_index];
2357 for(y=0; y < b_h; y++){
2358 for(x=0; x < b_w; x++){
2359 dst[x + y*stride]= color;
2363 const int scale= plane_index ? s->mv_scale : 2*s->mv_scale;
2364 int mx= block->mx*scale;
2365 int my= block->my*scale;
2366 const int dx= mx&15;
2367 const int dy= my&15;
2370 src += sx + sy*stride;
2371 if( (unsigned)sx >= w - b_w - 4
2372 || (unsigned)sy >= h - b_h - 4){
2373 ff_emulated_edge_mc(tmp + MB_SIZE, src, stride, b_w+5, b_h+5, sx, sy, w, h);
2376 if((dx&3) || (dy&3) || b_w!=b_h || (b_w!=4 && b_w!=8 && b_w!=16))
2377 mc_block(dst, src, tmp, stride, b_w, b_h, dx, dy);
2379 s->dsp.put_h264_qpel_pixels_tab[2-(b_w>>3)][dy+(dx>>2)](dst,src + 2 + 2*stride,stride);
2383 static always_inline int same_block(BlockNode *a, BlockNode *b){
2384 return !((a->mx - b->mx) | (a->my - b->my) | a->type | b->type);
2387 //FIXME name clenup (b_w, block_w, b_width stuff)
2388 static always_inline void add_yblock_buffered(SnowContext *s, slice_buffer * sb, DWTELEM *old_dst, uint8_t *dst8, uint8_t *src, uint8_t *obmc, int src_x, int src_y, int b_w, int b_h, int w, int h, int dst_stride, int src_stride, int obmc_stride, int b_x, int b_y, int add, int plane_index){
2389 DWTELEM * dst = NULL;
2390 const int b_width = s->b_width << s->block_max_depth;
2391 const int b_height= s->b_height << s->block_max_depth;
2392 const int b_stride= b_width;
2393 BlockNode *lt= &s->block[b_x + b_y*b_stride];
2394 BlockNode *rt= lt+1;
2395 BlockNode *lb= lt+b_stride;
2396 BlockNode *rb= lb+1;
2398 uint8_t tmp[src_stride*(b_h+5)]; //FIXME align
2404 }else if(b_x + 1 >= b_width){
2411 }else if(b_y + 1 >= b_height){
2416 if(src_x<0){ //FIXME merge with prev & always round internal width upto *16
2420 }else if(src_x + b_w > w){
2424 obmc -= src_y*obmc_stride;
2427 }else if(src_y + b_h> h){
2431 if(b_w<=0 || b_h<=0) return;
2433 assert(src_stride > 7*MB_SIZE);
2434 // old_dst += src_x + src_y*dst_stride;
2435 dst8+= src_x + src_y*src_stride;
2436 // src += src_x + src_y*src_stride;
2438 block[0]= tmp+3*MB_SIZE;
2439 pred_block(s, block[0], src, tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
2441 if(same_block(lt, rt)){
2444 block[1]= tmp + 4*MB_SIZE;
2445 pred_block(s, block[1], src, tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
2448 if(same_block(lt, lb)){
2450 }else if(same_block(rt, lb)){
2453 block[2]= tmp+5*MB_SIZE;
2454 pred_block(s, block[2], src, tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
2457 if(same_block(lt, rb) ){
2459 }else if(same_block(rt, rb)){
2461 }else if(same_block(lb, rb)){
2464 block[3]= tmp+6*MB_SIZE;
2465 pred_block(s, block[3], src, tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
2468 for(y=0; y<b_h; y++){
2469 for(x=0; x<b_w; x++){
2470 int v= obmc [x + y*obmc_stride] * block[3][x + y*src_stride] * (256/OBMC_MAX);
2471 if(add) dst[x + y*dst_stride] += v;
2472 else dst[x + y*dst_stride] -= v;
2475 for(y=0; y<b_h; y++){
2476 uint8_t *obmc2= obmc + (obmc_stride>>1);
2477 for(x=0; x<b_w; x++){
2478 int v= obmc2[x + y*obmc_stride] * block[2][x + y*src_stride] * (256/OBMC_MAX);
2479 if(add) dst[x + y*dst_stride] += v;
2480 else dst[x + y*dst_stride] -= v;
2483 for(y=0; y<b_h; y++){
2484 uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
2485 for(x=0; x<b_w; x++){
2486 int v= obmc3[x + y*obmc_stride] * block[1][x + y*src_stride] * (256/OBMC_MAX);
2487 if(add) dst[x + y*dst_stride] += v;
2488 else dst[x + y*dst_stride] -= v;
2491 for(y=0; y<b_h; y++){
2492 uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
2493 uint8_t *obmc4= obmc3+ (obmc_stride>>1);
2494 for(x=0; x<b_w; x++){
2495 int v= obmc4[x + y*obmc_stride] * block[0][x + y*src_stride] * (256/OBMC_MAX);
2496 if(add) dst[x + y*dst_stride] += v;
2497 else dst[x + y*dst_stride] -= v;
2505 int block_index = 0;
2506 for(y=0; y<b_h; y++){
2507 //FIXME ugly missue of obmc_stride
2508 uint8_t *obmc1= obmc + y*obmc_stride;
2509 uint8_t *obmc2= obmc1+ (obmc_stride>>1);
2510 uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
2511 uint8_t *obmc4= obmc3+ (obmc_stride>>1);
2512 dst = slice_buffer_get_line(sb, src_y + y);
2513 for(x=0; x<b_w; x++){
2514 int v= obmc1[x] * block[3][x + y*src_stride]
2515 +obmc2[x] * block[2][x + y*src_stride]
2516 +obmc3[x] * block[1][x + y*src_stride]
2517 +obmc4[x] * block[0][x + y*src_stride];
2519 v <<= 8 - LOG2_OBMC_MAX;
2521 v += 1<<(7 - FRAC_BITS);
2522 v >>= 8 - FRAC_BITS;
2525 // v += old_dst[x + y*dst_stride];
2526 v += dst[x + src_x];
2527 v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
2528 if(v&(~255)) v= ~(v>>31);
2529 dst8[x + y*src_stride] = v;
2531 // old_dst[x + y*dst_stride] -= v;
2532 dst[x + src_x] -= v;
2536 STOP_TIMER("Inner add y block")
2541 //FIXME name clenup (b_w, block_w, b_width stuff)
2542 static always_inline void add_yblock(SnowContext *s, DWTELEM *dst, uint8_t *dst8, uint8_t *src, uint8_t *obmc, int src_x, int src_y, int b_w, int b_h, int w, int h, int dst_stride, int src_stride, int obmc_stride, int b_x, int b_y, int add, int plane_index){
2543 const int b_width = s->b_width << s->block_max_depth;
2544 const int b_height= s->b_height << s->block_max_depth;
2545 const int b_stride= b_width;
2546 BlockNode *lt= &s->block[b_x + b_y*b_stride];
2547 BlockNode *rt= lt+1;
2548 BlockNode *lb= lt+b_stride;
2549 BlockNode *rb= lb+1;
2551 uint8_t tmp[src_stride*(b_h+5)]; //FIXME align
2557 }else if(b_x + 1 >= b_width){
2564 }else if(b_y + 1 >= b_height){
2569 if(src_x<0){ //FIXME merge with prev & always round internal width upto *16
2573 }else if(src_x + b_w > w){
2577 obmc -= src_y*obmc_stride;
2580 }else if(src_y + b_h> h){
2584 if(b_w<=0 || b_h<=0) return;
2586 assert(src_stride > 7*MB_SIZE);
2587 dst += src_x + src_y*dst_stride;
2588 dst8+= src_x + src_y*src_stride;
2589 // src += src_x + src_y*src_stride;
2591 block[0]= tmp+3*MB_SIZE;
2592 pred_block(s, block[0], src, tmp, src_stride, src_x, src_y, b_w, b_h, lt, plane_index, w, h);
2594 if(same_block(lt, rt)){
2597 block[1]= tmp + 4*MB_SIZE;
2598 pred_block(s, block[1], src, tmp, src_stride, src_x, src_y, b_w, b_h, rt, plane_index, w, h);
2601 if(same_block(lt, lb)){
2603 }else if(same_block(rt, lb)){
2606 block[2]= tmp+5*MB_SIZE;
2607 pred_block(s, block[2], src, tmp, src_stride, src_x, src_y, b_w, b_h, lb, plane_index, w, h);
2610 if(same_block(lt, rb) ){
2612 }else if(same_block(rt, rb)){
2614 }else if(same_block(lb, rb)){
2617 block[3]= tmp+6*MB_SIZE;
2618 pred_block(s, block[3], src, tmp, src_stride, src_x, src_y, b_w, b_h, rb, plane_index, w, h);
2621 for(y=0; y<b_h; y++){
2622 for(x=0; x<b_w; x++){
2623 int v= obmc [x + y*obmc_stride] * block[3][x + y*src_stride] * (256/OBMC_MAX);
2624 if(add) dst[x + y*dst_stride] += v;
2625 else dst[x + y*dst_stride] -= v;
2628 for(y=0; y<b_h; y++){
2629 uint8_t *obmc2= obmc + (obmc_stride>>1);
2630 for(x=0; x<b_w; x++){
2631 int v= obmc2[x + y*obmc_stride] * block[2][x + y*src_stride] * (256/OBMC_MAX);
2632 if(add) dst[x + y*dst_stride] += v;
2633 else dst[x + y*dst_stride] -= v;
2636 for(y=0; y<b_h; y++){
2637 uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
2638 for(x=0; x<b_w; x++){
2639 int v= obmc3[x + y*obmc_stride] * block[1][x + y*src_stride] * (256/OBMC_MAX);
2640 if(add) dst[x + y*dst_stride] += v;
2641 else dst[x + y*dst_stride] -= v;
2644 for(y=0; y<b_h; y++){
2645 uint8_t *obmc3= obmc + obmc_stride*(obmc_stride>>1);
2646 uint8_t *obmc4= obmc3+ (obmc_stride>>1);
2647 for(x=0; x<b_w; x++){
2648 int v= obmc4[x + y*obmc_stride] * block[0][x + y*src_stride] * (256/OBMC_MAX);
2649 if(add) dst[x + y*dst_stride] += v;
2650 else dst[x + y*dst_stride] -= v;
2654 for(y=0; y<b_h; y++){
2655 //FIXME ugly missue of obmc_stride
2656 uint8_t *obmc1= obmc + y*obmc_stride;
2657 uint8_t *obmc2= obmc1+ (obmc_stride>>1);
2658 uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
2659 uint8_t *obmc4= obmc3+ (obmc_stride>>1);
2660 for(x=0; x<b_w; x++){
2661 int v= obmc1[x] * block[3][x + y*src_stride]
2662 +obmc2[x] * block[2][x + y*src_stride]
2663 +obmc3[x] * block[1][x + y*src_stride]
2664 +obmc4[x] * block[0][x + y*src_stride];
2666 v <<= 8 - LOG2_OBMC_MAX;
2668 v += 1<<(7 - FRAC_BITS);
2669 v >>= 8 - FRAC_BITS;
2672 v += dst[x + y*dst_stride];
2673 v = (v + (1<<(FRAC_BITS-1))) >> FRAC_BITS;
2674 if(v&(~255)) v= ~(v>>31);
2675 dst8[x + y*src_stride] = v;
2677 dst[x + y*dst_stride] -= v;
2684 static always_inline void predict_slice_buffered(SnowContext *s, slice_buffer * sb, DWTELEM * old_buffer, int plane_index, int add, int mb_y){
2685 Plane *p= &s->plane[plane_index];
2686 const int mb_w= s->b_width << s->block_max_depth;
2687 const int mb_h= s->b_height << s->block_max_depth;
2689 int block_size = MB_SIZE >> s->block_max_depth;
2690 int block_w = plane_index ? block_size/2 : block_size;
2691 const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
2692 int obmc_stride= plane_index ? block_size : 2*block_size;
2693 int ref_stride= s->current_picture.linesize[plane_index];
2694 uint8_t *ref = s->last_picture.data[plane_index];
2695 uint8_t *dst8= s->current_picture.data[plane_index];
2700 if(s->keyframe || (s->avctx->debug&512)){
2705 for(y=block_w*mb_y; y<block_w*(mb_y+1); y++)
2707 // DWTELEM * line = slice_buffer_get_line(sb, y);
2708 DWTELEM * line = sb->line[y];
2711 // int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
2712 int v= line[x] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
2714 if(v&(~255)) v= ~(v>>31);
2715 dst8[x + y*ref_stride]= v;
2719 for(y=block_w*mb_y; y<block_w*(mb_y+1); y++)
2721 // DWTELEM * line = slice_buffer_get_line(sb, y);
2722 DWTELEM * line = sb->line[y];
2725 line[x] -= 128 << FRAC_BITS;
2726 // buf[x + y*w]-= 128<<FRAC_BITS;
2734 for(mb_x=0; mb_x<=mb_w; mb_x++){
2737 add_yblock_buffered(s, sb, old_buffer, dst8, ref, obmc,
2738 block_w*mb_x - block_w/2,
2739 block_w*mb_y - block_w/2,
2742 w, ref_stride, obmc_stride,
2746 STOP_TIMER("add_yblock")
2749 STOP_TIMER("predict_slice")
2752 static always_inline void predict_slice(SnowContext *s, DWTELEM *buf, int plane_index, int add, int mb_y){
2753 Plane *p= &s->plane[plane_index];
2754 const int mb_w= s->b_width << s->block_max_depth;
2755 const int mb_h= s->b_height << s->block_max_depth;
2757 int block_size = MB_SIZE >> s->block_max_depth;
2758 int block_w = plane_index ? block_size/2 : block_size;
2759 const uint8_t *obmc = plane_index ? obmc_tab[s->block_max_depth+1] : obmc_tab[s->block_max_depth];
2760 int obmc_stride= plane_index ? block_size : 2*block_size;
2761 int ref_stride= s->current_picture.linesize[plane_index];
2762 uint8_t *ref = s->last_picture.data[plane_index];
2763 uint8_t *dst8= s->current_picture.data[plane_index];
2768 if(s->keyframe || (s->avctx->debug&512)){
2773 for(y=block_w*mb_y; y<block_w*(mb_y+1); y++){
2775 int v= buf[x + y*w] + (128<<FRAC_BITS) + (1<<(FRAC_BITS-1));
2777 if(v&(~255)) v= ~(v>>31);
2778 dst8[x + y*ref_stride]= v;
2782 for(y=block_w*mb_y; y<block_w*(mb_y+1); y++){
2784 buf[x + y*w]-= 128<<FRAC_BITS;
2792 for(mb_x=0; mb_x<=mb_w; mb_x++){
2795 add_yblock(s, buf, dst8, ref, obmc,
2796 block_w*mb_x - block_w/2,
2797 block_w*mb_y - block_w/2,
2800 w, ref_stride, obmc_stride,
2804 STOP_TIMER("add_yblock")
2807 STOP_TIMER("predict_slice")
2810 static always_inline void predict_plane(SnowContext *s, DWTELEM *buf, int plane_index, int add){
2811 const int mb_h= s->b_height << s->block_max_depth;
2813 for(mb_y=0; mb_y<=mb_h; mb_y++)
2814 predict_slice(s, buf, plane_index, add, mb_y);
2817 static void quantize(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int bias){
2818 const int level= b->level;
2819 const int w= b->width;
2820 const int h= b->height;
2821 const int qlog= clip(s->qlog + b->qlog, 0, 128);
2822 const int qmul= qexp[qlog&7]<<(qlog>>3);
2823 int x,y, thres1, thres2;
2828 if(s->qlog == LOSSLESS_QLOG) return;
2830 bias= bias ? 0 : (3*qmul)>>3;
2831 thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
2837 int i= src[x + y*stride];
2839 if((unsigned)(i+thres1) > thres2){
2842 i/= qmul; //FIXME optimize
2843 src[x + y*stride]= i;
2847 i/= qmul; //FIXME optimize
2848 src[x + y*stride]= -i;
2851 src[x + y*stride]= 0;
2857 int i= src[x + y*stride];
2859 if((unsigned)(i+thres1) > thres2){
2862 i= (i + bias) / qmul; //FIXME optimize
2863 src[x + y*stride]= i;
2867 i= (i + bias) / qmul; //FIXME optimize
2868 src[x + y*stride]= -i;
2871 src[x + y*stride]= 0;
2875 if(level+1 == s->spatial_decomposition_count){
2876 // STOP_TIMER("quantize")
2880 static void dequantize_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, DWTELEM *src, int stride){
2881 const int w= b->width;
2882 const int h= b->height;
2883 const int qlog= clip(s->qlog + b->qlog, 0, 128);
2884 const int qmul= qexp[qlog&7]<<(qlog>>3);
2885 const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
2889 if(s->qlog == LOSSLESS_QLOG) return;
2894 // DWTELEM * line = slice_buffer_get_line_from_address(sb, src + (y * stride));
2895 DWTELEM * line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
2899 line[x]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
2901 line[x]= (( i*qmul + qadd)>>(QEXPSHIFT));
2905 if(w > 200 /*level+1 == s->spatial_decomposition_count*/){
2906 STOP_TIMER("dquant")
2910 static void dequantize(SnowContext *s, SubBand *b, DWTELEM *src, int stride){
2911 const int w= b->width;
2912 const int h= b->height;
2913 const int qlog= clip(s->qlog + b->qlog, 0, 128);
2914 const int qmul= qexp[qlog&7]<<(qlog>>3);
2915 const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
2919 if(s->qlog == LOSSLESS_QLOG) return;
2925 int i= src[x + y*stride];
2927 src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
2929 src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
2933 if(w > 200 /*level+1 == s->spatial_decomposition_count*/){
2934 STOP_TIMER("dquant")
2938 static void decorrelate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
2939 const int w= b->width;
2940 const int h= b->height;
2943 for(y=h-1; y>=0; y--){
2944 for(x=w-1; x>=0; x--){
2945 int i= x + y*stride;
2949 if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
2950 else src[i] -= src[i - 1];
2952 if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
2953 else src[i] -= src[i - 1];
2956 if(y) src[i] -= src[i - stride];
2962 static void correlate_buffered(SnowContext *s, slice_buffer * sb, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
2963 const int w= b->width;
2964 const int h= b->height;
2974 // line = slice_buffer_get_line_from_address(sb, src + (y * stride));
2975 line = slice_buffer_get_line(sb, (y * b->stride_line) + b->buf_y_offset) + b->buf_x_offset;
2979 if(y && x+1<w) line[x] += mid_pred(line[x - 1], prev[x], prev[x + 1]);
2980 else line[x] += line[x - 1];
2982 if(y) line[x] += mid_pred(line[x - 1], prev[x], line[x - 1] + prev[x] - prev[x - 1]);
2983 else line[x] += line[x - 1];
2986 if(y) line[x] += prev[x];
2991 // STOP_TIMER("correlate")
2994 static void correlate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
2995 const int w= b->width;
2996 const int h= b->height;
3001 int i= x + y*stride;
3005 if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
3006 else src[i] += src[i - 1];
3008 if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
3009 else src[i] += src[i - 1];
3012 if(y) src[i] += src[i - stride];
3018 static void encode_header(SnowContext *s){
3019 int plane_index, level, orientation;
3022 memset(kstate, MID_STATE, sizeof(kstate));
3024 put_rac(&s->c, kstate, s->keyframe);
3025 if(s->keyframe || s->always_reset)
3028 put_symbol(&s->c, s->header_state, s->version, 0);
3029 put_rac(&s->c, s->header_state, s->always_reset);
3030 put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
3031 put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
3032 put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
3033 put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
3034 put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
3035 put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
3036 put_rac(&s->c, s->header_state, s->spatial_scalability);
3037 // put_rac(&s->c, s->header_state, s->rate_scalability);
3039 for(plane_index=0; plane_index<2; plane_index++){
3040 for(level=0; level<s->spatial_decomposition_count; level++){
3041 for(orientation=level ? 1:0; orientation<4; orientation++){
3042 if(orientation==2) continue;
3043 put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
3048 put_symbol(&s->c, s->header_state, s->spatial_decomposition_type, 0);
3049 put_symbol(&s->c, s->header_state, s->qlog, 1);
3050 put_symbol(&s->c, s->header_state, s->mv_scale, 0);
3051 put_symbol(&s->c, s->header_state, s->qbias, 1);
3052 put_symbol(&s->c, s->header_state, s->block_max_depth, 0);
3055 static int decode_header(SnowContext *s){
3056 int plane_index, level, orientation;
3059 memset(kstate, MID_STATE, sizeof(kstate));
3061 s->keyframe= get_rac(&s->c, kstate);
3062 if(s->keyframe || s->always_reset)
3065 s->version= get_symbol(&s->c, s->header_state, 0);
3067 av_log(s->avctx, AV_LOG_ERROR, "version %d not supported", s->version);
3070 s->always_reset= get_rac(&s->c, s->header_state);
3071 s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0);
3072 s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0);
3073 s->spatial_decomposition_count= get_symbol(&s->c, s->header_state, 0);
3074 s->colorspace_type= get_symbol(&s->c, s->header_state, 0);
3075 s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0);
3076 s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0);
3077 s->spatial_scalability= get_rac(&s->c, s->header_state);
3078 // s->rate_scalability= get_rac(&s->c, s->header_state);
3080 for(plane_index=0; plane_index<3; plane_index++){
3081 for(level=0; level<s->spatial_decomposition_count; level++){
3082 for(orientation=level ? 1:0; orientation<4; orientation++){
3084 if (plane_index==2) q= s->plane[1].band[level][orientation].qlog;
3085 else if(orientation==2) q= s->plane[plane_index].band[level][1].qlog;
3086 else q= get_symbol(&s->c, s->header_state, 1);
3087 s->plane[plane_index].band[level][orientation].qlog= q;
3093 s->spatial_decomposition_type= get_symbol(&s->c, s->header_state, 0);
3094 if(s->spatial_decomposition_type > 2){
3095 av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported", s->spatial_decomposition_type);
3099 s->qlog= get_symbol(&s->c, s->header_state, 1);
3100 s->mv_scale= get_symbol(&s->c, s->header_state, 0);
3101 s->qbias= get_symbol(&s->c, s->header_state, 1);
3102 s->block_max_depth= get_symbol(&s->c, s->header_state, 0);
3107 static int common_init(AVCodecContext *avctx){
3108 SnowContext *s = avctx->priv_data;
3110 int level, orientation, plane_index, dec;
3114 dsputil_init(&s->dsp, avctx);
3117 s->dsp.put_qpel_pixels_tab [0][dy+dx/4]=\
3118 s->dsp.put_no_rnd_qpel_pixels_tab[0][dy+dx/4]=\
3119 s->dsp.put_h264_qpel_pixels_tab[0][dy+dx/4];\
3120 s->dsp.put_qpel_pixels_tab [1][dy+dx/4]=\
3121 s->dsp.put_no_rnd_qpel_pixels_tab[1][dy+dx/4]=\
3122 s->dsp.put_h264_qpel_pixels_tab[1][dy+dx/4];
3141 #define mcfh(dx,dy)\
3142 s->dsp.put_pixels_tab [0][dy/4+dx/8]=\
3143 s->dsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
3144 mc_block_hpel ## dx ## dy ## 16;\
3145 s->dsp.put_pixels_tab [1][dy/4+dx/8]=\
3146 s->dsp.put_no_rnd_pixels_tab[1][dy/4+dx/8]=\
3147 mc_block_hpel ## dx ## dy ## 8;
3154 dec= s->spatial_decomposition_count= 5;
3155 s->spatial_decomposition_type= avctx->prediction_method; //FIXME add decorrelator type r transform_type
3157 s->chroma_h_shift= 1; //FIXME XXX
3158 s->chroma_v_shift= 1;
3160 // dec += FFMAX(s->chroma_h_shift, s->chroma_v_shift);
3162 width= s->avctx->width;
3163 height= s->avctx->height;
3165 s->spatial_dwt_buffer= av_mallocz(width*height*sizeof(DWTELEM));
3167 s->mv_scale= (s->avctx->flags & CODEC_FLAG_QPEL) ? 2 : 4;
3168 s->block_max_depth= (s->avctx->flags & CODEC_FLAG_4MV) ? 1 : 0;
3170 for(plane_index=0; plane_index<3; plane_index++){
3171 int w= s->avctx->width;
3172 int h= s->avctx->height;
3175 w>>= s->chroma_h_shift;
3176 h>>= s->chroma_v_shift;
3178 s->plane[plane_index].width = w;
3179 s->plane[plane_index].height= h;
3180 //av_log(NULL, AV_LOG_DEBUG, "%d %d\n", w, h);
3181 for(level=s->spatial_decomposition_count-1; level>=0; level--){
3182 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3183 SubBand *b= &s->plane[plane_index].band[level][orientation];
3185 b->buf= s->spatial_dwt_buffer;
3187 b->stride= s->plane[plane_index].width << (s->spatial_decomposition_count - level);
3188 b->width = (w + !(orientation&1))>>1;
3189 b->height= (h + !(orientation>1))>>1;
3191 b->stride_line = 1 << (s->spatial_decomposition_count - level);
3192 b->buf_x_offset = 0;
3193 b->buf_y_offset = 0;
3197 b->buf_x_offset = (w+1)>>1;
3200 b->buf += b->stride>>1;
3201 b->buf_y_offset = b->stride_line >> 1;
3205 b->parent= &s->plane[plane_index].band[level-1][orientation];
3206 b->x_coeff=av_mallocz(((b->width+1) * b->height+1)*sizeof(x_and_coeff));
3215 width= s->width= avctx->width;
3216 height= s->height= avctx->height;
3218 assert(width && height);
3220 s->avctx->get_buffer(s->avctx, &s->mconly_picture);
3226 static void calculate_vissual_weight(SnowContext *s, Plane *p){
3227 int width = p->width;
3228 int height= p->height;
3229 int level, orientation, x, y;
3231 for(level=0; level<s->spatial_decomposition_count; level++){
3232 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3233 SubBand *b= &p->band[level][orientation];
3234 DWTELEM *buf= b->buf;
3237 memset(s->spatial_dwt_buffer, 0, sizeof(int)*width*height);
3238 buf[b->width/2 + b->height/2*b->stride]= 256*256;
3239 ff_spatial_idwt(s->spatial_dwt_buffer, width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
3240 for(y=0; y<height; y++){
3241 for(x=0; x<width; x++){
3242 int64_t d= s->spatial_dwt_buffer[x + y*width];
3247 b->qlog= (int)(log(352256.0/sqrt(error)) / log(pow(2.0, 1.0/QROOT))+0.5);
3248 // av_log(NULL, AV_LOG_DEBUG, "%d %d %d\n", level, orientation, b->qlog/*, sqrt(error)*/);
3253 static int encode_init(AVCodecContext *avctx)
3255 SnowContext *s = avctx->priv_data;
3258 if(avctx->strict_std_compliance >= 0){
3259 av_log(avctx, AV_LOG_ERROR, "this codec is under development, files encoded with it wont be decodeable with future versions!!!\n"
3260 "use vstrict=-1 / -strict -1 to use it anyway\n");
3269 s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
3270 s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
3271 s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
3272 h263_encode_init(&s->m); //mv_penalty
3274 for(plane_index=0; plane_index<3; plane_index++){
3275 calculate_vissual_weight(s, &s->plane[plane_index]);
3279 avctx->coded_frame= &s->current_picture;
3280 switch(avctx->pix_fmt){
3281 // case PIX_FMT_YUV444P:
3282 // case PIX_FMT_YUV422P:
3283 case PIX_FMT_YUV420P:
3285 // case PIX_FMT_YUV411P:
3286 // case PIX_FMT_YUV410P:
3287 s->colorspace_type= 0;
3289 /* case PIX_FMT_RGBA32:
3293 av_log(avctx, AV_LOG_ERROR, "format not supported\n");
3296 // avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
3297 s->chroma_h_shift= 1;
3298 s->chroma_v_shift= 1;
3302 static int frame_start(SnowContext *s){
3304 int w= s->avctx->width; //FIXME round up to x16 ?
3305 int h= s->avctx->height;
3307 if(s->current_picture.data[0]){
3308 draw_edges(s->current_picture.data[0], s->current_picture.linesize[0], w , h , EDGE_WIDTH );
3309 draw_edges(s->current_picture.data[1], s->current_picture.linesize[1], w>>1, h>>1, EDGE_WIDTH/2);
3310 draw_edges(s->current_picture.data[2], s->current_picture.linesize[2], w>>1, h>>1, EDGE_WIDTH/2);
3313 tmp= s->last_picture;
3314 s->last_picture= s->current_picture;
3315 s->current_picture= tmp;
3317 s->current_picture.reference= 1;
3318 if(s->avctx->get_buffer(s->avctx, &s->current_picture) < 0){
3319 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
3326 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
3327 SnowContext *s = avctx->priv_data;
3328 RangeCoder * const c= &s->c;
3329 AVFrame *pict = data;
3330 const int width= s->avctx->width;
3331 const int height= s->avctx->height;
3332 int level, orientation, plane_index;
3334 ff_init_range_encoder(c, buf, buf_size);
3335 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
3337 s->input_picture = *pict;
3339 s->keyframe=avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
3340 pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE;
3343 s->qlog= rint(QROOT*log(pict->quality / (float)FF_QP2LAMBDA)/log(2));
3347 s->qlog= LOSSLESS_QLOG;
3351 s->current_picture.key_frame= s->keyframe;
3353 if(pict->pict_type == P_TYPE){
3354 int block_width = (width +15)>>4;
3355 int block_height= (height+15)>>4;
3356 int stride= s->current_picture.linesize[0];
3358 assert(s->current_picture.data[0]);
3359 assert(s->last_picture.data[0]);
3361 s->m.avctx= s->avctx;
3362 s->m.current_picture.data[0]= s->current_picture.data[0];
3363 s->m. last_picture.data[0]= s-> last_picture.data[0];
3364 s->m. new_picture.data[0]= s-> input_picture.data[0];
3365 s->m.current_picture_ptr= &s->m.current_picture;
3366 s->m. last_picture_ptr= &s->m. last_picture;
3368 s->m. last_picture.linesize[0]=
3369 s->m. new_picture.linesize[0]=
3370 s->m.current_picture.linesize[0]= stride;
3371 s->m.uvlinesize= s->current_picture.linesize[1];
3373 s->m.height= height;
3374 s->m.mb_width = block_width;
3375 s->m.mb_height= block_height;
3376 s->m.mb_stride= s->m.mb_width+1;
3377 s->m.b8_stride= 2*s->m.mb_width+1;
3379 s->m.pict_type= pict->pict_type;
3380 s->m.me_method= s->avctx->me_method;
3381 s->m.me.scene_change_score=0;
3382 s->m.flags= s->avctx->flags;
3383 s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0;
3384 s->m.out_format= FMT_H263;
3385 s->m.unrestricted_mv= 1;
3387 s->lambda = s->m.lambda= pict->quality * 3/2; //FIXME bug somewhere else
3388 s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
3389 s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
3391 s->m.dsp= s->dsp; //move
3397 s->qbias= pict->pict_type == P_TYPE ? 2 : 0;
3402 for(plane_index=0; plane_index<3; plane_index++){
3403 Plane *p= &s->plane[plane_index];
3407 // int bits= put_bits_count(&s->c.pb);
3410 if(pict->data[plane_index]) //FIXME gray hack
3413 s->spatial_dwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<FRAC_BITS;
3416 predict_plane(s, s->spatial_dwt_buffer, plane_index, 0);
3419 && pict->pict_type == P_TYPE
3420 && s->m.me.scene_change_score > s->avctx->scenechange_threshold){
3421 ff_init_range_encoder(c, buf, buf_size);
3422 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
3423 pict->pict_type= FF_I_TYPE;
3429 if(s->qlog == LOSSLESS_QLOG){
3432 s->spatial_dwt_buffer[y*w + x]= (s->spatial_dwt_buffer[y*w + x] + (1<<(FRAC_BITS-1)))>>FRAC_BITS;
3437 ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
3439 for(level=0; level<s->spatial_decomposition_count; level++){
3440 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3441 SubBand *b= &p->band[level][orientation];
3443 quantize(s, b, b->buf, b->stride, s->qbias);
3445 decorrelate(s, b, b->buf, b->stride, pict->pict_type == P_TYPE, 0);
3446 encode_subband(s, b, b->buf, b->parent ? b->parent->buf : NULL, b->stride, orientation);
3447 assert(b->parent==NULL || b->parent->stride == b->stride*2);
3449 correlate(s, b, b->buf, b->stride, 1, 0);
3452 // av_log(NULL, AV_LOG_DEBUG, "plane:%d bits:%d\n", plane_index, put_bits_count(&s->c.pb) - bits);
3454 for(level=0; level<s->spatial_decomposition_count; level++){
3455 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3456 SubBand *b= &p->band[level][orientation];
3458 dequantize(s, b, b->buf, b->stride);
3462 ff_spatial_idwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
3463 if(s->qlog == LOSSLESS_QLOG){
3466 s->spatial_dwt_buffer[y*w + x]<<=FRAC_BITS;
3471 predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
3472 STOP_TIMER("pred-conv")}
3473 if(s->avctx->flags&CODEC_FLAG_PSNR){
3476 if(pict->data[plane_index]) //FIXME gray hack
3479 int d= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x] - pict->data[plane_index][y*pict->linesize[plane_index] + x];
3483 s->avctx->error[plane_index] += error;
3484 s->current_picture.error[plane_index] = error;
3488 if(s->last_picture.data[0])
3489 avctx->release_buffer(avctx, &s->last_picture);
3493 return ff_rac_terminate(c);
3496 static void common_end(SnowContext *s){
3497 int plane_index, level, orientation;
3499 av_freep(&s->spatial_dwt_buffer);
3501 av_freep(&s->m.me.scratchpad);
3502 av_freep(&s->m.me.map);
3503 av_freep(&s->m.me.score_map);
3505 av_freep(&s->block);
3507 for(plane_index=0; plane_index<3; plane_index++){
3508 for(level=s->spatial_decomposition_count-1; level>=0; level--){
3509 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3510 SubBand *b= &s->plane[plane_index].band[level][orientation];
3512 av_freep(&b->x_coeff);
3518 static int encode_end(AVCodecContext *avctx)
3520 SnowContext *s = avctx->priv_data;
3527 static int decode_init(AVCodecContext *avctx)
3529 SnowContext *s = avctx->priv_data;
3534 block_size = MB_SIZE >> s->block_max_depth;
3535 /* FIXME block_size * 2 is determined empirically. block_size * 1.5 is definitely needed, but I (Robert) cannot figure out why more than that is needed. Perhaps there is a bug, or perhaps I overlooked some demands that are placed on the buffer. */
3536 /* FIXME The formula is WRONG. For height > 480, the buffer will overflow. */
3537 /* FIXME For now, I will use a full frame of lines. Fortunately, this should not materially effect cache performance because lines are allocated using a stack, so if in fact only 50 out of 496 lines are needed at a time, the other 446 will sit allocated but never accessed. */
3538 // slice_buffer_init(s->plane[0].sb, s->plane[0].height, (block_size * 2) + (s->spatial_decomposition_count * s->spatial_decomposition_count), s->plane[0].width, s->spatial_dwt_buffer);
3539 slice_buffer_init(&s->sb, s->plane[0].height, s->plane[0].height, s->plane[0].width, s->spatial_dwt_buffer);
3544 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
3545 SnowContext *s = avctx->priv_data;
3546 RangeCoder * const c= &s->c;
3548 AVFrame *picture = data;
3549 int level, orientation, plane_index;
3551 ff_init_range_decoder(c, buf, buf_size);
3552 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
3554 s->current_picture.pict_type= FF_I_TYPE; //FIXME I vs. P
3556 if(!s->block) alloc_blocks(s);
3559 //keyframe flag dupliaction mess FIXME
3560 if(avctx->debug&FF_DEBUG_PICT_INFO)
3561 av_log(avctx, AV_LOG_ERROR, "keyframe:%d qlog:%d\n", s->keyframe, s->qlog);
3565 for(plane_index=0; plane_index<3; plane_index++){
3566 Plane *p= &s->plane[plane_index];
3570 int decode_state[MAX_DECOMPOSITIONS][4][1]; /* Stored state info for unpack_coeffs. 1 variable per instance. */
3571 SubBand * correlate_band;
3573 if(s->avctx->debug&2048){
3574 memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
3575 predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
3579 int v= s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x];
3580 s->mconly_picture.data[plane_index][y*s->mconly_picture.linesize[plane_index] + x]= v;
3586 for(level=0; level<s->spatial_decomposition_count; level++){
3587 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3588 SubBand *b= &p->band[level][orientation];
3589 unpack_coeffs(s, b, b->parent, orientation);
3592 STOP_TIMER("unpack coeffs");
3595 /* Handle level 0, orientation 0 specially. It is particularly resistant to slicing but fortunately quite small, so process it in one pass. */
3596 correlate_band = &p->band[0][0];
3597 decode_subband_slice_buffered(s, correlate_band, &s->sb, 0, correlate_band->height, decode_state[0][0]);
3598 correlate_buffered(s, &s->sb, correlate_band, correlate_band->buf, correlate_band->stride, 1, 0);
3599 dequantize_buffered(s, &s->sb, correlate_band, correlate_band->buf, correlate_band->stride);
3602 const int mb_h= s->b_height << s->block_max_depth;
3603 const int block_size = MB_SIZE >> s->block_max_depth;
3604 const int block_w = plane_index ? block_size/2 : block_size;
3606 dwt_compose_t cs[MAX_DECOMPOSITIONS];
3611 ff_spatial_idwt_buffered_init(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count);
3612 for(mb_y=0; mb_y<=mb_h; mb_y++){
3614 const int slice_starty = block_w*mb_y;
3615 const int slice_h = block_w*(mb_y+1);
3619 for(level=0; level<s->spatial_decomposition_count; level++){
3620 for(orientation=level ? 1 : 1; orientation<4; orientation++){
3621 SubBand *b= &p->band[level][orientation];
3624 int our_mb_start = mb_y;
3625 int our_mb_end = (mb_y + 1);
3626 start_y = FFMIN(b->height, (mb_y ? ((block_w * our_mb_start - 4) >> (s->spatial_decomposition_count - level)) + 5 : 0));
3627 end_y = FFMIN(b->height, (((block_w * our_mb_end - 4) >> (s->spatial_decomposition_count - level)) + 5));
3629 if (start_y != end_y)
3630 decode_subband_slice_buffered(s, b, &s->sb, start_y, end_y, decode_state[level][orientation]);
3633 STOP_TIMER("decode_subband_slice");
3637 for(; yd<slice_h; yd+=4){
3638 ff_spatial_idwt_buffered_slice(cs, &s->sb, w, h, 1, s->spatial_decomposition_type, s->spatial_decomposition_count, yd);
3640 STOP_TIMER("idwt slice");}
3643 if(s->qlog == LOSSLESS_QLOG){
3644 for(; yq<slice_h && yq<h; yq++){
3645 DWTELEM * line = slice_buffer_get_line(&s->sb, yq);
3647 line[x] <<= FRAC_BITS;
3652 predict_slice_buffered(s, &s->sb, s->spatial_dwt_buffer, plane_index, 1, mb_y);
3654 /* Nasty hack based empirically on how predict_slice_buffered() hits the buffer. */
3655 /* FIXME If possible, make predict_slice fit into the slice. As of now, it works on some previous lines (up to slice_height / 2) if the condition on the next line is false. */
3656 if (s->keyframe || (s->avctx->debug&512)){
3657 y = FFMIN(p->height, slice_starty);
3658 end_y = FFMIN(p->height, slice_h);
3661 y = FFMAX(0, FFMIN(p->height, slice_starty - (block_w >> 1)));
3662 end_y = FFMAX(0, FFMIN(p->height, slice_h - (block_w >> 1)));
3665 slice_buffer_release(&s->sb, y++);
3668 slice_buffer_flush(&s->sb);
3670 STOP_TIMER("idwt + predict_slices")}
3675 if(s->last_picture.data[0])
3676 avctx->release_buffer(avctx, &s->last_picture);
3678 if(!(s->avctx->debug&2048))
3679 *picture= s->current_picture;
3681 *picture= s->mconly_picture;
3683 *data_size = sizeof(AVFrame);
3685 bytes_read= c->bytestream - c->bytestream_start;
3686 if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n"); //FIXME
3691 static int decode_end(AVCodecContext *avctx)
3693 SnowContext *s = avctx->priv_data;
3695 slice_buffer_destroy(&s->sb);
3702 AVCodec snow_decoder = {
3706 sizeof(SnowContext),
3711 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
3715 #ifdef CONFIG_ENCODERS
3716 AVCodec snow_encoder = {
3720 sizeof(SnowContext),
3736 int buffer[2][width*height];
3739 s.spatial_decomposition_count=6;
3740 s.spatial_decomposition_type=1;
3742 printf("testing 5/3 DWT\n");
3743 for(i=0; i<width*height; i++)
3744 buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
3746 ff_spatial_dwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
3747 ff_spatial_idwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
3749 for(i=0; i<width*height; i++)
3750 if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
3752 printf("testing 9/7 DWT\n");
3753 s.spatial_decomposition_type=0;
3754 for(i=0; i<width*height; i++)
3755 buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
3757 ff_spatial_dwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
3758 ff_spatial_idwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
3760 for(i=0; i<width*height; i++)
3761 if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
3763 printf("testing AC coder\n");
3764 memset(s.header_state, 0, sizeof(s.header_state));
3765 ff_init_range_encoder(&s.c, buffer[0], 256*256);
3766 ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
3768 for(i=-256; i<256; i++){
3770 put_symbol(&s.c, s.header_state, i*i*i/3*ABS(i), 1);
3771 STOP_TIMER("put_symbol")
3773 ff_rac_terminate(&s.c);
3775 memset(s.header_state, 0, sizeof(s.header_state));
3776 ff_init_range_decoder(&s.c, buffer[0], 256*256);
3777 ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
3779 for(i=-256; i<256; i++){
3782 j= get_symbol(&s.c, s.header_state, 1);
3783 STOP_TIMER("get_symbol")
3784 if(j!=i*i*i/3*ABS(i)) printf("fsck: %d != %d\n", i, j);
3787 int level, orientation, x, y;
3788 int64_t errors[8][4];
3791 memset(errors, 0, sizeof(errors));
3792 s.spatial_decomposition_count=3;
3793 s.spatial_decomposition_type=0;
3794 for(level=0; level<s.spatial_decomposition_count; level++){
3795 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3796 int w= width >> (s.spatial_decomposition_count-level);
3797 int h= height >> (s.spatial_decomposition_count-level);
3798 int stride= width << (s.spatial_decomposition_count-level);
3799 DWTELEM *buf= buffer[0];
3802 if(orientation&1) buf+=w;
3803 if(orientation>1) buf+=stride>>1;
3805 memset(buffer[0], 0, sizeof(int)*width*height);
3806 buf[w/2 + h/2*stride]= 256*256;
3807 ff_spatial_idwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
3808 for(y=0; y<height; y++){
3809 for(x=0; x<width; x++){
3810 int64_t d= buffer[0][x + y*width];
3812 if(ABS(width/2-x)<9 && ABS(height/2-y)<9 && level==2) printf("%8lld ", d);
3814 if(ABS(height/2-y)<9 && level==2) printf("\n");
3816 error= (int)(sqrt(error)+0.5);
3817 errors[level][orientation]= error;
3818 if(g) g=ff_gcd(g, error);
3822 printf("static int const visual_weight[][4]={\n");
3823 for(level=0; level<s.spatial_decomposition_count; level++){
3825 for(orientation=0; orientation<4; orientation++){
3826 printf("%8lld,", errors[level][orientation]/g);
3834 int w= width >> (s.spatial_decomposition_count-level);
3835 int h= height >> (s.spatial_decomposition_count-level);
3836 int stride= width << (s.spatial_decomposition_count-level);
3837 DWTELEM *buf= buffer[0];
3843 memset(buffer[0], 0, sizeof(int)*width*height);
3845 for(y=0; y<height; y++){
3846 for(x=0; x<width; x++){
3847 int tab[4]={0,2,3,1};
3848 buffer[0][x+width*y]= 256*256*tab[(x&1) + 2*(y&1)];
3851 ff_spatial_dwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
3855 buf[x + y*stride ]=169;
3856 buf[x + y*stride-w]=64;
3859 ff_spatial_idwt(buffer[0], width, height, width, s->spatial_decomposition_type, s->spatial_decomposition_count);
3861 for(y=0; y<height; y++){
3862 for(x=0; x<width; x++){
3863 int64_t d= buffer[0][x + y*width];
3865 if(ABS(width/2-x)<9 && ABS(height/2-y)<9) printf("%8lld ", d);
3867 if(ABS(height/2-y)<9) printf("\n");