History

swscale/utils: add SwsFormat abstraction and helpers

Groups together all relevant color metadata from an AVFrame. While we could use AVFrame directly, keeping it a separate struct has three advantages: 1. Functions accepting an SwsFormat will definitely not care about the data pointers. 2. It clearly separates sanitized and raw metadata, since the function to construct an SwsFormat from an AVFrame will also sanitize. 3. It's slightly more lightweight to pass around. Move these into a new header file "utils.h" to avoid crowding swscale_internal.h even more, and also to solve a circular dependency issue down the line.

Niklas Haas committed 2 years ago

Commit e2637a01 parent 87baf9a2 files · +139 −0Browse files

Unified · Split

2 files
  1. libswscale/utils.c +64 −0
  2. libswscale/utils.h +75 −0
libswscale/utils.c+64−0
@@ -1,4 +1,5 @@
1/*1/*
2 * Copyright (C) 2024 Niklas Haas
2 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>3 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
3 *4 *
4 * This file is part of FFmpeg.5 * This file is part of FFmpeg.
@@ -59,6 +60,7 @@ · 54 unmodified lines
59#include "rgb2rgb.h"60#include "rgb2rgb.h"
60#include "swscale.h"61#include "swscale.h"
61#include "swscale_internal.h"62#include "swscale_internal.h"
63#include "utils.h"
62 64
63typedef struct FormatEntry {65typedef struct FormatEntry {
64 uint8_t is_supported_in :1;66 uint8_t is_supported_in :1;
@@ -2645,3 +2647,65 @@ int ff_range_add(RangeList *rl, unsigned int start, unsigned int len) · 2580 unmodified lines
2645 2647
2646 return 0;2648 return 0;
2647}2649}
2650
2651/**
2652 * This function also sanitizes and strips the input data, removing irrelevant
2653 * fields for certain formats.
2654 */
2655SwsFormat ff_fmt_from_frame(const AVFrame *frame, int field)
2656{
2657 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
2658 SwsFormat fmt = {
2659 .width = frame->width,
2660 .height = frame->height,
2661 .format = frame->format,
2662 .range = frame->color_range,
2663 .prim = frame->color_primaries,
2664 .trc = frame->color_trc,
2665 .csp = frame->colorspace,
2666 .loc = frame->chroma_location,
2667 .desc = desc,
2668 };
2669
2670 av_assert1(fmt.width > 0);
2671 av_assert1(fmt.height > 0);
2672 av_assert1(fmt.format != AV_PIX_FMT_NONE);
2673 av_assert0(desc);
2674 if (desc->flags & (AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_BAYER)) {
2675 /* RGB-like family */
2676 fmt.csp = AVCOL_SPC_RGB;
2677 fmt.range = AVCOL_RANGE_JPEG;
2678 } else if (desc->flags & AV_PIX_FMT_FLAG_XYZ) {
2679 fmt.csp = AVCOL_SPC_UNSPECIFIED;
2680 fmt.prim = AVCOL_PRI_SMPTE428;
2681 fmt.trc = AVCOL_TRC_SMPTE428;
2682 } else if (desc->nb_components < 3) {
2683 /* Grayscale formats */
2684 fmt.prim = AVCOL_PRI_UNSPECIFIED;
2685 fmt.csp = AVCOL_SPC_UNSPECIFIED;
2686 if (desc->flags & AV_PIX_FMT_FLAG_FLOAT)
2687 fmt.range = AVCOL_RANGE_UNSPECIFIED;
2688 else
2689 fmt.range = AVCOL_RANGE_JPEG; // FIXME: this restriction should be lifted
2690 }
2691
2692 switch (frame->format) {
2693 case AV_PIX_FMT_YUVJ420P:
2694 case AV_PIX_FMT_YUVJ411P:
2695 case AV_PIX_FMT_YUVJ422P:
2696 case AV_PIX_FMT_YUVJ444P:
2697 case AV_PIX_FMT_YUVJ440P:
2698 fmt.range = AVCOL_RANGE_JPEG;
2699 break;
2700 }
2701
2702 if (!desc->log2_chroma_w && !desc->log2_chroma_h)
2703 fmt.loc = AVCHROMA_LOC_UNSPECIFIED;
2704
2705 if (frame->flags & AV_FRAME_FLAG_INTERLACED) {
2706 fmt.height = (fmt.height + (field == FIELD_TOP)) >> 1;
2707 fmt.interlaced = 1;
2708 }
2709
2710 return fmt;
2711}
libswscale/utils.h+75−0
@@ -0,0 +1,75 @@
1/*
2 * Copyright (C) 2024 Niklas Haas
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifndef SWSCALE_UTILS_H
22#define SWSCALE_UTILS_H
23
24#include "libavutil/pixdesc.h"
25
26#include "swscale.h"
27
28enum {
29 FIELD_TOP, /* top/even rows, or progressive */
30 FIELD_BOTTOM, /* bottom/odd rows */
31};
32
33/* Subset of AVFrame parameters that uniquely determine pixel representation */
34typedef struct SwsFormat {
35 int width, height;
36 int interlaced;
37 enum AVPixelFormat format;
38 enum AVColorRange range;
39 enum AVColorPrimaries prim;
40 enum AVColorTransferCharacteristic trc;
41 enum AVColorSpace csp;
42 enum AVChromaLocation loc;
43 const AVPixFmtDescriptor *desc; /* convenience */
44} SwsFormat;
45
46/**
47 * This function also sanitizes and strips the input data, removing irrelevant
48 * fields for certain formats.
49 */
50SwsFormat ff_fmt_from_frame(const AVFrame *frame, int field);
51
52static inline int ff_fmt_equal(const SwsFormat *fmt1, const SwsFormat *fmt2)
53{
54 return fmt1->width == fmt2->width &&
55 fmt1->height == fmt2->height &&
56 fmt1->interlaced == fmt2->interlaced &&
57 fmt1->format == fmt2->format &&
58 fmt1->range == fmt2->range &&
59 fmt1->prim == fmt2->prim &&
60 fmt1->trc == fmt2->trc &&
61 fmt1->csp == fmt2->csp &&
62 fmt1->loc == fmt2->loc;
63}
64
65static inline int ff_fmt_align(enum AVPixelFormat fmt)
66{
67 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
68 if (desc->flags & AV_PIX_FMT_FLAG_BAYER) {
69 return 2;
70 } else {
71 return 1 << desc->log2_chroma_h;
72 }
73}
74
75#endif /* SWSCALE_UTILS_H */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t