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.

315 lines
9.9KB

  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_VAAPI_ENCODE_H
  19. #define AVCODEC_VAAPI_ENCODE_H
  20. #include <stdint.h>
  21. #include <va/va.h>
  22. #if VA_CHECK_VERSION(1, 0, 0)
  23. #include <va/va_str.h>
  24. #endif
  25. #include "libavutil/hwcontext.h"
  26. #include "libavutil/hwcontext_vaapi.h"
  27. #include "avcodec.h"
  28. struct VAAPIEncodeType;
  29. struct VAAPIEncodePicture;
  30. enum {
  31. MAX_CONFIG_ATTRIBUTES = 4,
  32. MAX_GLOBAL_PARAMS = 4,
  33. MAX_PICTURE_REFERENCES = 2,
  34. MAX_REORDER_DELAY = 16,
  35. MAX_PARAM_BUFFER_SIZE = 1024,
  36. };
  37. enum {
  38. PICTURE_TYPE_IDR = 0,
  39. PICTURE_TYPE_I = 1,
  40. PICTURE_TYPE_P = 2,
  41. PICTURE_TYPE_B = 3,
  42. };
  43. typedef struct VAAPIEncodeSlice {
  44. int index;
  45. void *priv_data;
  46. void *codec_slice_params;
  47. } VAAPIEncodeSlice;
  48. typedef struct VAAPIEncodePicture {
  49. struct VAAPIEncodePicture *next;
  50. int64_t display_order;
  51. int64_t encode_order;
  52. int64_t pts;
  53. int type;
  54. int input_available;
  55. int encode_issued;
  56. int encode_complete;
  57. AVFrame *input_image;
  58. VASurfaceID input_surface;
  59. AVFrame *recon_image;
  60. VASurfaceID recon_surface;
  61. int nb_param_buffers;
  62. VABufferID *param_buffers;
  63. AVBufferRef *output_buffer_ref;
  64. VABufferID output_buffer;
  65. void *priv_data;
  66. void *codec_picture_params;
  67. int nb_refs;
  68. struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
  69. int nb_slices;
  70. VAAPIEncodeSlice *slices;
  71. } VAAPIEncodePicture;
  72. typedef struct VAAPIEncodeProfile {
  73. // lavc profile value (FF_PROFILE_*).
  74. int av_profile;
  75. // Supported bit depth.
  76. int depth;
  77. // Number of components.
  78. int nb_components;
  79. // Chroma subsampling in width dimension.
  80. int log2_chroma_w;
  81. // Chroma subsampling in height dimension.
  82. int log2_chroma_h;
  83. // VAAPI profile value.
  84. VAProfile va_profile;
  85. } VAAPIEncodeProfile;
  86. typedef struct VAAPIEncodeContext {
  87. const AVClass *class;
  88. // Codec-specific hooks.
  89. const struct VAAPIEncodeType *codec;
  90. // Global options.
  91. // Use low power encoding mode.
  92. int low_power;
  93. // Desired packed headers.
  94. unsigned int desired_packed_headers;
  95. // The required size of surfaces. This is probably the input
  96. // size (AVCodecContext.width|height) aligned up to whatever
  97. // block size is required by the codec.
  98. int surface_width;
  99. int surface_height;
  100. // Everything above this point must be set before calling
  101. // ff_vaapi_encode_init().
  102. // Chosen encoding profile details.
  103. const VAAPIEncodeProfile *profile;
  104. // Encoding profile (VAProfile*).
  105. VAProfile va_profile;
  106. // Encoding entrypoint (VAEntryoint*).
  107. VAEntrypoint va_entrypoint;
  108. // Rate control mode.
  109. unsigned int va_rc_mode;
  110. // Bitrate for codec-specific encoder parameters.
  111. unsigned int va_bit_rate;
  112. // Packed headers which will actually be sent.
  113. unsigned int va_packed_headers;
  114. // Configuration attributes to use when creating va_config.
  115. VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
  116. int nb_config_attributes;
  117. VAConfigID va_config;
  118. VAContextID va_context;
  119. AVBufferRef *device_ref;
  120. AVHWDeviceContext *device;
  121. AVVAAPIDeviceContext *hwctx;
  122. // The hardware frame context containing the input frames.
  123. AVBufferRef *input_frames_ref;
  124. AVHWFramesContext *input_frames;
  125. // The hardware frame context containing the reconstructed frames.
  126. AVBufferRef *recon_frames_ref;
  127. AVHWFramesContext *recon_frames;
  128. // Pool of (reusable) bitstream output buffers.
  129. AVBufferPool *output_buffer_pool;
  130. // Global parameters which will be applied at the start of the
  131. // sequence (includes rate control parameters below).
  132. VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
  133. size_t global_params_size[MAX_GLOBAL_PARAMS];
  134. int nb_global_params;
  135. // Rate control parameters.
  136. struct {
  137. VAEncMiscParameterBuffer misc;
  138. VAEncMiscParameterRateControl rc;
  139. } rc_params;
  140. struct {
  141. VAEncMiscParameterBuffer misc;
  142. VAEncMiscParameterHRD hrd;
  143. } hrd_params;
  144. struct {
  145. VAEncMiscParameterBuffer misc;
  146. VAEncMiscParameterFrameRate fr;
  147. } fr_params;
  148. #if VA_CHECK_VERSION(0, 36, 0)
  149. struct {
  150. VAEncMiscParameterBuffer misc;
  151. VAEncMiscParameterBufferQualityLevel quality;
  152. } quality_params;
  153. #endif
  154. // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
  155. void *codec_sequence_params;
  156. // Per-sequence parameters found in the per-picture parameter
  157. // structure (VAEncPictureParameterBuffer*).
  158. void *codec_picture_params;
  159. // Current encoding window, in display (input) order.
  160. VAAPIEncodePicture *pic_start, *pic_end;
  161. // Next input order index (display order).
  162. int64_t input_order;
  163. // Number of frames that output is behind input.
  164. int64_t output_delay;
  165. // Number of frames decode output will need to be delayed.
  166. int64_t decode_delay;
  167. // Next output order index (encode order).
  168. int64_t output_order;
  169. enum {
  170. // All encode operations are done independently (synchronise
  171. // immediately after every operation).
  172. ISSUE_MODE_SERIALISE_EVERYTHING = 0,
  173. // Overlap as many operations as possible.
  174. ISSUE_MODE_MAXIMISE_THROUGHPUT,
  175. // Overlap operations only when satisfying parallel dependencies.
  176. ISSUE_MODE_MINIMISE_LATENCY,
  177. } issue_mode;
  178. // Timestamp handling.
  179. int64_t first_pts;
  180. int64_t dts_pts_diff;
  181. int64_t ts_ring[MAX_REORDER_DELAY * 3];
  182. // Frame type decision.
  183. int gop_size;
  184. int p_per_i;
  185. int b_per_p;
  186. int force_idr;
  187. int gop_counter;
  188. int p_counter;
  189. int end_of_stream;
  190. } VAAPIEncodeContext;
  191. typedef struct VAAPIEncodeType {
  192. // List of supported profiles and corresponding VAAPI profiles.
  193. // (Must end with FF_PROFILE_UNKNOWN.)
  194. const VAAPIEncodeProfile *profiles;
  195. // Perform any extra codec-specific configuration after the
  196. // codec context is initialised (set up the private data and
  197. // add any necessary global parameters).
  198. int (*configure)(AVCodecContext *avctx);
  199. // The size of the parameter structures:
  200. // sizeof(VAEnc{type}ParameterBuffer{codec}).
  201. size_t sequence_params_size;
  202. size_t picture_params_size;
  203. size_t slice_params_size;
  204. // Fill the parameter structures.
  205. int (*init_sequence_params)(AVCodecContext *avctx);
  206. int (*init_picture_params)(AVCodecContext *avctx,
  207. VAAPIEncodePicture *pic);
  208. int (*init_slice_params)(AVCodecContext *avctx,
  209. VAAPIEncodePicture *pic,
  210. VAAPIEncodeSlice *slice);
  211. // The type used by the packed header: this should look like
  212. // VAEncPackedHeader{something}.
  213. int sequence_header_type;
  214. int picture_header_type;
  215. int slice_header_type;
  216. // Write the packed header data to the provided buffer.
  217. // The sequence header is also used to fill the codec extradata
  218. // when the encoder is starting.
  219. int (*write_sequence_header)(AVCodecContext *avctx,
  220. char *data, size_t *data_len);
  221. int (*write_picture_header)(AVCodecContext *avctx,
  222. VAAPIEncodePicture *pic,
  223. char *data, size_t *data_len);
  224. int (*write_slice_header)(AVCodecContext *avctx,
  225. VAAPIEncodePicture *pic,
  226. VAAPIEncodeSlice *slice,
  227. char *data, size_t *data_len);
  228. // Fill an extra parameter structure, which will then be
  229. // passed to vaRenderPicture(). Will be called repeatedly
  230. // with increasing index argument until AVERROR_EOF is
  231. // returned.
  232. int (*write_extra_buffer)(AVCodecContext *avctx,
  233. VAAPIEncodePicture *pic,
  234. int index, int *type,
  235. char *data, size_t *data_len);
  236. // Write an extra packed header. Will be called repeatedly
  237. // with increasing index argument until AVERROR_EOF is
  238. // returned.
  239. int (*write_extra_header)(AVCodecContext *avctx,
  240. VAAPIEncodePicture *pic,
  241. int index, int *type,
  242. char *data, size_t *data_len);
  243. } VAAPIEncodeType;
  244. int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
  245. const AVFrame *input_image, int *got_packet);
  246. int ff_vaapi_encode_init(AVCodecContext *avctx);
  247. int ff_vaapi_encode_close(AVCodecContext *avctx);
  248. #define VAAPI_ENCODE_COMMON_OPTIONS \
  249. { "low_power", \
  250. "Use low-power encoding mode (only available on some platforms; " \
  251. "may not support all encoding features)", \
  252. OFFSET(common.low_power), AV_OPT_TYPE_BOOL, \
  253. { .i64 = 0 }, 0, 1, FLAGS }
  254. #endif /* AVCODEC_VAAPI_ENCODE_H */