FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/gain_map.c
Date: 2026-10-29 07:45:57
Exec Total Coverage
Lines: 102 115 88.7%
Functions: 12 12 100.0%
Branches: 58 104 55.8%

Line Branch Exec Source
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"
21 #include "libavcodec/bytestream.h"
22
23 23 static void gain_map_params_default(AVGainMapParams *p)
24 {
25 23 *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
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 23 times.
92 for (int c = 0; c < 3; c++) {
33 69 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 23 }
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
47 16 int av_gain_map_params_validate(const AVGainMapParams *p)
48 {
49
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
16 CHECK(0 <= p->version && p->version <= AV_ISO21496_VERSION);
50
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
16 CHECK(p->nb_channels == 1 || p->nb_channels == 3);
51
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
16 CHECK_UNSIGNED(p->base_hdr_headroom);
52
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
16 CHECK_UNSIGNED(p->alternate_hdr_headroom);
53
54
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 16 times.
36 for (int c = 0; c < p->nb_channels; c++) {
55 20 const struct AVGainMapChannel *const ch = &p->channels[c];
56
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 CHECK_SIGNED(ch->gain_map_min);
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 CHECK_SIGNED(ch->gain_map_max);
58
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
20 CHECK_UNSIGNED(ch->gamma);
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 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
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
20 CHECK_UNSIGNED(ch->base_offset);
69
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
20 CHECK_UNSIGNED(ch->alternate_offset);
70
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
20 CHECK(av_cmp_q(ch->gain_map_min, ch->gain_map_max) <= 0);
71 }
72
73 16 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
82 6 static size_t iso21496_payload_size(int nb_channels)
83 {
84 6 return 4 * 4 + (size_t) nb_channels * 10 * 4;
85 }
86
87 42 static int make_q(int64_t num, uint32_t den, AVRational *out)
88 {
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!den)
90 ✗ return AVERROR_INVALIDDATA;
91
92 /* Ensure the result fits in AVRational */
93 42 av_reduce(&out->num, &out->den, num, den, INT32_MAX);
94 42 return 0;
95 }
96
97 24 static int get_sq(GetByteContext *gb, AVRational *out)
98 {
99 24 int32_t num = (int32_t) bytestream2_get_be32(gb);
100 24 uint32_t den = bytestream2_get_be32u(gb);
101 24 return make_q(num, den, out);
102 }
103
104 18 static int get_uq(GetByteContext *gb, AVRational *out)
105 {
106 18 uint32_t num = bytestream2_get_be32u(gb);
107 18 uint32_t den = bytestream2_get_be32u(gb);
108 18 return make_q(num, den, out);
109 }
110
111 8 int av_gain_map_params_from_iso21496(AVGainMapParams *restrict p,
112 const uint8_t *data, size_t size)
113 {
114 int ret;
115
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 if (!p || !data)
116 ✗ return AVERROR(EINVAL);
117
118
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 if (size < ISO21496_HEADER_SIZE || size > INT_MAX)
119 ✗ return AVERROR_INVALIDDATA;
120
121 GetByteContext gb;
122 8 bytestream2_init(&gb, data, size);
123
124 /* Parse header */
125 8 const uint16_t minimum_version = bytestream2_get_be16(&gb);
126 8 const uint16_t writer_version = bytestream2_get_be16(&gb);
127
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
8 if (minimum_version > AV_ISO21496_VERSION || writer_version < minimum_version)
128 2 return AVERROR_PATCHWELCOME;
129 6 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
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (writer_version == 0 && (flags & ISO21496_FLAG_RESERVED))
140 ✗ return AVERROR_INVALIDDATA;
141
142 6 p->version = AV_ISO21496_VERSION;
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 p->nb_channels = (flags & ISO21496_FLAG_MULTICHANNEL) ? 3 : 1;
144 6 p->use_base_color_space = !!(flags & ISO21496_FLAG_USE_BASE_CG);
145
146 /* Parse payload */
147
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (bytestream2_get_bytes_left(&gb) < iso21496_payload_size(p->nb_channels))
148 ✗ return AVERROR_INVALIDDATA;
149
150
2/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
12 if ((ret = get_uq(&gb, &p->base_hdr_headroom)) < 0 ||
151 6 (ret = get_uq(&gb, &p->alternate_hdr_headroom)) < 0)
152 ✗ return ret;
153
154
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 for (int c = 0; c < p->nb_channels; c++) {
155 6 struct AVGainMapChannel *const ch = &p->channels[c];
156
2/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
12 if ((ret = get_sq(&gb, &ch->gain_map_min)) < 0 ||
157
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
12 (ret = get_sq(&gb, &ch->gain_map_max)) < 0 ||
158
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
12 (ret = get_uq(&gb, &ch->gamma)) < 0 ||
159
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
12 (ret = get_sq(&gb, &ch->base_offset)) < 0 ||
160 6 (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
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 for (int c = p->nb_channels; c < 3; c++)
166 12 p->channels[c] = p->channels[0];
167
168 6 return av_gain_map_params_validate(p);
169 }
170
171 7 static void put_q(PutByteContext *pb, AVRational q)
172 {
173 7 bytestream2_put_be32(pb, (unsigned) q.num);
174 7 bytestream2_put_be32(pb, (unsigned) q.den);
175 7 }
176
177 1 int av_gain_map_params_to_iso21496(const AVGainMapParams *p, uint8_t *buf)
178 {
179
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!p || !buf)
180 ✗ return AVERROR(EINVAL);
181
182 /* Ensures representability in the bitstream */
183 1 int ret = av_gain_map_params_validate(p);
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
185 ✗ return ret;
186
187 PutByteContext pb;
188 1 bytestream2_init_writer(&pb, buf, AV_GAIN_MAP_MAX_PAYLOAD_SIZE);
189
190
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 int channels = av_gain_map_channels_identical(p) ? 1 : p->nb_channels;
191 1 uint8_t flags = 0;
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (channels == 3)
193 ✗ flags |= ISO21496_FLAG_MULTICHANNEL;
194
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (p->use_base_color_space)
195 1 flags |= ISO21496_FLAG_USE_BASE_CG;
196
197 1 bytestream2_put_be16(&pb, p->version); /* minimum_version */
198 1 bytestream2_put_be16(&pb, p->version); /* writer_version */
199 1 bytestream2_put_byte(&pb, flags);
200 1 put_q(&pb, p->base_hdr_headroom);
201 1 put_q(&pb, p->alternate_hdr_headroom);
202
203
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (int c = 0; c < channels; c++) {
204 1 const struct AVGainMapChannel *const ch = &p->channels[c];
205 1 put_q(&pb, ch->gain_map_min);
206 1 put_q(&pb, ch->gain_map_max);
207 1 put_q(&pb, ch->gamma);
208 1 put_q(&pb, ch->base_offset);
209 1 put_q(&pb, ch->alternate_offset);
210 }
211
212 1 return bytestream2_tell_p(&pb);
213 }
214
215 1 int av_gain_map_channels_identical(const AVGainMapParams *p)
216 {
217 static_assert(sizeof(p->channels[0]) == sizeof(int[2]) * 5,
218 "AVGainMapChannel struct is tightly packed");
219
220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 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
224 18 AVGainMapParams *av_gain_map_params_alloc(size_t *size)
225 {
226 18 AVGainMapParams *p = av_malloc(sizeof(AVGainMapParams));
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (!p)
228 ✗ return NULL;
229
230 18 gain_map_params_default(p);
231
232
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (size)
233 18 *size = sizeof(*p);
234
235 18 return p;
236 }
237
238 5 AVGainMapParams *av_gain_map_params_create_side_data(AVFrameSideData ***fsd, int *nb_sd)
239 {
240 AVFrameSideData *sd;
241 5 sd = av_frame_side_data_new(fsd, nb_sd, AV_FRAME_DATA_GAIN_MAP_PARAMS,
242 sizeof(AVGainMapParams),
243 AV_FRAME_SIDE_DATA_FLAG_REPLACE);
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!sd)
245 ✗ return NULL;
246
247 5 AVGainMapParams *const p = (AVGainMapParams *) sd->data;
248 5 gain_map_params_default(p);
249 5 return p;
250 }
251