source: 3DVCSoftware/branches/HTM-7.0-Fix/source/Lib/TLibCommon/TComInterpolationFilter.cpp @ 458

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