History

avcodec: add external enc libvvenc for H266/VVC

Add external encoder VVenC for H266/VVC encoding. Register new encoder libvvenc. Add libvvenc to wrap the vvenc interface. libvvenc implements encoder option: preset,qp,qpa,period, passlogfile,stats,vvenc-params,level,tier. Enable encoder by adding --enable-libvvenc in configure step.

Thomas Siedel committed 2 years ago

Commit c75940d1 parent 4e4444f8 files · +564 −2Browse files

Unified · Split

8 files
  1. Changelog +1 −0
  2. configure +4 −0
  3. doc/encoders.texi +64 −0
  4. fftools/ffmpeg_mux_init.c +1 −1
  5. libavcodec/Makefile +1 −0
  6. libavcodec/allcodecs.c +1 −0
  7. libavcodec/libvvenc.c +491 −0
  8. libavcodec/version.h +1 −1
Changelog+1−0
@@ -13,6 +13,7 @@ version <next>: · 12 unmodified lines
13- VVC decoder compatible with DVB test content13- VVC decoder compatible with DVB test content
14- xHE-AAC decoder14- xHE-AAC decoder
15- removed DEC Alpha DSP and support code15- removed DEC Alpha DSP and support code
16- VVC encoding support via libvvenc
16 17
17 18
18version 7.0:19version 7.0:
configure+4−0
@@ -293,6 +293,7 @@ External library support: · 292 unmodified lines
293 --enable-libvorbis enable Vorbis en/decoding via libvorbis,293 --enable-libvorbis enable Vorbis en/decoding via libvorbis,
294 native implementation exists [no]294 native implementation exists [no]
295 --enable-libvpx enable VP8 and VP9 de/encoding via libvpx [no]295 --enable-libvpx enable VP8 and VP9 de/encoding via libvpx [no]
296 --enable-libvvenc enable H.266/VVC encoding via vvenc [no]
296 --enable-libwebp enable WebP encoding via libwebp [no]297 --enable-libwebp enable WebP encoding via libwebp [no]
297 --enable-libx264 enable H.264 encoding via x264 [no]298 --enable-libx264 enable H.264 encoding via x264 [no]
298 --enable-libx265 enable HEVC encoding via x265 [no]299 --enable-libx265 enable HEVC encoding via x265 [no]
@@ -1966,6 +1967,7 @@ EXTERNAL_LIBRARY_LIST=" · 1667 unmodified lines
1966 libvmaf1967 libvmaf
1967 libvorbis1968 libvorbis
1968 libvpx1969 libvpx
1970 libvvenc
1969 libwebp1971 libwebp
1970 libxevd1972 libxevd
1971 libxeve1973 libxeve
@@ -3554,6 +3556,7 @@ libvpx_vp8_decoder_deps="libvpx" · 1582 unmodified lines
3554libvpx_vp8_encoder_deps="libvpx"3556libvpx_vp8_encoder_deps="libvpx"
3555libvpx_vp9_decoder_deps="libvpx"3557libvpx_vp9_decoder_deps="libvpx"
3556libvpx_vp9_encoder_deps="libvpx"3558libvpx_vp9_encoder_deps="libvpx"
3559libvvenc_encoder_deps="libvvenc"
3557libwebp_encoder_deps="libwebp"3560libwebp_encoder_deps="libwebp"
3558libwebp_anim_encoder_deps="libwebp"3561libwebp_anim_encoder_deps="libwebp"
3559libx262_encoder_deps="libx262"3562libx262_encoder_deps="libx262"
@@ -6988,6 +6991,7 @@ enabled libvpx && { · 3428 unmodified lines
6988 fi6991 fi
6989}6992}
6990 6993
6994enabled libvvenc && require_pkg_config libvvenc "libvvenc >= 1.6.1" "vvenc/vvenc.h" vvenc_get_version
6991enabled libwebp && {6995enabled libwebp && {
6992 enabled libwebp_encoder && require_pkg_config libwebp "libwebp >= 0.2.0" webp/encode.h WebPGetEncoderVersion6996 enabled libwebp_encoder && require_pkg_config libwebp "libwebp >= 0.2.0" webp/encode.h WebPGetEncoderVersion
6993 enabled libwebp_anim_encoder && check_pkg_config libwebp_anim_encoder "libwebpmux >= 0.4.0" webp/mux.h WebPAnimEncoderOptionsInit; }6997 enabled libwebp_anim_encoder && check_pkg_config libwebp_anim_encoder "libwebpmux >= 0.4.0" webp/mux.h WebPAnimEncoderOptionsInit; }
doc/encoders.texi+64−0
@@ -2378,6 +2378,70 @@ Indicates frame duration · 2377 unmodified lines
2378For more information about libvpx see:2378For more information about libvpx see:
2379@url{http://www.webmproject.org/}2379@url{http://www.webmproject.org/}
2380 2380
2381@section libvvenc
2382
2383VVenC H.266/VVC encoder wrapper.
2384
2385This encoder requires the presence of the libvvenc headers and library
2386during configuration. You need to explicitly configure the build with
2387@option{--enable-libvvenc}.
2388
2389The VVenC project website is at
2390@url{https://github.com/fraunhoferhhi/vvenc}.
2391
2392@subsection Supported Pixel Formats
2393
2394VVenC supports only 10-bit color spaces as input. But the internal (encoded)
2395bit depth can be set to 8-bit or 10-bit at runtime.
2396
2397@subsection Options
2398
2399@table @option
2400@item b
2401Sets target video bitrate.
2402
2403@item g
2404Set the GOP size. Currently support for g=1 (Intra only) or default.
2405
2406@item preset
2407Set the VVenC preset.
2408
2409@item levelidc
2410Set level idc.
2411
2412@item tier
2413Set vvc tier.
2414
2415@item qp
2416Set constant quantization parameter.
2417
2418@item subopt @var{boolean}
2419Set subjective (perceptually motivated) optimization. Default is 1 (on).
2420
2421@item bitdepth8 @var{boolean}
2422Set 8bit coding mode instead of using 10bit. Default is 0 (off).
2423
2424@item period
2425set (intra) refresh period in seconds.
2426
2427@item vvenc-params
2428Set vvenc options using a list of @var{key}=@var{value} couples separated
2429by ":". See @command{vvencapp --fullhelp} or @command{vvencFFapp --fullhelp} for a list of options.
2430
2431For example, the options might be provided as:
2432
2433@example
2434intraperiod=64:decodingrefreshtype=idr:poc0idr=1:internalbitdepth=8
2435@end example
2436
2437For example the encoding options might be provided with @option{-vvenc-params}:
2438
2439@example
2440ffmpeg -i input -c:v libvvenc -b 1M -vvenc-params intraperiod=64:decodingrefreshtype=idr:poc0idr=1:internalbitdepth=8 output.mp4
2441@end example
2442
2443@end table
2444
2381@section libwebp2445@section libwebp
2382 2446
2383libwebp WebP Image encoder wrapper2447libwebp WebP Image encoder wrapper
fftools/ffmpeg_mux_init.c+1−1
@@ -739,7 +739,7 @@ static int new_stream_video(Muxer *mux, const OptionsContext *o, · 738 unmodified lines
739 ost->logfile_prefix ? ost->logfile_prefix :739 ost->logfile_prefix ? ost->logfile_prefix :
740 DEFAULT_PASS_LOGFILENAME_PREFIX,740 DEFAULT_PASS_LOGFILENAME_PREFIX,
741 ost_idx);741 ost_idx);
742 if (!strcmp(ost->enc_ctx->codec->name, "libx264")) {742 if (!strcmp(ost->enc_ctx->codec->name, "libx264") || !strcmp(ost->enc_ctx->codec->name, "libvvenc")) {
743 av_dict_set(&ost->encoder_opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE);743 av_dict_set(&ost->encoder_opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE);
744 } else {744 } else {
745 if (video_enc->flags & AV_CODEC_FLAG_PASS2) {745 if (video_enc->flags & AV_CODEC_FLAG_PASS2) {
libavcodec/Makefile+1−0
@@ -1150,6 +1150,7 @@ OBJS-$(CONFIG_LIBVPX_VP8_DECODER) += libvpxdec.o · 1149 unmodified lines
1150OBJS-$(CONFIG_LIBVPX_VP8_ENCODER) += libvpxenc.o1150OBJS-$(CONFIG_LIBVPX_VP8_ENCODER) += libvpxenc.o
1151OBJS-$(CONFIG_LIBVPX_VP9_DECODER) += libvpxdec.o1151OBJS-$(CONFIG_LIBVPX_VP9_DECODER) += libvpxdec.o
1152OBJS-$(CONFIG_LIBVPX_VP9_ENCODER) += libvpxenc.o1152OBJS-$(CONFIG_LIBVPX_VP9_ENCODER) += libvpxenc.o
1153OBJS-$(CONFIG_LIBVVENC_ENCODER) += libvvenc.o
1153OBJS-$(CONFIG_LIBWEBP_ENCODER) += libwebpenc_common.o libwebpenc.o1154OBJS-$(CONFIG_LIBWEBP_ENCODER) += libwebpenc_common.o libwebpenc.o
1154OBJS-$(CONFIG_LIBWEBP_ANIM_ENCODER) += libwebpenc_common.o libwebpenc_animencoder.o1155OBJS-$(CONFIG_LIBWEBP_ANIM_ENCODER) += libwebpenc_common.o libwebpenc_animencoder.o
1155OBJS-$(CONFIG_LIBX262_ENCODER) += libx264.o1156OBJS-$(CONFIG_LIBX262_ENCODER) += libx264.o
libavcodec/allcodecs.c+1−0
@@ -800,6 +800,7 @@ extern const FFCodec ff_libvpx_vp8_encoder; · 799 unmodified lines
800extern const FFCodec ff_libvpx_vp8_decoder;800extern const FFCodec ff_libvpx_vp8_decoder;
801extern FFCodec ff_libvpx_vp9_encoder;801extern FFCodec ff_libvpx_vp9_encoder;
802extern const FFCodec ff_libvpx_vp9_decoder;802extern const FFCodec ff_libvpx_vp9_decoder;
803extern const FFCodec ff_libvvenc_encoder;
803/* preferred over libwebp */804/* preferred over libwebp */
804extern const FFCodec ff_libwebp_anim_encoder;805extern const FFCodec ff_libwebp_anim_encoder;
805extern const FFCodec ff_libwebp_encoder;806extern const FFCodec ff_libwebp_encoder;
libavcodec/libvvenc.c+491−0
@@ -0,0 +1,491 @@
1/*
2 * H.266 encoding using the VVenC library
3 *
4 * Copyright (C) 2022, Thomas Siedel
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#include <vvenc/vvenc.h>
24#include <vvenc/vvencCfg.h>
25#include <vvenc/version.h>
26
27#include "libavutil/avstring.h"
28#include "libavutil/avutil.h"
29#include "libavutil/common.h"
30#include "libavutil/frame.h"
31#include "libavutil/imgutils.h"
32#include "libavutil/log.h"
33#include "libavutil/mem.h"
34#include "libavutil/pixdesc.h"
35#include "libavutil/opt.h"
36
37#include "avcodec.h"
38#include "codec_internal.h"
39#include "encode.h"
40#include "internal.h"
41#include "packet_internal.h"
42#include "profiles.h"
43
44#define VVENC_VERSION_INT AV_VERSION_INT(VVENC_VERSION_MAJOR, \
45 VVENC_VERSION_MINOR, \
46 VVENC_VERSION_PATCH)
47
48typedef struct VVenCContext {
49 AVClass *class;
50 vvencEncoder *encoder;
51 vvencAccessUnit *au;
52 bool encode_done;
53 int preset;
54 int qp;
55 int qpa;
56 int intra_refresh_sec;
57 char *level;
58 int tier;
59 char *stats;
60 AVDictionary *vvenc_opts;
61} VVenCContext;
62
63static void vvenc_log_callback(void *ctx, int level,
64 const char *fmt, va_list args)
65{
66 vvenc_config params;
67 vvencEncoder *encoder = ctx;
68 if (encoder) {
69 vvenc_config_default(&params);
70 vvenc_get_config(encoder, &params);
71 if ((int)params.m_verbosity >= level)
72 vfprintf(level == 1 ? stderr : stdout, fmt, args);
73 }
74}
75
76static void vvenc_set_verbository(vvenc_config *params)
77{
78 int loglevel = av_log_get_level();
79 params->m_verbosity = VVENC_SILENT;
80 if (loglevel >= AV_LOG_DEBUG)
81 params->m_verbosity = VVENC_DETAILS;
82 else if (loglevel >= AV_LOG_VERBOSE)
83 params->m_verbosity = VVENC_NOTICE;
84 else if (loglevel >= AV_LOG_INFO)
85 params->m_verbosity = VVENC_WARNING;
86}
87
88static void vvenc_set_pic_format(AVCodecContext *avctx, vvenc_config *params)
89{
90 params->m_internChromaFormat = VVENC_CHROMA_420;
91 params->m_inputBitDepth[0] = 10;
92}
93
94static void vvenc_set_color_format(AVCodecContext *avctx, vvenc_config *params)
95{
96 if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED)
97 params->m_colourPrimaries = (int) avctx->color_primaries;
98 if (avctx->colorspace != AVCOL_SPC_UNSPECIFIED)
99 params->m_matrixCoefficients = (int) avctx->colorspace;
100 if (avctx->color_trc != AVCOL_TRC_UNSPECIFIED) {
101 params->m_transferCharacteristics = (int) avctx->color_trc;
102
103 if (avctx->color_trc == AVCOL_TRC_SMPTE2084)
104 params->m_HdrMode = (avctx->color_primaries == AVCOL_PRI_BT2020) ?
105 VVENC_HDR_PQ_BT2020 : VVENC_HDR_PQ;
106 else if (avctx->color_trc == AVCOL_TRC_BT2020_10 || avctx->color_trc == AVCOL_TRC_ARIB_STD_B67)
107 params->m_HdrMode = (avctx->color_trc == AVCOL_TRC_BT2020_10 ||
108 avctx->color_primaries == AVCOL_PRI_BT2020 ||
109 avctx->colorspace == AVCOL_SPC_BT2020_NCL ||
110 avctx->colorspace == AVCOL_SPC_BT2020_CL) ?
111 VVENC_HDR_HLG_BT2020 : VVENC_HDR_HLG;
112 }
113
114 if (params->m_HdrMode == VVENC_HDR_OFF &&
115 (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED || avctx->colorspace != AVCOL_SPC_UNSPECIFIED)) {
116 params->m_vuiParametersPresent = 1;
117 params->m_colourDescriptionPresent = true;
118 }
119}
120
121static void vvenc_set_framerate(AVCodecContext *avctx, vvenc_config *params)
122{
123 if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
124 params->m_FrameRate = avctx->framerate.num;
125 params->m_FrameScale = avctx->framerate.den;
126 } else {
127 params->m_FrameRate = avctx->time_base.den;
128 params->m_FrameScale = avctx->time_base.num;
129 }
130
131FF_DISABLE_DEPRECATION_WARNINGS
132
133#if FF_API_TICKS_PER_FRAME
134 if (avctx->ticks_per_frame == 1) {
135#endif
136 params->m_TicksPerSecond = -1; /* auto mode for ticks per frame = 1 */
137#if FF_API_TICKS_PER_FRAME
138 } else {
139 params->m_TicksPerSecond =
140 ceil((avctx->time_base.den / (double) avctx->time_base.num) *
141 (double) avctx->ticks_per_frame);
142 }
143#endif
144FF_ENABLE_DEPRECATION_WARNINGS
145}
146
147static int vvenc_parse_vvenc_params(AVCodecContext *avctx, vvenc_config *params)
148{
149 VVenCContext *s = avctx->priv_data;
150 const AVDictionaryEntry *en = NULL;
151 int parse_ret;
152 int ret = 0;
153
154 while ((en = av_dict_iterate(s->vvenc_opts, en))) {
155 av_log(avctx, AV_LOG_DEBUG, "vvenc_set_param: '%s:%s'\n", en->key,
156 en->value);
157 parse_ret = vvenc_set_param(params, en->key, en->value);
158 switch (parse_ret) {
159 case VVENC_PARAM_BAD_NAME:
160 av_log(avctx, AV_LOG_ERROR, "Unknown vvenc option: %s.\n", en->key);
161 ret = AVERROR(EINVAL);
162 break;
163 case VVENC_PARAM_BAD_VALUE:
164 av_log(avctx, AV_LOG_ERROR, "Invalid vvenc value for %s: %s.\n", en->key, en->value);
165 ret = AVERROR(EINVAL);
166 break;
167 default:
168 break;
169 }
170
171 if (!av_strcasecmp(en->key, "rcstatsfile")) {
172 av_log(avctx, AV_LOG_ERROR, "vvenc-params 2pass option 'rcstatsfile' "
173 "not available. Use option 'passlogfile'\n");
174 ret = AVERROR(EINVAL);
175 }
176 if (!av_strcasecmp(en->key, "passes") || !av_strcasecmp(en->key, "pass")) {
177 av_log(avctx, AV_LOG_ERROR, "vvenc-params 2pass option '%s' "
178 "not available. Use option 'pass'\n", en->key);
179 ret = AVERROR(EINVAL);
180 }
181 }
182 return ret;
183}
184
185static int vvenc_set_rc_mode(AVCodecContext *avctx, vvenc_config *params)
186{
187 params->m_RCNumPasses = 1;
188 if ((avctx->flags & AV_CODEC_FLAG_PASS1 || avctx->flags & AV_CODEC_FLAG_PASS2)) {
189 if (!avctx->bit_rate) {
190 av_log(avctx, AV_LOG_ERROR, "A bitrate must be set to use two pass mode.\n");
191 return AVERROR(EINVAL);
192 }
193 params->m_RCNumPasses = 2;
194 if (avctx->flags & AV_CODEC_FLAG_PASS1)
195 params->m_RCPass = 1;
196 else
197 params->m_RCPass = 2;
198 }
199
200 if (avctx->rc_max_rate) {
201#if VVENC_VERSION_INT >= AV_VERSION_INT(1,8,0)
202 params->m_RCMaxBitrate = avctx->rc_max_rate;
203#endif
204
205#if VVENC_VERSION_INT < AV_VERSION_INT(1,11,0)
206 /* rc_max_rate without a bit_rate enables capped CQF mode.
207 (QP + subj. optimization + max. bitrate) */
208 if (!avctx->bit_rate) {
209 av_log(avctx, AV_LOG_ERROR, "Capped Constant Quality Factor mode (capped CQF) "
210 "needs at least vvenc version >= 1.11.0 (current version %s)\n", vvenc_get_version());
211 return AVERROR(EINVAL);
212 }
213#endif
214 }
215 return 0;
216}
217
218static int vvenc_init_extradata(AVCodecContext *avctx, VVenCContext *s)
219{
220 int ret;
221 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
222 ret = vvenc_get_headers(s->encoder, s->au);
223 if (0 != ret) {
224 av_log(avctx, AV_LOG_ERROR, "cannot get (SPS,PPS) headers: %s\n",
225 vvenc_get_last_error(s->encoder));
226 return AVERROR(EINVAL);
227 }
228
229 if (s->au->payloadUsedSize <= 0) {
230 return AVERROR_INVALIDDATA;
231 }
232
233 avctx->extradata_size = s->au->payloadUsedSize;
234 avctx->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
235 if (!avctx->extradata) {
236 return AVERROR(ENOMEM);
237 }
238
239 memcpy(avctx->extradata, s->au->payload, avctx->extradata_size);
240 }
241 return 0;
242}
243
244static av_cold int vvenc_init(AVCodecContext *avctx)
245{
246 int ret;
247 int framerate;
248 VVenCContext *s = avctx->priv_data;
249 vvenc_config params;
250 vvencPresetMode preset = (vvencPresetMode) s->preset;
251
252 if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
253 av_log(avctx, AV_LOG_ERROR, "interlaced not supported\n");
254 return AVERROR(EINVAL);
255 }
256
257 vvenc_config_default(&params);
258
259 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
260 framerate = avctx->framerate.num / avctx->framerate.den;
261 else
262 framerate = avctx->time_base.den / avctx->time_base.num;
263
264 vvenc_init_default(&params, avctx->width, avctx->height, framerate,
265 avctx->bit_rate, s->qp, preset);
266
267 vvenc_set_verbository(&params);
268
269 if (avctx->thread_count > 0)
270 params.m_numThreads = avctx->thread_count;
271
272 /* GOP settings (IDR/CRA) */
273 if (avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)
274 params.m_DecodingRefreshType = VVENC_DRT_IDR;
275
276 if (avctx->gop_size == 1) {
277 params.m_GOPSize = 1;
278 params.m_IntraPeriod = 1;
279 } else
280 params.m_IntraPeriodSec = s->intra_refresh_sec;
281
282 params.m_AccessUnitDelimiter = true;
283 params.m_usePerceptQPA = s->qpa;
284 params.m_levelTier = (vvencTier) s->tier;
285
286 if (avctx->level > 0)
287 params.m_level = (vvencLevel)avctx->level;
288
289 if (s->level) {
290 if (VVENC_PARAM_BAD_VALUE == vvenc_set_param(&params, "level", s->level)) {
291 av_log(avctx, AV_LOG_ERROR, "Invalid level_idc: %s.\n", s->level);
292 return AVERROR(EINVAL);
293 }
294 }
295
296 vvenc_set_framerate(avctx, &params);
297
298 vvenc_set_pic_format(avctx, &params);
299
300 vvenc_set_color_format(avctx, &params);
301
302 ret = vvenc_parse_vvenc_params(avctx, &params);
303 if (ret != 0)
304 return ret;
305
306 ret = vvenc_set_rc_mode(avctx, &params);
307 if (ret != 0)
308 return ret;
309
310 s->encoder = vvenc_encoder_create();
311 if (!s->encoder) {
312 av_log(avctx, AV_LOG_ERROR, "cannot create libvvenc encoder\n");
313 return AVERROR(ENOMEM);
314 }
315
316 vvenc_set_msg_callback(&params, s->encoder, vvenc_log_callback);
317 ret = vvenc_encoder_open(s->encoder, &params);
318 if (ret != 0) {
319 av_log(avctx, AV_LOG_ERROR, "cannot open libvvenc encoder: %s\n",
320 vvenc_get_last_error(s->encoder));
321 return AVERROR_EXTERNAL;
322 }
323
324 vvenc_get_config(s->encoder, &params); /* get the adapted config */
325
326 av_log(avctx, AV_LOG_INFO, "libvvenc version: %s\n", vvenc_get_version());
327 if (av_log_get_level() >= AV_LOG_VERBOSE)
328 av_log(avctx, AV_LOG_INFO, "%s\n", vvenc_get_config_as_string(&params, params.m_verbosity));
329
330 if (params.m_RCNumPasses == 2) {
331 ret = vvenc_init_pass(s->encoder, params.m_RCPass - 1, s->stats);
332 if (ret != 0) {
333 av_log(avctx, AV_LOG_ERROR, "cannot init pass %d: %s\n", params.m_RCPass,
334 vvenc_get_last_error(s->encoder));
335 return AVERROR_EXTERNAL;
336 }
337 }
338
339 s->au = vvenc_accessUnit_alloc();
340 if (!s->au) {
341 av_log(avctx, AV_LOG_FATAL, "cannot allocate memory for AU payload\n");
342 return AVERROR(ENOMEM);
343 }
344 vvenc_accessUnit_alloc_payload(s->au, avctx->width * avctx->height);
345 if (!s->au->payload) {
346 av_log(avctx, AV_LOG_FATAL, "cannot allocate payload memory of size %d\n",
347 avctx->width * avctx->height);
348 return AVERROR(ENOMEM);
349 }
350
351 ret = vvenc_init_extradata(avctx, s);
352 if (ret != 0)
353 return ret;
354
355 s->encode_done = false;
356 return 0;
357}
358
359static av_cold int vvenc_close(AVCodecContext *avctx)
360{
361 VVenCContext *s = avctx->priv_data;
362
363 if (s->au)
364 vvenc_accessUnit_free(s->au, true);
365
366 if (s->encoder) {
367 vvenc_print_summary(s->encoder);
368
369 if (0 != vvenc_encoder_close(s->encoder))
370 return AVERROR_EXTERNAL;
371 }
372
373 return 0;
374}
375
376static av_cold int vvenc_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame,
377 int *got_packet)
378{
379 VVenCContext *s = avctx->priv_data;
380 vvencYUVBuffer *pyuvbuf;
381 vvencYUVBuffer yuvbuf;
382 int ret;
383
384 pyuvbuf = NULL;
385 if (frame) {
386 vvenc_YUVBuffer_default(&yuvbuf);
387 yuvbuf.planes[0].ptr = (int16_t *) frame->data[0];
388 yuvbuf.planes[1].ptr = (int16_t *) frame->data[1];
389 yuvbuf.planes[2].ptr = (int16_t *) frame->data[2];
390
391 yuvbuf.planes[0].width = frame->width;
392 yuvbuf.planes[0].height = frame->height;
393 yuvbuf.planes[0].stride = frame->linesize[0] >> 1; /* stride is used in 16bit samples in vvenc */
394
395 yuvbuf.planes[1].width = frame->width >> 1;
396 yuvbuf.planes[1].height = frame->height >> 1;
397 yuvbuf.planes[1].stride = frame->linesize[1] >> 1;
398
399 yuvbuf.planes[2].width = frame->width >> 1;
400 yuvbuf.planes[2].height = frame->height >> 1;
401 yuvbuf.planes[2].stride = frame->linesize[2] >> 1;
402
403 yuvbuf.cts = frame->pts;
404 yuvbuf.ctsValid = true;
405 pyuvbuf = &yuvbuf;
406 }
407
408 if (!s->encode_done) {
409 if (vvenc_encode(s->encoder, pyuvbuf, s->au, &s->encode_done) != 0)
410 return AVERROR_EXTERNAL;
411 } else
412 return 0;
413
414 if (s->au->payloadUsedSize > 0) {
415 ret = ff_get_encode_buffer(avctx, pkt, s->au->payloadUsedSize, 0);
416 if (ret < 0)
417 return ret;
418
419 memcpy(pkt->data, s->au->payload, s->au->payloadUsedSize);
420
421 if (s->au->ctsValid)
422 pkt->pts = s->au->cts;
423 if (s->au->dtsValid)
424 pkt->dts = s->au->dts;
425 pkt->flags |= AV_PKT_FLAG_KEY * s->au->rap;
426
427 *got_packet = 1;
428 return 0;
429 }
430
431 return 0;
432}
433
434static const enum AVPixelFormat pix_fmts_vvenc[] = {
435 AV_PIX_FMT_YUV420P10,
436 AV_PIX_FMT_NONE
437};
438
439#define OFFSET(x) offsetof(VVenCContext, x)
440#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
441static const AVOption options[] = {
442 { "preset", "set encoding preset", OFFSET(preset), AV_OPT_TYPE_INT, {.i64 = 2}, 0, 4, VE, "preset"},
443 { "faster", "0", 0, AV_OPT_TYPE_CONST, {.i64 = VVENC_FASTER}, INT_MIN, INT_MAX, VE, "preset" },
444 { "fast", "1", 0, AV_OPT_TYPE_CONST, {.i64 = VVENC_FAST}, INT_MIN, INT_MAX, VE, "preset" },
445 { "medium", "2", 0, AV_OPT_TYPE_CONST, {.i64 = VVENC_MEDIUM}, INT_MIN, INT_MAX, VE, "preset" },
446 { "slow", "3", 0, AV_OPT_TYPE_CONST, {.i64 = VVENC_SLOW}, INT_MIN, INT_MAX, VE, "preset" },
447 { "slower", "4", 0, AV_OPT_TYPE_CONST, {.i64 = VVENC_SLOWER}, INT_MIN, INT_MAX, VE, "preset" },
448 { "qp", "set quantization", OFFSET(qp), AV_OPT_TYPE_INT, {.i64 = 32}, -1, 63, VE },
449 { "qpa", "set subjective (perceptually motivated) optimization", OFFSET(qpa), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, VE},
450 { "passlogfile", "Filename for 2 pass stats", OFFSET(stats), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, VE},
451 { "stats", "Filename for 2 pass stats", OFFSET(stats), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, VE},
452 { "period", "set (intra) refresh period in seconds", OFFSET(intra_refresh_sec), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, VE },
453 { "vvenc-params", "set the vvenc configuration using a :-separated list of key=value parameters", OFFSET(vvenc_opts), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE },
454 { "level", "Specify level (as defined by Annex A)", OFFSET(level), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, VE},
455 { "tier", "set vvc tier", OFFSET(tier), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, VE, "tier"},
456 { "main", "main", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, VE, "tier"},
457 { "high", "high", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, INT_MIN, INT_MAX, VE, "tier"},
458 {NULL}
459};
460
461static const AVClass class = {
462 .class_name = "libvvenc",
463 .item_name = av_default_item_name,
464 .option = options,
465 .version = LIBAVUTIL_VERSION_INT,
466};
467
468static const FFCodecDefault vvenc_defaults[] = {
469 { "b", "0" },
470 { "g", "-1" },
471 { NULL },
472};
473
474const FFCodec ff_libvvenc_encoder = {
475 .p.name = "libvvenc",
476 CODEC_LONG_NAME("libvvenc H.266 / VVC"),
477 .p.type = AVMEDIA_TYPE_VIDEO,
478 .p.id = AV_CODEC_ID_VVC,
479 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
480 AV_CODEC_CAP_OTHER_THREADS,
481 .p.profiles = NULL_IF_CONFIG_SMALL(ff_vvc_profiles),
482 .p.priv_class = &class,
483 .p.wrapper_name = "libvvenc",
484 .priv_data_size = sizeof(VVenCContext),
485 .p.pix_fmts = pix_fmts_vvenc,
486 .init = vvenc_init,
487 FF_CODEC_ENCODE_CB(vvenc_frame),
488 .close = vvenc_close,
489 .defaults = vvenc_defaults,
490 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_AUTO_THREADS
491};
libavcodec/version.h+1−1
@@ -29,7 +29,7 @@ · 28 unmodified lines
29 29
30#include "version_major.h"30#include "version_major.h"
31 31
32#define LIBAVCODEC_VERSION_MINOR 732#define LIBAVCODEC_VERSION_MINOR 8
33#define LIBAVCODEC_VERSION_MICRO 10033#define LIBAVCODEC_VERSION_MICRO 100
34 34
35#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \35#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t