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.

315 lines
11KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <va/va.h>
  19. #include <va/va_enc_vp9.h>
  20. #include "libavutil/avassert.h"
  21. #include "libavutil/common.h"
  22. #include "libavutil/internal.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/pixfmt.h"
  25. #include "avcodec.h"
  26. #include "internal.h"
  27. #include "vaapi_encode.h"
  28. #define VP9_MAX_QUANT 255
  29. typedef struct VAAPIEncodeVP9Context {
  30. int q_idx_idr;
  31. int q_idx_p;
  32. int q_idx_b;
  33. // Reference direction for B-like frames:
  34. // 0 - most recent P/IDR frame is last.
  35. // 1 - most recent P frame is golden.
  36. int last_ref_dir;
  37. } VAAPIEncodeVP9Context;
  38. typedef struct VAAPIEncodeVP9Options {
  39. int loop_filter_level;
  40. int loop_filter_sharpness;
  41. } VAAPIEncodeVP9Options;
  42. #define vseq_var(name) vseq->name, name
  43. #define vseq_field(name) vseq->seq_fields.bits.name, name
  44. #define vpic_var(name) vpic->name, name
  45. #define vpic_field(name) vpic->pic_fields.bits.name, name
  46. static int vaapi_encode_vp9_init_sequence_params(AVCodecContext *avctx)
  47. {
  48. VAAPIEncodeContext *ctx = avctx->priv_data;
  49. VAEncSequenceParameterBufferVP9 *vseq = ctx->codec_sequence_params;
  50. VAEncPictureParameterBufferVP9 *vpic = ctx->codec_picture_params;
  51. vseq->max_frame_width = avctx->width;
  52. vseq->max_frame_height = avctx->height;
  53. vseq->kf_auto = 0;
  54. if (!(ctx->va_rc_mode & VA_RC_CQP)) {
  55. vseq->bits_per_second = avctx->bit_rate;
  56. vseq->intra_period = avctx->gop_size;
  57. }
  58. vpic->frame_width_src = avctx->width;
  59. vpic->frame_height_src = avctx->height;
  60. vpic->frame_width_dst = avctx->width;
  61. vpic->frame_height_dst = avctx->height;
  62. return 0;
  63. }
  64. static int vaapi_encode_vp9_init_picture_params(AVCodecContext *avctx,
  65. VAAPIEncodePicture *pic)
  66. {
  67. VAAPIEncodeContext *ctx = avctx->priv_data;
  68. VAEncPictureParameterBufferVP9 *vpic = pic->codec_picture_params;
  69. VAAPIEncodeVP9Context *priv = ctx->priv_data;
  70. VAAPIEncodeVP9Options *opt = ctx->codec_options;
  71. int i;
  72. vpic->reconstructed_frame = pic->recon_surface;
  73. vpic->coded_buf = pic->output_buffer;
  74. switch (pic->type) {
  75. case PICTURE_TYPE_IDR:
  76. av_assert0(pic->nb_refs == 0);
  77. vpic->ref_flags.bits.force_kf = 1;
  78. vpic->refresh_frame_flags = 0x01;
  79. priv->last_ref_dir = 0;
  80. break;
  81. case PICTURE_TYPE_P:
  82. av_assert0(pic->nb_refs == 1);
  83. if (avctx->max_b_frames > 0) {
  84. if (priv->last_ref_dir) {
  85. vpic->ref_flags.bits.ref_frame_ctrl_l0 = 2;
  86. vpic->ref_flags.bits.ref_gf_idx = 1;
  87. vpic->ref_flags.bits.ref_gf_sign_bias = 1;
  88. vpic->refresh_frame_flags = 0x01;
  89. } else {
  90. vpic->ref_flags.bits.ref_frame_ctrl_l0 = 1;
  91. vpic->ref_flags.bits.ref_last_idx = 0;
  92. vpic->ref_flags.bits.ref_last_sign_bias = 1;
  93. vpic->refresh_frame_flags = 0x02;
  94. }
  95. } else {
  96. vpic->ref_flags.bits.ref_frame_ctrl_l0 = 1;
  97. vpic->ref_flags.bits.ref_last_idx = 0;
  98. vpic->ref_flags.bits.ref_last_sign_bias = 1;
  99. vpic->refresh_frame_flags = 0x01;
  100. }
  101. break;
  102. case PICTURE_TYPE_B:
  103. av_assert0(pic->nb_refs == 2);
  104. if (priv->last_ref_dir) {
  105. vpic->ref_flags.bits.ref_frame_ctrl_l0 = 1;
  106. vpic->ref_flags.bits.ref_frame_ctrl_l1 = 2;
  107. vpic->ref_flags.bits.ref_last_idx = 0;
  108. vpic->ref_flags.bits.ref_last_sign_bias = 1;
  109. vpic->ref_flags.bits.ref_gf_idx = 1;
  110. vpic->ref_flags.bits.ref_gf_sign_bias = 0;
  111. } else {
  112. vpic->ref_flags.bits.ref_frame_ctrl_l0 = 2;
  113. vpic->ref_flags.bits.ref_frame_ctrl_l1 = 1;
  114. vpic->ref_flags.bits.ref_last_idx = 0;
  115. vpic->ref_flags.bits.ref_last_sign_bias = 0;
  116. vpic->ref_flags.bits.ref_gf_idx = 1;
  117. vpic->ref_flags.bits.ref_gf_sign_bias = 1;
  118. }
  119. vpic->refresh_frame_flags = 0x00;
  120. break;
  121. default:
  122. av_assert0(0 && "invalid picture type");
  123. }
  124. for (i = 0; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++)
  125. vpic->reference_frames[i] = VA_INVALID_SURFACE;
  126. if (pic->type == PICTURE_TYPE_P) {
  127. av_assert0(pic->refs[0]);
  128. vpic->reference_frames[priv->last_ref_dir] =
  129. pic->refs[0]->recon_surface;
  130. } else if (pic->type == PICTURE_TYPE_B) {
  131. av_assert0(pic->refs[0] && pic->refs[1]);
  132. vpic->reference_frames[!priv->last_ref_dir] =
  133. pic->refs[0]->recon_surface;
  134. vpic->reference_frames[priv->last_ref_dir] =
  135. pic->refs[1]->recon_surface;
  136. }
  137. vpic->pic_flags.bits.frame_type = (pic->type != PICTURE_TYPE_IDR);
  138. vpic->pic_flags.bits.show_frame = pic->display_order <= pic->encode_order;
  139. if (pic->type == PICTURE_TYPE_IDR)
  140. vpic->luma_ac_qindex = priv->q_idx_idr;
  141. else if (pic->type == PICTURE_TYPE_P)
  142. vpic->luma_ac_qindex = priv->q_idx_p;
  143. else
  144. vpic->luma_ac_qindex = priv->q_idx_b;
  145. vpic->luma_dc_qindex_delta = 0;
  146. vpic->chroma_ac_qindex_delta = 0;
  147. vpic->chroma_dc_qindex_delta = 0;
  148. vpic->filter_level = opt->loop_filter_level;
  149. vpic->sharpness_level = opt->loop_filter_sharpness;
  150. if (avctx->max_b_frames > 0 && pic->type == PICTURE_TYPE_P)
  151. priv->last_ref_dir = !priv->last_ref_dir;
  152. return 0;
  153. }
  154. static av_cold int vaapi_encode_vp9_configure(AVCodecContext *avctx)
  155. {
  156. VAAPIEncodeContext *ctx = avctx->priv_data;
  157. VAAPIEncodeVP9Context *priv = ctx->priv_data;
  158. priv->q_idx_p = av_clip(avctx->global_quality, 0, VP9_MAX_QUANT);
  159. if (avctx->i_quant_factor > 0.0)
  160. priv->q_idx_idr = av_clip((avctx->global_quality *
  161. avctx->i_quant_factor +
  162. avctx->i_quant_offset) + 0.5,
  163. 0, VP9_MAX_QUANT);
  164. else
  165. priv->q_idx_idr = priv->q_idx_p;
  166. if (avctx->b_quant_factor > 0.0)
  167. priv->q_idx_b = av_clip((avctx->global_quality *
  168. avctx->b_quant_factor +
  169. avctx->b_quant_offset) + 0.5,
  170. 0, VP9_MAX_QUANT);
  171. else
  172. priv->q_idx_b = priv->q_idx_p;
  173. return 0;
  174. }
  175. static const VAAPIEncodeType vaapi_encode_type_vp9 = {
  176. .configure = &vaapi_encode_vp9_configure,
  177. .priv_data_size = sizeof(VAAPIEncodeVP9Context),
  178. .sequence_params_size = sizeof(VAEncSequenceParameterBufferVP9),
  179. .init_sequence_params = &vaapi_encode_vp9_init_sequence_params,
  180. .picture_params_size = sizeof(VAEncPictureParameterBufferVP9),
  181. .init_picture_params = &vaapi_encode_vp9_init_picture_params,
  182. };
  183. static av_cold int vaapi_encode_vp9_init(AVCodecContext *avctx)
  184. {
  185. VAAPIEncodeContext *ctx = avctx->priv_data;
  186. ctx->codec = &vaapi_encode_type_vp9;
  187. switch (avctx->profile) {
  188. case FF_PROFILE_VP9_0:
  189. case FF_PROFILE_UNKNOWN:
  190. ctx->va_profile = VAProfileVP9Profile0;
  191. ctx->va_rt_format = VA_RT_FORMAT_YUV420;
  192. break;
  193. case FF_PROFILE_VP9_1:
  194. av_log(avctx, AV_LOG_ERROR, "VP9 profile 1 is not "
  195. "supported.\n");
  196. return AVERROR_PATCHWELCOME;
  197. case FF_PROFILE_VP9_2:
  198. ctx->va_profile = VAProfileVP9Profile2;
  199. ctx->va_rt_format = VA_RT_FORMAT_YUV420_10BPP;
  200. break;
  201. case FF_PROFILE_VP9_3:
  202. av_log(avctx, AV_LOG_ERROR, "VP9 profile 3 is not "
  203. "supported.\n");
  204. return AVERROR_PATCHWELCOME;
  205. default:
  206. av_log(avctx, AV_LOG_ERROR, "Unknown VP9 profile %d.\n",
  207. avctx->profile);
  208. return AVERROR(EINVAL);
  209. }
  210. ctx->va_entrypoint = VAEntrypointEncSlice;
  211. if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
  212. ctx->va_rc_mode = VA_RC_CQP;
  213. } else if (avctx->bit_rate > 0) {
  214. if (avctx->bit_rate == avctx->rc_max_rate)
  215. ctx->va_rc_mode = VA_RC_CBR;
  216. else
  217. ctx->va_rc_mode = VA_RC_VBR;
  218. } else {
  219. ctx->va_rc_mode = VA_RC_CQP;
  220. }
  221. // Packed headers are not currently supported.
  222. ctx->va_packed_headers = 0;
  223. // Surfaces must be aligned to superblock boundaries.
  224. ctx->surface_width = FFALIGN(avctx->width, 64);
  225. ctx->surface_height = FFALIGN(avctx->height, 64);
  226. return ff_vaapi_encode_init(avctx);
  227. }
  228. #define OFFSET(x) (offsetof(VAAPIEncodeContext, codec_options_data) + \
  229. offsetof(VAAPIEncodeVP9Options, x))
  230. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  231. static const AVOption vaapi_encode_vp9_options[] = {
  232. { "loop_filter_level", "Loop filter level",
  233. OFFSET(loop_filter_level), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, 63, FLAGS },
  234. { "loop_filter_sharpness", "Loop filter sharpness",
  235. OFFSET(loop_filter_sharpness), AV_OPT_TYPE_INT, { .i64 = 4 }, 0, 15, FLAGS },
  236. { NULL },
  237. };
  238. static const AVCodecDefault vaapi_encode_vp9_defaults[] = {
  239. { "profile", "0" },
  240. { "b", "0" },
  241. { "bf", "0" },
  242. { "g", "250" },
  243. { "global_quality", "100" },
  244. { NULL },
  245. };
  246. static const AVClass vaapi_encode_vp9_class = {
  247. .class_name = "vp9_vaapi",
  248. .item_name = av_default_item_name,
  249. .option = vaapi_encode_vp9_options,
  250. .version = LIBAVUTIL_VERSION_INT,
  251. };
  252. AVCodec ff_vp9_vaapi_encoder = {
  253. .name = "vp9_vaapi",
  254. .long_name = NULL_IF_CONFIG_SMALL("VP9 (VAAPI)"),
  255. .type = AVMEDIA_TYPE_VIDEO,
  256. .id = AV_CODEC_ID_VP9,
  257. .priv_data_size = (sizeof(VAAPIEncodeContext) +
  258. sizeof(VAAPIEncodeVP9Options)),
  259. .init = &vaapi_encode_vp9_init,
  260. .encode2 = &ff_vaapi_encode2,
  261. .close = &ff_vaapi_encode_close,
  262. .priv_class = &vaapi_encode_vp9_class,
  263. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
  264. .defaults = vaapi_encode_vp9_defaults,
  265. .pix_fmts = (const enum AVPixelFormat[]) {
  266. AV_PIX_FMT_VAAPI,
  267. AV_PIX_FMT_NONE,
  268. },
  269. .wrapper_name = "vaapi",
  270. };