summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/avc/enc/src/sad.cpp
blob: ae7acd238175edd68b5b32529ceca71cff814921 (plain)
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* ------------------------------------------------------------------
 * Copyright (C) 1998-2009 PacketVideo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 * -------------------------------------------------------------------
 */
#include "avcenc_lib.h"
#include "sad_inline.h"

#define Cached_lx 176

#ifdef _SAD_STAT
uint32 num_sad_MB = 0;
uint32 num_sad_Blk = 0;
uint32 num_sad_MB_call = 0;
uint32 num_sad_Blk_call = 0;

#define NUM_SAD_MB_CALL()       num_sad_MB_call++
#define NUM_SAD_MB()            num_sad_MB++
#define NUM_SAD_BLK_CALL()      num_sad_Blk_call++
#define NUM_SAD_BLK()           num_sad_Blk++

#else

#define NUM_SAD_MB_CALL()
#define NUM_SAD_MB()
#define NUM_SAD_BLK_CALL()
#define NUM_SAD_BLK()

#endif


/* consist of
int AVCSAD_Macroblock_C(uint8 *ref,uint8 *blk,int dmin,int lx,void *extra_info)
int AVCSAD_MB_HTFM_Collect(uint8 *ref,uint8 *blk,int dmin,int lx,void *extra_info)
int AVCSAD_MB_HTFM(uint8 *ref,uint8 *blk,int dmin,int lx,void *extra_info)
*/


/*==================================================================
    Function:   SAD_Macroblock
    Date:       09/07/2000
    Purpose:    Compute SAD 16x16 between blk and ref.
    To do:      Uniform subsampling will be inserted later!
                Hypothesis Testing Fast Matching to be used later!
    Changes:
    11/7/00:    implemented MMX
    1/24/01:    implemented SSE
==================================================================*/
/********** C ************/
int AVCSAD_Macroblock_C(uint8 *ref, uint8 *blk, int dmin_lx, void *extra_info)
{
    (void)(extra_info);

    int32 x10;
    int dmin = (uint32)dmin_lx >> 16;
    int lx = dmin_lx & 0xFFFF;

    NUM_SAD_MB_CALL();

    x10 = simd_sad_mb(ref, blk, dmin, lx);

    return x10;
}

#ifdef HTFM   /* HTFM with uniform subsampling implementation 2/28/01 */
/*===============================================================
    Function:   AVCAVCSAD_MB_HTFM_Collect and AVCSAD_MB_HTFM
    Date:       3/2/1
    Purpose:    Compute the SAD on a 16x16 block using
                uniform subsampling and hypothesis testing fast matching
                for early dropout. SAD_MB_HP_HTFM_Collect is to collect
                the statistics to compute the thresholds to be used in
                SAD_MB_HP_HTFM.
    Input/Output:
    Changes:
  ===============================================================*/

