codec_internal.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef AVCODEC_CODEC_INTERNAL_H
  19. #define AVCODEC_CODEC_INTERNAL_H
  20. #include <stdint.h>
  21. #include "libavutil/attributes.h"
  22. #include "codec.h"
  23. #include "config.h"
  24. /**
  25. * The codec is not known to be init-threadsafe (i.e. it might be unsafe
  26. * to initialize this codec and another codec concurrently, typically because
  27. * the codec calls external APIs that are not known to be thread-safe).
  28. * Therefore calling the codec's init function needs to be guarded with a lock.
  29. */
  30. #define FF_CODEC_CAP_NOT_INIT_THREADSAFE (1 << 0)
  31. /**
  32. * The codec allows calling the close function for deallocation even if
  33. * the init function returned a failure. Without this capability flag, a
  34. * codec does such cleanup internally when returning failures from the
  35. * init function and does not expect the close function to be called at
  36. * all.
  37. */
  38. #define FF_CODEC_CAP_INIT_CLEANUP (1 << 1)
  39. /**
  40. * Decoders marked with FF_CODEC_CAP_SETS_PKT_DTS want to set
  41. * AVFrame.pkt_dts manually. If the flag is set, decode.c won't overwrite
  42. * this field. If it's unset, decode.c tries to guess the pkt_dts field
  43. * from the input AVPacket.
  44. */
  45. #define FF_CODEC_CAP_SETS_PKT_DTS (1 << 2)
  46. /**
  47. * The decoder extracts and fills its parameters even if the frame is
  48. * skipped due to the skip_frame setting.
  49. */
  50. #define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM (1 << 3)
  51. /**
  52. * The decoder sets the cropping fields in the output frames manually.
  53. * If this cap is set, the generic code will initialize output frame
  54. * dimensions to coded rather than display values.
  55. */
  56. #define FF_CODEC_CAP_EXPORTS_CROPPING (1 << 4)
  57. /**
  58. * Codec initializes slice-based threading with a main function
  59. */
  60. #define FF_CODEC_CAP_SLICE_THREAD_HAS_MF (1 << 5)
  61. /**
  62. * The decoder might make use of the ProgressFrame API.
  63. */
  64. #define FF_CODEC_CAP_USES_PROGRESSFRAMES (1 << 6)
  65. /**
  66. * Codec handles avctx->thread_count == 0 (auto) internally.
  67. */
  68. #define FF_CODEC_CAP_AUTO_THREADS (1 << 7)
  69. /**
  70. * Codec handles output frame properties internally instead of letting the
  71. * internal logic derive them from AVCodecInternal.last_pkt_props.
  72. */
  73. #define FF_CODEC_CAP_SETS_FRAME_PROPS (1 << 8)
  74. /**
  75. * Codec supports embedded ICC profiles (AV_FRAME_DATA_ICC_PROFILE).
  76. */
  77. #define FF_CODEC_CAP_ICC_PROFILES (1 << 9)
  78. /**
  79. * The encoder has AV_CODEC_CAP_DELAY set, but does not actually have delay - it
  80. * only wants to be flushed at the end to update some context variables (e.g.
  81. * 2pass stats) or produce a trailing packet. Besides that it immediately
  82. * produces exactly one output packet per each input frame, just as no-delay
  83. * encoders do.
  84. */
  85. #define FF_CODEC_CAP_EOF_FLUSH (1 << 10)
  86. /**
  87. * FFCodec.codec_tags termination value
  88. */
  89. #define FF_CODEC_TAGS_END -1
  90. typedef struct FFCodecDefault {
  91. const char *key;
  92. const char *value;
  93. } FFCodecDefault;
  94. struct AVCodecContext;
  95. struct AVSubtitle;
  96. struct AVPacket;
  97. enum FFCodecType {
  98. /* The codec is a decoder using the decode callback;
  99. * audio and video codecs only. */
  100. FF_CODEC_CB_TYPE_DECODE,
  101. /* The codec is a decoder using the decode_sub callback;
  102. * subtitle codecs only. */
  103. FF_CODEC_CB_TYPE_DECODE_SUB,
  104. /* The codec is a decoder using the receive_frame callback;
  105. * audio and video codecs only. */
  106. FF_CODEC_CB_TYPE_RECEIVE_FRAME,
  107. /* The codec is an encoder using the encode callback;
  108. * audio and video codecs only. */
  109. FF_CODEC_CB_TYPE_ENCODE,
  110. /* The codec is an encoder using the encode_sub callback;
  111. * subtitle codecs only. */
  112. FF_CODEC_CB_TYPE_ENCODE_SUB,
  113. /* The codec is an encoder using the receive_packet callback;
  114. * audio and video codecs only. */
  115. FF_CODEC_CB_TYPE_RECEIVE_PACKET,
  116. };
  117. typedef struct FFCodec {
  118. /**
  119. * The public AVCodec. See codec.h for it.
  120. */
  121. AVCodec p;
  122. /**
  123. * Internal codec capabilities FF_CODEC_CAP_*.
  124. */
  125. unsigned caps_internal:29;
  126. /**
  127. * This field determines the type of the codec (decoder/encoder)
  128. * and also the exact callback cb implemented by the codec.
  129. * cb_type uses enum FFCodecType values.
  130. */
  131. unsigned cb_type:3;
  132. int priv_data_size;
  133. /**
  134. * @name Frame-level threading support functions
  135. * @{
  136. */
  137. /**
  138. * Copy necessary context variables from a previous thread context to the current one.
  139. * If not defined, the next thread will start automatically; otherwise, the codec
  140. * must call ff_thread_finish_setup().
  141. *
  142. * dst and src will (rarely) point to the same context, in which case memcpy should be skipped.
  143. */
  144. int (*update_thread_context)(struct AVCodecContext *dst, const struct AVCodecContext *src);
  145. /**
  146. * Copy variables back to the user-facing context
  147. */
  148. int (*update_thread_context_for_user)(struct AVCodecContext *dst, const struct AVCodecContext *src);
  149. /** @} */
  150. /**
  151. * Private codec-specific defaults.
  152. */
  153. const FFCodecDefault *defaults;
  154. /**
  155. * Initialize codec static data, called from av_codec_iterate().
  156. *
  157. * This is not intended for time consuming operations as it is
  158. * run for every codec regardless of that codec being used.
  159. */
  160. void (*init_static_data)(struct FFCodec *codec);
  161. int (*init)(struct AVCodecContext *);
  162. union {
  163. /**
  164. * Decode to an AVFrame.
  165. * cb is in this state if cb_type is FF_CODEC_CB_TYPE_DECODE.
  166. *
  167. * @param avctx codec context
  168. * @param[out] frame AVFrame for output
  169. * @param[out] got_frame_ptr decoder sets to 0 or 1 to indicate that
  170. * a non-empty frame was returned in frame.
  171. * @param[in] avpkt AVPacket containing the data to be decoded
  172. * @return amount of bytes read from the packet on success,
  173. * negative error code on failure
  174. */
  175. int (*decode)(struct AVCodecContext *avctx, struct AVFrame *frame,
  176. int *got_frame_ptr, struct AVPacket *avpkt);
  177. /**
  178. * Decode subtitle data to an AVSubtitle.
  179. * cb is in this state if cb_type is FF_CODEC_CB_TYPE_DECODE_SUB.
  180. *
  181. * Apart from that this is like the decode callback.
  182. */
  183. int (*decode_sub)(struct AVCodecContext *avctx, struct AVSubtitle *sub,
  184. int *got_frame_ptr, const struct AVPacket *avpkt);
  185. /**
  186. * Decode API with decoupled packet/frame dataflow.
  187. * cb is in this state if cb_type is FF_CODEC_CB_TYPE_RECEIVE_FRAME.
  188. *
  189. * This function is called to get one output frame. It should call
  190. * ff_decode_get_packet() to obtain input data.
  191. */
  192. int (*receive_frame)(struct AVCodecContext *avctx, struct AVFrame *frame);
  193. /**
  194. * Encode data to an AVPacket.
  195. * cb is in this state if cb_type is FF_CODEC_CB_TYPE_ENCODE
  196. *
  197. * @param avctx codec context
  198. * @param[out] avpkt output AVPacket
  199. * @param[in] frame AVFrame containing the input to be encoded
  200. * @param[out] got_packet_ptr encoder sets to 0 or 1 to indicate that a
  201. * non-empty packet was returned in avpkt.
  202. * @return 0 on success, negative error code on failure
  203. */
  204. int (*encode)(struct AVCodecContext *avctx, struct AVPacket *avpkt,
  205. const struct AVFrame *frame, int *got_packet_ptr);
  206. /**
  207. * Encode subtitles to a raw buffer.
  208. * cb is in this state if cb_type is FF_CODEC_CB_TYPE_ENCODE_SUB.
  209. */
  210. int (*encode_sub)(struct AVCodecContext *avctx, uint8_t *buf,
  211. int buf_size, const struct AVSubtitle *sub);
  212. /**
  213. * Encode API with decoupled frame/packet dataflow.
  214. * cb is in this state if cb_type is FF_CODEC_CB_TYPE_RECEIVE_PACKET.
  215. *
  216. * This function is called to get one output packet.
  217. * It should call ff_encode_get_frame() to obtain input data.
  218. */
  219. int (*receive_packet)(struct AVCodecContext *avctx, struct AVPacket *avpkt);
  220. } cb;
  221. int (*close)(struct AVCodecContext *);
  222. /**
  223. * Flush buffers.
  224. * Will be called when seeking
  225. */
  226. void (*flush)(struct AVCodecContext *);
  227. /**
  228. * Decoding only, a comma-separated list of bitstream filters to apply to
  229. * packets before decoding.
  230. */
  231. const char *bsfs;
  232. /**
  233. * Array of pointers to hardware configurations supported by the codec,
  234. * or NULL if no hardware supported. The array is terminated by a NULL
  235. * pointer.
  236. *
  237. * The user can only access this field via avcodec_get_hw_config().
  238. */
  239. const struct AVCodecHWConfigInternal *const *hw_configs;
  240. /**
  241. * List of supported codec_tags, terminated by FF_CODEC_TAGS_END.
  242. */
  243. const uint32_t *codec_tags;
  244. } FFCodec;
  245. #if CONFIG_SMALL
  246. #define CODEC_LONG_NAME(str) .p.long_name = NULL
  247. #else
  248. #define CODEC_LONG_NAME(str) .p.long_name = str
  249. #endif
  250. #if HAVE_THREADS
  251. #define UPDATE_THREAD_CONTEXT(func) \
  252. .update_thread_context = (func)
  253. #define UPDATE_THREAD_CONTEXT_FOR_USER(func) \
  254. .update_thread_context_for_user = (func)
  255. #else
  256. #define UPDATE_THREAD_CONTEXT(func) \
  257. .update_thread_context = NULL
  258. #define UPDATE_THREAD_CONTEXT_FOR_USER(func) \
  259. .update_thread_context_for_user = NULL
  260. #endif
  261. #define FF_CODEC_DECODE_CB(func) \
  262. .cb_type = FF_CODEC_CB_TYPE_DECODE, \
  263. .cb.decode = (func)
  264. #define FF_CODEC_DECODE_SUB_CB(func) \
  265. .cb_type = FF_CODEC_CB_TYPE_DECODE_SUB, \
  266. .cb.decode_sub = (func)
  267. #define FF_CODEC_RECEIVE_FRAME_CB(func) \
  268. .cb_type = FF_CODEC_CB_TYPE_RECEIVE_FRAME, \
  269. .cb.receive_frame = (func)
  270. #define FF_CODEC_ENCODE_CB(func) \
  271. .cb_type = FF_CODEC_CB_TYPE_ENCODE, \
  272. .cb.encode = (func)
  273. #define FF_CODEC_ENCODE_SUB_CB(func) \
  274. .cb_type = FF_CODEC_CB_TYPE_ENCODE_SUB, \
  275. .cb.encode_sub = (func)
  276. #define FF_CODEC_RECEIVE_PACKET_CB(func) \
  277. .cb_type = FF_CODEC_CB_TYPE_RECEIVE_PACKET, \
  278. .cb.receive_packet = (func)
  279. static av_always_inline const FFCodec *ffcodec(const AVCodec *codec)
  280. {
  281. return (const FFCodec*)codec;
  282. }
  283. #endif /* AVCODEC_CODEC_INTERNAL_H */