codec.h 17 KB

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