Coverage Report

Created: 2026-01-25 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/codec_internal.h
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
#ifndef AVCODEC_CODEC_INTERNAL_H
20
#define AVCODEC_CODEC_INTERNAL_H
21
22
#include <stdint.h>
23
24
#include "libavutil/attributes.h"
25
#include "codec.h"
26
#include "config.h"
27
28
/**
29
 * The codec is not known to be init-threadsafe (i.e. it might be unsafe
30
 * to initialize this codec and another codec concurrently, typically because
31
 * the codec calls external APIs that are not known to be thread-safe).
32
 * Therefore calling the codec's init function needs to be guarded with a lock.
33
 */
34
0
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE    (1 << 0)
35
/**
36
 * The codec allows calling the close function for deallocation even if
37
 * the init function returned a failure. Without this capability flag, a
38
 * codec does such cleanup internally when returning failures from the
39
 * init function and does not expect the close function to be called at
40
 * all.
41
 */
42
0
#define FF_CODEC_CAP_INIT_CLEANUP           (1 << 1)
43
/**
44
 * Decoders marked with FF_CODEC_CAP_SETS_PKT_DTS want to set
45
 * AVFrame.pkt_dts manually. If the flag is set, decode.c won't overwrite
46
 * this field. If it's unset, decode.c tries to guess the pkt_dts field
47
 * from the input AVPacket.
48
 */
49
0
#define FF_CODEC_CAP_SETS_PKT_DTS           (1 << 2)
50
/**
51
 * The decoder extracts and fills its parameters even if the frame is
52
 * skipped due to the skip_frame setting.
53
 */
54
0
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM  (1 << 3)
55
/**
56
 * The decoder sets the cropping fields in the output frames manually.
57
 * If this cap is set, the generic code will initialize output frame
58
 * dimensions to coded rather than display values.
59
 */
60
0
#define FF_CODEC_CAP_EXPORTS_CROPPING       (1 << 4)
61
/**
62
 * Codec initializes slice-based threading with a main function
63
 */
64
0
#define FF_CODEC_CAP_SLICE_THREAD_HAS_MF    (1 << 5)
65
/**
66
 * The decoder might make use of the ProgressFrame API.
67
 */
68
0
#define FF_CODEC_CAP_USES_PROGRESSFRAMES    (1 << 6)
69
/**
70
 * Codec handles avctx->thread_count == 0 (auto) internally.
71
 */
72
0
#define FF_CODEC_CAP_AUTO_THREADS           (1 << 7)
73
/**
74
 * Codec handles output frame properties internally instead of letting the
75
 * internal logic derive them from AVCodecInternal.last_pkt_props.
76
 */
77
0
#define FF_CODEC_CAP_SETS_FRAME_PROPS       (1 << 8)
78
/**
79
 * Codec supports embedded ICC profiles (AV_FRAME_DATA_ICC_PROFILE).
80
 */
81
#define FF_CODEC_CAP_ICC_PROFILES           (1 << 9)
82
/**
83
 * The encoder has AV_CODEC_CAP_DELAY set, but does not actually have delay - it
84
 * only wants to be flushed at the end to update some context variables (e.g.
85
 * 2pass stats) or produce a trailing packet. Besides that it immediately
86
 * produces exactly one output packet per each input frame, just as no-delay
87
 * encoders do.
88
 */
89
0
#define FF_CODEC_CAP_EOF_FLUSH              (1 << 10)
90
91
/**
92
 * FFCodec.codec_tags termination value
93
 */
