Coverage Report

Created: 2026-04-29 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libswscale/format.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2024 Niklas Haas
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#include "libavutil/attributes.h"
22
#include "libavutil/avassert.h"
23
#include "libavutil/hdr_dynamic_metadata.h"
24
#include "libavutil/mastering_display_metadata.h"
25
#include "libavutil/refstruct.h"
26
27
#include "format.h"
28
#include "csputils.h"
29
#include "ops_internal.h"
30
#include "config_components.h"
31
32
#if CONFIG_UNSTABLE
33
#include "libavutil/hwcontext.h"
34
#endif
35
36
0
#define Q(N) ((AVRational) { N, 1 })
37
0
#define Q0   Q(0)
38
0
#define Q1   Q(1)
39
40
#define RET(x)                                                                 \
41
0
    do {                                                                       \
42
0
        int _ret = (x);                                                        \
43
0
        if (_ret  < 0)                                                         \
44
0
            return _ret;                                                       \
45
0
    } while (0)
46
47
typedef struct LegacyFormatEntry {
48
    uint8_t is_supported_in         :1;
49
    uint8_t is_supported_out        :1;
50
    uint8_t is_supported_endianness :1;
51
} LegacyFormatEntry;
52
53
/* Format support table for legacy swscale */
54
static const LegacyFormatEntry legacy_format_entries[] = {
55
    [AV_PIX_FMT_YUV420P]        = { 1, 1 },
56
    [AV_PIX_FMT_YUYV422]        = { 1, 1 },
57
    [AV_PIX_FMT_RGB24]          = { 1, 1 },
58
    [AV_PIX_FMT_BGR24]          = { 1, 1 },
59
    [AV_PIX_FMT_YUV422P]        = { 1, 1 },
60
    [AV_PIX_FMT_YUV444P]        = { 1, 1 },
61
    [AV_PIX_FMT_YUV410P]        = { 1, 1 },
62
    [AV_PIX_FMT_YUV411P]        = { 1, 1 },
63
    [AV_PIX_FMT_GRAY8]          = { 1, 1 },
64
    [AV_PIX_FMT_MONOWHITE]      = { 1, 1 },
65
    [AV_PIX_FMT_MONOBLACK]      = { 1, 1 },
66
    [AV_PIX_FMT_PAL8]           = { 1, 0 },
67
    [AV_PIX_FMT_YUVJ420P]       = { 1, 1 },
68
    [AV_PIX_FMT_YUVJ411P]       = { 1, 1 },
69
    [AV_PIX_FMT_YUVJ422P]       = { 1, 1 },
70
    [AV_PIX_FMT_YUVJ444P]       = { 1, 1 },
71
    [AV_PIX_FMT_YVYU422]        = { 1, 1 },
72
    [AV_PIX_FMT_UYVY422]        = { 1, 1 },
73
    [AV_PIX_FMT_UYYVYY411]      = { 1, 0 },
74
    [AV_PIX_FMT_BGR8]           = { 1, 1 },
75
    [AV_PIX_FMT_BGR4]           = { 0, 1 },
76
    [AV_PIX_FMT_BGR4_BYTE]      = { 1, 1 },
77
    [AV_PIX_FMT_RGB8]           = { 1, 1 },
78
    [AV_PIX_FMT_RGB4]           = { 0, 1 },
79
    [AV_PIX_FMT_RGB4_BYTE]      = { 1, 1 },
80
    [AV_PIX_FMT_NV12]           = { 1, 1 },
81
    [AV_PIX_FMT_NV21]           = { 1, 1 },
82
    [AV_PIX_FMT_ARGB]           = { 1, 1 },
83
    [AV_PIX_FMT_RGBA]           = { 1, 1 },
84
    [AV_PIX_FMT_ABGR]           = { 1, 1 },
85
    [AV_PIX_FMT_BGRA]           = { 1, 1 },
86
    [AV_PIX_FMT_0RGB]           = { 1, 1 },
87
    [AV_PIX_FMT_RGB0]           = { 1, 1 },
88
    [AV_PIX_FMT_0BGR]           = { 1, 1 },
89
    [AV_PIX_FMT_BGR0]           = { 1, 1 },
90
    [AV_PIX_FMT_GRAY9BE]        = { 1, 1 },
91
    [AV_PIX_FMT_GRAY9LE]        = { 1, 1 },
92
    [AV_PIX_FMT_GRAY10BE]       = { 1, 1 },
93
    [AV_PIX_FMT_GRAY10LE]       = { 1, 1 },
94
    [AV_PIX_FMT_GRAY12BE]       = { 1, 1 },
95
    [AV_PIX_FMT_GRAY12LE]       = { 1, 1 },
96
    [AV_PIX_FMT_GRAY14BE]       = { 1, 1 },
97
    [AV_PIX_FMT_GRAY14LE]       = { 1, 1 },
98
    [AV_PIX_FMT_GRAY16BE]       = { 1, 1 },
99
    [AV_PIX_FMT_GRAY16LE]       = { 1, 1 },
100
    [AV_PIX_FMT_YUV440P]        = { 1, 1 },
101
    [AV_PIX_FMT_YUVJ440P]       = { 1, 1 },
102
    [AV_PIX_FMT_YUV440P10LE]    = { 1, 1 },
103
    [AV_PIX_FMT_YUV440P10BE]    = { 1, 1 },
104
    [AV_PIX_FMT_YUV440P12LE]    = { 1, 1 },
105
    [AV_PIX_FMT_YUV440P12BE]    = { 1, 1 },
106
    [AV_PIX_FMT_YUVA420P]       = { 1, 1 },
107
    [AV_PIX_FMT_YUVA422P]       = { 1, 1 },
108
    [AV_PIX_FMT_YUVA444P]       = { 1, 1 },
109
    [AV_PIX_FMT_YUVA420P9BE]    = { 1, 1 },
110
    [AV_PIX_FMT_YUVA420P9LE]    = { 1, 1 },
111
    [AV_PIX_FMT_YUVA422P9BE]    = { 1, 1 },
112
    [AV_PIX_FMT_YUVA422P9LE]    = { 1, 1 },
113
    [AV_PIX_FMT_YUVA444P9BE]    = { 1, 1 },
114
    [AV_PIX_FMT_YUVA444P9LE]    = { 1, 1 },
115
    [AV_PIX_FMT_YUVA420P10BE]   = { 1, 1 },
116
    [AV_PIX_FMT_YUVA420P10LE]   = { 1, 1 },
117
    [AV_PIX_FMT_YUVA422P10BE]   = { 1, 1 },
118
    [AV_PIX_FMT_YUVA422P10LE]   = { 1, 1 },
119
    [AV_PIX_FMT_YUVA444P10BE]   = { 1, 1 },
120
    [AV_PIX_FMT_YUVA444P10LE]   = { 1, 1 },
121
    [AV_PIX_FMT_YUVA420P16BE]   = { 1, 1 },
122
    [AV_PIX_FMT_YUVA420P16LE]   = { 1, 1 },
123
    [AV_PIX_FMT_YUVA422P16BE]   = { 1, 1 },
124
    [AV_PIX_FMT_YUVA422P16LE]   = { 1, 1 },
125
    [AV_PIX_FMT_YUVA444P16BE]   = { 1, 1 },
126
    [AV_PIX_FMT_YUVA444P16LE]   = { 1, 1 },
127
    [AV_PIX_FMT_RGB48BE]        = { 1, 1 },
128
    [AV_PIX_FMT_RGB48LE]        = { 1, 1 },
129
    [AV_PIX_FMT_RGBA64BE]       = { 1, 1, 1 },
130
    [AV_PIX_FMT_RGBA64LE]       = { 1, 1, 1 },
131
    [AV_PIX_FMT_RGB565BE]       = { 1, 1 },
132
    [AV_PIX_FMT_RGB565LE]       = { 1, 1 },
133
    [AV_PIX_FMT_RGB555BE]       = { 1, 1 },
134
    [AV_PIX_FMT_RGB555LE]       = { 1, 1 },
135
    [AV_PIX_FMT_BGR565BE]       = { 1, 1 },
136
    [AV_PIX_FMT_BGR565LE]       = { 1, 1 },
137
    [AV_PIX_FMT_BGR555BE]       = { 1, 1 },
138
    [AV_PIX_FMT_BGR555LE]       = { 1, 1 },
139
    [AV_PIX_FMT_YUV420P16LE]    = { 1, 1 },
140
    [AV_PIX_FMT_YUV420P16BE]    = { 1, 1 },
141
    [AV_PIX_FMT_YUV422P16LE]    = { 1, 1 },
142
    [AV_PIX_FMT_YUV422P16BE]    = { 1, 1 },
143
    [AV_PIX_FMT_YUV444P16LE]    = { 1, 1 },
144
    [AV_PIX_FMT_YUV444P16BE]    = { 1, 1 },
145
    [AV_PIX_FMT_RGB444LE]       = { 1, 1 },
146
    [AV_PIX_FMT_RGB444BE]       = { 1, 1 },
147
    [AV_PIX_FMT_BGR444LE]       = { 1, 1 },
148
    [AV_PIX_FMT_BGR444BE]       = { 1, 1 },
149
    [AV_PIX_FMT_YA8]            = { 1, 1 },
150
    [AV_PIX_FMT_YA16BE]         = { 1, 1 },
151
    [AV_PIX_FMT_YA16LE]         = { 1, 1 },
152
    [AV_PIX_FMT_BGR48BE]        = { 1, 1 },
153
    [AV_PIX_FMT_BGR48LE]        = { 1, 1 },
154
    [AV_PIX_FMT_BGRA64BE]       = { 1, 1, 1 },
155
    [AV_PIX_FMT_BGRA64LE]       = { 1, 1, 1 },
156
    [AV_PIX_FMT_YUV420P9BE]     = { 1, 1 },
157
    [AV_PIX_FMT_YUV420P9LE]     = { 1, 1 },
158
    [AV_PIX_FMT_YUV420P10BE]    = { 1, 1 },
159
    [AV_PIX_FMT_YUV420P10LE]    = { 1, 1 },
160
    [AV_PIX_FMT_YUV420P12BE]    = { 1, 1 },
161
    [AV_PIX_FMT_YUV420P12LE]    = { 1, 1 },
162
    [AV_PIX_FMT_YUV420P14BE]    = { 1, 1 },
163
    [AV_PIX_FMT_YUV420P14LE]    = { 1, 1 },
164
    [AV_PIX_FMT_YUV422P9BE]     = { 1, 1 },
165
    [AV_PIX_FMT_YUV422P9LE]     = { 1, 1 },
166
    [AV_PIX_FMT_YUV422P10BE]    = { 1, 1 },
167
    [AV_PIX_FMT_YUV422P10LE]    = { 1, 1 },
168
    [AV_PIX_FMT_YUV422P12BE]    = { 1, 1 },
169
    [AV_PIX_FMT_YUV422P12LE]    = { 1, 1 },
170
    [AV_PIX_FMT_YUV422P14BE]    = { 1, 1 },
171
    [AV_PIX_FMT_YUV422P14LE]    = { 1, 1 },
172
    [AV_PIX_FMT_YUV444P9BE]     = { 1, 1 },
173
    [AV_PIX_FMT_YUV444P9LE]     = { 1, 1 },
174
    [AV_PIX_FMT_YUV444P10BE]    = { 1, 1 },
175
    [AV_PIX_FMT_YUV444P10LE]    = { 1, 1 },
176
    [AV_PIX_FMT_YUV444P12BE]    = { 1, 1 },
177
    [AV_PIX_FMT_YUV444P12LE]    = { 1, 1 },
178
    [AV_PIX_FMT_YUV444P14BE]    = { 1, 1 },
179
    [AV_PIX_FMT_YUV444P14LE]    = { 1, 1 },
180
    [AV_PIX_FMT_YUV444P10MSBBE] = { 1, 1 },
181
    [AV_PIX_FMT_YUV444P10MSBLE] = { 1, 1 },
182
    [AV_PIX_FMT_YUV444P12MSBBE] = { 1, 1 },
183
    [AV_PIX_FMT_YUV444P12MSBLE] = { 1, 1 },
184
    [AV_PIX_FMT_GBRP]           = { 1, 1 },
185
    [AV_PIX_FMT_GBRP9LE]        = { 1, 1 },
186
    [AV_PIX_FMT_GBRP9BE]        = { 1, 1 },
187
    [AV_PIX_FMT_GBRP10LE]       = { 1, 1 },
188
    [AV_PIX_FMT_GBRP10BE]       = { 1, 1 },
189
    [AV_PIX_FMT_GBRAP10LE]      = { 1, 1 },
190
    [AV_PIX_FMT_GBRAP10BE]      = { 1, 1 },
191
    [AV_PIX_FMT_GBRP10MSBLE]    = { 1, 1 },
192
    [AV_PIX_FMT_GBRP10MSBBE]    = { 1, 1 },
193
    [AV_PIX_FMT_GBRP12LE]       = { 1, 1 },
194
    [AV_PIX_FMT_GBRP12BE]       = { 1, 1 },
195
    [AV_PIX_FMT_GBRP12MSBLE]    = { 1, 1 },
196
    [AV_PIX_FMT_GBRP12MSBBE]    = { 1, 1 },
197
    [AV_PIX_FMT_GBRAP12LE]      = { 1, 1 },
198
    [AV_PIX_FMT_GBRAP12BE]      = { 1, 1 },
199
    [AV_PIX_FMT_GBRP14LE]       = { 1, 1 },
200
    [AV_PIX_FMT_GBRP14BE]       = { 1, 1 },
201
    [AV_PIX_FMT_GBRAP14LE]      = { 1, 1 },
202
    [AV_PIX_FMT_GBRAP14BE]      = { 1, 1 },
203
    [AV_PIX_FMT_GBRP16LE]       = { 1, 1 },
204
    [AV_PIX_FMT_GBRP16BE]       = { 1, 1 },
205
    [AV_PIX_FMT_GBRPF32LE]      = { 1, 1 },
206
    [AV_PIX_FMT_GBRPF32BE]      = { 1, 1 },
207
    [AV_PIX_FMT_GBRAPF32LE]     = { 1, 1 },
208
    [AV_PIX_FMT_GBRAPF32BE]     = { 1, 1 },
209
    [AV_PIX_FMT_GBRPF16LE]      = { 1, 0 },
210
    [AV_PIX_FMT_GBRPF16BE]      = { 1, 0 },
211
    [AV_PIX_FMT_GBRAPF16LE]     = { 1, 0 },
212
    [AV_PIX_FMT_GBRAPF16BE]     = { 1, 0 },
213
    [AV_PIX_FMT_GBRAP]          = { 1, 1 },
214
    [AV_PIX_FMT_GBRAP16LE]      = { 1, 1 },
215
    [AV_PIX_FMT_GBRAP16BE]      = { 1, 1 },
216
    [AV_PIX_FMT_BAYER_BGGR8]    = { 1, 0 },
217
    [AV_PIX_FMT_BAYER_RGGB8]    = { 1, 0 },
218
    [AV_PIX_FMT_BAYER_GBRG8]    = { 1, 0 },
219
    [AV_PIX_FMT_BAYER_GRBG8]    = { 1, 0 },
220
    [AV_PIX_FMT_BAYER_BGGR16LE] = { 1, 0 },
221
    [AV_PIX_FMT_BAYER_BGGR16BE] = { 1, 0 },
222
    [AV_PIX_FMT_BAYER_RGGB16LE] = { 1, 0 },
223
    [AV_PIX_FMT_BAYER_RGGB16BE] = { 1, 0 },
224
    [AV_PIX_FMT_BAYER_GBRG16LE] = { 1, 0 },
225
    [AV_PIX_FMT_BAYER_GBRG16BE] = { 1, 0 },
226
    [AV_PIX_FMT_BAYER_GRBG16LE] = { 1, 0 },
227
    [AV_PIX_FMT_BAYER_GRBG16BE] = { 1, 0 },
228
    [AV_PIX_FMT_XYZ12BE]        = { 1, 1, 1 },
229
    [AV_PIX_FMT_XYZ12LE]        = { 1, 1, 1 },
230
    [AV_PIX_FMT_AYUV64LE]       = { 1, 1},
231
    [AV_PIX_FMT_AYUV64BE]       = { 1, 1 },
232
    [AV_PIX_FMT_P010LE]         = { 1, 1 },
233
    [AV_PIX_FMT_P010BE]         = { 1, 1 },
234
    [AV_PIX_FMT_P012LE]         = { 1, 1 },
235
    [AV_PIX_FMT_P012BE]         = { 1, 1 },
236
    [AV_PIX_FMT_P016LE]         = { 1, 1 },
237
    [AV_PIX_FMT_P016BE]         = { 1, 1 },
238
    [AV_PIX_FMT_GRAYF32LE]      = { 1, 1 },
239
    [AV_PIX_FMT_GRAYF32BE]      = { 1, 1 },
240
    [AV_PIX_FMT_GRAYF16LE]      = { 1, 0 },
241
    [AV_PIX_FMT_GRAYF16BE]      = { 1, 0 },
242
    [AV_PIX_FMT_YAF32LE]        = { 1, 0 },
243
    [AV_PIX_FMT_YAF32BE]        = { 1, 0 },
244
    [AV_PIX_FMT_YAF16LE]        = { 1, 0 },
245
    [AV_PIX_FMT_YAF16BE]        = { 1, 0 },
246
    [AV_PIX_FMT_YUVA422P12BE]   = { 1, 1 },
247
    [AV_PIX_FMT_YUVA422P12LE]   = { 1, 1 },
248
    [AV_PIX_FMT_YUVA444P12BE]   = { 1, 1 },
249
    [AV_PIX_FMT_YUVA444P12LE]   = { 1, 1 },
250
    [AV_PIX_FMT_NV24]           = { 1, 1 },
251
    [AV_PIX_FMT_NV42]           = { 1, 1 },
252
    [AV_PIX_FMT_Y210LE]         = { 1, 1 },
253
    [AV_PIX_FMT_Y212LE]         = { 1, 1 },
254
    [AV_PIX_FMT_Y216LE]         = { 1, 1 },
255
    [AV_PIX_FMT_X2RGB10LE]      = { 1, 1 },
256
    [AV_PIX_FMT_X2BGR10LE]      = { 1, 1 },
257
    [AV_PIX_FMT_NV20BE]         = { 1, 1 },
258
    [AV_PIX_FMT_NV20LE]         = { 1, 1 },
259
    [AV_PIX_FMT_P210BE]         = { 1, 1 },
260
    [AV_PIX_FMT_P210LE]         = { 1, 1 },
261
    [AV_PIX_FMT_P212BE]         = { 1, 1 },
262
    [AV_PIX_FMT_P212LE]         = { 1, 1 },
263
    [AV_PIX_FMT_P410BE]         = { 1, 1 },
264
    [AV_PIX_FMT_P410LE]         = { 1, 1 },
265
    [AV_PIX_FMT_P412BE]         = { 1, 1 },
266
    [AV_PIX_FMT_P412LE]         = { 1, 1 },
267
    [AV_PIX_FMT_P216BE]         = { 1, 1 },
268
    [AV_PIX_FMT_P216LE]         = { 1, 1 },
269
    [AV_PIX_FMT_P416BE]         = { 1, 1 },
270
    [AV_PIX_FMT_P416LE]         = { 1, 1 },
271
    [AV_PIX_FMT_NV16]           = { 1, 1 },
272
    [AV_PIX_FMT_VUYA]           = { 1, 1 },
273
    [AV_PIX_FMT_VUYX]           = { 1, 1 },
274
    [AV_PIX_FMT_RGBAF16BE]      = { 1, 0 },
275
    [AV_PIX_FMT_RGBAF16LE]      = { 1, 0 },
276
    [AV_PIX_FMT_RGBF16BE]       = { 1, 0 },
277
    [AV_PIX_FMT_RGBF16LE]       = { 1, 0 },
278
    [AV_PIX_FMT_RGBF32BE]       = { 1, 0 },
279
    [AV_PIX_FMT_RGBF32LE]       = { 1, 0 },
280
    [AV_PIX_FMT_XV30LE]         = { 1, 1 },
281
    [AV_PIX_FMT_XV36LE]         = { 1, 1 },
282
    [AV_PIX_FMT_XV36BE]         = { 1, 1 },
283
    [AV_PIX_FMT_XV48LE]         = { 1, 1 },
284
    [AV_PIX_FMT_XV48BE]         = { 1, 1 },
285
    [AV_PIX_FMT_AYUV]           = { 1, 1 },
286
    [AV_PIX_FMT_UYVA]           = { 1, 1 },
287
    [AV_PIX_FMT_VYU444]         = { 1, 1 },
288
    [AV_PIX_FMT_V30XLE]         = { 1, 1 },
289
};
290
291
int sws_isSupportedInput(enum AVPixelFormat pix_fmt)
292
0
{
293
0
    return (unsigned)pix_fmt < FF_ARRAY_ELEMS(legacy_format_entries) ?
294
0
    legacy_format_entries[pix_fmt].is_supported_in : 0;
295
0
}
296
297
int sws_isSupportedOutput(enum AVPixelFormat pix_fmt)
298
0
{
299
0
    return (unsigned)pix_fmt < FF_ARRAY_ELEMS(legacy_format_entries) ?
300
0
    legacy_format_entries[pix_fmt].is_supported_out : 0;
301
0
}
302
303
int sws_isSupportedEndiannessConversion(enum AVPixelFormat pix_fmt)
304
0
{
305
0
    return (unsigned)pix_fmt < FF_ARRAY_ELEMS(legacy_format_entries) ?
306
0
    legacy_format_entries[pix_fmt].is_supported_endianness : 0;
307
0
}
308
309
static void sanitize_fmt(SwsFormat *fmt, const AVPixFmtDescriptor *desc)
310
0
{
311
0
    if (desc->flags & (AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_BAYER)) {
312
        /* RGB-like family */
313
0
        fmt->csp   = AVCOL_SPC_RGB;
314
0
        fmt->range = AVCOL_RANGE_JPEG;
315
0
    } else if (desc->flags & AV_PIX_FMT_FLAG_XYZ) {
316
0
        fmt->csp   = AVCOL_SPC_UNSPECIFIED;
317
0
        fmt->color = (SwsColor) {
318
0
            .prim = AVCOL_PRI_BT709, /* swscale currently hard-codes this XYZ matrix */
319
0
            .trc  = AVCOL_TRC_SMPTE428,
320
0
        };
321
0
    } else if (desc->nb_components < 3) {
322
        /* Grayscale formats */
323
0
        fmt->color.prim = AVCOL_PRI_UNSPECIFIED;
324
0
        fmt->csp        = AVCOL_SPC_UNSPECIFIED;
325
0
        if (desc->flags & AV_PIX_FMT_FLAG_FLOAT)
326
0
            fmt->range = AVCOL_RANGE_UNSPECIFIED;
327
0
        else
328
0
            fmt->range = AVCOL_RANGE_JPEG; // FIXME: this restriction should be lifted
329
0
    }
330
331
0
    switch (av_pix_fmt_desc_get_id(desc)) {
332
0
    case AV_PIX_FMT_YUVJ420P:
333
0
    case AV_PIX_FMT_YUVJ411P:
334
0
    case AV_PIX_FMT_YUVJ422P:
335
0
    case AV_PIX_FMT_YUVJ444P:
336
0
    case AV_PIX_FMT_YUVJ440P:
337
0
        fmt->range = AVCOL_RANGE_JPEG;
338
0
        break;
339
0
    }
340
341
0
    if (!desc->log2_chroma_w && !desc->log2_chroma_h)
342
0
        fmt->loc = AVCHROMA_LOC_UNSPECIFIED;
343
0
}
344
345
/**
346
 * This function also sanitizes and strips the input data, removing irrelevant
347
 * fields for certain formats.
348
 */
