1 /*
2 * OpenH264 video encoder
3 * Copyright (C) 2014 Martin Storsjo
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * 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 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * 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
18 * License along with FFmpeg; if not, write to the Free Software
19 * 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
32
37
38 #if !OPENH264_VER_AT_LEAST(1, 6)
39 #define SM_SIZELIMITED_SLICE SM_DYN_SLICE
40 #endif
41
42 #define TARGET_BITRATE_DEFAULT 2*1000*1000
43
54
55 // rate control mode
58
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"
69 #undef 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 },
78
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" },
86 #endif
87
89 };
90
96 };
97
99 {
101
103 WelsDestroySVCEncoder(
s->encoder);
106 return 0;
107 }
108
110 {
112 SEncParamExt param = { 0 };
113 int log_level;
114 WelsTraceCallback callback_function;
116
117 if (WelsCreateSVCEncoder(&
s->encoder)) {
120 }
121
122 // Pass all libopenh264 messages to our callback, to allow ourselves to filter them.
123 log_level = WELS_LOG_DETAIL;
124 (*
s->encoder)->SetOption(
s->encoder, ENCODER_OPTION_TRACE_LEVEL, &log_level);
125
126 // Set the logging callback function to one that uses av_log() (see implementation above).
128 (*
s->encoder)->SetOption(
s->encoder, ENCODER_OPTION_TRACE_CALLBACK, &callback_function);
129
130 // Set the AVCodecContext as the libopenh264 callback context so that it can be passed to av_log().
131 (*
s->encoder)->SetOption(
s->encoder, ENCODER_OPTION_TRACE_CALLBACK_CONTEXT, &avctx);
132
133 (*
s->encoder)->GetDefaultParams(
s->encoder, ¶m);
134
137 } else {
140 #if FF_API_TICKS_PER_FRAME
142 #endif
143 ;
145 }
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;
167 #else
168 param.bEnableSpsPpsIdAddition = 0;
169 #endif
170 param.bPrefixNalAddingCtrl = 0;
171 param.iLoopFilterDisableIdc = !
s->loopfilter;
172 param.iEntropyCodingModeFlag =
s->coder >= 0 ?
s->coder : 1;
174
175 /* Allow specifying the libopenh264 profile through AVCodecContext. */
183 break;
184 default:
186 "Unsupported avctx->profile: %d.\n", avctx->
profile);
187 break;
188 }
189
192 #if OPENH264_VER_AT_LEAST(1, 8)
194 #else
196 #endif
197
198 switch (
s->profile) {
201 "select EProfileIdc PRO_HIGH in libopenh264.\n",
202 param.iEntropyCodingModeFlag ? "CABAC" : "CAVLC");
203 break;
206 "select EProfileIdc PRO_MAIN in libopenh264.\n",
207 param.iEntropyCodingModeFlag ? "CABAC" : "CAVLC");
208 break;
212 param.iEntropyCodingModeFlag = 0;
214 "select EProfileIdc PRO_BASELINE in libopenh264.\n");
215 break;
216 default:
218 param.iEntropyCodingModeFlag = 0;
220 "select EProfileIdc PRO_BASELINE in libopenh264.\n");
221 break;
222 }
223
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;
230
231 #if OPENH264_VER_AT_LEAST(1, 7)
233 // Table E-1.
235 { 0, 0 }, // Unspecified (never written here).
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 },
239 { 160, 99 }, // Last 3 are unknown to openh264: { 4, 3 }, { 3, 2 }, { 2, 1 },
240 };
241 static const ESampleAspectRatio asp_idc[] = {
242 ASP_UNSPECIFIED,
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,
246 ASP_160x99,
247 };
249
252
254 if (num == sar_idc[
i].num &&
255 den == sar_idc[
i].den)
256 break;
257 }
259 param.sSpatialLayers[0].eAspectRatio = ASP_EXT_SAR;
260 param.sSpatialLayers[0].sAspectRatioExtWidth = num;
261 param.sSpatialLayers[0].sAspectRatioExtHeight = den;
262 } else {
263 param.sSpatialLayers[0].eAspectRatio = asp_idc[
i];
264 }
265 param.sSpatialLayers[0].bAspectRatioPresent = true;
266 } else {
267 param.sSpatialLayers[0].bAspectRatioPresent = false;
268 }
269 #endif
270
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);
276 }
277
279 s->slice_mode = SM_FIXEDSLCNUM_SLICE;
280
283
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;
287 #else
288 param.sSpatialLayers[0].sSliceCfg.uiSliceMode =
s->slice_mode;
289 param.sSpatialLayers[0].sSliceCfg.sSliceArgument.uiSliceNum = avctx->
slices;
290 #endif
291 if (avctx->
slices == 0 &&
s->slice_mode == SM_FIXEDSLCNUM_SLICE)
293
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;
299 #else
300 param.sSpatialLayers[0].sSliceCfg.sSliceArgument.uiSliceSizeConstraint =
s->max_nal_size;
301 #endif
302 } else {
304 "specify a valid max_nal_size to use -slice_mode dyn\n");
306 }
307 }
308
309 #if OPENH264_VER_AT_LEAST(1, 6)
310 param.sSpatialLayers[0].uiVideoFormat = VF_UNDEF;
311
315 param.sSpatialLayers[0].bFullRange = 1;
316
320 param.sSpatialLayers[0].bColorDescriptionPresent = true;
321 }
322
324 param.sSpatialLayers[0].uiColorMatrix = avctx->
colorspace;
328 param.sSpatialLayers[0].uiTransferCharacteristics = avctx->
color_trc;
329
330 param.sSpatialLayers[0].bVideoSignalTypePresent =
331 (param.sSpatialLayers[0].bFullRange || param.sSpatialLayers[0].bColorDescriptionPresent);
332 #endif
333
334 if ((*
s->encoder)->InitializeExt(
s->encoder, ¶m) != cmResultSuccess) {
337 }
338
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];
350 }
351
353 if (!props)
357
358 return 0;
359 }
360
363 {
365 SFrameBSInfo fbi = { 0 };
367 int encoded;
368 SSourcePicture sp = { 0 };
369 int size = 0, layer, first_layer = 0;
370 int layer_size[MAX_LAYER_NUM_OF_FRAME] = { 0 };
371
372 sp.iColorFormat = videoFormatI420;
373 for (
i = 0;
i < 3;
i++) {
374 sp.iStride[
i] =
frame->linesize[
i];
376 }
377 sp.iPicWidth = avctx->
width;
378 sp.iPicHeight = avctx->
height;
379
381 (*
s->encoder)->ForceIntraFrame(
s->encoder,
true);
382 }
383
384 encoded = (*
s->encoder)->EncodeFrame(
s->encoder, &sp, &fbi);
385 if (encoded != cmResultSuccess) {
388 }
389 if (fbi.eFrameType == videoFrameTypeSkip) {
392 return 0;
393 }
394 first_layer = 0;
395 // Normal frames are returned with one single layer, while IDR
396 // frames have two layers, where the first layer contains the SPS/PPS.
397 // If using global headers, don't include the SPS/PPS in the returned
398 // packet - thus, only return one layer.
400 first_layer = fbi.iLayerNum - 1;
401
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];
406 }
407 av_log(avctx,
AV_LOG_DEBUG,
"%d slices\n", fbi.sLayerInfo[fbi.iLayerNum - 1].iNalCount);
408
411
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];
416 }
418 if (fbi.eFrameType == videoFrameTypeIDR)
420 *got_packet = 1;
421 return 0;
422 }
423
425 { "b", "0" },
426 { "g", "-1" },
427 { "qmin", "-1" },
428 { "qmax", "-1" },
430 };
431
433 .
p.
name =
"libopenh264",
450 .p.priv_class = &class,
451 .p.wrapper_name = "libopenh264",
452 };