Statistics
| Branch: | Revision:

ffmpeg / libavcodec / snow.c @ fe5c7e58

History | View | Annotate | Download (180 KB)

1 791e7b83 Michael Niedermayer
/*
2
 * Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at>
3
 *
4 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7 791e7b83 Michael Niedermayer
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
10 791e7b83 Michael Niedermayer
 *
11 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
12 791e7b83 Michael Niedermayer
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
18 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 791e7b83 Michael Niedermayer
 */
20
21
#include "avcodec.h"
22
#include "dsputil.h"
23 059715a4 Robert Edele
#include "snow.h"
24 28869757 Michael Niedermayer
25
#include "rangecoder.h"
26 791e7b83 Michael Niedermayer
27
#include "mpegvideo.h"
28
29
#undef NDEBUG
30
#include <assert.h>
31
32
static const int8_t quant3[256]={
33
 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
34
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
35
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
36
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
37
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
38
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
39
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
40
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
41
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
42
-1,-1,-1,-1,-1,-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, 0,
49
};
50
static const int8_t quant3b[256]={
51
 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
52
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
53
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
54
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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
};
68 538a3841 Michael Niedermayer
static const int8_t quant3bA[256]={
69
 0, 0, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
70
 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
71
 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
72
 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
73
 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
74
 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
75
 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
76
 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
77
 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
78
 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
79
 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
80
 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
81
 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
82
 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
83
 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
84
 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1,
85
};
86 791e7b83 Michael Niedermayer
static const int8_t quant5[256]={
87
 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
88
 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
89
 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
90
 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
91
 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
92
 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
93
 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
94
 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
95
-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
96
-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
97
-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
98
-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
99
-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
100
-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
101
-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
102
-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,-1,
103
};
104
static const int8_t quant7[256]={
105
 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
106
 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
107
 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
108
 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
109
 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
110
 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
111
 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
112
 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
113
-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
114
-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
115
-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
116
-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
117
-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,
118
-3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-2,-2,-2,
119
-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,
120
-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-1,-1,
121
};
122
static const int8_t quant9[256]={
123
 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
124
 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
125
 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 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, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
128
 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
129
 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
130
 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
131
-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
132
-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
133
-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
134
-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
135
-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
136
-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
137
-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,
138
-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-1,-1,
139
};
140
static const int8_t quant11[256]={
141
 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
142
 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
143
 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
144
 5, 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, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
147
 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
148
 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
149
-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
150
-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
151
-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
152
-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
153
-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
154
-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-4,-4,
155
-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,
156
-4,-4,-4,-4,-4,-3,-3,-3,-3,-3,-3,-3,-2,-2,-2,-1,
157
};
158
static const int8_t quant13[256]={
159
 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
160
 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
161
 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
162
 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
163
 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
164
 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
165
 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
166
 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
167
-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
168
-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
169
-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
170
-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,
171
-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-6,-5,
172
-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
173
-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,
174
-4,-4,-4,-4,-4,-4,-4,-4,-4,-3,-3,-3,-3,-2,-2,-1,
175
};
176
177
#if 0 //64*cubic
178
static const uint8_t obmc32[1024]={
179 fa731ccd Michael Niedermayer
  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,
180
  0,  0,  0,  0,  0,  4,  4,  4,  4,  4,  4,  4,  4,  8,  8,  8,  8,  8,  8,  4,  4,  4,  4,  4,  4,  4,  4,  0,  0,  0,  0,  0,
181
  0,  0,  0,  4,  4,  4,  4,  8,  8, 12, 12, 12, 16, 16, 16, 16, 16, 16, 16, 16, 12, 12, 12,  8,  8,  4,  4,  4,  4,  0,  0,  0,
182
  0,  0,  4,  4,  8,  8, 12, 16, 16, 20, 24, 24, 28, 28, 32, 32, 32, 32, 28, 28, 24, 24, 20, 16, 16, 12,  8,  8,  4,  4,  0,  0,
183
  0,  0,  4,  8,  8, 12, 16, 24, 28, 32, 36, 40, 44, 48, 48, 48, 48, 48, 48, 44, 40, 36, 32, 28, 24, 16, 12,  8,  8,  4,  0,  0,
184
  0,  4,  4,  8, 12, 20, 24, 32, 40, 44, 52, 56, 60, 64, 68, 72, 72, 68, 64, 60, 56, 52, 44, 40, 32, 24, 20, 12,  8,  4,  4,  0,
185
  0,  4,  4, 12, 16, 24, 32, 40, 52, 60, 68, 76, 80, 88, 88, 92, 92, 88, 88, 80, 76, 68, 60, 52, 40, 32, 24, 16, 12,  4,  4,  0,
186
  0,  4,  8, 16, 24, 32, 40, 52, 64, 76, 84, 92,100,108,112,116,116,112,108,100, 92, 84, 76, 64, 52, 40, 32, 24, 16,  8,  4,  0,
187
  0,  4,  8, 16, 28, 40, 52, 64, 76, 88,100,112,124,132,136,140,140,136,132,124,112,100, 88, 76, 64, 52, 40, 28, 16,  8,  4,  0,
188
  0,  4, 12, 20, 32, 44, 60, 76, 88,104,120,132,144,152,160,164,164,160,152,144,132,120,104, 88, 76, 60, 44, 32, 20, 12,  4,  0,
189
  0,  4, 12, 24, 36, 48, 68, 84,100,120,136,152,164,176,180,184,184,180,176,164,152,136,120,100, 84, 68, 48, 36, 24, 12,  4,  0,
190
  0,  4, 12, 24, 40, 56, 76, 92,112,132,152,168,180,192,204,208,208,204,192,180,168,152,132,112, 92, 76, 56, 40, 24, 12,  4,  0,
191
  0,  4, 16, 28, 44, 60, 80,100,124,144,164,180,196,208,220,224,224,220,208,196,180,164,144,124,100, 80, 60, 44, 28, 16,  4,  0,
192
  0,  8, 16, 28, 48, 64, 88,108,132,152,176,192,208,224,232,240,240,232,224,208,192,176,152,132,108, 88, 64, 48, 28, 16,  8,  0,
193
  0,  4, 16, 32, 48, 68, 88,112,136,160,180,204,220,232,244,248,248,244,232,220,204,180,160,136,112, 88, 68, 48, 32, 16,  4,  0,
194
  1,  8, 16, 32, 48, 72, 92,116,140,164,184,208,224,240,248,255,255,248,240,224,208,184,164,140,116, 92, 72, 48, 32, 16,  8,  1,
195
  1,  8, 16, 32, 48, 72, 92,116,140,164,184,208,224,240,248,255,255,248,240,224,208,184,164,140,116, 92, 72, 48, 32, 16,  8,  1,
196
  0,  4, 16, 32, 48, 68, 88,112,136,160,180,204,220,232,244,248,248,244,232,220,204,180,160,136,112, 88, 68, 48, 32, 16,  4,  0,
197
  0,  8, 16, 28, 48, 64, 88,108,132,152,176,192,208,224,232,240,240,232,224,208,192,176,152,132,108, 88, 64, 48, 28, 16,  8,  0,
198
  0,  4, 16, 28, 44, 60, 80,100,124,144,164,180,196,208,220,224,224,220,208,196,180,164,144,124,100, 80, 60, 44, 28, 16,  4,  0,
199
  0,  4, 12, 24, 40, 56, 76, 92,112,132,152,168,180,192,204,208,208,204,192,180,168,152,132,112, 92, 76, 56, 40, 24, 12,  4,  0,
200
  0,  4, 12, 24, 36, 48, 68, 84,100,120,136,152,164,176,180,184,184,180,176,164,152,136,120,100, 84, 68, 48, 36, 24, 12,  4,  0,
201
  0,  4, 12, 20, 32, 44, 60, 76, 88,104,120,132,144,152,160,164,164,160,152,144,132,120,104, 88, 76, 60, 44, 32, 20, 12,  4,  0,
202
  0,  4,  8, 16, 28, 40, 52, 64, 76, 88,100,112,124,132,136,140,140,136,132,124,112,100, 88, 76, 64, 52, 40, 28, 16,  8,  4,  0,
203
  0,  4,  8, 16, 24, 32, 40, 52, 64, 76, 84, 92,100,108,112,116,116,112,108,100, 92, 84, 76, 64, 52, 40, 32, 24, 16,  8,  4,  0,
204
  0,  4,  4, 12, 16, 24, 32, 40, 52, 60, 68, 76, 80, 88, 88, 92, 92, 88, 88, 80, 76, 68, 60, 52, 40, 32, 24, 16, 12,  4,  4,  0,
205
  0,  4,  4,  8, 12, 20, 24, 32, 40, 44, 52, 56, 60, 64, 68, 72, 72, 68, 64, 60, 56, 52, 44, 40, 32, 24, 20, 12,  8,  4,  4,  0,
206
  0,  0,  4,  8,  8, 12, 16, 24, 28, 32, 36, 40, 44, 48, 48, 48, 48, 48, 48, 44, 40, 36, 32, 28, 24, 16, 12,  8,  8,  4,  0,  0,
207
  0,  0,  4,  4,  8,  8, 12, 16, 16, 20, 24, 24, 28, 28, 32, 32, 32, 32, 28, 28, 24, 24, 20, 16, 16, 12,  8,  8,  4,  4,  0,  0,
208
  0,  0,  0,  4,  4,  4,  4,  8,  8, 12, 12, 12, 16, 16, 16, 16, 16, 16, 16, 16, 12, 12, 12,  8,  8,  4,  4,  4,  4,  0,  0,  0,
209
  0,  0,  0,  0,  0,  4,  4,  4,  4,  4,  4,  4,  4,  8,  8,  8,  8,  8,  8,  4,  4,  4,  4,  4,  4,  4,  4,  0,  0,  0,  0,  0,
210
  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,
211 791e7b83 Michael Niedermayer
//error:0.000022
212
};
213
static const uint8_t obmc16[256]={
214 fa731ccd Michael Niedermayer
  0,  0,  0,  0,  0,  0,  4,  4,  4,  4,  0,  0,  0,  0,  0,  0,
215
  0,  4,  4,  8, 16, 20, 20, 24, 24, 20, 20, 16,  8,  4,  4,  0,
216
  0,  4, 16, 24, 36, 44, 52, 60, 60, 52, 44, 36, 24, 16,  4,  0,
217
  0,  8, 24, 44, 60, 80, 96,104,104, 96, 80, 60, 44, 24,  8,  0,
218
  0, 16, 36, 60, 92,116,136,152,152,136,116, 92, 60, 36, 16,  0,
219
  0, 20, 44, 80,116,152,180,196,196,180,152,116, 80, 44, 20,  0,
220
  4, 20, 52, 96,136,180,212,228,228,212,180,136, 96, 52, 20,  4,
221
  4, 24, 60,104,152,196,228,248,248,228,196,152,104, 60, 24,  4,
222
  4, 24, 60,104,152,196,228,248,248,228,196,152,104, 60, 24,  4,
223
  4, 20, 52, 96,136,180,212,228,228,212,180,136, 96, 52, 20,  4,
224
  0, 20, 44, 80,116,152,180,196,196,180,152,116, 80, 44, 20,  0,
225
  0, 16, 36, 60, 92,116,136,152,152,136,116, 92, 60, 36, 16,  0,
226
  0,  8, 24, 44, 60, 80, 96,104,104, 96, 80, 60, 44, 24,  8,  0,
227
  0,  4, 16, 24, 36, 44, 52, 60, 60, 52, 44, 36, 24, 16,  4,  0,
228
  0,  4,  4,  8, 16, 20, 20, 24, 24, 20, 20, 16,  8,  4,  4,  0,
229
  0,  0,  0,  0,  0,  0,  4,  4,  4,  4,  0,  0,  0,  0,  0,  0,
230 791e7b83 Michael Niedermayer
//error:0.000033
231
};
232
#elif 1 // 64*linear
233
static const uint8_t obmc32[1024]={
234 561a18d3 Robert Edele
  0,  0,  0,  0,  4,  4,  4,  4,  4,  4,  4,  4,  8,  8,  8,  8,  8,  8,  8,  8,  4,  4,  4,  4,  4,  4,  4,  4,  0,  0,  0,  0,
235
  0,  4,  4,  4,  8,  8,  8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12,  8,  8,  8,  4,  4,  4,  0,
236
  0,  4,  8,  8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12,  8,  8,  4,  0,
237
  0,  4,  8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12,  8,  4,  0,
238
  4,  8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12,  8,  4,
239
  4,  8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12,  8,  4,
240
  4,  8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16,  8,  4,
241
  4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12,  4,
242
  4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12,  4,
243
  4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16,  4,
244
  4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16,  4,
245
  4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16,  4,
246
  8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20,  8,
247
  8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20,  8,
248
  8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20,  8,
249
  8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24,  8,
250
  8, 24, 40, 56, 68, 84,100,116,132,148,164,180,192,208,224,240,240,224,208,192,180,164,148,132,116,100, 84, 68, 56, 40, 24,  8,
251
  8, 20, 36, 52, 64, 80, 96,108,124,136,152,168,180,196,212,224,224,212,196,180,168,152,136,124,108, 96, 80, 64, 52, 36, 20,  8,
252
  8, 20, 32, 48, 60, 76, 88,100,116,128,140,156,168,184,196,208,208,196,184,168,156,140,128,116,100, 88, 76, 60, 48, 32, 20,  8,
253
  8, 20, 32, 44, 56, 68, 80, 92,108,120,132,144,156,168,180,192,192,180,168,156,144,132,120,108, 92, 80, 68, 56, 44, 32, 20,  8,
254
  4, 16, 28, 40, 52, 64, 76, 88, 96,108,120,132,144,156,168,180,180,168,156,144,132,120,108, 96, 88, 76, 64, 52, 40, 28, 16,  4,
255
  4, 16, 28, 36, 48, 56, 68, 80, 88,100,112,120,132,140,152,164,164,152,140,132,120,112,100, 88, 80, 68, 56, 48, 36, 28, 16,  4,
256
  4, 16, 24, 32, 44, 52, 60, 72, 80, 92,100,108,120,128,136,148,148,136,128,120,108,100, 92, 80, 72, 60, 52, 44, 32, 24, 16,  4,
257
  4, 12, 20, 28, 40, 48, 56, 64, 72, 80, 88, 96,108,116,124,132,132,124,116,108, 96, 88, 80, 72, 64, 56, 48, 40, 28, 20, 12,  4,
258
  4, 12, 20, 28, 32, 40, 48, 56, 64, 72, 80, 88, 92,100,108,116,116,108,100, 92, 88, 80, 72, 64, 56, 48, 40, 32, 28, 20, 12,  4,
259
  4,  8, 16, 24, 28, 36, 44, 48, 56, 60, 68, 76, 80, 88, 96,100,100, 96, 88, 80, 76, 68, 60, 56, 48, 44, 36, 28, 24, 16,  8,  4,
260
  4,  8, 12, 20, 24, 32, 36, 40, 48, 52, 56, 64, 68, 76, 80, 84, 84, 80, 76, 68, 64, 56, 52, 48, 40, 36, 32, 24, 20, 12,  8,  4,
261
  4,  8, 12, 16, 20, 24, 28, 32, 40, 44, 48, 52, 56, 60, 64, 68, 68, 64, 60, 56, 52, 48, 44, 40, 32, 28, 24, 20, 16, 12,  8,  4,
262
  0,  4,  8, 12, 16, 20, 24, 28, 28, 32, 36, 40, 44, 48, 52, 56, 56, 52, 48, 44, 40, 36, 32, 28, 28, 24, 20, 16, 12,  8,  4,  0,
263
  0,  4,  8,  8, 12, 12, 16, 20, 20, 24, 28, 28, 32, 32, 36, 40, 40, 36, 32, 32, 28, 28, 24, 20, 20, 16, 12, 12,  8,  8,  4,  0,
264
  0,  4,  4,  4,  8,  8,  8, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 20, 20, 20, 16, 16, 16, 12, 12,  8,  8,  8,  4,  4,  4,  0,
265
  0,  0,  0,  0,  4,  4,  4,  4,  4,  4,  4,  4,  8,  8,  8,  8,  8,  8,  8,  8,  4,  4,  4,  4,  4,  4,  4,  4,  0,  0,  0,  0,
266 791e7b83 Michael Niedermayer
 //error:0.000020
267
};
268
static const uint8_t obmc16[256]={
269 561a18d3 Robert Edele
  0,  4,  4,  8,  8, 12, 12, 16, 16, 12, 12,  8,  8,  4,  4,  0,
270
  4,  8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16,  8,  4,
271
  4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16,  4,
272
  8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20,  8,
273
  8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28,  8,
274
 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
275
 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
276
 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
277
 16, 44, 76,104,136,164,196,224,224,196,164,136,104, 76, 44, 16,
278
 12, 40, 64, 92,116,144,168,196,196,168,144,116, 92, 64, 40, 12,
279
 12, 32, 56, 76,100,120,144,164,164,144,120,100, 76, 56, 32, 12,
280
  8, 28, 44, 64, 80,100,116,136,136,116,100, 80, 64, 44, 28,  8,
281
  8, 20, 36, 48, 64, 76, 92,104,104, 92, 76, 64, 48, 36, 20,  8,
282
  4, 16, 24, 36, 44, 56, 64, 76, 76, 64, 56, 44, 36, 24, 16,  4,
283
  4,  8, 16, 20, 28, 32, 40, 44, 44, 40, 32, 28, 20, 16,  8,  4,
284
  0,  4,  4,  8,  8, 12, 12, 16, 16, 12, 12,  8,  8,  4,  4,  0,
285 791e7b83 Michael Niedermayer
//error:0.000015
286
};
287
#else //64*cos
288
static const uint8_t obmc32[1024]={
289 fa731ccd Michael Niedermayer
  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,
290
  0,  0,  0,  0,  0,  0,  4,  4,  4,  4,  4,  4,  4,  4,  8,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  4,  0,  0,  0,  0,  0,  0,
291
  0,  0,  0,  4,  4,  4,  4,  8,  8, 12, 12, 12, 12, 16, 16, 16, 16, 16, 16, 12, 12, 12, 12,  8,  8,  4,  4,  4,  4,  0,  0,  0,
292
  0,  0,  4,  4,  4,  8,  8, 12, 16, 20, 20, 24, 28, 28, 28, 28, 28, 28, 28, 28, 24, 20, 20, 16, 12,  8,  8,  4,  4,  4,  0,  0,
293
  0,  0,  4,  4,  8, 12, 16, 20, 24, 28, 36, 40, 44, 44, 48, 48, 48, 48, 44, 44, 40, 36, 28, 24, 20, 16, 12,  8,  4,  4,  0,  0,
294
  0,  0,  4,  8, 12, 20, 24, 32, 36, 44, 48, 56, 60, 64, 68, 68, 68, 68, 64, 60, 56, 48, 44, 36, 32, 24, 20, 12,  8,  4,  0,  0,
295
  0,  4,  4,  8, 16, 24, 32, 40, 48, 60, 68, 76, 80, 84, 88, 92, 92, 88, 84, 80, 76, 68, 60, 48, 40, 32, 24, 16,  8,  4,  4,  0,
296
  0,  4,  8, 12, 20, 32, 40, 52, 64, 76, 84, 96,104,108,112,116,116,112,108,104, 96, 84, 76, 64, 52, 40, 32, 20, 12,  8,  4,  0,
297
  0,  4,  8, 16, 24, 36, 48, 64, 76, 92,104,116,124,132,136,140,140,136,132,124,116,104, 92, 76, 64, 48, 36, 24, 16,  8,  4,  0,
298
  0,  4, 12, 20, 28, 44, 60, 76, 92,104,120,136,148,156,160,164,164,160,156,148,136,120,104, 92, 76, 60, 44, 28, 20, 12,  4,  0,
299
  0,  4, 12, 20, 36, 48, 68, 84,104,120,140,152,168,176,184,188,188,184,176,168,152,140,120,104, 84, 68, 48, 36, 20, 12,  4,  0,
300
  0,  4, 12, 24, 36, 56, 76, 96,116,136,152,172,184,196,204,208,208,204,196,184,172,152,136,116, 96, 76, 56, 36, 24, 12,  4,  0,
301
  0,  4, 12, 24, 44, 60, 80,104,124,148,168,184,200,212,224,228,228,224,212,200,184,168,148,124,104, 80, 60, 44, 24, 12,  4,  0,
302
  0,  4, 12, 28, 44, 64, 84,108,132,156,176,196,212,228,236,240,240,236,228,212,196,176,156,132,108, 84, 64, 44, 28, 12,  4,  0,
303
  0,  4, 16, 28, 48, 68, 88,112,136,160,184,204,224,236,244,252,252,244,236,224,204,184,160,136,112, 88, 68, 48, 28, 16,  4,  0,
304
  1,  4, 16, 28, 48, 68, 92,116,140,164,188,208,228,240,252,255,255,252,240,228,208,188,164,140,116, 92, 68, 48, 28, 16,  4,  1,
305
  1,  4, 16, 28, 48, 68, 92,116,140,164,188,208,228,240,252,255,255,252,240,228,208,188,164,140,116, 92, 68, 48, 28, 16,  4,  1,
306
  0,  4, 16, 28, 48, 68, 88,112,136,160,184,204,224,236,244,252,252,244,236,224,204,184,160,136,112, 88, 68, 48, 28, 16,  4,  0,
307
  0,  4, 12, 28, 44, 64, 84,108,132,156,176,196,212,228,236,240,240,236,228,212,196,176,156,132,108, 84, 64, 44, 28, 12,  4,  0,
308
  0,  4, 12, 24, 44, 60, 80,104,124,148,168,184,200,212,224,228,228,224,212,200,184,168,148,124,104, 80, 60, 44, 24, 12,  4,  0,
309
  0,  4, 12, 24, 36, 56, 76, 96,116,136,152,172,184,196,204,208,208,204,196,184,172,152,136,116, 96, 76, 56, 36, 24, 12,  4,  0,
310
  0,  4, 12, 20, 36, 48, 68, 84,104,120,140,152,168,176,184,188,188,184,176,168,152,140,120,104, 84, 68, 48, 36, 20, 12,  4,  0,
311
  0,  4, 12, 20, 28, 44, 60, 76, 92,104,120,136,148,156,160,164,164,160,156,148,136,120,104, 92, 76, 60, 44, 28, 20, 12,  4,  0,
312
  0,  4,  8, 16, 24, 36, 48, 64, 76, 92,104,116,124,132,136,140,140,136,132,124,116,104, 92, 76, 64, 48, 36, 24, 16,  8,  4,  0,
313
  0,  4,  8, 12, 20, 32, 40, 52, 64, 76, 84, 96,104,108,112,116,116,112,108,104, 96, 84, 76, 64, 52, 40, 32, 20, 12,  8,  4,  0,
314
  0,  4,  4,  8, 16, 24, 32, 40, 48, 60, 68, 76, 80, 84, 88, 92, 92, 88, 84, 80, 76, 68, 60, 48, 40, 32, 24, 16,  8,  4,  4,  0,
315
  0,  0,  4,  8, 12, 20, 24, 32, 36, 44, 48, 56, 60, 64, 68, 68, 68, 68, 64, 60, 56, 48, 44, 36, 32, 24, 20, 12,  8,  4,  0,  0,
316
  0,  0,  4,  4,  8, 12, 16, 20, 24, 28, 36, 40, 44, 44, 48, 48, 48, 48, 44, 44, 40, 36, 28, 24, 20, 16, 12,  8,  4,  4,  0,  0,
317
  0,  0,  4,  4,  4,  8,  8, 12, 16, 20, 20, 24, 28, 28, 28, 28, 28, 28, 28, 28, 24, 20, 20, 16, 12,  8,  8,  4,  4,  4,  0,  0,
318
  0,  0,  0,  4,  4,  4,  4,  8,  8, 12, 12, 12, 12, 16, 16, 16, 16, 16, 16, 12, 12, 12, 12,  8,  8,  4,  4,  4,  4,  0,  0,  0,
319
  0,  0,  0,  0,  0,  0,  4,  4,  4,  4,  4,  4,  4,  4,  8,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  4,  0,  0,  0,  0,  0,  0,
320
  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,
321 791e7b83 Michael Niedermayer
//error:0.000022
322
};
323
static const uint8_t obmc16[256]={
324 fa731ccd Michael Niedermayer
  0,  0,  0,  0,  0,  4,  4,  4,  4,  4,  4,  0,  0,  0,  0,  0,
325
  0,  0,  4,  8, 12, 16, 20, 20, 20, 20, 16, 12,  8,  4,  0,  0,
326
  0,  4, 12, 24, 32, 44, 52, 56, 56, 52, 44, 32, 24, 12,  4,  0,
327
  0,  8, 24, 40, 60, 80, 96,104,104, 96, 80, 60, 40, 24,  8,  0,
328
  0, 12, 32, 64, 92,120,140,152,152,140,120, 92, 64, 32, 12,  0,
329
  4, 16, 44, 80,120,156,184,196,196,184,156,120, 80, 44, 16,  4,
330
  4, 20, 52, 96,140,184,216,232,232,216,184,140, 96, 52, 20,  4,
331
  0, 20, 56,104,152,196,232,252,252,232,196,152,104, 56, 20,  0,
332
  0, 20, 56,104,152,196,232,252,252,232,196,152,104, 56, 20,  0,
333
  4, 20, 52, 96,140,184,216,232,232,216,184,140, 96, 52, 20,  4,
334
  4, 16, 44, 80,120,156,184,196,196,184,156,120, 80, 44, 16,  4,
335
  0, 12, 32, 64, 92,120,140,152,152,140,120, 92, 64, 32, 12,  0,
336
  0,  8, 24, 40, 60, 80, 96,104,104, 96, 80, 60, 40, 24,  8,  0,
337
  0,  4, 12, 24, 32, 44, 52, 56, 56, 52, 44, 32, 24, 12,  4,  0,
338
  0,  0,  4,  8, 12, 16, 20, 20, 20, 20, 16, 12,  8,  4,  0,  0,
339
  0,  0,  0,  0,  0,  4,  4,  4,  4,  4,  4,  0,  0,  0,  0,  0,
340 791e7b83 Michael Niedermayer
//error:0.000022
341
};
342 96e2fbf2 Diego Biurrun
#endif /* 0 */
343 791e7b83 Michael Niedermayer
344 155ec6ed Michael Niedermayer
//linear *64
345
static const uint8_t obmc8[64]={
346 561a18d3 Robert Edele
  4, 12, 20, 28, 28, 20, 12,  4,
347
 12, 36, 60, 84, 84, 60, 36, 12,
348
 20, 60,100,140,140,100, 60, 20,
349
 28, 84,140,196,196,140, 84, 28,
350
 28, 84,140,196,196,140, 84, 28,
351
 20, 60,100,140,140,100, 60, 20,
352
 12, 36, 60, 84, 84, 60, 36, 12,
353
  4, 12, 20, 28, 28, 20, 12,  4,
354 155ec6ed Michael Niedermayer
//error:0.000000
355
};
356
357
//linear *64
358
static const uint8_t obmc4[16]={
359 561a18d3 Robert Edele
 16, 48, 48, 16,
360
 48,144,144, 48,
361
 48,144,144, 48,
362
 16, 48, 48, 16,
363 155ec6ed Michael Niedermayer
//error:0.000000
364
};
365
366 cf2baeb3 Stefan Gehrer
static const uint8_t * const obmc_tab[4]={
367 155ec6ed Michael Niedermayer
    obmc32, obmc16, obmc8, obmc4
368
};
369
370 85fc0e75 Loren Merritt
static int scale_mv_ref[MAX_REF_FRAMES][MAX_REF_FRAMES];
371
372 155ec6ed Michael Niedermayer
typedef struct BlockNode{
373
    int16_t mx;
374
    int16_t my;
375 8c36eaaa Loren Merritt
    uint8_t ref;
376 155ec6ed Michael Niedermayer
    uint8_t color[3];
377
    uint8_t type;
378
//#define TYPE_SPLIT    1
379
#define BLOCK_INTRA   1
380 51d6a3cf Michael Niedermayer
#define BLOCK_OPT     2
381 155ec6ed Michael Niedermayer
//#define TYPE_NOCOLOR  4
382
    uint8_t level; //FIXME merge into type?
383
}BlockNode;
384
385 51d6a3cf Michael Niedermayer
static const BlockNode null_block= { //FIXME add border maybe
386
    .color= {128,128,128},
387
    .mx= 0,
388
    .my= 0,
389 8c36eaaa Loren Merritt
    .ref= 0,
390 51d6a3cf Michael Niedermayer
    .type= 0,
391
    .level= 0,
392
};
393
394 155ec6ed Michael Niedermayer
#define LOG2_MB_SIZE 4
395
#define MB_SIZE (1<<LOG2_MB_SIZE)
396 b538791b Michael Niedermayer
#define ENCODER_EXTRA_BITS 4
397 61d6e445 Michael Niedermayer
#define HTAPS_MAX 8
398 155ec6ed Michael Niedermayer
399 a0d1931c Yartrebo
typedef struct x_and_coeff{
400
    int16_t x;
401 538a3841 Michael Niedermayer
    uint16_t coeff;
402 a0d1931c Yartrebo
} x_and_coeff;
403
404 791e7b83 Michael Niedermayer
typedef struct SubBand{
405
    int level;
406
    int stride;
407
    int width;
408
    int height;
409 e6464f8b Diego Biurrun
    int qlog;        ///< log(qscale)/log[2^(1/6)]
410 791e7b83 Michael Niedermayer
    DWTELEM *buf;
411 d593e329 Michael Niedermayer
    IDWTELEM *ibuf;
412 a0d1931c Yartrebo
    int buf_x_offset;
413
    int buf_y_offset;
414
    int stride_line; ///< Stride measured in lines, not pixels.
415
    x_and_coeff * x_coeff;
416 791e7b83 Michael Niedermayer
    struct SubBand *parent;
417
    uint8_t state[/*7*2*/ 7 + 512][32];
418
}SubBand;
419
420
typedef struct Plane{
421
    int width;
422
    int height;
423
    SubBand band[MAX_DECOMPOSITIONS][4];
424 7d7f57d9 Michael Niedermayer
425
    int htaps;
426 61d6e445 Michael Niedermayer
    int8_t hcoeff[HTAPS_MAX/2];
427 7d7f57d9 Michael Niedermayer
    int diag_mc;
428
    int fast_mc;
429
430
    int last_htaps;
431 61d6e445 Michael Niedermayer
    int8_t last_hcoeff[HTAPS_MAX/2];
432 7d7f57d9 Michael Niedermayer
    int last_diag_mc;
433 791e7b83 Michael Niedermayer
}Plane;
434
435
typedef struct SnowContext{
436 e6464f8b Diego Biurrun
//    MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to eventually make the motion estimation independent of MpegEncContext, so this will be removed then (FIXME/XXX)
437 791e7b83 Michael Niedermayer
438
    AVCodecContext *avctx;
439 28869757 Michael Niedermayer
    RangeCoder c;
440 791e7b83 Michael Niedermayer
    DSPContext dsp;
441 51d6a3cf Michael Niedermayer
    AVFrame new_picture;
442
    AVFrame input_picture;              ///< new_picture with the internal linesizes
443 791e7b83 Michael Niedermayer
    AVFrame current_picture;
444 8c36eaaa Loren Merritt
    AVFrame last_picture[MAX_REF_FRAMES];
445 5be3a818 Michael Niedermayer
    uint8_t *halfpel_plane[MAX_REF_FRAMES][4][4];
446 791e7b83 Michael Niedermayer
    AVFrame mconly_picture;
447
//     uint8_t q_context[16];
448
    uint8_t header_state[32];
449 155ec6ed Michael Niedermayer
    uint8_t block_state[128 + 32*128];
450 791e7b83 Michael Niedermayer
    int keyframe;
451 19aa028d Michael Niedermayer
    int always_reset;
452 791e7b83 Michael Niedermayer
    int version;
453
    int spatial_decomposition_type;
454 396a5e68 Michael Niedermayer
    int last_spatial_decomposition_type;
455 791e7b83 Michael Niedermayer
    int temporal_decomposition_type;
456
    int spatial_decomposition_count;
457 8db13728 Michael Niedermayer
    int last_spatial_decomposition_count;
458 791e7b83 Michael Niedermayer
    int temporal_decomposition_count;
459 8c36eaaa Loren Merritt
    int max_ref_frames;
460
    int ref_frames;
461
    int16_t (*ref_mvs[MAX_REF_FRAMES])[2];
462
    uint32_t *ref_scores[MAX_REF_FRAMES];
463 791e7b83 Michael Niedermayer
    DWTELEM *spatial_dwt_buffer;
464 d593e329 Michael Niedermayer
    IDWTELEM *spatial_idwt_buffer;
465 791e7b83 Michael Niedermayer
    int colorspace_type;
466
    int chroma_h_shift;
467
    int chroma_v_shift;
468
    int spatial_scalability;
469
    int qlog;
470 396a5e68 Michael Niedermayer
    int last_qlog;
471 155ec6ed Michael Niedermayer
    int lambda;
472
    int lambda2;
473 4e64bead Loren Merritt
    int pass1_rc;
474 791e7b83 Michael Niedermayer
    int mv_scale;
475 396a5e68 Michael Niedermayer
    int last_mv_scale;
476 791e7b83 Michael Niedermayer
    int qbias;
477 396a5e68 Michael Niedermayer
    int last_qbias;
478 791e7b83 Michael Niedermayer
#define QBIAS_SHIFT 3
479 155ec6ed Michael Niedermayer
    int b_width;
480
    int b_height;
481
    int block_max_depth;
482 396a5e68 Michael Niedermayer
    int last_block_max_depth;
483 791e7b83 Michael Niedermayer
    Plane plane[MAX_PLANES];
484 155ec6ed Michael Niedermayer
    BlockNode *block;
485 51d6a3cf Michael Niedermayer
#define ME_CACHE_SIZE 1024
486
    int me_cache[ME_CACHE_SIZE];
487
    int me_cache_generation;
488 a0d1931c Yartrebo
    slice_buffer sb;
489 155ec6ed Michael Niedermayer
490 e6464f8b Diego Biurrun
    MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to eventually make the motion estimation independent of MpegEncContext, so this will be removed then (FIXME/XXX)
491 bd2b6b33 Måns Rullgård
492
    uint8_t *scratchbuf;
493 791e7b83 Michael Niedermayer
}SnowContext;
494
495 f9e6ebf7 Loren Merritt
typedef struct {
496 d593e329 Michael Niedermayer
    IDWTELEM *b0;
497
    IDWTELEM *b1;
498
    IDWTELEM *b2;
499
    IDWTELEM *b3;
500 f9e6ebf7 Loren Merritt
    int y;
501 fe5c7e58 Michael Niedermayer
} DWTCompose;
502 f9e6ebf7 Loren Merritt
503 a0d1931c Yartrebo
#define slice_buffer_get_line(slice_buf, line_num) ((slice_buf)->line[line_num] ? (slice_buf)->line[line_num] : slice_buffer_load_line((slice_buf), (line_num)))
504
//#define slice_buffer_get_line(slice_buf, line_num) (slice_buffer_load_line((slice_buf), (line_num)))
505
506 51d6a3cf Michael Niedermayer
static void iterative_me(SnowContext *s);
507
508 d593e329 Michael Niedermayer
static void slice_buffer_init(slice_buffer * buf, int line_count, int max_allocated_lines, int line_width, IDWTELEM * base_buffer)
509 a0d1931c Yartrebo
{
510
    int i;
511 115329f1 Diego Biurrun
512 a0d1931c Yartrebo
    buf->base_buffer = base_buffer;
513
    buf->line_count = line_count;
514
    buf->line_width = line_width;
515
    buf->data_count = max_allocated_lines;
516 d593e329 Michael Niedermayer
    buf->line = av_mallocz (sizeof(IDWTELEM *) * line_count);
517
    buf->data_stack = av_malloc (sizeof(IDWTELEM *) * max_allocated_lines);
518 115329f1 Diego Biurrun
519 ef3dfbd4 Diego Biurrun
    for(i = 0; i < max_allocated_lines; i++){
520 d593e329 Michael Niedermayer
        buf->data_stack[i] = av_malloc (sizeof(IDWTELEM) * line_width);
521 a0d1931c Yartrebo
    }
522 115329f1 Diego Biurrun
523 a0d1931c Yartrebo
    buf->data_stack_top = max_allocated_lines - 1;
524
}
525
526 d593e329 Michael Niedermayer
static IDWTELEM * slice_buffer_load_line(slice_buffer * buf, int line)
527 a0d1931c Yartrebo
{
528
    int offset;
529 d593e329 Michael Niedermayer
    IDWTELEM * buffer;
530 115329f1 Diego Biurrun
531 a0d1931c Yartrebo
    assert(buf->data_stack_top >= 0);
532
//  assert(!buf->line[line]);
533
    if (buf->line[line])
534
        return buf->line[line];
535 115329f1 Diego Biurrun
536 a0d1931c Yartrebo
    offset = buf->line_width * line;
537
    buffer = buf->data_stack[buf->data_stack_top];
538
    buf->data_stack_top--;
539
    buf->line[line] = buffer;
540 115329f1 Diego Biurrun
541 a0d1931c Yartrebo
    return buffer;
542
}
543
544
static void slice_buffer_release(slice_buffer * buf, int line)
545
{
546
    int offset;
547 d593e329 Michael Niedermayer
    IDWTELEM * buffer;
548 a0d1931c Yartrebo
549
    assert(line >= 0 && line < buf->line_count);
550
    assert(buf->line[line]);
551
552
    offset = buf->line_width * line;
553
    buffer = buf->line[line];
554
    buf->data_stack_top++;
555
    buf->data_stack[buf->data_stack_top] = buffer;
556
    buf->line[line] = NULL;
557
}
558
559
static void slice_buffer_flush(slice_buffer * buf)
560
{
561
    int i;
562 ef3dfbd4 Diego Biurrun
    for(i = 0; i < buf->line_count; i++){
563 a0d1931c Yartrebo
        if (buf->line[i])
564
            slice_buffer_release(buf, i);
565
    }
566
}
567
568
static void slice_buffer_destroy(slice_buffer * buf)
569
{
570
    int i;
571
    slice_buffer_flush(buf);
572 115329f1 Diego Biurrun
573 ef3dfbd4 Diego Biurrun
    for(i = buf->data_count - 1; i >= 0; i--){
574 e7c8206e Michael Niedermayer
        av_freep(&buf->data_stack[i]);
575 a0d1931c Yartrebo
    }
576 e7c8206e Michael Niedermayer
    av_freep(&buf->data_stack);
577
    av_freep(&buf->line);
578 a0d1931c Yartrebo
}
579
580 bb270c08 Diego Biurrun
#ifdef __sgi
581 2554db9b Michael Niedermayer
// Avoid a name clash on SGI IRIX
582 bb270c08 Diego Biurrun
#undef qexp
583 2554db9b Michael Niedermayer
#endif
584 034aff03 Michael Niedermayer
#define QEXPSHIFT (7-FRAC_BITS+8) //FIXME try to change this to 0
585 c97de57c Michael Niedermayer
static uint8_t qexp[QROOT];
586 791e7b83 Michael Niedermayer
587
static inline int mirror(int v, int m){
588 13705b69 Michael Niedermayer
    while((unsigned)v > (unsigned)m){
589
        v=-v;
590
        if(v<0) v+= 2*m;
591
    }
592
    return v;
593 791e7b83 Michael Niedermayer
}
594
595 28869757 Michael Niedermayer
static inline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed){
596 791e7b83 Michael Niedermayer
    int i;
597
598
    if(v){
599 c26abfa5 Diego Biurrun
        const int a= FFABS(v);
600 791e7b83 Michael Niedermayer
        const int e= av_log2(a);
601
#if 1
602 115329f1 Diego Biurrun
        const int el= FFMIN(e, 10);
603 28869757 Michael Niedermayer
        put_rac(c, state+0, 0);
604 791e7b83 Michael Niedermayer
605
        for(i=0; i<el; i++){
606 28869757 Michael Niedermayer
            put_rac(c, state+1+i, 1);  //1..10
607 791e7b83 Michael Niedermayer
        }
608
        for(; i<e; i++){
609 28869757 Michael Niedermayer
            put_rac(c, state+1+9, 1);  //1..10
610 791e7b83 Michael Niedermayer
        }
611 28869757 Michael Niedermayer
        put_rac(c, state+1+FFMIN(i,9), 0);
612 791e7b83 Michael Niedermayer
613
        for(i=e-1; i>=el; i--){
614 28869757 Michael Niedermayer
            put_rac(c, state+22+9, (a>>i)&1); //22..31
615 791e7b83 Michael Niedermayer
        }
616
        for(; i>=0; i--){
617 28869757 Michael Niedermayer
            put_rac(c, state+22+i, (a>>i)&1); //22..31
618 791e7b83 Michael Niedermayer
        }
619
620
        if(is_signed)
621 28869757 Michael Niedermayer
            put_rac(c, state+11 + el, v < 0); //11..21
622 791e7b83 Michael Niedermayer
#else
623 115329f1 Diego Biurrun
624 28869757 Michael Niedermayer
        put_rac(c, state+0, 0);
625 791e7b83 Michael Niedermayer
        if(e<=9){
626
            for(i=0; i<e; i++){
627 28869757 Michael Niedermayer
                put_rac(c, state+1+i, 1);  //1..10
628 791e7b83 Michael Niedermayer
            }
629 28869757 Michael Niedermayer
            put_rac(c, state+1+i, 0);
630 791e7b83 Michael Niedermayer
631
            for(i=e-1; i>=0; i--){
632 28869757 Michael Niedermayer
                put_rac(c, state+22+i, (a>>i)&1); //22..31
633 791e7b83 Michael Niedermayer
            }
634
635
            if(is_signed)
636 28869757 Michael Niedermayer
                put_rac(c, state+11 + e, v < 0); //11..21
637 791e7b83 Michael Niedermayer
        }else{
638
            for(i=0; i<e; i++){
639 28869757 Michael Niedermayer
                put_rac(c, state+1+FFMIN(i,9), 1);  //1..10
640 791e7b83 Michael Niedermayer
            }
641 28869757 Michael Niedermayer
            put_rac(c, state+1+FFMIN(i,9), 0);
642 791e7b83 Michael Niedermayer
643
            for(i=e-1; i>=0; i--){
644 28869757 Michael Niedermayer
                put_rac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31
645 791e7b83 Michael Niedermayer
            }
646
647
            if(is_signed)
648 28869757 Michael Niedermayer
                put_rac(c, state+11 + FFMIN(e,10), v < 0); //11..21
649 791e7b83 Michael Niedermayer
        }
650 96e2fbf2 Diego Biurrun
#endif /* 1 */
651 791e7b83 Michael Niedermayer
    }else{
652 28869757 Michael Niedermayer
        put_rac(c, state+0, 1);
653 791e7b83 Michael Niedermayer
    }
654
}
655
656 28869757 Michael Niedermayer
static inline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
657
    if(get_rac(c, state+0))
