Files
FFmpeg/libswscale/ops_internal.h
Niklas Haas 3743d2851d swscale/ops: switch from AVRational to AVRational64
This has two immediate consequences:

1. Fixes overflow in the range tracker for some 32-bit packed formats:

 rgb24 -> v30xbe:
   [ u8 +++X] SWS_OP_READ         : 3 elem(s) packed >> 0
     min: {0 0 0 _}, max: {255 255 255 _}
   [ u8 +++X] SWS_OP_CONVERT      : u8 -> f32
     min: {0 0 0 _}, max: {255 255 255 _}
   [f32 ...X] SWS_OP_LINEAR       : matrix3+off3 [...]
     min: {64 64 64 _}, max: {940 960 960 _}
   [f32 ...X] SWS_OP_DITHER       : 16x16 matrix + {0 3 2 -1}
     min: {64.001953 64.001953 64.001953 _}, max: {940.998047 960.998047 960.998047 _}
   [f32 +++X] SWS_OP_CONVERT      : f32 -> u32
     min: {64 64 64 _}, max: {940 960 960 _}
   [u32 +++X] SWS_OP_SWIZZLE      : 2013
     min: {64 64 64 _}, max: {960 940 960 _}
   [u32 ++++] SWS_OP_CLEAR        : {_ _ _ 1}
     min: {64 64 64 1}, max: {960 940 960 1}
   [u32 +XXX] SWS_OP_PACK         : {10 10 10 2}
-    min: {268697857 _ _ _}, max: {-264581375 _ _ _}
+    min: {268697857 _ _ _}, max: {4030385921 _ _ _}
   [u32 zXXX] SWS_OP_SWAP_BYTES
-    min: {268697857 _ _ _}, max: {-264581375 _ _ _}
+    min: {268697857 _ _ _}, max: {4030385921 _ _ _}

2. Slightly increases the accuracy of intermediate values for some linear ops:

 yuv444p10be -> rgb48be:
   [u16 zzzX] SWS_OP_READ         : 3 elem(s) planar >> 0
     min: {0 0 0 _}, max: {1023 1023 1023 _}
   [u16 +++X] SWS_OP_SWAP_BYTES
     min: {0 0 0 _}, max: {1023 1023 1023 _}
   [u16 +++X] SWS_OP_CONVERT      : u16 -> f32
     min: {0 0 0 _}, max: {1023 1023 1023 _}
   [f32 ...X] SWS_OP_LINEAR       : matrix3+off3 [...]
46.813777] [0 0 0 1 0]]
-    min: {-57290.842348 -44341.337325 -71146.813777 _}, max: {124144.718860 111375.162457 137973.627845 _}
+    min: {-57290.842348 -44341.337326 -71146.813777 _}, max: {124144.718860 111375.162457 137973.627845 _}
   [f32 ...X] SWS_OP_MAX          : {0 0 0 _} <= x
     min: {0 0 0 _}, max: {124144.718860 111375.162457 137973.627845 _}
   [f32 ...X] SWS_OP_MIN          : x <= {65535 65535 65535 _}
     min: {0 0 0 _}, max: {65535 65535 65535 _}
   [f32 +++X] SWS_OP_CONVERT      : f32 -> u16
     min: {0 0 0 _}, max: {65535 65535 65535 _}
   [u16 zzzX] SWS_OP_SWAP_BYTES
     min: {0 0 0 _}, max: {65535 65535 65535 _}
   [u16 XXXX] SWS_OP_WRITE        : 3 elem(s) packed >> 0
     (X = unused, z = byteswapped, + = exact, 0 = zero)

Importantly, none of the changes affect the actual operation list, just the
range tracking metadata.

Sponsored-by: Sovereign Tech Fund
Signed-off-by: Niklas Haas <git@haasn.dev>
2026-06-25 01:20:44 +02:00

114 lines
4.3 KiB
C

/**
* Copyright (C) 2025 Niklas Haas
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef SWSCALE_OPS_INTERNAL_H
#define SWSCALE_OPS_INTERNAL_H
#include "libavutil/mem_internal.h"
#include "ops.h"
#include "ops_dispatch.h"
#define Q(N) ((AVRational64) { N, 1 })
static inline AVRational64 ff_sws_pixel_expand(SwsPixelType from, SwsPixelType to)
{
const int src = ff_sws_pixel_type_size(from);
const int dst = ff_sws_pixel_type_size(to);
if (src > dst)
return Q(0);
int scale = 1;
for (int i = 1; i < dst / src; i++)
scale = (scale << (src * 8)) | 1;
return Q(scale);
}
static inline void ff_sws_pack_op_decode(const SwsOp *op, uint64_t mask[4], int shift[4])
{
int size = 0;
for (int i = 0; i < 4; i++)
size += op->pack.pattern[i];
for (int i = 0; i < 4; i++) {
const int bits = op->pack.pattern[i];
mask[i] = (UINT64_C(1) << bits) - 1;
shift[i] = (i ? shift[i - 1] : size) - bits;
}
}
/**
* "Solve" an op list into a fixed shuffle mask, with an optional ability to
* also directly clear the output value (for e.g. rgb24 -> rgb0). This can
* accept any operation chain that only consists of the following operations:
*
* - SWS_OP_READ (non-planar, non-fractional)
* - SWS_OP_SWIZZLE
* - SWS_OP_SWAP_BYTES
* - SWS_OP_CLEAR to zero (when clear_val is specified)
* - SWS_OP_CONVERT (integer expand)
* - SWS_OP_WRITE (non-planar, non-fractional)
*
* Basically, any operation that purely consists of moving around and reordering
* bytes within a single plane, can be turned into a shuffle mask.
*
* @param ops The operation list to decompose.
* @param shuffle The output shuffle mask.
* @param size The size (in bytes) of the output shuffle mask.
* @param clear_val If nonzero, this index will be used to clear the output.
* @param read_bytes Returns the number of bytes read per shuffle iteration.
* @param write_bytes Returns the number of bytes written per shuffle iteration.
*
* @return The number of pixels processed per iteration, or a negative error
code; in particular AVERROR(ENOTSUP) for unsupported operations.
*/
int ff_sws_solve_shuffle(const SwsOpList *ops, uint8_t shuffle[], int size,
uint8_t clear_val, int *read_bytes, int *write_bytes);
/**
* Split an op list into two at the given index. The split will be mediated
* by a set of planar read/write operations, plus a swizzle (if necessary)
* to re-order only used components. If a split is performed, both output
* lists will be optimized before returning.
*
* @param ops1 The first part of the split op list. Will be modified in-place.
* @param ops2 The second part of the split op list will be returned here, or
* NULL if no split was necessary.
* @param index The index of the operation to split before. The operation
* itself will be absent from `ops1` and instead moved to the
* start of `ops2`.
*
* Returnse 0 or a negative error code.
*/
int ff_sws_op_list_split_at(SwsOpList *ops1, SwsOpList **ops2, int index);
/**
* Reduce an op list into a reduced subset that operates only on a given
* subset of planes. No effect if the output is not planar, or if the plane
* mask is empty or equal to all planes.
*
* @param ops1 Updated in-place to contain only the selected planes.
* @param ops2 The removed remainder is returned here, or NULL if no-op.
* @param planes A mask of the plane indices to keep.
*
* Returns 0 or a negative error code.
*/
int ff_sws_op_list_split_planes(SwsOpList *ops1, SwsOpList **ops2, SwsCompMask planes);
#endif /* SWSCALE_OPS_INTERNAL_H */