LCOV - code coverage report
Current view: top level - libswscale - utils.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 608 1193 51.0 %
Date: 2018-05-20 11:54:08 Functions: 19 47 40.4 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : #include "config.h"
      22             : 
      23             : #define _DEFAULT_SOURCE
      24             : #define _SVID_SOURCE // needed for MAP_ANONYMOUS
      25             : #define _DARWIN_C_SOURCE // needed for MAP_ANON
      26             : #include <inttypes.h>
      27             : #include <math.h>
      28             : #include <stdio.h>
      29             : #include <string.h>
      30             : #if HAVE_MMAP
      31             : #include <sys/mman.h>
      32             : #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
      33             : #define MAP_ANONYMOUS MAP_ANON
      34             : #endif
      35             : #endif
      36             : #if HAVE_VIRTUALALLOC
      37             : #define WIN32_LEAN_AND_MEAN
      38             : #include <windows.h>
      39             : #endif
      40             : 
      41             : #include "libavutil/attributes.h"
      42             : #include "libavutil/avassert.h"
      43             : #include "libavutil/avutil.h"
      44             : #include "libavutil/bswap.h"
      45             : #include "libavutil/cpu.h"
      46             : #include "libavutil/imgutils.h"
      47             : #include "libavutil/intreadwrite.h"
      48             : #include "libavutil/libm.h"
      49             : #include "libavutil/mathematics.h"
      50             : #include "libavutil/opt.h"
      51             : #include "libavutil/pixdesc.h"
      52             : #include "libavutil/aarch64/cpu.h"
      53             : #include "libavutil/ppc/cpu.h"
      54             : #include "libavutil/x86/asm.h"
      55             : #include "libavutil/x86/cpu.h"
      56             : 
      57             : // We have to implement deprecated functions until they are removed, this is the
      58             : // simplest way to prevent warnings
      59             : #undef attribute_deprecated
      60             : #define attribute_deprecated
      61             : 
      62             : #include "rgb2rgb.h"
      63             : #include "swscale.h"
      64             : #include "swscale_internal.h"
      65             : 
      66             : #if !FF_API_SWS_VECTOR
      67             : static SwsVector *sws_getIdentityVec(void);
      68             : static void sws_addVec(SwsVector *a, SwsVector *b);
      69             : static void sws_shiftVec(SwsVector *a, int shift);
      70             : static void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level);
      71             : #endif
      72             : 
      73             : static void handle_formats(SwsContext *c);
      74             : 
      75        5681 : unsigned swscale_version(void)
      76             : {
      77             :     av_assert0(LIBSWSCALE_VERSION_MICRO >= 100);
      78        5681 :     return LIBSWSCALE_VERSION_INT;
      79             : }
      80             : 
      81        5681 : const char *swscale_configuration(void)
      82             : {
      83        5681 :     return FFMPEG_CONFIGURATION;
      84             : }
      85             : 
      86           0 : const char *swscale_license(void)
      87             : {
      88             : #define LICENSE_PREFIX "libswscale license: "
      89           0 :     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
      90             : }
      91             : 
      92             : typedef struct FormatEntry {
      93             :     uint8_t is_supported_in         :1;
      94             :     uint8_t is_supported_out        :1;
      95             :     uint8_t is_supported_endianness :1;
      96             : } FormatEntry;
      97             : 
      98             : static const FormatEntry format_entries[AV_PIX_FMT_NB] = {
      99             :     [AV_PIX_FMT_YUV420P]     = { 1, 1 },
     100             :     [AV_PIX_FMT_YUYV422]     = { 1, 1 },
     101             :     [AV_PIX_FMT_RGB24]       = { 1, 1 },
     102             :     [AV_PIX_FMT_BGR24]       = { 1, 1 },
     103             :     [AV_PIX_FMT_YUV422P]     = { 1, 1 },
     104             :     [AV_PIX_FMT_YUV444P]     = { 1, 1 },
     105             :     [AV_PIX_FMT_YUV410P]     = { 1, 1 },
     106             :     [AV_PIX_FMT_YUV411P]     = { 1, 1 },
     107             :     [AV_PIX_FMT_GRAY8]       = { 1, 1 },
     108             :     [AV_PIX_FMT_MONOWHITE]   = { 1, 1 },
     109             :     [AV_PIX_FMT_MONOBLACK]   = { 1, 1 },
     110             :     [AV_PIX_FMT_PAL8]        = { 1, 0 },
     111             :     [AV_PIX_FMT_YUVJ420P]    = { 1, 1 },
     112             :     [AV_PIX_FMT_YUVJ411P]    = { 1, 1 },
     113             :     [AV_PIX_FMT_YUVJ422P]    = { 1, 1 },
     114             :     [AV_PIX_FMT_YUVJ444P]    = { 1, 1 },
     115             :     [AV_PIX_FMT_YVYU422]     = { 1, 1 },
     116             :     [AV_PIX_FMT_UYVY422]     = { 1, 1 },
     117             :     [AV_PIX_FMT_UYYVYY411]   = { 0, 0 },
     118             :     [AV_PIX_FMT_BGR8]        = { 1, 1 },
     119             :     [AV_PIX_FMT_BGR4]        = { 0, 1 },
     120             :     [AV_PIX_FMT_BGR4_BYTE]   = { 1, 1 },
     121             :     [AV_PIX_FMT_RGB8]        = { 1, 1 },
     122             :     [AV_PIX_FMT_RGB4]        = { 0, 1 },
     123             :     [AV_PIX_FMT_RGB4_BYTE]   = { 1, 1 },
     124             :     [AV_PIX_FMT_NV12]        = { 1, 1 },
     125             :     [AV_PIX_FMT_NV21]        = { 1, 1 },
     126             :     [AV_PIX_FMT_ARGB]        = { 1, 1 },
     127             :     [AV_PIX_FMT_RGBA]        = { 1, 1 },
     128             :     [AV_PIX_FMT_ABGR]        = { 1, 1 },
     129             :     [AV_PIX_FMT_BGRA]        = { 1, 1 },
     130             :     [AV_PIX_FMT_0RGB]        = { 1, 1 },
     131             :     [AV_PIX_FMT_RGB0]        = { 1, 1 },
     132             :     [AV_PIX_FMT_0BGR]        = { 1, 1 },
     133             :     [AV_PIX_FMT_BGR0]        = { 1, 1 },
     134             :     [AV_PIX_FMT_GRAY9BE]     = { 1, 1 },
     135             :     [AV_PIX_FMT_GRAY9LE]     = { 1, 1 },
     136             :     [AV_PIX_FMT_GRAY10BE]    = { 1, 1 },
     137             :     [AV_PIX_FMT_GRAY10LE]    = { 1, 1 },
     138             :     [AV_PIX_FMT_GRAY12BE]    = { 1, 1 },
     139             :     [AV_PIX_FMT_GRAY12LE]    = { 1, 1 },
     140             :     [AV_PIX_FMT_GRAY14BE]    = { 1, 1 },
     141             :     [AV_PIX_FMT_GRAY14LE]    = { 1, 1 },
     142             :     [AV_PIX_FMT_GRAY16BE]    = { 1, 1 },
     143             :     [AV_PIX_FMT_GRAY16LE]    = { 1, 1 },
     144             :     [AV_PIX_FMT_YUV440P]     = { 1, 1 },
     145             :     [AV_PIX_FMT_YUVJ440P]    = { 1, 1 },
     146             :     [AV_PIX_FMT_YUV440P10LE] = { 1, 1 },
     147             :     [AV_PIX_FMT_YUV440P10BE] = { 1, 1 },
     148             :     [AV_PIX_FMT_YUV440P12LE] = { 1, 1 },
     149             :     [AV_PIX_FMT_YUV440P12BE] = { 1, 1 },
     150             :     [AV_PIX_FMT_YUVA420P]    = { 1, 1 },
     151             :     [AV_PIX_FMT_YUVA422P]    = { 1, 1 },
     152             :     [AV_PIX_FMT_YUVA444P]    = { 1, 1 },
     153             :     [AV_PIX_FMT_YUVA420P9BE] = { 1, 1 },
     154             :     [AV_PIX_FMT_YUVA420P9LE] = { 1, 1 },
     155             :     [AV_PIX_FMT_YUVA422P9BE] = { 1, 1 },
     156             :     [AV_PIX_FMT_YUVA422P9LE] = { 1, 1 },
     157             :     [AV_PIX_FMT_YUVA444P9BE] = { 1, 1 },
     158             :     [AV_PIX_FMT_YUVA444P9LE] = { 1, 1 },
     159             :     [AV_PIX_FMT_YUVA420P10BE]= { 1, 1 },
     160             :     [AV_PIX_FMT_YUVA420P10LE]= { 1, 1 },
     161             :     [AV_PIX_FMT_YUVA422P10BE]= { 1, 1 },
     162             :     [AV_PIX_FMT_YUVA422P10LE]= { 1, 1 },
     163             :     [AV_PIX_FMT_YUVA444P10BE]= { 1, 1 },
     164             :     [AV_PIX_FMT_YUVA444P10LE]= { 1, 1 },
     165             :     [AV_PIX_FMT_YUVA420P16BE]= { 1, 1 },
     166             :     [AV_PIX_FMT_YUVA420P16LE]= { 1, 1 },
     167             :     [AV_PIX_FMT_YUVA422P16BE]= { 1, 1 },
     168             :     [AV_PIX_FMT_YUVA422P16LE]= { 1, 1 },
     169             :     [AV_PIX_FMT_YUVA444P16BE]= { 1, 1 },
     170             :     [AV_PIX_FMT_YUVA444P16LE]= { 1, 1 },
     171             :     [AV_PIX_FMT_RGB48BE]     = { 1, 1 },
     172             :     [AV_PIX_FMT_RGB48LE]     = { 1, 1 },
     173             :     [AV_PIX_FMT_RGBA64BE]    = { 1, 1, 1 },
     174             :     [AV_PIX_FMT_RGBA64LE]    = { 1, 1, 1 },
     175             :     [AV_PIX_FMT_RGB565BE]    = { 1, 1 },
     176             :     [AV_PIX_FMT_RGB565LE]    = { 1, 1 },
     177             :     [AV_PIX_FMT_RGB555BE]    = { 1, 1 },
     178             :     [AV_PIX_FMT_RGB555LE]    = { 1, 1 },
     179             :     [AV_PIX_FMT_BGR565BE]    = { 1, 1 },
     180             :     [AV_PIX_FMT_BGR565LE]    = { 1, 1 },
     181             :     [AV_PIX_FMT_BGR555BE]    = { 1, 1 },
     182             :     [AV_PIX_FMT_BGR555LE]    = { 1, 1 },
     183             :     [AV_PIX_FMT_YUV420P16LE] = { 1, 1 },
     184             :     [AV_PIX_FMT_YUV420P16BE] = { 1, 1 },
     185             :     [AV_PIX_FMT_YUV422P16LE] = { 1, 1 },
     186             :     [AV_PIX_FMT_YUV422P16BE] = { 1, 1 },
     187             :     [AV_PIX_FMT_YUV444P16LE] = { 1, 1 },
     188             :     [AV_PIX_FMT_YUV444P16BE] = { 1, 1 },
     189             :     [AV_PIX_FMT_RGB444LE]    = { 1, 1 },
     190             :     [AV_PIX_FMT_RGB444BE]    = { 1, 1 },
     191             :     [AV_PIX_FMT_BGR444LE]    = { 1, 1 },
     192             :     [AV_PIX_FMT_BGR444BE]    = { 1, 1 },
     193             :     [AV_PIX_FMT_YA8]         = { 1, 1 },
     194             :     [AV_PIX_FMT_YA16BE]      = { 1, 0 },
     195             :     [AV_PIX_FMT_YA16LE]      = { 1, 0 },
     196             :     [AV_PIX_FMT_BGR48BE]     = { 1, 1 },
     197             :     [AV_PIX_FMT_BGR48LE]     = { 1, 1 },
     198             :     [AV_PIX_FMT_BGRA64BE]    = { 1, 1, 1 },
     199             :     [AV_PIX_FMT_BGRA64LE]    = { 1, 1, 1 },
     200             :     [AV_PIX_FMT_YUV420P9BE]  = { 1, 1 },
     201             :     [AV_PIX_FMT_YUV420P9LE]  = { 1, 1 },
     202             :     [AV_PIX_FMT_YUV420P10BE] = { 1, 1 },
     203             :     [AV_PIX_FMT_YUV420P10LE] = { 1, 1 },
     204             :     [AV_PIX_FMT_YUV420P12BE] = { 1, 1 },
     205             :     [AV_PIX_FMT_YUV420P12LE] = { 1, 1 },
     206             :     [AV_PIX_FMT_YUV420P14BE] = { 1, 1 },
     207             :     [AV_PIX_FMT_YUV420P14LE] = { 1, 1 },
     208             :     [AV_PIX_FMT_YUV422P9BE]  = { 1, 1 },
     209             :     [AV_PIX_FMT_YUV422P9LE]  = { 1, 1 },
     210             :     [AV_PIX_FMT_YUV422P10BE] = { 1, 1 },
     211             :     [AV_PIX_FMT_YUV422P10LE] = { 1, 1 },
     212             :     [AV_PIX_FMT_YUV422P12BE] = { 1, 1 },
     213             :     [AV_PIX_FMT_YUV422P12LE] = { 1, 1 },
     214             :     [AV_PIX_FMT_YUV422P14BE] = { 1, 1 },
     215             :     [AV_PIX_FMT_YUV422P14LE] = { 1, 1 },
     216             :     [AV_PIX_FMT_YUV444P9BE]  = { 1, 1 },
     217             :     [AV_PIX_FMT_YUV444P9LE]  = { 1, 1 },
     218             :     [AV_PIX_FMT_YUV444P10BE] = { 1, 1 },
     219             :     [AV_PIX_FMT_YUV444P10LE] = { 1, 1 },
     220             :     [AV_PIX_FMT_YUV444P12BE] = { 1, 1 },
     221             :     [AV_PIX_FMT_YUV444P12LE] = { 1, 1 },
     222             :     [AV_PIX_FMT_YUV444P14BE] = { 1, 1 },
     223             :     [AV_PIX_FMT_YUV444P14LE] = { 1, 1 },
     224             :     [AV_PIX_FMT_GBRP]        = { 1, 1 },
     225             :     [AV_PIX_FMT_GBRP9LE]     = { 1, 1 },
     226             :     [AV_PIX_FMT_GBRP9BE]     = { 1, 1 },
     227             :     [AV_PIX_FMT_GBRP10LE]    = { 1, 1 },
     228             :     [AV_PIX_FMT_GBRP10BE]    = { 1, 1 },
     229             :     [AV_PIX_FMT_GBRAP10LE]   = { 1, 1 },
     230             :     [AV_PIX_FMT_GBRAP10BE]   = { 1, 1 },
     231             :     [AV_PIX_FMT_GBRP12LE]    = { 1, 1 },
     232             :     [AV_PIX_FMT_GBRP12BE]    = { 1, 1 },
     233             :     [AV_PIX_FMT_GBRAP12LE]   = { 1, 1 },
     234             :     [AV_PIX_FMT_GBRAP12BE]   = { 1, 1 },
     235             :     [AV_PIX_FMT_GBRP14LE]    = { 1, 1 },
     236             :     [AV_PIX_FMT_GBRP14BE]    = { 1, 1 },
     237             :     [AV_PIX_FMT_GBRP16LE]    = { 1, 1 },
     238             :     [AV_PIX_FMT_GBRP16BE]    = { 1, 1 },
     239             :     [AV_PIX_FMT_GBRAP]       = { 1, 1 },
     240             :     [AV_PIX_FMT_GBRAP16LE]   = { 1, 1 },
     241             :     [AV_PIX_FMT_GBRAP16BE]   = { 1, 1 },
     242             :     [AV_PIX_FMT_BAYER_BGGR8] = { 1, 0 },
     243             :     [AV_PIX_FMT_BAYER_RGGB8] = { 1, 0 },
     244             :     [AV_PIX_FMT_BAYER_GBRG8] = { 1, 0 },
     245             :     [AV_PIX_FMT_BAYER_GRBG8] = { 1, 0 },
     246             :     [AV_PIX_FMT_BAYER_BGGR16LE] = { 1, 0 },
     247             :     [AV_PIX_FMT_BAYER_BGGR16BE] = { 1, 0 },
     248             :     [AV_PIX_FMT_BAYER_RGGB16LE] = { 1, 0 },
     249             :     [AV_PIX_FMT_BAYER_RGGB16BE] = { 1, 0 },
     250             :     [AV_PIX_FMT_BAYER_GBRG16LE] = { 1, 0 },
     251             :     [AV_PIX_FMT_BAYER_GBRG16BE] = { 1, 0 },
     252             :     [AV_PIX_FMT_BAYER_GRBG16LE] = { 1, 0 },
     253             :     [AV_PIX_FMT_BAYER_GRBG16BE] = { 1, 0 },
     254             :     [AV_PIX_FMT_XYZ12BE]     = { 1, 1, 1 },
     255             :     [AV_PIX_FMT_XYZ12LE]     = { 1, 1, 1 },
     256             :     [AV_PIX_FMT_AYUV64LE]    = { 1, 1},
     257             :     [AV_PIX_FMT_P010LE]      = { 1, 1 },
     258             :     [AV_PIX_FMT_P010BE]      = { 1, 1 },
     259             :     [AV_PIX_FMT_P016LE]      = { 1, 1 },
     260             :     [AV_PIX_FMT_P016BE]      = { 1, 1 },
     261             : };
     262             : 
     263      594429 : int sws_isSupportedInput(enum AVPixelFormat pix_fmt)
     264             : {
     265      594429 :     return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
     266      594429 :            format_entries[pix_fmt].is_supported_in : 0;
     267             : }
     268             : 
     269      594429 : int sws_isSupportedOutput(enum AVPixelFormat pix_fmt)
     270             : {
     271      594429 :     return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
     272      594429 :            format_entries[pix_fmt].is_supported_out : 0;
     273             : }
     274             : 
     275      207153 : int sws_isSupportedEndiannessConversion(enum AVPixelFormat pix_fmt)
     276             : {
     277      207153 :     return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
     278      207153 :            format_entries[pix_fmt].is_supported_endianness : 0;
     279             : }
     280             : 
     281           0 : static double getSplineCoeff(double a, double b, double c, double d,
     282             :                              double dist)
     283             : {
     284           0 :     if (dist <= 1.0)
     285           0 :         return ((d * dist + c) * dist + b) * dist + a;
     286             :     else
     287           0 :         return getSplineCoeff(0.0,
     288           0 :                                b + 2.0 * c + 3.0 * d,
     289           0 :                                c + 3.0 * d,
     290           0 :                               -b - 3.0 * c - 6.0 * d,
     291             :                               dist - 1.0);
     292             : }
     293             : 
     294       23872 : static av_cold int get_local_pos(SwsContext *s, int chr_subsample, int pos, int dir)
     295             : {
     296       23872 :     if (pos == -1 || pos <= -513) {
     297        9269 :         pos = (128 << chr_subsample) - 128;
     298             :     }
     299       23872 :     pos += 128; // relative to ideal left edge
     300       23872 :     return pos >> chr_subsample;
     301             : }
     302             : 
     303             : typedef struct {
     304             :     int flag;                   ///< flag associated to the algorithm
     305             :     const char *description;    ///< human-readable description
     306             :     int size_factor;            ///< size factor used when initing the filters
     307             : } ScaleAlgorithm;
     308             : 
     309             : static const ScaleAlgorithm scale_algorithms[] = {
     310             :     { SWS_AREA,          "area averaging",                  1 /* downscale only, for upscale it is bilinear */ },
     311             :     { SWS_BICUBIC,       "bicubic",                         4 },
     312             :     { SWS_BICUBLIN,      "luma bicubic / chroma bilinear", -1 },
     313             :     { SWS_BILINEAR,      "bilinear",                        2 },
     314             :     { SWS_FAST_BILINEAR, "fast bilinear",                  -1 },
     315             :     { SWS_GAUSS,         "Gaussian",                        8 /* infinite ;) */ },
     316             :     { SWS_LANCZOS,       "Lanczos",                        -1 /* custom */ },
     317             :     { SWS_POINT,         "nearest neighbor / point",       -1 },
     318             :     { SWS_SINC,          "sinc",                           20 /* infinite ;) */ },
     319             :     { SWS_SPLINE,        "bicubic spline",                 20 /* infinite :)*/ },
     320             :     { SWS_X,             "experimental",                    8 },
     321             : };
     322             : 
     323       11936 : static av_cold int initFilter(int16_t **outFilter, int32_t **filterPos,
     324             :                               int *outFilterSize, int xInc, int srcW,
     325             :                               int dstW, int filterAlign, int one,
     326             :                               int flags, int cpu_flags,
     327             :                               SwsVector *srcFilter, SwsVector *dstFilter,
     328             :                               double param[2], int srcPos, int dstPos)
     329             : {
     330             :     int i;
     331             :     int filterSize;
     332             :     int filter2Size;
     333             :     int minFilterSize;
     334       11936 :     int64_t *filter    = NULL;
     335       11936 :     int64_t *filter2   = NULL;
     336       11936 :     const int64_t fone = 1LL << (54 - FFMIN(av_log2(srcW/dstW), 8));
     337       11936 :     int ret            = -1;
     338             : 
     339       11936 :     emms_c(); // FIXME should not be required but IS (even for non-MMX versions)
     340             : 
     341             :     // NOTE: the +3 is for the MMX(+1) / SSE(+3) scaler which reads over the end
     342       11936 :     FF_ALLOC_ARRAY_OR_GOTO(NULL, *filterPos, (dstW + 3), sizeof(**filterPos), fail);
     343             : 
     344       19471 :     if (FFABS(xInc - 0x10000) < 10 && srcPos == dstPos) { // unscaled
     345             :         int i;
     346        7535 :         filterSize = 1;
     347        7535 :         FF_ALLOCZ_ARRAY_OR_GOTO(NULL, filter,
     348             :                                 dstW, sizeof(*filter) * filterSize, fail);
     349             : 
     350     2037380 :         for (i = 0; i < dstW; i++) {
     351     2029845 :             filter[i * filterSize] = fone;
     352     2029845 :             (*filterPos)[i]        = i;
     353             :         }
     354        4401 :     } else if (flags & SWS_POINT) { // lame looking point sampling mode
     355             :         int i;
     356             :         int64_t xDstInSrc;
     357         188 :         filterSize = 1;
     358         188 :         FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
     359             :                                dstW, sizeof(*filter) * filterSize, fail);
     360             : 
     361         188 :         xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
     362       43549 :         for (i = 0; i < dstW; i++) {
     363       43361 :             int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
     364             : 
     365       43361 :             (*filterPos)[i] = xx;
     366       43361 :             filter[i]       = fone;
     367       43361 :             xDstInSrc      += xInc;
     368             :         }
     369        8374 :     } else if ((xInc <= (1 << 16) && (flags & SWS_AREA)) ||
     370        4213 :                (flags & SWS_FAST_BILINEAR)) { // bilinear upscale
     371             :         int i;
     372             :         int64_t xDstInSrc;
     373          52 :         filterSize = 2;
     374          52 :         FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
     375             :                                dstW, sizeof(*filter) * filterSize, fail);
     376             : 
     377          52 :         xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
     378       29092 :         for (i = 0; i < dstW; i++) {
     379       29040 :             int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
     380             :             int j;
     381             : 
     382       29040 :             (*filterPos)[i] = xx;
     383             :             // bilinear upscale / linear interpolate / area averaging
     384       87120 :             for (j = 0; j < filterSize; j++) {
     385       58080 :                 int64_t coeff= fone - FFABS(((int64_t)xx<<16) - xDstInSrc)*(fone>>16);
     386       58080 :                 if (coeff < 0)
     387           0 :                     coeff = 0;
     388       58080 :                 filter[i * filterSize + j] = coeff;
     389       58080 :                 xx++;
     390             :             }
     391       29040 :             xDstInSrc += xInc;
     392             :         }
     393             :     } else {
     394             :         int64_t xDstInSrc;
     395        4161 :         int sizeFactor = -1;
     396             : 
     397        8224 :         for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
     398        8224 :             if (flags & scale_algorithms[i].flag && scale_algorithms[i].size_factor > 0) {
     399        4161 :                 sizeFactor = scale_algorithms[i].size_factor;
     400        4161 :                 break;
     401             :             }
     402             :         }
     403        4161 :         if (flags & SWS_LANCZOS)
     404           0 :             sizeFactor = param[0] != SWS_PARAM_DEFAULT ? ceil(2 * param[0]) : 6;
     405        4161 :         av_assert0(sizeFactor > 0);
     406             : 
     407        4161 :         if (xInc <= 1 << 16)
     408        2903 :             filterSize = 1 + sizeFactor;    // upscale
     409             :         else
     410        1258 :             filterSize = 1 + (sizeFactor * srcW + dstW - 1) / dstW;
     411             : 
     412        4161 :         filterSize = FFMIN(filterSize, srcW - 2);
     413        4161 :         filterSize = FFMAX(filterSize, 1);
     414             : 
     415        4161 :         FF_ALLOC_ARRAY_OR_GOTO(NULL, filter,
     416             :                                dstW, sizeof(*filter) * filterSize, fail);
     417             : 
     418        4161 :         xDstInSrc = ((dstPos*(int64_t)xInc)>>7) - ((srcPos*0x10000LL)>>7);
     419     1303164 :         for (i = 0; i < dstW; i++) {
     420     1299003 :             int xx = (xDstInSrc - (filterSize - 2) * (1LL<<16)) / (1 << 17);
     421             :             int j;
     422     1299003 :             (*filterPos)[i] = xx;
     423     8708134 :             for (j = 0; j < filterSize; j++) {
     424     7409131 :                 int64_t d = (FFABS(((int64_t)xx * (1 << 17)) - xDstInSrc)) << 13;
     425             :                 double floatd;
     426             :                 int64_t coeff;
     427             : 
     428     7409131 :                 if (xInc > 1 << 16)
     429     1785796 :                     d = d * dstW / srcW;
     430     7409131 :                 floatd = d * (1.0 / (1 << 30));
     431             : 
     432     7409131 :                 if (flags & SWS_BICUBIC) {
     433     7078154 :                     int64_t B = (param[0] != SWS_PARAM_DEFAULT ? param[0] :   0) * (1 << 24);
     434     7078154 :                     int64_t C = (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1 << 24);
     435             : 
     436     7078154 :                     if (d >= 1LL << 31) {
     437     1346221 :                         coeff = 0.0;
     438             :                     } else {
     439     5731933 :                         int64_t dd  = (d  * d) >> 30;
     440     5731933 :                         int64_t ddd = (dd * d) >> 30;
     441             : 
     442     5731933 :                         if (d < 1LL << 30)
     443     8609076 :                             coeff =  (12 * (1 << 24) -  9 * B - 6 * C) * ddd +
     444     2869692 :                                     (-18 * (1 << 24) + 12 * B + 6 * C) *  dd +
     445     2869692 :                                       (6 * (1 << 24) -  2 * B)         * (1 << 30);
     446             :                         else
     447     8586723 :                             coeff =      (-B -  6 * C) * ddd +
     448     5724482 :                                       (6 * B + 30 * C) * dd  +
     449     2862241 :                                     (-12 * B - 48 * C) * d   +
     450     2862241 :                                       (8 * B + 24 * C) * (1 << 30);
     451             :                     }
     452     7078154 :                     coeff /= (1LL<<54)/fone;
     453      330977 :                 } else if (flags & SWS_X) {
     454           0 :                     double A = param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
     455             :                     double c;
     456             : 
     457           0 :                     if (floatd < 1.0)
     458           0 :                         c = cos(floatd * M_PI);
     459             :                     else
     460           0 :                         c = -1.0;
     461           0 :                     if (c < 0.0)
     462           0 :                         c = -pow(-c, A);
     463             :                     else
     464           0 :                         c = pow(c, A);
     465           0 :                     coeff = (c * 0.5 + 0.5) * fone;
     466      330977 :                 } else if (flags & SWS_AREA) {
     467      288927 :                     int64_t d2 = d - (1 << 29);
     468      288927 :                     if (d2 * xInc < -(1LL << (29 + 16)))
     469      218924 :                         coeff = 1.0 * (1LL << (30 + 16));
     470       70003 :                     else if (d2 * xInc < (1LL << (29 + 16)))
     471       60908 :                         coeff = -d2 * xInc + (1LL << (29 + 16));
     472             :                     else
     473        9095 :                         coeff = 0.0;
     474      288927 :                     coeff *= fone >> (30 + 16);
     475       42050 :                 } else if (flags & SWS_GAUSS) {
     476           0 :                     double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
     477           0 :                     coeff = exp2(-p * floatd * floatd) * fone;
     478       42050 :                 } else if (flags & SWS_SINC) {
     479           0 :                     coeff = (d ? sin(floatd * M_PI) / (floatd * M_PI) : 1.0) * fone;
     480       42050 :                 } else if (flags & SWS_LANCZOS) {
     481           0 :                     double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
     482           0 :                     coeff = (d ? sin(floatd * M_PI) * sin(floatd * M_PI / p) /
     483           0 :                              (floatd * floatd * M_PI * M_PI / p) : 1.0) * fone;
     484           0 :                     if (floatd > p)
     485           0 :                         coeff = 0;
     486       42050 :                 } else if (flags & SWS_BILINEAR) {
     487       42050 :                     coeff = (1 << 30) - d;
     488       42050 :                     if (coeff < 0)
     489        5238 :                         coeff = 0;
     490       42050 :                     coeff *= fone >> 30;
     491           0 :                 } else if (flags & SWS_SPLINE) {
     492           0 :                     double p = -2.196152422706632;
     493           0 :                     coeff = getSplineCoeff(1.0, 0.0, p, -p - 1.0, floatd) * fone;
     494             :                 } else {
     495           0 :                     av_assert0(0);
     496             :                 }
     497             : 
     498     7409131 :                 filter[i * filterSize + j] = coeff;
     499     7409131 :                 xx++;
     500             :             }
     501     1299003 :             xDstInSrc += 2 * xInc;
     502             :         }
     503             :     }
     504             : 
     505             :     /* apply src & dst Filter to filter -> filter2
     506             :      * av_free(filter);
     507             :      */
     508       11936 :     av_assert0(filterSize > 0);
     509       11936 :     filter2Size = filterSize;
     510       11936 :     if (srcFilter)
     511           0 :         filter2Size += srcFilter->length - 1;
     512       11936 :     if (dstFilter)
     513           0 :         filter2Size += dstFilter->length - 1;
     514       11936 :     av_assert0(filter2Size > 0);
     515       11936 :     FF_ALLOCZ_ARRAY_OR_GOTO(NULL, filter2, dstW, filter2Size * sizeof(*filter2), fail);
     516             : 
     517     3413185 :     for (i = 0; i < dstW; i++) {
     518             :         int j, k;
     519             : 
     520     3401249 :         if (srcFilter) {
     521           0 :             for (k = 0; k < srcFilter->length; k++) {
     522           0 :                 for (j = 0; j < filterSize; j++)
     523           0 :                     filter2[i * filter2Size + k + j] +=
     524           0 :                         srcFilter->coeff[k] * filter[i * filterSize + j];
     525             :             }
     526             :         } else {
     527    12941666 :             for (j = 0; j < filterSize; j++)
     528     9540417 :                 filter2[i * filter2Size + j] = filter[i * filterSize + j];
     529             :         }
     530             :         // FIXME dstFilter
     531             : 
     532     3401249 :         (*filterPos)[i] += (filterSize - 1) / 2 - (filter2Size - 1) / 2;
     533             :     }
     534       11936 :     av_freep(&filter);
     535             : 
     536             :     /* try to reduce the filter-size (step1 find size and shift left) */
     537             :     // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
     538       11936 :     minFilterSize = 0;
     539     3413185 :     for (i = dstW - 1; i >= 0; i--) {
     540     3401249 :         int min = filter2Size;
     541             :         int j;
     542     3401249 :         int64_t cutOff = 0.0;
     543             : 
     544             :         /* get rid of near zero elements on the left by shifting left */
     545     4067760 :         for (j = 0; j < filter2Size; j++) {
     546             :             int k;
     547     4067760 :             cutOff += FFABS(filter2[i * filter2Size]);
     548             : 
     549     4067760 :             if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
     550     3400148 :                 break;
     551             : 
     552             :             /* preserve monotonicity because the core can't handle the
     553             :              * filter otherwise */
     554      667612 :             if (i < dstW - 1 && (*filterPos)[i] >= (*filterPos)[i + 1])
     555        1101 :                 break;
     556             : 
     557             :             // move filter coefficients left
     558     3889189 :             for (k = 1; k < filter2Size; k++)
     559     3222678 :                 filter2[i * filter2Size + k - 1] = filter2[i * filter2Size + k];
     560      666511 :             filter2[i * filter2Size + k - 1] = 0;
     561      666511 :             (*filterPos)[i]++;
     562             :         }
     563             : 
     564     3401249 :         cutOff = 0;
     565             :         /* count near zeros on the right */
     566     4798213 :         for (j = filter2Size - 1; j > 0; j--) {
     567     2724904 :             cutOff += FFABS(filter2[i * filter2Size + j]);
     568             : 
     569     2724904 :             if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
     570     1327940 :                 break;
     571     1396964 :             min--;
     572             :         }
     573             : 
     574     3401249 :         if (min > minFilterSize)
     575       12402 :             minFilterSize = min;
     576             :     }
     577             : 
     578             :     if (PPC_ALTIVEC(cpu_flags)) {
     579             :         // we can handle the special case 4, so we don't want to go the full 8
     580             :         if (minFilterSize < 5)
     581             :             filterAlign = 4;
     582             : 
     583             :         /* We really don't want to waste our time doing useless computation, so
     584             :          * fall back on the scalar C code for very small filters.
     585             :          * Vectorizing is worth it only if you have a decent-sized vector. */
     586             :         if (minFilterSize < 3)
     587             :             filterAlign = 1;
     588             :     }
     589             : 
     590       11936 :     if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
     591             :         // special case for unscaled vertical filtering
     592          28 :         if (minFilterSize == 1 && filterAlign == 2)
     593          11 :             filterAlign = 1;
     594             :     }
     595             : 
     596       11936 :     av_assert0(minFilterSize > 0);
     597       11936 :     filterSize = (minFilterSize + (filterAlign - 1)) & (~(filterAlign - 1));
     598       11936 :     av_assert0(filterSize > 0);
     599       11936 :     filter = av_malloc_array(dstW, filterSize * sizeof(*filter));
     600       11936 :     if (!filter)
     601           0 :         goto fail;
     602       11936 :     if (filterSize >= MAX_FILTER_SIZE * 16 /
     603       11936 :                       ((flags & SWS_ACCURATE_RND) ? APCK_SIZE : 16)) {
     604           0 :         ret = RETCODE_USE_CASCADE;
     605           0 :         goto fail;
     606             :     }
     607       11936 :     *outFilterSize = filterSize;
     608             : 
     609       11936 :     if (flags & SWS_PRINT_INFO)
     610           0 :         av_log(NULL, AV_LOG_VERBOSE,
     611             :                "SwScaler: reducing / aligning filtersize %d -> %d\n",
     612             :                filter2Size, filterSize);
     613             :     /* try to reduce the filter-size (step2 reduce it) */
     614     3413185 :     for (i = 0; i < dstW; i++) {
     615             :         int j;
     616             : 
     617    11615895 :         for (j = 0; j < filterSize; j++) {
     618     8214646 :             if (j >= filter2Size)
     619        7056 :                 filter[i * filterSize + j] = 0;
     620             :             else
     621     8207590 :                 filter[i * filterSize + j] = filter2[i * filter2Size + j];
     622     8214646 :             if ((flags & SWS_BITEXACT) && j >= minFilterSize)
     623        4320 :                 filter[i * filterSize + j] = 0;
     624             :         }
     625             :     }
     626             : 
     627             :     // FIXME try to align filterPos if possible
     628             : 
     629             :     // fix borders
     630     3413185 :     for (i = 0; i < dstW; i++) {
     631             :         int j;
     632     3401249 :         if ((*filterPos)[i] < 0) {
     633             :             // move filter coefficients left to compensate for filterPos
     634       55867 :             for (j = 1; j < filterSize; j++) {
     635       44766 :                 int left = FFMAX(j + (*filterPos)[i], 0);
     636       44766 :                 filter[i * filterSize + left] += filter[i * filterSize + j];
     637       44766 :                 filter[i * filterSize + j]     = 0;
     638             :             }
     639       11101 :             (*filterPos)[i]= 0;
     640             :         }
     641             : 
     642     3401249 :         if ((*filterPos)[i] + filterSize > srcW) {
     643       15837 :             int shift = (*filterPos)[i] + FFMIN(filterSize - srcW, 0);
     644       15837 :             int64_t acc = 0;
     645             : 
     646       92982 :             for (j = filterSize - 1; j >= 0; j--) {
     647       77145 :                 if ((*filterPos)[i] + j >= srcW) {
     648       23866 :                     acc += filter[i * filterSize + j];
     649       23866 :                     filter[i * filterSize + j] = 0;
     650             :                 }
     651             :             }
     652       92982 :             for (j = filterSize - 1; j >= 0; j--) {
     653       77145 :                 if (j < shift) {
     654       23866 :                     filter[i * filterSize + j] = 0;
     655             :                 } else {
     656       53279 :                     filter[i * filterSize + j] = filter[i * filterSize + j - shift];
     657             :                 }
     658             :             }
     659             : 
     660       15837 :             (*filterPos)[i]-= shift;
     661       15837 :             filter[i * filterSize + srcW - 1 - (*filterPos)[i]] += acc;
     662             :         }
     663     3401249 :         av_assert0((*filterPos)[i] >= 0);
     664     3401249 :         av_assert0((*filterPos)[i] < srcW);
     665     3401249 :         if ((*filterPos)[i] + filterSize > srcW) {
     666           0 :             for (j = 0; j < filterSize; j++) {
     667           0 :                 av_assert0((*filterPos)[i] + j < srcW || !filter[i * filterSize + j]);
     668             :             }
     669             :         }
     670             :     }
     671             : 
     672             :     // Note the +1 is for the MMX scaler which reads over the end
     673             :     /* align at 16 for AltiVec (needed by hScale_altivec_real) */
     674       11936 :     FF_ALLOCZ_ARRAY_OR_GOTO(NULL, *outFilter,
     675             :                             (dstW + 3), *outFilterSize * sizeof(int16_t), fail);
     676             : 
     677             :     /* normalize & store in outFilter */
     678     3413185 :     for (i = 0; i < dstW; i++) {
     679             :         int j;
     680     3401249 :         int64_t error = 0;
     681     3401249 :         int64_t sum   = 0;
     682             : 
     683    11615895 :         for (j = 0; j < filterSize; j++) {
     684     8214646 :             sum += filter[i * filterSize + j];
     685             :         }
     686     3401249 :         sum = (sum + one / 2) / one;
     687     3401249 :         if (!sum) {
     688           0 :             av_log(NULL, AV_LOG_WARNING, "SwScaler: zero vector in scaling\n");
     689           0 :             sum = 1;
     690             :         }
     691    11615895 :         for (j = 0; j < *outFilterSize; j++) {
     692     8214646 :             int64_t v = filter[i * filterSize + j] + error;
     693     8214646 :             int intV  = ROUNDED_DIV(v, sum);
     694     8214646 :             (*outFilter)[i * (*outFilterSize) + j] = intV;
     695     8214646 :             error                                  = v - intV * sum;
     696             :         }
     697             :     }
     698             : 
     699       23872 :     (*filterPos)[dstW + 0] =
     700       35808 :     (*filterPos)[dstW + 1] =
     701       23872 :     (*filterPos)[dstW + 2] = (*filterPos)[dstW - 1]; /* the MMX/SSE scaler will
     702             :                                                       * read over the end */
     703       45212 :     for (i = 0; i < *outFilterSize; i++) {
     704       33276 :         int k = (dstW - 1) * (*outFilterSize) + i;
     705       66552 :         (*outFilter)[k + 1 * (*outFilterSize)] =
     706       99828 :         (*outFilter)[k + 2 * (*outFilterSize)] =
     707       66552 :         (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
     708             :     }
     709             : 
     710       11936 :     ret = 0;
     711             : 
     712       11936 : fail:
     713       11936 :     if(ret < 0)
     714           0 :         av_log(NULL, ret == RETCODE_USE_CASCADE ? AV_LOG_DEBUG : AV_LOG_ERROR, "sws: initFilter failed\n");
     715       11936 :     av_free(filter);
     716       11936 :     av_free(filter2);
     717       11936 :     return ret;
     718             : }
     719             : 
     720        1199 : static void fill_rgb2yuv_table(SwsContext *c, const int table[4], int dstRange)
     721             : {
     722             :     int64_t W, V, Z, Cy, Cu, Cv;
     723        1199 :     int64_t vr =  table[0];
     724        1199 :     int64_t ub =  table[1];
     725        1199 :     int64_t ug = -table[2];
     726        1199 :     int64_t vg = -table[3];
     727        1199 :     int64_t ONE = 65536;
     728        1199 :     int64_t cy = ONE;
     729        1199 :     uint8_t *p = (uint8_t*)c->input_rgb2yuv_table;
     730             :     int i;
     731             :     static const int8_t map[] = {
     732             :     BY_IDX, GY_IDX, -1    , BY_IDX, BY_IDX, GY_IDX, -1    , BY_IDX,
     733             :     RY_IDX, -1    , GY_IDX, RY_IDX, RY_IDX, -1    , GY_IDX, RY_IDX,
     734             :     RY_IDX, GY_IDX, -1    , RY_IDX, RY_IDX, GY_IDX, -1    , RY_IDX,
     735             :     BY_IDX, -1    , GY_IDX, BY_IDX, BY_IDX, -1    , GY_IDX, BY_IDX,
     736             :     BU_IDX, GU_IDX, -1    , BU_IDX, BU_IDX, GU_IDX, -1    , BU_IDX,
     737             :     RU_IDX, -1    , GU_IDX, RU_IDX, RU_IDX, -1    , GU_IDX, RU_IDX,
     738             :     RU_IDX, GU_IDX, -1    , RU_IDX, RU_IDX, GU_IDX, -1    , RU_IDX,
     739             :     BU_IDX, -1    , GU_IDX, BU_IDX, BU_IDX, -1    , GU_IDX, BU_IDX,
     740             :     BV_IDX, GV_IDX, -1    , BV_IDX, BV_IDX, GV_IDX, -1    , BV_IDX,
     741             :     RV_IDX, -1    , GV_IDX, RV_IDX, RV_IDX, -1    , GV_IDX, RV_IDX,
     742             :     RV_IDX, GV_IDX, -1    , RV_IDX, RV_IDX, GV_IDX, -1    , RV_IDX,
     743             :     BV_IDX, -1    , GV_IDX, BV_IDX, BV_IDX, -1    , GV_IDX, BV_IDX,
     744             :     RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX,
     745             :     BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX,
     746             :     GY_IDX, -1    , GY_IDX, -1    , GY_IDX, -1    , GY_IDX, -1    ,
     747             :     -1    , GY_IDX, -1    , GY_IDX, -1    , GY_IDX, -1    , GY_IDX,
     748             :     RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX,
     749             :     BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX,
     750             :     GU_IDX, -1    , GU_IDX, -1    , GU_IDX, -1    , GU_IDX, -1    ,
     751             :     -1    , GU_IDX, -1    , GU_IDX, -1    , GU_IDX, -1    , GU_IDX,
     752             :     RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX,
     753             :     BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX,
     754             :     GV_IDX, -1    , GV_IDX, -1    , GV_IDX, -1    , GV_IDX, -1    ,
     755             :     -1    , GV_IDX, -1    , GV_IDX, -1    , GV_IDX, -1    , GV_IDX, //23
     756             :     -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //24
     757             :     -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //25
     758             :     -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //26
     759             :     -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //27
     760             :     -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //28
     761             :     -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //29
     762             :     -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //30
     763             :     -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //31
     764             :     BY_IDX, GY_IDX, RY_IDX, -1    , -1    , -1    , -1    , -1    , //32
     765             :     BU_IDX, GU_IDX, RU_IDX, -1    , -1    , -1    , -1    , -1    , //33
     766             :     BV_IDX, GV_IDX, RV_IDX, -1    , -1    , -1    , -1    , -1    , //34
     767             :     };
     768             : 
     769        1199 :     dstRange = 0; //FIXME range = 1 is handled elsewhere
     770             : 
     771        1199 :     if (!dstRange) {
     772        1199 :         cy = cy * 255 / 219;
     773             :     } else {
     774           0 :         vr = vr * 224 / 255;
     775           0 :         ub = ub * 224 / 255;
     776           0 :         ug = ug * 224 / 255;
     777           0 :         vg = vg * 224 / 255;
     778             :     }
     779        1199 :     W = ROUNDED_DIV(ONE*ONE*ug, ub);
     780        1199 :     V = ROUNDED_DIV(ONE*ONE*vg, vr);
     781        1199 :     Z = ONE*ONE-W-V;
     782             : 
     783        1199 :     Cy = ROUNDED_DIV(cy*Z, ONE);
     784        1199 :     Cu = ROUNDED_DIV(ub*Z, ONE);
     785        1199 :     Cv = ROUNDED_DIV(vr*Z, ONE);
     786             : 
     787        1199 :     c->input_rgb2yuv_table[RY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V        , Cy);
     788        1199 :     c->input_rgb2yuv_table[GY_IDX] =  ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE  , Cy);
     789        1199 :     c->input_rgb2yuv_table[BY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W        , Cy);
     790             : 
     791        1199 :     c->input_rgb2yuv_table[RU_IDX] =  ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V        , Cu);
     792        1199 :     c->input_rgb2yuv_table[GU_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE  , Cu);
     793        1199 :     c->input_rgb2yuv_table[BU_IDX] =  ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(Z+W)    , Cu);
     794             : 
     795        1199 :     c->input_rgb2yuv_table[RV_IDX] =  ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(V+Z)    , Cv);
     796        1199 :     c->input_rgb2yuv_table[GV_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE  , Cv);
     797        1199 :     c->input_rgb2yuv_table[BV_IDX] =  ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W        , Cv);
     798             : 
     799        1199 :     if(/*!dstRange && */!memcmp(table, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], sizeof(ff_yuv2rgb_coeffs[SWS_CS_DEFAULT]))) {
     800        1199 :         c->input_rgb2yuv_table[BY_IDX] =  ((int)(0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
     801        1199 :         c->input_rgb2yuv_table[BV_IDX] = (-(int)(0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
     802        1199 :         c->input_rgb2yuv_table[BU_IDX] =  ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
     803        1199 :         c->input_rgb2yuv_table[GY_IDX] =  ((int)(0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
     804        1199 :         c->input_rgb2yuv_table[GV_IDX] = (-(int)(0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
     805        1199 :         c->input_rgb2yuv_table[GU_IDX] = (-(int)(0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
     806        1199 :         c->input_rgb2yuv_table[RY_IDX] =  ((int)(0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
     807        1199 :         c->input_rgb2yuv_table[RV_IDX] =  ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
     808        1199 :         c->input_rgb2yuv_table[RU_IDX] = (-(int)(0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
     809             :     }
     810      336919 :     for(i=0; i<FF_ARRAY_ELEMS(map); i++)
     811      335720 :         AV_WL16(p + 16*4 + 2*i, map[i] >= 0 ? c->input_rgb2yuv_table[map[i]] : 0);
     812        1199 : }
     813             : 
     814          83 : static void fill_xyztables(struct SwsContext *c)
     815             : {
     816             :     int i;
     817          83 :     double xyzgamma = XYZ_GAMMA;
     818          83 :     double rgbgamma = 1.0 / RGB_GAMMA;
     819          83 :     double xyzgammainv = 1.0 / XYZ_GAMMA;
     820          83 :     double rgbgammainv = RGB_GAMMA;
     821             :     static const int16_t xyz2rgb_matrix[3][4] = {
     822             :         {13270, -6295, -2041},
     823             :         {-3969,  7682,   170},
     824             :         {  228,  -835,  4329} };
     825             :     static const int16_t rgb2xyz_matrix[3][4] = {
     826             :         {1689, 1464,  739},
     827             :         { 871, 2929,  296},
     828             :         {  79,  488, 3891} };
     829             :     static int16_t xyzgamma_tab[4096], rgbgamma_tab[4096], xyzgammainv_tab[4096], rgbgammainv_tab[4096];
     830             : 
     831          83 :     memcpy(c->xyz2rgb_matrix, xyz2rgb_matrix, sizeof(c->xyz2rgb_matrix));
     832          83 :     memcpy(c->rgb2xyz_matrix, rgb2xyz_matrix, sizeof(c->rgb2xyz_matrix));
     833          83 :     c->xyzgamma = xyzgamma_tab;
     834          83 :     c->rgbgamma = rgbgamma_tab;
     835          83 :     c->xyzgammainv = xyzgammainv_tab;
     836          83 :     c->rgbgammainv = rgbgammainv_tab;
     837             : 
     838          83 :     if (rgbgamma_tab[4095])
     839          60 :         return;
     840             : 
     841             :     /* set gamma vectors */
     842       94231 :     for (i = 0; i < 4096; i++) {
     843       94208 :         xyzgamma_tab[i] = lrint(pow(i / 4095.0, xyzgamma) * 4095.0);
     844       94208 :         rgbgamma_tab[i] = lrint(pow(i / 4095.0, rgbgamma) * 4095.0);
     845       94208 :         xyzgammainv_tab[i] = lrint(pow(i / 4095.0, xyzgammainv) * 4095.0);
     846       94208 :         rgbgammainv_tab[i] = lrint(pow(i / 4095.0, rgbgammainv) * 4095.0);
     847             :     }
     848             : }
     849             : 
     850       41146 : int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
     851             :                              int srcRange, const int table[4], int dstRange,
     852             :                              int brightness, int contrast, int saturation)
     853             : {
     854             :     const AVPixFmtDescriptor *desc_dst;
     855             :     const AVPixFmtDescriptor *desc_src;
     856       41146 :     int need_reinit = 0;
     857             : 
     858       41146 :     handle_formats(c);
     859       41146 :     desc_dst = av_pix_fmt_desc_get(c->dstFormat);
     860       41146 :     desc_src = av_pix_fmt_desc_get(c->srcFormat);
     861             : 
     862       41146 :     if(!isYUV(c->dstFormat) && !isGray(c->dstFormat))
     863       16110 :         dstRange = 0;
     864       41146 :     if(!isYUV(c->srcFormat) && !isGray(c->srcFormat))
     865       12516 :         srcRange = 0;
     866             : 
     867       82292 :     if (c->srcRange != srcRange ||
     868       82292 :         c->dstRange != dstRange ||
     869       82292 :         c->brightness != brightness ||
     870       79308 :         c->contrast   != contrast ||
     871       76324 :         c->saturation != saturation ||
     872       76239 :         memcmp(c->srcColorspaceTable, inv_table, sizeof(int) * 4) ||
     873       38077 :         memcmp(c->dstColorspaceTable,     table, sizeof(int) * 4)
     874             :     )
     875        3069 :         need_reinit = 1;
     876             : 
     877       41146 :     memmove(c->srcColorspaceTable, inv_table, sizeof(int) * 4);
     878       41146 :     memmove(c->dstColorspaceTable, table, sizeof(int) * 4);
     879             : 
     880             : 
     881             : 
     882       41146 :     c->brightness = brightness;
     883       41146 :     c->contrast   = contrast;
     884       41146 :     c->saturation = saturation;
     885       41146 :     c->srcRange   = srcRange;
     886       41146 :     c->dstRange   = dstRange;
     887             : 
     888             :     //The srcBpc check is possibly wrong but we seem to lack a definitive reference to test this
     889             :     //and what we have in ticket 2939 looks better with this check
     890       41146 :     if (need_reinit && (c->srcBpc == 8 || !isYUV(c->srcFormat)))
     891         418 :         ff_sws_init_range_convert(c);
     892             : 
     893       41146 :     c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
     894       41146 :     c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
     895             : 
     896       41146 :     if (c->cascaded_context[c->cascaded_mainindex])
     897           0 :         return sws_setColorspaceDetails(c->cascaded_context[c->cascaded_mainindex],inv_table, srcRange,table, dstRange, brightness,  contrast, saturation);
     898             : 
     899       41146 :     if (!need_reinit)
     900       38077 :         return 0;
     901             : 
     902        3069 :     if ((isYUV(c->dstFormat) || isGray(c->dstFormat)) && (isYUV(c->srcFormat) || isGray(c->srcFormat))) {
     903        3740 :         if (!c->cascaded_context[0] &&
     904        1870 :             memcmp(c->dstColorspaceTable, c->srcColorspaceTable, sizeof(int) * 4) &&
     905           0 :             c->srcW && c->srcH && c->dstW && c->dstH) {
     906             :             enum AVPixelFormat tmp_format;
     907             :             int tmp_width, tmp_height;
     908           0 :             int srcW = c->srcW;
     909           0 :             int srcH = c->srcH;
     910           0 :             int dstW = c->dstW;
     911           0 :             int dstH = c->dstH;
     912             :             int ret;
     913           0 :             av_log(c, AV_LOG_VERBOSE, "YUV color matrix differs for YUV->YUV, using intermediate RGB to convert\n");
     914             : 
     915           0 :             if (isNBPS(c->dstFormat) || is16BPS(c->dstFormat)) {
     916           0 :                 if (isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) {
     917           0 :                     tmp_format = AV_PIX_FMT_BGRA64;
     918             :                 } else {
     919           0 :                     tmp_format = AV_PIX_FMT_BGR48;
     920             :                 }
     921             :             } else {
     922           0 :                 if (isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) {
     923           0 :                     tmp_format = AV_PIX_FMT_BGRA;
     924             :                 } else {
     925           0 :                     tmp_format = AV_PIX_FMT_BGR24;
     926             :                 }
     927             :             }
     928             : 
     929           0 :             if (srcW*srcH > dstW*dstH) {
     930           0 :                 tmp_width  = dstW;
     931           0 :                 tmp_height = dstH;
     932             :             } else {
     933           0 :                 tmp_width  = srcW;
     934           0 :                 tmp_height = srcH;
     935             :             }
     936             : 
     937           0 :             ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
     938             :                                 tmp_width, tmp_height, tmp_format, 64);
     939           0 :             if (ret < 0)
     940           0 :                 return ret;
     941             : 
     942           0 :             c->cascaded_context[0] = sws_alloc_set_opts(srcW, srcH, c->srcFormat,
     943             :                                                         tmp_width, tmp_height, tmp_format,
     944           0 :                                                         c->flags, c->param);
     945           0 :             if (!c->cascaded_context[0])
     946           0 :                 return -1;
     947             : 
     948           0 :             c->cascaded_context[0]->alphablend = c->alphablend;
     949           0 :             ret = sws_init_context(c->cascaded_context[0], NULL , NULL);
     950           0 :             if (ret < 0)
     951           0 :                 return ret;
     952             :             //we set both src and dst depending on that the RGB side will be ignored
     953           0 :             sws_setColorspaceDetails(c->cascaded_context[0], inv_table,
     954             :                                      srcRange, table, dstRange,
     955             :                                      brightness, contrast, saturation);
     956             : 
     957           0 :             c->cascaded_context[1] = sws_getContext(tmp_width, tmp_height, tmp_format,
     958             :                                                     dstW, dstH, c->dstFormat,
     959           0 :                                                     c->flags, NULL, NULL, c->param);
     960           0 :             if (!c->cascaded_context[1])
     961           0 :                 return -1;
     962           0 :             sws_setColorspaceDetails(c->cascaded_context[1], inv_table,
     963             :                                      srcRange, table, dstRange,
     964             :                                      0, 1 << 16, 1 << 16);
     965           0 :             return 0;
     966             :         }
     967        1870 :         return -1;
     968             :     }
     969             : 
     970        1199 :     if (!isYUV(c->dstFormat) && !isGray(c->dstFormat)) {
     971        1083 :         ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness,
     972             :                                  contrast, saturation);
     973             :         // FIXME factorize
     974             : 
     975             :         if (ARCH_PPC)
     976             :             ff_yuv2rgb_init_tables_ppc(c, inv_table, brightness,
     977             :                                        contrast, saturation);
     978             :     }
     979             : 
     980        1199 :     fill_rgb2yuv_table(c, table, dstRange);
     981             : 
     982        1199 :     return 0;
     983             : }
     984             : 
     985       38162 : int sws_getColorspaceDetails(struct SwsContext *c, int **inv_table,
     986             :                              int *srcRange, int **table, int *dstRange,
     987             :                              int *brightness, int *contrast, int *saturation)
     988             : {
     989       38162 :     if (!c )
     990           0 :         return -1;
     991             : 
     992       38162 :     *inv_table  = c->srcColorspaceTable;
     993       38162 :     *table      = c->dstColorspaceTable;
     994       38162 :     *srcRange   = c->srcRange;
     995       38162 :     *dstRange   = c->dstRange;
     996       38162 :     *brightness = c->brightness;
     997       38162 :     *contrast   = c->contrast;
     998       38162 :     *saturation = c->saturation;
     999             : 
    1000       38162 :     return 0;
    1001             : }
    1002             : 
    1003        5968 : static int handle_jpeg(enum AVPixelFormat *format)
    1004             : {
    1005        5968 :     switch (*format) {
    1006          68 :     case AV_PIX_FMT_YUVJ420P:
    1007          68 :         *format = AV_PIX_FMT_YUV420P;
    1008          68 :         return 1;
    1009          15 :     case AV_PIX_FMT_YUVJ411P:
    1010          15 :         *format = AV_PIX_FMT_YUV411P;
    1011          15 :         return 1;
    1012          30 :     case AV_PIX_FMT_YUVJ422P:
    1013          30 :         *format = AV_PIX_FMT_YUV422P;
    1014          30 :         return 1;
    1015          39 :     case AV_PIX_FMT_YUVJ444P:
    1016          39 :         *format = AV_PIX_FMT_YUV444P;
    1017          39 :         return 1;
    1018          21 :     case AV_PIX_FMT_YUVJ440P:
    1019          21 :         *format = AV_PIX_FMT_YUV440P;
    1020          21 :         return 1;
    1021         206 :     case AV_PIX_FMT_GRAY8:
    1022             :     case AV_PIX_FMT_YA8:
    1023             :     case AV_PIX_FMT_GRAY9LE:
    1024             :     case AV_PIX_FMT_GRAY9BE:
    1025             :     case AV_PIX_FMT_GRAY10LE:
    1026             :     case AV_PIX_FMT_GRAY10BE:
    1027             :     case AV_PIX_FMT_GRAY12LE:
    1028             :     case AV_PIX_FMT_GRAY12BE:
    1029             :     case AV_PIX_FMT_GRAY14LE:
    1030             :     case AV_PIX_FMT_GRAY14BE:
    1031             :     case AV_PIX_FMT_GRAY16LE:
    1032             :     case AV_PIX_FMT_GRAY16BE:
    1033             :     case AV_PIX_FMT_YA16BE:
    1034             :     case AV_PIX_FMT_YA16LE:
    1035         206 :         return 1;
    1036        5589 :     default:
    1037        5589 :         return 0;
    1038             :     }
    1039             : }
    1040             : 
    1041       88260 : static int handle_0alpha(enum AVPixelFormat *format)
    1042             : {
    1043       88260 :     switch (*format) {
    1044          16 :     case AV_PIX_FMT_0BGR    : *format = AV_PIX_FMT_ABGR   ; return 1;
    1045          47 :     case AV_PIX_FMT_BGR0    : *format = AV_PIX_FMT_BGRA   ; return 4;
    1046          16 :     case AV_PIX_FMT_0RGB    : *format = AV_PIX_FMT_ARGB   ; return 1;
    1047          16 :     case AV_PIX_FMT_RGB0    : *format = AV_PIX_FMT_RGBA   ; return 4;
    1048       88165 :     default:                                          return 0;
    1049             :     }
    1050             : }
    1051             : 
    1052       88260 : static int handle_xyz(enum AVPixelFormat *format)
    1053             : {
    1054       88260 :     switch (*format) {
    1055          13 :     case AV_PIX_FMT_XYZ12BE : *format = AV_PIX_FMT_RGB48BE; return 1;
    1056          14 :     case AV_PIX_FMT_XYZ12LE : *format = AV_PIX_FMT_RGB48LE; return 1;
    1057       88233 :     default:                                                return 0;
    1058             :     }
    1059             : }
    1060             : 
    1061       44130 : static void handle_formats(SwsContext *c)
    1062             : {
    1063       44130 :     c->src0Alpha |= handle_0alpha(&c->srcFormat);
    1064       44130 :     c->dst0Alpha |= handle_0alpha(&c->dstFormat);
    1065       44130 :     c->srcXYZ    |= handle_xyz(&c->srcFormat);
    1066       44130 :     c->dstXYZ    |= handle_xyz(&c->dstFormat);
    1067       44130 :     if (c->srcXYZ || c->dstXYZ)
    1068          83 :         fill_xyztables(c);
    1069       44130 : }
    1070             : 
    1071        9559 : SwsContext *sws_alloc_context(void)
    1072             : {
    1073        9559 :     SwsContext *c = av_mallocz(sizeof(SwsContext));
    1074             : 
    1075             :     av_assert0(offsetof(SwsContext, redDither) + DITHER32_INT == offsetof(SwsContext, dither32));
    1076             : 
    1077        9559 :     if (c) {
    1078        9559 :         c->av_class = &ff_sws_context_class;
    1079        9559 :         av_opt_set_defaults(c);
    1080             :     }
    1081             : 
    1082        9559 :     return c;
    1083             : }
    1084             : 
    1085           0 : static uint16_t * alloc_gamma_tbl(double e)
    1086             : {
    1087           0 :     int i = 0;
    1088             :     uint16_t * tbl;
    1089           0 :     tbl = (uint16_t*)av_malloc(sizeof(uint16_t) * 1 << 16);
    1090           0 :     if (!tbl)
    1091           0 :         return NULL;
    1092             : 
    1093           0 :     for (i = 0; i < 65536; ++i) {
    1094           0 :         tbl[i] = pow(i / 65535.0, e) * 65535.0;
    1095             :     }
    1096           0 :     return tbl;
    1097             : }
    1098             : 
    1099         131 : static enum AVPixelFormat alphaless_fmt(enum AVPixelFormat fmt)
    1100             : {
    1101         131 :     switch(fmt) {
    1102           1 :     case AV_PIX_FMT_ARGB:       return AV_PIX_FMT_RGB24;
    1103           1 :     case AV_PIX_FMT_RGBA:       return AV_PIX_FMT_RGB24;
    1104           0 :     case AV_PIX_FMT_ABGR:       return AV_PIX_FMT_BGR24;
    1105          29 :     case AV_PIX_FMT_BGRA:       return AV_PIX_FMT_BGR24;
    1106           2 :     case AV_PIX_FMT_YA8:        return AV_PIX_FMT_GRAY8;
    1107             : 
    1108           0 :     case AV_PIX_FMT_YUVA420P:   return AV_PIX_FMT_YUV420P;
    1109           3 :     case AV_PIX_FMT_YUVA422P:   return AV_PIX_FMT_YUV422P;
    1110           4 :     case AV_PIX_FMT_YUVA444P:           return AV_PIX_FMT_YUV444P;
    1111             : 
    1112           5 :     case AV_PIX_FMT_GBRAP:              return AV_PIX_FMT_GBRP;
    1113             : 
    1114           0 :     case AV_PIX_FMT_GBRAP10LE:          return AV_PIX_FMT_GBRP10;
    1115           0 :     case AV_PIX_FMT_GBRAP10BE:          return AV_PIX_FMT_GBRP10;
    1116             : 
    1117           0 :     case AV_PIX_FMT_GBRAP12LE:          return AV_PIX_FMT_GBRP12;
    1118           0 :     case AV_PIX_FMT_GBRAP12BE:          return AV_PIX_FMT_GBRP12;
    1119             : 
    1120           0 :     case AV_PIX_FMT_GBRAP16LE:          return AV_PIX_FMT_GBRP16;
    1121           1 :     case AV_PIX_FMT_GBRAP16BE:          return AV_PIX_FMT_GBRP16;
    1122             : 
    1123           0 :     case AV_PIX_FMT_RGBA64LE:   return AV_PIX_FMT_RGB48;
    1124           1 :     case AV_PIX_FMT_RGBA64BE:   return AV_PIX_FMT_RGB48;
    1125           0 :     case AV_PIX_FMT_BGRA64LE:   return AV_PIX_FMT_BGR48;
    1126           0 :     case AV_PIX_FMT_BGRA64BE:   return AV_PIX_FMT_BGR48;
    1127             : 
    1128           2 :     case AV_PIX_FMT_YA16BE:             return AV_PIX_FMT_GRAY16;
    1129           0 :     case AV_PIX_FMT_YA16LE:             return AV_PIX_FMT_GRAY16;
    1130             : 
    1131           0 :     case AV_PIX_FMT_YUVA420P9BE:        return AV_PIX_FMT_YUV420P9;
    1132           0 :     case AV_PIX_FMT_YUVA422P9BE:        return AV_PIX_FMT_YUV422P9;
    1133           0 :     case AV_PIX_FMT_YUVA444P9BE:        return AV_PIX_FMT_YUV444P9;
    1134           0 :     case AV_PIX_FMT_YUVA420P9LE:        return AV_PIX_FMT_YUV420P9;
    1135           0 :     case AV_PIX_FMT_YUVA422P9LE:        return AV_PIX_FMT_YUV422P9;
    1136           0 :     case AV_PIX_FMT_YUVA444P9LE:        return AV_PIX_FMT_YUV444P9;
    1137           0 :     case AV_PIX_FMT_YUVA420P10BE:       return AV_PIX_FMT_YUV420P10;
    1138           0 :     case AV_PIX_FMT_YUVA422P10BE:       return AV_PIX_FMT_YUV422P10;
    1139           0 :     case AV_PIX_FMT_YUVA444P10BE:       return AV_PIX_FMT_YUV444P10;
    1140           0 :     case AV_PIX_FMT_YUVA420P10LE:       return AV_PIX_FMT_YUV420P10;
    1141           0 :     case AV_PIX_FMT_YUVA422P10LE:       return AV_PIX_FMT_YUV422P10;
    1142           0 :     case AV_PIX_FMT_YUVA444P10LE:       return AV_PIX_FMT_YUV444P10;
    1143           0 :     case AV_PIX_FMT_YUVA420P16BE:       return AV_PIX_FMT_YUV420P16;
    1144           0 :     case AV_PIX_FMT_YUVA422P16BE:       return AV_PIX_FMT_YUV422P16;
    1145           0 :     case AV_PIX_FMT_YUVA444P16BE:       return AV_PIX_FMT_YUV444P16;
    1146           0 :     case AV_PIX_FMT_YUVA420P16LE:       return AV_PIX_FMT_YUV420P16;
    1147           1 :     case AV_PIX_FMT_YUVA422P16LE:       return AV_PIX_FMT_YUV422P16;
    1148           0 :     case AV_PIX_FMT_YUVA444P16LE:       return AV_PIX_FMT_YUV444P16;
    1149             : 
    1150             : //     case AV_PIX_FMT_AYUV64LE:
    1151             : //     case AV_PIX_FMT_AYUV64BE:
    1152             : //     case AV_PIX_FMT_PAL8:
    1153          81 :     default: return AV_PIX_FMT_NONE;
    1154             :     }
    1155             : }
    1156             : 
    1157        2984 : av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
    1158             :                              SwsFilter *dstFilter)
    1159             : {
    1160             :     int i;
    1161             :     int usesVFilter, usesHFilter;
    1162             :     int unscaled;
    1163        2984 :     SwsFilter dummyFilter = { NULL, NULL, NULL, NULL };
    1164        2984 :     int srcW              = c->srcW;
    1165        2984 :     int srcH              = c->srcH;
    1166        2984 :     int dstW              = c->dstW;
    1167        2984 :     int dstH              = c->dstH;
    1168        2984 :     int dst_stride        = FFALIGN(dstW * sizeof(int16_t) + 66, 16);
    1169             :     int flags, cpu_flags;
    1170        2984 :     enum AVPixelFormat srcFormat = c->srcFormat;
    1171        2984 :     enum AVPixelFormat dstFormat = c->dstFormat;
    1172             :     const AVPixFmtDescriptor *desc_src;
    1173             :     const AVPixFmtDescriptor *desc_dst;
    1174        2984 :     int ret = 0;
    1175             :     enum AVPixelFormat tmpFmt;
    1176             : 
    1177        2984 :     cpu_flags = av_get_cpu_flags();
    1178        2984 :     flags     = c->flags;
    1179        2984 :     emms_c();
    1180        2984 :     if (!rgb15to16)
    1181        2778 :         ff_sws_rgb2rgb_init();
    1182             : 
    1183        2984 :     unscaled = (srcW == dstW && srcH == dstH);
    1184             : 
    1185        2984 :     c->srcRange |= handle_jpeg(&c->srcFormat);
    1186        2984 :     c->dstRange |= handle_jpeg(&c->dstFormat);
    1187             : 
    1188        2984 :     if(srcFormat!=c->srcFormat || dstFormat!=c->dstFormat)
    1189         168 :         av_log(c, AV_LOG_WARNING, "deprecated pixel format used, make sure you did set range correctly\n");
    1190             : 
    1191        2984 :     if (!c->contrast && !c->saturation && !c->dstFormatBpp)
    1192        2984 :         sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange,
    1193             :                                  ff_yuv2rgb_coeffs[SWS_CS_DEFAULT],
    1194             :                                  c->dstRange, 0, 1 << 16, 1 << 16);
    1195             : 
    1196        2984 :     handle_formats(c);
    1197        2984 :     srcFormat = c->srcFormat;
    1198        2984 :     dstFormat = c->dstFormat;
    1199        2984 :     desc_src = av_pix_fmt_desc_get(srcFormat);
    1200        2984 :     desc_dst = av_pix_fmt_desc_get(dstFormat);
    1201             : 
    1202             :     // If the source has no alpha then disable alpha blendaway
    1203        2984 :     if (c->src0Alpha)
    1204          25 :         c->alphablend = SWS_ALPHA_BLEND_NONE;
    1205             : 
    1206        2986 :     if (!(unscaled && sws_isSupportedEndiannessConversion(srcFormat) &&
    1207           2 :           av_pix_fmt_swap_endianness(srcFormat) == dstFormat)) {
    1208        2984 :     if (!sws_isSupportedInput(srcFormat)) {
    1209           0 :         av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n",
    1210             :                av_get_pix_fmt_name(srcFormat));
    1211           0 :         return AVERROR(EINVAL);
    1212             :     }
    1213        2984 :     if (!sws_isSupportedOutput(dstFormat)) {
    1214           0 :         av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n",
    1215             :                av_get_pix_fmt_name(dstFormat));
    1216           0 :         return AVERROR(EINVAL);
    1217             :     }
    1218             :     }
    1219             :     av_assert2(desc_src && desc_dst);
    1220             : 
    1221        2984 :     i = flags & (SWS_POINT         |
    1222             :                  SWS_AREA          |
    1223             :                  SWS_BILINEAR      |
    1224             :                  SWS_FAST_BILINEAR |
    1225             :                  SWS_BICUBIC       |
    1226             :                  SWS_X             |
    1227             :                  SWS_GAUSS         |
    1228             :                  SWS_LANCZOS       |
    1229             :                  SWS_SINC          |
    1230             :                  SWS_SPLINE        |
    1231             :                  SWS_BICUBLIN);
    1232             : 
    1233             :     /* provide a default scaler if not set by caller */
    1234        2984 :     if (!i) {
    1235          30 :         if (dstW < srcW && dstH < srcH)
    1236           8 :             flags |= SWS_BICUBIC;
    1237          22 :         else if (dstW > srcW && dstH > srcH)
    1238           1 :             flags |= SWS_BICUBIC;
    1239             :         else
    1240          21 :             flags |= SWS_BICUBIC;
    1241          30 :         c->flags = flags;
    1242        2954 :     } else if (i & (i - 1)) {
    1243           0 :         av_log(c, AV_LOG_ERROR,
    1244             :                "Exactly one scaler algorithm must be chosen, got %X\n", i);
    1245           0 :         return AVERROR(EINVAL);
    1246             :     }
    1247             :     /* sanity check */
    1248        2984 :     if (srcW < 1 || srcH < 1 || dstW < 1 || dstH < 1) {
    1249             :         /* FIXME check if these are enough and try to lower them after
    1250             :          * fixing the relevant parts of the code */
    1251           0 :         av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
    1252             :                srcW, srcH, dstW, dstH);
    1253           0 :         return AVERROR(EINVAL);
    1254             :     }
    1255        2984 :     if (flags & SWS_FAST_BILINEAR) {
    1256           0 :         if (srcW < 8 || dstW < 8) {
    1257           0 :             flags ^= SWS_FAST_BILINEAR | SWS_BILINEAR;
    1258           0 :             c->flags = flags;
    1259             :         }
    1260             :     }
    1261             : 
    1262        2984 :     if (!dstFilter)
    1263        2984 :         dstFilter = &dummyFilter;
    1264        2984 :     if (!srcFilter)
    1265        2984 :         srcFilter = &dummyFilter;
    1266             : 
    1267        2984 :     c->lumXInc      = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW;
    1268        2984 :     c->lumYInc      = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH;
    1269        2984 :     c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
    1270        2984 :     c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
    1271        2984 :     c->vRounder     = 4 * 0x0001000100010001ULL;
    1272             : 
    1273        8952 :     usesVFilter = (srcFilter->lumV && srcFilter->lumV->length > 1) ||
    1274        5968 :                   (srcFilter->chrV && srcFilter->chrV->length > 1) ||
    1275        8952 :                   (dstFilter->lumV && dstFilter->lumV->length > 1) ||
    1276        2984 :                   (dstFilter->chrV && dstFilter->chrV->length > 1);
    1277        8952 :     usesHFilter = (srcFilter->lumH && srcFilter->lumH->length > 1) ||
    1278        5968 :                   (srcFilter->chrH && srcFilter->chrH->length > 1) ||
    1279        8952 :                   (dstFilter->lumH && dstFilter->lumH->length > 1) ||
    1280        2984 :                   (dstFilter->chrH && dstFilter->chrH->length > 1);
    1281             : 
    1282        2984 :     av_pix_fmt_get_chroma_sub_sample(srcFormat, &c->chrSrcHSubSample, &c->chrSrcVSubSample);
    1283        2984 :     av_pix_fmt_get_chroma_sub_sample(dstFormat, &c->chrDstHSubSample, &c->chrDstVSubSample);
    1284             : 
    1285        2984 :     if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) {
    1286        1071 :         if (dstW&1) {
    1287          28 :             av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to odd output size\n");
    1288          28 :             flags |= SWS_FULL_CHR_H_INT;
    1289          28 :             c->flags = flags;
    1290             :         }
    1291             : 
    1292        1071 :         if (   c->chrSrcHSubSample == 0
    1293         237 :             && c->chrSrcVSubSample == 0
    1294         237 :             && c->dither != SWS_DITHER_BAYER //SWS_FULL_CHR_H_INT is currently not supported with SWS_DITHER_BAYER
    1295         237 :             && !(c->flags & SWS_FAST_BILINEAR)
    1296             :         ) {
    1297         237 :             av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to input having non subsampled chroma\n");
    1298         237 :             flags |= SWS_FULL_CHR_H_INT;
    1299         237 :             c->flags = flags;
    1300             :         }
    1301             :     }
    1302             : 
    1303        2984 :     if (c->dither == SWS_DITHER_AUTO) {
    1304        2984 :         if (flags & SWS_ERROR_DIFFUSION)
    1305           0 :             c->dither = SWS_DITHER_ED;
    1306             :     }
    1307             : 
    1308        2984 :     if(dstFormat == AV_PIX_FMT_BGR4_BYTE ||
    1309        2957 :        dstFormat == AV_PIX_FMT_RGB4_BYTE ||
    1310        2935 :        dstFormat == AV_PIX_FMT_BGR8 ||
    1311             :        dstFormat == AV_PIX_FMT_RGB8) {
    1312          64 :         if (c->dither == SWS_DITHER_AUTO)
    1313          64 :             c->dither = (flags & SWS_FULL_CHR_H_INT) ? SWS_DITHER_ED : SWS_DITHER_BAYER;
    1314          64 :         if (!(flags & SWS_FULL_CHR_H_INT)) {
    1315          54 :             if (c->dither == SWS_DITHER_ED || c->dither == SWS_DITHER_A_DITHER || c->dither == SWS_DITHER_X_DITHER) {
    1316           0 :                 av_log(c, AV_LOG_DEBUG,
    1317             :                     "Desired dithering only supported in full chroma interpolation for destination format '%s'\n",
    1318             :                     av_get_pix_fmt_name(dstFormat));
    1319           0 :                 flags   |= SWS_FULL_CHR_H_INT;
    1320           0 :                 c->flags = flags;
    1321             :             }
    1322             :         }
    1323          64 :         if (flags & SWS_FULL_CHR_H_INT) {
    1324          10 :             if (c->dither == SWS_DITHER_BAYER) {
    1325           0 :                 av_log(c, AV_LOG_DEBUG,
    1326             :                     "Ordered dither is not supported in full chroma interpolation for destination format '%s'\n",
    1327             :                     av_get_pix_fmt_name(dstFormat));
    1328           0 :                 c->dither = SWS_DITHER_ED;
    1329             :             }
    1330             :         }
    1331             :     }
    1332        2984 :     if (isPlanarRGB(dstFormat)) {
    1333         266 :         if (!(flags & SWS_FULL_CHR_H_INT)) {
    1334         233 :             av_log(c, AV_LOG_DEBUG,
    1335             :                    "%s output is not supported with half chroma resolution, switching to full\n",
    1336             :                    av_get_pix_fmt_name(dstFormat));
    1337         233 :             flags   |= SWS_FULL_CHR_H_INT;
    1338         233 :             c->flags = flags;
    1339             :         }
    1340             :     }
    1341             : 
    1342             :     /* reuse chroma for 2 pixels RGB/BGR unless user wants full
    1343             :      * chroma interpolation */
    1344        3466 :     if (flags & SWS_FULL_CHR_H_INT &&
    1345         964 :         isAnyRGB(dstFormat)        &&
    1346         698 :         !isPlanarRGB(dstFormat)    &&
    1347         215 :         dstFormat != AV_PIX_FMT_RGBA64LE &&
    1348         214 :         dstFormat != AV_PIX_FMT_RGBA64BE &&
    1349         213 :         dstFormat != AV_PIX_FMT_BGRA64LE &&
    1350         212 :         dstFormat != AV_PIX_FMT_BGRA64BE &&
    1351         209 :         dstFormat != AV_PIX_FMT_RGB48LE &&
    1352         206 :         dstFormat != AV_PIX_FMT_RGB48BE &&
    1353         205 :         dstFormat != AV_PIX_FMT_BGR48LE &&
    1354         204 :         dstFormat != AV_PIX_FMT_BGR48BE &&
    1355         197 :         dstFormat != AV_PIX_FMT_RGBA  &&
    1356         194 :         dstFormat != AV_PIX_FMT_ARGB  &&
    1357         160 :         dstFormat != AV_PIX_FMT_BGRA  &&
    1358         158 :         dstFormat != AV_PIX_FMT_ABGR  &&
    1359          35 :         dstFormat != AV_PIX_FMT_RGB24 &&
    1360          24 :         dstFormat != AV_PIX_FMT_BGR24 &&
    1361          22 :         dstFormat != AV_PIX_FMT_BGR4_BYTE &&
    1362          20 :         dstFormat != AV_PIX_FMT_RGB4_BYTE &&
    1363          16 :         dstFormat != AV_PIX_FMT_BGR8 &&
    1364             :         dstFormat != AV_PIX_FMT_RGB8
    1365             :     ) {
    1366          14 :         av_log(c, AV_LOG_WARNING,
    1367             :                "full chroma interpolation for destination format '%s' not yet implemented\n",
    1368             :                av_get_pix_fmt_name(dstFormat));
    1369          14 :         flags   &= ~SWS_FULL_CHR_H_INT;
    1370          14 :         c->flags = flags;
    1371             :     }
    1372        2984 :     if (isAnyRGB(dstFormat) && !(flags & SWS_FULL_CHR_H_INT))
    1373         615 :         c->chrDstHSubSample = 1;
    1374             : 
    1375             :     // drop some chroma lines if the user wants it
    1376        2984 :     c->vChrDrop          = (flags & SWS_SRC_V_CHR_DROP_MASK) >>
    1377             :                            SWS_SRC_V_CHR_DROP_SHIFT;
    1378        2984 :     c->chrSrcVSubSample += c->vChrDrop;
    1379             : 
    1380             :     /* drop every other pixel for chroma calculation unless user
    1381             :      * wants full chroma */
    1382        2984 :     if (isAnyRGB(srcFormat) && !(flags & SWS_FULL_CHR_H_INP)   &&
    1383         250 :         srcFormat != AV_PIX_FMT_RGB8 && srcFormat != AV_PIX_FMT_BGR8 &&
    1384         249 :         srcFormat != AV_PIX_FMT_RGB4 && srcFormat != AV_PIX_FMT_BGR4 &&
    1385         248 :         srcFormat != AV_PIX_FMT_RGB4_BYTE && srcFormat != AV_PIX_FMT_BGR4_BYTE &&
    1386         246 :         srcFormat != AV_PIX_FMT_GBRP9BE   && srcFormat != AV_PIX_FMT_GBRP9LE  &&
    1387         244 :         srcFormat != AV_PIX_FMT_GBRP10BE  && srcFormat != AV_PIX_FMT_GBRP10LE &&
    1388         241 :         srcFormat != AV_PIX_FMT_GBRAP10BE && srcFormat != AV_PIX_FMT_GBRAP10LE &&
    1389         239 :         srcFormat != AV_PIX_FMT_GBRP12BE  && srcFormat != AV_PIX_FMT_GBRP12LE &&
    1390         237 :         srcFormat != AV_PIX_FMT_GBRAP12BE && srcFormat != AV_PIX_FMT_GBRAP12LE &&
    1391         235 :         srcFormat != AV_PIX_FMT_GBRP14BE  && srcFormat != AV_PIX_FMT_GBRP14LE &&
    1392         232 :         srcFormat != AV_PIX_FMT_GBRP16BE  && srcFormat != AV_PIX_FMT_GBRP16LE &&
    1393         446 :         srcFormat != AV_PIX_FMT_GBRAP16BE  && srcFormat != AV_PIX_FMT_GBRAP16LE &&
    1394         341 :         ((dstW >> c->chrDstHSubSample) <= (srcW >> 1) ||
    1395         119 :          (flags & SWS_FAST_BILINEAR)))
    1396         103 :         c->chrSrcHSubSample = 1;
    1397             : 
    1398             :     // Note the AV_CEIL_RSHIFT is so that we always round toward +inf.
    1399        2984 :     c->chrSrcW = AV_CEIL_RSHIFT(srcW, c->chrSrcHSubSample);
    1400        2984 :     c->chrSrcH = AV_CEIL_RSHIFT(srcH, c->chrSrcVSubSample);
    1401        2984 :     c->chrDstW = AV_CEIL_RSHIFT(dstW, c->chrDstHSubSample);
    1402        2984 :     c->chrDstH = AV_CEIL_RSHIFT(dstH, c->chrDstVSubSample);
    1403             : 
    1404        2984 :     FF_ALLOCZ_OR_GOTO(c, c->formatConvBuffer, FFALIGN(srcW*2+78, 16) * 2, fail);
    1405             : 
    1406        2984 :     c->srcBpc = desc_src->comp[0].depth;
    1407        2984 :     if (c->srcBpc < 8)
    1408          56 :         c->srcBpc = 8;
    1409        2984 :     c->dstBpc = desc_dst->comp[0].depth;
    1410        2984 :     if (c->dstBpc < 8)
    1411         265 :         c->dstBpc = 8;
    1412        2984 :     if (isAnyRGB(srcFormat) || srcFormat == AV_PIX_FMT_PAL8)
    1413         340 :         c->srcBpc = 16;
    1414        2984 :     if (c->dstBpc == 16)
    1415         440 :         dst_stride <<= 1;
    1416             : 
    1417        2984 :     if (INLINE_MMXEXT(cpu_flags) && c->srcBpc == 8 && c->dstBpc <= 14) {
    1418           6 :         c->canMMXEXTBeUsed = dstW >= srcW && (dstW & 31) == 0 &&
    1419           6 :                              c->chrDstW >= c->chrSrcW &&
    1420           2 :                              (srcW & 15) == 0;
    1421           2 :         if (!c->canMMXEXTBeUsed && dstW >= srcW && c->chrDstW >= c->chrSrcW && (srcW & 15) == 0
    1422             : 
    1423           0 :             && (flags & SWS_FAST_BILINEAR)) {
    1424           0 :             if (flags & SWS_PRINT_INFO)
    1425           0 :                 av_log(c, AV_LOG_INFO,
    1426             :                        "output width is not a multiple of 32 -> no MMXEXT scaler\n");
    1427             :         }
    1428           4 :         if (usesHFilter || isNBPS(c->srcFormat) || is16BPS(c->srcFormat) || isAnyRGB(c->srcFormat))
    1429           0 :             c->canMMXEXTBeUsed = 0;
    1430             :     } else
    1431        2982 :         c->canMMXEXTBeUsed = 0;
    1432             : 
    1433        2984 :     c->chrXInc = (((int64_t)c->chrSrcW << 16) + (c->chrDstW >> 1)) / c->chrDstW;
    1434        2984 :     c->chrYInc = (((int64_t)c->chrSrcH << 16) + (c->chrDstH >> 1)) / c->chrDstH;
    1435             : 
    1436             :     /* Match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src
    1437             :      * to pixel n-2 of dst, but only for the FAST_BILINEAR mode otherwise do
    1438             :      * correct scaling.
    1439             :      * n-2 is the last chrominance sample available.
    1440             :      * This is not perfect, but no one should notice the difference, the more
    1441             :      * correct variant would be like the vertical one, but that would require
    1442             :      * some special code for the first and last pixel */
    1443        2984 :     if (flags & SWS_FAST_BILINEAR) {
    1444           0 :         if (c->canMMXEXTBeUsed) {
    1445           0 :             c->lumXInc += 20;
    1446           0 :             c->chrXInc += 20;
    1447             :         }
    1448             :         // we don't use the x86 asm scaler if MMX is available
    1449           0 :         else if (INLINE_MMX(cpu_flags) && c->dstBpc <= 14) {
    1450           0 :             c->lumXInc = ((int64_t)(srcW       - 2) << 16) / (dstW       - 2) - 20;
    1451           0 :             c->chrXInc = ((int64_t)(c->chrSrcW - 2) << 16) / (c->chrDstW - 2) - 20;
    1452             :         }
    1453             :     }
    1454             : 
    1455             :     // hardcoded for now
    1456        2984 :     c->gamma_value = 2.2;
    1457        2984 :     tmpFmt = AV_PIX_FMT_RGBA64LE;
    1458             : 
    1459             : 
    1460        2984 :     if (!unscaled && c->gamma_flag && (srcFormat != tmpFmt || dstFormat != tmpFmt)) {
    1461             :         SwsContext *c2;
    1462           0 :         c->cascaded_context[0] = NULL;
    1463             : 
    1464           0 :         ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
    1465             :                             srcW, srcH, tmpFmt, 64);
    1466           0 :         if (ret < 0)
    1467           0 :             return ret;
    1468             : 
    1469           0 :         c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
    1470             :                                                 srcW, srcH, tmpFmt,
    1471           0 :                                                 flags, NULL, NULL, c->param);
    1472           0 :         if (!c->cascaded_context[0]) {
    1473           0 :             return -1;
    1474             :         }
    1475             : 
    1476           0 :         c->cascaded_context[1] = sws_getContext(srcW, srcH, tmpFmt,
    1477             :                                                 dstW, dstH, tmpFmt,
    1478           0 :                                                 flags, srcFilter, dstFilter, c->param);
    1479             : 
    1480           0 :         if (!c->cascaded_context[1])
    1481           0 :             return -1;
    1482             : 
    1483           0 :         c2 = c->cascaded_context[1];
    1484           0 :         c2->is_internal_gamma = 1;
    1485           0 :         c2->gamma     = alloc_gamma_tbl(    c->gamma_value);
    1486           0 :         c2->inv_gamma = alloc_gamma_tbl(1.f/c->gamma_value);
    1487           0 :         if (!c2->gamma || !c2->inv_gamma)
    1488           0 :             return AVERROR(ENOMEM);
    1489             : 
    1490             :         // is_internal_flag is set after creating the context
    1491             :         // to properly create the gamma convert FilterDescriptor
    1492             :         // we have to re-initialize it
    1493           0 :         ff_free_filters(c2);
    1494           0 :         if (ff_init_filters(c2) < 0) {
    1495           0 :             sws_freeContext(c2);
    1496           0 :             return -1;
    1497             :         }
    1498             : 
    1499           0 :         c->cascaded_context[2] = NULL;
    1500           0 :         if (dstFormat != tmpFmt) {
    1501           0 :             ret = av_image_alloc(c->cascaded1_tmp, c->cascaded1_tmpStride,
    1502             :                                 dstW, dstH, tmpFmt, 64);
    1503           0 :             if (ret < 0)
    1504           0 :                 return ret;
    1505             : 
    1506           0 :             c->cascaded_context[2] = sws_getContext(dstW, dstH, tmpFmt,
    1507             :                                                 dstW, dstH, dstFormat,
    1508           0 :                                                 flags, NULL, NULL, c->param);
    1509           0 :             if (!c->cascaded_context[2])
    1510           0 :                 return -1;
    1511             :         }
    1512           0 :         return 0;
    1513             :     }
    1514             : 
    1515        2984 :     if (isBayer(srcFormat)) {
    1516           0 :         if (!unscaled ||
    1517           0 :             (dstFormat != AV_PIX_FMT_RGB24 && dstFormat != AV_PIX_FMT_YUV420P)) {
    1518           0 :             enum AVPixelFormat tmpFormat = AV_PIX_FMT_RGB24;
    1519             : 
    1520           0 :             ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
    1521             :                                 srcW, srcH, tmpFormat, 64);
    1522           0 :             if (ret < 0)
    1523           0 :                 return ret;
    1524             : 
    1525           0 :             c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
    1526             :                                                     srcW, srcH, tmpFormat,
    1527           0 :                                                     flags, srcFilter, NULL, c->param);
    1528           0 :             if (!c->cascaded_context[0])
    1529           0 :                 return -1;
    1530             : 
    1531           0 :             c->cascaded_context[1] = sws_getContext(srcW, srcH, tmpFormat,
    1532             :                                                     dstW, dstH, dstFormat,
    1533           0 :                                                     flags, NULL, dstFilter, c->param);
    1534           0 :             if (!c->cascaded_context[1])
    1535           0 :                 return -1;
    1536           0 :             return 0;
    1537             :         }
    1538             :     }
    1539             : 
    1540        2984 :     if (CONFIG_SWSCALE_ALPHA && isALPHA(srcFormat) && !isALPHA(dstFormat)) {
    1541         131 :         enum AVPixelFormat tmpFormat = alphaless_fmt(srcFormat);
    1542             : 
    1543         131 :         if (tmpFormat != AV_PIX_FMT_NONE && c->alphablend != SWS_ALPHA_BLEND_NONE)
    1544           0 :         if (!unscaled ||
    1545           0 :             dstFormat != tmpFormat ||
    1546           0 :             usesHFilter || usesVFilter ||
    1547           0 :             c->srcRange != c->dstRange
    1548             :         ) {
    1549           0 :             c->cascaded_mainindex = 1;
    1550           0 :             ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
    1551             :                                 srcW, srcH, tmpFormat, 64);
    1552           0 :             if (ret < 0)
    1553           0 :                 return ret;
    1554             : 
    1555           0 :             c->cascaded_context[0] = sws_alloc_set_opts(srcW, srcH, srcFormat,
    1556             :                                                         srcW, srcH, tmpFormat,
    1557           0 :                                                         flags, c->param);
    1558           0 :             if (!c->cascaded_context[0])
    1559           0 :                 return -1;
    1560           0 :             c->cascaded_context[0]->alphablend = c->alphablend;
    1561           0 :             ret = sws_init_context(c->cascaded_context[0], NULL , NULL);
    1562           0 :             if (ret < 0)
    1563           0 :                 return ret;
    1564             : 
    1565           0 :             c->cascaded_context[1] = sws_alloc_set_opts(srcW, srcH, tmpFormat,
    1566             :                                                         dstW, dstH, dstFormat,
    1567           0 :                                                         flags, c->param);
    1568           0 :             if (!c->cascaded_context[1])
    1569           0 :                 return -1;
    1570             : 
    1571           0 :             c->cascaded_context[1]->srcRange = c->srcRange;
    1572           0 :             c->cascaded_context[1]->dstRange = c->dstRange;
    1573           0 :             ret = sws_init_context(c->cascaded_context[1], srcFilter , dstFilter);
    1574           0 :             if (ret < 0)
    1575           0 :                 return ret;
    1576             : 
    1577           0 :             return 0;
    1578             :         }
    1579             :     }
    1580             : 
    1581             : #if HAVE_MMAP && HAVE_MPROTECT && defined(MAP_ANONYMOUS)
    1582             : #define USE_MMAP 1
    1583             : #else
    1584             : #define USE_MMAP 0
    1585             : #endif
    1586             : 
    1587             :     /* precalculate horizontal scaler filter coefficients */
    1588             :     {
    1589             : #if HAVE_MMXEXT_INLINE
    1590             : // can't downscale !!!
    1591        2984 :         if (c->canMMXEXTBeUsed && (flags & SWS_FAST_BILINEAR)) {
    1592           0 :             c->lumMmxextFilterCodeSize = ff_init_hscaler_mmxext(dstW, c->lumXInc, NULL,
    1593             :                                                              NULL, NULL, 8);
    1594           0 :             c->chrMmxextFilterCodeSize = ff_init_hscaler_mmxext(c->chrDstW, c->chrXInc,
    1595             :                                                              NULL, NULL, NULL, 4);
    1596             : 
    1597             : #if USE_MMAP
    1598           0 :             c->lumMmxextFilterCode = mmap(NULL, c->lumMmxextFilterCodeSize,
    1599             :                                           PROT_READ | PROT_WRITE,
    1600             :                                           MAP_PRIVATE | MAP_ANONYMOUS,
    1601             :                                           -1, 0);
    1602           0 :             c->chrMmxextFilterCode = mmap(NULL, c->chrMmxextFilterCodeSize,
    1603             :                                           PROT_READ | PROT_WRITE,
    1604             :                                           MAP_PRIVATE | MAP_ANONYMOUS,
    1605             :                                           -1, 0);
    1606             : #elif HAVE_VIRTUALALLOC
    1607             :             c->lumMmxextFilterCode = VirtualAlloc(NULL,
    1608             :                                                   c->lumMmxextFilterCodeSize,
    1609             :                                                   MEM_COMMIT,
    1610             :                                                   PAGE_EXECUTE_READWRITE);
    1611             :             c->chrMmxextFilterCode = VirtualAlloc(NULL,
    1612             :                                                   c->chrMmxextFilterCodeSize,
    1613             :                                                   MEM_COMMIT,
    1614             :                                                   PAGE_EXECUTE_READWRITE);
    1615             : #else
    1616             :             c->lumMmxextFilterCode = av_malloc(c->lumMmxextFilterCodeSize);
    1617             :             c->chrMmxextFilterCode = av_malloc(c->chrMmxextFilterCodeSize);
    1618             : #endif
    1619             : 
    1620             : #ifdef MAP_ANONYMOUS
    1621           0 :             if (c->lumMmxextFilterCode == MAP_FAILED || c->chrMmxextFilterCode == MAP_FAILED)
    1622             : #else
    1623             :             if (!c->lumMmxextFilterCode || !c->chrMmxextFilterCode)
    1624             : #endif
    1625             :             {
    1626           0 :                 av_log(c, AV_LOG_ERROR, "Failed to allocate MMX2FilterCode\n");
    1627           0 :                 return AVERROR(ENOMEM);
    1628             :             }
    1629             : 
    1630           0 :             FF_ALLOCZ_OR_GOTO(c, c->hLumFilter,    (dstW           / 8 + 8) * sizeof(int16_t), fail);
    1631           0 :             FF_ALLOCZ_OR_GOTO(c, c->hChrFilter,    (c->chrDstW     / 4 + 8) * sizeof(int16_t), fail);
    1632           0 :             FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW       / 2 / 8 + 8) * sizeof(int32_t), fail);
    1633           0 :             FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW / 2 / 4 + 8) * sizeof(int32_t), fail);
    1634             : 
    1635           0 :             ff_init_hscaler_mmxext(      dstW, c->lumXInc, c->lumMmxextFilterCode,
    1636             :                                 c->hLumFilter, (uint32_t*)c->hLumFilterPos, 8);
    1637           0 :             ff_init_hscaler_mmxext(c->chrDstW, c->chrXInc, c->chrMmxextFilterCode,
    1638             :                                 c->hChrFilter, (uint32_t*)c->hChrFilterPos, 4);
    1639             : 
    1640             : #if USE_MMAP
    1641           0 :             if (   mprotect(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1
    1642           0 :                 || mprotect(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1) {
    1643           0 :                 av_log(c, AV_LOG_ERROR, "mprotect failed, cannot use fast bilinear scaler\n");
    1644           0 :                 goto fail;
    1645             :             }
    1646             : #endif
    1647             :         } else
    1648             : #endif /* HAVE_MMXEXT_INLINE */
    1649             :         {
    1650        2984 :             const int filterAlign = X86_MMX(cpu_flags)     ? 4 :
    1651             :                                     PPC_ALTIVEC(cpu_flags) ? 8 :
    1652             :                                     have_neon(cpu_flags)   ? 8 : 1;
    1653             : 
    1654        8952 :             if ((ret = initFilter(&c->hLumFilter, &c->hLumFilterPos,
    1655             :                            &c->hLumFilterSize, c->lumXInc,
    1656             :                            srcW, dstW, filterAlign, 1 << 14,
    1657        2984 :                            (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
    1658             :                            cpu_flags, srcFilter->lumH, dstFilter->lumH,
    1659        2984 :                            c->param,
    1660             :                            get_local_pos(c, 0, 0, 0),
    1661             :                            get_local_pos(c, 0, 0, 0))) < 0)
    1662           0 :                 goto fail;
    1663        8952 :             if ((ret = initFilter(&c->hChrFilter, &c->hChrFilterPos,
    1664             :                            &c->hChrFilterSize, c->chrXInc,
    1665             :                            c->chrSrcW, c->chrDstW, filterAlign, 1 << 14,
    1666        2984 :                            (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
    1667             :                            cpu_flags, srcFilter->chrH, dstFilter->chrH,
    1668        2984 :                            c->param,
    1669             :                            get_local_pos(c, c->chrSrcHSubSample, c->src_h_chr_pos, 0),
    1670             :                            get_local_pos(c, c->chrDstHSubSample, c->dst_h_chr_pos, 0))) < 0)
    1671           0 :                 goto fail;
    1672             :         }
    1673             :     } // initialize horizontal stuff
    1674             : 
    1675             :     /* precalculate vertical scaler filter coefficients */
    1676             :     {
    1677        2984 :         const int filterAlign = X86_MMX(cpu_flags)     ? 2 :
    1678             :                                 PPC_ALTIVEC(cpu_flags) ? 8 :
    1679             :                                 have_neon(cpu_flags)   ? 2 : 1;
    1680             : 
    1681        8952 :         if ((ret = initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize,
    1682             :                        c->lumYInc, srcH, dstH, filterAlign, (1 << 12),
    1683        2984 :                        (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
    1684             :                        cpu_flags, srcFilter->lumV, dstFilter->lumV,
    1685        2984 :                        c->param,
    1686             :                        get_local_pos(c, 0, 0, 1),
    1687             :                        get_local_pos(c, 0, 0, 1))) < 0)
    1688           0 :             goto fail;
    1689        8952 :         if ((ret = initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize,
    1690             :                        c->chrYInc, c->chrSrcH, c->chrDstH,
    1691             :                        filterAlign, (1 << 12),
    1692        2984 :                        (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
    1693             :                        cpu_flags, srcFilter->chrV, dstFilter->chrV,
    1694        2984 :                        c->param,
    1695             :                        get_local_pos(c, c->chrSrcVSubSample, c->src_v_chr_pos, 1),
    1696             :                        get_local_pos(c, c->chrDstVSubSample, c->dst_v_chr_pos, 1))) < 0)
    1697             : 
    1698           0 :             goto fail;
    1699             : 
    1700             : #if HAVE_ALTIVEC
    1701             :         FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof(vector signed short) * c->vLumFilterSize * c->dstH,    fail);
    1702             :         FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof(vector signed short) * c->vChrFilterSize * c->chrDstH, fail);
    1703             : 
    1704             :         for (i = 0; i < c->vLumFilterSize * c->dstH; i++) {
    1705             :             int j;
    1706             :             short *p = (short *)&c->vYCoeffsBank[i];
    1707             :             for (j = 0; j < 8; j++)
    1708             :                 p[j] = c->vLumFilter[i];
    1709             :         }
    1710             : 
    1711             :         for (i = 0; i < c->vChrFilterSize * c->chrDstH; i++) {
    1712             :             int j;
    1713             :             short *p = (short *)&c->vCCoeffsBank[i];
    1714             :             for (j = 0; j < 8; j++)
    1715             :                 p[j] = c->vChrFilter[i];
    1716             :         }
    1717             : #endif
    1718             :     }
    1719             : 
    1720       14920 :     for (i = 0; i < 4; i++)
    1721       11936 :         FF_ALLOCZ_OR_GOTO(c, c->dither_error[i], (c->dstW+2) * sizeof(int), fail);
    1722             : 
    1723        2984 :     c->needAlpha = (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) ? 1 : 0;
    1724             : 
    1725             :     // 64 / c->scalingBpp is the same as 16 / sizeof(scaling_intermediate)
    1726        2984 :     c->uv_off   = (dst_stride>>1) + 64 / (c->dstBpc &~ 7);
    1727        2984 :     c->uv_offx2 = dst_stride + 16;
    1728             : 
    1729        2984 :     av_assert0(c->chrDstH <= dstH);
    1730             : 
    1731        2984 :     if (flags & SWS_PRINT_INFO) {
    1732           0 :         const char *scaler = NULL, *cpucaps;
    1733             : 
    1734           0 :         for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
    1735           0 :             if (flags & scale_algorithms[i].flag) {
    1736           0 :                 scaler = scale_algorithms[i].description;
    1737           0 :                 break;
    1738             :             }
    1739             :         }
    1740           0 :         if (!scaler)
    1741           0 :             scaler =  "ehh flags invalid?!";
    1742           0 :         av_log(c, AV_LOG_INFO, "%s scaler, from %s to %s%s ",
    1743             :                scaler,
    1744             :                av_get_pix_fmt_name(srcFormat),
    1745             : #ifdef DITHER1XBPP
    1746           0 :                dstFormat == AV_PIX_FMT_BGR555   || dstFormat == AV_PIX_FMT_BGR565   ||
    1747           0 :                dstFormat == AV_PIX_FMT_RGB444BE || dstFormat == AV_PIX_FMT_RGB444LE ||
    1748           0 :                dstFormat == AV_PIX_FMT_BGR444BE || dstFormat == AV_PIX_FMT_BGR444LE ?
    1749             :                                                              "dithered " : "",
    1750             : #else
    1751             :                "",
    1752             : #endif
    1753             :                av_get_pix_fmt_name(dstFormat));
    1754             : 
    1755           0 :         if (INLINE_MMXEXT(cpu_flags))
    1756           0 :             cpucaps = "MMXEXT";
    1757           0 :         else if (INLINE_AMD3DNOW(cpu_flags))
    1758           0 :             cpucaps = "3DNOW";
    1759           0 :         else if (INLINE_MMX(cpu_flags))
    1760           0 :             cpucaps = "MMX";
    1761             :         else if (PPC_ALTIVEC(cpu_flags))
    1762             :             cpucaps = "AltiVec";
    1763             :         else
    1764           0 :             cpucaps = "C";
    1765             : 
    1766           0 :         av_log(c, AV_LOG_INFO, "using %s\n", cpucaps);
    1767             : 
    1768           0 :         av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
    1769           0 :         av_log(c, AV_LOG_DEBUG,
    1770             :                "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
    1771             :                c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
    1772           0 :         av_log(c, AV_LOG_DEBUG,
    1773             :                "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
    1774             :                c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH,
    1775             :                c->chrXInc, c->chrYInc);
    1776             :     }
    1777             : 
    1778             :     /* alpha blend special case, note this has been split via cascaded contexts if its scaled */
    1779        5593 :     if (unscaled && !usesHFilter && !usesVFilter &&
    1780        2609 :         c->alphablend != SWS_ALPHA_BLEND_NONE &&
    1781           0 :         isALPHA(srcFormat) &&
    1782           0 :         (c->srcRange == c->dstRange || isAnyRGB(dstFormat)) &&
    1783           0 :         alphaless_fmt(srcFormat) == dstFormat
    1784             :     ) {
    1785           0 :         c->swscale = ff_sws_alphablendaway;
    1786             : 
    1787           0 :         if (flags & SWS_PRINT_INFO)
    1788           0 :             av_log(c, AV_LOG_INFO,
    1789             :                     "using alpha blendaway %s -> %s special converter\n",
    1790             :                     av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
    1791           0 :         return 0;
    1792             :     }
    1793             : 
    1794             :     /* unscaled special cases */
    1795        5593 :     if (unscaled && !usesHFilter && !usesVFilter &&
    1796        2950 :         (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) {
    1797        2278 :         ff_get_unscaled_swscale(c);
    1798             : 
    1799        2278 :         if (c->swscale) {
    1800         446 :             if (flags & SWS_PRINT_INFO)
    1801           0 :                 av_log(c, AV_LOG_INFO,
    1802             :                        "using unscaled %s -> %s special converter\n",
    1803             :                        av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
    1804         446 :             return 0;
    1805             :         }
    1806             :     }
    1807             : 
    1808        2538 :     c->swscale = ff_getSwsFunc(c);
    1809        2538 :     return ff_init_filters(c);
    1810           0 : fail: // FIXME replace things by appropriate error codes
    1811           0 :     if (ret == RETCODE_USE_CASCADE)  {
    1812           0 :         int tmpW = sqrt(srcW * (int64_t)dstW);
    1813           0 :         int tmpH = sqrt(srcH * (int64_t)dstH);
    1814           0 :         enum AVPixelFormat tmpFormat = AV_PIX_FMT_YUV420P;
    1815             : 
    1816           0 :         if (isALPHA(srcFormat))
    1817           0 :             tmpFormat = AV_PIX_FMT_YUVA420P;
    1818             : 
    1819           0 :         if (srcW*(int64_t)srcH <= 4LL*dstW*dstH)
    1820           0 :             return AVERROR(EINVAL);
    1821             : 
    1822           0 :         ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
    1823             :                              tmpW, tmpH, tmpFormat, 64);
    1824           0 :         if (ret < 0)
    1825           0 :             return ret;
    1826             : 
    1827           0 :         c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
    1828             :                                                 tmpW, tmpH, tmpFormat,
    1829           0 :                                                 flags, srcFilter, NULL, c->param);
    1830           0 :         if (!c->cascaded_context[0])
    1831           0 :             return -1;
    1832             : 
    1833           0 :         c->cascaded_context[1] = sws_getContext(tmpW, tmpH, tmpFormat,
    1834             :                                                 dstW, dstH, dstFormat,
    1835           0 :                                                 flags, NULL, dstFilter, c->param);
    1836           0 :         if (!c->cascaded_context[1])
    1837           0 :             return -1;
    1838           0 :         return 0;
    1839             :     }
    1840           0 :     return -1;
    1841             : }
    1842             : 
    1843           0 : SwsContext *sws_alloc_set_opts(int srcW, int srcH, enum AVPixelFormat srcFormat,
    1844             :                                int dstW, int dstH, enum AVPixelFormat dstFormat,
    1845             :                                int flags, const double *param)
    1846             : {
    1847             :     SwsContext *c;
    1848             : 
    1849           0 :     if (!(c = sws_alloc_context()))
    1850           0 :         return NULL;
    1851             : 
    1852           0 :     c->flags     = flags;
    1853           0 :     c->srcW      = srcW;
    1854           0 :     c->srcH      = srcH;
    1855           0 :     c->dstW      = dstW;
    1856           0 :     c->dstH      = dstH;
    1857           0 :     c->srcFormat = srcFormat;
    1858           0 :     c->dstFormat = dstFormat;
    1859             : 
    1860           0 :     if (param) {
    1861           0 :         c->param[0] = param[0];
    1862           0 :         c->param[1] = param[1];
    1863             :     }
    1864             : 
    1865           0 :     return c;
    1866             : }
    1867             : 
    1868           0 : SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
    1869             :                            int dstW, int dstH, enum AVPixelFormat dstFormat,
    1870             :                            int flags, SwsFilter *srcFilter,
    1871             :                            SwsFilter *dstFilter, const double *param)
    1872             : {
    1873             :     SwsContext *c;
    1874             : 
    1875           0 :     c = sws_alloc_set_opts(srcW, srcH, srcFormat,
    1876             :                            dstW, dstH, dstFormat,
    1877             :                            flags, param);
    1878           0 :     if (!c)
    1879           0 :         return NULL;
    1880             : 
    1881           0 :     if (sws_init_context(c, srcFilter, dstFilter) < 0) {
    1882           0 :         sws_freeContext(c);
    1883           0 :         return NULL;
    1884             :     }
    1885             : 
    1886           0 :     return c;
    1887             : }
    1888             : 
    1889           0 : static int isnan_vec(SwsVector *a)
    1890             : {
    1891             :     int i;
    1892           0 :     for (i=0; i<a->length; i++)
    1893           0 :         if (isnan(a->coeff[i]))
    1894           0 :             return 1;
    1895           0 :     return 0;
    1896             : }
    1897             : 
    1898           0 : static void makenan_vec(SwsVector *a)
    1899             : {
    1900             :     int i;
    1901           0 :     for (i=0; i<a->length; i++)
    1902           0 :         a->coeff[i] = NAN;
    1903           0 : }
    1904             : 
    1905           0 : SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
    1906             :                                 float lumaSharpen, float chromaSharpen,
    1907             :                                 float chromaHShift, float chromaVShift,
    1908             :                                 int verbose)
    1909             : {
    1910           0 :     SwsFilter *filter = av_malloc(sizeof(SwsFilter));
    1911           0 :     if (!filter)
    1912           0 :         return NULL;
    1913             : 
    1914           0 :     if (lumaGBlur != 0.0) {
    1915           0 :         filter->lumH = sws_getGaussianVec(lumaGBlur, 3.0);
    1916           0 :         filter->lumV = sws_getGaussianVec(lumaGBlur, 3.0);
    1917             :     } else {
    1918           0 :         filter->lumH = sws_getIdentityVec();
    1919           0 :         filter->lumV = sws_getIdentityVec();
    1920             :     }
    1921             : 
    1922           0 :     if (chromaGBlur != 0.0) {
    1923           0 :         filter->chrH = sws_getGaussianVec(chromaGBlur, 3.0);
    1924           0 :         filter->chrV = sws_getGaussianVec(chromaGBlur, 3.0);
    1925             :     } else {
    1926           0 :         filter->chrH = sws_getIdentityVec();
    1927           0 :         filter->chrV = sws_getIdentityVec();
    1928             :     }
    1929             : 
    1930           0 :     if (!filter->lumH || !filter->lumV || !filter->chrH || !filter->chrV)
    1931             :         goto fail;
    1932             : 
    1933           0 :     if (chromaSharpen != 0.0) {
    1934           0 :         SwsVector *id = sws_getIdentityVec();
    1935           0 :         if (!id)
    1936           0 :             goto fail;
    1937           0 :         sws_scaleVec(filter->chrH, -chromaSharpen);
    1938           0 :         sws_scaleVec(filter->chrV, -chromaSharpen);
    1939           0 :         sws_addVec(filter->chrH, id);
    1940           0 :         sws_addVec(filter->chrV, id);
    1941           0 :         sws_freeVec(id);
    1942             :     }
    1943             : 
    1944           0 :     if (lumaSharpen != 0.0) {
    1945           0 :         SwsVector *id = sws_getIdentityVec();
    1946           0 :         if (!id)
    1947           0 :             goto fail;
    1948           0 :         sws_scaleVec(filter->lumH, -lumaSharpen);
    1949           0 :         sws_scaleVec(filter->lumV, -lumaSharpen);
    1950           0 :         sws_addVec(filter->lumH, id);
    1951           0 :         sws_addVec(filter->lumV, id);
    1952           0 :         sws_freeVec(id);
    1953             :     }
    1954             : 
    1955           0 :     if (chromaHShift != 0.0)
    1956           0 :         sws_shiftVec(filter->chrH, (int)(chromaHShift + 0.5));
    1957             : 
    1958           0 :     if (chromaVShift != 0.0)
    1959           0 :         sws_shiftVec(filter->chrV, (int)(chromaVShift + 0.5));
    1960             : 
    1961           0 :     sws_normalizeVec(filter->chrH, 1.0);
    1962           0 :     sws_normalizeVec(filter->chrV, 1.0);
    1963           0 :     sws_normalizeVec(filter->lumH, 1.0);
    1964           0 :     sws_normalizeVec(filter->lumV, 1.0);
    1965             : 
    1966           0 :     if (isnan_vec(filter->chrH) ||
    1967           0 :         isnan_vec(filter->chrV) ||
    1968           0 :         isnan_vec(filter->lumH) ||
    1969           0 :         isnan_vec(filter->lumV))
    1970             :         goto fail;
    1971             : 
    1972           0 :     if (verbose)
    1973           0 :         sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
    1974           0 :     if (verbose)
    1975           0 :         sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
    1976             : 
    1977           0 :     return filter;
    1978             : 
    1979           0 : fail:
    1980           0 :     sws_freeVec(filter->lumH);
    1981           0 :     sws_freeVec(filter->lumV);
    1982           0 :     sws_freeVec(filter->chrH);
    1983           0 :     sws_freeVec(filter->chrV);
    1984           0 :     av_freep(&filter);
    1985           0 :     return NULL;
    1986             : }
    1987             : 
    1988           0 : SwsVector *sws_allocVec(int length)
    1989             : {
    1990             :     SwsVector *vec;
    1991             : 
    1992           0 :     if(length <= 0 || length > INT_MAX/ sizeof(double))
    1993           0 :         return NULL;
    1994             : 
    1995           0 :     vec = av_malloc(sizeof(SwsVector));
    1996           0 :     if (!vec)
    1997           0 :         return NULL;
    1998           0 :     vec->length = length;
    1999           0 :     vec->coeff  = av_malloc(sizeof(double) * length);
    2000           0 :     if (!vec->coeff)
    2001           0 :         av_freep(&vec);
    2002           0 :     return vec;
    2003             : }
    2004             : 
    2005           0 : SwsVector *sws_getGaussianVec(double variance, double quality)
    2006             : {
    2007           0 :     const int length = (int)(variance * quality + 0.5) | 1;
    2008             :     int i;
    2009           0 :     double middle  = (length - 1) * 0.5;
    2010             :     SwsVector *vec;
    2011             : 
    2012           0 :     if(variance < 0 || quality < 0)
    2013           0 :         return NULL;
    2014             : 
    2015           0 :     vec = sws_allocVec(length);
    2016             : 
    2017           0 :     if (!vec)
    2018           0 :         return NULL;
    2019             : 
    2020           0 :     for (i = 0; i < length; i++) {
    2021           0 :         double dist = i - middle;
    2022           0 :         vec->coeff[i] = exp(-dist * dist / (2 * variance * variance)) /
    2023           0 :                         sqrt(2 * variance * M_PI);
    2024             :     }
    2025             : 
    2026           0 :     sws_normalizeVec(vec, 1.0);
    2027             : 
    2028           0 :     return vec;
    2029             : }
    2030             : 
    2031             : /**
    2032             :  * Allocate and return a vector with length coefficients, all
    2033             :  * with the same value c.
    2034             :  */
    2035             : #if !FF_API_SWS_VECTOR
    2036             : static
    2037             : #endif
    2038           0 : SwsVector *sws_getConstVec(double c, int length)
    2039             : {
    2040             :     int i;
    2041           0 :     SwsVector *vec = sws_allocVec(length);
    2042             : 
    2043           0 :     if (!vec)
    2044           0 :         return NULL;
    2045             : 
    2046           0 :     for (i = 0; i < length; i++)
    2047           0 :         vec->coeff[i] = c;
    2048             : 
    2049           0 :     return vec;
    2050             : }
    2051             : 
    2052             : /**
    2053             :  * Allocate and return a vector with just one coefficient, with
    2054             :  * value 1.0.
    2055             :  */
    2056             : #if !FF_API_SWS_VECTOR
    2057             : static
    2058             : #endif
    2059           0 : SwsVector *sws_getIdentityVec(void)
    2060             : {
    2061           0 :     return sws_getConstVec(1.0, 1);
    2062             : }
    2063             : 
    2064           0 : static double sws_dcVec(SwsVector *a)
    2065             : {
    2066             :     int i;
    2067           0 :     double sum = 0;
    2068             : 
    2069           0 :     for (i = 0; i < a->length; i++)
    2070           0 :         sum += a->coeff[i];
    2071             : 
    2072           0 :     return sum;
    2073             : }
    2074             : 
    2075           0 : void sws_scaleVec(SwsVector *a, double scalar)
    2076             : {
    2077             :     int i;
    2078             : 
    2079           0 :     for (i = 0; i < a->length; i++)
    2080           0 :         a->coeff[i] *= scalar;
    2081           0 : }
    2082             : 
    2083           0 : void sws_normalizeVec(SwsVector *a, double height)
    2084             : {
    2085           0 :     sws_scaleVec(a, height / sws_dcVec(a));
    2086           0 : }
    2087             : 
    2088             : #if FF_API_SWS_VECTOR
    2089           0 : static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
    2090             : {
    2091           0 :     int length = a->length + b->length - 1;
    2092             :     int i, j;
    2093           0 :     SwsVector *vec = sws_getConstVec(0.0, length);
    2094             : 
    2095           0 :     if (!vec)
    2096           0 :         return NULL;
    2097             : 
    2098           0 :     for (i = 0; i < a->length; i++) {
    2099           0 :         for (j = 0; j < b->length; j++) {
    2100           0 :             vec->coeff[i + j] += a->coeff[i] * b->coeff[j];
    2101             :         }
    2102             :     }
    2103             : 
    2104           0 :     return vec;
    2105             : }
    2106             : #endif
    2107             : 
    2108           0 : static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
    2109             : {
    2110           0 :     int length = FFMAX(a->length, b->length);
    2111             :     int i;
    2112           0 :     SwsVector *vec = sws_getConstVec(0.0, length);
    2113             : 
    2114           0 :     if (!vec)
    2115           0 :         return NULL;
    2116             : 
    2117           0 :     for (i = 0; i < a->length; i++)
    2118           0 :         vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
    2119           0 :     for (i = 0; i < b->length; i++)
    2120           0 :         vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] += b->coeff[i];
    2121             : 
    2122           0 :     return vec;
    2123             : }
    2124             : 
    2125             : #if FF_API_SWS_VECTOR
    2126           0 : static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
    2127             : {
    2128           0 :     int length = FFMAX(a->length, b->length);
    2129             :     int i;
    2130           0 :     SwsVector *vec = sws_getConstVec(0.0, length);
    2131             : 
    2132           0 :     if (!vec)
    2133           0 :         return NULL;
    2134             : 
    2135           0 :     for (i = 0; i < a->length; i++)
    2136           0 :         vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
    2137           0 :     for (i = 0; i < b->length; i++)
    2138           0 :         vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] -= b->coeff[i];
    2139             : 
    2140           0 :     return vec;
    2141             : }
    2142             : #endif
    2143             : 
    2144             : /* shift left / or right if "shift" is negative */
    2145           0 : static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
    2146             : {
    2147           0 :     int length = a->length + FFABS(shift) * 2;
    2148             :     int i;
    2149           0 :     SwsVector *vec = sws_getConstVec(0.0, length);
    2150             : 
    2151           0 :     if (!vec)
    2152           0 :         return NULL;
    2153             : 
    2154           0 :     for (i = 0; i < a->length; i++) {
    2155           0 :         vec->coeff[i + (length    - 1) / 2 -
    2156           0 :                        (a->length - 1) / 2 - shift] = a->coeff[i];
    2157             :     }
    2158             : 
    2159           0 :     return vec;
    2160             : }
    2161             : 
    2162             : #if !FF_API_SWS_VECTOR
    2163             : static
    2164             : #endif
    2165           0 : void sws_shiftVec(SwsVector *a, int shift)
    2166             : {
    2167           0 :     SwsVector *shifted = sws_getShiftedVec(a, shift);
    2168           0 :     if (!shifted) {
    2169           0 :         makenan_vec(a);
    2170           0 :         return;
    2171             :     }
    2172           0 :     av_free(a->coeff);
    2173           0 :     a->coeff  = shifted->coeff;
    2174           0 :     a->length = shifted->length;
    2175           0 :     av_free(shifted);
    2176             : }
    2177             : 
    2178             : #if !FF_API_SWS_VECTOR
    2179             : static
    2180             : #endif
    2181           0 : void sws_addVec(SwsVector *a, SwsVector *b)
    2182             : {
    2183           0 :     SwsVector *sum = sws_sumVec(a, b);
    2184           0 :     if (!sum) {
    2185           0 :         makenan_vec(a);
    2186           0 :         return;
    2187             :     }
    2188           0 :     av_free(a->coeff);
    2189           0 :     a->coeff  = sum->coeff;
    2190           0 :     a->length = sum->length;
    2191           0 :     av_free(sum);
    2192             : }
    2193             : 
    2194             : #if FF_API_SWS_VECTOR
    2195           0 : void sws_subVec(SwsVector *a, SwsVector *b)
    2196             : {
    2197           0 :     SwsVector *diff = sws_diffVec(a, b);
    2198           0 :     if (!diff) {
    2199           0 :         makenan_vec(a);
    2200           0 :         return;
    2201             :     }
    2202           0 :     av_free(a->coeff);
    2203           0 :     a->coeff  = diff->coeff;
    2204           0 :     a->length = diff->length;
    2205           0 :     av_free(diff);
    2206             : }
    2207             : 
    2208           0 : void sws_convVec(SwsVector *a, SwsVector *b)
    2209             : {
    2210           0 :     SwsVector *conv = sws_getConvVec(a, b);
    2211           0 :     if (!conv) {
    2212           0 :         makenan_vec(a);
    2213           0 :         return;
    2214             :     }
    2215           0 :     av_free(a->coeff);
    2216           0 :     a->coeff  = conv->coeff;
    2217           0 :     a->length = conv->length;
    2218           0 :     av_free(conv);
    2219             : }
    2220             : 
    2221           0 : SwsVector *sws_cloneVec(SwsVector *a)
    2222             : {
    2223           0 :     SwsVector *vec = sws_allocVec(a->length);
    2224             : 
    2225           0 :     if (!vec)
    2226           0 :         return NULL;
    2227             : 
    2228           0 :     memcpy(vec->coeff, a->coeff, a->length * sizeof(*a->coeff));
    2229             : 
    2230           0 :     return vec;
    2231             : }
    2232             : #endif
    2233             : 
    2234             : /**
    2235             :  * Print with av_log() a textual representation of the vector a
    2236             :  * if log_level <= av_log_level.
    2237             :  */
    2238             : #if !FF_API_SWS_VECTOR
    2239             : static
    2240             : #endif
    2241           0 : void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
    2242             : {
    2243             :     int i;
    2244           0 :     double max = 0;
    2245           0 :     double min = 0;
    2246             :     double range;
    2247             : 
    2248           0 :     for (i = 0; i < a->length; i++)
    2249           0 :         if (a->coeff[i] > max)
    2250           0 :             max = a->coeff[i];
    2251             : 
    2252           0 :     for (i = 0; i < a->length; i++)
    2253           0 :         if (a->coeff[i] < min)
    2254           0 :             min = a->coeff[i];
    2255             : 
    2256           0 :     range = max - min;
    2257             : 
    2258           0 :     for (i = 0; i < a->length; i++) {
    2259           0 :         int x = (int)((a->coeff[i] - min) * 60.0 / range + 0.5);
    2260           0 :         av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
    2261           0 :         for (; x > 0; x--)
    2262           0 :             av_log(log_ctx, log_level, " ");
    2263           0 :         av_log(log_ctx, log_level, "|\n");
    2264             :     }
    2265           0 : }
    2266             : 
    2267           0 : void sws_freeVec(SwsVector *a)
    2268             : {
    2269           0 :     if (!a)
    2270           0 :         return;
    2271           0 :     av_freep(&a->coeff);
    2272           0 :     a->length = 0;
    2273           0 :     av_free(a);
    2274             : }
    2275             : 
    2276           0 : void sws_freeFilter(SwsFilter *filter)
    2277             : {
    2278           0 :     if (!filter)
    2279           0 :         return;
    2280             : 
    2281           0 :     sws_freeVec(filter->lumH);
    2282           0 :     sws_freeVec(filter->lumV);
    2283           0 :     sws_freeVec(filter->chrH);
    2284           0 :     sws_freeVec(filter->chrV);
    2285           0 :     av_free(filter);
    2286             : }
    2287             : 
    2288       44868 : void sws_freeContext(SwsContext *c)
    2289             : {
    2290             :     int i;
    2291       44868 :     if (!c)
    2292       35309 :         return;
    2293             : 
    2294       47795 :     for (i = 0; i < 4; i++)
    2295       38236 :         av_freep(&c->dither_error[i]);
    2296             : 
    2297        9559 :     av_freep(&c->vLumFilter);
    2298        9559 :     av_freep(&c->vChrFilter);
    2299        9559 :     av_freep(&c->hLumFilter);
    2300        9559 :     av_freep(&c->hChrFilter);
    2301             : #if HAVE_ALTIVEC
    2302             :     av_freep(&c->vYCoeffsBank);
    2303             :     av_freep(&c->vCCoeffsBank);
    2304             : #endif
    2305             : 
    2306        9559 :     av_freep(&c->vLumFilterPos);
    2307        9559 :     av_freep(&c->vChrFilterPos);
    2308        9559 :     av_freep(&c->hLumFilterPos);
    2309        9559 :     av_freep(&c->hChrFilterPos);
    2310             : 
    2311             : #if HAVE_MMX_INLINE
    2312             : #if USE_MMAP
    2313        9559 :     if (c->lumMmxextFilterCode)
    2314           0 :         munmap(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize);
    2315        9559 :     if (c->chrMmxextFilterCode)
    2316           0 :         munmap(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize);
    2317             : #elif HAVE_VIRTUALALLOC
    2318             :     if (c->lumMmxextFilterCode)
    2319             :         VirtualFree(c->lumMmxextFilterCode, 0, MEM_RELEASE);
    2320             :     if (c->chrMmxextFilterCode)
    2321             :         VirtualFree(c->chrMmxextFilterCode, 0, MEM_RELEASE);
    2322             : #else
    2323             :     av_free(c->lumMmxextFilterCode);
    2324             :     av_free(c->chrMmxextFilterCode);
    2325             : #endif
    2326        9559 :     c->lumMmxextFilterCode = NULL;
    2327        9559 :     c->chrMmxextFilterCode = NULL;
    2328             : #endif /* HAVE_MMX_INLINE */
    2329             : 
    2330        9559 :     av_freep(&c->yuvTable);
    2331        9559 :     av_freep(&c->formatConvBuffer);
    2332             : 
    2333        9559 :     sws_freeContext(c->cascaded_context[0]);
    2334        9559 :     sws_freeContext(c->cascaded_context[1]);
    2335        9559 :     sws_freeContext(c->cascaded_context[2]);
    2336        9559 :     memset(c->cascaded_context, 0, sizeof(c->cascaded_context));
    2337        9559 :     av_freep(&c->cascaded_tmp[0]);
    2338        9559 :     av_freep(&c->cascaded1_tmp[0]);
    2339             : 
    2340        9559 :     av_freep(&c->gamma);
    2341        9559 :     av_freep(&c->inv_gamma);
    2342             : 
    2343        9559 :     ff_free_filters(c);
    2344             : 
    2345        9559 :     av_free(c);
    2346             : }
    2347             : 
    2348           0 : struct SwsContext *sws_getCachedContext(struct SwsContext *context, int srcW,
    2349             :                                         int srcH, enum AVPixelFormat srcFormat,
    2350             :                                         int dstW, int dstH,
    2351             :                                         enum AVPixelFormat dstFormat, int flags,
    2352             :                                         SwsFilter *srcFilter,
    2353             :                                         SwsFilter *dstFilter,
    2354             :                                         const double *param)
    2355             : {
    2356             :     static const double default_param[2] = { SWS_PARAM_DEFAULT,
    2357             :                                              SWS_PARAM_DEFAULT };
    2358           0 :     int64_t src_h_chr_pos = -513, dst_h_chr_pos = -513,
    2359           0 :             src_v_chr_pos = -513, dst_v_chr_pos = -513;
    2360             : 
    2361           0 :     if (!param)
    2362           0 :         param = default_param;
    2363             : 
    2364           0 :     if (context &&
    2365           0 :         (context->srcW      != srcW      ||
    2366           0 :          context->srcH      != srcH      ||
    2367           0 :          context->srcFormat != srcFormat ||
    2368           0 :          context->dstW      != dstW      ||
    2369           0 :          context->dstH      != dstH      ||
    2370           0 :          context->dstFormat != dstFormat ||
    2371           0 :          context->flags     != flags     ||
    2372           0 :          context->param[0]  != param[0]  ||
    2373           0 :          context->param[1]  != param[1])) {
    2374             : 
    2375           0 :         av_opt_get_int(context, "src_h_chr_pos", 0, &src_h_chr_pos);
    2376           0 :         av_opt_get_int(context, "src_v_chr_pos", 0, &src_v_chr_pos);
    2377           0 :         av_opt_get_int(context, "dst_h_chr_pos", 0, &dst_h_chr_pos);
    2378           0 :         av_opt_get_int(context, "dst_v_chr_pos", 0, &dst_v_chr_pos);
    2379           0 :         sws_freeContext(context);
    2380           0 :         context = NULL;
    2381             :     }
    2382             : 
    2383           0 :     if (!context) {
    2384           0 :         if (!(context = sws_alloc_context()))
    2385           0 :             return NULL;
    2386           0 :         context->srcW      = srcW;
    2387           0 :         context->srcH      = srcH;
    2388           0 :         context->srcFormat = srcFormat;
    2389           0 :         context->dstW      = dstW;
    2390           0 :         context->dstH      = dstH;
    2391           0 :         context->dstFormat = dstFormat;
    2392           0 :         context->flags     = flags;
    2393           0 :         context->param[0]  = param[0];
    2394           0 :         context->param[1]  = param[1];
    2395             : 
    2396           0 :         av_opt_set_int(context, "src_h_chr_pos", src_h_chr_pos, 0);
    2397           0 :         av_opt_set_int(context, "src_v_chr_pos", src_v_chr_pos, 0);
    2398           0 :         av_opt_set_int(context, "dst_h_chr_pos", dst_h_chr_pos, 0);
    2399           0 :         av_opt_set_int(context, "dst_v_chr_pos", dst_v_chr_pos, 0);
    2400             : 
    2401           0 :         if (sws_init_context(context, srcFilter, dstFilter) < 0) {
    2402           0 :             sws_freeContext(context);
    2403           0 :             return NULL;
    2404             :         }
    2405             :     }
    2406           0 :     return context;
    2407             : }

Generated by: LCOV version 1.13