Coverage Report

Created: 2026-01-25 07:18

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