[FFmpeg-devel] [PATCH] swscale/utils: split off format code into new file

Ramiro Polla ramiro.polla at gmail.com
Tue Mar 11 01:03:28 EET 2025


On 3/10/25 16:18, Niklas Haas wrote:
> From: Niklas Haas <git at haasn.dev>
> 
> utils.c is getting quite crowded, and I need a new place to dump a lot of
> format handling code for the ongoing rewrite. Rather than bloating this file
> even more, start splitting format handling helpers off into a new file.
> 
> This also renames the existing utils.h header, which didn't really contain
> anything except the SwsFormat definition anyway (the prototypes for what should
> have been in utils.h are all still in the legacy swscale_internal.h).
> ---
>   libswscale/Makefile              |   1 +
>   libswscale/cms.c                 |   2 +-
>   libswscale/cms.h                 |   2 +-
>   libswscale/csputils.c            |   2 +-
>   libswscale/format.c              | 583 +++++++++++++++++++++++++++++++
>   libswscale/{utils.h => format.h} |   6 +-
>   libswscale/graph.c               |   2 +-
>   libswscale/graph.h               |   2 +-
>   libswscale/lut3d.h               |   2 +-
>   libswscale/utils.c               | 559 -----------------------------
>   10 files changed, 593 insertions(+), 568 deletions(-)
>   create mode 100644 libswscale/format.c
>   rename libswscale/{utils.h => format.h} (98%)
> 
> diff --git a/libswscale/format.c b/libswscale/format.c
> new file mode 100644
> index 0000000000..89ed4eae6b
> --- /dev/null
> +++ b/libswscale/format.c
> @@ -0,0 +1,583 @@
> +/*
> + * Copyright (C) 2024 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
> + */
> +
> +#include "libavutil/avassert.h"
> +#include "libavutil/hdr_dynamic_metadata.h"
> +#include "libavutil/mastering_display_metadata.h"
> +
> +#include "format.h"
> +
> +typedef struct FormatEntry {
> +    uint8_t is_supported_in         :1;
> +    uint8_t is_supported_out        :1;
> +    uint8_t is_supported_endianness :1;
> +} FormatEntry;
> +
> +/* Format support table for legacy swscale */
> +static const FormatEntry format_entries[] = {
> +    [AV_PIX_FMT_YUV420P]     = { 1, 1 },
[...]
> +    [AV_PIX_FMT_BAYER_RGGB8] = { 1, 0 },
> +    [AV_PIX_FMT_BAYER_GBRG8] = { 1, 0 },
> +    [AV_PIX_FMT_BAYER_GRBG8] = { 1, 0 },
> +    [AV_PIX_FMT_BAYER_BGGR16LE] = { 1, 0 },

Perhaps you could vertically align this array while moving it.

[...]

> +/**
> + * This function also sanitizes and strips the input data, removing irrelevant
> + * fields for certain formats.
> + */
> +SwsFormat ff_fmt_from_frame(const AVFrame *frame, int field)
> +{
> +    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
> +    const AVColorPrimariesDesc *primaries;
> +    AVFrameSideData *sd;
> +
> +    SwsFormat fmt = {
> +        .width  = frame->width,
> +        .height = frame->height,
> +        .format = frame->format,
> +        .range  = frame->color_range,
> +        .csp    = frame->colorspace,
> +        .loc    = frame->chroma_location,
> +        .desc   = desc,
> +        .color = {
> +            .prim = frame->color_primaries,
> +            .trc  = frame->color_trc,
> +        },
> +    };
> +
> +    av_assert1(fmt.width > 0);
> +    av_assert1(fmt.height > 0);
> +    av_assert1(fmt.format != AV_PIX_FMT_NONE);
> +    av_assert0(desc);
> +    if (desc->flags & (AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_BAYER)) {
> +        /* RGB-like family */
> +        fmt.csp   = AVCOL_SPC_RGB;
> +        fmt.range = AVCOL_RANGE_JPEG;
> +    } else if (desc->flags & AV_PIX_FMT_FLAG_XYZ) {
> +        fmt.csp   = AVCOL_SPC_UNSPECIFIED;

> +        fmt.range = AVCOL_RANGE_UNSPECIFIED;

This line above was not in utils.c.

Ramiro



More information about the ffmpeg-devel mailing list