lin
2025-08-01 633231e833e21d5b8b1c00cb15aedb62b3b78e8f
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/******************************************************************************
 *
 * Copyright (C) 2015 The Android Open Source Project
 *
 * 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.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
/**
*******************************************************************************
* @file
*  ideint_utils.c
*
* @brief
*  This file contains the definitions of the core  processing of the de
* interlacer.
*
* @author
*  Ittiam
*
* @par List of Functions:
*  ideint_weave_pic()
*  init_bob_indices()
*  ideint_weave_blk()
*  ideint_spatial_filter()
*
* @remarks
*  None
*
*******************************************************************************
*/
/*****************************************************************************/
/* File Includes                                                             */
/*****************************************************************************/
/* System include files */
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
 
 
/* User include files */
#include "icv_datatypes.h"
#include "icv_macros.h"
#include "icv_platform_macros.h"
#include "icv.h"
#include "icv_variance.h"
#include "icv_sad.h"
#include "ideint.h"
#include "ideint_defs.h"
#include "ideint_structs.h"
#include "ideint_utils.h"
#include "ideint_cac.h"
 
/**
*******************************************************************************
*
* @brief
*  Weaves two fields to produce a frame
*
* @par   Description
*  Weaves two fields to produce a frame
*
* @param[in] ps_src_top
*  Top field source
*
* @param[in] ps_src_bot
*  Bottom field source
*
* @param[in] ps_dst_frm
*  Destination frame
*
* @returns
*   0 on Success
*
* @remarks
*
*******************************************************************************
*/
WORD32 ideint_weave_pic(icv_pic_t *ps_src_top,
                        icv_pic_t *ps_src_bot,
                        icv_pic_t *ps_dst_frm,
                        WORD32 start_row,
                        WORD32 num_rows)
{
    UWORD8 *pu1_src, *pu1_dst;
    WORD32 i, j, num_comp;
    icv_pic_t *ps_src_fld;
    WORD32 fld;
    icv_pic_t *ps_src_flds[2];
 
    num_comp = 3;
    ps_src_flds[0] = ps_src_top;
    ps_src_flds[1] = ps_src_bot;
 
    for(fld = 0; fld < 2; fld++)
    {
        ps_src_fld = ps_src_flds[fld];
        for(i = 0; i < num_comp; i++)
        {
            WORD32 src_strd;
            WORD32 dst_strd;
            WORD32 comp_row_start, comp_row_end;
            comp_row_start = start_row;
            comp_row_end = comp_row_start + num_rows;
            if(i)
            {
                comp_row_start >>= 1;
                comp_row_end >>= 1;
            }
 
            comp_row_end = MIN(comp_row_end, ps_dst_frm->ai4_ht[i]);
 
            pu1_src = ps_src_fld->apu1_buf[i];
            pu1_dst = ps_dst_frm->apu1_buf[i];
 
            src_strd = ps_src_fld->ai4_strd[i];
            dst_strd = ps_dst_frm->ai4_strd[i];
 
            /* If source field is bottom, increment destination */
            pu1_dst += fld * dst_strd;
 
            /* In case input and output are pointing to same buffer, then no need to copy */
            if((pu1_src != pu1_dst) || ((2 * dst_strd) != src_strd))
            {
                pu1_dst += ps_dst_frm->ai4_strd[i] * comp_row_start;
                pu1_src += ps_src_fld->ai4_strd[i] * comp_row_start / 2;
 
                for(j = comp_row_start; j < comp_row_end; j += 2)
                {
                    memcpy(pu1_dst, pu1_src, ps_dst_frm->ai4_wd[i]);
                    pu1_dst += ps_dst_frm->ai4_strd[i] * 2;
                    pu1_src += ps_src_fld->ai4_strd[i];
                }
            }
        }
    }
    return 0;
}
 
 
/**
*******************************************************************************
*
* @brief
*  Weaves a 8x8 block
*
* @par   Description
*  Weaves a 8x8 block from two fields
*
* @param[in] pu1_top
*  Top field source
*
* @param[in] pu1_bot
*  Bottom field source
*
* @param[in] pu1_dst
*  Destination
*
* @param[in] dst_strd
*  Destination stride
*
* @param[in] src_strd
*  Source stride
*
* @returns
*  0 on success
*
* @remarks
*
*******************************************************************************
*/
WORD32 ideint_weave_blk(UWORD8 *pu1_top,
                        UWORD8 *pu1_bot,
                        UWORD8 *pu1_dst,
                        WORD32 dst_strd,
                        WORD32 src_strd,
                        WORD32 wd,
                        WORD32 ht)
{
    WORD32 j;
 
    for(j = 0; j < ht; j += 2)
    {
        memcpy(pu1_dst, pu1_top, wd);
        pu1_dst += dst_strd;
        pu1_top += src_strd;
 
        memcpy(pu1_dst, pu1_bot, wd);
        pu1_dst += dst_strd;
        pu1_bot += src_strd;
    }
    return 0;
}
 
