Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/libtheora/encfrag.c
9903 views
1
/********************************************************************
2
* *
3
* THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. *
4
* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
5
* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6
* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
7
* *
8
* THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009,2025 *
9
* by the Xiph.Org Foundation https://www.xiph.org/ *
10
* *
11
********************************************************************
12
13
function:
14
15
********************************************************************/
16
#include <stdlib.h>
17
#include <string.h>
18
#include "encint.h"
19
20
21
void oc_enc_frag_sub_c(ogg_int16_t _diff[64],const unsigned char *_src,
22
const unsigned char *_ref,int _ystride){
23
int i;
24
for(i=0;i<8;i++){
25
int j;
26
for(j=0;j<8;j++)_diff[i*8+j]=(ogg_int16_t)(_src[j]-_ref[j]);
27
_src+=_ystride;
28
_ref+=_ystride;
29
}
30
}
31
32
void oc_enc_frag_sub_128_c(ogg_int16_t _diff[64],
33
const unsigned char *_src,int _ystride){
34
int i;
35
for(i=0;i<8;i++){
36
int j;
37
for(j=0;j<8;j++)_diff[i*8+j]=(ogg_int16_t)(_src[j]-128);
38
_src+=_ystride;
39
}
40
}
41
42
unsigned oc_enc_frag_sad_c(const unsigned char *_src,
43
const unsigned char *_ref,int _ystride){
44
unsigned sad;
45
int i;
46
sad=0;
47
for(i=8;i-->0;){
48
int j;
49
for(j=0;j<8;j++)sad+=abs(_src[j]-_ref[j]);
50
_src+=_ystride;
51
_ref+=_ystride;
52
}
53
return sad;
54
}
55
56
unsigned oc_enc_frag_sad_thresh_c(const unsigned char *_src,
57
const unsigned char *_ref,int _ystride,unsigned _thresh){
58
unsigned sad;
59
int i;
60
sad=0;
61
for(i=8;i-->0;){
62
int j;
63
for(j=0;j<8;j++)sad+=abs(_src[j]-_ref[j]);
64
if(sad>_thresh)break;
65
_src+=_ystride;
66
_ref+=_ystride;
67
}
68
return sad;
69
}
70
71
unsigned oc_enc_frag_sad2_thresh_c(const unsigned char *_src,
72
const unsigned char *_ref1,const unsigned char *_ref2,int _ystride,
73
unsigned _thresh){
74
unsigned sad;
75
int i;
76
sad=0;
77
for(i=8;i-->0;){
78
int j;
79
for(j=0;j<8;j++)sad+=abs(_src[j]-(_ref1[j]+_ref2[j]>>1));
80
if(sad>_thresh)break;
81
_src+=_ystride;
82
_ref1+=_ystride;
83
_ref2+=_ystride;
84
}
85
return sad;
86
}
87
88
unsigned oc_enc_frag_intra_sad_c(const unsigned char *_src, int _ystride){
89
const unsigned char *src = _src;
90
int dc;
91
unsigned sad;
92
int i;
93
dc=0;
94
for(i=8;i-->0;){
95
int j;
96
for(j=0;j<8;j++)dc+=src[j];
97
src+=_ystride;
98
}
99
dc=dc+32>>6;
100
sad=0;
101
for(i=8;i-->0;){
102
int j;
103
for(j=0;j<8;j++)sad+=abs(_src[j]-dc);
104
_src+=_ystride;
105
}
106
return sad;
107
}
108
109
static void oc_diff_hadamard(ogg_int16_t _buf[64],const unsigned char *_src,
110
const unsigned char *_ref,int _ystride){
111
int i;
112
for(i=0;i<8;i++){
113
int t0;
114
int t1;
115
int t2;
116
int t3;
117
int t4;
118
int t5;
119
int t6;
120
int t7;
121
int r;
122
/*Hadamard stage 1:*/
123
t0=_src[0]-_ref[0]+_src[4]-_ref[4];
124
t4=_src[0]-_ref[0]-_src[4]+_ref[4];
125
t1=_src[1]-_ref[1]+_src[5]-_ref[5];
126
t5=_src[1]-_ref[1]-_src[5]+_ref[5];
127
t2=_src[2]-_ref[2]+_src[6]-_ref[6];
128
t6=_src[2]-_ref[2]-_src[6]+_ref[6];
129
t3=_src[3]-_ref[3]+_src[7]-_ref[7];
130
t7=_src[3]-_ref[3]-_src[7]+_ref[7];
131
/*Hadamard stage 2:*/
132
r=t0;
133
t0+=t2;
134
t2=r-t2;
135
r=t1;
136
t1+=t3;
137
t3=r-t3;
138
r=t4;
139
t4+=t6;
140
t6=r-t6;
141
r=t5;
142
t5+=t7;
143
t7=r-t7;
144
/*Hadamard stage 3:*/
145
_buf[0*8+i]=(ogg_int16_t)(t0+t1);
146
_buf[1*8+i]=(ogg_int16_t)(t0-t1);
147
_buf[2*8+i]=(ogg_int16_t)(t2+t3);
148
_buf[3*8+i]=(ogg_int16_t)(t2-t3);
149
_buf[4*8+i]=(ogg_int16_t)(t4+t5);
150
_buf[5*8+i]=(ogg_int16_t)(t4-t5);
151
_buf[6*8+i]=(ogg_int16_t)(t6+t7);
152
_buf[7*8+i]=(ogg_int16_t)(t6-t7);
153
_src+=_ystride;
154
_ref+=_ystride;
155
}
156
}
157
158
static void oc_diff_hadamard2(ogg_int16_t _buf[64],const unsigned char *_src,
159
const unsigned char *_ref1,const unsigned char *_ref2,int _ystride){
160
int i;
161
for(i=0;i<8;i++){
162
int t0;
163
int t1;
164
int t2;
165
int t3;
166
int t4;
167
int t5;
168
int t6;
169
int t7;
170
int r;
171
/*Hadamard stage 1:*/
172
r=_ref1[0]+_ref2[0]>>1;
173
t4=_ref1[4]+_ref2[4]>>1;
174
t0=_src[0]-r+_src[4]-t4;
175
t4=_src[0]-r-_src[4]+t4;
176
r=_ref1[1]+_ref2[1]>>1;
177
t5=_ref1[5]+_ref2[5]>>1;
178
t1=_src[1]-r+_src[5]-t5;
179
t5=_src[1]-r-_src[5]+t5;
180
r=_ref1[2]+_ref2[2]>>1;
181
t6=_ref1[6]+_ref2[6]>>1;
182
t2=_src[2]-r+_src[6]-t6;
183
t6=_src[2]-r-_src[6]+t6;
184
r=_ref1[3]+_ref2[3]>>1;
185
t7=_ref1[7]+_ref2[7]>>1;
186
t3=_src[3]-r+_src[7]-t7;
187
t7=_src[3]-r-_src[7]+t7;
188
/*Hadamard stage 2:*/
189
r=t0;
190
t0+=t2;
191
t2=r-t2;
192
r=t1;
193
t1+=t3;
194
t3=r-t3;
195
r=t4;
196
t4+=t6;
197
t6=r-t6;
198
r=t5;
199
t5+=t7;
200
t7=r-t7;
201
/*Hadamard stage 3:*/
202
_buf[0*8+i]=(ogg_int16_t)(t0+t1);
203
_buf[1*8+i]=(ogg_int16_t)(t0-t1);
204
_buf[2*8+i]=(ogg_int16_t)(t2+t3);
205
_buf[3*8+i]=(ogg_int16_t)(t2-t3);
206
_buf[4*8+i]=(ogg_int16_t)(t4+t5);
207
_buf[5*8+i]=(ogg_int16_t)(t4-t5);
208
_buf[6*8+i]=(ogg_int16_t)(t6+t7);
209
_buf[7*8+i]=(ogg_int16_t)(t6-t7);
210
_src+=_ystride;
211
_ref1+=_ystride;
212
_ref2+=_ystride;
213
}
214
}
215
216
static void oc_intra_hadamard(ogg_int16_t _buf[64],const unsigned char *_src,
217
int _ystride){
218
int i;
219
for(i=0;i<8;i++){
220
int t0;
221
int t1;
222
int t2;
223
int t3;
224
int t4;
225
int t5;
226
int t6;
227
int t7;
228
int r;
229
/*Hadamard stage 1:*/
230
t0=_src[0]+_src[4];
231
t4=_src[0]-_src[4];
232
t1=_src[1]+_src[5];
233
t5=_src[1]-_src[5];
234
t2=_src[2]+_src[6];
235
t6=_src[2]-_src[6];
236
t3=_src[3]+_src[7];
237
t7=_src[3]-_src[7];
238
/*Hadamard stage 2:*/
239
r=t0;
240
t0+=t2;
241
t2=r-t2;
242
r=t1;
243
t1+=t3;
244
t3=r-t3;
245
r=t4;
246
t4+=t6;
247
t6=r-t6;
248
r=t5;
249
t5+=t7;
250
t7=r-t7;
251
/*Hadamard stage 3:*/
252
_buf[0*8+i]=(ogg_int16_t)(t0+t1);
253
_buf[1*8+i]=(ogg_int16_t)(t0-t1);
254
_buf[2*8+i]=(ogg_int16_t)(t2+t3);
255
_buf[3*8+i]=(ogg_int16_t)(t2-t3);
256
_buf[4*8+i]=(ogg_int16_t)(t4+t5);
257
_buf[5*8+i]=(ogg_int16_t)(t4-t5);
258
_buf[6*8+i]=(ogg_int16_t)(t6+t7);
259
_buf[7*8+i]=(ogg_int16_t)(t6-t7);
260
_src+=_ystride;
261
}
262
}
263
264
unsigned oc_hadamard_sad(int *_dc,const ogg_int16_t _buf[64]){
265
unsigned sad;
266
int dc;
267
int t0;
268
int t1;
269
int t2;
270
int t3;
271
int t4;
272
int t5;
273
int t6;
274
int t7;
275
int r;
276
int i;
277
sad=dc=0;
278
for(i=0;i<8;i++){
279
/*Hadamard stage 1:*/
280
t0=_buf[i*8+0]+_buf[i*8+4];
281
t4=_buf[i*8+0]-_buf[i*8+4];
282
t1=_buf[i*8+1]+_buf[i*8+5];
283
t5=_buf[i*8+1]-_buf[i*8+5];
284
t2=_buf[i*8+2]+_buf[i*8+6];
285
t6=_buf[i*8+2]-_buf[i*8+6];
286
t3=_buf[i*8+3]+_buf[i*8+7];
287
t7=_buf[i*8+3]-_buf[i*8+7];
288
/*Hadamard stage 2:*/
289
r=t0;
290
t0+=t2;
291
t2=r-t2;
292
r=t1;
293
t1+=t3;
294
t3=r-t3;
295
r=t4;
296
t4+=t6;
297
t6=r-t6;
298
r=t5;
299
t5+=t7;
300
t7=r-t7;
301
/*Hadamard stage 3:*/
302
r=abs(t0+t1)&-(i>0);
303
r+=abs(t0-t1);
304
r+=abs(t2+t3);
305
r+=abs(t2-t3);
306
r+=abs(t4+t5);
307
r+=abs(t4-t5);
308
r+=abs(t6+t7);
309
r+=abs(t6-t7);
310
sad+=r;
311
}
312
dc=_buf[0]+_buf[1]+_buf[2]+_buf[3]+_buf[4]+_buf[5]+_buf[6]+_buf[7];
313
*_dc=dc;
314
return sad;
315
}
316
317
unsigned oc_enc_frag_satd_c(int *_dc,const unsigned char *_src,
318
const unsigned char *_ref,int _ystride){
319
ogg_int16_t buf[64];
320
oc_diff_hadamard(buf,_src,_ref,_ystride);
321
return oc_hadamard_sad(_dc,buf);
322
}
323
324
unsigned oc_enc_frag_satd2_c(int *_dc,const unsigned char *_src,
325
const unsigned char *_ref1,const unsigned char *_ref2,int _ystride){
326
ogg_int16_t buf[64];
327
oc_diff_hadamard2(buf,_src,_ref1,_ref2,_ystride);
328
return oc_hadamard_sad(_dc,buf);
329
}
330
331
unsigned oc_enc_frag_intra_satd_c(int *_dc,
332
const unsigned char *_src,int _ystride){
333
ogg_int16_t buf[64];
334
oc_intra_hadamard(buf,_src,_ystride);
335
return oc_hadamard_sad(_dc,buf);
336
}
337
338
unsigned oc_enc_frag_ssd_c(const unsigned char *_src,
339
const unsigned char *_ref,int _ystride){
340
unsigned ret;
341
int y;
342
int x;
343
ret=0;
344
for(y=0;y<8;y++){
345
for(x=0;x<8;x++)ret+=(_src[x]-_ref[x])*(_src[x]-_ref[x]);
346
_src+=_ystride;
347
_ref+=_ystride;
348
}
349
return ret;
350
}
351
352
unsigned oc_enc_frag_border_ssd_c(const unsigned char *_src,
353
const unsigned char *_ref,int _ystride,ogg_int64_t _mask){
354
unsigned ret;
355
int y;
356
int x;
357
ret=0;
358
for(y=0;y<8;y++){
359
for(x=0;x<8;x++,_mask>>=1){
360
if(_mask&1)ret+=(_src[x]-_ref[x])*(_src[x]-_ref[x]);
361
}
362
_src+=_ystride;
363
_ref+=_ystride;
364
}
365
return ret;
366
}
367
368
void oc_enc_frag_copy2_c(unsigned char *_dst,
369
const unsigned char *_src1,const unsigned char *_src2,int _ystride){
370
int i;
371
int j;
372
for(i=8;i-->0;){
373
for(j=0;j<8;j++)_dst[j]=_src1[j]+_src2[j]>>1;
374
_dst+=_ystride;
375
_src1+=_ystride;
376
_src2+=_ystride;
377
}
378
}
379
380