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.

276 lines
8.9KB

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