94
#define FF_CODEC_TAGS_END -1
95
96
typedef struct FFCodecDefault {
97
    const char *key;
98
    const char *value;
99
} FFCodecDefault;
100
101
struct AVCodecContext;
102
struct AVSubtitle;
103
struct AVPacket;
104
enum AVCodecConfig;
105
106
enum FFCodecType {
107
    /* The codec is a decoder using the decode callback;
108
     * audio and video codecs only. */
109
    FF_CODEC_CB_TYPE_DECODE,
110
    /* The codec is a decoder using the decode_sub callback;
111
     * subtitle codecs only. */
112
    FF_CODEC_CB_TYPE_DECODE_SUB,
113
    /* The codec is a decoder using the receive_frame callback;
114
     * audio and video codecs only. */
115
    FF_CODEC_CB_TYPE_RECEIVE_FRAME,
116
    /* The codec is an encoder using the encode callback;
117
     * audio and video codecs only. */
118
    FF_CODEC_CB_TYPE_ENCODE,
119
    /* The codec is an encoder using the encode_sub callback;
120
     * subtitle codecs only. */
121
    FF_CODEC_CB_TYPE_ENCODE_SUB,
122
    /* The codec is an encoder using the receive_packet callback;
123
     * audio and video codecs only. */
124
    FF_CODEC_CB_TYPE_RECEIVE_PACKET,
125
};
126
127
typedef struct FFCodec {
128
    /**
129
     * The public AVCodec. See codec.h for it.
130
     */
131
    AVCodec p;
132
133
    /**
134
     * Internal codec capabilities FF_CODEC_CAP_*.
135
     */
136
    unsigned caps_internal:24;
137
138
    /**
139
     * Is this a decoder?
140
     */
141
    unsigned is_decoder:1;
142
143
    /**
144
     * This field determines the video color ranges supported by an encoder.
145
     * Should be set to a bitmask of AVCOL_RANGE_MPEG and AVCOL_RANGE_JPEG.
146
     */
147
    unsigned color_ranges:2;
148
149
    /**
150
     * This field determines the alpha modes supported by an encoder.
151
     * Should be set to a bitmask of AVALPHA_MODE_PREMULTIPLIED and AVALPHA_MODE_STRAIGHT.
152
     */
153
    unsigned alpha_modes:2;
154
155
    /**
156
     * This field determines the type of the codec (decoder/encoder)
157
     * and also the exact callback cb implemented by the codec.
158
     * cb_type uses enum FFCodecType values.
159
     */
160
    unsigned cb_type:3;
161
162
    int priv_data_size;
163
    /**
164
     * @name Frame-level threading support functions
165
     * @{
166
     */
167
    /**
168
     * Copy necessary context variables from a previous thread context to the current one.
169
     * If not defined, the next thread will start automatically; otherwise, the codec
170
     * must call ff_thread_finish_setup().
171
     *
172
     * dst and src will (rarely) point to the same context, in which case memcpy should be skipped.
173
     */
174
    int (*update_thread_context)(struct AVCodecContext *dst, const struct AVCodecContext *src);
175
176
    /**
177
     * Copy variables back to the user-facing context
178
     */
179
    int (*update_thread_context_for_user)(struct AVCodecContext *dst, const struct AVCodecContext *src);
180
    /** @} */
181
182
    /**
183
     * Private codec-specific defaults.
184
     */
185
    const FFCodecDefault *defaults;
186
187
    int (*init)(struct AVCodecContext *);
188
189
    union {
190
        /**
191
         * Decode to an AVFrame.
192
         * cb is in this state if cb_type is FF_CODEC_CB_TYPE_DECODE.
193
         *
194
         * @param      avctx          codec context
195
         * @param[out] frame          AVFrame for output
196
         * @param[out] got_frame_ptr  decoder sets to 0 or 1 to indicate that
197
         *                            a non-empty frame was returned in frame.
198
         * @param[in]  avpkt          AVPacket containing the data to be decoded
199
         * @return amount of bytes read from the packet on success,
200
         *         negative error code on failure
201
         */
202
        int (*decode)(struct AVCodecContext *avctx, struct AVFrame *frame,
203
                      int *got_frame_ptr, struct AVPacket *avpkt);
204
        /**
205
         * Decode subtitle data to an AVSubtitle.
206
         * cb is in this state if cb_type is FF_CODEC_CB_TYPE_DECODE_SUB.
207
         *
208
         * Apart from that this is like the decode callback.
209
         */
210
        int (*decode_sub)(struct AVCodecContext *avctx, struct AVSubtitle *sub,
211
                          int *got_frame_ptr, const struct AVPacket *avpkt);
212
        /**
213
         * Decode API with decoupled packet/frame dataflow.
214
         * cb is in this state if cb_type is FF_CODEC_CB_TYPE_RECEIVE_FRAME.
215
         *
216
         * This function is called to get one output frame. It should call
217
         * ff_decode_get_packet() to obtain input data.
218
         */
219
        int (*receive_frame)(struct AVCodecContext *avctx, struct AVFrame *frame);
220
        /**
221
         * Encode data to an AVPacket.
222
         * cb is in this state if cb_type is FF_CODEC_CB_TYPE_ENCODE
223
         *
224
         * @param      avctx          codec context
225
         * @param[out] avpkt          output AVPacket
226
         * @param[in]  frame          AVFrame containing the input to be encoded
227
         * @param[out] got_packet_ptr encoder sets to 0 or 1 to indicate that a
228
         *                            non-empty packet was returned in avpkt.
229
         * @return 0 on success, negative error code on failure
230
         */
231
        int (*encode)(struct AVCodecContext *avctx, struct AVPacket *avpkt,
232
                      const struct AVFrame *frame, int *got_packet_ptr);
233
        /**
234
         * Encode subtitles to a raw buffer.
235
         * cb is in this state if cb_type is FF_CODEC_CB_TYPE_ENCODE_SUB.
236
         */
237
        int (*encode_sub)(struct AVCodecContext *avctx, uint8_t *buf,
238
                          int buf_size, const struct AVSubtitle *sub);
239
        /**
240
         * Encode API with decoupled frame/packet dataflow.
241
         * cb is in this state if cb_type is FF_CODEC_CB_TYPE_RECEIVE_PACKET.
242
         *
243
         * This function is called to get one output packet.
244
         * It should call ff_encode_get_frame() to obtain input data.
245
         */
246
        int (*receive_packet)(struct AVCodecContext *avctx, struct AVPacket *avpkt);
247
    } cb;
248
249
    int (*close)(struct AVCodecContext *);
250
251
    /**
252
     * Flush buffers.
253
     * Will be called when seeking
254
     */
255
    void (*flush)(struct AVCodecContext *);
256
257
    /**
258
     * Decoding only, a comma-separated list of bitstream filters to apply to
259
     * packets before decoding.
260
     */
261
    const char *bsfs;
262
263
    /**
264
     * Array of pointers to hardware configurations supported by the codec,
265
     * or NULL if no hardware supported.  The array is terminated by a NULL
266
     * pointer.
267
     *
268
     * The user can only access this field via avcodec_get_hw_config().
269
     */
270
    const struct AVCodecHWConfigInternal *const *hw_configs;
271
272
    /**
273
     * List of supported codec_tags, terminated by FF_CODEC_TAGS_END.
274
     */
275
    const uint32_t *codec_tags;
276
277
    /**
278
     * Custom callback for avcodec_get_supported_config(). If absent,
279
     * ff_default_get_supported_config() will be used. `out_num_configs` will
280
     * always be set to a valid pointer.
281
     */
282
    int (*get_supported_config)(const struct AVCodecContext *avctx,
283
                                const AVCodec *codec,
284
                                enum AVCodecConfig config,
285
                                unsigned flags,
286
                                const void **out_configs,
287
                                int *out_num_configs);
288
} FFCodec;
289
290
static av_always_inline const FFCodec *ffcodec(const AVCodec *codec)
291
0
{
292
0
    return (const FFCodec*)codec;
293
0
}
Unexecuted instantiation: allcodecs.c:ffcodec
Unexecuted instantiation: avcodec.c:ffcodec
Unexecuted instantiation: decode.c:ffcodec
Unexecuted instantiation: encode.c:ffcodec
Unexecuted instantiation: options.c:ffcodec
Unexecuted instantiation: pthread.c:ffcodec
Unexecuted instantiation: pthread_frame.c:ffcodec
Unexecuted instantiation: pthread_slice.c:ffcodec
Unexecuted instantiation: utils.c:ffcodec
294
295
/**
296
 * Internal version of av_codec_is_encoder(). Must not be called with
297
 * a NULL AVCodec*.
298
 */
