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.

287 lines
9.7KB

  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 = ctx->va_bit_rate;
  58. vseq->intra_period = ctx->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. VAAPIEncodeContext *ctx = avctx->priv_data;
  70. VAAPIEncodeVP9Context *priv = avctx->priv_data;
  71. VAEncPictureParameterBufferVP9 *vpic = pic->codec_picture_params;
  72. int i;
  73. vpic->reconstructed_frame = pic->recon_surface;
  74. vpic->coded_buf = pic->output_buffer;
  75. switch (pic->type) {
  76. case PICTURE_TYPE_IDR:
  77. av_assert0(pic->nb_refs == 0);
  78. vpic->ref_flags.bits.force_kf = 1;
  79. vpic->refresh_frame_flags = 0x01;
  80. priv->last_ref_dir = 0;
  81. break;
  82. case PICTURE_TYPE_P:
  83. av_assert0(pic->nb_refs == 1);
  84. if (ctx->b_per_p > 0) {
  85. if (priv->last_ref_dir) {
  86. vpic->ref_flags.bits.ref_frame_ctrl_l0 = 2;
  87. vpic->ref_flags.bits.ref_gf_idx = 1;
  88. vpic->ref_flags.bits.ref_gf_sign_bias = 1;
  89. vpic->refresh_frame_flags = 0x01;
  90. } else {
  91. vpic->ref_flags.bits.ref_frame_ctrl_l0 = 1;
  92. vpic->ref_flags.bits.ref_last_idx = 0;
  93. vpic->ref_flags.bits.ref_last_sign_bias = 1;
  94. vpic->refresh_frame_flags = 0x02;
  95. }
  96. } else {
  97. vpic->ref_flags.bits.ref_frame_ctrl_l0 = 1;
  98. vpic->ref_flags.bits.ref_last_idx = 0;
  99. vpic->ref_flags.bits.ref_last_sign_bias = 1;
  100. vpic->refresh_frame_flags = 0x01;
  101. }
  102. break;
  103. case PICTURE_TYPE_B:
  104. av_assert0(pic->nb_refs == 2);
  105. if (priv->last_ref_dir) {
  106. vpic->ref_flags.bits.ref_frame_ctrl_l0 = 1;
  107. vpic->ref_flags.bits.ref_frame_ctrl_l1 = 2;
  108. vpic->ref_flags.bits.ref_last_idx = 0;
  109. vpic->ref_flags.bits.ref_last_sign_bias = 1;
  110. vpic->ref_flags.bits.ref_gf_idx = 1;
  111. vpic->ref_flags.bits.ref_gf_sign_bias = 0;
  112. } else {
  113. vpic->ref_flags.bits.ref_frame_ctrl_l0 = 2;
  114. vpic->ref_flags.bits.ref_frame_ctrl_l1 = 1;
  115. vpic->ref_flags.bits.ref_last_idx = 0;
  116. vpic->ref_flags.bits.ref_last_sign_bias = 0;
  117. vpic->ref_flags.bits.ref_gf_idx = 1;
  118. vpic->ref_flags.bits.ref_gf_sign_bias = 1;
  119. }
  120. vpic->refresh_frame_flags = 0x00;
  121. break;
  122. default:
  123. av_assert0(0 && "invalid picture type");
  124. }
  125. for (i = 0; i < FF_ARRAY_ELEMS(vpic->reference_frames); i++)
  126. vpic->reference_frames[i] = VA_INVALID_SURFACE;
  127. if (pic->type == PICTURE_TYPE_P) {
  128. av_assert0(pic->refs[0]);
  129. vpic->reference_frames[priv->last_ref_dir] =
  130. pic->refs[0]->recon_surface;
  131. } else if (pic->type == PICTURE_TYPE_B) {
  132. av_assert0(pic->refs[0] && pic->refs[1]);
  133. vpic->reference_frames[!priv->last_ref_dir] =
  134. pic->refs[0]->recon_surface;
  135. vpic->reference_frames[priv->last_ref_dir] =
  136. pic->refs[1]->recon_surface;
  137. }
  138. vpic->pic_flags.bits.frame_type = (pic->type != PICTURE_TYPE_IDR);
  139. vpic->pic_flags.bits.show_frame = pic->display_order <= pic->encode_order;
  140. if (pic->type == PICTURE_TYPE_IDR)
  141. vpic->luma_ac_qindex = priv->q_idx_idr;
  142. else if (pic->type == PICTURE_TYPE_P)
  143. vpic->luma_ac_qindex = priv->q_idx_p;
  144. else
  145. vpic->luma_ac_qindex = priv->q_idx_b;
  146. vpic->luma_dc_qindex_delta = 0;
  147. vpic->chroma_ac_qindex_delta = 0;
  148. vpic->chroma_dc_qindex_delta = 0;
  149. vpic->filter_level = priv->loop_filter_level;
  150. vpic->sharpness_level = priv->loop_filter_sharpness;
  151. if (ctx->b_per_p > 0 && pic->type == PICTURE_TYPE_P)
  152. priv->last_ref_dir = !priv->last_ref_dir;
  153. return 0;
  154. }
  155. static av_cold int vaapi_encode_vp9_configure(AVCodecContext *avctx)
  156. {
  157. VAAPIEncodeVP9Context *priv = avctx->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 VAAPIEncodeProfile vaapi_encode_vp9_profiles[] = {
  176. { FF_PROFILE_VP9_0, 8, 3, 1, 1, VAProfileVP9Profile0 },
  177. { FF_PROFILE_VP9_2, 10, 3, 1, 1, VAProfileVP9Profile2 },
  178. { FF_PROFILE_UNKNOWN }
  179. };
  180. static const VAAPIEncodeType vaapi_encode_type_vp9 = {
  181. .profiles = vaapi_encode_vp9_profiles,
  182. .configure = &vaapi_encode_vp9_configure,
  183. .sequence_params_size = sizeof(VAEncSequenceParameterBufferVP9),
  184. .init_sequence_params = &vaapi_encode_vp9_init_sequence_params,
  185. .picture_params_size = sizeof(VAEncPictureParameterBufferVP9),
  186. .init_picture_params = &vaapi_encode_vp9_init_picture_params,
  187. };
  188. static av_cold int vaapi_encode_vp9_init(AVCodecContext *avctx)
  189. {
  190. VAAPIEncodeContext *ctx = avctx->priv_data;
  191. ctx->codec = &vaapi_encode_type_vp9;
  192. // Packed headers are not currently supported.
  193. ctx->va_packed_headers = 0;
  194. // Surfaces must be aligned to superblock boundaries.
  195. ctx->surface_width = FFALIGN(avctx->width, 64);
  196. ctx->surface_height = FFALIGN(avctx->height, 64);
  197. return ff_vaapi_encode_init(avctx);
  198. }
  199. #define OFFSET(x) offsetof(VAAPIEncodeVP9Context, x)
  200. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  201. static const AVOption vaapi_encode_vp9_options[] = {
  202. VAAPI_ENCODE_COMMON_OPTIONS,
  203. { "loop_filter_level", "Loop filter level",
  204. OFFSET(loop_filter_level), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, 63, FLAGS },
  205. { "loop_filter_sharpness", "Loop filter sharpness",
  206. OFFSET(loop_filter_sharpness), AV_OPT_TYPE_INT, { .i64 = 4 }, 0, 15, FLAGS },
  207. { NULL },
  208. };
  209. static const AVCodecDefault vaapi_encode_vp9_defaults[] = {
  210. { "b", "0" },
  211. { "bf", "0" },
  212. { "g", "250" },
  213. { "global_quality", "100" },
  214. { "qmin", "-1" },
  215. { "qmax", "-1" },
  216. { NULL },
  217. };
  218. static const AVClass vaapi_encode_vp9_class = {
  219. .class_name = "vp9_vaapi",
  220. .item_name = av_default_item_name,
  221. .option = vaapi_encode_vp9_options,
  222. .version = LIBAVUTIL_VERSION_INT,
  223. };
  224. AVCodec ff_vp9_vaapi_encoder = {
  225. .name = "vp9_vaapi",
  226. .long_name = NULL_IF_CONFIG_SMALL("VP9 (VAAPI)"),
  227. .type = AVMEDIA_TYPE_VIDEO,
  228. .id = AV_CODEC_ID_VP9,
  229. .priv_data_size = sizeof(VAAPIEncodeVP9Context),
  230. .init = &vaapi_encode_vp9_init,
  231. .encode2 = &ff_vaapi_encode2,
  232. .close = &ff_vaapi_encode_close,
  233. .priv_class = &vaapi_encode_vp9_class,
  234. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
  235. .defaults = vaapi_encode_vp9_defaults,
  236. .pix_fmts = (const enum AVPixelFormat[]) {
  237. AV_PIX_FMT_VAAPI,
  238. AV_PIX_FMT_NONE,
  239. },
  240. .wrapper_name = "vaapi",
  241. };