Coverage Report

Created: 2026-07-30 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavutil/side_data.c
Line
Count
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 "avassert.h"
20
#include "buffer.h"
21
#include "common.h"
22
#include "dict.h"
23
#include "frame.h"
24
#include "mem.h"
25
#include "side_data.h"
26
27
static const AVSideDataDescriptor sd_props[] = {
28
    [AV_FRAME_DATA_PANSCAN]                     = { "AVPanScan",                                    AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
29
    [AV_FRAME_DATA_A53_CC]                      = { "ATSC A53 Part 4 Closed Captions" },
30
    [AV_FRAME_DATA_MATRIXENCODING]              = { "AVMatrixEncoding",                             AV_SIDE_DATA_PROP_CHANNEL_DEPENDENT },
31
    [AV_FRAME_DATA_DOWNMIX_INFO]                = { "Metadata relevant to a downmix procedure",     AV_SIDE_DATA_PROP_CHANNEL_DEPENDENT },
32
    [AV_FRAME_DATA_AFD]                         = { "Active format description" },
33
    [AV_FRAME_DATA_MOTION_VECTORS]              = { "Motion vectors",                               AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
34
    [AV_FRAME_DATA_SKIP_SAMPLES]                = { "Skip samples" },
35
    [AV_FRAME_DATA_GOP_TIMECODE]                = { "GOP timecode" },
36
    [AV_FRAME_DATA_S12M_TIMECODE]               = { "SMPTE 12-1 timecode" },
37
    [AV_FRAME_DATA_DYNAMIC_HDR_PLUS]            = { "HDR Dynamic Metadata SMPTE2094-40 (HDR10+)",   AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
38
    [AV_FRAME_DATA_DYNAMIC_HDR_VIVID]           = { "HDR Dynamic Metadata CUVA 005.1 2021 (Vivid)", AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
39
    [AV_FRAME_DATA_DYNAMIC_HDR_SMPTE_2094_APP5] = { "HDR Dynamic Metadata SMPTE2094-50",            AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
40
    [AV_FRAME_DATA_REGIONS_OF_INTEREST]         = { "Regions Of Interest",                          AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
41
    [AV_FRAME_DATA_VIDEO_ENC_PARAMS]            = { "Video encoding parameters" },
42
    [AV_FRAME_DATA_FILM_GRAIN_PARAMS]           = { "Film grain parameters" },
43
    [AV_FRAME_DATA_DETECTION_BBOXES]            = { "Bounding boxes for object detection and classification", AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
44
    [AV_FRAME_DATA_DOVI_RPU_BUFFER]             = { "Dolby Vision RPU Data",                        AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
45
    [AV_FRAME_DATA_DOVI_METADATA]               = { "Dolby Vision Metadata",                        AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
46
    [AV_FRAME_DATA_LCEVC]                       = { "LCEVC NAL data",                               AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
47
    [AV_FRAME_DATA_VIEW_ID]                     = { "View ID" },
48
    [AV_FRAME_DATA_STEREO3D]                    = { "Stereo 3D",                                    AV_SIDE_DATA_PROP_GLOBAL },
49
    [AV_FRAME_DATA_REPLAYGAIN]                  = { "AVReplayGain",                                 AV_SIDE_DATA_PROP_GLOBAL },
50
    [AV_FRAME_DATA_DISPLAYMATRIX]               = { "3x3 displaymatrix",                            AV_SIDE_DATA_PROP_GLOBAL },
51
    [AV_FRAME_DATA_AUDIO_SERVICE_TYPE]          = { "Audio service type",                           AV_SIDE_DATA_PROP_GLOBAL },
52
    [AV_FRAME_DATA_MASTERING_DISPLAY_METADATA]  = { "Mastering display metadata",                   AV_SIDE_DATA_PROP_GLOBAL | AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
53
    [AV_FRAME_DATA_CONTENT_LIGHT_LEVEL]         = { "Content light level metadata",                 AV_SIDE_DATA_PROP_GLOBAL | AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
54
    [AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT] = { "Ambient viewing environment",                  AV_SIDE_DATA_PROP_GLOBAL },
55
    [AV_FRAME_DATA_SPHERICAL]                   = { "Spherical Mapping",                            AV_SIDE_DATA_PROP_GLOBAL | AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
56
    [AV_FRAME_DATA_ICC_PROFILE]                 = { "ICC profile",                                  AV_SIDE_DATA_PROP_GLOBAL | AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
57
    [AV_FRAME_DATA_EXIF]                        = { "EXIF metadata",                                AV_SIDE_DATA_PROP_GLOBAL },
58
    [AV_FRAME_DATA_SEI_UNREGISTERED]            = { "H.26[45] User Data Unregistered SEI message",  AV_SIDE_DATA_PROP_MULTI },
59
    [AV_FRAME_DATA_VIDEO_HINT]                  = { "Encoding video hint",                          AV_SIDE_DATA_PROP_SIZE_DEPENDENT },
60
    [AV_FRAME_DATA_3D_REFERENCE_DISPLAYS]       = { "3D Reference Displays Information",            AV_SIDE_DATA_PROP_GLOBAL },
61
    [AV_FRAME_DATA_IAMF_MIX_GAIN_PARAM]         = { "IAMF Mix Gain Parameter Data" },
62
    [AV_FRAME_DATA_IAMF_DEMIXING_INFO_PARAM]    = { "IAMF Demixing Info Parameter Data",            AV_SIDE_DATA_PROP_CHANNEL_DEPENDENT },
63
    [AV_FRAME_DATA_IAMF_RECON_GAIN_INFO_PARAM]  = { "IAMF Recon Gain Info Parameter Data" },
64
    [AV_FRAME_DATA_RAW_COLOR_PARAMS]            = { "RAW camera color parameters",                  AV_SIDE_DATA_PROP_GLOBAL | AV_SIDE_DATA_PROP_COLOR_DEPENDENT },
65
    [AV_FRAME_DATA_DOWNMIX_MATRIX]              = { "Downmix Matrix",                               AV_SIDE_DATA_PROP_CHANNEL_DEPENDENT },
66
};
67
68
const AVSideDataDescriptor *av_frame_side_data_desc(enum AVFrameSideDataType type)
69
0
{
70
0
    unsigned t = type;
71
0
    if (t < FF_ARRAY_ELEMS(sd_props) && sd_props[t].name)
72
0
        return &sd_props[t];
73
0
    return NULL;
74
0
}
75
76
const char *av_frame_side_data_name(enum AVFrameSideDataType type)
77
0
{
78
0
    const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
79
0
    return desc ? desc->name : NULL;
80
0
}
81
82
static void free_side_data_entry(AVFrameSideData **ptr_sd)
83
0
{
84
0
    AVFrameSideData *sd = *ptr_sd;
85
86
0
    av_buffer_unref(&sd->buf);
87
0
    av_dict_free(&sd->metadata);
88
0
    av_freep(ptr_sd);
89
0
}
90
91
static void remove_side_data_by_entry(AVFrameSideData ***sd, int *nb_sd,
92
                                      const AVFrameSideData *target)
93
0
{
94
0
    for (int i = *nb_sd - 1; i >= 0; i--) {
95
0
        AVFrameSideData *entry = ((*sd)[i]);
96
0
        if (entry != target)
97
0
            continue;
98
99
0
        free_side_data_entry(&entry);
100
101
0
        ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
102
0
        (*nb_sd)--;
103
104
0
        return;
105
0
    }
106
0
}
107
108
void av_frame_side_data_remove(AVFrameSideData ***sd, int *nb_sd,
109
                               enum AVFrameSideDataType type)
110
0
{
111
0
    for (int i = *nb_sd - 1; i >= 0; i--) {
112
0
        AVFrameSideData *entry = ((*sd)[i]);
113
0
        if (entry->type != type)
114
0
            continue;
115
116
0
        free_side_data_entry(&entry);
117
118
0
        ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
119
0
        (*nb_sd)--;
120
0
    }
121
0
}
122
123
void av_frame_side_data_remove_by_props(AVFrameSideData ***sd, int *nb_sd,
124
                                        int props)
125
0
{
126
0
    for (int i = *nb_sd - 1; i >= 0; i--) {
127
0
        AVFrameSideData *entry = ((*sd)[i]);
128
0
        const AVSideDataDescriptor *desc = av_frame_side_data_desc(entry->type);
129
0
        if (!desc || !(desc->props & props))
130
0
            continue;
131
132
0
        free_side_data_entry(&entry);
133
134
0
        ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
135
0
        (*nb_sd)--;
136
0
    }
137
0
}
138
139
void av_frame_side_data_free(AVFrameSideData ***sd, int *nb_sd)
140
0
{
141
0
    for (int i = 0; i < *nb_sd; i++)
142
0
        free_side_data_entry(&((*sd)[i]));
143
0
    *nb_sd = 0;
144
145
0
    av_freep(sd);
146
0
}
147
148
static AVFrameSideData *add_side_data_from_buf_ext(AVFrameSideData ***sd,
149
                                                   int *nb_sd,
150
                                                   enum AVFrameSideDataType type,
151
                                                   AVBufferRef *buf, uint8_t *data,
152
                                                   size_t size)
153
0
{
154
0
    AVFrameSideData *ret, **tmp;
155
156
    // *nb_sd + 1 needs to fit into an int and a size_t.
157
0
    if ((unsigned)*nb_sd >= FFMIN(INT_MAX, SIZE_MAX))
158
0
        return NULL;
159
160
0
    tmp = av_realloc_array(*sd, *nb_sd + 1, sizeof(**sd));
161
0
    if (!tmp)
162
0
        return NULL;
163
0
    *sd = tmp;
164
165
0
    ret = av_mallocz(sizeof(*ret));
166
0
    if (!ret)
167
0
        return NULL;
168
169
0
    ret->buf = buf;
170
0
    ret->data = data;
171
0
    ret->size = size;
172
0
    ret->type = type;
173
174
0
    (*sd)[(*nb_sd)++] = ret;
175
176
0
    return ret;
177
0
}
178
179
AVFrameSideData *ff_frame_side_data_add_from_buf(AVFrameSideData ***sd,
180
                                                 int *nb_sd,
181
                                                 enum AVFrameSideDataType type,
182
                                                 AVBufferRef *buf)
183
0
{
184
0
    if (!buf)
185
0
        return NULL;
186
187
0
    return add_side_data_from_buf_ext(sd, nb_sd, type, buf, buf->data, buf->size);
188
0
}
189
190
static AVFrameSideData *replace_side_data_from_buf(AVFrameSideData *dst,
191
                                                   AVBufferRef *buf, int flags)
192
0
{
193
0
    if (!(flags & AV_FRAME_SIDE_DATA_FLAG_REPLACE))
194
0
        return NULL;
195
196
0
    av_dict_free(&dst->metadata);
197
0
    av_buffer_unref(&dst->buf);
198
0
    dst->buf  = buf;
199
0
    dst->data = buf->data;
200
0
    dst->size = buf->size;
201
0
    return dst;
202
0
}
203
204
AVFrameSideData *av_frame_side_data_new(AVFrameSideData ***sd, int *nb_sd,
205
                                        enum AVFrameSideDataType type,
206
                                        size_t size, unsigned int flags)
207
0
{
208
0
    const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
209
0
    AVBufferRef     *buf = av_buffer_alloc(size);
210
0
    AVFrameSideData *ret = NULL;
211
212
0
    if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE)
213
0
        av_frame_side_data_remove(sd, nb_sd, type);
214
0
    if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
215
0
        (ret = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, type))) {
216
0
        ret = replace_side_data_from_buf(ret, buf, flags);
217
0
        if (!ret)
218
0
            av_buffer_unref(&buf);
219
0
        return ret;
220
0
    }
221
222
0
    ret = ff_frame_side_data_add_from_buf(sd, nb_sd, type, buf);
223
0
    if (!ret)
224
0
        av_buffer_unref(&buf);
225
226
0
    return ret;
227
0
}
228
229
AVFrameSideData *av_frame_side_data_add(AVFrameSideData ***sd, int *nb_sd,
230
                                        enum AVFrameSideDataType type,
231
                                        AVBufferRef **pbuf, unsigned int flags)
232
0
{
233
0
    const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
234
0
    AVFrameSideData *sd_dst  = NULL;
235
0
    AVBufferRef *buf = *pbuf;
236
237
0
    if ((flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF) && !(buf = av_buffer_ref(*pbuf)))
238
0
        return NULL;
239
0
    if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE)
240
0
        av_frame_side_data_remove(sd, nb_sd, type);
241
0
    if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
242
0
        (sd_dst = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, type))) {
243
0
        sd_dst = replace_side_data_from_buf(sd_dst, buf, flags);
244
0
    } else
245
0
        sd_dst = ff_frame_side_data_add_from_buf(sd, nb_sd, type, buf);
246
247
0
    if (sd_dst && !(flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF))
248
0
        *pbuf = NULL;
249
0
    else if (!sd_dst && (flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF))
250
0
        av_buffer_unref(&buf);
251
0
    return sd_dst;
252
0
}
253
254
int av_frame_side_data_clone(AVFrameSideData ***sd, int *nb_sd,
255
                             const AVFrameSideData *src, unsigned int flags)
256
0
{
257
0
    const AVSideDataDescriptor *desc;
258
0
    AVBufferRef     *buf    = NULL;
259
0
    AVFrameSideData *sd_dst = NULL;
260
0
    int              ret    = AVERROR_BUG;
261
262
0
    if (!sd || !src || !nb_sd || (*nb_sd && !*sd))
263
0
        return AVERROR(EINVAL);
264
265
0
    desc = av_frame_side_data_desc(src->type);
266
0
    if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE)
267
0
        av_frame_side_data_remove(sd, nb_sd, src->type);
268
0
    if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
269
0
        (sd_dst = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, src->type))) {
270
0
        AVDictionary *dict = NULL;
271
272
0
        if (!(flags & AV_FRAME_SIDE_DATA_FLAG_REPLACE))
273
0
            return AVERROR(EEXIST);
274
275
0
        ret = av_dict_copy(&dict, src->metadata, 0);
276
0
        if (ret < 0)
277
0
            return ret;
278
279
0
        ret = av_buffer_replace(&sd_dst->buf, src->buf);
280
0
        if (ret < 0) {
281
0
            av_dict_free(&dict);
282
0
            return ret;
283
0
        }
284
285
0
        av_dict_free(&sd_dst->metadata);
286
0
        sd_dst->metadata = dict;
287
0
        sd_dst->data     = src->data;
288
0
        sd_dst->size     = src->size;
289
0
        return 0;
290
0
    }
291
292
0
    buf = av_buffer_ref(src->buf);
293
0
    if (!buf)
294
0
        return AVERROR(ENOMEM);
295
296
0
    sd_dst = add_side_data_from_buf_ext(sd, nb_sd, src->type, buf,
297
0
                                        src->data, src->size);
298
0
    if (!sd_dst) {
299
0
        av_buffer_unref(&buf);
300
0
        return AVERROR(ENOMEM);
301
0
    }
302
303
0
    ret = av_dict_copy(&sd_dst->metadata, src->metadata, 0);
304
0
    if (ret < 0) {
305
0
        remove_side_data_by_entry(sd, nb_sd, sd_dst);
306
0
        return ret;
307
0
    }
308
309
0
    return 0;
310
0
}
311
312
const AVFrameSideData *av_frame_side_data_get_c(const AVFrameSideData * const *sd,
313
                                                const int nb_sd,
314
                                                enum AVFrameSideDataType type)
315
0
{
316
0
    for (int i = 0; i < nb_sd; i++) {
317
0
        if (sd[i]->type == type)
318
0
            return sd[i];
319
0
    }
320
0
    return NULL;
321
0
}