From patchwork Mon Nov 7 01:49:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 39222 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:999a:b0:a4:2148:650a with SMTP id ve26csp1849120pzb; Sun, 6 Nov 2022 17:50:02 -0800 (PST) X-Google-Smtp-Source: AMsMyM50SJZGaBJ9Z81qpkdEo1hKIFeaCnd8hPEWAGIHPA/6EdqvjgFR+XIz9CgoZJPVjDHTaWDA X-Received: by 2002:a17:906:66ce:b0:7ad:d178:c252 with SMTP id k14-20020a17090666ce00b007add178c252mr37610840ejp.158.1667785802041; Sun, 06 Nov 2022 17:50:02 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id hb33-20020a170907162100b0078d027ceb41si7653111ejc.857.2022.11.06.17.50.01; Sun, 06 Nov 2022 17:50:02 -0800 (PST) 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=@outlook.com header.s=selector1 header.b=tTNdcu0H; arc=fail (body hash mismatch); 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=QUARANTINE dis=NONE) header.from=outlook.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DE6E668BA98; Mon, 7 Nov 2022 03:49:58 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR01-VE1-obe.outbound.protection.outlook.com (mail-oln040092066101.outbound.protection.outlook.com [40.92.66.101]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D049068BA05 for ; Mon, 7 Nov 2022 03:49:51 +0200 (EET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Nd2wegddSuJ3U7cZsskj4a+DqS4hKc/EZx/ui7BeYu+CjIn9blh7PT4zjzvb7NYK05VasXP1XWoYGDg3kh7BoCLdf3Gu5vejnMpb4qzFMXS9iTwpl8LnpD+ammOEoF5rLJ6hi+dxI1hw0HjbqmYzWD0LOCAXnulhamunKLf3CknfoP1Fa9O28C87sUVM/layEFLS6Nb46nfn2ZVY81eQQOg5VyysEnz2b0VozSFrr07PSk5eogDH7E9NDaU5xfoC+wJgIvLvAwJeO+LN9KrZMrDJYpfl5jWz12p/bHBpZSffrwxFsygq/gTdfkvnOpGB19Qcn23Yog5hVPFo42x/vQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Xn8iPLiNZ9LgA8BFnDq2BJ3/DDEgFQr2+vEpTWRIL7s=; b=AAIg6fKwPQSqMTUcSXKixUuB/bEzXkQFFdF1UFafQ/ZL5Y0u7ebLp7zM4wQl0veTdUzuvH764IewQ8gcamlONvy1NLacEeY0iv5dqfWwCYeY14zPHsXIhr79EhbwYcJSWxudpgBaTdrWMW0L9vdkTbbJIv/ZrOMXwFUyvgo5VkgWcMHdBmAnzNA7mQPSnlGFMBgM1VIWWdUcFR3DTZeXVhi5jP4DivhwuRLbE9g1KPcrauQPw9AGavSbsXG5hEi0WZq4nBvzW2sVoFmIxtRNWMxhE39gRhhzGN+IDOVFVyMeNWLa4ozemCLj9ErspiIQUQdABrGGFoWW604nX6VPCw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Xn8iPLiNZ9LgA8BFnDq2BJ3/DDEgFQr2+vEpTWRIL7s=; b=tTNdcu0HKLyLa0gR/Amu+bP4ukKfNzdiV9hd/Uw/iXyyoE7RDGzK64teRBSJVrwBDhCXVODQecC1c7ZAmqozCnMFp/WjpCVS8+1ZVKb+Kb9T5VX1m9isK/Lu3cC/H1SNISP3wFMjlO7NiuytSj88mUO6i/gn5d/Kzsky2OG50h4sYW5muKZY06gtcV8PtD9H5/+I4kz+UzLfnHls98OJgh4+T/WjpONCJJ7dblK6TRoXv2wxwb096aNm4IirZUKqTgikFMgUdDMxFsUuLyOp8vA9YdwNvZDwRKVnpeIcHhZYLsdepIBXqK5IIy0FaeNHWAELMi53uigS5uST22Fp7w== Received: from GV1P250MB0737.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:8e::17) by GV2P250MB0923.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:7a::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5791.25; Mon, 7 Nov 2022 01:49:50 +0000 Received: from GV1P250MB0737.EURP250.PROD.OUTLOOK.COM ([fe80::c323:d08c:85d0:dafe]) by GV1P250MB0737.EURP250.PROD.OUTLOOK.COM ([fe80::c323:d08c:85d0:dafe%4]) with mapi id 15.20.5791.026; Mon, 7 Nov 2022 01:49:50 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 7 Nov 2022 02:49:22 +0100 Message-ID: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: X-TMN: [HD1yJvUvWxPE0IpGnLwEOhZvAbD3Ltr+a7bUn8scO0o=] X-ClientProxiedBy: FR0P281CA0009.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:15::14) To GV1P250MB0737.EURP250.PROD.OUTLOOK.COM (2603:10a6:150:8e::17) X-Microsoft-Original-Message-ID: <20221107014922.1503434-4-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: GV1P250MB0737:EE_|GV2P250MB0923:EE_ X-MS-Office365-Filtering-Correlation-Id: 5f8f931a-ac2b-42c1-ce11-08dac0625bbb X-MS-Exchange-SLBlob-MailProps: ZILSnhm0P3lImNjLdz2HENqjqsZBpuhvuFMP0UFAcUUobC4GCXenGkNrQpO8TvSaMCuTQvVbLtkLb1osGuWjEv1Q6FkzB+pJLuJwQtwuE0nnOLXa5EIH+qJEaZGnxKO+ecoKjrRMA7Tzohah+f1wg3ILWTe+F0gEzVgcTUUE0QTNRmwoKx6lcyx7nCl41XipDLNZySxAREkMdkK28o/UYT92vrRezcLUewVcxTXAupl8JxA0rS2T4dCuWDSa8CYRgoUopFGdSmrmWZ+wU8ch8c5Wg/hY3I5ectwSV1dZ9vQuct8BpelIqUvdD34ePO7ZcdoqhOWjZto+K1VihjPPOvjz/E41OdUIFeTv94JuZJFZ6/wFnP1zMBHBtpnuXidwLD6cvdXjzqlxuf1NetRS0R71/2gDKYU0LAu7/LsY9Vj8g7wbr/a1v0jEtqp4Xb+tNG4+etKuLxz1S4JKkYlc3RNf+dgUfmXfX1L2Ott6hvbpQOpgMIjqkgl+b+GrZjydKo0AaM17LzQl75Y4kRXK9s5t9OpdE6oQV731glwYELgVEsGz/EvVaDvHKmzemo3lqh1zzIUhQ5JFLKUeLPnXHaEsJNp7dxKVKKUcTSS3ZmcRw6NP4g9HMnvS8hUZW1TSwoL+yR8sI1ocNCOIgzeul+HIdzhKVc2Eb983jmSPUgl95yq+eqQeLIKWA9j7ksO82Yh+GiqLbEgaICFkW+EfnOr+j08n/ArCPI0KbRz/RB8OWU4DISgmWQJZvoMkI8kpLRjA21vA3jMktuqAPn25GBDuj0Ir9M1R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-CrossTenant-Network-Message-Id: 5f8f931a-ac2b-42c1-ce11-08dac0625bbb X-MS-Exchange-CrossTenant-AuthSource: GV1P250MB0737.EURP250.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Nov 2022 01:49:50.6427 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: GV2P250MB0923 Subject: [FFmpeg-devel] [PATCH 5/5] avcodec/clearvideo: Avoid allocations when decoding tiles 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 2VQ2kTEGVhzn Up until now, the ClearVideo decoder separates parsing tiles and actually using the parsed information: The information is instead stored in structures which are constantly allocated and freed. This commit changes this to use the information immediately, avoiding said allocations. This e.g. reduced the amount of allocations for [1] from 2,866,462 to 24,720. For said sample decoding speed improved by 143%. [1]: https://samples.ffmpeg.org/V-codecs/UCOD/AccordianDance-300.avi Signed-off-by: Andreas Rheinhardt --- libavcodec/clearvideo.c | 99 +++++++++++++---------------------------- 1 file changed, 31 insertions(+), 68 deletions(-) diff --git a/libavcodec/clearvideo.c b/libavcodec/clearvideo.c index d3cbd71f72..e77661d187 100644 --- a/libavcodec/clearvideo.c +++ b/libavcodec/clearvideo.c @@ -59,13 +59,6 @@ typedef struct MVInfo { MV *mv; } MVInfo; -typedef struct TileInfo { - uint16_t flags; - int16_t bias; - MV mv; - struct TileInfo *child[4]; -} TileInfo; - typedef struct CLVContext { AVCodecContext *avctx; IDCTDSPContext idsp; @@ -383,12 +376,16 @@ static int tile_do_block(AVCodecContext *avctx, AVFrame *dst, const AVFrame *src return ret; } -static TileInfo *decode_tile_info(GetBitContext *gb, const LevelCodes *lc) +static int decode_tile(AVCodecContext *avctx, GetBitContext *gb, + const LevelCodes *lc, + AVFrame *dst, const AVFrame *src, + int plane, int x, int y, int size, + MV root_mv, MV *pred) { - TileInfo *ti; int i, flags = 0; int16_t bias = 0; MV mv = { 0 }; + int err; if (lc->flags_cb.table) flags = get_vlc2(gb, lc->flags_cb.table, CLV_VLC_BITS, 2); @@ -403,7 +400,11 @@ static TileInfo *decode_tile_info(GetBitContext *gb, const LevelCodes *lc) mv.x = get_sbits(gb, 8); mv.y = get_sbits(gb, 8); } + if (pred) + mvi_update_prediction(pred, mv); } + mv.x += root_mv.x; + mv.y += root_mv.y; if (lc->bias_cb.table) { uint16_t bias_val = get_vlc2(gb, lc->bias_cb.table, CLV_VLC_BITS, 2); @@ -415,55 +416,29 @@ static TileInfo *decode_tile_info(GetBitContext *gb, const LevelCodes *lc) } } - ti = av_calloc(1, sizeof(*ti)); - if (!ti) - return NULL; - - ti->flags = flags; - ti->mv = mv; - ti->bias = bias; - - if (ti->flags) { - for (i = 0; i < 4; i++) { - if (ti->flags & (1 << i)) { - TileInfo *subti = decode_tile_info(gb, lc + 1); - ti->child[i] = subti; - } - } - } - - return ti; -} - -static int restore_tree(AVCodecContext *avctx, AVFrame *dst, AVFrame *src, - int plane, int x, int y, int size, - TileInfo *tile, MV root_mv) -{ - int ret; - MV mv; - - mv.x = root_mv.x + tile->mv.x; - mv.y = root_mv.y + tile->mv.y; - - if (!tile->flags) { - ret = tile_do_block(avctx, dst, src, plane, x, y, mv.x, mv.y, size, tile->bias); - } else { - int i, hsize = size >> 1; - + if (flags) { + int hsize = size >> 1; for (i = 0; i < 4; i++) { int xoff = (i & 2) == 0 ? 0 : hsize; int yoff = (i & 1) == 0 ? 0 : hsize; - if (tile->child[i]) { - ret = restore_tree(avctx, dst, src, plane, x + xoff, y + yoff, hsize, tile->child[i], root_mv); - av_freep(&tile->child[i]); + if (flags & (1 << i)) { + err = decode_tile(avctx, gb, lc + 1, dst, src, plane, + x + xoff, y + yoff, hsize, root_mv, NULL); } else { - ret = tile_do_block(avctx, dst, src, plane, x + xoff, y + yoff, mv.x, mv.y, hsize, tile->bias); + err = tile_do_block(avctx, dst, src, plane, x + xoff, y + yoff, + mv.x, mv.y, hsize, bias); } + if (err < 0) + return err; } + } else { + err = tile_do_block(avctx, dst, src, plane, x, y, mv.x, mv.y, size, bias); + if (err < 0) + return err; } - return ret; + return 0; } static void extend_edges(AVFrame *buf, int tile_size) @@ -604,38 +579,26 @@ static int clv_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int x = i << c->tile_shift; int y = j << c->tile_shift; int size = 1 << c->tile_shift; - TileInfo *tile; MV cmv; - tile = decode_tile_info(&c->gb, &lev[0]); // Y - if (!tile) - return AVERROR(ENOMEM); - ret = restore_tree(avctx, c->pic, c->prev, 0, x, y, size, tile, mv); + ret = decode_tile(avctx, &c->gb, &lev[0], c->pic, c->prev, // Y + 0, x, y, size, mv, mvp); if (ret < 0) mb_ret = ret; - mvi_update_prediction(mvp, tile->mv); x = i << (c->tile_shift - 1); y = j << (c->tile_shift - 1); size = 1 << (c->tile_shift - 1); - cmv.x = mv.x + tile->mv.x; - cmv.y = mv.y + tile->mv.y; + cmv = *mvp; cmv.x /= 2; cmv.y /= 2; - av_freep(&tile); - tile = decode_tile_info(&c->gb, &lev[4]); // U - if (!tile) - return AVERROR(ENOMEM); - ret = restore_tree(avctx, c->pic, c->prev, 1, x, y, size, tile, cmv); + ret = decode_tile(avctx, &c->gb, &lev[4], c->pic, c->prev, // U + 1, x, y, size, cmv, NULL); if (ret < 0) mb_ret = ret; - av_freep(&tile); - tile = decode_tile_info(&c->gb, &lev[7]); // V - if (!tile) - return AVERROR(ENOMEM); - ret = restore_tree(avctx, c->pic, c->prev, 2, x, y, size, tile, cmv); + ret = decode_tile(avctx, &c->gb, &lev[7], c->pic, c->prev, // U + 2, x, y, size, cmv, NULL); if (ret < 0) mb_ret = ret; - av_freep(&tile); } } mvi_update_row(&c->mvi);