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