FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/codec_internal.h
Date: 2022-10-02 18:56:10
Exec Total Coverage
Lines: 2 2 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 "codec.h"
26 #include "config.h"
27
28 /**
29 * The codec is not known to be init-threadsafe (i.e. it might be unsafe
30 * to initialize this codec and another codec concurrently, typically because
31 * the codec calls external APIs that are not known to be thread-safe).
32 * Therefore calling the codec's init function needs to be guarded with a lock.
33 */
34 #define FF_CODEC_CAP_NOT_INIT_THREADSAFE (1 << 0)
35 /**
36 * The codec allows calling the close function for deallocation even if
37 * the init function returned a failure. Without this capability flag, a
38 * codec does such cleanup internally when returning failures from the
39 * init function and does not expect the close function to be called at
40 * all.
41 */
42 #define FF_CODEC_CAP_INIT_CLEANUP (1 << 1)
43 /**
44 * Decoders marked with FF_CODEC_CAP_SETS_PKT_DTS want to set
45 * AVFrame.pkt_dts manually. If the flag is set, decode.c won't overwrite
46 * this field. If it's unset, decode.c tries to guess the pkt_dts field
47 * from the input AVPacket.
48 */
49 #define FF_CODEC_CAP_SETS_PKT_DTS (1 << 2)
50 /**
51 * The decoder extracts and fills its parameters even if the frame is
52 * skipped due to the skip_frame setting.
53 */
54 #define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM (1 << 3)
55 /**
56 * The decoder sets the cropping fields in the output frames manually.
57 * If this cap is set, the generic code will initialize output frame
58 * dimensions to coded rather than display values.
59 */
60 #define FF_CODEC_CAP_EXPORTS_CROPPING (1 << 4)
61 /**
62 * Codec initializes slice-based threading with a main function
63 */
64 #define FF_CODEC_CAP_SLICE_THREAD_HAS_MF (1 << 5)
65 /*
66 * The codec supports frame threading and has inter-frame dependencies, so it
67 * uses ff_thread_report/await_progress().
68 */
69 #define FF_CODEC_CAP_ALLOCATE_PROGRESS (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 /**
85 * FFCodec.codec_tags termination value
86 */
87 #define FF_CODEC_TAGS_END -1
88
89 typedef struct FFCodecDefault {
90 const char *key;
91 const char *value;
92 } FFCodecDefault;
93
94 struct AVCodecContext;
95 struct AVSubtitle;
96 struct AVPacket;
97
98 enum FFCodecType {
99 /* The codec is a decoder using the decode callback;
100 * audio and video codecs only. */
101 FF_CODEC_CB_TYPE_DECODE,
102 /* The codec is a decoder using the decode_sub callback;
103 * subtitle codecs only. */
104 FF_CODEC_CB_TYPE_DECODE_SUB,
105 /* The codec is a decoder using the receive_frame callback;
106 * audio and video codecs only. */
107 FF_CODEC_CB_TYPE_RECEIVE_FRAME,
108 /* The codec is an encoder using the encode callback;
109 * audio and video codecs only. */
110 FF_CODEC_CB_TYPE_ENCODE,
111 /* The codec is an encoder using the encode_sub callback;
112 * subtitle codecs only. */
113 FF_CODEC_CB_TYPE_ENCODE_SUB,
114 /* The codec is an encoder using the receive_packet callback;
115 * audio and video codecs only. */
116 FF_CODEC_CB_TYPE_RECEIVE_PACKET,
117 };
118
119 typedef struct FFCodec {
120 /**
121 * The public AVCodec. See codec.h for it.
122 */
123 AVCodec p;
124
125 /**
126 * Internal codec capabilities FF_CODEC_CAP_*.
127 */
128 unsigned caps_internal:29;
129
130 /**
131 * This field determines the type of the codec (decoder/encoder)
132 * and also the exact callback cb implemented by the codec.
133 * cb_type uses enum FFCodecType values.
134 */
135 unsigned cb_type:3;
136
137 int priv_data_size;
138 /**
139 * @name Frame-level threading support functions
140 * @{
141 */
142 /**
143 * Copy necessary context variables from a previous thread context to the current one.
144 * If not defined, the next thread will start automatically; otherwise, the codec
145 * must call ff_thread_finish_setup().
146 *
147 * dst and src will (rarely) point to the same context, in which case memcpy should be skipped.
148 */
149 int (*update_thread_context)(struct AVCodecContext *dst, const struct AVCodecContext *src);
150
151 /**
152 * Copy variables back to the user-facing context
153 */
154 int (*update_thread_context_for_user)(struct AVCodecContext *dst, const struct AVCodecContext *src);
155 /** @} */
156
157 /**
158 * Private codec-specific defaults.
159 */
160 const FFCodecDefault *defaults;
161
162 /**
163 * Initialize codec static data, called from av_codec_iterate().
164 *
165 * This is not intended for time consuming operations as it is
166 * run for every codec regardless of that codec being used.
167 */
168 void (*init_static_data)(struct FFCodec *codec);
169
170 int (*init)(struct AVCodecContext *);
171
172 union {
173 /**
174 * Decode to an AVFrame.
175 * cb is in this state if cb_type is FF_CODEC_CB_TYPE_DECODE.
176 *
177 * @param avctx codec context
178 * @param[out] frame AVFrame for output
179 * @param[out] got_frame_ptr decoder sets to 0 or 1 to indicate that
180 * a non-empty frame was returned in frame.
181 * @param[in] avpkt AVPacket containing the data to be decoded
182 * @return amount of bytes read from the packet on success,
183 * negative error code on failure
184 */
185 int (*decode)(struct AVCodecContext *avctx, struct AVFrame *frame,
186 int *got_frame_ptr, struct AVPacket *avpkt);
187 /**
188 * Decode subtitle data to an AVSubtitle.
189 * cb is in this state if cb_type is FF_CODEC_CB_TYPE_DECODE_SUB.
190 *
191 * Apart from that this is like the decode callback.
192 */
193 int (*decode_sub)(struct AVCodecContext *avctx, struct AVSubtitle *sub,
194 int *got_frame_ptr, const struct AVPacket *avpkt);
195 /**
196 * Decode API with decoupled packet/frame dataflow.
197 * cb is in this state if cb_type is FF_CODEC_CB_TYPE_RECEIVE_FRAME.
198 *
199 * This function is called to get one output frame. It should call
200 * ff_decode_get_packet() to obtain input data.
201 */
202 int (*receive_frame)(struct AVCodecContext *avctx, struct AVFrame *frame);
203 /**
204 * Encode data to an AVPacket.
205 * cb is in this state if cb_type is FF_CODEC_CB_TYPE_ENCODE
206 *
207 * @param avctx codec context
208 * @param[out] avpkt output AVPacket
209 * @param[in] frame AVFrame containing the input to be encoded
210 * @param[out] got_packet_ptr encoder sets to 0 or 1 to indicate that a
211 * non-empty packet was returned in avpkt.
212 * @return 0 on success, negative error code on failure
213 */
214 int (*encode)(struct AVCodecContext *avctx, struct AVPacket *avpkt,
215 const struct AVFrame *frame, int *got_packet_ptr);
216 /**
217 * Encode subtitles to a raw buffer.
218 * cb is in this state if cb_type is FF_CODEC_CB_TYPE_ENCODE_SUB.
219 */
220 int (*encode_sub)(struct AVCodecContext *avctx, uint8_t *buf,
221 int buf_size, const struct AVSubtitle *sub);
222 /**
223 * Encode API with decoupled frame/packet dataflow.
224 * cb is in this state if cb_type is FF_CODEC_CB_TYPE_RECEIVE_PACKET.
225 *
226 * This function is called to get one output packet.
227 * It should call ff_encode_get_frame() to obtain input data.
228 */
229 int (*receive_packet)(struct AVCodecContext *avctx, struct AVPacket *avpkt);
230 } cb;
231
232 int (*close)(struct AVCodecContext *);
233
234 /**
235 * Flush buffers.
236 * Will be called when seeking
237 */
238 void (*flush)(struct AVCodecContext *);
239
240 /**
241 * Decoding only, a comma-separated list of bitstream filters to apply to
242 * packets before decoding.
243 */
244 const char *bsfs;
245
246 /**
247 * Array of pointers to hardware configurations supported by the codec,
248 * or NULL if no hardware supported. The array is terminated by a NULL
249 * pointer.
250 *
251 * The user can only access this field via avcodec_get_hw_config().
252 */
253 const struct AVCodecHWConfigInternal *const *hw_configs;
254
255 /**
256 * List of supported codec_tags, terminated by FF_CODEC_TAGS_END.
257 */
258 const uint32_t *codec_tags;
259 } FFCodec;
260
261 #if CONFIG_SMALL
262 #define CODEC_LONG_NAME(str) .p.long_name = NULL
263 #else
264 #define CODEC_LONG_NAME(str) .p.long_name = str
265 #endif
266
267 #if HAVE_THREADS
268 #define UPDATE_THREAD_CONTEXT(func) \
269 .update_thread_context = (func)
270 #define UPDATE_THREAD_CONTEXT_FOR_USER(func) \
271 .update_thread_context_for_user = (func)
272 #else
273 #define UPDATE_THREAD_CONTEXT(func) \
274 .update_thread_context = NULL
275 #define UPDATE_THREAD_CONTEXT_FOR_USER(func) \
276 .update_thread_context_for_user = NULL
277 #endif
278
279 #if FF_API_OLD_CHANNEL_LAYOUT
280 #define CODEC_OLD_CHANNEL_LAYOUTS(...) CODEC_OLD_CHANNEL_LAYOUTS_ARRAY(((const uint64_t[]) { __VA_ARGS__, 0 }))
281 #if defined(__clang__)
282 #define CODEC_OLD_CHANNEL_LAYOUTS_ARRAY(array) \
283 FF_DISABLE_DEPRECATION_WARNINGS \
284 .p.channel_layouts = (array), \
285 FF_ENABLE_DEPRECATION_WARNINGS
286 #else
287 #define CODEC_OLD_CHANNEL_LAYOUTS_ARRAY(array) .p.channel_layouts = (array),
288 #endif
289 #else
290 /* This is only provided to allow to test disabling FF_API_OLD_CHANNEL_LAYOUT
291 * without removing all the FF_API_OLD_CHANNEL_LAYOUT codeblocks.
292 * It is of course still expected to be removed when FF_API_OLD_CHANNEL_LAYOUT
293 * will be finally removed (along with all usages of these macros). */
294 #define CODEC_OLD_CHANNEL_LAYOUTS(...)
295 #define CODEC_OLD_CHANNEL_LAYOUTS_ARRAY(array)
296 #endif
297
298 #define FF_CODEC_DECODE_CB(func) \
299 .cb_type = FF_CODEC_CB_TYPE_DECODE, \
300 .cb.decode = (func)
301 #define FF_CODEC_DECODE_SUB_CB(func) \
302 .cb_type = FF_CODEC_CB_TYPE_DECODE_SUB, \
303 .cb.decode_sub = (func)
304 #define FF_CODEC_RECEIVE_FRAME_CB(func) \
305 .cb_type = FF_CODEC_CB_TYPE_RECEIVE_FRAME, \
306 .cb.receive_frame = (func)
307 #define FF_CODEC_ENCODE_CB(func) \
308 .cb_type = FF_CODEC_CB_TYPE_ENCODE, \
309 .cb.encode = (func)
310 #define FF_CODEC_ENCODE_SUB_CB(func) \
311 .cb_type = FF_CODEC_CB_TYPE_ENCODE_SUB, \
312 .cb.encode_sub = (func)
313 #define FF_CODEC_RECEIVE_PACKET_CB(func) \
314 .cb_type = FF_CODEC_CB_TYPE_RECEIVE_PACKET, \
315 .cb.receive_packet = (func)
316
317 26035810 static av_always_inline const FFCodec *ffcodec(const AVCodec *codec)
318 {
319 26035810 return (const FFCodec*)codec;
320 }
321
322 #endif /* AVCODEC_CODEC_INTERNAL_H */
323