349
SwsFormat ff_fmt_from_frame(const AVFrame *frame, int field)
350
0
{
351
0
    const AVColorPrimariesDesc *primaries;
352
0
    AVFrameSideData *sd;
353
354
0
    enum AVPixelFormat format = frame->format;
355
0
    enum AVPixelFormat hw_format = AV_PIX_FMT_NONE;
356
357
0
#if CONFIG_UNSTABLE
358
0
    if (frame->hw_frames_ctx) {
359
0
        AVHWFramesContext *hwfc = (AVHWFramesContext *)frame->hw_frames_ctx->data;
360
0
        hw_format = frame->format;
361
0
        format = hwfc->sw_format;
362
0
    }
363
0
#endif
364
365
0
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
366
367
0
    SwsFormat fmt = {
368
0
        .width     = frame->width,
369
0
        .height    = frame->height,
370
0
        .format    = format,
371
0
        .hw_format = hw_format,
372
0
        .range     = frame->color_range,
373
0
        .csp       = frame->colorspace,
374
0
        .loc       = frame->chroma_location,
375
0
        .desc      = desc,
376
0
        .color = {
377
0
            .prim = frame->color_primaries,
378
0
            .trc  = frame->color_trc,
379
0
        },
380
0
    };
381
382
0
    av_assert1(fmt.width > 0);
383
0
    av_assert1(fmt.height > 0);
384
0
    av_assert1(fmt.format != AV_PIX_FMT_NONE);
385
0
    av_assert0(desc);
386
0
    sanitize_fmt(&fmt, desc);
387
388
0
    if (frame->flags & AV_FRAME_FLAG_INTERLACED) {
389
0
        fmt.height = (fmt.height + (field == FIELD_TOP)) >> 1;
390
0
        fmt.interlaced = 1;
391
0
    }
392
393
    /* Set luminance and gamut information */
394
0
    fmt.color.min_luma = av_make_q(0, 1);
395
0
    switch (fmt.color.trc) {
396
0
    case AVCOL_TRC_SMPTE2084:
397
0
        fmt.color.max_luma = av_make_q(10000, 1); break;
398
0
    case AVCOL_TRC_ARIB_STD_B67:
399
0
        fmt.color.max_luma = av_make_q( 1000, 1); break; /* HLG reference display */
400
0
    default:
401
0
        fmt.color.max_luma = av_make_q(  203, 1); break; /* SDR reference brightness */
402
0
    }
403
404
0
    primaries = av_csp_primaries_desc_from_id(fmt.color.prim);
405
0
    if (primaries)
406
0
        fmt.color.gamut = primaries->prim;
407
408
0
    if ((sd = av_frame_get_side_data(frame, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA))) {
409
0
        const AVMasteringDisplayMetadata *mdm = (const AVMasteringDisplayMetadata *) sd->data;
410
0
        if (mdm->has_luminance) {
411
0
            fmt.color.min_luma = mdm->min_luminance;
412
0
            fmt.color.max_luma = mdm->max_luminance;
413
0
        }
414
415
0
        if (mdm->has_primaries) {
416
            /* Ignore mastering display white point as it has no bearance on
417
             * the underlying content */
418
0
            fmt.color.gamut.r.x = mdm->display_primaries[0][0];
419
0
            fmt.color.gamut.r.y = mdm->display_primaries[0][1];
420
0
            fmt.color.gamut.g.x = mdm->display_primaries[1][0];
421
0
            fmt.color.gamut.g.y = mdm->display_primaries[1][1];
422
0
            fmt.color.gamut.b.x = mdm->display_primaries[2][0];
423
0
            fmt.color.gamut.b.y = mdm->display_primaries[2][1];
424
0
        }
425
0
    }
426
427
0
    if ((sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DYNAMIC_HDR_PLUS))) {
428
0
        const AVDynamicHDRPlus *dhp = (const AVDynamicHDRPlus *) sd->data;
429
0
        const AVHDRPlusColorTransformParams *pars = &dhp->params[0];
430
0
        const AVRational nits = av_make_q(10000, 1);
431
0
        AVRational maxrgb = pars->maxscl[0];
432
433
0
        if (!dhp->num_windows || dhp->application_version > 1)
434
0
            goto skip_hdr10;
435
436
        /* Maximum of MaxSCL components */
437
0
        if (av_cmp_q(pars->maxscl[1], maxrgb) > 0)
438
0
            maxrgb = pars->maxscl[1];
439
0
        if (av_cmp_q(pars->maxscl[2], maxrgb) > 0)
440
0
            maxrgb = pars->maxscl[2];
441
442
0
        if (maxrgb.num > 0) {
443
            /* Estimate true luminance from MaxSCL */
444
0
            const AVLumaCoefficients *luma = av_csp_luma_coeffs_from_avcsp(fmt.csp);
445
0
            if (!luma)
446
0
                goto skip_hdr10;
447
0
            fmt.color.frame_peak = av_add_q(av_mul_q(luma->cr, pars->maxscl[0]),
448
0
                                   av_add_q(av_mul_q(luma->cg, pars->maxscl[1]),
449
0
                                            av_mul_q(luma->cb, pars->maxscl[2])));
450
            /* Scale the scene average brightness by the ratio between the
451
             * maximum luminance and the MaxRGB values */
452
0
            fmt.color.frame_avg = av_mul_q(pars->average_maxrgb,
453
0
                                           av_div_q(fmt.color.frame_peak, maxrgb));
454
0
        } else {
455
            /**
456
             * Calculate largest value from histogram to use as fallback for
457
             * clips with missing MaxSCL information. Note that this may end
458
             * up picking the "reserved" value at the 5% percentile, which in
459
             * practice appears to track the brightest pixel in the scene.
460
             */
461
0
            for (int i = 0; i < pars->num_distribution_maxrgb_percentiles; i++) {
462
0
                const AVRational pct = pars->distribution_maxrgb[i].percentile;
463
0
                if (av_cmp_q(pct, maxrgb) > 0)
464
0
                    maxrgb = pct;
465
0
                fmt.color.frame_peak = maxrgb;
466
0
                fmt.color.frame_avg  = pars->average_maxrgb;
467
0
            }
468
0
        }
469
470
        /* Rescale to nits */
471
0
        fmt.color.frame_peak = av_mul_q(nits, fmt.color.frame_peak);
472
0
        fmt.color.frame_avg  = av_mul_q(nits, fmt.color.frame_avg);
473
0
    }
