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.

295 lines
9.9KB

  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. VAAPIEncodeContext common;
  31. // User options.
  32. int loop_filter_level;
  33. int loop_filter_sharpness;
  34. // Derived settings.
  35. int q_idx_idr;
  36. int q_idx_p;
  37. int q_idx_b;
  38. // Stream state.
  39. // Reference direction for B-like frames:
  40. // 0 - most recent P/IDR frame is last.
  41. // 1 - most recent P frame is golden.
  42. int last_ref_dir;
  43. } VAAPIEncodeVP9Context;
  44. #define vseq_var(name) vseq->name, name
  45. #define vseq_field(name) vseq->seq_fields.bits.name, name
  46. #define vpic_var(name) vpic->name, name
  47. #define vpic_field(name) vpic->pic_fields.bits.name, name
  48. static int vaapi_encode_vp9_init_sequence_params(AVCodecContext *avctx)
  49. {
  50. VAAPIEncodeContext *ctx = avctx->priv_data;
  51. VAEncSequenceParameterBufferVP9 *vseq = ctx->codec_sequence_params;
  52. VAEncPictureParameterBufferVP9 *vpic = ctx->codec_picture_params;
  53. vseq->max_frame_width = avctx->width;
  54. vseq->max_frame_height = avctx->height;
  55. vseq->kf_auto = 0;
  56. if (!(ctx->va_rc_mode & VA_RC_CQP)) {
  57. vseq->bits_per_second = avctx->bit_rate;
  58. vseq->intra_period = avctx->gop_size;
  59. }
  60. vpic->frame_width_src = avctx->width;
  61. vpic->frame_height_src = avctx->height;
  62. vpic->frame_width_dst = avctx->width;
  63. vpic->frame_height_dst = avctx->height;
  64. return 0;
  65. }
  66. static int vaapi_encode_vp9_init_picture_params(AVCodecContext *avctx,
  67. VAAPIEncodePicture *pic)
  68. {
  69. VAAPIEncodeVP9Context *priv = avctx->priv_data;
  70. VAEncPictureParameterBufferVP9 *vpic = pic->codec_picture_params;
  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 = priv->loop_filter_level;
  149. vpic->sharpness_level = priv->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. VAAPIEncodeVP9Context *priv = avctx->priv_data;
  157. priv->q_idx_p = av_clip(avctx->global_quality, 0, VP9_MAX_QUANT);
  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, VP9_MAX_QUANT);
  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, VP9_MAX_QUANT);
  170. else
  171. priv->q_idx_b = priv->q_idx_p;
  172. return 0;
  173. }
  174. static const VAAPIEncodeProfile vaapi_encode_vp9_profiles[] = {
  175. { FF_PROFILE_VP9_0, 8, 3, 1, 1, VAProfileVP9Profile0 },
  176. { FF_PROFILE_VP9_2, 10, 3, 1, 1, VAProfileVP9Profile2 },
  177. { FF_PROFILE_UNKNOWN }
  178. };
  179. static const VAAPIEncodeType vaapi_encode_type_vp9 = {
  180. .profiles = vaapi_encode_vp9_profiles,
  181. .configure = &vaapi_encode_vp9_configure,
  182. .sequence_params_size = sizeof(VAEncSequenceParameterBufferVP9),
  183. .init_sequence_params = &vaapi_encode_vp9_init_sequence_params,
  184. .picture_params_size = sizeof(VAEncPictureParameterBufferVP9),
  185. .init_picture_params = &vaapi_encode_vp9_init_picture_params,
  186. };
  187. static av_cold int vaapi_encode_vp9_init(AVCodecContext *avctx)
  188. {
  189. VAAPIEncodeContext *ctx = avctx->priv_data;
  190. ctx->codec = &vaapi_encode_type_vp9;
  191. if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
  192. ctx->va_rc_mode = VA_RC_CQP;
  193. } else if (avctx->bit_rate > 0) {
  194. if (avctx->bit_rate == avctx->rc_max_rate)
  195. ctx->va_rc_mode = VA_RC_CBR;
  196. else
  197. ctx->va_rc_mode = VA_RC_VBR;
  198. } else {
  199. ctx->va_rc_mode = VA_RC_CQP;
  200. }
  201. // Packed headers are not currently supported.
  202. ctx->va_packed_headers = 0;
  203. // Surfaces must be aligned to superblock boundaries.
  204. ctx->surface_width = FFALIGN(avctx->width, 64);
  205. ctx->surface_height = FFALIGN(avctx->height, 64);
  206. return ff_vaapi_encode_init(avctx);
  207. }
  208. #define OFFSET(x) offsetof(VAAPIEncodeVP9Context, x)
  209. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  210. static const AVOption vaapi_encode_vp9_options[] = {
  211. VAAPI_ENCODE_COMMON_OPTIONS,
  212. { "loop_filter_level", "Loop filter level",
  213. OFFSET(loop_filter_level), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, 63, FLAGS },
  214. { "loop_filter_sharpness", "Loop filter sharpness",
  215. OFFSET(loop_filter_sharpness), AV_OPT_TYPE_INT, { .i64 = 4 }, 0, 15, FLAGS },
  216. { NULL },
  217. };
  218. static const AVCodecDefault vaapi_encode_vp9_defaults[] = {
  219. { "b", "0" },
  220. { "bf", "0" },
  221. { "g", "250" },
  222. { "global_quality", "100" },
  223. { NULL },
  224. };
  225. static const AVClass vaapi_encode_vp9_class = {
  226. .class_name = "vp9_vaapi",
  227. .item_name = av_default_item_name,
  228. .option = vaapi_encode_vp9_options,
  229. .version = LIBAVUTIL_VERSION_INT,
  230. };
  231. AVCodec ff_vp9_vaapi_encoder = {
  232. .name = "vp9_vaapi",
  233. .long_name = NULL_IF_CONFIG_SMALL("VP9 (VAAPI)"),
  234. .type = AVMEDIA_TYPE_VIDEO,
  235. .id = AV_CODEC_ID_VP9,
  236. .priv_data_size = sizeof(VAAPIEncodeVP9Context),
  237. .init = &vaapi_encode_vp9_init,
  238. .encode2 = &ff_vaapi_encode2,
  239. .close = &ff_vaapi_encode_close,
  240. .priv_class = &vaapi_encode_vp9_class,
  241. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
  242. .defaults = vaapi_encode_vp9_defaults,
  243. .pix_fmts = (const enum AVPixelFormat[]) {
  244. AV_PIX_FMT_VAAPI,
  245. AV_PIX_FMT_NONE,
  246. },
  247. .wrapper_name = "vaapi",
  248. };