658 791e7b83 Michael Niedermayer
        return 0;
659
    else{
660 7c2425d2 Loren Merritt
        int i, e, a;
661
        e= 0;
662 28869757 Michael Niedermayer
        while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
663 7c2425d2 Loren Merritt
            e++;
664 791e7b83 Michael Niedermayer
        }
665 7c2425d2 Loren Merritt
666 791e7b83 Michael Niedermayer
        a= 1;
667 7c2425d2 Loren Merritt
        for(i=e-1; i>=0; i--){
668 28869757 Michael Niedermayer
            a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
669 791e7b83 Michael Niedermayer
        }
670
671 28869757 Michael Niedermayer
        if(is_signed && get_rac(c, state+11 + FFMIN(e,10))) //11..21
672 791e7b83 Michael Niedermayer
            return -a;
673
        else
674
            return a;
675
    }
676
}
677
678 28869757 Michael Niedermayer
static inline void put_symbol2(RangeCoder *c, uint8_t *state, int v, int log2){
679 4f4e9633 Michael Niedermayer
    int i;
680 0635cbfc Michael Niedermayer
    int r= log2>=0 ? 1<<log2 : 1;
681 4f4e9633 Michael Niedermayer
682
    assert(v>=0);
683 0635cbfc Michael Niedermayer
    assert(log2>=-4);
684
685
    while(v >= r){
686 28869757 Michael Niedermayer
        put_rac(c, state+4+log2, 1);
687 0635cbfc Michael Niedermayer
        v -= r;
688 4f4e9633 Michael Niedermayer
        log2++;
689 0635cbfc Michael Niedermayer
        if(log2>0) r+=r;
690 4f4e9633 Michael Niedermayer
    }
691 28869757 Michael Niedermayer
    put_rac(c, state+4+log2, 0);
692 115329f1 Diego Biurrun
693 4f4e9633 Michael Niedermayer
    for(i=log2-1; i>=0; i--){
694 28869757 Michael Niedermayer
        put_rac(c, state+31-i, (v>>i)&1);
695 4f4e9633 Michael Niedermayer
    }
696
}
697
698 28869757 Michael Niedermayer
static inline int get_symbol2(RangeCoder *c, uint8_t *state, int log2){
699 4f4e9633 Michael Niedermayer
    int i;
700 0635cbfc Michael Niedermayer
    int r= log2>=0 ? 1<<log2 : 1;
701 4f4e9633 Michael Niedermayer
    int v=0;
702
703 0635cbfc Michael Niedermayer
    assert(log2>=-4);
704
705 28869757 Michael Niedermayer
    while(get_rac(c, state+4+log2)){
706 0635cbfc Michael Niedermayer
        v+= r;
707 4f4e9633 Michael Niedermayer
        log2++;
708 0635cbfc Michael Niedermayer
        if(log2>0) r+=r;
709 4f4e9633 Michael Niedermayer
    }
710 115329f1 Diego Biurrun
711 4f4e9633 Michael Niedermayer
    for(i=log2-1; i>=0; i--){
712 28869757 Michael Niedermayer
        v+= get_rac(c, state+31-i)<<i;
713 4f4e9633 Michael Niedermayer
    }
714
715
    return v;
716
}
717
718 9d14ffbc Luca Barbato
static av_always_inline void
719
lift(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
720
     int dst_step, int src_step, int ref_step,
721
     int width, int mul, int add, int shift,
722
     int highpass, int inverse){
723 791e7b83 Michael Niedermayer
    const int mirror_left= !highpass;
724
    const int mirror_right= (width&1) ^ highpass;
725
    const int w= (width>>1) - 1 + (highpass & width);
726
    int i;
727
728
#define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
729
    if(mirror_left){
730
        dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
731
        dst += dst_step;
732
        src += src_step;
733
    }
734 115329f1 Diego Biurrun
735 791e7b83 Michael Niedermayer
    for(i=0; i<w; i++){
736 9d14ffbc Luca Barbato
        dst[i*dst_step] =
737
            LIFT(src[i*src_step],
738
                 ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift),
739
                 inverse);
740 791e7b83 Michael Niedermayer
    }
