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.

280 lines
8.1KB

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