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.

274 lines
8.8KB

  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 = 1,
  32. MAX_PARAM_BUFFERS = 16,
  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. void *priv_data;
  44. void *codec_slice_params;
  45. } VAAPIEncodeSlice;
  46. typedef struct VAAPIEncodePicture {
  47. struct VAAPIEncodePicture *next;
  48. int64_t display_order;
  49. int64_t encode_order;
  50. int64_t pts;
  51. int type;
  52. int input_available;
  53. int encode_issued;
  54. int encode_complete;
  55. AVFrame *input_image;
  56. VASurfaceID input_surface;
  57. AVFrame *recon_image;
  58. VASurfaceID recon_surface;
  59. int nb_param_buffers;
  60. VABufferID param_buffers[MAX_PARAM_BUFFERS];
  61. AVBufferRef *output_buffer_ref;
  62. VABufferID output_buffer;
  63. void *priv_data;
  64. void *codec_picture_params;
  65. int nb_refs;
  66. struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
  67. int nb_slices;
  68. VAAPIEncodeSlice *slices[MAX_PICTURE_SLICES];
  69. } VAAPIEncodePicture;
  70. typedef struct VAAPIEncodeContext {
  71. const AVClass *class;
  72. // Codec-specific hooks.
  73. const struct VAAPIEncodeType *codec;
  74. // Encoding profile (VAProfileXXX).
  75. VAProfile va_profile;
  76. // Encoding entrypoint (usually VAEntryointEncSlice).
  77. VAEntrypoint va_entrypoint;
  78. // Surface colour/sampling format (usually VA_RT_FORMAT_YUV420).
  79. unsigned int va_rt_format;
  80. // Rate control mode.
  81. unsigned int va_rc_mode;
  82. // Supported packed headers (initially the desired set, modified
  83. // later to what is actually supported).
  84. unsigned int va_packed_headers;
  85. // The required size of surfaces. This is probably the input
  86. // size (AVCodecContext.width|height) aligned up to whatever
  87. // block size is required by the codec.
  88. int surface_width;
  89. int surface_height;
  90. // Everything above this point must be set before calling
  91. // ff_vaapi_encode_init().
  92. // Codec-specific state.
  93. void *priv_data;
  94. // Configuration attributes to use when creating va_config.
  95. VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
  96. int nb_config_attributes;
  97. VAConfigID va_config;
  98. VAContextID va_context;
  99. AVBufferRef *device_ref;
  100. AVHWDeviceContext *device;
  101. AVVAAPIDeviceContext *hwctx;
  102. // The hardware frame context containing the input frames.
  103. AVBufferRef *input_frames_ref;
  104. AVHWFramesContext *input_frames;
  105. // The hardware frame context containing the reconstructed frames.
  106. AVBufferRef *recon_frames_ref;
  107. AVHWFramesContext *recon_frames;
  108. // Pool of (reusable) bitstream output buffers.
  109. AVBufferPool *output_buffer_pool;
  110. // Global parameters which will be applied at the start of the
  111. // sequence (includes rate control parameters below).
  112. VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
  113. size_t global_params_size[MAX_GLOBAL_PARAMS];
  114. int nb_global_params;
  115. // Rate control parameters.
  116. struct {
  117. VAEncMiscParameterBuffer misc;
  118. VAEncMiscParameterRateControl rc;
  119. } rc_params;
  120. struct {
  121. VAEncMiscParameterBuffer misc;
  122. VAEncMiscParameterHRD hrd;
  123. } hrd_params;
  124. // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
  125. void *codec_sequence_params;
  126. // Per-sequence parameters found in the per-picture parameter
  127. // structure (VAEncPictureParameterBuffer*).
  128. void *codec_picture_params;
  129. // Current encoding window, in display (input) order.
  130. VAAPIEncodePicture *pic_start, *pic_end;
  131. // Next input order index (display order).
  132. int64_t input_order;
  133. // Number of frames that output is behind input.
  134. int64_t output_delay;
  135. // Number of frames decode output will need to be delayed.
  136. int64_t decode_delay;
  137. // Next output order index (encode order).
  138. int64_t output_order;
  139. enum {
  140. // All encode operations are done independently (synchronise
  141. // immediately after every operation).
  142. ISSUE_MODE_SERIALISE_EVERYTHING = 0,
  143. // Overlap as many operations as possible.
  144. ISSUE_MODE_MAXIMISE_THROUGHPUT,
  145. // Overlap operations only when satisfying parallel dependencies.
  146. ISSUE_MODE_MINIMISE_LATENCY,
  147. } issue_mode;
  148. // Timestamp handling.
  149. int64_t first_pts;
  150. int64_t dts_pts_diff;
  151. int64_t ts_ring[MAX_REORDER_DELAY * 3];
  152. // Frame type decision.
  153. int i_per_idr;
  154. int p_per_i;
  155. int b_per_p;
  156. int idr_counter;
  157. int i_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. int (*write_sequence_header)(AVCodecContext *avctx,
  191. char *data, size_t *data_len);
  192. int (*write_picture_header)(AVCodecContext *avctx,
  193. VAAPIEncodePicture *pic,
  194. char *data, size_t *data_len);
  195. int (*write_slice_header)(AVCodecContext *avctx,
  196. VAAPIEncodePicture *pic,
  197. VAAPIEncodeSlice *slice,
  198. char *data, size_t *data_len);
  199. // Fill an extra parameter structure, which will then be
  200. // passed to vaRenderPicture(). Will be called repeatedly
  201. // with increasing index argument until AVERROR_EOF is
  202. // returned.
  203. int (*write_extra_buffer)(AVCodecContext *avctx,
  204. VAAPIEncodePicture *pic,
  205. int index, int *type,
  206. char *data, size_t *data_len);
  207. // Write an extra packed header. Will be called repeatedly
  208. // with increasing index argument until AVERROR_EOF is
  209. // returned.
  210. int (*write_extra_header)(AVCodecContext *avctx,
  211. VAAPIEncodePicture *pic,
  212. int index, int *type,
  213. char *data, size_t *data_len);
  214. } VAAPIEncodeType;
  215. int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
  216. const AVFrame *input_image, int *got_packet);
  217. int ff_vaapi_encode_init(AVCodecContext *avctx);
  218. int ff_vaapi_encode_close(AVCodecContext *avctx);
  219. #endif /* AVCODEC_VAAPI_ENCODE_H */