/**
*******************************************************************************
*
* @brief
*  Copy a boundary block and pad
*
* @par   Description
*  Copies a block on one of the boundaries and pads
*
* @param[in] pu1_top
*  Top field source
*
* @param[in] pu1_bot
*  Bottom field source
*
* @param[in] pu1_pad
*  Padded destination
*
* @param[in] cur_strd
*  Stride for pu1_top and pu1_bot
*
* @param[in] row
*  Current block's row
*
* @param[in] col
*  Current block's column
*
* @param[in] num_blks_y
*  Number of blocks in Y direction
*
* @param[in] num_blks_x
*  Number of blocks in X direction
 
* @returns
*  None
*
* @remarks
*
*******************************************************************************
*/
void ideint_pad_blk(UWORD8 *pu1_top,
                    UWORD8 *pu1_bot,
                    UWORD8 *pu1_pad,
                    WORD32 cur_strd,
                    WORD32 row,
                    WORD32 col,
                    WORD32 num_blks_y,
                    WORD32 num_blks_x,
                    WORD32 blk_wd,
                    WORD32 blk_ht)
{
    WORD32 i;
    WORD32 num_cols, num_rows;
    UWORD8 *pu1_dst;
    UWORD8 *pu1_src_top;
    UWORD8 *pu1_src_bot;
 
    num_rows = blk_ht + 4;
    num_cols = blk_wd + 4;
 
    pu1_src_top = pu1_top - cur_strd - 2;
    pu1_src_bot = pu1_bot - cur_strd - 2;
    pu1_dst = pu1_pad;
 
    if(0 == col)
    {
        num_cols -= 2;
        pu1_dst += 2;
        pu1_src_top += 2;
        pu1_src_bot += 2;
    }
 
    if(0 == row)
    {
        num_rows -= 2;
        pu1_dst += 2 * (BLK_WD + 4);
        pu1_src_top += cur_strd;
        pu1_src_bot += cur_strd;
    }
 
    if((num_blks_x - 1) == col)
        num_cols -= 2;
 
    if((num_blks_y - 1) == row)
        num_rows -= 2;
 
    for(i = 0; i < num_rows; i += 2)
    {
        memcpy(pu1_dst, pu1_src_top, num_cols);
        pu1_dst += (BLK_WD + 4);
 
        memcpy(pu1_dst, pu1_src_bot, num_cols);
        pu1_dst += (BLK_WD + 4);
 
        pu1_src_top += cur_strd;
        pu1_src_bot += cur_strd;
    }
 
 
    /* Pad Left */
    if(0 == col)
    {
       for(i = 0; i < (BLK_HT + 4); i++)
       {
           WORD32 ofst = i * (BLK_WD + 4) + 2;
           pu1_pad[ofst - 1] = pu1_pad[ofst];
           pu1_pad[ofst - 2] = pu1_pad[ofst];
       }
    }
 
    /* Pad right */
    if((num_blks_x - 1) == col)
    {
       for(i = 0; i < (BLK_HT + 4); i++)
       {
           WORD32 ofst =  i * (BLK_WD + 4) + 2 + blk_wd - 1;
           WORD32 size = (BLK_WD - blk_wd) + 2;
           /* Padding on right should include padding for boundary
            * blocks when width is non-multiple of 8
            */
           memset(&pu1_pad[ofst + 1], pu1_pad[ofst], size);
       }
    }
 
    /* Pad Top */
    if(0 == row)
    {
        WORD32 src_ofst = 2 * (BLK_WD + 4);
        WORD32 dst_ofst = 0;
        memcpy(pu1_pad + dst_ofst, pu1_pad + src_ofst, (BLK_WD + 4));
        src_ofst += (BLK_WD + 4);
        dst_ofst += (BLK_WD + 4);
        memcpy(pu1_pad + dst_ofst, pu1_pad + src_ofst, (BLK_WD + 4));
    }
 
    /* Pad Bottom */
    if((num_blks_y - 1) == row)
    {
        WORD32 src_ofst = (0 + blk_ht) * (BLK_WD + 4);
        WORD32 dst_ofst = (1 + blk_ht) * (BLK_WD + 4);
        WORD32 size = (BLK_HT - blk_ht) + 2;
 
        /* Padding on bottom should include padding for boundary
         * blocks when height is non-multiple of 8
         */
        for(i = 0; i < size; i++)
        {
            memcpy(pu1_pad + dst_ofst, pu1_pad + src_ofst, (BLK_WD + 4));
            dst_ofst += (BLK_WD + 4);
        }
    }
}
 
