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