From patchwork Tue Jul 18 03:11:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Fei W" X-Patchwork-Id: 42792 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:b813:b0:130:ccc6:6c4b with SMTP id fi19csp220607pzb; Mon, 17 Jul 2023 20:12:47 -0700 (PDT) X-Google-Smtp-Source: APBJJlFnUXZjEE9lSlsZSASSrdUJuDNMdZqP3cyUhDJ78h9Ys33SD6dxeY7xZ5au3qXzd8eQ0U38 X-Received: by 2002:a2e:3001:0:b0:2b6:ee6d:b647 with SMTP id w1-20020a2e3001000000b002b6ee6db647mr9270007ljw.28.1689649967023; Mon, 17 Jul 2023 20:12:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689649966; cv=none; d=google.com; s=arc-20160816; b=0w1WKruIEhRxhfWF4JKFgLVrpyaHjSdmQPjmgonfvGZwcpCXOI+ummE4rAWbUnytc+ HgKHodivbtMpDRrnhaUOJALQtznCvGpHOPlveQYbMfEaV5TEWMThRBapqU6It8xEd2LB 3UknCAcVuTVg6R63D2UAj5MwdLxha+FsWfvEbLQV1j8oY0S1HvW0Tp0oXx+RlRJ4B77E d1+Au0pxyQVFGdiFp9LXjqwmer4TCP3IWqD9pbQKJnuO4A7cB3KYAS1krSJ2/Dj9fuFE pNn6Ad7auMucwI+ga6/N60KvYf0+3r1kiVeIec1+l1NMSZDNwU+ck56Y2syfq8XRLIgF DbmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=3MLip0LBKLCXNnW4tYhdHVmqqtGB3oRiJkVX8BTCDPc=; fh=mjbWUF9Ti4XQB/BKUMmQysKKolB1ZI3Lwoj9K8X2M00=; b=lCgaMdTNpfOOg1yogoVd3np7A716Nzwhk+mgYkybaStkll64tVeyCFMJrwNLn7I6X6 D+QECCKULqtcfHiZTVT0O6iwVQ/8Fb5Sw6N13mACZPH2scUHNWBjnrftBXxvhsE5XxS6 2lYj/33edxm3ixDyAPpeiZmA2RJ2r777m4szg6Ii21LlualpWl8fZJKstgmV4Gu0K6x3 Hp+Pkqe7O5yEbGaC9IlsZ2QVbAL3mqB+VjJYvFks5GrAwhYnD51OsNTr8s5r5aPu2wHc B/g8/AaWo24igJI0HBeby8e0LI3cybVJUYuyaxb9j+9GjxJJ0wOfEMo35GMBkbMc9Psv V7dw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@intel.com header.s=Intel header.b="nW1q3/HG"; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id z11-20020a1709064e0b00b00992b6af11f0si561585eju.362.2023.07.17.20.12.46; Mon, 17 Jul 2023 20:12:46 -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=@intel.com header.s=Intel header.b="nW1q3/HG"; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 98C6468BFE7; Tue, 18 Jul 2023 06:12:32 +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 ABE3668C1C5 for ; Tue, 18 Jul 2023 06:12:25 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1689649950; x=1721185950; h=from:to:subject:date:message-id:in-reply-to:references: mime-version:content-transfer-encoding; bh=NC3rAmC9MVIkVzFpoQ6Kfx7Ws2owaU4KANnpDuiUS4U=; b=nW1q3/HGma1IjkbSBbmY8LwATKtOV5shvD9RkgAn3/ebb0+mTDj/p8m8 6fQaWPc+vYTBuaQsDl4tFXpKTMInmYIEIJxf6AbpFVZIBKW8nFaNC3yPQ exZZWbB9C3ShpUlufFZI9tjmBH90R0N/MJZtscs/aUnIuVk/iTWynfwCv dQj4+8skEr36cqPmET8TOoFbHyVGJJ7edzuPYQlBVN11GEad1RGmMX4wY LQ3pqf6NGjAWuEKTCpeZB645UWtqvMbzJ64bnQqVbnjVcFul7cMkT2FkB Cw/gK/iipU6AVNdFRWGDiMN+THgP+Gsq6usGM0cjlpD/rAmjEylZWrDZl g==; X-IronPort-AV: E=McAfee;i="6600,9927,10774"; a="366133480" X-IronPort-AV: E=Sophos;i="6.01,213,1684825200"; d="scan'208";a="366133480" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jul 2023 20:12:21 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10774"; a="758646236" X-IronPort-AV: E=Sophos;i="6.01,213,1684825200"; d="scan'208";a="758646236" Received: from t-dg2.sh.intel.com ([10.238.200.63]) by orsmga001.jf.intel.com with ESMTP; 17 Jul 2023 20:12:20 -0700 From: Fei Wang To: ffmpeg-devel@ffmpeg.org Date: Tue, 18 Jul 2023 11:11:59 +0800 Message-Id: <20230718031203.87803-2-fei.w.wang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230718031203.87803-1-fei.w.wang@intel.com> References: <20230718031203.87803-1-fei.w.wang@intel.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 2/6] lavc/av1: Add common code and unit test for level handling 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: GfZkNXQDmAe1 Signed-off-by: Fei Wang --- libavcodec/Makefile | 4 +- libavcodec/av1_profile_level.c | 91 ++++++++++++++++++++++++ libavcodec/av1_profile_level.h | 58 +++++++++++++++ libavcodec/tests/.gitignore | 1 + libavcodec/tests/av1_levels.c | 124 +++++++++++++++++++++++++++++++++ tests/fate/libavcodec.mak | 5 ++ 6 files changed, 282 insertions(+), 1 deletion(-) create mode 100644 libavcodec/av1_profile_level.c create mode 100644 libavcodec/av1_profile_level.h create mode 100644 libavcodec/tests/av1_levels.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 1b0226c089..3cd5997e64 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -30,6 +30,7 @@ HEADERS = ac3_parser.h \ OBJS = ac3_parser.o \ adts_parser.o \ allcodecs.o \ + av1_profile_level.o \ avcodec.o \ avdct.o \ avpacket.o \ @@ -1308,7 +1309,8 @@ SKIPHEADERS-$(CONFIG_VULKAN) += vulkan.h vulkan_video.h vulkan_decode. SKIPHEADERS-$(CONFIG_V4L2_M2M) += v4l2_buffers.h v4l2_context.h v4l2_m2m.h SKIPHEADERS-$(CONFIG_ZLIB) += zlib_wrapper.h -TESTPROGS = avcodec \ +TESTPROGS = av1_levels \ + avcodec \ avpacket \ bitstream_be \ bitstream_le \ diff --git a/libavcodec/av1_profile_level.c b/libavcodec/av1_profile_level.c new file mode 100644 index 0000000000..5aacf6ddb0 --- /dev/null +++ b/libavcodec/av1_profile_level.c @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2023 Intel Corporation + * + * 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 "libavutil/common.h" +#include "av1_profile_level.h" + +/** ignore entries which named in spec but no details. Like level 2.2 and 7.0. */ +static const AV1LevelDescriptor av1_levels[] = { + // Name MaxVSize MainMbps MaxTiles + // | level_idx | MaxDisplayRate | HighMbps | MaxTileCols + // | | MaxPicSize | | MaxDecodeRate | | MainCR | | + // | | | MaxHSize | | | MaxHeaderRate | | | HighCR| | + // | | | | | | | | | | | | | | + { "2.0", 0, 147456, 2048, 1152, 4423680, 5529600, 150, 1.5, 0, 2, 0, 8, 4 }, + { "2.1", 1, 278784, 2816, 1584, 8363520, 10454400, 150, 3.0, 0, 2, 0, 8, 4 }, + { "3.0", 4, 665856, 4352, 2448, 19975680, 24969600, 150, 6.0, 0, 2, 0, 16, 6 }, + { "3.1", 5, 1065024, 5504, 3096, 31950720, 39938400, 150, 10.0, 0, 2, 0, 16, 6 }, + { "4.0", 8, 2359296, 6144, 3456, 70778880, 77856768, 300, 12.0, 30.0, 4, 4, 32, 8 }, + { "4.1", 9, 2359296, 6144, 3456, 141557760, 155713536, 300, 20.0, 50.0, 4, 4, 32, 8 }, + { "5.0", 12, 8912896, 8192, 4352, 267386880, 273715200, 300, 30.0, 100.0, 6, 4, 64, 8 }, + { "5.1", 13, 8912896, 8192, 4352, 534773760, 547430400, 300, 40.0, 160.0, 8, 4, 64, 8 }, + { "5.2", 14, 8912896, 8192, 4352, 1069547520, 1094860800, 300, 60.0, 240.0, 8, 4, 64, 8 }, + { "5.3", 15, 8912896, 8192, 4352, 1069547520, 1176502272, 300, 60.0, 240.0, 8, 4, 64, 8 }, + { "6.0", 16, 35651584, 16384, 8704, 1069547520, 1176502272, 300, 60.0, 240.0, 8, 4, 128, 16 }, + { "6.1", 17, 35651584, 16384, 8704, 2139095040, 2189721600, 300, 100.0, 480.0, 8, 4, 128, 16 }, + { "6.2", 18, 35651584, 16384, 8704, 4278190080, 4379443200, 300, 160.0, 800.0, 8, 4, 128, 16 }, + { "6.3", 19, 35651584, 16384, 8704, 4278190080, 4706009088, 300, 160.0, 800.0, 8, 4, 128, 16 }, +}; + +const AV1LevelDescriptor *ff_av1_guess_level(int64_t bitrate, + int tier, + int width, + int height, + int tiles, + int tile_cols, + float fps) +{ + int pic_size; + uint64_t display_rate; + float max_br; + + pic_size = width * height; + display_rate = (uint64_t)pic_size * fps; + + for (int i = 0; i < FF_ARRAY_ELEMS(av1_levels); i++) { + const AV1LevelDescriptor *level = &av1_levels[i]; + // Limitation: decode rate, header rate, compress rate, etc. are not considered. + if (pic_size > level->max_pic_size) + continue; + if (width > level->max_h_size) + continue; + if (height > level->max_v_size) + continue; + if (display_rate > level->max_display_rate) + continue; + + if (tier) + max_br = level->high_mbps; + else + max_br = level->main_mbps; + if (!max_br) + continue; + if (bitrate > (int64_t)(1000000.0 * max_br)) + continue; + + if (tiles > level->max_tiles) + continue; + if (tile_cols > level->max_tile_cols) + continue; + return level; + } + + return NULL; +} diff --git a/libavcodec/av1_profile_level.h b/libavcodec/av1_profile_level.h new file mode 100644 index 0000000000..b08dd7383f --- /dev/null +++ b/libavcodec/av1_profile_level.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023 Intel Corporation + * + * 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 + */ + +#ifndef AVCODEC_AV1_PROFILE_LEVEL_H +#define AVCODEC_AV1_PROFILE_LEVEL_H + +#include + +typedef struct AV1LevelDescriptor { + const char *name; + uint8_t level_idx; + + uint32_t max_pic_size; + uint32_t max_h_size; + uint32_t max_v_size; + uint64_t max_display_rate; + uint64_t max_decode_rate; + + uint32_t max_header_rate; + float main_mbps; + float high_mbps; + uint32_t main_cr; + uint32_t high_cr; + uint32_t max_tiles; + uint32_t max_tile_cols; +} AV1LevelDescriptor; + +/** + * Guess the level of a stream from some parameters. + * + * Unknown parameters may be zero, in which case they will be ignored. + */ +const AV1LevelDescriptor *ff_av1_guess_level(int64_t bitrate, + int tier, + int width, + int height, + int tile_rows, + int tile_cols, + float fps); + +#endif /* AVCODEC_AV1_PROFILE_LEVEL_H */ diff --git a/libavcodec/tests/.gitignore b/libavcodec/tests/.gitignore index 2acfc4e804..5e0ccc5838 100644 --- a/libavcodec/tests/.gitignore +++ b/libavcodec/tests/.gitignore @@ -1,3 +1,4 @@ +/av1_levels /avcodec /avfft /avpacket diff --git a/libavcodec/tests/av1_levels.c b/libavcodec/tests/av1_levels.c new file mode 100644 index 0000000000..d91e1cc073 --- /dev/null +++ b/libavcodec/tests/av1_levels.c @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2023 Intel Corporation + * + * 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 "libavutil/common.h" +#include "libavcodec/av1_profile_level.h" + +static const struct { + int width; + int height; + float framerate; + int level_idx; +} test_sizes[] = { + { 426, 240, 30.0, 0 }, + { 640, 360, 30.0, 1 }, + { 854, 480, 30.0, 4 }, + { 1280, 720, 30.0, 5 }, + { 1920, 1080, 30.0, 8 }, + { 1920, 1080, 60.0, 9 }, + { 3840, 2160, 30.0, 12 }, + { 3840, 2160, 60.0, 13 }, + { 3840, 2160, 120.0, 14 }, + { 7680, 4320, 30.0, 16 }, + { 7680, 4320, 60.0, 17 }, + { 7680, 4320, 120.0, 18 }, +}; + +static const struct { + int64_t bitrate; + int tier; + int level_idx; +} test_bitrate[] = { + { 1500000, 0, 0 }, + { 3000000, 0, 1 }, + { 6000000, 0, 4 }, + { 10000000, 0, 5 }, + { 12000000, 0, 8 }, + { 30000000, 1, 8 }, + { 20000000, 0, 9 }, + { 50000000, 1, 9 }, + { 30000000, 0, 12 }, + { 100000000, 1, 12 }, + { 40000000, 0, 13 }, + { 160000000, 1, 13 }, + { 60000000, 0, 14 }, + { 240000000, 1, 14 }, + { 100000000, 0, 17 }, + { 480000000, 1, 17 }, + { 160000000, 0, 18 }, + { 800000000, 1, 18 }, +}; + +static const struct { + int tiles; + int tile_cols; + int level_idx; +} test_tiles[] = { + { 8, 4, 0 }, + { 16, 6, 4 }, + { 32, 8, 8 }, + { 64, 8, 12 }, + { 128, 16, 16 }, +}; + +int main(void) +{ + const AV1LevelDescriptor *level; + int i; + +#define CHECK(expected, format, ...) do { \ + if (level ? (level->level_idx != expected) \ + : !level) { \ + av_log(NULL, AV_LOG_ERROR, "Incorrect level for " \ + format ": expected %d, got %d.\n", __VA_ARGS__, \ + expected, level ? level->level_idx : -1); \ + return 1; \ + } \ + } while (0) + + for (i = 0; i < FF_ARRAY_ELEMS(test_sizes); i++) { + level = ff_av1_guess_level(0, 0, + test_sizes[i].width, + test_sizes[i].height, + 0, 0, test_sizes[i].framerate); + CHECK(test_sizes[i].level_idx, "size %dx%d, framerate %f", + test_sizes[i].width, test_sizes[i].height, test_sizes[i].framerate); + } + + for (i = 0; i < FF_ARRAY_ELEMS(test_bitrate); i++) { + level = ff_av1_guess_level(test_bitrate[i].bitrate, + test_bitrate[i].tier, + 0, 0, 0, 0, 0); + CHECK(test_bitrate[i].level_idx, "bitrate %"PRId64" tier %d", + test_bitrate[i].bitrate, test_bitrate[i].tier); + } + + for (i = 0; i < FF_ARRAY_ELEMS(test_tiles); i++) { + level = ff_av1_guess_level(0, 0, 0, 0, + test_tiles[i].tiles, + test_tiles[i].tile_cols, + 0); + CHECK(test_tiles[i].level_idx, "tiles %d, tile cols %d", + test_tiles[i].tiles, + test_tiles[i].tile_cols); + } + + return 0; +} diff --git a/tests/fate/libavcodec.mak b/tests/fate/libavcodec.mak index 8f56fae3a8..9c4d1b6840 100644 --- a/tests/fate/libavcodec.mak +++ b/tests/fate/libavcodec.mak @@ -1,3 +1,8 @@ +FATE_LIBAVCODEC-yes += fate-av1-levels +fate-av1-levels: libavcodec/tests/av1_levels$(EXESUF) +fate-av1-levels: CMD = run libavcodec/tests/av1_levels$(EXESUF) +fate-av1-levels: REF = /dev/null + FATE_LIBAVCODEC-yes += fate-avpacket fate-avpacket: libavcodec/tests/avpacket$(EXESUF) fate-avpacket: CMD = run libavcodec/tests/avpacket$(EXESUF)