741 115329f1 Diego Biurrun
742 791e7b83 Michael Niedermayer
    if(mirror_right){
743 9d14ffbc Luca Barbato
        dst[w*dst_step] =
744
            LIFT(src[w*src_step],
745
                 ((mul*2*ref[w*ref_step]+add)>>shift),
746
                 inverse);
747 791e7b83 Michael Niedermayer
    }
748
}
749
750 9d14ffbc Luca Barbato
static av_always_inline void
751
inv_lift(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *ref,
752
         int dst_step, int src_step, int ref_step,
753
         int width, int mul, int add, int shift,
754
         int highpass, int inverse){
755 d593e329 Michael Niedermayer
    const int mirror_left= !highpass;
756
    const int mirror_right= (width&1) ^ highpass;
757
    const int w= (width>>1) - 1 + (highpass & width);
758
    int i;
759
760
#define LIFT(src, ref, inv) ((src) + ((inv) ? - (ref) : + (ref)))
761
    if(mirror_left){
762
        dst[0] = LIFT(src[0], ((mul*2*ref[0]+add)>>shift), inverse);
763
        dst += dst_step;
764
        src += src_step;
765
    }
766
767
    for(i=0; i<w; i++){
768 9d14ffbc Luca Barbato
        dst[i*dst_step] =
769
            LIFT(src[i*src_step],
770
                 ((mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add)>>shift),
771
                 inverse);
772 d593e329 Michael Niedermayer
    }
773
774
    if(mirror_right){
775 9d14ffbc Luca Barbato
        dst[w*dst_step] =
776
            LIFT(src[w*src_step],
777
                 ((mul*2*ref[w*ref_step]+add)>>shift),
778
                 inverse);
779 d593e329 Michael Niedermayer
    }
780
}
781
782 059715a4 Robert Edele
#ifndef liftS
783 9d14ffbc Luca Barbato
static av_always_inline void
784
liftS(DWTELEM *dst, DWTELEM *src, DWTELEM *ref,
785
      int dst_step, int src_step, int ref_step,
786
      int width, int mul, int add, int shift,
787
      int highpass, int inverse){
788 f5a71928 Michael Niedermayer
    const int mirror_left= !highpass;
789
    const int mirror_right= (width&1) ^ highpass;
790
    const int w= (width>>1) - 1 + (highpass & width);
791
    int i;
792
793
    assert(shift == 4);
794 9d14ffbc Luca Barbato
#define LIFTS(src, ref, inv) \
795
        ((inv) ? \
796
            (src) + (((ref) + 4*(src))>>shift): \
797
            -((-16*(src) + (ref) + add/4 + 1 + (5<<25))/(5*4) - (1<<23)))
798 f5a71928 Michael Niedermayer
    if(mirror_left){
799
        dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse);
800
        dst += dst_step;
801
        src += src_step;
802
    }
803 115329f1 Diego Biurrun
804 f5a71928 Michael Niedermayer
    for(i=0; i<w; i++){
805 9d14ffbc Luca Barbato
        dst[i*dst_step] =
806
            LIFTS(src[i*src_step],
807
                  mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add,
808
                  inverse);
809 f5a71928 Michael Niedermayer
    }
810 115329f1 Diego Biurrun
811 f5a71928 Michael Niedermayer
    if(mirror_right){
812 9d14ffbc Luca Barbato
        dst[w*dst_step] =
813
            LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse);
814 f5a71928 Michael Niedermayer
    }
815
}
816 9d14ffbc Luca Barbato
static av_always_inline void
817
inv_liftS(IDWTELEM *dst, IDWTELEM *src, IDWTELEM *ref,
818
          int dst_step, int src_step, int ref_step,
819
          int width, int mul, int add, int shift,
820
          int highpass, int inverse){
821 d593e329 Michael Niedermayer
    const int mirror_left= !highpass;
822
    const int mirror_right= (width&1) ^ highpass;
823
    const int w= (width>>1) - 1 + (highpass & width);
824
    int i;
825
826
    assert(shift == 4);
827 9d14ffbc Luca Barbato
#define LIFTS(src, ref, inv) \
828
    ((inv) ? \
829
        (src) + (((ref) + 4*(src))>>shift): \
830
        -((-16*(src) + (ref) + add/4 + 1 + (5<<25))/(5*4) - (1<<23)))
831 d593e329 Michael Niedermayer
    if(mirror_left){
832
        dst[0] = LIFTS(src[0], mul*2*ref[0]+add, inverse);
833
        dst += dst_step;
834
        src += src_step;
835
    }
836
837
    for(i=0; i<w; i++){
838 9d14ffbc Luca Barbato
        dst[i*dst_step] =
839
            LIFTS(src[i*src_step],
840
                  mul*(ref[i*ref_step] + ref[(i+1)*ref_step])+add,
841
                  inverse);
842 d593e329 Michael Niedermayer
    }
843
844
    if(mirror_right){
845 9d14ffbc Luca Barbato
        dst[w*dst_step] =
846
            LIFTS(src[w*src_step], mul*2*ref[w*ref_step]+add, inverse);
847 d593e329 Michael Niedermayer
    }
848
}
849 301df480 Diego Biurrun
#endif /* ! liftS */
850 f5a71928 Michael Niedermayer
851 aa25a462 D Richard Felker III
static void horizontal_decompose53i(DWTELEM *b, int width){
852
    DWTELEM temp[width];
853 791e7b83 Michael Niedermayer
    const int width2= width>>1;
854 62ab0b78 Aurelien Jacobs
    int x;
855 791e7b83 Michael Niedermayer
    const int w2= (width+1)>>1;
856
857
    for(x=0; x<width2; x++){
858
        temp[x   ]= b[2*x    ];
859
        temp[x+w2]= b[2*x + 1];
860
    }
861
    if(width&1)
862
        temp[x   ]= b[2*x    ];
863
#if 0
864 62ab0b78 Aurelien Jacobs
    {
865
    int A1,A2,A3,A4;
866 791e7b83 Michael Niedermayer
    A2= temp[1       ];
867
    A4= temp[0       ];
868
    A1= temp[0+width2];
869
    A1 -= (A2 + A4)>>1;
870
    A4 += (A1 + 1)>>1;
871
    b[0+width2] = A1;
872
    b[0       ] = A4;
873
    for(x=1; x+1<width2; x+=2){
874
        A3= temp[x+width2];
875
        A4= temp[x+1     ];
876
        A3 -= (A2 + A4)>>1;
877
        A2 += (A1 + A3 + 2)>>2;
878
        b[x+width2] = A3;
879
        b[x       ] = A2;
880

881
        A1= temp[x+1+width2];
882
        A2= temp[x+2       ];
883
        A1 -= (A2 + A4)>>1;
884
        A4 += (A1 + A3 + 2)>>2;
885
        b[x+1+width2] = A1;
886
        b[x+1       ] = A4;
887
    }
888
    A3= temp[width-1];
889
    A3 -= A2;
890
    A2 += (A1 + A3 + 2)>>2;
891
    b[width -1] = A3;
892
    b[width2-1] = A2;
893 62ab0b78 Aurelien Jacobs
    }
894 115329f1 Diego Biurrun
#else
895 791e7b83 Michael Niedermayer
    lift(b+w2, temp+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 0);
896
    lift(b   , temp   , b+w2, 1, 1, 1, width,  1, 2, 2, 0, 0);
897 301df480 Diego Biurrun
#endif /* 0 */
898 791e7b83 Michael Niedermayer
}
899
900 aa25a462 D Richard Felker III
static void vertical_decompose53iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
901 791e7b83 Michael Niedermayer
    int i;