299
static inline int ff_codec_is_encoder(const AVCodec *avcodec)
300
0
{
301
0
    const FFCodec *const codec = ffcodec(avcodec);
302
0
    return !codec->is_decoder;
303
0
}
Unexecuted instantiation: allcodecs.c:ff_codec_is_encoder
Unexecuted instantiation: avcodec.c:ff_codec_is_encoder
Unexecuted instantiation: decode.c:ff_codec_is_encoder
Unexecuted instantiation: encode.c:ff_codec_is_encoder
Unexecuted instantiation: options.c:ff_codec_is_encoder
Unexecuted instantiation: pthread.c:ff_codec_is_encoder
Unexecuted instantiation: pthread_frame.c:ff_codec_is_encoder
Unexecuted instantiation: pthread_slice.c:ff_codec_is_encoder
Unexecuted instantiation: utils.c:ff_codec_is_encoder
304
305
/**
306
 * Internal version of av_codec_is_decoder(). Must not be called with
307
 * a NULL AVCodec*.
308
 */
309
static inline int ff_codec_is_decoder(const AVCodec *avcodec)
310
0
{
311
0
    const FFCodec *const codec = ffcodec(avcodec);
312
0
    return codec->is_decoder;
313
0
}
Unexecuted instantiation: allcodecs.c:ff_codec_is_decoder
Unexecuted instantiation: avcodec.c:ff_codec_is_decoder
Unexecuted instantiation: decode.c:ff_codec_is_decoder
Unexecuted instantiation: encode.c:ff_codec_is_decoder
Unexecuted instantiation: options.c:ff_codec_is_decoder
Unexecuted instantiation: pthread.c:ff_codec_is_decoder
Unexecuted instantiation: pthread_frame.c:ff_codec_is_decoder
Unexecuted instantiation: pthread_slice.c:ff_codec_is_decoder
Unexecuted instantiation: utils.c:ff_codec_is_decoder
314
315
/**
316
 * Default implementation for avcodec_get_supported_config(). Will return the
317
 * relevant fields from AVCodec if present, or NULL otherwise.
318
 *
319
 * For AVCODEC_CONFIG_COLOR_RANGE, the output will depend on the bitmask in
320
 * FFCodec.color_ranges, with a value of 0 returning NULL.
321
 */
