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.

269 lines
8.3KB

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