source: 3DVCSoftware/trunk/source/Lib/TLibCommon/TComInterpolationFilter.cpp @ 622

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

Merged DEV-2.0-dev0@604.

File size: 15.6 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#if H_3D_ARP
76const Short TComInterpolationFilter::m_lumaFilterARP[4][NTAPS_LUMA_ARP] =
77{
78  {64,  0},
79  {48, 16},
80  {32, 32},
81  {16, 48}
82};
83const Short TComInterpolationFilter::m_chromaFilterARP[8][NTAPS_CHROMA_ARP] =
84{
85  {64,  0},
86  {56,  8},
87  {48, 16},
88  {40, 24},
89  {32, 32},
90  {24, 40},
91  {16, 48},
92  {8,  56}
93};
94#endif
95
96// ====================================================================================================================
97// Private member functions
98// ====================================================================================================================
99
100/**
101 * \brief Apply unit FIR filter to a block of samples
102 *
103 * \param bitDepth   bitDepth of samples
104 * \param src        Pointer to source samples
105 * \param srcStride  Stride of source samples
106 * \param dst        Pointer to destination samples
107 * \param dstStride  Stride of destination samples
108 * \param width      Width of block
109 * \param height     Height of block
110 * \param isFirst    Flag indicating whether it is the first filtering operation
111 * \param isLast     Flag indicating whether it is the last filtering operation
112 */
113Void TComInterpolationFilter::filterCopy(Int bitDepth, const Pel *src, Int srcStride, Short *dst, Int dstStride, Int width, Int height, Bool isFirst, Bool isLast)
114{
115  Int row, col;
116 
117  if ( isFirst == isLast )
118  {
119    for (row = 0; row < height; row++)
120    {
121      for (col = 0; col < width; col++)
122      {
123        dst[col] = src[col];
124      }
125     
126      src += srcStride;
127      dst += dstStride;
128    }             
129  }
130  else if ( isFirst )
131  {
132    Int shift = IF_INTERNAL_PREC - bitDepth;
133   
134    for (row = 0; row < height; row++)
135    {
136      for (col = 0; col < width; col++)
137      {
138        Short val = src[col] << shift;
139        dst[col] = val - (Short)IF_INTERNAL_OFFS;
140      }
141     
142      src += srcStride;
143      dst += dstStride;
144    }         
145  }
146  else
147  {
148    Int shift = IF_INTERNAL_PREC - bitDepth;
149    Short offset = IF_INTERNAL_OFFS;
150    offset += shift?(1 << (shift - 1)):0;
151    Short maxVal = (1 << bitDepth) - 1;
152    Short minVal = 0;
153    for (row = 0; row < height; row++)
154    {
155      for (col = 0; col < width; col++)
156      {
157        Short val = src[ col ];
158        val = ( val + offset ) >> shift;
159        if (val < minVal) val = minVal;
160        if (val > maxVal) val = maxVal;
161        dst[col] = val;
162      }
163     
164      src += srcStride;
165      dst += dstStride;
166    }             
167  }
168}
169
170/**
171 * \brief Apply FIR filter to a block of samples
172 *
173 * \tparam N          Number of taps
174 * \tparam isVertical Flag indicating filtering along vertical direction
175 * \tparam isFirst    Flag indicating whether it is the first filtering operation
176 * \tparam isLast     Flag indicating whether it is the last filtering operation
177 * \param  bitDepth   Bit depth of samples
178 * \param  src        Pointer to source samples
179 * \param  srcStride  Stride of source samples
180 * \param  dst        Pointer to destination samples
181 * \param  dstStride  Stride of destination samples
182 * \param  width      Width of block
183 * \param  height     Height of block
184 * \param  coeff      Pointer to filter taps
185 */
186template<Int N, Bool isVertical, Bool isFirst, Bool isLast>
187Void TComInterpolationFilter::filter(Int bitDepth, Short const *src, Int srcStride, Short *dst, Int dstStride, Int width, Int height, Short const *coeff)
188{
189  Int row, col;
190 
191  Short c[8];
192  c[0] = coeff[0];
193  c[1] = coeff[1];
194  if ( N >= 4 )
195  {
196    c[2] = coeff[2];
197    c[3] = coeff[3];
198  }
199  if ( N >= 6 )
200  {
201    c[4] = coeff[4];
202    c[5] = coeff[5];
203  }
204  if ( N == 8 )
205  {
206    c[6] = coeff[6];
207    c[7] = coeff[7];
208  }
209 
210  Int cStride = ( isVertical ) ? srcStride : 1;
211  src -= ( N/2 - 1 ) * cStride;
212
213  Int offset;
214  Short maxVal;
215  Int headRoom = IF_INTERNAL_PREC - bitDepth;
216  Int shift = IF_FILTER_PREC;
217  if ( isLast )
218  {
219    shift += (isFirst) ? 0 : headRoom;
220    offset = 1 << (shift - 1);
221    offset += (isFirst) ? 0 : IF_INTERNAL_OFFS << IF_FILTER_PREC;
222    maxVal = (1 << bitDepth) - 1;
223  }
224  else
225  {
226    shift -= (isFirst) ? headRoom : 0;
227    offset = (isFirst) ? -IF_INTERNAL_OFFS << shift : 0;
228    maxVal = 0;
229  }
230 
231  for (row = 0; row < height; row++)
232  {
233    for (col = 0; col < width; col++)
234    {
235      Int sum;
236     
237      sum  = src[ col + 0 * cStride] * c[0];
238      sum += src[ col + 1 * cStride] * c[1];
239      if ( N >= 4 )
240      {
241        sum += src[ col + 2 * cStride] * c[2];
242        sum += src[ col + 3 * cStride] * c[3];
243      }
244      if ( N >= 6 )
245      {
246        sum += src[ col + 4 * cStride] * c[4];
247        sum += src[ col + 5 * cStride] * c[5];
248      }
249      if ( N == 8 )
250      {
251        sum += src[ col + 6 * cStride] * c[6];
252        sum += src[ col + 7 * cStride] * c[7];       
253      }
254     
255      Short val = ( sum + offset ) >> shift;
256      if ( isLast )
257      {
258        val = ( val < 0 ) ? 0 : val;
259        val = ( val > maxVal ) ? maxVal : val;       
260      }
261      dst[col] = val;
262    }
263   
264    src += srcStride;
265    dst += dstStride;
266  }   
267}
268
269/**
270 * \brief Filter a block of samples (horizontal)
271 *
272 * \tparam N          Number of taps
273 * \param  bitDepth   Bit depth of samples
274 * \param  src        Pointer to source samples
275 * \param  srcStride  Stride of source samples
276 * \param  dst        Pointer to destination samples
277 * \param  dstStride  Stride of destination samples
278 * \param  width      Width of block
279 * \param  height     Height of block
280 * \param  isLast     Flag indicating whether it is the last filtering operation
281 * \param  coeff      Pointer to filter taps
282 */
283template<Int N>
284Void TComInterpolationFilter::filterHor(Int bitDepth, Pel *src, Int srcStride, Short *dst, Int dstStride, Int width, Int height, Bool isLast, Short const *coeff)
285{
286  if ( isLast )
287  {
288    filter<N, false, true, true>(bitDepth, src, srcStride, dst, dstStride, width, height, coeff);
289  }
290  else
291  {
292    filter<N, false, true, false>(bitDepth, src, srcStride, dst, dstStride, width, height, coeff);
293  }
294}
295
296/**
297 * \brief Filter a block of samples (vertical)
298 *
299 * \tparam N          Number of taps
300 * \param  bitDpeth   Sample bit depth
301 * \param  src        Pointer to source samples
302 * \param  srcStride  Stride of source samples
303 * \param  dst        Pointer to destination samples
304 * \param  dstStride  Stride of destination samples
305 * \param  width      Width of block
306 * \param  height     Height of block
307 * \param  isFirst    Flag indicating whether it is the first filtering operation
308 * \param  isLast     Flag indicating whether it is the last filtering operation
309 * \param  coeff      Pointer to filter taps
310 */
311template<Int N>
312Void TComInterpolationFilter::filterVer(Int bitDepth, Pel *src, Int srcStride, Short *dst, Int dstStride, Int width, Int height, Bool isFirst, Bool isLast, Short const *coeff)
313{
314  if ( isFirst && isLast )
315  {
316    filter<N, true, true, true>(bitDepth, src, srcStride, dst, dstStride, width, height, coeff);
317  }
318  else if ( isFirst && !isLast )
319  {
320    filter<N, true, true, false>(bitDepth, src, srcStride, dst, dstStride, width, height, coeff);
321  }
322  else if ( !isFirst && isLast )
323  {
324    filter<N, true, false, true>(bitDepth, src, srcStride, dst, dstStride, width, height, coeff);
325  }
326  else
327  {
328    filter<N, true, false, false>(bitDepth, src, srcStride, dst, dstStride, width, height, coeff);
329  }     
330}
331
332// ====================================================================================================================
333// Public member functions
334// ====================================================================================================================
335
336/**
337 * \brief Filter a block of luma samples (horizontal)
338 *
339 * \param  src        Pointer to source samples
340 * \param  srcStride  Stride of source samples
341 * \param  dst        Pointer to destination samples
342 * \param  dstStride  Stride of destination samples
343 * \param  width      Width of block
344 * \param  height     Height of block
345 * \param  frac       Fractional sample offset
346 * \param  isLast     Flag indicating whether it is the last filtering operation
347 */
348Void TComInterpolationFilter::filterHorLuma(Pel *src, Int srcStride, Short *dst, Int dstStride, Int width, Int height, Int frac, Bool isLast
349#if H_3D_ARP
350    , Bool filterType
351#endif
352  )
353{
354  assert(frac >= 0 && frac < 4);
355 
356  if ( frac == 0 )
357  {
358    filterCopy(g_bitDepthY, src, srcStride, dst, dstStride, width, height, true, isLast );
359  }
360  else
361  {
362#if H_3D_ARP
363    if(filterType)
364    {
365      filterHor<NTAPS_LUMA_ARP>(g_bitDepthY, src, srcStride, dst, dstStride, width, height, isLast, m_lumaFilterARP[frac]);
366    }
367    else
368    {
369#endif
370    filterHor<NTAPS_LUMA>(g_bitDepthY, src, srcStride, dst, dstStride, width, height, isLast, m_lumaFilter[frac]);
371#if H_3D_ARP
372    }
373#endif
374  }
375}
376
377/**
378 * \brief Filter a block of luma samples (vertical)
379 *
380 * \param  src        Pointer to source samples
381 * \param  srcStride  Stride of source samples
382 * \param  dst        Pointer to destination samples
383 * \param  dstStride  Stride of destination samples
384 * \param  width      Width of block
385 * \param  height     Height of block
386 * \param  frac       Fractional sample offset
387 * \param  isFirst    Flag indicating whether it is the first filtering operation
388 * \param  isLast     Flag indicating whether it is the last filtering operation
389 */
390Void TComInterpolationFilter::filterVerLuma(Pel *src, Int srcStride, Short *dst, Int dstStride, Int width, Int height, Int frac, Bool isFirst, Bool isLast
391#if H_3D_ARP
392    , Bool filterType
393#endif
394  )
395{
396  assert(frac >= 0 && frac < 4);
397 
398  if ( frac == 0 )
399  {
400    filterCopy(g_bitDepthY, src, srcStride, dst, dstStride, width, height, isFirst, isLast );
401  }
402  else
403  {
404#if H_3D_ARP
405    if(filterType)
406    {
407      filterVer<NTAPS_LUMA_ARP>(g_bitDepthY, src, srcStride, dst, dstStride, width, height, isFirst, isLast, m_lumaFilterARP[frac]);   
408    }
409    else
410    {
411#endif
412    filterVer<NTAPS_LUMA>(g_bitDepthY, src, srcStride, dst, dstStride, width, height, isFirst, isLast, m_lumaFilter[frac]);
413#if H_3D_ARP
414    }
415#endif
416  }
417}
418
419/**
420 * \brief Filter a block of chroma samples (horizontal)
421 *
422 * \param  src        Pointer to source samples
423 * \param  srcStride  Stride of source samples
424 * \param  dst        Pointer to destination samples
425 * \param  dstStride  Stride of destination samples
426 * \param  width      Width of block
427 * \param  height     Height of block
428 * \param  frac       Fractional sample offset
429 * \param  isLast     Flag indicating whether it is the last filtering operation
430 */
431Void TComInterpolationFilter::filterHorChroma(Pel *src, Int srcStride, Short *dst, Int dstStride, Int width, Int height, Int frac, Bool isLast
432#if H_3D_ARP
433    , Bool filterType
434#endif
435  )
436{
437  assert(frac >= 0 && frac < 8);
438 
439  if ( frac == 0 )
440  {
441    filterCopy(g_bitDepthC, src, srcStride, dst, dstStride, width, height, true, isLast );
442  }
443  else
444  {
445#if H_3D_ARP
446    if(filterType)
447    {
448      filterHor<NTAPS_CHROMA_ARP>(g_bitDepthC, src, srcStride, dst, dstStride, width, height, isLast, m_chromaFilterARP[frac]);
449    }
450    else
451    {
452#endif
453    filterHor<NTAPS_CHROMA>(g_bitDepthC, src, srcStride, dst, dstStride, width, height, isLast, m_chromaFilter[frac]);
454#if H_3D_ARP
455    }
456#endif
457  }
458}
459
460/**
461 * \brief Filter a block of chroma samples (vertical)
462 *
463 * \param  src        Pointer to source samples
464 * \param  srcStride  Stride of source samples
465 * \param  dst        Pointer to destination samples
466 * \param  dstStride  Stride of destination samples
467 * \param  width      Width of block
468 * \param  height     Height of block
469 * \param  frac       Fractional sample offset
470 * \param  isFirst    Flag indicating whether it is the first filtering operation
471 * \param  isLast     Flag indicating whether it is the last filtering operation
472 */
473Void TComInterpolationFilter::filterVerChroma(Pel *src, Int srcStride, Short *dst, Int dstStride, Int width, Int height, Int frac, Bool isFirst, Bool isLast
474#if H_3D_ARP
475    , Bool filterType
476#endif
477  )
478{
479  assert(frac >= 0 && frac < 8);
480 
481  if ( frac == 0 )
482  {
483    filterCopy(g_bitDepthC, src, srcStride, dst, dstStride, width, height, isFirst, isLast );
484  }
485  else
486  {
487#if H_3D_ARP
488    if(filterType)
489    {
490      filterVer<NTAPS_CHROMA_ARP>(g_bitDepthC, src, srcStride, dst, dstStride, width, height, isFirst, isLast, m_chromaFilterARP[frac]);
491    }
492    else
493    {
494#endif
495    filterVer<NTAPS_CHROMA>(g_bitDepthC, src, srcStride, dst, dstStride, width, height, isFirst, isLast, m_chromaFilter[frac]);
496#if H_3D_ARP
497    }
498#endif
499  }
500}
501
502//! \}
Note: See TracBrowser for help on using the repository browser.