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.

273 lines
9.2KB

  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 "hevcdec.h"
  30. #include "h2645_parse.h"
  31. #include "internal.h"
  32. #include "qsv.h"
  33. #include "qsv_internal.h"
  34. #include "qsvenc.h"
  35. enum LoadPlugin {
  36. LOAD_PLUGIN_NONE,
  37. LOAD_PLUGIN_HEVC_SW,
  38. LOAD_PLUGIN_HEVC_HW,
  39. };
  40. typedef struct QSVHEVCEncContext {
  41. AVClass *class;
  42. QSVEncContext qsv;
  43. int load_plugin;
  44. } QSVHEVCEncContext;
  45. static int generate_fake_vps(QSVEncContext *q, AVCodecContext *avctx)
  46. {
  47. GetByteContext gbc;
  48. PutByteContext pbc;
  49. GetBitContext gb;
  50. H2645NAL sps_nal = { NULL };
  51. HEVCSPS sps = { 0 };
  52. HEVCVPS vps = { 0 };
  53. uint8_t vps_buf[128], vps_rbsp_buf[128];
  54. uint8_t *new_extradata;
  55. unsigned int sps_id;
  56. int ret, i, type, vps_size;
  57. if (!avctx->extradata_size) {
  58. av_log(avctx, AV_LOG_ERROR, "No extradata returned from libmfx\n");
  59. return AVERROR_UNKNOWN;
  60. }
  61. /* parse the SPS */
  62. ret = ff_h2645_extract_rbsp(avctx->extradata + 4, avctx->extradata_size - 4, &sps_nal);
  63. if (ret < 0) {
  64. av_log(avctx, AV_LOG_ERROR, "Error unescaping the SPS buffer\n");
  65. return ret;
  66. }
  67. ret = init_get_bits8(&gb, sps_nal.data, sps_nal.size);
  68. if (ret < 0) {
  69. av_freep(&sps_nal.rbsp_buffer);
  70. return ret;
  71. }
  72. get_bits(&gb, 1);
  73. type = get_bits(&gb, 6);
  74. if (type != HEVC_NAL_SPS) {
  75. av_log(avctx, AV_LOG_ERROR, "Unexpected NAL type in the extradata: %d\n",
  76. type);
  77. av_freep(&sps_nal.rbsp_buffer);
  78. return AVERROR_INVALIDDATA;
  79. }
  80. get_bits(&gb, 9);
  81. ret = ff_hevc_parse_sps(&sps, &gb, &sps_id, 0, NULL, avctx);
  82. av_freep(&sps_nal.rbsp_buffer);
  83. if (ret < 0) {
  84. av_log(avctx, AV_LOG_ERROR, "Error parsing the SPS\n");
  85. return ret;
  86. }
  87. /* generate the VPS */
  88. vps.vps_max_layers = 1;
  89. vps.vps_max_sub_layers = sps.max_sub_layers;
  90. memcpy(&vps.ptl, &sps.ptl, sizeof(vps.ptl));
  91. vps.vps_sub_layer_ordering_info_present_flag = 1;
  92. for (i = 0; i < HEVC_MAX_SUB_LAYERS; i++) {
  93. vps.vps_max_dec_pic_buffering[i] = sps.temporal_layer[i].max_dec_pic_buffering;
  94. vps.vps_num_reorder_pics[i] = sps.temporal_layer[i].num_reorder_pics;
  95. vps.vps_max_latency_increase[i] = sps.temporal_layer[i].max_latency_increase;
  96. }
  97. vps.vps_num_layer_sets = 1;
  98. vps.vps_timing_info_present_flag = sps.vui.vui_timing_info_present_flag;
  99. vps.vps_num_units_in_tick = sps.vui.vui_num_units_in_tick;
  100. vps.vps_time_scale = sps.vui.vui_time_scale;
  101. vps.vps_poc_proportional_to_timing_flag = sps.vui.vui_poc_proportional_to_timing_flag;
  102. vps.vps_num_ticks_poc_diff_one = sps.vui.vui_num_ticks_poc_diff_one_minus1 + 1;
  103. /* generate the encoded RBSP form of the VPS */
  104. ret = ff_hevc_encode_nal_vps(&vps, sps.vps_id, vps_rbsp_buf, sizeof(vps_rbsp_buf));
  105. if (ret < 0) {
  106. av_log(avctx, AV_LOG_ERROR, "Error writing the VPS\n");
  107. return ret;
  108. }
  109. /* escape and add the startcode */
  110. bytestream2_init(&gbc, vps_rbsp_buf, ret);
  111. bytestream2_init_writer(&pbc, vps_buf, sizeof(vps_buf));
  112. bytestream2_put_be32(&pbc, 1); // startcode
  113. bytestream2_put_byte(&pbc, HEVC_NAL_VPS << 1); // NAL
  114. bytestream2_put_byte(&pbc, 1); // header
  115. while (bytestream2_get_bytes_left(&gbc)) {
  116. uint32_t b = bytestream2_peek_be24(&gbc);
  117. if (b <= 3) {
  118. bytestream2_put_be24(&pbc, 3);
  119. bytestream2_skip(&gbc, 2);
  120. } else
  121. bytestream2_put_byte(&pbc, bytestream2_get_byte(&gbc));
  122. }
  123. vps_size = bytestream2_tell_p(&pbc);
  124. new_extradata = av_mallocz(vps_size + avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  125. if (!new_extradata)
  126. return AVERROR(ENOMEM);
  127. memcpy(new_extradata, vps_buf, vps_size);
  128. memcpy(new_extradata + vps_size, avctx->extradata, avctx->extradata_size);
  129. av_freep(&avctx->extradata);
  130. avctx->extradata = new_extradata;
  131. avctx->extradata_size += vps_size;
  132. return 0;
  133. }
  134. static av_cold int qsv_enc_init(AVCodecContext *avctx)
  135. {
  136. QSVHEVCEncContext *q = avctx->priv_data;
  137. int ret;
  138. if (q->load_plugin != LOAD_PLUGIN_NONE) {
  139. static const char * const uid_hevcenc_sw = "2fca99749fdb49aeb121a5b63ef568f7";
  140. static const char * const uid_hevcenc_hw = "6fadc791a0c2eb479ab6dcd5ea9da347";
  141. if (q->qsv.load_plugins[0]) {
  142. av_log(avctx, AV_LOG_WARNING,
  143. "load_plugins is not empty, but load_plugin is not set to 'none'."
  144. "The load_plugin value will be ignored.\n");
  145. } else {
  146. av_freep(&q->qsv.load_plugins);
  147. if (q->load_plugin == LOAD_PLUGIN_HEVC_SW)
  148. q->qsv.load_plugins = av_strdup(uid_hevcenc_sw);
  149. else
  150. q->qsv.load_plugins = av_strdup(uid_hevcenc_hw);
  151. if (!q->qsv.load_plugins)
  152. return AVERROR(ENOMEM);
  153. }
  154. }
  155. ret = ff_qsv_enc_init(avctx, &q->qsv);
  156. if (ret < 0)
  157. return ret;
  158. ret = generate_fake_vps(&q->qsv, avctx);
  159. if (ret < 0) {
  160. ff_qsv_enc_close(avctx, &q->qsv);
  161. return ret;
  162. }
  163. return 0;
  164. }
  165. static int qsv_enc_frame(AVCodecContext *avctx, AVPacket *pkt,
  166. const AVFrame *frame, int *got_packet)
  167. {
  168. QSVHEVCEncContext *q = avctx->priv_data;
  169. return ff_qsv_encode(avctx, &q->qsv, pkt, frame, got_packet);
  170. }
  171. static av_cold int qsv_enc_close(AVCodecContext *avctx)
  172. {
  173. QSVHEVCEncContext *q = avctx->priv_data;
  174. return ff_qsv_enc_close(avctx, &q->qsv);
  175. }
  176. #define OFFSET(x) offsetof(QSVHEVCEncContext, x)
  177. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  178. static const AVOption options[] = {
  179. QSV_COMMON_OPTS
  180. { "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" },
  181. { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_NONE }, 0, 0, VE, "load_plugin" },
  182. { "hevc_sw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_HEVC_SW }, 0, 0, VE, "load_plugin" },
  183. { "hevc_hw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_HEVC_HW }, 0, 0, VE, "load_plugin" },
  184. { "load_plugins", "A :-separate list of hexadecimal plugin UIDs to load in an internal session",
  185. OFFSET(qsv.load_plugins), AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VE },
  186. { "profile", NULL, OFFSET(qsv.profile), AV_OPT_TYPE_INT, { .i64 = MFX_PROFILE_UNKNOWN }, 0, INT_MAX, VE, "profile" },
  187. { "unknown", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_UNKNOWN }, INT_MIN, INT_MAX, VE, "profile" },
  188. { "main", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_HEVC_MAIN }, INT_MIN, INT_MAX, VE, "profile" },
  189. { "main10", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_HEVC_MAIN10 }, INT_MIN, INT_MAX, VE, "profile" },
  190. { "mainsp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_HEVC_MAINSP }, INT_MIN, INT_MAX, VE, "profile" },
  191. { NULL },
  192. };
  193. static const AVClass class = {
  194. .class_name = "hevc_qsv encoder",
  195. .item_name = av_default_item_name,
  196. .option = options,
  197. .version = LIBAVUTIL_VERSION_INT,
  198. };
  199. static const AVCodecDefault qsv_enc_defaults[] = {
  200. { "b", "1M" },
  201. { "refs", "0" },
  202. // same as the x264 default
  203. { "g", "248" },
  204. { "bf", "8" },
  205. { "flags", "+cgop" },
  206. #if FF_API_PRIVATE_OPT
  207. { "b_strategy", "-1" },
  208. #endif
  209. { NULL },
  210. };
  211. AVCodec ff_hevc_qsv_encoder = {
  212. .name = "hevc_qsv",
  213. .long_name = NULL_IF_CONFIG_SMALL("HEVC (Intel Quick Sync Video acceleration)"),
  214. .priv_data_size = sizeof(QSVHEVCEncContext),
  215. .type = AVMEDIA_TYPE_VIDEO,
  216. .id = AV_CODEC_ID_HEVC,
  217. .init = qsv_enc_init,
  218. .encode2 = qsv_enc_frame,
  219. .close = qsv_enc_close,
  220. .capabilities = AV_CODEC_CAP_DELAY,
  221. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12,
  222. AV_PIX_FMT_P010,
  223. AV_PIX_FMT_QSV,
  224. AV_PIX_FMT_NONE },
  225. .priv_class = &class,
  226. .defaults = qsv_enc_defaults,
  227. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  228. };