qsvenc_h264.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Intel MediaSDK QSV based H.264 encoder
  3. *
  4. * copyright (c) 2013 Yukinori Yamazoe
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <stdint.h>
  23. #include <sys/types.h>
  24. #include <mfxvideo.h>
  25. #include "libavutil/common.h"
  26. #include "libavutil/opt.h"
  27. #include "avcodec.h"
  28. #include "codec_internal.h"
  29. #include "qsv.h"
  30. #include "qsvenc.h"
  31. #include "atsc_a53.h"
  32. typedef struct QSVH264EncContext {
  33. AVClass *class;
  34. QSVEncContext qsv;
  35. } QSVH264EncContext;
  36. static int qsv_h264_set_encode_ctrl(AVCodecContext *avctx,
  37. const AVFrame *frame, mfxEncodeCtrl* enc_ctrl)
  38. {
  39. QSVH264EncContext *qh264 = avctx->priv_data;
  40. QSVEncContext *q = &qh264->qsv;
  41. if (q->a53_cc && frame) {
  42. mfxPayload* payload;
  43. mfxU8* sei_data;
  44. size_t sei_size;
  45. int res;
  46. res = ff_alloc_a53_sei(frame, sizeof(mfxPayload) + 2, (void**)&payload, &sei_size);
  47. if (res < 0 || !payload)
  48. return res;
  49. sei_data = (mfxU8*)(payload + 1);
  50. // SEI header
  51. sei_data[0] = 4;
  52. sei_data[1] = (mfxU8)sei_size; // size of SEI data
  53. // SEI data filled in by ff_alloc_a53_sei
  54. payload->BufSize = sei_size + 2;
  55. payload->NumBit = payload->BufSize * 8;
  56. payload->Type = 4;
  57. payload->Data = sei_data;
  58. enc_ctrl->NumExtParam = 0;
  59. enc_ctrl->NumPayload = 1;
  60. enc_ctrl->Payload[0] = payload;
  61. }
  62. return 0;
  63. }
  64. static av_cold int qsv_enc_init(AVCodecContext *avctx)
  65. {
  66. QSVH264EncContext *q = avctx->priv_data;
  67. q->qsv.set_encode_ctrl_cb = qsv_h264_set_encode_ctrl;
  68. return ff_qsv_enc_init(avctx, &q->qsv);
  69. }
  70. static int qsv_enc_frame(AVCodecContext *avctx, AVPacket *pkt,
  71. const AVFrame *frame, int *got_packet)
  72. {
  73. QSVH264EncContext *q = avctx->priv_data;
  74. return ff_qsv_encode(avctx, &q->qsv, pkt, frame, got_packet);
  75. }
  76. static av_cold int qsv_enc_close(AVCodecContext *avctx)
  77. {
  78. QSVH264EncContext *q = avctx->priv_data;
  79. return ff_qsv_enc_close(avctx, &q->qsv);
  80. }
  81. #define OFFSET(x) offsetof(QSVH264EncContext, x)
  82. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  83. static const AVOption options[] = {
  84. QSV_COMMON_OPTS
  85. QSV_OPTION_RDO
  86. QSV_OPTION_MAX_FRAME_SIZE
  87. QSV_OPTION_MAX_SLICE_SIZE
  88. QSV_OPTION_BITRATE_LIMIT
  89. QSV_OPTION_MBBRC
  90. QSV_OPTION_EXTBRC
  91. QSV_OPTION_ADAPTIVE_I
  92. QSV_OPTION_ADAPTIVE_B
  93. QSV_OPTION_P_STRATEGY
  94. QSV_OPTION_B_STRATEGY
  95. QSV_OPTION_DBLK_IDC
  96. QSV_OPTION_LOW_DELAY_BRC
  97. QSV_OPTION_MAX_MIN_QP
  98. QSV_OPTION_SCENARIO
  99. QSV_OPTION_AVBR
  100. QSV_OPTION_SKIP_FRAME
  101. { "cavlc", "Enable CAVLC", OFFSET(qsv.cavlc), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
  102. #if QSV_HAVE_VCM
  103. { "vcm", "Use the video conferencing mode ratecontrol", OFFSET(qsv.vcm), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
  104. #endif
  105. { "idr_interval", "Distance (in I-frames) between IDR frames", OFFSET(qsv.idr_interval), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
  106. { "pic_timing_sei", "Insert picture timing SEI with pic_struct_syntax element", OFFSET(qsv.pic_timing_sei), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, VE },
  107. { "single_sei_nal_unit", "Put all the SEI messages into one NALU", OFFSET(qsv.single_sei_nal_unit), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
  108. { "max_dec_frame_buffering", "Maximum number of frames buffered in the DPB", OFFSET(qsv.max_dec_frame_buffering), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, UINT16_MAX, VE },
  109. { "look_ahead", "Use VBR algorithm with look ahead", OFFSET(qsv.look_ahead), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
  110. { "look_ahead_depth", "Depth of look ahead in number frames", OFFSET(qsv.look_ahead_depth), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, VE },
  111. { "look_ahead_downsampling", "Downscaling factor for the frames saved for the lookahead analysis", OFFSET(qsv.look_ahead_downsampling),
  112. AV_OPT_TYPE_INT, { .i64 = MFX_LOOKAHEAD_DS_UNKNOWN }, MFX_LOOKAHEAD_DS_UNKNOWN, MFX_LOOKAHEAD_DS_4x, VE, "look_ahead_downsampling" },
  113. { "unknown" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_LOOKAHEAD_DS_UNKNOWN }, INT_MIN, INT_MAX, VE, "look_ahead_downsampling" },
  114. { "auto" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_LOOKAHEAD_DS_UNKNOWN }, INT_MIN, INT_MAX, VE, "look_ahead_downsampling" },
  115. { "off" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_LOOKAHEAD_DS_OFF }, INT_MIN, INT_MAX, VE, "look_ahead_downsampling" },
  116. { "2x" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_LOOKAHEAD_DS_2x }, INT_MIN, INT_MAX, VE, "look_ahead_downsampling" },
  117. { "4x" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_LOOKAHEAD_DS_4x }, INT_MIN, INT_MAX, VE, "look_ahead_downsampling" },
  118. { "int_ref_type", "Intra refresh type. B frames should be set to 0.", OFFSET(qsv.int_ref_type), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, UINT16_MAX, VE, "int_ref_type" },
  119. { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .flags = VE, "int_ref_type" },
  120. { "vertical", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .flags = VE, "int_ref_type" },
  121. { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, .flags = VE, "int_ref_type" },
  122. { "slice" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 3 }, .flags = VE, "int_ref_type" },
  123. { "int_ref_cycle_size", "Number of frames in the intra refresh cycle", OFFSET(qsv.int_ref_cycle_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, UINT16_MAX, VE },
  124. { "int_ref_qp_delta", "QP difference for the refresh MBs", OFFSET(qsv.int_ref_qp_delta), AV_OPT_TYPE_INT, { .i64 = INT16_MIN }, INT16_MIN, INT16_MAX, VE },
  125. { "recovery_point_sei", "Insert recovery point SEI messages", OFFSET(qsv.recovery_point_sei), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE },
  126. { "int_ref_cycle_dist", "Distance between the beginnings of the intra-refresh cycles in frames", OFFSET(qsv.int_ref_cycle_dist), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT16_MAX, VE },
  127. { "profile", NULL, OFFSET(qsv.profile), AV_OPT_TYPE_INT, { .i64 = MFX_PROFILE_UNKNOWN }, 0, INT_MAX, VE, "profile" },
  128. { "unknown" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_UNKNOWN }, INT_MIN, INT_MAX, VE, "profile" },
  129. { "baseline", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_AVC_BASELINE }, INT_MIN, INT_MAX, VE, "profile" },
  130. { "main" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_AVC_MAIN }, INT_MIN, INT_MAX, VE, "profile" },
  131. { "high" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_AVC_HIGH }, INT_MIN, INT_MAX, VE, "profile" },
  132. { "a53cc" , "Use A53 Closed Captions (if available)", OFFSET(qsv.a53_cc), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, VE},
  133. { "aud", "Insert the Access Unit Delimiter NAL", OFFSET(qsv.aud), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE},
  134. #if QSV_HAVE_MF
  135. { "mfmode", "Multi-Frame Mode", OFFSET(qsv.mfmode), AV_OPT_TYPE_INT, { .i64 = MFX_MF_AUTO }, MFX_MF_DEFAULT, MFX_MF_AUTO, VE, "mfmode"},
  136. { "off" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_MF_DISABLED }, INT_MIN, INT_MAX, VE, "mfmode" },
  137. { "auto" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_MF_AUTO }, INT_MIN, INT_MAX, VE, "mfmode" },
  138. #endif
  139. { "repeat_pps", "repeat pps for every frame", OFFSET(qsv.repeat_pps), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
  140. { NULL },
  141. };
  142. static const AVClass class = {
  143. .class_name = "h264_qsv encoder",
  144. .item_name = av_default_item_name,
  145. .option = options,
  146. .version = LIBAVUTIL_VERSION_INT,
  147. };
  148. static const FFCodecDefault qsv_enc_defaults[] = {
  149. { "b", "1M" },
  150. { "refs", "0" },
  151. { "g", "-1" },
  152. { "bf", "-1" },
  153. { "qmin", "-1" },
  154. { "qmax", "-1" },
  155. { "trellis", "-1" },
  156. { "flags", "+cgop" },
  157. { NULL },
  158. };
  159. const FFCodec ff_h264_qsv_encoder = {
  160. .p.name = "h264_qsv",
  161. CODEC_LONG_NAME("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (Intel Quick Sync Video acceleration)"),
  162. .priv_data_size = sizeof(QSVH264EncContext),
  163. .p.type = AVMEDIA_TYPE_VIDEO,
  164. .p.id = AV_CODEC_ID_H264,
  165. .init = qsv_enc_init,
  166. FF_CODEC_ENCODE_CB(qsv_enc_frame),
  167. .close = qsv_enc_close,
  168. .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HYBRID,
  169. .p.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12,
  170. AV_PIX_FMT_P010,
  171. AV_PIX_FMT_QSV,
  172. AV_PIX_FMT_NONE },
  173. .p.priv_class = &class,
  174. .defaults = qsv_enc_defaults,
  175. .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
  176. FF_CODEC_CAP_INIT_CLEANUP,
  177. .p.wrapper_name = "qsv",
  178. .hw_configs = ff_qsv_enc_hw_configs,
  179. };