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 {
139 }
140 param.iPicWidth = avctx->
width;
141 param.iPicHeight = avctx->
height;
144 param.iRCMode =
s->rc_mode;
145 if (avctx->
qmax >= 0)
147 if (avctx->
qmin >= 0)
148 param.iMinQp =
av_clip(avctx->
qmin, 1, param.iMaxQp);
149 param.iTemporalLayerNum = 1;
150 param.iSpatialLayerNum = 1;
151 param.bEnableDenoise = 0;
152 param.bEnableBackgroundDetection = 1;
153 param.bEnableAdaptiveQuant = 1;
154 param.bEnableFrameSkip =
s->skip_frames;
155 param.bEnableLongTermReference = 0;
156 param.iLtrMarkPeriod = 30;
158 param.uiIntraPeriod = avctx->
gop_size;
159 #if OPENH264_VER_AT_LEAST(1, 4)
160 param.eSpsPpsIdStrategy = CONSTANT_ID;
161 #else
162 param.bEnableSpsPpsIdAddition = 0;
163 #endif
164 param.bPrefixNalAddingCtrl = 0;
165 param.iLoopFilterDisableIdc = !
s->loopfilter;
166 param.iEntropyCodingModeFlag =
s->coder >= 0 ?
s->coder : 1;
168
169 /* Allow specifying the libopenh264 profile through AVCodecContext. */
177 break;
178 default:
180 "Unsupported avctx->profile: %d.\n", avctx->
profile);
181 break;
182 }
183
186 #if OPENH264_VER_AT_LEAST(1, 8)
188 #else
190 #endif
191
192 switch (
s->profile) {
195 "select EProfileIdc PRO_HIGH in libopenh264.\n",
196 param.iEntropyCodingModeFlag ? "CABAC" : "CAVLC");
197 break;
200 "select EProfileIdc PRO_MAIN in libopenh264.\n",
201 param.iEntropyCodingModeFlag ? "CABAC" : "CAVLC");
202 break;
206 param.iEntropyCodingModeFlag = 0;
208 "select EProfileIdc PRO_BASELINE in libopenh264.\n");
209 break;
210 default:
212 param.iEntropyCodingModeFlag = 0;
214 "select EProfileIdc PRO_BASELINE in libopenh264.\n");
215 break;
216 }
217
218 param.sSpatialLayers[0].iVideoWidth = param.iPicWidth;
219 param.sSpatialLayers[0].iVideoHeight = param.iPicHeight;
220 param.sSpatialLayers[0].fFrameRate = param.fMaxFrameRate;
221 param.sSpatialLayers[0].iSpatialBitrate = param.iTargetBitrate;
222 param.sSpatialLayers[0].iMaxSpatialBitrate = param.iMaxBitrate;
223 param.sSpatialLayers[0].uiProfileIdc =
s->profile;
224
225 #if OPENH264_VER_AT_LEAST(1, 7)
227 // Table E-1.
229 { 0, 0 }, // Unspecified (never written here).
230 { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
231 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
232 { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
233 { 160, 99 }, // Last 3 are unknown to openh264: { 4, 3 }, { 3, 2 }, { 2, 1 },
234 };
235 static const ESampleAspectRatio asp_idc[] = {
236 ASP_UNSPECIFIED,
237 ASP_1x1, ASP_12x11, ASP_10x11, ASP_16x11,
238 ASP_40x33, ASP_24x11, ASP_20x11, ASP_32x11,
239 ASP_80x33, ASP_18x11, ASP_15x11, ASP_64x33,
240 ASP_160x99,
241 };
243
246
248 if (num == sar_idc[
i].num &&
249 den == sar_idc[
i].den)
250 break;
251 }
253 param.sSpatialLayers[0].eAspectRatio = ASP_EXT_SAR;
254 param.sSpatialLayers[0].sAspectRatioExtWidth = num;
255 param.sSpatialLayers[0].sAspectRatioExtHeight = den;
256 } else {
257 param.sSpatialLayers[0].eAspectRatio = asp_idc[
i];
258 }
259 param.sSpatialLayers[0].bAspectRatioPresent = true;
260 } else {
261 param.sSpatialLayers[0].bAspectRatioPresent = false;
262 }
263 #endif
264
265 if ((avctx->
slices > 1) && (
s->max_nal_size)) {
267 "Invalid combination -slices %d and -max_nal_size %d.\n",
268 avctx->
slices,
s->max_nal_size);
270 }
271
273 s->slice_mode = SM_FIXEDSLCNUM_SLICE;
274
277
278 #if OPENH264_VER_AT_LEAST(1, 6)
279 param.sSpatialLayers[0].sSliceArgument.uiSliceMode =
s->slice_mode;
280 param.sSpatialLayers[0].sSliceArgument.uiSliceNum = avctx->
slices;
281 #else
282 param.sSpatialLayers[0].sSliceCfg.uiSliceMode =
s->slice_mode;
283 param.sSpatialLayers[0].sSliceCfg.sSliceArgument.uiSliceNum = avctx->
slices;
284 #endif
285 if (avctx->
slices == 0 &&
s->slice_mode == SM_FIXEDSLCNUM_SLICE)
287
289 if (
s->max_nal_size) {
290 param.uiMaxNalSize =
s->max_nal_size;
291 #if OPENH264_VER_AT_LEAST(1, 6)
292 param.sSpatialLayers[0].sSliceArgument.uiSliceSizeConstraint =
s->max_nal_size;
293 #else
294 param.sSpatialLayers[0].sSliceCfg.sSliceArgument.uiSliceSizeConstraint =
s->max_nal_size;
295 #endif
296 } else {
298 "specify a valid max_nal_size to use -slice_mode dyn\n");
300 }
301 }
302
303 #if OPENH264_VER_AT_LEAST(1, 6)
304 param.sSpatialLayers[0].uiVideoFormat = VF_UNDEF;
305
309 param.sSpatialLayers[0].bFullRange = 1;
310
314 param.sSpatialLayers[0].bColorDescriptionPresent = true;
315 }
316
318 param.sSpatialLayers[0].uiColorMatrix = avctx->
colorspace;
322 param.sSpatialLayers[0].uiTransferCharacteristics = avctx->
color_trc;
323
324 param.sSpatialLayers[0].bVideoSignalTypePresent =
325 (param.sSpatialLayers[0].bFullRange || param.sSpatialLayers[0].bColorDescriptionPresent);
326 #endif
327
328 if ((*
s->encoder)->InitializeExt(
s->encoder, ¶m) != cmResultSuccess) {
331 }
332
334 SFrameBSInfo fbi = { 0 };
336 (*
s->encoder)->EncodeParameterSets(
s->encoder, &fbi);
337 for (
i = 0;
i < fbi.sLayerInfo[0].iNalCount;
i++)
338 size += fbi.sLayerInfo[0].pNalLengthInByte[
i];
344 }
345
347 if (!props)
351
352 return 0;
353 }
354
357 {
359 SFrameBSInfo fbi = { 0 };
361 int encoded;
362 SSourcePicture sp = { 0 };
363 int size = 0, layer, first_layer = 0;
364 int layer_size[MAX_LAYER_NUM_OF_FRAME] = { 0 };
365
366 sp.iColorFormat = videoFormatI420;
367 for (
i = 0;
i < 3;
i++) {
368 sp.iStride[
i] =
frame->linesize[
i];
370 }
371 sp.iPicWidth = avctx->
width;
372 sp.iPicHeight = avctx->
height;
373
375 (*
s->encoder)->ForceIntraFrame(
s->encoder,
true);
376 }
377
378 encoded = (*
s->encoder)->EncodeFrame(
s->encoder, &sp, &fbi);
379 if (encoded != cmResultSuccess) {
382 }
383 if (fbi.eFrameType == videoFrameTypeSkip) {
386 return 0;
387 }
388 first_layer = 0;
389 // Normal frames are returned with one single layer, while IDR
390 // frames have two layers, where the first layer contains the SPS/PPS.
391 // If using global headers, don't include the SPS/PPS in the returned
392 // packet - thus, only return one layer.
394 first_layer = fbi.iLayerNum - 1;
395
396 for (layer = first_layer; layer < fbi.iLayerNum; layer++) {
397 for (
i = 0;
i < fbi.sLayerInfo[layer].iNalCount;
i++)
398 layer_size[layer] += fbi.sLayerInfo[layer].pNalLengthInByte[
i];
399 size += layer_size[layer];
400 }
401 av_log(avctx,
AV_LOG_DEBUG,
"%d slices\n", fbi.sLayerInfo[fbi.iLayerNum - 1].iNalCount);
402
405
407 for (layer = first_layer; layer < fbi.iLayerNum; layer++) {
408 memcpy(avpkt->
data +
size, fbi.sLayerInfo[layer].pBsBuf, layer_size[layer]);
409 size += layer_size[layer];
410 }
412 if (fbi.eFrameType == videoFrameTypeIDR)
414 *got_packet = 1;
415 return 0;
416 }
417
419 { "b", "0" },
420 { "g", "-1" },
421 { "qmin", "-1" },
422 { "qmax", "-1" },
424 };
425
427 .
p.
name =
"libopenh264",
442 .p.priv_class = &class,
443 .p.wrapper_name = "libopenh264",
444 };