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
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_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. struct {
  126. VAEncMiscParameterBuffer misc;
  127. VAEncMiscParameterFrameRate fr;
  128. } fr_params;
  129. // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
  130. void *codec_sequence_params;
  131. // Per-sequence parameters found in the per-picture parameter
  132. // structure (VAEncPictureParameterBuffer*).
  133. void *codec_picture_params;
  134. // Current encoding window, in display (input) order.
  135. VAAPIEncodePicture *pic_start, *pic_end;
  136. // Next input order index (display order).
  137. int64_t input_order;
  138. // Number of frames that output is behind input.
  139. int64_t output_delay;
  140. // Number of frames decode output will need to be delayed.
  141. int64_t decode_delay;
  142. // Next output order index (encode order).
  143. int64_t output_order;
  144. enum {
  145. // All encode operations are done independently (synchronise
  146. // immediately after every operation).
  147. ISSUE_MODE_SERIALISE_EVERYTHING = 0,
  148. // Overlap as many operations as possible.
  149. ISSUE_MODE_MAXIMISE_THROUGHPUT,
  150. // Overlap operations only when satisfying parallel dependencies.
  151. ISSUE_MODE_MINIMISE_LATENCY,
  152. } issue_mode;
  153. // Timestamp handling.
  154. int64_t first_pts;
  155. int64_t dts_pts_diff;
  156. int64_t ts_ring[MAX_REORDER_DELAY * 3];
  157. // Frame type decision.
  158. int p_per_i;
  159. int b_per_p;
  160. int force_idr;
  161. int gop_counter;
  162. int p_counter;
  163. int end_of_stream;
  164. // Codec-local options are allocated to follow this structure in
  165. // memory (in the AVCodec definition, set priv_data_size to
  166. // sizeof(VAAPIEncodeContext) + sizeof(VAAPIEncodeFooOptions)).
  167. void *codec_options;
  168. char codec_options_data[0];
  169. } VAAPIEncodeContext;
  170. typedef struct VAAPIEncodeType {
  171. size_t priv_data_size;
  172. // Perform any extra codec-specific configuration after the
  173. // codec context is initialised (set up the private data and
  174. // add any necessary global parameters).
  175. int (*configure)(AVCodecContext *avctx);
  176. // The size of the parameter structures:
  177. // sizeof(VAEnc{type}ParameterBuffer{codec}).
  178. size_t sequence_params_size;
  179. size_t picture_params_size;
  180. size_t slice_params_size;
  181. // Fill the parameter structures.
  182. int (*init_sequence_params)(AVCodecContext *avctx);
  183. int (*init_picture_params)(AVCodecContext *avctx,
  184. VAAPIEncodePicture *pic);
  185. int (*init_slice_params)(AVCodecContext *avctx,
  186. VAAPIEncodePicture *pic,
  187. VAAPIEncodeSlice *slice);
  188. // The type used by the packed header: this should look like
  189. // VAEncPackedHeader{something}.
  190. int sequence_header_type;
  191. int picture_header_type;
  192. int slice_header_type;
  193. // Write the packed header data to the provided buffer.
  194. // The sequence header is also used to fill the codec extradata
  195. // when the encoder is starting.
  196. int (*write_sequence_header)(AVCodecContext *avctx,
  197. char *data, size_t *data_len);
  198. int (*write_picture_header)(AVCodecContext *avctx,
  199. VAAPIEncodePicture *pic,
  200. char *data, size_t *data_len);
  201. int (*write_slice_header)(AVCodecContext *avctx,
  202. VAAPIEncodePicture *pic,
  203. VAAPIEncodeSlice *slice,
  204. char *data, size_t *data_len);
  205. // Fill an extra parameter structure, which will then be
  206. // passed to vaRenderPicture(). Will be called repeatedly
  207. // with increasing index argument until AVERROR_EOF is
  208. // returned.
  209. int (*write_extra_buffer)(AVCodecContext *avctx,
  210. VAAPIEncodePicture *pic,
  211. int index, int *type,
  212. char *data, size_t *data_len);
  213. // Write an extra packed header. Will be called repeatedly
  214. // with increasing index argument until AVERROR_EOF is
  215. // returned.
  216. int (*write_extra_header)(AVCodecContext *avctx,
  217. VAAPIEncodePicture *pic,
  218. int index, int *type,
  219. char *data, size_t *data_len);
  220. } VAAPIEncodeType;
  221. int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
  222. const AVFrame *input_image, int *got_packet);
  223. int ff_vaapi_encode_init(AVCodecContext *avctx);
  224. int ff_vaapi_encode_close(AVCodecContext *avctx);
  225. #endif /* AVCODEC_VAAPI_ENCODE_H */