File: filter.c

package info (click to toggle)
vic 2.8ucl4-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,864 kB
  • ctags: 9,033
  • sloc: ansic: 56,989; cpp: 44,560; tcl: 5,550; sh: 1,382; perl: 1,329; makefile: 357
file content (214 lines) | stat: -rw-r--r-- 7,156 bytes parent folder | download | duplicates (5)
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
/************************************************************************
 *
 *  filter.c, filter routines of tmn (TMN encoder)
 *
 *  Copyright (C) 1997  University of BC, Canada
 *
 *  Contacts: 
 *  Michael Gallant                   <mikeg@ee.ubc.ca>
 *  Guy Cote                          <guyc@ee.ubc.ca>
 *  Berna Erol                        <bernae@ee.ubc.ca>
 *
 *  UBC Image Processing Laboratory   http://www.ee.ubc.ca/image
 *  2356 Main Mall                    tel.: +1 604 822 4051
 *  Vancouver BC Canada V6T1Z4        fax.: +1 604 822 5949
 *
 ************************************************************************/

/* Disclaimer of Warranty
 * 
 * These software programs are available to the user without any license fee
 * or royalty on an "as is" basis. The University of British Columbia
 * disclaims any and all warranties, whether express, implied, or
 * statuary, including any implied warranties or merchantability or of
 * fitness for a particular purpose.  In no event shall the
 * copyright-holder be liable for any incidental, punitive, or
 * consequential damages of any kind whatsoever arising from the use of
 * these programs.
 * 
 * This disclaimer of warranty extends to the user of these programs and
 * user's customers, employees, agents, transferees, successors, and
 * assigns.
 * 
 * The University of British Columbia does not represent or warrant that the
 * programs furnished hereunder are free of infringement of any
 * third-party patents.
 * 
 * Commercial implementations of H.263, including shareware, are subject to
 * royalty fees to patent holders.  Many of these patents are general
 * enough such that they are unavoidable regardless of implementation
 * design.
 * 
 */

#include"sim.h"
static int STRENGTH[] = {1,1,2,2,3,3,4,4,4,5,5,6,6,7,7,7,8,8,8,9,9,9,10,10,10,11,11,11,12,12,12};
static int MQ_chroma_QP_table[32] = {0,1,2,3,4,5,6,6,7,8,9,9,10,10,11,11,12,12,12,13,13,13,14,14,14,14,14,15,15,15,15,15};


/**********************************************************************
 *
 *	Name:		EdgeFilter
 *	Description:    performs in the loop edge-filtering on
 *                      reconstructed frames (Deblocking Filter)
 *
 *	Input:	        pointers to reconstructed frame and difference
 *                      image
 *	Returns:
 *	Side effects:
 *
 *      Date: 970820    Author: guyc@ee.ubc.ca
 *                              modified to implement annex J of H.263+
 *
 ***********************************************************************/


void EdgeFilter (PictImage * recon, Pict * pic)
{

  /* Luma */
  HorizEdgeFilter (recon->lum, pels, lines, pic, 0);
  VertEdgeFilter (recon->lum, pels, lines, pic, 0);

  /* Chroma */
  HorizEdgeFilter (recon->Cb, pels / 2, lines / 2, pic, 1);
  VertEdgeFilter (recon->Cb, pels / 2, lines / 2, pic, 1);
  HorizEdgeFilter (recon->Cr, pels / 2, lines / 2, pic, 1);
  VertEdgeFilter (recon->Cr, pels / 2, lines / 2, pic, 1);

  /* that's it */
  return;
}


/***********************************************************************/


