blob: 433accba226f96e2677a603a8904b742dfa23ece [file] [log] [blame]
Martin Storsjö8a3d9ca2013-12-12 15:13:551/*
2 * OpenH264 video encoder
3 * Copyright (C) 2014 Martin Storsjo
4 *
Michael Niedermayerc09eeca2015-01-06 23:48:235 * This file is part of FFmpeg.
Martin Storsjö8a3d9ca2013-12-12 15:13:556 *
Michael Niedermayerc09eeca2015-01-06 23:48:237 * FFmpeg is free software; you can redistribute it and/or
Martin Storsjö8a3d9ca2013-12-12 15:13:558 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
Michael Niedermayerc09eeca2015-01-06 23:48:2312 * FFmpeg is distributed in the hope that it will be useful,
Martin Storsjö8a3d9ca2013-12-12 15:13:5513 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
Michael Niedermayerc09eeca2015-01-06 23:48:2318 * License along with FFmpeg; if not, write to the Free Software
Martin Storsjö8a3d9ca2013-12-12 15:13:5519 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <wels/codec_api.h>
23#include <wels/codec_ver.h>
24
25#include "libavutil/attributes.h"
26#include "libavutil/common.h"
27#include "libavutil/opt.h"
Luca Barbato81c95eb2015-12-08 17:12:3328#include "libavutil/internal.h"
Martin Storsjö8a3d9ca2013-12-12 15:13:5529#include "libavutil/intreadwrite.h"
30#include "libavutil/mathematics.h"
31
32#include "avcodec.h"
Andreas Rheinhardta688f3c2022-03-16 17:18:2833#include "codec_internal.h"
Andreas Rheinhardt0812a602021-04-24 23:43:2634#include "encode.h"
Martin Storsjö8a3d9ca2013-12-12 15:13:5535#include "internal.h"
Martin Storsjöc5d326f2016-06-23 21:58:1736#include "libopenh264.h"
Martin Storsjö8a3d9ca2013-12-12 15:13:5537
Martin Storsjö293676c2016-07-08 20:21:4138#if !OPENH264_VER_AT_LEAST(1, 6)
39#define SM_SIZELIMITED_SLICE SM_DYN_SLICE
40#endif
41
Linjie Fue5f097e2020-04-29 03:00:4742#define TARGET_BITRATE_DEFAULT 2*1000*1000
43
Martin Storsjö8a3d9ca2013-12-12 15:13:5544typedef struct SVCContext {
45 const AVClass *av_class;
46 ISVCEncoder *encoder;
47 int slice_mode;
48 int loopfilter;
Linjie Fud3a7bdd2020-05-06 13:47:5049 int profile;
Mario Gasparonic3e5c472015-10-08 17:28:5550 int max_nal_size;
Martin Storsjö9e14a992015-10-28 09:06:3651 int skip_frames;
Martin Storsjö8edaf622015-10-28 09:12:4252 int skipped;
Linjie Fue4d37ab2020-05-06 13:47:5253 int coder;
Linjie Fu75fc3f92020-04-29 03:00:4854
55 // rate control mode
56 int rc_mode;
Martin Storsjö8a3d9ca2013-12-12 15:13:5557} SVCContext;
58
59#define OFFSET(x) offsetof(SVCContext, x)
60#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
Linjie Fu59a92042020-04-29 03:00:5061#define DEPRECATED AV_OPT_FLAG_DEPRECATED
Martin Storsjö8a3d9ca2013-12-12 15:13:5562static const AVOption options[] = {
Stefano Sabatiniae72b572015-09-08 13:53:4263 { "loopfilter", "enable loop filter", OFFSET(loopfilter), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
Linjie Fud3a7bdd2020-05-06 13:47:5064 { "profile", "set profile restrictions", OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 0xffff, VE, "profile" },
65#define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, { .i64 = value }, 0, 0, VE, "profile"
66 { PROFILE("constrained_baseline", FF_PROFILE_H264_CONSTRAINED_BASELINE) },
67 { PROFILE("main", FF_PROFILE_H264_MAIN) },
68 { PROFILE("high", FF_PROFILE_H264_HIGH) },
69#undef PROFILE
Stefano Sabatini6e891d52015-12-14 18:08:5370 { "max_nal_size", "set maximum NAL size in bytes", OFFSET(max_nal_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
71 { "allow_skip_frames", "allow skipping frames to hit the target bitrate", OFFSET(skip_frames), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
Linjie Fue4d37ab2020-05-06 13:47:5272 { "coder", "Coder type", OFFSET(coder), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE, "coder" },
73 { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, VE, "coder" },
74 { "cavlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "coder" },
75 { "cabac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "coder" },
76 { "vlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "coder" },
77 { "ac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "coder" },
Linjie Fu75fc3f92020-04-29 03:00:4878
79 { "rc_mode", "Select rate control mode", OFFSET(rc_mode), AV_OPT_TYPE_INT, { .i64 = RC_QUALITY_MODE }, RC_OFF_MODE, RC_TIMESTAMP_MODE, VE, "rc_mode" },
80 { "off", "bit rate control off", 0, AV_OPT_TYPE_CONST, { .i64 = RC_OFF_MODE }, 0, 0, VE, "rc_mode" },
81 { "quality", "quality mode", 0, AV_OPT_TYPE_CONST, { .i64 = RC_QUALITY_MODE }, 0, 0, VE, "rc_mode" },
82 { "bitrate", "bitrate mode", 0, AV_OPT_TYPE_CONST, { .i64 = RC_BITRATE_MODE }, 0, 0, VE, "rc_mode" },
83 { "buffer", "using buffer status to adjust the video quality (no bitrate control)", 0, AV_OPT_TYPE_CONST, { .i64 = RC_BUFFERBASED_MODE }, 0, 0, VE, "rc_mode" },
84#if OPENH264_VER_AT_LEAST(1, 4)
85 { "timestamp", "bit rate control based on timestamp", 0, AV_OPT_TYPE_CONST, { .i64 = RC_TIMESTAMP_MODE }, 0, 0, VE, "rc_mode" },
86#endif
87
Martin Storsjö8a3d9ca2013-12-12 15:13:5588 { NULL }
89};
90
91static const AVClass class = {
Tobias Rapp7992c282018-11-12 16:04:4392 .class_name = "libopenh264enc",
Diego Biurrun97cfe1d2017-06-10 14:45:0693 .item_name = av_default_item_name,
94 .option = options,
95 .version = LIBAVUTIL_VERSION_INT,
Martin Storsjö8a3d9ca2013-12-12 15:13:5596};
97
98static av_cold int svc_encode_close(AVCodecContext *avctx)
99{
100 SVCContext *s = avctx->priv_data;
101
102 if (s->encoder)
103 WelsDestroySVCEncoder(s->encoder);
Martin Storsjö8edaf622015-10-28 09:12:42104 if (s->skipped > 0)
105 av_log(avctx, AV_LOG_WARNING, "%d frames skipped\n", s->skipped);
Martin Storsjö8a3d9ca2013-12-12 15:13:55106 return 0;
107}
108
109static av_cold int svc_encode_init(AVCodecContext *avctx)
110{
111 SVCContext *s = avctx->priv_data;
112 SEncParamExt param = { 0 };
Martin Storsjöc5d326f2016-06-23 21:58:17113 int err;
Gregory J. Wolfe1a4c5fe2015-09-09 19:32:42114 int log_level;
Gregory J. Wolfee8c45b92015-09-09 19:32:42115 WelsTraceCallback callback_function;
Anton Khirnov11c9bd62015-10-03 13:19:10116 AVCPBProperties *props;
Martin Storsjö8a3d9ca2013-12-12 15:13:55117
Martin Storsjöc5d326f2016-06-23 21:58:17118 if ((err = ff_libopenh264_check_version(avctx)) < 0)
Andreas Schneider59018252022-02-18 12:53:19119 return AVERROR_ENCODER_NOT_FOUND;
Martin Storsjö8a3d9ca2013-12-12 15:13:55120
121 if (WelsCreateSVCEncoder(&s->encoder)) {
122 av_log(avctx, AV_LOG_ERROR, "Unable to create encoder\n");
123 return AVERROR_UNKNOWN;
124 }
125
Gregory J. Wolfe1a4c5fe2015-09-09 19:32:42126 // Pass all libopenh264 messages to our callback, to allow ourselves to filter them.
127 log_level = WELS_LOG_DETAIL;
128 (*s->encoder)->SetOption(s->encoder, ENCODER_OPTION_TRACE_LEVEL, &log_level);
Gregory J. Wolfee8c45b92015-09-09 19:32:42129
130 // Set the logging callback function to one that uses av_log() (see implementation above).
Martin Storsjöc5d326f2016-06-23 21:58:17131 callback_function = (WelsTraceCallback) ff_libopenh264_trace_callback;
Diego Biurrun800d91d2015-10-30 14:26:25132 (*s->encoder)->SetOption(s->encoder, ENCODER_OPTION_TRACE_CALLBACK, &callback_function);
Gregory J. Wolfee8c45b92015-09-09 19:32:42133
134 // Set the AVCodecContext as the libopenh264 callback context so that it can be passed to av_log().
Diego Biurrun800d91d2015-10-30 14:26:25135 (*s->encoder)->SetOption(s->encoder, ENCODER_OPTION_TRACE_CALLBACK_CONTEXT, &avctx);
Gregory J. Wolfee8c45b92015-09-09 19:32:42136
Martin Storsjö8a3d9ca2013-12-12 15:13:55137 (*s->encoder)->GetDefaultParams(s->encoder, &param);
138
Jun Zhaobecfdaa2020-07-26 11:16:43139 if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
140 param.fMaxFrameRate = av_q2d(avctx->framerate);
141 } else {
Anton Khirnov7d1d61c2023-05-04 09:50:48142FF_DISABLE_DEPRECATION_WARNINGS
143 param.fMaxFrameRate = 1.0 / av_q2d(avctx->time_base)
144#if FF_API_TICKS_PER_FRAME
145 / FFMAX(avctx->ticks_per_frame, 1)
146#endif
147 ;
148FF_ENABLE_DEPRECATION_WARNINGS
Jun Zhaobecfdaa2020-07-26 11:16:43149 }
Martin Storsjö8a3d9ca2013-12-12 15:13:55150 param.iPicWidth = avctx->width;
151 param.iPicHeight = avctx->height;
Linjie Fue5f097e2020-04-29 03:00:47152 param.iTargetBitrate = avctx->bit_rate > 0 ? avctx->bit_rate : TARGET_BITRATE_DEFAULT;
Martin Storsjö8a3d9ca2013-12-12 15:13:55153 param.iMaxBitrate = FFMAX(avctx->rc_max_rate, avctx->bit_rate);
Linjie Fu75fc3f92020-04-29 03:00:48154 param.iRCMode = s->rc_mode;
Linjie Fu433ece82020-04-29 03:00:46155 if (avctx->qmax >= 0)
156 param.iMaxQp = av_clip(avctx->qmax, 1, 51);
157 if (avctx->qmin >= 0)
158 param.iMinQp = av_clip(avctx->qmin, 1, param.iMaxQp);
Martin Storsjö8a3d9ca2013-12-12 15:13:55159 param.iTemporalLayerNum = 1;
160 param.iSpatialLayerNum = 1;
161 param.bEnableDenoise = 0;
162 param.bEnableBackgroundDetection = 1;
163 param.bEnableAdaptiveQuant = 1;
Martin Storsjö9e14a992015-10-28 09:06:36164 param.bEnableFrameSkip = s->skip_frames;
Martin Storsjö8a3d9ca2013-12-12 15:13:55165 param.bEnableLongTermReference = 0;
166 param.iLtrMarkPeriod = 30;
Linjie Fue5f097e2020-04-29 03:00:47167 if (avctx->gop_size >= 0)
168 param.uiIntraPeriod = avctx->gop_size;
Martin Storsjö25c29d32015-03-05 11:57:44169#if OPENH264_VER_AT_LEAST(1, 4)
170 param.eSpsPpsIdStrategy = CONSTANT_ID;
171#else
Martin Storsjö8a3d9ca2013-12-12 15:13:55172 param.bEnableSpsPpsIdAddition = 0;
Martin Storsjö25c29d32015-03-05 11:57:44173#endif
Martin Storsjö8a3d9ca2013-12-12 15:13:55174 param.bPrefixNalAddingCtrl = 0;
175 param.iLoopFilterDisableIdc = !s->loopfilter;
Limin Wangedd305e2022-01-14 09:48:54176 param.iEntropyCodingModeFlag = s->coder >= 0 ? s->coder : 1;
Martin Storsjö8a3d9ca2013-12-12 15:13:55177 param.iMultipleThreadIdc = avctx->thread_count;
Linjie Fud3a7bdd2020-05-06 13:47:50178
Linjie Fue3e27022020-05-06 13:47:51179 /* Allow specifying the libopenh264 profile through AVCodecContext. */
180 if (FF_PROFILE_UNKNOWN == s->profile &&
181 FF_PROFILE_UNKNOWN != avctx->profile)
182 switch (avctx->profile) {
183 case FF_PROFILE_H264_HIGH:
184 case FF_PROFILE_H264_MAIN:
185 case FF_PROFILE_H264_CONSTRAINED_BASELINE:
186 s->profile = avctx->profile;
187 break;
188 default:
189 av_log(avctx, AV_LOG_WARNING,
190 "Unsupported avctx->profile: %d.\n", avctx->profile);
191 break;
192 }
193
Linjie Fue4d37ab2020-05-06 13:47:52194 if (s->profile == FF_PROFILE_UNKNOWN && s->coder >= 0)
195 s->profile = s->coder == 0 ? FF_PROFILE_H264_CONSTRAINED_BASELINE :
Linjie Fud3a7bdd2020-05-06 13:47:50196#if OPENH264_VER_AT_LEAST(1, 8)
Linjie Fue4d37ab2020-05-06 13:47:52197 FF_PROFILE_H264_HIGH;
Linjie Fud3a7bdd2020-05-06 13:47:50198#else
Linjie Fue4d37ab2020-05-06 13:47:52199 FF_PROFILE_H264_MAIN;
Linjie Fud3a7bdd2020-05-06 13:47:50200#endif
201
202 switch (s->profile) {
Linjie Fud3a7bdd2020-05-06 13:47:50203 case FF_PROFILE_H264_HIGH:
Limin Wangedd305e2022-01-14 09:48:54204 av_log(avctx, AV_LOG_VERBOSE, "Using %s, "
205 "select EProfileIdc PRO_HIGH in libopenh264.\n",
206 param.iEntropyCodingModeFlag ? "CABAC" : "CAVLC");
Linjie Fud3a7bdd2020-05-06 13:47:50207 break;
Linjie Fud3a7bdd2020-05-06 13:47:50208 case FF_PROFILE_H264_MAIN:
Limin Wangedd305e2022-01-14 09:48:54209 av_log(avctx, AV_LOG_VERBOSE, "Using %s, "
210 "select EProfileIdc PRO_MAIN in libopenh264.\n",
211 param.iEntropyCodingModeFlag ? "CABAC" : "CAVLC");
Linjie Fud3a7bdd2020-05-06 13:47:50212 break;
Linjie Fud3a7bdd2020-05-06 13:47:50213 case FF_PROFILE_H264_CONSTRAINED_BASELINE:
214 case FF_PROFILE_UNKNOWN:
Limin Wangf74e90c2022-01-14 09:48:10215 s->profile = FF_PROFILE_H264_CONSTRAINED_BASELINE;
Linjie Fud3a7bdd2020-05-06 13:47:50216 param.iEntropyCodingModeFlag = 0;
217 av_log(avctx, AV_LOG_VERBOSE, "Using CAVLC, "
218 "select EProfileIdc PRO_BASELINE in libopenh264.\n");
219 break;
220 default:
Limin Wangf74e90c2022-01-14 09:48:10221 s->profile = FF_PROFILE_H264_CONSTRAINED_BASELINE;
Linjie Fud3a7bdd2020-05-06 13:47:50222 param.iEntropyCodingModeFlag = 0;
223 av_log(avctx, AV_LOG_WARNING, "Unsupported profile, "
224 "select EProfileIdc PRO_BASELINE in libopenh264.\n");
225 break;
226 }
Martin Storsjö8a3d9ca2013-12-12 15:13:55227
228 param.sSpatialLayers[0].iVideoWidth = param.iPicWidth;
229 param.sSpatialLayers[0].iVideoHeight = param.iPicHeight;
230 param.sSpatialLayers[0].fFrameRate = param.fMaxFrameRate;
231 param.sSpatialLayers[0].iSpatialBitrate = param.iTargetBitrate;
232 param.sSpatialLayers[0].iMaxSpatialBitrate = param.iMaxBitrate;
Limin Wangf74e90c2022-01-14 09:48:10233 param.sSpatialLayers[0].uiProfileIdc = s->profile;
Martin Storsjö8a3d9ca2013-12-12 15:13:55234
Valery Kotbe827e12018-11-01 13:15:11235#if OPENH264_VER_AT_LEAST(1, 7)
236 if (avctx->sample_aspect_ratio.num && avctx->sample_aspect_ratio.den) {
237 // Table E-1.
238 static const AVRational sar_idc[] = {
239 { 0, 0 }, // Unspecified (never written here).
240 { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
241 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
242 { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
243 { 160, 99 }, // Last 3 are unknown to openh264: { 4, 3 }, { 3, 2 }, { 2, 1 },
244 };
245 static const ESampleAspectRatio asp_idc[] = {
246 ASP_UNSPECIFIED,
247 ASP_1x1, ASP_12x11, ASP_10x11, ASP_16x11,
248 ASP_40x33, ASP_24x11, ASP_20x11, ASP_32x11,
249 ASP_80x33, ASP_18x11, ASP_15x11, ASP_64x33,
250 ASP_160x99,
251 };
252 int num, den, i;
253
254 av_reduce(&num, &den, avctx->sample_aspect_ratio.num,
255 avctx->sample_aspect_ratio.den, 65535);
256
257 for (i = 1; i < FF_ARRAY_ELEMS(sar_idc); i++) {
258 if (num == sar_idc[i].num &&
259 den == sar_idc[i].den)
260 break;
261 }
262 if (i == FF_ARRAY_ELEMS(sar_idc)) {
263 param.sSpatialLayers[0].eAspectRatio = ASP_EXT_SAR;
264 param.sSpatialLayers[0].sAspectRatioExtWidth = num;
265 param.sSpatialLayers[0].sAspectRatioExtHeight = den;
266 } else {
267 param.sSpatialLayers[0].eAspectRatio = asp_idc[i];
268 }
269 param.sSpatialLayers[0].bAspectRatioPresent = true;
Linjie Fu917d28d2020-04-06 11:14:45270 } else {
Valery Kotbe827e12018-11-01 13:15:11271 param.sSpatialLayers[0].bAspectRatioPresent = false;
272 }
273#endif
274
Martin Storsjö31aa5332016-06-23 22:06:12275 if ((avctx->slices > 1) && (s->max_nal_size)) {
276 av_log(avctx, AV_LOG_ERROR,
277 "Invalid combination -slices %d and -max_nal_size %d.\n",
278 avctx->slices, s->max_nal_size);
Martin Storsjö7a763712016-07-14 19:24:55279 return AVERROR(EINVAL);
Mario Gasparonic3e5c472015-10-08 17:28:55280 }
281
Martin Storsjö8a3d9ca2013-12-12 15:13:55282 if (avctx->slices > 1)
283 s->slice_mode = SM_FIXEDSLCNUM_SLICE;
Mario Gasparonic3e5c472015-10-08 17:28:55284
285 if (s->max_nal_size)
Martin Storsjö293676c2016-07-08 20:21:41286 s->slice_mode = SM_SIZELIMITED_SLICE;
Mario Gasparonic3e5c472015-10-08 17:28:55287
Martin Storsjö293676c2016-07-08 20:21:41288#if OPENH264_VER_AT_LEAST(1, 6)
289 param.sSpatialLayers[0].sSliceArgument.uiSliceMode = s->slice_mode;
290 param.sSpatialLayers[0].sSliceArgument.uiSliceNum = avctx->slices;
291#else
Martin Storsjö8a3d9ca2013-12-12 15:13:55292 param.sSpatialLayers[0].sSliceCfg.uiSliceMode = s->slice_mode;
293 param.sSpatialLayers[0].sSliceCfg.sSliceArgument.uiSliceNum = avctx->slices;
Martin Storsjö293676c2016-07-08 20:21:41294#endif
Linjie Fu93103612020-04-29 03:00:49295 if (avctx->slices == 0 && s->slice_mode == SM_FIXEDSLCNUM_SLICE)
296 av_log(avctx, AV_LOG_WARNING, "Slice count will be set automatically\n");
Martin Storsjö8a3d9ca2013-12-12 15:13:55297
Martin Storsjö293676c2016-07-08 20:21:41298 if (s->slice_mode == SM_SIZELIMITED_SLICE) {
Linjie Fu917d28d2020-04-06 11:14:45299 if (s->max_nal_size) {
Mario Gasparonic3e5c472015-10-08 17:28:55300 param.uiMaxNalSize = s->max_nal_size;
Martin Storsjö293676c2016-07-08 20:21:41301#if OPENH264_VER_AT_LEAST(1, 6)
302 param.sSpatialLayers[0].sSliceArgument.uiSliceSizeConstraint = s->max_nal_size;
303#else
Mario Gasparonic3e5c472015-10-08 17:28:55304 param.sSpatialLayers[0].sSliceCfg.sSliceArgument.uiSliceSizeConstraint = s->max_nal_size;
Martin Storsjö293676c2016-07-08 20:21:41305#endif
Mario Gasparonic3e5c472015-10-08 17:28:55306 } else {
Vittorio Giovara936f0d92015-09-13 21:45:24307 av_log(avctx, AV_LOG_ERROR, "Invalid -max_nal_size, "
308 "specify a valid max_nal_size to use -slice_mode dyn\n");
Martin Storsjö7a763712016-07-14 19:24:55309 return AVERROR(EINVAL);
Mario Gasparonic3e5c472015-10-08 17:28:55310 }
311 }
312
Limin Wang008cc902022-01-14 05:53:19313#if OPENH264_VER_AT_LEAST(1, 6)
314 param.sSpatialLayers[0].uiVideoFormat = VF_UNDEF;
Jun Zhaode5e2572023-04-18 09:08:52315
Limin Wang008cc902022-01-14 05:53:19316 if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED) {
Limin Wang008cc902022-01-14 05:53:19317 param.sSpatialLayers[0].bFullRange = (avctx->color_range == AVCOL_RANGE_JPEG);
Jun Zhaode5e2572023-04-18 09:08:52318 } else if (avctx->pix_fmt == AV_PIX_FMT_YUVJ420P)
319 param.sSpatialLayers[0].bFullRange = 1;
Limin Wang008cc902022-01-14 05:53:19320
321 if (avctx->colorspace != AVCOL_SPC_UNSPECIFIED ||
322 avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
323 avctx->color_trc != AVCOL_TRC_UNSPECIFIED) {
Limin Wang008cc902022-01-14 05:53:19324 param.sSpatialLayers[0].bColorDescriptionPresent = true;
325 }
326
327 if (avctx->colorspace != AVCOL_SPC_UNSPECIFIED)
328 param.sSpatialLayers[0].uiColorMatrix = avctx->colorspace;
329 if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED)
330 param.sSpatialLayers[0].uiColorPrimaries = avctx->color_primaries;
331 if (avctx->color_trc != AVCOL_TRC_UNSPECIFIED)
332 param.sSpatialLayers[0].uiTransferCharacteristics = avctx->color_trc;
Jun Zhao73ddcad2023-04-18 15:19:18333
334 param.sSpatialLayers[0].bVideoSignalTypePresent =
335 (param.sSpatialLayers[0].bFullRange || param.sSpatialLayers[0].bColorDescriptionPresent);
Limin Wang008cc902022-01-14 05:53:19336#endif
337
Martin Storsjö8a3d9ca2013-12-12 15:13:55338 if ((*s->encoder)->InitializeExt(s->encoder, &param) != cmResultSuccess) {
339 av_log(avctx, AV_LOG_ERROR, "Initialize failed\n");
Martin Storsjö7a763712016-07-14 19:24:55340 return AVERROR_UNKNOWN;
Martin Storsjö8a3d9ca2013-12-12 15:13:55341 }
342
Vittorio Giovara7c6eb0a2015-06-29 19:59:37343 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
Martin Storsjö8a3d9ca2013-12-12 15:13:55344 SFrameBSInfo fbi = { 0 };
345 int i, size = 0;
346 (*s->encoder)->EncodeParameterSets(s->encoder, &fbi);
347 for (i = 0; i < fbi.sLayerInfo[0].iNalCount; i++)
348 size += fbi.sLayerInfo[0].pNalLengthInByte[i];
Vittorio Giovara059a9342015-06-29 21:48:34349 avctx->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
Martin Storsjö7a763712016-07-14 19:24:55350 if (!avctx->extradata)
351 return AVERROR(ENOMEM);
Martin Storsjö8a3d9ca2013-12-12 15:13:55352 avctx->extradata_size = size;
353 memcpy(avctx->extradata, fbi.sLayerInfo[0].pBsBuf, size);
354 }
355
James Almer0231df52023-09-04 12:39:07356 props = ff_encode_add_cpb_side_data(avctx);
Martin Storsjö7a763712016-07-14 19:24:55357 if (!props)
358 return AVERROR(ENOMEM);
Anton Khirnov11c9bd62015-10-03 13:19:10359 props->max_bitrate = param.iMaxBitrate;
360 props->avg_bitrate = param.iTargetBitrate;
361
Martin Storsjö8a3d9ca2013-12-12 15:13:55362 return 0;
Martin Storsjö8a3d9ca2013-12-12 15:13:55363}
364
365static int svc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
366 const AVFrame *frame, int *got_packet)
367{
368 SVCContext *s = avctx->priv_data;
369 SFrameBSInfo fbi = { 0 };
370 int i, ret;
371 int encoded;
372 SSourcePicture sp = { 0 };
373 int size = 0, layer, first_layer = 0;
374 int layer_size[MAX_LAYER_NUM_OF_FRAME] = { 0 };
375
376 sp.iColorFormat = videoFormatI420;
377 for (i = 0; i < 3; i++) {
378 sp.iStride[i] = frame->linesize[i];
379 sp.pData[i] = frame->data[i];
380 }
381 sp.iPicWidth = avctx->width;
382 sp.iPicHeight = avctx->height;
383
Valery Kot67fd8df2018-03-16 13:50:34384 if (frame->pict_type == AV_PICTURE_TYPE_I) {
385 (*s->encoder)->ForceIntraFrame(s->encoder, true);
386 }
387
Martin Storsjö8a3d9ca2013-12-12 15:13:55388 encoded = (*s->encoder)->EncodeFrame(s->encoder, &sp, &fbi);
389 if (encoded != cmResultSuccess) {
390 av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed\n");
391 return AVERROR_UNKNOWN;
392 }
393 if (fbi.eFrameType == videoFrameTypeSkip) {
Martin Storsjö8edaf622015-10-28 09:12:42394 s->skipped++;
Martin Storsjö8a3d9ca2013-12-12 15:13:55395 av_log(avctx, AV_LOG_DEBUG, "frame skipped\n");
396 return 0;
397 }
398 first_layer = 0;
Martin Storsjö3852e2c2015-01-07 22:03:09399 // Normal frames are returned with one single layer, while IDR
Martin Storsjö8a3d9ca2013-12-12 15:13:55400 // frames have two layers, where the first layer contains the SPS/PPS.
401 // If using global headers, don't include the SPS/PPS in the returned
402 // packet - thus, only return one layer.
Vittorio Giovara7c6eb0a2015-06-29 19:59:37403 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)
Martin Storsjö8a3d9ca2013-12-12 15:13:55404 first_layer = fbi.iLayerNum - 1;
405
406 for (layer = first_layer; layer < fbi.iLayerNum; layer++) {
407 for (i = 0; i < fbi.sLayerInfo[layer].iNalCount; i++)
408 layer_size[layer] += fbi.sLayerInfo[layer].pNalLengthInByte[i];
409 size += layer_size[layer];
410 }
Martin Storsjö6996fd22015-01-28 20:17:29411 av_log(avctx, AV_LOG_DEBUG, "%d slices\n", fbi.sLayerInfo[fbi.iLayerNum - 1].iNalCount);
Martin Storsjö8a3d9ca2013-12-12 15:13:55412
Andreas Rheinhardt0812a602021-04-24 23:43:26413 if ((ret = ff_get_encode_buffer(avctx, avpkt, size, 0)))
Martin Storsjö8a3d9ca2013-12-12 15:13:55414 return ret;
Andreas Rheinhardt0812a602021-04-24 23:43:26415
Martin Storsjö8a3d9ca2013-12-12 15:13:55416 size = 0;
417 for (layer = first_layer; layer < fbi.iLayerNum; layer++) {
418 memcpy(avpkt->data + size, fbi.sLayerInfo[layer].pBsBuf, layer_size[layer]);
419 size += layer_size[layer];
420 }
421 avpkt->pts = frame->pts;
422 if (fbi.eFrameType == videoFrameTypeIDR)
423 avpkt->flags |= AV_PKT_FLAG_KEY;
424 *got_packet = 1;
425 return 0;
426}
427
Andreas Rheinhardt5aabb252022-03-16 20:26:11428static const FFCodecDefault svc_enc_defaults[] = {
Linjie Fue5f097e2020-04-29 03:00:47429 { "b", "0" },
430 { "g", "-1" },
Linjie Fu433ece82020-04-29 03:00:46431 { "qmin", "-1" },
432 { "qmax", "-1" },
433 { NULL },
434};
435
Andreas Rheinhardt20f97272022-03-16 20:09:54436const FFCodec ff_libopenh264_encoder = {
437 .p.name = "libopenh264",
Andreas Rheinhardt48286d42022-08-29 11:38:02438 CODEC_LONG_NAME("OpenH264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
Andreas Rheinhardt20f97272022-03-16 20:09:54439 .p.type = AVMEDIA_TYPE_VIDEO,
440 .p.id = AV_CODEC_ID_H264,
Anton Khirnov8d73f3c2022-11-27 12:37:10441 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_OTHER_THREADS |
442 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
Martin Storsjö8a3d9ca2013-12-12 15:13:55443 .priv_data_size = sizeof(SVCContext),
444 .init = svc_encode_init,
Andreas Rheinhardt4243da42022-03-30 21:28:24445 FF_CODEC_ENCODE_CB(svc_encode_frame),
Martin Storsjö8a3d9ca2013-12-12 15:13:55446 .close = svc_encode_close,
Andreas Rheinhardt21b23ce2022-07-09 22:05:45447 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
Anton Khirnov8a129072021-03-09 17:00:44448 FF_CODEC_CAP_AUTO_THREADS,
Andreas Rheinhardt20f97272022-03-16 20:09:54449 .p.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P,
Jun Zhaode5e2572023-04-18 09:08:52450 AV_PIX_FMT_YUVJ420P,
Martin Storsjö58a840e2015-03-17 12:40:40451 AV_PIX_FMT_NONE },
Linjie Fu433ece82020-04-29 03:00:46452 .defaults = svc_enc_defaults,
Andreas Rheinhardt20f97272022-03-16 20:09:54453 .p.priv_class = &class,
454 .p.wrapper_name = "libopenh264",
Martin Storsjö8a3d9ca2013-12-12 15:13:55455};