internal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. /**
  19. * @file
  20. * common internal api header.
  21. */
  22. #ifndef AVCODEC_INTERNAL_H
  23. #define AVCODEC_INTERNAL_H
  24. #include <stdint.h>
  25. #include "libavutil/buffer.h"
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/mathematics.h"
  28. #include "libavutil/pixfmt.h"
  29. #include "avcodec.h"
  30. #include "config.h"
  31. /**
  32. * The codec does not modify any global variables in the init function,
  33. * allowing to call the init function without locking any global mutexes.
  34. */
  35. #define FF_CODEC_CAP_INIT_THREADSAFE (1 << 0)
  36. /**
  37. * The codec allows calling the close function for deallocation even if
  38. * the init function returned a failure. Without this capability flag, a
  39. * codec does such cleanup internally when returning failures from the
  40. * init function and does not expect the close function to be called at
  41. * all.
  42. */
  43. #define FF_CODEC_CAP_INIT_CLEANUP (1 << 1)
  44. /**
  45. * Decoders marked with FF_CODEC_CAP_SETS_PKT_DTS want to set
  46. * AVFrame.pkt_dts manually. If the flag is set, decode.c won't overwrite
  47. * this field. If it's unset, decode.c tries to guess the pkt_dts field
  48. * from the input AVPacket.
  49. */
  50. #define FF_CODEC_CAP_SETS_PKT_DTS (1 << 2)
  51. /**
  52. * The decoder extracts and fills its parameters even if the frame is
  53. * skipped due to the skip_frame setting.
  54. */
  55. #define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM (1 << 3)
  56. /**
  57. * The decoder sets the cropping fields in the output frames manually.
  58. * If this cap is set, the generic code will initialize output frame
  59. * dimensions to coded rather than display values.
  60. */
  61. #define FF_CODEC_CAP_EXPORTS_CROPPING (1 << 4)
  62. /**
  63. * Codec initializes slice-based threading with a main function
  64. */
  65. #define FF_CODEC_CAP_SLICE_THREAD_HAS_MF (1 << 5)
  66. /*
  67. * The codec supports frame threading and has inter-frame dependencies, so it
  68. * uses ff_thread_report/await_progress().
  69. */
  70. #define FF_CODEC_CAP_ALLOCATE_PROGRESS (1 << 6)
  71. /**
  72. * AVCodec.codec_tags termination value
  73. */
  74. #define FF_CODEC_TAGS_END -1
  75. #ifdef TRACE
  76. # define ff_tlog(ctx, ...) av_log(ctx, AV_LOG_TRACE, __VA_ARGS__)
  77. #else
  78. # define ff_tlog(ctx, ...) do { } while(0)
  79. #endif
  80. #define FF_DEFAULT_QUANT_BIAS 999999
  81. #define FF_QSCALE_TYPE_MPEG1 0
  82. #define FF_QSCALE_TYPE_MPEG2 1
  83. #define FF_QSCALE_TYPE_H264 2
  84. #define FF_QSCALE_TYPE_VP56 3
  85. #define FF_SANE_NB_CHANNELS 512U
  86. #define FF_SIGNBIT(x) ((x) >> CHAR_BIT * sizeof(x) - 1)
  87. #if HAVE_SIMD_ALIGN_64
  88. # define STRIDE_ALIGN 64 /* AVX-512 */
  89. #elif HAVE_SIMD_ALIGN_32
  90. # define STRIDE_ALIGN 32
  91. #elif HAVE_SIMD_ALIGN_16
  92. # define STRIDE_ALIGN 16
  93. #else
  94. # define STRIDE_ALIGN 8
  95. #endif
  96. typedef struct DecodeSimpleContext {
  97. AVPacket *in_pkt;
  98. AVFrame *out_frame;
  99. } DecodeSimpleContext;
  100. typedef struct DecodeFilterContext {
  101. AVBSFContext **bsfs;
  102. int nb_bsfs;
  103. } DecodeFilterContext;
  104. typedef struct AVCodecInternal {
  105. /**
  106. * Whether the parent AVCodecContext is a copy of the context which had
  107. * init() called on it.
  108. * This is used by multithreading - shared tables and picture pointers
  109. * should be freed from the original context only.
  110. */
  111. int is_copy;
  112. /**
  113. * An audio frame with less than required samples has been submitted and
  114. * padded with silence. Reject all subsequent frames.
  115. */
  116. int last_audio_frame;
  117. AVFrame *to_free;
  118. AVBufferRef *pool;
  119. void *thread_ctx;
  120. DecodeSimpleContext ds;
  121. DecodeFilterContext filter;
  122. /**
  123. * Properties (timestamps+side data) extracted from the last packet passed
  124. * for decoding.
  125. */
  126. AVPacket *last_pkt_props;
  127. /**
  128. * temporary buffer used for encoders to store their bitstream
  129. */
  130. uint8_t *byte_buffer;
  131. unsigned int byte_buffer_size;
  132. void *frame_thread_encoder;
  133. /**
  134. * Number of audio samples to skip at the start of the next decoded frame
  135. */
  136. int skip_samples;
  137. /**
  138. * hwaccel-specific private data
  139. */
  140. void *hwaccel_priv_data;
  141. /**
  142. * checks API usage: after codec draining, flush is required to resume operation
  143. */
  144. int draining;
  145. /**
  146. * buffers for using new encode/decode API through legacy API
  147. */
  148. AVPacket *buffer_pkt;
  149. int buffer_pkt_valid; // encoding: packet without data can be valid
  150. AVFrame *buffer_frame;
  151. int draining_done;
  152. /* set to 1 when the caller is using the old decoding API */
  153. int compat_decode;
  154. int compat_decode_warned;
  155. /* this variable is set by the decoder internals to signal to the old
  156. * API compat wrappers the amount of data consumed from the last packet */
  157. size_t compat_decode_consumed;
  158. /* when a partial packet has been consumed, this stores the remaining size
  159. * of the packet (that should be submitted in the next decode call */
  160. size_t compat_decode_partial_size;
  161. AVFrame *compat_decode_frame;
  162. int showed_multi_packet_warning;
  163. int skip_samples_multiplier;
  164. /* to prevent infinite loop on errors when draining */
  165. int nb_draining_errors;
  166. /* used when avctx flag AV_CODEC_FLAG_DROPCHANGED is set */
  167. int changed_frames_dropped;
  168. int initial_format;
  169. int initial_width, initial_height;
  170. int initial_sample_rate;
  171. int initial_channels;
  172. uint64_t initial_channel_layout;
  173. } AVCodecInternal;
  174. struct AVCodecDefault {
  175. const uint8_t *key;
  176. const uint8_t *value;
  177. };
  178. extern const uint8_t ff_log2_run[41];
  179. /**
  180. * Return the index into tab at which {a,b} match elements {[0],[1]} of tab.
  181. * If there is no such matching pair then size is returned.
  182. */
  183. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b);
  184. unsigned int avpriv_toupper4(unsigned int x);
  185. void ff_color_frame(AVFrame *frame, const int color[4]);
  186. /**
  187. * Maximum size in bytes of extradata.
  188. * This value was chosen such that every bit of the buffer is
  189. * addressable by a 32-bit signed integer as used by get_bits.
  190. */
  191. #define FF_MAX_EXTRADATA_SIZE ((1 << 28) - AV_INPUT_BUFFER_PADDING_SIZE)
  192. /**
  193. * Check AVPacket size and/or allocate data.
  194. *
  195. * Encoders supporting AVCodec.encode2() can use this as a convenience to
  196. * ensure the output packet data is large enough, whether provided by the user
  197. * or allocated in this function.
  198. *
  199. * @param avctx the AVCodecContext of the encoder
  200. * @param avpkt the AVPacket
  201. * If avpkt->data is already set, avpkt->size is checked
  202. * to ensure it is large enough.
  203. * If avpkt->data is NULL, a new buffer is allocated.
  204. * avpkt->size is set to the specified size.
  205. * All other AVPacket fields will be reset with av_init_packet().
  206. * @param size the minimum required packet size
  207. * @param min_size This is a hint to the allocation algorithm, which indicates
  208. * to what minimal size the caller might later shrink the packet
  209. * to. Encoders often allocate packets which are larger than the
  210. * amount of data that is written into them as the exact amount is
  211. * not known at the time of allocation. min_size represents the
  212. * size a packet might be shrunk to by the caller. Can be set to
  213. * 0. setting this roughly correctly allows the allocation code
  214. * to choose between several allocation strategies to improve
  215. * speed slightly.
  216. * @return non negative on success, negative error code on failure
  217. */
  218. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size);
  219. attribute_deprecated int ff_alloc_packet(AVPacket *avpkt, int size);
  220. /**
  221. * Rescale from sample rate to AVCodecContext.time_base.
  222. */
  223. static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx,
  224. int64_t samples)
  225. {
  226. if(samples == AV_NOPTS_VALUE)
  227. return AV_NOPTS_VALUE;
  228. return av_rescale_q(samples, (AVRational){ 1, avctx->sample_rate },
  229. avctx->time_base);
  230. }
  231. /**
  232. * 2^(x) for integer x
  233. * @return correctly rounded float
  234. */
  235. static av_always_inline float ff_exp2fi(int x) {
  236. /* Normal range */
  237. if (-126 <= x && x <= 128)
  238. return av_int2float((x+127) << 23);
  239. /* Too large */
  240. else if (x > 128)
  241. return INFINITY;
  242. /* Subnormal numbers */
  243. else if (x > -150)
  244. return av_int2float(1 << (x+149));
  245. /* Negligibly small */
  246. else
  247. return 0;
  248. }
  249. /**
  250. * Get a buffer for a frame. This is a wrapper around
  251. * AVCodecContext.get_buffer() and should be used instead calling get_buffer()
  252. * directly.
  253. */
  254. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags);
  255. #define FF_REGET_BUFFER_FLAG_READONLY 1 ///< the returned buffer does not need to be writable
  256. /**
  257. * Identical in function to ff_get_buffer(), except it reuses the existing buffer
  258. * if available.
  259. */
  260. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags);
  261. int ff_thread_can_start_frame(AVCodecContext *avctx);
  262. int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx);
  263. /**
  264. * Call avcodec_open2 recursively by decrementing counter, unlocking mutex,
  265. * calling the function and then restoring again. Assumes the mutex is
  266. * already locked
  267. */
  268. int ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options);
  269. /**
  270. * Finalize buf into extradata and set its size appropriately.
  271. */
  272. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf);
  273. const uint8_t *avpriv_find_start_code(const uint8_t *p,
  274. const uint8_t *end,
  275. uint32_t *state);
  276. int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec);
  277. /**
  278. * Check that the provided frame dimensions are valid and set them on the codec
  279. * context.
  280. */
  281. int ff_set_dimensions(AVCodecContext *s, int width, int height);
  282. /**
  283. * Check that the provided sample aspect ratio is valid and set it on the codec
  284. * context.
  285. */
  286. int ff_set_sar(AVCodecContext *avctx, AVRational sar);
  287. /**
  288. * Add or update AV_FRAME_DATA_MATRIXENCODING side data.
  289. */
  290. int ff_side_data_update_matrix_encoding(AVFrame *frame,
  291. enum AVMatrixEncoding matrix_encoding);
  292. /**
  293. * Select the (possibly hardware accelerated) pixel format.
  294. * This is a wrapper around AVCodecContext.get_format() and should be used
  295. * instead of calling get_format() directly.
  296. *
  297. * The list of pixel formats must contain at least one valid entry, and is
  298. * terminated with AV_PIX_FMT_NONE. If it is possible to decode to software,
  299. * the last entry in the list must be the most accurate software format.
  300. * If it is not possible to decode to software, AVCodecContext.sw_pix_fmt
  301. * must be set before calling this function.
  302. */
  303. int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt);
  304. /**
  305. * Set various frame properties from the codec context / packet data.
  306. */
  307. int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame);
  308. /**
  309. * Add a CPB properties side data to an encoding context.
  310. */
  311. AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx);
  312. int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type);
  313. int ff_side_data_set_prft(AVPacket *pkt, int64_t timestamp);
  314. /**
  315. * Check AVFrame for A53 side data and allocate and fill SEI message with A53 info
  316. *
  317. * @param frame Raw frame to get A53 side data from
  318. * @param prefix_len Number of bytes to allocate before SEI message
  319. * @param data Pointer to a variable to store allocated memory
  320. * Upon return the variable will hold NULL on error or if frame has no A53 info.
  321. * Otherwise it will point to prefix_len uninitialized bytes followed by
  322. * *sei_size SEI message
  323. * @param sei_size Pointer to a variable to store generated SEI message length
  324. * @return Zero on success, negative error code on failure
  325. */
  326. int ff_alloc_a53_sei(const AVFrame *frame, size_t prefix_len,
  327. void **data, size_t *sei_size);
  328. /**
  329. * Get an estimated video bitrate based on frame size, frame rate and coded
  330. * bits per pixel.
  331. */
  332. int64_t ff_guess_coded_bitrate(AVCodecContext *avctx);
  333. /**
  334. * Check if a value is in the list. If not, return the default value
  335. *
  336. * @param ctx Context for the log msg
  337. * @param val_name Name of the checked value, for log msg
  338. * @param array_valid_values Array of valid int, ended with INT_MAX
  339. * @param default_value Value return if checked value is not in the array
  340. * @return Value or default_value.
  341. */
  342. int ff_int_from_list_or_default(void *ctx, const char * val_name, int val,
  343. const int * array_valid_values, int default_value);
  344. void ff_dvdsub_parse_palette(uint32_t *palette, const char *p);
  345. #if defined(_WIN32) && CONFIG_SHARED && !defined(BUILDING_avcodec)
  346. # define av_export_avcodec __declspec(dllimport)
  347. #else
  348. # define av_export_avcodec
  349. #endif
  350. #endif /* AVCODEC_INTERNAL_H */