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.

312 lines
11KB

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