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 "codec.h"
26
27/**
28 * The codec does not modify any global variables in the init function,
29 * allowing to call the init function without locking any global mutexes.
30 */
31#define FF_CODEC_CAP_INIT_THREADSAFE        (1 << 0)
32/**
33 * The codec allows calling the close function for deallocation even if
34 * the init function returned a failure. Without this capability flag, a
35 * codec does such cleanup internally when returning failures from the
36 * init function and does not expect the close function to be called at
37 * all.
38 */
39#define FF_CODEC_CAP_INIT_CLEANUP           (1 << 1)
40/**
41 * Decoders marked with FF_CODEC_CAP_SETS_PKT_DTS want to set
42 * AVFrame.pkt_dts manually. If the flag is set, decode.c won't overwrite
43 * this field. If it's unset, decode.c tries to guess the pkt_dts field
44 * from the input AVPacket.
45 */
46#define FF_CODEC_CAP_SETS_PKT_DTS           (1 << 2)
47/**
48 * The decoder extracts and fills its parameters even if the frame is
49 * skipped due to the skip_frame setting.
50 */
51#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM  (1 << 3)
52/**
53 * The decoder sets the cropping fields in the output frames manually.
54 * If this cap is set, the generic code will initialize output frame
55 * dimensions to coded rather than display values.
56 */
57#define FF_CODEC_CAP_EXPORTS_CROPPING       (1 << 4)
58/**
59 * Codec initializes slice-based threading with a main function
60 */
61#define FF_CODEC_CAP_SLICE_THREAD_HAS_MF    (1 << 5)
62/*
63 * The codec supports frame threading and has inter-frame dependencies, so it
64 * uses ff_thread_report/await_progress().
65 */
66#define FF_CODEC_CAP_ALLOCATE_PROGRESS      (1 << 6)
67/**
68 * Codec handles avctx->thread_count == 0 (auto) internally.
69 */
70#define FF_CODEC_CAP_AUTO_THREADS           (1 << 7)
71/**
72 * Codec handles output frame properties internally instead of letting the
73 * internal logic derive them from AVCodecInternal.last_pkt_props.
74 */
75#define FF_CODEC_CAP_SETS_FRAME_PROPS       (1 << 8)
76
77/**
78 * FFCodec.codec_tags termination value
79 */
80#define FF_CODEC_TAGS_END -1
81
82typedef struct FFCodecDefault {
83    const char *key;
84    const char *value;
85} FFCodecDefault;
86
87struct AVCodecContext;
88struct AVSubtitle;
89struct AVPacket;
90
91enum FFCodecType {
92    /* The codec is a decoder using the decode callback;
93     * audio and video codecs only. */
94    FF_CODEC_CB_TYPE_DECODE,
95    /* The codec is a decoder using the decode_sub callback;
96     * subtitle codecs only. */
97    FF_CODEC_CB_TYPE_DECODE_SUB,
98    /* The codec is a decoder using the receive_frame callback;
99     * audio and video codecs only. */
100    FF_CODEC_CB_TYPE_RECEIVE_FRAME,
101    /* The codec is an encoder using the encode callback;
102     * audio and video codecs only. */
103    FF_CODEC_CB_TYPE_ENCODE,
104    /* The codec is an encoder using the encode_sub callback;
105     * subtitle codecs only. */
106    FF_CODEC_CB_TYPE_ENCODE_SUB,
107    /* The codec is an encoder using the receive_packet callback;
108     * audio and video codecs only. */
109    FF_CODEC_CB_TYPE_RECEIVE_PACKET,
110};
111
112typedef struct FFCodec {
113    /**
114     * The public AVCodec. See codec.h for it.
115     */
116    AVCodec p;
117
118    /**
119     * Internal codec capabilities FF_CODEC_CAP_*.
120     */
121    unsigned caps_internal:29;
122
123    /**
124     * This field determines the type of the codec (decoder/encoder)
125     * and also the exact callback cb implemented by the codec.
126     * cb_type uses enum FFCodecType values.
127     */
128    unsigned cb_type:3;
129
130    int priv_data_size;
131    /**
132     * @name Frame-level threading support functions
133     * @{
134     */
135    /**
136     * Copy necessary context variables from a previous thread context to the current one.
137     * If not defined, the next thread will start automatically; otherwise, the codec
138     * must call ff_thread_finish_setup().
139     *
140     * dst and src will (rarely) point to the same context, in which case memcpy should be skipped.
141     */
142    int (*update_thread_context)(struct AVCodecContext *dst, const struct AVCodecContext *src);
143
144    /**
145     * Copy variables back to the user-facing context
146     */
147    int (*update_thread_context_for_user)(struct AVCodecContext *dst, const struct AVCodecContext *src);
148    /** @} */
149
150    /**
151     * Private codec-specific defaults.
152     */
153    const FFCodecDefault *defaults;
154
155    /**
156     * Initialize codec static data, called from av_codec_iterate().
157     *
158     * This is not intended for time consuming operations as it is
159     * run for every codec regardless of that codec being used.
160     */
161    void (*init_static_data)(struct FFCodec *codec);
162
163    int (*init)(struct AVCodecContext *);
164
165    union {
166        /**
167         * Decode to an AVFrame.
168         * cb is in this state if cb_type is FF_CODEC_CB_TYPE_DECODE.
169         *
170         * @param      avctx          codec context
171         * @param[out] frame          AVFrame for output
172         * @param[out] got_frame_ptr  decoder sets to 0 or 1 to indicate that
173         *                            a non-empty frame was returned in frame.
174         * @param[in]  avpkt          AVPacket containing the data to be decoded
175         * @return amount of bytes read from the packet on success,
176         *         negative error code on failure
177         */
178        int (*decode)(struct AVCodecContext *avctx, struct AVFrame *frame,
179                      int *got_frame_ptr, struct AVPacket *avpkt);
180        /**
181         * Decode subtitle data to an AVSubtitle.
182         * cb is in this state if cb_type is FF_CODEC_CB_TYPE_DECODE_SUB.
183         *
184         * Apart from that this is like the decode callback.
185         */
186        int (*decode_sub)(struct AVCodecContext *avctx, struct AVSubtitle *sub,
187                          int *got_frame_ptr, const struct AVPacket *avpkt);
188        /**
189         * Decode API with decoupled packet/frame dataflow.
190         * cb is in this state if cb_type is FF_CODEC_CB_TYPE_RECEIVE_FRAME.
191         *
192         * This function is called to get one output frame. It should call
193         * ff_decode_get_packet() to obtain input data.
194         */
195        int (*receive_frame)(struct AVCodecContext *avctx, struct AVFrame *frame);
196        /**
197         * Encode data to an AVPacket.
198         * cb is in this state if cb_type is FF_CODEC_CB_TYPE_ENCODE
199         *
200         * @param      avctx          codec context
201         * @param[out] avpkt          output AVPacket
202         * @param[in]  frame          AVFrame containing the input to be encoded
203         * @param[out] got_packet_ptr encoder sets to 0 or 1 to indicate that a
204         *                            non-empty packet was returned in avpkt.
205         * @return 0 on success, negative error code on failure
206         */
207        int (*encode)(struct AVCodecContext *avctx, struct AVPacket *avpkt,
208                      const struct AVFrame *frame, int *got_packet_ptr);
209        /**
210         * Encode subtitles to a raw buffer.
211         * cb is in this state if cb_type is FF_CODEC_CB_TYPE_ENCODE_SUB.
212         */
213        int (*encode_sub)(struct AVCodecContext *avctx, uint8_t *buf,
214                          int buf_size, const struct AVSubtitle *sub);
215        /**
216         * Encode API with decoupled frame/packet dataflow.
217         * cb is in this state if cb_type is FF_CODEC_CB_TYPE_RECEIVE_PACKET.
218         *
219         * This function is called to get one output packet.
220         * It should call ff_encode_get_frame() to obtain input data.
221         */
222        int (*receive_packet)(struct AVCodecContext *avctx, struct AVPacket *avpkt);
223    } cb;
224
225    int (*close)(struct AVCodecContext *);
226
227    /**
228     * Flush buffers.
229     * Will be called when seeking
230     */
231    void (*flush)(struct AVCodecContext *);
232
233    /**
234     * Decoding only, a comma-separated list of bitstream filters to apply to
235     * packets before decoding.
236     */
237    const char *bsfs;
238
239    /**
240     * Array of pointers to hardware configurations supported by the codec,
241     * or NULL if no hardware supported.  The array is terminated by a NULL
242     * pointer.
243     *
244     * The user can only access this field via avcodec_get_hw_config().
245     */
246    const struct AVCodecHWConfigInternal *const *hw_configs;
247
248    /**
249     * List of supported codec_tags, terminated by FF_CODEC_TAGS_END.
250     */
251    const uint32_t *codec_tags;
252} FFCodec;
253
254#define FF_CODEC_DECODE_CB(func)                          \
255    .cb_type           = FF_CODEC_CB_TYPE_DECODE,         \
256    .cb.decode         = (func)
257#define FF_CODEC_DECODE_SUB_CB(func)                      \
258    .cb_type           = FF_CODEC_CB_TYPE_DECODE_SUB,     \
259    .cb.decode_sub     = (func)
260#define FF_CODEC_RECEIVE_FRAME_CB(func)                   \
261    .cb_type           = FF_CODEC_CB_TYPE_RECEIVE_FRAME,  \
262    .cb.receive_frame  = (func)
263#define FF_CODEC_ENCODE_CB(func)                          \
264    .cb_type           = FF_CODEC_CB_TYPE_ENCODE,         \
265    .cb.encode         = (func)
266#define FF_CODEC_ENCODE_SUB_CB(func)                      \
267    .cb_type           = FF_CODEC_CB_TYPE_ENCODE_SUB,     \
268    .cb.encode_sub     = (func)
269#define FF_CODEC_RECEIVE_PACKET_CB(func)                  \
270    .cb_type           = FF_CODEC_CB_TYPE_RECEIVE_PACKET, \
271    .cb.receive_packet = (func)
272
273static av_always_inline const FFCodec *ffcodec(const AVCodec *codec)
274{
275    return (const FFCodec*)codec;
276}
277
278#endif /* AVCODEC_CODEC_INTERNAL_H */
279