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.

476 lines
17KB

  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 avctx->thread_count == 0 (auto).
  112. */
  113. #define AV_CODEC_CAP_AUTO_THREADS (1 << 15)
  114. /**
  115. * Audio encoder supports receiving a different number of samples in each call.
  116. */
  117. #define AV_CODEC_CAP_VARIABLE_FRAME_SIZE (1 << 16)
  118. /**
  119. * Decoder is not a preferred choice for probing.
  120. * This indicates that the decoder is not a good choice for probing.
  121. * It could for example be an expensive to spin up hardware decoder,
  122. * or it could simply not provide a lot of useful information about
  123. * the stream.
  124. * A decoder marked with this flag should only be used as last resort
  125. * choice for probing.
  126. */
  127. #define AV_CODEC_CAP_AVOID_PROBING (1 << 17)
  128. #if FF_API_UNUSED_CODEC_CAPS
  129. /**
  130. * Deprecated and unused. Use AVCodecDescriptor.props instead
  131. */
  132. #define AV_CODEC_CAP_INTRA_ONLY 0x40000000
  133. /**
  134. * Deprecated and unused. Use AVCodecDescriptor.props instead
  135. */
  136. #define AV_CODEC_CAP_LOSSLESS 0x80000000
  137. #endif
  138. /**
  139. * Codec is backed by a hardware implementation. Typically used to
  140. * identify a non-hwaccel hardware decoder. For information about hwaccels, use
  141. * avcodec_get_hw_config() instead.
  142. */
  143. #define AV_CODEC_CAP_HARDWARE (1 << 18)
  144. /**
  145. * Codec is potentially backed by a hardware implementation, but not
  146. * necessarily. This is used instead of AV_CODEC_CAP_HARDWARE, if the
  147. * implementation provides some sort of internal fallback.
  148. */
  149. #define AV_CODEC_CAP_HYBRID (1 << 19)
  150. /**
  151. * This codec takes the reordered_opaque field from input AVFrames
  152. * and returns it in the corresponding field in AVCodecContext after
  153. * encoding.
  154. */
  155. #define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE (1 << 20)
  156. /**
  157. * This encoder can be flushed using avcodec_flush_buffers(). If this flag is
  158. * not set, the encoder must be closed and reopened to ensure that no frames
  159. * remain pending.
  160. */
  161. #define AV_CODEC_CAP_ENCODER_FLUSH (1 << 21)
  162. /**
  163. * AVProfile.
  164. */
  165. typedef struct AVProfile {
  166. int profile;
  167. const char *name; ///< short name for the profile
  168. } AVProfile;
  169. typedef struct AVCodecDefault AVCodecDefault;
  170. struct AVCodecContext;
  171. struct AVSubtitle;
  172. struct AVPacket;
  173. /**
  174. * AVCodec.
  175. */
  176. typedef struct AVCodec {
  177. /**
  178. * Name of the codec implementation.
  179. * The name is globally unique among encoders and among decoders (but an
  180. * encoder and a decoder can share the same name).
  181. * This is the primary way to find a codec from the user perspective.
  182. */
  183. const char *name;
  184. /**
  185. * Descriptive name for the codec, meant to be more human readable than name.
  186. * You should use the NULL_IF_CONFIG_SMALL() macro to define it.
  187. */
  188. const char *long_name;
  189. enum AVMediaType type;
  190. enum AVCodecID id;
  191. /**
  192. * Codec capabilities.
  193. * see AV_CODEC_CAP_*
  194. */
  195. int capabilities;
  196. const AVRational *supported_framerates; ///< array of supported framerates, or NULL if any, array is terminated by {0,0}
  197. const enum AVPixelFormat *pix_fmts; ///< array of supported pixel formats, or NULL if unknown, array is terminated by -1
  198. const int *supported_samplerates; ///< array of supported audio samplerates, or NULL if unknown, array is terminated by 0
  199. const enum AVSampleFormat *sample_fmts; ///< array of supported sample formats, or NULL if unknown, array is terminated by -1
  200. const uint64_t *channel_layouts; ///< array of support channel layouts, or NULL if unknown. array is terminated by 0
  201. uint8_t max_lowres; ///< maximum value for lowres supported by the decoder
  202. const AVClass *priv_class; ///< AVClass for the private context
  203. const AVProfile *profiles; ///< array of recognized profiles, or NULL if unknown, array is terminated by {FF_PROFILE_UNKNOWN}
  204. /**
  205. * Group name of the codec implementation.
  206. * This is a short symbolic name of the wrapper backing this codec. A
  207. * wrapper uses some kind of external implementation for the codec, such
  208. * as an external library, or a codec implementation provided by the OS or
  209. * the hardware.
  210. * If this field is NULL, this is a builtin, libavcodec native codec.
  211. * If non-NULL, this will be the suffix in AVCodec.name in most cases
  212. * (usually AVCodec.name will be of the form "<codec_name>_<wrapper_name>").
  213. */
  214. const char *wrapper_name;
  215. /*****************************************************************
  216. * No fields below this line are part of the public API. They
  217. * may not be used outside of libavcodec and can be changed and
  218. * removed at will.
  219. * New public fields should be added right above.
  220. *****************************************************************
  221. */
  222. int priv_data_size;
  223. #if FF_API_NEXT
  224. struct AVCodec *next;
  225. #endif
  226. /**
  227. * @name Frame-level threading support functions
  228. * @{
  229. */
  230. /**
  231. * Copy necessary context variables from a previous thread context to the current one.
  232. * If not defined, the next thread will start automatically; otherwise, the codec
  233. * must call ff_thread_finish_setup().
  234. *
  235. * dst and src will (rarely) point to the same context, in which case memcpy should be skipped.
  236. */
  237. int (*update_thread_context)(struct AVCodecContext *dst, const struct AVCodecContext *src);
  238. /** @} */
  239. /**
  240. * Private codec-specific defaults.
  241. */
  242. const AVCodecDefault *defaults;
  243. /**
  244. * Initialize codec static data, called from av_codec_iterate().
  245. *
  246. * This is not intended for time consuming operations as it is
  247. * run for every codec regardless of that codec being used.
  248. */
  249. void (*init_static_data)(struct AVCodec *codec);
  250. int (*init)(struct AVCodecContext *);
  251. int (*encode_sub)(struct AVCodecContext *, uint8_t *buf, int buf_size,
  252. const struct AVSubtitle *sub);
  253. /**
  254. * Encode data to an AVPacket.
  255. *
  256. * @param avctx codec context
  257. * @param avpkt output AVPacket
  258. * @param[in] frame AVFrame containing the raw data to be encoded
  259. * @param[out] got_packet_ptr encoder sets to 0 or 1 to indicate that a
  260. * non-empty packet was returned in avpkt.
  261. * @return 0 on success, negative error code on failure
  262. */
  263. int (*encode2)(struct AVCodecContext *avctx, struct AVPacket *avpkt,
  264. const struct AVFrame *frame, int *got_packet_ptr);
  265. /**
  266. * Decode picture or subtitle data.
  267. *
  268. * @param avctx codec context
  269. * @param outdata codec type dependent output struct
  270. * @param[out] got_frame_ptr decoder sets to 0 or 1 to indicate that a
  271. * non-empty frame or subtitle was returned in
  272. * outdata.
  273. * @param[in] avpkt AVPacket containing the data to be decoded
  274. * @return amount of bytes read from the packet on success, negative error
  275. * code on failure
  276. */
  277. int (*decode)(struct AVCodecContext *avctx, void *outdata,
  278. int *got_frame_ptr, struct AVPacket *avpkt);
  279. int (*close)(struct AVCodecContext *);
  280. /**
  281. * Encode API with decoupled frame/packet dataflow. This function is called
  282. * to get one output packet. It should call ff_encode_get_frame() to obtain
  283. * input data.
  284. */
  285. int (*receive_packet)(struct AVCodecContext *avctx, struct AVPacket *avpkt);
  286. /**
  287. * Decode API with decoupled packet/frame dataflow. This function is called
  288. * to get one output frame. It should call ff_decode_get_packet() to obtain
  289. * input data.
  290. */
  291. int (*receive_frame)(struct AVCodecContext *avctx, struct AVFrame *frame);
  292. /**
  293. * Flush buffers.
  294. * Will be called when seeking
  295. */
  296. void (*flush)(struct AVCodecContext *);
  297. /**
  298. * Internal codec capabilities.
  299. * See FF_CODEC_CAP_* in internal.h
  300. */
  301. int caps_internal;
  302. /**
  303. * Decoding only, a comma-separated list of bitstream filters to apply to
  304. * packets before decoding.
  305. */
  306. const char *bsfs;
  307. /**
  308. * Array of pointers to hardware configurations supported by the codec,
  309. * or NULL if no hardware supported. The array is terminated by a NULL
  310. * pointer.
  311. *
  312. * The user can only access this field via avcodec_get_hw_config().
  313. */
  314. const struct AVCodecHWConfigInternal *const *hw_configs;
  315. /**
  316. * List of supported codec_tags, terminated by FF_CODEC_TAGS_END.
  317. */
  318. const uint32_t *codec_tags;
  319. } AVCodec;
  320. /**
  321. * Iterate over all registered codecs.
  322. *
  323. * @param opaque a pointer where libavcodec will store the iteration state. Must
  324. * point to NULL to start the iteration.
  325. *
  326. * @return the next registered codec or NULL when the iteration is
  327. * finished
  328. */
  329. const AVCodec *av_codec_iterate(void **opaque);
  330. /**
  331. * Find a registered decoder with a matching codec ID.
  332. *
  333. * @param id AVCodecID of the requested decoder
  334. * @return A decoder if one was found, NULL otherwise.
  335. */
  336. AVCodec *avcodec_find_decoder(enum AVCodecID id);
  337. /**
  338. * Find a registered decoder with the specified name.
  339. *
  340. * @param name name of the requested decoder
  341. * @return A decoder if one was found, NULL otherwise.
  342. */
  343. AVCodec *avcodec_find_decoder_by_name(const char *name);
  344. /**
  345. * Find a registered encoder with a matching codec ID.
  346. *
  347. * @param id AVCodecID of the requested encoder
  348. * @return An encoder if one was found, NULL otherwise.
  349. */
  350. AVCodec *avcodec_find_encoder(enum AVCodecID id);
  351. /**
  352. * Find a registered encoder with the specified name.
  353. *
  354. * @param name name of the requested encoder
  355. * @return An encoder if one was found, NULL otherwise.
  356. */
  357. AVCodec *avcodec_find_encoder_by_name(const char *name);
  358. /**
  359. * @return a non-zero number if codec is an encoder, zero otherwise
  360. */
  361. int av_codec_is_encoder(const AVCodec *codec);
  362. /**
  363. * @return a non-zero number if codec is a decoder, zero otherwise
  364. */
  365. int av_codec_is_decoder(const AVCodec *codec);
  366. enum {
  367. /**
  368. * The codec supports this format via the hw_device_ctx interface.
  369. *
  370. * When selecting this format, AVCodecContext.hw_device_ctx should
  371. * have been set to a device of the specified type before calling
  372. * avcodec_open2().
  373. */
  374. AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX = 0x01,
  375. /**
  376. * The codec supports this format via the hw_frames_ctx interface.
  377. *
  378. * When selecting this format for a decoder,
  379. * AVCodecContext.hw_frames_ctx should be set to a suitable frames
  380. * context inside the get_format() callback. The frames context
  381. * must have been created on a device of the specified type.
  382. *
  383. * When selecting this format for an encoder,
  384. * AVCodecContext.hw_frames_ctx should be set to the context which
  385. * will be used for the input frames before calling avcodec_open2().
  386. */
  387. AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX = 0x02,
  388. /**
  389. * The codec supports this format by some internal method.
  390. *
  391. * This format can be selected without any additional configuration -
  392. * no device or frames context is required.
  393. */
  394. AV_CODEC_HW_CONFIG_METHOD_INTERNAL = 0x04,
  395. /**
  396. * The codec supports this format by some ad-hoc method.
  397. *
  398. * Additional settings and/or function calls are required. See the
  399. * codec-specific documentation for details. (Methods requiring
  400. * this sort of configuration are deprecated and others should be
  401. * used in preference.)
  402. */
  403. AV_CODEC_HW_CONFIG_METHOD_AD_HOC = 0x08,
  404. };
  405. typedef struct AVCodecHWConfig {
  406. /**
  407. * For decoders, a hardware pixel format which that decoder may be
  408. * able to decode to if suitable hardware is available.
  409. *
  410. * For encoders, a pixel format which the encoder may be able to
  411. * accept. If set to AV_PIX_FMT_NONE, this applies to all pixel
  412. * formats supported by the codec.
  413. */
  414. enum AVPixelFormat pix_fmt;
  415. /**
  416. * Bit set of AV_CODEC_HW_CONFIG_METHOD_* flags, describing the possible
  417. * setup methods which can be used with this configuration.
  418. */
  419. int methods;
  420. /**
  421. * The device type associated with the configuration.
  422. *
  423. * Must be set for AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX and
  424. * AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX, otherwise unused.
  425. */
  426. enum AVHWDeviceType device_type;
  427. } AVCodecHWConfig;
  428. /**
  429. * Retrieve supported hardware configurations for a codec.
  430. *
  431. * Values of index from zero to some maximum return the indexed configuration
  432. * descriptor; all other values return NULL. If the codec does not support
  433. * any hardware configurations then it will always return NULL.
  434. */
  435. const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *codec, int index);
  436. /**
  437. * @}
  438. */
  439. #endif /* AVCODEC_CODEC_H */