~ubuntu-branches/ubuntu/trusty/libav/trusty-proposed

« back to all changes in this revision

Viewing changes to libswscale/utils.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2013-10-22 23:24:08 UTC
  • mfrom: (1.3.36 sid)
  • Revision ID: package-import@ubuntu.com-20131022232408-b8tvvn4pyzri9mi3
Tags: 6:9.10-1ubuntu1
* Build all -extra flavors from this source package, as libav got demoted
  from main to universe, cf LP: #1243235
* Simplify debian/rules to follow exactly the code that debian executes
* New upstream (LP: #1180288) fixes lots of security issues (LP: #1242802)
* Merge from unstable, remaining changes:
  - build-depend on libtiff5-dev rather than libtiff4-dev,
    avoids FTBFS caused by imlib
  - follow the regular debian codepaths

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
19
 */
20
20
 
21
 
#define _SVID_SOURCE //needed for MAP_ANONYMOUS
 
21
#include "config.h"
 
22
 
 
23
#define _SVID_SOURCE // needed for MAP_ANONYMOUS
 
24
#include <assert.h>
22
25
#include <inttypes.h>
23
 
#include <string.h>
24
26
#include <math.h>
25
27
#include <stdio.h>
26
 
#include "config.h"
27
 
#include <assert.h>
 
28
#include <string.h>
28
29
#if HAVE_SYS_MMAN_H
29
30
#include <sys/mman.h>
30
31
#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
35
36
#define WIN32_LEAN_AND_MEAN
36
37
#include <windows.h>
37
38
#endif
38
 
#include "swscale.h"
39
 
#include "swscale_internal.h"
40
 
#include "rgb2rgb.h"
41
 
#include "libavutil/intreadwrite.h"
42
 
#include "libavutil/x86_cpu.h"
43
 
#include "libavutil/cpu.h"
 
39
 
 
40
#include "libavutil/attributes.h"
44
41
#include "libavutil/avutil.h"
45
42
#include "libavutil/bswap.h"
 
43
#include "libavutil/cpu.h"
 
44
#include "libavutil/intreadwrite.h"
46
45
#include "libavutil/mathematics.h"
47
46
#include "libavutil/opt.h"
48
47
#include "libavutil/pixdesc.h"
 
48
#include "libavutil/x86/asm.h"
 
49
#include "libavutil/x86/cpu.h"
 
50
#include "rgb2rgb.h"
 
51
#include "swscale.h"
 
52
#include "swscale_internal.h"
49
53
 
50
54
unsigned swscale_version(void)
51
55
{
63
67
    return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
64
68
}
65
69
 
66
 
#define RET 0xC3 //near return opcode for x86
 
70
#define RET 0xC3 // near return opcode for x86
67
71
 
68
72
typedef struct FormatEntry {
69
73
    int is_supported_in, is_supported_out;
70
74
} FormatEntry;
71
75
 
72
 
static const FormatEntry format_entries[PIX_FMT_NB] = {
73
 
    [PIX_FMT_YUV420P]     = { 1 , 1 },
74
 
    [PIX_FMT_YUYV422]     = { 1 , 1 },
75
 
    [PIX_FMT_RGB24]       = { 1 , 1 },
76
 
    [PIX_FMT_BGR24]       = { 1 , 1 },
77
 
    [PIX_FMT_YUV422P]     = { 1 , 1 },
78
 
    [PIX_FMT_YUV444P]     = { 1 , 1 },
79
 
    [PIX_FMT_YUV410P]     = { 1 , 1 },
80
 
    [PIX_FMT_YUV411P]     = { 1 , 1 },
81
 
    [PIX_FMT_GRAY8]       = { 1 , 1 },
82
 
    [PIX_FMT_MONOWHITE]   = { 1 , 1 },
83
 
    [PIX_FMT_MONOBLACK]   = { 1 , 1 },
84
 
    [PIX_FMT_PAL8]        = { 1 , 0 },
85
 
    [PIX_FMT_YUVJ420P]    = { 1 , 1 },
86
 
    [PIX_FMT_YUVJ422P]    = { 1 , 1 },
87
 
    [PIX_FMT_YUVJ444P]    = { 1 , 1 },
88
 
    [PIX_FMT_UYVY422]     = { 1 , 1 },
89
 
    [PIX_FMT_UYYVYY411]   = { 0 , 0 },
90
 
    [PIX_FMT_BGR8]        = { 1 , 1 },
91
 
    [PIX_FMT_BGR4]        = { 0 , 1 },
92
 
    [PIX_FMT_BGR4_BYTE]   = { 1 , 1 },
93
 
    [PIX_FMT_RGB8]        = { 1 , 1 },
94
 
    [PIX_FMT_RGB4]        = { 0 , 1 },
95
 
    [PIX_FMT_RGB4_BYTE]   = { 1 , 1 },
96
 
    [PIX_FMT_NV12]        = { 1 , 1 },
97
 
    [PIX_FMT_NV21]        = { 1 , 1 },
98
 
    [PIX_FMT_ARGB]        = { 1 , 1 },
99
 
    [PIX_FMT_RGBA]        = { 1 , 1 },
100
 
    [PIX_FMT_ABGR]        = { 1 , 1 },
101
 
    [PIX_FMT_BGRA]        = { 1 , 1 },
102
 
    [PIX_FMT_GRAY16BE]    = { 1 , 1 },
103
 
    [PIX_FMT_GRAY16LE]    = { 1 , 1 },
104
 
    [PIX_FMT_YUV440P]     = { 1 , 1 },
105
 
    [PIX_FMT_YUVJ440P]    = { 1 , 1 },
106
 
    [PIX_FMT_YUVA420P]    = { 1 , 1 },
107
 
    [PIX_FMT_RGB48BE]     = { 1 , 1 },
108
 
    [PIX_FMT_RGB48LE]     = { 1 , 1 },
109
 
    [PIX_FMT_RGB565BE]    = { 1 , 1 },
110
 
    [PIX_FMT_RGB565LE]    = { 1 , 1 },
111
 
    [PIX_FMT_RGB555BE]    = { 1 , 1 },
112
 
    [PIX_FMT_RGB555LE]    = { 1 , 1 },
113
 
    [PIX_FMT_BGR565BE]    = { 1 , 1 },
114
 
    [PIX_FMT_BGR565LE]    = { 1 , 1 },
115
 
    [PIX_FMT_BGR555BE]    = { 1 , 1 },
116
 
    [PIX_FMT_BGR555LE]    = { 1 , 1 },
117
 
    [PIX_FMT_YUV420P16LE] = { 1 , 1 },
118
 
    [PIX_FMT_YUV420P16BE] = { 1 , 1 },
119
 
    [PIX_FMT_YUV422P16LE] = { 1 , 1 },
120
 
    [PIX_FMT_YUV422P16BE] = { 1 , 1 },
121
 
    [PIX_FMT_YUV444P16LE] = { 1 , 1 },
122
 
    [PIX_FMT_YUV444P16BE] = { 1 , 1 },
123
 
    [PIX_FMT_RGB444LE]    = { 1 , 1 },
124
 
    [PIX_FMT_RGB444BE]    = { 1 , 1 },
125
 
    [PIX_FMT_BGR444LE]    = { 1 , 1 },
126
 
    [PIX_FMT_BGR444BE]    = { 1 , 1 },
127
 
    [PIX_FMT_Y400A]       = { 1 , 0 },
128
 
    [PIX_FMT_BGR48BE]     = { 1 , 1 },
129
 
    [PIX_FMT_BGR48LE]     = { 1 , 1 },
130
 
    [PIX_FMT_YUV420P9BE]  = { 1 , 1 },
131
 
    [PIX_FMT_YUV420P9LE]  = { 1 , 1 },
132
 
    [PIX_FMT_YUV420P10BE] = { 1 , 1 },
133
 
    [PIX_FMT_YUV420P10LE] = { 1 , 1 },
134
 
    [PIX_FMT_YUV422P9BE]  = { 1 , 1 },
135
 
    [PIX_FMT_YUV422P9LE]  = { 1 , 1 },
136
 
    [PIX_FMT_YUV422P10BE] = { 1 , 1 },
137
 
    [PIX_FMT_YUV422P10LE] = { 1 , 1 },
138
 
    [PIX_FMT_YUV444P9BE]  = { 1 , 1 },
139
 
    [PIX_FMT_YUV444P9LE]  = { 1 , 1 },
140
 
    [PIX_FMT_YUV444P10BE] = { 1 , 1 },
141
 
    [PIX_FMT_YUV444P10LE] = { 1 , 1 },
142
 
    [PIX_FMT_GBRP]        = { 1 , 0 },
143
 
    [PIX_FMT_GBRP9LE]     = { 1 , 0 },
144
 
    [PIX_FMT_GBRP9BE]     = { 1 , 0 },
145
 
    [PIX_FMT_GBRP10LE]    = { 1 , 0 },
146
 
    [PIX_FMT_GBRP10BE]    = { 1 , 0 },
147
 
    [PIX_FMT_GBRP16LE]    = { 1 , 0 },
148
 
    [PIX_FMT_GBRP16BE]    = { 1 , 0 },
 
76
static const FormatEntry format_entries[AV_PIX_FMT_NB] = {
 
77
    [AV_PIX_FMT_YUV420P]     = { 1, 1 },
 
78
    [AV_PIX_FMT_YUYV422]     = { 1, 1 },
 
79
    [AV_PIX_FMT_RGB24]       = { 1, 1 },
 
80
    [AV_PIX_FMT_BGR24]       = { 1, 1 },
 
81
    [AV_PIX_FMT_YUV422P]     = { 1, 1 },
 
82
    [AV_PIX_FMT_YUV444P]     = { 1, 1 },
 
83
    [AV_PIX_FMT_YUV410P]     = { 1, 1 },
 
84
    [AV_PIX_FMT_YUV411P]     = { 1, 1 },
 
85
    [AV_PIX_FMT_GRAY8]       = { 1, 1 },
 
86
    [AV_PIX_FMT_MONOWHITE]   = { 1, 1 },
 
87
    [AV_PIX_FMT_MONOBLACK]   = { 1, 1 },
 
88
    [AV_PIX_FMT_PAL8]        = { 1, 0 },
 
89
    [AV_PIX_FMT_YUVJ420P]    = { 1, 1 },
 
90
    [AV_PIX_FMT_YUVJ422P]    = { 1, 1 },
 
91
    [AV_PIX_FMT_YUVJ444P]    = { 1, 1 },
 
92
    [AV_PIX_FMT_UYVY422]     = { 1, 1 },
 
93
    [AV_PIX_FMT_UYYVYY411]   = { 0, 0 },
 
94
    [AV_PIX_FMT_BGR8]        = { 1, 1 },
 
95
    [AV_PIX_FMT_BGR4]        = { 0, 1 },
 
96
    [AV_PIX_FMT_BGR4_BYTE]   = { 1, 1 },
 
97
    [AV_PIX_FMT_RGB8]        = { 1, 1 },
 
98
    [AV_PIX_FMT_RGB4]        = { 0, 1 },
 
99
    [AV_PIX_FMT_RGB4_BYTE]   = { 1, 1 },
 
100
    [AV_PIX_FMT_NV12]        = { 1, 1 },
 
101
    [AV_PIX_FMT_NV21]        = { 1, 1 },
 
102
    [AV_PIX_FMT_ARGB]        = { 1, 1 },
 
103
    [AV_PIX_FMT_RGBA]        = { 1, 1 },
 
104
    [AV_PIX_FMT_ABGR]        = { 1, 1 },
 
105
    [AV_PIX_FMT_BGRA]        = { 1, 1 },
 
106
    [AV_PIX_FMT_GRAY16BE]    = { 1, 1 },
 
107
    [AV_PIX_FMT_GRAY16LE]    = { 1, 1 },
 
108
    [AV_PIX_FMT_YUV440P]     = { 1, 1 },
 
109
    [AV_PIX_FMT_YUVJ440P]    = { 1, 1 },
 
110
    [AV_PIX_FMT_YUVA420P]    = { 1, 1 },
 
111
    [AV_PIX_FMT_YUVA422P]    = { 1, 1 },
 
112
    [AV_PIX_FMT_YUVA444P]    = { 1, 1 },
 
113
    [AV_PIX_FMT_YUVA420P9BE] = { 1, 1 },
 
114
    [AV_PIX_FMT_YUVA420P9LE] = { 1, 1 },
 
115
    [AV_PIX_FMT_YUVA422P9BE] = { 1, 1 },
 
116
    [AV_PIX_FMT_YUVA422P9LE] = { 1, 1 },
 
117
    [AV_PIX_FMT_YUVA444P9BE] = { 1, 1 },
 
118
    [AV_PIX_FMT_YUVA444P9LE] = { 1, 1 },
 
119
    [AV_PIX_FMT_YUVA420P10BE]= { 1, 1 },
 
120
    [AV_PIX_FMT_YUVA420P10LE]= { 1, 1 },
 
121
    [AV_PIX_FMT_YUVA422P10BE]= { 1, 1 },
 
122
    [AV_PIX_FMT_YUVA422P10LE]= { 1, 1 },
 
123
    [AV_PIX_FMT_YUVA444P10BE]= { 1, 1 },
 
124
    [AV_PIX_FMT_YUVA444P10LE]= { 1, 1 },
 
125
    [AV_PIX_FMT_YUVA420P16BE]= { 1, 1 },
 
126
    [AV_PIX_FMT_YUVA420P16LE]= { 1, 1 },
 
127
    [AV_PIX_FMT_YUVA422P16BE]= { 1, 1 },
 
128
    [AV_PIX_FMT_YUVA422P16LE]= { 1, 1 },
 
129
    [AV_PIX_FMT_YUVA444P16BE]= { 1, 1 },
 
130
    [AV_PIX_FMT_YUVA444P16LE]= { 1, 1 },
 
131
    [AV_PIX_FMT_RGB48BE]     = { 1, 1 },
 
132
    [AV_PIX_FMT_RGB48LE]     = { 1, 1 },
 
133
    [AV_PIX_FMT_RGB565BE]    = { 1, 1 },
 
134
    [AV_PIX_FMT_RGB565LE]    = { 1, 1 },
 
135
    [AV_PIX_FMT_RGB555BE]    = { 1, 1 },
 
136
    [AV_PIX_FMT_RGB555LE]    = { 1, 1 },
 
137
    [AV_PIX_FMT_BGR565BE]    = { 1, 1 },
 
138
    [AV_PIX_FMT_BGR565LE]    = { 1, 1 },
 
139
    [AV_PIX_FMT_BGR555BE]    = { 1, 1 },
 
140
    [AV_PIX_FMT_BGR555LE]    = { 1, 1 },
 
141
    [AV_PIX_FMT_YUV420P16LE] = { 1, 1 },
 
142
    [AV_PIX_FMT_YUV420P16BE] = { 1, 1 },
 
143
    [AV_PIX_FMT_YUV422P16LE] = { 1, 1 },
 
144
    [AV_PIX_FMT_YUV422P16BE] = { 1, 1 },
 
145
    [AV_PIX_FMT_YUV444P16LE] = { 1, 1 },
 
146
    [AV_PIX_FMT_YUV444P16BE] = { 1, 1 },
 
147
    [AV_PIX_FMT_RGB444LE]    = { 1, 1 },
 
148
    [AV_PIX_FMT_RGB444BE]    = { 1, 1 },
 
149
    [AV_PIX_FMT_BGR444LE]    = { 1, 1 },
 
150
    [AV_PIX_FMT_BGR444BE]    = { 1, 1 },
 
151
    [AV_PIX_FMT_Y400A]       = { 1, 0 },
 
152
    [AV_PIX_FMT_BGR48BE]     = { 1, 1 },
 
153
    [AV_PIX_FMT_BGR48LE]     = { 1, 1 },
 
154
    [AV_PIX_FMT_YUV420P9BE]  = { 1, 1 },
 
155
    [AV_PIX_FMT_YUV420P9LE]  = { 1, 1 },
 
156
    [AV_PIX_FMT_YUV420P10BE] = { 1, 1 },
 
157
    [AV_PIX_FMT_YUV420P10LE] = { 1, 1 },
 
158
    [AV_PIX_FMT_YUV422P9BE]  = { 1, 1 },
 
159
    [AV_PIX_FMT_YUV422P9LE]  = { 1, 1 },
 
160
    [AV_PIX_FMT_YUV422P10BE] = { 1, 1 },
 
161
    [AV_PIX_FMT_YUV422P10LE] = { 1, 1 },
 
162
    [AV_PIX_FMT_YUV444P9BE]  = { 1, 1 },
 
163
    [AV_PIX_FMT_YUV444P9LE]  = { 1, 1 },
 
164
    [AV_PIX_FMT_YUV444P10BE] = { 1, 1 },
 
165
    [AV_PIX_FMT_YUV444P10LE] = { 1, 1 },
 
166
    [AV_PIX_FMT_GBRP]        = { 1, 0 },
 
167
    [AV_PIX_FMT_GBRP9LE]     = { 1, 0 },
 
168
    [AV_PIX_FMT_GBRP9BE]     = { 1, 0 },
 
169
    [AV_PIX_FMT_GBRP10LE]    = { 1, 0 },
 
170
    [AV_PIX_FMT_GBRP10BE]    = { 1, 0 },
 
171
    [AV_PIX_FMT_GBRP16LE]    = { 1, 0 },
 
172
    [AV_PIX_FMT_GBRP16BE]    = { 1, 0 },
149
173
};
150
174
 
151
 
int sws_isSupportedInput(enum PixelFormat pix_fmt)
 
175
int sws_isSupportedInput(enum AVPixelFormat pix_fmt)
152
176
{
153
 
    return (unsigned)pix_fmt < PIX_FMT_NB ?
154
 
        format_entries[pix_fmt].is_supported_in : 0;
 
177
    return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
 
178
           format_entries[pix_fmt].is_supported_in : 0;
155
179
}
156
180
 
157
 
int sws_isSupportedOutput(enum PixelFormat pix_fmt)
 
181
int sws_isSupportedOutput(enum AVPixelFormat pix_fmt)
158
182
{
159
 
    return (unsigned)pix_fmt < PIX_FMT_NB ?
160
 
        format_entries[pix_fmt].is_supported_out : 0;
 
183
    return (unsigned)pix_fmt < AV_PIX_FMT_NB ?
 
184
           format_entries[pix_fmt].is_supported_out : 0;
161
185
}
162
186
 
163
187
extern const int32_t ff_yuv2rgb_coeffs[8][4];
164
188
 
165
 
const char *sws_format_name(enum PixelFormat format)
 
189
const char *sws_format_name(enum AVPixelFormat format)
166
190
{
167
 
    if ((unsigned)format < PIX_FMT_NB && av_pix_fmt_descriptors[format].name)
168
 
        return av_pix_fmt_descriptors[format].name;
 
191
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
 
192
    if (desc)
 
193
        return desc->name;
169
194
    else
170
195
        return "Unknown format";
171
196
}
172
197
 
173
 
static double getSplineCoeff(double a, double b, double c, double d, double dist)
 
198
static double getSplineCoeff(double a, double b, double c, double d,
 
199
                             double dist)
174
200
{
175
 
    if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
176
 
    else           return getSplineCoeff(        0.0,
177
 
                                          b+ 2.0*c + 3.0*d,
178
 
                                                 c + 3.0*d,
179
 
                                         -b- 3.0*c - 6.0*d,
180
 
                                         dist-1.0);
 
201
    if (dist <= 1.0)
 
202
        return ((d * dist + c) * dist + b) * dist + a;
 
203
    else
 
204
        return getSplineCoeff(0.0,
 
205
                               b + 2.0 * c + 3.0 * d,
 
206
                               c + 3.0 * d,
 
207
                              -b - 3.0 * c - 6.0 * d,
 
208
                              dist - 1.0);
181
209
}
182
210
 
183
 
static int initFilter(int16_t **outFilter, int32_t **filterPos, int *outFilterSize, int xInc,
184
 
                      int srcW, int dstW, int filterAlign, int one, int flags, int cpu_flags,
185
 
                      SwsVector *srcFilter, SwsVector *dstFilter, double param[2], int is_horizontal)
 
211
static int initFilter(int16_t **outFilter, int32_t **filterPos,
 
212
                      int *outFilterSize, int xInc, int srcW, int dstW,
 
213
                      int filterAlign, int one, int flags, int cpu_flags,
 
214
                      SwsVector *srcFilter, SwsVector *dstFilter,
 
215
                      double param[2], int is_horizontal)
186
216
{
187
217
    int i;
188
218
    int filterSize;
189
219
    int filter2Size;
190
220
    int minFilterSize;
191
 
    int64_t *filter=NULL;
192
 
    int64_t *filter2=NULL;
193
 
    const int64_t fone= 1LL<<54;
194
 
    int ret= -1;
195
 
 
196
 
    emms_c(); //FIXME this should not be required but it IS (even for non-MMX versions)
197
 
 
198
 
    // NOTE: the +3 is for the MMX(+1)/SSE(+3) scaler which reads over the end
199
 
    FF_ALLOC_OR_GOTO(NULL, *filterPos, (dstW+3)*sizeof(**filterPos), fail);
200
 
 
201
 
    if (FFABS(xInc - 0x10000) <10) { // unscaled
202
 
        int i;
203
 
        filterSize= 1;
204
 
        FF_ALLOCZ_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
205
 
 
206
 
        for (i=0; i<dstW; i++) {
207
 
            filter[i*filterSize]= fone;
208
 
            (*filterPos)[i]=i;
209
 
        }
210
 
 
211
 
    } else if (flags&SWS_POINT) { // lame looking point sampling mode
212
 
        int i;
213
 
        int xDstInSrc;
214
 
        filterSize= 1;
215
 
        FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
216
 
 
217
 
        xDstInSrc= xInc/2 - 0x8000;
218
 
        for (i=0; i<dstW; i++) {
219
 
            int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
220
 
 
221
 
            (*filterPos)[i]= xx;
222
 
            filter[i]= fone;
223
 
            xDstInSrc+= xInc;
224
 
        }
225
 
    } else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) { // bilinear upscale
226
 
        int i;
227
 
        int xDstInSrc;
228
 
        filterSize= 2;
229
 
        FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
230
 
 
231
 
        xDstInSrc= xInc/2 - 0x8000;
232
 
        for (i=0; i<dstW; i++) {
233
 
            int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
 
221
    int64_t *filter    = NULL;
 
222
    int64_t *filter2   = NULL;
 
223
    const int64_t fone = 1LL << 54;
 
224
    int ret            = -1;
 
225
 
 
226
    emms_c(); // FIXME should not be required but IS (even for non-MMX versions)
 
227
 
 
228
    // NOTE: the +3 is for the MMX(+1) / SSE(+3) scaler which reads over the end
 
229
    FF_ALLOC_OR_GOTO(NULL, *filterPos, (dstW + 3) * sizeof(**filterPos), fail);
 
230
 
 
231
    if (FFABS(xInc - 0x10000) < 10) { // unscaled
 
232
        int i;
 
233
        filterSize = 1;
 
234
        FF_ALLOCZ_OR_GOTO(NULL, filter,
 
235
                          dstW * sizeof(*filter) * filterSize, fail);
 
236
 
 
237
        for (i = 0; i < dstW; i++) {
 
238
            filter[i * filterSize] = fone;
 
239
            (*filterPos)[i]        = i;
 
240
        }
 
241
    } else if (flags & SWS_POINT) { // lame looking point sampling mode
 
242
        int i;
 
243
        int xDstInSrc;
 
244
        filterSize = 1;
 
245
        FF_ALLOC_OR_GOTO(NULL, filter,
 
246
                         dstW * sizeof(*filter) * filterSize, fail);
 
247
 
 
248
        xDstInSrc = xInc / 2 - 0x8000;
 
249
        for (i = 0; i < dstW; i++) {
 
250
            int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
 
251
 
 
252
            (*filterPos)[i] = xx;
 
253
            filter[i]       = fone;
 
254
            xDstInSrc      += xInc;
 
255
        }
 
256
    } else if ((xInc <= (1 << 16) && (flags & SWS_AREA)) ||
 
257
               (flags & SWS_FAST_BILINEAR)) { // bilinear upscale
 
258
        int i;
 
259
        int xDstInSrc;
 
260
        filterSize = 2;
 
261
        FF_ALLOC_OR_GOTO(NULL, filter,
 
262
                         dstW * sizeof(*filter) * filterSize, fail);
 
263
 
 
264
        xDstInSrc = xInc / 2 - 0x8000;
 
265
        for (i = 0; i < dstW; i++) {
 
266
            int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
234
267
            int j;
235
268
 
236
 
            (*filterPos)[i]= xx;
237
 
            //bilinear upscale / linear interpolate / area averaging
238
 
            for (j=0; j<filterSize; j++) {
239
 
                int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
240
 
                if (coeff<0) coeff=0;
241
 
                filter[i*filterSize + j]= coeff;
 
269
            (*filterPos)[i] = xx;
 
270
            // bilinear upscale / linear interpolate / area averaging
 
271
            for (j = 0; j < filterSize; j++) {
 
272
                int64_t coeff = fone - FFABS((xx << 16) - xDstInSrc) *
 
273
                                (fone >> 16);
 
274
                if (coeff < 0)
 
275
                    coeff = 0;
 
276
                filter[i * filterSize + j] = coeff;
242
277
                xx++;
243
278
            }
244
 
            xDstInSrc+= xInc;
 
279
            xDstInSrc += xInc;
245
280
        }
246
281
    } else {
247
282
        int64_t xDstInSrc;
248
283
        int sizeFactor;
249
284
 
250
 
        if      (flags&SWS_BICUBIC)      sizeFactor=  4;
251
 
        else if (flags&SWS_X)            sizeFactor=  8;
252
 
        else if (flags&SWS_AREA)         sizeFactor=  1; //downscale only, for upscale it is bilinear
253
 
        else if (flags&SWS_GAUSS)        sizeFactor=  8;   // infinite ;)
254
 
        else if (flags&SWS_LANCZOS)      sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
255
 
        else if (flags&SWS_SINC)         sizeFactor= 20; // infinite ;)
256
 
        else if (flags&SWS_SPLINE)       sizeFactor= 20;  // infinite ;)
257
 
        else if (flags&SWS_BILINEAR)     sizeFactor=  2;
 
285
        if (flags & SWS_BICUBIC)
 
286
            sizeFactor = 4;
 
287
        else if (flags & SWS_X)
 
288
            sizeFactor = 8;
 
289
        else if (flags & SWS_AREA)
 
290
            sizeFactor = 1;     // downscale only, for upscale it is bilinear
 
291
        else if (flags & SWS_GAUSS)
 
292
            sizeFactor = 8;     // infinite ;)
 
293
        else if (flags & SWS_LANCZOS)
 
294
            sizeFactor = param[0] != SWS_PARAM_DEFAULT ? ceil(2 * param[0]) : 6;
 
295
        else if (flags & SWS_SINC)
 
296
            sizeFactor = 20;    // infinite ;)
 
297
        else if (flags & SWS_SPLINE)
 
298
            sizeFactor = 20;    // infinite ;)
 
299
        else if (flags & SWS_BILINEAR)
 
300
            sizeFactor = 2;
258
301
        else {
259
 
            sizeFactor= 0; //GCC warning killer
 
302
            sizeFactor = 0;     // GCC warning killer
260
303
            assert(0);
261
304
        }
262
305
 
263
 
        if (xInc <= 1<<16)      filterSize= 1 + sizeFactor; // upscale
264
 
        else                    filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
265
 
 
266
 
        filterSize = av_clip(filterSize, 1, srcW - 2);
267
 
 
268
 
        FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
269
 
 
270
 
        xDstInSrc= xInc - 0x10000;
271
 
        for (i=0; i<dstW; i++) {
272
 
            int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
 
306
        if (xInc <= 1 << 16)
 
307
            filterSize = 1 + sizeFactor;    // upscale
 
308
        else
 
309
            filterSize = 1 + (sizeFactor * srcW + dstW - 1) / dstW;
 
310
 
 
311
        filterSize = FFMIN(filterSize, srcW - 2);
 
312
        filterSize = FFMAX(filterSize, 1);
 
313
 
 
314
        FF_ALLOC_OR_GOTO(NULL, filter,
 
315
                         dstW * sizeof(*filter) * filterSize, fail);
 
316
 
 
317
        xDstInSrc = xInc - 0x10000;
 
318
        for (i = 0; i < dstW; i++) {
 
319
            int xx = (xDstInSrc - ((filterSize - 2) << 16)) / (1 << 17);
273
320
            int j;
274
 
            (*filterPos)[i]= xx;
275
 
            for (j=0; j<filterSize; j++) {
276
 
                int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
 
321
            (*filterPos)[i] = xx;
 
322
            for (j = 0; j < filterSize; j++) {
 
323
                int64_t d = (FFABS(((int64_t)xx << 17) - xDstInSrc)) << 13;
277
324
                double floatd;
278
325
                int64_t coeff;
279
326
 
280
 
                if (xInc > 1<<16)
281
 
                    d= d*dstW/srcW;
282
 
                floatd= d * (1.0/(1<<30));
 
327
                if (xInc > 1 << 16)
 
328
                    d = d * dstW / srcW;
 
329
                floatd = d * (1.0 / (1 << 30));
283
330
 
284
331
                if (flags & SWS_BICUBIC) {
285
 
                    int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] :   0) * (1<<24);
286
 
                    int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
 
332
                    int64_t B = (param[0] != SWS_PARAM_DEFAULT ? param[0] :   0) * (1 << 24);
 
333
                    int64_t C = (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1 << 24);
287
334
 
288
 
                    if (d >= 1LL<<31) {
 
335
                    if (d >= 1LL << 31) {
289
336
                        coeff = 0.0;
290
337
                    } else {
291
338
                        int64_t dd  = (d  * d) >> 30;
292
339
                        int64_t ddd = (dd * d) >> 30;
293
340
 
294
 
                        if (d < 1LL<<30)
295
 
                            coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
 
341
                        if (d < 1LL << 30)
 
342
                            coeff =  (12 * (1 << 24) -  9 * B - 6 * C) * ddd +
 
343
                                    (-18 * (1 << 24) + 12 * B + 6 * C) *  dd +
 
344
                                      (6 * (1 << 24) -  2 * B)         * (1 << 30);
296
345
                        else
297
 
                            coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
 
346
                            coeff =      (-B -  6 * C) * ddd +
 
347
                                      (6 * B + 30 * C) * dd  +
 
348
                                    (-12 * B - 48 * C) * d   +
 
349
                                      (8 * B + 24 * C) * (1 << 30);
298
350
                    }
299
 
                    coeff *= fone>>(30+24);
300
 
                }
301
 
/*                else if (flags & SWS_X) {
302
 
                    double p= param ? param*0.01 : 0.3;
303
 
                    coeff = d ? sin(d*M_PI)/(d*M_PI) : 1.0;
304
 
                    coeff*= pow(2.0, - p*d*d);
305
 
                }*/
306
 
                else if (flags & SWS_X) {
307
 
                    double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
 
351
                    coeff *= fone >> (30 + 24);
 
352
                }
 
353
#if 0
 
354
                else if (flags & SWS_X) {
 
355
                    double p  = param ? param * 0.01 : 0.3;
 
356
                    coeff     = d ? sin(d * M_PI) / (d * M_PI) : 1.0;
 
357
                    coeff    *= pow(2.0, -p * d * d);
 
358
                }
 
359
#endif
 
360
                else if (flags & SWS_X) {
 
361
                    double A = param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
308
362
                    double c;
309
363
 
310
 
                    if (floatd<1.0)
311
 
                        c = cos(floatd*M_PI);
312
 
                    else
313
 
                        c=-1.0;
314
 
                    if (c<0.0)      c= -pow(-c, A);
315
 
                    else            c=  pow( c, A);
316
 
                    coeff= (c*0.5 + 0.5)*fone;
 
364
                    if (floatd < 1.0)
 
365
                        c = cos(floatd * M_PI);
 
366
                    else
 
367
                        c = -1.0;
 
368
                    if (c < 0.0)
 
369
                        c = -pow(-c, A);
 
370
                    else
 
371
                        c = pow(c, A);
 
372
                    coeff = (c * 0.5 + 0.5) * fone;
317
373
                } else if (flags & SWS_AREA) {
318
 
                    int64_t d2= d - (1<<29);
319
 
                    if      (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
320
 
                    else if (d2*xInc <  (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
321
 
                    else coeff=0.0;
322
 
                    coeff *= fone>>(30+16);
 
374
                    int64_t d2 = d - (1 << 29);
 
375
                    if (d2 * xInc < -(1LL << (29 + 16)))
 
376
                        coeff = 1.0 * (1LL << (30 + 16));
 
377
                    else if (d2 * xInc < (1LL << (29 + 16)))
 
378
                        coeff = -d2 * xInc + (1LL << (29 + 16));
 
379
                    else
 
380
                        coeff = 0.0;
 
381
                    coeff *= fone >> (30 + 16);
323
382
                } else if (flags & SWS_GAUSS) {
324
 
                    double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
325
 
                    coeff = (pow(2.0, - p*floatd*floatd))*fone;
 
383
                    double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
 
384
                    coeff = (pow(2.0, -p * floatd * floatd)) * fone;
326
385
                } else if (flags & SWS_SINC) {
327
 
                    coeff = (d ? sin(floatd*M_PI)/(floatd*M_PI) : 1.0)*fone;
 
386
                    coeff = (d ? sin(floatd * M_PI) / (floatd * M_PI) : 1.0) * fone;
328
387
                } else if (flags & SWS_LANCZOS) {
329
 
                    double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
330
 
                    coeff = (d ? sin(floatd*M_PI)*sin(floatd*M_PI/p)/(floatd*floatd*M_PI*M_PI/p) : 1.0)*fone;
331
 
                    if (floatd>p) coeff=0;
 
388
                    double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
 
389
                    coeff = (d ? sin(floatd * M_PI) * sin(floatd * M_PI / p) /
 
390
                             (floatd * floatd * M_PI * M_PI / p) : 1.0) * fone;
 
391
                    if (floatd > p)
 
392
                        coeff = 0;
332
393
                } else if (flags & SWS_BILINEAR) {
333
 
                    coeff= (1<<30) - d;
334
 
                    if (coeff<0) coeff=0;
 
394
                    coeff = (1 << 30) - d;
 
395
                    if (coeff < 0)
 
396
                        coeff = 0;
335
397
                    coeff *= fone >> 30;
336
398
                } else if (flags & SWS_SPLINE) {
337
 
                    double p=-2.196152422706632;
338
 
                    coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
 
399
                    double p = -2.196152422706632;
 
400
                    coeff = getSplineCoeff(1.0, 0.0, p, -p - 1.0, floatd) * fone;
339
401
                } else {
340
 
                    coeff= 0.0; //GCC warning killer
 
402
                    coeff = 0.0; // GCC warning killer
341
403
                    assert(0);
342
404
                }
343
405
 
344
 
                filter[i*filterSize + j]= coeff;
 
406
                filter[i * filterSize + j] = coeff;
345
407
                xx++;
346
408
            }
347
 
            xDstInSrc+= 2*xInc;
 
409
            xDstInSrc += 2 * xInc;
348
410
        }
349
411
    }
350
412
 
351
413
    /* apply src & dst Filter to filter -> filter2
352
 
       av_free(filter);
353
 
    */
354
 
    assert(filterSize>0);
355
 
    filter2Size= filterSize;
356
 
    if (srcFilter) filter2Size+= srcFilter->length - 1;
357
 
    if (dstFilter) filter2Size+= dstFilter->length - 1;
358
 
    assert(filter2Size>0);
359
 
    FF_ALLOCZ_OR_GOTO(NULL, filter2, filter2Size*dstW*sizeof(*filter2), fail);
 
414
     * av_free(filter);
 
415
     */
 
416
    assert(filterSize > 0);
 
417
    filter2Size = filterSize;
 
418
    if (srcFilter)
 
419
        filter2Size += srcFilter->length - 1;
 
420
    if (dstFilter)
 
421
        filter2Size += dstFilter->length - 1;
 
422
    assert(filter2Size > 0);
 
423
    FF_ALLOCZ_OR_GOTO(NULL, filter2, filter2Size * dstW * sizeof(*filter2), fail);
360
424
 
361
 
    for (i=0; i<dstW; i++) {
 
425
    for (i = 0; i < dstW; i++) {
362
426
        int j, k;
363
427
 
364
 
        if(srcFilter) {
365
 
            for (k=0; k<srcFilter->length; k++) {
366
 
                for (j=0; j<filterSize; j++)
367
 
                    filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
 
428
        if (srcFilter) {
 
429
            for (k = 0; k < srcFilter->length; k++) {
 
430
                for (j = 0; j < filterSize; j++)
 
431
                    filter2[i * filter2Size + k + j] +=
 
432
                        srcFilter->coeff[k] * filter[i * filterSize + j];
368
433
            }
369
434
        } else {
370
 
            for (j=0; j<filterSize; j++)
371
 
                filter2[i*filter2Size + j]= filter[i*filterSize + j];
 
435
            for (j = 0; j < filterSize; j++)
 
436
                filter2[i * filter2Size + j] = filter[i * filterSize + j];
372
437
        }
373
 
        //FIXME dstFilter
 
438
        // FIXME dstFilter
374
439
 
375
 
        (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
 
440
        (*filterPos)[i] += (filterSize - 1) / 2 - (filter2Size - 1) / 2;
376
441
    }
377
442
    av_freep(&filter);
378
443
 
379
444
    /* try to reduce the filter-size (step1 find size and shift left) */
380
445
    // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
381
 
    minFilterSize= 0;
382
 
    for (i=dstW-1; i>=0; i--) {
383
 
        int min= filter2Size;
 
446
    minFilterSize = 0;
 
447
    for (i = dstW - 1; i >= 0; i--) {
 
448
        int min = filter2Size;
384
449
        int j;
385
 
        int64_t cutOff=0.0;
 
450
        int64_t cutOff = 0.0;
386
451
 
387
452
        /* get rid of near zero elements on the left by shifting left */
388
 
        for (j=0; j<filter2Size; j++) {
 
453
        for (j = 0; j < filter2Size; j++) {
389
454
            int k;
390
 
            cutOff += FFABS(filter2[i*filter2Size]);
391
 
 
392
 
            if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
393
 
 
394
 
            /* preserve monotonicity because the core can't handle the filter otherwise */
395
 
            if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
 
455
            cutOff += FFABS(filter2[i * filter2Size]);
 
456
 
 
457
            if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
 
458
                break;
 
459
 
 
460
            /* preserve monotonicity because the core can't handle the
 
461
             * filter otherwise */
 
462
            if (i < dstW - 1 && (*filterPos)[i] >= (*filterPos)[i + 1])
 
463
                break;
396
464
 
397
465
            // move filter coefficients left
398
 
            for (k=1; k<filter2Size; k++)
399
 
                filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
400
 
            filter2[i*filter2Size + k - 1]= 0;
 
466
            for (k = 1; k < filter2Size; k++)
 
467
                filter2[i * filter2Size + k - 1] = filter2[i * filter2Size + k];
 
468
            filter2[i * filter2Size + k - 1] = 0;
401
469
            (*filterPos)[i]++;
402
470
        }
403
471
 
404
 
        cutOff=0;
 
472
        cutOff = 0;
405
473
        /* count near zeros on the right */
406
 
        for (j=filter2Size-1; j>0; j--) {
407
 
            cutOff += FFABS(filter2[i*filter2Size + j]);
 
474
        for (j = filter2Size - 1; j > 0; j--) {
 
475
            cutOff += FFABS(filter2[i * filter2Size + j]);
408
476
 
409
 
            if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
 
477
            if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
 
478
                break;
410
479
            min--;
411
480
        }
412
481
 
413
 
        if (min>minFilterSize) minFilterSize= min;
 
482
        if (min > minFilterSize)
 
483
            minFilterSize = min;
414
484
    }
415
485
 
416
486
    if (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) {
417
 
        // we can handle the special case 4,
418
 
        // so we don't want to go to the full 8
 
487
        // we can handle the special case 4, so we don't want to go the full 8
419
488
        if (minFilterSize < 5)
420
489
            filterAlign = 4;
421
490
 
422
 
        // We really don't want to waste our time
423
 
        // doing useless computation, so fall back on
424
 
        // the scalar C code for very small filters.
425
 
        // Vectorizing is worth it only if you have a
426
 
        // decent-sized vector.
 
491
        /* We really don't want to waste our time doing useless computation, so
 
492
         * fall back on the scalar C code for very small filters.
 
493
         * Vectorizing is worth it only if you have a decent-sized vector. */
427
494
        if (minFilterSize < 3)
428
495
            filterAlign = 1;
429
496
    }
430
497
 
431
 
    if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
 
498
    if (INLINE_MMX(cpu_flags)) {
432
499
        // special case for unscaled vertical filtering
433
500
        if (minFilterSize == 1 && filterAlign == 2)
434
 
            filterAlign= 1;
 
501
            filterAlign = 1;
435
502
    }
436
503
 
437
504
    assert(minFilterSize > 0);
438
 
    filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
 
505
    filterSize = (minFilterSize + (filterAlign - 1)) & (~(filterAlign - 1));
439
506
    assert(filterSize > 0);
440
 
    filter= av_malloc(filterSize*dstW*sizeof(*filter));
441
 
    if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
 
507
    filter = av_malloc(filterSize * dstW * sizeof(*filter));
 
508
    if (filterSize >= MAX_FILTER_SIZE * 16 /
 
509
                      ((flags & SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
442
510
        goto fail;
443
 
    *outFilterSize= filterSize;
 
511
    *outFilterSize = filterSize;
444
512
 
445
 
    if (flags&SWS_PRINT_INFO)
446
 
        av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
 
513
    if (flags & SWS_PRINT_INFO)
 
514
        av_log(NULL, AV_LOG_VERBOSE,
 
515
               "SwScaler: reducing / aligning filtersize %d -> %d\n",
 
516
               filter2Size, filterSize);
447
517
    /* try to reduce the filter-size (step2 reduce it) */
448
 
    for (i=0; i<dstW; i++) {
 
518
    for (i = 0; i < dstW; i++) {
449
519
        int j;
450
520
 
451
 
        for (j=0; j<filterSize; j++) {
452
 
            if (j>=filter2Size) filter[i*filterSize + j]= 0;
453
 
            else               filter[i*filterSize + j]= filter2[i*filter2Size + j];
454
 
            if((flags & SWS_BITEXACT) && j>=minFilterSize)
455
 
                filter[i*filterSize + j]= 0;
 
521
        for (j = 0; j < filterSize; j++) {
 
522
            if (j >= filter2Size)
 
523
                filter[i * filterSize + j] = 0;
 
524
            else
 
525
                filter[i * filterSize + j] = filter2[i * filter2Size + j];
 
526
            if ((flags & SWS_BITEXACT) && j >= minFilterSize)
 
527
                filter[i * filterSize + j] = 0;
456
528
        }
457
529
    }
458
530
 
459
 
    //FIXME try to align filterPos if possible
 
531
    // FIXME try to align filterPos if possible
460
532
 
461
 
    //fix borders
 
533
    // fix borders
462
534
    if (is_horizontal) {
463
535
        for (i = 0; i < dstW; i++) {
464
536
            int j;
467
539
                for (j = 1; j < filterSize; j++) {
468
540
                    int left = FFMAX(j + (*filterPos)[i], 0);
469
541
                    filter[i * filterSize + left] += filter[i * filterSize + j];
470
 
                    filter[i * filterSize + j   ]  = 0;
 
542
                    filter[i * filterSize + j]     = 0;
471
543
                }
472
544
                (*filterPos)[i] = 0;
473
545
            }
478
550
                for (j = filterSize - 2; j >= 0; j--) {
479
551
                    int right = FFMIN(j + shift, filterSize - 1);
480
552
                    filter[i * filterSize + right] += filter[i * filterSize + j];
481
 
                    filter[i * filterSize + j    ]  = 0;
 
553
                    filter[i * filterSize + j]      = 0;
482
554
                }
483
555
                (*filterPos)[i] = srcW - filterSize;
484
556
            }
487
559
 
488
560
    // Note the +1 is for the MMX scaler which reads over the end
489
561
    /* align at 16 for AltiVec (needed by hScale_altivec_real) */
490
 
    FF_ALLOCZ_OR_GOTO(NULL, *outFilter, *outFilterSize*(dstW+3)*sizeof(int16_t), fail);
 
562
    FF_ALLOCZ_OR_GOTO(NULL, *outFilter,
 
563
                      *outFilterSize * (dstW + 3) * sizeof(int16_t), fail);
491
564
 
492
565
    /* normalize & store in outFilter */
493
 
    for (i=0; i<dstW; i++) {
 
566
    for (i = 0; i < dstW; i++) {
494
567
        int j;
495
 
        int64_t error=0;
496
 
        int64_t sum=0;
 
568
        int64_t error = 0;
 
569
        int64_t sum   = 0;
497
570
 
498
 
        for (j=0; j<filterSize; j++) {
499
 
            sum+= filter[i*filterSize + j];
 
571
        for (j = 0; j < filterSize; j++) {
 
572
            sum += filter[i * filterSize + j];
500
573
        }
501
 
        sum= (sum + one/2)/ one;
502
 
        for (j=0; j<*outFilterSize; j++) {
503
 
            int64_t v= filter[i*filterSize + j] + error;
504
 
            int intV= ROUNDED_DIV(v, sum);
505
 
            (*outFilter)[i*(*outFilterSize) + j]= intV;
506
 
            error= v - intV*sum;
 
574
        sum = (sum + one / 2) / one;
 
575
        for (j = 0; j < *outFilterSize; j++) {
 
576
            int64_t v = filter[i * filterSize + j] + error;
 
577
            int intV  = ROUNDED_DIV(v, sum);
 
578
            (*outFilter)[i * (*outFilterSize) + j] = intV;
 
579
            error                                  = v - intV * sum;
507
580
        }
508
581
    }
509
582
 
510
 
    (*filterPos)[dstW+0] =
511
 
    (*filterPos)[dstW+1] =
512
 
    (*filterPos)[dstW+2] = (*filterPos)[dstW-1]; // the MMX/SSE scaler will read over the end
513
 
    for (i=0; i<*outFilterSize; i++) {
514
 
        int k= (dstW - 1) * (*outFilterSize) + i;
 
583
    (*filterPos)[dstW + 0] =
 
584
    (*filterPos)[dstW + 1] =
 
585
    (*filterPos)[dstW + 2] = (*filterPos)[dstW - 1]; /* the MMX/SSE scaler will
 
586
                                                      * read over the end */
 
587
    for (i = 0; i < *outFilterSize; i++) {
 
588
        int k = (dstW - 1) * (*outFilterSize) + i;
515
589
        (*outFilter)[k + 1 * (*outFilterSize)] =
516
590
        (*outFilter)[k + 2 * (*outFilterSize)] =
517
591
        (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
518
592
    }
519
593
 
520
 
    ret=0;
 
594
    ret = 0;
 
595
 
521
596
fail:
522
597
    av_free(filter);
523
598
    av_free(filter2);
524
599
    return ret;
525
600
}
526
601
 
527
 
#if HAVE_MMX2
528
 
static int initMMX2HScaler(int dstW, int xInc, uint8_t *filterCode, int16_t *filter, int32_t *filterPos, int numSplits)
 
602
#if HAVE_MMXEXT_INLINE
 
603
static int init_hscaler_mmxext(int dstW, int xInc, uint8_t *filterCode,
 
604
                               int16_t *filter, int32_t *filterPos,
 
605
                               int numSplits)
529
606
{
530
607
    uint8_t *fragmentA;
531
608
    x86_reg imm8OfPShufW1A;
540
617
    int xpos, i;
541
618
 
542
619
    // create an optimized horizontal scaling routine
543
 
    /* This scaler is made of runtime-generated MMX2 code using specially
544
 
     * tuned pshufw instructions. For every four output pixels, if four
545
 
     * input pixels are enough for the fast bilinear scaling, then a chunk
546
 
     * of fragmentB is used. If five input pixels are needed, then a chunk
547
 
     * of fragmentA is used.
 
620
    /* This scaler is made of runtime-generated MMXEXT code using specially tuned
 
621
     * pshufw instructions. For every four output pixels, if four input pixels
 
622
     * are enough for the fast bilinear scaling, then a chunk of fragmentB is
 
623
     * used. If five input pixels are needed, then a chunk of fragmentA is used.
548
624
     */
549
625
 
550
 
    //code fragment
 
626
    // code fragment
551
627
 
552
 
    __asm__ volatile(
 
628
    __asm__ volatile (
553
629
        "jmp                         9f                 \n\t"
554
 
    // Begin
 
630
        // Begin
555
631
        "0:                                             \n\t"
556
632
        "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
557
633
        "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
571
647
        "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
572
648
 
573
649
        "add                         $8, %%"REG_a"      \n\t"
574
 
    // End
 
650
        // End
575
651
        "9:                                             \n\t"
576
 
//        "int $3                                         \n\t"
577
 
        "lea                 " LOCAL_MANGLE(0b) ", %0   \n\t"
578
 
        "lea                 " LOCAL_MANGLE(1b) ", %1   \n\t"
579
 
        "lea                 " LOCAL_MANGLE(2b) ", %2   \n\t"
 
652
        // "int $3                                         \n\t"
 
653
        "lea       " LOCAL_MANGLE(0b) ", %0             \n\t"
 
654
        "lea       " LOCAL_MANGLE(1b) ", %1             \n\t"
 
655
        "lea       " LOCAL_MANGLE(2b) ", %2             \n\t"
580
656
        "dec                         %1                 \n\t"
581
657
        "dec                         %2                 \n\t"
582
658
        "sub                         %0, %1             \n\t"
583
659
        "sub                         %0, %2             \n\t"
584
 
        "lea                 " LOCAL_MANGLE(9b) ", %3   \n\t"
 
660
        "lea       " LOCAL_MANGLE(9b) ", %3             \n\t"
585
661
        "sub                         %0, %3             \n\t"
586
662
 
587
663
 
588
 
        :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
589
 
        "=r" (fragmentLengthA)
590
 
    );
 
664
        : "=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
 
665
          "=r" (fragmentLengthA)
 
666
        );
591
667
 
592
 
    __asm__ volatile(
 
668
    __asm__ volatile (
593
669
        "jmp                         9f                 \n\t"
594
 
    // Begin
 
670
        // Begin
595
671
        "0:                                             \n\t"
596
672
        "movq    (%%"REG_d", %%"REG_a"), %%mm3          \n\t"
597
673
        "movd    (%%"REG_c", %%"REG_S"), %%mm0          \n\t"
609
685
        "movq                     %%mm0, (%%"REG_D", %%"REG_a") \n\t"
610
686
 
611
687
        "add                         $8, %%"REG_a"      \n\t"
612
 
    // End
 
688
        // End
613
689
        "9:                                             \n\t"
614
 
//        "int                       $3                   \n\t"
615
 
        "lea                 " LOCAL_MANGLE(0b) ", %0   \n\t"
616
 
        "lea                 " LOCAL_MANGLE(1b) ", %1   \n\t"
617
 
        "lea                 " LOCAL_MANGLE(2b) ", %2   \n\t"
 
690
        // "int                       $3                   \n\t"
 
691
        "lea       " LOCAL_MANGLE(0b) ", %0             \n\t"
 
692
        "lea       " LOCAL_MANGLE(1b) ", %1             \n\t"
 
693
        "lea       " LOCAL_MANGLE(2b) ", %2             \n\t"
618
694
        "dec                         %1                 \n\t"
619
695
        "dec                         %2                 \n\t"
620
696
        "sub                         %0, %1             \n\t"
621
697
        "sub                         %0, %2             \n\t"
622
 
        "lea                 " LOCAL_MANGLE(9b) ", %3   \n\t"
 
698
        "lea       " LOCAL_MANGLE(9b) ", %3             \n\t"
623
699
        "sub                         %0, %3             \n\t"
624
700
 
625
701
 
626
 
        :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
627
 
        "=r" (fragmentLengthB)
628
 
    );
629
 
 
630
 
    xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
631
 
    fragmentPos=0;
632
 
 
633
 
    for (i=0; i<dstW/numSplits; i++) {
634
 
        int xx=xpos>>16;
635
 
 
636
 
        if ((i&3) == 0) {
637
 
            int a=0;
638
 
            int b=((xpos+xInc)>>16) - xx;
639
 
            int c=((xpos+xInc*2)>>16) - xx;
640
 
            int d=((xpos+xInc*3)>>16) - xx;
641
 
            int inc                = (d+1<4);
642
 
            uint8_t *fragment      = (d+1<4) ? fragmentB       : fragmentA;
643
 
            x86_reg imm8OfPShufW1  = (d+1<4) ? imm8OfPShufW1B  : imm8OfPShufW1A;
644
 
            x86_reg imm8OfPShufW2  = (d+1<4) ? imm8OfPShufW2B  : imm8OfPShufW2A;
645
 
            x86_reg fragmentLength = (d+1<4) ? fragmentLengthB : fragmentLengthA;
646
 
            int maxShift= 3-(d+inc);
647
 
            int shift=0;
 
702
        : "=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
 
703
          "=r" (fragmentLengthB)
 
704
        );
 
705
 
 
706
    xpos        = 0; // lumXInc/2 - 0x8000; // difference between pixel centers
 
707
    fragmentPos = 0;
 
708
 
 
709
    for (i = 0; i < dstW / numSplits; i++) {
 
710
        int xx = xpos >> 16;
 
711
 
 
712
        if ((i & 3) == 0) {
 
713
            int a                  = 0;
 
714
            int b                  = ((xpos + xInc) >> 16) - xx;
 
715
            int c                  = ((xpos + xInc * 2) >> 16) - xx;
 
716
            int d                  = ((xpos + xInc * 3) >> 16) - xx;
 
717
            int inc                = (d + 1 < 4);
 
718
            uint8_t *fragment      = (d + 1 < 4) ? fragmentB : fragmentA;
 
719
            x86_reg imm8OfPShufW1  = (d + 1 < 4) ? imm8OfPShufW1B : imm8OfPShufW1A;
 
720
            x86_reg imm8OfPShufW2  = (d + 1 < 4) ? imm8OfPShufW2B : imm8OfPShufW2A;
 
721
            x86_reg fragmentLength = (d + 1 < 4) ? fragmentLengthB : fragmentLengthA;
 
722
            int maxShift           = 3 - (d + inc);
 
723
            int shift              = 0;
648
724
 
649
725
            if (filterCode) {
650
 
                filter[i  ] = (( xpos         & 0xFFFF) ^ 0xFFFF)>>9;
651
 
                filter[i+1] = (((xpos+xInc  ) & 0xFFFF) ^ 0xFFFF)>>9;
652
 
                filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
653
 
                filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
654
 
                filterPos[i/2]= xx;
 
726
                filter[i]        = ((xpos              & 0xFFFF) ^ 0xFFFF) >> 9;
 
727
                filter[i + 1]    = (((xpos + xInc)     & 0xFFFF) ^ 0xFFFF) >> 9;
 
728
                filter[i + 2]    = (((xpos + xInc * 2) & 0xFFFF) ^ 0xFFFF) >> 9;
 
729
                filter[i + 3]    = (((xpos + xInc * 3) & 0xFFFF) ^ 0xFFFF) >> 9;
 
730
                filterPos[i / 2] = xx;
655
731
 
656
732
                memcpy(filterCode + fragmentPos, fragment, fragmentLength);
657
733
 
658
 
                filterCode[fragmentPos + imm8OfPShufW1]=
659
 
                    (a+inc) | ((b+inc)<<2) | ((c+inc)<<4) | ((d+inc)<<6);
660
 
                filterCode[fragmentPos + imm8OfPShufW2]=
661
 
                    a | (b<<2) | (c<<4) | (d<<6);
662
 
 
663
 
                if (i+4-inc>=dstW) shift=maxShift; //avoid overread
664
 
                else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
665
 
 
666
 
                if (shift && i>=shift) {
667
 
                    filterCode[fragmentPos + imm8OfPShufW1]+= 0x55*shift;
668
 
                    filterCode[fragmentPos + imm8OfPShufW2]+= 0x55*shift;
669
 
                    filterPos[i/2]-=shift;
 
734
                filterCode[fragmentPos + imm8OfPShufW1] =  (a + inc)       |
 
735
                                                          ((b + inc) << 2) |
 
736
                                                          ((c + inc) << 4) |
 
737
                                                          ((d + inc) << 6);
 
738
                filterCode[fragmentPos + imm8OfPShufW2] =  a | (b << 2) |
 
739
                                                               (c << 4) |
 
740
                                                               (d << 6);
 
741
 
 
742
                if (i + 4 - inc >= dstW)
 
743
                    shift = maxShift;               // avoid overread
 
744
                else if ((filterPos[i / 2] & 3) <= maxShift)
 
745
                    shift = filterPos[i / 2] & 3;   // align
 
746
 
 
747
                if (shift && i >= shift) {
 
748
                    filterCode[fragmentPos + imm8OfPShufW1] += 0x55 * shift;
 
749
                    filterCode[fragmentPos + imm8OfPShufW2] += 0x55 * shift;
 
750
                    filterPos[i / 2]                        -= shift;
670
751
                }
671
752
            }
672
753
 
673
 
            fragmentPos+= fragmentLength;
 
754
            fragmentPos += fragmentLength;
674
755
 
675
756
            if (filterCode)
676
 
                filterCode[fragmentPos]= RET;
 
757
                filterCode[fragmentPos] = RET;
677
758
        }
678
 
        xpos+=xInc;
 
759
        xpos += xInc;
679
760
    }
680
761
    if (filterCode)
681
 
        filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
 
762
        filterPos[((i / 2) + 1) & (~1)] = xpos >> 16;  // needed to jump to the next part
682
763
 
683
764
    return fragmentPos + 1;
684
765
}
685
 
#endif /* HAVE_MMX2 */
 
766
#endif /* HAVE_MMXEXT_INLINE */
686
767
 
687
 
static void getSubSampleFactors(int *h, int *v, enum PixelFormat format)
 
768
static void getSubSampleFactors(int *h, int *v, enum AVPixelFormat format)
688
769
{
689
 
    *h = av_pix_fmt_descriptors[format].log2_chroma_w;
690
 
    *v = av_pix_fmt_descriptors[format].log2_chroma_h;
 
770
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
 
771
    *h = desc->log2_chroma_w;
 
772
    *v = desc->log2_chroma_h;
691
773
}
692
774
 
693
775
int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
694
776
                             int srcRange, const int table[4], int dstRange,
695
777
                             int brightness, int contrast, int saturation)
696
778
{
697
 
    memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
698
 
    memcpy(c->dstColorspaceTable,     table, sizeof(int)*4);
699
 
 
700
 
    c->brightness= brightness;
701
 
    c->contrast  = contrast;
702
 
    c->saturation= saturation;
703
 
    c->srcRange  = srcRange;
704
 
    c->dstRange  = dstRange;
705
 
    if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
706
 
 
707
 
    c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[c->dstFormat]);
708
 
    c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[c->srcFormat]);
709
 
 
710
 
    ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
711
 
    //FIXME factorize
 
779
    const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(c->dstFormat);
 
780
    const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(c->srcFormat);
 
781
    memcpy(c->srcColorspaceTable, inv_table, sizeof(int) * 4);
 
782
    memcpy(c->dstColorspaceTable, table, sizeof(int) * 4);
 
783
 
 
784
    c->brightness = brightness;
 
785
    c->contrast   = contrast;
 
786
    c->saturation = saturation;
 
787
    c->srcRange   = srcRange;
 
788
    c->dstRange   = dstRange;
 
789
    if (isYUV(c->dstFormat) || isGray(c->dstFormat))
 
790
        return -1;
 
791
 
 
792
    c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
 
793
    c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
 
794
 
 
795
    ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness,
 
796
                             contrast, saturation);
 
797
    // FIXME factorize
712
798
 
713
799
    if (HAVE_ALTIVEC && av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC)
714
 
        ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
 
800
        ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness,
 
801
                                       contrast, saturation);
715
802
    return 0;
716
803
}
717
804
 
719
806
                             int *srcRange, int **table, int *dstRange,
720
807
                             int *brightness, int *contrast, int *saturation)
721
808
{
722
 
    if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
 
809
    if (isYUV(c->dstFormat) || isGray(c->dstFormat))
 
810
        return -1;
723
811
 
724
 
    *inv_table = c->srcColorspaceTable;
725
 
    *table     = c->dstColorspaceTable;
726
 
    *srcRange  = c->srcRange;
727
 
    *dstRange  = c->dstRange;
728
 
    *brightness= c->brightness;
729
 
    *contrast  = c->contrast;
730
 
    *saturation= c->saturation;
 
812
    *inv_table  = c->srcColorspaceTable;
 
813
    *table      = c->dstColorspaceTable;
 
814
    *srcRange   = c->srcRange;
 
815
    *dstRange   = c->dstRange;
 
816
    *brightness = c->brightness;
 
817
    *contrast   = c->contrast;
 
818
    *saturation = c->saturation;
731
819
 
732
820
    return 0;
733
821
}
734
822
 
735
 
static int handle_jpeg(enum PixelFormat *format)
 
823
static int handle_jpeg(enum AVPixelFormat *format)
736
824
{
737
825
    switch (*format) {
738
 
    case PIX_FMT_YUVJ420P: *format = PIX_FMT_YUV420P; return 1;
739
 
    case PIX_FMT_YUVJ422P: *format = PIX_FMT_YUV422P; return 1;
740
 
    case PIX_FMT_YUVJ444P: *format = PIX_FMT_YUV444P; return 1;
741
 
    case PIX_FMT_YUVJ440P: *format = PIX_FMT_YUV440P; return 1;
742
 
    default:                                          return 0;
 
826
    case AV_PIX_FMT_YUVJ420P:
 
827
        *format = AV_PIX_FMT_YUV420P;
 
828
        return 1;
 
829
    case AV_PIX_FMT_YUVJ422P:
 
830
        *format = AV_PIX_FMT_YUV422P;
 
831
        return 1;
 
832
    case AV_PIX_FMT_YUVJ444P:
 
833
        *format = AV_PIX_FMT_YUV444P;
 
834
        return 1;
 
835
    case AV_PIX_FMT_YUVJ440P:
 
836
        *format = AV_PIX_FMT_YUV440P;
 
837
        return 1;
 
838
    default:
 
839
        return 0;
743
840
    }
744
841
}
745
842
 
746
843
SwsContext *sws_alloc_context(void)
747
844
{
748
 
    SwsContext *c= av_mallocz(sizeof(SwsContext));
 
845
    SwsContext *c = av_mallocz(sizeof(SwsContext));
749
846
 
750
 
    c->av_class = &sws_context_class;
751
 
    av_opt_set_defaults(c);
 
847
    if (c) {
 
848
        c->av_class = &sws_context_class;
 
849
        av_opt_set_defaults(c);
 
850
    }
752
851
 
753
852
    return c;
754
853
}
755
854
 
756
 
int sws_init_context(SwsContext *c, SwsFilter *srcFilter, SwsFilter *dstFilter)
 
855
av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
 
856
                             SwsFilter *dstFilter)
757
857
{
758
858
    int i;
759
859
    int usesVFilter, usesHFilter;
760
860
    int unscaled;
761
 
    SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
762
 
    int srcW= c->srcW;
763
 
    int srcH= c->srcH;
764
 
    int dstW= c->dstW;
765
 
    int dstH= c->dstH;
766
 
    int dst_stride = FFALIGN(dstW * sizeof(int16_t) + 16, 16), dst_stride_px = dst_stride >> 1;
 
861
    SwsFilter dummyFilter = { NULL, NULL, NULL, NULL };
 
862
    int srcW              = c->srcW;
 
863
    int srcH              = c->srcH;
 
864
    int dstW              = c->dstW;
 
865
    int dstH              = c->dstH;
 
866
    int dst_stride        = FFALIGN(dstW * sizeof(int16_t) + 16, 16);
 
867
    int dst_stride_px     = dst_stride >> 1;
767
868
    int flags, cpu_flags;
768
 
    enum PixelFormat srcFormat= c->srcFormat;
769
 
    enum PixelFormat dstFormat= c->dstFormat;
 
869
    enum AVPixelFormat srcFormat = c->srcFormat;
 
870
    enum AVPixelFormat dstFormat = c->dstFormat;
 
871
    const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(srcFormat);
 
872
    const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(dstFormat);
770
873
 
771
874
    cpu_flags = av_get_cpu_flags();
772
875
    flags     = c->flags;
773
876
    emms_c();
774
 
    if (!rgb15to16) sws_rgb2rgb_init();
 
877
    if (!rgb15to16)
 
878
        sws_rgb2rgb_init();
775
879
 
776
880
    unscaled = (srcW == dstW && srcH == dstH);
777
881
 
778
882
    if (!sws_isSupportedInput(srcFormat)) {
779
 
        av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n", sws_format_name(srcFormat));
 
883
        av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n",
 
884
               sws_format_name(srcFormat));
780
885
        return AVERROR(EINVAL);
781
886
    }
782
887
    if (!sws_isSupportedOutput(dstFormat)) {
783
 
        av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n", sws_format_name(dstFormat));
 
888
        av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n",
 
889
               sws_format_name(dstFormat));
784
890
        return AVERROR(EINVAL);
785
891
    }
786
892
 
787
 
    i= flags & ( SWS_POINT
788
 
                |SWS_AREA
789
 
                |SWS_BILINEAR
790
 
                |SWS_FAST_BILINEAR
791
 
                |SWS_BICUBIC
792
 
                |SWS_X
793
 
                |SWS_GAUSS
794
 
                |SWS_LANCZOS
795
 
                |SWS_SINC
796
 
                |SWS_SPLINE
797
 
                |SWS_BICUBLIN);
798
 
    if(!i || (i & (i-1))) {
799
 
        av_log(c, AV_LOG_ERROR, "Exactly one scaler algorithm must be chosen\n");
 
893
    i = flags & (SWS_POINT         |
 
894
                 SWS_AREA          |
 
895
                 SWS_BILINEAR      |
 
896
                 SWS_FAST_BILINEAR |
 
897
                 SWS_BICUBIC       |
 
898
                 SWS_X             |
 
899
                 SWS_GAUSS         |
 
900
                 SWS_LANCZOS       |
 
901
                 SWS_SINC          |
 
902
                 SWS_SPLINE        |
 
903
                 SWS_BICUBLIN);
 
904
    if (!i || (i & (i - 1))) {
 
905
        av_log(c, AV_LOG_ERROR,
 
906
               "Exactly one scaler algorithm must be chosen\n");
800
907
        return AVERROR(EINVAL);
801
908
    }
802
909
    /* sanity check */
803
 
    if (srcW<4 || srcH<1 || dstW<8 || dstH<1) { //FIXME check if these are enough and try to lowwer them after fixing the relevant parts of the code
 
910
    if (srcW < 4 || srcH < 1 || dstW < 8 || dstH < 1) {
 
911
        /* FIXME check if these are enough and try to lower them after
 
912
         * fixing the relevant parts of the code */
804
913
        av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
805
914
               srcW, srcH, dstW, dstH);
806
915
        return AVERROR(EINVAL);
807
916
    }
808
917
 
809
 
    if (!dstFilter) dstFilter= &dummyFilter;
810
 
    if (!srcFilter) srcFilter= &dummyFilter;
811
 
 
812
 
    c->lumXInc= (((int64_t)srcW<<16) + (dstW>>1))/dstW;
813
 
    c->lumYInc= (((int64_t)srcH<<16) + (dstH>>1))/dstH;
814
 
    c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[dstFormat]);
815
 
    c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[srcFormat]);
816
 
    c->vRounder= 4* 0x0001000100010001ULL;
817
 
 
818
 
    usesVFilter = (srcFilter->lumV && srcFilter->lumV->length>1) ||
819
 
                  (srcFilter->chrV && srcFilter->chrV->length>1) ||
820
 
                  (dstFilter->lumV && dstFilter->lumV->length>1) ||
821
 
                  (dstFilter->chrV && dstFilter->chrV->length>1);
822
 
    usesHFilter = (srcFilter->lumH && srcFilter->lumH->length>1) ||
823
 
                  (srcFilter->chrH && srcFilter->chrH->length>1) ||
824
 
                  (dstFilter->lumH && dstFilter->lumH->length>1) ||
825
 
                  (dstFilter->chrH && dstFilter->chrH->length>1);
 
918
    if (!dstFilter)
 
919
        dstFilter = &dummyFilter;
 
920
    if (!srcFilter)
 
921
        srcFilter = &dummyFilter;
 
922
 
 
923
    c->lumXInc      = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW;
 
924
    c->lumYInc      = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH;
 
925
    c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
 
926
    c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
 
927
    c->vRounder     = 4 * 0x0001000100010001ULL;
 
928
 
 
929
    usesVFilter = (srcFilter->lumV && srcFilter->lumV->length > 1) ||
 
930
                  (srcFilter->chrV && srcFilter->chrV->length > 1) ||
 
931
                  (dstFilter->lumV && dstFilter->lumV->length > 1) ||
 
932
                  (dstFilter->chrV && dstFilter->chrV->length > 1);
 
933
    usesHFilter = (srcFilter->lumH && srcFilter->lumH->length > 1) ||
 
934
                  (srcFilter->chrH && srcFilter->chrH->length > 1) ||
 
935
                  (dstFilter->lumH && dstFilter->lumH->length > 1) ||
 
936
                  (dstFilter->chrH && dstFilter->chrH->length > 1);
826
937
 
827
938
    getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
828
939
    getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
829
940
 
830
 
    // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
 
941
    /* reuse chroma for 2 pixels RGB/BGR unless user wants full
 
942
     * chroma interpolation */
831
943
    if (flags & SWS_FULL_CHR_H_INT &&
832
 
        isAnyRGB(dstFormat)       &&
833
 
        dstFormat != PIX_FMT_RGBA &&
834
 
        dstFormat != PIX_FMT_ARGB &&
835
 
        dstFormat != PIX_FMT_BGRA &&
836
 
        dstFormat != PIX_FMT_ABGR &&
837
 
        dstFormat != PIX_FMT_RGB24 &&
838
 
        dstFormat != PIX_FMT_BGR24) {
 
944
        isAnyRGB(dstFormat)        &&
 
945
        dstFormat != AV_PIX_FMT_RGBA  &&
 
946
        dstFormat != AV_PIX_FMT_ARGB  &&
 
947
        dstFormat != AV_PIX_FMT_BGRA  &&
 
948
        dstFormat != AV_PIX_FMT_ABGR  &&
 
949
        dstFormat != AV_PIX_FMT_RGB24 &&
 
950
        dstFormat != AV_PIX_FMT_BGR24) {
839
951
        av_log(c, AV_LOG_ERROR,
840
952
               "full chroma interpolation for destination format '%s' not yet implemented\n",
841
953
               sws_format_name(dstFormat));
842
 
        flags &= ~SWS_FULL_CHR_H_INT;
 
954
        flags   &= ~SWS_FULL_CHR_H_INT;
843
955
        c->flags = flags;
844
956
    }
845
 
    if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
 
957
    if (isAnyRGB(dstFormat) && !(flags & SWS_FULL_CHR_H_INT))
 
958
        c->chrDstHSubSample = 1;
846
959
 
847
960
    // drop some chroma lines if the user wants it
848
 
    c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
849
 
    c->chrSrcVSubSample+= c->vChrDrop;
 
961
    c->vChrDrop          = (flags & SWS_SRC_V_CHR_DROP_MASK) >>
 
962
                           SWS_SRC_V_CHR_DROP_SHIFT;
 
963
    c->chrSrcVSubSample += c->vChrDrop;
850
964
 
851
 
    // drop every other pixel for chroma calculation unless user wants full chroma
852
 
    if (isAnyRGB(srcFormat) && !(flags&SWS_FULL_CHR_H_INP)
853
 
      && srcFormat!=PIX_FMT_RGB8      && srcFormat!=PIX_FMT_BGR8
854
 
      && srcFormat!=PIX_FMT_RGB4      && srcFormat!=PIX_FMT_BGR4
855
 
      && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
856
 
      && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&SWS_FAST_BILINEAR)))
857
 
        c->chrSrcHSubSample=1;
 
965
    /* drop every other pixel for chroma calculation unless user
 
966
     * wants full chroma */
 
967
    if (isAnyRGB(srcFormat) && !(flags & SWS_FULL_CHR_H_INP)   &&
 
968
        srcFormat != AV_PIX_FMT_RGB8 && srcFormat != AV_PIX_FMT_BGR8 &&
 
969
        srcFormat != AV_PIX_FMT_RGB4 && srcFormat != AV_PIX_FMT_BGR4 &&
 
970
        srcFormat != AV_PIX_FMT_RGB4_BYTE && srcFormat != AV_PIX_FMT_BGR4_BYTE &&
 
971
        ((dstW >> c->chrDstHSubSample) <= (srcW >> 1) ||
 
972
         (flags & SWS_FAST_BILINEAR)))
 
973
        c->chrSrcHSubSample = 1;
858
974
 
859
975
    // Note the -((-x)>>y) is so that we always round toward +inf.
860
 
    c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
861
 
    c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
862
 
    c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
863
 
    c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
 
976
    c->chrSrcW = -((-srcW) >> c->chrSrcHSubSample);
 
977
    c->chrSrcH = -((-srcH) >> c->chrSrcVSubSample);
 
978
    c->chrDstW = -((-dstW) >> c->chrDstHSubSample);
 
979
    c->chrDstH = -((-dstH) >> c->chrDstVSubSample);
864
980
 
865
981
    /* unscaled special cases */
866
 
    if (unscaled && !usesHFilter && !usesVFilter && (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) {
 
982
    if (unscaled && !usesHFilter && !usesVFilter &&
 
983
        (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) {
867
984
        ff_get_unscaled_swscale(c);
868
985
 
869
986
        if (c->swScale) {
870
 
            if (flags&SWS_PRINT_INFO)
871
 
                av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
 
987
            if (flags & SWS_PRINT_INFO)
 
988
                av_log(c, AV_LOG_INFO,
 
989
                       "using unscaled %s -> %s special converter\n",
872
990
                       sws_format_name(srcFormat), sws_format_name(dstFormat));
873
991
            return 0;
874
992
        }
875
993
    }
876
994
 
877
 
    c->srcBpc = 1 + av_pix_fmt_descriptors[srcFormat].comp[0].depth_minus1;
 
995
    c->srcBpc = 1 + desc_src->comp[0].depth_minus1;
878
996
    if (c->srcBpc < 8)
879
997
        c->srcBpc = 8;
880
 
    c->dstBpc = 1 + av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1;
 
998
    c->dstBpc = 1 + desc_dst->comp[0].depth_minus1;
881
999
    if (c->dstBpc < 8)
882
1000
        c->dstBpc = 8;
883
1001
    if (c->dstBpc == 16)
885
1003
    FF_ALLOC_OR_GOTO(c, c->formatConvBuffer,
886
1004
                     (FFALIGN(srcW, 16) * 2 * FFALIGN(c->srcBpc, 8) >> 3) + 16,
887
1005
                     fail);
888
 
    if (HAVE_MMX2 && cpu_flags & AV_CPU_FLAG_MMX2 && c->srcBpc == 8 && c->dstBpc <= 10) {
889
 
        c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
890
 
        if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR)) {
891
 
            if (flags&SWS_PRINT_INFO)
892
 
                av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
893
 
        }
894
 
        if (usesHFilter) c->canMMX2BeUsed=0;
895
 
    }
896
 
    else
897
 
        c->canMMX2BeUsed=0;
898
 
 
899
 
    c->chrXInc= (((int64_t)c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
900
 
    c->chrYInc= (((int64_t)c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
901
 
 
902
 
    // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
903
 
    // but only for the FAST_BILINEAR mode otherwise do correct scaling
904
 
    // n-2 is the last chrominance sample available
905
 
    // this is not perfect, but no one should notice the difference, the more correct variant
906
 
    // would be like the vertical one, but that would require some special code for the
907
 
    // first and last pixel
908
 
    if (flags&SWS_FAST_BILINEAR) {
909
 
        if (c->canMMX2BeUsed) {
910
 
            c->lumXInc+= 20;
911
 
            c->chrXInc+= 20;
912
 
        }
913
 
        //we don't use the x86 asm scaler if MMX is available
914
 
        else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
915
 
            c->lumXInc = ((int64_t)(srcW-2)<<16)/(dstW-2) - 20;
916
 
            c->chrXInc = ((int64_t)(c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
917
 
        }
918
 
    }
 
1006
    if (INLINE_MMXEXT(cpu_flags) && c->srcBpc == 8 && c->dstBpc <= 10) {
 
1007
        c->canMMXEXTBeUsed = (dstW >= srcW && (dstW & 31) == 0 &&
 
1008
                              (srcW & 15) == 0) ? 1 : 0;
 
1009
        if (!c->canMMXEXTBeUsed && dstW >= srcW && (srcW & 15) == 0
 
1010
            && (flags & SWS_FAST_BILINEAR)) {
 
1011
            if (flags & SWS_PRINT_INFO)
 
1012
                av_log(c, AV_LOG_INFO,
 
1013
                       "output width is not a multiple of 32 -> no MMXEXT scaler\n");
 
1014
        }
 
1015
        if (usesHFilter)
 
1016
            c->canMMXEXTBeUsed = 0;
 
1017
    } else
 
1018
        c->canMMXEXTBeUsed = 0;
 
1019
 
 
1020
    c->chrXInc = (((int64_t)c->chrSrcW << 16) + (c->chrDstW >> 1)) / c->chrDstW;
 
1021
    c->chrYInc = (((int64_t)c->chrSrcH << 16) + (c->chrDstH >> 1)) / c->chrDstH;
 
1022
 
 
1023
    /* Match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src
 
1024
     * to pixel n-2 of dst, but only for the FAST_BILINEAR mode otherwise do
 
1025
     * correct scaling.
 
1026
     * n-2 is the last chrominance sample available.
 
1027
     * This is not perfect, but no one should notice the difference, the more
 
1028
     * correct variant would be like the vertical one, but that would require
 
1029
     * some special code for the first and last pixel */
 
1030
    if (flags & SWS_FAST_BILINEAR) {
 
1031
        if (c->canMMXEXTBeUsed) {
 
1032
            c->lumXInc += 20;
 
1033
            c->chrXInc += 20;
 
1034
        }
 
1035
        // we don't use the x86 asm scaler if MMX is available
 
1036
        else if (INLINE_MMX(cpu_flags)) {
 
1037
            c->lumXInc = ((int64_t)(srcW       - 2) << 16) / (dstW       - 2) - 20;
 
1038
            c->chrXInc = ((int64_t)(c->chrSrcW - 2) << 16) / (c->chrDstW - 2) - 20;
 
1039
        }
 
1040
    }
 
1041
 
 
1042
#define USE_MMAP (HAVE_MMAP && HAVE_MPROTECT && defined MAP_ANONYMOUS)
919
1043
 
920
1044
    /* precalculate horizontal scaler filter coefficients */
921
1045
    {
922
 
#if HAVE_MMX2
 
1046
#if HAVE_MMXEXT_INLINE
923
1047
// can't downscale !!!
924
 
        if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR)) {
925
 
            c->lumMmx2FilterCodeSize = initMMX2HScaler(      dstW, c->lumXInc, NULL, NULL, NULL, 8);
926
 
            c->chrMmx2FilterCodeSize = initMMX2HScaler(c->chrDstW, c->chrXInc, NULL, NULL, NULL, 4);
 
1048
        if (c->canMMXEXTBeUsed && (flags & SWS_FAST_BILINEAR)) {
 
1049
            c->lumMmxextFilterCodeSize = init_hscaler_mmxext(dstW, c->lumXInc, NULL,
 
1050
                                                             NULL, NULL, 8);
 
1051
            c->chrMmxextFilterCodeSize = init_hscaler_mmxext(c->chrDstW, c->chrXInc,
 
1052
                                                             NULL, NULL, NULL, 4);
927
1053
 
928
 
#ifdef MAP_ANONYMOUS
929
 
            c->lumMmx2FilterCode = mmap(NULL, c->lumMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
930
 
            c->chrMmx2FilterCode = mmap(NULL, c->chrMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
 
1054
#if USE_MMAP
 
1055
            c->lumMmxextFilterCode = mmap(NULL, c->lumMmxextFilterCodeSize,
 
1056
                                          PROT_READ | PROT_WRITE,
 
1057
                                          MAP_PRIVATE | MAP_ANONYMOUS,
 
1058
                                          -1, 0);
 
1059
            c->chrMmxextFilterCode = mmap(NULL, c->chrMmxextFilterCodeSize,
 
1060
                                          PROT_READ | PROT_WRITE,
 
1061
                                          MAP_PRIVATE | MAP_ANONYMOUS,
 
1062
                                          -1, 0);
931
1063
#elif HAVE_VIRTUALALLOC
932
 
            c->lumMmx2FilterCode = VirtualAlloc(NULL, c->lumMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
933
 
            c->chrMmx2FilterCode = VirtualAlloc(NULL, c->chrMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
 
1064
            c->lumMmxextFilterCode = VirtualAlloc(NULL,
 
1065
                                                  c->lumMmxextFilterCodeSize,
 
1066
                                                  MEM_COMMIT,
 
1067
                                                  PAGE_EXECUTE_READWRITE);
 
1068
            c->chrMmxextFilterCode = VirtualAlloc(NULL,
 
1069
                                                  c->chrMmxextFilterCodeSize,
 
1070
                                                  MEM_COMMIT,
 
1071
                                                  PAGE_EXECUTE_READWRITE);
934
1072
#else
935
 
            c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize);
936
 
            c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize);
 
1073
            c->lumMmxextFilterCode = av_malloc(c->lumMmxextFilterCodeSize);
 
1074
            c->chrMmxextFilterCode = av_malloc(c->chrMmxextFilterCodeSize);
937
1075
#endif
938
1076
 
939
 
            if (!c->lumMmx2FilterCode || !c->chrMmx2FilterCode)
 
1077
            if (!c->lumMmxextFilterCode || !c->chrMmxextFilterCode)
940
1078
                return AVERROR(ENOMEM);
941
 
            FF_ALLOCZ_OR_GOTO(c, c->hLumFilter   , (dstW        /8+8)*sizeof(int16_t), fail);
942
 
            FF_ALLOCZ_OR_GOTO(c, c->hChrFilter   , (c->chrDstW  /4+8)*sizeof(int16_t), fail);
943
 
            FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW      /2/8+8)*sizeof(int32_t), fail);
944
 
            FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW/2/4+8)*sizeof(int32_t), fail);
945
 
 
946
 
            initMMX2HScaler(      dstW, c->lumXInc, c->lumMmx2FilterCode, c->hLumFilter, c->hLumFilterPos, 8);
947
 
            initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->hChrFilter, c->hChrFilterPos, 4);
948
 
 
949
 
#ifdef MAP_ANONYMOUS
950
 
            mprotect(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
951
 
            mprotect(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
 
1079
            FF_ALLOCZ_OR_GOTO(c, c->hLumFilter,    (dstW           / 8 + 8) * sizeof(int16_t), fail);
 
1080
            FF_ALLOCZ_OR_GOTO(c, c->hChrFilter,    (c->chrDstW     / 4 + 8) * sizeof(int16_t), fail);
 
1081
            FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW       / 2 / 8 + 8) * sizeof(int32_t), fail);
 
1082
            FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW / 2 / 4 + 8) * sizeof(int32_t), fail);
 
1083
 
 
1084
            init_hscaler_mmxext(dstW, c->lumXInc, c->lumMmxextFilterCode,
 
1085
                                c->hLumFilter, c->hLumFilterPos, 8);
 
1086
            init_hscaler_mmxext(c->chrDstW, c->chrXInc, c->chrMmxextFilterCode,
 
1087
                                c->hChrFilter, c->hChrFilterPos, 4);
 
1088
 
 
1089
#if USE_MMAP
 
1090
            mprotect(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize, PROT_EXEC | PROT_READ);
 
1091
            mprotect(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize, PROT_EXEC | PROT_READ);
952
1092
#endif
953
1093
        } else
954
 
#endif /* HAVE_MMX2 */
 
1094
#endif /* HAVE_MMXEXT_INLINE */
955
1095
        {
956
 
            const int filterAlign=
957
 
                (HAVE_MMX     && cpu_flags & AV_CPU_FLAG_MMX) ? 4 :
 
1096
            const int filterAlign =
 
1097
                (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? 4 :
958
1098
                (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
959
1099
                1;
960
1100
 
961
 
            if (initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
962
 
                           srcW      ,       dstW, filterAlign, 1<<14,
963
 
                           (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags, cpu_flags,
964
 
                           srcFilter->lumH, dstFilter->lumH, c->param, 1) < 0)
 
1101
            if (initFilter(&c->hLumFilter, &c->hLumFilterPos,
 
1102
                           &c->hLumFilterSize, c->lumXInc,
 
1103
                           srcW, dstW, filterAlign, 1 << 14,
 
1104
                           (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
 
1105
                           cpu_flags, srcFilter->lumH, dstFilter->lumH,
 
1106
                           c->param, 1) < 0)
965
1107
                goto fail;
966
 
            if (initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
967
 
                           c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
968
 
                           (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags, cpu_flags,
969
 
                           srcFilter->chrH, dstFilter->chrH, c->param, 1) < 0)
 
1108
            if (initFilter(&c->hChrFilter, &c->hChrFilterPos,
 
1109
                           &c->hChrFilterSize, c->chrXInc,
 
1110
                           c->chrSrcW, c->chrDstW, filterAlign, 1 << 14,
 
1111
                           (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
 
1112
                           cpu_flags, srcFilter->chrH, dstFilter->chrH,
 
1113
                           c->param, 1) < 0)
970
1114
                goto fail;
971
1115
        }
972
1116
    } // initialize horizontal stuff
973
1117
 
974
1118
    /* precalculate vertical scaler filter coefficients */
975
1119
    {
976
 
        const int filterAlign=
977
 
            (HAVE_MMX     && cpu_flags & AV_CPU_FLAG_MMX) ? 2 :
 
1120
        const int filterAlign =
 
1121
            (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) ? 2 :
978
1122
            (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC) ? 8 :
979
1123
            1;
980
1124
 
981
 
        if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
982
 
                       srcH      ,        dstH, filterAlign, (1<<12),
983
 
                       (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC)  : flags, cpu_flags,
984
 
                       srcFilter->lumV, dstFilter->lumV, c->param, 0) < 0)
 
1125
        if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize,
 
1126
                       c->lumYInc, srcH, dstH, filterAlign, (1 << 12),
 
1127
                       (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
 
1128
                       cpu_flags, srcFilter->lumV, dstFilter->lumV,
 
1129
                       c->param, 0) < 0)
985
1130
            goto fail;
986
 
        if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
987
 
                       c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
988
 
                       (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags, cpu_flags,
989
 
                       srcFilter->chrV, dstFilter->chrV, c->param, 0) < 0)
 
1131
        if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize,
 
1132
                       c->chrYInc, c->chrSrcH, c->chrDstH,
 
1133
                       filterAlign, (1 << 12),
 
1134
                       (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
 
1135
                       cpu_flags, srcFilter->chrV, dstFilter->chrV,
 
1136
                       c->param, 0) < 0)
990
1137
            goto fail;
991
1138
 
992
1139
#if HAVE_ALTIVEC
993
 
        FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof (vector signed short)*c->vLumFilterSize*c->dstH, fail);
994
 
        FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH, fail);
 
1140
        FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof(vector signed short) * c->vLumFilterSize * c->dstH,    fail);
 
1141
        FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof(vector signed short) * c->vChrFilterSize * c->chrDstH, fail);
995
1142
 
996
 
        for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
 
1143
        for (i = 0; i < c->vLumFilterSize * c->dstH; i++) {
997
1144
            int j;
998
1145
            short *p = (short *)&c->vYCoeffsBank[i];
999
 
            for (j=0;j<8;j++)
 
1146
            for (j = 0; j < 8; j++)
1000
1147
                p[j] = c->vLumFilter[i];
1001
1148
        }
1002
1149
 
1003
 
        for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
 
1150
        for (i = 0; i < c->vChrFilterSize * c->chrDstH; i++) {
1004
1151
            int j;
1005
1152
            short *p = (short *)&c->vCCoeffsBank[i];
1006
 
            for (j=0;j<8;j++)
 
1153
            for (j = 0; j < 8; j++)
1007
1154
                p[j] = c->vChrFilter[i];
1008
1155
        }
1009
1156
#endif
1010
1157
    }
1011
1158
 
1012
1159
    // calculate buffer sizes so that they won't run out while handling these damn slices
1013
 
    c->vLumBufSize= c->vLumFilterSize;
1014
 
    c->vChrBufSize= c->vChrFilterSize;
1015
 
    for (i=0; i<dstH; i++) {
1016
 
        int chrI = (int64_t) i * c->chrDstH / dstH;
1017
 
        int nextSlice= FFMAX(c->vLumFilterPos[i   ] + c->vLumFilterSize - 1,
1018
 
                           ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
 
1160
    c->vLumBufSize = c->vLumFilterSize;
 
1161
    c->vChrBufSize = c->vChrFilterSize;
 
1162
    for (i = 0; i < dstH; i++) {
 
1163
        int chrI      = (int64_t)i * c->chrDstH / dstH;
 
1164
        int nextSlice = FFMAX(c->vLumFilterPos[i] + c->vLumFilterSize - 1,
 
1165
                              ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)
 
1166
                               << c->chrSrcVSubSample));
1019
1167
 
1020
 
        nextSlice>>= c->chrSrcVSubSample;
1021
 
        nextSlice<<= c->chrSrcVSubSample;
1022
 
        if (c->vLumFilterPos[i   ] + c->vLumBufSize < nextSlice)
1023
 
            c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
1024
 
        if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
1025
 
            c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
 
1168
        nextSlice >>= c->chrSrcVSubSample;
 
1169
        nextSlice <<= c->chrSrcVSubSample;
 
1170
        if (c->vLumFilterPos[i] + c->vLumBufSize < nextSlice)
 
1171
            c->vLumBufSize = nextSlice - c->vLumFilterPos[i];
 
1172
        if (c->vChrFilterPos[chrI] + c->vChrBufSize <
 
1173
            (nextSlice >> c->chrSrcVSubSample))
 
1174
            c->vChrBufSize = (nextSlice >> c->chrSrcVSubSample) -
 
1175
                             c->vChrFilterPos[chrI];
1026
1176
    }
1027
1177
 
1028
 
    // allocate pixbufs (we use dynamic allocation because otherwise we would need to
1029
 
    // allocate several megabytes to handle all possible cases)
1030
 
    FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize*3*sizeof(int16_t*), fail);
1031
 
    FF_ALLOC_OR_GOTO(c, c->chrUPixBuf, c->vChrBufSize*3*sizeof(int16_t*), fail);
1032
 
    FF_ALLOC_OR_GOTO(c, c->chrVPixBuf, c->vChrBufSize*3*sizeof(int16_t*), fail);
 
1178
    /* Allocate pixbufs (we use dynamic allocation because otherwise we would
 
1179
     * need to allocate several megabytes to handle all possible cases) */
 
1180
    FF_ALLOC_OR_GOTO(c, c->lumPixBuf,  c->vLumBufSize * 3 * sizeof(int16_t *), fail);
 
1181
    FF_ALLOC_OR_GOTO(c, c->chrUPixBuf, c->vChrBufSize * 3 * sizeof(int16_t *), fail);
 
1182
    FF_ALLOC_OR_GOTO(c, c->chrVPixBuf, c->vChrBufSize * 3 * sizeof(int16_t *), fail);
1033
1183
    if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
1034
 
        FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize*3*sizeof(int16_t*), fail);
1035
 
    //Note we need at least one pixel more at the end because of the MMX code (just in case someone wanna replace the 4000/8000)
 
1184
        FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize * 3 * sizeof(int16_t *), fail);
 
1185
    /* Note we need at least one pixel more at the end because of the MMX code
 
1186
     * (just in case someone wants to replace the 4000/8000). */
1036
1187
    /* align at 16 bytes for AltiVec */
1037
 
    for (i=0; i<c->vLumBufSize; i++) {
1038
 
        FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i+c->vLumBufSize], dst_stride+16, fail);
1039
 
        c->lumPixBuf[i] = c->lumPixBuf[i+c->vLumBufSize];
 
1188
    for (i = 0; i < c->vLumBufSize; i++) {
 
1189
        FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i + c->vLumBufSize],
 
1190
                          dst_stride + 16, fail);
 
1191
        c->lumPixBuf[i] = c->lumPixBuf[i + c->vLumBufSize];
1040
1192
    }
1041
1193
    // 64 / (c->dstBpc & ~7) is the same as 16 / sizeof(scaling_intermediate)
1042
 
    c->uv_off_px   = dst_stride_px + 64 / (c->dstBpc &~ 7);
 
1194
    c->uv_off_px   = dst_stride_px + 64 / (c->dstBpc & ~7);
1043
1195
    c->uv_off_byte = dst_stride + 16;
1044
 
    for (i=0; i<c->vChrBufSize; i++) {
1045
 
        FF_ALLOC_OR_GOTO(c, c->chrUPixBuf[i+c->vChrBufSize], dst_stride*2+32, fail);
1046
 
        c->chrUPixBuf[i] = c->chrUPixBuf[i+c->vChrBufSize];
1047
 
        c->chrVPixBuf[i] = c->chrVPixBuf[i+c->vChrBufSize] = c->chrUPixBuf[i] + (dst_stride >> 1) + 8;
 
1196
    for (i = 0; i < c->vChrBufSize; i++) {
 
1197
        FF_ALLOC_OR_GOTO(c, c->chrUPixBuf[i + c->vChrBufSize],
 
1198
                         dst_stride * 2 + 32, fail);
 
1199
        c->chrUPixBuf[i] = c->chrUPixBuf[i + c->vChrBufSize];
 
1200
        c->chrVPixBuf[i] = c->chrVPixBuf[i + c->vChrBufSize]
 
1201
                         = c->chrUPixBuf[i] + (dst_stride >> 1) + 8;
1048
1202
    }
1049
1203
    if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1050
 
        for (i=0; i<c->vLumBufSize; i++) {
1051
 
            FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i+c->vLumBufSize], dst_stride+16, fail);
1052
 
            c->alpPixBuf[i] = c->alpPixBuf[i+c->vLumBufSize];
 
1204
        for (i = 0; i < c->vLumBufSize; i++) {
 
1205
            FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i + c->vLumBufSize],
 
1206
                              dst_stride + 16, fail);
 
1207
            c->alpPixBuf[i] = c->alpPixBuf[i + c->vLumBufSize];
1053
1208
        }
1054
1209
 
1055
 
    //try to avoid drawing green stuff between the right end and the stride end
1056
 
    for (i=0; i<c->vChrBufSize; i++)
1057
 
        memset(c->chrUPixBuf[i], 64, dst_stride*2+1);
 
1210
    // try to avoid drawing green stuff between the right end and the stride end
 
1211
    for (i = 0; i < c->vChrBufSize; i++)
 
1212
        memset(c->chrUPixBuf[i], 64, dst_stride * 2 + 1);
1058
1213
 
1059
1214
    assert(c->chrDstH <= dstH);
1060
1215
 
1061
 
    if (flags&SWS_PRINT_INFO) {
1062
 
        if      (flags&SWS_FAST_BILINEAR) av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
1063
 
        else if (flags&SWS_BILINEAR)      av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
1064
 
        else if (flags&SWS_BICUBIC)       av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
1065
 
        else if (flags&SWS_X)             av_log(c, AV_LOG_INFO, "Experimental scaler, ");
1066
 
        else if (flags&SWS_POINT)         av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
1067
 
        else if (flags&SWS_AREA)          av_log(c, AV_LOG_INFO, "Area Averaging scaler, ");
1068
 
        else if (flags&SWS_BICUBLIN)      av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
1069
 
        else if (flags&SWS_GAUSS)         av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
1070
 
        else if (flags&SWS_SINC)          av_log(c, AV_LOG_INFO, "Sinc scaler, ");
1071
 
        else if (flags&SWS_LANCZOS)       av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
1072
 
        else if (flags&SWS_SPLINE)        av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
1073
 
        else                              av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
 
1216
    if (flags & SWS_PRINT_INFO) {
 
1217
        if (flags & SWS_FAST_BILINEAR)
 
1218
            av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
 
1219
        else if (flags & SWS_BILINEAR)
 
1220
            av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
 
1221
        else if (flags & SWS_BICUBIC)
 
1222
            av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
 
1223
        else if (flags & SWS_X)
 
1224
            av_log(c, AV_LOG_INFO, "Experimental scaler, ");
 
1225
        else if (flags & SWS_POINT)
 
1226
            av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
 
1227
        else if (flags & SWS_AREA)
 
1228
            av_log(c, AV_LOG_INFO, "Area Averaging scaler, ");
 
1229
        else if (flags & SWS_BICUBLIN)
 
1230
            av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
 
1231
        else if (flags & SWS_GAUSS)
 
1232
            av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
 
1233
        else if (flags & SWS_SINC)
 
1234
            av_log(c, AV_LOG_INFO, "Sinc scaler, ");
 
1235
        else if (flags & SWS_LANCZOS)
 
1236
            av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
 
1237
        else if (flags & SWS_SPLINE)
 
1238
            av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
 
1239
        else
 
1240
            av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
1074
1241
 
1075
1242
        av_log(c, AV_LOG_INFO, "from %s to %s%s ",
1076
1243
               sws_format_name(srcFormat),
1077
1244
#ifdef DITHER1XBPP
1078
 
               dstFormat == PIX_FMT_BGR555 || dstFormat == PIX_FMT_BGR565 ||
1079
 
               dstFormat == PIX_FMT_RGB444BE || dstFormat == PIX_FMT_RGB444LE ||
1080
 
               dstFormat == PIX_FMT_BGR444BE || dstFormat == PIX_FMT_BGR444LE ? "dithered " : "",
 
1245
               dstFormat == AV_PIX_FMT_BGR555   || dstFormat == AV_PIX_FMT_BGR565   ||
 
1246
               dstFormat == AV_PIX_FMT_RGB444BE || dstFormat == AV_PIX_FMT_RGB444LE ||
 
1247
               dstFormat == AV_PIX_FMT_BGR444BE || dstFormat == AV_PIX_FMT_BGR444LE ?
 
1248
                                                             "dithered " : "",
1081
1249
#else
1082
1250
               "",
1083
1251
#endif
1084
1252
               sws_format_name(dstFormat));
1085
1253
 
1086
 
        if      (HAVE_MMX2     && cpu_flags & AV_CPU_FLAG_MMX2)    av_log(c, AV_LOG_INFO, "using MMX2\n");
1087
 
        else if (HAVE_AMD3DNOW && cpu_flags & AV_CPU_FLAG_3DNOW)   av_log(c, AV_LOG_INFO, "using 3DNOW\n");
1088
 
        else if (HAVE_MMX      && cpu_flags & AV_CPU_FLAG_MMX)     av_log(c, AV_LOG_INFO, "using MMX\n");
1089
 
        else if (HAVE_ALTIVEC  && cpu_flags & AV_CPU_FLAG_ALTIVEC) av_log(c, AV_LOG_INFO, "using AltiVec\n");
1090
 
        else                                   av_log(c, AV_LOG_INFO, "using C\n");
 
1254
        if (INLINE_MMXEXT(cpu_flags))
 
1255
            av_log(c, AV_LOG_INFO, "using MMXEXT\n");
 
1256
        else if (INLINE_AMD3DNOW(cpu_flags))
 
1257
            av_log(c, AV_LOG_INFO, "using 3DNOW\n");
 
1258
        else if (INLINE_MMX(cpu_flags))
 
1259
            av_log(c, AV_LOG_INFO, "using MMX\n");
 
1260
        else if (HAVE_ALTIVEC && cpu_flags & AV_CPU_FLAG_ALTIVEC)
 
1261
            av_log(c, AV_LOG_INFO, "using AltiVec\n");
 
1262
        else
 
1263
            av_log(c, AV_LOG_INFO, "using C\n");
1091
1264
 
1092
1265
        av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
1093
 
        av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
 
1266
        av_log(c, AV_LOG_DEBUG,
 
1267
               "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1094
1268
               c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
1095
 
        av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1096
 
               c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
 
1269
        av_log(c, AV_LOG_DEBUG,
 
1270
               "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
 
1271
               c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH,
 
1272
               c->chrXInc, c->chrYInc);
1097
1273
    }
1098
1274
 
1099
 
    c->swScale= ff_getSwsFunc(c);
 
1275
    c->swScale = ff_getSwsFunc(c);
1100
1276
    return 0;
1101
 
fail: //FIXME replace things by appropriate error codes
 
1277
fail: // FIXME replace things by appropriate error codes
1102
1278
    return -1;
1103
1279
}
1104
1280
 
1105
1281
#if FF_API_SWS_GETCONTEXT
1106
 
SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat,
1107
 
                           int dstW, int dstH, enum PixelFormat dstFormat, int flags,
1108
 
                           SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
 
1282
SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
 
1283
                           int dstW, int dstH, enum AVPixelFormat dstFormat,
 
1284
                           int flags, SwsFilter *srcFilter,
 
1285
                           SwsFilter *dstFilter, const double *param)
1109
1286
{
1110
1287
    SwsContext *c;
1111
1288
 
1112
 
    if(!(c=sws_alloc_context()))
 
1289
    if (!(c = sws_alloc_context()))
1113
1290
        return NULL;
1114
1291
 
1115
 
    c->flags= flags;
1116
 
    c->srcW= srcW;
1117
 
    c->srcH= srcH;
1118
 
    c->dstW= dstW;
1119
 
    c->dstH= dstH;
1120
 
    c->srcRange = handle_jpeg(&srcFormat);
1121
 
    c->dstRange = handle_jpeg(&dstFormat);
1122
 
    c->srcFormat= srcFormat;
1123
 
    c->dstFormat= dstFormat;
 
1292
    c->flags     = flags;
 
1293
    c->srcW      = srcW;
 
1294
    c->srcH      = srcH;
 
1295
    c->dstW      = dstW;
 
1296
    c->dstH      = dstH;
 
1297
    c->srcRange  = handle_jpeg(&srcFormat);
 
1298
    c->dstRange  = handle_jpeg(&dstFormat);
 
1299
    c->srcFormat = srcFormat;
 
1300
    c->dstFormat = dstFormat;
1124
1301
 
1125
1302
    if (param) {
1126
1303
        c->param[0] = param[0];
1127
1304
        c->param[1] = param[1];
1128
1305
    }
1129
 
    sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, c->dstRange, 0, 1<<16, 1<<16);
 
1306
    sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange,
 
1307
                             ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/,
 
1308
                             c->dstRange, 0, 1 << 16, 1 << 16);
1130
1309
 
1131
 
    if(sws_init_context(c, srcFilter, dstFilter) < 0){
 
1310
    if (sws_init_context(c, srcFilter, dstFilter) < 0) {
1132
1311
        sws_freeContext(c);
1133
1312
        return NULL;
1134
1313
    }
1142
1321
                                float chromaHShift, float chromaVShift,
1143
1322
                                int verbose)
1144
1323
{
1145
 
    SwsFilter *filter= av_malloc(sizeof(SwsFilter));
 
1324
    SwsFilter *filter = av_malloc(sizeof(SwsFilter));
1146
1325
    if (!filter)
1147
1326
        return NULL;
1148
1327
 
1149
 
    if (lumaGBlur!=0.0) {
1150
 
        filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
1151
 
        filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
1152
 
    } else {
1153
 
        filter->lumH= sws_getIdentityVec();
1154
 
        filter->lumV= sws_getIdentityVec();
1155
 
    }
1156
 
 
1157
 
    if (chromaGBlur!=0.0) {
1158
 
        filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
1159
 
        filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
1160
 
    } else {
1161
 
        filter->chrH= sws_getIdentityVec();
1162
 
        filter->chrV= sws_getIdentityVec();
1163
 
    }
1164
 
 
1165
 
    if (chromaSharpen!=0.0) {
1166
 
        SwsVector *id= sws_getIdentityVec();
 
1328
    if (lumaGBlur != 0.0) {
 
1329
        filter->lumH = sws_getGaussianVec(lumaGBlur, 3.0);
 
1330
        filter->lumV = sws_getGaussianVec(lumaGBlur, 3.0);
 
1331
    } else {
 
1332
        filter->lumH = sws_getIdentityVec();
 
1333
        filter->lumV = sws_getIdentityVec();
 
1334
    }
 
1335
 
 
1336
    if (chromaGBlur != 0.0) {
 
1337
        filter->chrH = sws_getGaussianVec(chromaGBlur, 3.0);
 
1338
        filter->chrV = sws_getGaussianVec(chromaGBlur, 3.0);
 
1339
    } else {
 
1340
        filter->chrH = sws_getIdentityVec();
 
1341
        filter->chrV = sws_getIdentityVec();
 
1342
    }
 
1343
 
 
1344
    if (chromaSharpen != 0.0) {
 
1345
        SwsVector *id = sws_getIdentityVec();
1167
1346
        sws_scaleVec(filter->chrH, -chromaSharpen);
1168
1347
        sws_scaleVec(filter->chrV, -chromaSharpen);
1169
1348
        sws_addVec(filter->chrH, id);
1171
1350
        sws_freeVec(id);
1172
1351
    }
1173
1352
 
1174
 
    if (lumaSharpen!=0.0) {
1175
 
        SwsVector *id= sws_getIdentityVec();
 
1353
    if (lumaSharpen != 0.0) {
 
1354
        SwsVector *id = sws_getIdentityVec();
1176
1355
        sws_scaleVec(filter->lumH, -lumaSharpen);
1177
1356
        sws_scaleVec(filter->lumV, -lumaSharpen);
1178
1357
        sws_addVec(filter->lumH, id);
1181
1360
    }
1182
1361
 
1183
1362
    if (chromaHShift != 0.0)
1184
 
        sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
 
1363
        sws_shiftVec(filter->chrH, (int)(chromaHShift + 0.5));
1185
1364
 
1186
1365
    if (chromaVShift != 0.0)
1187
 
        sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
 
1366
        sws_shiftVec(filter->chrV, (int)(chromaVShift + 0.5));
1188
1367
 
1189
1368
    sws_normalizeVec(filter->chrH, 1.0);
1190
1369
    sws_normalizeVec(filter->chrV, 1.0);
1191
1370
    sws_normalizeVec(filter->lumH, 1.0);
1192
1371
    sws_normalizeVec(filter->lumV, 1.0);
1193
1372
 
1194
 
    if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
1195
 
    if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
 
1373
    if (verbose)
 
1374
        sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
 
1375
    if (verbose)
 
1376
        sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
1196
1377
 
1197
1378
    return filter;
1198
1379
}
1211
1392
 
1212
1393
SwsVector *sws_getGaussianVec(double variance, double quality)
1213
1394
{
1214
 
    const int length= (int)(variance*quality + 0.5) | 1;
 
1395
    const int length = (int)(variance * quality + 0.5) | 1;
1215
1396
    int i;
1216
 
    double middle= (length-1)*0.5;
1217
 
    SwsVector *vec= sws_allocVec(length);
 
1397
    double middle  = (length - 1) * 0.5;
 
1398
    SwsVector *vec = sws_allocVec(length);
1218
1399
 
1219
1400
    if (!vec)
1220
1401
        return NULL;
1221
1402
 
1222
 
    for (i=0; i<length; i++) {
1223
 
        double dist= i-middle;
1224
 
        vec->coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*M_PI);
 
1403
    for (i = 0; i < length; i++) {
 
1404
        double dist = i - middle;
 
1405
        vec->coeff[i] = exp(-dist * dist / (2 * variance * variance)) /
 
1406
                        sqrt(2 * variance * M_PI);
1225
1407
    }
1226
1408
 
1227
1409
    sws_normalizeVec(vec, 1.0);
1232
1414
SwsVector *sws_getConstVec(double c, int length)
1233
1415
{
1234
1416
    int i;
1235
 
    SwsVector *vec= sws_allocVec(length);
 
1417
    SwsVector *vec = sws_allocVec(length);
1236
1418
 
1237
1419
    if (!vec)
1238
1420
        return NULL;
1239
1421
 
1240
 
    for (i=0; i<length; i++)
1241
 
        vec->coeff[i]= c;
 
1422
    for (i = 0; i < length; i++)
 
1423
        vec->coeff[i] = c;
1242
1424
 
1243
1425
    return vec;
1244
1426
}
1251
1433
static double sws_dcVec(SwsVector *a)
1252
1434
{
1253
1435
    int i;
1254
 
    double sum=0;
 
1436
    double sum = 0;
1255
1437
 
1256
 
    for (i=0; i<a->length; i++)
1257
 
        sum+= a->coeff[i];
 
1438
    for (i = 0; i < a->length; i++)
 
1439
        sum += a->coeff[i];
1258
1440
 
1259
1441
    return sum;
1260
1442
}
1263
1445
{
1264
1446
    int i;
1265
1447
 
1266
 
    for (i=0; i<a->length; i++)
1267
 
        a->coeff[i]*= scalar;
 
1448
    for (i = 0; i < a->length; i++)
 
1449
        a->coeff[i] *= scalar;
1268
1450
}
1269
1451
 
1270
1452
void sws_normalizeVec(SwsVector *a, double height)
1271
1453
{
1272
 
    sws_scaleVec(a, height/sws_dcVec(a));
 
1454
    sws_scaleVec(a, height / sws_dcVec(a));
1273
1455
}
1274
1456
 
1275
1457
static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
1276
1458
{
1277
 
    int length= a->length + b->length - 1;
 
1459
    int length = a->length + b->length - 1;
1278
1460
    int i, j;
1279
 
    SwsVector *vec= sws_getConstVec(0.0, length);
 
1461
    SwsVector *vec = sws_getConstVec(0.0, length);
1280
1462
 
1281
1463
    if (!vec)
1282
1464
        return NULL;
1283
1465
 
1284
 
    for (i=0; i<a->length; i++) {
1285
 
        for (j=0; j<b->length; j++) {
1286
 
            vec->coeff[i+j]+= a->coeff[i]*b->coeff[j];
 
1466
    for (i = 0; i < a->length; i++) {
 
1467
        for (j = 0; j < b->length; j++) {
 
1468
            vec->coeff[i + j] += a->coeff[i] * b->coeff[j];
1287
1469
        }
1288
1470
    }
1289
1471
 
1292
1474
 
1293
1475
static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
1294
1476
{
1295
 
    int length= FFMAX(a->length, b->length);
 
1477
    int length = FFMAX(a->length, b->length);
1296
1478
    int i;
1297
 
    SwsVector *vec= sws_getConstVec(0.0, length);
 
1479
    SwsVector *vec = sws_getConstVec(0.0, length);
1298
1480
 
1299
1481
    if (!vec)
1300
1482
        return NULL;
1301
1483
 
1302
 
    for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
1303
 
    for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
 
1484
    for (i = 0; i < a->length; i++)
 
1485
        vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
 
1486
    for (i = 0; i < b->length; i++)
 
1487
        vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] += b->coeff[i];
1304
1488
 
1305
1489
    return vec;
1306
1490
}
1307
1491
 
1308
1492
static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
1309
1493
{
1310
 
    int length= FFMAX(a->length, b->length);
 
1494
    int length = FFMAX(a->length, b->length);
1311
1495
    int i;
1312
 
    SwsVector *vec= sws_getConstVec(0.0, length);
 
1496
    SwsVector *vec = sws_getConstVec(0.0, length);
1313
1497
 
1314
1498
    if (!vec)
1315
1499
        return NULL;
1316
1500
 
1317
 
    for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
1318
 
    for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
 
1501
    for (i = 0; i < a->length; i++)
 
1502
        vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
 
1503
    for (i = 0; i < b->length; i++)
 
1504
        vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] -= b->coeff[i];
1319
1505
 
1320
1506
    return vec;
1321
1507
}
1323
1509
/* shift left / or right if "shift" is negative */
1324
1510
static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
1325
1511
{
1326
 
    int length= a->length + FFABS(shift)*2;
 
1512
    int length = a->length + FFABS(shift) * 2;
1327
1513
    int i;
1328
 
    SwsVector *vec= sws_getConstVec(0.0, length);
 
1514
    SwsVector *vec = sws_getConstVec(0.0, length);
1329
1515
 
1330
1516
    if (!vec)
1331
1517
        return NULL;
1332
1518
 
1333
 
    for (i=0; i<a->length; i++) {
1334
 
        vec->coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
 
1519
    for (i = 0; i < a->length; i++) {
 
1520
        vec->coeff[i + (length    - 1) / 2 -
 
1521
                       (a->length - 1) / 2 - shift] = a->coeff[i];
1335
1522
    }
1336
1523
 
1337
1524
    return vec;
1339
1526
 
1340
1527
void sws_shiftVec(SwsVector *a, int shift)
1341
1528
{
1342
 
    SwsVector *shifted= sws_getShiftedVec(a, shift);
 
1529
    SwsVector *shifted = sws_getShiftedVec(a, shift);
1343
1530
    av_free(a->coeff);
1344
 
    a->coeff= shifted->coeff;
1345
 
    a->length= shifted->length;
 
1531
    a->coeff  = shifted->coeff;
 
1532
    a->length = shifted->length;
1346
1533
    av_free(shifted);
1347
1534
}
1348
1535
 
1349
1536
void sws_addVec(SwsVector *a, SwsVector *b)
1350
1537
{
1351
 
    SwsVector *sum= sws_sumVec(a, b);
 
1538
    SwsVector *sum = sws_sumVec(a, b);
1352
1539
    av_free(a->coeff);
1353
 
    a->coeff= sum->coeff;
1354
 
    a->length= sum->length;
 
1540
    a->coeff  = sum->coeff;
 
1541
    a->length = sum->length;
1355
1542
    av_free(sum);
1356
1543
}
1357
1544
 
1358
1545
void sws_subVec(SwsVector *a, SwsVector *b)
1359
1546
{
1360
 
    SwsVector *diff= sws_diffVec(a, b);
 
1547
    SwsVector *diff = sws_diffVec(a, b);
1361
1548
    av_free(a->coeff);
1362
 
    a->coeff= diff->coeff;
1363
 
    a->length= diff->length;
 
1549
    a->coeff  = diff->coeff;
 
1550
    a->length = diff->length;
1364
1551
    av_free(diff);
1365
1552
}
1366
1553
 
1367
1554
void sws_convVec(SwsVector *a, SwsVector *b)
1368
1555
{
1369
 
    SwsVector *conv= sws_getConvVec(a, b);
 
1556
    SwsVector *conv = sws_getConvVec(a, b);
1370
1557
    av_free(a->coeff);
1371
 
    a->coeff= conv->coeff;
1372
 
    a->length= conv->length;
 
1558
    a->coeff  = conv->coeff;
 
1559
    a->length = conv->length;
1373
1560
    av_free(conv);
1374
1561
}
1375
1562
 
1376
1563
SwsVector *sws_cloneVec(SwsVector *a)
1377
1564
{
1378
1565
    int i;
1379
 
    SwsVector *vec= sws_allocVec(a->length);
 
1566
    SwsVector *vec = sws_allocVec(a->length);
1380
1567
 
1381
1568
    if (!vec)
1382
1569
        return NULL;
1383
1570
 
1384
 
    for (i=0; i<a->length; i++) vec->coeff[i]= a->coeff[i];
 
1571
    for (i = 0; i < a->length; i++)
 
1572
        vec->coeff[i] = a->coeff[i];
1385
1573
 
1386
1574
    return vec;
1387
1575
}
1389
1577
void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
1390
1578
{
1391
1579
    int i;
1392
 
    double max=0;
1393
 
    double min=0;
 
1580
    double max = 0;
 
1581
    double min = 0;
1394
1582
    double range;
1395
1583
 
1396
 
    for (i=0; i<a->length; i++)
1397
 
        if (a->coeff[i]>max) max= a->coeff[i];
1398
 
 
1399
 
    for (i=0; i<a->length; i++)
1400
 
        if (a->coeff[i]<min) min= a->coeff[i];
1401
 
 
1402
 
    range= max - min;
1403
 
 
1404
 
    for (i=0; i<a->length; i++) {
1405
 
        int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
 
1584
    for (i = 0; i < a->length; i++)
 
1585
        if (a->coeff[i] > max)
 
1586
            max = a->coeff[i];
 
1587
 
 
1588
    for (i = 0; i < a->length; i++)
 
1589
        if (a->coeff[i] < min)
 
1590
            min = a->coeff[i];
 
1591
 
 
1592
    range = max - min;
 
1593
 
 
1594
    for (i = 0; i < a->length; i++) {
 
1595
        int x = (int)((a->coeff[i] - min) * 60.0 / range + 0.5);
1406
1596
        av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
1407
 
        for (;x>0; x--) av_log(log_ctx, log_level, " ");
 
1597
        for (; x > 0; x--)
 
1598
            av_log(log_ctx, log_level, " ");
1408
1599
        av_log(log_ctx, log_level, "|\n");
1409
1600
    }
1410
1601
}
1411
1602
 
1412
1603
void sws_freeVec(SwsVector *a)
1413
1604
{
1414
 
    if (!a) return;
 
1605
    if (!a)
 
1606
        return;
1415
1607
    av_freep(&a->coeff);
1416
 
    a->length=0;
 
1608
    a->length = 0;
1417
1609
    av_free(a);
1418
1610
}
1419
1611
 
1420
1612
void sws_freeFilter(SwsFilter *filter)
1421
1613
{
1422
 
    if (!filter) return;
 
1614
    if (!filter)
 
1615
        return;
1423
1616
 
1424
 
    if (filter->lumH) sws_freeVec(filter->lumH);
1425
 
    if (filter->lumV) sws_freeVec(filter->lumV);
1426
 
    if (filter->chrH) sws_freeVec(filter->chrH);
1427
 
    if (filter->chrV) sws_freeVec(filter->chrV);
 
1617
    if (filter->lumH)
 
1618
        sws_freeVec(filter->lumH);
 
1619
    if (filter->lumV)
 
1620
        sws_freeVec(filter->lumV);
 
1621
    if (filter->chrH)
 
1622
        sws_freeVec(filter->chrH);
 
1623
    if (filter->chrV)
 
1624
        sws_freeVec(filter->chrV);
1428
1625
    av_free(filter);
1429
1626
}
1430
1627
 
1431
1628
void sws_freeContext(SwsContext *c)
1432
1629
{
1433
1630
    int i;
1434
 
    if (!c) return;
 
1631
    if (!c)
 
1632
        return;
1435
1633
 
1436
1634
    if (c->lumPixBuf) {
1437
 
        for (i=0; i<c->vLumBufSize; i++)
 
1635
        for (i = 0; i < c->vLumBufSize; i++)
1438
1636
            av_freep(&c->lumPixBuf[i]);
1439
1637
        av_freep(&c->lumPixBuf);
1440
1638
    }
1441
1639
 
1442
1640
    if (c->chrUPixBuf) {
1443
 
        for (i=0; i<c->vChrBufSize; i++)
 
1641
        for (i = 0; i < c->vChrBufSize; i++)
1444
1642
            av_freep(&c->chrUPixBuf[i]);
1445
1643
        av_freep(&c->chrUPixBuf);
1446
1644
        av_freep(&c->chrVPixBuf);
1447
1645
    }
1448
1646
 
1449
1647
    if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1450
 
        for (i=0; i<c->vLumBufSize; i++)
 
1648
        for (i = 0; i < c->vLumBufSize; i++)
1451
1649
            av_freep(&c->alpPixBuf[i]);
1452
1650
        av_freep(&c->alpPixBuf);
1453
1651
    }
1466
1664
    av_freep(&c->hLumFilterPos);
1467
1665
    av_freep(&c->hChrFilterPos);
1468
1666
 
1469
 
#if HAVE_MMX
1470
 
#ifdef MAP_ANONYMOUS
1471
 
    if (c->lumMmx2FilterCode) munmap(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize);
1472
 
    if (c->chrMmx2FilterCode) munmap(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize);
 
1667
#if HAVE_MMX_INLINE
 
1668
#if USE_MMAP
 
1669
    if (c->lumMmxextFilterCode)
 
1670
        munmap(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize);
 
1671
    if (c->chrMmxextFilterCode)
 
1672
        munmap(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize);
1473
1673
#elif HAVE_VIRTUALALLOC
1474
 
    if (c->lumMmx2FilterCode) VirtualFree(c->lumMmx2FilterCode, 0, MEM_RELEASE);
1475
 
    if (c->chrMmx2FilterCode) VirtualFree(c->chrMmx2FilterCode, 0, MEM_RELEASE);
 
1674
    if (c->lumMmxextFilterCode)
 
1675
        VirtualFree(c->lumMmxextFilterCode, 0, MEM_RELEASE);
 
1676
    if (c->chrMmxextFilterCode)
 
1677
        VirtualFree(c->chrMmxextFilterCode, 0, MEM_RELEASE);
1476
1678
#else
1477
 
    av_free(c->lumMmx2FilterCode);
1478
 
    av_free(c->chrMmx2FilterCode);
 
1679
    av_free(c->lumMmxextFilterCode);
 
1680
    av_free(c->chrMmxextFilterCode);
1479
1681
#endif
1480
 
    c->lumMmx2FilterCode=NULL;
1481
 
    c->chrMmx2FilterCode=NULL;
1482
 
#endif /* HAVE_MMX */
 
1682
    c->lumMmxextFilterCode = NULL;
 
1683
    c->chrMmxextFilterCode = NULL;
 
1684
#endif /* HAVE_MMX_INLINE */
1483
1685
 
1484
1686
    av_freep(&c->yuvTable);
1485
1687
    av_free(c->formatConvBuffer);
1487
1689
    av_free(c);
1488
1690
}
1489
1691
 
1490
 
struct SwsContext *sws_getCachedContext(struct SwsContext *context,
1491
 
                                        int srcW, int srcH, enum PixelFormat srcFormat,
1492
 
                                        int dstW, int dstH, enum PixelFormat dstFormat, int flags,
1493
 
                                        SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
 
1692
struct SwsContext *sws_getCachedContext(struct SwsContext *context, int srcW,
 
1693
                                        int srcH, enum AVPixelFormat srcFormat,
 
1694
                                        int dstW, int dstH,
 
1695
                                        enum AVPixelFormat dstFormat, int flags,
 
1696
                                        SwsFilter *srcFilter,
 
1697
                                        SwsFilter *dstFilter,
 
1698
                                        const double *param)
1494
1699
{
1495
 
    static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
 
1700
    static const double default_param[2] = { SWS_PARAM_DEFAULT,
 
1701
                                             SWS_PARAM_DEFAULT };
1496
1702
 
1497
1703
    if (!param)
1498
1704
        param = default_param;
1525
1731
        context->flags     = flags;
1526
1732
        context->param[0]  = param[0];
1527
1733
        context->param[1]  = param[1];
1528
 
        sws_setColorspaceDetails(context, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], context->srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, context->dstRange, 0, 1<<16, 1<<16);
 
1734
        sws_setColorspaceDetails(context, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT],
 
1735
                                 context->srcRange,
 
1736
                                 ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/,
 
1737
                                 context->dstRange, 0, 1 << 16, 1 << 16);
1529
1738
        if (sws_init_context(context, srcFilter, dstFilter) < 0) {
1530
1739
            sws_freeContext(context);
1531
1740
            return NULL;
1533
1742
    }
1534
1743
    return context;
1535
1744
}
1536