4 * This file is part of FFmpeg.
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.
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.
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
21 #ifndef AVCODEC_CODEC_H
22 #define AVCODEC_CODEC_H
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"
33 #include "libavcodec/codec_id.h"
34 #include "libavcodec/version.h"
37 * @addtogroup lavc_core
42 * Decoder can use draw_horiz_band callback.
44 #define AV_CODEC_CAP_DRAW_HORIZ_BAND (1 << 0)
46 * Codec uses get_buffer() for allocating buffers and supports custom allocators.
47 * If not set, it might not use get_buffer() at all or use operations that
48 * assume the buffer was allocated by avcodec_default_get_buffer.
50 #define AV_CODEC_CAP_DR1 (1 << 1)
51 #define AV_CODEC_CAP_TRUNCATED (1 << 3)
53 * Encoder or decoder requires flushing with NULL input at the end in order to
54 * give the complete and correct output.
56 * NOTE: If this flag is not set, the codec is guaranteed to never be fed with
57 * with NULL data. The user can still send NULL data to the public encode
58 * or decode function, but libavcodec will not pass it along to the codec
59 * unless this flag is set.
62 * The decoder has a non-zero delay and needs to be fed with avpkt->data=NULL,
63 * avpkt->size=0 at the end to get the delayed data until the decoder no longer
67 * The encoder needs to be fed with NULL data at the end of encoding until the
68 * encoder no longer returns data.
70 * NOTE: For encoders implementing the AVCodec.encode2() function, setting this
71 * flag also means that the encoder must set the pts and duration for
72 * each output packet. If this flag is not set, the pts and duration will
73 * be determined by libavcodec from the input frame.
75 #define AV_CODEC_CAP_DELAY (1 << 5)
77 * Codec can be fed a final frame with a smaller size.
78 * This can be used to prevent truncation of the last audio samples.
80 #define AV_CODEC_CAP_SMALL_LAST_FRAME (1 << 6)
83 * Codec can output multiple frames per AVPacket
84 * Normally demuxers return one frame at a time, demuxers which do not do
85 * are connected to a parser to split what they return into proper frames.
86 * This flag is reserved to the very rare category of codecs which have a
87 * bitstream that cannot be split into frames without timeconsuming
88 * operations like full decoding. Demuxers carrying such bitstreams thus
89 * may return multiple frames in a packet. This has many disadvantages like
90 * prohibiting stream copy in many cases thus it should only be considered
93 #define AV_CODEC_CAP_SUBFRAMES (1 << 8)
95 * Codec is experimental and is thus avoided in favor of non experimental
98 #define AV_CODEC_CAP_EXPERIMENTAL (1 << 9)
100 * Codec should fill in channel configuration and samplerate instead of container
102 #define AV_CODEC_CAP_CHANNEL_CONF (1 << 10)
104 * Codec supports frame-level multithreading.
106 #define AV_CODEC_CAP_FRAME_THREADS (1 << 12)
108 * Codec supports slice-based (or partition-based) multithreading.
110 #define AV_CODEC_CAP_SLICE_THREADS (1 << 13)
112 * Codec supports changed parameters at any point.
114 #define AV_CODEC_CAP_PARAM_CHANGE (1 << 14)
116 * Codec supports avctx->thread_count == 0 (auto).
118 #define AV_CODEC_CAP_AUTO_THREADS (1 << 15)
120 * Audio encoder supports receiving a different number of samples in each call.
122 #define AV_CODEC_CAP_VARIABLE_FRAME_SIZE (1 << 16)
124 * Decoder is not a preferred choice for probing.
125 * This indicates that the decoder is not a good choice for probing.
126 * It could for example be an expensive to spin up hardware decoder,
127 * or it could simply not provide a lot of useful information about
129 * A decoder marked with this flag should only be used as last resort
130 * choice for probing.
132 #define AV_CODEC_CAP_AVOID_PROBING (1 << 17)
134 #if FF_API_UNUSED_CODEC_CAPS
136 * Deprecated and unused. Use AVCodecDescriptor.props instead
138 #define AV_CODEC_CAP_INTRA_ONLY 0x40000000
140 * Deprecated and unused. Use AVCodecDescriptor.props instead
142 #define AV_CODEC_CAP_LOSSLESS 0x80000000
146 * Codec is backed by a hardware implementation. Typically used to
147 * identify a non-hwaccel hardware decoder. For information about hwaccels, use
148 * avcodec_get_hw_config() instead.
150 #define AV_CODEC_CAP_HARDWARE (1 << 18)
153 * Codec is potentially backed by a hardware implementation, but not
154 * necessarily. This is used instead of AV_CODEC_CAP_HARDWARE, if the
155 * implementation provides some sort of internal fallback.
157 #define AV_CODEC_CAP_HYBRID (1 << 19)
160 * This codec takes the reordered_opaque field from input AVFrames
161 * and returns it in the corresponding field in AVCodecContext after
164 #define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE (1 << 20)
167 * This encoder can be flushed using avcodec_flush_buffers(). If this flag is
168 * not set, the encoder must be closed and reopened to ensure that no frames
171 #define AV_CODEC_CAP_ENCODER_FLUSH (1 << 21)
176 typedef struct AVProfile {
178 const char *name; ///< short name for the profile
181 typedef struct AVCodecDefault AVCodecDefault;
183 struct AVCodecContext;
190 typedef struct AVCodec {
192 * Name of the codec implementation.
193 * The name is globally unique among encoders and among decoders (but an
194 * encoder and a decoder can share the same name).
195 * This is the primary way to find a codec from the user perspective.
199 * Descriptive name for the codec, meant to be more human readable than name.
200 * You should use the NULL_IF_CONFIG_SMALL() macro to define it.
202 const char *long_name;
203 enum AVMediaType type;
206 * Codec capabilities.
210 const AVRational *supported_framerates; ///< array of supported framerates, or NULL if any, array is terminated by {0,0}
211 const enum AVPixelFormat *pix_fmts; ///< array of supported pixel formats, or NULL if unknown, array is terminated by -1
212 const int *supported_samplerates; ///< array of supported audio samplerates, or NULL if unknown, array is terminated by 0
213 const enum AVSampleFormat *sample_fmts; ///< array of supported sample formats, or NULL if unknown, array is terminated by -1
214 const uint64_t *channel_layouts; ///< array of support channel layouts, or NULL if unknown. array is terminated by 0
215 uint8_t max_lowres; ///< maximum value for lowres supported by the decoder
216 const AVClass *priv_class; ///< AVClass for the private context
217 const AVProfile *profiles; ///< array of recognized profiles, or NULL if unknown, array is terminated by {FF_PROFILE_UNKNOWN}
220 * Group name of the codec implementation.
221 * This is a short symbolic name of the wrapper backing this codec. A
222 * wrapper uses some kind of external implementation for the codec, such
223 * as an external library, or a codec implementation provided by the OS or
225 * If this field is NULL, this is a builtin, libavcodec native codec.
226 * If non-NULL, this will be the suffix in AVCodec.name in most cases
227 * (usually AVCodec.name will be of the form "<codec_name>_<wrapper_name>").
229 const char *wrapper_name;
231 /*****************************************************************
232 * No fields below this line are part of the public API. They
233 * may not be used outside of libavcodec and can be changed and
235 * New public fields should be added right above.
236 *****************************************************************
239 struct AVCodec *next;
241 * @name Frame-level threading support functions
245 * Copy necessary context variables from a previous thread context to the current one.
246 * If not defined, the next thread will start automatically; otherwise, the codec
247 * must call ff_thread_finish_setup().
249 * dst and src will (rarely) point to the same context, in which case memcpy should be skipped.
251 int (*update_thread_context)(struct AVCodecContext *dst, const struct AVCodecContext *src);
255 * Private codec-specific defaults.
257 const AVCodecDefault *defaults;
260 * Initialize codec static data, called from avcodec_register().
262 * This is not intended for time consuming operations as it is
263 * run for every codec regardless of that codec being used.
265 void (*init_static_data)(struct AVCodec *codec);
267 int (*init)(struct AVCodecContext *);
268 int (*encode_sub)(struct AVCodecContext *, uint8_t *buf, int buf_size,
269 const struct AVSubtitle *sub);
271 * Encode data to an AVPacket.
273 * @param avctx codec context
274 * @param avpkt output AVPacket (may contain a user-provided buffer)
275 * @param[in] frame AVFrame containing the raw data to be encoded
276 * @param[out] got_packet_ptr encoder sets to 0 or 1 to indicate that a
277 * non-empty packet was returned in avpkt.
278 * @return 0 on success, negative error code on failure
280 int (*encode2)(struct AVCodecContext *avctx, struct AVPacket *avpkt,
281 const struct AVFrame *frame, int *got_packet_ptr);
282 int (*decode)(struct AVCodecContext *, void *outdata, int *outdata_size, struct AVPacket *avpkt);
283 int (*close)(struct AVCodecContext *);
285 * Encode API with decoupled packet/frame dataflow. The API is the
286 * same as the avcodec_ prefixed APIs (avcodec_send_frame() etc.), except
288 * - never called if the codec is closed or the wrong type,
289 * - if AV_CODEC_CAP_DELAY is not set, drain frames are never sent,
290 * - only one drain frame is ever passed down,
292 int (*send_frame)(struct AVCodecContext *avctx, const struct AVFrame *frame);
293 int (*receive_packet)(struct AVCodecContext *avctx, struct AVPacket *avpkt);
296 * Decode API with decoupled packet/frame dataflow. This function is called
297 * to get one output frame. It should call ff_decode_get_packet() to obtain
300 int (*receive_frame)(struct AVCodecContext *avctx, struct AVFrame *frame);
303 * Will be called when seeking
305 void (*flush)(struct AVCodecContext *);
307 * Internal codec capabilities.
308 * See FF_CODEC_CAP_* in internal.h
313 * Decoding only, a comma-separated list of bitstream filters to apply to
314 * packets before decoding.
319 * Array of pointers to hardware configurations supported by the codec,
320 * or NULL if no hardware supported. The array is terminated by a NULL
323 * The user can only access this field via avcodec_get_hw_config().
325 const struct AVCodecHWConfigInternal **hw_configs;
328 * List of supported codec_tags, terminated by FF_CODEC_TAGS_END.
330 const uint32_t *codec_tags;
334 * Iterate over all registered codecs.
336 * @param opaque a pointer where libavcodec will store the iteration state. Must
337 * point to NULL to start the iteration.
339 * @return the next registered codec or NULL when the iteration is
342 const AVCodec *av_codec_iterate(void **opaque);
345 * Find a registered decoder with a matching codec ID.
347 * @param id AVCodecID of the requested decoder
348 * @return A decoder if one was found, NULL otherwise.
350 AVCodec *avcodec_find_decoder(enum AVCodecID id);
353 * Find a registered decoder with the specified name.
355 * @param name name of the requested decoder
356 * @return A decoder if one was found, NULL otherwise.
358 AVCodec *avcodec_find_decoder_by_name(const char *name);
361 * Find a registered encoder with a matching codec ID.
363 * @param id AVCodecID of the requested encoder
364 * @return An encoder if one was found, NULL otherwise.
366 AVCodec *avcodec_find_encoder(enum AVCodecID id);
369 * Find a registered encoder with the specified name.
371 * @param name name of the requested encoder
372 * @return An encoder if one was found, NULL otherwise.
374 AVCodec *avcodec_find_encoder_by_name(const char *name);
376 * @return a non-zero number if codec is an encoder, zero otherwise
378 int av_codec_is_encoder(const AVCodec *codec);
381 * @return a non-zero number if codec is a decoder, zero otherwise
383 int av_codec_is_decoder(const AVCodec *codec);
387 * The codec supports this format via the hw_device_ctx interface.
389 * When selecting this format, AVCodecContext.hw_device_ctx should
390 * have been set to a device of the specified type before calling
393 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX = 0x01,
395 * The codec supports this format via the hw_frames_ctx interface.
397 * When selecting this format for a decoder,
398 * AVCodecContext.hw_frames_ctx should be set to a suitable frames
399 * context inside the get_format() callback. The frames context
400 * must have been created on a device of the specified type.
402 * When selecting this format for an encoder,
403 * AVCodecContext.hw_frames_ctx should be set to the context which
404 * will be used for the input frames before calling avcodec_open2().
406 AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX = 0x02,
408 * The codec supports this format by some internal method.
410 * This format can be selected without any additional configuration -
411 * no device or frames context is required.
413 AV_CODEC_HW_CONFIG_METHOD_INTERNAL = 0x04,
415 * The codec supports this format by some ad-hoc method.
417 * Additional settings and/or function calls are required. See the
418 * codec-specific documentation for details. (Methods requiring
419 * this sort of configuration are deprecated and others should be
420 * used in preference.)
422 AV_CODEC_HW_CONFIG_METHOD_AD_HOC = 0x08,
425 typedef struct AVCodecHWConfig {
427 * For decoders, a hardware pixel format which that decoder may be
428 * able to decode to if suitable hardware is available.
430 * For encoders, a pixel format which the encoder may be able to
431 * accept. If set to AV_PIX_FMT_NONE, this applies to all pixel
432 * formats supported by the codec.
434 enum AVPixelFormat pix_fmt;
436 * Bit set of AV_CODEC_HW_CONFIG_METHOD_* flags, describing the possible
437 * setup methods which can be used with this configuration.
441 * The device type associated with the configuration.
443 * Must be set for AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX and
444 * AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX, otherwise unused.
446 enum AVHWDeviceType device_type;
450 * Retrieve supported hardware configurations for a codec.
452 * Values of index from zero to some maximum return the indexed configuration
453 * descriptor; all other values return NULL. If the codec does not support
454 * any hardware configurations then it will always return NULL.
456 const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *codec, int index);
462 #endif /* AVCODEC_CODEC_H */