902 115329f1 Diego Biurrun
903 791e7b83 Michael Niedermayer
    for(i=0; i<width; i++){
904
        b1[i] -= (b0[i] + b2[i])>>1;
905
    }
906
}
907
908 aa25a462 D Richard Felker III
static void vertical_decompose53iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
909 791e7b83 Michael Niedermayer
    int i;
910 115329f1 Diego Biurrun
911 791e7b83 Michael Niedermayer
    for(i=0; i<width; i++){
912
        b1[i] += (b0[i] + b2[i] + 2)>>2;
913
    }
914
}
915
916 aa25a462 D Richard Felker III
static void spatial_decompose53i(DWTELEM *buffer, int width, int height, int stride){
917 39c61bbb Michael Niedermayer
    int y;
918 791e7b83 Michael Niedermayer
    DWTELEM *b0= buffer + mirror(-2-1, height-1)*stride;
919
    DWTELEM *b1= buffer + mirror(-2  , height-1)*stride;
920 115329f1 Diego Biurrun
921 791e7b83 Michael Niedermayer
    for(y=-2; y<height; y+=2){
922
        DWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
923
        DWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
924
925 13705b69 Michael Niedermayer
        if(y+1<(unsigned)height) horizontal_decompose53i(b2, width);
926
        if(y+2<(unsigned)height) horizontal_decompose53i(b3, width);
927 115329f1 Diego Biurrun
928 13705b69 Michael Niedermayer
        if(y+1<(unsigned)height) vertical_decompose53iH0(b1, b2, b3, width);
929
        if(y+0<(unsigned)height) vertical_decompose53iL0(b0, b1, b2, width);
930 115329f1 Diego Biurrun
931 791e7b83 Michael Niedermayer
        b0=b2;
932
        b1=b3;
933
    }
934
}
935
936 aa25a462 D Richard Felker III
static void horizontal_decompose97i(DWTELEM *b, int width){
937
    DWTELEM temp[width];
938 791e7b83 Michael Niedermayer
    const int w2= (width+1)>>1;
939
940 ce611a27 Michael Niedermayer
    lift (temp+w2, b    +1, b      , 1, 2, 2, width,  W_AM, W_AO, W_AS, 1, 1);
941
    liftS(temp   , b      , temp+w2, 1, 2, 1, width,  W_BM, W_BO, W_BS, 0, 0);
942 ff06e067 Michael Niedermayer
    lift (b   +w2, temp+w2, temp   , 1, 1, 1, width,  W_CM, W_CO, W_CS, 1, 0);
943 791e7b83 Michael Niedermayer
    lift (b      , temp   , b   +w2, 1, 1, 1, width,  W_DM, W_DO, W_DS, 0, 0);
944
}
945
946
947 aa25a462 D Richard Felker III
static void vertical_decompose97iH0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
948 791e7b83 Michael Niedermayer
    int i;
949 115329f1 Diego Biurrun
950 791e7b83 Michael Niedermayer
    for(i=0; i<width; i++){
951
        b1[i] -= (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
952
    }
953
}
954
955 aa25a462 D Richard Felker III
static void vertical_decompose97iH1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
956 791e7b83 Michael Niedermayer
    int i;
957 115329f1 Diego Biurrun
958 791e7b83 Michael Niedermayer
    for(i=0; i<width; i++){
959
        b1[i] += (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
960
    }
961
}
962
963 aa25a462 D Richard Felker III
static void vertical_decompose97iL0(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
964 791e7b83 Michael Niedermayer
    int i;
965 115329f1 Diego Biurrun
966 791e7b83 Michael Niedermayer
    for(i=0; i<width; i++){
967 f5a71928 Michael Niedermayer
#ifdef liftS
968 791e7b83 Michael Niedermayer
        b1[i] -= (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
969 f5a71928 Michael Niedermayer
#else
970 ce611a27 Michael Niedermayer
        b1[i] = (16*4*b1[i] - 4*(b0[i] + b2[i]) + W_BO*5 + (5<<27)) / (5*16) - (1<<23);
971 f5a71928 Michael Niedermayer
#endif
972 791e7b83 Michael Niedermayer
    }
973
}
974
975 aa25a462 D Richard Felker III
static void vertical_decompose97iL1(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, int width){
976 791e7b83 Michael Niedermayer
    int i;
977 115329f1 Diego Biurrun
978 791e7b83 Michael Niedermayer
    for(i=0; i<width; i++){
979
        b1[i] += (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
980
    }
981
}
982
983 aa25a462 D Richard Felker III
static void spatial_decompose97i(DWTELEM *buffer, int width, int height, int stride){
984 39c61bbb Michael Niedermayer
    int y;
985 791e7b83 Michael Niedermayer
    DWTELEM *b0= buffer + mirror(-4-1, height-1)*stride;
986
    DWTELEM *b1= buffer + mirror(-4  , height-1)*stride;
987
    DWTELEM *b2= buffer + mirror(-4+1, height-1)*stride;
988
    DWTELEM *b3= buffer + mirror(-4+2, height-1)*stride;
989 115329f1 Diego Biurrun
990 791e7b83 Michael Niedermayer
    for(y=-4; y<height; y+=2){
991
        DWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
992
        DWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
993
994 13705b69 Michael Niedermayer
        if(y+3<(unsigned)height) horizontal_decompose97i(b4, width);
995
        if(y+4<(unsigned)height) horizontal_decompose97i(b5, width);
996 115329f1 Diego Biurrun
997 13705b69 Michael Niedermayer
        if(y+3<(unsigned)height) vertical_decompose97iH0(b3, b4, b5, width);
998
        if(y+2<(unsigned)height) vertical_decompose97iL0(b2, b3, b4, width);
999
        if(y+1<(unsigned)height) vertical_decompose97iH1(b1, b2, b3, width);
1000
        if(y+0<(unsigned)height) vertical_decompose97iL1(b0, b1, b2, width);
1001 791e7b83 Michael Niedermayer
1002
        b0=b2;
1003
        b1=b3;
1004
        b2=b4;
1005
        b3=b5;
1006
    }
1007
}
1008
1009 aa25a462 D Richard Felker III
void ff_spatial_dwt(DWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
1010 791e7b83 Michael Niedermayer
    int level;
1011 115329f1 Diego Biurrun
1012 46c281e8 Michael Niedermayer
    for(level=0; level<decomposition_count; level++){
1013
        switch(type){
1014 d4b287ed Loren Merritt
        case DWT_97: spatial_decompose97i(buffer, width>>level, height>>level, stride<<level); break;
1015
        case DWT_53: spatial_decompose53i(buffer, width>>level, height>>level, stride<<level); break;
1016 791e7b83 Michael Niedermayer
        }
1017
    }
1018
}
1019
1020 d593e329 Michael Niedermayer
static void horizontal_compose53i(IDWTELEM *b, int width){
1021
    IDWTELEM temp[width];
1022 791e7b83 Michael Niedermayer
    const int width2= width>>1;
1023
    const int w2= (width+1)>>1;
1024 62ab0b78 Aurelien Jacobs
    int x;
1025 791e7b83 Michael Niedermayer
1026
#if 0
1027 62ab0b78 Aurelien Jacobs
    int A1,A2,A3,A4;
1028 791e7b83 Michael Niedermayer
    A2= temp[1       ];
1029
    A4= temp[0       ];
1030
    A1= temp[0+width2];
1031
    A1 -= (A2 + A4)>>1;
1032
    A4 += (A1 + 1)>>1;
1033
    b[0+width2] = A1;
1034
    b[0       ] = A4;
1035
    for(x=1; x+1<width2; x+=2){
1036
        A3= temp[x+width2];
1037
        A4= temp[x+1     ];
1038
        A3 -= (A2 + A4)>>1;
1039
        A2 += (A1 + A3 + 2)>>2;
1040
        b[x+width2] = A3;
1041
        b[x       ] = A2;
1042

1043
        A1= temp[x+1+width2];
1044
        A2= temp[x+2       ];
1045
        A1 -= (A2 + A4)>>1;
1046
        A4 += (A1 + A3 + 2)>>2;
1047
        b[x+1+width2] = A1;
1048
        b[x+1       ] = A4;
1049
    }
1050
    A3= temp[width-1];
1051
    A3 -= A2;
1052
    A2 += (A1 + A3 + 2)>>2;
1053
    b[width -1] = A3;
1054
    b[width2-1] = A2;
1055 115329f1 Diego Biurrun
#else
1056 d593e329 Michael Niedermayer
    inv_lift(temp   , b   , b+w2, 1, 1, 1, width,  1, 2, 2, 0, 1);
1057
    inv_lift(temp+w2, b+w2, temp, 1, 1, 1, width, -1, 0, 1, 1, 1);
1058 96e2fbf2 Diego Biurrun
#endif /* 0 */
1059 791e7b83 Michael Niedermayer
    for(x=0; x<width2; x++){
1060
        b[2*x    ]= temp[x   ];
1061
        b[2*x + 1]= temp[x+w2];
1062
    }
1063
    if(width&1)
1064
        b[2*x    ]= temp[x   ];
1065
}
1066
1067 d593e329 Michael Niedermayer
static void vertical_compose53iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
1068 791e7b83 Michael Niedermayer
    int i;
1069 115329f1 Diego Biurrun
1070 791e7b83 Michael Niedermayer
    for(i=0; i<width; i++){
1071
        b1[i] += (b0[i] + b2[i])>>1;
1072
    }
1073
}
1074
1075 d593e329 Michael Niedermayer
static void vertical_compose53iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
1076 791e7b83 Michael Niedermayer
    int i;
1077 115329f1 Diego Biurrun
1078 791e7b83 Michael Niedermayer
    for(i=0; i<width; i++){
1079
        b1[i] -= (b0[i] + b2[i] + 2)>>2;
1080
    }
1081
}
1082
1083 fe5c7e58 Michael Niedermayer
static void spatial_compose53i_buffered_init(DWTCompose *cs, slice_buffer * sb, int height, int stride_line){
1084 a0d1931c Yartrebo
    cs->b0 = slice_buffer_get_line(sb, mirror(-1-1, height-1) * stride_line);
1085
    cs->b1 = slice_buffer_get_line(sb, mirror(-1  , height-1) * stride_line);
1086
    cs->y = -1;
1087
}
1088
1089 fe5c7e58 Michael Niedermayer
static void spatial_compose53i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride){
1090 f9e6ebf7 Loren Merritt
    cs->b0 = buffer + mirror(-1-1, height-1)*stride;
1091
    cs->b1 = buffer + mirror(-1  , height-1)*stride;
1092
    cs->y = -1;
1093
}
1094
1095 fe5c7e58 Michael Niedermayer
static void spatial_compose53i_dy_buffered(DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line){
1096 a0d1931c Yartrebo
    int y= cs->y;
1097 115329f1 Diego Biurrun
1098 d593e329 Michael Niedermayer
    IDWTELEM *b0= cs->b0;
1099
    IDWTELEM *b1= cs->b1;
1100
    IDWTELEM *b2= slice_buffer_get_line(sb, mirror(y+1, height-1) * stride_line);
1101
    IDWTELEM *b3= slice_buffer_get_line(sb, mirror(y+2, height-1) * stride_line);
1102 a0d1931c Yartrebo
1103 13705b69 Michael Niedermayer
        if(y+1<(unsigned)height) vertical_compose53iL0(b1, b2, b3, width);
1104
        if(y+0<(unsigned)height) vertical_compose53iH0(b0, b1, b2, width);
1105 a0d1931c Yartrebo
1106 13705b69 Michael Niedermayer
        if(y-1<(unsigned)height) horizontal_compose53i(b0, width);
1107
        if(y+0<(unsigned)height) horizontal_compose53i(b1, width);
1108 a0d1931c Yartrebo
1109
    cs->b0 = b2;
1110
    cs->b1 = b3;
1111
    cs->y += 2;
1112
}
1113
1114 fe5c7e58 Michael Niedermayer
static void spatial_compose53i_dy(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride){
1115 f9e6ebf7 Loren Merritt
    int y= cs->y;
1116 d593e329 Michael Niedermayer
    IDWTELEM *b0= cs->b0;
1117
    IDWTELEM *b1= cs->b1;
1118
    IDWTELEM *b2= buffer + mirror(y+1, height-1)*stride;
1119
    IDWTELEM *b3= buffer + mirror(y+2, height-1)*stride;
1120 791e7b83 Michael Niedermayer
1121 13705b69 Michael Niedermayer
        if(y+1<(unsigned)height) vertical_compose53iL0(b1, b2, b3, width);
1122
        if(y+0<(unsigned)height) vertical_compose53iH0(b0, b1, b2, width);
1123 791e7b83 Michael Niedermayer
1124 13705b69 Michael Niedermayer
        if(y-1<(unsigned)height) horizontal_compose53i(b0, width);
1125
        if(y+0<(unsigned)height) horizontal_compose53i(b1, width);
1126 791e7b83 Michael Niedermayer
1127 f9e6ebf7 Loren Merritt
    cs->b0 = b2;
1128
    cs->b1 = b3;
1129
    cs->y += 2;
1130
}
1131
1132 1918057c Michael Niedermayer
static void av_unused spatial_compose53i(IDWTELEM *buffer, int width, int height, int stride){
1133 fe5c7e58 Michael Niedermayer
    DWTCompose cs;
1134 9a3bb2b8 Michael Niedermayer
    spatial_compose53i_init(&cs, buffer, height, stride);
1135
    while(cs.y <= height)
1136
        spatial_compose53i_dy(&cs, buffer, width, height, stride);
1137
}
1138
1139 791e7b83 Michael Niedermayer
1140 d593e329 Michael Niedermayer
void ff_snow_horizontal_compose97i(IDWTELEM *b, int width){
1141
    IDWTELEM temp[width];
1142 791e7b83 Michael Niedermayer
    const int w2= (width+1)>>1;
1143
1144 d593e329 Michael Niedermayer
    inv_lift (temp   , b      , b   +w2, 1, 1, 1, width,  W_DM, W_DO, W_DS, 0, 1);
1145 ff06e067 Michael Niedermayer
    inv_lift (temp+w2, b   +w2, temp   , 1, 1, 1, width,  W_CM, W_CO, W_CS, 1, 1);
1146 d593e329 Michael Niedermayer
    inv_liftS(b      , temp   , temp+w2, 2, 1, 1, width,  W_BM, W_BO, W_BS, 0, 1);
1147
    inv_lift (b+1    , temp+w2, b      , 2, 1, 2, width,  W_AM, W_AO, W_AS, 1, 0);
1148 791e7b83 Michael Niedermayer
}
1149
1150 d593e329 Michael Niedermayer
static void vertical_compose97iH0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
1151 791e7b83 Michael Niedermayer
    int i;
1152 115329f1 Diego Biurrun
1153 791e7b83 Michael Niedermayer
    for(i=0; i<width; i++){
1154
        b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
1155
    }
1156
}
1157
1158 d593e329 Michael Niedermayer
static void vertical_compose97iH1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
1159 791e7b83 Michael Niedermayer
    int i;
1160 115329f1 Diego Biurrun
1161 791e7b83 Michael Niedermayer
    for(i=0; i<width; i++){
1162
        b1[i] -= (W_CM*(b0[i] + b2[i])+W_CO)>>W_CS;
1163
    }
