/src/ffmpeg/libswscale/swscale.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2001-2011 Michael Niedermayer <michaelni@gmx.at> |
3 | | * |
4 | | * This file is part of FFmpeg. |
5 | | * |
6 | | * FFmpeg is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * FFmpeg is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with FFmpeg; if not, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | | */ |
20 | | |
21 | | #include <stdint.h> |
22 | | #include <stdio.h> |
23 | | #include <string.h> |
24 | | |
25 | | #include "libavutil/avassert.h" |
26 | | #include "libavutil/bswap.h" |
27 | | #include "libavutil/common.h" |
28 | | #include "libavutil/cpu.h" |
29 | | #include "libavutil/emms.h" |
30 | | #include "libavutil/intreadwrite.h" |
31 | | #include "libavutil/mem.h" |
32 | | #include "libavutil/mem_internal.h" |
33 | | #include "libavutil/pixdesc.h" |
34 | | #include "libavutil/hwcontext.h" |
35 | | #include "config.h" |
36 | | #include "swscale_internal.h" |
37 | | #include "swscale.h" |
38 | | #if CONFIG_VULKAN |
39 | | #include "vulkan/ops.h" |
40 | | #endif |
41 | | |
42 | | DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_128)[9][8] = { |
43 | | { 36, 68, 60, 92, 34, 66, 58, 90, }, |
44 | | { 100, 4, 124, 28, 98, 2, 122, 26, }, |
45 | | { 52, 84, 44, 76, 50, 82, 42, 74, }, |
46 | | { 116, 20, 108, 12, 114, 18, 106, 10, }, |
47 | | { 32, 64, 56, 88, 38, 70, 62, 94, }, |
48 | | { 96, 0, 120, 24, 102, 6, 126, 30, }, |
49 | | { 48, 80, 40, 72, 54, 86, 46, 78, }, |
50 | | { 112, 16, 104, 8, 118, 22, 110, 14, }, |
51 | | { 36, 68, 60, 92, 34, 66, 58, 90, }, |
52 | | }; |
53 | | |
54 | | DECLARE_ALIGNED(8, static const uint8_t, sws_pb_64)[8] = { |
55 | | 64, 64, 64, 64, 64, 64, 64, 64 |
56 | | }; |
57 | | |
58 | | static av_always_inline void fillPlane(uint8_t *plane, int stride, int width, |
59 | | int height, int y, uint8_t val) |
60 | 0 | { |
61 | 0 | int i; |
62 | 0 | uint8_t *ptr = plane + stride * y; |
63 | 0 | for (i = 0; i < height; i++) { |
64 | 0 | memset(ptr, val, width); |
65 | 0 | ptr += stride; |
66 | 0 | } |
67 | 0 | } |
68 | | |
69 | | static void hScale16To19_c(SwsInternal *c, int16_t *_dst, int dstW, |
70 | | const uint8_t *_src, const int16_t *filter, |
71 | | const int32_t *filterPos, int filterSize) |
72 | 0 | { |
73 | 0 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->opts.src_format); |
74 | 0 | int i; |
75 | 0 | int32_t *dst = (int32_t *) _dst; |
76 | 0 | const uint16_t *src = (const uint16_t *) _src; |
77 | 0 | int bits = desc->comp[0].depth - 1; |
78 | 0 | int sh = bits - 4; |
79 | |
|
80 | 0 | if ((isAnyRGB(c->opts.src_format) || c->opts.src_format==AV_PIX_FMT_PAL8) && desc->comp[0].depth<16) { |
81 | 0 | sh = 9; |
82 | 0 | } else if (desc->flags & AV_PIX_FMT_FLAG_FLOAT) { /* float input are process like uint 16bpc */ |
83 | 0 | sh = 16 - 1 - 4; |
84 | 0 | } |
85 | |
|
86 | 0 | for (i = 0; i < dstW; i++) { |
87 | 0 | int j; |
88 | 0 | int srcPos = filterPos[i]; |
89 | 0 | int val = 0; |
90 | |
|
91 | 0 | for (j = 0; j < filterSize; j++) { |
92 | 0 | val += src[srcPos + j] * filter[filterSize * i + j]; |
93 | 0 | } |
94 | | // filter=14 bit, input=16 bit, output=30 bit, >> 11 makes 19 bit |
95 | 0 | dst[i] = FFMIN(val >> sh, (1 << 19) - 1); |
96 | 0 | } |
97 | 0 | } |
98 | | |
99 | | static void hScale16To15_c(SwsInternal *c, int16_t *dst, int dstW, |
100 | | const uint8_t *_src, const int16_t *filter, |
101 | | const int32_t *filterPos, int filterSize) |
102 | 0 | { |
103 | 0 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->opts.src_format); |
104 | 0 | int i; |
105 | 0 | const uint16_t *src = (const uint16_t *) _src; |
106 | 0 | int sh = desc->comp[0].depth - 1; |
107 | |
|
108 | 0 | if (sh<15) { |
109 | 0 | sh = isAnyRGB(c->opts.src_format) || c->opts.src_format==AV_PIX_FMT_PAL8 ? 13 : (desc->comp[0].depth - 1); |
110 | 0 | } else if (desc->flags & AV_PIX_FMT_FLAG_FLOAT) { /* float input are process like uint 16bpc */ |
111 | 0 | sh = 16 - 1; |
112 | 0 | } |
113 | |
|
114 | 0 | for (i = 0; i < dstW; i++) { |
115 | 0 | int j; |
116 | 0 | int srcPos = filterPos[i]; |
117 | 0 | int val = 0; |
118 | |
|
119 | 0 | for (j = 0; j < filterSize; j++) { |
120 | 0 | val += src[srcPos + j] * filter[filterSize * i + j]; |
121 | 0 | } |
122 | | // filter=14 bit, input=16 bit, output=30 bit, >> 15 makes 15 bit |
123 | 0 | dst[i] = FFMIN(val >> sh, (1 << 15) - 1); |
124 | 0 | } |
125 | 0 | } |
126 | | |
127 | | // bilinear / bicubic scaling |
128 | | static void hScale8To15_c(SwsInternal *c, int16_t *dst, int dstW, |
129 | | const uint8_t *src, const int16_t *filter, |
130 | | const int32_t *filterPos, int filterSize) |
131 | 0 | { |
132 | 0 | int i; |
133 | 0 | for (i = 0; i < dstW; i++) { |
134 | 0 | int j; |
135 | 0 | int srcPos = filterPos[i]; |
136 | 0 | int val = 0; |
137 | 0 | for (j = 0; j < filterSize; j++) { |
138 | 0 | val += ((int)src[srcPos + j]) * filter[filterSize * i + j]; |
139 | 0 | } |
140 | 0 | dst[i] = FFMIN(val >> 7, (1 << 15) - 1); // the cubic equation does overflow ... |
141 | 0 | } |
142 | 0 | } |
143 | | |
144 | | static void hScale8To19_c(SwsInternal *c, int16_t *_dst, int dstW, |
145 | | const uint8_t *src, const int16_t *filter, |
146 | | const int32_t *filterPos, int filterSize) |
147 | 0 | { |
148 | 0 | int i; |
149 | 0 | int32_t *dst = (int32_t *) _dst; |
150 | 0 | for (i = 0; i < dstW; i++) { |
151 | 0 | int j; |
152 | 0 | int srcPos = filterPos[i]; |
153 | 0 | int val = 0; |
154 | 0 | for (j = 0; j < filterSize; j++) { |
155 | 0 | val += ((int)src[srcPos + j]) * filter[filterSize * i + j]; |
156 | 0 | } |
157 | 0 | dst[i] = FFMIN(val >> 3, (1 << 19) - 1); // the cubic equation does overflow ... |
158 | 0 | } |
159 | 0 | } |
160 | | |
161 | | // FIXME all pal and rgb srcFormats could do this conversion as well |
162 | | // FIXME all scalers more complex than bilinear could do half of this transform |
163 | | static void chrRangeToJpeg_c(int16_t *dstU, int16_t *dstV, int width, |
164 | | uint32_t _coeff, int64_t _offset) |
165 | 0 | { |
166 | 0 | uint16_t coeff = _coeff; |
167 | 0 | int32_t offset = _offset; |
168 | 0 | int i; |
169 | 0 | for (i = 0; i < width; i++) { |
170 | 0 | int U = (dstU[i] * coeff + offset) >> 14; |
171 | 0 | int V = (dstV[i] * coeff + offset) >> 14; |
172 | 0 | dstU[i] = FFMIN(U, (1 << 15) - 1); |
173 | 0 | dstV[i] = FFMIN(V, (1 << 15) - 1); |
174 | 0 | } |
175 | 0 | } |
176 | | |
177 | | static void chrRangeFromJpeg_c(int16_t *dstU, int16_t *dstV, int width, |
178 | | uint32_t _coeff, int64_t _offset) |
179 | 0 | { |
180 | 0 | uint16_t coeff = _coeff; |
181 | 0 | int32_t offset = _offset; |
182 | 0 | int i; |
183 | 0 | for (i = 0; i < width; i++) { |
184 | 0 | dstU[i] = (dstU[i] * coeff + offset) >> 14; |
185 | 0 | dstV[i] = (dstV[i] * coeff + offset) >> 14; |
186 | 0 | } |
187 | 0 | } |
188 | | |
189 | | static void lumRangeToJpeg_c(int16_t *dst, int width, |
190 | | uint32_t _coeff, int64_t _offset) |
191 | 0 | { |
192 | 0 | uint16_t coeff = _coeff; |
193 | 0 | int32_t offset = _offset; |
194 | 0 | int i; |
195 | 0 | for (i = 0; i < width; i++) { |
196 | 0 | int Y = (dst[i] * coeff + offset) >> 14; |
197 | 0 | dst[i] = FFMIN(Y, (1 << 15) - 1); |
198 | 0 | } |
199 | 0 | } |
200 | | |
201 | | static void lumRangeFromJpeg_c(int16_t *dst, int width, |
202 | | uint32_t _coeff, int64_t _offset) |
203 | 0 | { |
204 | 0 | uint16_t coeff = _coeff; |
205 | 0 | int32_t offset = _offset; |
206 | 0 | int i; |
207 | 0 | for (i = 0; i < width; i++) |
208 | 0 | dst[i] = (dst[i] * coeff + offset) >> 14; |
209 | 0 | } |
210 | | |
211 | | static void chrRangeToJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width, |
212 | | uint32_t coeff, int64_t offset) |
213 | 0 | { |
214 | 0 | int i; |
215 | 0 | int32_t *dstU = (int32_t *) _dstU; |
216 | 0 | int32_t *dstV = (int32_t *) _dstV; |
217 | 0 | for (i = 0; i < width; i++) { |
218 | 0 | int U = ((int64_t) dstU[i] * coeff + offset) >> 18; |
219 | 0 | int V = ((int64_t) dstV[i] * coeff + offset) >> 18; |
220 | 0 | dstU[i] = FFMIN(U, (1 << 19) - 1); |
221 | 0 | dstV[i] = FFMIN(V, (1 << 19) - 1); |
222 | 0 | } |
223 | 0 | } |
224 | | |
225 | | static void chrRangeFromJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width, |
226 | | uint32_t coeff, int64_t offset) |
227 | 0 | { |
228 | 0 | int i; |
229 | 0 | int32_t *dstU = (int32_t *) _dstU; |
230 | 0 | int32_t *dstV = (int32_t *) _dstV; |
231 | 0 | for (i = 0; i < width; i++) { |
232 | 0 | dstU[i] = ((int64_t) dstU[i] * coeff + offset) >> 18; |
233 | 0 | dstV[i] = ((int64_t) dstV[i] * coeff + offset) >> 18; |
234 | 0 | } |
235 | 0 | } |
236 | | |
237 | | static void lumRangeToJpeg16_c(int16_t *_dst, int width, |
238 | | uint32_t coeff, int64_t offset) |
239 | 0 | { |
240 | 0 | int i; |
241 | 0 | int32_t *dst = (int32_t *) _dst; |
242 | 0 | for (i = 0; i < width; i++) { |
243 | 0 | int Y = ((int64_t) dst[i] * coeff + offset) >> 18; |
244 | 0 | dst[i] = FFMIN(Y, (1 << 19) - 1); |
245 | 0 | } |
246 | 0 | } |
247 | | |
248 | | static void lumRangeFromJpeg16_c(int16_t *_dst, int width, |
249 | | uint32_t coeff, int64_t offset) |
250 | 0 | { |
251 | 0 | int i; |
252 | 0 | int32_t *dst = (int32_t *) _dst; |
253 | 0 | for (i = 0; i < width; i++) |
254 | 0 | dst[i] = ((int64_t) dst[i] * coeff + offset) >> 18; |
255 | 0 | } |
256 | | |
257 | | |
258 | 0 | #define DEBUG_SWSCALE_BUFFERS 0 |
259 | | #define DEBUG_BUFFERS(...) \ |
260 | 0 | if (DEBUG_SWSCALE_BUFFERS) \ |
261 | 0 | av_log(c, AV_LOG_DEBUG, __VA_ARGS__) |
262 | | |
263 | | int ff_swscale(SwsInternal *c, const uint8_t *const src[], const int srcStride[], |
264 | | int srcSliceY, int srcSliceH, uint8_t *const dst[], |
265 | | const int dstStride[], int dstSliceY, int dstSliceH) |
266 | 0 | { |
267 | 0 | const int scale_dst = dstSliceY > 0 || dstSliceH < c->opts.dst_h; |
268 | | |
269 | | /* load a few things into local vars to make the code more readable? |
270 | | * and faster */ |
271 | 0 | const int dstW = c->opts.dst_w; |
272 | 0 | int dstH = c->opts.dst_h; |
273 | |
|
274 | 0 | const enum AVPixelFormat dstFormat = c->opts.dst_format; |
275 | 0 | const int flags = c->opts.flags; |
276 | 0 | int32_t *vLumFilterPos = c->vLumFilterPos; |
277 | 0 | int32_t *vChrFilterPos = c->vChrFilterPos; |
278 | |
|
279 | 0 | const int vLumFilterSize = c->vLumFilterSize; |
280 | 0 | const int vChrFilterSize = c->vChrFilterSize; |
281 | |
|
282 | 0 | yuv2planar1_fn yuv2plane1 = c->yuv2plane1; |
283 | 0 | yuv2planarX_fn yuv2planeX = c->yuv2planeX; |
284 | 0 | yuv2interleavedX_fn yuv2nv12cX = c->yuv2nv12cX; |
285 | 0 | yuv2packed1_fn yuv2packed1 = c->yuv2packed1; |
286 | 0 | yuv2packed2_fn yuv2packed2 = c->yuv2packed2; |
287 | 0 | yuv2packedX_fn yuv2packedX = c->yuv2packedX; |
288 | 0 | yuv2anyX_fn yuv2anyX = c->yuv2anyX; |
289 | 0 | const int chrSrcSliceY = srcSliceY >> c->chrSrcVSubSample; |
290 | 0 | const int chrSrcSliceH = AV_CEIL_RSHIFT(srcSliceH, c->chrSrcVSubSample); |
291 | 0 | int should_dither = isNBPS(c->opts.src_format) || |
292 | 0 | is16BPS(c->opts.src_format); |
293 | 0 | int lastDstY; |
294 | | |
295 | | /* vars which will change and which we need to store back in the context */ |
296 | 0 | int dstY = c->dstY; |
297 | 0 | int lastInLumBuf = c->lastInLumBuf; |
298 | 0 | int lastInChrBuf = c->lastInChrBuf; |
299 | |
|
300 | 0 | int lumStart = 0; |
301 | 0 | int lumEnd = c->descIndex[0]; |
302 | 0 | int chrStart = lumEnd; |
303 | 0 | int chrEnd = c->descIndex[1]; |
304 | 0 | int vStart = chrEnd; |
305 | 0 | int vEnd = c->numDesc; |
306 | 0 | SwsSlice *src_slice = &c->slice[lumStart]; |
307 | 0 | SwsSlice *hout_slice = &c->slice[c->numSlice-2]; |
308 | 0 | SwsSlice *vout_slice = &c->slice[c->numSlice-1]; |
309 | 0 | SwsFilterDescriptor *desc = c->desc; |
310 | |
|
311 | 0 | int needAlpha = c->needAlpha; |
312 | |
|
313 | 0 | int hasLumHoles = 1; |
314 | 0 | int hasChrHoles = 1; |
315 | |
|
316 | 0 | const uint8_t *src2[4]; |
317 | 0 | int srcStride2[4]; |
318 | |
|
319 | 0 | if (isPacked(c->opts.src_format)) { |
320 | 0 | src2[0] = |
321 | 0 | src2[1] = |
322 | 0 | src2[2] = |
323 | 0 | src2[3] = src[0]; |
324 | 0 | srcStride2[0] = |
325 | 0 | srcStride2[1] = |
326 | 0 | srcStride2[2] = |
327 | 0 | srcStride2[3] = srcStride[0]; |
328 | 0 | } else { |
329 | 0 | memcpy(src2, src, sizeof(src2)); |
330 | 0 | memcpy(srcStride2, srcStride, sizeof(srcStride2)); |
331 | 0 | } |
332 | |
|
333 | 0 | srcStride2[1] *= 1 << c->vChrDrop; |
334 | 0 | srcStride2[2] *= 1 << c->vChrDrop; |
335 | |
|
336 | 0 | DEBUG_BUFFERS("swscale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n", |
337 | 0 | src2[0], srcStride2[0], src2[1], srcStride2[1], |
338 | 0 | src2[2], srcStride2[2], src2[3], srcStride2[3], |
339 | 0 | dst[0], dstStride[0], dst[1], dstStride[1], |
340 | 0 | dst[2], dstStride[2], dst[3], dstStride[3]); |
341 | 0 | DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n", |
342 | 0 | srcSliceY, srcSliceH, dstY, dstH); |
343 | 0 | DEBUG_BUFFERS("vLumFilterSize: %d vChrFilterSize: %d\n", |
344 | 0 | vLumFilterSize, vChrFilterSize); |
345 | |
|
346 | 0 | if (dstStride[0]&15 || dstStride[1]&15 || |
347 | 0 | dstStride[2]&15 || dstStride[3]&15) { |
348 | 0 | SwsInternal *const ctx = c->parent ? sws_internal(c->parent) : c; |
349 | 0 | if (flags & SWS_PRINT_INFO && |
350 | 0 | !atomic_exchange_explicit(&ctx->stride_unaligned_warned, 1, memory_order_relaxed)) { |
351 | 0 | av_log(c, AV_LOG_WARNING, |
352 | 0 | "Warning: dstStride is not aligned!\n" |
353 | 0 | " ->cannot do aligned memory accesses anymore\n"); |
354 | 0 | } |
355 | 0 | } |
356 | |
|
357 | | #if ARCH_X86 |
358 | | if ( (uintptr_t) dst[0]&15 || (uintptr_t) dst[1]&15 || (uintptr_t) dst[2]&15 |
359 | | || (uintptr_t)src2[0]&15 || (uintptr_t)src2[1]&15 || (uintptr_t)src2[2]&15 |
360 | | || srcStride2[0]&15 || srcStride2[1]&15 || srcStride2[2]&15 || srcStride2[3]&15 |
361 | | ) { |
362 | | SwsInternal *const ctx = c->parent ? sws_internal(c->parent) : c; |
363 | | int cpu_flags = av_get_cpu_flags(); |
364 | | if (flags & SWS_PRINT_INFO && HAVE_MMXEXT && (cpu_flags & AV_CPU_FLAG_SSE2) && |
365 | | !atomic_exchange_explicit(&ctx->stride_unaligned_warned,1, memory_order_relaxed)) { |
366 | | av_log(c, AV_LOG_WARNING, "Warning: data is not aligned! This can lead to a speed loss\n"); |
367 | | } |
368 | | } |
369 | | #endif |
370 | |
|
371 | 0 | if (scale_dst) { |
372 | 0 | dstY = dstSliceY; |
373 | 0 | dstH = dstY + dstSliceH; |
374 | 0 | lastInLumBuf = -1; |
375 | 0 | lastInChrBuf = -1; |
376 | 0 | } else if (srcSliceY == 0) { |
377 | | /* Note the user might start scaling the picture in the middle so this |
378 | | * will not get executed. This is not really intended but works |
379 | | * currently, so people might do it. */ |
380 | 0 | dstY = 0; |
381 | 0 | lastInLumBuf = -1; |
382 | 0 | lastInChrBuf = -1; |
383 | 0 | } |
384 | |
|
385 | 0 | if (!should_dither) { |
386 | 0 | c->chrDither8 = c->lumDither8 = sws_pb_64; |
387 | 0 | } |
388 | 0 | lastDstY = dstY; |
389 | |
|
390 | 0 | ff_init_vscale_pfn(c, yuv2plane1, yuv2planeX, yuv2nv12cX, |
391 | 0 | yuv2packed1, yuv2packed2, yuv2packedX, yuv2anyX, c->use_mmx_vfilter); |
392 | |
|
393 | 0 | ff_init_slice_from_src(src_slice, (uint8_t**)src2, srcStride2, c->opts.src_w, |
394 | 0 | srcSliceY, srcSliceH, chrSrcSliceY, chrSrcSliceH, 1); |
395 | |
|
396 | 0 | ff_init_slice_from_src(vout_slice, (uint8_t**)dst, dstStride, c->opts.dst_w, |
397 | 0 | dstY, dstSliceH, dstY >> c->chrDstVSubSample, |
398 | 0 | AV_CEIL_RSHIFT(dstSliceH, c->chrDstVSubSample), scale_dst); |
399 | 0 | if (srcSliceY == 0) { |
400 | 0 | hout_slice->plane[0].sliceY = lastInLumBuf + 1; |
401 | 0 | hout_slice->plane[1].sliceY = lastInChrBuf + 1; |
402 | 0 | hout_slice->plane[2].sliceY = lastInChrBuf + 1; |
403 | 0 | hout_slice->plane[3].sliceY = lastInLumBuf + 1; |
404 | |
|
405 | 0 | hout_slice->plane[0].sliceH = |
406 | 0 | hout_slice->plane[1].sliceH = |
407 | 0 | hout_slice->plane[2].sliceH = |
408 | 0 | hout_slice->plane[3].sliceH = 0; |
409 | 0 | hout_slice->width = dstW; |
410 | 0 | } |
411 | |
|
412 | 0 | for (; dstY < dstH; dstY++) { |
413 | 0 | const int chrDstY = dstY >> c->chrDstVSubSample; |
414 | 0 | int use_mmx_vfilter= c->use_mmx_vfilter; |
415 | | |
416 | | // First line needed as input |
417 | 0 | const int firstLumSrcY = FFMAX(1 - vLumFilterSize, vLumFilterPos[dstY]); |
418 | 0 | const int firstLumSrcY2 = FFMAX(1 - vLumFilterSize, vLumFilterPos[FFMIN(dstY | ((1 << c->chrDstVSubSample) - 1), c->opts.dst_h - 1)]); |
419 | | // First line needed as input |
420 | 0 | const int firstChrSrcY = FFMAX(1 - vChrFilterSize, vChrFilterPos[chrDstY]); |
421 | | |
422 | | // Last line needed as input |
423 | 0 | int lastLumSrcY = FFMIN(c->opts.src_h, firstLumSrcY + vLumFilterSize) - 1; |
424 | 0 | int lastLumSrcY2 = FFMIN(c->opts.src_h, firstLumSrcY2 + vLumFilterSize) - 1; |
425 | 0 | int lastChrSrcY = FFMIN(c->chrSrcH, firstChrSrcY + vChrFilterSize) - 1; |
426 | 0 | int enough_lines; |
427 | |
|
428 | 0 | int i; |
429 | 0 | int posY, cPosY, firstPosY, lastPosY, firstCPosY, lastCPosY; |
430 | | |
431 | | // handle holes (FAST_BILINEAR & weird filters) |
432 | 0 | if (firstLumSrcY > lastInLumBuf) { |
433 | |
|
434 | 0 | hasLumHoles = lastInLumBuf != firstLumSrcY - 1; |
435 | 0 | if (hasLumHoles) { |
436 | 0 | hout_slice->plane[0].sliceY = firstLumSrcY; |
437 | 0 | hout_slice->plane[3].sliceY = firstLumSrcY; |
438 | 0 | hout_slice->plane[0].sliceH = |
439 | 0 | hout_slice->plane[3].sliceH = 0; |
440 | 0 | } |
441 | |
|
442 | 0 | lastInLumBuf = firstLumSrcY - 1; |
443 | 0 | } |
444 | 0 | if (firstChrSrcY > lastInChrBuf) { |
445 | |
|
446 | 0 | hasChrHoles = lastInChrBuf != firstChrSrcY - 1; |
447 | 0 | if (hasChrHoles) { |
448 | 0 | hout_slice->plane[1].sliceY = firstChrSrcY; |
449 | 0 | hout_slice->plane[2].sliceY = firstChrSrcY; |
450 | 0 | hout_slice->plane[1].sliceH = |
451 | 0 | hout_slice->plane[2].sliceH = 0; |
452 | 0 | } |
453 | |
|
454 | 0 | lastInChrBuf = firstChrSrcY - 1; |
455 | 0 | } |
456 | |
|
457 | 0 | DEBUG_BUFFERS("dstY: %d\n", dstY); |
458 | 0 | DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n", |
459 | 0 | firstLumSrcY, lastLumSrcY, lastInLumBuf); |
460 | 0 | DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n", |
461 | 0 | firstChrSrcY, lastChrSrcY, lastInChrBuf); |
462 | | |
463 | | // Do we have enough lines in this slice to output the dstY line |
464 | 0 | enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH && |
465 | 0 | lastChrSrcY < AV_CEIL_RSHIFT(srcSliceY + srcSliceH, c->chrSrcVSubSample); |
466 | |
|
467 | 0 | if (!enough_lines) { |
468 | 0 | lastLumSrcY = srcSliceY + srcSliceH - 1; |
469 | 0 | lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1; |
470 | 0 | DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n", |
471 | 0 | lastLumSrcY, lastChrSrcY); |
472 | 0 | } |
473 | |
|
474 | 0 | av_assert0((lastLumSrcY - firstLumSrcY + 1) <= hout_slice->plane[0].available_lines); |
475 | 0 | av_assert0((lastChrSrcY - firstChrSrcY + 1) <= hout_slice->plane[1].available_lines); |
476 | | |
477 | | |
478 | 0 | posY = hout_slice->plane[0].sliceY + hout_slice->plane[0].sliceH; |
479 | 0 | if (posY <= lastLumSrcY && !hasLumHoles) { |
480 | 0 | firstPosY = FFMAX(firstLumSrcY, posY); |
481 | 0 | lastPosY = FFMIN(firstLumSrcY + hout_slice->plane[0].available_lines - 1, srcSliceY + srcSliceH - 1); |
482 | 0 | } else { |
483 | 0 | firstPosY = posY; |
484 | 0 | lastPosY = lastLumSrcY; |
485 | 0 | } |
486 | |
|
487 | 0 | cPosY = hout_slice->plane[1].sliceY + hout_slice->plane[1].sliceH; |
488 | 0 | if (cPosY <= lastChrSrcY && !hasChrHoles) { |
489 | 0 | firstCPosY = FFMAX(firstChrSrcY, cPosY); |
490 | 0 | lastCPosY = FFMIN(firstChrSrcY + hout_slice->plane[1].available_lines - 1, AV_CEIL_RSHIFT(srcSliceY + srcSliceH, c->chrSrcVSubSample) - 1); |
491 | 0 | } else { |
492 | 0 | firstCPosY = cPosY; |
493 | 0 | lastCPosY = lastChrSrcY; |
494 | 0 | } |
495 | |
|
496 | 0 | ff_rotate_slice(hout_slice, lastPosY, lastCPosY); |
497 | |
|
498 | 0 | if (posY < lastLumSrcY + 1) { |
499 | 0 | for (i = lumStart; i < lumEnd; ++i) |
500 | 0 | desc[i].process(c, &desc[i], firstPosY, lastPosY - firstPosY + 1); |
501 | 0 | } |
502 | |
|
503 | 0 | lastInLumBuf = lastLumSrcY; |
504 | |
|
505 | 0 | if (cPosY < lastChrSrcY + 1) { |
506 | 0 | for (i = chrStart; i < chrEnd; ++i) |
507 | 0 | desc[i].process(c, &desc[i], firstCPosY, lastCPosY - firstCPosY + 1); |
508 | 0 | } |
509 | |
|
510 | 0 | lastInChrBuf = lastChrSrcY; |
511 | |
|
512 | 0 | if (!enough_lines) |
513 | 0 | break; // we can't output a dstY line so let's try with the next slice |
514 | | |
515 | | #if ARCH_X86 && HAVE_MMX |
516 | | ff_updateMMXDitherTables(c, dstY); |
517 | | c->dstW_mmx = c->opts.dst_w; |
518 | | #endif |
519 | 0 | if (should_dither) { |
520 | 0 | c->chrDither8 = ff_dither_8x8_128[chrDstY & 7]; |
521 | 0 | c->lumDither8 = ff_dither_8x8_128[dstY & 7]; |
522 | 0 | } |
523 | 0 | if (dstY >= c->opts.dst_h - 2) { |
524 | | /* hmm looks like we can't use MMX here without overwriting |
525 | | * this array's tail */ |
526 | 0 | ff_sws_init_output_funcs(c, &yuv2plane1, &yuv2planeX, &yuv2nv12cX, |
527 | 0 | &yuv2packed1, &yuv2packed2, &yuv2packedX, &yuv2anyX); |
528 | 0 | use_mmx_vfilter= 0; |
529 | 0 | ff_init_vscale_pfn(c, yuv2plane1, yuv2planeX, yuv2nv12cX, |
530 | 0 | yuv2packed1, yuv2packed2, yuv2packedX, yuv2anyX, use_mmx_vfilter); |
531 | 0 | } |
532 | |
|
533 | 0 | for (i = vStart; i < vEnd; ++i) |
534 | 0 | desc[i].process(c, &desc[i], dstY, 1); |
535 | 0 | } |
536 | 0 | if (isPlanar(dstFormat) && isALPHA(dstFormat) && !needAlpha) { |
537 | 0 | int offset = lastDstY - dstSliceY; |
538 | 0 | int length = dstW; |
539 | 0 | int height = dstY - lastDstY; |
540 | |
|
541 | 0 | if (is16BPS(dstFormat) || isNBPS(dstFormat)) { |
542 | 0 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat); |
543 | 0 | fillPlane16(dst[3], dstStride[3], length, height, offset, |
544 | 0 | 1, desc->comp[3].depth, |
545 | 0 | isBE(dstFormat)); |
546 | 0 | } else if (is32BPS(dstFormat)) { |
547 | 0 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat); |
548 | 0 | fillPlane32(dst[3], dstStride[3], length, height, offset, |
549 | 0 | 1, desc->comp[3].depth, |
550 | 0 | isBE(dstFormat), desc->flags & AV_PIX_FMT_FLAG_FLOAT); |
551 | 0 | } else |
552 | 0 | fillPlane(dst[3], dstStride[3], length, height, offset, 255); |
553 | 0 | } |
554 | |
|
555 | | #if HAVE_MMXEXT_INLINE |
556 | | if (av_get_cpu_flags() & AV_CPU_FLAG_MMXEXT) |
557 | | __asm__ volatile ("sfence" ::: "memory"); |
558 | | #endif |
559 | 0 | emms_c(); |
560 | | |
561 | | /* store changed local vars back in the context */ |
562 | 0 | c->dstY = dstY; |
563 | 0 | c->lastInLumBuf = lastInLumBuf; |
564 | 0 | c->lastInChrBuf = lastInChrBuf; |
565 | |
|
566 | 0 | return dstY - lastDstY; |
567 | 0 | } |
568 | | |
569 | | /* |
570 | | * Solve for coeff and offset: |
571 | | * dst = ((src << src_shift) * coeff + offset) >> (mult_shift + src_shift) |
572 | | * |
573 | | * If SwsInternal->dstBpc is > 14, coeff is uint16_t and offset is int32_t, |
574 | | * otherwise (SwsInternal->dstBpc is <= 14) coeff is uint32_t and offset is |
575 | | * int64_t. |
576 | | */ |
577 | | static void solve_range_convert(uint16_t src_min, uint16_t src_max, |
578 | | uint16_t dst_min, uint16_t dst_max, |
579 | | int src_bits, int src_shift, int mult_shift, |
580 | | uint32_t *coeff, int64_t *offset) |
581 | 0 | { |
582 | 0 | uint16_t src_range = src_max - src_min; |
583 | 0 | uint16_t dst_range = dst_max - dst_min; |
584 | 0 | int total_shift = mult_shift + src_shift; |
585 | 0 | *coeff = AV_CEIL_RSHIFT(((uint64_t) dst_range << total_shift) / src_range, src_shift); |
586 | 0 | *offset = ((int64_t) dst_max << total_shift) - |
587 | 0 | ((int64_t) src_max << src_shift) * *coeff + |
588 | 0 | (1U << (mult_shift - 1)); |
589 | 0 | } |
590 | | |
591 | | static void init_range_convert_constants(SwsInternal *c) |
592 | 0 | { |
593 | 0 | const int bit_depth = c->dstBpc ? FFMIN(c->dstBpc, 16) : 8; |
594 | 0 | const int src_bits = bit_depth <= 14 ? 15 : 19; |
595 | 0 | const int src_shift = src_bits - bit_depth; |
596 | 0 | const int mult_shift = bit_depth <= 14 ? 14 : 18; |
597 | 0 | const uint16_t mpeg_min = 16U << (bit_depth - 8); |
598 | 0 | const uint16_t mpeg_max_lum = 235U << (bit_depth - 8); |
599 | 0 | const uint16_t mpeg_max_chr = 240U << (bit_depth - 8); |
600 | 0 | const uint16_t jpeg_max = (1U << bit_depth) - 1; |
601 | 0 | uint16_t src_min, src_max_lum, src_max_chr; |
602 | 0 | uint16_t dst_min, dst_max_lum, dst_max_chr; |
603 | 0 | if (c->opts.src_range) { |
604 | 0 | src_min = 0; |
605 | 0 | src_max_lum = jpeg_max; |
606 | 0 | src_max_chr = jpeg_max; |
607 | 0 | dst_min = mpeg_min; |
608 | 0 | dst_max_lum = mpeg_max_lum; |
609 | 0 | dst_max_chr = mpeg_max_chr; |
610 | 0 | } else { |
611 | 0 | src_min = mpeg_min; |
612 | 0 | src_max_lum = mpeg_max_lum; |
613 | 0 | src_max_chr = mpeg_max_chr; |
614 | 0 | dst_min = 0; |
615 | 0 | dst_max_lum = jpeg_max; |
616 | 0 | dst_max_chr = jpeg_max; |
617 | 0 | } |
618 | 0 | solve_range_convert(src_min, src_max_lum, dst_min, dst_max_lum, |
619 | 0 | src_bits, src_shift, mult_shift, |
620 | 0 | &c->lumConvertRange_coeff, &c->lumConvertRange_offset); |
621 | 0 | solve_range_convert(src_min, src_max_chr, dst_min, dst_max_chr, |
622 | 0 | src_bits, src_shift, mult_shift, |
623 | 0 | &c->chrConvertRange_coeff, &c->chrConvertRange_offset); |
624 | 0 | } |
625 | | |
626 | | av_cold void ff_sws_init_range_convert(SwsInternal *c) |
627 | 0 | { |
628 | 0 | c->lumConvertRange = NULL; |
629 | 0 | c->chrConvertRange = NULL; |
630 | 0 | if (c->opts.src_range != c->opts.dst_range && !isAnyRGB(c->opts.dst_format) && c->dstBpc < 32) { |
631 | 0 | init_range_convert_constants(c); |
632 | 0 | if (c->dstBpc <= 14) { |
633 | 0 | if (c->opts.src_range) { |
634 | 0 | c->lumConvertRange = lumRangeFromJpeg_c; |
635 | 0 | c->chrConvertRange = chrRangeFromJpeg_c; |
636 | 0 | } else { |
637 | 0 | c->lumConvertRange = lumRangeToJpeg_c; |
638 | 0 | c->chrConvertRange = chrRangeToJpeg_c; |
639 | 0 | } |
640 | 0 | } else { |
641 | 0 | if (c->opts.src_range) { |
642 | 0 | c->lumConvertRange = lumRangeFromJpeg16_c; |
643 | 0 | c->chrConvertRange = chrRangeFromJpeg16_c; |
644 | 0 | } else { |
645 | 0 | c->lumConvertRange = lumRangeToJpeg16_c; |
646 | 0 | c->chrConvertRange = chrRangeToJpeg16_c; |
647 | 0 | } |
648 | 0 | } |
649 | |
|
650 | | #if ARCH_AARCH64 |
651 | | ff_sws_init_range_convert_aarch64(c); |
652 | | #elif ARCH_LOONGARCH64 |
653 | | ff_sws_init_range_convert_loongarch(c); |
654 | | #elif ARCH_RISCV |
655 | | ff_sws_init_range_convert_riscv(c); |
656 | | #elif ARCH_X86 |
657 | | ff_sws_init_range_convert_x86(c); |
658 | | #endif |
659 | 0 | } |
660 | 0 | } |
661 | | |
662 | | static av_cold void sws_init_swscale(SwsInternal *c) |
663 | 0 | { |
664 | 0 | enum AVPixelFormat srcFormat = c->opts.src_format; |
665 | |
|
666 | 0 | ff_sws_init_xyzdsp(c); |
667 | |
|
668 | 0 | ff_sws_init_output_funcs(c, &c->yuv2plane1, &c->yuv2planeX, |
669 | 0 | &c->yuv2nv12cX, &c->yuv2packed1, |
670 | 0 | &c->yuv2packed2, &c->yuv2packedX, &c->yuv2anyX); |
671 | |
|
672 | 0 | ff_sws_init_input_funcs(c, &c->lumToYV12, &c->alpToYV12, &c->chrToYV12, |
673 | 0 | &c->readLumPlanar, &c->readAlpPlanar, &c->readChrPlanar); |
674 | |
|
675 | 0 | if (c->srcBpc == 8) { |
676 | 0 | if (c->dstBpc <= 14) { |
677 | 0 | c->hyScale = c->hcScale = hScale8To15_c; |
678 | 0 | if (c->opts.flags & SWS_FAST_BILINEAR) { |
679 | 0 | c->hyscale_fast = ff_hyscale_fast_c; |
680 | 0 | c->hcscale_fast = ff_hcscale_fast_c; |
681 | 0 | } |
682 | 0 | } else { |
683 | 0 | c->hyScale = c->hcScale = hScale8To19_c; |
684 | 0 | } |
685 | 0 | } else { |
686 | 0 | c->hyScale = c->hcScale = c->dstBpc > 14 ? hScale16To19_c |
687 | 0 | : hScale16To15_c; |
688 | 0 | } |
689 | |
|
690 | 0 | ff_sws_init_range_convert(c); |
691 | |
|
692 | 0 | if (!(isGray(srcFormat) || isGray(c->opts.dst_format) || |
693 | 0 | srcFormat == AV_PIX_FMT_MONOBLACK || srcFormat == AV_PIX_FMT_MONOWHITE)) |
694 | 0 | c->needs_hcscale = 1; |
695 | 0 | } |
696 | | |
697 | | void ff_sws_init_scale(SwsInternal *c) |
698 | 0 | { |
699 | 0 | sws_init_swscale(c); |
700 | |
|
701 | | #if ARCH_PPC |
702 | | ff_sws_init_swscale_ppc(c); |
703 | | #elif ARCH_X86 |
704 | | ff_sws_init_swscale_x86(c); |
705 | | #elif ARCH_AARCH64 |
706 | | ff_sws_init_swscale_aarch64(c); |
707 | | #elif ARCH_ARM |
708 | | ff_sws_init_swscale_arm(c); |
709 | | #elif ARCH_LOONGARCH64 |
710 | | ff_sws_init_swscale_loongarch(c); |
711 | | #elif ARCH_RISCV |
712 | | ff_sws_init_swscale_riscv(c); |
713 | | #endif |
714 | 0 | } |
715 | | |
716 | | static void reset_ptr(const uint8_t *src[], enum AVPixelFormat format) |
717 | 0 | { |
718 | 0 | if (!isALPHA(format)) |
719 | 0 | src[3] = NULL; |
720 | 0 | if (!isPlanar(format)) { |
721 | 0 | src[3] = src[2] = NULL; |
722 | |
|
723 | 0 | if (!usePal(format)) |
724 | 0 | src[1] = NULL; |
725 | 0 | } |
726 | 0 | } |
727 | | |
728 | | static int check_image_pointers(const uint8_t * const data[4], enum AVPixelFormat pix_fmt, |
729 | | const int linesizes[4]) |
730 | 0 | { |
731 | 0 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); |
732 | 0 | int i; |
733 | |
|
734 | 0 | av_assert2(desc); |
735 | |
|
736 | 0 | for (i = 0; i < 4; i++) { |
737 | 0 | int plane = desc->comp[i].plane; |
738 | 0 | if (!data[plane] || !linesizes[plane]) |
739 | 0 | return 0; |
740 | 0 | } |
741 | | |
742 | 0 | return 1; |
743 | 0 | } |
744 | | |
745 | | static void xyz12Torgb48_c(const SwsInternal *c, uint8_t *dst, int dst_stride, |
746 | | const uint8_t *src, int src_stride, int w, int h) |
747 | 0 | { |
748 | 0 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->opts.src_format); |
749 | |
|
750 | 0 | for (int yp = 0; yp < h; yp++) { |
751 | 0 | const uint16_t *src16 = (const uint16_t *) src; |
752 | 0 | uint16_t *dst16 = (uint16_t *) dst; |
753 | |
|
754 | 0 | for (int xp = 0; xp < 3 * w; xp += 3) { |
755 | 0 | int x, y, z, r, g, b; |
756 | |
|
757 | 0 | if (desc->flags & AV_PIX_FMT_FLAG_BE) { |
758 | 0 | x = AV_RB16(src16 + xp + 0); |
759 | 0 | y = AV_RB16(src16 + xp + 1); |
760 | 0 | z = AV_RB16(src16 + xp + 2); |
761 | 0 | } else { |
762 | 0 | x = AV_RL16(src16 + xp + 0); |
763 | 0 | y = AV_RL16(src16 + xp + 1); |
764 | 0 | z = AV_RL16(src16 + xp + 2); |
765 | 0 | } |
766 | |
|
767 | 0 | x = c->xyz2rgb.gamma.in[x >> 4]; |
768 | 0 | y = c->xyz2rgb.gamma.in[y >> 4]; |
769 | 0 | z = c->xyz2rgb.gamma.in[z >> 4]; |
770 | | |
771 | | // convert from XYZlinear to sRGBlinear |
772 | 0 | r = c->xyz2rgb.mat[0][0] * x + |
773 | 0 | c->xyz2rgb.mat[0][1] * y + |
774 | 0 | c->xyz2rgb.mat[0][2] * z >> 12; |
775 | 0 | g = c->xyz2rgb.mat[1][0] * x + |
776 | 0 | c->xyz2rgb.mat[1][1] * y + |
777 | 0 | c->xyz2rgb.mat[1][2] * z >> 12; |
778 | 0 | b = c->xyz2rgb.mat[2][0] * x + |
779 | 0 | c->xyz2rgb.mat[2][1] * y + |
780 | 0 | c->xyz2rgb.mat[2][2] * z >> 12; |
781 | | |
782 | | // limit values to 16-bit depth |
783 | 0 | r = av_clip_uint16(r); |
784 | 0 | g = av_clip_uint16(g); |
785 | 0 | b = av_clip_uint16(b); |
786 | | |
787 | | // convert from sRGBlinear to RGB and scale from 12bit to 16bit |
788 | 0 | if (desc->flags & AV_PIX_FMT_FLAG_BE) { |
789 | 0 | AV_WB16(dst16 + xp + 0, c->xyz2rgb.gamma.out[r] << 4); |
790 | 0 | AV_WB16(dst16 + xp + 1, c->xyz2rgb.gamma.out[g] << 4); |
791 | 0 | AV_WB16(dst16 + xp + 2, c->xyz2rgb.gamma.out[b] << 4); |
792 | 0 | } else { |
793 | 0 | AV_WL16(dst16 + xp + 0, c->xyz2rgb.gamma.out[r] << 4); |
794 | 0 | AV_WL16(dst16 + xp + 1, c->xyz2rgb.gamma.out[g] << 4); |
795 | 0 | AV_WL16(dst16 + xp + 2, c->xyz2rgb.gamma.out[b] << 4); |
796 | 0 | } |
797 | 0 | } |
798 | |
|
799 | 0 | src += src_stride; |
800 | 0 | dst += dst_stride; |
801 | 0 | } |
802 | 0 | } |
803 | | |
804 | | static void rgb48Toxyz12_c(const SwsInternal *c, uint8_t *dst, int dst_stride, |
805 | | const uint8_t *src, int src_stride, int w, int h) |
806 | 0 | { |
807 | 0 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->opts.dst_format); |
808 | |
|
809 | 0 | for (int yp = 0; yp < h; yp++) { |
810 | 0 | uint16_t *src16 = (uint16_t *) src; |
811 | 0 | uint16_t *dst16 = (uint16_t *) dst; |
812 | |
|
813 | 0 | for (int xp = 0; xp < 3 * w; xp += 3) { |
814 | 0 | int x, y, z, r, g, b; |
815 | |
|
816 | 0 | if (desc->flags & AV_PIX_FMT_FLAG_BE) { |
817 | 0 | r = AV_RB16(src16 + xp + 0); |
818 | 0 | g = AV_RB16(src16 + xp + 1); |
819 | 0 | b = AV_RB16(src16 + xp + 2); |
820 | 0 | } else { |
821 | 0 | r = AV_RL16(src16 + xp + 0); |
822 | 0 | g = AV_RL16(src16 + xp + 1); |
823 | 0 | b = AV_RL16(src16 + xp + 2); |
824 | 0 | } |
825 | |
|
826 | 0 | r = c->rgb2xyz.gamma.in[r >> 4]; |
827 | 0 | g = c->rgb2xyz.gamma.in[g >> 4]; |
828 | 0 | b = c->rgb2xyz.gamma.in[b >> 4]; |
829 | | |
830 | | // convert from sRGBlinear to XYZlinear |
831 | 0 | x = c->rgb2xyz.mat[0][0] * r + |
832 | 0 | c->rgb2xyz.mat[0][1] * g + |
833 | 0 | c->rgb2xyz.mat[0][2] * b >> 12; |
834 | 0 | y = c->rgb2xyz.mat[1][0] * r + |
835 | 0 | c->rgb2xyz.mat[1][1] * g + |
836 | 0 | c->rgb2xyz.mat[1][2] * b >> 12; |
837 | 0 | z = c->rgb2xyz.mat[2][0] * r + |
838 | 0 | c->rgb2xyz.mat[2][1] * g + |
839 | 0 | c->rgb2xyz.mat[2][2] * b >> 12; |
840 | | |
841 | | // limit values to 16-bit depth |
842 | 0 | x = av_clip_uint16(x); |
843 | 0 | y = av_clip_uint16(y); |
844 | 0 | z = av_clip_uint16(z); |
845 | | |
846 | | // convert from XYZlinear to X'Y'Z' and scale from 12bit to 16bit |
847 | 0 | if (desc->flags & AV_PIX_FMT_FLAG_BE) { |
848 | 0 | AV_WB16(dst16 + xp + 0, c->rgb2xyz.gamma.out[x] << 4); |
849 | 0 | AV_WB16(dst16 + xp + 1, c->rgb2xyz.gamma.out[y] << 4); |
850 | 0 | AV_WB16(dst16 + xp + 2, c->rgb2xyz.gamma.out[z] << 4); |
851 | 0 | } else { |
852 | 0 | AV_WL16(dst16 + xp + 0, c->rgb2xyz.gamma.out[x] << 4); |
853 | 0 | AV_WL16(dst16 + xp + 1, c->rgb2xyz.gamma.out[y] << 4); |
854 | 0 | AV_WL16(dst16 + xp + 2, c->rgb2xyz.gamma.out[z] << 4); |
855 | 0 | } |
856 | 0 | } |
857 | |
|
858 | 0 | src += src_stride; |
859 | 0 | dst += dst_stride; |
860 | 0 | } |
861 | 0 | } |
862 | | |
863 | | av_cold void ff_sws_init_xyzdsp(SwsInternal *c) |
864 | 0 | { |
865 | 0 | c->xyz12Torgb48 = xyz12Torgb48_c; |
866 | 0 | c->rgb48Toxyz12 = rgb48Toxyz12_c; |
867 | |
|
868 | | #if ARCH_AARCH64 |
869 | | ff_sws_init_xyzdsp_aarch64(c); |
870 | | #endif |
871 | 0 | } |
872 | | |
873 | | void ff_update_palette(SwsInternal *c, const uint32_t *pal) |
874 | 0 | { |
875 | 0 | uint32_t *rgb2yuv = c->input_rgb2yuv_table; |
876 | |
|
877 | 0 | int32_t ry = rgb2yuv[RY_IDX], gy = rgb2yuv[GY_IDX], by = rgb2yuv[BY_IDX]; |
878 | 0 | int32_t ru = rgb2yuv[RU_IDX], gu = rgb2yuv[GU_IDX], bu = rgb2yuv[BU_IDX]; |
879 | 0 | int32_t rv = rgb2yuv[RV_IDX], gv = rgb2yuv[GV_IDX], bv = rgb2yuv[BV_IDX]; |
880 | |
|
881 | 0 | for (int i = 0; i < 256; i++) { |
882 | 0 | int r, g, b, y, u, v, a = 0xff; |
883 | 0 | if (c->opts.src_format == AV_PIX_FMT_PAL8) { |
884 | 0 | uint32_t p = pal[i]; |
885 | 0 | a = (p >> 24) & 0xFF; |
886 | 0 | r = (p >> 16) & 0xFF; |
887 | 0 | g = (p >> 8) & 0xFF; |
888 | 0 | b = p & 0xFF; |
889 | 0 | } else if (c->opts.src_format == AV_PIX_FMT_RGB8) { |
890 | 0 | r = ( i >> 5 ) * 36; |
891 | 0 | g = ((i >> 2) & 7) * 36; |
892 | 0 | b = ( i & 3) * 85; |
893 | 0 | } else if (c->opts.src_format == AV_PIX_FMT_BGR8) { |
894 | 0 | b = ( i >> 6 ) * 85; |
895 | 0 | g = ((i >> 3) & 7) * 36; |
896 | 0 | r = ( i & 7) * 36; |
897 | 0 | } else if (c->opts.src_format == AV_PIX_FMT_RGB4_BYTE) { |
898 | 0 | r = ( i >> 3 ) * 255; |
899 | 0 | g = ((i >> 1) & 3) * 85; |
900 | 0 | b = ( i & 1) * 255; |
901 | 0 | } else if (c->opts.src_format == AV_PIX_FMT_GRAY8 || c->opts.src_format == AV_PIX_FMT_GRAY8A) { |
902 | 0 | r = g = b = i; |
903 | 0 | } else { |
904 | 0 | av_assert1(c->opts.src_format == AV_PIX_FMT_BGR4_BYTE); |
905 | 0 | b = ( i >> 3 ) * 255; |
906 | 0 | g = ((i >> 1) & 3) * 85; |
907 | 0 | r = ( i & 1) * 255; |
908 | 0 | } |
909 | |
|
910 | 0 | y = av_clip_uint8((ry * r + gy * g + by * b + ( 33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT); |
911 | 0 | u = av_clip_uint8((ru * r + gu * g + bu * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT); |
912 | 0 | v = av_clip_uint8((rv * r + gv * g + bv * b + (257 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT); |
913 | |
|
914 | 0 | c->pal_yuv[i]= y + (u<<8) + (v<<16) + ((unsigned)a<<24); |
915 | |
|
916 | 0 | switch (c->opts.dst_format) { |
917 | 0 | case AV_PIX_FMT_BGR32: |
918 | 0 | #if !HAVE_BIGENDIAN |
919 | 0 | case AV_PIX_FMT_RGB24: |
920 | 0 | #endif |
921 | 0 | c->pal_rgb[i]= r + (g<<8) + (b<<16) + ((unsigned)a<<24); |
922 | 0 | break; |
923 | 0 | case AV_PIX_FMT_BGR32_1: |
924 | | #if HAVE_BIGENDIAN |
925 | | case AV_PIX_FMT_BGR24: |
926 | | #endif |
927 | 0 | c->pal_rgb[i]= a + (r<<8) + (g<<16) + ((unsigned)b<<24); |
928 | 0 | break; |
929 | 0 | case AV_PIX_FMT_RGB32_1: |
930 | | #if HAVE_BIGENDIAN |
931 | | case AV_PIX_FMT_RGB24: |
932 | | #endif |
933 | 0 | c->pal_rgb[i]= a + (b<<8) + (g<<16) + ((unsigned)r<<24); |
934 | 0 | break; |
935 | 0 | case AV_PIX_FMT_GBRP: |
936 | 0 | case AV_PIX_FMT_GBRAP: |
937 | | #if HAVE_BIGENDIAN |
938 | | c->pal_rgb[i]= a + (r<<8) + (b<<16) + ((unsigned)g<<24); |
939 | | #else |
940 | 0 | c->pal_rgb[i]= g + (b<<8) + (r<<16) + ((unsigned)a<<24); |
941 | 0 | #endif |
942 | 0 | break; |
943 | 0 | case AV_PIX_FMT_RGB32: |
944 | 0 | #if !HAVE_BIGENDIAN |
945 | 0 | case AV_PIX_FMT_BGR24: |
946 | 0 | #endif |
947 | 0 | default: |
948 | 0 | c->pal_rgb[i]= b + (g<<8) + (r<<16) + ((unsigned)a<<24); |
949 | 0 | } |
950 | 0 | } |
951 | 0 | } |
952 | | |
953 | | static int scale_internal(SwsContext *sws, |
954 | | const uint8_t * const srcSlice[], const int srcStride[], |
955 | | int srcSliceY, int srcSliceH, |
956 | | uint8_t *const dstSlice[], const int dstStride[], |
957 | | int dstSliceY, int dstSliceH); |
958 | | |
959 | | static int scale_gamma(SwsInternal *c, |
960 | | const uint8_t * const srcSlice[], const int srcStride[], |
961 | | int srcSliceY, int srcSliceH, |
962 | | uint8_t * const dstSlice[], const int dstStride[], |
963 | | int dstSliceY, int dstSliceH) |
964 | 0 | { |
965 | 0 | int ret = scale_internal(c->cascaded_context[0], |
966 | 0 | srcSlice, srcStride, srcSliceY, srcSliceH, |
967 | 0 | c->cascaded_tmp[0], c->cascaded_tmpStride[0], 0, c->opts.src_h); |
968 | |
|
969 | 0 | if (ret < 0) |
970 | 0 | return ret; |
971 | | |
972 | 0 | if (c->cascaded_context[2]) |
973 | 0 | ret = scale_internal(c->cascaded_context[1], (const uint8_t * const *)c->cascaded_tmp[0], |
974 | 0 | c->cascaded_tmpStride[0], srcSliceY, srcSliceH, |
975 | 0 | c->cascaded_tmp[1], c->cascaded_tmpStride[1], 0, c->opts.dst_h); |
976 | 0 | else |
977 | 0 | ret = scale_internal(c->cascaded_context[1], (const uint8_t * const *)c->cascaded_tmp[0], |
978 | 0 | c->cascaded_tmpStride[0], srcSliceY, srcSliceH, |
979 | 0 | dstSlice, dstStride, dstSliceY, dstSliceH); |
980 | |
|
981 | 0 | if (ret < 0) |
982 | 0 | return ret; |
983 | | |
984 | 0 | if (c->cascaded_context[2]) { |
985 | 0 | const int dstY1 = sws_internal(c->cascaded_context[1])->dstY; |
986 | 0 | ret = scale_internal(c->cascaded_context[2], (const uint8_t * const *)c->cascaded_tmp[1], |
987 | 0 | c->cascaded_tmpStride[1], dstY1 - ret, dstY1, |
988 | 0 | dstSlice, dstStride, dstSliceY, dstSliceH); |
989 | 0 | } |
990 | 0 | return ret; |
991 | 0 | } |
992 | | |
993 | | static int scale_cascaded(SwsInternal *c, |
994 | | const uint8_t * const srcSlice[], const int srcStride[], |
995 | | int srcSliceY, int srcSliceH, |
996 | | uint8_t * const dstSlice[], const int dstStride[], |
997 | | int dstSliceY, int dstSliceH) |
998 | 0 | { |
999 | 0 | const int dstH0 = c->cascaded_context[0]->dst_h; |
1000 | 0 | int ret = scale_internal(c->cascaded_context[0], |
1001 | 0 | srcSlice, srcStride, srcSliceY, srcSliceH, |
1002 | 0 | c->cascaded_tmp[0], c->cascaded_tmpStride[0], |
1003 | 0 | 0, dstH0); |
1004 | 0 | if (ret < 0) |
1005 | 0 | return ret; |
1006 | 0 | ret = scale_internal(c->cascaded_context[1], |
1007 | 0 | (const uint8_t * const * )c->cascaded_tmp[0], c->cascaded_tmpStride[0], |
1008 | 0 | 0, dstH0, dstSlice, dstStride, dstSliceY, dstSliceH); |
1009 | 0 | return ret; |
1010 | 0 | } |
1011 | | |
1012 | | static int scale_internal(SwsContext *sws, |
1013 | | const uint8_t * const srcSlice[], const int srcStride[], |
1014 | | int srcSliceY, int srcSliceH, |
1015 | | uint8_t *const dstSlice[], const int dstStride[], |
1016 | | int dstSliceY, int dstSliceH) |
1017 | 0 | { |
1018 | 0 | SwsInternal *c = sws_internal(sws); |
1019 | 0 | const int scale_dst = dstSliceY > 0 || dstSliceH < sws->dst_h; |
1020 | 0 | const int frame_start = scale_dst || !c->sliceDir; |
1021 | 0 | int i, ret; |
1022 | 0 | const uint8_t *src2[4]; |
1023 | 0 | uint8_t *dst2[4]; |
1024 | 0 | int macro_height_src = isBayer(sws->src_format) ? 2 : (1 << c->chrSrcVSubSample); |
1025 | 0 | int macro_height_dst = isBayer(sws->dst_format) ? 2 : (1 << c->chrDstVSubSample); |
1026 | | // copy strides, so they can safely be modified |
1027 | 0 | int srcStride2[4]; |
1028 | 0 | int dstStride2[4]; |
1029 | 0 | int srcSliceY_internal = srcSliceY; |
1030 | |
|
1031 | 0 | if (!srcStride || !dstStride || !dstSlice || !srcSlice) { |
1032 | 0 | av_log(c, AV_LOG_ERROR, "One of the input parameters to sws_scale() is NULL, please check the calling code\n"); |
1033 | 0 | return AVERROR(EINVAL); |
1034 | 0 | } |
1035 | | |
1036 | 0 | if ((srcSliceY & (macro_height_src - 1)) || |
1037 | 0 | ((srcSliceH & (macro_height_src - 1)) && srcSliceY + srcSliceH != sws->src_h) || |
1038 | 0 | srcSliceY + srcSliceH > sws->src_h || |
1039 | 0 | (isBayer(sws->src_format) && srcSliceH <= 1)) { |
1040 | 0 | av_log(c, AV_LOG_ERROR, "Slice parameters %d, %d are invalid\n", srcSliceY, srcSliceH); |
1041 | 0 | return AVERROR(EINVAL); |
1042 | 0 | } |
1043 | | |
1044 | 0 | if ((dstSliceY & (macro_height_dst - 1)) || |
1045 | 0 | ((dstSliceH & (macro_height_dst - 1)) && dstSliceY + dstSliceH != sws->dst_h) || |
1046 | 0 | dstSliceY + dstSliceH > sws->dst_h) { |
1047 | 0 | av_log(c, AV_LOG_ERROR, "Slice parameters %d, %d are invalid\n", dstSliceY, dstSliceH); |
1048 | 0 | return AVERROR(EINVAL); |
1049 | 0 | } |
1050 | | |
1051 | 0 | if (!check_image_pointers(srcSlice, sws->src_format, srcStride)) { |
1052 | 0 | av_log(c, AV_LOG_ERROR, "bad src image pointers\n"); |
1053 | 0 | return AVERROR(EINVAL); |
1054 | 0 | } |
1055 | 0 | if (!check_image_pointers((const uint8_t* const*)dstSlice, sws->dst_format, dstStride)) { |
1056 | 0 | av_log(c, AV_LOG_ERROR, "bad dst image pointers\n"); |
1057 | 0 | return AVERROR(EINVAL); |
1058 | 0 | } |
1059 | | |
1060 | | // do not mess up sliceDir if we have a "trailing" 0-size slice |
1061 | 0 | if (srcSliceH == 0) |
1062 | 0 | return 0; |
1063 | | |
1064 | 0 | if (sws->gamma_flag && c->cascaded_context[0]) |
1065 | 0 | return scale_gamma(c, srcSlice, srcStride, srcSliceY, srcSliceH, |
1066 | 0 | dstSlice, dstStride, dstSliceY, dstSliceH); |
1067 | | |
1068 | 0 | if (c->cascaded_context[0] && srcSliceY == 0 && srcSliceH == c->cascaded_context[0]->src_h) |
1069 | 0 | return scale_cascaded(c, srcSlice, srcStride, srcSliceY, srcSliceH, |
1070 | 0 | dstSlice, dstStride, dstSliceY, dstSliceH); |
1071 | | |
1072 | 0 | if (!srcSliceY && (sws->flags & SWS_BITEXACT) && sws->dither == SWS_DITHER_ED && c->dither_error[0]) |
1073 | 0 | for (i = 0; i < 4; i++) |
1074 | 0 | memset(c->dither_error[i], 0, sizeof(c->dither_error[0][0]) * (sws->dst_w+2)); |
1075 | |
|
1076 | 0 | if (usePal(sws->src_format)) |
1077 | 0 | ff_update_palette(c, (const uint32_t *)srcSlice[1]); |
1078 | |
|
1079 | 0 | memcpy(src2, srcSlice, sizeof(src2)); |
1080 | 0 | memcpy(dst2, dstSlice, sizeof(dst2)); |
1081 | 0 | memcpy(srcStride2, srcStride, sizeof(srcStride2)); |
1082 | 0 | memcpy(dstStride2, dstStride, sizeof(dstStride2)); |
1083 | |
|
1084 | 0 | if (frame_start && !scale_dst) { |
1085 | 0 | if (srcSliceY != 0 && srcSliceY + srcSliceH != sws->src_h) { |
1086 | 0 | av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n"); |
1087 | 0 | return AVERROR(EINVAL); |
1088 | 0 | } |
1089 | | |
1090 | 0 | c->sliceDir = (srcSliceY == 0) ? 1 : -1; |
1091 | 0 | } else if (scale_dst) |
1092 | 0 | c->sliceDir = 1; |
1093 | | |
1094 | 0 | if (c->src0Alpha && !c->dst0Alpha && isALPHA(sws->dst_format)) { |
1095 | 0 | uint8_t *base; |
1096 | 0 | int x,y; |
1097 | |
|
1098 | 0 | av_fast_malloc(&c->rgb0_scratch, &c->rgb0_scratch_allocated, |
1099 | 0 | FFABS(srcStride[0]) * srcSliceH + 32); |
1100 | 0 | if (!c->rgb0_scratch) |
1101 | 0 | return AVERROR(ENOMEM); |
1102 | | |
1103 | 0 | base = srcStride[0] < 0 ? c->rgb0_scratch - srcStride[0] * (srcSliceH-1) : |
1104 | 0 | c->rgb0_scratch; |
1105 | 0 | for (y=0; y<srcSliceH; y++){ |
1106 | 0 | memcpy(base + srcStride[0]*y, src2[0] + srcStride[0]*y, 4*sws->src_w); |
1107 | 0 | for (x=c->src0Alpha-1; x<4*sws->src_w; x+=4) { |
1108 | 0 | base[ srcStride[0]*y + x] = 0xFF; |
1109 | 0 | } |
1110 | 0 | } |
1111 | 0 | src2[0] = base; |
1112 | 0 | } |
1113 | | |
1114 | 0 | if (c->srcXYZ && !(c->dstXYZ && sws->src_w==sws->dst_w && sws->src_h==sws->dst_h)) { |
1115 | 0 | uint8_t *base; |
1116 | |
|
1117 | 0 | av_fast_malloc(&c->xyz_scratch, &c->xyz_scratch_allocated, |
1118 | 0 | FFABS(srcStride[0]) * srcSliceH + 32); |
1119 | 0 | if (!c->xyz_scratch) |
1120 | 0 | return AVERROR(ENOMEM); |
1121 | | |
1122 | 0 | base = srcStride[0] < 0 ? c->xyz_scratch - srcStride[0] * (srcSliceH-1) : |
1123 | 0 | c->xyz_scratch; |
1124 | |
|
1125 | 0 | c->xyz12Torgb48(c, base, srcStride[0], src2[0], srcStride[0], sws->src_w, srcSliceH); |
1126 | 0 | src2[0] = base; |
1127 | 0 | } |
1128 | | |
1129 | 0 | if (c->sliceDir != 1) { |
1130 | | // slices go from bottom to top => we flip the image internally |
1131 | 0 | for (i=0; i<4; i++) { |
1132 | 0 | srcStride2[i] *= -1; |
1133 | 0 | dstStride2[i] *= -1; |
1134 | 0 | } |
1135 | |
|
1136 | 0 | src2[0] += (srcSliceH - 1) * srcStride[0]; |
1137 | 0 | if (!usePal(sws->src_format)) |
1138 | 0 | src2[1] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[1]; |
1139 | 0 | src2[2] += ((srcSliceH >> c->chrSrcVSubSample) - 1) * srcStride[2]; |
1140 | 0 | src2[3] += (srcSliceH - 1) * srcStride[3]; |
1141 | 0 | dst2[0] += ( sws->dst_h - 1) * dstStride[0]; |
1142 | 0 | dst2[1] += ((sws->dst_h >> c->chrDstVSubSample) - 1) * dstStride[1]; |
1143 | 0 | dst2[2] += ((sws->dst_h >> c->chrDstVSubSample) - 1) * dstStride[2]; |
1144 | 0 | dst2[3] += ( sws->dst_h - 1) * dstStride[3]; |
1145 | |
|
1146 | 0 | srcSliceY_internal = sws->src_h-srcSliceY-srcSliceH; |
1147 | 0 | } |
1148 | 0 | reset_ptr(src2, sws->src_format); |
1149 | 0 | reset_ptr((void*)dst2, sws->dst_format); |
1150 | |
|
1151 | 0 | if (c->convert_unscaled) { |
1152 | 0 | int offset = srcSliceY_internal; |
1153 | 0 | int slice_h = srcSliceH; |
1154 | | |
1155 | | // for dst slice scaling, offset the pointers to match the unscaled API |
1156 | 0 | if (scale_dst) { |
1157 | 0 | av_assert0(offset == 0); |
1158 | 0 | for (i = 0; i < 4 && src2[i]; i++) { |
1159 | 0 | if (!src2[i] || (i > 0 && usePal(sws->src_format))) |
1160 | 0 | break; |
1161 | 0 | src2[i] += (dstSliceY >> ((i == 1 || i == 2) ? c->chrSrcVSubSample : 0)) * srcStride2[i]; |
1162 | 0 | } |
1163 | |
|
1164 | 0 | for (i = 0; i < 4 && dst2[i]; i++) { |
1165 | 0 | if (!dst2[i] || (i > 0 && usePal(sws->dst_format))) |
1166 | 0 | break; |
1167 | 0 | dst2[i] -= (dstSliceY >> ((i == 1 || i == 2) ? c->chrDstVSubSample : 0)) * dstStride2[i]; |
1168 | 0 | } |
1169 | 0 | offset = dstSliceY; |
1170 | 0 | slice_h = dstSliceH; |
1171 | 0 | } |
1172 | | |
1173 | 0 | ret = c->convert_unscaled(c, src2, srcStride2, offset, slice_h, |
1174 | 0 | dst2, dstStride2); |
1175 | 0 | if (scale_dst) |
1176 | 0 | dst2[0] += dstSliceY * dstStride2[0]; |
1177 | 0 | } else { |
1178 | 0 | ret = ff_swscale(c, src2, srcStride2, srcSliceY_internal, srcSliceH, |
1179 | 0 | dst2, dstStride2, dstSliceY, dstSliceH); |
1180 | 0 | } |
1181 | | |
1182 | 0 | if (c->dstXYZ && !(c->srcXYZ && sws->src_w==sws->dst_w && sws->src_h==sws->dst_h)) { |
1183 | 0 | uint8_t *dst; |
1184 | |
|
1185 | 0 | if (scale_dst) { |
1186 | 0 | dst = dst2[0]; |
1187 | 0 | } else { |
1188 | 0 | int dstY = c->dstY ? c->dstY : srcSliceY + srcSliceH; |
1189 | |
|
1190 | 0 | av_assert0(dstY >= ret); |
1191 | 0 | av_assert0(ret >= 0); |
1192 | 0 | av_assert0(sws->dst_h >= dstY); |
1193 | 0 | dst = dst2[0] + (dstY - ret) * dstStride2[0]; |
1194 | 0 | } |
1195 | | |
1196 | | /* replace on the same data */ |
1197 | 0 | c->rgb48Toxyz12(c, dst, dstStride2[0], dst, dstStride2[0], sws->dst_w, ret); |
1198 | 0 | } |
1199 | | |
1200 | | /* reset slice direction at end of frame */ |
1201 | 0 | if ((srcSliceY_internal + srcSliceH == sws->src_h) || scale_dst) |
1202 | 0 | c->sliceDir = 0; |
1203 | |
|
1204 | 0 | return ret; |
1205 | 0 | } |
1206 | | |
1207 | | void sws_frame_end(SwsContext *sws) |
1208 | 0 | { |
1209 | 0 | SwsInternal *c = sws_internal(sws); |
1210 | 0 | if (!c->is_legacy_init) |
1211 | 0 | return; |
1212 | 0 | av_frame_unref(c->frame_src); |
1213 | 0 | av_frame_unref(c->frame_dst); |
1214 | 0 | c->src_ranges.nb_ranges = 0; |
1215 | 0 | } |
1216 | | |
1217 | | static int frame_alloc_buffers(SwsContext *sws, AVFrame *frame) |
1218 | 0 | { |
1219 | 0 | SwsInternal *c = sws_internal(sws); |
1220 | 0 | FFFramePool *pool = &c->frame_pool; |
1221 | |
|
1222 | 0 | av_assert0(!frame->hw_frames_ctx); |
1223 | 0 | const int nb_planes = av_pix_fmt_count_planes(frame->format); |
1224 | 0 | for (int i = 0; i < nb_planes; i++) { |
1225 | 0 | frame->linesize[i] = pool->linesize[i]; |
1226 | 0 | frame->buf[i] = av_buffer_pool_get(pool->pools[i]); |
1227 | 0 | if (!frame->buf[i]) { |
1228 | 0 | av_frame_unref(frame); |
1229 | 0 | return AVERROR(ENOMEM); |
1230 | 0 | } |
1231 | 0 | frame->data[i] = frame->buf[i]->data; |
1232 | 0 | } |
1233 | | |
1234 | 0 | return 0; |
1235 | 0 | } |
1236 | | |
1237 | | int sws_frame_start(SwsContext *sws, AVFrame *dst, const AVFrame *src) |
1238 | 0 | { |
1239 | 0 | SwsInternal *c = sws_internal(sws); |
1240 | 0 | int ret, allocated = 0; |
1241 | 0 | if (!c->is_legacy_init) |
1242 | 0 | return AVERROR(EINVAL); |
1243 | | |
1244 | 0 | ret = av_frame_ref(c->frame_src, src); |
1245 | 0 | if (ret < 0) |
1246 | 0 | return ret; |
1247 | | |
1248 | 0 | if (!dst->buf[0]) { |
1249 | 0 | dst->width = sws->dst_w; |
1250 | 0 | dst->height = sws->dst_h; |
1251 | 0 | dst->format = sws->dst_format; |
1252 | |
|
1253 | 0 | ret = av_frame_get_buffer(dst, 0); |
1254 | 0 | if (ret < 0) |
1255 | 0 | return ret; |
1256 | 0 | allocated = 1; |
1257 | 0 | } |
1258 | | |
1259 | 0 | ret = av_frame_ref(c->frame_dst, dst); |
1260 | 0 | if (ret < 0) { |
1261 | 0 | if (allocated) |
1262 | 0 | av_frame_unref(dst); |
1263 | |
|
1264 | 0 | return ret; |
1265 | 0 | } |
1266 | | |
1267 | 0 | return 0; |
1268 | 0 | } |
1269 | | |
1270 | | int sws_send_slice(SwsContext *sws, unsigned int slice_start, |
1271 | | unsigned int slice_height) |
1272 | 0 | { |
1273 | 0 | SwsInternal *c = sws_internal(sws); |
1274 | 0 | int ret; |
1275 | 0 | if (!c->is_legacy_init) |
1276 | 0 | return AVERROR(EINVAL); |
1277 | | |
1278 | 0 | ret = ff_range_add(&c->src_ranges, slice_start, slice_height); |
1279 | 0 | if (ret < 0) |
1280 | 0 | return ret; |
1281 | | |
1282 | 0 | return 0; |
1283 | 0 | } |
1284 | | |
1285 | | unsigned int sws_receive_slice_alignment(const SwsContext *sws) |
1286 | 0 | { |
1287 | 0 | SwsInternal *c = sws_internal(sws); |
1288 | 0 | if (c->slice_ctx) |
1289 | 0 | return sws_internal(c->slice_ctx[0])->dst_slice_align; |
1290 | | |
1291 | 0 | return c->dst_slice_align; |
1292 | 0 | } |
1293 | | |
1294 | | int sws_receive_slice(SwsContext *sws, unsigned int slice_start, |
1295 | | unsigned int slice_height) |
1296 | 0 | { |
1297 | 0 | SwsInternal *c = sws_internal(sws); |
1298 | 0 | unsigned int align = sws_receive_slice_alignment(sws); |
1299 | 0 | uint8_t *dst[4]; |
1300 | 0 | if (!c->is_legacy_init) |
1301 | 0 | return AVERROR(EINVAL); |
1302 | | |
1303 | | /* wait until complete input has been received */ |
1304 | 0 | if (!(c->src_ranges.nb_ranges == 1 && |
1305 | 0 | c->src_ranges.ranges[0].start == 0 && |
1306 | 0 | c->src_ranges.ranges[0].len == sws->src_h)) |
1307 | 0 | return AVERROR(EAGAIN); |
1308 | | |
1309 | 0 | if ((slice_start > 0 || slice_height < sws->dst_h) && |
1310 | 0 | (slice_start % align || slice_height % align)) { |
1311 | 0 | av_log(c, AV_LOG_ERROR, |
1312 | 0 | "Incorrectly aligned output: %u/%u not multiples of %u\n", |
1313 | 0 | slice_start, slice_height, align); |
1314 | 0 | return AVERROR(EINVAL); |
1315 | 0 | } |
1316 | | |
1317 | 0 | if (c->slicethread) { |
1318 | 0 | int nb_jobs = c->nb_slice_ctx; |
1319 | 0 | int ret = 0; |
1320 | |
|
1321 | 0 | if (c->slice_ctx[0]->dither == SWS_DITHER_ED) |
1322 | 0 | nb_jobs = 1; |
1323 | |
|
1324 | 0 | c->dst_slice_start = slice_start; |
1325 | 0 | c->dst_slice_height = slice_height; |
1326 | |
|
1327 | 0 | avpriv_slicethread_execute(c->slicethread, nb_jobs, 0); |
1328 | |
|
1329 | 0 | for (int i = 0; i < c->nb_slice_ctx; i++) { |
1330 | 0 | if (c->slice_err[i] < 0) { |
1331 | 0 | ret = c->slice_err[i]; |
1332 | 0 | break; |
1333 | 0 | } |
1334 | 0 | } |
1335 | |
|
1336 | 0 | memset(c->slice_err, 0, c->nb_slice_ctx * sizeof(*c->slice_err)); |
1337 | |
|
1338 | 0 | return ret; |
1339 | 0 | } |
1340 | | |
1341 | 0 | for (int i = 0; i < FF_ARRAY_ELEMS(dst); i++) { |
1342 | 0 | ptrdiff_t offset = c->frame_dst->linesize[i] * (ptrdiff_t)(slice_start >> c->chrDstVSubSample); |
1343 | 0 | dst[i] = FF_PTR_ADD(c->frame_dst->data[i], offset); |
1344 | 0 | } |
1345 | |
|
1346 | 0 | return scale_internal(sws, (const uint8_t * const *)c->frame_src->data, |
1347 | 0 | c->frame_src->linesize, 0, sws->src_h, |
1348 | 0 | dst, c->frame_dst->linesize, slice_start, slice_height); |
1349 | 0 | } |
1350 | | |
1351 | | /* Subset of av_frame_ref() that only references (video) data buffers */ |
1352 | | static int frame_ref(AVFrame *dst, const AVFrame *src) |
1353 | 0 | { |
1354 | | /* ref the buffers */ |
1355 | 0 | for (int i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) { |
1356 | 0 | if (!src->buf[i]) |
1357 | 0 | break; |
1358 | 0 | dst->buf[i] = av_buffer_ref(src->buf[i]); |
1359 | 0 | if (!dst->buf[i]) |
1360 | 0 | return AVERROR(ENOMEM); |
1361 | 0 | } |
1362 | | |
1363 | 0 | memcpy(dst->data, src->data, sizeof(src->data)); |
1364 | 0 | memcpy(dst->linesize, src->linesize, sizeof(src->linesize)); |
1365 | 0 | return 0; |
1366 | 0 | } |
1367 | | |
1368 | | int sws_scale_frame(SwsContext *sws, AVFrame *dst, const AVFrame *src) |
1369 | 0 | { |
1370 | 0 | int ret, allocated = 0; |
1371 | 0 | SwsInternal *c = sws_internal(sws); |
1372 | 0 | if (!src || !dst) |
1373 | 0 | return AVERROR(EINVAL); |
1374 | | |
1375 | 0 | if (c->is_legacy_init) { |
1376 | | /* Context has been initialized with explicit values, fall back to |
1377 | | * legacy API behavior. */ |
1378 | 0 | ret = sws_frame_start(sws, dst, src); |
1379 | 0 | if (ret < 0) |
1380 | 0 | return ret; |
1381 | | |
1382 | 0 | ret = sws_send_slice(sws, 0, src->height); |
1383 | 0 | if (ret >= 0) |
1384 | 0 | ret = sws_receive_slice(sws, 0, dst->height); |
1385 | |
|
1386 | 0 | sws_frame_end(sws); |
1387 | |
|
1388 | 0 | return ret; |
1389 | 0 | } |
1390 | | |
1391 | 0 | ret = sws_frame_setup(sws, dst, src); |
1392 | 0 | if (ret < 0) |
1393 | 0 | return ret; |
1394 | | |
1395 | 0 | if (!src->data[0]) |
1396 | 0 | return 0; |
1397 | | |
1398 | 0 | const SwsGraph *top = c->graph[FIELD_TOP]; |
1399 | 0 | const SwsGraph *bot = c->graph[FIELD_BOTTOM]; |
1400 | 0 | if (dst->data[0]) /* user-provided buffers */ |
1401 | 0 | goto process_frame; |
1402 | | |
1403 | | /* Sanity */ |
1404 | 0 | memset(dst->buf, 0, sizeof(dst->buf)); |
1405 | 0 | memset(dst->data, 0, sizeof(dst->data)); |
1406 | 0 | memset(dst->linesize, 0, sizeof(dst->linesize)); |
1407 | 0 | dst->extended_data = dst->data; |
1408 | |
|
1409 | 0 | if (src->buf[0] && top->noop && (!bot || bot->noop)) |
1410 | 0 | return frame_ref(dst, src); |
1411 | | |
1412 | 0 | ret = frame_alloc_buffers(sws, dst); |
1413 | 0 | if (ret < 0) |
1414 | 0 | return ret; |
1415 | 0 | allocated = 1; |
1416 | |
|
1417 | 0 | process_frame: |
1418 | 0 | for (int field = 0; field < (bot ? 2 : 1); field++) { |
1419 | 0 | ret = ff_sws_graph_run(c->graph[field], dst, src); |
1420 | 0 | if (ret < 0) { |
1421 | 0 | if (allocated) |
1422 | 0 | av_frame_unref(dst); |
1423 | 0 | return ret; |
1424 | 0 | } |
1425 | 0 | } |
1426 | | |
1427 | 0 | return 0; |
1428 | 0 | } |
1429 | | |
1430 | | static int validate_params(SwsContext *ctx) |
1431 | 0 | { |
1432 | 0 | #define VALIDATE(field, min, max) \ |
1433 | 0 | if (ctx->field < min || ctx->field > max) { \ |
1434 | 0 | av_log(ctx, AV_LOG_ERROR, "'%s' (%d) out of range [%d, %d]\n", \ |
1435 | 0 | #field, (int) ctx->field, min, max); \ |
1436 | 0 | return AVERROR(EINVAL); \ |
1437 | 0 | } |
1438 | |
|
1439 | 0 | VALIDATE(threads, 0, SWS_MAX_THREADS); |
1440 | 0 | VALIDATE(dither, 0, SWS_DITHER_NB - 1) |
1441 | 0 | VALIDATE(alpha_blend, 0, SWS_ALPHA_BLEND_NB - 1) |
1442 | 0 | return 0; |
1443 | 0 | } |
1444 | | |
1445 | | int sws_frame_setup(SwsContext *ctx, const AVFrame *dst, const AVFrame *src) |
1446 | 0 | { |
1447 | 0 | SwsInternal *s = sws_internal(ctx); |
1448 | 0 | const char *err_msg; |
1449 | 0 | int ret; |
1450 | |
|
1451 | 0 | if (!src || !dst) |
1452 | 0 | return AVERROR(EINVAL); |
1453 | 0 | if ((ret = validate_params(ctx)) < 0) |
1454 | 0 | return ret; |
1455 | | |
1456 | | /* For now, if a single frame has a context, then both need a context */ |
1457 | 0 | if (!!src->hw_frames_ctx != !!dst->hw_frames_ctx) { |
1458 | 0 | return AVERROR(ENOTSUP); |
1459 | 0 | } else if (!!src->hw_frames_ctx) { |
1460 | | /* Both hardware frames must already be allocated */ |
1461 | 0 | if (!src->data[0] || !dst->data[0]) |
1462 | 0 | return AVERROR(EINVAL); |
1463 | | |
1464 | 0 | AVHWFramesContext *src_hwfc, *dst_hwfc; |
1465 | 0 | src_hwfc = (AVHWFramesContext *)src->hw_frames_ctx->data; |
1466 | 0 | dst_hwfc = (AVHWFramesContext *)dst->hw_frames_ctx->data; |
1467 | | |
1468 | | /* Both frames must live on the same device */ |
1469 | 0 | if (src_hwfc->device_ref->data != dst_hwfc->device_ref->data) |
1470 | 0 | return AVERROR(EINVAL); |
1471 | | |
1472 | | /* Only Vulkan devices are supported */ |
1473 | 0 | AVHWDeviceContext *dev_ctx; |
1474 | 0 | dev_ctx = (AVHWDeviceContext *)src_hwfc->device_ref->data; |
1475 | 0 | if (dev_ctx->type != AV_HWDEVICE_TYPE_VULKAN) |
1476 | 0 | return AVERROR(ENOTSUP); |
1477 | |
|
1478 | | #if CONFIG_UNSTABLE && CONFIG_VULKAN |
1479 | | ret = ff_sws_vk_init(ctx, src_hwfc->device_ref); |
1480 | | if (ret < 0) |
1481 | | return ret; |
1482 | | #endif |
1483 | 0 | } |
1484 | | |
1485 | 0 | int dst_width = dst->width; |
1486 | 0 | for (int field = 0; field < 2; field++) { |
1487 | 0 | SwsFormat src_fmt = ff_fmt_from_frame(src, field); |
1488 | 0 | SwsFormat dst_fmt = ff_fmt_from_frame(dst, field); |
1489 | 0 | int src_ok, dst_ok; |
1490 | |
|
1491 | 0 | if ((src->flags ^ dst->flags) & AV_FRAME_FLAG_INTERLACED) { |
1492 | 0 | err_msg = "Cannot convert interlaced to progressive frames or vice versa.\n"; |
1493 | 0 | ret = AVERROR(EINVAL); |
1494 | 0 | goto fail; |
1495 | 0 | } |
1496 | | |
1497 | 0 | src_ok = ff_test_fmt(&src_fmt, 0); |
1498 | 0 | dst_ok = ff_test_fmt(&dst_fmt, 1); |
1499 | 0 | if ((!src_ok || !dst_ok) && !ff_props_equal(&src_fmt, &dst_fmt)) { |
1500 | 0 | err_msg = src_ok ? "Unsupported output" : "Unsupported input"; |
1501 | 0 | ret = AVERROR(ENOTSUP); |
1502 | 0 | goto fail; |
1503 | 0 | } |
1504 | | |
1505 | 0 | ret = ff_sws_graph_reinit(ctx, &dst_fmt, &src_fmt, field, &s->graph[field]); |
1506 | 0 | if (ret < 0) { |
1507 | 0 | err_msg = "Failed initializing scaling graph"; |
1508 | 0 | goto fail; |
1509 | 0 | } |
1510 | | |
1511 | 0 | const SwsGraph *graph = s->graph[field]; |
1512 | 0 | if (graph->incomplete && ctx->flags & SWS_STRICT) { |
1513 | 0 | err_msg = "Incomplete scaling graph"; |
1514 | 0 | ret = AVERROR(EINVAL); |
1515 | 0 | goto fail; |
1516 | 0 | } |
1517 | | |
1518 | 0 | if (!graph->noop) { |
1519 | 0 | av_assert0(graph->num_passes); |
1520 | 0 | const SwsPass *last_pass = graph->passes[graph->num_passes - 1]; |
1521 | 0 | const int aligned_w = ff_sws_pass_aligned_width(last_pass, dst->width); |
1522 | 0 | dst_width = FFMAX(dst_width, aligned_w); |
1523 | 0 | } |
1524 | | |
1525 | 0 | if (!src_fmt.interlaced) { |
1526 | 0 | ff_sws_graph_free(&s->graph[FIELD_BOTTOM]); |
1527 | 0 | break; |
1528 | 0 | } |
1529 | | |
1530 | 0 | continue; |
1531 | | |
1532 | 0 | fail: |
1533 | 0 | av_log(ctx, AV_LOG_ERROR, "%s (%s): fmt:%s csp:%s prim:%s trc:%s ->" |
1534 | 0 | " fmt:%s csp:%s prim:%s trc:%s\n", |
1535 | 0 | err_msg, av_err2str(ret), |
1536 | 0 | av_get_pix_fmt_name(src_fmt.format), av_color_space_name(src_fmt.csp), |
1537 | 0 | av_color_primaries_name(src_fmt.color.prim), av_color_transfer_name(src_fmt.color.trc), |
1538 | 0 | av_get_pix_fmt_name(dst_fmt.format), av_color_space_name(dst_fmt.csp), |
1539 | 0 | av_color_primaries_name(dst_fmt.color.prim), av_color_transfer_name(dst_fmt.color.trc)); |
1540 | |
|
1541 | 0 | for (int i = 0; i < FF_ARRAY_ELEMS(s->graph); i++) |
1542 | 0 | ff_sws_graph_free(&s->graph[i]); |
1543 | |
|
1544 | 0 | return ret; |
1545 | 0 | } |
1546 | | |
1547 | 0 | if (!dst->hw_frames_ctx) { |
1548 | 0 | ret = ff_frame_pool_video_reinit(&s->frame_pool, dst_width, dst->height, |
1549 | 0 | dst->format, av_cpu_max_align()); |
1550 | 0 | if (ret < 0) |
1551 | 0 | return ret; |
1552 | 0 | } |
1553 | | |
1554 | 0 | return 0; |
1555 | 0 | } |
1556 | | |
1557 | | /** |
1558 | | * swscale wrapper, so we don't need to export the SwsContext. |
1559 | | * Assumes planar YUV to be in YUV order instead of YVU. |
1560 | | */ |
1561 | | int attribute_align_arg sws_scale(SwsContext *sws, |
1562 | | const uint8_t * const srcSlice[], |
1563 | | const int srcStride[], int srcSliceY, |
1564 | | int srcSliceH, uint8_t *const dst[], |
1565 | | const int dstStride[]) |
1566 | 0 | { |
1567 | 0 | SwsInternal *c = sws_internal(sws); |
1568 | 0 | if (!c->is_legacy_init) |
1569 | 0 | return AVERROR(EINVAL); |
1570 | | |
1571 | 0 | if (c->nb_slice_ctx) { |
1572 | 0 | sws = c->slice_ctx[0]; |
1573 | 0 | c = sws_internal(sws); |
1574 | 0 | } |
1575 | |
|
1576 | 0 | return scale_internal(sws, srcSlice, srcStride, srcSliceY, srcSliceH, |
1577 | 0 | dst, dstStride, 0, sws->dst_h); |
1578 | 0 | } |
1579 | | |
1580 | | void ff_sws_slice_worker(void *priv, int jobnr, int threadnr, |
1581 | | int nb_jobs, int nb_threads) |
1582 | 0 | { |
1583 | 0 | SwsInternal *parent = priv; |
1584 | 0 | SwsContext *sws = parent->slice_ctx[threadnr]; |
1585 | 0 | SwsInternal *c = sws_internal(sws); |
1586 | |
|
1587 | 0 | const int slice_height = FFALIGN(FFMAX((parent->dst_slice_height + nb_jobs - 1) / nb_jobs, 1), |
1588 | 0 | c->dst_slice_align); |
1589 | 0 | const int slice_start = jobnr * slice_height; |
1590 | 0 | const int slice_end = FFMIN((jobnr + 1) * slice_height, parent->dst_slice_height); |
1591 | 0 | int err = 0; |
1592 | |
|
1593 | 0 | if (slice_end > slice_start) { |
1594 | 0 | uint8_t *dst[4] = { NULL }; |
1595 | |
|
1596 | 0 | for (int i = 0; i < FF_ARRAY_ELEMS(dst) && parent->frame_dst->data[i]; i++) { |
1597 | 0 | const int vshift = (i == 1 || i == 2) ? c->chrDstVSubSample : 0; |
1598 | 0 | const ptrdiff_t offset = parent->frame_dst->linesize[i] * |
1599 | 0 | (ptrdiff_t)((slice_start + parent->dst_slice_start) >> vshift); |
1600 | |
|
1601 | 0 | dst[i] = parent->frame_dst->data[i] + offset; |
1602 | 0 | } |
1603 | |
|
1604 | 0 | err = scale_internal(sws, (const uint8_t * const *)parent->frame_src->data, |
1605 | 0 | parent->frame_src->linesize, 0, sws->src_h, |
1606 | 0 | dst, parent->frame_dst->linesize, |
1607 | 0 | parent->dst_slice_start + slice_start, slice_end - slice_start); |
1608 | 0 | } |
1609 | |
|
1610 | 0 | parent->slice_err[threadnr] = err; |
1611 | 0 | } |