322
int ff_default_get_supported_config(const struct AVCodecContext *avctx,
323
                                    const AVCodec *codec,
324
                                    enum AVCodecConfig config,
325
                                    unsigned flags,
326
                                    const void **out_configs,
327
                                    int *out_num_configs);
328
329
#if CONFIG_SMALL
330
#define CODEC_LONG_NAME(str) .p.long_name = NULL
331
#else
332
#define CODEC_LONG_NAME(str) .p.long_name = str
333
#endif
334
335
#if HAVE_THREADS
336
#define UPDATE_THREAD_CONTEXT(func) \
337
        .update_thread_context          = (func)
338
#define UPDATE_THREAD_CONTEXT_FOR_USER(func) \
339
        .update_thread_context_for_user = (func)
340
#else
341
#define UPDATE_THREAD_CONTEXT(func) \
342
        .update_thread_context          = NULL
343
#define UPDATE_THREAD_CONTEXT_FOR_USER(func) \
344
        .update_thread_context_for_user = NULL
345
#endif
346
347
#define FF_CODEC_DECODE_CB(func)                          \
348
    .is_decoder        = 1,                               \
349
    .cb_type           = FF_CODEC_CB_TYPE_DECODE,         \
350
    .cb.decode         = (func)
351
#define FF_CODEC_DECODE_SUB_CB(func)                      \
352
    .is_decoder        = 1,                               \
