Coverage Report

Created: 2025-08-28 07:12

/src/theora/lib/encfrag.c
Line
Count
Source (jump to first uncovered line)
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
0
 const unsigned char *_ref,int _ystride){
23
0
  int i;
24
0
  for(i=0;i<8;i++){
25
0
    int j;
26
0
    for(j=0;j<8;j++)_diff[i*8+j]=(ogg_int16_t)(_src[j]-_ref[j]);
27
0
    _src+=_ystride;
28
0
    _ref+=_ystride;
29
0
  }
30
0
}
31
32
void oc_enc_frag_sub_128_c(ogg_int16_t _diff[64],
33
0
 const unsigned char *_src,int _ystride){
34
0
  int i;
35
0
  for(i=0;i<8;i++){
36
0
    int j;
37
0
    for(j=0;j<8;j++)_diff[i*8+j]=(ogg_int16_t)(_src[j]-128);
38
0
    _src+=_ystride;
39
0
  }
40
0
}
41
42
unsigned oc_enc_frag_sad_c(const unsigned char *_src,
43
0
 const unsigned char *_ref,int _ystride){
44
0
  unsigned sad;
45
0
  int      i;
46
0
  sad=0;
47
0
  for(i=8;i-->0;){
48
0
    int j;
49
0
    for(j=0;j<8;j++)sad+=abs(_src[j]-_ref[j]);
50
0
    _src+=_ystride;
51
0
    _ref+=_ystride;
52
0
  }
53
0
  return sad;
54
0
}
55
56
unsigned oc_enc_frag_sad_thresh_c(const unsigned char *_src,
57
0
 const unsigned char *_ref,int _ystride,unsigned _thresh){
58
0
  unsigned sad;
59
0
  int      i;
60
0
  sad=0;
61
0
  for(i=8;i-->0;){
62
0
    int j;
63
0
    for(j=0;j<8;j++)sad+=abs(_src[j]-_ref[j]);
64
0
    if(sad>_thresh)break;
65
0
    _src+=_ystride;
66
0
    _ref+=_ystride;
67
0
  }
68
0
  return sad;
69
0
}
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
0
 unsigned _thresh){
74
0
  unsigned sad;
75
0
  int      i;
76
0
  sad=0;
77
0
  for(i=8;i-->0;){
78
0
    int j;
79
0
    for(j=0;j<8;j++)sad+=abs(_src[j]-(_ref1[j]+_ref2[j]>>1));
80
0
    if(sad>_thresh)break;
81
0
    _src+=_ystride;
82
0
    _ref1+=_ystride;
83
0
    _ref2+=_ystride;
84
0
  }
85
0
  return sad;
86
0
}
87
88
0
unsigned oc_enc_frag_intra_sad_c(const unsigned char *_src, int _ystride){
89
0
  const unsigned char *src = _src;
90
0
  int dc;
91
0
  unsigned sad;
92
0
  int      i;
93
0
  dc=0;
94
0
  for(i=8;i-->0;){
95
0
    int j;
96
0
    for(j=0;j<8;j++)dc+=src[j];
97
0
    src+=_ystride;
98
0
  }
99
0
  dc=dc+32>>6;
100
0
  sad=0;
101
0
  for(i=8;i-->0;){
102
0
    int j;
103
0
    for(j=0;j<8;j++)sad+=abs(_src[j]-dc);
104
0
    _src+=_ystride;
105
0
  }
106
0
  return sad;
107
0
}
108
109
static void oc_diff_hadamard(ogg_int16_t _buf[64],const unsigned char *_src,
110
0
 const unsigned char *_ref,int _ystride){
111
0
  int i;
112
0
  for(i=0;i<8;i++){
113
0
    int t0;
114
0
    int t1;
115
0
    int t2;
116
0
    int t3;
117
0
    int t4;
118
0
    int t5;
119
0
    int t6;
120
0
    int t7;
121
0
    int r;
122
    /*Hadamard stage 1:*/
123
0
    t0=_src[0]-_ref[0]+_src[4]-_ref[4];
124
0
    t4=_src[0]-_ref[0]-_src[4]+_ref[4];
125
0
    t1=_src[1]-_ref[1]+_src[5]-_ref[5];
126
0
    t5=_src[1]-_ref[1]-_src[5]+_ref[5];
127
0
    t2=_src[2]-_ref[2]+_src[6]-_ref[6];
128
0
    t6=_src[2]-_ref[2]-_src[6]+_ref[6];
129
0
    t3=_src[3]-_ref[3]+_src[7]-_ref[7];
130
0
    t7=_src[3]-_ref[3]-_src[7]+_ref[7];
131
    /*Hadamard stage 2:*/
132
0
    r=t0;
133
0
    t0+=t2;
134
0
    t2=r-t2;
135
0
    r=t1;
136
0
    t1+=t3;
137
0
    t3=r-t3;
138
0
    r=t4;
139
0
    t4+=t6;
140
0
    t6=r-t6;
141
0
    r=t5;
142
0
    t5+=t7;
143
0
    t7=r-t7;
144
    /*Hadamard stage 3:*/
145
0
    _buf[0*8+i]=(ogg_int16_t)(t0+t1);
146
0
    _buf[1*8+i]=(ogg_int16_t)(t0-t1);
147
0
    _buf[2*8+i]=(ogg_int16_t)(t2+t3);
148
0
    _buf[3*8+i]=(ogg_int16_t)(t2-t3);
149
0
    _buf[4*8+i]=(ogg_int16_t)(t4+t5);
150
0
    _buf[5*8+i]=(ogg_int16_t)(t4-t5);
151
0
    _buf[6*8+i]=(ogg_int16_t)(t6+t7);
152
0
    _buf[7*8+i]=(ogg_int16_t)(t6-t7);
153
0
    _src+=_ystride;
154
0
    _ref+=_ystride;
155
0
  }
156
0
}
157
158
static void oc_diff_hadamard2(ogg_int16_t _buf[64],const unsigned char *_src,
159
0
 const unsigned char *_ref1,const unsigned char *_ref2,int _ystride){
160
0
  int i;
161
0
  for(i=0;i<8;i++){
162
0
    int t0;
163
0
    int t1;
164
0
    int t2;
165
0
    int t3;
166
0
    int t4;
167
0
    int t5;
168
0
    int t6;
169
0
    int t7;
170
0
    int r;
171
    /*Hadamard stage 1:*/
172
0
    r=_ref1[0]+_ref2[0]>>1;
173
0
    t4=_ref1[4]+_ref2[4]>>1;
174
0
    t0=_src[0]-r+_src[4]-t4;
175
0
    t4=_src[0]-r-_src[4]+t4;
176
0
    r=_ref1[1]+_ref2[1]>>1;
177
0
    t5=_ref1[5]+_ref2[5]>>1;
178
0
    t1=_src[1]-r+_src[5]-t5;
179
0
    t5=_src[1]-r-_src[5]+t5;
180
0
    r=_ref1[2]+_ref2[2]>>1;
181
0
    t6=_ref1[6]+_ref2[6]>>1;
182
0
    t2=_src[2]-r+_src[6]-t6;
183
0
    t6=_src[2]-r-_src[6]+t6;
184
0
    r=_ref1[3]+_ref2[3]>>1;
185
0
    t7=_ref1[7]+_ref2[7]>>1;
186
0
    t3=_src[3]-r+_src[7]-t7;
187
0
    t7=_src[3]-r-_src[7]+t7;
188
    /*Hadamard stage 2:*/
189
0
    r=t0;
190
0
    t0+=t2;
191
0
    t2=r-t2;
192
0
    r=t1;
193
0
    t1+=t3;
194
0
    t3=r-t3;
195
0
    r=t4;
196
0
    t4+=t6;
197
0
    t6=r-t6;
198
0
    r=t5;
199
0
    t5+=t7;
200
0
    t7=r-t7;
201
    /*Hadamard stage 3:*/
202
0
    _buf[0*8+i]=(ogg_int16_t)(t0+t1);
203
0
    _buf[1*8+i]=(ogg_int16_t)(t0-t1);
204
0
    _buf[2*8+i]=(ogg_int16_t)(t2+t3);
205
0
    _buf[3*8+i]=(ogg_int16_t)(t2-t3);
206
0
    _buf[4*8+i]=(ogg_int16_t)(t4+t5);
207
0
    _buf[5*8+i]=(ogg_int16_t)(t4-t5);
208
0
    _buf[6*8+i]=(ogg_int16_t)(t6+t7);
209
0
    _buf[7*8+i]=(ogg_int16_t)(t6-t7);
210
0
    _src+=_ystride;
211
0
    _ref1+=_ystride;
212
0
    _ref2+=_ystride;
213
0
  }
214
0
}
215
216
static void oc_intra_hadamard(ogg_int16_t _buf[64],const unsigned char *_src,
217
0
 int _ystride){
218
0
  int i;
219
0
  for(i=0;i<8;i++){
220
0
    int t0;
221
0
    int t1;
222
0
    int t2;
223
0
    int t3;
224
0
    int t4;
225
0
    int t5;
226
0
    int t6;
227
0
    int t7;
228
0
    int r;
229
    /*Hadamard stage 1:*/
230
0
    t0=_src[0]+_src[4];
231
0
    t4=_src[0]-_src[4];
232
0
    t1=_src[1]+_src[5];
233
0
    t5=_src[1]-_src[5];
234
0
    t2=_src[2]+_src[6];
235
0
    t6=_src[2]-_src[6];
236
0
    t3=_src[3]+_src[7];
237
0
    t7=_src[3]-_src[7];
238
    /*Hadamard stage 2:*/
239
0
    r=t0;
240
0
    t0+=t2;
241
0
    t2=r-t2;
242
0
    r=t1;
243
0
    t1+=t3;
244
0
    t3=r-t3;
245
0
    r=t4;
246
0
    t4+=t6;
247
0
    t6=r-t6;
248
0
    r=t5;
249
0
    t5+=t7;
250
0
    t7=r-t7;
251
    /*Hadamard stage 3:*/
252
0
    _buf[0*8+i]=(ogg_int16_t)(t0+t1);
253
0
    _buf[1*8+i]=(ogg_int16_t)(t0-t1);
254
0
    _buf[2*8+i]=(ogg_int16_t)(t2+t3);
255
0
    _buf[3*8+i]=(ogg_int16_t)(t2-t3);
256
0
    _buf[4*8+i]=(ogg_int16_t)(t4+t5);
257
0
    _buf[5*8+i]=(ogg_int16_t)(t4-t5);
258
0
    _buf[6*8+i]=(ogg_int16_t)(t6+t7);
259
0
    _buf[7*8+i]=(ogg_int16_t)(t6-t7);
260
0
    _src+=_ystride;
261
0
  }
262
0
}
263
264
0
unsigned oc_hadamard_sad(int *_dc,const ogg_int16_t _buf[64]){
265
0
  unsigned sad;
266
0
  int      dc;
267
0
  int      t0;
268
0
  int      t1;
269
0
  int      t2;
270
0
  int      t3;
271
0
  int      t4;
272
0
  int      t5;
273
0
  int      t6;
274
0
  int      t7;
275
0
  int      r;
276
0
  int      i;
277
0
  sad=dc=0;
278
0
  for(i=0;i<8;i++){
279
    /*Hadamard stage 1:*/
280
0
    t0=_buf[i*8+0]+_buf[i*8+4];
281
0
    t4=_buf[i*8+0]-_buf[i*8+4];
282
0
    t1=_buf[i*8+1]+_buf[i*8+5];
283
0
    t5=_buf[i*8+1]-_buf[i*8+5];
284
0
    t2=_buf[i*8+2]+_buf[i*8+6];
285
0
    t6=_buf[i*8+2]-_buf[i*8+6];
286
0
    t3=_buf[i*8+3]+_buf[i*8+7];
287
0
    t7=_buf[i*8+3]-_buf[i*8+7];
288
    /*Hadamard stage 2:*/
289
0
    r=t0;
290
0
    t0+=t2;
291
0
    t2=r-t2;
292
0
    r=t1;
293
0
    t1+=t3;
294
0
    t3=r-t3;
295
0
    r=t4;
296
0
    t4+=t6;
297
0
    t6=r-t6;
298
0
    r=t5;
299
0
    t5+=t7;
300
0
    t7=r-t7;
301
    /*Hadamard stage 3:*/
302
0
    r=abs(t0+t1)&-(i>0);
303
0
    r+=abs(t0-t1);
304
0
    r+=abs(t2+t3);
305
0
    r+=abs(t2-t3);
306
0
    r+=abs(t4+t5);
307
0
    r+=abs(t4-t5);
308
0
    r+=abs(t6+t7);
309
0
    r+=abs(t6-t7);
310
0
    sad+=r;
311
0
  }
312
0
  dc=_buf[0]+_buf[1]+_buf[2]+_buf[3]+_buf[4]+_buf[5]+_buf[6]+_buf[7];
313
0
  *_dc=dc;
314
0
  return sad;
315
0
}
316
317
unsigned oc_enc_frag_satd_c(int *_dc,const unsigned char *_src,
318
0
 const unsigned char *_ref,int _ystride){
319
0
  ogg_int16_t buf[64];
320
0
  oc_diff_hadamard(buf,_src,_ref,_ystride);
321
0
  return oc_hadamard_sad(_dc,buf);
322
0
}
323
324
unsigned oc_enc_frag_satd2_c(int *_dc,const unsigned char *_src,
325
0
 const unsigned char *_ref1,const unsigned char *_ref2,int _ystride){
326
0
  ogg_int16_t buf[64];
327
0
  oc_diff_hadamard2(buf,_src,_ref1,_ref2,_ystride);
328
0
  return oc_hadamard_sad(_dc,buf);
329
0
}
330
331
unsigned oc_enc_frag_intra_satd_c(int *_dc,
332
0
 const unsigned char *_src,int _ystride){
333
0
  ogg_int16_t buf[64];
334
0
  oc_intra_hadamard(buf,_src,_ystride);
335
0
  return oc_hadamard_sad(_dc,buf);
336
0
}
337
338
unsigned oc_enc_frag_ssd_c(const unsigned char *_src,
339
0
 const unsigned char *_ref,int _ystride){
340
0
  unsigned ret;
341
0
  int      y;
342
0
  int      x;
343
0
  ret=0;
344
0
  for(y=0;y<8;y++){
345
0
    for(x=0;x<8;x++)ret+=(_src[x]-_ref[x])*(_src[x]-_ref[x]);
346
0
    _src+=_ystride;
347
0
    _ref+=_ystride;
348
0
  }
349
0
  return ret;
350
0
}
351
352
unsigned oc_enc_frag_border_ssd_c(const unsigned char *_src,
353
0
 const unsigned char *_ref,int _ystride,ogg_int64_t _mask){
354
0
  unsigned ret;
355
0
  int      y;
356
0
  int      x;
357
0
  ret=0;
358
0
  for(y=0;y<8;y++){
359
0
    for(x=0;x<8;x++,_mask>>=1){
360
0
      if(_mask&1)ret+=(_src[x]-_ref[x])*(_src[x]-_ref[x]);
361
0
    }
362
0
    _src+=_ystride;
363
0
    _ref+=_ystride;
364
0
  }
365
0
  return ret;
366
0
}
367
368
void oc_enc_frag_copy2_c(unsigned char *_dst,
369
0
 const unsigned char *_src1,const unsigned char *_src2,int _ystride){
370
0
  int i;
371
0
  int j;
372
0
  for(i=8;i-->0;){
373
0
    for(j=0;j<8;j++)_dst[j]=_src1[j]+_src2[j]>>1;
374
0
    _dst+=_ystride;
375
0
    _src1+=_ystride;
376
0
    _src2+=_ystride;
377
0
  }
378
0
}