codec.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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_major.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. #if FF_API_FLAG_TRUNCATED
  48. /**
  49. * @deprecated Use parsers to always send proper frames.
  50. */
  51. #define AV_CODEC_CAP_TRUNCATED (1 << 3)
  52. #endif
  53. /**
  54. * Encoder or decoder requires flushing with NULL input at the end in order to
  55. * give the complete and correct output.
  56. *
  57. * NOTE: If this flag is not set, the codec is guaranteed to never be fed with
  58. * with NULL data. The user can still send NULL data to the public encode
  59. * or decode function, but libavcodec will not pass it along to the codec
  60. * unless this flag is set.
  61. *
  62. * Decoders:
  63. * The decoder has a non-zero delay and needs to be fed with avpkt->data=NULL,
  64. * avpkt->size=0 at the end to get the delayed data until the decoder no longer
  65. * returns frames.
  66. *
  67. * Encoders:
  68. * The encoder needs to be fed with NULL data at the end of encoding until the
  69. * encoder no longer returns data.
  70. *
  71. * NOTE: For encoders implementing the AVCodec.encode2() function, setting this
  72. * flag also means that the encoder must set the pts and duration for
  73. * each output packet. If this flag is not set, the pts and duration will
  74. * be determined by libavcodec from the input frame.
  75. */
  76. #define AV_CODEC_CAP_DELAY (1 << 5)
  77. /**
  78. * Codec can be fed a final frame with a smaller size.
  79. * This can be used to prevent truncation of the last audio samples.
  80. */
  81. #define AV_CODEC_CAP_SMALL_LAST_FRAME (1 << 6)
  82. /**
  83. * Codec can output multiple frames per AVPacket
  84. * Normally demuxers return one frame at a time, demuxers which do not do
  85. * are connected to a parser to split what they return into proper frames.
  86. * This flag is reserved to the very rare category of codecs which have a
  87. * bitstream that cannot be split into frames without timeconsuming
  88. * operations like full decoding. Demuxers carrying such bitstreams thus
  89. * may return multiple frames in a packet. This has many disadvantages like
  90. * prohibiting stream copy in many cases thus it should only be considered
  91. * as a last resort.
  92. */
  93. #define AV_CODEC_CAP_SUBFRAMES (1 << 8)
  94. /**
  95. * Codec is experimental and is thus avoided in favor of non experimental
  96. * encoders
  97. */
  98. #define AV_CODEC_CAP_EXPERIMENTAL (1 << 9)
  99. /**
  100. * Codec should fill in channel configuration and samplerate instead of container
  101. */
  102. #define AV_CODEC_CAP_CHANNEL_CONF (1 << 10)
  103. /**
  104. * Codec supports frame-level multithreading.
  105. */
  106. #define AV_CODEC_CAP_FRAME_THREADS (1 << 12)
  107. /**
  108. * Codec supports slice-based (or partition-based) multithreading.
  109. */
  110. #define AV_CODEC_CAP_SLICE_THREADS (1 << 13)
  111. /**
  112. * Codec supports changed parameters at any point.
  113. */
  114. #define AV_CODEC_CAP_PARAM_CHANGE (1 << 14)
  115. /**
  116. * Codec supports multithreading through a method other than slice- or
  117. * frame-level multithreading. Typically this marks wrappers around
  118. * multithreading-capable external libraries.
  119. */
  120. #define AV_CODEC_CAP_OTHER_THREADS (1 << 15)
  121. #if FF_API_AUTO_THREADS
  122. #define AV_CODEC_CAP_AUTO_THREADS AV_CODEC_CAP_OTHER_THREADS
  123. #endif
  124. /**
  125. * Audio encoder supports receiving a different number of samples in each call.
  126. */
  127. #define AV_CODEC_CAP_VARIABLE_FRAME_SIZE (1 << 16)
  128. /**
  129. * Decoder is not a preferred choice for probing.
  130. * This indicates that the decoder is not a good choice for probing.
  131. * It could for example be an expensive to spin up hardware decoder,
  132. * or it could simply not provide a lot of useful information about
  133. * the stream.
  134. * A decoder marked with this flag should only be used as last resort
  135. * choice for probing.
  136. */
  137. #define AV_CODEC_CAP_AVOID_PROBING (1 << 17)
  138. #if FF_API_UNUSED_CODEC_CAPS
  139. /**
  140. * Deprecated and unused. Use AVCodecDescriptor.props instead
  141. */
  142. #define AV_CODEC_CAP_INTRA_ONLY 0x40000000
  143. /**
  144. * Deprecated and unused. Use AVCodecDescriptor.props instead
  145. */
  146. #define AV_CODEC_CAP_LOSSLESS 0x80000000
  147. #endif
  148. /**
  149. * Codec is backed by a hardware implementation. Typically used to
  150. * identify a non-hwaccel hardware decoder. For information about hwaccels, use
  151. * avcodec_get_hw_config() instead.
  152. */
  153. #define AV_CODEC_CAP_HARDWARE (1 << 18)
  154. /**
  155. * Codec is potentially backed by a hardware implementation, but not
  156. * necessarily. This is used instead of AV_CODEC_CAP_HARDWARE, if the
  157. * implementation provides some sort of internal fallback.
  158. */
  159. #define AV_CODEC_CAP_HYBRID (1 << 19)
  160. /**
  161. * This codec takes the reordered_opaque field from input AVFrames
  162. * and returns it in the corresponding field in AVCodecContext after
  163. * encoding.
  164. */
  165. #define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE (1 << 20)
  166. /**
  167. * This encoder can be flushed using avcodec_flush_buffers(). If this flag is
  168. * not set, the encoder must be closed and reopened to ensure that no frames
  169. * remain pending.
  170. */
  171. #define AV_CODEC_CAP_ENCODER_FLUSH (1 << 21)
  172. /**
  173. * AVProfile.
  174. */
  175. typedef struct AVProfile {
  176. int profile;
  177. const char *name; ///< short name for the profile
  178. } AVProfile;
  179. /**
  180. * AVCodec.
  181. */
  182. typedef struct AVCodec {
  183. /**
  184. * 编解码器实现的名称。
  185. * 该名称在编码器和解码器之间是全局唯一的(但编码器和解码器可以共享相同的名称)。
  186. * 这是从用户角度查找编解码器的主要方法。
  187. */
  188. const char *name;
  189. /**
  190. * 编解码器的描述性名称,意味着比名称更易于阅读。
  191. * 您应该使用NULL_IF_CONFIG_SMALL()宏来定义它。
  192. */
  193. const char *long_name;
  194. enum AVMediaType type;
  195. enum AVCodecID id;
  196. /**
  197. * 编解码器功能。
  198. * see AV_CODEC_CAP_*
  199. */
  200. int capabilities;
  201. uint8_t max_lowres; ///< maximum value for lowres supported by the decoder
  202. const AVRational *supported_framerates; ///< array of supported framerates, or NULL if any, array is terminated by {0,0}
  203. const enum AVPixelFormat *pix_fmts; ///< array of supported pixel formats, or NULL if unknown, array is terminated by -1
  204. const int *supported_samplerates; ///< array of supported audio samplerates, or NULL if unknown, array is terminated by 0
  205. const enum AVSampleFormat *sample_fmts; ///< array of supported sample formats, or NULL if unknown, array is terminated by -1
  206. #if FF_API_OLD_CHANNEL_LAYOUT
  207. /**
  208. * @deprecated use ch_layouts instead
  209. */
  210. attribute_deprecated
  211. const uint64_t *channel_layouts; ///< array of support channel layouts, or NULL if unknown. array is terminated by 0
  212. #endif
  213. const AVClass *priv_class; ///< AVClass for the private context
  214. const AVProfile *profiles; ///< array of recognized profiles, or NULL if unknown, array is terminated by {FF_PROFILE_UNKNOWN}
  215. /**
  216. * Group name of the codec implementation.
  217. * This is a short symbolic name of the wrapper backing this codec. A
  218. * wrapper uses some kind of external implementation for the codec, such
  219. * as an external library, or a codec implementation provided by the OS or
  220. * the hardware.
  221. * If this field is NULL, this is a builtin, libavcodec native codec.
  222. * If non-NULL, this will be the suffix in AVCodec.name in most cases
  223. * (usually AVCodec.name will be of the form "<codec_name>_<wrapper_name>").
  224. */
  225. const char *wrapper_name;
  226. /**
  227. * Array of supported channel layouts, terminated with a zeroed layout.
  228. */
  229. const AVChannelLayout *ch_layouts;
  230. } AVCodec;
  231. /**
  232. * Iterate over all registered codecs.
  233. *
  234. * @param opaque a pointer where libavcodec will store the iteration state. Must
  235. * point to NULL to start the iteration.
  236. *
  237. * @return the next registered codec or NULL when the iteration is
  238. * finished
  239. */
  240. const AVCodec *av_codec_iterate(void **opaque);
  241. /**
  242. * 查找具有匹配编解码器ID的注册解码器。
  243. *
  244. * @param id 请求解码器的AVCodecID
  245. * @return A 解码器(如果找到),否则为NULL。
  246. */
  247. const AVCodec *avcodec_find_decoder(enum AVCodecID id);
  248. /**
  249. * Find a registered decoder with the specified name.
  250. *
  251. * @param name name of the requested decoder
  252. * @return A decoder if one was found, NULL otherwise.
  253. */
  254. const AVCodec *avcodec_find_decoder_by_name(const char *name);
  255. /**
  256. * Find a registered encoder with a matching codec ID.
  257. *
  258. * @param id AVCodecID of the requested encoder
  259. * @return An encoder if one was found, NULL otherwise.
  260. */
  261. const AVCodec *avcodec_find_encoder(enum AVCodecID id);
  262. /**
  263. * Find a registered encoder with the specified name.
  264. *
  265. * @param name name of the requested encoder
  266. * @return An encoder if one was found, NULL otherwise.
  267. */
  268. const AVCodec *avcodec_find_encoder_by_name(const char *name);
  269. /**
  270. * @return a non-zero number if codec is an encoder, zero otherwise
  271. */
  272. int av_codec_is_encoder(const AVCodec *codec);
  273. /**
  274. * @return a non-zero number if codec is a decoder, zero otherwise
  275. */
  276. int av_codec_is_decoder(const AVCodec *codec);
  277. /**
  278. * Return a name for the specified profile, if available.
  279. *
  280. * @param codec the codec that is searched for the given profile
  281. * @param profile the profile value for which a name is requested
  282. * @return A name for the profile if found, NULL otherwise.
  283. */
  284. const char *av_get_profile_name(const AVCodec *codec, int profile);
  285. enum {
  286. /**
  287. * 编解码器通过hw_device_ctx接口支持此格式。
  288. *
  289. * 选择此格式时,AVCodecContext。在调用avcodec_open2()之前,应将hw_device_ctx设置为指定类型的设备。
  290. */
  291. AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX = 0x01,
  292. /**
  293. * 编解码器通过hw_frames_ctx接口支持这种格式。
  294. *
  295. * 当为解码器选择这种格式时,AVCodecContext。Hw_frames_ctx应该在get_format()回调函数中设置一个合适的帧上下文。
  296. * 帧上下文必须已经在指定类型的设备上创建。
  297. *
  298. * 为编码器选择此格式时,AVCodecContext。在调用avcodec_open2()之前,应将hw_frames_ctx设置为将用于输入帧的上下文。
  299. */
  300. AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX = 0x02,
  301. /**
  302. * 编解码器通过一些内部方法支持这种格式。
  303. *
  304. * 这种格式的选择不需要任何额外的配置—不需要设备或帧上下文。
  305. */
  306. AV_CODEC_HW_CONFIG_METHOD_INTERNAL = 0x04,
  307. /**
  308. * 编解码器通过一些特殊方法支持这种格式。
  309. *
  310. * 需要额外的设置和/或函数调用。
  311. * 有关详细信息,请参阅特定于编解码器的文档。(不推荐使用要求这种配置的方法,应该优先使用其他方法。)
  312. */
  313. AV_CODEC_HW_CONFIG_METHOD_AD_HOC = 0x08,
  314. };
  315. typedef struct AVCodecHWConfig {
  316. /**
  317. * 对于解码器而言,一种硬件像素格式,如果有合适的硬件可用,解码器可以解码到该格式。
  318. *
  319. * 对于编码器,编码器可以接受的像素格式。
  320. * 如果设置为AV_PIX_FMT_NONE,则这适用于编解码器支持的所有像素格式。
  321. */
  322. enum AVPixelFormat pix_fmt;
  323. /**
  324. * AV_CODEC_HW_CONFIG_METHOD_*标志的位集,描述可用于此配置的可能设置方法。
  325. */
  326. int methods;
  327. /**
  328. * 与配置相关联的设备类型。
  329. *
  330. * 必须为AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX和AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX设置,否则不使用。
  331. */
  332. enum AVHWDeviceType device_type;
  333. } AVCodecHWConfig;
  334. /**
  335. * 检索编解码器支持的硬件配置。
  336. *
  337. * 索引值从零到某个最大值返回索引的配置描述符;
  338. * 所有其他值都返回NULL。如果编解码器不支持任何硬件配置,则它将始终返回NULL。
  339. */
  340. const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *codec, int index);
  341. /**
  342. * @}
  343. */
  344. #endif /* AVCODEC_CODEC_H */