22 #include <wels/codec_api.h>
23 #include <wels/codec_ver.h>
38 #if !OPENH264_VER_AT_LEAST(1, 6)
39 #define SM_SIZELIMITED_SLICE SM_DYN_SLICE
42 #define TARGET_BITRATE_DEFAULT 2*1000*1000
59 #define OFFSET(x) offsetof(SVCContext, x)
60 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
61 #define DEPRECATED AV_OPT_FLAG_DEPRECATED
65 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, { .i64 = value }, 0, 0, VE, "profile"
70 {
"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 },
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" },
103 WelsDestroySVCEncoder(
s->encoder);
112 SEncParamExt param = { 0 };
115 WelsTraceCallback callback_function;
121 if (WelsCreateSVCEncoder(&
s->encoder)) {
127 log_level = WELS_LOG_DETAIL;
128 (*
s->encoder)->SetOption(
s->encoder, ENCODER_OPTION_TRACE_LEVEL, &log_level);
132 (*
s->encoder)->SetOption(
s->encoder, ENCODER_OPTION_TRACE_CALLBACK, &callback_function);
135 (*
s->encoder)->SetOption(
s->encoder, ENCODER_OPTION_TRACE_CALLBACK_CONTEXT, &avctx);
137 (*
s->encoder)->GetDefaultParams(
s->encoder, ¶m);
144 #if FF_API_TICKS_PER_FRAME
150 param.iPicWidth = avctx->
width;
151 param.iPicHeight = avctx->
height;
154 param.iRCMode =
s->rc_mode;
155 if (avctx->
qmax >= 0)
157 if (avctx->
qmin >= 0)
158 param.iMinQp =
av_clip(avctx->
qmin, 1, param.iMaxQp);
159 param.iTemporalLayerNum = 1;
160 param.iSpatialLayerNum = 1;
161 param.bEnableDenoise = 0;
162 param.bEnableBackgroundDetection = 1;
163 param.bEnableAdaptiveQuant = 1;
164 param.bEnableFrameSkip =
s->skip_frames;
165 param.bEnableLongTermReference = 0;
166 param.iLtrMarkPeriod = 30;
168 param.uiIntraPeriod = avctx->
gop_size;
169 #if OPENH264_VER_AT_LEAST(1, 4)
170 param.eSpsPpsIdStrategy = CONSTANT_ID;
172 param.bEnableSpsPpsIdAddition = 0;
174 param.bPrefixNalAddingCtrl = 0;
175 param.iLoopFilterDisableIdc = !
s->loopfilter;
176 param.iEntropyCodingModeFlag =
s->coder >= 0 ?
s->coder : 1;
190 "Unsupported avctx->profile: %d.\n", avctx->
profile);
196 #if OPENH264_VER_AT_LEAST(1, 8)
202 switch (
s->profile) {
205 "select EProfileIdc PRO_HIGH in libopenh264.\n",
206 param.iEntropyCodingModeFlag ?
"CABAC" :
"CAVLC");
210 "select EProfileIdc PRO_MAIN in libopenh264.\n",
211 param.iEntropyCodingModeFlag ?
"CABAC" :
"CAVLC");
216 param.iEntropyCodingModeFlag = 0;
218 "select EProfileIdc PRO_BASELINE in libopenh264.\n");
222 param.iEntropyCodingModeFlag = 0;
224 "select EProfileIdc PRO_BASELINE in libopenh264.\n");
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;
233 param.sSpatialLayers[0].uiProfileIdc =
s->profile;
235 #if OPENH264_VER_AT_LEAST(1, 7)
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 },
245 static const ESampleAspectRatio asp_idc[] = {
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,
258 if (num == sar_idc[
i].num &&
259 den == sar_idc[
i].den)
263 param.sSpatialLayers[0].eAspectRatio = ASP_EXT_SAR;
264 param.sSpatialLayers[0].sAspectRatioExtWidth = num;
265 param.sSpatialLayers[0].sAspectRatioExtHeight = den;
267 param.sSpatialLayers[0].eAspectRatio = asp_idc[
i];
269 param.sSpatialLayers[0].bAspectRatioPresent =
true;
271 param.sSpatialLayers[0].bAspectRatioPresent =
false;
275 if ((avctx->
slices > 1) && (
s->max_nal_size)) {
277 "Invalid combination -slices %d and -max_nal_size %d.\n",
278 avctx->
slices,
s->max_nal_size);
283 s->slice_mode = SM_FIXEDSLCNUM_SLICE;
288 #if OPENH264_VER_AT_LEAST(1, 6)
289 param.sSpatialLayers[0].sSliceArgument.uiSliceMode =
s->slice_mode;
290 param.sSpatialLayers[0].sSliceArgument.uiSliceNum = avctx->
slices;
292 param.sSpatialLayers[0].sSliceCfg.uiSliceMode =
s->slice_mode;
293 param.sSpatialLayers[0].sSliceCfg.sSliceArgument.uiSliceNum = avctx->
slices;
295 if (avctx->
slices == 0 &&
s->slice_mode == SM_FIXEDSLCNUM_SLICE)
299 if (
s->max_nal_size) {
300 param.uiMaxNalSize =
s->max_nal_size;
301 #if OPENH264_VER_AT_LEAST(1, 6)
302 param.sSpatialLayers[0].sSliceArgument.uiSliceSizeConstraint =
s->max_nal_size;
304 param.sSpatialLayers[0].sSliceCfg.sSliceArgument.uiSliceSizeConstraint =
s->max_nal_size;
308 "specify a valid max_nal_size to use -slice_mode dyn\n");
313 #if OPENH264_VER_AT_LEAST(1, 6)
314 param.sSpatialLayers[0].uiVideoFormat = VF_UNDEF;
319 param.sSpatialLayers[0].bFullRange = 1;
324 param.sSpatialLayers[0].bColorDescriptionPresent =
true;
328 param.sSpatialLayers[0].uiColorMatrix = avctx->
colorspace;
332 param.sSpatialLayers[0].uiTransferCharacteristics = avctx->
color_trc;
334 param.sSpatialLayers[0].bVideoSignalTypePresent =
335 (param.sSpatialLayers[0].bFullRange || param.sSpatialLayers[0].bColorDescriptionPresent);
338 if ((*
s->encoder)->InitializeExt(
s->encoder, ¶m) != cmResultSuccess) {
344 SFrameBSInfo fbi = { 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];
369 SFrameBSInfo fbi = { 0 };
372 SSourcePicture
sp = { 0 };
373 int size = 0, layer, first_layer = 0;
374 int layer_size[MAX_LAYER_NUM_OF_FRAME] = { 0 };
376 sp.iColorFormat = videoFormatI420;
377 for (
i = 0;
i < 3;
i++) {
385 (*
s->encoder)->ForceIntraFrame(
s->encoder,
true);
388 encoded = (*
s->encoder)->EncodeFrame(
s->encoder, &
sp, &fbi);
389 if (encoded != cmResultSuccess) {
393 if (fbi.eFrameType == videoFrameTypeSkip) {
404 first_layer = fbi.iLayerNum - 1;
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];
411 av_log(avctx,
AV_LOG_DEBUG,
"%d slices\n", fbi.sLayerInfo[fbi.iLayerNum - 1].iNalCount);
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];
422 if (fbi.eFrameType == videoFrameTypeIDR)
437 .
p.
name =
"libopenh264",
453 .p.priv_class = &
class,
454 .p.wrapper_name =
"libopenh264",