FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/codec_internal.h
Date: 2026-08-15 14:54:27
Exec Total Coverage
Lines: 8 8 100.0%
Functions: 3 3 100.0%
Branches: 0 0 -%

Line Branch Exec 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 #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 #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 #define FF_CODEC_CAP_SETS_PKT_DTS (1 << 2)
51 /**
52 * The decoder extracts and fills its parameters even if the frame is
53 * skipped due to the skip_frame setting.
54 */
55 #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 #define FF_CODEC_CAP_EXPORTS_CROPPING (1 << 4)
62 /**
63 * Codec initializes slice-based threading with a main function
64 */
65 #define FF_CODEC_CAP_SLICE_THREAD_HAS_MF (1 << 5)
66 /**
67 * The decoder might make use of the ProgressFrame API.
68 */
69 #define FF_CODEC_CAP_USES_PROGRESSFRAMES (1 << 6)
70 /**
71 * Codec handles avctx->thread_count == 0 (auto) internally.
72 */
73 #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 #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 #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 type of the codec (decoder/encoder)
159 * and also the exact callback cb implemented by the codec.
160 * cb_type uses enum FFCodecType values.
161 */
162 unsigned cb_type:3;
163
164 int priv_data_size;
165 /**
166 * @name Frame-level threading support functions
167 * @{
168 */
169 /**
170 * Copy necessary context variables from a previous thread context to the current one.
171 * If not defined, the next thread will start automatically; otherwise, the codec
172 * must call ff_thread_finish_setup().
173 *
174 * dst and src will (rarely) point to the same context, in which case memcpy should be skipped.
175 */
176 int (*update_thread_context)(struct AVCodecContext *dst, const struct AVCodecContext *src);
177
178 /**
179 * Copy variables back to the user-facing context
180 */
181 int (*update_thread_context_for_user)(struct AVCodecContext *dst, const struct AVCodecContext *src);
182 /** @} */
183
184 /**
185 * Private codec-specific defaults.
186 */
187 const FFCodecDefault *defaults;
188
189 int (*init)(struct AVCodecContext *);
190
191 union {
192 /**
193 * Decode to an AVFrame.
194 * cb is in this state if cb_type is FF_CODEC_CB_TYPE_DECODE.
195 *
196 * @param avctx codec context
197 * @param[out] frame AVFrame for output
198 * @param[out] got_frame_ptr decoder sets to 0 or 1 to indicate that
199 * a non-empty frame was returned in frame.
200 * @param[in] avpkt AVPacket containing the data to be decoded
201 * @return amount of bytes read from the packet on success,
202 * negative error code on failure
203 */
204 int (*decode)(struct AVCodecContext *avctx, struct AVFrame *frame,
205 int *got_frame_ptr, struct AVPacket *avpkt);
206 /**
207 * Decode subtitle data to an AVSubtitle.
208 * cb is in this state if cb_type is FF_CODEC_CB_TYPE_DECODE_SUB.
209 *
210 * Apart from that this is like the decode callback.
211 */
212 int (*decode_sub)(struct AVCodecContext *avctx, struct AVSubtitle *sub,
213 int *got_frame_ptr, const struct AVPacket *avpkt);
214 /**
215 * Decode API with decoupled packet/frame dataflow.
216 * cb is in this state if cb_type is FF_CODEC_CB_TYPE_RECEIVE_FRAME.
217 *
218 * This function is called to get one output frame. It should call
219 * ff_decode_get_packet() to obtain input data.
220 */
221 int (*receive_frame)(struct AVCodecContext *avctx, struct AVFrame *frame);
222 /**
223 * Encode data to an AVPacket.
224 * cb is in this state if cb_type is FF_CODEC_CB_TYPE_ENCODE
225 *
226 * @param avctx codec context
227 * @param[out] avpkt output AVPacket
228 * @param[in] frame AVFrame containing the input to be encoded
229 * @param[out] got_packet_ptr encoder sets to 0 or 1 to indicate that a
230 * non-empty packet was returned in avpkt.
231 * @return 0 on success, negative error code on failure
232 */
233 int (*encode)(struct AVCodecContext *avctx, struct AVPacket *avpkt,
234 const struct AVFrame *frame, int *got_packet_ptr);
235 /**
236 * Encode subtitles to a raw buffer.
237 * cb is in this state if cb_type is FF_CODEC_CB_TYPE_ENCODE_SUB.
238 */
239 int (*encode_sub)(struct AVCodecContext *avctx, uint8_t *buf,
240 int buf_size, const struct AVSubtitle *sub);
241 /**
242 * Encode API with decoupled frame/packet dataflow.
243 * cb is in this state if cb_type is FF_CODEC_CB_TYPE_RECEIVE_PACKET.
244 *
245 * This function is called to get one output packet.
246 * It should call ff_encode_get_frame() to obtain input data.
247 */
248 int (*receive_packet)(struct AVCodecContext *avctx, struct AVPacket *avpkt);
249 } cb;
250
251 int (*close)(struct AVCodecContext *);
252
253 /**
254 * Flush buffers.
255 * Will be called when seeking
256 */
257 void (*flush)(struct AVCodecContext *);
258
259 union {
260 /**
261 * Encoding only. Reconfigure the encoder
262 * Called by avcodec_encode_reconfigure()
263 */
264 int (*reconf)(struct AVCodecContext *avctx, struct AVDictionary **dict);
265
266 /**
267 * Decoding only, a comma-separated list of bitstream filters to apply to
268 * packets before decoding.
269 */
270 const char *bsfs;
271 };
272
273 /**
274 * Array of pointers to hardware configurations supported by the codec,
275 * or NULL if no hardware supported. The array is terminated by a NULL
276 * pointer.
277 *
278 * The user can only access this field via avcodec_get_hw_config().
279 */
280 const struct AVCodecHWConfigInternal *const *hw_configs;
281
282 /**
283 * List of supported codec_tags, terminated by FF_CODEC_TAGS_END.
284 */
285 const uint32_t *codec_tags;
286
287 /**
288 * Custom callback for avcodec_get_supported_config(). If absent,
289 * ff_default_get_supported_config() will be used. `out_num_configs` will
290 * always be set to a valid pointer.
291 */
292 int (*get_supported_config)(const struct AVCodecContext *avctx,
293 const AVCodec *codec,
294 enum AVCodecConfig config,
295 unsigned flags,
296 const void **out_configs,
297 int *out_num_configs);
298 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
299 struct {
300 #else
301 union {
302 #endif
303 /// Video-only fields
304 struct {
305 const AVRational *supported_framerates;
306 const enum AVPixelFormat *pix_fmts;
307 };
308 /// Audio-only fields
309 struct {
310 const AVChannelLayout *ch_layouts;
311 const int *supported_samplerates;
312 const enum AVSampleFormat *sample_fmts;
313 };
314 };
315 } FFCodec;
316
317 27994228 static av_always_inline const FFCodec *ffcodec(const AVCodec *codec)
318 {
319 27994228 return (const FFCodec*)codec;
320 }
321
322 /**
323 * Internal version of av_codec_is_encoder(). Must not be called with
324 * a NULL AVCodec*.
325 */
326 6571173 static inline int ff_codec_is_encoder(const AVCodec *avcodec)
327 {
328 6571173 const FFCodec *const codec = ffcodec(avcodec);
329 6571173 return !codec->is_decoder;
330 }
331
332 /**
333 * Internal version of av_codec_is_decoder(). Must not be called with
334 * a NULL AVCodec*.
335 */
336 12251768 static inline int ff_codec_is_decoder(const AVCodec *avcodec)
337 {
338 12251768 const FFCodec *const codec = ffcodec(avcodec);
339 12251768 return codec->is_decoder;
340 }
341
342 /**
343 * Default implementation for avcodec_get_supported_config(). Will return the
344 * relevant fields from AVCodec if present, or NULL otherwise.
345 *
346 * For AVCODEC_CONFIG_COLOR_RANGE, the output will depend on the bitmask in
347 * FFCodec.color_ranges, with a value of 0 returning NULL.
348 */
349 int ff_default_get_supported_config(const struct AVCodecContext *avctx,
350 const AVCodec *codec,
351 enum AVCodecConfig config,
352 unsigned flags,
353 const void **out_configs,
354 int *out_num_configs);
355
356 #if CONFIG_SMALL
357 #define CODEC_LONG_NAME(str) .p.long_name = NULL
358 #else
359 #define CODEC_LONG_NAME(str) .p.long_name = str
360 #endif
361
362 #if HAVE_THREADS
363 #define UPDATE_THREAD_CONTEXT(func) \
364 .update_thread_context = (func)
365 #define UPDATE_THREAD_CONTEXT_FOR_USER(func) \
366 .update_thread_context_for_user = (func)
367 #else
368 #define UPDATE_THREAD_CONTEXT(func) \
369 .update_thread_context = NULL
370 #define UPDATE_THREAD_CONTEXT_FOR_USER(func) \
371 .update_thread_context_for_user = NULL
372 #endif
373
374 #define FF_CODEC_DECODE_CB(func) \
375 .is_decoder = 1, \
376 .cb_type = FF_CODEC_CB_TYPE_DECODE, \
377 .cb.decode = (func)
378 #define FF_CODEC_DECODE_SUB_CB(func) \
379 .is_decoder = 1, \
380 .cb_type = FF_CODEC_CB_TYPE_DECODE_SUB, \
381 .cb.decode_sub = (func)
382 #define FF_CODEC_RECEIVE_FRAME_CB(func) \
383 .is_decoder = 1, \
384 .cb_type = FF_CODEC_CB_TYPE_RECEIVE_FRAME, \
385 .cb.receive_frame = (func)
386 #define FF_CODEC_ENCODE_CB(func) \
387 .is_decoder = 0, \
388 .cb_type = FF_CODEC_CB_TYPE_ENCODE, \
389 .cb.encode = (func)
390 #define FF_CODEC_ENCODE_SUB_CB(func) \
391 .is_decoder = 0, \
392 .cb_type = FF_CODEC_CB_TYPE_ENCODE_SUB, \
393 .cb.encode_sub = (func)
394 #define FF_CODEC_RECEIVE_PACKET_CB(func) \
395 .is_decoder = 0, \
396 .cb_type = FF_CODEC_CB_TYPE_RECEIVE_PACKET, \
397 .cb.receive_packet = (func)
398
399 #define CODEC_CH_LAYOUTS(...) CODEC_CH_LAYOUTS_ARRAY(((const AVChannelLayout[]) { __VA_ARGS__, { 0 } }))
400 #define CODEC_CH_LAYOUTS_ARRAY(array) CODEC_ARRAY(ch_layouts, (array))
401
402 #define CODEC_SAMPLERATES(...) CODEC_SAMPLERATES_ARRAY(((const int[]) { __VA_ARGS__, 0 }))
403 #define CODEC_SAMPLERATES_ARRAY(array) CODEC_ARRAY(supported_samplerates, (array))
404
405 #define CODEC_SAMPLEFMTS(...) CODEC_SAMPLEFMTS_ARRAY(((const enum AVSampleFormat[]) { __VA_ARGS__, AV_SAMPLE_FMT_NONE }))
406 #define CODEC_SAMPLEFMTS_ARRAY(array) CODEC_ARRAY(sample_fmts, (array))
407
408 #define CODEC_FRAMERATES(...) CODEC_FRAMERATES_ARRAY(((const AVRational[]) { __VA_ARGS__, { 0, 0 } }))
409 #define CODEC_FRAMERATES_ARRAY(array) CODEC_ARRAY(supported_framerates, (array))
410
411 #define CODEC_PIXFMTS(...) CODEC_PIXFMTS_ARRAY(((const enum AVPixelFormat[]) { __VA_ARGS__, AV_PIX_FMT_NONE }))
412 #define CODEC_PIXFMTS_ARRAY(array) CODEC_ARRAY(pix_fmts, (array))
413
414 #define CODEC_ARRAY(field, array) \
415 .field = (array) \
416
417 #endif /* AVCODEC_CODEC_INTERNAL_H */
418