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