Coverage Report

Created: 2026-04-29 07:00

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