FFmpeg
Loading...
Searching...
No Matches
qsvenc_av1.c
Go to the documentation of this file.
1/*
2 * Intel MediaSDK QSV based AV1 encoder
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21
22#include <stdint.h>
23#include <sys/types.h>
24
25#include "libavutil/common.h"
27#include "libavutil/mem.h"
28#include "libavutil/opt.h"
29
30#include "avcodec.h"
31#include "codec_internal.h"
32#include "bsf.h"
33#include "qsv.h"
34#include "qsvenc.h"
35
41
43 const AVFrame *frame, mfxEncodeCtrl *enc_ctrl)
44{
45 QSVAV1EncContext *q = avctx->priv_data;
47
48 if (!frame || !QSV_RUNTIME_VERSION_ATLEAST(q->qsv.ver, 2, 11))
49 return 0;
50
52 if (sd) {
54 if (mdm->has_primaries && mdm->has_luminance) {
55 const int chroma_den = 1 << 16;
56 const int max_luma_den = 1 << 8;
57 const int min_luma_den = 1 << 14;
58 mfxExtMasteringDisplayColourVolume *mdcv = av_mallocz(sizeof(*mdcv));
59 if (!mdcv)
60 return AVERROR(ENOMEM);
61
62 mdcv->Header.BufferId = MFX_EXTBUFF_MASTERING_DISPLAY_COLOUR_VOLUME;
63 mdcv->Header.BufferSz = sizeof(*mdcv);
64
65 for (int i = 0; i < 3; i++) {
66 mdcv->DisplayPrimariesX[i] =
67 av_rescale(mdm->display_primaries[i][0].num, chroma_den,
68 mdm->display_primaries[i][0].den);
69 mdcv->DisplayPrimariesY[i] =
70 av_rescale(mdm->display_primaries[i][1].num, chroma_den,
71 mdm->display_primaries[i][1].den);
72 }
73
74 mdcv->WhitePointX =
75 av_rescale(mdm->white_point[0].num, chroma_den,
76 mdm->white_point[0].den);
77 mdcv->WhitePointY =
78 av_rescale(mdm->white_point[1].num, chroma_den,
79 mdm->white_point[1].den);
80
81 mdcv->MaxDisplayMasteringLuminance =
82 av_rescale(mdm->max_luminance.num, max_luma_den,
83 mdm->max_luminance.den);
84 mdcv->MinDisplayMasteringLuminance =
85 av_rescale(mdm->min_luminance.num, min_luma_den,
86 mdm->min_luminance.den);
87
88 enc_ctrl->ExtParam[enc_ctrl->NumExtParam++] = (mfxExtBuffer *)mdcv;
89 }
90 }
91
93 if (sd) {
95 mfxExtContentLightLevelInfo *clli = av_mallocz(sizeof(*clli));
96 if (!clli)
97 return AVERROR(ENOMEM);
98
99 clli->Header.BufferId = MFX_EXTBUFF_CONTENT_LIGHT_LEVEL_INFO;
100 clli->Header.BufferSz = sizeof(*clli);
101
102 clli->MaxContentLightLevel = clm->MaxCLL;
103 clli->MaxPicAverageLightLevel = clm->MaxFALL;
104
105 enc_ctrl->ExtParam[enc_ctrl->NumExtParam++] = (mfxExtBuffer *)clli;
106 }
107
108 return 0;
109}
110
112{
113 QSVAV1EncContext *q = avctx->priv_data;
114 int ret;
115
116 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
117 const AVBitStreamFilter *filter = av_bsf_get_by_name("extract_extradata");
118 if (!filter) {
119 av_log(avctx, AV_LOG_ERROR, "Cannot get extract_extradata bitstream filter\n");
120 return AVERROR_BUG;
121 }
123 if (ret < 0)
124 return ret;
126 if (ret < 0)
127 return ret;
128 ret = av_bsf_init(q->extra_data_bsf);
129 if (ret < 0)
130 return ret;
131 }
132
134
135 return ff_qsv_enc_init(avctx, &q->qsv);
136}
137
139 const AVFrame *frame, int *got_packet)
140{
141 QSVAV1EncContext *q = avctx->priv_data;
142 int ret;
143
144 ret = ff_qsv_encode(avctx, &q->qsv, pkt, frame, got_packet);
145 if (ret < 0)
146 return ret;
147
148 if (*got_packet && avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
150 if (ret < 0) {
151 av_log(avctx, AV_LOG_ERROR, "extract_extradata filter "
152 "failed to send input packet\n");
153 return ret;
154 }
155
157 if (ret < 0) {
158 av_log(avctx, AV_LOG_ERROR, "extract_extradata filter "
159 "failed to receive output packet\n");
160 return ret;
161 }
162 }
163
164 return ret;
165}
166
168{
169 QSVAV1EncContext *q = avctx->priv_data;
170
172
173 return ff_qsv_enc_close(avctx, &q->qsv);
174}
175
176#define OFFSET(x) offsetof(QSVAV1EncContext, x)
177#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
178static const AVOption options[] = {
186 { "profile", NULL, OFFSET(qsv.profile), AV_OPT_TYPE_INT, { .i64 = MFX_PROFILE_UNKNOWN }, 0, INT_MAX, VE, .unit = "profile" },
187 { "unknown" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_UNKNOWN }, INT_MIN, INT_MAX, VE, .unit = "profile" },
188 { "main" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_PROFILE_AV1_MAIN }, INT_MIN, INT_MAX, VE, .unit = "profile" },
189 { "tile_cols", "Number of columns for tiled encoding", OFFSET(qsv.tile_cols), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, UINT16_MAX, VE },
190 { "tile_rows", "Number of rows for tiled encoding", OFFSET(qsv.tile_rows), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, UINT16_MAX, VE },
191 { "look_ahead_depth", "Depth of look ahead in number frames, available when extbrc option is enabled", OFFSET(qsv.look_ahead_depth), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100, VE },
192 { NULL },
193};
194
195static const AVClass class = {
196 .class_name = "av1_qsv encoder",
198 .option = options,
200};
201
203 { "b", "0" },
204 { "g", "-1" },
205 { "bf", "-1" },
206 { "refs", "0" },
207 { NULL },
208};
209
211 .p.name = "av1_qsv",
212 .p.long_name = NULL_IF_CONFIG_SMALL("AV1 (Intel Quick Sync Video acceleration)"),
213 .priv_data_size = sizeof(QSVAV1EncContext),
214 .p.type = AVMEDIA_TYPE_VIDEO,
215 .p.id = AV_CODEC_ID_AV1,
216 .init = qsv_enc_init,
218 .close = qsv_enc_close,
219 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HYBRID,
221 .color_ranges = AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG,
222 .p.priv_class = &class,
223 .defaults = qsv_enc_defaults,
224 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
225 .p.wrapper_name = "qsv",
226 .hw_configs = ff_qsv_enc_hw_configs,
227};
const FFCodec ff_av1_qsv_encoder
Definition qsvenc_av1.c:210
#define VE
Definition amfenc_av1.c:30
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define CODEC_PIXFMTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Definition codec_par.c:138
common internal and external API header
#define NULL
Definition coverity.c:32
static AVPacket * pkt
static AVFrame * frame
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
void av_bsf_free(AVBSFContext **pctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
Definition bsf.c:47
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition bsf.c:147
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
Allocate a context for a given bitstream filter.
Definition bsf.c:99
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition bsf.c:228
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition bsf.c:200
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition codec.h:79
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition avcodec.h:318
#define AV_CODEC_CAP_HYBRID
Codec is potentially backed by a hardware implementation, but not necessarily.
Definition codec.h:140
@ AV_CODEC_ID_AV1
Definition codec_id.h:275
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR(e)
Definition error.h:45
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition frame.c:659
@ AV_FRAME_DATA_CONTENT_LIGHT_LEVEL
Content light level (based on CTA-861.3).
Definition frame.h:137
@ AV_FRAME_DATA_MASTERING_DISPLAY_METADATA
Mastering display metadata associated with a video frame.
Definition frame.h:120
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
Memory handling functions.
AVOptions.
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:766
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:783
#define AV_PIX_FMT_P010
Definition pixfmt.h:608
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition pixfmt.h:96
@ AV_PIX_FMT_QSV
HW acceleration through QSV, data[3] contains a pointer to the mfxFrameSurface1 structure.
Definition pixfmt.h:247
#define QSV_RUNTIME_VERSION_ATLEAST(MFX_VERSION, MAJOR, MINOR)
int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q)
Definition qsvenc.c:2710
int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q)
Definition qsvenc.c:1637
int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition qsvenc.c:2642
const AVCodecHWConfigInternal *const ff_qsv_enc_hw_configs[]
Definition qsvenc.c:2762
#define QSV_OPTION_LOW_DELAY_BRC
Definition qsvenc.h:111
#define QSV_OPTION_B_STRATEGY
Definition qsvenc.h:105
#define QSV_OPTION_MAX_FRAME_SIZE
Definition qsvenc.h:79
#define QSV_OPTION_ADAPTIVE_I
Definition qsvenc.h:96
#define QSV_COMMON_OPTS
Definition qsvenc.h:54
#define QSV_OPTION_EXTBRC
Definition qsvenc.h:93
#define QSV_OPTION_ADAPTIVE_B
Definition qsvenc.h:99
static int qsv_enc_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition qsvenc_av1.c:138
static int qsv_av1_set_encode_ctrl(AVCodecContext *avctx, const AVFrame *frame, mfxEncodeCtrl *enc_ctrl)
Definition qsvenc_av1.c:42
static const FFCodecDefault qsv_enc_defaults[]
Definition qsvenc_av1.c:202
static av_cold int qsv_enc_init(AVCodecContext *avctx)
Definition qsvenc_av1.c:111
static av_cold int qsv_enc_close(AVCodecContext *avctx)
Definition qsvenc_av1.c:167
#define OFFSET(x)
Definition qsvenc_av1.c:176
The bitstream filter state.
Definition bsf.h:68
AVCodecParameters * par_in
Parameters of the input stream.
Definition bsf.h:90
Describe the class of an AVClass context structure.
Definition log.h:76
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition log.h:81
main external API structure.
Definition avcodec.h:443
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
void * priv_data
Definition avcodec.h:470
Content light level needed by to transmit HDR over HDMI (CTA-861.3).
unsigned MaxFALL
Max average light level per frame (cd/m^2).
unsigned MaxCLL
Max content light level (cd/m^2).
Structure to hold side data for an AVFrame.
Definition frame.h:327
uint8_t * data
Definition frame.h:329
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
Mastering display metadata capable of representing the color volume of the display used to master the...
int has_primaries
Flag indicating whether the display primaries (and white point) are set.
AVRational max_luminance
Max luminance of mastering display (cd/m^2).
AVRational min_luminance
Min luminance of mastering display (cd/m^2).
AVRational display_primaries[3][2]
CIE 1931 xy chromaticity coords of color primaries (r, g, b order).
AVRational white_point[2]
CIE 1931 xy chromaticity coords of white point.
int has_luminance
Flag indicating whether the luminance (min_ and max_) have been set.
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
QSVEncContext qsv
Definition qsvenc_av1.c:39
AVBSFContext * extra_data_bsf
Definition qsvenc_av1.c:38
mfxVersion ver
Definition qsvenc.h:207
SetEncodeCtrlCB * set_encode_ctrl_cb
Definition qsvenc.h:266
#define av_mallocz(s)
#define av_log(a,...)
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29