-
Notifications
You must be signed in to change notification settings - Fork 4
/
block.c
executable file
·276 lines (241 loc) · 5.27 KB
/
block.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "codec.h"
#include "cif.h"
#include "misc.h"
int mb_out_bound(MACROBLOCK *mb, int vx, int vy)
{
if (2*mb->mx*MB_SIZE+vx < 0) return -1;
if (2*((mb->mx+1)*MB_SIZE-1)+vx > 2*(mb->pic->image.width-1)) return -1;
if (2*mb->my*MB_SIZE+vy < 0) return -1;
if (2*((mb->my+1)*MB_SIZE-1)+vy > 2*(mb->pic->image.height-1)) return -1;
return 0;
}
int get_block_x(MACROBLOCK *mb, int k)
{
int x;
if (k < 4) {
x = mb->mx*2*BLOCK_SIZE;
x += (k&1)*BLOCK_SIZE;
} else
x = mb->mx*BLOCK_SIZE;
return x;
}
int get_block_y(MACROBLOCK *mb, int k)
{
int y;
if (k < 4) {
y = mb->my*2*BLOCK_SIZE;
y += (k>1)*BLOCK_SIZE;
} else
y = mb->my*BLOCK_SIZE;
return y;
}
int get_block_pos(MACROBLOCK *mb, int k)
{
return get_block_x(mb, k) + mb->pic->cwidth[k]*get_block_y(mb, k);
}
int load_block_data(MACROBLOCK *mb, int k, int dx, int dy)
{
int hx, hy;
hx = 2*get_block_x(mb, k) + dx;
hy = 2*get_block_y(mb, k) + dy;
return load_buf(mb->pic, mb->data[k], BLOCK_SIZE, k, hx, hy);
}
int load_mb_data(MACROBLOCK *mb, int dx, int dy)
{
int k;
mb->cbpc = mb->cbpy = 0;
for (k=0; k<6; k++)
if (load_block_data(mb, k, dx, dy) < 0)
return -1;
return 0;
}
int write_mb_data(MACROBLOCK *mb)
{
int k;
for (k=0; k<6; k++)
store_block_data(mb, k);
return 0;
}
int store_block_data(MACROBLOCK *mb, int k)
{
int hx, hy;
hx = 2*get_block_x(mb, k);
hy = 2*get_block_y(mb, k);
return store_buf(mb->pic, mb->data[k], BLOCK_SIZE, k, hx, hy);
}
int load_buf(PICTURE *pic, int *buf, int size, int k, int hx, int hy)
{
int i, j;
unsigned char *p;
int height, width;
p = pic->cdata[k] + pic->cwidth[k]*hy/2 + hx/2;
height = pic->image.height+(k<4?BLOCK_SIZE:0);
if (p<pic->cdata[k] || p+(pic->cwidth[k])*size>pic->cdata[k]+pic->cwidth[k]*height)
return -1;
for (i=0; i<size; i++) {
for (j=0; j<size; j++)
*buf++ = *p++;
p += pic->cwidth[k] - size;
}
return 0;
}
int store_buf(PICTURE *pic, int *buf, int size, int k, int hx, int hy)
{
int i, j;
unsigned char *p;
p = pic->cdata[k] + pic->cwidth[k]*hy/2 + hx/2;
for (i=0; i<size; i++) {
for (j=0; j<size; j++)
*p++ = *buf++;
p += pic->cwidth[k] - size;
}
return 0;
}
int diff_buf(PICTURE *pic, int *buf, int size, int k, int hx, int hy)
{
int i, j;
unsigned char *p;
p = pic->cdata[k] + pic->cwidth[k]*hy/2 + hx/2;
for (i=0; i<size; i++) {
for (j=0; j<size; j++)
*buf++ -= *p++;
p += pic->cwidth[k] - size;
}
return 0;
}
int add_buf(PICTURE *pic, int *buf, int size, int k, int hx, int hy)
{
int i, j;
unsigned char *p;
p = pic->cdata[k] + pic->cwidth[k]*hy/2 + hx/2;
for (i=0; i<size; i++) {
for (j=0; j<size; j++)
*buf++ += *p++;
p += pic->cwidth[k] - size;
}
return 0;
}
int clip_buf(int *buf, int size, int min, int max, int skip_dc)
{
int i;
buf += skip_dc;
for (i=skip_dc; i<size*size; i++) {
*buf = *buf<=max ? *buf : max;
*buf = *buf>=min ? *buf : min;
buf++;
}
return 0;
}
int sad_buf(PICTURE *pic, int *buf, int size, int k, int hx, int hy)
{
int i, j;
unsigned char *p;
int sad = 0;
p = pic->cdata[k] + pic->cwidth[k]*hy/2 + hx/2;
for (i=0; i<size; i++) {
for (j=0; j<size; j++)
sad += abs(*buf++ - *p++);
p += pic->cwidth[k] - size;
}
return sad;
}
// interpolate [hx-1][hy-1] to buf[2*size+1][2*size+1]
// hx,hy - as even
int load_buf_ipol(PICTURE *pic, unsigned char *buf, int size,
int k, int hx, int hy)
{
int i, j;
unsigned char *p;
int width;
int rc;
rc = pic->rtype;
width = pic->cwidth[k];
p = pic->cdata[k] + hy/2*width + hx/2;
// if (k>3)
// p=pic->cdata[k]+hy/4*width+hx/4;
if (hx>1 && hy>1)
*buf++ = (*(p-width-1) + *(p-width) + *(p-1) + *p + 2 - rc)/4;
else
*buf++ = 0;
for (j=0; j<size; j++) {
if (hy>1) {
*buf++ = (*p + *(p-width) + 1 - rc)/2;
*buf++ = (*(p-width) + *(p-width+1) + *p + *(p+1) + 2 - rc)/4;
p++;
} else {
*buf++ = 0; *buf++ = 0;
p++;
}
}
p-=size;
for (i=0; i<size; i++) {
if (hx>1) {
*buf = (*(p-1) + *p + 1 - rc)/2;
*(buf+2*size+1) = (*(p-1) + *(p-1+width) + *p + *(p+width) + 2 - rc)/4;
} else {
*buf = 0;
*(buf+2*size+1) = 0;
}
buf++;
for (j=0; j<size; j++) {
*buf = *p;
*(buf+2*size+1) = (*p + *(p+width) + 1 - rc)/2;
buf++;
*buf = (*p + *(p+1) + 1 - rc)/2;
*(buf+2*size+1) = (*p + *(p+1) + *(p+width) + *(p+width+1) + 2 - rc)/4;
buf++;
p++;
}
p += width - size;
buf += 2*size+1;
}
return 0;
}
// ibuf[2*size+1][2*size+1] - interolated
// -1 <= hx, hy <= 1
int sad_buf_ipol(int *dbuf, unsigned char *ibuf, int size, int vx, int vy)
{
int i, j;
unsigned char *ipol;
int sad = 0;
ipol = ibuf + (vx+1) + (vy+1)*(2*size+1);
for (i=0; i<size; i++) {
for (j=0; j<size; j++) {
sad += abs(*ipol - *dbuf);
ipol += 2;
dbuf++;
}
ipol += 2*size+1 + 1;
}
return sad;
}
int diff_buf_ipol(int *dbuf, unsigned char *ibuf, int size, int vx, int vy)
{
int i, j;
unsigned char *ipol;
ipol = ibuf + (vx+1) + (vy+1)*(2*size+1);
for (i=0; i<size; i++) {
for (j=0; j<size; j++) {
*dbuf -= *ipol;
ipol += 2;
dbuf++;
}
ipol += 2*size+1 + 1;
}
return 0;
}
int add_buf_ipol(int *dbuf, unsigned char *ibuf, int size, int vx, int vy)
{
int i, j;
unsigned char *ipol;
ipol = ibuf + (vx+1) + (vy+1)*(2*size+1);
for (i=0; i<size; i++) {
for (j=0; j<size; j++) {
*dbuf += *ipol;
ipol += 2;
dbuf++;
}
ipol += 2*size+1 + 1;
}
return 0;
}