1164
}
1165
1166 d593e329 Michael Niedermayer
static void vertical_compose97iL0(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
1167 791e7b83 Michael Niedermayer
    int i;
1168 115329f1 Diego Biurrun
1169 791e7b83 Michael Niedermayer
    for(i=0; i<width; i++){
1170 f5a71928 Michael Niedermayer
#ifdef liftS
1171 791e7b83 Michael Niedermayer
        b1[i] += (W_BM*(b0[i] + b2[i])+W_BO)>>W_BS;
1172 f5a71928 Michael Niedermayer
#else
1173
        b1[i] += (W_BM*(b0[i] + b2[i])+4*b1[i]+W_BO)>>W_BS;
1174
#endif
1175 791e7b83 Michael Niedermayer
    }
1176
}
1177
1178 d593e329 Michael Niedermayer
static void vertical_compose97iL1(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, int width){
1179 791e7b83 Michael Niedermayer
    int i;
1180 115329f1 Diego Biurrun
1181 791e7b83 Michael Niedermayer
    for(i=0; i<width; i++){
1182
        b1[i] -= (W_DM*(b0[i] + b2[i])+W_DO)>>W_DS;
1183
    }
1184
}
1185
1186 d593e329 Michael Niedermayer
void ff_snow_vertical_compose97i(IDWTELEM *b0, IDWTELEM *b1, IDWTELEM *b2, IDWTELEM *b3, IDWTELEM *b4, IDWTELEM *b5, int width){
1187 565a45ac Michael Niedermayer
    int i;
1188 115329f1 Diego Biurrun
1189 565a45ac Michael Niedermayer
    for(i=0; i<width; i++){
1190
        b4[i] -= (W_DM*(b3[i] + b5[i])+W_DO)>>W_DS;
1191
        b3[i] -= (W_CM*(b2[i] + b4[i])+W_CO)>>W_CS;
1192 f5a71928 Michael Niedermayer
#ifdef liftS
1193 565a45ac Michael Niedermayer
        b2[i] += (W_BM*(b1[i] + b3[i])+W_BO)>>W_BS;
1194 f5a71928 Michael Niedermayer
#else
1195
        b2[i] += (W_BM*(b1[i] + b3[i])+4*b2[i]+W_BO)>>W_BS;
1196
#endif
1197 565a45ac Michael Niedermayer
        b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
1198
    }
1199
}
1200
1201 fe5c7e58 Michael Niedermayer
static void spatial_compose97i_buffered_init(DWTCompose *cs, slice_buffer * sb, int height, int stride_line){
1202 a0d1931c Yartrebo
    cs->b0 = slice_buffer_get_line(sb, mirror(-3-1, height-1) * stride_line);
1203
    cs->b1 = slice_buffer_get_line(sb, mirror(-3  , height-1) * stride_line);
1204
    cs->b2 = slice_buffer_get_line(sb, mirror(-3+1, height-1) * stride_line);
1205
    cs->b3 = slice_buffer_get_line(sb, mirror(-3+2, height-1) * stride_line);
1206
    cs->y = -3;
1207
}
1208
1209 fe5c7e58 Michael Niedermayer
static void spatial_compose97i_init(DWTCompose *cs, IDWTELEM *buffer, int height, int stride){
1210 f9e6ebf7 Loren Merritt
    cs->b0 = buffer + mirror(-3-1, height-1)*stride;
1211
    cs->b1 = buffer + mirror(-3  , height-1)*stride;
1212
    cs->b2 = buffer + mirror(-3+1, height-1)*stride;
1213
    cs->b3 = buffer + mirror(-3+2, height-1)*stride;
1214
    cs->y = -3;
1215
}
1216 791e7b83 Michael Niedermayer
1217 fe5c7e58 Michael Niedermayer
static void spatial_compose97i_dy_buffered(DSPContext *dsp, DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line){
1218 a0d1931c Yartrebo
    int y = cs->y;
1219 115329f1 Diego Biurrun
1220 d593e329 Michael Niedermayer
    IDWTELEM *b0= cs->b0;
1221
    IDWTELEM *b1= cs->b1;
1222
    IDWTELEM *b2= cs->b2;
1223
    IDWTELEM *b3= cs->b3;
1224
    IDWTELEM *b4= slice_buffer_get_line(sb, mirror(y + 3, height - 1) * stride_line);
1225
    IDWTELEM *b5= slice_buffer_get_line(sb, mirror(y + 4, height - 1) * stride_line);
1226 115329f1 Diego Biurrun
1227 565a45ac Michael Niedermayer
    if(y>0 && y+4<height){
1228 059715a4 Robert Edele
        dsp->vertical_compose97i(b0, b1, b2, b3, b4, b5, width);
1229 565a45ac Michael Niedermayer
    }else{
1230 13705b69 Michael Niedermayer
        if(y+3<(unsigned)height) vertical_compose97iL1(b3, b4, b5, width);
1231
        if(y+2<(unsigned)height) vertical_compose97iH1(b2, b3, b4, width);
1232
        if(y+1<(unsigned)height) vertical_compose97iL0(b1, b2, b3, width);
1233
        if(y+0<(unsigned)height) vertical_compose97iH0(b0, b1, b2, width);
1234 565a45ac Michael Niedermayer
    }
1235 a0d1931c Yartrebo
1236 059715a4 Robert Edele
        if(y-1<(unsigned)height) dsp->horizontal_compose97i(b0, width);
1237
        if(y+0<(unsigned)height) dsp->horizontal_compose97i(b1, width);
1238 a0d1931c Yartrebo
1239
    cs->b0=b2;
1240
    cs->b1=b3;
1241
    cs->b2=b4;
1242
    cs->b3=b5;
1243
    cs->y += 2;
1244
}
1245
1246 fe5c7e58 Michael Niedermayer
static void spatial_compose97i_dy(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride){
1247 f9e6ebf7 Loren Merritt
    int y = cs->y;
1248 d593e329 Michael Niedermayer
    IDWTELEM *b0= cs->b0;
1249
    IDWTELEM *b1= cs->b1;
1250
    IDWTELEM *b2= cs->b2;
1251
    IDWTELEM *b3= cs->b3;
1252
    IDWTELEM *b4= buffer + mirror(y+3, height-1)*stride;
1253
    IDWTELEM *b5= buffer + mirror(y+4, height-1)*stride;
1254 791e7b83 Michael Niedermayer
1255 13705b69 Michael Niedermayer
        if(y+3<(unsigned)height) vertical_compose97iL1(b3, b4, b5, width);
1256
        if(y+2<(unsigned)height) vertical_compose97iH1(b2, b3, b4, width);
1257
        if(y+1<(unsigned)height) vertical_compose97iL0(b1, b2, b3, width);
1258
        if(y+0<(unsigned)height) vertical_compose97iH0(b0, b1, b2, width);
1259 791e7b83 Michael Niedermayer
1260 059715a4 Robert Edele
        if(y-1<(unsigned)height) ff_snow_horizontal_compose97i(b0, width);
1261
        if(y+0<(unsigned)height) ff_snow_horizontal_compose97i(b1, width);
1262 f9e6ebf7 Loren Merritt
1263
    cs->b0=b2;
1264
    cs->b1=b3;
1265
    cs->b2=b4;
1266
    cs->b3=b5;
1267
    cs->y += 2;
1268
}
1269
1270 1918057c Michael Niedermayer
static void av_unused spatial_compose97i(IDWTELEM *buffer, int width, int height, int stride){
1271 fe5c7e58 Michael Niedermayer
    DWTCompose cs;
1272 9a3bb2b8 Michael Niedermayer
    spatial_compose97i_init(&cs, buffer, height, stride);
1273
    while(cs.y <= height)
1274
        spatial_compose97i_dy(&cs, buffer, width, height, stride);
1275
}
1276
1277 fe5c7e58 Michael Niedermayer
static void ff_spatial_idwt_buffered_init(DWTCompose *cs, slice_buffer * sb, int width, int height, int stride_line, int type, int decomposition_count){
1278 a0d1931c Yartrebo
    int level;
1279
    for(level=decomposition_count-1; level>=0; level--){
1280
        switch(type){
1281 d4b287ed Loren Merritt
        case DWT_97: spatial_compose97i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
1282
        case DWT_53: spatial_compose53i_buffered_init(cs+level, sb, height>>level, stride_line<<level); break;
1283 a0d1931c Yartrebo
        }
1284
    }
1285
}
1286
1287 fe5c7e58 Michael Niedermayer
static void ff_spatial_idwt_init(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
1288 f9e6ebf7 Loren Merritt
    int level;
1289
    for(level=decomposition_count-1; level>=0; level--){
1290
        switch(type){
1291 d4b287ed Loren Merritt
        case DWT_97: spatial_compose97i_init(cs+level, buffer, height>>level, stride<<level); break;
1292
        case DWT_53: spatial_compose53i_init(cs+level, buffer, height>>level, stride<<level); break;
1293 f9e6ebf7 Loren Merritt
        }
1294 791e7b83 Michael Niedermayer
    }
1295
}
1296
1297 fe5c7e58 Michael Niedermayer
static void ff_spatial_idwt_slice(DWTCompose *cs, IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count, int y){
1298 f9e6ebf7 Loren Merritt
    const int support = type==1 ? 3 : 5;
1299 791e7b83 Michael Niedermayer
    int level;
1300 f9e6ebf7 Loren Merritt
    if(type==2) return;
1301 791e7b83 Michael Niedermayer
1302 46c281e8 Michael Niedermayer
    for(level=decomposition_count-1; level>=0; level--){
1303 f9e6ebf7 Loren Merritt
        while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
1304
            switch(type){
1305 d4b287ed Loren Merritt
            case DWT_97: spatial_compose97i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
1306 94ae6788 Diego Biurrun
                break;
1307 d4b287ed Loren Merritt
            case DWT_53: spatial_compose53i_dy(cs+level, buffer, width>>level, height>>level, stride<<level);
1308 94ae6788 Diego Biurrun
                break;
1309 f9e6ebf7 Loren Merritt
            }
1310 791e7b83 Michael Niedermayer
        }
1311
    }
1312
}
1313
1314 fe5c7e58 Michael Niedermayer
static void ff_spatial_idwt_buffered_slice(DSPContext *dsp, DWTCompose *cs, slice_buffer * slice_buf, int width, int height, int stride_line, int type, int decomposition_count, int y){
1315 a0d1931c Yartrebo
    const int support = type==1 ? 3 : 5;
1316
    int level;
1317
    if(type==2) return;
1318
1319
    for(level=decomposition_count-1; level>=0; level--){
1320
        while(cs[level].y <= FFMIN((y>>level)+support, height>>level)){
1321
            switch(type){
1322 d4b287ed Loren Merritt
            case DWT_97: spatial_compose97i_dy_buffered(dsp, cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
1323 94ae6788 Diego Biurrun
                break;
1324 d4b287ed Loren Merritt
            case DWT_53: spatial_compose53i_dy_buffered(cs+level, slice_buf, width>>level, height>>level, stride_line<<level);
1325 94ae6788 Diego Biurrun
                break;
1326 a0d1931c Yartrebo
            }
1327
        }
1328
    }
1329
}
1330
1331 d593e329 Michael Niedermayer
static void ff_spatial_idwt(IDWTELEM *buffer, int width, int height, int stride, int type, int decomposition_count){
1332 fe5c7e58 Michael Niedermayer
        DWTCompose cs[MAX_DECOMPOSITIONS];
1333 f9e6ebf7 Loren Merritt
        int y;
1334
        ff_spatial_idwt_init(cs, buffer, width, height, stride, type, decomposition_count);
1335
        for(y=0; y<height; y+=4)
1336
            ff_spatial_idwt_slice(cs, buffer, width, height, stride, type, decomposition_count, y);
1337
}
1338
1339 d593e329 Michael Niedermayer
static int encode_subband_c0run(SnowContext *s, SubBand *b, IDWTELEM *src, IDWTELEM *parent, int stride, int orientation){
1340 4f4e9633 Michael Niedermayer
    const int w= b->width;
1341
    const int h= b->height;
1342
    int x, y;
1343
1344 791e7b83 Michael Niedermayer
    if(1){
1345
        int run=0;
1346 a8d73e56 Michael Niedermayer
        int runs[w*h];
1347 791e7b83 Michael Niedermayer
        int run_index=0;
1348 b44985ba Michael Niedermayer
        int max_index;
1349 115329f1 Diego Biurrun
1350 791e7b83 Michael Niedermayer
        for(y=0; y<h; y++){
1351
            for(x=0; x<w; x++){
1352 78486403 Michael Niedermayer
                int v, p=0;
1353 6b2f6646 Michael Niedermayer
                int /*ll=0, */l=0, lt=0, t=0, rt=0;
1354 a8d73e56 Michael Niedermayer
                v= src[x + y*stride];
1355 791e7b83 Michael Niedermayer
1356
                if(y){
1357 a8d73e56 Michael Niedermayer
                    t= src[x + (y-1)*stride];
1358 791e7b83 Michael Niedermayer
                    if(x){
1359 a8d73e56 Michael Niedermayer
                        lt= src[x - 1 + (y-1)*stride];
1360 791e7b83 Michael Niedermayer
                    }
1361
                    if(x + 1 < w){
1362 a8d73e56 Michael Niedermayer
                        rt= src[x + 1 + (y-1)*stride];
1363 791e7b83 Michael Niedermayer
                    }
1364
                }
1365
                if(x){
1366 a8d73e56 Michael Niedermayer
                    l= src[x - 1 + y*stride];
1367 6b2f6646 Michael Niedermayer
                    /*if(x > 1){
1368
                        if(orientation==1) ll= src[y + (x-2)*stride];
1369
                        else               ll= src[x - 2 + y*stride];
1370 791e7b83 Michael Niedermayer
                    }*/
1371
                }
1372 78486403 Michael Niedermayer
                if(parent){
1373 a8d73e56 Michael Niedermayer
                    int px= x>>1;
1374
                    int py= y>>1;
1375 115329f1 Diego Biurrun
                    if(px<b->parent->width && py<b->parent->height)
1376 78486403 Michael Niedermayer
                        p= parent[px + py*2*stride];
1377
                }
1378
                if(!(/*ll|*/l|lt|t|rt|p)){
1379 791e7b83 Michael Niedermayer
                    if(v){
1380
                        runs[run_index++]= run;
1381
                        run=0;
1382
                    }else{
1383
                        run++;
1384
                    }
1385
                }
1386
            }
1387
        }
1388 b44985ba Michael Niedermayer
        max_index= run_index;
1389 791e7b83 Michael Niedermayer
        runs[run_index++]= run;
1390
        run_index=0;
1391
        run= runs[run_index++];
1392
1393 b44985ba Michael Niedermayer
        put_symbol2(&s->c, b->state[30], max_index, 0);
1394
        if(run_index <= max_index)
1395
            put_symbol2(&s->c, b->state[1], run, 3);
1396 115329f1 Diego Biurrun
1397 791e7b83 Michael Niedermayer
        for(y=0; y<h; y++){
1398 d06c75a8 Matthieu Castet
            if(s->c.bytestream_end - s->c.bytestream < w*40){
1399 0ecca7a4 Michael Niedermayer
                av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
1400
                return -1;
1401
            }
1402 791e7b83 Michael Niedermayer
            for(x=0; x<w; x++){
1403 78486403 Michael Niedermayer
                int v, p=0;
1404 6b2f6646 Michael Niedermayer
                int /*ll=0, */l=0, lt=0, t=0, rt=0;
1405 a8d73e56 Michael Niedermayer
                v= src[x + y*stride];
1406 791e7b83 Michael Niedermayer
1407
                if(y){
1408 a8d73e56 Michael Niedermayer
                    t= src[x + (y-1)*stride];
1409 791e7b83 Michael Niedermayer
                    if(x){
1410 a8d73e56 Michael Niedermayer
                        lt= src[x - 1 + (y-1)*stride];
1411 791e7b83 Michael Niedermayer
                    }
1412
                    if(x + 1 < w){
1413 a8d73e56 Michael Niedermayer
                        rt= src[x + 1 + (y-1)*stride];
1414 791e7b83 Michael Niedermayer
                    }
1415
                }
1416
                if(x){
1417 a8d73e56 Michael Niedermayer
                    l= src[x - 1 + y*stride];
1418 6b2f6646 Michael Niedermayer
                    /*if(x > 1){
1419
                        if(orientation==1) ll= src[y + (x-2)*stride];
1420
                        else               ll= src[x - 2 + y*stride];
1421 791e7b83 Michael Niedermayer
                    }*/
1422
                }
1423 78486403 Michael Niedermayer
                if(parent){
1424 a8d73e56 Michael Niedermayer
                    int px= x>>1;
1425
                    int py= y>>1;
1426 115329f1 Diego Biurrun
                    if(px<b->parent->width && py<b->parent->height)
1427 78486403 Michael Niedermayer
                        p= parent[px + py*2*stride];
1428
                }
1429
                if(/*ll|*/l|lt|t|rt|p){
1430 c26abfa5 Diego Biurrun
                    int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
1431 6b2f6646 Michael Niedermayer
1432 28869757 Michael Niedermayer
                    put_rac(&s->c, &b->state[0][context], !!v);
1433 791e7b83 Michael Niedermayer
                }else{
1434
                    if(!run){
1435
                        run= runs[run_index++];
1436 4f4e9633 Michael Niedermayer
1437 b44985ba Michael Niedermayer
                        if(run_index <= max_index)
1438
                            put_symbol2(&s->c, b->state[1], run, 3);
1439 791e7b83 Michael Niedermayer
                        assert(v);
1440
                    }else{
1441
                        run--;
1442
                        assert(!v);
1443
                    }
1444
                }
1445
                if(v){
1446 c26abfa5 Diego Biurrun
                    int context= av_log2(/*FFABS(ll) + */3*FFABS(l) + FFABS(lt) + 2*FFABS(t) + FFABS(rt) + FFABS(p));
1447
                    int l2= 2*FFABS(l) + (l<0);
1448
                    int t2= 2*FFABS(t) + (t<0);
1449 6b2f6646 Michael Niedermayer
1450 c26abfa5 Diego Biurrun
                    put_symbol2(&s->c, b->state[context + 2], FFABS(v)-1, context-4);
1451 538a3841 Michael Niedermayer
                    put_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3bA[l2&0xFF] + 3*quant3bA[t2&0xFF]], v<0);
1452 791e7b83 Michael Niedermayer
                }
1453
            }
1454
        }
1455
    }
1456 0ecca7a4 Michael Niedermayer
    return 0;
