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, .unit = "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, .unit =
"rc_mode" },
80 {
"off",
"bit rate control off", 0,
AV_OPT_TYPE_CONST, { .i64 = RC_OFF_MODE }, 0, 0,
VE, .unit =
"rc_mode" },
81 {
"quality",
"quality mode", 0,
AV_OPT_TYPE_CONST, { .i64 = RC_QUALITY_MODE }, 0, 0,
VE, .unit =
"rc_mode" },
82 {
"bitrate",
"bitrate mode", 0,
AV_OPT_TYPE_CONST, { .i64 = RC_BITRATE_MODE }, 0, 0,
VE, .unit =
"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, .unit =
"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, .unit =
"rc_mode" },
103 WelsDestroySVCEncoder(
s->encoder);
112 SEncParamExt param = { 0 };
114 WelsTraceCallback callback_function;
117 if (WelsCreateSVCEncoder(&
s->encoder)) {
123 log_level = WELS_LOG_DETAIL;
124 (*
s->encoder)->SetOption(
s->encoder, ENCODER_OPTION_TRACE_LEVEL, &log_level);
128 (*
s->encoder)->SetOption(
s->encoder, ENCODER_OPTION_TRACE_CALLBACK, &callback_function);
131 (*
s->encoder)->SetOption(
s->encoder, ENCODER_OPTION_TRACE_CALLBACK_CONTEXT, &avctx);
133 (*
s->encoder)->GetDefaultParams(
s->encoder, ¶m);
140 #if FF_API_TICKS_PER_FRAME
146 param.iPicWidth = avctx->
width;
147 param.iPicHeight = avctx->
height;
150 param.iRCMode =
s->rc_mode;
151 if (avctx->
qmax >= 0)
153 if (avctx->
qmin >= 0)
154 param.iMinQp =
av_clip(avctx->
qmin, 1, param.iMaxQp);
155 param.iTemporalLayerNum = 1;
156 param.iSpatialLayerNum = 1;
157 param.bEnableDenoise = 0;
158 param.bEnableBackgroundDetection = 1;
159 param.bEnableAdaptiveQuant = 1;
160 param.bEnableFrameSkip =
s->skip_frames;
161 param.bEnableLongTermReference = 0;
162 param.iLtrMarkPeriod = 30;
164 param.uiIntraPeriod = avctx->
gop_size;
165 #if OPENH264_VER_AT_LEAST(1, 4)
166 param.eSpsPpsIdStrategy = CONSTANT_ID;
168 param.bEnableSpsPpsIdAddition = 0;
170 param.bPrefixNalAddingCtrl = 0;
171 param.iLoopFilterDisableIdc = !
s->loopfilter;
172 param.iEntropyCodingModeFlag =
s->coder >= 0 ?
s->coder : 1;
186 "Unsupported avctx->profile: %d.\n", avctx->
profile);
192 #if OPENH264_VER_AT_LEAST(1, 8)
198 switch (
s->profile) {
201 "select EProfileIdc PRO_HIGH in libopenh264.\n",
202 param.iEntropyCodingModeFlag ?
"CABAC" :
"CAVLC");
206 "select EProfileIdc PRO_MAIN in libopenh264.\n",
207 param.iEntropyCodingModeFlag ?
"CABAC" :
"CAVLC");
212 param.iEntropyCodingModeFlag = 0;
214 "select EProfileIdc PRO_BASELINE in libopenh264.\n");
218 param.iEntropyCodingModeFlag = 0;
220 "select EProfileIdc PRO_BASELINE in libopenh264.\n");
224 param.sSpatialLayers[0].iVideoWidth = param.iPicWidth;
225 param.sSpatialLayers[0].iVideoHeight = param.iPicHeight;
226 param.sSpatialLayers[0].fFrameRate = param.fMaxFrameRate;
227 param.sSpatialLayers[0].iSpatialBitrate = param.iTargetBitrate;
228 param.sSpatialLayers[0].iMaxSpatialBitrate = param.iMaxBitrate;
229 param.sSpatialLayers[0].uiProfileIdc =
s->profile;
231 #if OPENH264_VER_AT_LEAST(1, 7)
236 { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
237 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
238 { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
241 static const ESampleAspectRatio asp_idc[] = {
243 ASP_1x1, ASP_12x11, ASP_10x11, ASP_16x11,
244 ASP_40x33, ASP_24x11, ASP_20x11, ASP_32x11,
245 ASP_80x33, ASP_18x11, ASP_15x11, ASP_64x33,
254 if (num == sar_idc[
i].num &&
255 den == sar_idc[
i].den)
259 param.sSpatialLayers[0].eAspectRatio = ASP_EXT_SAR;
260 param.sSpatialLayers[0].sAspectRatioExtWidth = num;
261 param.sSpatialLayers[0].sAspectRatioExtHeight = den;
263 param.sSpatialLayers[0].eAspectRatio = asp_idc[
i];
265 param.sSpatialLayers[0].bAspectRatioPresent =
true;
267 param.sSpatialLayers[0].bAspectRatioPresent =
false;
271 if ((avctx->
slices > 1) && (
s->max_nal_size)) {
273 "Invalid combination -slices %d and -max_nal_size %d.\n",
274 avctx->
slices,
s->max_nal_size);
279 s->slice_mode = SM_FIXEDSLCNUM_SLICE;
284 #if OPENH264_VER_AT_LEAST(1, 6)
285 param.sSpatialLayers[0].sSliceArgument.uiSliceMode =
s->slice_mode;
286 param.sSpatialLayers[0].sSliceArgument.uiSliceNum = avctx->
slices;
288 param.sSpatialLayers[0].sSliceCfg.uiSliceMode =
s->slice_mode;
289 param.sSpatialLayers[0].sSliceCfg.sSliceArgument.uiSliceNum = avctx->
slices;
291 if (avctx->
slices == 0 &&
s->slice_mode == SM_FIXEDSLCNUM_SLICE)
295 if (
s->max_nal_size) {
296 param.uiMaxNalSize =
s->max_nal_size;
297 #if OPENH264_VER_AT_LEAST(1, 6)
298 param.sSpatialLayers[0].sSliceArgument.uiSliceSizeConstraint =
s->max_nal_size;
300 param.sSpatialLayers[0].sSliceCfg.sSliceArgument.uiSliceSizeConstraint =
s->max_nal_size;
304 "specify a valid max_nal_size to use -slice_mode dyn\n");
309 #if OPENH264_VER_AT_LEAST(1, 6)
310 param.sSpatialLayers[0].uiVideoFormat = VF_UNDEF;
315 param.sSpatialLayers[0].bFullRange = 1;
320 param.sSpatialLayers[0].bColorDescriptionPresent =
true;
324 param.sSpatialLayers[0].uiColorMatrix = avctx->
colorspace;
328 param.sSpatialLayers[0].uiTransferCharacteristics = avctx->
color_trc;
330 param.sSpatialLayers[0].bVideoSignalTypePresent =
331 (param.sSpatialLayers[0].bFullRange || param.sSpatialLayers[0].bColorDescriptionPresent);
334 if ((*
s->encoder)->InitializeExt(
s->encoder, ¶m) != cmResultSuccess) {
340 SFrameBSInfo fbi = { 0 };
342 (*
s->encoder)->EncodeParameterSets(
s->encoder, &fbi);
343 for (
i = 0;
i < fbi.sLayerInfo[0].iNalCount;
i++)
344 size += fbi.sLayerInfo[0].pNalLengthInByte[
i];
365 SFrameBSInfo fbi = { 0 };
368 SSourcePicture sp = { 0 };
369 int size = 0, layer, first_layer = 0;
370 int layer_size[MAX_LAYER_NUM_OF_FRAME] = { 0 };
372 sp.iColorFormat = videoFormatI420;
373 for (
i = 0;
i < 3;
i++) {
374 sp.iStride[
i] =
frame->linesize[
i];
377 sp.iPicWidth = avctx->
width;
378 sp.iPicHeight = avctx->
height;
381 (*
s->encoder)->ForceIntraFrame(
s->encoder,
true);
384 encoded = (*
s->encoder)->EncodeFrame(
s->encoder, &sp, &fbi);
385 if (encoded != cmResultSuccess) {
389 if (fbi.eFrameType == videoFrameTypeSkip) {
400 first_layer = fbi.iLayerNum - 1;
402 for (layer = first_layer; layer < fbi.iLayerNum; layer++) {
403 for (
i = 0;
i < fbi.sLayerInfo[layer].iNalCount;
i++)
404 layer_size[layer] += fbi.sLayerInfo[layer].pNalLengthInByte[
i];
405 size += layer_size[layer];
407 av_log(avctx,
AV_LOG_DEBUG,
"%d slices\n", fbi.sLayerInfo[fbi.iLayerNum - 1].iNalCount);
413 for (layer = first_layer; layer < fbi.iLayerNum; layer++) {
414 memcpy(avpkt->
data +
size, fbi.sLayerInfo[layer].pBsBuf, layer_size[layer]);
415 size += layer_size[layer];
418 if (fbi.eFrameType == videoFrameTypeIDR)
433 .
p.
name =
"libopenh264",
450 .p.priv_class = &
class,
451 .p.wrapper_name =
"libopenh264",