summaryrefslogtreecommitdiffstats
path: root/video/sws_utils.c
blob: 7d1845f4313249786b4ab766437333b3645abc00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * This file is part of MPlayer.
 *
 * MPlayer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * MPlayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <assert.h>

#include <libavutil/opt.h>

#include "sws_utils.h"

#include "video/mp_image.h"
#include "video/img_format.h"
#include "fmt-conversion.h"
#include "csputils.h"
#include "core/mp_msg.h"

//global sws_flags from the command line
int sws_flags = 2;

float sws_lum_gblur = 0.0;
float sws_chr_gblur = 0.0;
int sws_chr_vshift = 0;
int sws_chr_hshift = 0;
float sws_chr_sharpen = 0.0;
float sws_lum_sharpen = 0.0;

//global srcFilter
static SwsFilter *src_filter = NULL;

void sws_getFlagsAndFilterFromCmdLine(int *flags, SwsFilter **srcFilterParam,
                                      SwsFilter **dstFilterParam)
{
    static int firstTime = 1;
    *flags = 0;

    if (firstTime) {
        firstTime = 0;
        *flags = SWS_PRINT_INFO;
    } else if (mp_msg_test(MSGT_VFILTER, MSGL_DBG2))
        *flags = SWS_PRINT_INFO;

    if (src_filter)
        sws_freeFilter(src_filter);

    src_filter = sws_getDefaultFilter(
        sws_lum_gblur, sws_chr_gblur,
        sws_lum_sharpen, sws_chr_sharpen,
        sws_chr_hshift, sws_chr_vshift, verbose > 1);

    switch (sws_flags) {
    case 0: *flags |= SWS_FAST_BILINEAR;
        break;
    case 1: *flags |= SWS_BILINEAR;
        break;
    case 2: *flags |= SWS_BICUBIC;
        break;
    case 3: *flags |= SWS_X;
        break;
    case 4: *flags |= SWS_POINT;
        break;
    case 5: *flags |= SWS_AREA;
        break;
    case 6: *flags |= SWS_BICUBLIN;
        break;
    case 7: *flags |= SWS_GAUSS;
        break;
    case 8: *flags |= SWS_SINC;
        break;
    case 9: *flags |= SWS_LANCZOS;
        break;
    case 10: *flags |= SWS_SPLINE;
        break;
    default: *flags |= SWS_BILINEAR;
        break;
    }

    *srcFilterParam = src_filter;
    *dstFilterParam = NULL;
}

// will use sws_flags & src_filter (from cmd line)
static struct SwsContext *sws_getContextFromCmdLine2(int srcW, int srcH,
                                                     int srcFormat, int dstW,
                                                     int dstH, int dstFormat,
                                                     int extraflags)
{
    int flags;
    SwsFilter *dstFilterParam, *srcFilterParam;
    enum PixelFormat dfmt, sfmt;

    dfmt = imgfmt2pixfmt(dstFormat);
    sfmt = imgfmt2pixfmt(srcFormat);
    if (srcFormat == IMGFMT_RGB8 || srcFormat == IMGFMT_BGR8)
        sfmt = PIX_FMT_PAL8;
    sws_getFlagsAndFilterFromCmdLine(&flags, &srcFilterParam, &dstFilterParam);

    return sws_getContext(srcW, srcH, sfmt, dstW, dstH, dfmt, flags |
                          extraflags, srcFilterParam, dstFilterParam,
                          NULL);
}

struct SwsContext *sws_getContextFromCmdLine(int srcW, int srcH, int srcFormat,
                                             int dstW, int dstH,
                                             int dstFormat)
{
    return sws_getContextFromCmdLine2(srcW, srcH, srcFormat, dstW, dstH,
                                      dstFormat,
                                      0);
}

struct SwsContext *sws_getContextFromCmdLine_hq(int srcW, int srcH,
                                                int srcFormat, int dstW,
                                                int dstH,
                                                int dstFormat)
{
    return sws_getContextFromCmdLine2(
               srcW, srcH, srcFormat, dstW, dstH, dstFormat,
               SWS_FULL_CHR_H_INT | SWS_FULL_CHR_H_INP |
               SWS_ACCURATE_RND | SWS_BITEXACT);
}

bool mp_sws_supported_format(int imgfmt)
{
    enum PixelFormat av_format = imgfmt2pixfmt(imgfmt);

    return av_format != PIX_FMT_NONE && sws_isSupportedInput(av_format)
        && sws_isSupportedOutput(av_format);
}

