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
24 #include "mpegvideo.h"
29 #define MAX_DECOMPOSITIONS 8
34 static const int8_t quant3[256]={
35 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
36 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
37 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
38 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
39 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
40 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
41 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
42 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
43 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
44 -1,-1,-1,-1,-1,-1,-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, 0,
52 static const int8_t quant3b[256]={
53 0, 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, 1,
55 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
56 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
57 1, 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,
70 static const int8_t quant5[256]={
71 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
72 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
73 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
74 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
75 2, 2, 2, 2, 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,-1,-1,-1,
88 static const int8_t quant7[256]={
89 0, 1, 1, 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, 2, 2, 2,
91 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
92 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
93 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
94 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
95 3, 3, 3, 3, 3, 3, 3, 3, 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,-2,-2,-2,-2,-2,-2,-2,
103 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
104 -2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,
106 static const int8_t quant9[256]={
107 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
108 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
109 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
110 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
111 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
112 4, 4, 4, 4, 4, 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,-3,-3,-3,-3,
122 -3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-1,-1,
124 static const int8_t quant11[256]={
125 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
126 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
127 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
128 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
129 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
130 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
131 5, 5, 5, 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,-4,-4,
139 -4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
140 -4,-4,-4,-4,-4,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-1,
142 static const int8_t quant13[256]={
143 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
144 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
145 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
146 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
147 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
148 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
149 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
150 6, 6, 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,-5,
156 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
157 -5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
158 -4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
163 static const uint8_t obmc32[1024]={
164 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
165 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
166 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0,
167 0, 0, 1, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 8, 8, 8, 7, 7, 6, 6, 5, 4, 4, 3, 2, 2, 1, 1, 0, 0,
168 0, 0, 1, 2, 2, 3, 4, 6, 7, 8, 9,10,11,12,12,12,12,12,12,11,10, 9, 8, 7, 6, 4, 3, 2, 2, 1, 0, 0,
169 0, 1, 1, 2, 3, 5, 6, 8,10,11,13,14,15,16,17,18,18,17,16,15,14,13,11,10, 8, 6, 5, 3, 2, 1, 1, 0,
170 0, 1, 1, 3, 4, 6, 8,10,13,15,17,19,20,22,22,23,23,22,22,20,19,17,15,13,10, 8, 6, 4, 3, 1, 1, 0,
171 0, 1, 2, 4, 6, 8,10,13,16,19,21,23,25,27,28,29,29,28,27,25,23,21,19,16,13,10, 8, 6, 4, 2, 1, 0,
172 0, 1, 2, 4, 7,10,13,16,19,22,25,28,31,33,34,35,35,34,33,31,28,25,22,19,16,13,10, 7, 4, 2, 1, 0,
173 0, 1, 3, 5, 8,11,15,19,22,26,30,33,36,38,40,41,41,40,38,36,33,30,26,22,19,15,11, 8, 5, 3, 1, 0,
174 0, 1, 3, 6, 9,12,17,21,25,30,34,38,41,44,45,46,46,45,44,41,38,34,30,25,21,17,12, 9, 6, 3, 1, 0,
175 0, 1, 3, 6,10,14,19,23,28,33,38,42,45,48,51,52,52,51,48,45,42,38,33,28,23,19,14,10, 6, 3, 1, 0,
176 0, 1, 4, 7,11,15,20,25,31,36,41,45,49,52,55,56,56,55,52,49,45,41,36,31,25,20,15,11, 7, 4, 1, 0,
177 0, 2, 4, 7,12,16,22,27,33,38,44,48,52,56,58,60,60,58,56,52,48,44,38,33,27,22,16,12, 7, 4, 2, 0,
178 0, 1, 4, 8,12,17,22,28,34,40,45,51,55,58,61,62,62,61,58,55,51,45,40,34,28,22,17,12, 8, 4, 1, 0,
179 0, 2, 4, 8,12,18,23,29,35,41,46,52,56,60,62,64,64,62,60,56,52,46,41,35,29,23,18,12, 8, 4, 2, 0,
180 0, 2, 4, 8,12,18,23,29,35,41,46,52,56,60,62,64,64,62,60,56,52,46,41,35,29,23,18,12, 8, 4, 2, 0,
181 0, 1, 4, 8,12,17,22,28,34,40,45,51,55,58,61,62,62,61,58,55,51,45,40,34,28,22,17,12, 8, 4, 1, 0,
182 0, 2, 4, 7,12,16,22,27,33,38,44,48,52,56,58,60,60,58,56,52,48,44,38,33,27,22,16,12, 7, 4, 2, 0,
183 0, 1, 4, 7,11,15,20,25,31,36,41,45,49,52,55,56,56,55,52,49,45,41,36,31,25,20,15,11, 7, 4, 1, 0,
184 0, 1, 3, 6,10,14,19,23,28,33,38,42,45,48,51,52,52,51,48,45,42,38,33,28,23,19,14,10, 6, 3, 1, 0,
185 0, 1, 3, 6, 9,12,17,21,25,30,34,38,41,44,45,46,46,45,44,41,38,34,30,25,21,17,12, 9, 6, 3, 1, 0,
186 0, 1, 3, 5, 8,11,15,19,22,26,30,33,36,38,40,41,41,40,38,36,33,30,26,22,19,15,11, 8, 5, 3, 1, 0,
187 0, 1, 2, 4, 7,10,13,16,19,22,25,28,31,33,34,35,35,34,33,31,28,25,22,19,16,13,10, 7, 4, 2, 1, 0,
188 0, 1, 2, 4, 6, 8,10,13,16,19,21,23,25,27,28,29,29,28,27,25,23,21,19,16,13,10, 8, 6, 4, 2, 1, 0,
189 0, 1, 1, 3, 4, 6, 8,10,13,15,17,19,20,22,22,23,23,22,22,20,19,17,15,13,10, 8, 6, 4, 3, 1, 1, 0,
190 0, 1, 1, 2, 3, 5, 6, 8,10,11,13,14,15,16,17,18,18,17,16,15,14,13,11,10, 8, 6, 5, 3, 2, 1, 1, 0,
191 0, 0, 1, 2, 2, 3, 4, 6, 7, 8, 9,10,11,12,12,12,12,12,12,11,10, 9, 8, 7, 6, 4, 3, 2, 2, 1, 0, 0,
192 0, 0, 1, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 8, 8, 8, 7, 7, 6, 6, 5, 4, 4, 3, 2, 2, 1, 1, 0, 0,
193 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0,
194 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
195 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198 static const uint8_t obmc16[256]={
199 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
200 0, 1, 1, 2, 4, 5, 5, 6, 6, 5, 5, 4, 2, 1, 1, 0,
201 0, 1, 4, 6, 9,11,13,15,15,13,11, 9, 6, 4, 1, 0,
202 0, 2, 6,11,15,20,24,26,26,24,20,15,11, 6, 2, 0,
203 0, 4, 9,15,23,29,34,38,38,34,29,23,15, 9, 4, 0,
204 0, 5,11,20,29,38,45,49,49,45,38,29,20,11, 5, 0,
205 1, 5,13,24,34,45,53,57,57,53,45,34,24,13, 5, 1,
206 1, 6,15,26,38,49,57,62,62,57,49,38,26,15, 6, 1,
207 1, 6,15,26,38,49,57,62,62,57,49,38,26,15, 6, 1,
208 1, 5,13,24,34,45,53,57,57,53,45,34,24,13, 5, 1,
209 0, 5,11,20,29,38,45,49,49,45,38,29,20,11, 5, 0,
210 0, 4, 9,15,23,29,34,38,38,34,29,23,15, 9, 4, 0,
211 0, 2, 6,11,15,20,24,26,26,24,20,15,11, 6, 2, 0,
212 0, 1, 4, 6, 9,11,13,15,15,13,11, 9, 6, 4, 1, 0,
213 0, 1, 1, 2, 4, 5, 5, 6, 6, 5, 5, 4, 2, 1, 1, 0,
214 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
218 static const uint8_t obmc32[1024]={
219 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
220 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 5, 5, 5, 4, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0,
221 0, 1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 8, 9,10,10, 9, 8, 8, 7, 7, 6, 5, 5, 4, 3, 3, 2, 2, 1, 0,
222 0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9,10,11,12,13,14,14,13,12,11,10, 9, 8, 7, 7, 6, 5, 4, 3, 2, 1, 0,
223 1, 2, 3, 4, 5, 6, 7, 8,10,11,12,13,14,15,16,17,17,16,15,14,13,12,11,10, 8, 7, 6, 5, 4, 3, 2, 1,
224 1, 2, 3, 5, 6, 8, 9,10,12,13,14,16,17,19,20,21,21,20,19,17,16,14,13,12,10, 9, 8, 6, 5, 3, 2, 1,
225 1, 2, 4, 6, 7, 9,11,12,14,15,17,19,20,22,24,25,25,24,22,20,19,17,15,14,12,11, 9, 7, 6, 4, 2, 1,
226 1, 3, 5, 7, 8,10,12,14,16,18,20,22,23,25,27,29,29,27,25,23,22,20,18,16,14,12,10, 8, 7, 5, 3, 1,
227 1, 3, 5, 7,10,12,14,16,18,20,22,24,27,29,31,33,33,31,29,27,24,22,20,18,16,14,12,10, 7, 5, 3, 1,
228 1, 4, 6, 8,11,13,15,18,20,23,25,27,30,32,34,37,37,34,32,30,27,25,23,20,18,15,13,11, 8, 6, 4, 1,
229 1, 4, 7, 9,12,14,17,20,22,25,28,30,33,35,38,41,41,38,35,33,30,28,25,22,20,17,14,12, 9, 7, 4, 1,
230 1, 4, 7,10,13,16,19,22,24,27,30,33,36,39,42,45,45,42,39,36,33,30,27,24,22,19,16,13,10, 7, 4, 1,
231 2, 5, 8,11,14,17,20,23,27,30,33,36,39,42,45,48,48,45,42,39,36,33,30,27,23,20,17,14,11, 8, 5, 2,
232 2, 5, 8,12,15,19,22,25,29,32,35,39,42,46,49,52,52,49,46,42,39,35,32,29,25,22,19,15,12, 8, 5, 2,
233 2, 5, 9,13,16,20,24,27,31,34,38,42,45,49,53,56,56,53,49,45,42,38,34,31,27,24,20,16,13, 9, 5, 2,
234 2, 6,10,14,17,21,25,29,33,37,41,45,48,52,56,60,60,56,52,48,45,41,37,33,29,25,21,17,14,10, 6, 2,
235 2, 6,10,14,17,21,25,29,33,37,41,45,48,52,56,60,60,56,52,48,45,41,37,33,29,25,21,17,14,10, 6, 2,
236 2, 5, 9,13,16,20,24,27,31,34,38,42,45,49,53,56,56,53,49,45,42,38,34,31,27,24,20,16,13, 9, 5, 2,
237 2, 5, 8,12,15,19,22,25,29,32,35,39,42,46,49,52,52,49,46,42,39,35,32,29,25,22,19,15,12, 8, 5, 2,
238 2, 5, 8,11,14,17,20,23,27,30,33,36,39,42,45,48,48,45,42,39,36,33,30,27,23,20,17,14,11, 8, 5, 2,
239 1, 4, 7,10,13,16,19,22,24,27,30,33,36,39,42,45,45,42,39,36,33,30,27,24,22,19,16,13,10, 7, 4, 1,
240 1, 4, 7, 9,12,14,17,20,22,25,28,30,33,35,38,41,41,38,35,33,30,28,25,22,20,17,14,12, 9, 7, 4, 1,
241 1, 4, 6, 8,11,13,15,18,20,23,25,27,30,32,34,37,37,34,32,30,27,25,23,20,18,15,13,11, 8, 6, 4, 1,
242 1, 3, 5, 7,10,12,14,16,18,20,22,24,27,29,31,33,33,31,29,27,24,22,20,18,16,14,12,10, 7, 5, 3, 1,
243 1, 3, 5, 7, 8,10,12,14,16,18,20,22,23,25,27,29,29,27,25,23,22,20,18,16,14,12,10, 8, 7, 5, 3, 1,
244 1, 2, 4, 6, 7, 9,11,12,14,15,17,19,20,22,24,25,25,24,22,20,19,17,15,14,12,11, 9, 7, 6, 4, 2, 1,
245 1, 2, 3, 5, 6, 8, 9,10,12,13,14,16,17,19,20,21,21,20,19,17,16,14,13,12,10, 9, 8, 6, 5, 3, 2, 1,
246 1, 2, 3, 4, 5, 6, 7, 8,10,11,12,13,14,15,16,17,17,16,15,14,13,12,11,10, 8, 7, 6, 5, 4, 3, 2, 1,
247 0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9,10,11,12,13,14,14,13,12,11,10, 9, 8, 7, 7, 6, 5, 4, 3, 2, 1, 0,
248 0, 1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 8, 9,10,10, 9, 8, 8, 7, 7, 6, 5, 5, 4, 3, 3, 2, 2, 1, 0,
249 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 5, 5, 5, 4, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0,
250 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
253 static const uint8_t obmc16[256]={
254 0, 1, 1, 2, 2, 3, 3, 4, 4, 3, 3, 2, 2, 1, 1, 0,
255 1, 2, 4, 5, 7, 8,10,11,11,10, 8, 7, 5, 4, 2, 1,
256 1, 4, 6, 9,11,14,16,19,19,16,14,11, 9, 6, 4, 1,
257 2, 5, 9,12,16,19,23,26,26,23,19,16,12, 9, 5, 2,
258 2, 7,11,16,20,25,29,34,34,29,25,20,16,11, 7, 2,
259 3, 8,14,19,25,30,36,41,41,36,30,25,19,14, 8, 3,
260 3,10,16,23,29,36,42,49,49,42,36,29,23,16,10, 3,
261 4,11,19,26,34,41,49,56,56,49,41,34,26,19,11, 4,
262 4,11,19,26,34,41,49,56,56,49,41,34,26,19,11, 4,
263 3,10,16,23,29,36,42,49,49,42,36,29,23,16,10, 3,
264 3, 8,14,19,25,30,36,41,41,36,30,25,19,14, 8, 3,
265 2, 7,11,16,20,25,29,34,34,29,25,20,16,11, 7, 2,
266 2, 5, 9,12,16,19,23,26,26,23,19,16,12, 9, 5, 2,
267 1, 4, 6, 9,11,14,16,19,19,16,14,11, 9, 6, 4, 1,
268 1, 2, 4, 5, 7, 8,10,11,11,10, 8, 7, 5, 4, 2, 1,
269 0, 1, 1, 2, 2, 3, 3, 4, 4, 3, 3, 2, 2, 1, 1, 0,
273 static const uint8_t obmc32[1024]={
274 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
275 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
276 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0,
277 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0,
278 0, 0, 1, 1, 2, 3, 4, 5, 6, 7, 9,10,11,11,12,12,12,12,11,11,10, 9, 7, 6, 5, 4, 3, 2, 1, 1, 0, 0,
279 0, 0, 1, 2, 3, 5, 6, 8, 9,11,12,14,15,16,17,17,17,17,16,15,14,12,11, 9, 8, 6, 5, 3, 2, 1, 0, 0,
280 0, 1, 1, 2, 4, 6, 8,10,12,15,17,19,20,21,22,23,23,22,21,20,19,17,15,12,10, 8, 6, 4, 2, 1, 1, 0,
281 0, 1, 2, 3, 5, 8,10,13,16,19,21,24,26,27,28,29,29,28,27,26,24,21,19,16,13,10, 8, 5, 3, 2, 1, 0,
282 0, 1, 2, 4, 6, 9,12,16,19,23,26,29,31,33,34,35,35,34,33,31,29,26,23,19,16,12, 9, 6, 4, 2, 1, 0,
283 0, 1, 3, 5, 7,11,15,19,23,26,30,34,37,39,40,41,41,40,39,37,34,30,26,23,19,15,11, 7, 5, 3, 1, 0,
284 0, 1, 3, 5, 9,12,17,21,26,30,35,38,42,44,46,47,47,46,44,42,38,35,30,26,21,17,12, 9, 5, 3, 1, 0,
285 0, 1, 3, 6, 9,14,19,24,29,34,38,43,46,49,51,52,52,51,49,46,43,38,34,29,24,19,14, 9, 6, 3, 1, 0,
286 0, 1, 3, 6,11,15,20,26,31,37,42,46,50,53,56,57,57,56,53,50,46,42,37,31,26,20,15,11, 6, 3, 1, 0,
287 0, 1, 3, 7,11,16,21,27,33,39,44,49,53,57,59,60,60,59,57,53,49,44,39,33,27,21,16,11, 7, 3, 1, 0,
288 0, 1, 4, 7,12,17,22,28,34,40,46,51,56,59,61,63,63,61,59,56,51,46,40,34,28,22,17,12, 7, 4, 1, 0,
289 0, 1, 4, 7,12,17,23,29,35,41,47,52,57,60,63,64,64,63,60,57,52,47,41,35,29,23,17,12, 7, 4, 1, 0,
290 0, 1, 4, 7,12,17,23,29,35,41,47,52,57,60,63,64,64,63,60,57,52,47,41,35,29,23,17,12, 7, 4, 1, 0,
291 0, 1, 4, 7,12,17,22,28,34,40,46,51,56,59,61,63,63,61,59,56,51,46,40,34,28,22,17,12, 7, 4, 1, 0,
292 0, 1, 3, 7,11,16,21,27,33,39,44,49,53,57,59,60,60,59,57,53,49,44,39,33,27,21,16,11, 7, 3, 1, 0,
293 0, 1, 3, 6,11,15,20,26,31,37,42,46,50,53,56,57,57,56,53,50,46,42,37,31,26,20,15,11, 6, 3, 1, 0,
294 0, 1, 3, 6, 9,14,19,24,29,34,38,43,46,49,51,52,52,51,49,46,43,38,34,29,24,19,14, 9, 6, 3, 1, 0,
295 0, 1, 3, 5, 9,12,17,21,26,30,35,38,42,44,46,47,47,46,44,42,38,35,30,26,21,17,12, 9, 5, 3, 1, 0,
296 0, 1, 3, 5, 7,11,15,19,23,26,30,34,37,39,40,41,41,40,39,37,34,30,26,23,19,15,11, 7, 5, 3, 1, 0,
297 0, 1, 2, 4, 6, 9,12,16,19,23,26,29,31,33,34,35,35,34,33,31,29,26,23,19,16,12, 9, 6, 4, 2, 1, 0,
298 0, 1, 2, 3, 5, 8,10,13,16,19,21,24,26,27,28,29,29,28,27,26,24,21,19,16,13,10, 8, 5, 3, 2, 1, 0,
299 0, 1, 1, 2, 4, 6, 8,10,12,15,17,19,20,21,22,23,23,22,21,20,19,17,15,12,10, 8, 6, 4, 2, 1, 1, 0,
300 0, 0, 1, 2, 3, 5, 6, 8, 9,11,12,14,15,16,17,17,17,17,16,15,14,12,11, 9, 8, 6, 5, 3, 2, 1, 0, 0,
301 0, 0, 1, 1, 2, 3, 4, 5, 6, 7, 9,10,11,11,12,12,12,12,11,11,10, 9, 7, 6, 5, 4, 3, 2, 1, 1, 0, 0,
302 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0,
303 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0,
304 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
305 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
308 static const uint8_t obmc16[256]={
309 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
310 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0,
311 0, 1, 3, 6, 8,11,13,14,14,13,11, 8, 6, 3, 1, 0,
312 0, 2, 6,10,15,20,24,26,26,24,20,15,10, 6, 2, 0,
313 0, 3, 8,16,23,30,35,38,38,35,30,23,16, 8, 3, 0,
314 1, 4,11,20,30,39,46,49,49,46,39,30,20,11, 4, 1,
315 1, 5,13,24,35,46,54,58,58,54,46,35,24,13, 5, 1,
316 0, 5,14,26,38,49,58,63,63,58,49,38,26,14, 5, 0,
317 0, 5,14,26,38,49,58,63,63,58,49,38,26,14, 5, 0,
318 1, 5,13,24,35,46,54,58,58,54,46,35,24,13, 5, 1,
319 1, 4,11,20,30,39,46,49,49,46,39,30,20,11, 4, 1,
320 0, 3, 8,16,23,30,35,38,38,35,30,23,16, 8, 3, 0,
321 0, 2, 6,10,15,20,24,26,26,24,20,15,10, 6, 2, 0,
322 0, 1, 3, 6, 8,11,13,14,14,13,11, 8, 6, 3, 1, 0,
323 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0,
324 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
329 typedef struct QTree{
330 int treedim[MAX_DECOMPOSITIONS][2];
331 uint8_t *tree[MAX_DECOMPOSITIONS];
336 typedef struct SubBand{
341 int qlog; ///< log(qscale)/log[2^(1/6)]
344 struct SubBand *parent;
345 uint8_t state[/*7*2*/ 7 + 512][32];
348 typedef struct Plane{
351 SubBand band[MAX_DECOMPOSITIONS][4];
354 typedef struct SnowContext{
355 // MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to make the motion estimation eventually independant of MpegEncContext, so this will be removed then (FIXME/XXX)
357 AVCodecContext *avctx;
360 AVFrame input_picture;
361 AVFrame current_picture;
362 AVFrame last_picture;
363 AVFrame mconly_picture;
364 // uint8_t q_context[16];
365 uint8_t header_state[32];
368 int spatial_decomposition_type;
369 int temporal_decomposition_type;
370 int spatial_decomposition_count;
371 int temporal_decomposition_count;
372 DWTELEM *spatial_dwt_buffer;
373 DWTELEM *pred_buffer;
377 int spatial_scalability;
381 #define QBIAS_SHIFT 3
382 int b_width; //FIXME remove?
383 int b_height; //FIXME remove?
384 Plane plane[MAX_PLANES];
391 int16_t (*motion_val8)[2];
392 int16_t (*motion_val16)[2];
393 MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to make the motion estimation eventually independant of MpegEncContext, so this will be removed then (FIXME/XXX)
396 #define QEXPSHIFT 7 //FIXME try to change this to 0
397 static const uint8_t qexp[8]={
398 128, 140, 152, 166, 181, 197, 215, 235
399 // 64, 70, 76, 83, 91, 99, 108, 117
400 // 32, 35, 38, 41, 45, 49, 54, 59
401 // 16, 17, 19, 21, 23, 25, 27, 29
402 // 8, 9, 10, 10, 11, 12, 13, 15
405 static inline int mirror(int v, int m){
407 else if(v>m) return 2*m-v;
411 static inline void put_symbol(CABACContext *c, uint8_t *state, int v, int is_signed){
416 const int e= av_log2(a);
418 const int el= FFMIN(e, 10);
419 put_cabac(c, state+0, 0);
422 put_cabac(c, state+1+i, 1); //1..10
425 put_cabac(c, state+1+9, 1); //1..10
427 put_cabac(c, state+1+FFMIN(i,9), 0);
429 for(i=e-1; i>=el; i--){
430 put_cabac(c, state+22+9, (a>>i)&1); //22..31
433 put_cabac(c, state+22+i, (a>>i)&1); //22..31
437 put_cabac(c, state+11 + el, v < 0); //11..21
440 put_cabac(c, state+0, 0);
443 put_cabac(c, state+1+i, 1); //1..10
445 put_cabac(c, state+1+i, 0);
447 for(i=e-1; i>=0; i--){
448 put_cabac(c, state+22+i, (a>>i)&1); //22..31
452 put_cabac(c, state+11 + e, v < 0); //11..21
455 put_cabac(c, state+1+FFMIN(i,9), 1); //1..10
457 put_cabac(c, state+1+FFMIN(i,9), 0);
459 for(i=e-1; i>=0; i--){
460 put_cabac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31
464 put_cabac(c, state+11 + FFMIN(e,10), v < 0); //11..21
468 put_cabac(c, state+0, 1);
472 static inline int get_symbol(CABACContext *c, uint8_t *state, int is_signed){
473 if(get_cabac(c, state+0))
477 //FIXME try to merge loops with FFMIN() maybe they are equally fast and they are surly cuter
479 if(get_cabac(c, state + 1 + e)==0) // 1..10
485 while(get_cabac(c, state + 1 + 9)) //10
489 for(i=e-1; i>=el; i--){
490 a += a + get_cabac(c, state+22+9); //31
493 a += a + get_cabac(c, state+22+i); //22..31
496 if(is_signed && get_cabac(c, state+11 + el)) //11..21
503 static inline void put_symbol2(CABACContext *c, uint8_t *state, int v, int log2){
505 int e= av_log2(v<<1);
508 if(v==0) assert(e==0);
511 put_cabac(c, state+log2, 1);
517 put_cabac(c, state+log2, 0);
519 for(i=log2-1; i>=0; i--){
520 put_cabac(c, state+31-i, (v>>i)&1);
525 static inline int get_symbol2(CABACContext *c, uint8_t *state, int log2){
529 while(get_cabac(c, state+log2)){
534 for(i=log2-1; i>=0; i--){
535 v+= get_cabac(c, state+31-i)<<i;
541 static always_inline void lift(DWTELEM *dst, DWTELEM *src, DWTELEM *ref, int dst_step, int src_step, int ref_step, int width, int mul, int add, int shift, int highpass, int inverse){
542 const int mirror_left= !highpass;
543 const int mirror_right= (width&1) ^ highpass;
544 const int w= (width>>1) - 1 + (highpass & width);
547 #define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
549 dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
555 dst[i*dst_step] = LIFT(src[i*src_step], ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift), inverse);
559 dst[w*dst_step] = LIFT(src[w*src_step], ((mul*2*ref[w*ref_step]+add)>>shift), inverse);
563 static always_inline void lift5(DWTELEM *dst, DWTELEM *src, DWTELEM *ref, int dst_step, int src_step, int ref_step, int width, int mul, int add, int shift, int highpass, int inverse){
564 const int mirror_left= !highpass;
565 const int mirror_right= (width&1) ^ highpass;
566 const int w= (width>>1) - 1 + (highpass & width);
573 dst[0] = LIFT(src[0], ((r+add)>>shift), inverse);
579 int r= 3*(ref[i*ref_step] + ref[(i+1)*ref_step]);
582 dst[i*dst_step] = LIFT(src[i*src_step], ((r+add)>>shift), inverse);
586 int r= 3*2*ref[w*ref_step];
589 dst[w*dst_step] = LIFT(src[w*src_step], ((r+add)>>shift), inverse);
594 static void inplace_lift(int *dst, int width, int *coeffs, int n, int shift, int start, int inverse){
597 for(x=start; x<width; x+=2){
601 int x2= x + 2*i - n + 1;
603 else if(x2>=width) x2= 2*width-x2-2;
604 sum += coeffs[i]*(int64_t)dst[x2];
606 if(inverse) dst[x] -= (sum + (1<<shift)/2)>>shift;
607 else dst[x] += (sum + (1<<shift)/2)>>shift;
611 static void inplace_liftV(int *dst, int width, int height, int stride, int *coeffs, int n, int shift, int start, int inverse){
613 for(y=start; y<height; y+=2){
614 for(x=0; x<width; x++){
618 int y2= y + 2*i - n + 1;
620 else if(y2>=height) y2= 2*height-y2-2;
621 sum += coeffs[i]*(int64_t)dst[x + y2*stride];
623 if(inverse) dst[x + y*stride] -= (sum + (1<<shift)/2)>>shift;
624 else dst[x + y*stride] += (sum + (1<<shift)/2)>>shift;
633 #if 0 // more accurate 9/7
636 #define COEFFS1 (int[]){-25987,-25987}
639 #define COEFFS2 (int[]){-27777,-27777}
642 #define COEFFS3 (int[]){28931,28931}
645 #define COEFFS4 (int[]){14533,14533}
649 #define COEFFS1 (int[]){1,-9,-9,1}
652 #define COEFFS2 (int[]){-1,5,5,-1}
665 #define COEFFS1 (int[]){1,1}
668 #define COEFFS2 (int[]){-1,-1}
681 #define COEFFS2 (int[]){-1,-1}
684 #define COEFFS3 (int[]){-1,-1}
687 #define COEFFS4 (int[]){-5,29,29,-5}
692 #define COEFFS1 (int[]){-203,-203}
695 #define COEFFS2 (int[]){-217,-217}
698 #define COEFFS3 (int[]){113,113}
701 #define COEFFS4 (int[]){227,227}
709 #define COEFFS2 (int[]){-1,-1}
712 #define COEFFS3 (int[]){-1,-1}
715 #define COEFFS4 (int[]){3,3}
719 #define COEFFS1 (int[]){1,-9,-9,1}
722 #define COEFFS2 (int[]){1,1}
732 #define COEFFS1 (int[]){1,-9,-9,1}
735 #define COEFFS2 (int[]){-1,5,5,-1}
743 static void horizontal_decomposeX(int *b, int width){
745 const int width2= width>>1;
746 const int w2= (width+1)>>1;
749 inplace_lift(b, width, COEFFS1, N1, SHIFT1, LX1, 0);
750 inplace_lift(b, width, COEFFS2, N2, SHIFT2, LX0, 0);
751 inplace_lift(b, width, COEFFS3, N3, SHIFT3, LX1, 0);
752 inplace_lift(b, width, COEFFS4, N4, SHIFT4, LX0, 0);
754 for(x=0; x<width2; x++){
756 temp[x+w2]= b[2*x + 1];
760 memcpy(b, temp, width*sizeof(int));
763 static void horizontal_composeX(int *b, int width){
765 const int width2= width>>1;
767 const int w2= (width+1)>>1;
769 memcpy(temp, b, width*sizeof(int));
770 for(x=0; x<width2; x++){
772 b[2*x + 1]= temp[x+w2];
777 inplace_lift(b, width, COEFFS4, N4, SHIFT4, LX0, 1);
778 inplace_lift(b, width, COEFFS3, N3, SHIFT3, LX1, 1);
779 inplace_lift(b, width, COEFFS2, N2, SHIFT2, LX0, 1);
780 inplace_lift(b, width, COEFFS1, N1, SHIFT1, LX1, 1);
783 static void spatial_decomposeX(int *buffer, int width, int height, int stride){
786 for(y=0; y<height; y++){
787 for(x=0; x<width; x++){
788 buffer[y*stride + x] *= SCALEX;
792 for(y=0; y<height; y++){
793 horizontal_decomposeX(buffer + y*stride, width);
796 inplace_liftV(buffer, width, height, stride, COEFFS1, N1, SHIFT1, LX1, 0);
797 inplace_liftV(buffer, width, height, stride, COEFFS2, N2, SHIFT2, LX0, 0);
798 inplace_liftV(buffer, width, height, stride, COEFFS3, N3, SHIFT3, LX1, 0);
799 inplace_liftV(buffer, width, height, stride, COEFFS4, N4, SHIFT4, LX0, 0);
802 static void spatial_composeX(int *buffer, int width, int height, int stride){
805 inplace_liftV(buffer, width, height, stride, COEFFS4, N4, SHIFT4, LX0, 1);
806 inplace_liftV(buffer, width, height, stride, COEFFS3, N3, SHIFT3, LX1, 1);
807 inplace_liftV(buffer, width, height, stride, COEFFS2, N2, SHIFT2, LX0, 1);
808 inplace_liftV(buffer, width, height, stride, COEFFS1, N1, SHIFT1, LX1, 1);
810 for(y=0; y<height; y++){
811 horizontal_composeX(buffer + y*stride, width);
814 for(y=0; y<height; y++){
815 for(x=0; x<width; x++){
816 buffer[y*stride + x] /= SCALEX;
821 static void horizontal_decompose53i(int *b, int width){
823 const int width2= width>>1;
825 const int w2= (width+1)>>1;
827 for(x=0; x<width2; x++){
829 temp[x+w2]= b[2*x + 1];
841 for(x=1; x+1<width2; x+=2){
845 A2 += (A1 + A3 + 2)>>2;
849 A1= temp[x+1+width2];
852 A4 += (A1 + A3 + 2)>>2;
858 A2 += (A1 + A3 + 2)>>2;
862 lift(b+w2, temp+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
863 lift(b , temp , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 0);
867 static void vertical_decompose53iH0(int *b0, int *b1, int *b2, int width){
870 for(i=0; i<width; i++){
871 b1[i] -= (b0[i] + b2[i])>>1;
875 static void vertical_decompose53iL0(int *b0, int *b1, int *b2, int width){
878 for(i=0; i<width; i++){
879 b1[i] += (b0[i] + b2[i] + 2)>>2;
883 static void spatial_decompose53i(int *buffer, int width, int height, int stride){
885 DWTELEM *b0= buffer + mirror(-2-1, height-1)*stride;
886 DWTELEM *b1= buffer + mirror(-2 , height-1)*stride;
888 for(y=-2; y<height; y+=2){
889 DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
890 DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
893 if(b1 <= b3) horizontal_decompose53i(b2, width);
894 if(y+2 < height) horizontal_decompose53i(b3, width);
895 STOP_TIMER("horizontal_decompose53i")}
898 if(b1 <= b3) vertical_decompose53iH0(b1, b2, b3, width);
899 if(b0 <= b2) vertical_decompose53iL0(b0, b1, b2, width);
900 STOP_TIMER("vertical_decompose53i*")}
976 static void horizontal_decompose97i(int *b, int width){
978 const int w2= (width+1)>>1;
980 lift (temp+w2, b +1, b , 1, 2, 2, width, -W_AM, W_AO, W_AS, 1, 0);
981 lift (temp , b , temp+w2, 1, 2, 1, width, -W_BM, W_BO, W_BS, 0, 0);
982 lift5(b +w2, temp+w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 0);
983 lift (b , temp , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 0);
987 static void vertical_decompose97iH0(int *b0, int *b1, int *b2, int width){
990 for(i=0; i<width; i++){
991 b1[i] -= (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
995 static void vertical_decompose97iH1(int *b0, int *b1, int *b2, int width){
998 for(i=0; i<width; i++){
1000 b1[i] += (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
1002 int r= 3*(b0[i] + b2[i]);
1005 b1[i] += (r+W_CO)>>W_CS;
1010 static void vertical_decompose97iL0(int *b0, int *b1, int *b2, int width){
1013 for(i=0; i<width; i++){
1014 b1[i] -= (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
1018 static void vertical_decompose97iL1(int *b0, int *b1, int *b2, int width){
1021 for(i=0; i<width; i++){
1022 b1[i] += (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
1026 static void spatial_decompose97i(int *buffer, int width, int height, int stride){
1028 DWTELEM *b0= buffer + mirror(-4-1, height-1)*stride;
1029 DWTELEM *b1= buffer + mirror(-4 , height-1)*stride;
1030 DWTELEM *b2= buffer + mirror(-4+1, height-1)*stride;
1031 DWTELEM *b3= buffer + mirror(-4+2, height-1)*stride;
1033 for(y=-4; y<height; y+=2){
1034 DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
1035 DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
1038 if(b3 <= b5) horizontal_decompose97i(b4, width);
1039 if(y+4 < height) horizontal_decompose97i(b5, width);
1041 STOP_TIMER("horizontal_decompose97i")
1045 if(b3 <= b5) vertical_decompose97iH0(b3, b4, b5, width);
1046 if(b2 <= b4) vertical_decompose97iL0(b2, b3, b4, width);
1047 if(b1 <= b3) vertical_decompose97iH1(b1, b2, b3, width);
1048 if(b0 <= b2) vertical_decompose97iL1(b0, b1, b2, width);
1051 STOP_TIMER("vertical_decompose97i")
1061 static void spatial_dwt(SnowContext *s, int *buffer, int width, int height, int stride){
1064 for(level=0; level<s->spatial_decomposition_count; level++){
1065 switch(s->spatial_decomposition_type){
1066 case 0: spatial_decompose97i(buffer, width>>level, height>>level, stride<<level); break;
1067 case 1: spatial_decompose53i(buffer, width>>level, height>>level, stride<<level); break;
1068 case 2: spatial_decomposeX (buffer, width>>level, height>>level, stride<<level); break;
1073 static void horizontal_compose53i(int *b, int width){
1075 const int width2= width>>1;
1076 const int w2= (width+1)>>1;
1087 for(x=1; x+1<width2; x+=2){
1091 A2 += (A1 + A3 + 2)>>2;
1095 A1= temp[x+1+width2];
1098 A4 += (A1 + A3 + 2)>>2;
1104 A2 += (A1 + A3 + 2)>>2;
1108 lift(temp , b , b+w2, 1, 1, 1, width, 1, 2, 2, 0, 1);
1109 lift(temp+w2, b+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 1);
1111 for(x=0; x<width2; x++){
1113 b[2*x + 1]= temp[x+w2];
1119 static void vertical_compose53iH0(int *b0, int *b1, int *b2, int width){
1122 for(i=0; i<width; i++){
1123 b1[i] += (b0[i] + b2[i])>>1;
1127 static void vertical_compose53iL0(int *b0, int *b1, int *b2, int width){
1130 for(i=0; i<width; i++){
1131 b1[i] -= (b0[i] + b2[i] + 2)>>2;
1135 static void spatial_compose53i(int *buffer, int width, int height, int stride){
1137 DWTELEM *b0= buffer + mirror(-1-1, height-1)*stride;
1138 DWTELEM *b1= buffer + mirror(-1 , height-1)*stride;
1140 for(y=-1; y<=height; y+=2){
1141 DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
1142 DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
1145 if(b1 <= b3) vertical_compose53iL0(b1, b2, b3, width);
1146 if(b0 <= b2) vertical_compose53iH0(b0, b1, b2, width);
1147 STOP_TIMER("vertical_compose53i*")}
1150 if(y-1 >= 0) horizontal_compose53i(b0, width);
1151 if(b0 <= b2) horizontal_compose53i(b1, width);
1152 STOP_TIMER("horizontal_compose53i")}
1160 static void horizontal_compose97i(int *b, int width){
1162 const int w2= (width+1)>>1;
1164 lift (temp , b , b +w2, 1, 1, 1, width, W_DM, W_DO, W_DS, 0, 1);
1165 lift5(temp+w2, b +w2, temp , 1, 1, 1, width, W_CM, W_CO, W_CS, 1, 1);
1166 lift (b , temp , temp+w2, 2, 1, 1, width, -W_BM, W_BO, W_BS, 0, 1);
1167 lift (b+1 , temp+w2, b , 2, 1, 2, width, -W_AM, W_AO, W_AS, 1, 1);
1170 static void vertical_compose97iH0(int *b0, int *b1, int *b2, int width){
1173 for(i=0; i<width; i++){
1174 b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
1178 static void vertical_compose97iH1(int *b0, int *b1, int *b2, int width){
1181 for(i=0; i<width; i++){
1183 b1[i] -= (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
1185 int r= 3*(b0[i] + b2[i]);
1188 b1[i] -= (r+W_CO)>>W_CS;
1193 static void vertical_compose97iL0(int *b0, int *b1, int *b2, int width){
1196 for(i=0; i<width; i++){
1197 b1[i] += (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
1201 static void vertical_compose97iL1(int *b0, int *b1, int *b2, int width){
1204 for(i=0; i<width; i++){
1205 b1[i] -= (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
1209 static void spatial_compose97i(int *buffer, int width, int height, int stride){
1211 DWTELEM *b0= buffer + mirror(-3-1, height-1)*stride;
1212 DWTELEM *b1= buffer + mirror(-3 , height-1)*stride;
1213 DWTELEM *b2= buffer + mirror(-3+1, height-1)*stride;
1214 DWTELEM *b3= buffer + mirror(-3+2, height-1)*stride;
1216 for(y=-3; y<=height; y+=2){
1217 DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
1218 DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
1220 if(stride == width && y+4 < height && 0){
1222 for(x=0; x<width/2; x++)
1229 if(b3 <= b5) vertical_compose97iL1(b3, b4, b5, width);
1230 if(b2 <= b4) vertical_compose97iH1(b2, b3, b4, width);
1231 if(b1 <= b3) vertical_compose97iL0(b1, b2, b3, width);
1232 if(b0 <= b2) vertical_compose97iH0(b0, b1, b2, width);
1234 STOP_TIMER("vertical_compose97i")}}
1237 if(y-1>= 0) horizontal_compose97i(b0, width);
1238 if(b0 <= b2) horizontal_compose97i(b1, width);
1239 if(width>400 && b0 <= b2){
1240 STOP_TIMER("horizontal_compose97i")}}
1249 static void spatial_idwt(SnowContext *s, int *buffer, int width, int height, int stride){
1252 for(level=s->spatial_decomposition_count-1; level>=0; level--){
1253 switch(s->spatial_decomposition_type){
1254 case 0: spatial_compose97i(buffer, width>>level, height>>level, stride<<level); break;
1255 case 1: spatial_compose53i(buffer, width>>level, height>>level, stride<<level); break;
1256 case 2: spatial_composeX (buffer, width>>level, height>>level, stride<<level); break;
1261 static const int hilbert[16][2]={
1262 {0,0}, {1,0}, {1,1}, {0,1},
1263 {0,2}, {0,3}, {1,3}, {1,2},
1264 {2,2}, {2,3}, {3,3}, {3,2},
1265 {3,1}, {2,1}, {2,0}, {3,0},
1280 0112122312232334122323342334
1281 0123456789ABCDEF0123456789AB
1282 RLLRMRRLLRRMRLLMLRRLMLLRRLLM
1305 #define SVI(a, i, x, y) \
1312 static int sig_cmp(const void *a, const void *b){
1313 const int16_t* da = (const int16_t *) a;
1314 const int16_t* db = (const int16_t *) b;
1316 if(da[1] != db[1]) return da[1] - db[1];
1317 else return da[0] - db[0];
1320 static int alloc_qtree(QTree *t, int w, int h){
1321 int lev, x, y, tree_h;
1326 t->max_level= av_log2(2*FFMAX(w,h)-1);
1328 for(lev=t->max_level; lev>=0; lev--){
1329 if(lev!=t->max_level)
1331 t->treedim[lev][0]= w2;
1332 t->treedim[lev][1]= h2;
1333 av_log(NULL, AV_LOG_DEBUG, "alloc %p %d %d %d\n", t, w2, h2, t->max_level);
1337 t->stride= FFMAX(t->stride, w);
1338 tree_h= h + t->treedim[t->max_level-1][1];
1340 t->tree[t->max_level]= av_mallocz(t->stride * tree_h);
1341 t->tree[t->max_level-1]= t->tree[t->max_level] + h*t->stride;
1343 for(lev=t->max_level-2; lev>=0; lev--){
1344 t->tree[lev]= t->tree[lev+1] + t->treedim[lev+1][0];
1349 static void free_qtree(QTree *t){
1351 av_freep(&t->tree[t->max_level]);
1354 static void init_quandtree(QTree *t, DWTELEM *src, int w, int h, int stride){
1355 const int max_level= t->max_level;
1356 const int tree_stride= t->stride;
1357 uint8_t **tree= t->tree;
1358 int lev, x, y, tree_h, w2, h2;
1360 //av_log(NULL, AV_LOG_DEBUG, "init %p %d %d %d %d %d\n", t, w, h, t->max_level, t->treedim[max_level][0], t->treedim[max_level][1]);
1361 assert(w==t->treedim[max_level][0]);
1362 assert(h==t->treedim[max_level][1]);
1366 tree[max_level][x + y*tree_stride]= clip(ABS(src[x + y*stride]), 0, 16);
1370 for(lev=max_level-1; lev>=0; lev--){
1371 w2= t->treedim[lev+1][0]>>1;
1372 h2= t->treedim[lev+1][1]>>1;
1373 for(y=0; y<h2; y++){
1374 for(x=0; x<w2; x++){
1375 tree[lev][x + y*tree_stride]=clip( (tree[lev+1][2*x + 2*y *tree_stride])
1376 + (tree[lev+1][2*x + 1 + 2*y *tree_stride])
1377 + (tree[lev+1][2*x + (2*y+1)*tree_stride])
1378 + (tree[lev+1][2*x + 1 + (2*y+1)*tree_stride])+3, 0, 64)/4;
1381 if(w2 != t->treedim[lev][0]){
1382 for(y=0; y<h2; y++){
1383 tree[lev][w2 + y*tree_stride]=clip( (tree[lev+1][2*w2 + 2*y *tree_stride])
1384 +(tree[lev+1][2*w2 + (2*y+1)*tree_stride])+3, 0, 64)/4;
1387 if(h2 != t->treedim[lev][1]){
1388 for(x=0; x<w2; x++){
1389 tree[lev][x + h2*tree_stride]=clip( (tree[lev+1][2*x + 2*h2*tree_stride])
1390 +(tree[lev+1][2*x + 1 + 2*h2*tree_stride])+3, 0, 64)/4;
1393 if(w2 != t->treedim[lev][0] && h2 != t->treedim[lev][1]){
1394 tree[lev][w2 + h2*tree_stride]= tree[lev+1][2*w2 + 2*h2*tree_stride];
1399 int white_leaf, gray_leaf;
1401 static void encode_branch(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int lev, int x, int y, int first){
1402 const int max_level= b->tree.max_level;
1403 const int pmax_level= b->parent ? b->parent->tree.max_level : 0;
1404 const int tree_stride= b->tree.stride;
1405 const int ptree_stride= b->parent ? b->parent->tree.stride : 0;
1406 int (*treedim)[2]= b->tree.treedim;
1407 int (*ptreedim)[2]= b->parent ? b->parent->tree.treedim : NULL;
1408 uint8_t **tree= b->tree.tree;
1409 uint8_t **ptree= b->parent ? b->parent->tree.tree : NULL;
1412 int l=0, t=0, lt=0, p=0;
1413 int v= tree[lev][x + y*tree_stride];
1415 if(!first && !tree[lev-1][x/2 + y/2*tree_stride])
1418 if(x) l= tree[lev][x - 1 + y*tree_stride];
1420 t= tree[lev][x + (y-1)*tree_stride];
1421 if(x) lt= tree[lev][x - 1 + (y-1)*tree_stride];
1423 if(lev < max_level && parent && x<ptreedim[lev][0] && y<ptreedim[lev][1])
1424 p= ptree[lev - max_level + pmax_level + 1][x + y*ptree_stride];
1426 if(lev != max_level)
1427 context= lev + 32*av_log2(2*(3*(l) + 2*(t) + (lt) + 2*(p)));
1429 int p=0, l=0, lt=0, t=0, rt=0;
1432 t= src[x + (y-1)*stride];
1434 lt= src[x - 1 + (y-1)*stride];
1437 l= src[x - 1 + y*stride];
1443 if(px<b->parent->width && py<b->parent->height){
1444 p= parent[px + py*2*stride];
1447 context= lev + 32*av_log2(2*(3*ABS(l) + 2*ABS(t) + ABS(lt) + ABS(p)));
1450 if( (x&1) && l) sig=1;
1451 else if((y&1) && t) sig=1;
1452 else if((x&1) && (y&1) && lt) sig=1;
1456 if(sig) context+= 8+16;
1457 else context+= 8*(x&1) + 16*(y&1);
1460 if(l||t||lt||(x&1)==0||(y&1)==0||first){
1461 put_cabac(&s->c, &b->state[98][context], !!v);
1468 int /*ll=0, */l=0, lt=0, t=0;
1469 int v= src[x + y*stride];
1472 t= src[x + (y-1)*stride];
1474 lt= src[x - 1 + (y-1)*stride];
1478 l= src[x - 1 + y*stride];
1484 if(px<b->parent->width && py<b->parent->height){
1485 p= parent[px + py*2*stride];
1489 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(p)
1490 /*+ 3*(!!r) + 2*(!!d)*/);
1492 put_symbol(&s->c, b->state[context + 2], ABS(v)-1, 0);
1493 put_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]], v<0);
1494 assert(tree[max_level][x + y*tree_stride]);
1495 assert(tree[max_level-1][x/2 + y/2*tree_stride]);
1499 int r= 2*x+1 < treedim[lev+1][0];
1500 int d= 2*y+1 < treedim[lev+1][1];
1501 encode_branch (s, b, src, parent, stride, lev+1, 2*x , 2*y , 0);
1502 if(r) encode_branch(s, b, src, parent, stride, lev+1, 2*x+1, 2*y , 0);
1503 if(d) encode_branch(s, b, src, parent, stride, lev+1, 2*x , 2*y+1, 0);
1504 if(r&&d)encode_branch(s, b, src, parent, stride, lev+1, 2*x+1, 2*y+1, 0);
1509 static void encode_subband_qtree(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
1510 const int level= b->level;
1511 const int w= b->width;
1512 const int h= b->height;
1515 init_quandtree(&b->tree, src, b->width, b->height, stride);
1518 init_quandtree(&b->parent->tree, parent, b->parent->width, b->parent->height, 2*stride);
1521 for(i=0; i<b->tree.max_level; i++){
1523 for(y=0; y<b->tree.treedim[i][1]; y++){
1524 for(x=0; x<b->tree.treedim[i][0]; x++){
1525 if(b->tree.tree[i][x + y*b->tree.stride])
1529 if(2*count < b->tree.treedim[i][1]*b->tree.treedim[i][0])
1533 //FIXME try recursive scan
1534 for(y=0; y<b->tree.treedim[i][1]; y++){
1535 for(x=0; x<b->tree.treedim[i][0]; x++){
1536 encode_branch(s, b, src, parent, stride, i, x, y, 1);
1539 // encode_branch(s, b, src, parent, stride, 0, 0, 0, 1);
1540 // av_log(NULL, AV_LOG_DEBUG, "%d %d\n", gray_leaf, white_leaf);
1542 for(lev=0; lev<=max_level; lev++){
1543 w2= treedim[lev][0];
1544 h2= treedim[lev][1];
1545 for(y=0; y<h2; y++){
1546 for(x=0; x<w2; x++){
1547 int l= 0, t=0, rt=0, lt=0, p=0;
1548 int v= tree[lev][x + y*tree_stride];
1550 if(lev && !tree[lev-1][x/2 + y/2*tree_stride])
1553 if(x) l= tree[lev][x - 1 + y*tree_stride];
1555 t= tree[lev][x + (y-1)*tree_stride];
1556 if(x) lt= tree[lev][x - 1 + (y-1)*tree_stride];
1557 if(x+1<w2) rt= tree[lev][x + 1 + (y-1)*tree_stride];
1559 if(lev < max_level && parent && x<ptreedim[lev][0] && y<ptreedim[lev][1])
1560 p= ptree[lev][x + y*ptree_stride];
1562 context= lev + 32*av_log2(2*(3*l + 2*t + lt + rt + 8*p));
1564 if( (x&1) && l) sig=1;
1565 else if((y&1) && t) sig=1;
1566 else if((x&1) && (y&1) && lt) sig=1;
1569 if(sig) context+= 8+16;
1570 else context+= 8*(x&1) + 16*(y&1);
1572 if(l||t||lt||(x&1)==0||(y&1)==0)
1573 put_cabac(&s->c, &b->state[98][context], !!v);
1576 if(v && lev==max_level){
1578 int /*ll=0, */l=0, lt=0, t=0, rt=0;
1579 int v= src[x + y*stride];
1582 t= src[x + (y-1)*stride];
1584 lt= src[x - 1 + (y-1)*stride];
1587 rt= src[x + 1 + (y-1)*stride];
1591 l= src[x - 1 + y*stride];
1593 if(orientation==1) ll= src[y + (x-2)*stride];
1594 else ll= src[x - 2 + y*stride];
1601 if(px<b->parent->width && py<b->parent->height){
1602 p= parent[px + py*2*stride];
1606 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(p)
1607 /*+ 3*(!!r) + 2*(!!d)*/);
1609 put_symbol(&s->c, b->state[context + 2], ABS(v)-1, 0);
1610 put_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]], v<0);
1611 assert(tree[max_level][x + y*tree_stride]);
1612 assert(tree[max_level-1][x/2 + y/2*tree_stride]);
1622 static int deint(unsigned int a){
1623 a &= 0x55555555; //0 1 2 3 4 5 6 7 8 9 A B C D E F
1624 a += a & 0x11111111; // 01 23 45 67 89 AB CD EF
1625 a += 3*(a & 0x0F0F0F0F);// 0123 4567 89AB CDEF
1626 a += 15*(a & 0x00FF00FF);// 01234567 89ABCDEF
1627 a +=255*(a & 0x0000FFFF);// 0123456789ABCDEF
1631 static void encode_subband_z0run(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
1632 const int level= b->level;
1633 const int w= b->width;
1634 const int h= b->height;
1643 for(pos=0; ; pos++){
1645 int y= deint(pos>>1);
1646 int v, p=0, pr=0, pd=0;
1647 int /*ll=0, */l=0, lt=0, t=0/*, rt=0*/;
1656 v= src[x + y*stride];
1659 t= src[x + (y-1)*stride];
1661 lt= src[x - 1 + (y-1)*stride];
1664 /*rt= src[x + 1 + (y-1)*stride]*/;
1668 l= src[x - 1 + y*stride];
1670 if(orientation==1) ll= src[y + (x-2)*stride];
1671 else ll= src[x - 2 + y*stride];
1677 if(px<b->parent->width && py<b->parent->height){
1678 p= parent[px + py*2*stride];
1679 /*if(px+1<b->parent->width)
1680 pr= parent[px + 1 + py*2*stride];
1681 if(py+1<b->parent->height)
1682 pd= parent[px + (py+1)*2*stride];*/
1685 if(!(/*ll|*/l|lt|t|/*rt|*/p)){
1687 runs[run_index++]= run;
1695 runs[run_index++]= run;
1697 run= runs[run_index++];
1699 put_symbol(&s->c, b->state[1], run, 0);
1701 for(pos=0; ; pos++){
1703 int y= deint(pos>>1);
1704 int v, p=0, pr=0, pd=0;
1705 int /*ll=0, */l=0, lt=0, t=0/*, rt=0*/;
1712 v= src[x + y*stride];
1715 t= src[x + (y-1)*stride];
1717 lt= src[x - 1 + (y-1)*stride];
1720 // rt= src[x + 1 + (y-1)*stride];
1724 l= src[x - 1 + y*stride];
1726 if(orientation==1) ll= src[y + (x-2)*stride];
1727 else ll= src[x - 2 + y*stride];
1734 if(px<b->parent->width && py<b->parent->height){
1735 p= parent[px + py*2*stride];
1736 /* if(px+1<b->parent->width)
1737 pr= parent[px + 1 + py*2*stride];
1738 if(py+1<b->parent->height)
1739 pd= parent[px + (py+1)*2*stride];*/
1742 if(/*ll|*/l|lt|t|/*rt|*/p){
1743 int context= av_log2(/*ABS(ll) + */2*(3*ABS(l) + ABS(lt) + 2*ABS(t) + /*ABS(rt) +*/ ABS(p)));
1745 put_cabac(&s->c, &b->state[0][context], !!v);
1748 run= runs[run_index++];
1749 put_symbol(&s->c, b->state[1], run, 0);
1757 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + /*ABS(rt) +*/ ABS(p));
1759 put_symbol(&s->c, b->state[context + 2], ABS(v)-1, 0);
1760 put_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]], v<0);
1766 static void encode_subband_bp(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
1767 const int level= b->level;
1768 const int w= b->width;
1769 const int h= b->height;
1774 for(plane=24; plane>=0; plane--){
1782 int d=0, r=0, rd=0, ld=0;
1783 int /*ll=0, */l=0, lt=0, t=0, rt=0;
1784 v= src[x + y*stride];
1787 t= src[x + (y-1)*stride];
1789 lt= src[x - 1 + (y-1)*stride];
1792 rt= src[x + 1 + (y-1)*stride];
1796 l= src[x - 1 + y*stride];
1798 if(orientation==1) ll= src[y + (x-2)*stride];
1799 else ll= src[x - 2 + y*stride];
1803 d= src[x + (y+1)*stride];
1804 if(x) ld= src[x - 1 + (y+1)*stride];
1805 if(x + 1 < w) rd= src[x + 1 + (y+1)*stride];
1808 r= src[x + 1 + y*stride];
1812 if(px<b->parent->width && py<b->parent->height)
1813 p= parent[px + py*2*stride];
1815 #define HIDE(c, plane) c= c>=0 ? c&((-1)<<(plane)) : -((-c)&((-1)<<(plane)));
1828 if(!(/*ll|*/l|lt|t|rt|r|rd|ld|d|p|lv)){
1830 runs[run_index++]= run;
1838 runs[run_index++]= run;
1840 run= runs[run_index++];
1842 put_symbol(&s->c, b->state[1], run, 0);
1847 int /*ll=0, */l=0, lt=0, t=0, rt=0;
1848 int d=0, r=0, rd=0, ld=0;
1849 v= src[x + y*stride];
1852 t= src[x + (y-1)*stride];
1854 lt= src[x - 1 + (y-1)*stride];
1857 rt= src[x + 1 + (y-1)*stride];
1861 l= src[x - 1 + y*stride];
1863 if(orientation==1) ll= src[y + (x-2)*stride];
1864 else ll= src[x - 2 + y*stride];
1868 d= src[x + (y+1)*stride];
1869 if(x) ld= src[x - 1 + (y+1)*stride];
1870 if(x + 1 < w) rd= src[x + 1 + (y+1)*stride];
1873 r= src[x + 1 + y*stride];
1878 if(px<b->parent->width && py<b->parent->height)
1879 p= parent[px + py*2*stride];
1893 if(/*ll|*/l|lt|t|rt|r|rd|ld|d|p|lv){
1894 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p)
1895 +3*ABS(r) + ABS(rd) + 2*ABS(d) + ABS(ld));
1897 if(lv) put_cabac(&s->c, &b->state[99][context + 8*(av_log2(ABS(lv))-plane)], !!(v-lv));
1898 else put_cabac(&s->c, &b->state[ 0][context], !!v);
1902 run= runs[run_index++];
1903 put_symbol(&s->c, b->state[1], run, 0);
1911 int context= clip(quant3b[l&0xFF] + quant3b[r&0xFF], -1,1)
1912 + 3*clip(quant3b[t&0xFF] + quant3b[d&0xFF], -1,1);
1913 put_cabac(&s->c, &b->state[0][16 + 1 + 3 + context], v<0);
1922 static void encode_subband_X(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
1923 const int level= b->level;
1924 const int w= b->width;
1925 const int h= b->height;
1929 if(orientation==3 && parent && 0){
1930 int16_t candidate[w*h][2];
1932 int16_t boarder[3][w*h*4][2];
1933 int16_t significant[w*h][2];
1934 int candidate_count=0;
1935 int boarder_count[3]={0,0,0};
1936 int significant_count=0;
1939 int primary= orientation==1;
1941 memset(candidate, 0, sizeof(candidate));
1942 memset(state, 0, sizeof(state));
1943 memset(boarder, 0, sizeof(boarder));
1947 if(parent[(x>>1) + (y>>1)*2*stride])
1948 SVI(candidate, candidate_count, x, y)
1953 while(candidate_count && !boarder_count[0] && !boarder_count[1] && !boarder_count[2]){
1955 x= candidate[ candidate_count][0];
1956 y= candidate[ candidate_count][1];
1960 v= !!src[x + y*stride];
1961 put_cabac(&s->c, &b->state[0][0], v);
1963 SVI(significant, significant_count, x,y)
1964 if(x && !state[x - 1 + y *w]) SVI(boarder[0],boarder_count[0],x-1,y )
1965 if(y && !state[x + (y-1)*w]) SVI(boarder[1],boarder_count[1],x ,y-1)
1966 if(x+1<w && !state[x + 1 + y *w]) SVI(boarder[0],boarder_count[0],x+1,y )
1967 if(y+1<h && !state[x + (y+1)*w]) SVI(boarder[1],boarder_count[1],x ,y+1)
1968 if(x && y && !state[x - 1 + (y-1)*w]) SVI(boarder[2],boarder_count[2],x-1,y-1)
1969 if(x && y+1<h && !state[x - 1 + (y+1)*w]) SVI(boarder[2],boarder_count[2],x-1,y+1)
1970 if(x+1<w && y+1<h && !state[x + 1 + (y+1)*w]) SVI(boarder[2],boarder_count[2],x+1,y+1)
1971 if(x+1<w && y && !state[x + 1 + (y-1)*w]) SVI(boarder[2],boarder_count[2],x+1,y-1)
1974 while(!boarder_count[0] && !boarder_count[1] && !boarder_count[2] && rle_pos < w*h){
1976 for(; rle_pos < w*h;){
1977 x= rle_pos % w; //FIXME speed
1983 v= !!src[x + y*stride];
1985 put_symbol(&s->c, b->state[1], run, 0);
1986 SVI(significant, significant_count, x,y)
1987 if(x && !state[x - 1 + y *w]) SVI(boarder[0],boarder_count[0],x-1,y )
1988 if(y && !state[x + (y-1)*w]) SVI(boarder[1],boarder_count[1],x ,y-1)
1989 if(x+1<w && !state[x + 1 + y *w]) SVI(boarder[0],boarder_count[0],x+1,y )
1990 if(y+1<h && !state[x + (y+1)*w]) SVI(boarder[1],boarder_count[1],x ,y+1)
1991 if(x && y && !state[x - 1 + (y-1)*w]) SVI(boarder[2],boarder_count[2],x-1,y-1)
1992 if(x && y+1<h && !state[x - 1 + (y+1)*w]) SVI(boarder[2],boarder_count[2],x-1,y+1)
1993 if(x+1<w && y+1<h && !state[x + 1 + (y+1)*w]) SVI(boarder[2],boarder_count[2],x+1,y+1)
1994 if(x+1<w && y && !state[x + 1 + (y-1)*w]) SVI(boarder[2],boarder_count[2],x+1,y-1)
1996 //FIXME note only right & down can be boarders
2001 if(!boarder_count[0] && !boarder_count[1] && !boarder_count[2])
2004 while(boarder_count[0] || boarder_count[1] || boarder_count[2]){
2007 if (boarder_count[ primary]) index= primary;
2008 else if(boarder_count[1-primary]) index=1-primary;
2011 boarder_count[index]--;
2012 x= boarder[index][ boarder_count[index] ][0];
2013 y= boarder[index][ boarder_count[index] ][1];
2014 if(state[x + y*w]) //FIXME maybe check earlier
2017 v= !!src[x + y*stride];
2018 put_cabac(&s->c, &b->state[0][index+1], v);
2020 SVI(significant, significant_count, x,y)
2021 if(x && !state[x - 1 + y *w]) SVI(boarder[0],boarder_count[0],x-1,y )
2022 if(y && !state[x + (y-1)*w]) SVI(boarder[1],boarder_count[1],x ,y-1)
2023 if(x+1<w && !state[x + 1 + y *w]) SVI(boarder[0],boarder_count[0],x+1,y )
2024 if(y+1<h && !state[x + (y+1)*w]) SVI(boarder[1],boarder_count[1],x ,y+1)
2025 if(x && y && !state[x - 1 + (y-1)*w]) SVI(boarder[2],boarder_count[2],x-1,y-1)
2026 if(x && y+1<h && !state[x - 1 + (y+1)*w]) SVI(boarder[2],boarder_count[2],x-1,y+1)
2027 if(x+1<w && y+1<h && !state[x + 1 + (y+1)*w]) SVI(boarder[2],boarder_count[2],x+1,y+1)
2028 if(x+1<w && y && !state[x + 1 + (y-1)*w]) SVI(boarder[2],boarder_count[2],x+1,y-1)
2032 //FIXME sort significant coeffs maybe
2034 qsort(significant, significant_count, sizeof(int16_t[2]), sig_cmp);
2038 while(significant_count){
2039 int context= 3 + quant7[last_v&0xFF]; //use significance of suroundings
2040 significant_count--;
2041 x= significant[significant_count][0];//FIXME try opposit direction
2042 y= significant[significant_count][1];
2043 v= src[x + y*stride];
2044 put_symbol(&s->c, b->state[context + 2], v, 1); //FIXME try to avoid first bit, try this with the old code too!!
2051 static void encode_subband_c0run(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
2052 const int level= b->level;
2053 const int w= b->width;
2054 const int h= b->height;
2065 int /*ll=0, */l=0, lt=0, t=0, rt=0;
2066 v= src[x + y*stride];
2069 t= src[x + (y-1)*stride];
2071 lt= src[x - 1 + (y-1)*stride];
2074 rt= src[x + 1 + (y-1)*stride];
2078 l= src[x - 1 + y*stride];
2080 if(orientation==1) ll= src[y + (x-2)*stride];
2081 else ll= src[x - 2 + y*stride];
2087 if(px<b->parent->width && py<b->parent->height)
2088 p= parent[px + py*2*stride];
2090 if(!(/*ll|*/l|lt|t|rt|p)){
2092 runs[run_index++]= run;
2100 runs[run_index++]= run;
2102 run= runs[run_index++];
2104 put_symbol2(&s->c, b->state[1], run, 3);
2109 int /*ll=0, */l=0, lt=0, t=0, rt=0;
2110 v= src[x + y*stride];
2113 t= src[x + (y-1)*stride];
2115 lt= src[x - 1 + (y-1)*stride];
2118 rt= src[x + 1 + (y-1)*stride];
2122 l= src[x - 1 + y*stride];
2124 if(orientation==1) ll= src[y + (x-2)*stride];
2125 else ll= src[x - 2 + y*stride];
2131 if(px<b->parent->width && py<b->parent->height)
2132 p= parent[px + py*2*stride];
2134 if(/*ll|*/l|lt|t|rt|p){
2135 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
2137 put_cabac(&s->c, &b->state[0][context], !!v);
2140 run= runs[run_index++];
2142 put_symbol2(&s->c, b->state[1], run, 3);
2150 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
2152 put_symbol(&s->c, b->state[context + 2], ABS(v)-1, 0);
2153 put_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]], v<0);
2160 static void encode_subband_dzr(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
2161 const int level= b->level;
2162 const int w= b->width;
2163 const int h= b->height;
2168 int runs[16][w*h]; //FIXME do something about the size
2169 int run_index[16]={0};
2170 int positions[2][w];
2171 int distances[2][w];
2176 int * pos = positions[ y&1];
2177 int *last_pos = positions[(y&1)^1];
2178 int * dist= distances[ y&1];
2179 int *last_dist= distances[(y&1)^1];
2181 int last_dist_index=0;
2184 int p=0, l=0, lt=0, t=0, rt=0;
2185 int v= src[x + y*stride];
2188 t= src[x + (y-1)*stride];
2190 lt= src[x - 1 + (y-1)*stride];
2193 rt= src[x + 1 + (y-1)*stride];
2197 l= src[x - 1 + y*stride];
2202 if(px<b->parent->width && py<b->parent->height)
2203 p= parent[px + py*2*stride];
2205 if(last_dist_index < dist_count && last_pos[last_dist_index] == x){
2206 if(dist_index==0 || x - pos[dist_index-1] > dist[dist_index-1] - last_dist[last_dist_index]){
2208 dist[dist_index++]= last_dist[last_dist_index];
2217 if(last_dist_index < dist_count)
2218 cur_dist= last_pos[last_dist_index] - x + y - last_dist[last_dist_index];
2220 cur_dist= FFMIN(cur_dist, x - pos[dist_index-1] + y - dist[dist_index-1]);
2221 assert(cur_dist>=2);
2222 run_class= av_log2(cur_dist+62);
2225 runs[run_class][run_index[run_class]++]= run[run_class];
2232 while(dist_index && x - pos[dist_index-1] <= y - dist[dist_index-1])
2235 dist[dist_index++]= y;
2238 dist_count= dist_index;
2240 for(i=0; i<12; i++){
2241 runs[i][run_index[i]++]= run[i];
2249 int * pos = positions[ y&1];
2250 int *last_pos = positions[(y&1)^1];
2251 int * dist= distances[ y&1];
2252 int *last_dist= distances[(y&1)^1];
2254 int last_dist_index=0;
2257 int p=0, l=0, lt=0, t=0, rt=0;
2258 int v= src[x + y*stride];
2261 t= src[x + (y-1)*stride];
2263 lt= src[x - 1 + (y-1)*stride];
2266 rt= src[x + 1 + (y-1)*stride];
2270 l= src[x - 1 + y*stride];
2275 if(px<b->parent->width && py<b->parent->height)
2276 p= parent[px + py*2*stride];
2278 if(last_dist_index < dist_count && last_pos[last_dist_index] == x){
2279 if(dist_index==0 || x - pos[dist_index-1] > dist[dist_index-1] - last_dist[last_dist_index]){
2281 dist[dist_index++]= last_dist[last_dist_index];
2286 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
2288 put_cabac(&s->c, &b->state[0][context], !!v);
2293 if(last_dist_index < dist_count)
2294 cur_dist= last_pos[last_dist_index] - x + y - last_dist[last_dist_index];
2296 cur_dist= FFMIN(cur_dist, x - pos[dist_index-1] + y - dist[dist_index-1]);
2297 assert(cur_dist>=2);
2298 assert(!dist_index || (pos[dist_index-1] >= 0 && pos[dist_index-1] <w));
2299 assert(last_dist_index >= dist_count || (last_pos[last_dist_index] >= 0 && last_pos[last_dist_index] <w));
2300 assert(!dist_index || dist[dist_index-1] <= y);
2301 assert(last_dist_index >= dist_count || last_dist[last_dist_index] < y);
2302 assert(cur_dist <= y + FFMAX(x, w-x-1));
2303 run_class= av_log2(cur_dist+62);
2305 if(!run_index[run_class]){
2306 run[run_class]= runs[run_class][run_index[run_class]++];
2307 put_symbol(&s->c, b->state[run_class+1], run[run_class], 0);
2309 if(!run[run_class]){
2310 run[run_class]= runs[run_class][run_index[run_class]++];
2311 put_symbol(&s->c, b->state[run_class+1], run[run_class], 0);
2319 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
2321 put_symbol(&s->c, b->state[context + 16], ABS(v)-1, 0);
2322 put_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]], v<0);
2324 while(dist_index && x - pos[dist_index-1] <= y - dist[dist_index-1])
2327 dist[dist_index++]= y;
2330 dist_count= dist_index;
2335 static void encode_subband(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
2336 // encode_subband_qtree(s, b, src, parent, stride, orientation);
2337 // encode_subband_z0run(s, b, src, parent, stride, orientation);
2338 encode_subband_c0run(s, b, src, parent, stride, orientation);
2339 // encode_subband_dzr(s, b, src, parent, stride, orientation);
2342 static inline void decode_subband(SnowContext *s, SubBand *b, DWTELEM *src, DWTELEM *parent, int stride, int orientation){
2343 const int level= b->level;
2344 const int w= b->width;
2345 const int h= b->height;
2350 for(y=0; y<b->height; y++)
2351 memset(&src[y*stride], 0, b->width*sizeof(DWTELEM));
2354 for(plane=24; plane>=0; plane--){
2357 run= get_symbol(&s->c, b->state[1], 0);
2359 #define HIDE(c, plane) c= c>=0 ? c&((-1)<<(plane)) : -((-c)&((-1)<<(plane)));
2364 int /*ll=0, */l=0, lt=0, t=0, rt=0;
2365 int d=0, r=0, rd=0, ld=0;
2366 lv= src[x + y*stride];
2369 t= src[x + (y-1)*stride];
2371 lt= src[x - 1 + (y-1)*stride];
2374 rt= src[x + 1 + (y-1)*stride];
2378 l= src[x - 1 + y*stride];
2380 if(orientation==1) ll= src[y + (x-2)*stride];
2381 else ll= src[x - 2 + y*stride];
2385 d= src[x + (y+1)*stride];
2386 if(x) ld= src[x - 1 + (y+1)*stride];
2387 if(x + 1 < w) rd= src[x + 1 + (y+1)*stride];
2390 r= src[x + 1 + y*stride];
2395 if(px<b->parent->width && py<b->parent->height)
2396 p= parent[px + py*2*stride];
2399 if(/*ll|*/l|lt|t|rt|r|rd|ld|d|p|lv){
2400 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p)
2401 +3*ABS(r) + ABS(rd) + 2*ABS(d) + ABS(ld));
2404 assert(context + 8*av_log2(ABS(lv)) < 512 - 100);
2405 if(get_cabac(&s->c, &b->state[99][context + 8*(av_log2(ABS(lv))-plane)])){
2406 if(lv<0) v= lv - (1<<plane);
2407 else v= lv + (1<<plane);
2411 v= get_cabac(&s->c, &b->state[ 0][context]) << plane;
2416 run= get_symbol(&s->c, b->state[1], 0);
2424 int context= clip(quant3b[l&0xFF] + quant3b[r&0xFF], -1,1)
2425 + 3*clip(quant3b[t&0xFF] + quant3b[d&0xFF], -1,1);
2426 if(get_cabac(&s->c, &b->state[0][16 + 1 + 3 + context]))
2429 src[x + y*stride]= v;
2436 int tree[10][w*h]; //FIXME space waste ...
2439 const int max_level= av_log2(2*FFMAX(w,h)-1);
2441 memset(tree, 0, sizeof(tree));
2443 // assert(w%2==0 && h%2==0);
2445 for(lev=max_level; lev>=0; lev--){
2446 treedim[lev][0]= w2;
2447 treedim[lev][1]= h2;
2452 for(lev=0; lev<=max_level; lev++){
2453 w2= treedim[lev][0];
2454 h2= treedim[lev][1];
2455 for(y=0; y<h2; y++){
2456 for(x=0; x<w2; x++){
2459 if(lev && !tree[lev-1][x/2 + y/2*w])
2462 if(x) l= tree[lev][x - 1 + y*w];
2463 if(y) t= tree[lev][x + (y-1)*w];
2465 context= lev + 8*(!!l) + 16*(!!t);
2466 tree[lev][x + y*w]= get_cabac(&s->c, &b->state[98][context]);
2471 for(y=0; y<b->height; y++)
2472 memset(&src[y*stride], 0, b->width*sizeof(DWTELEM));
2477 int /*ll=0, */l=0, lt=0, t=0, rt=0;
2480 t= src[x + (y-1)*stride];
2482 lt= src[x - 1 + (y-1)*stride];
2485 rt= src[x + 1 + (y-1)*stride];
2489 l= src[x - 1 + y*stride];
2491 if(orientation==1) ll= src[y + (x-2)*stride];
2492 else ll= src[x - 2 + y*stride];
2498 if(px<b->parent->width && py<b->parent->height)
2499 p= parent[px + py*2*stride];
2501 if(tree[max_level][x + y*w]){
2502 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
2503 v= get_symbol(&s->c, b->state[context + 2], 0) + 1;
2504 if(get_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]]))
2506 src[x + y*stride]= v;
2510 if(level+1 == s->spatial_decomposition_count){
2511 STOP_TIMER("decode_subband")
2520 for(y=0; y<b->height; y++)
2521 memset(&src[y*stride], 0, b->width*sizeof(DWTELEM));
2523 run= get_symbol2(&s->c, b->state[1], 3);
2527 int /*ll=0, */l=0, lt=0, t=0, rt=0;
2530 t= src[x + (y-1)*stride];
2532 lt= src[x - 1 + (y-1)*stride];
2535 rt= src[x + 1 + (y-1)*stride];
2539 l= src[x - 1 + y*stride];
2541 if(orientation==1) ll= src[y + (x-2)*stride];
2542 else ll= src[x - 2 + y*stride];
2548 if(px<b->parent->width && py<b->parent->height)
2549 p= parent[px + py*2*stride];
2551 if(/*ll|*/l|lt|t|rt|p){
2552 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
2554 v=get_cabac(&s->c, &b->state[0][context]);
2557 run= get_symbol2(&s->c, b->state[1], 3);
2558 //FIXME optimize this here
2559 //FIXME try to store a more naive run
2567 int context= av_log2(/*ABS(ll) + */3*ABS(l) + ABS(lt) + 2*ABS(t) + ABS(rt) + ABS(p));
2568 v= get_symbol(&s->c, b->state[context + 2], 0) + 1;
2569 if(get_cabac(&s->c, &b->state[0][16 + 1 + 3 + quant3b[l&0xFF] + 3*quant3b[t&0xFF]]))
2571 src[x + y*stride]= v;
2575 if(level+1 == s->spatial_decomposition_count){
2576 STOP_TIMER("decode_subband")
2583 static void reset_contexts(SnowContext *s){
2584 int plane_index, level, orientation;
2586 for(plane_index=0; plane_index<2; plane_index++){
2587 for(level=0; level<s->spatial_decomposition_count; level++){
2588 for(orientation=level ? 1:0; orientation<4; orientation++){
2589 memset(s->plane[plane_index].band[level][orientation].state, 0, sizeof(s->plane[plane_index].band[level][orientation].state));
2593 memset(s->mb_band.state, 0, sizeof(s->mb_band.state));
2594 memset(s->mv_band[0].state, 0, sizeof(s->mv_band[0].state));
2595 memset(s->mv_band[1].state, 0, sizeof(s->mv_band[1].state));
2596 memset(s->header_state, 0, sizeof(s->header_state));
2599 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){
2602 for(y=0; y < b_h+5; y++){
2603 for(x=0; x < b_w; x++){
2604 int a0= src[x + y*stride];
2605 int a1= src[x + 1 + y*stride];
2606 int a2= src[x + 2 + y*stride];
2607 int a3= src[x + 3 + y*stride];
2608 int a4= src[x + 4 + y*stride];
2609 int a5= src[x + 5 + y*stride];
2610 // int am= 9*(a1+a2) - (a0+a3);
2611 int am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
2612 // int am= 18*(a2+a3) - 2*(a1+a4);
2613 // int aL= (-7*a0 + 105*a1 + 35*a2 - 5*a3)>>3;
2614 // int aR= (-7*a3 + 105*a2 + 35*a1 - 5*a0)>>3;
2616 // if(b_w==16) am= 8*(a1+a2);
2618 if(dx<8) tmp[x + y*stride]= (32*a2*( 8-dx) + am* dx + 128)>>8;
2619 else tmp[x + y*stride]= ( am*(16-dx) + 32*a3*(dx-8) + 128)>>8;
2621 /* if (dx< 4) tmp[x + y*stride]= (16*a1*( 4-dx) + aL* dx + 32)>>6;
2622 else if(dx< 8) tmp[x + y*stride]= ( aL*( 8-dx) + am*(dx- 4) + 32)>>6;
2623 else if(dx<12) tmp[x + y*stride]= ( am*(12-dx) + aR*(dx- 8) + 32)>>6;
2624 else tmp[x + y*stride]= ( aR*(16-dx) + 16*a2*(dx-12) + 32)>>6;*/
2627 for(y=0; y < b_h; y++){
2628 for(x=0; x < b_w; x++){
2629 int a0= tmp[x + y *stride];
2630 int a1= tmp[x + (y + 1)*stride];
2631 int a2= tmp[x + (y + 2)*stride];
2632 int a3= tmp[x + (y + 3)*stride];
2633 int a4= tmp[x + (y + 4)*stride];
2634 int a5= tmp[x + (y + 5)*stride];
2635 int am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
2636 // int am= 18*(a2+a3) - 2*(a1+a4);
2637 /* int aL= (-7*a0 + 105*a1 + 35*a2 - 5*a3)>>3;
2638 int aR= (-7*a3 + 105*a2 + 35*a1 - 5*a0)>>3;*/
2640 // if(b_w==16) am= 8*(a1+a2);
2642 if(dy<8) dst[x + y*stride]= (32*a2*( 8-dy) + am* dy + 128)>>8;
2643 else dst[x + y*stride]= ( am*(16-dy) + 32*a3*(dy-8) + 128)>>8;
2645 /* if (dy< 4) tmp[x + y*stride]= (16*a1*( 4-dy) + aL* dy + 32)>>6;
2646 else if(dy< 8) tmp[x + y*stride]= ( aL*( 8-dy) + am*(dy- 4) + 32)>>6;
2647 else if(dy<12) tmp[x + y*stride]= ( am*(12-dy) + aR*(dy- 8) + 32)>>6;
2648 else tmp[x + y*stride]= ( aR*(16-dy) + 16*a2*(dy-12) + 32)>>6;*/
2653 #define mcb(dx,dy,b_w)\
2654 static void mc_block ## dx ## dy(uint8_t *dst, uint8_t *src, int stride){\
2655 uint8_t tmp[stride*(b_w+5)];\
2656 mc_block(dst, src-2-2*stride, tmp, stride, b_w, b_w, dx, dy);\
2676 #define mca(dx,dy,b_w)\
2677 static void mc_block_hpel ## dx ## dy(uint8_t *dst, uint8_t *src, int stride, int h){\
2678 uint8_t tmp[stride*(b_w+5)];\
2680 mc_block(dst, src-2-2*stride, tmp, stride, b_w, b_w, dx, dy);\
2688 static void add_xblock(DWTELEM *dst, uint8_t *src, uint8_t *obmc, int s_x, int s_y, int b_w, int b_h, int mv_x, int mv_y, int w, int h, int dst_stride, int src_stride, int obmc_stride, int mb_type, int add){
2689 uint8_t tmp[src_stride*(b_h+5)]; //FIXME move to context to gurantee alignment
2696 }else if(s_x + b_w > w){
2700 obmc -= s_y*obmc_stride;
2703 }else if(s_y + b_h> h){
2707 if(b_w<=0 || b_h<=0) return;
2709 dst += s_x + s_y*dst_stride;
2712 src += s_x + s_y*src_stride;
2713 for(y=0; y < b_h; y++){
2714 for(x=0; x < b_w; x++){
2715 if(add) dst[x + y*dst_stride] += obmc[x + y*obmc_stride] * 128 * (256/OBMC_MAX);
2716 else dst[x + y*dst_stride] -= obmc[x + y*obmc_stride] * 128 * (256/OBMC_MAX);
2722 // int dxy= (mv_x&1) + 2*(mv_y&1);
2724 s_x += (mv_x>>4) - 2;
2725 s_y += (mv_y>>4) - 2;
2726 src += s_x + s_y*src_stride;
2729 if( (unsigned)s_x >= w - b_w - 4
2730 || (unsigned)s_y >= h - b_h - 4){
2731 ff_emulated_edge_mc(tmp + 32, src, src_stride, b_w+5, b_h+5, s_x, s_y, w, h);
2736 mc_block(tmp, src, tmp + 64+8, src_stride, b_w, b_h, dx, dy);
2739 for(y=0; y < b_h; y++){
2740 for(x=0; x < b_w; x++){
2741 sum += src[x+ y*src_stride];
2744 sum= (sum + b_h*b_w/2) / (b_h*b_w);
2745 for(y=0; y < b_h; y++){
2746 for(x=0; x < b_w; x++){
2747 tmp[x + y*src_stride]= sum;
2752 for(y=0; y < b_h; y++){
2753 for(x=0; x < b_w; x++){
2754 if(add) dst[x + y*dst_stride] += obmc[x + y*obmc_stride] * tmp[x + y*src_stride] * (256/OBMC_MAX);
2755 else dst[x + y*dst_stride] -= obmc[x + y*obmc_stride] * tmp[x + y*src_stride] * (256/OBMC_MAX);
2761 static void predict_plane(SnowContext *s, DWTELEM *buf, int plane_index, int add){
2762 Plane *p= &s->plane[plane_index];
2763 const int mb_w= s->mb_band.width;
2764 const int mb_h= s->mb_band.height;
2765 const int mb_stride= s->mb_band.stride;
2766 int x, y, mb_x, mb_y;
2767 int scale = plane_index ? s->mv_scale : 2*s->mv_scale;
2768 int block_w = plane_index ? 8 : 16;
2769 uint8_t *obmc = plane_index ? obmc16 : obmc32;
2770 int obmc_stride= plane_index ? 16 : 32;
2771 int ref_stride= s->last_picture.linesize[plane_index];
2772 uint8_t *ref = s->last_picture.data[plane_index];
2776 if(s->avctx->debug&512){
2779 if(add) buf[x + y*w]+= 128*256;
2780 else buf[x + y*w]-= 128*256;
2786 for(mb_y=-1; mb_y<=mb_h; mb_y++){
2787 for(mb_x=-1; mb_x<=mb_w; mb_x++){
2788 int index= clip(mb_x, 0, mb_w-1) + clip(mb_y, 0, mb_h-1)*mb_stride;
2790 add_xblock(buf, ref, obmc,
2791 block_w*mb_x - block_w/2,
2792 block_w*mb_y - block_w/2,
2793 2*block_w, 2*block_w,
2794 s->mv_band[0].buf[index]*scale, s->mv_band[1].buf[index]*scale,
2796 w, ref_stride, obmc_stride,
2797 s->mb_band.buf[index], add);
2803 static void quantize(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int bias){
2804 const int level= b->level;
2805 const int w= b->width;
2806 const int h= b->height;
2807 const int qlog= clip(s->qlog + b->qlog, 0, 128);
2808 const int qmul= qexp[qlog&7]<<(qlog>>3);
2809 int x,y, thres1, thres2;
2814 bias= bias ? 0 : (3*qmul)>>3;
2815 thres1= ((qmul - bias)>>QEXPSHIFT) - 1;
2821 int i= src[x + y*stride];
2823 if((unsigned)(i+thres1) > thres2){
2826 i/= qmul; //FIXME optimize
2827 src[x + y*stride]= i;
2831 i/= qmul; //FIXME optimize
2832 src[x + y*stride]= -i;
2835 src[x + y*stride]= 0;
2841 int i= src[x + y*stride];
2843 if((unsigned)(i+thres1) > thres2){
2846 i= (i + bias) / qmul; //FIXME optimize
2847 src[x + y*stride]= i;
2851 i= (i + bias) / qmul; //FIXME optimize
2852 src[x + y*stride]= -i;
2855 src[x + y*stride]= 0;
2859 if(level+1 == s->spatial_decomposition_count){
2860 // STOP_TIMER("quantize")
2864 static void dequantize(SnowContext *s, SubBand *b, DWTELEM *src, int stride){
2865 const int level= b->level;
2866 const int w= b->width;
2867 const int h= b->height;
2868 const int qlog= clip(s->qlog + b->qlog, 0, 128);
2869 const int qmul= qexp[qlog&7]<<(qlog>>3);
2870 const int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
2877 int i= src[x + y*stride];
2879 src[x + y*stride]= -((-i*qmul + qadd)>>(QEXPSHIFT)); //FIXME try different bias
2881 src[x + y*stride]= (( i*qmul + qadd)>>(QEXPSHIFT));
2887 static void decorrelate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
2888 const int w= b->width;
2889 const int h= b->height;
2892 for(y=h-1; y>=0; y--){
2893 for(x=w-1; x>=0; x--){
2894 int i= x + y*stride;
2898 if(y && x+1<w) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
2899 else src[i] -= src[i - 1];
2901 if(y) src[i] -= mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
2902 else src[i] -= src[i - 1];
2905 if(y) src[i] -= src[i - stride];
2911 static void correlate(SnowContext *s, SubBand *b, DWTELEM *src, int stride, int inverse, int use_median){
2912 const int w= b->width;
2913 const int h= b->height;
2918 int i= x + y*stride;
2922 if(y && x+1<w) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - stride + 1]);
2923 else src[i] += src[i - 1];
2925 if(y) src[i] += mid_pred(src[i - 1], src[i - stride], src[i - 1] + src[i - stride] - src[i - 1 - stride]);
2926 else src[i] += src[i - 1];
2929 if(y) src[i] += src[i - stride];
2935 static void encode_header(SnowContext *s){
2936 int plane_index, level, orientation;
2938 put_cabac(&s->c, s->header_state, s->keyframe); // state clearing stuff?
2940 put_symbol(&s->c, s->header_state, s->version, 0);
2941 put_symbol(&s->c, s->header_state, s->temporal_decomposition_type, 0);
2942 put_symbol(&s->c, s->header_state, s->temporal_decomposition_count, 0);
2943 put_symbol(&s->c, s->header_state, s->spatial_decomposition_count, 0);
2944 put_symbol(&s->c, s->header_state, s->colorspace_type, 0);
2945 put_symbol(&s->c, s->header_state, s->b_width, 0);
2946 put_symbol(&s->c, s->header_state, s->b_height, 0);
2947 put_symbol(&s->c, s->header_state, s->chroma_h_shift, 0);
2948 put_symbol(&s->c, s->header_state, s->chroma_v_shift, 0);
2949 put_cabac(&s->c, s->header_state, s->spatial_scalability);
2950 // put_cabac(&s->c, s->header_state, s->rate_scalability);
2952 for(plane_index=0; plane_index<2; plane_index++){
2953 for(level=0; level<s->spatial_decomposition_count; level++){
2954 for(orientation=level ? 1:0; orientation<4; orientation++){
2955 if(orientation==2) continue;
2956 put_symbol(&s->c, s->header_state, s->plane[plane_index].band[level][orientation].qlog, 1);
2961 put_symbol(&s->c, s->header_state, s->spatial_decomposition_type, 0);
2962 put_symbol(&s->c, s->header_state, s->qlog, 1);
2963 put_symbol(&s->c, s->header_state, s->mv_scale, 0);
2964 put_symbol(&s->c, s->header_state, s->qbias, 1);
2967 static int decode_header(SnowContext *s){
2968 int plane_index, level, orientation;
2970 s->keyframe= get_cabac(&s->c, s->header_state);
2972 s->version= get_symbol(&s->c, s->header_state, 0);
2974 av_log(s->avctx, AV_LOG_ERROR, "version %d not supported", s->version);
2977 s->temporal_decomposition_type= get_symbol(&s->c, s->header_state, 0);
2978 s->temporal_decomposition_count= get_symbol(&s->c, s->header_state, 0);
2979 s->spatial_decomposition_count= get_symbol(&s->c, s->header_state, 0);
2980 s->colorspace_type= get_symbol(&s->c, s->header_state, 0);
2981 s->b_width= get_symbol(&s->c, s->header_state, 0);
2982 s->b_height= get_symbol(&s->c, s->header_state, 0);
2983 s->chroma_h_shift= get_symbol(&s->c, s->header_state, 0);
2984 s->chroma_v_shift= get_symbol(&s->c, s->header_state, 0);
2985 s->spatial_scalability= get_cabac(&s->c, s->header_state);
2986 // s->rate_scalability= get_cabac(&s->c, s->header_state);
2988 for(plane_index=0; plane_index<3; plane_index++){
2989 for(level=0; level<s->spatial_decomposition_count; level++){
2990 for(orientation=level ? 1:0; orientation<4; orientation++){
2992 if (plane_index==2) q= s->plane[1].band[level][orientation].qlog;
2993 else if(orientation==2) q= s->plane[plane_index].band[level][1].qlog;
2994 else q= get_symbol(&s->c, s->header_state, 1);
2995 s->plane[plane_index].band[level][orientation].qlog= q;
3001 s->spatial_decomposition_type= get_symbol(&s->c, s->header_state, 0);
3002 if(s->spatial_decomposition_type > 2){
3003 av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_type %d not supported", s->spatial_decomposition_type);
3007 s->qlog= get_symbol(&s->c, s->header_state, 1);
3008 s->mv_scale= get_symbol(&s->c, s->header_state, 0);
3009 s->qbias= get_symbol(&s->c, s->header_state, 1);
3014 static int common_init(AVCodecContext *avctx){
3015 SnowContext *s = avctx->priv_data;
3017 int level, orientation, plane_index, dec;
3021 dsputil_init(&s->dsp, avctx);
3024 s->dsp.put_qpel_pixels_tab [0][dy+dx/4]=\
3025 s->dsp.put_no_rnd_qpel_pixels_tab[0][dy+dx/4]=\
3026 mc_block ## dx ## dy;
3045 #define mcfh(dx,dy)\
3046 s->dsp.put_pixels_tab [0][dy/4+dx/8]=\
3047 s->dsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
3048 mc_block_hpel ## dx ## dy;
3055 dec= s->spatial_decomposition_count= 5;
3056 s->spatial_decomposition_type= avctx->prediction_method; //FIXME add decorrelator type r transform_type
3058 s->chroma_h_shift= 1; //FIXME XXX
3059 s->chroma_v_shift= 1;
3061 // dec += FFMAX(s->chroma_h_shift, s->chroma_v_shift);
3063 s->b_width = (s->avctx->width +(1<<dec)-1)>>dec;
3064 s->b_height= (s->avctx->height+(1<<dec)-1)>>dec;
3066 s->spatial_dwt_buffer= av_mallocz(s->b_width*s->b_height*sizeof(DWTELEM)<<(2*dec));
3067 s->pred_buffer= av_mallocz(s->b_width*s->b_height*sizeof(DWTELEM)<<(2*dec));
3069 s->mv_scale= (s->avctx->flags & CODEC_FLAG_QPEL) ? 2 : 4;
3071 for(plane_index=0; plane_index<3; plane_index++){
3072 int w= s->avctx->width;
3073 int h= s->avctx->height;
3076 w>>= s->chroma_h_shift;
3077 h>>= s->chroma_v_shift;
3079 s->plane[plane_index].width = w;
3080 s->plane[plane_index].height= h;
3081 av_log(NULL, AV_LOG_DEBUG, "%d %d\n", w, h);
3082 for(level=s->spatial_decomposition_count-1; level>=0; level--){
3083 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3084 SubBand *b= &s->plane[plane_index].band[level][orientation];
3086 b->buf= s->spatial_dwt_buffer;
3088 b->stride= s->plane[plane_index].width << (s->spatial_decomposition_count - level);
3089 b->width = (w + !(orientation&1))>>1;
3090 b->height= (h + !(orientation>1))>>1;
3092 if(orientation&1) b->buf += (w+1)>>1;
3093 if(orientation>1) b->buf += b->stride>>1;
3095 // alloc_qtree(&b->tree, b->width, b->height);
3098 b->parent= &s->plane[plane_index].band[level-1][orientation];
3105 //FIXME init_subband() ?
3106 s->mb_band.stride= s->mv_band[0].stride= s->mv_band[1].stride=
3107 s->mb_band.width = s->mv_band[0].width = s->mv_band[1].width = (s->avctx->width + 15)>>4;
3108 s->mb_band.height= s->mv_band[0].height= s->mv_band[1].height= (s->avctx->height+ 15)>>4;
3109 s->mb_band .buf= av_mallocz(s->mb_band .stride * s->mb_band .height*sizeof(DWTELEM));
3110 s->mv_band[0].buf= av_mallocz(s->mv_band[0].stride * s->mv_band[0].height*sizeof(DWTELEM));
3111 s->mv_band[1].buf= av_mallocz(s->mv_band[1].stride * s->mv_band[1].height*sizeof(DWTELEM));
3112 /* alloc_qtree(&s->mb_band .tree, s->mb_band .width, s->mb_band .height); //FIXME free these 3
3113 alloc_qtree(&s->mv_band[0].tree, s->mv_band[0].width, s->mv_band[0].height);
3114 alloc_qtree(&s->mv_band[1].tree, s->mv_band[0].width, s->mv_band[1].height);*/
3118 width= s->width= avctx->width;
3119 height= s->height= avctx->height;
3121 assert(width && height);
3123 s->avctx->get_buffer(s->avctx, &s->mconly_picture);
3129 static void calculate_vissual_weight(SnowContext *s, Plane *p){
3130 int width = p->width;
3131 int height= p->height;
3132 int i, level, orientation, x, y;
3134 for(level=0; level<s->spatial_decomposition_count; level++){
3135 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3136 SubBand *b= &p->band[level][orientation];
3137 DWTELEM *buf= b->buf;
3140 memset(s->spatial_dwt_buffer, 0, sizeof(int)*width*height);
3141 buf[b->width/2 + b->height/2*b->stride]= 256*256;
3142 spatial_idwt(s, s->spatial_dwt_buffer, width, height, width);
3143 for(y=0; y<height; y++){
3144 for(x=0; x<width; x++){
3145 int64_t d= s->spatial_dwt_buffer[x + y*width];
3150 b->qlog= (int)(log(352256.0/sqrt(error)) / log(pow(2.0, 1.0/QROOT))+0.5);
3151 av_log(NULL, AV_LOG_DEBUG, "%d %d %d\n", level, orientation, b->qlog/*, sqrt(error)*/);
3156 static int encode_init(AVCodecContext *avctx)
3158 SnowContext *s = avctx->priv_data;
3160 int level, orientation, plane_index;
3162 if(avctx->strict_std_compliance >= 0){
3163 av_log(avctx, AV_LOG_ERROR, "this codec is under development, files encoded with it wont be decodeable with future versions!!!\n"
3164 "use vstrict=-1 to use it anyway\n");
3172 s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
3173 s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
3174 s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
3175 s->mb_type = av_mallocz((s->mb_band.width+1)*s->mb_band.height*sizeof(int16_t));
3176 s->mb_mean = av_mallocz((s->mb_band.width+1)*s->mb_band.height*sizeof(int8_t ));
3177 s->dummy = av_mallocz((s->mb_band.width+1)*s->mb_band.height*sizeof(int32_t));
3178 h263_encode_init(&s->m); //mv_penalty
3180 for(plane_index=0; plane_index<3; plane_index++){
3181 calculate_vissual_weight(s, &s->plane[plane_index]);
3185 avctx->coded_frame= &s->current_picture;
3186 switch(avctx->pix_fmt){
3187 // case PIX_FMT_YUV444P:
3188 // case PIX_FMT_YUV422P:
3189 case PIX_FMT_YUV420P:
3191 // case PIX_FMT_YUV411P:
3192 // case PIX_FMT_YUV410P:
3193 s->colorspace_type= 0;
3195 /* case PIX_FMT_RGBA32:
3199 av_log(avctx, AV_LOG_ERROR, "format not supported\n");
3202 // avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_h_shift, &s->chroma_v_shift);
3203 s->chroma_h_shift= 1;
3204 s->chroma_v_shift= 1;
3208 static int frame_start(SnowContext *s){
3214 tmp= s->last_picture;
3215 s->last_picture= s->current_picture;
3216 s->current_picture= tmp;
3218 s->current_picture.reference= 1;
3219 if(s->avctx->get_buffer(s->avctx, &s->current_picture) < 0){
3220 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
3227 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
3228 SnowContext *s = avctx->priv_data;
3229 CABACContext * const c= &s->c;
3230 AVFrame *pict = data;
3231 const int width= s->avctx->width;
3232 const int height= s->avctx->height;
3234 int log2_threshold, level, orientation, plane_index, i;
3236 ff_init_cabac_encoder(c, buf, buf_size);
3237 ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
3239 s->input_picture = *pict;
3241 memset(s->header_state, 0, sizeof(s->header_state));
3243 s->keyframe=avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
3244 pict->pict_type= s->keyframe ? FF_I_TYPE : FF_P_TYPE;
3246 s->qlog= rint(QROOT*log(pict->quality / (float)FF_QP2LAMBDA)/log(2));
3250 for(i=0; i<s->mb_band.stride * s->mb_band.height; i++){
3251 s->mb_band.buf[i]= s->keyframe;
3256 if(pict->pict_type == P_TYPE){
3257 int block_width = (width +15)>>4;
3258 int block_height= (height+15)>>4;
3259 int stride= s->current_picture.linesize[0];
3260 uint8_t *src_plane= s->input_picture.data[0];
3261 int src_stride= s->input_picture.linesize[0];
3264 assert(s->current_picture.data[0]);
3265 assert(s->last_picture.data[0]);
3267 s->m.avctx= s->avctx;
3268 s->m.current_picture.data[0]= s->current_picture.data[0];
3269 s->m. last_picture.data[0]= s-> last_picture.data[0];
3270 s->m. new_picture.data[0]= s-> input_picture.data[0];
3271 s->m.current_picture_ptr= &s->m.current_picture;
3272 s->m. last_picture_ptr= &s->m. last_picture;
3274 s->m. last_picture.linesize[0]=
3275 s->m. new_picture.linesize[0]=
3276 s->m.current_picture.linesize[0]= stride;
3278 s->m.height= height;
3279 s->m.mb_width = block_width;
3280 s->m.mb_height= block_height;
3281 s->m.mb_stride= s->m.mb_width+1;
3282 s->m.b8_stride= 2*s->m.mb_width+1;
3284 s->m.pict_type= pict->pict_type;
3285 s->m.me_method= s->avctx->me_method;
3286 s->m.me.scene_change_score=0;
3287 s->m.flags= s->avctx->flags;
3288 s->m.quarter_sample= (s->avctx->flags & CODEC_FLAG_QPEL)!=0;
3289 s->m.out_format= FMT_H263;
3290 s->m.unrestricted_mv= 1;
3292 s->m.lambda= pict->quality * 3/2; //FIXME bug somewhere else
3293 s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
3294 s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
3296 if(!s->motion_val8){
3297 s->motion_val8 = av_mallocz(s->m.b8_stride*block_height*2*2*sizeof(int16_t));
3298 s->motion_val16= av_mallocz(s->m.mb_stride*block_height*2*sizeof(int16_t));
3301 s->m.mb_type= s->mb_type;
3303 //dummies, to avoid segfaults
3304 s->m.current_picture.mb_mean = s->mb_mean;
3305 s->m.current_picture.mb_var = (int16_t*)s->dummy;
3306 s->m.current_picture.mc_mb_var= (int16_t*)s->dummy;
3307 s->m.current_picture.mb_type = s->dummy;
3309 s->m.current_picture.motion_val[0]= s->motion_val8;
3310 s->m.p_mv_table= s->motion_val16;
3311 s->m.dsp= s->dsp; //move
3316 s->m.me.dia_size= s->avctx->pre_dia_size;
3317 s->m.first_slice_line=1;
3318 for(y= block_height-1; y >= 0; y--) {
3319 uint8_t src[stride*16];
3321 s->m.new_picture.data[0]= src - y*16*stride; //ugly
3323 for(i=0; i<16 && i + 16*y<height; i++){
3324 memcpy(&src[i*stride], &src_plane[(i+16*y)*src_stride], width);
3325 for(x=width; x<16*block_width; x++)
3326 src[i*stride+x]= src[i*stride+x-1];
3328 for(; i<16 && i + 16*y<16*block_height; i++)
3329 memcpy(&src[i*stride], &src[(i-1)*stride], 16*block_width);
3331 for(x=block_width-1; x >=0 ;x--) {
3333 ff_init_block_index(&s->m);
3334 ff_update_block_index(&s->m);
3335 ff_pre_estimate_p_frame_motion(&s->m, x, y);
3337 s->m.first_slice_line=0;
3342 s->m.me.dia_size= s->avctx->dia_size;
3343 s->m.first_slice_line=1;
3344 for (y = 0; y < block_height; y++) {
3345 uint8_t src[stride*16];
3347 s->m.new_picture.data[0]= src - y*16*stride; //ugly
3350 assert(width <= stride);
3351 assert(width <= 16*block_width);
3353 for(i=0; i<16 && i + 16*y<height; i++){
3354 memcpy(&src[i*stride], &src_plane[(i+16*y)*src_stride], width);
3355 for(x=width; x<16*block_width; x++)
3356 src[i*stride+x]= src[i*stride+x-1];
3358 for(; i<16 && i + 16*y<16*block_height; i++)
3359 memcpy(&src[i*stride], &src[(i-1)*stride], 16*block_width);
3361 for (x = 0; x < block_width; x++) {
3362 int mb_xy= x + y*(s->mb_band.stride);
3364 ff_init_block_index(&s->m);
3365 ff_update_block_index(&s->m);
3367 ff_estimate_p_frame_motion(&s->m, x, y);
3369 s->mb_band .buf[mb_xy]= (s->m.mb_type[x + y*s->m.mb_stride]&CANDIDATE_MB_TYPE_INTER)
3371 s->mv_band[0].buf[mb_xy]= s->motion_val16[x + y*s->m.mb_stride][0];
3372 s->mv_band[1].buf[mb_xy]= s->motion_val16[x + y*s->m.mb_stride][1];
3374 if(s->mb_band .buf[x + y*(s->mb_band.stride)]==2 && 0){
3375 int dc0=128, dc1=128, dc, dc2, dir;
3376 int offset= (s->avctx->flags & CODEC_FLAG_QPEL) ? 64 : 32;
3378 dc =s->mb_mean[x + y *s->m.mb_stride ];
3379 if(x) dc0=s->mb_mean[x + y *s->m.mb_stride - 1];
3380 if(y) dc1=s->mb_mean[x + (y-1)*s->m.mb_stride ];
3383 if (ABS(dc0 - dc) < ABS(dc1 - dc) && ABS(dc0 - dc) < ABS(dc2 - dc))
3385 else if(ABS(dc0 - dc) >=ABS(dc1 - dc) && ABS(dc1 - dc) < ABS(dc2 - dc))
3390 if(ABS(dc0 - dc) < ABS(dc1 - dc) && x){
3391 s->mv_band[0].buf[mb_xy]= s->mv_band[0].buf[x + y*(s->mb_band.stride)-1] - offset;
3392 s->mv_band[1].buf[mb_xy]= s->mv_band[1].buf[x + y*(s->mb_band.stride)-1];
3393 s->mb_mean[x + y *s->m.mb_stride ]= dc0;
3395 s->mv_band[0].buf[mb_xy]= s->mv_band[0].buf[x + (y-1)*(s->mb_band.stride)];
3396 s->mv_band[1].buf[mb_xy]= s->mv_band[1].buf[x + (y-1)*(s->mb_band.stride)] - offset;
3397 s->mb_mean[x + y *s->m.mb_stride ]= dc1;
3400 // s->mb_band .buf[x + y*(s->mb_band.stride)]=1; //FIXME intra only test
3402 s->m.first_slice_line=0;
3404 assert(s->m.pict_type == P_TYPE);
3405 if(s->m.me.scene_change_score > s->avctx->scenechange_threshold){
3407 pict->pict_type =I_TYPE;
3408 for(i=0; i<s->mb_band.stride * s->mb_band.height; i++){
3409 s->mb_band.buf[i]= 1;
3410 s->mv_band[0].buf[i]=
3411 s->mv_band[1].buf[i]= 0;
3413 //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
3417 s->m.first_slice_line=1;
3419 s->qbias= pict->pict_type == P_TYPE ? 2 : 0;
3423 decorrelate(s, &s->mb_band , s->mb_band .buf, s->mb_band .stride, 0, 1);
3424 decorrelate(s, &s->mv_band[0], s->mv_band[0].buf, s->mv_band[0].stride, 0, 1);
3425 decorrelate(s, &s->mv_band[1], s->mv_band[1].buf, s->mv_band[1].stride, 0 ,1);
3426 encode_subband(s, &s->mb_band , s->mb_band .buf, NULL, s->mb_band .stride, 0);
3427 encode_subband(s, &s->mv_band[0], s->mv_band[0].buf, NULL, s->mv_band[0].stride, 0);
3428 encode_subband(s, &s->mv_band[1], s->mv_band[1].buf, NULL, s->mv_band[1].stride, 0);
3431 correlate(s, &s->mb_band , s->mb_band .buf, s->mb_band .stride, 1, 1);
3432 correlate(s, &s->mv_band[0], s->mv_band[0].buf, s->mv_band[0].stride, 1, 1);
3433 correlate(s, &s->mv_band[1], s->mv_band[1].buf, s->mv_band[1].stride, 1, 1);
3435 for(plane_index=0; plane_index<3; plane_index++){
3436 Plane *p= &s->plane[plane_index];
3440 int bits= put_bits_count(&s->c.pb);
3444 memset(s->pred_buffer, 0, sizeof(DWTELEM)*w*h);
3445 predict_plane(s, s->pred_buffer, plane_index, 1);
3446 spatial_dwt(s, s->pred_buffer, w, h, w);
3447 for(level=0; level<s->spatial_decomposition_count; level++){
3448 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3449 SubBand *b= &p->band[level][orientation];
3450 int delta= ((int)s->pred_buffer - (int)s->spatial_dwt_buffer)/sizeof(DWTELEM);
3452 quantize (s, b, b->buf + delta, b->stride, s->qbias);
3453 dequantize(s, b, b->buf + delta, b->stride);
3458 s->spatial_dwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<8;
3461 spatial_dwt(s, s->spatial_dwt_buffer, w, h, w);
3464 s->spatial_dwt_buffer[y*w + x]-= s->pred_buffer[y*w + x];
3468 if(pict->data[plane_index]) //FIXME gray hack
3471 s->spatial_dwt_buffer[y*w + x]= pict->data[plane_index][y*pict->linesize[plane_index] + x]<<8;
3474 predict_plane(s, s->spatial_dwt_buffer, plane_index, 0);
3475 spatial_dwt(s, s->spatial_dwt_buffer, w, h, w);
3478 for(level=0; level<s->spatial_decomposition_count; level++){
3479 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3480 SubBand *b= &p->band[level][orientation];
3482 quantize(s, b, b->buf, b->stride, s->qbias);
3484 decorrelate(s, b, b->buf, b->stride, pict->pict_type == P_TYPE, 0);
3485 encode_subband(s, b, b->buf, b->parent ? b->parent->buf : NULL, b->stride, orientation);
3486 assert(b->parent==NULL || b->parent->stride == b->stride*2);
3488 correlate(s, b, b->buf, b->stride, 1, 0);
3491 // av_log(NULL, AV_LOG_DEBUG, "plane:%d bits:%d\n", plane_index, put_bits_count(&s->c.pb) - bits);
3493 for(level=0; level<s->spatial_decomposition_count; level++){
3494 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3495 SubBand *b= &p->band[level][orientation];
3497 dequantize(s, b, b->buf, b->stride);
3504 s->spatial_dwt_buffer[y*w + x]+= s->pred_buffer[y*w + x];
3507 spatial_idwt(s, s->spatial_dwt_buffer, w, h, w);
3509 spatial_idwt(s, s->spatial_dwt_buffer, w, h, w);
3510 predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
3515 int v= (s->spatial_dwt_buffer[y*w + x]+128)>>8;
3516 if(v&(~255)) v= ~(v>>31);
3517 s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x]= v;
3520 if(s->avctx->flags&CODEC_FLAG_PSNR){
3523 if(pict->data[plane_index]) //FIXME gray hack
3526 int d= s->spatial_dwt_buffer[y*w + x] - pict->data[plane_index][y*pict->linesize[plane_index] + x]*256;
3530 error= (error + 128*256)>>16;
3531 s->avctx->error[plane_index] += error;
3532 s->avctx->error[3] += error;
3536 if(s->last_picture.data[0])
3537 avctx->release_buffer(avctx, &s->last_picture);
3541 return put_cabac_terminate(c, 1);
3544 static void common_end(SnowContext *s){
3545 int plane_index, level, orientation;
3547 av_freep(&s->spatial_dwt_buffer);
3548 av_freep(&s->mb_band.buf);
3549 av_freep(&s->mv_band[0].buf);
3550 av_freep(&s->mv_band[1].buf);
3552 av_freep(&s->m.me.scratchpad);
3553 av_freep(&s->m.me.map);
3554 av_freep(&s->m.me.score_map);
3555 av_freep(&s->mb_type);
3556 av_freep(&s->mb_mean);
3557 av_freep(&s->dummy);
3558 av_freep(&s->motion_val8);
3559 av_freep(&s->motion_val16);
3561 for(plane_index=0; plane_index<3; plane_index++){
3562 for(level=s->spatial_decomposition_count-1; level>=0; level--){
3563 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3564 SubBand *b= &s->plane[plane_index].band[level][orientation];
3566 free_qtree(&b->tree);
3572 static int encode_end(AVCodecContext *avctx)
3574 SnowContext *s = avctx->priv_data;
3581 static int decode_init(AVCodecContext *avctx)
3583 // SnowContext *s = avctx->priv_data;
3590 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){
3591 SnowContext *s = avctx->priv_data;
3592 CABACContext * const c= &s->c;
3593 const int width= s->avctx->width;
3594 const int height= s->avctx->height;
3596 AVFrame *picture = data;
3597 int log2_threshold, level, orientation, plane_index;
3600 /* no supplementary picture */
3604 ff_init_cabac_decoder(c, buf, buf_size);
3605 ff_init_cabac_states(c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
3607 memset(s->header_state, 0, sizeof(s->header_state));
3609 s->current_picture.pict_type= FF_I_TYPE; //FIXME I vs. P
3613 //keyframe flag dupliaction mess FIXME
3614 if(avctx->debug&FF_DEBUG_PICT_INFO)
3615 av_log(avctx, AV_LOG_ERROR, "keyframe:%d qlog:%d\n", s->keyframe, s->qlog);
3617 decode_subband(s, &s->mb_band , s->mb_band .buf, NULL, s->mb_band .stride, 0);
3618 decode_subband(s, &s->mv_band[0], s->mv_band[0].buf, NULL, s->mv_band[0].stride, 0);
3619 decode_subband(s, &s->mv_band[1], s->mv_band[1].buf, NULL, s->mv_band[1].stride, 0);
3620 correlate(s, &s->mb_band , s->mb_band .buf, s->mb_band .stride, 1, 1);
3621 correlate(s, &s->mv_band[0], s->mv_band[0].buf, s->mv_band[0].stride, 1, 1);
3622 correlate(s, &s->mv_band[1], s->mv_band[1].buf, s->mv_band[1].stride, 1, 1);
3624 for(plane_index=0; plane_index<3; plane_index++){
3625 Plane *p= &s->plane[plane_index];
3630 if(s->avctx->debug&2048){
3631 memset(s->spatial_dwt_buffer, 0, sizeof(DWTELEM)*w*h);
3632 predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
3636 int v= (s->spatial_dwt_buffer[y*w + x]+128)>>8;
3637 if(v&(~255)) v= ~(v>>31);
3638 s->mconly_picture.data[plane_index][y*s->mconly_picture.linesize[plane_index] + x]= v;
3642 for(level=0; level<s->spatial_decomposition_count; level++){
3643 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3644 SubBand *b= &p->band[level][orientation];
3646 decode_subband(s, b, b->buf, b->parent ? b->parent->buf : NULL, b->stride, orientation);
3648 correlate(s, b, b->buf, b->stride, 1, 0);
3651 if(!(s->avctx->debug&1024))
3652 for(level=0; level<s->spatial_decomposition_count; level++){
3653 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3654 SubBand *b= &p->band[level][orientation];
3656 dequantize(s, b, b->buf, b->stride);
3661 memset(s->pred_buffer, 0, sizeof(DWTELEM)*w*h);
3662 predict_plane(s, s->pred_buffer, plane_index, 1);
3663 spatial_dwt(s, s->pred_buffer, w, h, w);
3664 for(level=0; level<s->spatial_decomposition_count; level++){
3665 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3666 SubBand *b= &p->band[level][orientation];
3667 int delta= ((int)s->pred_buffer - (int)s->spatial_dwt_buffer)/sizeof(DWTELEM);
3669 quantize (s, b, b->buf + delta, b->stride, s->qbias);
3670 dequantize(s, b, b->buf + delta, b->stride);
3675 s->spatial_dwt_buffer[y*w + x]+= s->pred_buffer[y*w + x];
3678 spatial_idwt(s, s->spatial_dwt_buffer, w, h, w);
3680 spatial_idwt(s, s->spatial_dwt_buffer, w, h, w);
3681 predict_plane(s, s->spatial_dwt_buffer, plane_index, 1);
3687 int v= (s->spatial_dwt_buffer[y*w + x]+128)>>8;
3688 if(v&(~255)) v= ~(v>>31);
3689 s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x]= v;
3696 if(s->last_picture.data[0])
3697 avctx->release_buffer(avctx, &s->last_picture);
3699 if(!(s->avctx->debug&2048))
3700 *picture= s->current_picture;
3702 *picture= s->mconly_picture;
3704 *data_size = sizeof(AVFrame);
3706 bytes_read= get_cabac_terminate(c);
3707 if(bytes_read ==0) av_log(s->avctx, AV_LOG_ERROR, "error at end of frame\n");
3712 static int decode_end(AVCodecContext *avctx)
3714 SnowContext *s = avctx->priv_data;
3721 AVCodec snow_decoder = {
3725 sizeof(SnowContext),
3730 0 /*CODEC_CAP_DR1*/ /*| CODEC_CAP_DRAW_HORIZ_BAND*/,
3734 AVCodec snow_encoder = {
3738 sizeof(SnowContext),
3753 int buffer[2][width*height];
3756 s.spatial_decomposition_count=6;
3757 s.spatial_decomposition_type=1;
3759 printf("testing 5/3 DWT\n");
3760 for(i=0; i<width*height; i++)
3761 buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
3763 spatial_dwt(&s, buffer[0], width, height, width);
3764 spatial_idwt(&s, buffer[0], width, height, width);
3766 for(i=0; i<width*height; i++)
3767 if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
3769 printf("testing 9/7 DWT\n");
3770 s.spatial_decomposition_type=0;
3771 for(i=0; i<width*height; i++)
3772 buffer[0][i]= buffer[1][i]= random()%54321 - 12345;
3774 spatial_dwt(&s, buffer[0], width, height, width);
3775 spatial_idwt(&s, buffer[0], width, height, width);
3777 for(i=0; i<width*height; i++)
3778 if(buffer[0][i]!= buffer[1][i]) printf("fsck: %d %d %d\n",i, buffer[0][i], buffer[1][i]);
3780 printf("testing AC coder\n");
3781 memset(s.header_state, 0, sizeof(s.header_state));
3782 ff_init_cabac_encoder(&s.c, buffer[0], 256*256);
3783 ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
3785 for(i=-256; i<256; i++){
3787 put_symbol(&s.c, s.header_state, i*i*i/3*ABS(i), 1);
3788 STOP_TIMER("put_symbol")
3790 put_cabac_terminate(&s.c, 1);
3792 memset(s.header_state, 0, sizeof(s.header_state));
3793 ff_init_cabac_decoder(&s.c, buffer[0], 256*256);
3794 ff_init_cabac_states(&s.c, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64);
3796 for(i=-256; i<256; i++){
3799 j= get_symbol(&s.c, s.header_state, 1);
3800 STOP_TIMER("get_symbol")
3801 if(j!=i*i*i/3*ABS(i)) printf("fsck: %d != %d\n", i, j);
3804 int level, orientation, x, y;
3805 int64_t errors[8][4];
3808 memset(errors, 0, sizeof(errors));
3809 s.spatial_decomposition_count=3;
3810 s.spatial_decomposition_type=0;
3811 for(level=0; level<s.spatial_decomposition_count; level++){
3812 for(orientation=level ? 1 : 0; orientation<4; orientation++){
3813 int w= width >> (s.spatial_decomposition_count-level);
3814 int h= height >> (s.spatial_decomposition_count-level);
3815 int stride= width << (s.spatial_decomposition_count-level);
3816 DWTELEM *buf= buffer[0];
3819 if(orientation&1) buf+=w;
3820 if(orientation>1) buf+=stride>>1;
3822 memset(buffer[0], 0, sizeof(int)*width*height);
3823 buf[w/2 + h/2*stride]= 256*256;
3824 spatial_idwt(&s, buffer[0], width, height, width);
3825 for(y=0; y<height; y++){
3826 for(x=0; x<width; x++){
3827 int64_t d= buffer[0][x + y*width];
3829 if(ABS(width/2-x)<9 && ABS(height/2-y)<9 && level==2) printf("%8lld ", d);
3831 if(ABS(height/2-y)<9 && level==2) printf("\n");
3833 error= (int)(sqrt(error)+0.5);
3834 errors[level][orientation]= error;
3835 if(g) g=ff_gcd(g, error);
3839 printf("static int const visual_weight[][4]={\n");
3840 for(level=0; level<s.spatial_decomposition_count; level++){
3842 for(orientation=0; orientation<4; orientation++){
3843 printf("%8lld,", errors[level][orientation]/g);
3851 int w= width >> (s.spatial_decomposition_count-level);
3852 int h= height >> (s.spatial_decomposition_count-level);
3853 int stride= width << (s.spatial_decomposition_count-level);
3854 DWTELEM *buf= buffer[0];
3860 memset(buffer[0], 0, sizeof(int)*width*height);
3862 for(y=0; y<height; y++){
3863 for(x=0; x<width; x++){
3864 int tab[4]={0,2,3,1};
3865 buffer[0][x+width*y]= 256*256*tab[(x&1) + 2*(y&1)];
3868 spatial_dwt(&s, buffer[0], width, height, width);
3872 buf[x + y*stride ]=169;
3873 buf[x + y*stride-w]=64;
3876 spatial_idwt(&s, buffer[0], width, height, width);
3878 for(y=0; y<height; y++){
3879 for(x=0; x<width; x++){
3880 int64_t d= buffer[0][x + y*width];
3882 if(ABS(width/2-x)<9 && ABS(height/2-y)<9) printf("%8lld ", d);
3884 if(ABS(height/2-y)<9) printf("\n");