474
0
skip_hdr10:
475
476
    /* PQ is always scaled down to absolute zero, so ignore mastering metadata */
477
0
    if (fmt.color.trc == AVCOL_TRC_SMPTE2084)
478
0
        fmt.color.min_luma = av_make_q(0, 1);
479
480
0
    return fmt;
481
0
}
482
483
void ff_fmt_from_pixfmt(enum AVPixelFormat pixfmt, SwsFormat *fmt)
484
0
{
485
0
    ff_fmt_clear(fmt);
486
0
    fmt->format = pixfmt;
487
0
    fmt->desc = av_pix_fmt_desc_get(pixfmt);
488
0
    sanitize_fmt(fmt, fmt->desc);
489
0
}
490
491
static int infer_prim_ref(SwsColor *csp, const SwsColor *ref)
492
0
{
493
0
    if (csp->prim != AVCOL_PRI_UNSPECIFIED)
494
0
        return 0;
495
496
    /* Reuse the reference gamut only for "safe", similar primaries */
497
0
    switch (ref->prim) {
498
0
    case AVCOL_PRI_BT709:
499
0
    case AVCOL_PRI_BT470M:
500
0
    case AVCOL_PRI_BT470BG:
501
0
    case AVCOL_PRI_SMPTE170M:
502
0
    case AVCOL_PRI_SMPTE240M:
503
0
        csp->prim  = ref->prim;
504
0
        csp->gamut = ref->gamut;
505
0
        break;
506
0
    default:
507
0
        csp->prim  = AVCOL_PRI_BT709;
508
0
        csp->gamut = av_csp_primaries_desc_from_id(csp->prim)->prim;
509
0
        break;
510
0
    }
511
512
0
    return 1;
513
0
}
514
515
static int infer_trc_ref(SwsColor *csp, const SwsColor *ref)
516
0
{
517
0
    if (csp->trc != AVCOL_TRC_UNSPECIFIED)
518
0
        return 0;
519
520
    /* Pick a suitable SDR transfer function, to try and minimize conversions */
521
0
    switch (ref->trc) {
522
0
    case AVCOL_TRC_UNSPECIFIED:
523
    /* HDR curves, never default to these */
524
0
    case AVCOL_TRC_SMPTE2084:
525
0
    case AVCOL_TRC_ARIB_STD_B67:
526
0
        csp->trc = AVCOL_TRC_BT709;
527
0
        csp->min_luma = av_make_q(0, 1);
528
0
        csp->max_luma = av_make_q(203, 1);
529
0
        break;
530
0
    default:
531
0
        csp->trc = ref->trc;
532
0
        csp->min_luma = ref->min_luma;
533
0
        csp->max_luma = ref->max_luma;
534
0
        break;
535
0
    }
536
537
0
    return 1;
538
0
}
539
540
bool ff_infer_colors(SwsColor *src, SwsColor *dst)
541
0
{
542
0
    int incomplete = 0;
543
544
0
    incomplete |= infer_prim_ref(dst, src);
545
0
    incomplete |= infer_prim_ref(src, dst);
546
0
    av_assert0(src->prim != AVCOL_PRI_UNSPECIFIED);
547
0
    av_assert0(dst->prim != AVCOL_PRI_UNSPECIFIED);
548
549
0
    incomplete |= infer_trc_ref(dst, src);
550
0
    incomplete |= infer_trc_ref(src, dst);
551
0
    av_assert0(src->trc != AVCOL_TRC_UNSPECIFIED);
552
0
    av_assert0(dst->trc != AVCOL_TRC_UNSPECIFIED);
553
554
0
    return incomplete;
555
0
}
556
557
int sws_test_format(enum AVPixelFormat format, int output)
558
0
{
559
0
    return output ? sws_isSupportedOutput(format) : sws_isSupportedInput(format);
560
0
}
561
562
int sws_test_hw_format(enum AVPixelFormat format)
563
0
{
564
0
    switch (format) {
565
0
    case AV_PIX_FMT_NONE: return 1;
566
#if CONFIG_VULKAN
567
    case AV_PIX_FMT_VULKAN: return 1;
568
#endif
569
0
    default: return 0;
570
0
    }
571
0
}
572
573
int sws_test_colorspace(enum AVColorSpace csp, int output)
574
0
{
575
0
    switch (csp) {
576
0
    case AVCOL_SPC_UNSPECIFIED:
577
0
    case AVCOL_SPC_RGB:
578
0
    case AVCOL_SPC_BT709:
579
0
    case AVCOL_SPC_BT470BG:
580
0
    case AVCOL_SPC_SMPTE170M:
581
0
    case AVCOL_SPC_FCC:
582
0
    case AVCOL_SPC_SMPTE240M:
583
0
    case AVCOL_SPC_BT2020_NCL:
584
0
        return 1;
585
0
    default:
586
0
        return 0;
587
0
    }
588
0
}
589
590
int sws_test_primaries(enum AVColorPrimaries prim, int output)
591
0
{
592
0
    return ((prim > AVCOL_PRI_RESERVED0 && prim < AVCOL_PRI_NB) ||
593
0
            (prim >= AVCOL_PRI_EXT_BASE && prim < AVCOL_PRI_EXT_NB)) &&
594
0
           prim != AVCOL_PRI_RESERVED;
595
0
}
596
597
int sws_test_transfer(enum AVColorTransferCharacteristic trc, int output)
598
0
{
599
0
    av_csp_eotf_function eotf = output ? av_csp_itu_eotf_inv(trc)
600
0
                                       : av_csp_itu_eotf(trc);
601
0
    return trc == AVCOL_TRC_UNSPECIFIED || eotf != NULL;
602
0
}
603
604
static int test_range(enum AVColorRange range)
605
0
{
606
0
    return (unsigned)range < AVCOL_RANGE_NB;
607
0
}
608
609
static int test_loc(enum AVChromaLocation loc)
610
0
{
611
0
    return (unsigned)loc < AVCHROMA_LOC_NB;
612
0
}
613
614
int ff_test_fmt(const SwsFormat *fmt, int output)
615
0
{
616
0
    return fmt->width > 0 && fmt->height > 0            &&
617
0
           sws_test_format    (fmt->format,     output) &&
618
0
           sws_test_colorspace(fmt->csp,        output) &&
619
0
           sws_test_primaries (fmt->color.prim, output) &&
620
0
           sws_test_transfer  (fmt->color.trc,  output) &&
621
0
           sws_test_hw_format (fmt->hw_format)          &&
622
0
           test_range         (fmt->range)              &&
623
0
           test_loc           (fmt->loc);
624
0
}
625
626
int sws_test_frame(const AVFrame *frame, int output)
627
0
{
628
0
    for (int field = 0; field < 2; field++) {
629
0
        const SwsFormat fmt = ff_fmt_from_frame(frame, field);
630
0
        if (!ff_test_fmt(&fmt, output))
631
0
            return 0;
632
0
        if (!fmt.interlaced)
633
0
            break;
634
0
    }
635
636
0
    return 1;
637
0
}
638
639
int sws_is_noop(const AVFrame *dst, const AVFrame *src)
640
0
{
641
0
    for (int field = 0; field < 2; field++) {
642
0
        SwsFormat dst_fmt = ff_fmt_from_frame(dst, field);
643
0
        SwsFormat src_fmt = ff_fmt_from_frame(src, field);
644
0
        if (!ff_fmt_equal(&dst_fmt, &src_fmt))
645
0
            return 0;
646
0
        if (!dst_fmt.interlaced)
647
0
            break;
648
0
    }
649
650
0
    return 1;
651
0
}
652
653
void ff_sws_frame_from_avframe(SwsFrame *dst, const AVFrame *src)
654
0
{
655
0
    dst->format  = src->format;
656
0
    dst->width   = src->width;
657
0
    dst->height  = src->height;
658
0
    dst->avframe = src;
659
0
    for (int i = 0; i < FF_ARRAY_ELEMS(dst->data); i++) {
660
0
        dst->data[i]     = src->data[i];
661
0
        dst->linesize[i] = src->linesize[i];
662
0
    }
663
0
}
664
665
#if CONFIG_UNSTABLE
666
667
/* Returns the type suitable for a pixel after fully decoding/unpacking it */
668
static SwsPixelType fmt_pixel_type(enum AVPixelFormat fmt)
669
0
{
670
0
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
671
0
    const int bits = FFALIGN(desc->comp[0].depth, 8);
672
0
    if (desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
673
0
        switch (bits) {
674
0
        case 32: return SWS_PIXEL_F32;
675
        /* TODO: no support for 16-bit float yet */
676
0
        }
677
0
    } else {
678
0
        switch (bits) {
679
0
        case  8: return SWS_PIXEL_U8;
680
0
        case 16: return SWS_PIXEL_U16;
681
        /* TODO: AVRational cannot represent UINT32_MAX */
682
0
        }
683
0
    }
684
685
0
    return SWS_PIXEL_NONE;
686
0
}
687
688
/* A regular format is defined as any format that contains only a single
689
 * component per elementary data type (i.e. no sub-byte pack/unpack needed),
690
 * and whose components map 1:1 onto elementary data units */
