Coverage Report

Created: 2026-05-31 06:50

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
};
65
66
const AVSideDataDescriptor *av_frame_side_data_desc(enum AVFrameSideDataType type)
67
0
{
68
0
    unsigned t = type;
69
0
    if (t < FF_ARRAY_ELEMS(sd_props) && sd_props[t].name)
70
0
        return &sd_props[t];
71
0
    return NULL;
72
0
}
73
74
const char *av_frame_side_data_name(enum AVFrameSideDataType type)
75
0
{
76
0
    const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
77
0
    return desc ? desc->name : NULL;
78
0
}
79
80
static void free_side_data_entry(AVFrameSideData **ptr_sd)
81
0
{
82
0
    AVFrameSideData *sd = *ptr_sd;
83
84
0
    av_buffer_unref(&sd->buf);
85
0
    av_dict_free(&sd->metadata);
86
0
    av_freep(ptr_sd);
87
0
}
88
89
static void remove_side_data_by_entry(AVFrameSideData ***sd, int *nb_sd,
90
                                      const AVFrameSideData *target)
91
0
{
92
0
    for (int i = *nb_sd - 1; i >= 0; i--) {
93
0
        AVFrameSideData *entry = ((*sd)[i]);
94
0
        if (entry != target)
95
0
            continue;
96
97
0
        free_side_data_entry(&entry);
98
99
0
        ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
100
0
        (*nb_sd)--;
101
102
0
        return;
103
0
    }
104
0
}
105
106
void av_frame_side_data_remove(AVFrameSideData ***sd, int *nb_sd,
107
                               enum AVFrameSideDataType type)
108
0
{
109
0
    for (int i = *nb_sd - 1; i >= 0; i--) {
110
0
        AVFrameSideData *entry = ((*sd)[i]);
111
0
        if (entry->type != type)
112
0
            continue;
113
114
0
        free_side_data_entry(&entry);
115
116
0
        ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
117
0
        (*nb_sd)--;
118
0
    }
119
0
}
120
121
void av_frame_side_data_remove_by_props(AVFrameSideData ***sd, int *nb_sd,
122
                                        int props)
123
0
{
124
0
    for (int i = *nb_sd - 1; i >= 0; i--) {
125
0
        AVFrameSideData *entry = ((*sd)[i]);
126
0
        const AVSideDataDescriptor *desc = av_frame_side_data_desc(entry->type);
127
0
        if (!desc || !(desc->props & props))
128
0
            continue;
129
130
0
        free_side_data_entry(&entry);
131
132
0
        ((*sd)[i]) = ((*sd)[*nb_sd - 1]);
133
0
        (*nb_sd)--;
134
0
    }
135
0
}
136
137
void av_frame_side_data_free(AVFrameSideData ***sd, int *nb_sd)
138
0
{
139
0
    for (int i = 0; i < *nb_sd; i++)
140
0
        free_side_data_entry(&((*sd)[i]));
141
0
    *nb_sd = 0;
142
143
0
    av_freep(sd);
144
0
}
145
146
static AVFrameSideData *add_side_data_from_buf_ext(AVFrameSideData ***sd,
147
                                                   int *nb_sd,
148
                                                   enum AVFrameSideDataType type,
149
                                                   AVBufferRef *buf, uint8_t *data,
150
                                                   size_t size)
151
0
{
152
0
    AVFrameSideData *ret, **tmp;
153
154
    // *nb_sd + 1 needs to fit into an int and a size_t.
155
0
    if ((unsigned)*nb_sd >= FFMIN(INT_MAX, SIZE_MAX))
156
0
        return NULL;
157
158
0
    tmp = av_realloc_array(*sd, *nb_sd + 1, sizeof(**sd));
159
0
    if (!tmp)
160
0
        return NULL;
161
0
    *sd = tmp;
162
163
0
    ret = av_mallocz(sizeof(*ret));
164
0
    if (!ret)
165
0
        return NULL;
166
167
0
    ret->buf = buf;
168
0
    ret->data = data;
169
0
    ret->size = size;
170
0
    ret->type = type;
171
172
0
    (*sd)[(*nb_sd)++] = ret;
173
174
0
    return ret;
175
0
}
176
177
AVFrameSideData *ff_frame_side_data_add_from_buf(AVFrameSideData ***sd,
178
                                                 int *nb_sd,
179
                                                 enum AVFrameSideDataType type,
180
                                                 AVBufferRef *buf)
181
0
{
182
0
    if (!buf)
183
0
        return NULL;
184
185
0
    return add_side_data_from_buf_ext(sd, nb_sd, type, buf, buf->data, buf->size);
186
0
}
187
188
static AVFrameSideData *replace_side_data_from_buf(AVFrameSideData *dst,
189
                                                   AVBufferRef *buf, int flags)
190
0
{
191
0
    if (!(flags & AV_FRAME_SIDE_DATA_FLAG_REPLACE))
192
0
        return NULL;
193
194
0
    av_dict_free(&dst->metadata);
195
0
    av_buffer_unref(&dst->buf);
196
0
    dst->buf  = buf;
197
0
    dst->data = buf->data;
198
0
    dst->size = buf->size;
199
0
    return dst;
200
0
}
201
202
AVFrameSideData *av_frame_side_data_new(AVFrameSideData ***sd, int *nb_sd,
203
                                        enum AVFrameSideDataType type,
204
                                        size_t size, unsigned int flags)
