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.

235 lines
7.0KB

  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. enum {
  43. // All encode operations are done independently.
  44. ISSUE_MODE_SERIALISE_EVERYTHING = 0,
  45. // Overlap as many operations as possible.
  46. ISSUE_MODE_MAXIMISE_THROUGHPUT,
  47. // Overlap operations only when satisfying parallel dependencies.
  48. ISSUE_MODE_MINIMISE_LATENCY,
  49. };
  50. typedef struct VAAPIEncodeSlice {
  51. void *priv_data;
  52. void *codec_slice_params;
  53. } VAAPIEncodeSlice;
  54. typedef struct VAAPIEncodePicture {
  55. struct VAAPIEncodePicture *next;
  56. int64_t display_order;
  57. int64_t encode_order;
  58. int64_t pts;
  59. int type;
  60. int input_available;
  61. int encode_issued;
  62. int encode_complete;
  63. AVFrame *input_image;
  64. VASurfaceID input_surface;
  65. AVFrame *recon_image;
  66. VASurfaceID recon_surface;
  67. int nb_param_buffers;
  68. VABufferID param_buffers[MAX_PARAM_BUFFERS];
  69. AVBufferRef *output_buffer_ref;
  70. VABufferID output_buffer;
  71. void *priv_data;
  72. void *codec_picture_params;
  73. int nb_refs;
  74. struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
  75. int nb_slices;
  76. VAAPIEncodeSlice *slices[MAX_PICTURE_SLICES];
  77. } VAAPIEncodePicture;
  78. typedef struct VAAPIEncodeContext {
  79. const AVClass *class;
  80. // Codec-specific hooks.
  81. const struct VAAPIEncodeType *codec;
  82. // Codec-specific state.
  83. void *priv_data;
  84. VAProfile va_profile;
  85. VAEntrypoint va_entrypoint;
  86. VAConfigID va_config;
  87. VAContextID va_context;
  88. int va_rc_mode;
  89. AVBufferRef *device_ref;
  90. AVHWDeviceContext *device;
  91. AVVAAPIDeviceContext *hwctx;
  92. AVBufferRef *input_frames_ref;
  93. AVHWFramesContext *input_frames;
  94. // Input size, set from input frames.
  95. int input_width;
  96. int input_height;
  97. // Aligned size, set by codec init, becomes hwframe size.
  98. int aligned_width;
  99. int aligned_height;
  100. int nb_recon_frames;
  101. AVBufferRef *recon_frames_ref;
  102. AVHWFramesContext *recon_frames;
  103. AVBufferPool *output_buffer_pool;
  104. VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
  105. int nb_config_attributes;
  106. VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
  107. size_t global_params_size[MAX_GLOBAL_PARAMS];
  108. int nb_global_params;
  109. // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
  110. void *codec_sequence_params;
  111. // Per-sequence parameters found in the per-picture parameter
  112. // structure (VAEncPictureParameterBuffer*).
  113. void *codec_picture_params;
  114. // Current encoding window, in display (input) order.
  115. VAAPIEncodePicture *pic_start, *pic_end;
  116. // Next input order index (display order).
  117. int64_t input_order;
  118. // Number of frames that output is behind input.
  119. int64_t output_delay;
  120. // Number of frames decode output will need to be delayed.
  121. int64_t decode_delay;
  122. // Next output order index (encode order).
  123. int64_t output_order;
  124. int issue_mode;
  125. // Timestamp handling.
  126. int64_t first_pts;
  127. int64_t dts_pts_diff;
  128. int64_t ts_ring[MAX_REORDER_DELAY * 3];
  129. // Frame type decision.
  130. int i_per_idr;
  131. int p_per_i;
  132. int b_per_p;
  133. int idr_counter;
  134. int i_counter;
  135. int p_counter;
  136. int end_of_stream;
  137. // Codec-local options are allocated to follow this structure in
  138. // memory (in the AVCodec definition, set priv_data_size to
  139. // sizeof(VAAPIEncodeContext) + sizeof(VAAPIEncodeFooOptions)).
  140. void *codec_options;
  141. char codec_options_data[0];
  142. } VAAPIEncodeContext;
  143. typedef struct VAAPIEncodeType {
  144. size_t priv_data_size;
  145. int (*init)(AVCodecContext *avctx);
  146. int (*close)(AVCodecContext *avctx);
  147. size_t sequence_params_size;
  148. size_t picture_params_size;
  149. size_t slice_params_size;
  150. int (*init_sequence_params)(AVCodecContext *avctx);
  151. int (*init_picture_params)(AVCodecContext *avctx,
  152. VAAPIEncodePicture *pic);
  153. int (*init_slice_params)(AVCodecContext *avctx,
  154. VAAPIEncodePicture *pic,
  155. VAAPIEncodeSlice *slice);
  156. int sequence_header_type;
  157. int picture_header_type;
  158. int slice_header_type;
  159. int (*write_sequence_header)(AVCodecContext *avctx,
  160. char *data, size_t *data_len);
  161. int (*write_picture_header)(AVCodecContext *avctx,
  162. VAAPIEncodePicture *pic,
  163. char *data, size_t *data_len);
  164. int (*write_slice_header)(AVCodecContext *avctx,
  165. VAAPIEncodePicture *pic,
  166. VAAPIEncodeSlice *slice,
  167. char *data, size_t *data_len);
  168. int (*write_extra_buffer)(AVCodecContext *avctx,
  169. VAAPIEncodePicture *pic,
  170. int index, int *type,
  171. char *data, size_t *data_len);
  172. int (*write_extra_header)(AVCodecContext *avctx,
  173. VAAPIEncodePicture *pic,
  174. int index, int *type,
  175. char *data, size_t *data_len);
  176. } VAAPIEncodeType;
  177. int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
  178. const AVFrame *input_image, int *got_packet);
  179. int ff_vaapi_encode_init(AVCodecContext *avctx,
  180. const VAAPIEncodeType *type);
  181. int ff_vaapi_encode_close(AVCodecContext *avctx);
  182. #endif /* AVCODEC_VAAPI_ENCODE_H */