691
static int is_regular_fmt(enum AVPixelFormat fmt)
692
0
{
693
0
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
694
0
    if (desc->flags & (AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_BAYER))
695
0
        return 0; /* no 1:1 correspondence between components and data units */
696
0
    if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM))
697
0
        return 0; /* bitstream formats are packed by definition */
698
0
    if ((desc->flags & AV_PIX_FMT_FLAG_PLANAR) || desc->nb_components == 1)
699
0
        return 1; /* planar formats are regular by definition */
700
701
0
    const int step = desc->comp[0].step;
702
0
    int total_bits = 0;
703
704
0
    for (int i = 0; i < desc->nb_components; i++) {
705
0
        if (desc->comp[i].shift || desc->comp[i].step != step)
706
0
            return 0; /* irregular/packed format */
707
0
        total_bits += desc->comp[i].depth;
708
0
    }
709
710
    /* Exclude formats with missing components like RGB0, 0RGB, etc. */
711
0
    return total_bits == step * 8;
712
0
}
713
714
typedef struct FmtInfo {
715
    SwsReadWriteOp rw;
716
    SwsSwizzleOp   swizzle;
717
    SwsPackOp      pack;
718
    int            shift;
719
} FmtInfo;
720
721
0
#define BITSTREAM_FMT(SWIZ, FRAC, PACKED, ...) (FmtInfo) {      \
722
0
    .rw = { .elems = 1, .frac = FRAC, .packed = PACKED },       \
723
0
    .swizzle = SWIZ,                                            \
724
0
    __VA_ARGS__                                                 \
725
0
}
726
727
0
#define SUBPACKED_FMT(SWIZ, ...) (FmtInfo) {                    \
728
0
    .rw = { .elems = 1, .packed = true },                       \
729
0
    .swizzle = SWIZ,                                            \
730
0
    .pack.pattern = {__VA_ARGS__},                              \
