source: 3DVCSoftware/tags/HTM-DEV-0.1/source/Lib/TLibCommon/TComInterpolationFilter.cpp @ 324

Last change on this file since 324 was 324, checked in by tech, 11 years ago

Initial development version for update to latest HM version.
Includes MV-HEVC and basic extensions for 3D-HEVC.

  • Property svn:eol-style set to native
File size: 13.8 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6 * Copyright (c) 2010-2013, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/**
35 * \file
36 * \brief Implementation of TComInterpolationFilter class
37 */
38
39// ====================================================================================================================
40// Includes
41// ====================================================================================================================
42
43#include "TComRom.h"
44#include "TComInterpolationFilter.h"
45#include <assert.h>
46
47
48//! \ingroup TLibCommon
49//! \{
50
51// ====================================================================================================================
52// Tables
53// ====================================================================================================================
54
55const Short TComInterpolationFilter::m_lumaFilter[4][NTAPS_LUMA] =
56{
57  {  0, 0,   0, 64,  0,   0, 0,  0 },
58  { -1, 4, -10, 58, 17,  -5, 1,  0 },
59  { -1, 4, -11, 40, 40, -11, 4, -1 },
60  {  0, 1,  -5, 17, 58, -10, 4, -1 }
61};
62
63const Short TComInterpolationFilter::m_chromaFilter[8][NTAPS_CHROMA] =
64{
65  {  0, 64,  0,  0 },
66  { -2, 58, 10, -2 },
67  { -4, 54, 16, -2 },
68  { -6, 46, 28, -4 },
69  { -4, 36, 36, -4 },
70  { -4, 28, 46, -6 },
71  { -2, 16, 54, -4 },
72  { -2, 10, 58, -2 }
73};
74
75// ====================================================================================================================
76// Private member functions
77// ====================================================================================================================
78
79/**
80 * \brief Apply unit FIR filter to a block of samples
81 *
82 * \param bitDepth   bitDepth of samples
83 * \param src        Pointer to source samples
84 * \param srcStride  Stride of source samples
85 * \param dst        Pointer to destination samples
86 * \param dstStride  Stride of destination samples
87 * \param width      Width of block
88 * \param height     Height of block
89 * \param isFirst    Flag indicating whether it is the first filtering operation
90 * \param isLast     Flag indicating whether it is the last filtering operation
91 */
92Void TComInterpolationFilter::filterCopy(Int bitDepth, const Pel *src, Int srcStride, Short *dst, Int dstStride, Int width, Int height, Bool isFirst, Bool isLast)
93{
94  Int row, col;
95 
96  if ( isFirst == isLast )
97  {
98    for (row = 0; row < height; row++)
99    {
100      for (col = 0; col < width; col++)
101      {
102        dst[col] = src[col];
103      }
104     
105      src += srcStride;
106      dst += dstStride;
107    }             
108  }
109  else if ( isFirst )
110  {
111    Int shift = IF_INTERNAL_PREC - bitDepth;
112   
113    for (row = 0; row < height; row++)
114    {
115      for (col = 0; col < width; col++)
116      {
117        Short val = src[col] << shift;
118        dst[col] = val - (Short)IF_INTERNAL_OFFS;
119      }
120     
121      src += srcStride;
122      dst += dstStride;
123    }         
124  }
125  else
126  {
127    Int shift = IF_INTERNAL_PREC - bitDepth;
128    Short offset = IF_INTERNAL_OFFS;
129    offset += shift?(1 << (shift - 1)):0;
130    Short maxVal = (1 << bitDepth) - 1;
131    Short minVal = 0;
132    for (row = 0; row < height; row++)
133    {
134      for (col = 0; col < width; col++)
135      {
136        Short val = src[ col ];
137        val = ( val + offset ) >> shift;
138        if (val < minVal) val = minVal;
139        if (val > maxVal) val = maxVal;
140        dst[col] = val;
141      }
142     
143      src += srcStride;
144      dst += dstStride;
145    }             
146  }
147}
148
149/**
150 * \brief Apply FIR filter to a block of samples
151 *
152 * \tparam N          Number of taps
153 * \tparam isVertical Flag indicating filtering along vertical direction
154 * \tparam isFirst    Flag indicating whether it is the first filtering operation
155 * \tparam isLast     Flag indicating whether it is the last filtering operation
156 * \param  bitDepth   Bit depth of samples
157 * \param  src        Pointer to source samples
158 * \param  srcStride  Stride of source samples
159 * \param  dst        Pointer to destination samples
160 * \param  dstStride  Stride of destination samples
161 * \param  width      Width of block
162 * \param  height     Height of block
163 * \param  coeff      Pointer to filter taps
164 */
165template<Int N, Bool isVertical, Bool isFirst, Bool isLast>
166Void TComInterpolationFilter::filter(Int bitDepth, Short const *src, Int srcStride, Short *dst, Int dstStride, Int width, Int height, Short const *coeff)
167{
168  Int row, col;
169 
170  Short c[8];
171  c[0] = coeff[0];
172  c[1] = coeff[1];
173  if ( N >= 4 )
174  {
175    c[2] = coeff[2];
176    c[3] = coeff[3];
177  }
178  if ( N >= 6 )
179  {
180    c[4] = coeff[4];
181    c[5] = coeff[5];
182  }
183  if ( N == 8 )
184  {
185    c[6] = coeff[6];
186    c[7] = coeff[7];
187  }
188 
189  Int cStride = ( isVertical ) ? srcStride : 1;
190  src -= ( N/2 - 1 ) * cStride;
191
192  Int offset;
193  Short maxVal;
194  Int headRoom = IF_INTERNAL_PREC - bitDepth;
195  Int shift = IF_FILTER_PREC;
196  if ( isLast )
197  {
198    shift += (isFirst) ? 0 : headRoom;
199    offset = 1 << (shift - 1);
200    offset += (isFirst) ? 0 : IF_INTERNAL_OFFS << IF_FILTER_PREC;
201    maxVal = (1 << bitDepth) - 1;
202  }
203  else
204  {
205    shift -= (isFirst) ? headRoom : 0;
206    offset = (isFirst) ? -IF_INTERNAL_OFFS << shift : 0;
207    maxVal = 0;
208  }
209 
210  for (row = 0; row < height; row++)
211  {
212    for (col = 0; col < width; col++)
213    {
214      Int sum;
215     
216      sum  = src[ col + 0 * cStride] * c[0];
217      sum += src[ col + 1 * cStride] * c[1];
218      if ( N >= 4 )
219      {
220        sum += src[ col + 2 * cStride] * c[2];
221        sum += src[ col + 3 * cStride] * c[3];
222      }
223      if ( N >= 6 )
224      {
225        sum += src[ col + 4 * cStride] * c[4];
226        sum += src[ col + 5 * cStride] * c[5];
227      }
228      if ( N == 8 )
229      {
230        sum += src[ col + 6 * cStride] * c[6];
231        sum += src[ col + 7 * cStride] * c[7];       
232      }
233     
234      Short val = ( sum + offset ) >> shift;
235      if ( isLast )
236      {
237        val = ( val < 0 ) ? 0 : val;
238        val = ( val > maxVal ) ? maxVal : val;       
239      }
240      dst[col] = val;
241    }
242   
243    src += srcStride;
244    dst += dstStride;
245  }   
246}
247
248/**
249 * \brief Filter a block of samples (horizontal)
250 *
251 * \tparam N          Number of taps
252 * \param  bitDepth   Bit depth of samples
253 * \param  src        Pointer to source samples
254 * \param  srcStride  Stride of source samples
255 * \param  dst        Pointer to destination samples
256 * \param  dstStride  Stride of destination samples
257 * \param  width      Width of block
258 * \param  height     Height of block
259 * \param  isLast     Flag indicating whether it is the last filtering operation
260 * \param  coeff      Pointer to filter taps
261 */
262template<Int N>
263Void TComInterpolationFilter::filterHor(Int bitDepth, Pel *src, Int srcStride, Short *dst, Int dstStride, Int width, Int height, Bool isLast, Short const *coeff)
264{
265  if ( isLast )
266  {
267    filter<N, false, true, true>(bitDepth, src, srcStride, dst, dstStride, width, height, coeff);
268  }
269  else
270  {
271    filter<N, false, true, false>(bitDepth, src, srcStride, dst, dstStride, width, height, coeff);
272  }
273}
274
275/**
276 * \brief Filter a block of samples (vertical)
277 *
278 * \tparam N          Number of taps
279 * \param  bitDpeth   Sample bit depth
280 * \param  src        Pointer to source samples
281 * \param  srcStride  Stride of source samples
282 * \param  dst        Pointer to destination samples
283 * \param  dstStride  Stride of destination samples
284 * \param  width      Width of block
285 * \param  height     Height of block
286 * \param  isFirst    Flag indicating whether it is the first filtering operation
287 * \param  isLast     Flag indicating whether it is the last filtering operation
288 * \param  coeff      Pointer to filter taps
289 */
290template<Int N>
291Void TComInterpolationFilter::filterVer(Int bitDepth, Pel *src, Int srcStride, Short *dst, Int dstStride, Int width, Int height, Bool isFirst, Bool isLast, Short const *coeff)
292{
293  if ( isFirst && isLast )
294  {
295    filter<N, true, true, true>(bitDepth, src, srcStride, dst, dstStride, width, height, coeff);
296  }
297  else if ( isFirst && !isLast )
298  {
299    filter<N, true, true, false>(bitDepth, src, srcStride, dst, dstStride, width, height, coeff);
300  }
301  else if ( !isFirst && isLast )
302  {
303    filter<N, true, false, true>(bitDepth, src, srcStride, dst, dstStride, width, height, coeff);
304  }
305  else
306  {
307    filter<N, true, false, false>(bitDepth, src, srcStride, dst, dstStride, width, height, coeff);
308  }     
309}
310
311// ====================================================================================================================
312// Public member functions
313// ====================================================================================================================
314
315/**
316 * \brief Filter a block of luma samples (horizontal)
317 *
318 * \param  src        Pointer to source samples
319 * \param  srcStride  Stride of source samples
320 * \param  dst        Pointer to destination samples
321 * \param  dstStride  Stride of destination samples
322 * \param  width      Width of block
323 * \param  height     Height of block
324 * \param  frac       Fractional sample offset
325 * \param  isLast     Flag indicating whether it is the last filtering operation
326 */
327Void TComInterpolationFilter::filterHorLuma(Pel *src, Int srcStride, Short *dst, Int dstStride, Int width, Int height, Int frac, Bool isLast )
328{
329  assert(frac >= 0 && frac < 4);
330 
331  if ( frac == 0 )
332  {
333    filterCopy(g_bitDepthY, src, srcStride, dst, dstStride, width, height, true, isLast );
334  }
335  else
336  {
337    filterHor<NTAPS_LUMA>(g_bitDepthY, src, srcStride, dst, dstStride, width, height, isLast, m_lumaFilter[frac]);
338  }
339}
340
341/**
342 * \brief Filter a block of luma samples (vertical)
343 *
344 * \param  src        Pointer to source samples
345 * \param  srcStride  Stride of source samples
346 * \param  dst        Pointer to destination samples
347 * \param  dstStride  Stride of destination samples
348 * \param  width      Width of block
349 * \param  height     Height of block
350 * \param  frac       Fractional sample offset
351 * \param  isFirst    Flag indicating whether it is the first filtering operation
352 * \param  isLast     Flag indicating whether it is the last filtering operation
353 */
354Void TComInterpolationFilter::filterVerLuma(Pel *src, Int srcStride, Short *dst, Int dstStride, Int width, Int height, Int frac, Bool isFirst, Bool isLast )
355{
356  assert(frac >= 0 && frac < 4);
357 
358  if ( frac == 0 )
359  {
360    filterCopy(g_bitDepthY, src, srcStride, dst, dstStride, width, height, isFirst, isLast );
361  }
362  else
363  {
364    filterVer<NTAPS_LUMA>(g_bitDepthY, src, srcStride, dst, dstStride, width, height, isFirst, isLast, m_lumaFilter[frac]);
365  }
366}
367
368/**
369 * \brief Filter a block of chroma samples (horizontal)
370 *
371 * \param  src        Pointer to source samples
372 * \param  srcStride  Stride of source samples
373 * \param  dst        Pointer to destination samples
374 * \param  dstStride  Stride of destination samples
375 * \param  width      Width of block
376 * \param  height     Height of block
377 * \param  frac       Fractional sample offset
378 * \param  isLast     Flag indicating whether it is the last filtering operation
379 */
380Void TComInterpolationFilter::filterHorChroma(Pel *src, Int srcStride, Short *dst, Int dstStride, Int width, Int height, Int frac, Bool isLast )
381{
382  assert(frac >= 0 && frac < 8);
383 
384  if ( frac == 0 )
385  {
386    filterCopy(g_bitDepthC, src, srcStride, dst, dstStride, width, height, true, isLast );
387  }
388  else
389  {
390    filterHor<NTAPS_CHROMA>(g_bitDepthC, src, srcStride, dst, dstStride, width, height, isLast, m_chromaFilter[frac]);
391  }
392}
393
394/**
395 * \brief Filter a block of chroma samples (vertical)
396 *
397 * \param  src        Pointer to source samples
398 * \param  srcStride  Stride of source samples
399 * \param  dst        Pointer to destination samples
400 * \param  dstStride  Stride of destination samples
401 * \param  width      Width of block
402 * \param  height     Height of block
403 * \param  frac       Fractional sample offset
404 * \param  isFirst    Flag indicating whether it is the first filtering operation
405 * \param  isLast     Flag indicating whether it is the last filtering operation
406 */
407Void TComInterpolationFilter::filterVerChroma(Pel *src, Int srcStride, Short *dst, Int dstStride, Int width, Int height, Int frac, Bool isFirst, Bool isLast )
408{
409  assert(frac >= 0 && frac < 8);
410 
411  if ( frac == 0 )
412  {
413    filterCopy(g_bitDepthC, src, srcStride, dst, dstStride, width, height, isFirst, isLast );
414  }
415  else
416  {
417    filterVer<NTAPS_CHROMA>(g_bitDepthC, src, srcStride, dst, dstStride, width, height, isFirst, isLast, m_chromaFilter[frac]);
418  }
419}
420
421//! \}
Note: See TracBrowser for help on using the repository browser.