From patchwork Sun May 21 13:03:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nuo Mi X-Patchwork-Id: 41759 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:ba91:b0:105:feb:71f2 with SMTP id fb17csp1090191pzb; Sun, 21 May 2023 06:03:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ45LLXNyuZTq5r7XgQnNAv4HUZzM5WL1xf6Bec2pJzbIve/OSfTA77h8nZoiGU09RmSg2CB X-Received: by 2002:a05:6402:5213:b0:512:7129:1346 with SMTP id s19-20020a056402521300b0051271291346mr4249692edd.16.1684674232036; Sun, 21 May 2023 06:03:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684674232; cv=none; d=google.com; s=arc-20160816; b=ZupBnpWPoMAz6k7x3HjkHcaP9oaS5k9+1y0VH6BBf6sHMImF02EuSuzFwVmEuxJT8w 26iKyCkpBGDdOHOKSRNH47MiZE+Ky1hemO1f8y2ABmLBHfuwIXuJKlCPcs5OptJijH/9 Erukie7+Z04XIkLYybE1lJ9FXhHG2bituhDburikolmzhgIsDof3IasLeQUz2J/aO4kZ a1SOPr9yD1M2udgWlM3fxErlNs3CfdxxXdkIdBrCIS7cipANdxaa2aTQG9cCbjg1y1zF qA7XXlnUze/RmhoAYVjP7UlhFOOw1t2SGNmjHg0IuX15pgVZ3nUC/DQFdb/APVGpO4Hu 0ftw== 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:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=LQa23GN4mdcVDX7iboSDl3G+otQL2CtUH863LLu/7+U=; b=f39lHyARbEotsS36yHVU70uoljxQJv2hLAMHS9UaypkFgv8zlccr9L8QhMSiCzJvTK Qt9WfUxCHMG0nXGATm2yBEXRZvQlMf59jlea67eAOFiHOvVXRxLBj1HmtghV69IMF+5I t2T12vUyymJmjEKy/0e3LSF62IziVZcTMADtasU3kG6v9pkIZeAMVuIyr9fvn7FapOoY XHJDufaiW42l9xgwuwDYQ3D0Efv1fF/qT9n6NZ5uRZ2l+j1d4TsMQ71v2HfbL27p8vUB gbEoCVH1qq4wxiiHCHGavg6oPSix2EOkn4qmFGoA2O16Oqf3gpHcNO4ZjfNdjQZAzwRa F7GQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20221208 header.b=JGG2eNuk; 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=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id i21-20020a056402055500b005075e52cd9csi2728724edx.47.2023.05.21.06.03.51; Sun, 21 May 2023 06:03: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=@gmail.com header.s=20221208 header.b=JGG2eNuk; 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=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 50D1968C025; Sun, 21 May 2023 16:03:36 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 765E768B577 for ; Sun, 21 May 2023 16:03:29 +0300 (EEST) Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-64d30ab1f89so1675364b3a.3 for ; Sun, 21 May 2023 06:03:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684674207; x=1687266207; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Vb4ac+NHLyHlASD9OyUnjW/HSwQ4BYh1F7RoumtC6A8=; b=JGG2eNuktRGx15X2ivk10caZi3rAk2Y8OGwYRb8iElVUOLp9adcSNxEcwSb8qx0JXh 5Hkdtlw5OOrvldaCmtOKImbY8qw4KNVkx8RjlXllUdqQz3PI51MTZAVkeYWyOCCFPK1P bkuk5O7QoZppPyRFKKuLLSFIBl8ZNMNoMM5nWMMfDlHWPJelT7AuLyXRt2OMH+yGBPXO Fvi8M5Apt/JgNKUXiQj9CnmCfD1vvkUTIHsDW3V2LssLnUnvzNqIWhEDbA2KgTxcB9wV 6pPtEnNVXgIPsO1a+6a8gtacf01tYnXHooshRKCaTJhpxv/ZTsHv6T1k5AA+gd5tUbZS zK9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684674207; x=1687266207; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Vb4ac+NHLyHlASD9OyUnjW/HSwQ4BYh1F7RoumtC6A8=; b=IZ/MdiIwpT6Scxx4gfM7juSShHE7Vy8OJCKl1V9IMVTXKx6blbcJy6LxrqH7buVwr9 ICxrBCzjkSZg/FGZmY1QJIJxfTmedDwsToUwn04g5nEPFT6asEY8J1t5D/TCQ1Ng2byO mLyiDeMrrSu9aG6mw+BhwJ3BQxSDjohXd95sEOMrSzy15PYLa8CpjrkZgjzObd8GKI6I dQhlS6J6mxyVgJNxOHHBEDnYylSfNtob5ba+PrAL3eFOnkQrqQIArGYRbwcnHlFta1tr SFTiveHaJ9/inkvCZ3AFkAp7/pELoQmhJpK0P07OeQSQ1XF8Vfbelc0RETptiQh0wMg7 k42w== X-Gm-Message-State: AC+VfDyKcYebFSmhBHuOPSlGlwmf6rTpg9DfNO9QtqzGxObh8fjXY6zD AdQrci+0E60L7CkYCL3328iUkk8SK/o4Iw== X-Received: by 2002:a05:6a00:15c7:b0:626:2ce1:263c with SMTP id o7-20020a056a0015c700b006262ce1263cmr10503874pfu.5.1684674207203; Sun, 21 May 2023 06:03:27 -0700 (PDT) Received: from NuoMi.localdomain ([112.64.8.97]) by smtp.gmail.com with ESMTPSA id t9-20020aa79389000000b0064d45eea573sm1671872pfe.41.2023.05.21.06.03.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 May 2023 06:03:26 -0700 (PDT) From: Nuo Mi To: ffmpeg-devel@ffmpeg.org Date: Sun, 21 May 2023 21:03:06 +0800 Message-Id: <20230521130319.13813-2-nuomi2021@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230521130319.13813-1-nuomi2021@gmail.com> References: <20230521130319.13813-1-nuomi2021@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 01/14] vvcdec: add thread executor 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: Nuo Mi Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: pE12tPNiizof The executor design pattern was inroduced by java it also adapted by python Compared to handcrafted thread pool management, it greatly simplifies the thread code. --- libavcodec/Makefile | 1 + libavcodec/vvc/Makefile | 4 + libavcodec/vvc/vvc_executor.c | 193 ++++++++++++++++++++++++++++++++++ libavcodec/vvc/vvc_executor.h | 73 +++++++++++++ 4 files changed, 271 insertions(+) create mode 100644 libavcodec/vvc/Makefile create mode 100644 libavcodec/vvc/vvc_executor.c create mode 100644 libavcodec/vvc/vvc_executor.h diff --git a/libavcodec/Makefile b/libavcodec/Makefile index dab09f483a..b1fcbf71b2 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -62,6 +62,7 @@ OBJS = ac3_parser.o \ xiph.o \ # subsystems +include $(SRC_PATH)/libavcodec/vvc/Makefile OBJS-$(CONFIG_AANDCTTABLES) += aandcttab.o OBJS-$(CONFIG_AC3DSP) += ac3dsp.o ac3.o ac3tab.o OBJS-$(CONFIG_ADTS_HEADER) += adts_header.o mpeg4audio_sample_rates.o diff --git a/libavcodec/vvc/Makefile b/libavcodec/vvc/Makefile new file mode 100644 index 0000000000..c4b93e0389 --- /dev/null +++ b/libavcodec/vvc/Makefile @@ -0,0 +1,4 @@ +clean:: + $(RM) $(CLEANSUFFIXES:%=libavcodec/vvc/%) + +OBJS-$(CONFIG_VVC_DECODER) += vvc/vvc_executor.o diff --git a/libavcodec/vvc/vvc_executor.c b/libavcodec/vvc/vvc_executor.c new file mode 100644 index 0000000000..f2afdf79ae --- /dev/null +++ b/libavcodec/vvc/vvc_executor.c @@ -0,0 +1,193 @@ +/* + * VVC video Decoder + * + * Copyright (C) 2022 Nuo Mi + * + * 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/avutil.h" +#include "libavutil/thread.h" + +#include "vvc_executor.h" + +typedef struct ThreadInfo { + int idx; + VVCExecutor *e; + pthread_t thread; +} ThreadInfo; + +struct VVCExecutor { + VVCTaskCallbacks cb; + ThreadInfo *threads; + uint8_t *local_contexts; + int thread_count; + + pthread_mutex_t lock; + pthread_cond_t cond; + int die; + VVCTasklet *tasks; +}; + +static void remove_task(VVCTasklet **prev, VVCTasklet *t) +{ + *prev = t->next; + t->next = NULL; +} + +static void add_task(VVCTasklet **prev, VVCTasklet *t) +{ + t->next = *prev; + *prev = t; +} + +static void *executor_worker_task(void *data) +{ + ThreadInfo *ti = (ThreadInfo*)data; + VVCExecutor *e = ti->e; + void *lc = e->local_contexts + ti->idx * e->cb.local_context_size; + VVCTasklet **prev; + VVCTaskCallbacks *cb = &e->cb; + + pthread_mutex_lock(&e->lock); + while (1) { + VVCTasklet* t = NULL; + if (e->die) break; + + for (prev = &e->tasks; *prev; prev = &(*prev)->next) { + if (cb->ready(*prev, cb->user_data)) { + t = *prev; + break; + } + } + if (t) { + //found one task + remove_task(prev, t); + pthread_mutex_unlock(&e->lock); + cb->run(t, lc, cb->user_data); + pthread_mutex_lock(&e->lock); + } else { + //no task in one loop + pthread_cond_wait(&e->cond, &e->lock); + } + } + pthread_mutex_unlock(&e->lock); + return NULL; +} + +VVCExecutor* ff_vvc_executor_alloc(const VVCTaskCallbacks *cb, int thread_count) +{ + VVCExecutor *e; + int i, j, ret; + if (!cb || !cb->user_data || !cb->ready || !cb->run || !cb->priority_higher) + return NULL; + e = av_calloc(1, sizeof(*e)); + if (!e) + return NULL; + e->cb = *cb; + + e->local_contexts = av_malloc(thread_count * e->cb.local_context_size); + if (!e->local_contexts) + goto free_executor; + + e->threads = av_calloc(thread_count, sizeof(*e->threads)); + if (!e->threads) + goto free_contexts; + for (i = 0; i < thread_count; i++) { + ThreadInfo *ti = e->threads + i; + ti->e = e; + ti->idx = i; + } + + ret = pthread_mutex_init(&e->lock, NULL); + if (ret) + goto free_threads; + + ret = pthread_cond_init(&e->cond, NULL); + if (ret) + goto destroy_lock; + + for (i = 0; i < thread_count; i++) { + ThreadInfo *ti = e->threads + i; + ret = pthread_create(&ti->thread, NULL, executor_worker_task, ti); + if (ret) + goto join_threads; + } + e->thread_count = thread_count; + return e; + +join_threads: + pthread_mutex_lock(&e->lock); + e->die = 1; + pthread_cond_broadcast(&e->cond); + pthread_mutex_unlock(&e->lock); + for (j = 0; j < i; j++) + pthread_join(e->threads[j].thread, NULL); + pthread_cond_destroy(&e->cond); +destroy_lock: + pthread_mutex_destroy(&e->lock); +free_threads: + av_free(e->threads); +free_contexts: + av_free(e->local_contexts); +free_executor: + free(e); + return NULL; +} + +void ff_vvc_executor_free(VVCExecutor **executor) +{ + VVCExecutor *e; + if (!executor || !*executor) + return; + e = *executor; + + //singal die + pthread_mutex_lock(&e->lock); + e->die = 1; + pthread_cond_broadcast(&e->cond); + pthread_mutex_unlock(&e->lock); + + for (int i = 0; i < e->thread_count; i++) + pthread_join(e->threads[i].thread, NULL); + pthread_cond_destroy(&e->cond); + pthread_mutex_destroy(&e->lock); + + av_free(e->threads); + av_free(e->local_contexts); + + av_freep(executor); +} + +void ff_vvc_executor_execute(VVCExecutor *e, VVCTasklet *t) +{ + VVCTaskCallbacks *cb = &e->cb; + VVCTasklet **prev; + + pthread_mutex_lock(&e->lock); + for (prev = &e->tasks; *prev && cb->priority_higher(*prev, t); prev = &(*prev)->next) + /* nothing */; + add_task(prev, t); + pthread_cond_signal(&e->cond); + pthread_mutex_unlock(&e->lock); +} + +void ff_vvc_executor_wakeup(VVCExecutor *e) +{ + pthread_mutex_lock(&e->lock); + pthread_cond_broadcast(&e->cond); + pthread_mutex_unlock(&e->lock); +} diff --git a/libavcodec/vvc/vvc_executor.h b/libavcodec/vvc/vvc_executor.h new file mode 100644 index 0000000000..79c00d58ca --- /dev/null +++ b/libavcodec/vvc/vvc_executor.h @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2022 Nuo Mi + * + * 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_VVC_EXECUTOR_H +#define AVCODEC_VVC_EXECUTOR_H + +typedef struct VVCExecutor VVCExecutor; +typedef struct VVCTasklet VVCTasklet; + +struct VVCTasklet { + VVCTasklet *next; +}; + +typedef struct VVCTaskCallbacks { + void *user_data; + + int local_context_size; + + // return 1 if a's priority > b's priority + int (*priority_higher)(const VVCTasklet *a, const VVCTasklet *b); + + // task is ready for run + int (*ready)(const VVCTasklet *t, void *user_data); + + // run the task + int (*run)(VVCTasklet *t, void *local_context, void *user_data); +} VVCTaskCallbacks; + +/** + * Alloc executor + * @param callbacks callback strucutre for executor + * @param thread_count worker thread number + * @return return the executor + */ +VVCExecutor* ff_vvc_executor_alloc(const VVCTaskCallbacks *callbacks, int thread_count); + +/** + * Free executor + * @param e pointer to executor + */ +void ff_vvc_executor_free(VVCExecutor **e); + +/** + * Add task to executor + * @param e pointer to executor + * @param t pointer to task + */ +void ff_vvc_executor_execute(VVCExecutor *e, VVCTasklet *t); + +/** + * Wakeup all threads + * @param e pointer to executor + */ +void ff_vvc_executor_wakeup(VVCExecutor *e); + +#endif //AVCODEC_VVC_EXECUTOR_H