731
0
}
732
733
0
#define PACKED_FMT(SWIZ, N, ...) (FmtInfo) {                    \
734
0
    .rw = { .elems = N, .packed = (N) > 1 },                    \
735
0
    .swizzle = SWIZ,                                            \
736
0
    __VA_ARGS__                                                 \
737
0
}
738
739
#define RGBA SWS_SWIZZLE(0, 1, 2, 3)
740
#define BGRA SWS_SWIZZLE(2, 1, 0, 3)
741
#define ARGB SWS_SWIZZLE(3, 0, 1, 2)
742
#define ABGR SWS_SWIZZLE(3, 2, 1, 0)
743
#define AVYU SWS_SWIZZLE(3, 2, 0, 1)
744
#define VYUA SWS_SWIZZLE(2, 0, 1, 3)
745
#define UYVA SWS_SWIZZLE(1, 0, 2, 3)
746
#define VUYA BGRA
747
748
static FmtInfo fmt_info_irregular(enum AVPixelFormat fmt)
749
0
{
750
0
    switch (fmt) {
751
    /* Bitstream formats */
752
0
    case AV_PIX_FMT_MONOWHITE:
753
0
    case AV_PIX_FMT_MONOBLACK:
754
0
        return BITSTREAM_FMT(RGBA, 3, false);
755
0
    case AV_PIX_FMT_RGB4: return BITSTREAM_FMT(RGBA, 1, true, .pack = {{ 1, 2, 1 }});
756
0
    case AV_PIX_FMT_BGR4: return BITSTREAM_FMT(BGRA, 1, true, .pack = {{ 1, 2, 1 }});
757
758
    /* Sub-packed 8-bit aligned formats */
759
0
    case AV_PIX_FMT_RGB4_BYTE:  return SUBPACKED_FMT(RGBA, 1, 2, 1);
760
0
    case AV_PIX_FMT_BGR4_BYTE:  return SUBPACKED_FMT(BGRA, 1, 2, 1);
761
0
    case AV_PIX_FMT_RGB8:       return SUBPACKED_FMT(RGBA, 3, 3, 2);
762
0
    case AV_PIX_FMT_BGR8:       return SUBPACKED_FMT(BGRA, 2, 3, 3);
763
764
    /* Sub-packed 16-bit aligned formats */
765
0
    case AV_PIX_FMT_RGB565LE:
766
0
    case AV_PIX_FMT_RGB565BE:
767
0
        return SUBPACKED_FMT(RGBA, 5, 6, 5);
768
0
    case AV_PIX_FMT_BGR565LE:
769
0
    case AV_PIX_FMT_BGR565BE:
770
0
        return SUBPACKED_FMT(BGRA, 5, 6, 5);
771
0
    case AV_PIX_FMT_RGB555LE:
772
0
    case AV_PIX_FMT_RGB555BE:
773
0
        return SUBPACKED_FMT(RGBA, 5, 5, 5);
774
0
    case AV_PIX_FMT_BGR555LE:
775
0
    case AV_PIX_FMT_BGR555BE:
776
0
        return SUBPACKED_FMT(BGRA, 5, 5, 5);
777
0
    case AV_PIX_FMT_RGB444LE:
778
0
    case AV_PIX_FMT_RGB444BE:
779
0
        return SUBPACKED_FMT(RGBA, 4, 4, 4);
780
0
    case AV_PIX_FMT_BGR444LE:
781
0
    case AV_PIX_FMT_BGR444BE:
782
0
        return SUBPACKED_FMT(BGRA, 4, 4, 4);
783
784
    /* Sub-packed 32-bit aligned formats */
785
0
    case AV_PIX_FMT_X2RGB10LE:
786
0
    case AV_PIX_FMT_X2RGB10BE:
787
0
        return SUBPACKED_FMT(ARGB, 2, 10, 10, 10);
788
0
    case AV_PIX_FMT_X2BGR10LE:
789
0
    case AV_PIX_FMT_X2BGR10BE:
790
0
        return SUBPACKED_FMT(ABGR, 2, 10, 10, 10);
791
0
    case AV_PIX_FMT_XV30LE:
792
0
    case AV_PIX_FMT_XV30BE:
793
0
        return SUBPACKED_FMT(AVYU, 2, 10, 10, 10);
794
0
    case AV_PIX_FMT_V30XLE:
795
0
    case AV_PIX_FMT_V30XBE:
796
0
        return SUBPACKED_FMT(VYUA, 10, 10, 10, 2);
797
798
    /* 3-component formats with extra padding */
799
0
    case AV_PIX_FMT_RGB0:   return PACKED_FMT(RGBA, 4);
800
0
    case AV_PIX_FMT_BGR0:   return PACKED_FMT(BGRA, 4);
801
0
    case AV_PIX_FMT_0RGB:   return PACKED_FMT(ARGB, 4);
802
0
    case AV_PIX_FMT_0BGR:   return PACKED_FMT(ABGR, 4);
803
0
    case AV_PIX_FMT_VUYX:   return PACKED_FMT(VUYA, 4);
804
0
    case AV_PIX_FMT_XV36LE:
805
0
    case AV_PIX_FMT_XV36BE:
806
0
        return PACKED_FMT(UYVA, 4, .shift = 4);
807
0
    case AV_PIX_FMT_XV48LE:
808
0
    case AV_PIX_FMT_XV48BE:
809
0
        return PACKED_FMT(UYVA, 4);
810
0
    }
811
812
0
    return (FmtInfo) {0};
813
0
}
814
815
struct comp {
816
    int index;
817
    int plane;
818
    int offset;
819
};
820
821
/* Compare by (plane, offset) */
822
0
static int cmp_comp(const void *a, const void *b) {
823
0
    const struct comp *ca = a;
824
0
    const struct comp *cb = b;
825
0
    if (ca->plane != cb->plane)
826
0
        return ca->plane - cb->plane;
827
0
    return ca->offset - cb->offset;
828
0
}
829
830
static int fmt_analyze_regular(const AVPixFmtDescriptor *desc, SwsReadWriteOp *rw_op,
831
                               SwsSwizzleOp *swizzle, SwsShiftOp *shift)
832
0
{
833
0
    if (desc->nb_components == 2) {
834
        /* YA formats */
835
0
        *swizzle = SWS_SWIZZLE(0, 3, 1, 2);
836
0
    } else {
837
        /* Sort by increasing component order */
838
0
        struct comp sorted[4] = { {0}, {1}, {2}, {3} };
839
0
        for (int i = 0; i < desc->nb_components; i++) {
840
0
            sorted[i].plane  = desc->comp[i].plane;
841
0
            sorted[i].offset = desc->comp[i].offset;
842
0
        }
843
844
0
        qsort(sorted, desc->nb_components, sizeof(struct comp), cmp_comp);
845
846
0
        SwsSwizzleOp swiz = SWS_SWIZZLE(0, 1, 2, 3);
847
0
        for (int i = 0; i < desc->nb_components; i++)
848
0
            swiz.in[i] = sorted[i].index;
849
0
        *swizzle = swiz;
850
0
    }
851
852
0
    *shift = (SwsShiftOp) { desc->comp[0].shift };
853
0
    *rw_op = (SwsReadWriteOp) {
854
0
        .elems  = desc->nb_components,
855
0
        .packed = desc->nb_components > 1 && !(desc->flags & AV_PIX_FMT_FLAG_PLANAR),
856
0
    };
857
0
    return 0;
858
0
}
859
860
static int fmt_analyze(enum AVPixelFormat fmt, SwsReadWriteOp *rw_op,
861
                       SwsPackOp *pack_op, SwsSwizzleOp *swizzle,
862
                       SwsShiftOp *shift, SwsPixelType *pixel_type,
863
                       SwsPixelType *raw_type)
864
0
{
865
0
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
866
0
    if (!desc)
867
0
        return AVERROR(EINVAL);
868
869
    /* No support for subsampled formats at the moment */
870
0
    if (desc->log2_chroma_w || desc->log2_chroma_h)
871
0
        return AVERROR(ENOTSUP);
872
873
    /* No support for semi-planar formats at the moment */
874
0
    if (desc->flags & AV_PIX_FMT_FLAG_PLANAR &&
875
0
        av_pix_fmt_count_planes(fmt) < desc->nb_components)
876
0
        return AVERROR(ENOTSUP);
877
878
0
    *pixel_type = *raw_type = fmt_pixel_type(fmt);
879
0
    if (!*pixel_type)
880
0
        return AVERROR(ENOTSUP);
881
882
0
    if (is_regular_fmt(fmt)) {
883
0
        *pack_op = (SwsPackOp) {0};
884
0
        return fmt_analyze_regular(desc, rw_op, swizzle, shift);
885
0
    }
886
887
0
    FmtInfo info = fmt_info_irregular(fmt);
888
0
    if (!info.rw.elems)
889
0
        return AVERROR(ENOTSUP);
890
891
0
    *rw_op   = info.rw;
892
0
    *pack_op = info.pack;
893
0
    *swizzle = info.swizzle;
894
0
    *shift   = (SwsShiftOp) { info.shift };
895
896
0
    if (info.pack.pattern[0]) {
897
0
        const int sum = info.pack.pattern[0] + info.pack.pattern[1] +
898
0
                        info.pack.pattern[2] + info.pack.pattern[3];
899
0
        if (sum > 16)
900
0
            *raw_type = SWS_PIXEL_U32;
901
0
        else if (sum > 8)
902
0
            *raw_type = SWS_PIXEL_U16;
903
0
        else
904
0
            *raw_type = SWS_PIXEL_U8;
905
0
    }
906
907
0
    return 0;
908
0
}
909
910
0
static SwsSwizzleOp swizzle_inv(SwsSwizzleOp swiz) {
911
    /* Input[x] =: Output[swizzle.x] */
912
0
    unsigned out[4];
913
0
    out[swiz.x] = 0;
914
0
    out[swiz.y] = 1;
915
0
    out[swiz.z] = 2;
916
0
    out[swiz.w] = 3;
917
0
    return (SwsSwizzleOp) {{ .x = out[0], out[1], out[2], out[3] }};
918
0
}
919
920
/**
921
 * This initializes all absent components explicitly to zero. There is no
922
 * need to worry about the correct neutral value as fmt_decode() will
923
 * implicitly ignore and overwrite absent components in any case. This function
924
 * is just to ensure that we don't operate on undefined memory. In most cases,
925
 * it will end up getting pushed towards the output or optimized away entirely
926
 * by the optimization pass.
927
 */