353
    .cb_type           = FF_CODEC_CB_TYPE_DECODE_SUB,     \
354
    .cb.decode_sub     = (func)
355
#define FF_CODEC_RECEIVE_FRAME_CB(func)                   \
356
    .is_decoder        = 1,                               \
357
    .cb_type           = FF_CODEC_CB_TYPE_RECEIVE_FRAME,  \
358
    .cb.receive_frame  = (func)
359
#define FF_CODEC_ENCODE_CB(func)                          \
360
    .is_decoder        = 0,                               \
361
    .cb_type           = FF_CODEC_CB_TYPE_ENCODE,         \
362
    .cb.encode         = (func)
363
#define FF_CODEC_ENCODE_SUB_CB(func)                      \
364
    .is_decoder        = 0,                               \
365
    .cb_type           = FF_CODEC_CB_TYPE_ENCODE_SUB,     \
366
    .cb.encode_sub     = (func)
367
#define FF_CODEC_RECEIVE_PACKET_CB(func)                  \
368
    .is_decoder        = 0,                               \
369
    .cb_type           = FF_CODEC_CB_TYPE_RECEIVE_PACKET, \
370
    .cb.receive_packet = (func)
371
372
#ifdef __clang__
373
#define DISABLE_DEPRECATION_WARNINGS FF_DISABLE_DEPRECATION_WARNINGS
374
#define ENABLE_DEPRECATION_WARNINGS  FF_ENABLE_DEPRECATION_WARNINGS
375
#else
376
#define DISABLE_DEPRECATION_WARNINGS
377
#define ENABLE_DEPRECATION_WARNINGS
378
#endif
379
380
#define CODEC_CH_LAYOUTS(...) CODEC_CH_LAYOUTS_ARRAY(((const AVChannelLayout[]) { __VA_ARGS__, { 0 } }))
381
#define CODEC_CH_LAYOUTS_ARRAY(array) CODEC_ARRAY(ch_layouts, (array))
382
383
#define CODEC_SAMPLERATES(...) CODEC_SAMPLERATES_ARRAY(((const int[]) { __VA_ARGS__, 0 }))
384
#define CODEC_SAMPLERATES_ARRAY(array) CODEC_ARRAY(supported_samplerates, (array))
385
386
#define CODEC_SAMPLEFMTS(...) CODEC_SAMPLEFMTS_ARRAY(((const enum AVSampleFormat[]) { __VA_ARGS__, AV_SAMPLE_FMT_NONE }))
387
#define CODEC_SAMPLEFMTS_ARRAY(array) CODEC_ARRAY(sample_fmts, (array))
388
389
#define CODEC_FRAMERATES(...) CODEC_FRAMERATES_ARRAY(((const AVRational[]) { __VA_ARGS__, { 0, 0 } }))
390
#define CODEC_FRAMERATES_ARRAY(array) CODEC_ARRAY(supported_framerates, (array))
391
392
#define CODEC_PIXFMTS(...) CODEC_PIXFMTS_ARRAY(((const enum AVPixelFormat[]) { __VA_ARGS__, AV_PIX_FMT_NONE }))
393
#define CODEC_PIXFMTS_ARRAY(array) CODEC_ARRAY(pix_fmts, (array))
394
395
#define CODEC_ARRAY(field, array) \
396
    DISABLE_DEPRECATION_WARNINGS  \
397
    .p.field = (array)            \
398
    ENABLE_DEPRECATION_WARNINGS
399
400
#endif /* AVCODEC_CODEC_INTERNAL_H */