From patchwork Wed Aug 16 11:11:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dawid Kozinski X-Patchwork-Id: 43236 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:4c9d:b0:130:ccc6:6c4b with SMTP id fq29csp1171560pzb; Wed, 16 Aug 2023 04:11:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFNF6yQhiFxEuFPSc7Cfz6q9OCBOR/aWxrpOXYcE2g2wmZPKRoKh03HE9FH41wVcF6q40JA X-Received: by 2002:a05:6402:14cd:b0:525:7659:66f9 with SMTP id f13-20020a05640214cd00b00525765966f9mr1256946edx.12.1692184292372; Wed, 16 Aug 2023 04:11:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692184292; cv=none; d=google.com; s=arc-20160816; b=aXfh6nlemv1zon3NfkvGhdw1K+50xIDB49rqSA7YupK0grWEPAY8cQ8+0vnX+9mrhA 2Jpdf8YIw4dX+ffQFIC0w+1/o1NDwK4v+TWrFwF0LujlfUxtnvBq7AtMUD8IpfpPfFOf 8wrpL/i474jCWmfFxlJnri6Wee8vdwty0uIMOFarS0ZIcmjShVhrKB7thd6f8IceC+/q HK9hniuOq2Dh87JvGE7ngfCnxa/GzkC0dFBqZm2ijgjFEXVIpot8vWQk3BqTSzcMFxMi ptf7kwenmtFOqQN5PQtQHP9jla2CLB2OhjY123aPYLp+Ano4fD9lQXOVnr3BxX0nVpBg 31Bw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:cms-type:mime-version :message-id:date:to:from:dkim-signature:dkim-filter:delivered-to; bh=CDA7GvPeqBCp0rOlPN5DwQLrdPSAoGnXavcZXutndzY=; fh=EdLpQc+axqrQtc1PbXA/3aaNYrMDzen0n951paqcS+k=; b=qJbPPlMot6WddnhXhkgkIkJaclL5ntNLBb6f4tRVDPvjkxruKGM1Ix1IHD2kqzL0D7 LsOtvc3qWHa9MF5BezEUcwBw7FxljUOYK6tE80oe8F1KXqeX2UT/hGgs+3dgNozGm8BP BYzLkXqOUYsvg/P8vf+LjaZfaTSuzufnPzCnQpqnLEkfu51bk0NsN86NUkbOyqbcnGfZ KWeCKdksQu4svtLHkFm1ZPN2lvVD55qTunuWRmCqspO+v8i5XAHEGcPWmipiYyXvR55a Yeugw3COWlhC5SnaGZCTPsEi3c+vxTcnXJ5g4iJxWq6SFwjAtztFJoK4SUzBJZhXYXv7 NHmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@samsung.com header.s=mail20170921 header.b=BmbkMvoC; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=samsung.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id m1-20020a50ef01000000b005232df72071si11326408eds.404.2023.08.16.04.11.31; Wed, 16 Aug 2023 04:11:32 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@samsung.com header.s=mail20170921 header.b=BmbkMvoC; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=samsung.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9B6D168C5D5; Wed, 16 Aug 2023 14:11:27 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mailout2.w1.samsung.com (mailout2.w1.samsung.com [210.118.77.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 678D668C159 for ; Wed, 16 Aug 2023 14:11:21 +0300 (EEST) Received: from eucas1p1.samsung.com (unknown [182.198.249.206]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20230816111117euoutp02b00de9e2e9c3681950e4f4fab764af22~72KKVICi22803728037euoutp02H for ; Wed, 16 Aug 2023 11:11:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20230816111117euoutp02b00de9e2e9c3681950e4f4fab764af22~72KKVICi22803728037euoutp02H DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1692184277; bh=FyRhrB0qEbdUxMmIo0SpH1+E8gcoyZP+RPvTIu29Qqc=; h=From:To:Cc:Subject:Date:References:From; b=BmbkMvoCegU+3mvHshixz1+glUsERdGzhxXN1+n+VmXJdt8hSvIKw5dac7XrCJX+P O5rS5MopeQEVq7JyHG/wzNH3D5psumvoZAEItNnTv+07QR1Ac7YxtFzllDPmUMHasH eYFLyqRiyiNJ5uDvJRZb+WgRaJYFI6pHc2NCEHXA= Received: from eusmges3new.samsung.com (unknown [203.254.199.245]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20230816111117eucas1p15322a629b227f2993ac0fd192ceb1e11~72KKJ-TJz1949019490eucas1p1q; Wed, 16 Aug 2023 11:11:17 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges3new.samsung.com (EUCPMTA) with SMTP id 03.16.37758.5DEACD46; Wed, 16 Aug 2023 12:11:17 +0100 (BST) Received: from eusmtrp2.samsung.com (unknown [182.198.249.139]) by eucas1p1.samsung.com (KnoxPortal) with ESMTPA id 20230816111117eucas1p193bac18fd75aa8545a0a6724f0ce183c~72KJ0iYxn1949019490eucas1p1o; Wed, 16 Aug 2023 11:11:17 +0000 (GMT) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eusmtrp2.samsung.com (KnoxPortal) with ESMTP id 20230816111117eusmtrp28029ebd64a37a817fd5a0c9ef811ea7d~72KJxf1uT2508825088eusmtrp2C; Wed, 16 Aug 2023 11:11:17 +0000 (GMT) X-AuditID: cbfec7f5-815ff7000002937e-d9-64dcaed57beb Received: from eusmtip2.samsung.com ( [203.254.199.222]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id 3A.8D.14344.5DEACD46; Wed, 16 Aug 2023 12:11:17 +0100 (BST) Received: from AMDN5164.EU.corp.samsungelectronics.net (unknown [106.210.132.171]) by eusmtip2.samsung.com (KnoxPortal) with ESMTPA id 20230816111116eusmtip26ade33e0408a39a0d92c19eb5852206f~72KJVtFwi0592505925eusmtip2K; Wed, 16 Aug 2023 11:11:16 +0000 (GMT) From: Dawid Kozinski To: d.frankiewic@samsung.com, ffmpeg-devel@ffmpeg.org Date: Wed, 16 Aug 2023 13:11:02 +0200 Message-Id: <20230816111102.949-1-d.kozinski@samsung.com> X-Mailer: git-send-email 2.37.3.windows.1 MIME-Version: 1.0 X-Unsent: 1 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrHIsWRmVeSWpSXmKPExsWy7djPc7pX191JMZi4Sdriy7UvzBYrv/1m sfj26QyzA7PHn0WbWTz6tqxiDGCK4rJJSc3JLEst0rdL4Mq49Hw6S8HV34wVqyc1szYwvt3L 2MXIySEhYCJx68cj5i5GLg4hgRWMEv/vrWWHcL4wSjy+eBvK+cwocereNnaYlnNLv7BCJJYz Sixf9p4Nwmlnkjjx4ABYFZuArsTLD4+YQGwRAVOJk8172UBsZgFtiXV3v4PZwgIBEnMfbQCz WQRUJZbPfgVm8wpYSnxb384CsU1TYsKWOUwQcUGJkzOfQMW5JdbNusQKMVNeonnrbLAnJAR+ skscvTcHqshFovXjDyYIW1ji1fEtUC/ISPzfOR8ozgFkF0sc6neAMGskDv1Ih6iwlnjbeJwR JMwMdML6XfoQYUeJZftfsEJU80nceCsIcQCfxKRt05khwrwSHW1CEKaKRF+nGESjlMTTZXOY IWwPibOH3rBNYFScheSrWUg+mYWwdgEj8ypG8dTS4tz01GLjvNRyveLE3OLSvHS95PzcTYzA JHH63/GvOxhXvPqod4iRiYPxEKMEB7OSCG8P760UId6UxMqq1KL8+KLSnNTiQ4zSHCxK4rza tieThQTSE0tSs1NTC1KLYLJMHJxSDUze0j4+ViH/pn1aETdl9fLNyxrz1u26k8sqce6L8sZX O1J0LZZIPf4wS0W+ZaKCAOc0Y55tx1Ocltz4kPzCtaZBT8zq5EYvyzvLtlw6rVdzqeP7oq3u j4U9VA93Z6zI6rL1CwyTudL8zCUnbu7nB09mKi141lm+tHBznntowj7th792hJRc1woTWy53 lOXm1k/31i9YP/n384musp91+JrVuHdKWVaIHJt55+nDmojaxEfZsaYxx/ff/JHrOn1zia3n 81dPV2yMPuNstjP+0cr8vb69v/d+vH+GybkgXXEtf/Q+B/nEsocro2N7bJ4/3GLekfO7+nT5 TGd2FcEMxev3vZzv9M7cePLjt4pTKyLnK7EUZyQaajEXFScCADIMsMiBAwAA X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrCLMWRmVeSWpSXmKPExsVy+t/xe7pX191JMfh1k9Xiy7UvzBYrv/1m sfj26QyzA7PHn0WbWTz6tqxiDGCK0rMpyi8tSVXIyC8usVWKNrQw0jO0tNAzMrHUMzQ2j7Uy MlXSt7NJSc3JLEst0rdL0Mu49Hw6S8HV34wVqyc1szYwvt3L2MXIySEhYCJxbukX1i5GLg4h gaWMEqv+fWKGSEhJLF26CKpIWOLPtS42iKJWJomFsxrZQBJsAroSLz88YgKxRQTMJS6uuwXW wCygLbHu7newGmEBP4nLkxeB1bAIqEosn/0KLM4rYCnxbX07C8QCTYkJW+YwQcQFJU7OfAIV 55ZYN+sSK8RMeYnmrbOZJzDyz0JSNgtJagEj0ypGkdTS4tz03GIjveLE3OLSvHS95PzcTYzA 4N127OeWHYwrX33UO8TIxMF4iFGCg1lJhLeH91aKEG9KYmVValF+fFFpTmrxIUZToFsnMkuJ JucD4yevJN7QzMDU0MTM0sDU0sxYSZzXs6AjUUggPbEkNTs1tSC1CKaPiYNTqoGpaKNCrv/3 9e2rJd24PqrrLHWId/1sP/dr+zybQBOenNkf9lRKqAafus+7wu5kUHLL3tKfQW+4PgmYtjPo dlhcvzDX4cLSOUv+Jyi4njtSx2bz83tkx97fU+p/pv+J4HgX8z8x8ujRPPlVz14Jc114xb2j J77a/zL7rrpwCX//f6+dPFxYXsR+7d1kafpd5rZQ0BzTe/UnzCTvbP9d7axRu+S/Q/TswC+3 tL6Jzy7ccDTcsKFf5dbPiN/JW5RrrnIEWSaU//C3klsxh99OM+6msVbhqueuFx9xRGk8FQi7 sVuN++JN110VEco5p/T/rVu/91TuTk2lvADx6Cs/L/Pwcb1Um/cp52nYslouzr9KLMUZiYZa zEXFiQA5SGRR5wIAAA== X-CMS-MailID: 20230816111117eucas1p193bac18fd75aa8545a0a6724f0ce183c X-Msg-Generator: CA X-RootMTR: 20230816111117eucas1p193bac18fd75aa8545a0a6724f0ce183c X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20230816111117eucas1p193bac18fd75aa8545a0a6724f0ce183c References: Subject: [FFmpeg-devel] [PATCH v27 1/2] avcodec/evc_encoder: Provided support for EVC encoder X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: Dawid Kozinski Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: D9Ft7ElBkx3c - Added EVC encoder wrapper - Changes in project configuration file and libavcodec Makefile - Added documentation for xeve wrapper Signed-off-by: Dawid Kozinski --- configure | 4 + doc/encoders.texi | 69 +++++ doc/general_contents.texi | 11 + libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/libxeve.c | 616 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 702 insertions(+) create mode 100644 libavcodec/libxeve.c diff --git a/configure b/configure index ff6a5c3600..e3e71e61e8 100755 --- a/configure +++ b/configure @@ -293,6 +293,7 @@ External library support: --enable-libwebp enable WebP encoding via libwebp [no] --enable-libx264 enable H.264 encoding via x264 [no] --enable-libx265 enable HEVC encoding via x265 [no] + --enable-libxeve enable EVC encoding via libxeve [no] --enable-libxavs enable AVS encoding via xavs [no] --enable-libxavs2 enable AVS2 encoding via xavs2 [no] --enable-libxcb enable X11 grabbing using XCB [autodetect] @@ -1905,6 +1906,7 @@ EXTERNAL_LIBRARY_LIST=" libvorbis libvpx libwebp + libxeve libxml2 libzimg libzmq @@ -3465,6 +3467,7 @@ libx265_encoder_deps="libx265" libx265_encoder_select="atsc_a53" libxavs_encoder_deps="libxavs" libxavs2_encoder_deps="libxavs2" +libxeve_encoder_deps="libxeve" libxvid_encoder_deps="libxvid" libzvbi_teletext_decoder_deps="libzvbi" vapoursynth_demuxer_deps="vapoursynth" @@ -6847,6 +6850,7 @@ enabled libx265 && require_pkg_config libx265 x265 x265.h x265_api_get require_cpp_condition libx265 x265.h "X265_BUILD >= 89" enabled libxavs && require libxavs "stdint.h xavs.h" xavs_encoder_encode "-lxavs $pthreads_extralibs $libm_extralibs" enabled libxavs2 && require_pkg_config libxavs2 "xavs2 >= 1.3.0" "stdint.h xavs2.h" xavs2_api_get +enabled libxeve && require_pkg_config libxeve "xeve >= 0.4.3" "xeve.h" xeve_encode enabled libxvid && require libxvid xvid.h xvid_global -lxvidcore enabled libzimg && require_pkg_config libzimg "zimg >= 2.7.0" zimg.h zimg_get_api_version enabled libzmq && require_pkg_config libzmq "libzmq >= 4.2.1" zmq.h zmq_ctx_new diff --git a/doc/encoders.texi b/doc/encoders.texi index 6f8f5e127e..7d1caa2438 100644 --- a/doc/encoders.texi +++ b/doc/encoders.texi @@ -2894,6 +2894,75 @@ ffmpeg -i input -c:v libxavs2 -xavs2-params RdoqLevel=0 output.avs2 @end example @end table +@section libxeve + +eXtra-fast Essential Video Encoder (XEVE) MPEG-5 EVC encoder wrapper. +The xeve-equivalent options or values are listed in parentheses for easy migration. + +This encoder requires the presence of the libxeve headers and library +during configuration. You need to explicitly configure the build with +@option{--enable-libxeve}. + +@float NOTE +Many libxeve encoder options are mapped to FFmpeg global codec options, +while unique encoder options are provided through private options. +Additionally the xeve-params private options allows one to pass a list +of key=value tuples as accepted by the libxeve @code{parse_xeve_params} function. +@end float + +The xeve project website is at @url{https://github.com/mpeg5/xeve}. + +@subsection Options + +The following options are supported by the libxeve wrapper. +The xeve-equivalent options or values are listed in parentheses for easy migration. + +@float NOTE +To reduce the duplication of documentation, only the private options +and some others requiring special attention are documented here. For +the documentation of the undocumented generic options, see +@ref{codec-options,,the Codec Options chapter}. +@end float + +@float NOTE +To get a more accurate and extensive documentation of the libxeve options, +invoke the command @code{xeve_app --help} or consult the libxeve documentation. +@end float + +@table @option +@item b (@emph{bitrate}) +Set target video bitrate in bits/s. +Note that FFmpeg's b option is expressed in bits/s, while xeve's bitrate is in kilobits/s. + +@item bf (@emph{bframes}) +Set the maximum number of B frames (1,3,7,15). + +@item g (@emph{keyint}) +Set the GOP size (I-picture period). + +@item preset (@emph{preset}) +Set the xeve preset. +Set the encoder preset value to determine encoding speed [fast, medium, slow, placebo] + +@item tune (@emph{tune}) +Set the encoder tune parameter [psnr, zerolatency] + +@item profile (@emph{profile}) +Set the encoder profile [0: baselie; 1: main] + +@item crf (@emph{crf}) +Set the quality for constant quality mode. +Constant rate factor <10..49> [default: 32] + +@item qp (@emph{qp}) +Set constant quantization rate control method parameter. +Quantization parameter qp <0..51> [default: 32] + +@item threads (@emph{threads}) +Force to use a specific number of threads + +@end table + @section libxvid Xvid MPEG-4 Part 2 encoder wrapper. diff --git a/doc/general_contents.texi b/doc/general_contents.texi index 8ac121dee1..b482253fd5 100644 --- a/doc/general_contents.texi +++ b/doc/general_contents.texi @@ -343,6 +343,14 @@ libxavs2 is under the GNU Public License Version 2 or later details), you must upgrade FFmpeg's license to GPL in order to use it. @end float +@section eXtra-fast Essential Video Encoder (XEVE) + +FFmpeg can make use of the XEVE library for EVC video encoding. + +Go to @url{https://github.com/mpeg5/xeve} and follow the instructions for +installing the XEVE library. Then pass @code{--enable-libxeve} to configure to +enable it. + @section ZVBI ZVBI is a VBI decoding library which can be used by FFmpeg to decode DVB @@ -598,6 +606,7 @@ library: @item raw DTS @tab X @tab X @item raw DTS-HD @tab @tab X @item raw E-AC-3 @tab X @tab X +@item raw EVC @tab X @tab X @item raw FLAC @tab X @tab X @item raw GSM @tab @tab X @item raw H.261 @tab X @tab X @@ -943,6 +952,8 @@ following image formats are supported: @item Electronic Arts TQI video @tab @tab X @item Escape 124 @tab @tab X @item Escape 130 @tab @tab X +@item EVC / MPEG-5 Part 1 @tab X @tab X + @tab encoding and decoding supported through external library libxeve @item FFmpeg video codec #1 @tab X @tab X @tab lossless codec (fourcc: FFV1) @item Flash Screen Video v1 @tab X @tab X diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 3c16b51462..658b3baa7e 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1144,6 +1144,7 @@ OBJS-$(CONFIG_LIBX264_ENCODER) += libx264.o OBJS-$(CONFIG_LIBX265_ENCODER) += libx265.o OBJS-$(CONFIG_LIBXAVS_ENCODER) += libxavs.o OBJS-$(CONFIG_LIBXAVS2_ENCODER) += libxavs2.o +OBJS-$(CONFIG_LIBXEVE_ENCODER) += libxeve.o OBJS-$(CONFIG_LIBXVID_ENCODER) += libxvid.o OBJS-$(CONFIG_LIBZVBI_TELETEXT_DECODER) += libzvbi-teletextdec.o ass.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 8775d15a4f..aa22e7b6c1 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -819,6 +819,7 @@ extern LIBX264_CONST FFCodec ff_libx264_encoder; #endif extern const FFCodec ff_libx264rgb_encoder; extern FFCodec ff_libx265_encoder; +extern const FFCodec ff_libxeve_encoder; extern const FFCodec ff_libxavs_encoder; extern const FFCodec ff_libxavs2_encoder; extern const FFCodec ff_libxvid_encoder; diff --git a/libavcodec/libxeve.c b/libavcodec/libxeve.c new file mode 100644 index 0000000000..c10b7de023 --- /dev/null +++ b/libavcodec/libxeve.c @@ -0,0 +1,616 @@ +/* + * libxeve encoder + * EVC (MPEG-5 Essential Video Coding) encoding using XEVE MPEG-5 EVC encoder library + * + * Copyright (C) 2021 Dawid Kozinski + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +#include + +#include "libavutil/internal.h" +#include "libavutil/common.h" +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" +#include "libavutil/pixfmt.h" +#include "libavutil/time.h" +#include "libavutil/cpu.h" +#include "libavutil/avstring.h" + +#include "avcodec.h" +#include "internal.h" +#include "packet_internal.h" +#include "codec_internal.h" +#include "profiles.h" +#include "encode.h" + +#define MAX_BS_BUF (16*1024*1024) + +/** + * Error codes + */ +#define XEVE_PARAM_BAD_NAME -100 +#define XEVE_PARAM_BAD_VALUE -200 + +/** + * Encoder states + * + * STATE_ENCODING - the encoder receives and processes input frames + * STATE_BUMPING - there are no more input frames, however the encoder still processes previously received data + */ +typedef enum State { + STATE_ENCODING, + STATE_BUMPING, +} State; + +/** + * The structure stores all the states associated with the instance of Xeve MPEG-5 EVC encoder + */ +typedef struct XeveContext { + const AVClass *class; + + XEVE id; // XEVE instance identifier + XEVE_CDSC cdsc; // coding parameters i.e profile, width & height of input frame, num of therads, frame rate ... + XEVE_BITB bitb; // bitstream buffer (output) + XEVE_STAT stat; // encoding status (output) + XEVE_IMGB imgb; // image buffer (input) + + State state; // encoder state (skipping, encoding, bumping) + + int profile_id; // encoder profile (main, baseline) + int preset_id; // preset of xeve ( fast, medium, slow, placebo) + int tune_id; // tune of xeve (psnr, zerolatency) + + // variables for rate control modes + int rc_mode; // Rate control mode [ 0(CQP) / 1(ABR) / 2(CRF) ] + int qp; // quantization parameter (QP) [0,51] + int crf; // constant rate factor (CRF) [10,49] + + int hash; // embed picture signature (HASH) for conformance checking in decoding + int sei_info; // embed Supplemental enhancement information while encoding + + int color_format; // input data color format: currently only XEVE_CF_YCBCR420 is supported + + AVDictionary *xeve_params; +} XeveContext; + +/** + * Convert FFmpeg pixel format (AVPixelFormat) to XEVE pre-defined color format + * + * @param[in] av_pix_fmt pixel format (@see https://ffmpeg.org/doxygen/trunk/pixfmt_8h.html#a9a8e335cf3be472042bc9f0cf80cd4c5) + * @param[out] xeve_col_fmt XEVE pre-defined color format (@see xeve.h) + * + * @return 0 on success, negative value on failure + */ +static int libxeve_color_fmt(enum AVPixelFormat av_pix_fmt, int *xeve_col_fmt) +{ + switch (av_pix_fmt) { + case AV_PIX_FMT_YUV420P: + *xeve_col_fmt = XEVE_CF_YCBCR420; + break; + case AV_PIX_FMT_YUV420P10: + *xeve_col_fmt = XEVE_CF_YCBCR420; + break; + default: + *xeve_col_fmt = XEVE_CF_UNKNOWN; + return AVERROR_INVALIDDATA; + } + + return 0; +} + +/** + * Convert FFmpeg pixel format (AVPixelFormat) into XEVE pre-defined color space + * + * @param[in] px_fmt pixel format (@see https://ffmpeg.org/doxygen/trunk/pixfmt_8h.html#a9a8e335cf3be472042bc9f0cf80cd4c5) + * + * @return XEVE pre-defined color space (@see xeve.h) on success, XEVE_CF_UNKNOWN on failure + */ +static int libxeve_color_space(enum AVPixelFormat av_pix_fmt) +{ + /* color space of input image */ + int cs = XEVE_CF_UNKNOWN; + + switch (av_pix_fmt) { + case AV_PIX_FMT_YUV420P: + cs = XEVE_CS_YCBCR420; + break; + case AV_PIX_FMT_YUV420P10: +#if AV_HAVE_BIGENDIAN + cs = XEVE_CS_SET(XEVE_CF_YCBCR420, 10, 1); +#else + cs = XEVE_CS_YCBCR420_10LE; +#endif + + break; + default: + cs = XEVE_CF_UNKNOWN; + break; + } + + return cs; +} + +/** + * The function returns a pointer to the object of the XEVE_CDSC type. + * XEVE_CDSC contains all encoder parameters that should be initialized before the encoder is used. + * + * The field values of the XEVE_CDSC structure are populated based on: + * - the corresponding field values of the AvCodecConetxt structure, + * - the xeve encoder specific option values, + * (the full list of options available for xeve encoder is displayed after executing the command ./ffmpeg --help encoder = libxeve) + * + * The order of processing input data and populating the XEVE_CDSC structure + * 1) first, the fields of the AVCodecContext structure corresponding to the provided input options are processed, + * (i.e -pix_fmt yuv420p -s:v 1920x1080 -r 30 -profile:v 0) + * 2) then xeve-specific options added as AVOption to the xeve AVCodec implementation + * (i.e -preset 0) + * + * Keep in mind that, there are options that can be set in different ways. + * In this case, please follow the above-mentioned order of processing. + * The most recent assignments overwrite the previous values. + * + * @param[in] avctx codec context (AVCodecContext) + * @param[out] cdsc contains all Xeve MPEG-5 EVC encoder encoder parameters that should be initialized before the encoder is use + * + * @return 0 on success, negative error code on failure + */ +static int get_conf(AVCodecContext *avctx, XEVE_CDSC *cdsc) +{ + XeveContext *xectx = NULL; + int ret; + + xectx = avctx->priv_data; + + /* initialize xeve_param struct with default values */ + ret = xeve_param_default(&cdsc->param); + if (XEVE_FAILED(ret)) { + av_log(avctx, AV_LOG_ERROR, "Cannot set_default parameter\n"); + return AVERROR_EXTERNAL; + } + + /* read options from AVCodecContext */ + if (avctx->width > 0) + cdsc->param.w = avctx->width; + + if (avctx->height > 0) + cdsc->param.h = avctx->height; + + if (avctx->framerate.num > 0) { + // fps can be float number, but xeve API doesn't support it + cdsc->param.fps = lrintf(av_q2d(avctx->framerate)); + } + + // GOP size (key-frame interval, I-picture period) + cdsc->param.keyint = avctx->gop_size; // 0: only one I-frame at the first time; 1: every frame is coded in I-frame + + if (avctx->max_b_frames == 0 || avctx->max_b_frames == 1 || avctx->max_b_frames == 3 || + avctx->max_b_frames == 7 || avctx->max_b_frames == 15) // number of b-frames + cdsc->param.bframes = avctx->max_b_frames; + else { + av_log(avctx, AV_LOG_ERROR, "Incorrect value for maximum number of B frames: (%d) \n" + "Acceptable values for bf option (maximum number of B frames) are 0,1,3,7 or 15\n", avctx->max_b_frames); + return AVERROR_INVALIDDATA; + } + + cdsc->param.level_idc = avctx->level; + + if (avctx->rc_buffer_size) // VBV buf size + cdsc->param.vbv_bufsize = (int)(avctx->rc_buffer_size / 1000); + + cdsc->param.rc_type = xectx->rc_mode; + + if (xectx->rc_mode == XEVE_RC_CQP) + cdsc->param.qp = xectx->qp; + else if (xectx->rc_mode == XEVE_RC_ABR) { + if (avctx->bit_rate / 1000 > INT_MAX || avctx->rc_max_rate / 1000 > INT_MAX) { + av_log(avctx, AV_LOG_ERROR, "Not supported bitrate bit_rate and rc_max_rate > %d000\n", INT_MAX); + return AVERROR_INVALIDDATA; + } + cdsc->param.bitrate = (int)(avctx->bit_rate / 1000); + } else if (xectx->rc_mode == XEVE_RC_CRF) + cdsc->param.crf = xectx->crf; + else { + av_log(avctx, AV_LOG_ERROR, "Not supported rate control type: %d\n", xectx->rc_mode); + return AVERROR_INVALIDDATA; + } + + if (avctx->thread_count <= 0) { + int cpu_count = av_cpu_count(); + cdsc->param.threads = (cpu_count < XEVE_MAX_THREADS) ? cpu_count : XEVE_MAX_THREADS; + } else if (avctx->thread_count > XEVE_MAX_THREADS) + cdsc->param.threads = XEVE_MAX_THREADS; + else + cdsc->param.threads = avctx->thread_count; + + + libxeve_color_fmt(avctx->pix_fmt, &xectx->color_format); + + cdsc->param.cs = XEVE_CS_SET(xectx->color_format, cdsc->param.codec_bit_depth, AV_HAVE_BIGENDIAN); + + cdsc->max_bs_buf_size = MAX_BS_BUF; + + ret = xeve_param_ppt(&cdsc->param, xectx->profile_id, xectx->preset_id, xectx->tune_id); + if (XEVE_FAILED(ret)) { + av_log(avctx, AV_LOG_ERROR, "Cannot set profile(%d), preset(%d), tune(%d)\n", xectx->profile_id, xectx->preset_id, xectx->tune_id); + return AVERROR_EXTERNAL; + } + + return 0; +} + +/** + * Set XEVE_CFG_SET_USE_PIC_SIGNATURE for encoder + * + * @param[in] logger context + * @param[in] id XEVE encodec instance identifier + * @param[in] ctx the structure stores all the states associated with the instance of Xeve MPEG-5 EVC encoder + * + * @return 0 on success, negative error code on failure + */ +static int set_extra_config(AVCodecContext *avctx, XEVE id, XeveContext *ctx) +{ + int ret, size; + size = 4; + + // embed SEI messages identifying encoder parameters and command line arguments + // - 0: off\n" + // - 1: emit sei info" + // + // SEI - Supplemental enhancement information contains information + // that is not necessary to decode the samples of coded pictures from VCL NAL units. + // Some SEI message information is required to check bitstream conformance + // and for output timing decoder conformance. + // @see ISO_IEC_23094-1_2020 7.4.3.5 + // @see ISO_IEC_23094-1_2020 Annex D + ret = xeve_config(id, XEVE_CFG_SET_SEI_CMD, &ctx->sei_info, &size); // sei_cmd_info + if (XEVE_FAILED(ret)) { + av_log(avctx, AV_LOG_ERROR, "Failed to set config for sei command info messages\n"); + return AVERROR_EXTERNAL; + } + + ret = xeve_config(id, XEVE_CFG_SET_USE_PIC_SIGNATURE, &ctx->hash, &size); + if (XEVE_FAILED(ret)) { + av_log(avctx, AV_LOG_ERROR, "Failed to set config for picture signature\n"); + return AVERROR_EXTERNAL; + } + + return 0; +} + +/** + * @brief Switch encoder to bumping mode + * + * @param id XEVE encodec instance identifier + * @return 0 on success, negative error code on failure + */ +static int setup_bumping(XEVE id) +{ + int val = 1; + int size = sizeof(int); + if (XEVE_FAILED(xeve_config(id, XEVE_CFG_SET_FORCE_OUT, (void *)(&val), &size))) + return AVERROR_EXTERNAL; + + return 0; +} + +/** + * @brief Initialize eXtra-fast Essential Video Encoder codec + * Create an encoder instance and allocate all the needed resources + * + * @param avctx codec context + * @return 0 on success, negative error code on failure + */ +static av_cold int libxeve_init(AVCodecContext *avctx) +{ + XeveContext *xectx = avctx->priv_data; + unsigned char *bs_buf = NULL; + int i; + int shift_h = 0; + int shift_v = 0; + int width_chroma = 0; + int height_chroma = 0; + XEVE_IMGB *imgb = NULL; + int ret = 0; + + XEVE_CDSC *cdsc = &(xectx->cdsc); + + /* allocate bitstream buffer */ + bs_buf = av_malloc(MAX_BS_BUF); + if (bs_buf == NULL) { + av_log(avctx, AV_LOG_ERROR, "Cannot allocate bitstream buffer\n"); + return AVERROR(ENOMEM); + } + xectx->bitb.addr = bs_buf; + xectx->bitb.bsize = MAX_BS_BUF; + + /* read configurations and set values for created descriptor (XEVE_CDSC) */ + if ((ret = get_conf(avctx, cdsc)) != 0) { + av_log(avctx, AV_LOG_ERROR, "Cannot get configuration\n"); + return AVERROR(EINVAL); + } + + if ((ret = xeve_param_check(&cdsc->param)) != 0) { + av_log(avctx, AV_LOG_ERROR, "Invalid configuration\n"); + return AVERROR(EINVAL); + } + + { + AVDictionaryEntry *en = NULL; + while (en = av_dict_get(xectx->xeve_params, "", en, AV_DICT_IGNORE_SUFFIX)) { + if ((ret = xeve_param_parse(&cdsc->param, en->key, en->value)) < 0) { + av_log(avctx, AV_LOG_WARNING, + "Error parsing option '%s = %s'.\n", + en->key, en->value); + } + } + } + + /* create encoder */ + xectx->id = xeve_create(cdsc, NULL); + if (xectx->id == NULL) { + av_log(avctx, AV_LOG_ERROR, "Cannot create XEVE encoder\n"); + return AVERROR_EXTERNAL; + } + + if ((ret = set_extra_config(avctx, xectx->id, xectx)) != 0) { + av_log(avctx, AV_LOG_ERROR, "Cannot set extra configuration\n"); + return AVERROR(EINVAL); + } + + if ((ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &shift_h, &shift_v)) != 0) { + av_log(avctx, AV_LOG_ERROR, "Failed to get chroma shift\n"); + return AVERROR(EINVAL); + } + + // Chroma subsampling + // + // YUV format explanation + // shift_h == 1 && shift_v == 1 : YUV420 + // shift_h == 1 && shift_v == 0 : YUV422 + // shift_h == 0 && shift_v == 0 : YUV444 + // + width_chroma = AV_CEIL_RSHIFT(avctx->width, shift_h); + height_chroma = AV_CEIL_RSHIFT(avctx->height, shift_v); + + /* set default values for input image buffer */ + imgb = &xectx->imgb; + imgb->cs = libxeve_color_space(avctx->pix_fmt); + imgb->np = 3; /* only for yuv420p, yuv420ple */ + + for (i = 0; i < imgb->np; i++) + imgb->x[i] = imgb->y[i] = 0; + + imgb->w[0] = imgb->aw[0] = avctx->width; // width luma + imgb->w[1] = imgb->w[2] = imgb->aw[1] = imgb->aw[2] = width_chroma; + imgb->h[0] = imgb->ah[0] = avctx->height; // height luma + imgb->h[1] = imgb->h[2] = imgb->ah[1] = imgb->ah[2] = height_chroma; + + xectx->state = STATE_ENCODING; + + return 0; +} + +/** + * Encode raw data frame into EVC packet + * + * @param[in] avctx codec context + * @param[out] avpkt output AVPacket containing encoded data + * @param[in] frame AVFrame containing the raw data to be encoded + * @param[out] got_packet encoder sets to 0 or 1 to indicate that a + * non-empty packet was returned in pkt + * + * @return 0 on success, negative error code on failure + */ +static int libxeve_encode(AVCodecContext *avctx, AVPacket *avpkt, + const AVFrame *frame, int *got_packet) +{ + XeveContext *xectx = avctx->priv_data; + int ret = -1; + + // No more input frames are available but encoder still can have some data in its internal buffer to process + // and some frames to dump. + if (xectx->state == STATE_ENCODING && frame == NULL) { + if (setup_bumping(xectx->id) == 0) + xectx->state = STATE_BUMPING; // Entering bumping process + else { + av_log(avctx, AV_LOG_ERROR, "Failed to setup bumping\n"); + return 0; + } + } + + if (xectx->state == STATE_ENCODING) { + int i; + XEVE_IMGB *imgb = NULL; + + imgb = &xectx->imgb; + + for (i = 0; i < imgb->np; i++) { + imgb->a[i] = frame->data[i]; + imgb->s[i] = frame->linesize[i]; + } + + imgb->ts[XEVE_TS_PTS] = frame->pts; + + /* push image to encoder */ + ret = xeve_push(xectx->id, imgb); + if (XEVE_FAILED(ret)) { + av_log(avctx, AV_LOG_ERROR, "xeve_push() failed\n"); + return AVERROR_EXTERNAL; + } + } + if (xectx->state == STATE_ENCODING || xectx->state == STATE_BUMPING) { + /* encoding */ + ret = xeve_encode(xectx->id, &(xectx->bitb), &(xectx->stat)); + if (XEVE_FAILED(ret)) { + av_log(avctx, AV_LOG_ERROR, "xeve_encode() failed\n"); + return AVERROR_EXTERNAL; + } + + /* store bitstream */ + if (ret == XEVE_OK_OUT_NOT_AVAILABLE) { // Return OK but picture is not available yet + *got_packet = 0; + return 0; + } else if (ret == XEVE_OK) { + int av_pic_type; + + if (xectx->stat.write > 0) { + + ret = ff_get_encode_buffer(avctx, avpkt, xectx->stat.write, 0); + if (ret < 0) + return ret; + + memcpy(avpkt->data, xectx->bitb.addr, xectx->stat.write); + + avpkt->time_base.num = 1; + avpkt->time_base.den = xectx->cdsc.param.fps; + + avpkt->pts = xectx->bitb.ts[XEVE_TS_PTS]; + avpkt->dts = xectx->bitb.ts[XEVE_TS_DTS]; + + switch(xectx->stat.stype) { + case XEVE_ST_I: + av_pic_type = AV_PICTURE_TYPE_I; + avpkt->flags |= AV_PKT_FLAG_KEY; + break; + case XEVE_ST_P: + av_pic_type = AV_PICTURE_TYPE_P; + break; + case XEVE_ST_B: + av_pic_type = AV_PICTURE_TYPE_B; + break; + case XEVE_ST_UNKNOWN: + av_log(avctx, AV_LOG_ERROR, "Unknown slice type\n"); + return AVERROR_INVALIDDATA; + } + + ff_side_data_set_encoder_stats(avpkt, xectx->stat.qp * FF_QP2LAMBDA, NULL, 0, av_pic_type); + + *got_packet = 1; + } + } else if (ret == XEVE_OK_NO_MORE_FRM) { + // Return OK but no more frames + return 0; + } else { + av_log(avctx, AV_LOG_ERROR, "Invalid return value: %d\n", ret); + return AVERROR_EXTERNAL; + } + } else { + av_log(avctx, AV_LOG_ERROR, "Udefined encoder state\n"); + return AVERROR_INVALIDDATA; + } + + return 0; +} + +/** + * Destroy the encoder and release all the allocated resources + * + * @param avctx codec context + * @return 0 on success, negative error code on failure + */ +static av_cold int libxeve_close(AVCodecContext *avctx) +{ + XeveContext *xectx = avctx->priv_data; + + if (xectx->id) { + xeve_delete(xectx->id); + xectx->id = NULL; + } + + av_free(xectx->bitb.addr); /* release bitstream buffer */ + + return 0; +} + +#define OFFSET(x) offsetof(XeveContext, x) +#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM + +static const enum AVPixelFormat supported_pixel_formats[] = { + AV_PIX_FMT_YUV420P, + AV_PIX_FMT_YUV420P10, + AV_PIX_FMT_NONE +}; + +// Consider using following options (./ffmpeg --help encoder=libxeve) +// +static const AVOption libxeve_options[] = { + { "preset", "Encoding preset for setting encoding speed", OFFSET(preset_id), AV_OPT_TYPE_INT, { .i64 = XEVE_PRESET_MEDIUM }, XEVE_PRESET_DEFAULT, XEVE_PRESET_PLACEBO, VE, "preset" }, + { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PRESET_DEFAULT }, INT_MIN, INT_MAX, VE, "preset" }, + { "fast", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PRESET_FAST }, INT_MIN, INT_MAX, VE, "preset" }, + { "medium", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PRESET_MEDIUM }, INT_MIN, INT_MAX, VE, "preset" }, + { "slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PRESET_SLOW }, INT_MIN, INT_MAX, VE, "preset" }, + { "placebo", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PRESET_PLACEBO }, INT_MIN, INT_MAX, VE, "preset" }, + { "tune", "Tuning parameter for special purpose operation", OFFSET(tune_id), AV_OPT_TYPE_INT, { .i64 = XEVE_TUNE_NONE }, XEVE_TUNE_NONE, XEVE_TUNE_PSNR, VE, "tune"}, + { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_TUNE_NONE }, INT_MIN, INT_MAX, VE, "tune" }, + { "zerolatency", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_TUNE_ZEROLATENCY }, INT_MIN, INT_MAX, VE, "tune" }, + { "psnr", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_TUNE_PSNR }, INT_MIN, INT_MAX, VE, "tune" }, + { "profile", "Encoding profile", OFFSET(profile_id), AV_OPT_TYPE_INT, { .i64 = XEVE_PROFILE_BASELINE }, XEVE_PROFILE_BASELINE, XEVE_PROFILE_MAIN, VE, "profile" }, + { "baseline", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PROFILE_BASELINE }, INT_MIN, INT_MAX, VE, "profile" }, + { "main", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PROFILE_MAIN }, INT_MIN, INT_MAX, VE, "profile" }, + { "rc_mode", "Rate control mode", OFFSET(rc_mode), AV_OPT_TYPE_INT, { .i64 = XEVE_RC_CQP }, XEVE_RC_CQP, XEVE_RC_CRF, VE, "rc_mode" }, + { "CQP", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_RC_CQP }, INT_MIN, INT_MAX, VE, "rc_mode" }, + { "ABR", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_RC_ABR }, INT_MIN, INT_MAX, VE, "rc_mode" }, + { "CRF", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_RC_CRF }, INT_MIN, INT_MAX, VE, "rc_mode" }, + { "qp", "Quantization parameter value for CQP rate control mode", OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 32 }, 0, 51, VE }, + { "crf", "Constant rate factor value for CRF rate control mode", OFFSET(crf), AV_OPT_TYPE_INT, { .i64 = 32 }, 10, 49, VE }, + { "hash", "Embed picture signature (HASH) for conformance checking in decoding", OFFSET(hash), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, + { "sei_info", "Embed SEI messages identifying encoder parameters and command line arguments", OFFSET(sei_info), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, + { "xeve-params", "Override the xeve configuration using a :-separated list of key=value parameters", OFFSET(xeve_params), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE }, + { NULL } +}; + +static const AVClass libxeve_class = { + .class_name = "libxeve", + .item_name = av_default_item_name, + .option = libxeve_options, + .version = LIBAVUTIL_VERSION_INT, +}; + +/** + * libavcodec generic global options, which can be set on all the encoders and decoders + * @see https://www.ffmpeg.org/ffmpeg-codecs.html#Codec-Options + */ +static const FFCodecDefault libxeve_defaults[] = { + { "b", "0" }, // bitrate in terms of kilo-bits per second + { "g", "0" }, // gop_size (key-frame interval 0: only one I-frame at the first time; 1: every frame is coded in I-frame) + { "bf", "15"}, // maximum number of B frames (0: no B-frames, 1,3,7,15) + { "threads", "0"}, // number of threads to be used (0: automatically select the number of threads to set) + { NULL }, +}; + +const FFCodec ff_libxeve_encoder = { + .p.name = "libxeve", + .p.long_name = NULL_IF_CONFIG_SMALL("libxeve MPEG-5 EVC"), + .p.type = AVMEDIA_TYPE_VIDEO, + .p.id = AV_CODEC_ID_EVC, + .init = libxeve_init, + FF_CODEC_ENCODE_CB(libxeve_encode), + .close = libxeve_close, + .priv_data_size = sizeof(XeveContext), + .p.priv_class = &libxeve_class, + .defaults = libxeve_defaults, + .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS | AV_CODEC_CAP_DR1, + .p.profiles = NULL_IF_CONFIG_SMALL(ff_evc_profiles), + .p.wrapper_name = "libxeve", + .p.pix_fmts = supported_pixel_formats, + .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_NOT_INIT_THREADSAFE, +}; From patchwork Wed Aug 16 11:11:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dawid Kozinski X-Patchwork-Id: 43237 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:4c9d:b0:130:ccc6:6c4b with SMTP id fq29csp1172036pzb; Wed, 16 Aug 2023 04:12:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHAQL1+15AJZK1BYPEDQAHRDosFF+76JMCVNJrvo8VcuKBrSofs7VWrlU2c3nL5FiLJTOnN X-Received: by 2002:aa7:c58c:0:b0:523:b37e:b83d with SMTP id g12-20020aa7c58c000000b00523b37eb83dmr1081941edq.41.1692184338582; Wed, 16 Aug 2023 04:12:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692184338; cv=none; d=google.com; s=arc-20160816; b=uzbH40NE2jTGL+QuS47U7Y7eYBAvOYRY7fwk3zh1KNUBz/y1FV1mHhv20c6P3TNUsg GpUiNuIn30yYGSbPlZLUwDqgC13QnU1+R0bulcZ0fQ/77NIB8KeCrGAA409QaEvFOOnQ 7E49DZwiLAYk0ExEgufufxN4pjzZxg7izI3UfZH+QnecyR5swi6WzpylempB27sgPjwW 5QthU+8ZekFSopMGhdQ/GORzZDRJg/FYlyVAB0oplgfCOR+RMYRpf0l7bNKLDa7Lrngf 6a5GxtlZhEtAMpLjQjQB+qTwMlMu6+2UUO7dGyy53HBvvVXW4m/sSOrsCLJE3E/uNCN4 vDtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:cms-type:mime-version :message-id:date:to:from:dkim-signature:dkim-filter:delivered-to; bh=ak+KAeB+jimaw1NX0yWD2s7pVhxQQ3XLqGUk9gWiJhc=; fh=EdLpQc+axqrQtc1PbXA/3aaNYrMDzen0n951paqcS+k=; b=xWmjPPX7SdK94ahGTyP7VQZuVfbQa1W3mnce04CAOi6O8T+9P1shjWwdDXAKKqlXXb zqr6AvxFvSzb39Xvx0ndrBNf2jGy9+OyMfykFhKEleKIACyRqtzGml/kcYMTljj74sll jLu+rHXJroUVnNAghw108SQwLKRNX7i0FKQaaMjThnhxEGZtR9duqF5JSsmZFPvYpB1N ngh2b/G48vhZAEvOL4IZ8JjDYGY/evn4nSqfzCjwYAV3VP2xKPOr4fiGG94zCe0oFV5+ uX6UuTmrED0XjxnbIuIGiAIp0lcEcsYcCrexNrt9eumH0eYYqunsRSz/ooBTFTgA+Ako g6qw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@samsung.com header.s=mail20170921 header.b=BDycJ14F; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=samsung.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id bc22-20020a056402205600b005222b88bb90si10877729edb.133.2023.08.16.04.12.18; Wed, 16 Aug 2023 04:12:18 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@samsung.com header.s=mail20170921 header.b=BDycJ14F; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=samsung.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BD7A368C649; Wed, 16 Aug 2023 14:12:15 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mailout2.w1.samsung.com (mailout2.w1.samsung.com [210.118.77.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DC51D68C57D for ; Wed, 16 Aug 2023 14:12:09 +0300 (EEST) Received: from eucas1p2.samsung.com (unknown [182.198.249.207]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20230816111209euoutp02208fafa7097c1325e44c1e312b5ace68~72K6bZG8B2804028040euoutp02g for ; Wed, 16 Aug 2023 11:12:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20230816111209euoutp02208fafa7097c1325e44c1e312b5ace68~72K6bZG8B2804028040euoutp02g DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1692184329; bh=g1iVrzCKBSrFKDk032Cq1hPMhp1WlbITGHMMRkX3m/Y=; h=From:To:Cc:Subject:Date:References:From; b=BDycJ14FTtK60nKf3lHOJGXZ/huFdGLo6sB+vigitjfNmq/+jeTXwmNVRuYzsBVE7 yEOUcmo5LEYNRU61eMhHZp4V5Y4gsWuc0qvFuWhMbVoul3gHbXzw5dqWdOBZD2A9aj j1tvshd7UxVmm/qnoYrw6EWwKo+GIORMuMyXHA8o= Received: from eusmges3new.samsung.com (unknown [203.254.199.245]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20230816111209eucas1p1d341d3616d4b5ba0a6983c7ef4481d42~72K6SYfD61308413084eucas1p1Q; Wed, 16 Aug 2023 11:12:09 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges3new.samsung.com (EUCPMTA) with SMTP id 01.46.37758.90FACD46; Wed, 16 Aug 2023 12:12:09 +0100 (BST) Received: from eusmtrp2.samsung.com (unknown [182.198.249.139]) by eucas1p1.samsung.com (KnoxPortal) with ESMTPA id 20230816111208eucas1p1cd471546a2fce47a1e0d970999e29d87~72K59svP61949019490eucas1p1Z; Wed, 16 Aug 2023 11:12:08 +0000 (GMT) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eusmtrp2.samsung.com (KnoxPortal) with ESMTP id 20230816111208eusmtrp277db3caea7b274ba56988b316b767da5~72K58gmFQ2532225322eusmtrp2o; Wed, 16 Aug 2023 11:12:08 +0000 (GMT) X-AuditID: cbfec7f5-7ffff7000002937e-7c-64dcaf09b754 Received: from eusmtip1.samsung.com ( [203.254.199.221]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id 28.AD.14344.80FACD46; Wed, 16 Aug 2023 12:12:08 +0100 (BST) Received: from AMDN5164.EU.corp.samsungelectronics.net (unknown [106.210.132.171]) by eusmtip1.samsung.com (KnoxPortal) with ESMTPA id 20230816111208eusmtip1f894313d22dc7fe3d0bcf1bbf44876d0~72K5iYbKt1338113381eusmtip1A; Wed, 16 Aug 2023 11:12:08 +0000 (GMT) From: Dawid Kozinski To: d.frankiewic@samsung.com, ffmpeg-devel@ffmpeg.org Date: Wed, 16 Aug 2023 13:11:31 +0200 Message-Id: <20230816111131.494-1-d.kozinski@samsung.com> X-Mailer: git-send-email 2.37.3.windows.1 MIME-Version: 1.0 X-Unsent: 1 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrLIsWRmVeSWpSXmKPExsWy7djPc7qc6++kGDw8w23x5doXZouV336z WHz7dIbZgdnjz6LNLB59W1YxBjBFcdmkpOZklqUW6dslcGV8O/aMqeD2EcaKzy8uszYwPulk 7GLk5JAQMJGYuu4gkM3FISSwglFi/cF7UM4XRonrN04zQTifGSUuPutmg2l5NuEBM0RiOaPE tc0voaramSReTf4ENphNQFfi5YdHTCC2iICpxMnmvWDdzALaEuvufgezhQUCJO7NXM8OYrMI qEq8WL0QrJdXwFLiat9DqG2aEhO2zGGCiAtKnJz5hAUizi2xbtYlVoiZ8hLNW2czQ8T/skv0 HPWFsF0kll7+BVUvLPHq+BZ2CFtG4v/O+UAzOYDsYolD/Q4QZo3EoR/pEBXWEm8bjzOChJmB Lli/Sx8i7Cjx+c5rRohqPokbbwUh9vNJTNo2nRkizCvR0SYEYapI9HWKQTRKSTxdNgfqQg+J W28mskxgVJyF5KlZSB6ZhbB2ASPzKkbx1NLi3PTUYuO81HK94sTc4tK8dL3k/NxNjMAUcfrf 8a87GFe8+qh3iJGJg/EQowQHs5IIbw/vrRQh3pTEyqrUovz4otKc1OJDjNIcLErivNq2J5OF BNITS1KzU1MLUotgskwcnFINTKEWWyyLNkvu8Luxv36haNecDf+9b8gd+Pj1zdx9B9ketGg6 sHe0uy5/N1Wrn3X90il+uTsfiYc57PuvU2w5/8SEvo1bZSb/5fZg7lXxnvZY3i5D3jpfxch1 pWoU987jv99EKzzwrVrS+Te5wVj+yfNZGt7+1RN7osvbQgJ0lp++93zPVhs2pfaL7S5R50+I 62ae9mN8L3rlg7OYj/33Py9iPV6qzL3Hcen4a57dz0Ne6c/tnH1xqavgwoRIj4A1gTcVnF5f sWvxSo4VnpKSd6lJsNq1X+kZyxOhef01eZfETsnEWR+5+LZNrrK77TL/FH3rQ5XuTIVVZq63 607p1HKkW6coGqT+jX/mFaOlxFKckWioxVxUnAgAdtku+oADAAA= X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrMLMWRmVeSWpSXmKPExsVy+t/xu7oc6++kGNx5aWzx5doXZouV336z WHz7dIbZgdnjz6LNLB59W1YxBjBF6dkU5ZeWpCpk5BeX2CpFG1oY6RlaWugZmVjqGRqbx1oZ mSrp29mkpOZklqUW6dsl6GV8O/aMqeD2EcaKzy8uszYwPulk7GLk5JAQMJF4NuEBM4gtJLCU UeLR1RiIuJTE0qWLoGqEJf5c62LrYuQCqmllklh3tJcVJMEmoCvx8sMjJhBbRMBc4uK6W2AN zALaEuvufmcDsYUF/CQer9zEAmKzCKhKvFi9EKyGV8BS4mrfQzaIBZoSE7bMYYKIC0qcnPmE BSLOLbFu1iVWiJnyEs1bZzNPYOSfhaRsFpLUAkamVYwiqaXFuem5xUZ6xYm5xaV56XrJ+bmb GIGhu+3Yzy07GFe++qh3iJGJg/EQowQHs5IIbw/vrRQh3pTEyqrUovz4otKc1OJDjKZAt05k lhJNzgdGT15JvKGZgamhiZmlgamlmbGSOK9nQUeikEB6YklqdmpqQWoRTB8TB6dUA9NWU+at jyJsdm1qr18T/+yUQMXLwmfLv1ofm72u4f2uPp6vipNWqy36fu9LbemeJQ1MlS7JDrmSUUpv jrGrW7Y0Vc6SeKpyYdbSI3lLQzV2rpuVWPxX6XJ/6JPTi7wVuc/2Kv+7b33VRGZDDtfTf3kv uP9EJvAXbAzvyzp32l/g53w3izefLpa9erCeO/Di8Y2FJh7mf5MeJPt8XLev9tSOtSFhJeVp ayTvyC5ztG2LnnDyb+yXx2+Ef5U0/o+YYtDLW+/EL3liyrJfT9s+PZoxp/nJ+WnfRa46r9zf IfxxKWdL6fpXwlVHn4Vy9Z9ZY39vtmuU7lP7oq27PvQ/4JI45LE69OHFzy2zy5d+5bk+Q4ml OCPRUIu5qDgRAMAiqC/mAgAA X-CMS-MailID: 20230816111208eucas1p1cd471546a2fce47a1e0d970999e29d87 X-Msg-Generator: CA X-RootMTR: 20230816111208eucas1p1cd471546a2fce47a1e0d970999e29d87 X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20230816111208eucas1p1cd471546a2fce47a1e0d970999e29d87 References: Subject: [FFmpeg-devel] [PATCH v27 2/2] avcodec/evc_decoder: Provided support for EVC decoder X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: Dawid Kozinski Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 6YCM6auYRnss - Added EVC decoder wrapper - Changes in project configuration file and libavcodec Makefile - Added documentation for xevd wrapper Signed-off-by: Dawid Kozinski --- configure | 4 + doc/decoders.texi | 24 ++ doc/general_contents.texi | 10 +- libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/libxevd.c | 546 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 585 insertions(+), 1 deletion(-) create mode 100644 libavcodec/libxevd.c diff --git a/configure b/configure index e3e71e61e8..e58e2de279 100755 --- a/configure +++ b/configure @@ -294,6 +294,7 @@ External library support: --enable-libx264 enable H.264 encoding via x264 [no] --enable-libx265 enable HEVC encoding via x265 [no] --enable-libxeve enable EVC encoding via libxeve [no] + --enable-libxevd enable EVC decoding via libxevd [no] --enable-libxavs enable AVS encoding via xavs [no] --enable-libxavs2 enable AVS2 encoding via xavs2 [no] --enable-libxcb enable X11 grabbing using XCB [autodetect] @@ -1906,6 +1907,7 @@ EXTERNAL_LIBRARY_LIST=" libvorbis libvpx libwebp + libxevd libxeve libxml2 libzimg @@ -3467,6 +3469,7 @@ libx265_encoder_deps="libx265" libx265_encoder_select="atsc_a53" libxavs_encoder_deps="libxavs" libxavs2_encoder_deps="libxavs2" +libxevd_decoder_deps="libxevd" libxeve_encoder_deps="libxeve" libxvid_encoder_deps="libxvid" libzvbi_teletext_decoder_deps="libzvbi" @@ -6850,6 +6853,7 @@ enabled libx265 && require_pkg_config libx265 x265 x265.h x265_api_get require_cpp_condition libx265 x265.h "X265_BUILD >= 89" enabled libxavs && require libxavs "stdint.h xavs.h" xavs_encoder_encode "-lxavs $pthreads_extralibs $libm_extralibs" enabled libxavs2 && require_pkg_config libxavs2 "xavs2 >= 1.3.0" "stdint.h xavs2.h" xavs2_api_get +enabled libxevd && require_pkg_config libxevd "xevd >= 0.4.1" "xevd.h" xevd_decode enabled libxeve && require_pkg_config libxeve "xeve >= 0.4.3" "xeve.h" xeve_encode enabled libxvid && require libxvid xvid.h xvid_global -lxvidcore enabled libzimg && require_pkg_config libzimg "zimg >= 2.7.0" zimg.h zimg_get_api_version diff --git a/doc/decoders.texi b/doc/decoders.texi index 09b8314dd2..6311af229f 100644 --- a/doc/decoders.texi +++ b/doc/decoders.texi @@ -130,6 +130,30 @@ Set amount of frame threads to use during decoding. The default value is 0 (auto @end table +@section libxevd + +eXtra-fast Essential Video Decoder (XEVD) MPEG-5 EVC decoder wrapper. + +This decoder requires the presence of the libxevd headers and library +during configuration. You need to explicitly configure the build with +@option{--enable-libxevd}. + +The xevd project website is at @url{https://github.com/mpeg5/xevd}. + +@subsection Options + +The following options are supported by the libxevd wrapper. +The xevd-equivalent options or values are listed in parentheses for easy migration. + +To get a more accurate and extensive documentation of the libxevd options, +invoke the command @code{xevd_app --help} or consult the libxevd documentation. + +@table @option +@item threads (@emph{threads}) +Force to use a specific number of threads + +@end table + @section QSV Decoders The family of Intel QuickSync Video decoders (VC1, MPEG-2, H.264, HEVC, diff --git a/doc/general_contents.texi b/doc/general_contents.texi index b482253fd5..12645a071a 100644 --- a/doc/general_contents.texi +++ b/doc/general_contents.texi @@ -351,6 +351,14 @@ Go to @url{https://github.com/mpeg5/xeve} and follow the instructions for installing the XEVE library. Then pass @code{--enable-libxeve} to configure to enable it. +@section eXtra-fast Essential Video Decoder (XEVD) + +FFmpeg can make use of the XEVD library for EVC video decoding. + +Go to @url{https://github.com/mpeg5/xevd} and follow the instructions for +installing the XEVD library. Then pass @code{--enable-libxevd} to configure to +enable it. + @section ZVBI ZVBI is a VBI decoding library which can be used by FFmpeg to decode DVB @@ -953,7 +961,7 @@ following image formats are supported: @item Escape 124 @tab @tab X @item Escape 130 @tab @tab X @item EVC / MPEG-5 Part 1 @tab X @tab X - @tab encoding and decoding supported through external library libxeve + @tab encoding and decoding supported through external libraries libxeve and libxevd @item FFmpeg video codec #1 @tab X @tab X @tab lossless codec (fourcc: FFV1) @item Flash Screen Video v1 @tab X @tab X diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 658b3baa7e..b587ecf686 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1144,6 +1144,7 @@ OBJS-$(CONFIG_LIBX264_ENCODER) += libx264.o OBJS-$(CONFIG_LIBX265_ENCODER) += libx265.o OBJS-$(CONFIG_LIBXAVS_ENCODER) += libxavs.o OBJS-$(CONFIG_LIBXAVS2_ENCODER) += libxavs2.o +OBJS-$(CONFIG_LIBXEVD_DECODER) += libxevd.o OBJS-$(CONFIG_LIBXEVE_ENCODER) += libxeve.o OBJS-$(CONFIG_LIBXVID_ENCODER) += libxvid.o OBJS-$(CONFIG_LIBZVBI_TELETEXT_DECODER) += libzvbi-teletextdec.o ass.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index aa22e7b6c1..0ee4ded84c 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -820,6 +820,7 @@ extern LIBX264_CONST FFCodec ff_libx264_encoder; extern const FFCodec ff_libx264rgb_encoder; extern FFCodec ff_libx265_encoder; extern const FFCodec ff_libxeve_encoder; +extern const FFCodec ff_libxevd_decoder; extern const FFCodec ff_libxavs_encoder; extern const FFCodec ff_libxavs2_encoder; extern const FFCodec ff_libxvid_encoder; diff --git a/libavcodec/libxevd.c b/libavcodec/libxevd.c new file mode 100644 index 0000000000..81350ae35b --- /dev/null +++ b/libavcodec/libxevd.c @@ -0,0 +1,546 @@ +/* + * libxevd decoder + * EVC (MPEG-5 Essential Video Coding) decoding using XEVD MPEG-5 EVC decoder library + * + * Copyright (C) 2021 Dawid Kozinski + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +#include + +#include "libavutil/internal.h" +#include "libavutil/common.h" +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" +#include "libavutil/pixfmt.h" +#include "libavutil/imgutils.h" +#include "libavutil/cpu.h" + +#include "avcodec.h" +#include "internal.h" +#include "packet_internal.h" +#include "codec_internal.h" +#include "profiles.h" +#include "decode.h" + +#define XEVD_PARAM_BAD_NAME -1 +#define XEVD_PARAM_BAD_VALUE -2 + +#define EVC_NAL_HEADER_SIZE 2 /* byte */ + +/** + * The structure stores all the states associated with the instance of Xeve MPEG-5 EVC decoder + */ +typedef struct XevdContext { + const AVClass *class; + + XEVD id; // XEVD instance identifier @see xevd.h + XEVD_CDSC cdsc; // decoding parameters @see xevd.h + + // If end of stream occurs it is required "flushing" (aka draining) the codec, + // as the codec might buffer multiple frames or packets internally. + int draining_mode; // The flag is set if codec enters draining mode. + + AVPacket *pkt; // access unit (a set of NAL units that are consecutive in decoding order and containing exactly one encoded image) +} XevdContext; + +/** + * The function populates the XEVD_CDSC structure. + * XEVD_CDSC contains all decoder parameters that should be initialized before its use. + * + * @param[in] avctx codec context + * @param[out] cdsc contains all decoder parameters that should be initialized before its use + * + */ +static void get_conf(AVCodecContext *avctx, XEVD_CDSC *cdsc) +{ + int cpu_count = av_cpu_count(); + + /* clear XEVS_CDSC structure */ + memset(cdsc, 0, sizeof(XEVD_CDSC)); + + /* init XEVD_CDSC */ + if (avctx->thread_count <= 0) + cdsc->threads = (cpu_count < XEVD_MAX_TASK_CNT) ? cpu_count : XEVD_MAX_TASK_CNT; + else if (avctx->thread_count > XEVD_MAX_TASK_CNT) + cdsc->threads = XEVD_MAX_TASK_CNT; + else + cdsc->threads = avctx->thread_count; +} + +/** + * Read NAL unit length + * @param bs input data (bitstream) + * @return the length of NAL unit on success, 0 value on failure + */ +static uint32_t read_nal_unit_length(const uint8_t *bs, int bs_size, AVCodecContext *avctx) +{ + uint32_t len = 0; + XEVD_INFO info; + int ret; + + if (bs_size == XEVD_NAL_UNIT_LENGTH_BYTE) { + ret = xevd_info((void *)bs, XEVD_NAL_UNIT_LENGTH_BYTE, 1, &info); + if (XEVD_FAILED(ret)) { + av_log(avctx, AV_LOG_ERROR, "Cannot get bitstream information\n"); + return 0; + } + len = info.nalu_len; + if (len == 0) { + av_log(avctx, AV_LOG_ERROR, "Invalid bitstream size! [%d]\n", bs_size); + return 0; + } + } + + return len; +} + +/** + * @param[in] xectx the structure that stores all the state associated with the instance of Xeve MPEG-5 EVC decoder + * @param[out] avctx codec context + * @return 0 on success, negative value on failure + */ +static int export_stream_params(const XevdContext *xectx, AVCodecContext *avctx) +{ + int ret; + int size; + int color_space; + + avctx->pix_fmt = AV_PIX_FMT_YUV420P10; + + size = 4; + ret = xevd_config(xectx->id, XEVD_CFG_GET_CODED_WIDTH, &avctx->coded_width, &size); + if (XEVD_FAILED(ret)) { + av_log(avctx, AV_LOG_ERROR, "Failed to get coded_width\n"); + return AVERROR_EXTERNAL; + } + + ret = xevd_config(xectx->id, XEVD_CFG_GET_CODED_HEIGHT, &avctx->coded_height, &size); + if (XEVD_FAILED(ret)) { + av_log(avctx, AV_LOG_ERROR, "Failed to get coded_height\n"); + return AVERROR_EXTERNAL; + } + + ret = xevd_config(xectx->id, XEVD_CFG_GET_WIDTH, &avctx->width, &size); + if (XEVD_FAILED(ret)) { + av_log(avctx, AV_LOG_ERROR, "Failed to get width\n"); + return AVERROR_EXTERNAL; + } + + ret = xevd_config(xectx->id, XEVD_CFG_GET_HEIGHT, &avctx->height, &size); + if (XEVD_FAILED(ret)) { + av_log(avctx, AV_LOG_ERROR, "Failed to get height\n"); + return AVERROR_EXTERNAL; + } + + ret = xevd_config(xectx->id, XEVD_CFG_GET_COLOR_SPACE, &color_space, &size); + if (XEVD_FAILED(ret)) { + av_log(avctx, AV_LOG_ERROR, "Failed to get color_space\n"); + return AVERROR_EXTERNAL; + } + switch(color_space) { + case XEVD_CS_YCBCR400_10LE: + avctx->pix_fmt = AV_PIX_FMT_GRAY10LE; + break; + case XEVD_CS_YCBCR420_10LE: + avctx->pix_fmt = AV_PIX_FMT_YUV420P10LE; + break; + case XEVD_CS_YCBCR422_10LE: + avctx->pix_fmt = AV_PIX_FMT_YUV422P10LE; + break; + case XEVD_CS_YCBCR444_10LE: + avctx->pix_fmt = AV_PIX_FMT_YUV444P10LE; + break; + default: + av_log(avctx, AV_LOG_ERROR, "Unknown color space\n"); + avctx->pix_fmt = AV_PIX_FMT_NONE; + return AVERROR_INVALIDDATA; + } + + // the function returns sps->num_reorder_pics + ret = xevd_config(xectx->id, XEVD_CFG_GET_MAX_CODING_DELAY, &avctx->max_b_frames, &size); + if (XEVD_FAILED(ret)) { + av_log(avctx, AV_LOG_ERROR, "Failed to get max_coding_delay\n"); + return AVERROR_EXTERNAL; + } + + avctx->has_b_frames = (avctx->max_b_frames) ? 1 : 0; + + return 0; +} + +/** + * @brief Copy image in imgb to frame. + * + * @param avctx codec context + * @param[in] imgb + * @param[out] frame + * @return 0 on success, negative value on failure + */ +static int libxevd_image_copy(struct AVCodecContext *avctx, XEVD_IMGB *imgb, struct AVFrame *frame) +{ + int ret; + if (imgb->cs != XEVD_CS_YCBCR420_10LE) { + av_log(avctx, AV_LOG_ERROR, "Not supported pixel format: %s\n", av_get_pix_fmt_name(avctx->pix_fmt)); + return AVERROR_INVALIDDATA; + } + + if (imgb->w[0] != avctx->width || imgb->h[0] != avctx->height) { // stream resolution changed + if (ff_set_dimensions(avctx, imgb->w[0], imgb->h[0]) < 0) { + av_log(avctx, AV_LOG_ERROR, "Cannot set new dimension\n"); + return AVERROR_INVALIDDATA; + } + } + + if (ret = ff_get_buffer(avctx, frame, 0) < 0) + return ret; + + av_image_copy(frame->data, frame->linesize, (const uint8_t **)imgb->a, + imgb->s, avctx->pix_fmt, + imgb->w[0], imgb->h[0]); + + return 0; +} + +/** + * Initialize decoder + * Create a decoder instance and allocate all the needed resources + * + * @param avctx codec context + * @return 0 on success, negative error code on failure + */ +static av_cold int libxevd_init(AVCodecContext *avctx) +{ + XevdContext *xectx = avctx->priv_data; + XEVD_CDSC *cdsc = &(xectx->cdsc); + + /* read configurations and set values for created descriptor (XEVD_CDSC) */ + get_conf(avctx, cdsc); + + /* create decoder */ + xectx->id = xevd_create(&(xectx->cdsc), NULL); + if (xectx->id == NULL) { + av_log(avctx, AV_LOG_ERROR, "Cannot create XEVD encoder\n"); + return AVERROR_EXTERNAL; + } + + xectx->draining_mode = 0; + xectx->pkt = av_packet_alloc(); + + return 0; +} + +/** + * Decode frame with decoupled packet/frame dataflow + * + * @param avctx codec context + * @param[out] frame decoded frame + * + * @return 0 on success, negative error code on failure + */ +static int libxevd_receive_frame(AVCodecContext *avctx, AVFrame *frame) +{ + XevdContext *xectx = avctx->priv_data; + AVPacket *pkt = xectx->pkt; + XEVD_IMGB *imgb = NULL; + + int xevd_ret = 0; + int ret = 0; + + if (!pkt) + return AVERROR(ENOMEM); + + // obtain access unit (input data) - a set of NAL units that are consecutive in decoding order and containing exactly one encoded image + ret = ff_decode_get_packet(avctx, pkt); + if (ret < 0 && ret != AVERROR_EOF) { + av_packet_unref(pkt); + + return ret; + } else if(ret == AVERROR_EOF && xectx->draining_mode == 0) { // End of stream situations. Enter draining mode + + xectx->draining_mode = 1; + av_packet_unref(pkt); + } + + if (pkt->size > 0) { + int bs_read_pos = 0; + XEVD_STAT stat; + XEVD_BITB bitb; + int nalu_size; + AVPacket* pkt_au; + imgb = NULL; + + pkt_au = av_packet_clone(pkt); + av_packet_unref(pkt); + + // get all nal units from AU + while(pkt_au->size > (bs_read_pos + XEVD_NAL_UNIT_LENGTH_BYTE)) { + memset(&stat, 0, sizeof(XEVD_STAT)); + + nalu_size = read_nal_unit_length(pkt_au->data + bs_read_pos, XEVD_NAL_UNIT_LENGTH_BYTE, avctx); + if (nalu_size == 0) { + av_log(avctx, AV_LOG_ERROR, "Invalid bitstream\n"); + av_packet_free(&pkt_au); + ret = AVERROR_INVALIDDATA; + + return ret; + } + bs_read_pos += XEVD_NAL_UNIT_LENGTH_BYTE; + + bitb.addr = pkt_au->data + bs_read_pos; + bitb.ssize = nalu_size; + bitb.pdata[0] = pkt_au; + bitb.ts[XEVD_TS_DTS] = pkt_au->dts; + + /* main decoding block */ + xevd_ret = xevd_decode(xectx->id, &bitb, &stat); + if (XEVD_FAILED(xevd_ret)) { + av_log(avctx, AV_LOG_ERROR, "Failed to decode bitstream\n"); + av_packet_free(&pkt_au); + ret = AVERROR_EXTERNAL; + + return ret; + } + + bs_read_pos += nalu_size; + + if (stat.nalu_type == XEVD_NUT_SPS) { // EVC stream parameters changed + if ((ret = export_stream_params(xectx, avctx)) != 0) { + av_log(avctx, AV_LOG_ERROR, "Failed to export stream params\n"); + av_packet_free(&pkt_au); + + return ret; + } + } + + if (stat.read != nalu_size) + av_log(avctx, AV_LOG_INFO, "Different reading of bitstream (in:%d, read:%d)\n,", nalu_size, stat.read); + + // stat.fnum - has negative value if the decoded data is not frame + if (stat.fnum >= 0) { + + xevd_ret = xevd_pull(xectx->id, &imgb); // The function returns a valid image only if the return code is XEVD_OK + + if (XEVD_FAILED(xevd_ret)) { + av_log(avctx, AV_LOG_ERROR, "Failed to pull the decoded image (xevd error code: %d, frame#=%d)\n", xevd_ret, stat.fnum); + ret = AVERROR_EXTERNAL; + av_packet_free(&pkt_au); + + return ret; + } else if (xevd_ret == XEVD_OK_FRM_DELAYED) { + if(bs_read_pos == pkt->size) { + return AVERROR(EAGAIN); + } + } else { // XEVD_OK + if (!imgb) { + if(bs_read_pos == pkt->size) { + av_log(avctx, AV_LOG_ERROR, "Invalid decoded image data\n"); + + av_packet_free(&pkt_au); + return AVERROR(EAGAIN); + } + } else { + // got frame + AVPacket* pkt_au_imgb = (AVPacket*)imgb->pdata[0]; + if(!pkt_au_imgb) { + av_log(avctx, AV_LOG_ERROR, "Invalid data needed to fill frame properties\n"); + + ret = AVERROR_INVALIDDATA; + + av_packet_free(&pkt_au); + + imgb->release(imgb); + imgb = NULL; + + av_frame_unref(frame); + + return ret; + } + + ret = libxevd_image_copy(avctx, imgb, frame); + if(ret < 0) { + av_log(avctx, AV_LOG_ERROR, "Image copying error\n"); + + av_packet_free(&pkt_au); + av_packet_free(&pkt_au_imgb); + + imgb->release(imgb); + imgb = NULL; + + av_frame_unref(frame); + + return ret; + } + + // use ff_decode_frame_props_from_pkt() to fill frame properties + ret = ff_decode_frame_props_from_pkt(avctx, frame, pkt_au_imgb); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "ff_decode_frame_props_from_pkt error\n"); + + av_packet_free(&pkt_au); + av_packet_free(&pkt_au_imgb); + + imgb->release(imgb); + imgb = NULL; + + av_frame_unref(frame); + + return ret; + } + + frame->pkt_dts = imgb->ts[XEVD_TS_DTS]; + frame->pts = imgb->ts[XEVD_TS_PTS]; + + // xevd_pull uses pool of objects of type XEVD_IMGB. + // The pool size is equal MAX_PB_SIZE (26), so release object when it is no more needed + imgb->release(imgb); + imgb = NULL; + + if(bs_read_pos == pkt->size) { + av_packet_free(&pkt_au); + av_packet_free(&pkt_au_imgb); + + av_frame_unref(frame); + return 0; + } + } + } + } + } + } else { // decoder draining mode handling + + xevd_ret = xevd_pull(xectx->id, &imgb); + + if (xevd_ret == XEVD_ERR_UNEXPECTED) { // draining process completed + av_log(avctx, AV_LOG_DEBUG, "Draining process completed\n"); + + return AVERROR_EOF; + } else if (XEVD_FAILED(xevd_ret)) { // handle all other errors + av_log(avctx, AV_LOG_ERROR, "Failed to pull the decoded image (xevd error code: %d)\n", xevd_ret); + + return AVERROR_EXTERNAL; + } else { // XEVD_OK + AVPacket* pkt_au_imgb; + if (!imgb) { + av_log(avctx, AV_LOG_ERROR, "Invalid decoded image data\n"); + + return AVERROR_EXTERNAL; + } + + pkt_au_imgb = (AVPacket*)imgb->pdata[0]; + if(!pkt_au_imgb) { + av_log(avctx, AV_LOG_ERROR, "Invalid data needed to fill frame properties\n"); + ret = AVERROR_INVALIDDATA; + + imgb->release(imgb); + imgb = NULL; + + av_frame_unref(frame); + + return ret; + } + + // got frame + ret = libxevd_image_copy(avctx, imgb, frame); + if(ret < 0) { + av_packet_free(&pkt_au_imgb); + av_frame_unref(frame); + + imgb->release(imgb); + imgb = NULL; + + return ret; + } + // use ff_decode_frame_props_from_pkt() to fill frame properties + ret = ff_decode_frame_props_from_pkt(avctx, frame, pkt_au_imgb); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "ff_decode_frame_props_from_pkt error\n"); + + av_packet_free(&pkt_au_imgb); + av_frame_unref(frame); + + imgb->release(imgb); + imgb = NULL; + + return ret; + } + + frame->pkt_dts = imgb->ts[XEVD_TS_DTS]; + frame->pts = imgb->ts[XEVD_TS_PTS]; + + av_packet_free(&pkt_au_imgb); + + // xevd_pull uses pool of objects of type XEVD_IMGB. + // The pool size is equal MAX_PB_SIZE (26), so release object when it is no more needed + imgb->release(imgb); + imgb = NULL; + + return 0; + } + } + + return ret; +} + +/** + * Destroy decoder + * + * @param avctx codec context + * @return 0 on success + */ +static av_cold int libxevd_close(AVCodecContext *avctx) +{ + XevdContext *xectx = avctx->priv_data; + if (xectx->id) { + xevd_delete(xectx->id); + xectx->id = NULL; + } + + xectx->draining_mode = 0; + av_packet_free(&xectx->pkt); + + return 0; +} + +#define OFFSET(x) offsetof(XevdContext, x) +#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM + +static const AVClass libxevd_class = { + .class_name = "libxevd", + .item_name = av_default_item_name, + .version = LIBAVUTIL_VERSION_INT, +}; + +const FFCodec ff_libxevd_decoder = { + .p.name = "evc", + .p.long_name = NULL_IF_CONFIG_SMALL("EVC / MPEG-5 Essential Video Coding (EVC)"), + .p.type = AVMEDIA_TYPE_VIDEO, + .p.id = AV_CODEC_ID_EVC, + .init = libxevd_init, + FF_CODEC_RECEIVE_FRAME_CB(libxevd_receive_frame), + .close = libxevd_close, + .priv_data_size = sizeof(XevdContext), + .p.priv_class = &libxevd_class, + .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS | AV_CODEC_CAP_AVOID_PROBING, + .p.profiles = NULL_IF_CONFIG_SMALL(ff_evc_profiles), + .p.wrapper_name = "libxevd", + .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_NOT_INIT_THREADSAFE | FF_CODEC_CAP_SETS_PKT_DTS | FF_CODEC_CAP_SETS_FRAME_PROPS +};