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