From patchwork Tue Mar 31 15:31:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fu, Linjie" X-Patchwork-Id: 18548 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id F120444BC7F for ; Tue, 31 Mar 2020 18:37:59 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DBA8368B0BF; Tue, 31 Mar 2020 18:37:59 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 837FA68B096 for ; Tue, 31 Mar 2020 18:37:52 +0300 (EEST) IronPort-SDR: Qj1emrogw5HXiT9dmEPj3Z4Y/JIDcPHL4T+twVXXnuhy7SlzRPr2BRFiMLHcMlLhSTevvoAsgE ERIZlfjykeMQ== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Mar 2020 08:37:50 -0700 IronPort-SDR: mLxky9MX6gO2/XKtRULcDCa6Ft7L6rYcSul9iH386Eb4iZu+tw5CiI30CZdfa4Sb12OHnClPMW xlIcHDpatYIg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,328,1580803200"; d="scan'208";a="241946126" Received: from icl-dev.sh.intel.com ([10.239.158.73]) by fmsmga008.fm.intel.com with ESMTP; 31 Mar 2020 08:37:49 -0700 From: Linjie Fu To: ffmpeg-devel@ffmpeg.org Date: Tue, 31 Mar 2020 23:31:38 +0800 Message-Id: <1585668698-29401-1-git-send-email-linjie.fu@intel.com> X-Mailer: git-send-email 2.7.4 Subject: [FFmpeg-devel] [PATCH 1/7] lavc/libopenh264enc: Add default qmin/qmax support X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Linjie Fu MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Set default QP range to (1, 51) instead of (2, 32). QP = 0 is not well supported currently in libopenh264. If iMaxQp/iMinQp equals 0, the QP range would be changed unexpectedly inside libopenh264 with a warning: Warning:Change QP Range from(0,51) to (12,42) Signed-off-by: Linjie Fu --- [1] [2] libavcodec/libopenh264enc.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/libavcodec/libopenh264enc.c b/libavcodec/libopenh264enc.c index ae6d17c..7cd1efe 100644 --- a/libavcodec/libopenh264enc.c +++ b/libavcodec/libopenh264enc.c @@ -135,6 +135,9 @@ FF_ENABLE_DEPRECATION_WARNINGS param.iTargetBitrate = avctx->bit_rate; param.iMaxBitrate = FFMAX(avctx->rc_max_rate, avctx->bit_rate); param.iRCMode = RC_QUALITY_MODE; + // QP = 0 is not well supported, so default to (1, 51) + param.iMaxQp = avctx->qmax >= 0 ? av_clip(avctx->qmax, 1, 51) : 51; + param.iMinQp = avctx->qmin >= 0 ? av_clip(avctx->qmin, 1, avctx->qmax) : 1; param.iTemporalLayerNum = 1; param.iSpatialLayerNum = 1; param.bEnableDenoise = 0; @@ -332,6 +335,12 @@ static int svc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, return 0; } +static const AVCodecDefault svc_enc_defaults[] = { + { "qmin", "-1" }, + { "qmax", "-1" }, + { NULL }, +}; + AVCodec ff_libopenh264_encoder = { .name = "libopenh264", .long_name = NULL_IF_CONFIG_SMALL("OpenH264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"), @@ -345,6 +354,7 @@ AVCodec ff_libopenh264_encoder = { .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP, .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE }, + .defaults = svc_enc_defaults, .priv_class = &class, .wrapper_name = "libopenh264", }; From patchwork Tue Mar 31 15:31:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fu, Linjie" X-Patchwork-Id: 18549 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 01C8944BC7F for ; Tue, 31 Mar 2020 18:38:09 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DDB9668AE45; Tue, 31 Mar 2020 18:38:08 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4857168AC53 for ; Tue, 31 Mar 2020 18:38:06 +0300 (EEST) IronPort-SDR: 2iz4HBbKS53otSNBfl5LwsbUZuAbOr5agUyz+6oACkXpREwO/SV9gZDLzsI+/oRiWwOMCskcs4 TiAKxlpLphkA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Mar 2020 08:38:05 -0700 IronPort-SDR: jNcDoeGExlrOCe8xSUInhZSUx5g/KTCZJquau698ru5Njo/7sfFYs7lZs1wCV4gFOJg8x65Lw+ oEbbqpIveHUg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,328,1580803200"; d="scan'208";a="241946162" Received: from icl-dev.sh.intel.com ([10.239.158.73]) by fmsmga008.fm.intel.com with ESMTP; 31 Mar 2020 08:38:04 -0700 From: Linjie Fu To: ffmpeg-devel@ffmpeg.org Date: Tue, 31 Mar 2020 23:31:52 +0800 Message-Id: <1585668712-29510-1-git-send-email-linjie.fu@intel.com> X-Mailer: git-send-email 2.7.4 Subject: [FFmpeg-devel] [PATCH 2/7] lavc/libopenh264enc: fix the if-else coding style X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Linjie Fu MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Linjie Fu --- libavcodec/libopenh264enc.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libavcodec/libopenh264enc.c b/libavcodec/libopenh264enc.c index 7cd1efe..47d66e4 100644 --- a/libavcodec/libopenh264enc.c +++ b/libavcodec/libopenh264enc.c @@ -202,8 +202,7 @@ FF_ENABLE_DEPRECATION_WARNINGS param.sSpatialLayers[0].eAspectRatio = asp_idc[i]; } param.sSpatialLayers[0].bAspectRatioPresent = true; - } - else { + } else { param.sSpatialLayers[0].bAspectRatioPresent = false; } #endif @@ -230,7 +229,7 @@ FF_ENABLE_DEPRECATION_WARNINGS #endif if (s->slice_mode == SM_SIZELIMITED_SLICE) { - if (s->max_nal_size){ + if (s->max_nal_size) { param.uiMaxNalSize = s->max_nal_size; #if OPENH264_VER_AT_LEAST(1, 6) param.sSpatialLayers[0].sSliceArgument.uiSliceSizeConstraint = s->max_nal_size; From patchwork Tue Mar 31 15:32:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fu, Linjie" X-Patchwork-Id: 18550 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 1E30144BC7F for ; Tue, 31 Mar 2020 18:38:34 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EBFCB68B01C; Tue, 31 Mar 2020 18:38:33 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 44C6368A7E1 for ; Tue, 31 Mar 2020 18:38:27 +0300 (EEST) IronPort-SDR: wkRCmi0/+oFb5Q0VkmHRPcsTuX7fP15edf3r7Gh0AiCFu2GQRodvc8dbTP+tIYdT9MQ1BKWLIq bMfaXTAnsUDw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Mar 2020 08:38:25 -0700 IronPort-SDR: dcCwdw6GBO5zhArE4vjpRQneaX1808ugFNXaeztKAed7k4tSQCS7dvkDbZIU7EzpBrlbt+n/rq KwJeEXUyMOQg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,328,1580803200"; d="scan'208";a="241946193" Received: from icl-dev.sh.intel.com ([10.239.158.73]) by fmsmga008.fm.intel.com with ESMTP; 31 Mar 2020 08:38:24 -0700 From: Linjie Fu To: ffmpeg-devel@ffmpeg.org Date: Tue, 31 Mar 2020 23:32:12 +0800 Message-Id: <1585668732-29647-1-git-send-email-linjie.fu@intel.com> X-Mailer: git-send-email 2.7.4 Subject: [FFmpeg-devel] [PATCH 3/7] lavc/libopenh264enc: add default gop size and bit rate X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Linjie Fu MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Linjie Fu --- libavcodec/libopenh264enc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libavcodec/libopenh264enc.c b/libavcodec/libopenh264enc.c index 47d66e4..fcf3e4d 100644 --- a/libavcodec/libopenh264enc.c +++ b/libavcodec/libopenh264enc.c @@ -335,6 +335,8 @@ static int svc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, } static const AVCodecDefault svc_enc_defaults[] = { + { "b", "2M" }, + { "g", "120" }, { "qmin", "-1" }, { "qmax", "-1" }, { NULL }, From patchwork Tue Mar 31 15:32:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fu, Linjie" X-Patchwork-Id: 18551 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 1C8F844BC7F for ; Tue, 31 Mar 2020 18:38:49 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 09BFB68AFEB; Tue, 31 Mar 2020 18:38:49 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A342F68B0BF for ; Tue, 31 Mar 2020 18:38:46 +0300 (EEST) IronPort-SDR: e594SpheIwdVI7BMcIo8gf7nWQz+sA7vM0UtW/FD2mNFXKoc/b2tH/HsQUFA23M9NVbiPnjs83 orZUN6Q+nF9Q== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Mar 2020 08:38:45 -0700 IronPort-SDR: zcKvMEjMFBAqliV5mriUH59Hl4OEpRzbcURvCK5gYkZIPT4XRe6GJow2+LCE09HKl0WxNLYubW YPMohI+NXsfQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,328,1580803200"; d="scan'208";a="241946254" Received: from icl-dev.sh.intel.com ([10.239.158.73]) by fmsmga008.fm.intel.com with ESMTP; 31 Mar 2020 08:38:44 -0700 From: Linjie Fu To: ffmpeg-devel@ffmpeg.org Date: Tue, 31 Mar 2020 23:32:32 +0800 Message-Id: <1585668752-29778-1-git-send-email-linjie.fu@intel.com> X-Mailer: git-send-email 2.7.4 Subject: [FFmpeg-devel] [PATCH 4/7] lavc/libopenh264enc: add bit rate control select support X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Linjie Fu MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Linjie Fu --- libavcodec/libopenh264enc.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/libavcodec/libopenh264enc.c b/libavcodec/libopenh264enc.c index fcf3e4d..56703a4 100644 --- a/libavcodec/libopenh264enc.c +++ b/libavcodec/libopenh264enc.c @@ -47,6 +47,9 @@ typedef struct SVCContext { int skip_frames; int skipped; int cabac; + + // rate control mode + int rc_mode; } SVCContext; #define OFFSET(x) offsetof(SVCContext, x) @@ -71,6 +74,13 @@ static const AVOption options[] = { { "max_nal_size", "set maximum NAL size in bytes", OFFSET(max_nal_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE }, { "allow_skip_frames", "allow skipping frames to hit the target bitrate", OFFSET(skip_frames), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, { "cabac", "Enable cabac", OFFSET(cabac), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, + + { "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" }, + { "off", "bit rate control off", 0, AV_OPT_TYPE_CONST, { .i64 = RC_OFF_MODE }, 0, 0, VE, "rc_mode" }, + { "quality", "quality mode", 0, AV_OPT_TYPE_CONST, { .i64 = RC_QUALITY_MODE }, 0, 0, VE, "rc_mode" }, + { "bitrate", "bitrate mode", 0, AV_OPT_TYPE_CONST, { .i64 = RC_BITRATE_MODE }, 0, 0, VE, "rc_mode" }, + { "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" }, + { "timestamp", "bit rate control based on timestamp", 0, AV_OPT_TYPE_CONST, { .i64 = RC_TIMESTAMP_MODE }, 0, 0, VE, "rc_mode" }, { NULL } }; @@ -134,7 +144,7 @@ FF_ENABLE_DEPRECATION_WARNINGS param.iPicHeight = avctx->height; param.iTargetBitrate = avctx->bit_rate; param.iMaxBitrate = FFMAX(avctx->rc_max_rate, avctx->bit_rate); - param.iRCMode = RC_QUALITY_MODE; + param.iRCMode = s->rc_mode; // QP = 0 is not well supported, so default to (1, 51) param.iMaxQp = avctx->qmax >= 0 ? av_clip(avctx->qmax, 1, 51) : 51; param.iMinQp = avctx->qmin >= 0 ? av_clip(avctx->qmin, 1, avctx->qmax) : 1; From patchwork Tue Mar 31 15:32:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fu, Linjie" X-Patchwork-Id: 18552 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 2B7CA44BC8B for ; Tue, 31 Mar 2020 18:39:21 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0E71F68B0BF; Tue, 31 Mar 2020 18:39:21 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9A55468AFA2 for ; Tue, 31 Mar 2020 18:39:14 +0300 (EEST) IronPort-SDR: YbqboDayrtlJzLNRb0sYue/tGgZxSf1zjweKrp+zHFEIMFZ/gmVeqNgHPdYLPzK2tl+0A3AHgY PY5Nm9iW1qag== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Mar 2020 08:39:03 -0700 IronPort-SDR: HDCCuNzobFJUKkpNcjJmu4xPH9J3QuBYrrNqLpWf3i6h1NH2AZTM2aUj21SYM4IavKXhFQKPpC TpVP2jovr9LQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,328,1580803200"; d="scan'208";a="241946315" Received: from icl-dev.sh.intel.com ([10.239.158.73]) by fmsmga008.fm.intel.com with ESMTP; 31 Mar 2020 08:39:01 -0700 From: Linjie Fu To: ffmpeg-devel@ffmpeg.org Date: Tue, 31 Mar 2020 23:32:50 +0800 Message-Id: <1585668770-29906-1-git-send-email-linjie.fu@intel.com> X-Mailer: git-send-email 2.7.4 Subject: [FFmpeg-devel] [PATCH 5/7] lavc/libopenh264enc: prompt slice number changing according to cpus X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Linjie Fu MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Libopenh264enc would set the slice according to the number of cpu cores if uiSliceNum equals to 0 (auto) in SM_FIXEDSLCNUM_SLICE mode. Prompt a warning for user to catch this. Signed-off-by: Linjie Fu --- libavcodec/libopenh264enc.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libavcodec/libopenh264enc.c b/libavcodec/libopenh264enc.c index 56703a4..019093c 100644 --- a/libavcodec/libopenh264enc.c +++ b/libavcodec/libopenh264enc.c @@ -237,6 +237,9 @@ FF_ENABLE_DEPRECATION_WARNINGS param.sSpatialLayers[0].sSliceCfg.uiSliceMode = s->slice_mode; param.sSpatialLayers[0].sSliceCfg.sSliceArgument.uiSliceNum = avctx->slices; #endif + if (avctx->slices == 0 && s->slice_mode == SM_FIXEDSLCNUM_SLICE) + av_log(avctx, AV_LOG_WARNING, "Auto slice number, " + "default to use the number of CPU cores: %d\n", av_cpu_count()); if (s->slice_mode == SM_SIZELIMITED_SLICE) { if (s->max_nal_size) { From patchwork Tue Mar 31 15:33:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fu, Linjie" X-Patchwork-Id: 18553 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 319AB44BC8B for ; Tue, 31 Mar 2020 18:39:50 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 18D0C68B053; Tue, 31 Mar 2020 18:39:50 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 518F0689ECB for ; Tue, 31 Mar 2020 18:39:43 +0300 (EEST) IronPort-SDR: tlxdDZ95+KuZrZE/THRZlBzmlfC28YQr6SlvYQyb/+B0Kog1ivdEK6Zqq8BrT0x5yTgKgs2jdc grfLxO/cUytw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Mar 2020 08:39:41 -0700 IronPort-SDR: 9sOLEuUNw2IlAbOIn5FSFN+IkMmZ4jm0PtfTMJqlfUil0rW8tn0NYad6+XyEs4f4LBMQZYvJpd gAdYz5fyX0yA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,328,1580803200"; d="scan'208";a="241946470" Received: from icl-dev.sh.intel.com ([10.239.158.73]) by fmsmga008.fm.intel.com with ESMTP; 31 Mar 2020 08:39:40 -0700 From: Linjie Fu To: ffmpeg-devel@ffmpeg.org Date: Tue, 31 Mar 2020 23:33:22 +0800 Message-Id: <1585668802-30088-1-git-send-email-linjie.fu@intel.com> X-Mailer: git-send-email 2.7.4 Subject: [FFmpeg-devel] [PATCH 6/7] lavc/libopenh264enc: set slice_mode option to deprecated X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Linjie Fu MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" "slice_mode" seems to be unnecessary since it could be determined by -slices/max_nal_size. default: SM_FIXEDSLCNUM_SLICE mode with cpu-number slices. -slices N: SM_FIXEDSLCNUM_SLICE mode with N slices. -max_nal_size: SM_SIZELIMITED_SLICE mode with limited size slices. This could be removed later. Signed-off-by: Linjie Fu --- libavcodec/libopenh264enc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/libavcodec/libopenh264enc.c b/libavcodec/libopenh264enc.c index 019093c..692aba9 100644 --- a/libavcodec/libopenh264enc.c +++ b/libavcodec/libopenh264enc.c @@ -40,7 +40,7 @@ typedef struct SVCContext { const AVClass *av_class; ISVCEncoder *encoder; - int slice_mode; + int slice_mode; // deprecated int loopfilter; char *profile; int max_nal_size; @@ -54,11 +54,12 @@ typedef struct SVCContext { #define OFFSET(x) offsetof(SVCContext, x) #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM +#define DEPRECATED AV_OPT_FLAG_DEPRECATED static const AVOption options[] = { #if OPENH264_VER_AT_LEAST(1, 6) - { "slice_mode", "set slice mode", OFFSET(slice_mode), AV_OPT_TYPE_INT, { .i64 = SM_FIXEDSLCNUM_SLICE }, SM_SINGLE_SLICE, SM_RESERVED, VE, "slice_mode" }, + { "slice_mode", "set slice mode, use slices/max_nal_size", OFFSET(slice_mode), AV_OPT_TYPE_INT, { .i64 = SM_FIXEDSLCNUM_SLICE }, SM_SINGLE_SLICE, SM_RESERVED, VE|DEPRECATED, "slice_mode" }, #else - { "slice_mode", "set slice mode", OFFSET(slice_mode), AV_OPT_TYPE_INT, { .i64 = SM_AUTO_SLICE }, SM_SINGLE_SLICE, SM_RESERVED, VE, "slice_mode" }, + { "slice_mode", "set slice mode, use slices/max_nal_size", OFFSET(slice_mode), AV_OPT_TYPE_INT, { .i64 = SM_AUTO_SLICE }, SM_SINGLE_SLICE, SM_RESERVED, VE|DEPRECATED, "slice_mode" }, #endif { "fixed", "a fixed number of slices", 0, AV_OPT_TYPE_CONST, { .i64 = SM_FIXEDSLCNUM_SLICE }, 0, 0, VE, "slice_mode" }, #if OPENH264_VER_AT_LEAST(1, 6) From patchwork Tue Mar 31 15:33:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fu, Linjie" X-Patchwork-Id: 18555 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 70A3A44BDF2 for ; Tue, 31 Mar 2020 18:40:23 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4A48B68B4F3; Tue, 31 Mar 2020 18:40:23 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A0199689ECB for ; Tue, 31 Mar 2020 18:40:16 +0300 (EEST) IronPort-SDR: ctCnxx0JMfForfF9NMkmgDrbBAozLFDQX5D5Z+b5PtNcNM31mdN+/5DeVVf4bA3f1M1oaoAjZ/ WSoLLJ/OHR2g== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Mar 2020 08:39:58 -0700 IronPort-SDR: +PJbu9vTuqx0AKw9K5nuC3jEZrDQVu9JAAmbkZfDKhcTQ/sihd8Mjz9uc2sTIhi3kG6udGRbMM mZAeSQw2rhUQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,328,1580803200"; d="scan'208";a="241946528" Received: from icl-dev.sh.intel.com ([10.239.158.73]) by fmsmga008.fm.intel.com with ESMTP; 31 Mar 2020 08:39:57 -0700 From: Linjie Fu To: ffmpeg-devel@ffmpeg.org Date: Tue, 31 Mar 2020 23:33:45 +0800 Message-Id: <1585668825-30238-1-git-send-email-linjie.fu@intel.com> X-Mailer: git-send-email 2.7.4 Subject: [FFmpeg-devel] [PATCH 7/7] lavc/libopenh264enc: separate svc_encode_init() into several functions X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Linjie Fu MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Separate the initialization procedure into different functions. Make it more readable and easier to be extended. Signed-off-by: Linjie Fu --- libavcodec/libopenh264enc.c | 302 +++++++++++++++++++++++++++----------------- 1 file changed, 186 insertions(+), 116 deletions(-) diff --git a/libavcodec/libopenh264enc.c b/libavcodec/libopenh264enc.c index 692aba9..ab54454 100644 --- a/libavcodec/libopenh264enc.c +++ b/libavcodec/libopenh264enc.c @@ -85,6 +85,11 @@ static const AVOption options[] = { { NULL } }; +static av_cold int svc_encode_init_profile(AVCodecContext *avctx, SEncParamExt *param); +static av_cold int svc_encode_init_rate_control(AVCodecContext *avctx, SEncParamExt *param); +static av_cold int svc_encode_init_spatial_layer(AVCodecContext *avctx, SEncParamExt *param); +static av_cold int svc_encode_init_params(AVCodecContext *avctx, SEncParamExt *param); + static const AVClass class = { .class_name = "libopenh264enc", .item_name = av_default_item_name, @@ -140,122 +145,8 @@ FF_DISABLE_DEPRECATION_WARNINGS FF_ENABLE_DEPRECATION_WARNINGS #endif - param.fMaxFrameRate = 1/av_q2d(avctx->time_base); - param.iPicWidth = avctx->width; - param.iPicHeight = avctx->height; - param.iTargetBitrate = avctx->bit_rate; - param.iMaxBitrate = FFMAX(avctx->rc_max_rate, avctx->bit_rate); - param.iRCMode = s->rc_mode; - // QP = 0 is not well supported, so default to (1, 51) - param.iMaxQp = avctx->qmax >= 0 ? av_clip(avctx->qmax, 1, 51) : 51; - param.iMinQp = avctx->qmin >= 0 ? av_clip(avctx->qmin, 1, avctx->qmax) : 1; - param.iTemporalLayerNum = 1; - param.iSpatialLayerNum = 1; - param.bEnableDenoise = 0; - param.bEnableBackgroundDetection = 1; - param.bEnableAdaptiveQuant = 1; - param.bEnableFrameSkip = s->skip_frames; - param.bEnableLongTermReference = 0; - param.iLtrMarkPeriod = 30; - param.uiIntraPeriod = avctx->gop_size; -#if OPENH264_VER_AT_LEAST(1, 4) - param.eSpsPpsIdStrategy = CONSTANT_ID; -#else - param.bEnableSpsPpsIdAddition = 0; -#endif - param.bPrefixNalAddingCtrl = 0; - param.iLoopFilterDisableIdc = !s->loopfilter; - param.iEntropyCodingModeFlag = 0; - param.iMultipleThreadIdc = avctx->thread_count; - if (s->profile && !strcmp(s->profile, "main")) - param.iEntropyCodingModeFlag = 1; - else if (!s->profile && s->cabac) - param.iEntropyCodingModeFlag = 1; - - param.sSpatialLayers[0].iVideoWidth = param.iPicWidth; - param.sSpatialLayers[0].iVideoHeight = param.iPicHeight; - param.sSpatialLayers[0].fFrameRate = param.fMaxFrameRate; - param.sSpatialLayers[0].iSpatialBitrate = param.iTargetBitrate; - param.sSpatialLayers[0].iMaxSpatialBitrate = param.iMaxBitrate; - -#if OPENH264_VER_AT_LEAST(1, 7) - if (avctx->sample_aspect_ratio.num && avctx->sample_aspect_ratio.den) { - // Table E-1. - static const AVRational sar_idc[] = { - { 0, 0 }, // Unspecified (never written here). - { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 }, - { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 }, - { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 }, - { 160, 99 }, // Last 3 are unknown to openh264: { 4, 3 }, { 3, 2 }, { 2, 1 }, - }; - static const ESampleAspectRatio asp_idc[] = { - ASP_UNSPECIFIED, - ASP_1x1, ASP_12x11, ASP_10x11, ASP_16x11, - ASP_40x33, ASP_24x11, ASP_20x11, ASP_32x11, - ASP_80x33, ASP_18x11, ASP_15x11, ASP_64x33, - ASP_160x99, - }; - int num, den, i; - - av_reduce(&num, &den, avctx->sample_aspect_ratio.num, - avctx->sample_aspect_ratio.den, 65535); - - for (i = 1; i < FF_ARRAY_ELEMS(sar_idc); i++) { - if (num == sar_idc[i].num && - den == sar_idc[i].den) - break; - } - if (i == FF_ARRAY_ELEMS(sar_idc)) { - param.sSpatialLayers[0].eAspectRatio = ASP_EXT_SAR; - param.sSpatialLayers[0].sAspectRatioExtWidth = num; - param.sSpatialLayers[0].sAspectRatioExtHeight = den; - } else { - param.sSpatialLayers[0].eAspectRatio = asp_idc[i]; - } - param.sSpatialLayers[0].bAspectRatioPresent = true; - } else { - param.sSpatialLayers[0].bAspectRatioPresent = false; - } -#endif - - if ((avctx->slices > 1) && (s->max_nal_size)) { - av_log(avctx, AV_LOG_ERROR, - "Invalid combination -slices %d and -max_nal_size %d.\n", - avctx->slices, s->max_nal_size); - return AVERROR(EINVAL); - } - - if (avctx->slices > 1) - s->slice_mode = SM_FIXEDSLCNUM_SLICE; - - if (s->max_nal_size) - s->slice_mode = SM_SIZELIMITED_SLICE; - -#if OPENH264_VER_AT_LEAST(1, 6) - param.sSpatialLayers[0].sSliceArgument.uiSliceMode = s->slice_mode; - param.sSpatialLayers[0].sSliceArgument.uiSliceNum = avctx->slices; -#else - param.sSpatialLayers[0].sSliceCfg.uiSliceMode = s->slice_mode; - param.sSpatialLayers[0].sSliceCfg.sSliceArgument.uiSliceNum = avctx->slices; -#endif - if (avctx->slices == 0 && s->slice_mode == SM_FIXEDSLCNUM_SLICE) - av_log(avctx, AV_LOG_WARNING, "Auto slice number, " - "default to use the number of CPU cores: %d\n", av_cpu_count()); - - if (s->slice_mode == SM_SIZELIMITED_SLICE) { - if (s->max_nal_size) { - param.uiMaxNalSize = s->max_nal_size; -#if OPENH264_VER_AT_LEAST(1, 6) - param.sSpatialLayers[0].sSliceArgument.uiSliceSizeConstraint = s->max_nal_size; -#else - param.sSpatialLayers[0].sSliceCfg.sSliceArgument.uiSliceSizeConstraint = s->max_nal_size; -#endif - } else { - av_log(avctx, AV_LOG_ERROR, "Invalid -max_nal_size, " - "specify a valid max_nal_size to use -slice_mode dyn\n"); - return AVERROR(EINVAL); - } - } + if (err = svc_encode_init_params(avctx, ¶m) < 0) + return err; if ((*s->encoder)->InitializeExt(s->encoder, ¶m) != cmResultSuccess) { av_log(avctx, AV_LOG_ERROR, "Initialize failed\n"); @@ -348,6 +239,185 @@ static int svc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, return 0; } +static av_cold int svc_encode_init_params(AVCodecContext *avctx, SEncParamExt *param) +{ + SVCContext *s = avctx->priv_data; + int err; + + // The default parameters would be got by FillDefault() in codec/encoder/core/inc/param_svc.h + param->iPicWidth = avctx->width; + param->iPicHeight = avctx->height; + param->uiIntraPeriod = avctx->gop_size; + param->fMaxFrameRate = 1/av_q2d(avctx->time_base); + param->iMultipleThreadIdc = avctx->thread_count; + param->iLoopFilterDisableIdc = !s->loopfilter; + // Nal unit control +#if OPENH264_VER_AT_LEAST(1, 4) + param->eSpsPpsIdStrategy = CONSTANT_ID; +#else + param->bEnableSpsPpsIdAddition = 0; +#endif + param->bPrefixNalAddingCtrl = 0; + + if (err = svc_encode_init_profile(avctx, param) < 0) + return err; + + if (err = svc_encode_init_rate_control(avctx, param) < 0) + return err; + + if (err = svc_encode_init_spatial_layer(avctx, param) < 0) + return err; + + return 0; +} + +static av_cold int svc_encode_init_profile(AVCodecContext *avctx, SEncParamExt *param) +{ + SVCContext *s = avctx->priv_data; + + if (s->profile && !strcmp(s->profile, "main")) + param->iEntropyCodingModeFlag = 1; //< 0:CAVLC 1:CABAC + else if (!s->profile && s->cabac) + param->iEntropyCodingModeFlag = 1; + else + param->iEntropyCodingModeFlag = 0; + + return 0; +} + +static av_cold int svc_encode_init_rate_control(AVCodecContext *avctx, SEncParamExt *param) +{ + SVCContext *s = avctx->priv_data; + + /* Rate Control */ + param->iRCMode = s->rc_mode; + + param->iTargetBitrate = avctx->bit_rate; + param->iMaxBitrate = FFMAX(avctx->rc_max_rate, avctx->bit_rate); + param->bEnableFrameSkip = s->skip_frames; + + // QP = 0 is not well supported, so default to (1, 51) + param->iMaxQp = avctx->qmax >= 0 ? av_clip(avctx->qmax, 1, 51) : 51; + param->iMinQp = avctx->qmin >= 0 ? av_clip(avctx->qmin, 1, avctx->qmax) : 1; + param->bEnableAdaptiveQuant = 1; + + param->iSpatialLayerNum = 1; // Number of dependency(Spatial/CGS) layers used to be encoded + param->iTemporalLayerNum = 1; // Number of temporal layer specified + + param->bEnableDenoise = 0; // Denoise control + param->bEnableBackgroundDetection = 1; // Background detection control + + param->bEnableLongTermReference = 0; // Long term reference control + param->iLtrMarkPeriod = 30; // the min distance of two int32_t references + + return 0; +} + +static av_cold int svc_encode_init_slice(AVCodecContext *avctx, SEncParamExt *param, int iLayer) +{ + SVCContext *s = avctx->priv_data; + + if ((avctx->slices > 1) && (s->max_nal_size)) { + av_log(avctx, AV_LOG_ERROR, + "Invalid combination -slices %d and -max_nal_size %d.\n", + avctx->slices, s->max_nal_size); + return AVERROR(EINVAL); + } + + if (avctx->slices > 1) + s->slice_mode = SM_FIXEDSLCNUM_SLICE; + + if (s->max_nal_size) + s->slice_mode = SM_SIZELIMITED_SLICE; + +#if OPENH264_VER_AT_LEAST(1, 6) + param->sSpatialLayers[iLayer].sSliceArgument.uiSliceMode = s->slice_mode; + param->sSpatialLayers[iLayer].sSliceArgument.uiSliceNum = avctx->slices; +#else + param->sSpatialLayers[iLayer].sSliceCfg.uiSliceMode = s->slice_mode; + param->sSpatialLayers[iLayer].sSliceCfg.sSliceArgument.uiSliceNum = avctx->slices; +#endif + if (avctx->slices == 0 && s->slice_mode == SM_FIXEDSLCNUM_SLICE) + av_log(avctx, AV_LOG_WARNING, "Auto slice number, " + "default to use the number of CPU cores: %d\n", av_cpu_count()); + + if (s->slice_mode == SM_SIZELIMITED_SLICE) { + if (s->max_nal_size) { + param->uiMaxNalSize = s->max_nal_size; +#if OPENH264_VER_AT_LEAST(1, 6) + param->sSpatialLayers[iLayer].sSliceArgument.uiSliceSizeConstraint = s->max_nal_size; +#else + param->sSpatialLayers[iLayer].sSliceCfg.sSliceArgument.uiSliceSizeConstraint = s->max_nal_size; +#endif + } else { + av_log(avctx, AV_LOG_ERROR, "Invalid -max_nal_size, " + "specify a valid max_nal_size to use -slice_mode dyn\n"); + return AVERROR(EINVAL); + } + } + + return 0; +} + +static av_cold int svc_encode_init_spatial_layer(AVCodecContext *avctx, SEncParamExt *param) +{ + int iLayer, err; + + // Default iSpatialLayerNum = 1, should be extended to support MAX_SPATIAL_LAYER_NUM = 4 + for (iLayer = 0; iLayer < param->iSpatialLayerNum; iLayer++) { + param->sSpatialLayers[iLayer].iVideoWidth = param->iPicWidth; + param->sSpatialLayers[iLayer].iVideoHeight = param->iPicHeight; + param->sSpatialLayers[iLayer].fFrameRate = param->fMaxFrameRate; + param->sSpatialLayers[iLayer].iSpatialBitrate = param->iTargetBitrate; + param->sSpatialLayers[iLayer].iMaxSpatialBitrate = param->iMaxBitrate; + + if (err = svc_encode_init_slice(avctx, param, iLayer) < 0) + return err; + +#if OPENH264_VER_AT_LEAST(1, 7) + if (avctx->sample_aspect_ratio.num && avctx->sample_aspect_ratio.den) { + // Table E-1. + static const AVRational sar_idc[] = { + { 0, 0 }, // Unspecified (never written here). + { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 }, + { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 }, + { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 }, + { 160, 99 }, // Last 3 are unknown to openh264: { 4, 3 }, { 3, 2 }, { 2, 1 }, + }; + static const ESampleAspectRatio asp_idc[] = { + ASP_UNSPECIFIED, + ASP_1x1, ASP_12x11, ASP_10x11, ASP_16x11, + ASP_40x33, ASP_24x11, ASP_20x11, ASP_32x11, + ASP_80x33, ASP_18x11, ASP_15x11, ASP_64x33, + ASP_160x99, + }; + int num, den, i; + + av_reduce(&num, &den, avctx->sample_aspect_ratio.num, + avctx->sample_aspect_ratio.den, 65535); + + for (i = 1; i < FF_ARRAY_ELEMS(sar_idc); i++) { + if (num == sar_idc[i].num && + den == sar_idc[i].den) + break; + } + if (i == FF_ARRAY_ELEMS(sar_idc)) { + param->sSpatialLayers[iLayer].eAspectRatio = ASP_EXT_SAR; + param->sSpatialLayers[iLayer].sAspectRatioExtWidth = num; + param->sSpatialLayers[iLayer].sAspectRatioExtHeight = den; + } else { + param->sSpatialLayers[iLayer].eAspectRatio = asp_idc[i]; + } + param->sSpatialLayers[iLayer].bAspectRatioPresent = true; + } else { + param->sSpatialLayers[iLayer].bAspectRatioPresent = false; + } +#endif + } + + return 0; +} + static const AVCodecDefault svc_enc_defaults[] = { { "b", "2M" }, { "g", "120" },