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.

271 lines
8.7KB

  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. // The required size of surfaces. This is probably the input
  83. // size (AVCodecContext.width|height) aligned up to whatever
  84. // block size is required by the codec.
  85. int surface_width;
  86. int surface_height;
  87. // Everything above this point must be set before calling
  88. // ff_vaapi_encode_init().
  89. // Codec-specific state.
  90. void *priv_data;
  91. // Configuration attributes to use when creating va_config.
  92. VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
  93. int nb_config_attributes;
  94. VAConfigID va_config;
  95. VAContextID va_context;
  96. AVBufferRef *device_ref;
  97. AVHWDeviceContext *device;
  98. AVVAAPIDeviceContext *hwctx;
  99. // The hardware frame context containing the input frames.
  100. AVBufferRef *input_frames_ref;
  101. AVHWFramesContext *input_frames;
  102. // The hardware frame context containing the reconstructed frames.
  103. AVBufferRef *recon_frames_ref;
  104. AVHWFramesContext *recon_frames;
  105. // Pool of (reusable) bitstream output buffers.
  106. AVBufferPool *output_buffer_pool;
  107. // Global parameters which will be applied at the start of the
  108. // sequence (includes rate control parameters below).
  109. VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
  110. size_t global_params_size[MAX_GLOBAL_PARAMS];
  111. int nb_global_params;
  112. // Rate control parameters.
  113. struct {
  114. VAEncMiscParameterBuffer misc;
  115. VAEncMiscParameterRateControl rc;
  116. } rc_params;
  117. struct {
  118. VAEncMiscParameterBuffer misc;
  119. VAEncMiscParameterHRD hrd;
  120. } hrd_params;
  121. // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
  122. void *codec_sequence_params;
  123. // Per-sequence parameters found in the per-picture parameter
  124. // structure (VAEncPictureParameterBuffer*).
  125. void *codec_picture_params;
  126. // Current encoding window, in display (input) order.
  127. VAAPIEncodePicture *pic_start, *pic_end;
  128. // Next input order index (display order).
  129. int64_t input_order;
  130. // Number of frames that output is behind input.
  131. int64_t output_delay;
  132. // Number of frames decode output will need to be delayed.
  133. int64_t decode_delay;
  134. // Next output order index (encode order).
  135. int64_t output_order;
  136. enum {
  137. // All encode operations are done independently (synchronise
  138. // immediately after every operation).
  139. ISSUE_MODE_SERIALISE_EVERYTHING = 0,
  140. // Overlap as many operations as possible.
  141. ISSUE_MODE_MAXIMISE_THROUGHPUT,
  142. // Overlap operations only when satisfying parallel dependencies.
  143. ISSUE_MODE_MINIMISE_LATENCY,
  144. } issue_mode;
  145. // Timestamp handling.
  146. int64_t first_pts;
  147. int64_t dts_pts_diff;
  148. int64_t ts_ring[MAX_REORDER_DELAY * 3];
  149. // Frame type decision.
  150. int i_per_idr;
  151. int p_per_i;
  152. int b_per_p;
  153. int idr_counter;
  154. int i_counter;
  155. int p_counter;
  156. int end_of_stream;
  157. // Codec-local options are allocated to follow this structure in
  158. // memory (in the AVCodec definition, set priv_data_size to
  159. // sizeof(VAAPIEncodeContext) + sizeof(VAAPIEncodeFooOptions)).
  160. void *codec_options;
  161. char codec_options_data[0];
  162. } VAAPIEncodeContext;
  163. typedef struct VAAPIEncodeType {
  164. size_t priv_data_size;
  165. // Perform any extra codec-specific configuration after the
  166. // codec context is initialised (set up the private data and
  167. // add any necessary global parameters).
  168. int (*configure)(AVCodecContext *avctx);
  169. // The size of the parameter structures:
  170. // sizeof(VAEnc{type}ParameterBuffer{codec}).
  171. size_t sequence_params_size;
  172. size_t picture_params_size;
  173. size_t slice_params_size;
  174. // Fill the parameter structures.
  175. int (*init_sequence_params)(AVCodecContext *avctx);
  176. int (*init_picture_params)(AVCodecContext *avctx,
  177. VAAPIEncodePicture *pic);
  178. int (*init_slice_params)(AVCodecContext *avctx,
  179. VAAPIEncodePicture *pic,
  180. VAAPIEncodeSlice *slice);
  181. // The type used by the packed header: this should look like
  182. // VAEncPackedHeader{something}.
  183. int sequence_header_type;
  184. int picture_header_type;
  185. int slice_header_type;
  186. // Write the packed header data to the provided buffer.
  187. int (*write_sequence_header)(AVCodecContext *avctx,
  188. char *data, size_t *data_len);
  189. int (*write_picture_header)(AVCodecContext *avctx,
  190. VAAPIEncodePicture *pic,
  191. char *data, size_t *data_len);
  192. int (*write_slice_header)(AVCodecContext *avctx,
  193. VAAPIEncodePicture *pic,
  194. VAAPIEncodeSlice *slice,
  195. char *data, size_t *data_len);
  196. // Fill an extra parameter structure, which will then be
  197. // passed to vaRenderPicture(). Will be called repeatedly
  198. // with increasing index argument until AVERROR_EOF is
  199. // returned.
  200. int (*write_extra_buffer)(AVCodecContext *avctx,
  201. VAAPIEncodePicture *pic,
  202. int index, int *type,
  203. char *data, size_t *data_len);
  204. // Write an extra packed header. Will be called repeatedly
  205. // with increasing index argument until AVERROR_EOF is
  206. // returned.
  207. int (*write_extra_header)(AVCodecContext *avctx,
  208. VAAPIEncodePicture *pic,
  209. int index, int *type,
  210. char *data, size_t *data_len);
  211. } VAAPIEncodeType;
  212. int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
  213. const AVFrame *input_image, int *got_packet);
  214. int ff_vaapi_encode_init(AVCodecContext *avctx);
  215. int ff_vaapi_encode_close(AVCodecContext *avctx);
  216. #endif /* AVCODEC_VAAPI_ENCODE_H */