928
static SwsClearOp fmt_clear(enum AVPixelFormat fmt)
929
0
{
930
0
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
931
0
    const bool has_chroma = desc->nb_components >= 3;
932
0
    const bool has_alpha  = desc->flags & AV_PIX_FMT_FLAG_ALPHA;
933
934
0
    SwsClearOp c = {0};
935
0
    if (!has_chroma) {
936
0
        c.mask |= SWS_COMP(1) | SWS_COMP(2);
937
0
        c.value[1] = c.value[2] = Q0;
938
0
    }
939
940
0
    if (!has_alpha) {
941
0
        c.mask |= SWS_COMP(3);
942
0
        c.value[3] = Q0;
943
0
    }
944
945
0
    return c;
946
0
}
947
948
#if HAVE_BIGENDIAN
949
#  define NATIVE_ENDIAN_FLAG AV_PIX_FMT_FLAG_BE
950
#else
951
0
#  define NATIVE_ENDIAN_FLAG 0
952
#endif
953
954
int ff_sws_decode_pixfmt(SwsOpList *ops, enum AVPixelFormat fmt)
955
0
{
956
0
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
957
0
    SwsPixelType pixel_type, raw_type;
958
0
    SwsReadWriteOp rw_op;
959
0
    SwsSwizzleOp swizzle;
960
0
    SwsPackOp unpack;
961
0
    SwsComps *comps = &ops->comps_src;
962
0
    SwsShiftOp shift;
963
964
0
    RET(fmt_analyze(fmt, &rw_op, &unpack, &swizzle, &shift,
965
0
                    &pixel_type, &raw_type));
966
967
0
    swizzle = swizzle_inv(swizzle);
968
969
    /* Set baseline pixel content flags */
970
0
    const int integer = ff_sws_pixel_type_is_int(raw_type);
971
0
    const int swapped = ff_sws_pixel_type_size(raw_type) > 1 &&
972
0
                        (desc->flags & AV_PIX_FMT_FLAG_BE) != NATIVE_ENDIAN_FLAG;
973
0
    for (int i = 0; i < rw_op.elems; i++) {
974
0
        comps->flags[i] = (integer ? SWS_COMP_EXACT   : 0) |
975
0
                          (swapped ? SWS_COMP_SWAPPED : 0);
976
0
    }
977
978
    /* Generate value range information for simple unpacked formats */
979
0
    if (integer && !unpack.pattern[0]) {
980
        /* YA formats have desc->comp[] in the order {Y, A} instead of the
981
         * canonical order {Y, U, V, A} */
982
0
        const int is_ya = desc->nb_components == 2;
983
0
        for (int c = 0; c < desc->nb_components; c++) {
984
0
            const int bits   = desc->comp[c].depth + shift.amount;
985
0
            const int idx    = swizzle.in[is_ya ? 3 * c : c];
986
0
            comps->min[idx]  = Q0;
987
0
            if (bits < 32) /* FIXME: AVRational is limited to INT_MAX */
988
0
                comps->max[idx] = Q((1ULL << bits) - 1);
989
0
        }
990
0
    }
991
992
    /* TODO: handle subsampled or semipacked input formats */
993
0
    RET(ff_sws_op_list_append(ops, &(SwsOp) {
994
0
        .op    = SWS_OP_READ,
995
0
        .type  = raw_type,
996
0
        .rw    = rw_op,
997
0
    }));
998
999
0
    if (swapped) {
1000
0
        RET(ff_sws_op_list_append(ops, &(SwsOp) {
1001
0
            .op   = SWS_OP_SWAP_BYTES,
1002
0
            .type = raw_type,
1003
0
        }));
1004
0
    }
1005
1006
0
    if (unpack.pattern[0]) {
1007
0
        RET(ff_sws_op_list_append(ops, &(SwsOp) {
1008
0
            .op   = SWS_OP_UNPACK,
1009
0
            .type = raw_type,
1010
0
            .pack = unpack,
1011
0
        }));
1012
1013
0
        RET(ff_sws_op_list_append(ops, &(SwsOp) {
1014
0
            .op   = SWS_OP_CONVERT,
1015
0
            .type = raw_type,
1016
0
            .convert.to = pixel_type,
1017
0
        }));
1018
0
    }
1019
1020
0
    RET(ff_sws_op_list_append(ops, &(SwsOp) {
1021
0
        .op      = SWS_OP_SWIZZLE,
1022
0
        .type    = pixel_type,
1023
0
        .swizzle = swizzle,
1024
0
    }));
1025
1026
0
    if (shift.amount) {
1027
0
        RET(ff_sws_op_list_append(ops, &(SwsOp) {
1028
0
            .op    = SWS_OP_RSHIFT,
1029
0
            .type  = pixel_type,
1030
0
            .shift = shift,
1031
0
        }));
1032
0
    }
1033
1034
0
    RET(ff_sws_op_list_append(ops, &(SwsOp) {
1035
0
        .op    = SWS_OP_CLEAR,
1036
0
        .type  = pixel_type,
1037
0
        .clear = fmt_clear(fmt),
1038
0
    }));
1039
1040
0
    return 0;
1041
0
}
1042
1043
int ff_sws_encode_pixfmt(SwsOpList *ops, enum AVPixelFormat fmt)
1044
0
{
1045
0
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
1046
0
    SwsPixelType pixel_type, raw_type;
1047
0
    SwsReadWriteOp rw_op;
1048
0
    SwsSwizzleOp swizzle;
1049
0
    SwsPackOp pack;
1050
0
    SwsShiftOp shift;
1051
1052
0
    RET(fmt_analyze(fmt, &rw_op, &pack, &swizzle, &shift,
1053
0
                    &pixel_type, &raw_type));
1054
1055
0
    if (shift.amount) {
1056
0
        RET(ff_sws_op_list_append(ops, &(SwsOp) {
1057
0
            .op    = SWS_OP_LSHIFT,
1058
0
            .type  = pixel_type,
1059
0
            .shift = shift,
1060
0
        }));
1061
0
    }
1062
1063
0
    if (rw_op.elems > desc->nb_components) {
1064
        /* Format writes unused alpha channel, clear it explicitly for sanity */
1065
0
        av_assert1(!(desc->flags & AV_PIX_FMT_FLAG_ALPHA));
1066
0
        RET(ff_sws_op_list_append(ops, &(SwsOp) {
1067
0
            .op   = SWS_OP_CLEAR,
1068
0
            .type = pixel_type,
1069
0
            .clear.mask = SWS_COMP(3),
1070
0
            .clear.value[3] = Q0,
1071
0
        }));
1072
0
    }
1073
1074
0
    RET(ff_sws_op_list_append(ops, &(SwsOp) {
1075
0
        .op      = SWS_OP_SWIZZLE,
1076
0
        .type    = pixel_type,
1077
0
        .swizzle = swizzle,
1078
0
    }));
1079
1080
0
    if (pack.pattern[0]) {
1081
0
        RET(ff_sws_op_list_append(ops, &(SwsOp) {
1082
0
            .op   = SWS_OP_CONVERT,
1083
0
            .type = pixel_type,
1084
0
            .convert.to = raw_type,
1085
0
        }));
1086
1087
0
        RET(ff_sws_op_list_append(ops, &(SwsOp) {
1088
0
            .op   = SWS_OP_PACK,
1089
0
            .type = raw_type,
1090
0
            .pack = pack,
1091
0
        }));
1092
0
    }
1093
1094
0
    if (ff_sws_pixel_type_size(raw_type) > 1 &&
1095
0
        (desc->flags & AV_PIX_FMT_FLAG_BE) != NATIVE_ENDIAN_FLAG) {
1096
0
        RET(ff_sws_op_list_append(ops, &(SwsOp) {
1097
0
            .op   = SWS_OP_SWAP_BYTES,
1098
0
            .type = raw_type,
1099
0
        }));
1100
0
    }
1101
1102
0
    RET(ff_sws_op_list_append(ops, &(SwsOp) {
1103
0
        .op   = SWS_OP_WRITE,
1104
0
        .type = raw_type,
1105
0
        .rw   = rw_op,
1106
0
    }));
1107
1108
0
    return 0;
1109
0
}
1110
1111
static inline AVRational av_neg_q(AVRational x)
1112
0
{
1113
0
    return (AVRational) { -x.num, x.den };
1114
0
}
1115
1116
static SwsLinearOp fmt_encode_range(const SwsFormat *fmt, bool *incomplete)
1117
0
{
1118
0
    SwsLinearOp c = { .m = {
1119
0
        { Q1, Q0, Q0, Q0, Q0 },
1120
0
        { Q0, Q1, Q0, Q0, Q0 },
1121
0
        { Q0, Q0, Q1, Q0, Q0 },
1122
0
        { Q0, Q0, Q0, Q1, Q0 },
1123
0
    }};
1124
1125
0
    const int depth0 = fmt->desc->comp[0].depth;
1126
0
    const int depth1 = fmt->desc->comp[1].depth;
1127
0
    const int depth2 = fmt->desc->comp[2].depth;
1128
0
    const int depth3 = fmt->desc->comp[3].depth;
1129
1130
0
    if (fmt->desc->flags & AV_PIX_FMT_FLAG_FLOAT)
1131
0
        return c; /* floats are directly output as-is */
1132
1133
0
    av_assert0(depth0 < 32 && depth1 < 32 && depth2 < 32 && depth3 < 32);
1134
0
    if (fmt->csp == AVCOL_SPC_RGB || (fmt->desc->flags & AV_PIX_FMT_FLAG_XYZ)) {
1135
0
        c.m[0][0] = Q((1 << depth0) - 1);
1136
0
        c.m[1][1] = Q((1 << depth1) - 1);
1137
0
        c.m[2][2] = Q((1 << depth2) - 1);
1138
0
    } else if (fmt->range == AVCOL_RANGE_JPEG) {
1139
        /* Full range YUV */
1140
0
        c.m[0][0] = Q((1 << depth0) - 1);
1141
0
        if (fmt->desc->nb_components >= 3) {
1142
            /* This follows the ITU-R convention, which is slightly different
1143
             * from the JFIF convention. */
1144
0
            c.m[1][1] = Q((1 << depth1) - 1);
1145
0
            c.m[2][2] = Q((1 << depth2) - 1);
1146
0
            c.m[1][4] = Q(1 << (depth1 - 1));
1147
0
            c.m[2][4] = Q(1 << (depth2 - 1));
1148
0
        }
1149
0
    } else {
1150
        /* Limited range YUV */
1151
0
        if (fmt->range == AVCOL_RANGE_UNSPECIFIED)
1152
0
            *incomplete = true;
1153
0
        c.m[0][0] = Q(219 << (depth0 - 8));
1154
0
        c.m[0][4] = Q( 16 << (depth0 - 8));
1155
0
        if (fmt->desc->nb_components >= 3) {
1156
0
            c.m[1][1] = Q(224 << (depth1 - 8));
1157
0
            c.m[2][2] = Q(224 << (depth2 - 8));
1158
0
            c.m[1][4] = Q(128 << (depth1 - 8));
1159
0
            c.m[2][4] = Q(128 << (depth2 - 8));
1160
0
        }
1161
0
    }
1162
1163
0
    if (fmt->desc->flags & AV_PIX_FMT_FLAG_ALPHA) {
1164
0
        const bool is_ya = fmt->desc->nb_components == 2;
1165
0
        c.m[3][3] = Q((1 << (is_ya ? depth1 : depth3)) - 1);
1166
0
    }
1167
1168
0
    if (fmt->format == AV_PIX_FMT_MONOWHITE) {
1169
        /* This format is inverted, 0 = white, 1 = black */
1170
0
        c.m[0][4] = av_add_q(c.m[0][4], c.m[0][0]);
1171
0
        c.m[0][0] = av_neg_q(c.m[0][0]);
1172
0
    }
1173
1174
0
    c.mask = ff_sws_linear_mask(c);
1175
0
    return c;
1176
0
}
1177
1178
static SwsLinearOp fmt_decode_range(const SwsFormat *fmt, bool *incomplete)
1179
0
{
1180
0
    SwsLinearOp c = fmt_encode_range(fmt, incomplete);
1181
1182
    /* Invert main diagonal + offset: x = s * y + k  ==>  y = (x - k) / s */
1183
0
    for (int i = 0; i < 4; i++) {
1184
0
        av_assert1(c.m[i][i].num);
1185
0
        c.m[i][i] = av_inv_q(c.m[i][i]);
1186
0
        c.m[i][4] = av_mul_q(c.m[i][4], av_neg_q(c.m[i][i]));
1187
0
    }
1188
1189
    /* Explicitly initialize alpha for sanity */
1190
0
    if (!(fmt->desc->flags & AV_PIX_FMT_FLAG_ALPHA))
1191
0
        c.m[3][4] = Q1;
1192
1193
0
    c.mask = ff_sws_linear_mask(c);
1194
0
    return c;
1195
0
}
1196
1197
static AVRational *generate_bayer_matrix(const int size_log2)
1198
0
{
1199
0
    const int size = 1 << size_log2;
1200
0
    const int num_entries = size * size;
1201
0
    AVRational *m = av_refstruct_allocz(sizeof(*m) * num_entries);
1202
0
    av_assert1(size_log2 < 16);
1203
0
    if (!m)
1204
0
        return NULL;
1205
1206
    /* Start with a 1x1 matrix */
1207
0
    m[0] = Q0;
1208
1209
    /* Generate three copies of the current, appropriately scaled and offset */
1210
0
    for (int sz = 1; sz < size; sz <<= 1) {
1211
0
        const int den = 4 * sz * sz;
1212
0
        for (int y = 0; y < sz; y++) {
1213
0
            for (int x = 0; x < sz; x++) {
1214
0
                const AVRational cur = m[y * size + x];
1215
0
                m[(y + sz) * size + x + sz] = av_add_q(cur, av_make_q(1, den));
1216
0
                m[(y     ) * size + x + sz] = av_add_q(cur, av_make_q(2, den));
1217
0
                m[(y + sz) * size + x     ] = av_add_q(cur, av_make_q(3, den));
1218
0
            }
1219
0
        }
1220
0
    }
1221
1222
    /**
1223
     * To correctly round, we need to evenly distribute the result on [0, 1),
1224
     * giving an average value of 1/2.
1225
     *
1226
     * After the above construction, we have a matrix with average value:
1227
     *   [ 0/N + 1/N + 2/N + ... (N-1)/N ] / N = (N-1)/(2N)
1228
     * where N = size * size is the total number of entries.
1229
     *
1230
     * To make the average value equal to 1/2 = N/(2N), add a bias of 1/(2N).
1231
     */
1232
0
    for (int i = 0; i < num_entries; i++)
1233
0
        m[i] = av_add_q(m[i], av_make_q(1, 2 * num_entries));
1234
1235
0
    return m;
1236
0
}
1237
1238
static bool trc_is_hdr(enum AVColorTransferCharacteristic trc)
1239
0
{
1240
0
    static_assert(AVCOL_TRC_NB == 19, "Update this list when adding TRCs");
1241
0
    static_assert(AVCOL_TRC_EXT_NB == 257, "Update this list when adding TRCs");
1242
0
    switch (trc) {
1243
0
    case AVCOL_TRC_LOG:
1244
0
    case AVCOL_TRC_LOG_SQRT:
1245
0
    case AVCOL_TRC_V_LOG:
1246
0
    case AVCOL_TRC_SMPTEST2084:
1247
0
    case AVCOL_TRC_ARIB_STD_B67:
1248
0
        return true;
1249
0
    default:
1250
0
        return false;
1251
0
    }
1252
0
}
1253
1254
static int fmt_dither(SwsContext *ctx, SwsOpList *ops,
1255
                      const SwsPixelType type,
1256
                      const SwsFormat *src, const SwsFormat *dst)
