Home | History | Annotate | Download | only in src
      1 /* ------------------------------------------------------------------
      2  * Copyright (C) 1998-2009 PacketVideo
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
     13  * express or implied.
     14  * See the License for the specific language governing permissions
     15  * and limitations under the License.
     16  * -------------------------------------------------------------------
     17  */
     18 /*
     19 
     20  Description: Separated modules into one function per file and put into
     21     new template.
     22 
     23  Description: Optimizing C code and adding comments.  Also changing variable
     24     names to make them more meaningful.
     25 
     26  Who:                   Date:
     27  Description:
     28 
     29 ------------------------------------------------------------------------------
     30  INPUT AND OUTPUT DEFINITIONS
     31 
     32  Inputs:
     33 
     34     Rec_Y = pointer to 0th position in buffer containing luminance values
     35         of type uint8.
     36     y_start = value of y coordinate of type int that specifies the first
     37         row of pixels to be used in the filter algorithm.
     38     x_start = value of x coordinate of type int that specifies the first
     39         column of pixels to be used in the filter algorithm.
     40     y_blk_start = value of the y coordinate of type int that specifies the
     41         row of pixels which contains the start of a block. The row
     42         specified by y_blk_start+BLK_SIZE is the last row of pixels
     43         that are used in the filter algorithm.
     44     x_blk_start = value of the x coordinate of type int that specifies the
     45         column of pixels which contains the start of a block.  The
     46         column specified by x_blk_start+BLK_SIZE is the last column of
     47         pixels that are used in the filter algorithm.
     48     thr = value of type int that is compared to the elements in Rec_Y to
     49         determine if a particular value in Rec_Y will be modified by
     50         the filter or not
     51     width = value of type int that specifies the width of the display
     52         in pixels (or pels, equivalently).
     53     max_diff = value of type int that specifies the value that may be added
     54         or subtracted from the pixel in Rec_Y that is being filtered
     55         if the filter algorithm decides to change that particular
     56         pixel's luminance value.
     57 
     58 
     59  Local Stores/Buffers/Pointers Needed:
     60     None
     61 
     62  Global Stores/Buffers/Pointers Needed:
     63     None
     64 
     65  Outputs:
     66     None
     67 
     68  Pointers and Buffers Modified:
     69     Buffer pointed to by Rec_Y is modified with the filtered
     70     luminance values.
     71 
     72  Local Stores Modified:
     73     None
     74 
     75  Global Stores Modified:
     76     None
     77 
     78 ------------------------------------------------------------------------------
     79  FUNCTION DESCRIPTION
     80 
     81  This function implements a motion compensated noise filter using adaptive
     82  weighted averaging of luminance values.  *Rec_Y contains the luminance values
     83  that are being filtered.
     84 
     85  The picture below depicts a 3x3 group of pixel luminance values.  The "u", "c",
     86  and "l" stand for "upper", "center" and "lower", respectively.  The location
     87  of pelc0 is specified by x_start and y_start in the 1-D array "Rec_Y" as
     88  follows (assuming x_start=0):
     89 
     90  location of pelc0 = [(y_start+1) * width] + x_start
     91 
     92  Moving up or down 1 row (moving from pelu2 to pelc2, for example) is done by
     93  incrementing or decrementing "width" elements within Rec_Y.
     94 
     95  The coordinates of the upper left hand corner of a block (not the group of
     96  9 pixels depicted in the figure below) is specified by
     97  (y_blk_start, x_blk_start).  The width and height of the block is BLKSIZE.
     98  (y_start,x_start) may be specified independently of (y_blk_start, x_blk_start).
     99 
    100     (y_start,x_start)
    101  -----------|--------------------------
    102     |   |   |   |   |
    103     |   X   | pelu1 | pelu2 |
    104     | pelu0 |   |   |
    105     |   |   |   |
    106  --------------------------------------
    107     |   |   |   |
    108     | pelc0 | pelc1 | pelc2 |
    109     |   |   |   |
    110     |   |   |   |
    111  --------------------------------------
    112     |   |   |   |
    113     | pell0 | pell1 | pell2 |
    114     |   |   |   |
    115     |   |   |   |
    116  --------------------------------------
    117 
    118  The filtering of the luminance values is achieved by comparing the 9
    119  luminance values to a threshold value ("thr") and then changing the
    120  luminance value of pelc1 if all of the values are above or all of the values
    121  are below the threshold.  The amount that the luminance value is changed
    122  depends on a weighted sum of the 9 luminance values. The position of Pelc1
    123  is then advanced to the right by one (as well as all of the surrounding pixels)
    124  and the same calculation is performed again for the luminance value of the new
    125  Pelc1. This continues row-wise until pixels in the last row of the block are
    126  filtered.
    127 
    128 
    129 ------------------------------------------------------------------------------
    130  REQUIREMENTS
    131 
    132  None.
    133 
    134 ------------------------------------------------------------------------------
    135  REFERENCES
    136 
    137  ..\corelibs\decoder\common\src\post_proc.c
    138 
    139 ------------------------------------------------------------------------------
    140  PSEUDO-CODE
    141 
    142 ------------------------------------------------------------------------------
    143  RESOURCES USED
    144    When the code is written for a specific target processor the
    145      the resources used should be documented below.
    146 
    147  STACK USAGE: [stack count for this module] + [variable to represent
    148           stack usage for each subroutine called]
    149 
    150      where: [stack usage variable] = stack usage for [subroutine
    151          name] (see [filename].ext)
    152 
    153  DATA MEMORY USED: x words
    154 
    155  PROGRAM MEMORY USED: x words
    156 
    157  CLOCK CYCLES: [cycle count equation for this module] + [variable
    158            used to represent cycle count for each subroutine
    159            called]
    160 
    161      where: [cycle count variable] = cycle count for [subroutine
    162         name] (see [filename].ext)
    163 
    164 ------------------------------------------------------------------------------
    165 */
    166 
    167 
    168 /*----------------------------------------------------------------------------
    169 ; INCLUDES
    170 ----------------------------------------------------------------------------*/
    171 #include    "mp4dec_lib.h"
    172 #include    "post_proc.h"
    173 #include    "mp4def.h"
    174 
    175 #define OSCL_DISABLE_WARNING_CONV_POSSIBLE_LOSS_OF_DATA
    176 
    177 /*----------------------------------------------------------------------------
    178 ; MACROS
    179 ; Define module specific macros here
    180 ----------------------------------------------------------------------------*/
    181 
    182 
    183 /*----------------------------------------------------------------------------
    184 ; DEFINES
    185 ; Include all pre-processor statements here. Include conditional
    186 ; compile variables also.
    187 ----------------------------------------------------------------------------*/
    188 
    189 /*----------------------------------------------------------------------------
    190 ; LOCAL FUNCTION DEFINITIONS
    191 ; Function Prototype declaration
    192 ----------------------------------------------------------------------------*/
    193 
    194 /*----------------------------------------------------------------------------
    195 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
    196 ; Variable declaration - defined here and used outside this module
    197 ----------------------------------------------------------------------------*/
    198 
    199 /*----------------------------------------------------------------------------
    200 ; EXTERNAL FUNCTION REFERENCES
    201 ; Declare functions defined elsewhere and referenced in this module
    202 ----------------------------------------------------------------------------*/
    203 
    204 /*----------------------------------------------------------------------------
    205 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
    206 ; Declare variables used in this module but defined elsewhere
    207 ----------------------------------------------------------------------------*/
    208 #ifdef PV_POSTPROC_ON
    209 /*----------------------------------------------------------------------------
    210 ; FUNCTION CODE
    211 ----------------------------------------------------------------------------*/
    212 void AdaptiveSmooth_NoMMX(
    213     uint8 *Rec_Y,       /* i/o  */
    214     int y_start,        /* i    */
    215     int x_start,        /* i    */
    216     int y_blk_start,    /* i    */
    217     int x_blk_start,    /* i    */
    218     int thr,        /* i    */
    219     int width,      /* i    */
    220     int max_diff        /* i    */
    221 )
    222 {
    223 
    224     /*----------------------------------------------------------------------------
    225     ; Define all local variables
    226     ----------------------------------------------------------------------------*/
    227     int  sign_v[15];
    228     int sum_v[15];
    229     int *sum_V_ptr;
    230     int *sign_V_ptr;
    231     uint8 pelu;
    232     uint8 pelc;
    233     uint8 pell;
    234     uint8 *pelp;
    235     uint8 oldrow[15];
    236     int  sum;
    237     int sum1;
    238     uint8 *Rec_Y_ptr;
    239     int32  addr_v;
    240     int row_cntr;
    241     int col_cntr;
    242 
    243     /*----------------------------------------------------------------------------
    244     ; Function body here
    245     ----------------------------------------------------------------------------*/
    246     /*  first row
    247     */
    248     addr_v = (int32)(y_start + 1) * width;  /* y coord of 1st element in the row  /
    249                      /containing pelc pixel /     */
    250     Rec_Y_ptr = &Rec_Y[addr_v + x_start];  /* initializing pointer to
    251                            /  pelc0 position  */
    252     sum_V_ptr = &sum_v[0];  /* initializing pointer to 0th element of array
    253                 /   that will contain weighted sums of pixel
    254                 /   luminance values */
    255     sign_V_ptr = &sign_v[0];  /*  initializing pointer to 0th element of
    256                   /   array that will contain sums that indicate
    257                   /    how many of the 9 pixels are above or below
    258                   /    the threshold value (thr)    */
    259     pelp = &oldrow[0];  /* initializing pointer to the 0th element of array
    260                 /    that will contain current values of pelc that
    261                 /   are saved and used as values of pelu when the
    262                 /   next row of pixels are filtered */
    263 
    264     pelu = *(Rec_Y_ptr - width);  /* assigning value of pelu0 to pelu  */
    265     *pelp++ = pelc = *Rec_Y_ptr; /* assigning value of pelc0 to pelc and
    266                      /  storing this value in pelp which
    267                      /   will be used as value of pelu0 when
    268                      /  next row is filtered */
    269     pell = *(Rec_Y_ptr + width);  /* assigning value of pell0 to pell */
    270     Rec_Y_ptr++; /* advancing pointer from pelc0 to pelc1 */
    271     *sum_V_ptr++ = pelu + (pelc << 1) + pell;  /* weighted sum of pelu0,
    272                          /  pelc0 and pell0  */
    273     /* sum of 0's and 1's (0 if pixel value is below thr, 1 if value
    274     /is above thr)  */
    275     *sign_V_ptr++ = INDEX(pelu, thr) + INDEX(pelc, thr) + INDEX(pell, thr);
    276 
    277 
    278     pelu = *(Rec_Y_ptr - width);  /* assigning value of pelu1 to pelu */
    279     *pelp++ = pelc = *Rec_Y_ptr; /* assigning value of pelc1 to pelc and
    280                      /  storing this value in pelp which
    281                      /  will be used as the value of pelu1 when
    282                      /  next row is filtered */
    283     pell = *(Rec_Y_ptr + width);  /* assigning value of pell1 to pell */
    284     Rec_Y_ptr++;  /* advancing pointer from pelc1 to pelc2 */
    285     *sum_V_ptr++ = pelu + (pelc << 1) + pell; /* weighted sum of pelu1,
    286                         / pelc1 and pell1  */
    287     /* sum of 0's and 1's (0 if pixel value is below thr, 1 if value
    288     /is above thr)  */
    289     *sign_V_ptr++ = INDEX(pelu, thr) + INDEX(pelc, thr) + INDEX(pell, thr);
    290 
    291     /* The loop below performs the filtering for the first row of
    292     /   pixels in the region.  It steps across the remaining pixels in
    293     /   the row and alters the luminance value of pelc1 if necessary,
    294     /   depending on the luminance values of the adjacent pixels*/
    295 
    296     for (col_cntr = (x_blk_start + BLKSIZE - 1) - x_start; col_cntr > 0; col_cntr--)
    297     {
    298         pelu = *(Rec_Y_ptr - width);  /* assigning value of pelu2 to
    299                         /   pelu */
    300         *pelp++ = pelc = *Rec_Y_ptr; /* assigning value of pelc2 to pelc
    301                          / and storing this value in pelp
    302                          / which will be used   as value of pelu2
    303                          / when next row is filtered */
    304         pell = *(Rec_Y_ptr + width); /* assigning value of pell2 to pell */
    305 
    306         /* weighted sum of pelu1, pelc1 and pell1  */
    307         *sum_V_ptr = pelu + (pelc << 1) + pell;
    308         /* sum of 0's and 1's (0 if pixel value is below thr,
    309         /1 if value is above thr)  */
    310         *sign_V_ptr = INDEX(pelu, thr) + INDEX(pelc, thr) +
    311                       INDEX(pell, thr);
    312         /* the value of sum1 indicates how many of the 9 pixels'
    313         /luminance values are above or equal to thr */
    314         sum1 = *(sign_V_ptr - 2) + *(sign_V_ptr - 1) + *sign_V_ptr;
    315 
    316         /* alter the luminance value of pelc1 if all 9 luminance values
    317         /are above or equal to thr or if all 9 values are below thr */
    318         if (sum1 == 0 || sum1 == 9)
    319         {
    320             /* sum is a weighted average of the 9 pixel luminance
    321             /values   */
    322             sum = (*(sum_V_ptr - 2) + (*(sum_V_ptr - 1) << 1) +
    323                    *sum_V_ptr + 8) >> 4;
    324 
    325             Rec_Y_ptr--;  /* move pointer back to pelc1  */
    326             /* If luminance value of pelc1 is larger than
    327             / sum by more than max_diff, then subract max_diff
    328             / from luminance value of pelc1*/
    329             if ((int)(*Rec_Y_ptr - sum) > max_diff)
    330             {
    331                 sum = *Rec_Y_ptr - max_diff;
    332             }
    333             /* If luminance value of pelc1 is smaller than
    334             / sum by more than max_diff, then add max_diff
    335             / to luminance value of pelc1*/
    336             else if ((int)(*Rec_Y_ptr - sum) < -max_diff)
    337             {
    338                 sum = *Rec_Y_ptr + max_diff;
    339             }
    340             *Rec_Y_ptr++ = sum; /* assign value of sum to pelc1
    341                          and advance pointer to pelc2 */
    342         }
    343         Rec_Y_ptr++; /* advance pointer to new value of pelc2
    344                  /   old pelc2 is now treated as pelc1*/
    345         sum_V_ptr++; /* pointer is advanced so next weighted sum may
    346                  /  be saved */
    347         sign_V_ptr++; /* pointer is advanced so next sum of 0's and
    348                   / 1's may be saved  */
    349     }
    350 
    351     /* The nested loops below perform the filtering for the remaining rows */
    352 
    353     addr_v = (y_start + 2) * width;  /* advance addr_v to the next row
    354                      /   (corresponding to pell0)*/
    355     /* The outer loop steps throught the rows.   */
    356     for (row_cntr = (y_blk_start + BLKSIZE) - (y_start + 2); row_cntr > 0; row_cntr--)
    357     {
    358         Rec_Y_ptr = &Rec_Y[addr_v + x_start]; /* advance pointer to
    359             /the old pell0, which has become the new pelc0 */
    360         addr_v += width;  /* move addr_v down 1 row */
    361         sum_V_ptr = &sum_v[0];  /* re-initializing pointer */
    362         sign_V_ptr = &sign_v[0];  /* re-initilaizing pointer */
    363         pelp = &oldrow[0]; /* re-initializing pointer */
    364 
    365         pelu = *pelp; /* setting pelu0 to old value of pelc0 */
    366         *pelp++ = pelc = *Rec_Y_ptr;
    367         pell = *(Rec_Y_ptr + width);
    368         Rec_Y_ptr++;
    369         *sum_V_ptr++ = pelu + (pelc << 1) + pell;
    370         *sign_V_ptr++ = INDEX(pelu, thr) + INDEX(pelc, thr) +
    371                         INDEX(pell, thr);
    372 
    373         pelu = *pelp; /* setting pelu1 to old value of pelc1 */
    374         *pelp++ = pelc = *Rec_Y_ptr;
    375         pell = *(Rec_Y_ptr + width);
    376         Rec_Y_ptr++;
    377         *sum_V_ptr++ = pelu + (pelc << 1) + pell;
    378         *sign_V_ptr++ = INDEX(pelu, thr) + INDEX(pelc, thr) +
    379                         INDEX(pell, thr);
    380         /* The inner loop steps through the columns */
    381         for (col_cntr = (x_blk_start + BLKSIZE - 1) - x_start; col_cntr > 0; col_cntr--)
    382         {
    383             pelu = *pelp; /* setting pelu2 to old value of pelc2 */
    384             *pelp++ = pelc = *Rec_Y_ptr;
    385             pell = *(Rec_Y_ptr + width);
    386 
    387             *sum_V_ptr = pelu + (pelc << 1) + pell;
    388             *sign_V_ptr = INDEX(pelu, thr) + INDEX(pelc, thr) +
    389                           INDEX(pell, thr);
    390 
    391             sum1 = *(sign_V_ptr - 2) + *(sign_V_ptr - 1) + *sign_V_ptr;
    392             /* the "if" statement below is the same as the one in
    393             / the first loop */
    394             if (sum1 == 0 || sum1 == 9)
    395             {
    396                 sum = (*(sum_V_ptr - 2) + (*(sum_V_ptr - 1) << 1) +
    397                        *sum_V_ptr + 8) >> 4;
    398 
    399                 Rec_Y_ptr--;
    400                 if ((int)(*Rec_Y_ptr - sum) > max_diff)
    401                 {
    402                     sum = *Rec_Y_ptr - max_diff;
    403                 }
    404                 else if ((int)(*Rec_Y_ptr - sum) < -max_diff)
    405                 {
    406                     sum = *Rec_Y_ptr + max_diff;
    407                 }
    408                 *Rec_Y_ptr++ = (uint8) sum;
    409             }
    410             Rec_Y_ptr++;
    411             sum_V_ptr++;
    412             sign_V_ptr++;
    413         }
    414     }
    415 
    416     /*----------------------------------------------------------------------------
    417     ; Return nothing or data or data pointer
    418     ----------------------------------------------------------------------------*/
    419     return;
    420 }
    421 #endif
    422