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.

258 lines
7.8KB

  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_vp8.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. #include "vp8.h"
  29. typedef struct VAAPIEncodeVP8Context {
  30. VAAPIEncodeContext common;
  31. // User options.
  32. int loop_filter_level;
  33. int loop_filter_sharpness;
  34. // Derived settings.
  35. int q_index_i;
  36. int q_index_p;
  37. } VAAPIEncodeVP8Context;
  38. #define vseq_var(name) vseq->name, name
  39. #define vseq_field(name) vseq->seq_fields.bits.name, name
  40. #define vpic_var(name) vpic->name, name
  41. #define vpic_field(name) vpic->pic_fields.bits.name, name
  42. static int vaapi_encode_vp8_init_sequence_params(AVCodecContext *avctx)
  43. {
  44. VAAPIEncodeContext *ctx = avctx->priv_data;
  45. VAEncSequenceParameterBufferVP8 *vseq = ctx->codec_sequence_params;
  46. vseq->frame_width = avctx->width;
  47. vseq->frame_height = avctx->height;
  48. vseq->frame_width_scale = 0;
  49. vseq->frame_height_scale = 0;
  50. vseq->error_resilient = 0;
  51. vseq->kf_auto = 0;
  52. if (!(ctx->va_rc_mode & VA_RC_CQP)) {
  53. vseq->bits_per_second = ctx->va_bit_rate;
  54. vseq->intra_period = ctx->gop_size;
  55. }
  56. return 0;
  57. }
  58. static int vaapi_encode_vp8_init_picture_params(AVCodecContext *avctx,
  59. VAAPIEncodePicture *pic)
  60. {
  61. VAAPIEncodeVP8Context *priv = avctx->priv_data;
  62. VAEncPictureParameterBufferVP8 *vpic = pic->codec_picture_params;
  63. int i;
  64. vpic->reconstructed_frame = pic->recon_surface;
  65. vpic->coded_buf = pic->output_buffer;
  66. switch (pic->type) {
  67. case PICTURE_TYPE_IDR:
  68. case PICTURE_TYPE_I:
  69. av_assert0(pic->nb_refs == 0);
  70. vpic->ref_flags.bits.force_kf = 1;
  71. vpic->ref_last_frame =
  72. vpic->ref_gf_frame =
  73. vpic->ref_arf_frame =
  74. VA_INVALID_SURFACE;
  75. break;
  76. case PICTURE_TYPE_P:
  77. av_assert0(pic->nb_refs == 1);
  78. vpic->ref_flags.bits.no_ref_last = 0;
  79. vpic->ref_flags.bits.no_ref_gf = 1;
  80. vpic->ref_flags.bits.no_ref_arf = 1;
  81. vpic->ref_last_frame =
  82. vpic->ref_gf_frame =
  83. vpic->ref_arf_frame =
  84. pic->refs[0]->recon_surface;
  85. break;
  86. default:
  87. av_assert0(0 && "invalid picture type");
  88. }
  89. vpic->pic_flags.bits.frame_type = (pic->type != PICTURE_TYPE_IDR);
  90. vpic->pic_flags.bits.show_frame = 1;
  91. vpic->pic_flags.bits.refresh_last = 1;
  92. vpic->pic_flags.bits.refresh_golden_frame = 1;
  93. vpic->pic_flags.bits.refresh_alternate_frame = 1;
  94. vpic->pic_flags.bits.version = 0;
  95. vpic->pic_flags.bits.loop_filter_type = 0;
  96. for (i = 0; i < 4; i++)
  97. vpic->loop_filter_level[i] = priv->loop_filter_level;
  98. vpic->sharpness_level = priv->loop_filter_sharpness;
  99. vpic->clamp_qindex_low = 0;
  100. vpic->clamp_qindex_high = 127;
  101. return 0;
  102. }
  103. static int vaapi_encode_vp8_write_quant_table(AVCodecContext *avctx,
  104. VAAPIEncodePicture *pic,
  105. int index, int *type,
  106. char *data, size_t *data_len)
  107. {
  108. VAAPIEncodeVP8Context *priv = avctx->priv_data;
  109. VAQMatrixBufferVP8 quant;
  110. int i, q;
  111. if (index > 0)
  112. return AVERROR_EOF;
  113. if (*data_len < sizeof(quant))
  114. return AVERROR(EINVAL);
  115. *type = VAQMatrixBufferType;
  116. *data_len = sizeof(quant);
  117. memset(&quant, 0, sizeof(quant));
  118. if (pic->type == PICTURE_TYPE_P)
  119. q = priv->q_index_p;
  120. else
  121. q = priv->q_index_i;
  122. for (i = 0; i < 4; i++)
  123. quant.quantization_index[i] = q;
  124. for (i = 0; i < 5; i++)
  125. quant.quantization_index_delta[i] = 0;
  126. memcpy(data, &quant, sizeof(quant));
  127. return 0;
  128. }
  129. static av_cold int vaapi_encode_vp8_configure(AVCodecContext *avctx)
  130. {
  131. VAAPIEncodeVP8Context *priv = avctx->priv_data;
  132. priv->q_index_p = av_clip(avctx->global_quality, 0, VP8_MAX_QUANT);
  133. if (avctx->i_quant_factor > 0.0)
  134. priv->q_index_i = av_clip((avctx->global_quality *
  135. avctx->i_quant_factor +
  136. avctx->i_quant_offset) + 0.5,
  137. 0, VP8_MAX_QUANT);
  138. else
  139. priv->q_index_i = priv->q_index_p;
  140. return 0;
  141. }
  142. static const VAAPIEncodeProfile vaapi_encode_vp8_profiles[] = {
  143. { 0 /* VP8 has no profiles */, 8, 3, 1, 1, VAProfileVP8Version0_3 },
  144. { FF_PROFILE_UNKNOWN }
  145. };
  146. static const VAAPIEncodeType vaapi_encode_type_vp8 = {
  147. .profiles = vaapi_encode_vp8_profiles,
  148. .configure = &vaapi_encode_vp8_configure,
  149. .sequence_params_size = sizeof(VAEncSequenceParameterBufferVP8),
  150. .init_sequence_params = &vaapi_encode_vp8_init_sequence_params,
  151. .picture_params_size = sizeof(VAEncPictureParameterBufferVP8),
  152. .init_picture_params = &vaapi_encode_vp8_init_picture_params,
  153. .write_extra_buffer = &vaapi_encode_vp8_write_quant_table,
  154. };
  155. static av_cold int vaapi_encode_vp8_init(AVCodecContext *avctx)
  156. {
  157. VAAPIEncodeContext *ctx = avctx->priv_data;
  158. ctx->codec = &vaapi_encode_type_vp8;
  159. // Packed headers are not currently supported.
  160. ctx->va_packed_headers = 0;
  161. ctx->surface_width = FFALIGN(avctx->width, 16);
  162. ctx->surface_height = FFALIGN(avctx->height, 16);
  163. return ff_vaapi_encode_init(avctx);
  164. }
  165. #define OFFSET(x) offsetof(VAAPIEncodeVP8Context, x)
  166. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  167. static const AVOption vaapi_encode_vp8_options[] = {
  168. VAAPI_ENCODE_COMMON_OPTIONS,
  169. { "loop_filter_level", "Loop filter level",
  170. OFFSET(loop_filter_level), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, 63, FLAGS },
  171. { "loop_filter_sharpness", "Loop filter sharpness",
  172. OFFSET(loop_filter_sharpness), AV_OPT_TYPE_INT, { .i64 = 4 }, 0, 15, FLAGS },
  173. { NULL },
  174. };
  175. static const AVCodecDefault vaapi_encode_vp8_defaults[] = {
  176. { "b", "0" },
  177. { "bf", "0" },
  178. { "g", "120" },
  179. { "global_quality", "40" },
  180. { "qmin", "-1" },
  181. { "qmax", "-1" },
  182. { NULL },
  183. };
  184. static const AVClass vaapi_encode_vp8_class = {
  185. .class_name = "vp8_vaapi",
  186. .item_name = av_default_item_name,
  187. .option = vaapi_encode_vp8_options,
  188. .version = LIBAVUTIL_VERSION_INT,
  189. };
  190. AVCodec ff_vp8_vaapi_encoder = {
  191. .name = "vp8_vaapi",
  192. .long_name = NULL_IF_CONFIG_SMALL("VP8 (VAAPI)"),
  193. .type = AVMEDIA_TYPE_VIDEO,
  194. .id = AV_CODEC_ID_VP8,
  195. .priv_data_size = sizeof(VAAPIEncodeVP8Context),
  196. .init = &vaapi_encode_vp8_init,
  197. .encode2 = &ff_vaapi_encode2,
  198. .close = &ff_vaapi_encode_close,
  199. .priv_class = &vaapi_encode_vp8_class,
  200. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
  201. .defaults = vaapi_encode_vp8_defaults,
  202. .pix_fmts = (const enum AVPixelFormat[]) {
  203. AV_PIX_FMT_VAAPI,
  204. AV_PIX_FMT_NONE,
  205. },
  206. .wrapper_name = "vaapi",
  207. };