FFmpeg
Loading...
Searching...
No Matches
gain_map.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <stdio.h>
20
21#include "libavutil/avassert.h"
22#include "libavutil/common.h"
23#include "libavutil/error.h"
24#include "libavutil/gain_map.h"
25#include "libavutil/mem.h"
26#include "libavutil/rational.h"
27
28#include "bytestream.h"
29#include "gain_map.h"
30
32{
34 return 0;
35
36 if (p->version > FF_GAIN_MAP_VERSION)
37 return 0; /* e.g. libraries out of sync */
38
39 /* XMP gain maps only store one set of parameters (shared across channels),
40 * have no way to describe a map applied in the alternate rendition's
41 * color space, and explicitly reject no-op parameters */
42 return (p->nb_channels == 1 || av_gain_map_channels_identical(p)) &&
43 p->use_base_color_space &&
44 av_cmp_q(p->alternate_hdr_headroom, p->base_hdr_headroom) != 0;
45}
46
47#define XMP_GAIN_MAP_TEMPLATE \
48 "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" x:xmptk=\"XMP Core 5.5.0\">\n" \
49 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" \
50 " <rdf:Description rdf:about=\"\"\n" \
51 " xmlns:hdrgm=\"" FF_GAIN_MAP_XMP_NAMESPACE "\"\n" \
52 " hdrgm:Version=\"1.0\"\n" \
53 " hdrgm:GainMapMin=\"%g\"\n" \
54 " hdrgm:GainMapMax=\"%g\"\n" \
55 " hdrgm:Gamma=\"%g\"\n" \
56 " hdrgm:OffsetSDR=\"%g\"\n" \
57 " hdrgm:OffsetHDR=\"%g\"\n" \
58 " hdrgm:HDRCapacityMin=\"%g\"\n" \
59 " hdrgm:HDRCapacityMax=\"%g\"\n" \
60 " hdrgm:BaseRenditionIsHDR=\"%s\"/>\n" \
61 " </rdf:RDF>\n" \
62 "</x:xmpmeta>"
63
64static_assert(FF_GAIN_MAP_XMP_MAX_LEN > sizeof(XMP_GAIN_MAP_TEMPLATE) + 8 * 10,
65 "XMP buffer too small for template and values");
66
68{
69 if (!p || !buf)
70 return AVERROR(EINVAL);
71
73 return AVERROR(ENOTSUP);
74
75 const struct AVGainMapChannel *const ch = &p->channels[0];
76 AVRational offset_sdr = ch->base_offset;
77 AVRational offset_hdr = ch->alternate_offset;
78 AVRational hdr_cap_min = p->base_hdr_headroom;
79 AVRational hdr_cap_max = p->alternate_hdr_headroom;
80
81 const int base_is_hdr = av_cmp_q(p->base_hdr_headroom,
82 p->alternate_hdr_headroom) > 0;
83 if (base_is_hdr) {
84 FFSWAP(AVRational, offset_sdr, offset_hdr);
85 FFSWAP(AVRational, hdr_cap_min, hdr_cap_max);
86 }
87
88 const size_t size = FF_GAIN_MAP_XMP_MAX_LEN;
89 int ret = snprintf(buf, size, XMP_GAIN_MAP_TEMPLATE,
90 av_q2d(ch->gain_map_min), av_q2d(ch->gain_map_max),
91 av_q2d(ch->gamma),
92 av_q2d(offset_sdr), av_q2d(offset_hdr),
93 av_q2d(hdr_cap_min), av_q2d(hdr_cap_max),
94 base_is_hdr ? "True" : "False");
96 return AVERROR_BUG;
97
98 return ret;
99}
simple assert() macros that are a bit more flexible than ISO C assert().
common internal and external API header
error code definitions
int ret
Definition ffplay.c:391
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR(e)
Definition error.h:45
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition rational.h:89
int ff_gain_map_params_check_xmp(const AVGainMapParams *p)
Returns 1 if the gain map parameters are compatible with the XMP-based encoding, or 0 otherwise.
Definition gain_map.c:31
int ff_gain_map_params_to_xmp(const AVGainMapParams *p, char *buf)
Serialize an AVGainMapParams as an Ultra HDR XMP packet.
Definition gain_map.c:67
#define XMP_GAIN_MAP_TEMPLATE
Definition gain_map.c:47
Serialization of AVGainMapParams into the metadata blobs that image formats carry it in,...
#define FF_GAIN_MAP_XMP_MAX_LEN
Definition gain_map.h:38
#define FF_GAIN_MAP_VERSION
Definition gain_map.h:33
int av_gain_map_params_validate(const AVGainMapParams *p)
Returns >= 0 if the given AVGainMapParams struct contains valid data; or a negative AVERROR otherwise...
Definition gain_map.c:47
int av_gain_map_channels_identical(const AVGainMapParams *p)
Returns 1 if all channels of the gain map have identical parameters, 0 otherwise.
Definition gain_map.c:215
#define FFSWAP(type, a, b)
Definition macros.h:52
Memory handling functions.
Utilities for rational number calculation.
Parameters describing how to combine a gain map rendition with its base rendition to recover the alte...
Definition gain_map.h:39
Rational number (pair of numerator and denominator).
Definition rational.h:58
int size