1457 791e7b83 Michael Niedermayer
}
1458
1459 d593e329 Michael Niedermayer
static int encode_subband(SnowContext *s, SubBand *b, IDWTELEM *src, IDWTELEM *parent, int stride, int orientation){
1460 4f4e9633 Michael Niedermayer
//    encode_subband_qtree(s, b, src, parent, stride, orientation);
1461
//    encode_subband_z0run(s, b, src, parent, stride, orientation);
1462 0ecca7a4 Michael Niedermayer
    return encode_subband_c0run(s, b, src, parent, stride, orientation);
1463 4f4e9633 Michael Niedermayer
//    encode_subband_dzr(s, b, src, parent, stride, orientation);
1464
}
1465
1466 a0d1931c Yartrebo
static inline void unpack_coeffs(SnowContext *s, SubBand *b, SubBand * parent, int orientation){
1467 791e7b83 Michael Niedermayer
    const int w= b->width;
1468
    const int h= b->height;
1469
    int x,y;
1470 115329f1 Diego Biurrun
1471 791e7b83 Michael Niedermayer
    if(1){
1472 b44985ba Michael Niedermayer
        int run, runs;
1473 cbb1d2b1 Michael Niedermayer
        x_and_coeff *xc= b->x_coeff;
1474
        x_and_coeff *prev_xc= NULL;
1475
        x_and_coeff *prev2_xc= xc;
1476
        x_and_coeff *parent_xc= parent ? parent->x_coeff : NULL;
1477
        x_and_coeff *prev_parent_xc= parent_xc;
1478 791e7b83 Michael Niedermayer
1479 b44985ba Michael Niedermayer
        runs= get_symbol2(&s->c, b->state[30], 0);
1480
        if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
1481
        else           run= INT_MAX;
1482
1483 791e7b83 Michael Niedermayer
        for(y=0; y<h; y++){
1484 0cea8a03 Michael Niedermayer
            int v=0;
1485
            int lt=0, t=0, rt=0;
1486
1487 cbb1d2b1 Michael Niedermayer
            if(y && prev_xc->x == 0){
1488
                rt= prev_xc->coeff;
1489 0cea8a03 Michael Niedermayer
            }
1490 791e7b83 Michael Niedermayer
            for(x=0; x<w; x++){
1491 0cea8a03 Michael Niedermayer
                int p=0;
1492
                const int l= v;
1493 115329f1 Diego Biurrun
1494 0cea8a03 Michael Niedermayer
                lt= t; t= rt;
1495 791e7b83 Michael Niedermayer
1496 ff765159 Michael Niedermayer
                if(y){
1497 cbb1d2b1 Michael Niedermayer
                    if(prev_xc->x <= x)
1498
                        prev_xc++;
1499
                    if(prev_xc->x == x + 1)
1500
                        rt= prev_xc->coeff;
1501 ff765159 Michael Niedermayer
                    else
1502
                        rt=0;
1503
                }
1504 cbb1d2b1 Michael Niedermayer
                if(parent_xc){
1505
                    if(x>>1 > parent_xc->x){
1506
                        parent_xc++;
1507 7b49c309 Michael Niedermayer
                    }
1508 cbb1d2b1 Michael Niedermayer
                    if(x>>1 == parent_xc->x){
1509
                        p= parent_xc->coeff;
1510 ff765159 Michael Niedermayer
                    }
1511 78486403 Michael Niedermayer
                }
1512
                if(/*ll|*/l|lt|t|rt|p){
1513 c26abfa5 Diego Biurrun
                    int context= av_log2(/*FFABS(ll) + */3*(l>>1) + (lt>>1) + (t&~1) + (rt>>1) + (p>>1));
1514 6b2f6646 Michael Niedermayer
1515 28869757 Michael Niedermayer
                    v=get_rac(&s->c, &b->state[0][context]);
1516 3c096ac7 Michael Niedermayer
                    if(v){
1517
                        v= 2*(get_symbol2(&s->c, b->state[context + 2], context-4) + 1);
1518
                        v+=get_rac(&s->c, &b->state[0][16 + 1 + 3 + quant3bA[l&0xFF] + 3*quant3bA[t&0xFF]]);
1519 115329f1 Diego Biurrun
1520 cbb1d2b1 Michael Niedermayer
                        xc->x=x;
1521
                        (xc++)->coeff= v;
1522 3c096ac7 Michael Niedermayer
                    }
1523 791e7b83 Michael Niedermayer
                }else{
1524
                    if(!run){
1525 b44985ba Michael Niedermayer
                        if(runs-- > 0) run= get_symbol2(&s->c, b->state[1], 3);
1526
                        else           run= INT_MAX;
1527 3c096ac7 Michael Niedermayer
                        v= 2*(get_symbol2(&s->c, b->state[0 + 2], 0-4) + 1);
1528
                        v+=get_rac(&s->c, &b->state[0][16 + 1 + 3]);
1529 115329f1 Diego Biurrun
1530 cbb1d2b1 Michael Niedermayer
                        xc->x=x;
1531
                        (xc++)->coeff= v;
1532 791e7b83 Michael Niedermayer
                    }else{
1533 99cd59e5 Michael Niedermayer
                        int max_run;
1534 791e7b83 Michael Niedermayer
                        run--;
1535
                        v=0;
1536 3c1adccd Michael Niedermayer
1537 cbb1d2b1 Michael Niedermayer
                        if(y) max_run= FFMIN(run, prev_xc->x - x - 2);
1538 99cd59e5 Michael Niedermayer
                        else  max_run= FFMIN(run, w-x-1);
1539 cbb1d2b1 Michael Niedermayer
                        if(parent_xc)
1540
                            max_run= FFMIN(max_run, 2*parent_xc->x - x - 1);
1541 99cd59e5 Michael Niedermayer
                        x+= max_run;
1542
                        run-= max_run;
1543 791e7b83 Michael Niedermayer
                    }
1544
                }
1545 7b49c309 Michael Niedermayer
            }
1546 cbb1d2b1 Michael Niedermayer
            (xc++)->x= w+1; //end marker
1547
            prev_xc= prev2_xc;
1548
            prev2_xc= xc;
1549 115329f1 Diego Biurrun
1550 cbb1d2b1 Michael Niedermayer
            if(parent_xc){
1551 7b49c309 Michael Niedermayer
                if(y&1){
1552 cbb1d2b1 Michael Niedermayer
                    while(parent_xc->x != parent->width+1)
1553
                        parent_xc++;
1554
                    parent_xc++;
1555
                    prev_parent_xc= parent_xc;
1556 7b49c309 Michael Niedermayer
                }else{
1557 cbb1d2b1 Michael Niedermayer
                    parent_xc= prev_parent_xc;
1558 791e7b83 Michael Niedermayer
                }
1559
            }
1560
        }
1561 a0d1931c Yartrebo
1562 cbb1d2b1 Michael Niedermayer
        (xc++)->x= w+1; //end marker
1563 a0d1931c Yartrebo
    }
1564
}
1565
1566
static inline void decode_subband_slice_buffered(SnowContext *s, SubBand *b, slice_buffer * sb, int start_y, int h, int save_state[1]){
1567
    const int w= b->width;
1568 62ab0b78 Aurelien Jacobs
    int y;
1569 f66e4f5f Reimar Döffinger
    const int qlog= av_clip(s->qlog + b->qlog, 0, QROOT*16);
1570 c97de57c Michael Niedermayer
    int qmul= qexp[qlog&(QROOT-1)]<<(qlog>>QSHIFT);
1571 a0d1931c Yartrebo
    int qadd= (s->qbias*qmul)>>QBIAS_SHIFT;
1572
    int new_index = 0;
1573 115329f1 Diego Biurrun
1574 d593e329 Michael Niedermayer
    if(b->ibuf == s->spatial_idwt_buffer || s->qlog == LOSSLESS_QLOG){
1575 a0d1931c Yartrebo
        qadd= 0;
1576
        qmul= 1<<QEXPSHIFT;
1577
    }
1578
1579
    /* If we are on the second or later slice, restore our index. */
1580
    if (start_y != 0)
1581
        new_index = save_state[0];
1582
1583 115329f1 Diego Biurrun
1584 a0d1931c Yartrebo
    for(y=start_y; y<h; y++){
1585
        int x = 0;
1586
        int v;
1587 d593e329 Michael Niedermayer
        IDWTELEM * line = slice_buffer_get_line(sb, y * b->stride_line + b->buf_y_offset) + b->buf_x_offset;
1588
        memset(line, 0, b->width*sizeof(IDWTELEM));
1589 a0d1931c Yartrebo
        v = b->x_coeff[new_index].coeff;
1590
        x = b->x_coeff[new_index++].x;
1591 ef3dfbd4 Diego Biurrun
        while(x < w){
1592 538a3841 Michael Niedermayer
            register int t= ( (v>>1)*qmul + qadd)>>QEXPSHIFT;
1593
            register int u= -(v&1);
1594
            line[x] = (t^u) - u;
1595
1596 a0d1931c Yartrebo
            v = b->x_coeff[new_index].coeff;
1597
            x = b->x_coeff[new_index++].x;
1598
        }
1599 791e7b83 Michael Niedermayer
    }
1600 115329f1 Diego Biurrun
1601 a0d1931c Yartrebo
    /* Save our variables for the next slice. */
1602
    save_state[0] = new_index;
1603 115329f1 Diego Biurrun
1604 a0d1931c Yartrebo
    return;
1605 791e7b83 Michael Niedermayer
}
1606
1607 396a5e68 Michael Niedermayer
static void reset_contexts(SnowContext *s){ //FIXME better initial contexts
1608 791e7b83 Michael Niedermayer
    int plane_index, level, orientation;
1609
1610 19aa028d Michael Niedermayer
    for(plane_index=0; plane_index<3; plane_index++){
1611 4f90f33a Michael Niedermayer
        for(level=0; level<MAX_DECOMPOSITIONS; level++){
1612 791e7b83 Michael Niedermayer
            for(orientation=level ? 1:0; orientation<4; orientation++){
1613 28869757 Michael Niedermayer
                memset(s->plane[plane_index].band[level][orientation].state, MID_STATE, sizeof(s->plane[plane_index].band[level][orientation].state));
1614 791e7b83 Michael Niedermayer
            }
1615
        }
1616
    }
1617 28869757 Michael Niedermayer
    memset(s->header_state, MID_STATE, sizeof(s->header_state));
1618
    memset(s->block_state, MID_STATE, sizeof(s->block_state));
1619 155ec6ed Michael Niedermayer
}
1620
1621
static int alloc_blocks(SnowContext *s){
1622
    int w= -((-s->avctx->width )>>LOG2_MB_SIZE);
1623
    int h= -((-s->avctx->height)>>LOG2_MB_SIZE);
1624 115329f1 Diego Biurrun
1625 155ec6ed Michael Niedermayer
    s->b_width = w;
1626
    s->b_height= h;
1627 115329f1 Diego Biurrun
1628 155ec6ed Michael Niedermayer
    s->block= av_mallocz(w * h * sizeof(BlockNode) << (s->block_max_depth*2));
1629
    return 0;
1630
}
1631
1632 28869757 Michael Niedermayer
static inline void copy_rac_state(RangeCoder *d, RangeCoder *s){
1633
    uint8_t *bytestream= d->bytestream;
1634
    uint8_t *bytestream_start= d->bytestream_start;
1635 155ec6ed Michael Niedermayer
    *d= *s;
1636 28869757 Michael Niedermayer
    d->bytestream= bytestream;
1637
    d->bytestream_start= bytestream_start;
1638 155ec6ed Michael Niedermayer
}
1639
1640
//near copy & paste from dsputil, FIXME
1641
static int pix_sum(uint8_t * pix, int line_size, int w)
1642
{
1643
    int s, i, j;
1644
1645
    s = 0;
1646
    for (i = 0; i < w; i++) {
1647
        for (j = 0; j < w; j++) {
1648
            s += pix[0];
1649
            pix ++;
1650
        }
1651
        pix += line_size - w;
1652
    }
1653
    return s;
1654
}
1655
1656
//near copy & paste from dsputil, FIXME
1657
static int pix_norm1(uint8_t * pix, int line_size, int w)
1658
{
1659
    int s, i, j;
1660 1d503957 Måns Rullgård
    uint32_t *sq = ff_squareTbl + 256;
1661 155ec6ed Michael Niedermayer
1662
    s = 0;
1663
    for (i = 0; i < w; i++) {
1664
        for (j = 0; j < w; j ++) {
1665
            s += sq[pix[0]];
1666
            pix ++;
1667
        }
1668
        pix += line_size - w;
1669
    }
1670
    return s;
1671
}
1672
1673 8c36eaaa Loren Merritt
static inline void set_blocks(SnowContext *s, int level, int x, int y, int l, int cb, int cr, int mx, int my, int ref, int type){
1674 155ec6ed Michael Niedermayer
    const int w= s->b_width << s->block_max_depth;
1675
    const int rem_depth= s->block_max_depth - level;
1676
    const int index= (x + y*w) << rem_depth;
1677
    const int block_w= 1<<rem_depth;
1678
    BlockNode block;
1679
    int i,j;
1680 115329f1 Diego Biurrun
1681 155ec6ed Michael Niedermayer
    block.color[0]= l;
1682
    block.color[1]= cb;
1683
    block.color[2]= cr;
1684
    block.mx= mx;
1685
    block.my= my;
1686 8c36eaaa Loren Merritt
    block.ref= ref;
1687 155ec6ed Michael Niedermayer
    block.type= type;
1688
    block.level= level;
1689
1690
    for(j=0; j<block_w; j++){
1691
        for(i=0; i<block_w; i++){
1692
            s->block[index + i + j*w]= block;
1693
        }
1694
    }
1695
}
1696
1697
static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){
1698
    const int offset[3]= {
1699
          y*c->  stride + x,
1700
        ((y*c->uvstride + x)>>1),
1701
        ((y*c->uvstride + x)>>1),
1702
    };
1703
    int i;
1704
    for(i=0; i<3; i++){
1705
        c->src[0][i]= src [i];
1706
        c->ref[0][i]= ref [i] + offset[i];
1707
    }
1708
    assert(!ref_index);
1709
}
1710
1711 85fc0e75 Loren Merritt
static inline void pred_mv(SnowContext *s, int *mx, int *my, int ref,
1712 aadcc5ce Panagiotis Issaris
                           const BlockNode *left, const BlockNode *top, const BlockNode *tr){
1713 85fc0e75 Loren Merritt
    if(s->ref_frames == 1){
1714
        *mx = mid_pred(left->mx, top->mx, tr->mx);
1715
        *my = mid_pred(left->my, top->my, tr->my);
1716
    }else{
1717
        const int *scale = scale_mv_ref[ref];
1718 6884c36c Panagiotis Issaris
        *mx = mid_pred((left->mx * scale[left->ref] + 128) >>8,
1719
                       (top ->mx * scale[top ->ref] + 128) >>8,
1720
                       (tr  ->mx * scale[tr  ->ref] + 128) >>8);
1721
        *my = mid_pred((left->my * scale[left->ref] + 128) >>8,
1722
                       (top ->my * scale[top ->ref] + 128) >>8,
1723
                       (tr  ->my * scale[tr  ->ref] + 128) >>8);
1724 85fc0e75 Loren Merritt
    }
1725
}
1726
1727 155ec6ed Michael Niedermayer
//FIXME copy&paste
1728
#define P_LEFT P[1]
1729
#define P_TOP P[2]
1730
#define P_TOPRIGHT P[3]
1731
#define P_MEDIAN P[4]
1732
#define P_MV1 P[9]
1733
#define FLAG_QPEL   1 //must be 1
1734
1735
static int encode_q_branch(SnowContext *s, int level, int x, int y){
1736
    uint8_t p_buffer[1024];
1737
    uint8_t i_buffer[1024];
1738
    uint8_t p_state[sizeof(s->block_state)];
1739
    uint8_t i_state[sizeof(s->block_state)];
1740 28869757 Michael Niedermayer
    RangeCoder pc, ic;
1741
    uint8_t *pbbak= s->c.bytestream;
1742
    uint8_t *pbbak_start= s->c.bytestream_start;
1743 1e6b5700 Michael Niedermayer
    int score, score2, iscore, i_len, p_len, block_s, sum, base_bits;
1744 155ec6ed Michael Niedermayer
    const int w= s->b_width  << s->block_max_depth;
1745
    const int h= s->b_height << s->block_max_depth;
1746
    const int rem_depth= s->block_max_depth - level;
1747
    const int index= (x + y*w) << rem_depth;
1748
    const int block_w= 1<<(LOG2_MB_SIZE - level);
1749
    int trx= (x+1)<<rem_depth;
1750
    int try= (y+1)<<rem_depth;
1751 aadcc5ce Panagiotis Issaris
    const BlockNode *left  = x ? &s->block[index-1] : &null_block;
1752
    const BlockNode *top   = y ? &s->block[index-w] : &null_block;
1753
    const BlockNode *right = trx<w ? &s->block[index+1] : &null_block;
1754
    const BlockNode *bottom= try<h ? &s->block[index+w] : &null_block;
1755
    const BlockNode *tl    = y && x ? &s->block[index-w-1] : left;
1756
    const BlockNode *tr    = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
1757 155ec6ed Michael Niedermayer
    int pl = left->color[0];
1758
    int pcb= left->color[1];
1759
    int pcr= left->color[2];
1760 85fc0e75 Loren Merritt
    int pmx, pmy;
1761 155ec6ed Michael Niedermayer
    int mx=0, my=0;
1762 51d6a3cf Michael Niedermayer
    int l,cr,cb;
1763 155ec6ed Michael Niedermayer
    const int stride= s->current_picture.linesize[0];
1764
    const int uvstride= s->current_picture.linesize[1];
1765 51d6a3cf Michael Niedermayer
    uint8_t *current_data[3]= { s->input_picture.data[0] + (x + y*  stride)*block_w,
1766
                                s->input_picture.data[1] + (x + y*uvstride)*block_w/2,
1767
                                s->input_picture.data[2] + (x + y*uvstride)*block_w/2};
1768 155ec6ed Michael Niedermayer
    int P[10][2];
1769
    int16_t last_mv[3][2];
1770
    int qpel= !!(s->avctx->flags & CODEC_FLAG_QPEL); //unused
1771
    const int shift= 1+qpel;
1772
    MotionEstContext *c= &s->m.me;
1773 8c36eaaa Loren Merritt
    int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
1774 c26abfa5 Diego Biurrun
    int mx_context= av_log2(2*FFABS(left->mx - top->mx));
1775
    int my_context= av_log2(2*FFABS(left->my - top->my));
1776 155ec6ed Michael Niedermayer
    int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
1777 8c36eaaa Loren Merritt
    int ref, best_ref, ref_score, ref_mx, ref_my;
1778 155ec6ed Michael Niedermayer
1779
    assert(sizeof(s->block_state) >= 256);
1780
    if(s->keyframe){
1781 85fc0e75 Loren Merritt
        set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
1782 155ec6ed Michael Niedermayer
        return 0;
1783
    }
1784
1785
//    clip predictors / edge ?
1786
1787
    P_LEFT[0]= left->mx;
1788
    P_LEFT[1]= left->my;
1789
    P_TOP [0]= top->mx;
1790
    P_TOP [1]= top->my;
1791
    P_TOPRIGHT[0]= tr->mx;
1792
    P_TOPRIGHT[1]= tr->my;
1793 115329f1 Diego Biurrun
1794 155ec6ed Michael Niedermayer
    last_mv[0][0]= s->block[index].mx;
1795
    last_mv[0][1]= s->block[index].my;
1796
    last_mv[1][0]= right->mx;
1797
    last_mv[1][1]= right->my;
1798
    last_mv[2][0]= bottom->mx;
1799
    last_mv[2][1]= bottom->my;
1800 115329f1 Diego Biurrun
1801 155ec6ed Michael Niedermayer
    s->m.mb_stride=2;
1802 115329f1 Diego Biurrun
    s->m.mb_x=
1803 155ec6ed Michael Niedermayer
    s->m.mb_y= 0;
1804 e2158da8 Panagiotis Issaris
    c->skip= 0;
1805 155ec6ed Michael Niedermayer
1806 e2158da8 Panagiotis Issaris
    assert(c->  stride ==   stride);
1807
    assert(c->uvstride == uvstride);
1808 115329f1 Diego Biurrun
1809 155ec6ed Michael Niedermayer
    c->penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
1810
    c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
1811
    c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
1812
    c->current_mv_penalty= c->mv_penalty[s->m.f_code=1] + MAX_MV;
1813 115329f1 Diego Biurrun
1814 ff158dc9 Michael Niedermayer
    c->xmin = - x*block_w - 16+2;
1815
    c->ymin = - y*block_w - 16+2;
1816
    c->xmax = - (x+1)*block_w + (w<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-2;
1817
    c->ymax = - (y+1)*block_w + (h<<(LOG2_MB_SIZE - s->block_max_depth)) + 16-2;
1818 155ec6ed Michael Niedermayer
1819
    if(P_LEFT[0]     > (c->xmax<<shift)) P_LEFT[0]    = (c->xmax<<shift);
1820 115329f1 Diego Biurrun
    if(P_LEFT[1]     > (c->ymax<<shift)) P_LEFT[1]    = (c->ymax<<shift);
1821 155ec6ed Michael Niedermayer
    if(P_TOP[0]      > (c->xmax<<shift)) P_TOP[0]     = (c->xmax<<shift);
1822
    if(P_TOP[1]      > (c->ymax<<shift)) P_TOP[1]     = (c->ymax<<shift);
1823
    if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
1824
    if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); //due to pmx no clip
1825
    if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
1826
1827
    P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1828
    P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1829
1830
    if (!y) {
1831
        c->pred_x= P_LEFT[0];
1832
        c->pred_y= P_LEFT[1];
1833
    } else {
1834
        c->pred_x = P_MEDIAN[0];
1835
        c->pred_y = P_MEDIAN[1];
1836
    }
1837
1838 8c36eaaa Loren Merritt
    score= INT_MAX;
1839
    best_ref= 0;
1840
    for(ref=0; ref<s->ref_frames; ref++){
1841
        init_ref(c, current_data, s->last_picture[ref].data, NULL, block_w*x, block_w*y, 0);
1842
1843
        ref_score= ff_epzs_motion_search(&s->m, &ref_mx, &ref_my, P, 0, /*ref_index*/ 0, last_mv,
1844
                                         (1<<16)>>shift, level-LOG2_MB_SIZE+4, block_w);
1845 155ec6ed Michael Niedermayer
1846 8c36eaaa Loren Merritt
        assert(ref_mx >= c->xmin);
1847
        assert(ref_mx <= c->xmax);
1848
        assert(ref_my >= c->ymin);
1849
        assert(ref_my <= c->ymax);
1850 115329f1 Diego Biurrun
1851 e2158da8 Panagiotis Issaris
        ref_score= c->sub_motion_search(&s->m, &ref_mx, &ref_my, ref_score, 0, 0, level-LOG2_MB_SIZE+4, block_w);
1852 8c36eaaa Loren Merritt
        ref_score= ff_get_mb_score(&s->m, ref_mx, ref_my, 0, 0, level-LOG2_MB_SIZE+4, block_w, 0);
1853
        ref_score+= 2*av_log2(2*ref)*c->penalty_factor;
1854
        if(s->ref_mvs[ref]){
1855
            s->ref_mvs[ref][index][0]= ref_mx;
1856
            s->ref_mvs[ref][index][1]= ref_my;
1857
            s->ref_scores[ref][index]= ref_score;
1858
        }
1859
        if(score > ref_score){
1860
            score= ref_score;
1861
            best_ref= ref;
1862
            mx= ref_mx;
1863
            my= ref_my;
1864
        }
1865
    }
1866 755bfeab Diego Biurrun
    //FIXME if mb_cmp != SSE then intra cannot be compared currently and mb_penalty vs. lambda2
1867 115329f1 Diego Biurrun
1868 155ec6ed Michael Niedermayer
  //  subpel search
1869 61d49d12 Michael Niedermayer
    base_bits= get_rac_count(&s->c) - 8*(s->c.bytestream - s->c.bytestream_start);
1870 155ec6ed Michael Niedermayer
    pc= s->c;
1871 28869757 Michael Niedermayer
    pc.bytestream_start=
1872
    pc.bytestream= p_buffer; //FIXME end/start? and at the other stoo
1873 155ec6ed Michael Niedermayer
    memcpy(p_state, s->block_state, sizeof(s->block_state));
1874
1875
    if(level!=s->block_max_depth)
1876 28869757 Michael Niedermayer
        put_rac(&pc, &p_state[4 + s_context], 1);
1877
    put_rac(&pc, &p_state[1 + left->type + top->type], 0);
1878 8c36eaaa Loren Merritt
    if(s->ref_frames > 1)
1879
        put_symbol(&pc, &p_state[128 + 1024 + 32*ref_context], best_ref, 0);
1880 85fc0e75 Loren Merritt
    pred_mv(s, &pmx, &pmy, best_ref, left, top, tr);
1881 8c36eaaa Loren Merritt
    put_symbol(&pc, &p_state[128 + 32*(mx_context + 16*!!best_ref)], mx - pmx, 1);
1882
    put_symbol(&pc, &p_state[128 + 32*(my_context + 16*!!best_ref)], my - pmy, 1);
1883 28869757 Michael Niedermayer
    p_len= pc.bytestream - pc.bytestream_start;
1884 1e6b5700 Michael Niedermayer
    score += (s->lambda2*(get_rac_count(&pc)-base_bits))>>FF_LAMBDA_SHIFT;
1885 155ec6ed Michael Niedermayer
1886
    block_s= block_w*block_w;
1887 51d6a3cf Michael Niedermayer
    sum = pix_sum(current_data[0], stride, block_w);
1888 155ec6ed Michael Niedermayer
    l= (sum + block_s/2)/block_s;
1889 51d6a3cf Michael Niedermayer
    iscore = pix_norm1(current_data[0], stride, block_w) - 2*l*sum + l*l*block_s;
1890 115329f1 Diego Biurrun
1891 155ec6ed Michael Niedermayer
    block_s= block_w*block_w>>2;
1892 51d6a3cf Michael Niedermayer
    sum = pix_sum(current_data[1], uvstride, block_w>>1);
1893 155ec6ed Michael Niedermayer
    cb= (sum + block_s/2)/block_s;
1894
//    iscore += pix_norm1(&current_mb[1][0], uvstride, block_w>>1) - 2*cb*sum + cb*cb*block_s;
1895 51d6a3cf Michael Niedermayer
    sum = pix_sum(current_data[2], uvstride, block_w>>1);
1896 155ec6ed Michael Niedermayer
    cr= (sum + block_s/2)/block_s;
1897
//    iscore += pix_norm1(&current_mb[2][0], uvstride, block_w>>1) - 2*cr*sum + cr*cr*block_s;
1898
1899
    ic= s->c;
1900 28869757 Michael Niedermayer
    ic.bytestream_start=
1901
    ic.bytestream= i_buffer; //FIXME end/start? and at the other stoo
1902 155ec6ed Michael Niedermayer
    memcpy(i_state, s->block_state, sizeof(s->block_state));
1903
    if(level!=s->block_max_depth)
1904 28869757 Michael Niedermayer
        put_rac(&ic, &i_state[4 + s_context], 1);
1905
    put_rac(&ic, &i_state[1 + left->type + top->type], 1);
1906 155ec6ed Michael Niedermayer
    put_symbol(&ic, &i_state[32],  l-pl , 1);
1907
    put_symbol(&ic, &i_state[64], cb-pcb, 1);
1908
    put_symbol(&ic, &i_state[96], cr-pcr, 1);
1909 28869757 Michael Niedermayer
    i_len= ic.bytestream - ic.bytestream_start;
1910 1e6b5700 Michael Niedermayer
    iscore += (s->lambda2*(get_rac_count(&ic)-base_bits))>>FF_LAMBDA_SHIFT;
1911 155ec6ed Michael Niedermayer
1912
//    assert(score==256*256*256*64-1);
1913
    assert(iscore < 255*255*256 + s->lambda2*10);
1914
    assert(iscore >= 0);
1915
    assert(l>=0 && l<=255);
1916
    assert(pl>=0 && pl<=255);
1917
1918
    if(level==0){
1919
        int varc= iscore >> 8;
1920
        int vard= score >> 8;
1921
        if (vard <= 64 || vard < varc)
1922
            c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
1923
        else
1924
            c->scene_change_score+= s->m.qscale;
1925
    }
1926 115329f1 Diego Biurrun
1927 155ec6ed Michael Niedermayer
    if(level!=s->block_max_depth){
1928 28869757 Michael Niedermayer
        put_rac(&s->c, &s->block_state[4 + s_context], 0);
1929 155ec6ed Michael Niedermayer
        score2 = encode_q_branch(s, level+1, 2*x+0, 2*y+0);
1930
        score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+0);
1931
        score2+= encode_q_branch(s, level+1, 2*x+0, 2*y+1);
1932
        score2+= encode_q_branch(s, level+1, 2*x+1, 2*y+1);
1933
        score2+= s->lambda2>>FF_LAMBDA_SHIFT; //FIXME exact split overhead
1934 115329f1 Diego Biurrun
1935 155ec6ed Michael Niedermayer
        if(score2 < score && score2 < iscore)
1936
            return score2;
1937
    }
