LCOV - code coverage report
Current view: top level - libavfilter - transform.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 88 0.0 %
Date: 2017-12-17 04:34:43 Functions: 0 8 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2010 Georg Martius <georg.martius@web.de>
       3             :  * Copyright (C) 2010 Daniel G. Taylor <dan@programmer-art.org>
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : /**
      23             :  * @file
      24             :  * transform input video
      25             :  */
      26             : 
      27             : #include "libavutil/common.h"
      28             : #include "libavutil/avassert.h"
      29             : 
      30             : #include "transform.h"
      31             : 
      32             : #define INTERPOLATE_METHOD(name) \
      33             :     static uint8_t name(float x, float y, const uint8_t *src, \
      34             :                         int width, int height, int stride, uint8_t def)
      35             : 
      36             : #define PIXEL(img, x, y, w, h, stride, def) \
      37             :     ((x) < 0 || (y) < 0) ? (def) : \
      38             :     (((x) >= (w) || (y) >= (h)) ? (def) : \
      39             :     img[(x) + (y) * (stride)])
      40             : 
      41             : /**
      42             :  * Nearest neighbor interpolation
      43             :  */
      44           0 : INTERPOLATE_METHOD(interpolate_nearest)
      45             : {
      46           0 :     return PIXEL(src, (int)(x + 0.5), (int)(y + 0.5), width, height, stride, def);
      47             : }
      48             : 
      49             : /**
      50             :  * Bilinear interpolation
      51             :  */
      52           0 : INTERPOLATE_METHOD(interpolate_bilinear)
      53             : {
      54             :     int x_c, x_f, y_c, y_f;
      55             :     int v1, v2, v3, v4;
      56             : 
      57           0 :     if (x < -1 || x > width || y < -1 || y > height) {
      58           0 :         return def;
      59             :     } else {
      60           0 :         x_f = (int)x;
      61           0 :         x_c = x_f + 1;
      62             : 
      63           0 :         y_f = (int)y;
      64           0 :         y_c = y_f + 1;
      65             : 
      66           0 :         v1 = PIXEL(src, x_c, y_c, width, height, stride, def);
      67           0 :         v2 = PIXEL(src, x_c, y_f, width, height, stride, def);
      68           0 :         v3 = PIXEL(src, x_f, y_c, width, height, stride, def);
      69           0 :         v4 = PIXEL(src, x_f, y_f, width, height, stride, def);
      70             : 
      71           0 :         return (v1*(x - x_f)*(y - y_f) + v2*((x - x_f)*(y_c - y)) +
      72           0 :                 v3*(x_c - x)*(y - y_f) + v4*((x_c - x)*(y_c - y)));
      73             :     }
      74             : }
      75             : 
      76             : /**
      77             :  * Biquadratic interpolation
      78             :  */
      79           0 : INTERPOLATE_METHOD(interpolate_biquadratic)
      80             : {
      81             :     int     x_c, x_f, y_c, y_f;
      82             :     uint8_t v1,  v2,  v3,  v4;
      83             :     float   f1,  f2,  f3,  f4;
      84             : 
      85           0 :     if (x < - 1 || x > width || y < -1 || y > height)
      86           0 :         return def;
      87             :     else {
      88           0 :         x_f = (int)x;
      89           0 :         x_c = x_f + 1;
      90           0 :         y_f = (int)y;
      91           0 :         y_c = y_f + 1;
      92             : 
      93           0 :         v1 = PIXEL(src, x_c, y_c, width, height, stride, def);
      94           0 :         v2 = PIXEL(src, x_c, y_f, width, height, stride, def);
      95           0 :         v3 = PIXEL(src, x_f, y_c, width, height, stride, def);
      96           0 :         v4 = PIXEL(src, x_f, y_f, width, height, stride, def);
      97             : 
      98           0 :         f1 = 1 - sqrt((x_c - x) * (y_c - y));
      99           0 :         f2 = 1 - sqrt((x_c - x) * (y - y_f));
     100           0 :         f3 = 1 - sqrt((x - x_f) * (y_c - y));
     101           0 :         f4 = 1 - sqrt((x - x_f) * (y - y_f));
     102           0 :         return (v1 * f1 + v2 * f2 + v3 * f3 + v4 * f4) / (f1 + f2 + f3 + f4);
     103             :     }
     104             : }
     105             : 
     106           0 : void avfilter_get_matrix(float x_shift, float y_shift, float angle, float zoom, float *matrix) {
     107           0 :     matrix[0] = zoom * cos(angle);
     108           0 :     matrix[1] = -sin(angle);
     109           0 :     matrix[2] = x_shift;
     110           0 :     matrix[3] = -matrix[1];
     111           0 :     matrix[4] = matrix[0];
     112           0 :     matrix[5] = y_shift;
     113           0 :     matrix[6] = 0;
     114           0 :     matrix[7] = 0;
     115           0 :     matrix[8] = 1;
     116           0 : }
     117             : 
     118           0 : void avfilter_add_matrix(const float *m1, const float *m2, float *result)
     119             : {
     120             :     int i;
     121           0 :     for (i = 0; i < 9; i++)
     122           0 :         result[i] = m1[i] + m2[i];
     123           0 : }
     124             : 
     125           0 : void avfilter_sub_matrix(const float *m1, const float *m2, float *result)
     126             : {
     127             :     int i;
     128           0 :     for (i = 0; i < 9; i++)
     129           0 :         result[i] = m1[i] - m2[i];
     130           0 : }
     131             : 
     132           0 : void avfilter_mul_matrix(const float *m1, float scalar, float *result)
     133             : {
     134             :     int i;
     135           0 :     for (i = 0; i < 9; i++)
     136           0 :         result[i] = m1[i] * scalar;
     137           0 : }
     138             : 
     139           0 : int avfilter_transform(const uint8_t *src, uint8_t *dst,
     140             :                         int src_stride, int dst_stride,
     141             :                         int width, int height, const float *matrix,
     142             :                         enum InterpolateMethod interpolate,
     143             :                         enum FillMethod fill)
     144             : {
     145             :     int x, y;
     146             :     float x_s, y_s;
     147           0 :     uint8_t def = 0;
     148           0 :     uint8_t (*func)(float, float, const uint8_t *, int, int, int, uint8_t) = NULL;
     149             : 
     150           0 :     switch(interpolate) {
     151           0 :         case INTERPOLATE_NEAREST:
     152           0 :             func = interpolate_nearest;
     153           0 :             break;
     154           0 :         case INTERPOLATE_BILINEAR:
     155           0 :             func = interpolate_bilinear;
     156           0 :             break;
     157           0 :         case INTERPOLATE_BIQUADRATIC:
     158           0 :             func = interpolate_biquadratic;
     159           0 :             break;
     160           0 :         default:
     161           0 :             return AVERROR(EINVAL);
     162             :     }
     163             : 
     164           0 :     for (y = 0; y < height; y++) {
     165           0 :         for(x = 0; x < width; x++) {
     166           0 :             x_s = x * matrix[0] + y * matrix[1] + matrix[2];
     167           0 :             y_s = x * matrix[3] + y * matrix[4] + matrix[5];
     168             : 
     169           0 :             switch(fill) {
     170           0 :                 case FILL_ORIGINAL:
     171           0 :                     def = src[y * src_stride + x];
     172           0 :                     break;
     173           0 :                 case FILL_CLAMP:
     174           0 :                     y_s = av_clipf(y_s, 0, height - 1);
     175           0 :                     x_s = av_clipf(x_s, 0, width - 1);
     176           0 :                     def = src[(int)y_s * src_stride + (int)x_s];
     177           0 :                     break;
     178           0 :                 case FILL_MIRROR:
     179           0 :                     x_s = avpriv_mirror(x_s,  width-1);
     180           0 :                     y_s = avpriv_mirror(y_s, height-1);
     181             : 
     182             :                     av_assert2(x_s >= 0 && y_s >= 0);
     183             :                     av_assert2(x_s < width && y_s < height);
     184           0 :                     def = src[(int)y_s * src_stride + (int)x_s];
     185             :             }
     186             : 
     187           0 :             dst[y * dst_stride + x] = func(x_s, y_s, src, width, height, src_stride, def);
     188             :         }
     189             :     }
     190           0 :     return 0;
     191             : }

Generated by: LCOV version 1.13