1257
0
{
1258
0
    SwsDither mode = ctx->dither;
1259
0
    SwsDitherOp dither;
1260
0
    const int bpc = dst->desc->comp[0].depth;
1261
1262
0
    if (mode == SWS_DITHER_AUTO) {
1263
        /* Visual threshold of perception: 12 bits for SDR, 14 bits for HDR */
1264
0
        const int jnd_bits = trc_is_hdr(dst->color.trc) ? 14 : 12;
1265
0
        mode = bpc >= jnd_bits ? SWS_DITHER_NONE : SWS_DITHER_BAYER;
1266
0
    }
1267
1268
0
    switch (mode) {
1269
0
    case SWS_DITHER_NONE:
1270
0
        if (ctx->flags & SWS_ACCURATE_RND) {
1271
            /* Add constant 0.5 for correct rounding */
1272
0
            AVRational *bias = av_refstruct_allocz(sizeof(*bias));
1273
0
            if (!bias)
1274
0
                return AVERROR(ENOMEM);
1275
0
            *bias = (AVRational) {1, 2};
1276
0
            return ff_sws_op_list_append(ops, &(SwsOp) {
1277
0
                .op   = SWS_OP_DITHER,
1278
0
                .type = type,
1279
0
                .dither.matrix = bias,
1280
0
                .dither.min = *bias,
1281
0
                .dither.max = *bias,
1282
0
            });
1283
0
        } else {
1284
0
            return 0; /* No-op */
1285
0
        }
1286
0
    case SWS_DITHER_BAYER:
1287
        /* Hardcode 16x16 matrix for now; in theory we could adjust this
1288
         * based on the expected level of precision in the output, since lower
1289
         * bit depth outputs can suffice with smaller dither matrices; however
1290
         * in practice we probably want to use error diffusion for such low bit
1291
         * depths anyway */
1292
0
        dither.size_log2 = 4;
1293
0
        dither.matrix = generate_bayer_matrix(dither.size_log2);
1294
0
        if (!dither.matrix)
1295
0
            return AVERROR(ENOMEM);
1296
1297
0
        const int size = 1 << dither.size_log2;
1298
0
        dither.min = dither.max = dither.matrix[0];
1299
0
        for (int i = 1; i < size * size; i++) {
1300
0
            if (av_cmp_q(dither.min, dither.matrix[i]) > 0)
1301
0
                dither.min = dither.matrix[i];
1302
0
            if (av_cmp_q(dither.matrix[i], dither.max) > 0)
1303
0
                dither.max = dither.matrix[i];
1304
0
        }
1305
1306
        /* Brute-forced offsets; minimizes quantization error across a 16x16
1307
         * bayer dither pattern for standard RGBA and YUVA pixel formats */
1308
0
        const int offsets_16x16[4] = {0, 3, 2, 5};
1309
0
        for (int i = 0; i < 4; i++) {
1310
0
            av_assert0(offsets_16x16[i] <= INT8_MAX);
1311
0
            dither.y_offset[i] = offsets_16x16[i];
1312
0
        }
1313
1314
0
        if (src->desc->nb_components < 3 && bpc >= 8) {
1315
            /**
1316
             * For high-bit-depth sources without chroma, use same matrix
1317
             * offset for all color channels. This prevents introducing color
1318
             * noise in grayscale images; and also allows optimizing the dither
1319
             * operation. Skipped for low bit depth (<8 bpc) as the loss in
1320
             * PSNR, from the inability to diffuse error among all three
1321
             * channels, can be substantial.
1322
             *
1323
             * This shifts: { X, Y, Z, W } -> { X, X, X, Y }
1324
             */
1325
0
            dither.y_offset[3] = dither.y_offset[1];
1326
0
            dither.y_offset[1] = dither.y_offset[2] = dither.y_offset[0];
1327
0
        }
1328
1329
0
        return ff_sws_op_list_append(ops, &(SwsOp) {
1330
0
            .op     = SWS_OP_DITHER,
1331
0
            .type   = type,
1332
0
            .dither = dither,
1333
0
        });
1334
0
    case SWS_DITHER_ED:
1335
0
    case SWS_DITHER_A_DITHER:
1336
0
    case SWS_DITHER_X_DITHER:
1337
0
        return AVERROR(ENOTSUP);
1338
1339
0
    case SWS_DITHER_NB:
1340
0
        break;
1341
0
    }
1342
1343
0
    av_unreachable("Invalid dither mode");
1344
0
    return AVERROR(EINVAL);
1345
0
}
1346
1347
static inline SwsLinearOp
1348
linear_mat3(const AVRational m00, const AVRational m01, const AVRational m02,
1349
            const AVRational m10, const AVRational m11, const AVRational m12,
1350
            const AVRational m20, const AVRational m21, const AVRational m22)
