From patchwork Fri Sep 17 02:08:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 30296 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2a4a:0:0:0:0 with SMTP id k10csp1785015iov; Thu, 16 Sep 2021 19:09:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxuLKOTZl8f2YDi9ltLCzmq/abo8AW+nFzNP5HMFIvNre5F7bH6DQEx+4ZwqaT6cNaE81D/ X-Received: by 2002:a17:906:2cd6:: with SMTP id r22mr9464794ejr.398.1631844592913; Thu, 16 Sep 2021 19:09:52 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id b25si5204132edr.544.2021.09.16.19.09.52; Thu, 16 Sep 2021 19:09:52 -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=@outlook.com header.s=selector1 header.b=LTkFB7yD; 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 0F41868B0FD; Fri, 17 Sep 2021 05:08:42 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR04-VI1-obe.outbound.protection.outlook.com (mail-oln040092075092.outbound.protection.outlook.com [40.92.75.92]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6185F68B0FD for ; Fri, 17 Sep 2021 05:08:33 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=n9uZRlLAax++Rvfd6WNIxWIhd0jPM1/Bkel/sBCzG2Yz2QCgX9MYtVhsXsLEUa0iGiA8tdRA0oSYqXHzERxChwVJTpThUKZWpWtftaXXliU3Cmt2xR6C3NqhfbDIId38SwkPW3bdsEDAtyY9zyQOvYwEOXNqsiXilv23mu0zjB73qW7++qU5rsMy0E62GVv4yvUjWm6fxoUR0ghu3zsAg7FsVPLmFSLGQIYLNNFVNZqBnf/wZr3EuIh10GY7X/VrUIZL17cHYVQVbOJbr8DKtthFJx20qHMqeaaBH1fByBfj8EmL5V4LQ60US/zNMu+gSNzqeBgtxe75lbF6sgyc2Q== 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; bh=A8fbGaEu0sYkgrZ6zgl2Ux1brv5+gXE1Xgc5oyImnvU=; b=OvY7OgolqRGyO1CqDh8feJA/MHDq5PattItx0P+/eXRz8h0ujSeswgyEgsfHF3DuC5sGGmAXh9BjCjJv48XGOp5X/1/iyCtaVAFMcKPwipZcGbRfzgn7kgG1frKEoXUFq8cRvaKJfnTK+BvIxzFTXr7LusLgC1xw6AJxEt8i4dmXQgZu/xABLEgs9QBkdEXrs34easdoRwaP7gfaJY7TKq+eI2wfGGDfWiIltHSLnXh0aZQfab+QS67LC/k6DQm6ziYtVnb2w4rGsbdRqpgp3T5Pr2r4KvLRjie8f+Gj7AA/fXE3xHLTySdjZzir3+VitNkSww19N74IEiY+F8wE+Q== 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=A8fbGaEu0sYkgrZ6zgl2Ux1brv5+gXE1Xgc5oyImnvU=; b=LTkFB7yD+8u7bpi1dADvQEvgeJzQsuuMZUV7YSshVe8bnUwe+lPzirH8DsQh7ftBlDYmAGGSLPy4y9pZTbJ2z7bB5ZZNph0YgsFi5MMIjyOZrHvRu03JAvzdursOW6eylGKDzRiuNagSF5p5v8N4ZIeDPmYTFDtQduS+9K8CcuXSP5uFzdimrBUPqY3luJH/b7DTA+gta6OUB7ENswji2a+Uq8XOoDZKn/Ug3tO/Qqf7hB8hixoJ94aBubZiW0K7ls+Y7xd+mYprHVhzLn5eDxr/Sg806+npZ2d2CG6TIhWFOpQyUSobooNPRSgr5GGVCfluGDKtu1TcVcpjAiXqcQ== Received: from AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) by AM6PR03MB5297.eurprd03.prod.outlook.com (2603:10a6:20b:c5::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4523.16; Fri, 17 Sep 2021 02:08:26 +0000 Received: from AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::787b:2156:ca99:fe00]) by AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::787b:2156:ca99:fe00%3]) with mapi id 15.20.4523.016; Fri, 17 Sep 2021 02:08:26 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 17 Sep 2021 04:08:02 +0200 Message-ID: X-Mailer: git-send-email 2.30.2 In-Reply-To: References: X-TMN: [qCyJ2kVQIvJOM+QgY15l0VMk17reudCC] X-ClientProxiedBy: AM8P191CA0018.EURP191.PROD.OUTLOOK.COM (2603:10a6:20b:21a::23) To AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) X-Microsoft-Original-Message-ID: <20210917020808.275498-6-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sblaptop.fritz.box (188.192.142.38) by AM8P191CA0018.EURP191.PROD.OUTLOOK.COM (2603:10a6:20b:21a::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4523.14 via Frontend Transport; Fri, 17 Sep 2021 02:08:25 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: ae422440-7bf8-47b1-4752-08d9798008bc X-MS-Exchange-SLBlob-MailProps: S/btQ8cKWiQB5KKqMro6wr3957+0yV3UpxF2hECgJwCH5+NMXoVFW0wTUPdwcyiZv/4r5D4v+4C1vWch2rdiLA6VNlZ/1j/hklQzfCo4jVf/k+3p6kh8rqomrWOEZ6tCbwBsl3Es9R9sykX+wpjlWRSILWYbcDImggP0p1pcLrnK0JxQ82auCX8JIFpMV6ab+UGsbVTxNBqdLanxaVfGRAeKeHrr71PpHTbrUCzWldKqEf56+xYXyUX/13crdMNJ7frvk4z2Xay6gs8m41JEc8/k2xJMGoeliB+X6SeHpLAFWCDZNxLHzySeXyITW9LMK2rrApoS8xuX6BF5Gj2gNw/fSJL6bxM02O+yxSMRwvQTS87DdsBxAezWFbA8WZhQJBulNpbLVZEzZ3yOA1d2XtDW/3m8DKa1UlrCTG+2iOdwJyXdPOf7IWd8dSvApJuG9Egz8XbgCXPnzA1s0480HR++CcezGk3Nq6VUSWXPNdzGv0PsCxSHa9aYhiKz7taLTEWmhx82wv3l5mHvtzrCk8MpBvtuELP0FOzze6OMqwplol5BE/pGz9dtGIA7PxX5aM9g2hA03t/2Fna7w+uXSkYTf3EdbM3A12GrpqEstSrzA7btJlOJA9f8fV2GiuPr4awa5iQNgt0RXthgSkeaPz9DlWOiz9cFZlRoyI5AvzPRxte9o6iJJZrhtj2rZYTYD6vsuu6g9+bf3WAKrweWPA6A3vB2K+zZkchGoNNwmVnsPPDXbnZyQ95gt4ehS1co7LAnEqhV/qc= X-MS-TrafficTypeDiagnostic: AM6PR03MB5297: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Vhq4pTk43Ik377HOd0rd87GFHGtM76AAkNxAY9CblYIwyFsn0aymL1amFlz6h9PyyKliQgNjOVKNJ5Wfv7ytXT+9PEVz0dwUoU6Nt2A7j9BmOaympqcuzqOnC5rg+3loUiOoepxQnPmfs1mBvQqBGhWn2h1bSk8wyMErqS9hNIyAOtjAg7i56knPX6eOHRAAXXpGPQ7sonPrYowGKd6cFq4JnDKQP+hLfOv63MZI01EljyU+5P95lCbzq+mRETqr19WZCf2D0okbMoKsRpvgqgoFCBYsygJIN5QttFopB+Hw2v941wY4RAiOSurl4/J/8rU5b4/MQbAXyvF8YjT+m17yLycIajfXyj9CW1dj580K1f8CuHVcurH0duSGJnjfICWkBqksM7wAA/yPRt6LRWa8b7vQPN5TDnwyN7OMGqIjGz7EB/FkagQwWy4OVGqs X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Mi5R7f77rIwmbSyftUM5mnVwI+jPaudrjzwoiIcSziNdJNJQ1GWbo1thSNTZidXU67kypdABn2BkhjZ5G+4Gjc2TBFz+qTwbRXgf05jyfhaI/nHE3S+3np/fvQx7Z4LzjdSpB/+1uxQ6ojOUzkk8tA== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: ae422440-7bf8-47b1-4752-08d9798008bc X-MS-Exchange-CrossTenant-AuthSource: AM7PR03MB6660.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Sep 2021 02:08:26.0779 (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: AM6PR03MB5297 Subject: [FFmpeg-devel] [PATCH 07/13] avcodec/elbg: Move arguments to the context early if possible 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: lNghaUnq6pHv This affects all the arguments that don't change during a call to avpriv_elbg_do(); doing so makes it easily recognizable which arguments change upon recursive calls. Signed-off-by: Andreas Rheinhardt --- libavcodec/elbg.c | 73 +++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 38 deletions(-) diff --git a/libavcodec/elbg.c b/libavcodec/elbg.c index 9eac802688..24c6f06f54 100644 --- a/libavcodec/elbg.c +++ b/libavcodec/elbg.c @@ -332,30 +332,22 @@ static void do_shiftings(ELBGContext *elbg) } } -static int do_elbg(int *points, int dim, int numpoints, int *codebook, - int num_cb, int max_steps, int *closest_cb, - AVLFG *rand_state) +static int do_elbg(ELBGContext *elbg, int *points, int numpoints, + int max_steps) { - int dist; - ELBGContext elbg_d; - ELBGContext *elbg = &elbg_d; int i, j, steps = 0, ret = 0; - int *size_part = av_malloc_array(num_cb, sizeof(int)); + int *size_part = av_malloc_array(elbg->num_cb, sizeof(int)); cell *list_buffer = av_malloc_array(numpoints, sizeof(cell)); cell *free_cells; - int best_dist, best_idx = 0; + int best_idx = 0; int64_t last_error; elbg->error = INT64_MAX; - elbg->dim = dim; - elbg->num_cb = num_cb; - elbg->codebook = codebook; - elbg->cells = av_malloc_array(num_cb, sizeof(cell *)); - elbg->utility = av_malloc_array(num_cb, sizeof(*elbg->utility)); - elbg->nearest_cb = closest_cb; + elbg->cells = av_malloc_array(elbg->num_cb, sizeof(cell *)); + elbg->utility = av_malloc_array(elbg->num_cb, sizeof(*elbg->utility)); elbg->points = points; - elbg->utility_inc = av_malloc_array(num_cb, sizeof(*elbg->utility_inc)); - elbg->scratchbuf = av_malloc_array(5*dim, sizeof(int)); + elbg->utility_inc = av_malloc_array(elbg->num_cb, sizeof(*elbg->utility_inc)); + elbg->scratchbuf = av_malloc_array(5 * elbg->dim, sizeof(int)); if (!size_part || !list_buffer || !elbg->cells || !elbg->utility || !elbg->utility_inc || !elbg->scratchbuf) { @@ -363,23 +355,26 @@ static int do_elbg(int *points, int dim, int numpoints, int *codebook, goto out; } - elbg->rand_state = rand_state; do { free_cells = list_buffer; last_error = elbg->error; steps++; - memset(elbg->utility, 0, num_cb * sizeof(*elbg->utility)); - memset(elbg->cells, 0, num_cb * sizeof(*elbg->cells)); + memset(elbg->utility, 0, elbg->num_cb * sizeof(*elbg->utility)); + memset(elbg->cells, 0, elbg->num_cb * sizeof(*elbg->cells)); elbg->error = 0; /* This loop evaluate the actual Voronoi partition. It is the most costly part of the algorithm. */ for (i=0; i < numpoints; i++) { - best_dist = distance_limited(elbg->points + i*elbg->dim, elbg->codebook + best_idx*elbg->dim, dim, INT_MAX); + int best_dist = distance_limited(elbg->points + i * elbg->dim, + elbg->codebook + best_idx * elbg->dim, + elbg->dim, INT_MAX); for (int k = 0; k < elbg->num_cb; k++) { - dist = distance_limited(elbg->points + i*elbg->dim, elbg->codebook + k*elbg->dim, dim, best_dist); + int dist = distance_limited(elbg->points + i * elbg->dim, + elbg->codebook + k * elbg->dim, + elbg->dim, best_dist); if (dist < best_dist) { best_dist = dist; best_idx = k; @@ -396,9 +391,9 @@ static int do_elbg(int *points, int dim, int numpoints, int *codebook, do_shiftings(elbg); - memset(size_part, 0, num_cb * sizeof(*size_part)); + memset(size_part, 0, elbg->num_cb * sizeof(*size_part)); - memset(elbg->codebook, 0, elbg->num_cb * dim * sizeof(*elbg->codebook)); + memset(elbg->codebook, 0, elbg->num_cb * elbg->dim * sizeof(*elbg->codebook)); for (i=0; i < numpoints; i++) { size_part[elbg->nearest_cb[i]]++; @@ -433,13 +428,13 @@ out: * points. * @return < 0 in case of error, 0 otherwise */ -static int init_elbg(int *points, int dim, int numpoints, int *codebook, - int num_cb, int max_steps, int *closest_cb, - AVLFG *rand_state) +static int init_elbg(ELBGContext *elbg, int *points, int numpoints, + int max_steps) { + int dim = elbg->dim; int ret = 0; - if (numpoints > 24LL * num_cb) { + if (numpoints > 24LL * elbg->num_cb) { /* ELBG is very costly for a big number of points. So if we have a lot of them, get a good initial codebook to save on iterations */ int *temp_points = av_malloc_array(dim, (numpoints/8)*sizeof(*temp_points)); @@ -450,19 +445,17 @@ static int init_elbg(int *points, int dim, int numpoints, int *codebook, memcpy(temp_points + i*dim, points + k*dim, dim * sizeof(*temp_points)); } - ret = init_elbg(temp_points, dim, numpoints / 8, codebook, - num_cb, 2 * max_steps, closest_cb, rand_state); + ret = init_elbg(elbg, temp_points, numpoints / 8, 2 * max_steps); if (ret < 0) { av_freep(&temp_points); return ret; } - ret = do_elbg (temp_points, dim, numpoints / 8, codebook, - num_cb, 2 * max_steps, closest_cb, rand_state); + ret = do_elbg(elbg, temp_points, numpoints / 8, 2 * max_steps); av_free(temp_points); } else // If not, initialize the codebook with random positions - for (int i = 0; i < num_cb; i++) - memcpy(codebook + i * dim, points + ((i*BIG_PRIME)%numpoints)*dim, - dim * sizeof(*codebook)); + for (int i = 0; i < elbg->num_cb; i++) + memcpy(elbg->codebook + i * dim, points + ((i*BIG_PRIME)%numpoints)*dim, + dim * sizeof(*elbg->codebook)); return ret; } @@ -477,12 +470,16 @@ int avpriv_elbg_do(ELBGContext **elbgp, int *points, int dim, int numpoints, return AVERROR(ENOMEM); *elbgp = elbg; - ret = init_elbg(points, dim, numpoints, codebook, - num_cb, max_steps, closest_cb, rand_state); + elbg->nearest_cb = closest_cb; + elbg->rand_state = rand_state; + elbg->codebook = codebook; + elbg->num_cb = num_cb; + elbg->dim = dim; + + ret = init_elbg(elbg, points, numpoints, max_steps); if (ret < 0) return ret; - return do_elbg (points, dim, numpoints, codebook, - num_cb, max_steps, closest_cb, rand_state); + return do_elbg (elbg, points, numpoints, max_steps); } av_cold void avpriv_elbg_free(ELBGContext **elbgp)