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.

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