void HorizEdgeFilter (unsigned char *rec, int width, int height, Pict * pic, int chr)
{
  int i, j;
  int delta, d1, d2;
  int mbc, mbr, do_filter;
  int QP;
  int bquant[] = {5, 6, 7, 8};
  int mbr_above;

  /* horizontal edges */
  for (j = 8; j < height; j += 8)
  {
    for (i = 0; i < width; i++)
    {
      if (!chr)
      {
        mbr = j >> 4;
        mbc = i >> 4;
        mbr_above = (j - 8) >> 4;
      } else
      {
        mbr = j >> 3;
        mbc = i >> 3;
        mbr_above = mbr - 1;
      }

      do_filter = coded_map[mbr + 1][mbc + 1] || coded_map[mbr_above + 1][mbc + 1];

      if (do_filter)
      {
        if (pic->PB)
        {
          QP = coded_map[mbr + 1][mbc + 1] ?
            mmin (31, bquant[pic->BQUANT] * quant_map[mbr + 1][mbc + 1] / 4) :
            mmin (31, bquant[pic->BQUANT] * quant_map[mbr_above + 1][mbc + 1] / 4);
        } else
          QP = coded_map[mbr + 1][mbc + 1] ? quant_map[mbr + 1][mbc + 1] : quant_map[mbr_above + 1][mbc + 1];

        if (modified_quantization && chr) 
        {
          QP=MQ_chroma_QP_table[QP];                   
        }
        delta = (int) (((int) (*(rec + i + (j - 2) * width)) +
                        (int) (*(rec + i + (j - 1) * width) * (-4)) +
                        (int) (*(rec + i + (j) * width) * (4)) +
                     (int) (*(rec + i + (j + 1) * width) * (-1))) / 8.0);

        d1 = sign (delta) * mmax (0, abs (delta) - mmax (0, 2 * (abs (delta) - STRENGTH[QP - 1])));

        d2 = mmin (abs (d1 / 2), mmax (-abs (d1 / 2), (int) (((*(rec + i + (j - 2) * width) -
                                   *(rec + i + (j + 1) * width))) / 4)));

        *(rec + i + (j + 1) * width) = (int) (*(rec + i + (j + 1) * width)) + d2;
        *(rec + i + (j) * width) =
          mmin (255, mmax (0, (int) (*(rec + i + (j) * width)) - d1));
        *(rec + i + (j - 1) * width) =
          mmin (255, mmax (0, (int) (*(rec + i + (j - 1) * width)) + d1));
        *(rec + i + (j - 2) * width) = (int) (*(rec + i + (j - 2) * width)) - d2;
      }
    }
  }
  return;
}

void VertEdgeFilter (unsigned char *rec, int width, int height, Pict * pic, int chr)
{
  int i, j;
  int delta, d1, d2;
  int mbc, mbr;
  int do_filter;
  int bquant[] = {5, 6, 7, 8};
  int QP;
  int mbc_left;

  /* vertical edges */
  for (i = 8; i < width; i += 8)
  {
    for (j = 0; j < height; j++)
    {
      if (!chr)
      {
        mbr = j >> 4;
        mbc = i >> 4;
        mbc_left = (i - 8) >> 4;
      } else
      {
        mbr = j >> 3;
        mbc = i >> 3;
        mbc_left = mbc - 1;
      }
      do_filter = coded_map[mbr + 1][mbc + 1] || coded_map[mbr + 1][mbc_left + 1];

      if (do_filter)
      {
        if (pic->PB)
        {
          QP = coded_map[mbr + 1][mbc + 1] ?
            mmin (31, bquant[pic->BQUANT] * quant_map[mbr + 1][mbc + 1] / 4) :
            mmin (31, bquant[pic->BQUANT] * quant_map[mbr + 1][mbc_left + 1] / 4);
        } else
          QP = coded_map[mbr + 1][mbc + 1] ? quant_map[mbr + 1][mbc + 1] : quant_map[mbr + 1][mbc_left + 1];

        if (modified_quantization && chr) 
        {
          QP=MQ_chroma_QP_table[QP];                   
        }
        delta = (int) (((int) (*(rec + i - 2 + j * width)) +
                        (int) (*(rec + i - 1 + j * width) * (-4)) +
                        (int) (*(rec + i + j * width) * (4)) +
                        (int) (*(rec + i + 1 + j * width) * (-1))) / 8.0);

        d1 = sign (delta) * mmax (0, abs (delta) - mmax (0, 2 * (abs (delta) - STRENGTH[QP - 1])));
        d2 = mmin (abs (d1 / 2), mmax (-abs (d1 / 2), (int) ((*(rec + i - 2 + j * width) -
                                      *(rec + i + 1 + j * width)) / 4)));

        *(rec + i + 1 + j * width) = (int) (*(rec + i + 1 + j * width)) + d2;
        *(rec + i + j * width) =
          mmin (255, mmax (0, (int) (*(rec + i + j * width)) - d1));
        *(rec + i - 1 + j * width) =
          mmin (255, mmax (0, (int) (*(rec + i - 1 + j * width)) + d1));
        *(rec + i - 2 + j * width) = (int) (*(rec + i - 2 + j * width)) - d2;
      }
    }
  }
  return;
}