Coverage Report

Created: 2026-10-28 07:09

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