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.

218 lines
6.3KB

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