205
0
{
206
0
    const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
207
0
    AVBufferRef     *buf = av_buffer_alloc(size);
208
0
    AVFrameSideData *ret = NULL;
209
210
0
    if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE)
211
0
        av_frame_side_data_remove(sd, nb_sd, type);
212
0
    if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
213
0
        (ret = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, type))) {
214
0
        ret = replace_side_data_from_buf(ret, buf, flags);
215
0
        if (!ret)
216
0
            av_buffer_unref(&buf);
217
0
        return ret;
218
0
    }
219
220
0
    ret = ff_frame_side_data_add_from_buf(sd, nb_sd, type, buf);
221
0
    if (!ret)
222
0
        av_buffer_unref(&buf);
223
224
0
    return ret;
225
0
}
226
227
AVFrameSideData *av_frame_side_data_add(AVFrameSideData ***sd, int *nb_sd,
228
                                        enum AVFrameSideDataType type,
229
                                        AVBufferRef **pbuf, unsigned int flags)
230
0
{
231
0
    const AVSideDataDescriptor *desc = av_frame_side_data_desc(type);
232
0
    AVFrameSideData *sd_dst  = NULL;
233
0
    AVBufferRef *buf = *pbuf;
234
235
0
    if ((flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF) && !(buf = av_buffer_ref(*pbuf)))
236
0
        return NULL;
237
0
    if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE)
238
0
        av_frame_side_data_remove(sd, nb_sd, type);
239
0
    if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
240
0
        (sd_dst = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, type))) {
241
0
        sd_dst = replace_side_data_from_buf(sd_dst, buf, flags);
242
0
    } else
243
0
        sd_dst = ff_frame_side_data_add_from_buf(sd, nb_sd, type, buf);
244
245
0
    if (sd_dst && !(flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF))
246
0
        *pbuf = NULL;
247
0
    else if (!sd_dst && (flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF))
248
0
        av_buffer_unref(&buf);
249
0
    return sd_dst;
250
0
}
251
252
int av_frame_side_data_clone(AVFrameSideData ***sd, int *nb_sd,
253
                             const AVFrameSideData *src, unsigned int flags)
254
0
{
255
0
    const AVSideDataDescriptor *desc;
256
0
    AVBufferRef     *buf    = NULL;
257
0
    AVFrameSideData *sd_dst = NULL;
258
0
    int              ret    = AVERROR_BUG;
259
260
0
    if (!sd || !src || !nb_sd || (*nb_sd && !*sd))
261
0
        return AVERROR(EINVAL);
262
263
0
    desc = av_frame_side_data_desc(src->type);
264
0
    if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE)
265
0
        av_frame_side_data_remove(sd, nb_sd, src->type);
266
0
    if ((!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) &&
267
0
        (sd_dst = (AVFrameSideData *)av_frame_side_data_get(*sd, *nb_sd, src->type))) {
268
0
        AVDictionary *dict = NULL;
269
270
0
        if (!(flags & AV_FRAME_SIDE_DATA_FLAG_REPLACE))
271
0
            return AVERROR(EEXIST);
272
273
0
        ret = av_dict_copy(&dict, src->metadata, 0);
274
0
        if (ret < 0)
275
0
            return ret;
276
277
0
        ret = av_buffer_replace(&sd_dst->buf, src->buf);
278
0
        if (ret < 0) {
279
0
            av_dict_free(&dict);
280
0
            return ret;
281
0
        }
282
283
0
        av_dict_free(&sd_dst->metadata);
284
0
        sd_dst->metadata = dict;
285
0
        sd_dst->data     = src->data;
286
0
        sd_dst->size     = src->size;
287
0
        return 0;
288
0
    }
289
290
0
    buf = av_buffer_ref(src->buf);
291
0
    if (!buf)
292
0
        return AVERROR(ENOMEM);
293
294
0
    sd_dst = add_side_data_from_buf_ext(sd, nb_sd, src->type, buf,
295
0
                                        src->data, src->size);
296
0
    if (!sd_dst) {
297
0
        av_buffer_unref(&buf);
298
0
        return AVERROR(ENOMEM);
299
0
    }
300
301
0
    ret = av_dict_copy(&sd_dst->metadata, src->metadata, 0);
302
0
    if (ret < 0) {
303
0
        remove_side_data_by_entry(sd, nb_sd, sd_dst);
304
0
        return ret;
305
0
    }
306
307
0
    return 0;
308
0
}
309
310
const AVFrameSideData *av_frame_side_data_get_c(const AVFrameSideData * const *sd,
311
                                                const int nb_sd,
312
                                                enum AVFrameSideDataType type)
313
0
{
314
0
    for (int i = 0; i < nb_sd; i++) {
315
0
        if (sd[i]->type == type)
316
0
            return sd[i];
317
0
    }
318
0
    return NULL;
319
0
}