From patchwork Fri Oct 7 09:12:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dawid Kozinski X-Patchwork-Id: 38600 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:4d9:b0:9c:f4b:4e41 with SMTP id 25csp173832pzd; Fri, 7 Oct 2022 02:13:18 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7m2fU1/hyxLKhhy/51bYm2eG7APfZJ8Jpapjyry8qweisICQQkrKjY/FUujwgE8M1t/5kv X-Received: by 2002:a17:907:2be6:b0:78d:45d5:4867 with SMTP id gv38-20020a1709072be600b0078d45d54867mr3261974ejc.105.1665133997802; Fri, 07 Oct 2022 02:13:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665133997; cv=none; d=google.com; s=arc-20160816; b=HZ1ioqQTjG+wIHEdk1nqXd62AuXb2EnEPj2y6Q54oXiy2jcajlVRRzc0mJWbx0ITrg OjF+zOskNfKfG2eKXQN5uDiALj0I8t+Z9wBtCFNU99F5E3YidhTu+fi+KMOiQOmuvYhe R0KbVJ1stkow3eBNXCAIXslO8Za5YmBFzWkejpP//+j4hbvC5EhDcMhjG3lIoGA/FlK6 agzQZOLWN2QN+nAvbfOKl86hES6rOMLXv++D+e7YjNkhZDubsa8xNLVkQtyax28Olpbi ThwtRxowR6CaEqHj2z1Y8xp9TyacLQeGOZZnSVdio1SdS+2qKPmfjLqETq8e0WQUaBkN TVqA== 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=H4b/CFokSo9pGarVKPQRQGj8MUVA3FrfqgdWl8r9cts=; b=mR2izEBr/mAdZ0sjG/JdllzPILq+GvV3WWD8j8yxxJq/bmGYzHa2ZPqVAQfLyyOKgW 5zU8N0u3KRJXLL1K+Vb6jfjV/jr2LY73kpDw+Wuqqq/6bIf5RzebLXN46LysO/LL0gFr 9kM09nbOPl1kKcSgir2nRdMZM0wBjesWGG7s7cAdt98DXlXAOwh4Xy7qylAx3vPvhe72 rhA8RIWFx2s1TwjS8MCdbcvaWie/ByiwoIXGAOzkuOhaMrXdTOv0AElDLARRhyTjdPsO hlwEvP/uCudkxncKI+Bn5apV/y6O7pmaMWAQxEdPU+ih1K8lzTsLnvOwRthYZuNH27YX bi+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@samsung.com header.s=mail20170921 header.b=vL14z+2K; 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 t9-20020a50d709000000b004542c9947c1si1699086edi.217.2022.10.07.02.13.16; Fri, 07 Oct 2022 02:13:17 -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=vL14z+2K; 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 34AE568BBFD; Fri, 7 Oct 2022 12:13:14 +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 C798F68A72F for ; Fri, 7 Oct 2022 12:13:07 +0300 (EEST) Received: from eucas1p1.samsung.com (unknown [182.198.249.206]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20221007091307euoutp02b7c30526f11e5e8c8b1420543d393889~bvooMknHz2052520525euoutp02M for ; Fri, 7 Oct 2022 09:13:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20221007091307euoutp02b7c30526f11e5e8c8b1420543d393889~bvooMknHz2052520525euoutp02M DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1665133987; bh=Y/IvSa00RO80AevP/J43aWwjHBnWG3L9DDH/dymtR4c=; h=From:To:Cc:Subject:Date:References:From; b=vL14z+2KvJwSoLcKpdnZL16zy8dJp1UDn413WcEw1SYSmWaO3hBCtvANzsnBwQv7I nJkJ7MLhAANm3icxdqrIEEBU4noaqMt4ky35kEkb9Aa+HC05jd6f82AielUn/24VBO GAmlt9tlBRHRU61s39DF97kSzxM6Z+bx3/12r9fE= Received: from eusmges2new.samsung.com (unknown [203.254.199.244]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20221007091307eucas1p1a4224ff0b02b3a05c676442ef00850d0~bvon55IKJ2633626336eucas1p1g; Fri, 7 Oct 2022 09:13:07 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges2new.samsung.com (EUCPMTA) with SMTP id 97.D5.07817.2ADEF336; Fri, 7 Oct 2022 10:13:06 +0100 (BST) Received: from eusmtrp1.samsung.com (unknown [182.198.249.138]) by eucas1p1.samsung.com (KnoxPortal) with ESMTPA id 20221007091306eucas1p1dada13179d77572d38c4aacd625a1801~bvonbdzr62633626336eucas1p1f; Fri, 7 Oct 2022 09:13:06 +0000 (GMT) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eusmtrp1.samsung.com (KnoxPortal) with ESMTP id 20221007091306eusmtrp1633c9a4568eec702cc7b44df77c3f422~bvonatEAe1622516225eusmtrp1A; Fri, 7 Oct 2022 09:13:06 +0000 (GMT) X-AuditID: cbfec7f4-8abff70000011e89-29-633feda21ebd Received: from eusmtip2.samsung.com ( [203.254.199.222]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id D4.C3.10862.2ADEF336; Fri, 7 Oct 2022 10:13:06 +0100 (BST) Received: from AMDN5164.EU.corp.samsungelectronics.net (unknown [106.210.132.171]) by eusmtip2.samsung.com (KnoxPortal) with ESMTPA id 20221007091306eusmtip2450c4719021230e92b27f83cf8b8e8e9~bvonHjK7D2452924529eusmtip2e; Fri, 7 Oct 2022 09:13:06 +0000 (GMT) From: Dawid Kozinski To: d.frankiewic@samsung.com, ffmpeg-devel@ffmpeg.org Date: Fri, 7 Oct 2022 11:12:57 +0200 Message-Id: <20221007091257.425-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+NgFnrNIsWRmVeSWpSXmKPExsWy7djPc7qL3tonG7z6aW7x5doXZouV336z WHz7dIbZgdnjz6LNLB59W1YxBjBFcdmkpOZklqUW6dslcGX8+3SVveDIIsaK89cOsDYw7i7r YuTkkBAwkXjdf46ti5GLQ0hgBaPE9WOnmCGcL4wSi3fPg3I+M0o0z5jJDNOypKWPFSKxnFFi 99xXLBBOO5PEjl8L2EGq2AR0JV5+eMQEYosImEqcbN7LBmIzC2hLrLv7HcwWFgiQWPFhHpjN IqAqsf3+VKB6Dg5eAUuJuTt4IZZpSkzYMgdsDK+AoMTJmU9YIOLcEutmXWKFGCkv0bx1Ntil EgJ/2SWufDzPClHkItHyayYThC0s8er4FnYIW0bi9OQeFpBdEgLFEof6HSDMGolDP9IhKqwl 3jYeZwQJMwOdsH6XPkTYUWLJnF42iGo+iRtvBSEO4JOYtG06M0SYV6KjTQjCVJHo6xSDaJSS eLpsDlSFh8S+1WETGBVnIXlqFpJHZiFsXcDIvIpRPLW0ODc9tdgoL7Vcrzgxt7g0L10vOT93 EyMwPZz+d/zLDsblrz7qHWJk4mA8xCjBwawkwrtzp12yEG9KYmVValF+fFFpTmrxIUZpDhYl cV62GVrJQgLpiSWp2ampBalFMFkmDk6pBqYZ3SqzZbIbVU6qXe/slbJlO7U3pZ/v9q8a5zvB Toc1Df4Wv2U+r/t8UnCp9IxP2ws6ztTOPMg/N/LgqWPvIo6rKKZPkgh4rLD9wL6vf1/IbTWS urGOxSllcg7LwZMea4VZn1bc9VxtfPTJB79v7pK7a9tLdJvm6seyOazN37d17+f7ncuWnQ4/ 2TV9mbf+4Vvrf+pdeFfO63Hx3iXrQpM/dh3XHyupt8mXlwoJ8N2b3vL069YLrnpGXQ4/EyIO +iUsL8yqdp6iJJXw5Kn4ksN8dfZCl5b8331ZeapdX1Pgz1q/uzNb97x+ZasipG94UmW3j0bu Wc+udbd4pyosO3D/UZpTYlzhhcK8U5vLTCuUWIozEg21mIuKEwEpa+ypfgMAAA== X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrELMWRmVeSWpSXmKPExsVy+t/xe7qL3tonGxy/JGvx5doXZouV336z WHz7dIbZgdnjz6LNLB59W1YxBjBF6dkU5ZeWpCpk5BeX2CpFG1oY6RlaWugZmVjqGRqbx1oZ mSrp29mkpOZklqUW6dsl6GX8+3SVveDIIsaK89cOsDYw7i7rYuTkkBAwkVjS0sfaxcjFISSw lFFi86r3zBAJKYmlSxcxQtjCEn+udbFBFLUySfzaMJcdJMEmoCvx8sMjJhBbRMBc4uK6W2AN zALaEuvufmcDsYUF/CSeNr8Hs1kEVCW2358KVM/BwStgKTF3By/EfE2JCVvmgI3hFRCUODnz CQtEnFti3axLrBAj5SWat85mnsDIPwtJ2SwkqQWMTKsYRVJLi3PTc4uN9IoTc4tL89L1kvNz NzECA3fbsZ9bdjCufPVR7xAjEwfjIUYJDmYlEd6dO+2ShXhTEiurUovy44tKc1KLDzGaAp06 kVlKNDkfGDt5JfGGZgamhiZmlgamlmbGSuK8ngUdiUIC6YklqdmpqQWpRTB9TBycUg1MzQ6/ 989dUPp65UvtsJu/H7Ze3XyfzyTAZ3mU3enmhS6TjAIlnOM2nzQtXmkmn/vGV9YtxNrEdfc9 led6GdfYnd5/EuVyVmY1P7PIrXXnOa+tR2OTpB5989r3rntNsY1a2wfXUwJMBz+3nunPYlP0 aGfbNKOx8B3r9asi+vG3Q3w/f9+Z5BR6ILPOdlPttISr/MuZlFavWhGctsgq8brp1OIXVqwb Joif6r6QPjfIbUu61/nC83Oa0tYLnj5Yb150aO5/k/l18U9lemwUlYOmbfW3YggNf293+N9E P956ibdZHol/Uq40Lvffxdvxs2y5K0ccK8OO9NkrGOceKU+9FXruqyGX4NwNOV3uWzYpsRRn JBpqMRcVJwIAm6En8+UCAAA= X-CMS-MailID: 20221007091306eucas1p1dada13179d77572d38c4aacd625a1801 X-Msg-Generator: CA X-RootMTR: 20221007091306eucas1p1dada13179d77572d38c4aacd625a1801 X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20221007091306eucas1p1dada13179d77572d38c4aacd625a1801 References: Subject: [FFmpeg-devel] [PATCH v13 6/9] 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: la4Y2e2jqhza - 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 | 423 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 462 insertions(+), 1 deletion(-) create mode 100644 libavcodec/libxevd.c diff --git a/configure b/configure index 7f430995a6..0adbd4291e 100755 --- a/configure +++ b/configure @@ -292,6 +292,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] @@ -1877,6 +1878,7 @@ EXTERNAL_LIBRARY_LIST=" libvorbis libvpx libwebp + libxevd libxeve libxml2 libzimg @@ -3409,6 +3411,7 @@ libx264rgb_encoder_select="libx264_encoder" libx265_encoder_deps="libx265" 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" @@ -6742,6 +6745,7 @@ enabled libx265 && require_pkg_config libx265 x265 x265.h x265_api_get require_cpp_condition libx265 x265.h "X265_BUILD >= 70" 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.0" "xevd.h" xevd_decode enabled libxeve && require_pkg_config libxeve "xeve >= 0.4.0" "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 e2fcbf5dc9..0bd9096114 100644 --- a/doc/decoders.texi +++ b/doc/decoders.texi @@ -126,6 +126,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 417f0ab64f..0d9daf43a7 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 @@ -944,7 +952,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 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 8944e2a0d6..f2799ae19b 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1107,6 +1107,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 5ce398f9ee..571ca31056 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -808,6 +808,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..133d004f4b --- /dev/null +++ b/libavcodec/libxevd.c @@ -0,0 +1,423 @@ +/* + * 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 +} 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; + + avctx->color_primaries = AVCOL_PRI_UNSPECIFIED; + avctx->color_trc = AVCOL_TRC_UNSPECIFIED; + avctx->colorspace = AVCOL_SPC_UNSPECIFIED; + + 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; + } + + return 0; +} + +/** + * Decode picture + * + * @param avctx codec context + * @param[out] frame decoded frame + * @param[out] got_frame_ptr decoder sets to 0 or 1 to indicate that a + * non-empty frame or subtitle was returned in + * outdata. + * @param[in] avpkt AVPacket containing encoded data to be decoded + * + * @return amount of bytes read from the packet on success, negative error + * code on failure + */ +static int libxevd_decode(struct AVCodecContext *avctx, struct AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt) +{ + XevdContext *xectx = NULL; + XEVD_IMGB *imgb = NULL; + XEVD_STAT stat; + XEVD_BITB bitb; + int xevd_ret, nalu_size, bs_read_pos; + int ret = 0; + + xectx = avctx->priv_data; + + if (avpkt->size > 0) { + bs_read_pos = 0; + imgb = NULL; + while(avpkt->size > (bs_read_pos + XEVD_NAL_UNIT_LENGTH_BYTE)) { + memset(&stat, 0, sizeof(XEVD_STAT)); + memset(&bitb, 0, sizeof(XEVD_BITB)); + + nalu_size = read_nal_unit_length(avpkt->data + bs_read_pos, XEVD_NAL_UNIT_LENGTH_BYTE, avctx); + if (nalu_size == 0) { + av_log(avctx, AV_LOG_ERROR, "Invalid bitstream\n"); + ret = AVERROR_INVALIDDATA; + goto ERR; + } + bs_read_pos += XEVD_NAL_UNIT_LENGTH_BYTE; + + bitb.addr = avpkt->data + bs_read_pos; + bitb.ssize = nalu_size; + + /* 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"); + ret = AVERROR_EXTERNAL; + goto ERR; + } + + bs_read_pos += nalu_size; + + if (stat.nalu_type == XEVD_NUT_SPS) { // EVC stream parameters changed + if ((ret = export_stream_params(xectx, avctx)) != 0) + goto ERR; + } + + if (stat.read != nalu_size) + av_log(avctx, AV_LOG_INFO, "Different reading of bitstream (in:%d, read:%d)\n,", nalu_size, stat.read); + if (stat.fnum >= 0) { + // already has a decoded image + if (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; + } + xevd_ret = xevd_pull(xectx->id, &imgb); + 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; + goto ERR; + } else if (xevd_ret == XEVD_OK_FRM_DELAYED) { + if (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; + } + } + if (imgb) { + int ret = libxevd_image_copy(avctx, imgb, frame); + if(ret < 0) { + goto ERR; + } + + frame->pts = avpkt->pts; + *got_frame_ptr = 1; + + // 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; + } else + *got_frame_ptr = 0; + } + } + } else { // bumping + xevd_ret = xevd_pull(xectx->id, &(imgb)); + if (xevd_ret == XEVD_ERR_UNEXPECTED) { // bumping process completed + *got_frame_ptr = 0; + return 0; + } else if (XEVD_FAILED(xevd_ret)) { + av_log(avctx, AV_LOG_ERROR, "Failed to pull the decoded image (xevd error code: %d)\n", xevd_ret); + ret = AVERROR_EXTERNAL; + goto ERR; + } + if (imgb) { + int ret = libxevd_image_copy(avctx, imgb, frame); + if(ret < 0) { + goto ERR; + } + + frame->pts = avpkt->pts; + *got_frame_ptr = 1; + + // 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; + } else + *got_frame_ptr = 0; + } + + return avpkt->size; + +ERR: + if (imgb) { + imgb->release(imgb); + imgb = NULL; + } + *got_frame_ptr = 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; + } + + 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_DECODE_CB(libxevd_decode), + .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 | AV_CODEC_CAP_DR1, + .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, +};