1351
0
{
1352
0
    SwsLinearOp c = {{
1353
0
        { m00, m01, m02, Q0, Q0 },
1354
0
        { m10, m11, m12, Q0, Q0 },
1355
0
        { m20, m21, m22, Q0, Q0 },
1356
0
        {  Q0,  Q0,  Q0, Q1, Q0 },
1357
0
    }};
1358
1359
0
    c.mask = ff_sws_linear_mask(c);
1360
0
    return c;
1361
0
}
1362
1363
int ff_sws_decode_colors(SwsContext *ctx, SwsPixelType type,
1364
                         SwsOpList *ops, const SwsFormat *fmt, bool *incomplete)
1365
0
{
1366
0
    const AVLumaCoefficients *c = av_csp_luma_coeffs_from_avcsp(fmt->csp);
1367
0
    const SwsPixelType pixel_type = fmt_pixel_type(fmt->format);
1368
0
    if (!pixel_type)
1369
0
         return AVERROR(ENOTSUP);
1370
1371
0
    RET(ff_sws_op_list_append(ops, &(SwsOp) {
1372
0
        .op         = SWS_OP_CONVERT,
1373
0
        .type       = pixel_type,
1374
0
        .convert.to = type,
1375
0
    }));
1376
1377
    /* Decode pixel format into standardized range */
1378
0
    RET(ff_sws_op_list_append(ops, &(SwsOp) {
1379
0
        .type = type,
1380
0
        .op   = SWS_OP_LINEAR,
1381
0
        .lin  = fmt_decode_range(fmt, incomplete),
1382
0
    }));
1383
1384
    /* Final step, decode colorspace */
1385
0
    switch (fmt->csp) {
1386
0
    case AVCOL_SPC_RGB:
1387
0
        return 0;
1388
0
    case AVCOL_SPC_UNSPECIFIED:
1389
0
        c = av_csp_luma_coeffs_from_avcsp(AVCOL_SPC_BT470BG);
1390
0
        *incomplete = true;
1391
0
        av_fallthrough;
1392
0
    case AVCOL_SPC_FCC:
1393
0
    case AVCOL_SPC_BT470BG:
1394
0
    case AVCOL_SPC_SMPTE170M:
1395
0
    case AVCOL_SPC_BT709:
1396
0
    case AVCOL_SPC_SMPTE240M:
1397
0
    case AVCOL_SPC_BT2020_NCL: {
1398
0
        AVRational crg = av_sub_q(Q0, av_div_q(c->cr, c->cg));
1399
0
        AVRational cbg = av_sub_q(Q0, av_div_q(c->cb, c->cg));
1400
0
        AVRational m02 = av_mul_q(Q(2), av_sub_q(Q1, c->cr));
1401
0
        AVRational m21 = av_mul_q(Q(2), av_sub_q(Q1, c->cb));
1402
0
        AVRational m11 = av_mul_q(cbg, m21);
1403
0
        AVRational m12 = av_mul_q(crg, m02);
1404
1405
0
        return ff_sws_op_list_append(ops, &(SwsOp) {
1406
0
            .type = type,
1407
0
            .op   = SWS_OP_LINEAR,
1408
0
            .lin  = linear_mat3(
1409
0
                Q1,  Q0, m02,
1410
0
                Q1, m11, m12,
1411
0
                Q1, m21,  Q0
1412
0
            ),
1413
0
        });
1414
0
    }
1415
1416
0
    case AVCOL_SPC_YCGCO:
1417
0
        return ff_sws_op_list_append(ops, &(SwsOp) {
1418
0
            .type = type,
1419
0
            .op   = SWS_OP_LINEAR,
1420
0
            .lin  = linear_mat3(
1421
0
                Q1, Q(-1), Q( 1),
1422
0
                Q1, Q( 1), Q( 0),
1423
0
                Q1, Q(-1), Q(-1)
1424
0
            ),
1425
0
        });
1426
1427
0
    case AVCOL_SPC_BT2020_CL:
1428
0
    case AVCOL_SPC_SMPTE2085:
1429
0
    case AVCOL_SPC_CHROMA_DERIVED_NCL:
1430
0
    case AVCOL_SPC_CHROMA_DERIVED_CL:
1431
0
    case AVCOL_SPC_ICTCP:
1432
0
    case AVCOL_SPC_IPT_C2:
1433
0
    case AVCOL_SPC_YCGCO_RE:
1434
0
    case AVCOL_SPC_YCGCO_RO:
1435
0
        return AVERROR(ENOTSUP);
1436
1437
0
    case AVCOL_SPC_RESERVED:
1438
0
        return AVERROR(EINVAL);
1439
1440
0
    case AVCOL_SPC_NB:
1441
0
        break;
1442
0
    }
1443
1444
0
    av_unreachable("Corrupt AVColorSpace value?");
1445
0
    return AVERROR(EINVAL);
1446
0
}
1447
1448
int ff_sws_encode_colors(SwsContext *ctx, SwsPixelType type,
1449
                         SwsOpList *ops, const SwsFormat *src,
1450
                         const SwsFormat *dst, bool *incomplete)
1451
0
{
1452
0
    const AVLumaCoefficients *c = av_csp_luma_coeffs_from_avcsp(dst->csp);
1453
0
    const SwsPixelType pixel_type = fmt_pixel_type(dst->format);
1454
0
    if (!pixel_type)
1455
0
         return AVERROR(ENOTSUP);
1456
1457
0
    switch (dst->csp) {
1458
0
    case AVCOL_SPC_RGB:
1459
0
        break;
1460
0
    case AVCOL_SPC_UNSPECIFIED:
1461
0
        c = av_csp_luma_coeffs_from_avcsp(AVCOL_SPC_BT470BG);
1462
0
        *incomplete = true;
1463
0
        av_fallthrough;
1464
0
    case AVCOL_SPC_FCC:
1465
0
    case AVCOL_SPC_BT470BG:
1466
0
    case AVCOL_SPC_SMPTE170M:
1467
0
    case AVCOL_SPC_BT709:
1468
0
    case AVCOL_SPC_SMPTE240M:
1469
0
    case AVCOL_SPC_BT2020_NCL: {
1470
0
        AVRational cb1 = av_sub_q(c->cb, Q1);
1471
0
        AVRational cr1 = av_sub_q(c->cr, Q1);
1472
0
        AVRational m20 = av_make_q(1,2);
1473
0
        AVRational m10 = av_mul_q(m20, av_div_q(c->cr, cb1));
1474
0
        AVRational m11 = av_mul_q(m20, av_div_q(c->cg, cb1));
1475
0
        AVRational m21 = av_mul_q(m20, av_div_q(c->cg, cr1));
1476
0
        AVRational m22 = av_mul_q(m20, av_div_q(c->cb, cr1));
1477
1478
0
        RET(ff_sws_op_list_append(ops, &(SwsOp) {
1479
0
            .type = type,
1480
0
            .op   = SWS_OP_LINEAR,
1481
0
            .lin  = linear_mat3(
1482
0
                c->cr, c->cg, c->cb,
1483
0
                m10,     m11,   m20,
1484
0
                m20,     m21,   m22
1485
0
            ),
1486
0
        }));
1487
0
        break;
1488
0
    }
1489
1490
0
    case AVCOL_SPC_YCGCO:
1491
0
        RET(ff_sws_op_list_append(ops, &(SwsOp) {
1492
0
            .type = type,
1493
0
            .op   = SWS_OP_LINEAR,
1494
0
            .lin  = linear_mat3(
1495
0
                av_make_q( 1, 4), av_make_q(1, 2), av_make_q( 1, 4),
1496
0
                av_make_q( 1, 2), av_make_q(0, 1), av_make_q(-1, 2),
1497
0
                av_make_q(-1, 4), av_make_q(1, 2), av_make_q(-1, 4)
1498
0
            ),
1499
0
        }));
1500
0
        break;
1501
1502
0
    case AVCOL_SPC_BT2020_CL:
1503
0
    case AVCOL_SPC_SMPTE2085:
1504
0
    case AVCOL_SPC_CHROMA_DERIVED_NCL:
1505
0
    case AVCOL_SPC_CHROMA_DERIVED_CL:
1506
0
    case AVCOL_SPC_ICTCP:
1507
0
    case AVCOL_SPC_IPT_C2:
1508
0
    case AVCOL_SPC_YCGCO_RE:
1509
0
    case AVCOL_SPC_YCGCO_RO:
1510
0
        return AVERROR(ENOTSUP);
1511
1512
0
    case AVCOL_SPC_RESERVED:
1513
0
    case AVCOL_SPC_NB:
1514
0
        return AVERROR(EINVAL);
1515
0
    }
1516
1517
0
    RET(ff_sws_op_list_append(ops, &(SwsOp) {
1518
0
        .type = type,
1519
0
        .op   = SWS_OP_LINEAR,
1520
0
        .lin  = fmt_encode_range(dst, incomplete),
1521
0
    }));
1522
1523
0
    if (!(dst->desc->flags & AV_PIX_FMT_FLAG_FLOAT)) {
1524
0
        SwsClampOp range = {0};
1525
1526
0
        const bool is_ya = dst->desc->nb_components == 2;
1527
0
        for (int i = 0; i < dst->desc->nb_components; i++) {
1528
            /* Clamp to legal pixel range */
1529
0
            const int idx = i * (is_ya ? 3 : 1);
1530
0
            range.limit[idx] = Q((1 << dst->desc->comp[i].depth) - 1);
1531
0
        }
1532
1533
0
        RET(fmt_dither(ctx, ops, type, src, dst));
1534
0
        RET(ff_sws_op_list_append(ops, &(SwsOp) {
1535
0
            .op    = SWS_OP_MAX,
1536
0
            .type  = type,
1537
0
            .clamp = {{ Q0, Q0, Q0, Q0 }},
1538
0
        }));
1539
1540
0
        RET(ff_sws_op_list_append(ops, &(SwsOp) {
1541
0
            .op    = SWS_OP_MIN,
1542
0
            .type  = type,
1543
0
            .clamp = range,
1544
0
        }));
1545
0
    }
1546
1547
0
    return ff_sws_op_list_append(ops, &(SwsOp) {
1548
0
        .type       = type,
1549
0
        .op         = SWS_OP_CONVERT,
1550
0
        .convert.to = pixel_type,
1551
0
    });
1552
0
}
1553
1554
#endif /* CONFIG_UNSTABLE */