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"
21
23{
24 *p = (AVGainMapParams) {
25 .base_hdr_headroom = { 0, 1 },
26 .alternate_hdr_headroom = { 0, 1 },
27 .nb_channels = 1,
28 };
29
30 /* Pre-fill all three channels as a convenience */
31 for (int c = 0; c < 3; c++) {
32 p->channels[c] = (struct AVGainMapChannel) {
33 .gain_map_min = { 0, 1 },
34 .gain_map_max = { 0, 1 },
35 .gamma = { 1, 1 },
36 .base_offset = { 1, 64 },
37 .alternate_offset = { 1, 64 },
38 };
39 }
40}
41
42#define CHECK(cond) do { if (!(cond)) return AVERROR_INVALIDDATA; } while (0)
43#define CHECK_SIGNED(q) CHECK((q).den > 0);
44#define CHECK_UNSIGNED(q) CHECK((q).num >= 0 && (q).den > 0);
45
47{
48 CHECK(0 <= p->version && p->version <= AV_ISO21496_VERSION);
49 CHECK(p->nb_channels == 1 || p->nb_channels == 3);
50 CHECK_UNSIGNED(p->base_hdr_headroom);
51 CHECK_UNSIGNED(p->alternate_hdr_headroom);
52
53 for (int c = 0; c < p->nb_channels; c++) {
54 const struct AVGainMapChannel *const ch = &p->channels[c];
55 CHECK_SIGNED(ch->gain_map_min);
56 CHECK_SIGNED(ch->gain_map_max);
57 CHECK_UNSIGNED(ch->gamma);
58 CHECK(ch->gamma.num != 0);
59 /**
60 * Note: The ISO spec declares these as signed, but a negative value
61 * makes no numeric sense and would result in infinities in the
62 * middle of the valid signal range; so just constrain them to be
63 * unsigned as a safeguard. Note that the Adobe hdrgm spec explicitly
64 * requires them to be nonnegative as well, so this avoids an
65 * inconsistency.
66 */
67 CHECK_UNSIGNED(ch->base_offset);
68 CHECK_UNSIGNED(ch->alternate_offset);
69 CHECK(av_cmp_q(ch->gain_map_min, ch->gain_map_max) <= 0);
70 }
71
72 return 0;
73}
74
76{
77 static_assert(sizeof(p->channels[0]) == sizeof(int[2]) * 5,
78 "AVGainMapChannel struct is tightly packed");
79
80 return !memcmp(&p->channels[0], &p->channels[1], sizeof(p->channels[0])) &&
81 !memcmp(&p->channels[0], &p->channels[2], sizeof(p->channels[0]));
82}
83
85{
87 if (!p)
88 return NULL;
89
91
92 if (size)
93 *size = sizeof(*p);
94
95 return p;
96}
97
99{
100 AVFrameSideData *sd;
102 sizeof(AVGainMapParams),
104 if (!sd)
105 return NULL;
106
107 AVGainMapParams *const p = (AVGainMapParams *) sd->data;
109 return p;
110}
#define NULL
Definition coverity.c:32
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:1105
@ 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
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition rational.h:89
#define CHECK_UNSIGNED(q)
Definition gain_map.c:44
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
#define CHECK_SIGNED(q)
Definition gain_map.c:43
AVGainMapParams * av_gain_map_params_alloc(size_t *size)
Allocate an AVGainMapParams structure and initialize it to default values.
Definition gain_map.c:84
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 CHECK(cond)
Definition gain_map.c:42
static void gain_map_params_default(AVGainMapParams *p)
Definition gain_map.c:22
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:98
#define AV_ISO21496_VERSION
Definition gain_map.h:27
Memory handling functions.
#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
int size
static double c[64]