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.

229 lines
6.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. MAX_OUTPUT_BUFFER_SIZE = 1024 * 1024,
  36. };
  37. enum {
  38. PICTURE_TYPE_IDR = 0,
  39. PICTURE_TYPE_I = 1,
  40. PICTURE_TYPE_P = 2,
  41. PICTURE_TYPE_B = 3,
  42. };
  43. enum {
  44. // All encode operations are done independently.
  45. ISSUE_MODE_SERIALISE_EVERYTHING = 0,
  46. // Overlap as many operations as possible.
  47. ISSUE_MODE_MAXIMISE_THROUGHPUT,
  48. // Overlap operations only when satisfying parallel dependencies.
  49. ISSUE_MODE_MINIMISE_LATENCY,
  50. };
  51. typedef struct VAAPIEncodeSlice {
  52. void *priv_data;
  53. void *codec_slice_params;
  54. } VAAPIEncodeSlice;
  55. typedef struct VAAPIEncodePicture {
  56. struct VAAPIEncodePicture *next;
  57. int64_t display_order;
  58. int64_t encode_order;
  59. int64_t pts;
  60. int type;
  61. int input_available;
  62. int encode_issued;
  63. int encode_complete;
  64. AVFrame *input_image;
  65. VASurfaceID input_surface;
  66. AVFrame *recon_image;
  67. VASurfaceID recon_surface;
  68. int nb_param_buffers;
  69. VABufferID param_buffers[MAX_PARAM_BUFFERS];
  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. VAConfigAttrib config_attributes[MAX_CONFIG_ATTRIBUTES];
  104. int nb_config_attributes;
  105. VAEncMiscParameterBuffer *global_params[MAX_GLOBAL_PARAMS];
  106. size_t global_params_size[MAX_GLOBAL_PARAMS];
  107. int nb_global_params;
  108. // Per-sequence parameter structure (VAEncSequenceParameterBuffer*).
  109. void *codec_sequence_params;
  110. // Per-sequence parameters found in the per-picture parameter
  111. // structure (VAEncPictureParameterBuffer*).
  112. void *codec_picture_params;
  113. // Current encoding window, in display (input) order.
  114. VAAPIEncodePicture *pic_start, *pic_end;
  115. // Next input order index (display order).
  116. int64_t input_order;
  117. // Number of frames that output is behind input.
  118. int64_t output_delay;
  119. // Number of frames decode output will need to be delayed.
  120. int64_t decode_delay;
  121. // Next output order index (encode order).
  122. int64_t output_order;
  123. int issue_mode;
  124. // Timestamp handling.
  125. int64_t first_pts;
  126. int64_t dts_pts_diff;
  127. int64_t ts_ring[MAX_REORDER_DELAY * 3];
  128. // Frame type decision.
  129. int i_per_idr;
  130. int p_per_i;
  131. int b_per_p;
  132. int idr_counter;
  133. int i_counter;
  134. int p_counter;
  135. int end_of_stream;
  136. // Codec-local options are allocated to follow this structure in
  137. // memory (in the AVCodec definition, set priv_data_size to
  138. // sizeof(VAAPIEncodeContext) + sizeof(VAAPIEncodeFooOptions)).
  139. void *codec_options;
  140. char codec_options_data[0];
  141. } VAAPIEncodeContext;
  142. typedef struct VAAPIEncodeType {
  143. size_t priv_data_size;
  144. int (*init)(AVCodecContext *avctx);
  145. int (*close)(AVCodecContext *avctx);
  146. size_t sequence_params_size;
  147. size_t picture_params_size;
  148. size_t slice_params_size;
  149. int (*init_sequence_params)(AVCodecContext *avctx);
  150. int (*init_picture_params)(AVCodecContext *avctx,
  151. VAAPIEncodePicture *pic);
  152. int (*init_slice_params)(AVCodecContext *avctx,
  153. VAAPIEncodePicture *pic,
  154. VAAPIEncodeSlice *slice);
  155. int sequence_header_type;
  156. int picture_header_type;
  157. int slice_header_type;
  158. int (*write_sequence_header)(AVCodecContext *avctx,
  159. char *data, size_t *data_len);
  160. int (*write_picture_header)(AVCodecContext *avctx,
  161. VAAPIEncodePicture *pic,
  162. char *data, size_t *data_len);
  163. int (*write_slice_header)(AVCodecContext *avctx,
  164. VAAPIEncodePicture *pic,
  165. VAAPIEncodeSlice *slice,
  166. char *data, size_t *data_len);
  167. int (*write_extra_buffer)(AVCodecContext *avctx,
  168. VAAPIEncodePicture *pic,
  169. int index, int *type,
  170. char *data, size_t *data_len);
  171. } VAAPIEncodeType;
  172. int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
  173. const AVFrame *input_image, int *got_packet);
  174. int ff_vaapi_encode_init(AVCodecContext *avctx,
  175. const VAAPIEncodeType *type);
  176. int ff_vaapi_encode_close(AVCodecContext *avctx);
  177. #endif /* AVCODEC_VAAPI_ENCODE_H */