Coverage Report

Created: 2025-07-11 06:23

/src/libmpeg2/common/ideint.c
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *
3
 * Copyright (C) 2015 The Android Open Source Project
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at:
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 *****************************************************************************
18
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19
*/
20
/**
21
*******************************************************************************
22
* @file
23
*  ideint_api.c
24
*
25
* @brief
26
*  This file contains the definitions of the core  processing of the de-
27
* interlacer.
28
*
29
* @author
30
*  Ittiam
31
*
32
* @par List of Functions:
33
*
34
* @remarks
35
*  None
36
*
37
*******************************************************************************
38
*/
39
/*****************************************************************************/
40
/* File Includes                                                             */
41
/*****************************************************************************/
42
/* System include files */
43
#include <stdio.h>
44
#include <stdint.h>
45
#include <string.h>
46
#include <stdlib.h>
47
#include <assert.h>
48
49
/* User include files */
50
#include "icv_datatypes.h"
51
#include "icv_macros.h"
52
#include "icv_platform_macros.h"
53
#include "icv.h"
54
#include "icv_variance.h"
55
#include "icv_sad.h"
56
#include "ideint.h"
57
58
#include "ideint_defs.h"
59
#include "ideint_structs.h"
60
61
#include "ideint_utils.h"
62
#include "ideint_cac.h"
63
#include "ideint_debug.h"
64
#include "ideint_function_selector.h"
65
66
/**
67
*******************************************************************************
68
*
69
* @brief
70
*  Return deinterlacer context size
71
*
72
* @par   Description
73
*  Return deinterlacer context size, application will allocate this memory
74
*  and send it as context to process call
75
*
76
* @param[in] None
77
*
78
* @returns
79
* Size of deinterlacer context
80
*
81
* @remarks
82
* None
83
*
84
*******************************************************************************
85
*/
86
WORD32 ideint_ctxt_size(void)
87
11.2k
{
88
11.2k
    return sizeof(ctxt_t);
89
11.2k
}
90
91
/**
92
*******************************************************************************
93
*
94
* @brief
95
* Deinterlace given fields and produce a frame
96
*
97
* @par   Description
98
*  Deinterlacer function that deinterlaces given fields and produces a frame
99
*
100
* @param[in] pv_ctxt
101
*  Deinterlacer context returned by ideint_create()
102
*
103
* @param[in] ps_prv_fld
104
*  Previous field (can be null, in which case spatial filtering is done
105
*  unconditionally)
106
*
107
* @param[in] ps_cur_fld
108
*  Current field
109
*
110
* @param[in] ps_nxt_fld
111
*  Next field
112
*
113
* @param[in] ps_out_frm
114
*  Output frame
115
*
116
* @param[in] ps_params
117
*  Parameters
118
*
119
* @param[in] start_row
120
*  Start row
121
*
122
* @param[in] num_rows
123
*  Number of rows to be processed
124
*
125
* @returns
126
*  IDEINT_ERROR_T
127
*
128
* @remarks
129
*
130
*******************************************************************************
131
*/
132
IDEINT_ERROR_T ideint_process(void *pv_ctxt,
133
                              icv_pic_t *ps_prv_fld,
134
                              icv_pic_t *ps_cur_fld,
135
                              icv_pic_t *ps_nxt_fld,
136
                              icv_pic_t *ps_out_frm,
137
                              ideint_params_t *ps_params,
138
                              WORD32 start_row,
139
                              WORD32 num_rows)