int AVCAVCSAD_MB_HTFM_Collect(uint8 *ref, uint8 *blk, int dmin_lx, void *extra_info)
{
    int i;
    int sad = 0;
    uint8 *p1;
    int lx4 = (dmin_lx << 2) & 0x3FFFC;
    uint32 cur_word;
    int saddata[16], tmp, tmp2;    /* used when collecting flag (global) is on */
    int difmad;
    int madstar;
    HTFM_Stat *htfm_stat = (HTFM_Stat*) extra_info;
    int *abs_dif_mad_avg = &(htfm_stat->abs_dif_mad_avg);
    uint *countbreak = &(htfm_stat->countbreak);
    int *offsetRef = htfm_stat->offsetRef;

    madstar = (uint32)dmin_lx >> 20;

    NUM_SAD_MB_CALL();

    blk -= 4;
    for (i = 0; i < 16; i++)
    {
        p1 = ref + offsetRef[i];
        cur_word = *((uint32*)(blk += 4));
        tmp = p1[12];
        tmp2 = (cur_word >> 24) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[8];
        tmp2 = (cur_word >> 16) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[4];
        tmp2 = (cur_word >> 8) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[0];
        p1 += lx4;
        tmp2 = (cur_word & 0xFF);
        sad = SUB_SAD(sad, tmp, tmp2);

        cur_word = *((uint32*)(blk += 4));
        tmp = p1[12];
        tmp2 = (cur_word >> 24) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[8];
        tmp2 = (cur_word >> 16) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[4];
        tmp2 = (cur_word >> 8) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[0];
        p1 += lx4;
        tmp2 = (cur_word & 0xFF);
        sad = SUB_SAD(sad, tmp, tmp2);

        cur_word = *((uint32*)(blk += 4));
        tmp = p1[12];
        tmp2 = (cur_word >> 24) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[8];
        tmp2 = (cur_word >> 16) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[4];
        tmp2 = (cur_word >> 8) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[0];
        p1 += lx4;
        tmp2 = (cur_word & 0xFF);
        sad = SUB_SAD(sad, tmp, tmp2);

        cur_word = *((uint32*)(blk += 4));
        tmp = p1[12];
        tmp2 = (cur_word >> 24) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[8];
        tmp2 = (cur_word >> 16) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[4];
        tmp2 = (cur_word >> 8) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[0];
        p1 += lx4;
        tmp2 = (cur_word & 0xFF);
        sad = SUB_SAD(sad, tmp, tmp2);

        NUM_SAD_MB();

        saddata[i] = sad;

        if (i > 0)
        {
            if ((uint32)sad > ((uint32)dmin_lx >> 16))
            {
                difmad = saddata[0] - ((saddata[1] + 1) >> 1);
                (*abs_dif_mad_avg) += ((difmad > 0) ? difmad : -difmad);
                (*countbreak)++;
                return sad;
            }
        }
    }

    difmad = saddata[0] - ((saddata[1] + 1) >> 1);
    (*abs_dif_mad_avg) += ((difmad > 0) ? difmad : -difmad);
    (*countbreak)++;
    return sad;
}

int AVCSAD_MB_HTFM(uint8 *ref, uint8 *blk, int dmin_lx, void *extra_info)
{
    int sad = 0;
    uint8 *p1;

    int i;
    int tmp, tmp2;
    int lx4 = (dmin_lx << 2) & 0x3FFFC;
    int sadstar = 0, madstar;
    int *nrmlz_th = (int*) extra_info;
    int *offsetRef = (int*) extra_info + 32;
    uint32 cur_word;

    madstar = (uint32)dmin_lx >> 20;

    NUM_SAD_MB_CALL();

    blk -= 4;
    for (i = 0; i < 16; i++)
    {
        p1 = ref + offsetRef[i];
        cur_word = *((uint32*)(blk += 4));
        tmp = p1[12];
        tmp2 = (cur_word >> 24) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[8];
        tmp2 = (cur_word >> 16) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[4];
        tmp2 = (cur_word >> 8) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[0];
        p1 += lx4;
        tmp2 = (cur_word & 0xFF);
        sad = SUB_SAD(sad, tmp, tmp2);

        cur_word = *((uint32*)(blk += 4));
        tmp = p1[12];
        tmp2 = (cur_word >> 24) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[8];
        tmp2 = (cur_word >> 16) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[4];
        tmp2 = (cur_word >> 8) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[0];
        p1 += lx4;
        tmp2 = (cur_word & 0xFF);
        sad = SUB_SAD(sad, tmp, tmp2);

        cur_word = *((uint32*)(blk += 4));
        tmp = p1[12];
        tmp2 = (cur_word >> 24) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[8];
        tmp2 = (cur_word >> 16) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[4];
        tmp2 = (cur_word >> 8) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[0];
        p1 += lx4;
        tmp2 = (cur_word & 0xFF);
        sad = SUB_SAD(sad, tmp, tmp2);

        cur_word = *((uint32*)(blk += 4));
        tmp = p1[12];
        tmp2 = (cur_word >> 24) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[8];
        tmp2 = (cur_word >> 16) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[4];
        tmp2 = (cur_word >> 8) & 0xFF;
        sad = SUB_SAD(sad, tmp, tmp2);
        tmp = p1[0];
        p1 += lx4;
        tmp2 = (cur_word & 0xFF);
        sad = SUB_SAD(sad, tmp, tmp2);

        NUM_SAD_MB();

        sadstar += madstar;
        if (((uint32)sad <= ((uint32)dmin_lx >> 16)) && (sad <= (sadstar - *nrmlz_th++)))
            ;
        else
            return 65536;
    }

    return sad;
}
#endif /* HTFM */