1 /*
2  * Intel MediaSDK QSV encoder utility functions
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 
23 #ifndef AVCODEC_QSVENC_H
24 #define AVCODEC_QSVENC_H
25 
26 #include <stdint.h>
27 #include <sys/types.h>
28 
29 #include <mfx/mfxvideo.h>
30 
31 #include "libavutil/avutil.h"
32 #include "libavutil/fifo.h"
33 
34 #include "avcodec.h"
35 #include "hwconfig.h"
36 #include "qsv_internal.h"
37 
38 #define QSV_HAVE_CO2 QSV_VERSION_ATLEAST(1, 6)
39 #define QSV_HAVE_CO3 QSV_VERSION_ATLEAST(1, 11)
40 #define QSV_HAVE_CO_VPS  QSV_VERSION_ATLEAST(1, 17)
41 
42 #define QSV_HAVE_EXT_HEVC_TILES QSV_VERSION_ATLEAST(1, 13)
43 #define QSV_HAVE_EXT_VP9_PARAM QSV_VERSION_ATLEAST(1, 26)
44 
45 #define QSV_HAVE_TRELLIS QSV_VERSION_ATLEAST(1, 8)
46 #define QSV_HAVE_MAX_SLICE_SIZE QSV_VERSION_ATLEAST(1, 9)
47 #define QSV_HAVE_BREF_TYPE      QSV_VERSION_ATLEAST(1, 8)
48 
49 #define QSV_HAVE_LA     QSV_VERSION_ATLEAST(1, 7)
50 #define QSV_HAVE_LA_DS  QSV_VERSION_ATLEAST(1, 8)
51 #define QSV_HAVE_LA_HRD QSV_VERSION_ATLEAST(1, 11)
52 #define QSV_HAVE_VDENC  QSV_VERSION_ATLEAST(1, 15)
53 
54 #define QSV_HAVE_GPB    QSV_VERSION_ATLEAST(1, 18)
55 
56 #if defined(_WIN32) || defined(__CYGWIN__)
57 #define QSV_HAVE_AVBR   QSV_VERSION_ATLEAST(1, 3)
58 #define QSV_HAVE_ICQ    QSV_VERSION_ATLEAST(1, 8)
59 #define QSV_HAVE_VCM    QSV_VERSION_ATLEAST(1, 8)
60 #define QSV_HAVE_QVBR   QSV_VERSION_ATLEAST(1, 11)
61 #define QSV_HAVE_MF     0
62 #else
63 #define QSV_HAVE_AVBR   0
64 #define QSV_HAVE_ICQ    QSV_VERSION_ATLEAST(1, 28)
65 #define QSV_HAVE_VCM    0
66 #define QSV_HAVE_QVBR   QSV_VERSION_ATLEAST(1, 28)
67 #define QSV_HAVE_MF     QSV_VERSION_ATLEAST(1, 25)
68 #endif
69 
70 #if !QSV_HAVE_LA_DS
71 #define MFX_LOOKAHEAD_DS_UNKNOWN 0
72 #define MFX_LOOKAHEAD_DS_OFF 0
73 #define MFX_LOOKAHEAD_DS_2x 0
74 #define MFX_LOOKAHEAD_DS_4x 0
75 #endif
76 
77 #define QSV_COMMON_OPTS \
78 { "async_depth", "Maximum processing parallelism", OFFSET(qsv.async_depth), AV_OPT_TYPE_INT, { .i64 = ASYNC_DEPTH_DEFAULT }, 1, INT_MAX, VE },                          \
79 { "avbr_accuracy",    "Accuracy of the AVBR ratecontrol",    OFFSET(qsv.avbr_accuracy),    AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },                             \
80 { "avbr_convergence", "Convergence of the AVBR ratecontrol", OFFSET(qsv.avbr_convergence), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },                             \
81 { "preset", NULL, OFFSET(qsv.preset), AV_OPT_TYPE_INT, { .i64 = MFX_TARGETUSAGE_BALANCED }, MFX_TARGETUSAGE_BEST_QUALITY, MFX_TARGETUSAGE_BEST_SPEED,   VE, "preset" }, \
82 { "veryfast",    NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_BEST_SPEED  },   INT_MIN, INT_MAX, VE, "preset" },                                                \
83 { "faster",      NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_6  },            INT_MIN, INT_MAX, VE, "preset" },                                                \
84 { "fast",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_5  },            INT_MIN, INT_MAX, VE, "preset" },                                                \
85 { "medium",      NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_BALANCED  },     INT_MIN, INT_MAX, VE, "preset" },                                                \
86 { "slow",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_3  },            INT_MIN, INT_MAX, VE, "preset" },                                                \
87 { "slower",      NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_2  },            INT_MIN, INT_MAX, VE, "preset" },                                                \
88 { "veryslow",    NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_BEST_QUALITY  }, INT_MIN, INT_MAX, VE, "preset" },                                                \
89 { "rdo",            "Enable rate distortion optimization",    OFFSET(qsv.rdo),            AV_OPT_TYPE_INT, { .i64 = -1 }, -1,          1, VE },                         \
90 { "max_frame_size", "Maximum encoded frame size in bytes",    OFFSET(qsv.max_frame_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, UINT16_MAX, VE },                         \
91 { "max_slice_size", "Maximum encoded slice size in bytes",    OFFSET(qsv.max_slice_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, UINT16_MAX, VE },                         \
92 { "bitrate_limit",  "Toggle bitrate limitations",             OFFSET(qsv.bitrate_limit),  AV_OPT_TYPE_INT, { .i64 = -1 }, -1,          1, VE },                         \
93 { "mbbrc",          "MB level bitrate control",               OFFSET(qsv.mbbrc),          AV_OPT_TYPE_INT, { .i64 = -1 }, -1,          1, VE },                         \
94 { "extbrc",         "Extended bitrate control",               OFFSET(qsv.extbrc),         AV_OPT_TYPE_INT, { .i64 = -1 }, -1,          1, VE },                         \
95 { "adaptive_i",     "Adaptive I-frame placement",             OFFSET(qsv.adaptive_i),     AV_OPT_TYPE_INT, { .i64 = -1 }, -1,          1, VE },                         \
96 { "adaptive_b",     "Adaptive B-frame placement",             OFFSET(qsv.adaptive_b),     AV_OPT_TYPE_INT, { .i64 = -1 }, -1,          1, VE },                         \
97 { "b_strategy",     "Strategy to choose between I/P/B-frames", OFFSET(qsv.b_strategy),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1,          1, VE },                         \
98 { "forced_idr",     "Forcing I frames as IDR frames",         OFFSET(qsv.forced_idr),     AV_OPT_TYPE_BOOL,{ .i64 = 0  },  0,          1, VE },                         \
99 { "low_power", "enable low power mode(experimental: many limitations by mfx version, BRC modes, etc.)", OFFSET(qsv.low_power), AV_OPT_TYPE_BOOL, { .i64 = 0}, 0, 1, VE},\
100 
101 extern const AVCodecHWConfigInternal *const ff_qsv_enc_hw_configs[];
102 
103 typedef int SetEncodeCtrlCB (AVCodecContext *avctx,
104                              const AVFrame *frame, mfxEncodeCtrl* enc_ctrl);
105 typedef struct QSVEncContext {
106     AVCodecContext *avctx;
107 
108     QSVFrame *work_frames;
109 
110     mfxSession session;
111     QSVSession internal_qs;
112 
113     int packet_size;
114     int width_align;
115     int height_align;
116 
117     mfxVideoParam param;
118     mfxFrameAllocRequest req;
119 
120     mfxExtCodingOption  extco;
121 #if QSV_HAVE_CO2
122     mfxExtCodingOption2 extco2;
123 #endif
124 #if QSV_HAVE_CO3
125     mfxExtCodingOption3 extco3;
126 #endif
127 #if QSV_HAVE_MF
128     mfxExtMultiFrameParam   extmfp;
129     mfxExtMultiFrameControl extmfc;
130 #endif
131 #if QSV_HAVE_EXT_HEVC_TILES
132     mfxExtHEVCTiles exthevctiles;
133 #endif
134 #if QSV_HAVE_EXT_VP9_PARAM
135     mfxExtVP9Param  extvp9param;
136 #endif
137 
138     mfxExtOpaqueSurfaceAlloc opaque_alloc;
139     mfxFrameSurface1       **opaque_surfaces;
140     AVBufferRef             *opaque_alloc_buf;
141 
142     mfxExtBuffer  *extparam_internal[2 + QSV_HAVE_CO2 + QSV_HAVE_CO3 + (QSV_HAVE_MF * 2)];
143     int         nb_extparam_internal;
144 
145     mfxExtBuffer **extparam;
146 
147     AVFifoBuffer *async_fifo;
148 
149     QSVFramesContext frames_ctx;
150 
151     mfxVersion          ver;
152 
153     int hevc_vps;
154 
155     // options set by the caller
156     int async_depth;
157     int idr_interval;
158     int profile;
159     int preset;
160     int avbr_accuracy;
161     int avbr_convergence;
162     int pic_timing_sei;
163     int look_ahead;
164     int look_ahead_depth;
165     int look_ahead_downsampling;
166     int vcm;
167     int rdo;
168     int max_frame_size;
169     int max_slice_size;
170 
171     int tile_cols;
172     int tile_rows;
173 
174     int aud;
175 
176     int single_sei_nal_unit;
177     int max_dec_frame_buffering;
178 
179     int bitrate_limit;
180     int mbbrc;
181     int extbrc;
182     int adaptive_i;
183     int adaptive_b;
184     int b_strategy;
185     int cavlc;
186 
187     int int_ref_type;
188     int int_ref_cycle_size;
189     int int_ref_qp_delta;
190     int recovery_point_sei;
191 
192     int repeat_pps;
193     int low_power;
194     int gpb;
195 
196     int a53_cc;
197 
198 #if QSV_HAVE_MF
199     int mfmode;
200 #endif
201     char *load_plugins;
202     SetEncodeCtrlCB *set_encode_ctrl_cb;
203     int forced_idr;
204 } QSVEncContext;
205 
206 int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q);
207 
208 int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q,
209                   AVPacket *pkt, const AVFrame *frame, int *got_packet);
210 
211 int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q);
212 
213 #endif /* AVCODEC_QSVENC_H */
214