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.

284 lines
9.1KB

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