/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 | 12.4k | { |
88 | 12.4k | return sizeof(ctxt_t); |
89 | 12.4k | } |
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 | 63.1k | { |
141 | 63.1k | ctxt_t *ps_ctxt; |
142 | 63.1k | WORD32 num_blks_x, num_blks_y; |
143 | 63.1k | WORD32 num_comp; |
144 | 63.1k | WORD32 i, row, col; |
145 | 63.1k | WORD32 rows_remaining; |
146 | | |
147 | 63.1k | if(NULL == pv_ctxt) |
148 | 0 | return IDEINT_INVALID_CTXT; |
149 | | |
150 | 63.1k | ps_ctxt = (ctxt_t *)pv_ctxt; |
151 | | |
152 | | /* Copy the parameters */ |
153 | 63.1k | if(ps_params) |
154 | 63.1k | { |
155 | 63.1k | ps_ctxt->s_params = *ps_params; |
156 | 63.1k | } |
157 | 18.4E | else |
158 | 18.4E | { |
159 | | /* Use default params if ps_params is NULL */ |
160 | 18.4E | ps_ctxt->s_params.i4_cur_fld_top = 1; |
161 | 18.4E | ps_ctxt->s_params.e_mode = IDEINT_MODE_SPATIAL; |
162 | 18.4E | ps_ctxt->s_params.e_arch = ideint_default_arch(); |
163 | 18.4E | ps_ctxt->s_params.e_soc = ICV_SOC_GENERIC; |
164 | 18.4E | ps_ctxt->s_params.i4_disable_weave = 0; |
165 | 18.4E | ps_ctxt->s_params.pf_aligned_alloc = NULL; |
166 | 18.4E | ps_ctxt->s_params.pf_aligned_free = NULL; |
167 | 18.4E | } |
168 | | |
169 | | /* Start row has to be multiple of 8 */ |
170 | 63.1k | if(start_row & 0x7) |
171 | 0 | { |
172 | 0 | return IDEINT_START_ROW_UNALIGNED; |
173 | 0 | } |
174 | | |
175 | | /* Initialize variances */ |
176 | 63.1k | ps_ctxt->ai4_vrnc_avg_fb[0] = VAR_AVG_LUMA; |
177 | 63.1k | ps_ctxt->ai4_vrnc_avg_fb[1] = VAR_AVG_CHROMA; |
178 | 63.1k | ps_ctxt->ai4_vrnc_avg_fb[2] = VAR_AVG_CHROMA; |
179 | | |
180 | 63.1k | ideint_init_function_ptr(ps_ctxt); |
181 | | |
182 | 63.1k | rows_remaining = ps_out_frm->ai4_ht[0] - start_row; |
183 | 63.1k | num_rows = MIN(num_rows, |
184 | 63.1k | rows_remaining); |
185 | | |
186 | 63.1k | IDEINT_CORRUPT_PIC(ps_out_frm, 0xCD); |
187 | | |
188 | | //Weave two fields to get a frame |
189 | 63.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 | 63.1k | num_comp = 3; |
206 | | |
207 | 252k | for(i = 0; i < num_comp; i++) |
208 | 189k | { |
209 | 189k | UWORD8 *pu1_prv = NULL, *pu1_out; |
210 | 189k | UWORD8 *pu1_top, *pu1_bot, *pu1_dst; |
211 | 189k | WORD32 cur_strd, out_strd, dst_strd; |
212 | | |
213 | 189k | WORD32 st_thresh; |
214 | 189k | WORD32 vrnc_avg_st; |
215 | 189k | WORD32 disable_cac_sad; |
216 | 189k | WORD32 comp_row_start, comp_row_end; |
217 | 189k | num_blks_x = ALIGN8(ps_out_frm->ai4_wd[i]) >> 3; |
218 | 189k | num_blks_y = ALIGN8(ps_out_frm->ai4_ht[i]) >> 3; |
219 | 189k | comp_row_start = start_row; |
220 | 189k | comp_row_end = comp_row_start + num_rows; |
221 | | |
222 | 189k | if(i) |
223 | 126k | { |
224 | 126k | comp_row_start >>= 1; |
225 | 126k | comp_row_end >>= 1; |
226 | 126k | } |
227 | | |
228 | 189k | comp_row_end = MIN(comp_row_end, ps_out_frm->ai4_ht[i]); |
229 | | |
230 | 189k | comp_row_start = ALIGN8(comp_row_start) >> 3; |
231 | 189k | comp_row_end = ALIGN8(comp_row_end) >> 3; |
232 | 189k | st_thresh = ST_THRESH; |
233 | 189k | vrnc_avg_st = VAR_AVG_LUMA; |
234 | | |
235 | 189k | if(i) |
236 | 126k | { |
237 | 126k | st_thresh = ST_THRESH >> 1; |
238 | 126k | vrnc_avg_st = VAR_AVG_CHROMA; |
239 | 126k | } |
240 | | |
241 | 189k | out_strd = ps_out_frm->ai4_strd[i]; |
242 | 189k | if(ps_ctxt->s_params.i4_cur_fld_top) |
243 | 97.7k | { |
244 | 97.7k | cur_strd = ps_cur_fld->ai4_strd[i]; |
245 | 97.7k | } |
246 | 91.4k | else |
247 | 91.4k | { |
248 | 91.4k | cur_strd = ps_nxt_fld->ai4_strd[i]; |
249 | 91.4k | } |
250 | | |
251 | | |
252 | 189k | disable_cac_sad = 0; |
253 | | /* If previous field is not provided, then change to SPATIAL mode */ |
254 | 189k | if(ps_prv_fld->apu1_buf[i] == NULL) |
255 | 68.4k | { |
256 | 68.4k | disable_cac_sad = 1; |
257 | 68.4k | } |
258 | 1.24M | for(row = comp_row_start; row < comp_row_end; row++) |
259 | 1.05M | { |
260 | 1.05M | pu1_out = ps_out_frm->apu1_buf[i]; |
261 | 1.05M | pu1_out += (ps_out_frm->ai4_strd[i] * row << 3); |
262 | | |
263 | 1.05M | if(0 == disable_cac_sad) |
264 | 648k | { |
265 | 648k | pu1_prv = ps_prv_fld->apu1_buf[i]; |
266 | 648k | pu1_prv += (ps_prv_fld->ai4_strd[i] * row << 2); |
267 | 648k | } |
268 | | |
269 | 1.05M | if(ps_ctxt->s_params.i4_cur_fld_top) |
270 | 521k | { |
271 | 521k | pu1_top = ps_cur_fld->apu1_buf[i]; |
272 | 521k | pu1_bot = ps_nxt_fld->apu1_buf[i]; |
273 | 521k | } |
274 | 538k | else |
275 | 538k | { |
276 | 538k | pu1_top = ps_nxt_fld->apu1_buf[i]; |
277 | 538k | pu1_bot = ps_cur_fld->apu1_buf[i]; |
278 | 538k | } |
279 | 1.05M | pu1_top += (cur_strd * row << 2); |
280 | 1.05M | pu1_bot += (cur_strd * row << 2); |
281 | | |
282 | 159M | for(col = 0; col < num_blks_x; col++) |
283 | 158M | { |
284 | 158M | WORD32 cac, sad, vrnc; |
285 | 158M | WORD32 th_num, th_den; |
286 | 158M | UWORD8 au1_dst[BLK_WD * BLK_HT]; |
287 | 158M | WORD32 blk_wd, blk_ht; |
288 | 158M | WORD32 input_boundary; |
289 | 158M | cac = 0; |
290 | 158M | sad = 0; |
291 | 158M | th_den = 0; |
292 | 158M | th_num = st_thresh; |
293 | 158M | vrnc = 0; |
294 | | |
295 | 158M | disable_cac_sad = 0; |
296 | | /* If previous field is not provided, then change to SPATIAL mode */ |
297 | 158M | if(ps_prv_fld->apu1_buf[i] == NULL) |
298 | 62.4M | { |
299 | 62.4M | disable_cac_sad = 1; |
300 | 62.4M | } |
301 | | /* For boundary blocks when input dimensions are not multiple of 8, |
302 | | * then change to spatial mode */ |
303 | 158M | input_boundary = 0; |
304 | | |
305 | 158M | blk_wd = BLK_WD; |
306 | 158M | blk_ht = BLK_HT; |
307 | | |
308 | 158M | if((((num_blks_x - 1) == col) && (ps_out_frm->ai4_wd[i] & 0x7)) || |
309 | 158M | (((num_blks_y - 1) == row) && (ps_out_frm->ai4_ht[i] & 0x7))) |
310 | 1.21M | { |
311 | 1.21M | disable_cac_sad = 1; |
312 | 1.21M | input_boundary = 1; |
313 | | |
314 | 1.21M | if(((num_blks_x - 1) == col) && (ps_out_frm->ai4_wd[i] & 0x7)) |
315 | 19.3k | blk_wd = (ps_out_frm->ai4_wd[i] & 0x7); |
316 | | |
317 | 1.21M | if(((num_blks_y - 1) == row) && (ps_out_frm->ai4_ht[i] & 0x7)) |
318 | 1.19M | blk_ht = (ps_out_frm->ai4_ht[i] & 0x7); |
319 | | |
320 | 1.21M | } |
321 | | |
322 | 158M | if(0 == disable_cac_sad) |
323 | 95.4M | { |
324 | | /* Compute SAD */ |
325 | 95.4M | PROFILE_DISABLE_SAD |
326 | 95.4M | sad = ps_ctxt->pf_sad_8x4(pu1_prv, pu1_bot, cur_strd, |
327 | 95.4M | cur_strd, |
328 | 95.4M | BLK_WD, |
329 | 95.4M | BLK_HT >> 1); |
330 | | /* Compute Variance */ |
331 | 95.4M | PROFILE_DISABLE_VARIANCE |
332 | 95.4M | vrnc = ps_ctxt->pf_variance_8x4(pu1_top, cur_strd, BLK_WD, |
333 | 95.4M | BLK_HT >> 1); |
334 | | |
335 | 95.4M | th_num = st_thresh; |
336 | | |
337 | 95.4M | th_num *= vrnc_avg_st + |
338 | 95.4M | ((MOD_IDX_ST_NUM * vrnc) >> MOD_IDX_ST_SHIFT); |
339 | | |
340 | 95.4M | th_den = vrnc + |
341 | 95.4M | ((MOD_IDX_ST_NUM * vrnc_avg_st) >> MOD_IDX_ST_SHIFT); |
342 | | |
343 | 95.4M | if((sad * th_den) <= th_num) |
344 | 47.6M | { |
345 | | /* Calculate Combing Artifact if SAD test fails */ |
346 | 47.6M | PROFILE_DISABLE_CAC |
347 | 47.6M | cac = ps_ctxt->pf_cac_8x8(pu1_top, pu1_bot, cur_strd, cur_strd); |
348 | 47.6M | } |
349 | 95.4M | } |
350 | | |
351 | 158M | pu1_dst = pu1_out; |
352 | 158M | 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 | 158M | if(input_boundary) |
359 | 1.21M | { |
360 | 1.21M | pu1_dst = au1_dst; |
361 | 1.21M | dst_strd = BLK_WD; |
362 | 1.21M | ideint_weave_blk(pu1_top, pu1_bot, pu1_dst, dst_strd, |
363 | 1.21M | cur_strd, blk_wd, blk_ht); |
364 | 1.21M | } |
365 | | |
366 | | /* Weave the two fields unconditionally */ |
367 | 158M | if(0 == ps_ctxt->s_params.i4_disable_weave) |
368 | 153M | { |
369 | 153M | ideint_weave_blk(pu1_top, pu1_bot, pu1_dst, dst_strd, |
370 | 153M | cur_strd, blk_wd, blk_ht); |
371 | 153M | } |
372 | | |
373 | 158M | if(disable_cac_sad || cac || (sad * th_den > th_num)) |
374 | 111M | { |
375 | | /* Pad the input fields in an intermediate buffer if required */ |
376 | 111M | if((0 == row) || (0 == col) || |
377 | 111M | ((num_blks_x - 1) == col) || ((num_blks_y - 1) == row)) |
378 | 4.18M | { |
379 | 4.18M | UWORD8 *pu1_dst_top; |
380 | 4.18M | UWORD8 au1_pad[(BLK_HT + 4) * (BLK_WD + 4)]; |
381 | | |
382 | 4.18M | ideint_pad_blk(pu1_top, pu1_bot, au1_pad, cur_strd, row, |
383 | 4.18M | col, num_blks_y, num_blks_x, blk_wd, blk_ht); |
384 | | |
385 | 4.18M | pu1_dst_top = au1_pad + 2 * (BLK_WD + 4) + 2; |
386 | | |
387 | 4.18M | PROFILE_DISABLE_SPATIAL |
388 | 4.18M | ps_ctxt->pf_spatial_filter(pu1_dst_top, pu1_dst + dst_strd, |
389 | 4.18M | (BLK_WD + 4) * 2, |
390 | 4.18M | dst_strd * 2); |
391 | 4.18M | } |
392 | 106M | else |
393 | 106M | { |
394 | 106M | PROFILE_DISABLE_SPATIAL |
395 | 106M | ps_ctxt->pf_spatial_filter(pu1_top, pu1_dst + dst_strd, |
396 | 106M | cur_strd, dst_strd * 2); |
397 | | |
398 | 106M | } |
399 | 111M | } |
400 | | |
401 | | /* copy required pixels to output buffer for boundary blocks |
402 | | * when dimensions are not multiple of 8 |
403 | | */ |
404 | 158M | if(input_boundary) |
405 | 1.21M | { |
406 | 1.21M | WORD32 j; |
407 | | |
408 | 5.92M | for(j = 0; j < blk_ht; j++) |
409 | 4.71M | { |
410 | 4.71M | memcpy(pu1_out + j * out_strd, au1_dst + j * BLK_WD, blk_wd); |
411 | 4.71M | } |
412 | 1.21M | } |
413 | 158M | if(NULL != pu1_prv) |
414 | 95.2M | { |
415 | 95.2M | pu1_prv += 8; |
416 | 95.2M | } |
417 | 158M | pu1_top += 8; |
418 | 158M | pu1_bot += 8; |
419 | 158M | pu1_out += 8; |
420 | 158M | } |
421 | 1.05M | } |
422 | 189k | } |
423 | 63.1k | return IDEINT_ERROR_NONE; |
424 | 63.1k | } |