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