codec_internal.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 codec supports frame threading and has inter-frame dependencies, so it
  63. * uses ff_thread_report/await_progress().
  64. */
  65. #define FF_CODEC_CAP_ALLOCATE_PROGRESS (1 << 6)
  66. /**
  67. * Codec handles avctx->thread_count == 0 (auto) internally.
  68. */
  69. #define FF_CODEC_CAP_AUTO_THREADS (1 << 7)
  70. /**
  71. * Codec handles output frame properties internally instead of letting the
  72. * internal logic derive them from AVCodecInternal.last_pkt_props.
  73. */
  74. #define FF_CODEC_CAP_SETS_FRAME_PROPS (1 << 8)
  75. /**
  76. * Codec supports embedded ICC profiles (AV_FRAME_DATA_ICC_PROFILE).
  77. */
  78. #define FF_CODEC_CAP_ICC_PROFILES (1 << 9)
  79. /**
  80. * FFCodec.codec_tags termination value
  81. */
  82. #define FF_CODEC_TAGS_END -1
  83. typedef struct FFCodecDefault {
  84. const char *key;
  85. const char *value;
  86. } FFCodecDefault;
  87. struct AVCodecContext;
  88. struct AVSubtitle;
  89. struct AVPacket;
  90. enum FFCodecType {
  91. /* The codec is a decoder using the decode callback;
  92. * audio and video codecs only. */
  93. FF_CODEC_CB_TYPE_DECODE,
  94. /* The codec is a decoder using the decode_sub callback;
  95. * subtitle codecs only. */
  96. FF_CODEC_CB_TYPE_DECODE_SUB,
  97. /* The codec is a decoder using the receive_frame callback;
  98. * audio and video codecs only. */
  99. FF_CODEC_CB_TYPE_RECEIVE_FRAME,
  100. /* The codec is an encoder using the encode callback;
  101. * audio and video codecs only. */
  102. FF_CODEC_CB_TYPE_ENCODE,
  103. /* The codec is an encoder using the encode_sub callback;
  104. * subtitle codecs only. */
  105. FF_CODEC_CB_TYPE_ENCODE_SUB,
  106. /* The codec is an encoder using the receive_packet callback;
  107. * audio and video codecs only. */
  108. FF_CODEC_CB_TYPE_RECEIVE_PACKET,
  109. };
  110. typedef struct FFCodec {
  111. /**
  112. * The public AVCodec. See codec.h for it.
  113. */
  114. AVCodec p;
  115. /**
  116. * Internal codec capabilities FF_CODEC_CAP_*.
  117. */
  118. unsigned caps_internal:29;
  119. /**
  120. * This field determines the type of the codec (decoder/encoder)
  121. * and also the exact callback cb implemented by the codec.
  122. * cb_type uses enum FFCodecType values.
  123. */
  124. unsigned cb_type:3;
  125. int priv_data_size;
  126. /**
  127. * @name Frame-level threading support functions
  128. * @{
  129. */
  130. /**
  131. * Copy necessary context variables from a previous thread context to the current one.
  132. * If not defined, the next thread will start automatically; otherwise, the codec
  133. * must call ff_thread_finish_setup().
  134. *
  135. * dst and src will (rarely) point to the same context, in which case memcpy should be skipped.
  136. */
  137. int (*update_thread_context)(struct AVCodecContext *dst, const struct AVCodecContext *src);
  138. /**
  139. * Copy variables back to the user-facing context
  140. */
  141. int (*update_thread_context_for_user)(struct AVCodecContext *dst, const struct AVCodecContext *src);
  142. /** @} */
  143. /**
  144. * Private codec-specific defaults.
  145. */
  146. const FFCodecDefault *defaults;
  147. /**
  148. * Initialize codec static data, called from av_codec_iterate().
  149. *
  150. * This is not intended for time consuming operations as it is
  151. * run for every codec regardless of that codec being used.
  152. */
  153. void (*init_static_data)(struct FFCodec *codec);
  154. int (*init)(struct AVCodecContext *);
  155. union {
  156. /**
  157. * Decode to an AVFrame.
  158. * cb is in this state if cb_type is FF_CODEC_CB_TYPE_DECODE.
  159. *
  160. * @param avctx codec context
  161. * @param[out] frame AVFrame for output
  162. * @param[out] got_frame_ptr decoder sets to 0 or 1 to indicate that
  163. * a non-empty frame was returned in frame.
  164. * @param[in] avpkt AVPacket containing the data to be decoded
  165. * @return amount of bytes read from the packet on success,
  166. * negative error code on failure
  167. */
  168. int (*decode)(struct AVCodecContext *avctx, struct AVFrame *frame,
  169. int *got_frame_ptr, struct AVPacket *avpkt);
  170. /**
  171. * Decode subtitle data to an AVSubtitle.
  172. * cb is in this state if cb_type is FF_CODEC_CB_TYPE_DECODE_SUB.
  173. *
  174. * Apart from that this is like the decode callback.
  175. */
  176. int (*decode_sub)(struct AVCodecContext *avctx, struct AVSubtitle *sub,
  177. int *got_frame_ptr, const struct AVPacket *avpkt);
  178. /**
  179. * Decode API with decoupled packet/frame dataflow.
  180. * cb is in this state if cb_type is FF_CODEC_CB_TYPE_RECEIVE_FRAME.
  181. *
  182. * This function is called to get one output frame. It should call
  183. * ff_decode_get_packet() to obtain input data.
  184. */
  185. int (*receive_frame)(struct AVCodecContext *avctx, struct AVFrame *frame);
  186. /**
  187. * Encode data to an AVPacket.
  188. * cb is in this state if cb_type is FF_CODEC_CB_TYPE_ENCODE
  189. *
  190. * @param avctx codec context
  191. * @param[out] avpkt output AVPacket
  192. * @param[in] frame AVFrame containing the input to be encoded
  193. * @param[out] got_packet_ptr encoder sets to 0 or 1 to indicate that a
  194. * non-empty packet was returned in avpkt.
  195. * @return 0 on success, negative error code on failure
  196. */
  197. int (*encode)(struct AVCodecContext *avctx, struct AVPacket *avpkt,
  198. const struct AVFrame *frame, int *got_packet_ptr);
  199. /**
  200. * Encode subtitles to a raw buffer.
  201. * cb is in this state if cb_type is FF_CODEC_CB_TYPE_ENCODE_SUB.
  202. */
  203. int (*encode_sub)(struct AVCodecContext *avctx, uint8_t *buf,
  204. int buf_size, const struct AVSubtitle *sub);
  205. /**
  206. * Encode API with decoupled frame/packet dataflow.
  207. * cb is in this state if cb_type is FF_CODEC_CB_TYPE_RECEIVE_PACKET.
  208. *
  209. * This function is called to get one output packet.
  210. * It should call ff_encode_get_frame() to obtain input data.
  211. */
  212. int (*receive_packet)(struct AVCodecContext *avctx, struct AVPacket *avpkt);
  213. } cb;
  214. int (*close)(struct AVCodecContext *);
  215. /**
  216. * Flush buffers.
  217. * Will be called when seeking
  218. */
  219. void (*flush)(struct AVCodecContext *);
  220. /**
  221. * Decoding only, a comma-separated list of bitstream filters to apply to
  222. * packets before decoding.
  223. */
  224. const char *bsfs;
  225. /**
  226. * Array of pointers to hardware configurations supported by the codec,
  227. * or NULL if no hardware supported. The array is terminated by a NULL
  228. * pointer.
  229. *
  230. * The user can only access this field via avcodec_get_hw_config().
  231. */
  232. const struct AVCodecHWConfigInternal *const *hw_configs;
  233. /**
  234. * List of supported codec_tags, terminated by FF_CODEC_TAGS_END.
  235. */
  236. const uint32_t *codec_tags;
  237. } FFCodec;
  238. #if CONFIG_SMALL
  239. #define CODEC_LONG_NAME(str) .p.long_name = NULL
  240. #else
  241. #define CODEC_LONG_NAME(str) .p.long_name = str
  242. #endif
  243. #if HAVE_THREADS
  244. #define UPDATE_THREAD_CONTEXT(func) \
  245. .update_thread_context = (func)
  246. #define UPDATE_THREAD_CONTEXT_FOR_USER(func) \
  247. .update_thread_context_for_user = (func)
  248. #else
  249. #define UPDATE_THREAD_CONTEXT(func) \
  250. .update_thread_context = NULL
  251. #define UPDATE_THREAD_CONTEXT_FOR_USER(func) \
  252. .update_thread_context_for_user = NULL
  253. #endif
  254. #define FF_CODEC_DECODE_CB(func) \
  255. .cb_type = FF_CODEC_CB_TYPE_DECODE, \
  256. .cb.decode = (func)
  257. #define FF_CODEC_DECODE_SUB_CB(func) \
  258. .cb_type = FF_CODEC_CB_TYPE_DECODE_SUB, \
  259. .cb.decode_sub = (func)
  260. #define FF_CODEC_RECEIVE_FRAME_CB(func) \
  261. .cb_type = FF_CODEC_CB_TYPE_RECEIVE_FRAME, \
  262. .cb.receive_frame = (func)
  263. #define FF_CODEC_ENCODE_CB(func) \
  264. .cb_type = FF_CODEC_CB_TYPE_ENCODE, \
  265. .cb.encode = (func)
  266. #define FF_CODEC_ENCODE_SUB_CB(func) \
  267. .cb_type = FF_CODEC_CB_TYPE_ENCODE_SUB, \
  268. .cb.encode_sub = (func)
  269. #define FF_CODEC_RECEIVE_PACKET_CB(func) \
  270. .cb_type = FF_CODEC_CB_TYPE_RECEIVE_PACKET, \
  271. .cb.receive_packet = (func)
  272. static av_always_inline const FFCodec *ffcodec(const AVCodec *codec)
  273. {
  274. return (const FFCodec*)codec;
  275. }
  276. #endif /* AVCODEC_CODEC_INTERNAL_H */