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.

268 lines
9.1KB

  1. /*
  2. * Intel MediaSDK QSV based HEVC encoder
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdint.h>
  21. #include <sys/types.h>
  22. #include <mfx/mfxvideo.h>
  23. #include "libavutil/common.h"
  24. #include "libavutil/opt.h"
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. #include "get_bits.h"
  28. #include "hevc.h"
  29. #include "internal.h"
  30. #include "qsv.h"
  31. #include "qsv_internal.h"
  32. #include "qsvenc.h"
  33. enum LoadPlugin {
  34. LOAD_PLUGIN_NONE,
  35. LOAD_PLUGIN_HEVC_SW,
  36. LOAD_PLUGIN_HEVC_HW,
  37. };
  38. typedef struct QSVHEVCEncContext {
  39. AVClass *class;
  40. QSVEncContext qsv;
  41. int load_plugin;
  42. } QSVHEVCEncContext;
  43. static int generate_fake_vps(QSVEncContext *q, AVCodecContext *avctx)
  44. {
  45. GetByteContext gbc;
  46. PutByteContext pbc;
  47. GetBitContext gb;
  48. HEVCNAL sps_nal = { NULL };
  49. HEVCSPS sps = { 0 };
  50. HEVCVPS vps = { 0 };
  51. uint8_t vps_buf[128], vps_rbsp_buf[128];
  52. uint8_t *new_extradata;
  53. unsigned int sps_id;
  54. int ret, i, type, vps_size;
  55. if (!avctx->extradata_size) {
  56. av_log(avctx, AV_LOG_ERROR, "No extradata returned from libmfx\n");
  57. return AVERROR_UNKNOWN;
  58. }
  59. /* parse the SPS */
  60. ret = ff_hevc_extract_rbsp(avctx->extradata + 4, avctx->extradata_size - 4, &sps_nal);
  61. if (ret < 0) {
  62. av_log(avctx, AV_LOG_ERROR, "Error unescaping the SPS buffer\n");
  63. return ret;
  64. }
  65. ret = init_get_bits8(&gb, sps_nal.data, sps_nal.size);
  66. if (ret < 0) {
  67. av_freep(&sps_nal.rbsp_buffer);
  68. return ret;
  69. }
  70. get_bits(&gb, 1);
  71. type = get_bits(&gb, 6);
  72. if (type != NAL_SPS) {
  73. av_log(avctx, AV_LOG_ERROR, "Unexpected NAL type in the extradata: %d\n",
  74. type);
  75. av_freep(&sps_nal.rbsp_buffer);
  76. return AVERROR_INVALIDDATA;
  77. }
  78. get_bits(&gb, 9);
  79. ret = ff_hevc_parse_sps(&sps, &gb, &sps_id, 0, NULL, avctx);
  80. av_freep(&sps_nal.rbsp_buffer);
  81. if (ret < 0) {
  82. av_log(avctx, AV_LOG_ERROR, "Error parsing the SPS\n");
  83. return ret;
  84. }
  85. /* generate the VPS */
  86. vps.vps_max_layers = 1;
  87. vps.vps_max_sub_layers = sps.max_sub_layers;
  88. memcpy(&vps.ptl, &sps.ptl, sizeof(vps.ptl));
  89. vps.vps_sub_layer_ordering_info_present_flag = 1;
  90. for (i = 0; i < MAX_SUB_LAYERS; i++) {
  91. vps.vps_max_dec_pic_buffering[i] = sps.temporal_layer[i].max_dec_pic_buffering;
  92. vps.vps_num_reorder_pics[i] = sps.temporal_layer[i].num_reorder_pics;
  93. vps.vps_max_latency_increase[i] = sps.temporal_layer[i].max_latency_increase;
  94. }
  95. vps.vps_num_layer_sets = 1;
  96. vps.vps_timing_info_present_flag = sps.vui.vui_timing_info_present_flag;
  97. vps.vps_num_units_in_tick = sps.vui.vui_num_units_in_tick;
  98. vps.vps_time_scale = sps.vui.vui_time_scale;
  99. vps.vps_poc_proportional_to_timing_flag = sps.vui.vui_poc_proportional_to_timing_flag;
  100. vps.vps_num_ticks_poc_diff_one = sps.vui.vui_num_ticks_poc_diff_one_minus1 + 1;
  101. /* generate the encoded RBSP form of the VPS */
  102. ret = ff_hevc_encode_nal_vps(&vps, sps.vps_id, vps_rbsp_buf, sizeof(vps_rbsp_buf));
  103. if (ret < 0) {
  104. av_log(avctx, AV_LOG_ERROR, "Error writing the VPS\n");
  105. return ret;
  106. }
  107. /* escape and add the startcode */
  108. bytestream2_init(&gbc, vps_rbsp_buf, ret);
  109. bytestream2_init_writer(&pbc, vps_buf, sizeof(vps_buf));
  110. bytestream2_put_be32(&pbc, 1); // startcode
  111. bytestream2_put_byte(&pbc, NAL_VPS << 1); // NAL
  112. bytestream2_put_byte(&pbc, 1); // header
  113. while (bytestream2_get_bytes_left(&gbc)) {
  114. uint32_t b = bytestream2_peek_be24(&gbc);
  115. if (b <= 3) {
  116. bytestream2_put_be24(&pbc, 3);
  117. bytestream2_skip(&gbc, 2);
  118. } else
  119. bytestream2_put_byte(&pbc, bytestream2_get_byte(&gbc));
  120. }
  121. vps_size = bytestream2_tell_p(&pbc);
  122. new_extradata = av_mallocz(vps_size + avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  123. if (!new_extradata)
  124. return AVERROR(ENOMEM);
  125. memcpy(new_extradata, vps_buf, vps_size);
  126. memcpy(new_extradata + vps_size, avctx->extradata, avctx->extradata_size);
  127. av_freep(&avctx->extradata);
  128. avctx->extradata = new_extradata;
  129. avctx->extradata_size += vps_size;
  130. return 0;
  131. }
  132. static av_cold int qsv_enc_init(AVCodecContext *avctx)
  133. {
  134. QSVHEVCEncContext *q = avctx->priv_data;
  135. int ret;
  136. if (q->load_plugin != LOAD_PLUGIN_NONE) {
  137. static const char *uid_hevcenc_sw = "2fca99749fdb49aeb121a5b63ef568f7";
  138. static const char *uid_hevcenc_hw = "6fadc791a0c2eb479ab6dcd5ea9da347";
  139. if (q->qsv.load_plugins[0]) {
  140. av_log(avctx, AV_LOG_WARNING,
  141. "load_plugins is not empty, but load_plugin is not set to 'none'."
  142. "The load_plugin value will be ignored.\n");
  143. } else {
  144. av_freep(&q->qsv.load_plugins);
  145. if (q->load_plugin == LOAD_PLUGIN_HEVC_SW)
  146. q->qsv.load_plugins = av_strdup(uid_hevcenc_sw);
  147. else
  148. q->qsv.load_plugins = av_strdup(uid_hevcenc_hw);
  149. if (!q->qsv.load_plugins)
  150. return AVERROR(ENOMEM);
  151. }
  152. }
  153. ret = ff_qsv_enc_init(avctx, &q->qsv);
  154. if (ret < 0)
  155. return ret;
  156. ret = generate_fake_vps(&q->qsv, avctx);
  157. if (ret < 0) {
  158. ff_qsv_enc_close(avctx, &q->qsv);
  159. return ret;
  160. }
  161. return 0;
  162. }
  163. static int qsv_enc_frame(AVCodecContext *avctx, AVPacket *pkt,
  164. const AVFrame *frame, int *got_packet)
  165. {
  166. QSVHEVCEncContext *q = avctx->priv_data;
  167. return ff_qsv_encode(avctx, &q->qsv, pkt, frame, got_packet);
  168. }
  169. static av_cold int qsv_enc_close(AVCodecContext *avctx)
  170. {
  171. QSVHEVCEncContext *q = avctx->priv_data;
  172. return ff_qsv_enc_close(avctx, &q->qsv);
  173. }
  174. #define OFFSET(x) offsetof(QSVHEVCEncContext, x)
  175. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  176. static const AVOption options[] = {
  177. QSV_COMMON_OPTS
  178. { "load_plugin", "A user plugin to load in an internal session", OFFSET(load_plugin), AV_OPT_TYPE_INT, { .i64 = LOAD_PLUGIN_HEVC_SW }, LOAD_PLUGIN_NONE, LOAD_PLUGIN_HEVC_HW, VE, "load_plugin" },
  179. { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_NONE }, 0, 0, VE, "load_plugin" },
  180. { "hevc_sw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_HEVC_SW }, 0, 0, VE, "load_plugin" },
  181. { "hevc_hw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_HEVC_HW }, 0, 0, VE, "load_plugin" },
  182. { "load_plugins", "A :-separate list of hexadecimal plugin UIDs to load in an internal session",
  183. OFFSET(qsv.load_plugins), AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VE },
  184. { "profile", NULL, OFFSET(qsv.profile), AV_OPT_TYPE_INT, { .i64 = MFX_PROFILE_UNKNOWN }, 0, INT_MAX, VE, "profile" },
  185. { "unknown", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_UNKNOWN }, INT_MIN, INT_MAX, VE, "profile" },
  186. { "main", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_HEVC_MAIN }, INT_MIN, INT_MAX, VE, "profile" },
  187. { "main10", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_HEVC_MAIN10 }, INT_MIN, INT_MAX, VE, "profile" },
  188. { "mainsp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_HEVC_MAINSP }, INT_MIN, INT_MAX, VE, "profile" },
  189. { NULL },
  190. };
  191. static const AVClass class = {
  192. .class_name = "hevc_qsv encoder",
  193. .item_name = av_default_item_name,
  194. .option = options,
  195. .version = LIBAVUTIL_VERSION_INT,
  196. };
  197. static const AVCodecDefault qsv_enc_defaults[] = {
  198. { "b", "1M" },
  199. { "refs", "0" },
  200. // same as the x264 default
  201. { "g", "250" },
  202. { "bf", "3" },
  203. { "flags", "+cgop" },
  204. { "b_strategy", "-1" },
  205. { NULL },
  206. };
  207. AVCodec ff_hevc_qsv_encoder = {
  208. .name = "hevc_qsv",
  209. .long_name = NULL_IF_CONFIG_SMALL("HEVC (Intel Quick Sync Video acceleration)"),
  210. .priv_data_size = sizeof(QSVHEVCEncContext),
  211. .type = AVMEDIA_TYPE_VIDEO,
  212. .id = AV_CODEC_ID_HEVC,
  213. .init = qsv_enc_init,
  214. .encode2 = qsv_enc_frame,
  215. .close = qsv_enc_close,
  216. .capabilities = AV_CODEC_CAP_DELAY,
  217. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12,
  218. AV_PIX_FMT_QSV,
  219. AV_PIX_FMT_NONE },
  220. .priv_class = &class,
  221. .defaults = qsv_enc_defaults,
  222. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  223. };