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.

269 lines
8.0KB

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