140
88.1k
{
141
88.1k
    ctxt_t *ps_ctxt;
142
88.1k
    WORD32 num_blks_x, num_blks_y;
143
88.1k
    WORD32 num_comp;
144
88.1k
    WORD32 i, row, col;
145
88.1k
    WORD32 rows_remaining;
146
147
88.1k
    if(NULL == pv_ctxt)
148
0
        return IDEINT_INVALID_CTXT;
149
150
88.1k
    ps_ctxt = (ctxt_t *)pv_ctxt;
151
152
    /* Copy the parameters */
153
88.1k
    if(ps_params)
154
88.1k
    {
155
88.1k
        ps_ctxt->s_params = *ps_params;
156
88.1k
    }
157
0
    else
158
0
    {
159
        /* Use default params if ps_params is NULL */
160
0
        ps_ctxt->s_params.i4_cur_fld_top = 1;
161
0
        ps_ctxt->s_params.e_mode = IDEINT_MODE_SPATIAL;
162
0
        ps_ctxt->s_params.e_arch = ideint_default_arch();
163
0
        ps_ctxt->s_params.e_soc = ICV_SOC_GENERIC;
164
0
        ps_ctxt->s_params.i4_disable_weave = 0;
165
0
        ps_ctxt->s_params.pf_aligned_alloc = NULL;
166
0
        ps_ctxt->s_params.pf_aligned_free = NULL;
167
0
    }
168
169
    /* Start row has to be multiple of 8 */
170
88.1k
    if(start_row & 0x7)
171
0
    {
172
0
        return IDEINT_START_ROW_UNALIGNED;
173
0
    }
174
175
    /* Initialize variances */
176
88.1k
    ps_ctxt->ai4_vrnc_avg_fb[0] = VAR_AVG_LUMA;
177
88.1k
    ps_ctxt->ai4_vrnc_avg_fb[1] = VAR_AVG_CHROMA;
178
88.1k
    ps_ctxt->ai4_vrnc_avg_fb[2] = VAR_AVG_CHROMA;
179
180
88.1k
    ideint_init_function_ptr(ps_ctxt);
181
182
88.1k
    rows_remaining = ps_out_frm->ai4_ht[0] - start_row;
183
88.1k
    num_rows = MIN(num_rows,
184
88.1k
                                        rows_remaining);
185
186
88.1k
    IDEINT_CORRUPT_PIC(ps_out_frm, 0xCD);
187
188
    //Weave two fields to get a frame
189
88.1k
    if(IDEINT_MODE_WEAVE == ps_ctxt->s_params.e_mode)
190
0
    {
191
0
        if(0 == ps_ctxt->s_params.i4_disable_weave)
192
0
        {
193
0
            if(ps_ctxt->s_params.i4_cur_fld_top)
194
0
                ideint_weave_pic(ps_cur_fld, ps_nxt_fld, ps_out_frm,
195
0
                                 start_row,
196
0
                                 num_rows);
197
0
            else
198
0
                ideint_weave_pic(ps_nxt_fld, ps_cur_fld, ps_out_frm,
199
0
                                 start_row,
200
0
                                 num_rows);
201
0
        }
202
0
        return IDEINT_ERROR_NONE;
203
0
    }
204
205
88.1k
    num_comp = 3;
206
207
352k
    for(i = 0; i < num_comp; i++)
208
264k
    {
209
264k
        UWORD8 *pu1_prv = NULL, *pu1_out;
210
264k
        UWORD8 *pu1_top, *pu1_bot, *pu1_dst;
211
264k
        WORD32 cur_strd, out_strd, dst_strd;
212
213
264k
        WORD32 st_thresh;
214
264k
        WORD32 vrnc_avg_st;
215
264k
        WORD32 disable_cac_sad;
216
264k
        WORD32 comp_row_start, comp_row_end;
217
264k
        num_blks_x = ALIGN8(ps_out_frm->ai4_wd[i]) >> 3;
218
264k
        num_blks_y = ALIGN8(ps_out_frm->ai4_ht[i]) >> 3;
219
264k
        comp_row_start = start_row;
220
264k
        comp_row_end = comp_row_start + num_rows;
221
222
264k
        if(i)
223
176k
        {
224
176k
            comp_row_start >>= 1;
225
176k
            comp_row_end >>= 1;
226
176k
        }
227
228
264k
        comp_row_end = MIN(comp_row_end, ps_out_frm->ai4_ht[i]);
229
230
264k
        comp_row_start =  ALIGN8(comp_row_start) >> 3;
231
264k
        comp_row_end  = ALIGN8(comp_row_end) >> 3;
232
264k
        st_thresh        = ST_THRESH;
233
264k
        vrnc_avg_st      = VAR_AVG_LUMA;
234
235
264k
        if(i)
236
176k
        {
237
176k
            st_thresh = ST_THRESH >> 1;
238
176k
            vrnc_avg_st = VAR_AVG_CHROMA;
239
176k
        }
240
241
264k
        out_strd = ps_out_frm->ai4_strd[i];
242
264k
        if(ps_ctxt->s_params.i4_cur_fld_top)
243
88.1k
        {
244
88.1k
            cur_strd = ps_cur_fld->ai4_strd[i];
245
88.1k
        }
246
175k
        else
247
175k
        {
248
175k
            cur_strd = ps_nxt_fld->ai4_strd[i];
249
175k
        }
250
251
252
264k
        disable_cac_sad = 0;
253
        /* If previous field is not provided, then change to SPATIAL mode */
254
264k
        if(ps_prv_fld->apu1_buf[i] == NULL)
255
97.7k
        {
256
97.7k
            disable_cac_sad = 1;
257
97.7k
        }
258
1.71M
        for(row = comp_row_start; row < comp_row_end; row++)
259
1.45M
        {
260
1.45M
            pu1_out = ps_out_frm->apu1_buf[i];
261
1.45M
            pu1_out += (ps_out_frm->ai4_strd[i] * row << 3);
262
263
1.45M
            if(0 == disable_cac_sad)
264
825k
            {
265
825k
                pu1_prv = ps_prv_fld->apu1_buf[i];
266
825k
                pu1_prv += (ps_prv_fld->ai4_strd[i] * row << 2);
267
825k
            }
268
269
1.45M
            if(ps_ctxt->s_params.i4_cur_fld_top)
270
473k
            {
271
473k
                pu1_top = ps_cur_fld->apu1_buf[i];
272
473k
                pu1_bot = ps_nxt_fld->apu1_buf[i];
273
473k
            }
274
978k
            else
275
978k
            {
276
978k
                pu1_top = ps_nxt_fld->apu1_buf[i];
277
978k
                pu1_bot = ps_cur_fld->apu1_buf[i];
278
978k
            }
279
1.45M
            pu1_top += (cur_strd * row << 2);
280
1.45M
            pu1_bot += (cur_strd * row << 2);
281
282
113M
            for(col = 0; col < num_blks_x; col++)
283
112M
            {
284
112M
                WORD32 cac, sad, vrnc;
285
112M
                WORD32 th_num, th_den;
286
112M
                UWORD8 au1_dst[BLK_WD * BLK_HT];
287
112M
                WORD32 blk_wd, blk_ht;
288
112M
                WORD32 input_boundary;
289
112M
                cac = 0;
290
112M
                sad = 0;
291
112M
                th_den = 0;
292
112M
                th_num = st_thresh;
293
112M
                vrnc = 0;
294
295
112M
                disable_cac_sad = 0;
296
                /* If previous field is not provided, then change to SPATIAL mode */
297
112M
                if(ps_prv_fld->apu1_buf[i] == NULL)
298
46.4M
                {
299
46.4M
                    disable_cac_sad = 1;
300
46.4M
                }
301
                /* For boundary blocks when input dimensions are not multiple of 8,
302
                 * then change to spatial mode */
303
112M
                input_boundary = 0;
304
305
112M
                blk_wd = BLK_WD;
306
112M
                blk_ht = BLK_HT;
307
308
112M
                if((((num_blks_x - 1) == col) && (ps_out_frm->ai4_wd[i] & 0x7)) ||
309
112M
                    (((num_blks_y - 1) == row) && (ps_out_frm->ai4_ht[i] & 0x7)))
310
1.06M
                {
311
1.06M
                    disable_cac_sad = 1;
312
1.06M
                    input_boundary = 1;
313
314
1.06M
                    if(((num_blks_x - 1) == col) && (ps_out_frm->ai4_wd[i] & 0x7))
315
156k
                        blk_wd = (ps_out_frm->ai4_wd[i] & 0x7);
316
317
1.06M
                    if(((num_blks_y - 1) == row) && (ps_out_frm->ai4_ht[i] & 0x7))
318
911k
                        blk_ht = (ps_out_frm->ai4_ht[i] & 0x7);
319
320
1.06M
                }
321
322
112M
                if(0 == disable_cac_sad)
323
64.7M
                {
324
                    /* Compute SAD */
325
64.7M
                    PROFILE_DISABLE_SAD
326
64.7M
                    sad = ps_ctxt->pf_sad_8x4(pu1_prv, pu1_bot, cur_strd,
327
64.7M
                                              cur_strd,
328
64.7M
                                              BLK_WD,
329
64.7M
                                              BLK_HT >> 1);
330
                    /* Compute Variance */
331
64.7M
                    PROFILE_DISABLE_VARIANCE
332
64.7M
                    vrnc = ps_ctxt->pf_variance_8x4(pu1_top, cur_strd, BLK_WD,
333
64.7M
                                                    BLK_HT >> 1);
334
335
64.7M
                    th_num = st_thresh;
336
337
64.7M
                    th_num *= vrnc_avg_st +
338
64.7M
                              ((MOD_IDX_ST_NUM * vrnc) >> MOD_IDX_ST_SHIFT);
339
340
64.7M
                    th_den = vrnc +
341
64.7M
                             ((MOD_IDX_ST_NUM * vrnc_avg_st) >> MOD_IDX_ST_SHIFT);
342
343
64.7M
                    if((sad * th_den) <= th_num)
344
29.2M
                    {
345
                        /* Calculate Combing Artifact if SAD test fails */
346
29.2M
                        PROFILE_DISABLE_CAC
347
29.2M
                        cac = ps_ctxt->pf_cac_8x8(pu1_top, pu1_bot, cur_strd, cur_strd);
348
29.2M
                    }
349
64.7M
                }
350
351
112M
                pu1_dst = pu1_out;
352
112M
                dst_strd = out_strd;
353
354
                /* In case boundary blocks are not complete (dimensions non-multiple of 8)
355
                 * Use intermediate buffer as destination and copy required pixels to output
356
                 * buffer later
357
                 */
358
112M
                if(input_boundary)
359
1.06M
                {
360
1.06M
                    pu1_dst = au1_dst;
361
1.06M
                    dst_strd = BLK_WD;
362
1.06M
                    ideint_weave_blk(pu1_top, pu1_bot, pu1_dst, dst_strd,
363
1.06M
                                     cur_strd, blk_wd, blk_ht);
364
1.06M
                }
365
366
                /* Weave the two fields unconditionally */
367
112M
                if(0 == ps_ctxt->s_params.i4_disable_weave)
368
105M
                {
369
105M
                    ideint_weave_blk(pu1_top, pu1_bot, pu1_dst, dst_strd,
370
105M
                                     cur_strd, blk_wd, blk_ht);
371
105M
                }
372
373
112M
                if(disable_cac_sad || cac || (sad * th_den > th_num))
374
81.8M
                {
375
                    /* Pad the input fields in an intermediate buffer if required */
376
81.8M
                    if((0 == row) || (0 == col) ||
377
81.8M
                       ((num_blks_x - 1) == col) || ((num_blks_y - 1) == row))
378
3.92M
                    {
379
3.92M
                        UWORD8 *pu1_dst_top;
380
3.92M
                        UWORD8 au1_pad[(BLK_HT + 4) * (BLK_WD + 4)];
381
382
3.92M
                        ideint_pad_blk(pu1_top, pu1_bot, au1_pad, cur_strd, row,
383
3.92M
                                       col, num_blks_y, num_blks_x, blk_wd, blk_ht);
384
385
3.92M
                        pu1_dst_top = au1_pad + 2 * (BLK_WD + 4) + 2;
386
387
3.92M
                        PROFILE_DISABLE_SPATIAL
388
3.92M
                        ps_ctxt->pf_spatial_filter(pu1_dst_top, pu1_dst + dst_strd,
389
3.92M
                                                   (BLK_WD + 4) * 2,
390
3.92M
                                                   dst_strd * 2);
391
3.92M
                    }
392
77.9M
                    else
393
77.9M
                    {
394
77.9M
                        PROFILE_DISABLE_SPATIAL
395
77.9M
                        ps_ctxt->pf_spatial_filter(pu1_top, pu1_dst + dst_strd,
396
77.9M
                                                   cur_strd, dst_strd * 2);
397
398
77.9M
                    }
399
81.8M
                }
400
401
                /* copy required pixels to output buffer for boundary blocks
402
                 * when dimensions are not multiple of 8
403
                 */
404
112M
                if(input_boundary)
405
1.06M
                {
406
1.06M
                    WORD32 j;
407
408
5.98M
                    for(j = 0; j < blk_ht; j++)
409
4.91M
                    {
410
4.91M
                        memcpy(pu1_out + j * out_strd, au1_dst + j * BLK_WD, blk_wd);
411
4.91M
                    }
412
1.06M
                }
413
112M
                if(NULL != pu1_prv)
414
65.1M
                {
415
65.1M
                    pu1_prv += 8;
416
65.1M
                }
417
112M
                pu1_top += 8;
418
112M
                pu1_bot += 8;
419
112M
                pu1_out += 8;
420
112M
            }
421
1.45M
        }
422
264k
    }
423
88.1k
    return IDEINT_ERROR_NONE;
424
88.1k
}