/**
*******************************************************************************
*
* @brief
*  Performs spatial edge adaptive filtering
*
* @par   Description
*  Performs spatial edge adaptive filtering by detecting edge direction
*
* @param[in] pu1_src
*  Source buffer
*
* @param[in] pu1_out
*  Destination buffer
*
* @param[in] src_strd
*  Source stride
*
* @param[in] out_strd
*  Destination stride
 
* @returns
* None
*
* @remarks
*
*******************************************************************************
*/
void ideint_spatial_filter(UWORD8 *pu1_src,
                           UWORD8 *pu1_out,
                           WORD32 src_strd,
                           WORD32 out_strd)
{
    WORD32 i;
    WORD32 j;
    WORD32 k;
 
    /*********************************************************************/
    /* This loop is for the two halves inside the 8x4 block.             */
    /*********************************************************************/
    for(k = 0; k < 2; k++)
    {
        WORD32 adiff[3] = {0, 0, 0};
        WORD32 shift;
        WORD32 dir_45_le_90, dir_45_le_135, dir_135_le_90;
        UWORD8 *pu1_row_1, *pu1_row_2, *pu1_dst;
 
        /*****************************************************************/
        /* Direction detection                                           */
        /*****************************************************************/
        pu1_row_1 = pu1_src;
        pu1_row_2 = pu1_src + src_strd;
 
        /*****************************************************************/
        /* Calculating the difference along each of the 3 directions.    */
        /*****************************************************************/
        for(j = 0; j < SUB_BLK_HT; j ++)
        {
            for(i = 0; i < SUB_BLK_WD; i++)
            {
                adiff[0] += ABS_DIF(pu1_row_1[i], pu1_row_2[i]); /*  90 */
 
                adiff[1] += ABS_DIF(pu1_row_1[i - 1], pu1_row_2[i + 1]); /* 135 */
 
                adiff[2] += ABS_DIF(pu1_row_1[i + 1], pu1_row_2[i - 1]); /*  45 */
            }
            pu1_row_1 += src_strd;
            pu1_row_2 += src_strd;
        }
 
        /*****************************************************************/
        /* Applying bias, to make the diff comparision more robust.      */
        /*****************************************************************/
        adiff[0] *= EDGE_BIAS_0;
        adiff[1] *= EDGE_BIAS_1;
        adiff[2] *= EDGE_BIAS_1;
 
        /*****************************************************************/
        /* comapring the diffs */
        /*****************************************************************/
        dir_45_le_90  = (adiff[2] <= adiff[0]);
        dir_45_le_135 = (adiff[2] <= adiff[1]);
        dir_135_le_90 = (adiff[1] <= adiff[0]);
 
        /*****************************************************************/
        /* Direction selection. */
        /*****************************************************************/
        shift = 0;
        if(1 == dir_45_le_135)
        {
            if(1 == dir_45_le_90)
                shift = 1;
        }
        else
        {
            if(1 == dir_135_le_90)
                shift = -1;
        }
 
        /*****************************************************************/
        /* Directional interpolation */
        /*****************************************************************/
        pu1_row_1 = pu1_src + shift;
        pu1_row_2 = pu1_src + src_strd - shift;
        pu1_dst   = pu1_out;
 
        for(j = 0; j < SUB_BLK_HT; j++)
        {
            for(i = 0; i < SUB_BLK_WD; i++)
            {
                pu1_dst[i] = (UWORD8)AVG(pu1_row_1[i], pu1_row_2[i]);
            }
            pu1_row_1 += src_strd;
            pu1_row_2 += src_strd;
            pu1_dst   += out_strd;
        }
 
        pu1_out += SUB_BLK_WD;
        pu1_src += SUB_BLK_WD;
    }
}