qsvenc.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Intel MediaSDK QSV encoder utility functions
  3. *
  4. * copyright (c) 2013 Yukinori Yamazoe
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #ifndef AVCODEC_QSVENC_H
  23. #define AVCODEC_QSVENC_H
  24. #include <stdint.h>
  25. #include <sys/types.h>
  26. #include <mfx/mfxvideo.h>
  27. #include "libavutil/avutil.h"
  28. #include "libavutil/fifo.h"
  29. #include "avcodec.h"
  30. #include "hwconfig.h"
  31. #include "qsv_internal.h"
  32. #define QSV_HAVE_CO2 QSV_VERSION_ATLEAST(1, 6)
  33. #define QSV_HAVE_CO3 QSV_VERSION_ATLEAST(1, 11)
  34. #define QSV_HAVE_CO_VPS QSV_VERSION_ATLEAST(1, 17)
  35. #define QSV_HAVE_EXT_HEVC_TILES QSV_VERSION_ATLEAST(1, 13)
  36. #define QSV_HAVE_EXT_VP9_PARAM QSV_VERSION_ATLEAST(1, 26)
  37. #define QSV_HAVE_TRELLIS QSV_VERSION_ATLEAST(1, 8)
  38. #define QSV_HAVE_MAX_SLICE_SIZE QSV_VERSION_ATLEAST(1, 9)
  39. #define QSV_HAVE_BREF_TYPE QSV_VERSION_ATLEAST(1, 8)
  40. #define QSV_HAVE_LA QSV_VERSION_ATLEAST(1, 7)
  41. #define QSV_HAVE_LA_DS QSV_VERSION_ATLEAST(1, 8)
  42. #define QSV_HAVE_LA_HRD QSV_VERSION_ATLEAST(1, 11)
  43. #define QSV_HAVE_VDENC QSV_VERSION_ATLEAST(1, 15)
  44. #define QSV_HAVE_GPB QSV_VERSION_ATLEAST(1, 18)
  45. #if defined(_WIN32) || defined(__CYGWIN__)
  46. #define QSV_HAVE_AVBR QSV_VERSION_ATLEAST(1, 3)
  47. #define QSV_HAVE_ICQ QSV_VERSION_ATLEAST(1, 8)
  48. #define QSV_HAVE_VCM QSV_VERSION_ATLEAST(1, 8)
  49. #define QSV_HAVE_QVBR QSV_VERSION_ATLEAST(1, 11)
  50. #define QSV_HAVE_MF 0
  51. #else
  52. #define QSV_HAVE_AVBR 0
  53. #define QSV_HAVE_ICQ QSV_VERSION_ATLEAST(1, 28)
  54. #define QSV_HAVE_VCM 0
  55. #define QSV_HAVE_QVBR QSV_VERSION_ATLEAST(1, 28)
  56. #define QSV_HAVE_MF QSV_VERSION_ATLEAST(1, 25)
  57. #endif
  58. #if !QSV_HAVE_LA_DS
  59. #define MFX_LOOKAHEAD_DS_UNKNOWN 0
  60. #define MFX_LOOKAHEAD_DS_OFF 0
  61. #define MFX_LOOKAHEAD_DS_2x 0
  62. #define MFX_LOOKAHEAD_DS_4x 0
  63. #endif
  64. #define QSV_COMMON_OPTS \
  65. { "async_depth", "Maximum processing parallelism", OFFSET(qsv.async_depth), AV_OPT_TYPE_INT, { .i64 = ASYNC_DEPTH_DEFAULT }, 1, INT_MAX, VE }, \
  66. { "avbr_accuracy", "Accuracy of the AVBR ratecontrol", OFFSET(qsv.avbr_accuracy), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE }, \
  67. { "avbr_convergence", "Convergence of the AVBR ratecontrol", OFFSET(qsv.avbr_convergence), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE }, \
  68. { "preset", NULL, OFFSET(qsv.preset), AV_OPT_TYPE_INT, { .i64 = MFX_TARGETUSAGE_BALANCED }, MFX_TARGETUSAGE_BEST_QUALITY, MFX_TARGETUSAGE_BEST_SPEED, VE, "preset" }, \
  69. { "veryfast", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_BEST_SPEED }, INT_MIN, INT_MAX, VE, "preset" }, \
  70. { "faster", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_6 }, INT_MIN, INT_MAX, VE, "preset" }, \
  71. { "fast", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_5 }, INT_MIN, INT_MAX, VE, "preset" }, \
  72. { "medium", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_BALANCED }, INT_MIN, INT_MAX, VE, "preset" }, \
  73. { "slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_3 }, INT_MIN, INT_MAX, VE, "preset" }, \
  74. { "slower", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_2 }, INT_MIN, INT_MAX, VE, "preset" }, \
  75. { "veryslow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_BEST_QUALITY }, INT_MIN, INT_MAX, VE, "preset" }, \
  76. { "rdo", "Enable rate distortion optimization", OFFSET(qsv.rdo), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE }, \
  77. { "max_frame_size", "Maximum encoded frame size in bytes", OFFSET(qsv.max_frame_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, UINT16_MAX, VE }, \
  78. { "max_slice_size", "Maximum encoded slice size in bytes", OFFSET(qsv.max_slice_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, UINT16_MAX, VE }, \
  79. { "bitrate_limit", "Toggle bitrate limitations", OFFSET(qsv.bitrate_limit), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE }, \
  80. { "mbbrc", "MB level bitrate control", OFFSET(qsv.mbbrc), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE }, \
  81. { "extbrc", "Extended bitrate control", OFFSET(qsv.extbrc), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE }, \
  82. { "adaptive_i", "Adaptive I-frame placement", OFFSET(qsv.adaptive_i), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE }, \
  83. { "adaptive_b", "Adaptive B-frame placement", OFFSET(qsv.adaptive_b), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE }, \
  84. { "b_strategy", "Strategy to choose between I/P/B-frames", OFFSET(qsv.b_strategy), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE }, \
  85. { "forced_idr", "Forcing I frames as IDR frames", OFFSET(qsv.forced_idr), AV_OPT_TYPE_BOOL,{ .i64 = 0 }, 0, 1, VE }, \
  86. { "low_power", "enable low power mode(experimental: many limitations by mfx version, BRC modes, etc.)", OFFSET(qsv.low_power), AV_OPT_TYPE_BOOL, { .i64 = 0}, 0, 1, VE},\
  87. extern const AVCodecHWConfigInternal *ff_qsv_enc_hw_configs[];
  88. typedef int SetEncodeCtrlCB (AVCodecContext *avctx,
  89. const AVFrame *frame, mfxEncodeCtrl* enc_ctrl);
  90. typedef struct QSVEncContext {
  91. AVCodecContext *avctx;
  92. QSVFrame *work_frames;
  93. mfxSession session;
  94. QSVSession internal_qs;
  95. int packet_size;
  96. int width_align;
  97. int height_align;
  98. mfxVideoParam param;
  99. mfxFrameAllocRequest req;
  100. mfxExtCodingOption extco;
  101. #if QSV_HAVE_CO2
  102. mfxExtCodingOption2 extco2;
  103. #endif
  104. #if QSV_HAVE_CO3
  105. mfxExtCodingOption3 extco3;
  106. #endif
  107. #if QSV_HAVE_MF
  108. mfxExtMultiFrameParam extmfp;
  109. mfxExtMultiFrameControl extmfc;
  110. #endif
  111. #if QSV_HAVE_EXT_HEVC_TILES
  112. mfxExtHEVCTiles exthevctiles;
  113. #endif
  114. #if QSV_HAVE_EXT_VP9_PARAM
  115. mfxExtVP9Param extvp9param;
  116. #endif
  117. mfxExtOpaqueSurfaceAlloc opaque_alloc;
  118. mfxFrameSurface1 **opaque_surfaces;
  119. AVBufferRef *opaque_alloc_buf;
  120. mfxExtBuffer *extparam_internal[2 + QSV_HAVE_CO2 + QSV_HAVE_CO3 + (QSV_HAVE_MF * 2)];
  121. int nb_extparam_internal;
  122. mfxExtBuffer **extparam;
  123. AVFifoBuffer *async_fifo;
  124. QSVFramesContext frames_ctx;
  125. mfxVersion ver;
  126. int hevc_vps;
  127. // options set by the caller
  128. int async_depth;
  129. int idr_interval;
  130. int profile;
  131. int preset;
  132. int avbr_accuracy;
  133. int avbr_convergence;
  134. int pic_timing_sei;
  135. int look_ahead;
  136. int look_ahead_depth;
  137. int look_ahead_downsampling;
  138. int vcm;
  139. int rdo;
  140. int max_frame_size;
  141. int max_slice_size;
  142. int tile_cols;
  143. int tile_rows;
  144. int aud;
  145. int single_sei_nal_unit;
  146. int max_dec_frame_buffering;
  147. int bitrate_limit;
  148. int mbbrc;
  149. int extbrc;
  150. int adaptive_i;
  151. int adaptive_b;
  152. int b_strategy;
  153. int cavlc;
  154. int int_ref_type;
  155. int int_ref_cycle_size;
  156. int int_ref_qp_delta;
  157. int recovery_point_sei;
  158. int repeat_pps;
  159. int low_power;
  160. int gpb;
  161. int a53_cc;
  162. #if QSV_HAVE_MF
  163. int mfmode;
  164. #endif
  165. char *load_plugins;
  166. SetEncodeCtrlCB *set_encode_ctrl_cb;
  167. int forced_idr;
  168. } QSVEncContext;
  169. int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q);
  170. int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q,
  171. AVPacket *pkt, const AVFrame *frame, int *got_packet);
  172. int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q);
  173. #endif /* AVCODEC_QSVENC_H */