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.

298 lines
8.8KB

  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. #ifdef DEBUG
  45. # define ff_dlog(ctx, ...) av_log(ctx, AV_LOG_DEBUG, __VA_ARGS__)
  46. #else
  47. # define ff_dlog(ctx, ...) do { if (0) av_log(ctx, AV_LOG_DEBUG, __VA_ARGS__); } while (0)
  48. #endif
  49. #ifdef TRACE
  50. # define ff_tlog(ctx, ...) av_log(ctx, AV_LOG_TRACE, __VA_ARGS__)
  51. #else
  52. # define ff_tlog(ctx, ...) while(0)
  53. #endif
  54. #define FF_SANE_NB_CHANNELS 63U
  55. #define FF_SIGNBIT(x) ((x) >> CHAR_BIT * sizeof(x) - 1)
  56. #if HAVE_AVX
  57. # define STRIDE_ALIGN 32
  58. #elif HAVE_SIMD_ALIGN_16
  59. # define STRIDE_ALIGN 16
  60. #else
  61. # define STRIDE_ALIGN 8
  62. #endif
  63. typedef struct FramePool {
  64. /**
  65. * Pools for each data plane. For audio all the planes have the same size,
  66. * so only pools[0] is used.
  67. */
  68. AVBufferPool *pools[4];
  69. /*
  70. * Pool parameters
  71. */
  72. int format;
  73. int width, height;
  74. int stride_align[AV_NUM_DATA_POINTERS];
  75. int linesize[4];
  76. int planes;
  77. int channels;
  78. int samples;
  79. } FramePool;
  80. typedef struct AVCodecInternal {
  81. /**
  82. * Whether the parent AVCodecContext is a copy of the context which had
  83. * init() called on it.
  84. * This is used by multithreading - shared tables and picture pointers
  85. * should be freed from the original context only.
  86. */
  87. int is_copy;
  88. /**
  89. * Whether to allocate progress for frame threading.
  90. *
  91. * The codec must set it to 1 if it uses ff_thread_await/report_progress(),
  92. * then progress will be allocated in ff_thread_get_buffer(). The frames
  93. * then MUST be freed with ff_thread_release_buffer().
  94. *
  95. * If the codec does not need to call the progress functions (there are no
  96. * dependencies between the frames), it should leave this at 0. Then it can
  97. * decode straight to the user-provided frames (which the user will then
  98. * free with av_frame_unref()), there is no need to call
  99. * ff_thread_release_buffer().
  100. */
  101. int allocate_progress;
  102. #if FF_API_OLD_ENCODE_AUDIO
  103. /**
  104. * Internal sample count used by avcodec_encode_audio() to fabricate pts.
  105. * Can be removed along with avcodec_encode_audio().
  106. */
  107. int64_t sample_count;
  108. #endif
  109. /**
  110. * An audio frame with less than required samples has been submitted and
  111. * padded with silence. Reject all subsequent frames.
  112. */
  113. int last_audio_frame;
  114. AVFrame *to_free;
  115. FramePool *pool;
  116. void *thread_ctx;
  117. /**
  118. * Current packet as passed into the decoder, to avoid having to pass the
  119. * packet into every function.
  120. */
  121. AVPacket *pkt;
  122. /**
  123. * temporary buffer used for encoders to store their bitstream
  124. */
  125. uint8_t *byte_buffer;
  126. unsigned int byte_buffer_size;
  127. void *frame_thread_encoder;
  128. /**
  129. * Number of audio samples to skip at the start of the next decoded frame
  130. */
  131. int skip_samples;
  132. /**
  133. * hwaccel-specific private data
  134. */
  135. void *hwaccel_priv_data;
  136. } AVCodecInternal;
  137. struct AVCodecDefault {
  138. const uint8_t *key;
  139. const uint8_t *value;
  140. };
  141. extern const uint8_t ff_log2_run[41];
  142. /**
  143. * Return the index into tab at which {a,b} match elements {[0],[1]} of tab.
  144. * If there is no such matching pair then size is returned.
  145. */
  146. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b);
  147. unsigned int avpriv_toupper4(unsigned int x);
  148. /**
  149. * does needed setup of pkt_pts/pos and such for (re)get_buffer();
  150. */
  151. int ff_init_buffer_info(AVCodecContext *s, AVFrame *frame);
  152. void avpriv_color_frame(AVFrame *frame, const int color[4]);
  153. extern volatile int ff_avcodec_locked;
  154. int ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec);
  155. int ff_unlock_avcodec(void);
  156. int avpriv_lock_avformat(void);
  157. int avpriv_unlock_avformat(void);
  158. /**
  159. * Maximum size in bytes of extradata.
  160. * This value was chosen such that every bit of the buffer is
  161. * addressable by a 32-bit signed integer as used by get_bits.
  162. */
  163. #define FF_MAX_EXTRADATA_SIZE ((1 << 28) - FF_INPUT_BUFFER_PADDING_SIZE)
  164. /**
  165. * Check AVPacket size and/or allocate data.
  166. *
  167. * Encoders supporting AVCodec.encode2() can use this as a convenience to
  168. * ensure the output packet data is large enough, whether provided by the user
  169. * or allocated in this function.
  170. *
  171. * @param avctx the AVCodecContext of the encoder
  172. * @param avpkt the AVPacket
  173. * If avpkt->data is already set, avpkt->size is checked
  174. * to ensure it is large enough.
  175. * If avpkt->data is NULL, a new buffer is allocated.
  176. * avpkt->size is set to the specified size.
  177. * All other AVPacket fields will be reset with av_init_packet().
  178. * @param size the minimum required packet size
  179. * @return non negative on success, negative error code on failure
  180. */
  181. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size);
  182. int ff_alloc_packet(AVPacket *avpkt, int size);
  183. /**
  184. * Rescale from sample rate to AVCodecContext.time_base.
  185. */
  186. static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx,
  187. int64_t samples)
  188. {
  189. if(samples == AV_NOPTS_VALUE)
  190. return AV_NOPTS_VALUE;
  191. return av_rescale_q(samples, (AVRational){ 1, avctx->sample_rate },
  192. avctx->time_base);
  193. }
  194. /**
  195. * Get a buffer for a frame. This is a wrapper around
  196. * AVCodecContext.get_buffer() and should be used instead calling get_buffer()
  197. * directly.
  198. */
  199. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags);
  200. /**
  201. * Identical in function to av_frame_make_writable(), except it uses
  202. * ff_get_buffer() to allocate the buffer when needed.
  203. */
  204. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame);
  205. int ff_thread_can_start_frame(AVCodecContext *avctx);
  206. int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx);
  207. /**
  208. * Call avcodec_open2 recursively by decrementing counter, unlocking mutex,
  209. * calling the function and then restoring again. Assumes the mutex is
  210. * already locked
  211. */
  212. int ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options);
  213. /**
  214. * Finalize buf into extradata and set its size appropriately.
  215. */
  216. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf);
  217. const uint8_t *avpriv_find_start_code(const uint8_t *p,
  218. const uint8_t *end,
  219. uint32_t *state);
  220. /**
  221. * Check that the provided frame dimensions are valid and set them on the codec
  222. * context.
  223. */
  224. int ff_set_dimensions(AVCodecContext *s, int width, int height);
  225. /**
  226. * Check that the provided sample aspect ratio is valid and set it on the codec
  227. * context.
  228. */
  229. int ff_set_sar(AVCodecContext *avctx, AVRational sar);
  230. /**
  231. * Add or update AV_FRAME_DATA_MATRIXENCODING side data.
  232. */
  233. int ff_side_data_update_matrix_encoding(AVFrame *frame,
  234. enum AVMatrixEncoding matrix_encoding);
  235. /**
  236. * Select the (possibly hardware accelerated) pixel format.
  237. * This is a wrapper around AVCodecContext.get_format() and should be used
  238. * instead of calling get_format() directly.
  239. */
  240. int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt);
  241. /**
  242. * Set various frame properties from the codec context / packet data.
  243. */
  244. int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame);
  245. #endif /* AVCODEC_INTERNAL_H */