static int mp_csp_to_sws_colorspace(enum mp_csp csp)
{
    switch (csp) {
    case MP_CSP_BT_601:     return SWS_CS_ITU601;
    case MP_CSP_BT_709:     return SWS_CS_ITU709;
    case MP_CSP_SMPTE_240M: return SWS_CS_SMPTE240M;
    default:                return SWS_CS_DEFAULT;
    }
}

// component_offset[]: byte index of each r (0), g (1), b (2), a (3) component
static void planarize32(struct mp_image *dst, struct mp_image *src,
                        int component_offset[4])
{
    for (int y = 0; y < dst->h; y++) {
        for (int p = 0; p < 3; p++) {
            uint8_t *d_line = dst->planes[p] + y * dst->stride[p];
            uint8_t *s_line = src->planes[0] + y * src->stride[0];
            s_line += component_offset[(p + 1) % 3]; // GBR => RGB
            for (int x = 0; x < dst->w; x++) {
                d_line[x] = s_line[x * 4];
            }
        }
    }
}

#define SET_COMPS(comp, r, g, b, a) \
    { (comp)[0] = (r); (comp)[1] = (g); (comp)[2] = (b); (comp)[3] = (a); }

static void to_gbrp(struct mp_image *dst, struct mp_image *src,
                    int my_sws_flags)
{
    struct mp_image *temp = NULL;
    int comp[4];

    switch (src->imgfmt) {
    case IMGFMT_ABGR: SET_COMPS(comp, 3, 2, 1, 0); break;
    case IMGFMT_BGRA: SET_COMPS(comp, 2, 1, 0, 3); break;
    case IMGFMT_ARGB: SET_COMPS(comp, 1, 2, 3, 0); break;
    case IMGFMT_RGBA: SET_COMPS(comp, 0, 1, 2, 3); break;
    default:
        temp = alloc_mpi(dst->w, dst->h, IMGFMT_RGBA);
        mp_image_swscale(temp, src, my_sws_flags);
        src = temp;
        SET_COMPS(comp, 0, 1, 2, 3);
    }

    planarize32(dst, src, comp);

    talloc_free(temp);
}

void mp_image_swscale(struct mp_image *dst, struct mp_image *src,
                      int my_sws_flags)
{
    if (dst->imgfmt == IMGFMT_GBRP)
        return to_gbrp(dst, src, my_sws_flags);

    enum PixelFormat s_fmt = imgfmt2pixfmt(src->imgfmt);
    if (src->imgfmt == IMGFMT_RGB8 || src->imgfmt == IMGFMT_BGR8)
        s_fmt = PIX_FMT_PAL8;
    int s_csp = mp_csp_to_sws_colorspace(mp_image_csp(src));
    int s_range = mp_image_levels(src) == MP_CSP_LEVELS_PC;

    enum PixelFormat d_fmt = imgfmt2pixfmt(dst->imgfmt);
    int d_csp = mp_csp_to_sws_colorspace(mp_image_csp(dst));
    int d_range = mp_image_levels(dst) == MP_CSP_LEVELS_PC;

    // Work around libswscale bug #1852 (fixed in ffmpeg commit 8edf9b1fa):
    // setting range flags for RGB gives random bogus results.
    // Newer libswscale always ignores range flags for RGB.
    bool s_yuv = src->flags & MP_IMGFLAG_YUV;
    bool d_yuv = dst->flags & MP_IMGFLAG_YUV;
    s_range = s_range && s_yuv;
    d_range = d_range && d_yuv;

    struct SwsContext *sws = sws_alloc_context();

    av_opt_set_int(sws, "sws_flags", my_sws_flags, 0);

    av_opt_set_int(sws, "srcw", src->w, 0);
    av_opt_set_int(sws, "srch", src->h, 0);
    av_opt_set_int(sws, "src_format", s_fmt, 0);

    av_opt_set_int(sws, "dstw", dst->w, 0);
    av_opt_set_int(sws, "dsth", dst->h, 0);
    av_opt_set_int(sws, "dst_format", d_fmt, 0);

    sws_setColorspaceDetails(sws, sws_getCoefficients(s_csp), s_range,
                             sws_getCoefficients(d_csp), d_range,
                             0, 1 << 16, 1 << 16);

    int res = sws_init_context(sws, NULL, NULL);
    assert(res >= 0);

    sws_scale(sws, (const uint8_t *const *) src->planes, src->stride,
              0, src->h, dst->planes, dst->stride);
    sws_freeContext(sws);
}

// vim: ts=4 sw=4 et tw=80