FFmpeg  5.0.1
codec.h
Go to the documentation of this file.
1 /*
2  * AVCodec public API
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef AVCODEC_CODEC_H
22 #define AVCODEC_CODEC_H
23 
24 #include <stdint.h>
25 
26 #include "libavutil/avutil.h"
27 #include "libavutil/hwcontext.h"
28 #include "libavutil/log.h"
29 #include "libavutil/pixfmt.h"
30 #include "libavutil/rational.h"
31 #include "libavutil/samplefmt.h"
32 
33 #include "libavcodec/codec_id.h"
34 #include "libavcodec/version.h"
35 
36 /**
37  * @addtogroup lavc_core
38  * @{
39  */
40 
41 /**
42  * Decoder can use draw_horiz_band callback.
43  */
44 #define AV_CODEC_CAP_DRAW_HORIZ_BAND (1 << 0)
45 /**
46  * Codec uses get_buffer() or get_encode_buffer() for allocating buffers and
47  * supports custom allocators.
48  * If not set, it might not use get_buffer() or get_encode_buffer() at all, or
49  * use operations that assume the buffer was allocated by
50  * avcodec_default_get_buffer2 or avcodec_default_get_encode_buffer.
51  */
52 #define AV_CODEC_CAP_DR1 (1 << 1)
53 #if FF_API_FLAG_TRUNCATED
54 /**
55  * @deprecated Use parsers to always send proper frames.
56  */
57 #define AV_CODEC_CAP_TRUNCATED (1 << 3)
58 #endif
59 /**
60  * Encoder or decoder requires flushing with NULL input at the end in order to
61  * give the complete and correct output.
62  *
63  * NOTE: If this flag is not set, the codec is guaranteed to never be fed with
64  * with NULL data. The user can still send NULL data to the public encode
65  * or decode function, but libavcodec will not pass it along to the codec
66  * unless this flag is set.
67  *
68  * Decoders:
69  * The decoder has a non-zero delay and needs to be fed with avpkt->data=NULL,
70  * avpkt->size=0 at the end to get the delayed data until the decoder no longer
71  * returns frames.
72  *
73  * Encoders:
74  * The encoder needs to be fed with NULL data at the end of encoding until the
75  * encoder no longer returns data.
76  *
77  * NOTE: For encoders implementing the AVCodec.encode2() function, setting this
78  * flag also means that the encoder must set the pts and duration for
79  * each output packet. If this flag is not set, the pts and duration will
80  * be determined by libavcodec from the input frame.
81  */
82 #define AV_CODEC_CAP_DELAY (1 << 5)
83 /**
84  * Codec can be fed a final frame with a smaller size.
85  * This can be used to prevent truncation of the last audio samples.
86  */
87 #define AV_CODEC_CAP_SMALL_LAST_FRAME (1 << 6)
88 
89 /**
90  * Codec can output multiple frames per AVPacket
91  * Normally demuxers return one frame at a time, demuxers which do not do
92  * are connected to a parser to split what they return into proper frames.
93  * This flag is reserved to the very rare category of codecs which have a
94  * bitstream that cannot be split into frames without timeconsuming
95  * operations like full decoding. Demuxers carrying such bitstreams thus
96  * may return multiple frames in a packet. This has many disadvantages like
97  * prohibiting stream copy in many cases thus it should only be considered
98  * as a last resort.
99  */
100 #define AV_CODEC_CAP_SUBFRAMES (1 << 8)
101 /**
102  * Codec is experimental and is thus avoided in favor of non experimental
103  * encoders
104  */
105 #define AV_CODEC_CAP_EXPERIMENTAL (1 << 9)
106 /**
107  * Codec should fill in channel configuration and samplerate instead of container
108  */
109 #define AV_CODEC_CAP_CHANNEL_CONF (1 << 10)
110 /**
111  * Codec supports frame-level multithreading.
112  */
113 #define AV_CODEC_CAP_FRAME_THREADS (1 << 12)
114 /**
115  * Codec supports slice-based (or partition-based) multithreading.
116  */
117 #define AV_CODEC_CAP_SLICE_THREADS (1 << 13)
118 /**
119  * Codec supports changed parameters at any point.
120  */
121 #define AV_CODEC_CAP_PARAM_CHANGE (1 << 14)
122 /**
123  * Codec supports multithreading through a method other than slice- or
124  * frame-level multithreading. Typically this marks wrappers around
125  * multithreading-capable external libraries.
126  */
127 #define AV_CODEC_CAP_OTHER_THREADS (1 << 15)
128 #if FF_API_AUTO_THREADS
129 #define AV_CODEC_CAP_AUTO_THREADS AV_CODEC_CAP_OTHER_THREADS
130 #endif
131 /**
132  * Audio encoder supports receiving a different number of samples in each call.
133  */
134 #define AV_CODEC_CAP_VARIABLE_FRAME_SIZE (1 << 16)
135 /**
136  * Decoder is not a preferred choice for probing.
137  * This indicates that the decoder is not a good choice for probing.
138  * It could for example be an expensive to spin up hardware decoder,
139  * or it could simply not provide a lot of useful information about
140  * the stream.
141  * A decoder marked with this flag should only be used as last resort
142  * choice for probing.
143  */
144 #define AV_CODEC_CAP_AVOID_PROBING (1 << 17)
145 
146 #if FF_API_UNUSED_CODEC_CAPS
147 /**
148  * Deprecated and unused. Use AVCodecDescriptor.props instead
149  */
150 #define AV_CODEC_CAP_INTRA_ONLY 0x40000000
151 /**
152  * Deprecated and unused. Use AVCodecDescriptor.props instead
153  */
154 #define AV_CODEC_CAP_LOSSLESS 0x80000000
155 #endif
156 
157 /**
158  * Codec is backed by a hardware implementation. Typically used to
159  * identify a non-hwaccel hardware decoder. For information about hwaccels, use
160  * avcodec_get_hw_config() instead.
161  */
162 #define AV_CODEC_CAP_HARDWARE (1 << 18)
163 
164 /**
165  * Codec is potentially backed by a hardware implementation, but not
166  * necessarily. This is used instead of AV_CODEC_CAP_HARDWARE, if the
167  * implementation provides some sort of internal fallback.
168  */
169 #define AV_CODEC_CAP_HYBRID (1 << 19)
170 
171 /**
172  * This codec takes the reordered_opaque field from input AVFrames
173  * and returns it in the corresponding field in AVCodecContext after
174  * encoding.
175  */
176 #define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE (1 << 20)
177 
178 /**
179  * This encoder can be flushed using avcodec_flush_buffers(). If this flag is
180  * not set, the encoder must be closed and reopened to ensure that no frames
181  * remain pending.
182  */
183 #define AV_CODEC_CAP_ENCODER_FLUSH (1 << 21)
184 
185 /**
186  * AVProfile.
187  */
188 typedef struct AVProfile {
189  int profile;
190  const char *name; ///< short name for the profile
191 } AVProfile;
192 
193 typedef struct AVCodecDefault AVCodecDefault;
194 
195 struct AVCodecContext;
196 struct AVSubtitle;
197 struct AVPacket;
198 
199 /**
200  * AVCodec.
201  */
202 typedef struct AVCodec {
203  /**
204  * Name of the codec implementation.
205  * The name is globally unique among encoders and among decoders (but an
206  * encoder and a decoder can share the same name).
207  * This is the primary way to find a codec from the user perspective.
208  */
209  const char *name;
210  /**
211  * Descriptive name for the codec, meant to be more human readable than name.
212  * You should use the NULL_IF_CONFIG_SMALL() macro to define it.
213  */
214  const char *long_name;
215  enum AVMediaType type;
216  enum AVCodecID id;
217  /**
218  * Codec capabilities.
219  * see AV_CODEC_CAP_*
220  */
222  uint8_t max_lowres; ///< maximum value for lowres supported by the decoder
223  const AVRational *supported_framerates; ///< array of supported framerates, or NULL if any, array is terminated by {0,0}
224  const enum AVPixelFormat *pix_fmts; ///< array of supported pixel formats, or NULL if unknown, array is terminated by -1
225  const int *supported_samplerates; ///< array of supported audio samplerates, or NULL if unknown, array is terminated by 0
226  const enum AVSampleFormat *sample_fmts; ///< array of supported sample formats, or NULL if unknown, array is terminated by -1
227  const uint64_t *channel_layouts; ///< array of support channel layouts, or NULL if unknown. array is terminated by 0
228  const AVClass *priv_class; ///< AVClass for the private context
229  const AVProfile *profiles; ///< array of recognized profiles, or NULL if unknown, array is terminated by {FF_PROFILE_UNKNOWN}
230 
231  /**
232  * Group name of the codec implementation.
233  * This is a short symbolic name of the wrapper backing this codec. A
234  * wrapper uses some kind of external implementation for the codec, such
235  * as an external library, or a codec implementation provided by the OS or
236  * the hardware.
237  * If this field is NULL, this is a builtin, libavcodec native codec.
238  * If non-NULL, this will be the suffix in AVCodec.name in most cases
239  * (usually AVCodec.name will be of the form "<codec_name>_<wrapper_name>").
240  */
241  const char *wrapper_name;
242 
243  /*****************************************************************
244  * No fields below this line are part of the public API. They
245  * may not be used outside of libavcodec and can be changed and
246  * removed at will.
247  * New public fields should be added right above.
248  *****************************************************************
249  */
250  /**
251  * Internal codec capabilities.
252  * See FF_CODEC_CAP_* in internal.h
253  */
255 
257  /**
258  * @name Frame-level threading support functions
259  * @{
260  */
261  /**
262  * Copy necessary context variables from a previous thread context to the current one.
263  * If not defined, the next thread will start automatically; otherwise, the codec
264  * must call ff_thread_finish_setup().
265  *
266  * dst and src will (rarely) point to the same context, in which case memcpy should be skipped.
267  */
268  int (*update_thread_context)(struct AVCodecContext *dst, const struct AVCodecContext *src);
269 
270  /**
271  * Copy variables back to the user-facing context
272  */
273  int (*update_thread_context_for_user)(struct AVCodecContext *dst, const struct AVCodecContext *src);
274  /** @} */
275 
276  /**
277  * Private codec-specific defaults.
278  */
280 
281  /**
282  * Initialize codec static data, called from av_codec_iterate().
283  *
284  * This is not intended for time consuming operations as it is
285  * run for every codec regardless of that codec being used.
286  */
287  void (*init_static_data)(struct AVCodec *codec);
288 
289  int (*init)(struct AVCodecContext *);
290  int (*encode_sub)(struct AVCodecContext *, uint8_t *buf, int buf_size,
291  const struct AVSubtitle *sub);
292  /**
293  * Encode data to an AVPacket.
294  *
295  * @param avctx codec context
296  * @param avpkt output AVPacket
297  * @param[in] frame AVFrame containing the raw data to be encoded
298  * @param[out] got_packet_ptr encoder sets to 0 or 1 to indicate that a
299  * non-empty packet was returned in avpkt.
300  * @return 0 on success, negative error code on failure
301  */
302  int (*encode2)(struct AVCodecContext *avctx, struct AVPacket *avpkt,
303  const struct AVFrame *frame, int *got_packet_ptr);
304  /**
305  * Decode picture or subtitle data.
306  *
307  * @param avctx codec context
308  * @param outdata codec type dependent output struct
309  * @param[out] got_frame_ptr decoder sets to 0 or 1 to indicate that a
310  * non-empty frame or subtitle was returned in
311  * outdata.
312  * @param[in] avpkt AVPacket containing the data to be decoded
313  * @return amount of bytes read from the packet on success, negative error
314  * code on failure
315  */
316  int (*decode)(struct AVCodecContext *avctx, void *outdata,
317  int *got_frame_ptr, struct AVPacket *avpkt);
318  int (*close)(struct AVCodecContext *);
319  /**
320  * Encode API with decoupled frame/packet dataflow. This function is called
321  * to get one output packet. It should call ff_encode_get_frame() to obtain
322  * input data.
323  */
324  int (*receive_packet)(struct AVCodecContext *avctx, struct AVPacket *avpkt);
325 
326  /**
327  * Decode API with decoupled packet/frame dataflow. This function is called
328  * to get one output frame. It should call ff_decode_get_packet() to obtain
329  * input data.
330  */
331  int (*receive_frame)(struct AVCodecContext *avctx, struct AVFrame *frame);
332  /**
333  * Flush buffers.
334  * Will be called when seeking
335  */
336  void (*flush)(struct AVCodecContext *);
337 
338  /**
339  * Decoding only, a comma-separated list of bitstream filters to apply to
340  * packets before decoding.
341  */
342  const char *bsfs;
343 
344  /**
345  * Array of pointers to hardware configurations supported by the codec,
346  * or NULL if no hardware supported. The array is terminated by a NULL
347  * pointer.
348  *
349  * The user can only access this field via avcodec_get_hw_config().
350  */
351  const struct AVCodecHWConfigInternal *const *hw_configs;
352 
353  /**
354  * List of supported codec_tags, terminated by FF_CODEC_TAGS_END.
355  */
356  const uint32_t *codec_tags;
357 } AVCodec;
358 
359 /**
360  * Iterate over all registered codecs.
361  *
362  * @param opaque a pointer where libavcodec will store the iteration state. Must
363  * point to NULL to start the iteration.
364  *
365  * @return the next registered codec or NULL when the iteration is
366  * finished
367  */
368 const AVCodec *av_codec_iterate(void **opaque);
369 
370 /**
371  * Find a registered decoder with a matching codec ID.
372  *
373  * @param id AVCodecID of the requested decoder
374  * @return A decoder if one was found, NULL otherwise.
375  */
377 
378 /**
379  * Find a registered decoder with the specified name.
380  *
381  * @param name name of the requested decoder
382  * @return A decoder if one was found, NULL otherwise.
383  */
384 const AVCodec *avcodec_find_decoder_by_name(const char *name);
385 
386 /**
387  * Find a registered encoder with a matching codec ID.
388  *
389  * @param id AVCodecID of the requested encoder
390  * @return An encoder if one was found, NULL otherwise.
391  */
393 
394 /**
395  * Find a registered encoder with the specified name.
396  *
397  * @param name name of the requested encoder
398  * @return An encoder if one was found, NULL otherwise.
399  */
400 const AVCodec *avcodec_find_encoder_by_name(const char *name);
401 /**
402  * @return a non-zero number if codec is an encoder, zero otherwise
403  */
404 int av_codec_is_encoder(const AVCodec *codec);
405 
406 /**
407  * @return a non-zero number if codec is a decoder, zero otherwise
408  */
409 int av_codec_is_decoder(const AVCodec *codec);
410 
411 /**
412  * Return a name for the specified profile, if available.
413  *
414  * @param codec the codec that is searched for the given profile
415  * @param profile the profile value for which a name is requested
416  * @return A name for the profile if found, NULL otherwise.
417  */
418 const char *av_get_profile_name(const AVCodec *codec, int profile);
419 
420 enum {
421  /**
422  * The codec supports this format via the hw_device_ctx interface.
423  *
424  * When selecting this format, AVCodecContext.hw_device_ctx should
425  * have been set to a device of the specified type before calling
426  * avcodec_open2().
427  */
429  /**
430  * The codec supports this format via the hw_frames_ctx interface.
431  *
432  * When selecting this format for a decoder,
433  * AVCodecContext.hw_frames_ctx should be set to a suitable frames
434  * context inside the get_format() callback. The frames context
435  * must have been created on a device of the specified type.
436  *
437  * When selecting this format for an encoder,
438  * AVCodecContext.hw_frames_ctx should be set to the context which
439  * will be used for the input frames before calling avcodec_open2().
440  */
442  /**
443  * The codec supports this format by some internal method.
444  *
445  * This format can be selected without any additional configuration -
446  * no device or frames context is required.
447  */
449  /**
450  * The codec supports this format by some ad-hoc method.
451  *
452  * Additional settings and/or function calls are required. See the
453  * codec-specific documentation for details. (Methods requiring
454  * this sort of configuration are deprecated and others should be
455  * used in preference.)
456  */
458 };
459 
460 typedef struct AVCodecHWConfig {
461  /**
462  * For decoders, a hardware pixel format which that decoder may be
463  * able to decode to if suitable hardware is available.
464  *
465  * For encoders, a pixel format which the encoder may be able to
466  * accept. If set to AV_PIX_FMT_NONE, this applies to all pixel
467  * formats supported by the codec.
468  */
469  enum AVPixelFormat pix_fmt;
470  /**
471  * Bit set of AV_CODEC_HW_CONFIG_METHOD_* flags, describing the possible
472  * setup methods which can be used with this configuration.
473  */
474  int methods;
475  /**
476  * The device type associated with the configuration.
477  *
478  * Must be set for AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX and
479  * AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX, otherwise unused.
480  */
483 
484 /**
485  * Retrieve supported hardware configurations for a codec.
486  *
487  * Values of index from zero to some maximum return the indexed configuration
488  * descriptor; all other values return NULL. If the codec does not support
489  * any hardware configurations then it will always return NULL.
490  */
491 const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *codec, int index);
492 
493 /**
494  * @}
495  */
496 
497 #endif /* AVCODEC_CODEC_H */
Convenience header that includes libavutil's core.
static AVFrame * frame
const char * av_get_profile_name(const AVCodec *codec, int profile)
Return a name for the specified profile, if available.
struct AVCodecDefault AVCodecDefault
Definition: codec.h:193
int av_codec_is_encoder(const AVCodec *codec)
const AVCodecHWConfig * avcodec_get_hw_config(const AVCodec *codec, int index)
Retrieve supported hardware configurations for a codec.
const AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
const AVCodec * avcodec_find_encoder(enum AVCodecID id)
Find a registered encoder with a matching codec ID.
int av_codec_is_decoder(const AVCodec *codec)
const AVCodec * av_codec_iterate(void **opaque)
Iterate over all registered codecs.
const AVCodec * avcodec_find_encoder_by_name(const char *name)
Find a registered encoder with the specified name.
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:47
const AVCodec * avcodec_find_decoder_by_name(const char *name)
Find a registered decoder with the specified name.
@ AV_CODEC_HW_CONFIG_METHOD_AD_HOC
The codec supports this format by some ad-hoc method.
Definition: codec.h:457
@ AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX
The codec supports this format via the hw_frames_ctx interface.
Definition: codec.h:441
@ AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
The codec supports this format via the hw_device_ctx interface.
Definition: codec.h:428
@ AV_CODEC_HW_CONFIG_METHOD_INTERNAL
The codec supports this format by some internal method.
Definition: codec.h:448
AVMediaType
Definition: avutil.h:199
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
AVHWDeviceType
Definition: hwcontext.h:27
Libavcodec version macros.
pixel format definitions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
Utilties for rational number calculation.
Describe the class of an AVClass context structure.
Definition: log.h:66
main external API structure.
Definition: avcodec.h:383
enum AVHWDeviceType device_type
The device type associated with the configuration.
Definition: codec.h:481
int methods
Bit set of AV_CODEC_HW_CONFIG_METHOD_* flags, describing the possible setup methods which can be used...
Definition: codec.h:474
enum AVPixelFormat pix_fmt
For decoders, a hardware pixel format which that decoder may be able to decode to if suitable hardwar...
Definition: codec.h:469
AVCodec.
Definition: codec.h:202
enum AVCodecID id
Definition: codec.h:216
int(* update_thread_context_for_user)(struct AVCodecContext *dst, const struct AVCodecContext *src)
Copy variables back to the user-facing context.
Definition: codec.h:273
const AVCodecDefault * defaults
Private codec-specific defaults.
Definition: codec.h:279
const uint64_t * channel_layouts
array of support channel layouts, or NULL if unknown. array is terminated by 0
Definition: codec.h:227
int(* receive_packet)(struct AVCodecContext *avctx, struct AVPacket *avpkt)
Encode API with decoupled frame/packet dataflow.
Definition: codec.h:324
int caps_internal
Internal codec capabilities.
Definition: codec.h:254
int(* receive_frame)(struct AVCodecContext *avctx, struct AVFrame *frame)
Decode API with decoupled packet/frame dataflow.
Definition: codec.h:331
const char * bsfs
Decoding only, a comma-separated list of bitstream filters to apply to packets before decoding.
Definition: codec.h:342
void(* init_static_data)(struct AVCodec *codec)
Initialize codec static data, called from av_codec_iterate().
Definition: codec.h:287
const AVClass * priv_class
AVClass for the private context.
Definition: codec.h:228
int(* init)(struct AVCodecContext *)
Definition: codec.h:289
const AVRational * supported_framerates
array of supported framerates, or NULL if any, array is terminated by {0,0}
Definition: codec.h:223
const struct AVCodecHWConfigInternal *const * hw_configs
Array of pointers to hardware configurations supported by the codec, or NULL if no hardware supported...
Definition: codec.h:351
int(* update_thread_context)(struct AVCodecContext *dst, const struct AVCodecContext *src)
Copy necessary context variables from a previous thread context to the current one.
Definition: codec.h:268
int(* decode)(struct AVCodecContext *avctx, void *outdata, int *got_frame_ptr, struct AVPacket *avpkt)
Decode picture or subtitle data.
Definition: codec.h:316
int priv_data_size
Definition: codec.h:256
void(* flush)(struct AVCodecContext *)
Flush buffers.
Definition: codec.h:336
enum AVSampleFormat * sample_fmts
array of supported sample formats, or NULL if unknown, array is terminated by -1
Definition: codec.h:226
const uint32_t * codec_tags
List of supported codec_tags, terminated by FF_CODEC_TAGS_END.
Definition: codec.h:356
enum AVMediaType type
Definition: codec.h:215
enum AVPixelFormat * pix_fmts
array of supported pixel formats, or NULL if unknown, array is terminated by -1
Definition: codec.h:224
const AVProfile * profiles
array of recognized profiles, or NULL if unknown, array is terminated by {FF_PROFILE_UNKNOWN}
Definition: codec.h:229
const char * long_name
Descriptive name for the codec, meant to be more human readable than name.
Definition: codec.h:214
const char * wrapper_name
Group name of the codec implementation.
Definition: codec.h:241
const char * name
Name of the codec implementation.
Definition: codec.h:209
int(* encode2)(struct AVCodecContext *avctx, struct AVPacket *avpkt, const struct AVFrame *frame, int *got_packet_ptr)
Encode data to an AVPacket.
Definition: codec.h:302
int(* close)(struct AVCodecContext *)
Definition: codec.h:318
int(* encode_sub)(struct AVCodecContext *, uint8_t *buf, int buf_size, const struct AVSubtitle *sub)
Definition: codec.h:290
const int * supported_samplerates
array of supported audio samplerates, or NULL if unknown, array is terminated by 0
Definition: codec.h:225
int capabilities
Codec capabilities.
Definition: codec.h:221
uint8_t max_lowres
maximum value for lowres supported by the decoder
Definition: codec.h:222
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
This structure stores compressed data.
Definition: packet.h:350
AVProfile.
Definition: codec.h:188
int profile
Definition: codec.h:189
const char * name
short name for the profile
Definition: codec.h:190
Rational number (pair of numerator and denominator).
Definition: rational.h:58