[FFmpeg-devel] [PATCH v2 01/14] vvcdec: add thread executor

Michael Niedermayer michael at niedermayer.cc
Sat Jul 15 01:38:31 EEST 2023


On Fri, Jul 07, 2023 at 10:05:27PM +0800, Nuo Mi wrote:
> The executor design pattern was inroduced by java
> <https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/util/concurrent/Executor.html>
> it also adapted by python
> <https://docs.python.org/3/library/concurrent.futures.html>
> Compared to handcrafted thread pool management, it greatly simplifies the thread code.
> ---
>  libavcodec/Makefile     |   1 +
>  libavcodec/executor.c   | 182 ++++++++++++++++++++++++++++++++++++++++
>  libavcodec/executor.h   |  67 +++++++++++++++
>  libavcodec/vvc/Makefile |   4 +
>  4 files changed, 254 insertions(+)
>  create mode 100644 libavcodec/executor.c
>  create mode 100644 libavcodec/executor.h
>  create mode 100644 libavcodec/vvc/Makefile

[...]

> +++ b/libavcodec/executor.h
> @@ -0,0 +1,67 @@
> +/*
> + * 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_EXECUTOR_H
> +#define AVCODEC_EXECUTOR_H
> +
> +typedef struct Executor Executor;
> +typedef struct Tasklet Tasklet;
> +
> +struct Tasklet {
> +    Tasklet *next;
> +};
> +
> +typedef struct TaskletCallbacks {
> +    void *user_data;
> +
> +    int local_context_size;
> +
> +    // return 1 if a's priority > b's priority
> +    int (*priority_higher)(const Tasklet *a, const Tasklet *b);
> +
> +    // task is ready for run
> +    int (*ready)(const Tasklet *t, void *user_data);
> +
> +    // run the task
> +    int (*run)(Tasklet *t, void *local_context, void *user_data);
> +} TaskletCallbacks;
> +
> +/**
> + * Alloc executor
> + * @param callbacks callback strucutre for executor
> + * @param thread_count worker thread number
> + * @return return the executor
> + */
> +Executor* ff_executor_alloc(const TaskletCallbacks *callbacks, int thread_count);
> +
> +/**
> + * Free executor
> + * @param e  pointer to executor
> + */
> +void ff_executor_free(Executor **e);
> +
> +/**
> + * Add task to executor
> + * @param e pointer to executor
> + * @param t pointer to task. If NULL, it will wakeup one work thread
> + */
> +void ff_executor_execute(Executor *e, Tasklet *t);
> +
> +#endif //AVCODEC_EXECUTOR_H

This would be quite useful outside libavcodec
In fact id like to use it in libavradio to do the first stage of FFTs (which
are the overwhelming bulk of computations ATM) and maybe eventually later
stages too

Maybe this could be made available to the outside of libavcodec
as avpriv_ or av_ ?
maybe from libavutil ?

thx

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Take away the freedom of one citizen and you will be jailed, take away
the freedom of all citizens and you will be congratulated by your peers
in Parliament.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 195 bytes
Desc: not available
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20230715/31c96859/attachment.sig>


More information about the ffmpeg-devel mailing list