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 "gain_map.h"
20#include "mem.h"
22
24{
25 *p = (AVGainMapParams) {
26 .base_hdr_headroom = { 0, 1 },
27 .alternate_hdr_headroom = { 0, 1 },
28 .nb_channels = 1,
29 };
30
31 /* Pre-fill all three channels as a convenience */
32 for (int c = 0; c < 3; c++) {
33 p->channels[c] = (struct AVGainMapChannel) {
34 .gain_map_min = { 0, 1 },
35 .gain_map_max = { 0, 1 },
36 .gamma = { 1, 1 },
37 .base_offset = { 1, 64 },
38 .alternate_offset = { 1, 64 },
39 };
40 }
41}
42
43#define CHECK(cond) do { if (!(cond)) return AVERROR_INVALIDDATA; } while (0)
44#define CHECK_SIGNED(q) CHECK((q).den > 0);
45#define CHECK_UNSIGNED(q) CHECK((q).num >= 0 && (q).den > 0);
46
48{
49 CHECK(0 <= p->version && p->version <= AV_ISO21496_VERSION);
50 CHECK(p->nb_channels == 1 || p->nb_channels == 3);
51 CHECK_UNSIGNED(p->base_hdr_headroom);
52 CHECK_UNSIGNED(p->alternate_hdr_headroom);
53
54 for (int c = 0; c < p->nb_channels; c++) {
55 const struct AVGainMapChannel *const ch = &p->channels[c];
56 CHECK_SIGNED(ch->gain_map_min);
57 CHECK_SIGNED(ch->gain_map_max);
58 CHECK_UNSIGNED(ch->gamma);
59 CHECK(ch->gamma.num != 0);
60 /**
61 * Note: The ISO spec declares these as signed, but a negative value
62 * makes no numeric sense and would result in infinities in the
63 * middle of the valid signal range; so just constrain them to be
64 * unsigned as a safeguard. Note that the Adobe hdrgm spec explicitly
65 * requires them to be nonnegative as well, so this avoids an
66 * inconsistency.
67 */
68 CHECK_UNSIGNED(ch->base_offset);
69 CHECK_UNSIGNED(ch->alternate_offset);
70 CHECK(av_cmp_q(ch->gain_map_min, ch->gain_map_max) <= 0);
71 }
72
73 return 0;
74}
75
76#define ISO21496_HEADER_SIZE 5
77
78#define ISO21496_FLAG_MULTICHANNEL (1 << 7)
79#define ISO21496_FLAG_USE_BASE_CG (1 << 6)
80#define ISO21496_FLAG_RESERVED 0x3f
81
82static size_t iso21496_payload_size(int nb_channels)
83{
84 return 4 * 4 + (size_t) nb_channels * 10 * 4;
85}
86
87static int make_q(int64_t num, uint32_t den, AVRational *out)
88{
89 if (!den)
91
92 /* Ensure the result fits in AVRational */
93 av_reduce(&out->num, &out->den, num, den, INT32_MAX);
94 return 0;
95}
96
98{
99 int32_t num = (int32_t) bytestream2_get_be32(gb);
100 uint32_t den = bytestream2_get_be32u(gb);
101 return make_q(num, den, out);
102}
103
105{
106 uint32_t num = bytestream2_get_be32u(gb);
107 uint32_t den = bytestream2_get_be32u(gb);
108 return make_q(num, den, out);
109}
110
112 const uint8_t *data, size_t size)
113{
114 int ret;
115 if (!p || !data)
116 return AVERROR(EINVAL);
117
119 return AVERROR_INVALIDDATA;
120
123
124 /* Parse header */
125 const uint16_t minimum_version = bytestream2_get_be16(&gb);
126 const uint16_t writer_version = bytestream2_get_be16(&gb);
127 if (minimum_version > AV_ISO21496_VERSION || writer_version < minimum_version)
129 const uint8_t flags = bytestream2_get_byte(&gb);
130
131 /**
132 * While the spec does not require reserved bits to be 0, older versions
133 * of the ISO 21496-1 draft specification had extra flags that got removed
134 * from the final publication, so conservatively error out just in case;
135 * unless the writer version is >0 in which case these may be genuine
136 * additions to later revisions of the published spec, the validity of
137 * ignoring which is already guarded by the `minimum_version` check.
138 */
139 if (writer_version == 0 && (flags & ISO21496_FLAG_RESERVED))
140 return AVERROR_INVALIDDATA;
141
142 p->version = AV_ISO21496_VERSION;
143 p->nb_channels = (flags & ISO21496_FLAG_MULTICHANNEL) ? 3 : 1;
144 p->use_base_color_space = !!(flags & ISO21496_FLAG_USE_BASE_CG);
145
146 /* Parse payload */
147 if (bytestream2_get_bytes_left(&gb) < iso21496_payload_size(p->nb_channels))
148 return AVERROR_INVALIDDATA;
149
150 if ((ret = get_uq(&gb, &p->base_hdr_headroom)) < 0 ||
151 (ret = get_uq(&gb, &p->alternate_hdr_headroom)) < 0)
152 return ret;
153
154 for (int c = 0; c < p->nb_channels; c++) {
155 struct AVGainMapChannel *const ch = &p->channels[c];
156 if ((ret = get_sq(&gb, &ch->gain_map_min)) < 0 ||
157 (ret = get_sq(&gb, &ch->gain_map_max)) < 0 ||
158 (ret = get_uq(&gb, &ch->gamma)) < 0 ||
159 (ret = get_sq(&gb, &ch->base_offset)) < 0 ||
160 (ret = get_sq(&gb, &ch->alternate_offset)) < 0)
161 return ret;
162 }
163
164 /* nb_channels == 1 means every channel shares the same parameters */
165 for (int c = p->nb_channels; c < 3; c++)
166 p->channels[c] = p->channels[0];
167
169}
170
171static void put_q(PutByteContext *pb, AVRational q)
172{
173 bytestream2_put_be32(pb, (unsigned) q.num);
174 bytestream2_put_be32(pb, (unsigned) q.den);
175}
176
178{
179 if (!p || !buf)
180 return AVERROR(EINVAL);
181
182 /* Ensures representability in the bitstream */
184 if (ret < 0)
185 return ret;
186
189
190 int channels = av_gain_map_channels_identical(p) ? 1 : p->nb_channels;
191 uint8_t flags = 0;
192 if (channels == 3)
194 if (p->use_base_color_space)
196
197 bytestream2_put_be16(&pb, p->version); /* minimum_version */
198 bytestream2_put_be16(&pb, p->version); /* writer_version */
199 bytestream2_put_byte(&pb, flags);
200 put_q(&pb, p->base_hdr_headroom);
201 put_q(&pb, p->alternate_hdr_headroom);
202
203 for (int c = 0; c < channels; c++) {
204 const struct AVGainMapChannel *const ch = &p->channels[c];
205 put_q(&pb, ch->gain_map_min);
206 put_q(&pb, ch->gain_map_max);
207 put_q(&pb, ch->gamma);
208 put_q(&pb, ch->base_offset);
209 put_q(&pb, ch->alternate_offset);
210 }
211
212 return bytestream2_tell_p(&pb);
213}
214
216{
217 static_assert(sizeof(p->channels[0]) == sizeof(int[2]) * 5,
218 "AVGainMapChannel struct is tightly packed");
219
220 return !memcmp(&p->channels[0], &p->channels[1], sizeof(p->channels[0])) &&
221 !memcmp(&p->channels[0], &p->channels[2], sizeof(p->channels[0]));
222}
223
225{
227 if (!p)
228 return NULL;
229
231
232 if (size)
233 *size = sizeof(*p);
234
235 return p;
236}
237
239{
240 AVFrameSideData *sd;
242 sizeof(AVGainMapParams),
244 if (!sd)
245 return NULL;
246
247 AVGainMapParams *const p = (AVGainMapParams *) sd->data;
249 return p;
250}
static FILE * out
channels
Definition aptx.h:31
int32_t
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
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
int ret
Definition ffplay.c:391
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
AVFrameSideData * av_frame_side_data_new(AVFrameSideData ***sd, int *nb_sd, enum AVFrameSideDataType type, size_t size, unsigned int flags)
Add new side data entry to an array.
Definition side_data.c:205
#define AV_FRAME_SIDE_DATA_FLAG_REPLACE
Don't add a new entry if another of the same type exists.
Definition frame.h:1108
@ AV_FRAME_DATA_GAIN_MAP_PARAMS
Parameters describing how to combine this gain map with its base image to form an alternate rendition...
Definition frame.h:314
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 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:82
#define CHECK_UNSIGNED(q)
Definition gain_map.c:45
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
#define ISO21496_FLAG_MULTICHANNEL
Definition gain_map.c:78
#define ISO21496_FLAG_USE_BASE_CG
Definition gain_map.c:79
#define ISO21496_FLAG_RESERVED
Definition gain_map.c:80
int av_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:177
static void put_q(PutByteContext *pb, AVRational q)
Definition gain_map.c:171
#define CHECK_SIGNED(q)
Definition gain_map.c:44
AVGainMapParams * av_gain_map_params_alloc(size_t *size)
Allocate an AVGainMapParams structure and initialize it to default values.
Definition gain_map.c:224
static int get_uq(GetByteContext *gb, AVRational *out)
Definition gain_map.c:104
static int make_q(int64_t num, uint32_t den, AVRational *out)
Definition gain_map.c:87
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
static int get_sq(GetByteContext *gb, AVRational *out)
Definition gain_map.c:97
int av_gain_map_params_from_iso21496(AVGainMapParams *restrict p, const uint8_t *data, size_t size)
Definition gain_map.c:111
#define CHECK(cond)
Definition gain_map.c:43
static void gain_map_params_default(AVGainMapParams *p)
Definition gain_map.c:23
AVGainMapParams * av_gain_map_params_create_side_data(AVFrameSideData ***fsd, int *nb_sd)
Allocate and add an AVGainMapParams structure to an existing AVFrameSideData array as AV_FRAME_DATA_G...
Definition gain_map.c:238
#define AV_GAIN_MAP_MAX_PAYLOAD_SIZE
Definition gain_map.h:98
#define AV_ISO21496_VERSION
Definition gain_map.h:27
Memory handling functions.
const char data[16]
Definition mxf.c:149
#define av_malloc(s)
Definition ops_static.c:52
Structure to hold side data for an AVFrame.
Definition frame.h:334
uint8_t * data
Definition frame.h:336
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]