You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

461 lines
16KB

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