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
31#define ISO21496_HEADER_SIZE 5
32
33#define ISO21496_FLAG_MULTICHANNEL (1 << 7)
34#define ISO21496_FLAG_USE_BASE_CG (1 << 6)
35#define ISO21496_FLAG_RESERVED 0x3f
36
37static size_t iso21496_payload_size(int nb_channels)
38{
39 return 4 * 4 + (size_t) nb_channels * 10 * 4;
40}
41
42static int make_q(int64_t num, uint32_t den, AVRational *out)
43{
44 if (!den)
46
47 /* Ensure the result fits in AVRational */
48 av_reduce(&out->num, &out->den, num, den, INT32_MAX);
49 return 0;
50}
51
53{
54 int32_t num = (int32_t) bytestream2_get_be32(gb);
55 uint32_t den = bytestream2_get_be32u(gb);
56 return make_q(num, den, out);
57}
58
60{
61 uint32_t num = bytestream2_get_be32u(gb);
62 uint32_t den = bytestream2_get_be32u(gb);
63 return make_q(num, den, out);
64}
65
67 const uint8_t *data, size_t size)
68{
69 int ret;
70 if (!p || !data)
71 return AVERROR(EINVAL);
72
75
78
79 /* Parse header */
80 const uint16_t minimum_version = bytestream2_get_be16(&gb);
81 const uint16_t writer_version = bytestream2_get_be16(&gb);
82 if (minimum_version > FF_GAIN_MAP_VERSION || writer_version < minimum_version)
84 const uint8_t flags = bytestream2_get_byte(&gb);
85
86 /**
87 * While the spec does not require reserved bits to be 0, older versions
88 * of the ISO 21496-1 draft specification had extra flags that got removed
89 * from the final publication, so conservatively error out just in case;
90 * unless the writer version is >0 in which case these may be genuine
91 * additions to later revisions of the published spec, the validity of
92 * ignoring which is already guarded by the `minimum_version` check.
93 */
94 if (writer_version == 0 && (flags & ISO21496_FLAG_RESERVED))
96
97 p->version = FF_GAIN_MAP_VERSION;
98 p->nb_channels = (flags & ISO21496_FLAG_MULTICHANNEL) ? 3 : 1;
99 p->use_base_color_space = !!(flags & ISO21496_FLAG_USE_BASE_CG);
100
101 /* Parse payload */
102 if (bytestream2_get_bytes_left(&gb) < iso21496_payload_size(p->nb_channels))
103 return AVERROR_INVALIDDATA;
104
105 if ((ret = get_uq(&gb, &p->base_hdr_headroom)) < 0 ||
106 (ret = get_uq(&gb, &p->alternate_hdr_headroom)) < 0)
107 return ret;
108
109 for (int c = 0; c < p->nb_channels; c++) {
110 struct AVGainMapChannel *const ch = &p->channels[c];
111 if ((ret = get_sq(&gb, &ch->gain_map_min)) < 0 ||
112 (ret = get_sq(&gb, &ch->gain_map_max)) < 0 ||
113 (ret = get_uq(&gb, &ch->gamma)) < 0 ||
114 (ret = get_sq(&gb, &ch->base_offset)) < 0 ||
115 (ret = get_sq(&gb, &ch->alternate_offset)) < 0)
116 return ret;
117 }
118
119 /* nb_channels == 1 means every channel shares the same parameters */
120 for (int c = p->nb_channels; c < 3; c++)
121 p->channels[c] = p->channels[0];
122
124}
125
126static void put_q(PutByteContext *pb, AVRational q)
127{
128 bytestream2_put_be32(pb, (unsigned) q.num);
129 bytestream2_put_be32(pb, (unsigned) q.den);
130}
131
133{
134 if (!p || !buf)
135 return AVERROR(EINVAL);
136
137 /* Ensures representability in the bitstream */
138 int ret = av_gain_map_params_validate(p);
139 if (ret < 0)
140 return ret;
141
142 if (p->version > FF_GAIN_MAP_VERSION)
143 return AVERROR_PATCHWELCOME; /* e.g. libraries out of sync */
144
147
148 int channels = av_gain_map_channels_identical(p) ? 1 : p->nb_channels;
149 uint8_t flags = 0;
150 if (channels == 3)
152 if (p->use_base_color_space)
154
155 bytestream2_put_be16(&pb, p->version); /* minimum_version */
156 bytestream2_put_be16(&pb, p->version); /* writer_version */
157 bytestream2_put_byte(&pb, flags);
158 put_q(&pb, p->base_hdr_headroom);
159 put_q(&pb, p->alternate_hdr_headroom);
160
161 for (int c = 0; c < channels; c++) {
162 const struct AVGainMapChannel *const ch = &p->channels[c];
163 put_q(&pb, ch->gain_map_min);
164 put_q(&pb, ch->gain_map_max);
165 put_q(&pb, ch->gamma);
166 put_q(&pb, ch->base_offset);
167 put_q(&pb, ch->alternate_offset);
168 }
169
170 return bytestream2_tell_p(&pb);
171}
172
174{
176 return 0;
177
178 if (p->version > FF_GAIN_MAP_VERSION)
179 return 0; /* e.g. libraries out of sync */
180
181 /* XMP gain maps only store one set of parameters (shared across channels),
182 * have no way to describe a map applied in the alternate rendition's
183 * color space, and explicitly reject no-op parameters */
184 return (p->nb_channels == 1 || av_gain_map_channels_identical(p)) &&
185 p->use_base_color_space &&
186 av_cmp_q(p->alternate_hdr_headroom, p->base_hdr_headroom) != 0;
187}
188
189#define XMP_GAIN_MAP_TEMPLATE \
190 "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" x:xmptk=\"XMP Core 5.5.0\">\n" \
191 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" \
192 " <rdf:Description rdf:about=\"\"\n" \
193 " xmlns:hdrgm=\"" FF_GAIN_MAP_XMP_NAMESPACE "\"\n" \
194 " hdrgm:Version=\"1.0\"\n" \
195 " hdrgm:GainMapMin=\"%g\"\n" \
196 " hdrgm:GainMapMax=\"%g\"\n" \
197 " hdrgm:Gamma=\"%g\"\n" \
198 " hdrgm:OffsetSDR=\"%g\"\n" \
199 " hdrgm:OffsetHDR=\"%g\"\n" \
200 " hdrgm:HDRCapacityMin=\"%g\"\n" \
201 " hdrgm:HDRCapacityMax=\"%g\"\n" \
202 " hdrgm:BaseRenditionIsHDR=\"%s\"/>\n" \
203 " </rdf:RDF>\n" \
204 "</x:xmpmeta>"
205
206static_assert(FF_GAIN_MAP_XMP_MAX_LEN > sizeof(XMP_GAIN_MAP_TEMPLATE) + 8 * 10,
207 "XMP buffer too small for template and values");
208
210{
211 if (!p || !buf)
212 return AVERROR(EINVAL);
213
215 return AVERROR(ENOTSUP);
216
217 const struct AVGainMapChannel *const ch = &p->channels[0];
218 AVRational offset_sdr = ch->base_offset;
219 AVRational offset_hdr = ch->alternate_offset;
220 AVRational hdr_cap_min = p->base_hdr_headroom;
221 AVRational hdr_cap_max = p->alternate_hdr_headroom;
222
223 const int base_is_hdr = av_cmp_q(p->base_hdr_headroom,
224 p->alternate_hdr_headroom) > 0;
225 if (base_is_hdr) {
226 FFSWAP(AVRational, offset_sdr, offset_hdr);
227 FFSWAP(AVRational, hdr_cap_min, hdr_cap_max);
228 }
229
230 const size_t size = FF_GAIN_MAP_XMP_MAX_LEN;
231 int ret = snprintf(buf, size, XMP_GAIN_MAP_TEMPLATE,
232 av_q2d(ch->gain_map_min), av_q2d(ch->gain_map_max),
233 av_q2d(ch->gamma),
234 av_q2d(offset_sdr), av_q2d(offset_hdr),
235 av_q2d(hdr_cap_min), av_q2d(hdr_cap_max),
236 base_is_hdr ? "True" : "False");
237 if (ret < 0 || ret >= size)
238 return AVERROR_BUG;
239
240 return ret;
241}
static FILE * out
channels
Definition aptx.h:31
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
static av_always_inline int bytestream2_tell_p(const PutByteContext *p)
Definition bytestream.h:197
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition bytestream.h:147
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
#define flags(name, subs,...)
Definition cbs_h264.c:74
common internal and external API header
long long int64_t
Definition coverity.c:34
error code definitions
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition rational.c:35
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
static size_t iso21496_payload_size(int nb_channels)
Definition gain_map.c:37
#define ISO21496_FLAG_MULTICHANNEL
Definition gain_map.c:33
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:173
#define ISO21496_FLAG_USE_BASE_CG
Definition gain_map.c:34
#define ISO21496_FLAG_RESERVED
Definition gain_map.c:35
int ff_gain_map_params_from_iso21496(AVGainMapParams *restrict p, const uint8_t *data, size_t size)
Definition gain_map.c:66
static void put_q(PutByteContext *pb, AVRational q)
Definition gain_map.c:126
static int get_uq(GetByteContext *gb, AVRational *out)
Definition gain_map.c:59
static int make_q(int64_t num, uint32_t den, AVRational *out)
Definition gain_map.c:42
static int get_sq(GetByteContext *gb, AVRational *out)
Definition gain_map.c:52
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:209
#define XMP_GAIN_MAP_TEMPLATE
Definition gain_map.c:189
int ff_gain_map_params_to_iso21496(const AVGainMapParams *p, uint8_t *buf)
Serialize an AVGainMapParams as an ISO 21496-1 gain map metadata blob.
Definition gain_map.c:132
Serialization of AVGainMapParams into the metadata blobs that image formats carry it in,...
#define FF_GAIN_MAP_XMP_MAX_LEN
Definition gain_map.h:39
#define FF_GAIN_MAP_MAX_PAYLOAD_SIZE
Definition gain_map.h:34
#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:46
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:75
#define FFSWAP(type, a, b)
Definition macros.h:52
Memory handling functions.
const char data[16]
Definition mxf.c:149
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 num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
int num
Definition error.c:23
int size
static double c[64]