1938 115329f1 Diego Biurrun
1939 155ec6ed Michael Niedermayer
    if(iscore < score){
1940 85fc0e75 Loren Merritt
        pred_mv(s, &pmx, &pmy, 0, left, top, tr);
1941 28869757 Michael Niedermayer
        memcpy(pbbak, i_buffer, i_len);
1942 155ec6ed Michael Niedermayer
        s->c= ic;
1943 28869757 Michael Niedermayer
        s->c.bytestream_start= pbbak_start;
1944
        s->c.bytestream= pbbak + i_len;
1945 8c36eaaa Loren Merritt
        set_blocks(s, level, x, y, l, cb, cr, pmx, pmy, 0, BLOCK_INTRA);
1946 155ec6ed Michael Niedermayer
        memcpy(s->block_state, i_state, sizeof(s->block_state));
1947
        return iscore;
1948
    }else{
1949 28869757 Michael Niedermayer
        memcpy(pbbak, p_buffer, p_len);
1950 155ec6ed Michael Niedermayer
        s->c= pc;
1951 28869757 Michael Niedermayer
        s->c.bytestream_start= pbbak_start;
1952
        s->c.bytestream= pbbak + p_len;
1953 8c36eaaa Loren Merritt
        set_blocks(s, level, x, y, pl, pcb, pcr, mx, my, best_ref, 0);
1954 155ec6ed Michael Niedermayer
        memcpy(s->block_state, p_state, sizeof(s->block_state));
1955
        return score;
1956
    }
1957
}
1958
1959 849f1035 Måns Rullgård
static av_always_inline int same_block(BlockNode *a, BlockNode *b){
1960 51d6a3cf Michael Niedermayer
    if((a->type&BLOCK_INTRA) && (b->type&BLOCK_INTRA)){
1961
        return !((a->color[0] - b->color[0]) | (a->color[1] - b->color[1]) | (a->color[2] - b->color[2]));
1962
    }else{
1963 8c36eaaa Loren Merritt
        return !((a->mx - b->mx) | (a->my - b->my) | (a->ref - b->ref) | ((a->type ^ b->type)&BLOCK_INTRA));
1964 51d6a3cf Michael Niedermayer
    }
1965
}
1966
1967
static void encode_q_branch2(SnowContext *s, int level, int x, int y){
1968
    const int w= s->b_width  << s->block_max_depth;
1969
    const int rem_depth= s->block_max_depth - level;
1970
    const int index= (x + y*w) << rem_depth;
1971
    int trx= (x+1)<<rem_depth;
1972
    BlockNode *b= &s->block[index];
1973 aadcc5ce Panagiotis Issaris
    const BlockNode *left  = x ? &s->block[index-1] : &null_block;
1974
    const BlockNode *top   = y ? &s->block[index-w] : &null_block;
1975
    const BlockNode *tl    = y && x ? &s->block[index-w-1] : left;
1976
    const BlockNode *tr    = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
1977 51d6a3cf Michael Niedermayer
    int pl = left->color[0];
1978
    int pcb= left->color[1];
1979
    int pcr= left->color[2];
1980 85fc0e75 Loren Merritt
    int pmx, pmy;
1981 8c36eaaa Loren Merritt
    int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
1982 c26abfa5 Diego Biurrun
    int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 16*!!b->ref;
1983
    int my_context= av_log2(2*FFABS(left->my - top->my)) + 16*!!b->ref;
1984 51d6a3cf Michael Niedermayer
    int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
1985
1986
    if(s->keyframe){
1987 85fc0e75 Loren Merritt
        set_blocks(s, level, x, y, pl, pcb, pcr, 0, 0, 0, BLOCK_INTRA);
1988 51d6a3cf Michael Niedermayer
        return;
1989
    }
1990
1991
    if(level!=s->block_max_depth){
1992
        if(same_block(b,b+1) && same_block(b,b+w) && same_block(b,b+w+1)){
1993 d6f41eed Michael Niedermayer
            put_rac(&s->c, &s->block_state[4 + s_context], 1);
1994
        }else{
1995 51d6a3cf Michael Niedermayer
            put_rac(&s->c, &s->block_state[4 + s_context], 0);
1996
            encode_q_branch2(s, level+1, 2*x+0, 2*y+0);
1997
            encode_q_branch2(s, level+1, 2*x+1, 2*y+0);
1998
            encode_q_branch2(s, level+1, 2*x+0, 2*y+1);
1999
            encode_q_branch2(s, level+1, 2*x+1, 2*y+1);
2000
            return;
2001
        }
2002
    }
2003
    if(b->type & BLOCK_INTRA){
2004 85fc0e75 Loren Merritt
        pred_mv(s, &pmx, &pmy, 0, left, top, tr);
2005 51d6a3cf Michael Niedermayer
        put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 1);
2006
        put_symbol(&s->c, &s->block_state[32], b->color[0]-pl , 1);
2007
        put_symbol(&s->c, &s->block_state[64], b->color[1]-pcb, 1);
2008
        put_symbol(&s->c, &s->block_state[96], b->color[2]-pcr, 1);
2009 8c36eaaa Loren Merritt
        set_blocks(s, level, x, y, b->color[0], b->color[1], b->color[2], pmx, pmy, 0, BLOCK_INTRA);
2010 51d6a3cf Michael Niedermayer
    }else{
2011 85fc0e75 Loren Merritt
        pred_mv(s, &pmx, &pmy, b->ref, left, top, tr);
2012 51d6a3cf Michael Niedermayer
        put_rac(&s->c, &s->block_state[1 + (left->type&1) + (top->type&1)], 0);
2013 8c36eaaa Loren Merritt
        if(s->ref_frames > 1)
2014
            put_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], b->ref, 0);
2015 51d6a3cf Michael Niedermayer
        put_symbol(&s->c, &s->block_state[128 + 32*mx_context], b->mx - pmx, 1);
2016
        put_symbol(&s->c, &s->block_state[128 + 32*my_context], b->my - pmy, 1);
2017 8c36eaaa Loren Merritt
        set_blocks(s, level, x, y, pl, pcb, pcr, b->mx, b->my, b->ref, 0);
2018 51d6a3cf Michael Niedermayer
    }
2019
}
2020
2021 155ec6ed Michael Niedermayer
static void decode_q_branch(SnowContext *s, int level, int x, int y){
2022
    const int w= s->b_width << s->block_max_depth;
2023
    const int rem_depth= s->block_max_depth - level;
2024
    const int index= (x + y*w) << rem_depth;
2025
    int trx= (x+1)<<rem_depth;
2026 aadcc5ce Panagiotis Issaris
    const BlockNode *left  = x ? &s->block[index-1] : &null_block;
2027
    const BlockNode *top   = y ? &s->block[index-w] : &null_block;
2028
    const BlockNode *tl    = y && x ? &s->block[index-w-1] : left;
2029
    const BlockNode *tr    = y && trx<w && ((x&1)==0 || level==0) ? &s->block[index-w+(1<<rem_depth)] : tl; //FIXME use lt
2030 155ec6ed Michael Niedermayer
    int s_context= 2*left->level + 2*top->level + tl->level + tr->level;
2031 115329f1 Diego Biurrun
2032 155ec6ed Michael Niedermayer
    if(s->keyframe){
2033 8c36eaaa Loren Merritt
        set_blocks(s, level, x, y, null_block.color[0], null_block.color[1], null_block.color[2], null_block.mx, null_block.my, null_block.ref, BLOCK_INTRA);
2034 155ec6ed Michael Niedermayer
        return;
2035
    }
2036
2037 28869757 Michael Niedermayer
    if(level==s->block_max_depth || get_rac(&s->c, &s->block_state[4 + s_context])){
2038 1e90b34f Michael Niedermayer
        int type, mx, my;
2039 155ec6ed Michael Niedermayer
        int l = left->color[0];
2040
        int cb= left->color[1];
2041
        int cr= left->color[2];
2042 8c36eaaa Loren Merritt
        int ref = 0;
2043
        int ref_context= av_log2(2*left->ref) + av_log2(2*top->ref);
2044 c26abfa5 Diego Biurrun
        int mx_context= av_log2(2*FFABS(left->mx - top->mx)) + 0*av_log2(2*FFABS(tr->mx - top->mx));
2045
        int my_context= av_log2(2*FFABS(left->my - top->my)) + 0*av_log2(2*FFABS(tr->my - top->my));
2046 115329f1 Diego Biurrun
2047 28869757 Michael Niedermayer
        type= get_rac(&s->c, &s->block_state[1 + left->type + top->type]) ? BLOCK_INTRA : 0;
2048 155ec6ed Michael Niedermayer
2049
        if(type){
2050 85fc0e75 Loren Merritt
            pred_mv(s, &mx, &my, 0, left, top, tr);
2051 155ec6ed Michael Niedermayer
            l += get_symbol(&s->c, &s->block_state[32], 1);
2052
            cb+= get_symbol(&s->c, &s->block_state[64], 1);
2053
            cr+= get_symbol(&s->c, &s->block_state[96], 1);
2054
        }else{
2055 8c36eaaa Loren Merritt
            if(s->ref_frames > 1)
2056
                ref= get_symbol(&s->c, &s->block_state[128 + 1024 + 32*ref_context], 0);
2057 85fc0e75 Loren Merritt
            pred_mv(s, &mx, &my, ref, left, top, tr);
2058 8c36eaaa Loren Merritt
            mx+= get_symbol(&s->c, &s->block_state[128 + 32*(mx_context + 16*!!ref)], 1);
2059
            my+= get_symbol(&s->c, &s->block_state[128 + 32*(my_context + 16*!!ref)], 1);
2060 155ec6ed Michael Niedermayer
        }
2061 8c36eaaa Loren Merritt
        set_blocks(s, level, x, y, l, cb, cr, mx, my, ref, type);
2062 155ec6ed Michael Niedermayer
    }else{
2063
        decode_q_branch(s, level+1, 2*x+0, 2*y+0);
2064
        decode_q_branch(s, level+1, 2*x+1, 2*y+0);
2065
        decode_q_branch(s, level+1, 2*x+0, 2*y+1);
2066
        decode_q_branch(s, level+1, 2*x+1, 2*y+1);
2067
    }
2068
}
2069
2070 74e6a8aa Loren Merritt
static void encode_blocks(SnowContext *s, int search){
2071 155ec6ed Michael Niedermayer
    int x, y;
2072
    int w= s->b_width;
2073
    int h= s->b_height;
2074
2075 74e6a8aa Loren Merritt
    if(s->avctx->me_method == ME_ITER && !s->keyframe && search)
2076 51d6a3cf Michael Niedermayer
        iterative_me(s);
2077
2078 155ec6ed Michael Niedermayer
    for(y=0; y<h; y++){
2079 d06c75a8 Matthieu Castet
        if(s->c.bytestream_end - s->c.bytestream < w*MB_SIZE*MB_SIZE*3){ //FIXME nicer limit
2080 0ecca7a4 Michael Niedermayer
            av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2081
            return;
2082
        }
2083 155ec6ed Michael Niedermayer
        for(x=0; x<w; x++){
2084 74e6a8aa Loren Merritt
            if(s->avctx->me_method == ME_ITER || !search)
2085 51d6a3cf Michael Niedermayer
                encode_q_branch2(s, 0, x, y);
2086
            else
2087
                encode_q_branch (s, 0, x, y);
2088 155ec6ed Michael Niedermayer
        }
2089
    }
2090
}
2091
2092
static void decode_blocks(SnowContext *s){
2093
    int x, y;
2094
    int w= s->b_width;
2095
    int h= s->b_height;
2096
2097
    for(y=0; y<h; y++){
2098
        for(x=0; x<w; x++){
2099
            decode_q_branch(s, 0, x, y);
2100
        }
2101
    }
2102 791e7b83 Michael Niedermayer
}
2103
2104 7d7f57d9 Michael Niedermayer
static void mc_block(Plane *p, uint8_t *dst, const uint8_t *src, uint8_t *tmp, int stride, int b_w, int b_h, int dx, int dy){
2105 7942269b Mathieu Malaterre
    static const uint8_t weight[64]={
2106 a68ca08e Michael Niedermayer
    8,7,6,5,4,3,2,1,
2107
    7,7,0,0,0,0,0,1,
2108
    6,0,6,0,0,0,2,0,
2109
    5,0,0,5,0,3,0,0,
2110
    4,0,0,0,4,0,0,0,
2111
    3,0,0,5,0,3,0,0,
2112
    2,0,6,0,0,0,2,0,
2113
    1,7,0,0,0,0,0,1,
2114
    };
2115
2116 7942269b Mathieu Malaterre
    static const uint8_t brane[256]={
2117 a68ca08e Michael Niedermayer
    0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x11,0x12,0x12,0x12,0x12,0x12,0x12,0x12,
2118
    0x04,0x05,0xcc,0xcc,0xcc,0xcc,0xcc,0x41,0x15,0x16,0xcc,0xcc,0xcc,0xcc,0xcc,0x52,
2119
    0x04,0xcc,0x05,0xcc,0xcc,0xcc,0x41,0xcc,0x15,0xcc,0x16,0xcc,0xcc,0xcc,0x52,0xcc,
2120
    0x04,0xcc,0xcc,0x05,0xcc,0x41,0xcc,0xcc,0x15,0xcc,0xcc,0x16,0xcc,0x52,0xcc,0xcc,
2121
    0x04,0xcc,0xcc,0xcc,0x41,0xcc,0xcc,0xcc,0x15,0xcc,0xcc,0xcc,0x16,0xcc,0xcc,0xcc,
2122
    0x04,0xcc,0xcc,0x41,0xcc,0x05,0xcc,0xcc,0x15,0xcc,0xcc,0x52,0xcc,0x16,0xcc,0xcc,
2123
    0x04,0xcc,0x41,0xcc,0xcc,0xcc,0x05,0xcc,0x15,0xcc,0x52,0xcc,0xcc,0xcc,0x16,0xcc,
2124
    0x04,0x41,0xcc,0xcc,0xcc,0xcc,0xcc,0x05,0x15,0x52,0xcc,0xcc,0xcc,0xcc,0xcc,0x16,
2125
    0x44,0x45,0x45,0x45,0x45,0x45,0x45,0x45,0x55,0x56,0x56,0x56,0x56,0x56,0x56,0x56,
2126
    0x48,0x49,0xcc,0xcc,0xcc,0xcc,0xcc,0x85,0x59,0x5A,0xcc,0xcc,0xcc,0xcc,0xcc,0x96,
2127
    0x48,0xcc,0x49,0xcc,0xcc,0xcc,0x85,0xcc,0x59,0xcc,0x5A,0xcc,0xcc,0xcc,0x96,0xcc,
2128
    0x48,0xcc,0xcc,0x49,0xcc,0x85,0xcc,0xcc,0x59,0xcc,0xcc,0x5A,0xcc,0x96,0xcc,0xcc,
2129
    0x48,0xcc,0xcc,0xcc,0x49,0xcc,0xcc,0xcc,0x59,0xcc,0xcc,0xcc,0x96,0xcc,0xcc,0xcc,
2130
    0x48,0xcc,0xcc,0x85,0xcc,0x49,0xcc,0xcc,0x59,0xcc,0xcc,0x96,0xcc,0x5A,0xcc,0xcc,
2131
    0x48,0xcc,0x85,0xcc,0xcc,0xcc,0x49,0xcc,0x59,0xcc,0x96,0xcc,0xcc,0xcc,0x5A,0xcc,
2132
    0x48,0x85,0xcc,0xcc,0xcc,0xcc,0xcc,0x49,0x59,0x96,0xcc,0xcc,0xcc,0xcc,0xcc,0x5A,
2133
    };
2134
2135 7942269b Mathieu Malaterre
    static const uint8_t needs[16]={
2136 a68ca08e Michael Niedermayer
    0,1,0,0,
2137
    2,4,2,0,
2138
    0,1,0,0,
2139
    15
2140
    };
2141
2142
    int x, y, b, r, l;
2143 61d6e445 Michael Niedermayer
    int16_t tmpIt   [64*(32+HTAPS_MAX)];
2144
    uint8_t tmp2t[3][stride*(32+HTAPS_MAX)];
2145 a68ca08e Michael Niedermayer
    int16_t *tmpI= tmpIt;
2146
    uint8_t *tmp2= tmp2t[0];
2147 f0a70840 Baptiste Coudurier
    const uint8_t *hpel[11];
2148 a68ca08e Michael Niedermayer
    assert(dx<16 && dy<16);
2149
    r= brane[dx + 16*dy]&15;
2150
    l= brane[dx + 16*dy]>>4;
2151
2152
    b= needs[l] | needs[r];
2153 7d7f57d9 Michael Niedermayer
    if(p && !p->diag_mc)
2154
        b= 15;
2155 a68ca08e Michael Niedermayer
2156
    if(b&5){
2157 61d6e445 Michael Niedermayer
        for(y=0; y < b_h+HTAPS_MAX-1; y++){
2158 65dc0f53 Michael Niedermayer
            for(x=0; x < b_w; x++){
2159 61d6e445 Michael Niedermayer
                int a_1=src[x + HTAPS_MAX/2-4];
2160
                int a0= src[x + HTAPS_MAX/2-3];
2161
                int a1= src[x + HTAPS_MAX/2-2];
2162
                int a2= src[x + HTAPS_MAX/2-1];
2163
                int a3= src[x + HTAPS_MAX/2+0];
2164
                int a4= src[x + HTAPS_MAX/2+1];
2165
                int a5= src[x + HTAPS_MAX/2+2];
2166
                int a6= src[x + HTAPS_MAX/2+3];
2167 7d7f57d9 Michael Niedermayer
                int am=0;
2168
                if(!p || p->fast_mc){
2169
                    am= 20*(a2+a3) - 5*(a1+a4) + (a0+a5);
2170
                    tmpI[x]= am;
2171
                    am= (am+16)>>5;
2172
                }else{
2173
                    am= p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6);
2174
                    tmpI[x]= am;
2175
                    am= (am+32)>>6;
2176
                }
2177 791e7b83 Michael Niedermayer
2178 65dc0f53 Michael Niedermayer
                if(am&(~255)) am= ~(am>>31);
2179
                tmp2[x]= am;
2180
            }
2181
            tmpI+= 64;
2182
            tmp2+= stride;
2183
            src += stride;
2184 791e7b83 Michael Niedermayer
        }
2185 65dc0f53 Michael Niedermayer
        src -= stride*y;
2186 a68ca08e Michael Niedermayer
    }
2187 61d6e445 Michael Niedermayer
    src += HTAPS_MAX/2 - 1;
2188 a68ca08e Michael Niedermayer
    tmp2= tmp2t[1];
2189 115329f1 Diego Biurrun
2190 a68ca08e Michael Niedermayer
    if(b&2){
2191 65dc0f53 Michael Niedermayer
        for(y=0; y < b_h; y++){
2192
            for(x=0; x < b_w+1; x++){
2193 61d6e445 Michael Niedermayer
                int a_1=src[x + (HTAPS_MAX/2-4)*stride];
2194
                int a0= src[x + (HTAPS_MAX/2-3)*stride];
2195
                int a1= src[x + (HTAPS_MAX/2-2)*stride];
2196
                int a2= src[x + (HTAPS_MAX/2-1)*stride];
2197
                int a3= src[x + (HTAPS_MAX/2+0)*stride];
2198
                int a4= src[x + (HTAPS_MAX/2+1)*stride];
2199
                int a5= src[x + (HTAPS_MAX/2+2)*stride];
2200
                int a6= src[x + (HTAPS_MAX/2+3)*stride];
2201 7d7f57d9 Michael Niedermayer
                int am=0;
2202
                if(!p || p->fast_mc)
2203
                    am= (20*(a2+a3) - 5*(a1+a4) + (a0+a5) + 16)>>5;
2204
                else
2205
                    am= (p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6) + 32)>>6;
2206 791e7b83 Michael Niedermayer
2207 65dc0f53 Michael Niedermayer
                if(am&(~255)) am= ~(am>>31);
2208
                tmp2[x]= am;
2209
            }
2210
            src += stride;
2211
            tmp2+= stride;
2212 a68ca08e Michael Niedermayer
        }
2213 65dc0f53 Michael Niedermayer
        src -= stride*y;
2214 a68ca08e Michael Niedermayer
    }
2215 61d6e445 Michael Niedermayer
    src += stride*(HTAPS_MAX/2 - 1);
2216 a68ca08e Michael Niedermayer
    tmp2= tmp2t[2];
2217
    tmpI= tmpIt;
2218
    if(b&4){
2219
        for(y=0; y < b_h; y++){
2220
            for(x=0; x < b_w; x++){
2221 61d6e445 Michael Niedermayer
                int a_1=tmpI[x + (HTAPS_MAX/2-4)*64];
2222
                int a0= tmpI[x + (HTAPS_MAX/2-3)*64];
2223
                int a1= tmpI[x + (HTAPS_MAX/2-2)*64];
2224
                int a2= tmpI[x + (HTAPS_MAX/2-1)*64];
2225
                int a3= tmpI[x + (HTAPS_MAX/2+0)*64];
2226
                int a4= tmpI[x + (HTAPS_MAX/2+1)*64];
2227
                int a5= tmpI[x + (HTAPS_MAX/2+2)*64];
2228
                int a6= tmpI[x + (HTAPS_MAX/2+3)*64];
2229 7d7f57d9 Michael Niedermayer
                int am=0;
2230
                if(!p || p->fast_mc)
2231
                    am= (20*(a2+a3) - 5*(a1+a4) + (a0+a5) + 512)>>10;
2232
                else
2233
                    am= (p->hcoeff[0]*(a2+a3) + p->hcoeff[1]*(a1+a4) + p->hcoeff[2]*(a0+a5) + p->hcoeff[3]*(a_1+a6) + 2048)>>12;
2234 a68ca08e Michael Niedermayer
                if(am&(~255)) am= ~(am>>31);
2235
                tmp2[x]= am;
2236
            }
2237
            tmpI+= 64;
2238
            tmp2+= stride;
2239
        }
2240
    }
2241 115329f1 Diego Biurrun
2242 a68ca08e Michael Niedermayer
    hpel[ 0]= src;
2243 61d6e445 Michael Niedermayer
    hpel[ 1]= tmp2t[0] + stride*(HTAPS_MAX/2-1);
2244 a68ca08e Michael Niedermayer
    hpel[ 2]= src + 1;
2245
2246
    hpel[ 4]= tmp2t[1];
2247
    hpel[ 5]= tmp2t[2];
2248
    hpel[ 6]= tmp2t[1] + 1;
2249
2250
    hpel[ 8]= src + stride;
2251
    hpel[ 9]= hpel[1] + stride;
2252
    hpel[10]= hpel[8] + 1;
2253
2254
    if(b==15){
2255 f0a70840 Baptiste Coudurier
        const uint8_t *src1= hpel[dx/8 + dy/8*4  ];
2256
        const uint8_t *src2= hpel[dx/8 + dy/8*4+1];
2257
        const uint8_t *src3= hpel[dx/8 + dy/8*4+4];
2258
        const uint8_t *src4= hpel[dx/8 + dy/8*4+5];
2259 a68ca08e Michael Niedermayer
        dx&=7;
2260
        dy&=7;
2261
        for(y=0; y < b_h; y++){
2262
            for(x=0; x < b_w; x++){
2263
                dst[x]= ((8-dx)*(8-dy)*src1[x] + dx*(8-dy)*src2[x]+
2264
                         (8-dx)*   dy *src3[x] + dx*   dy *src4[x]+32)>>6;
2265
            }
2266
            src1+=stride;
2267
            src2+=stride;
2268
            src3+=stride;
2269
            src4+=stride;
2270
            dst +=stride;
2271
        }
2272
    }else{
2273 f0a70840 Baptiste Coudurier
        const uint8_t *src1= hpel[l];
2274
        const uint8_t *src2= hpel[r];
2275 a68ca08e Michael Niedermayer
        int a= weight[((dx&7) + (8*(dy&7)))];
2276
        int b= 8-a;
2277
        for(y=0; y < b_h; y++){
2278
            for(x=0; x < b_w; x++){
2279
                dst[x]= (a*src1[x] + b*src2[x] + 4)>>3;
2280
            }
2281
            src1+=stride;
2282
            src2+=stride;
2283
            dst +=stride;
2284 791e7b83 Michael Niedermayer
        }
2285
    }
2286
}
2287
2288
#define mca(dx,dy,b_w)\
2289 bad700e3 Måns Rullgård
static void mc_block_hpel ## dx ## dy ## b_w(uint8_t *dst, const uint8_t *src, int stride, int h){\
2290 61d6e445 Michael Niedermayer
    uint8_t tmp[stride*(b_w+HTAPS_MAX-1)];\
2291 791e7b83 Michael Niedermayer
    assert(h==b_w);\
2292 61d6e445 Michael Niedermayer
    mc_block(NULL, dst, src-(HTAPS_MAX/2-1)-(HTAPS_MAX/2-1)*stride, tmp, stride, b_w, b_w, dx, dy);\
2293 791e7b83 Michael Niedermayer
}
2294
2295
mca( 0, 0,16)
2296
mca( 8, 0,16)
2297
mca( 0, 8,16)
2298
mca( 8, 8,16)
2299 d92b5807 Michael Niedermayer
mca( 0, 0,8)
2300
mca( 8, 0,8)
2301
mca( 0, 8,8)
2302
mca( 8, 8,8)
2303 791e7b83 Michael Niedermayer
2304 8c36eaaa Loren Merritt
static void pred_block(SnowContext *s, uint8_t *dst, uint8_t *tmp, int stride, int sx, int sy, int b_w, int b_h, BlockNode *block, int plane_index, int w, int h){
2305 51d6a3cf Michael Niedermayer
    if(block->type & BLOCK_INTRA){
2306 ff158dc9 Michael Niedermayer
        int x, y;
2307 2692ceab Michael Niedermayer
        const int color = block->color[plane_index];
2308
        const int color4= color*0x01010101;
2309 1015631b Loren Merritt
        if(b_w==32){
2310
            for(y=0; y < b_h; y++){
2311
                *(uint32_t*)&dst[0 + y*stride]= color4;
2312
                *(uint32_t*)&dst[4 + y*stride]= color4;
2313
                *(uint32_t*)&dst[8 + y*stride]= color4;
2314
                *(uint32_t*)&dst[12+ y*stride]= color4;
2315
                *(uint32_t*)&dst[16+ y*stride]= color4;
2316
                *(uint32_t*)&dst[20+ y*stride]= color4;
2317
                *(uint32_t*)&dst[24+ y*stride]= color4;
2318
                *(uint32_t*)&dst[28+ y*stride]= color4;
2319
            }
2320
        }else if(b_w==16){
2321