2013-05-11 21:41:46 +03:00
|
|
|
/*
|
2013-05-24 14:47:45 +03:00
|
|
|
* This file is part of FFmpeg.
|
2013-05-11 21:41:46 +03:00
|
|
|
*
|
2013-05-24 14:47:45 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2013-05-11 21:41:46 +03:00
|
|
|
* 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.
|
|
|
|
*
|
2013-05-24 14:47:45 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2013-05-11 21:41:46 +03:00
|
|
|
* 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
|
2013-05-24 14:47:45 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2013-05-11 21:41:46 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Libavfilter multithreading support
|
|
|
|
*/
|
|
|
|
|
2021-08-01 08:36:09 +02:00
|
|
|
#include <stddef.h>
|
2013-05-11 21:41:46 +03:00
|
|
|
|
2021-08-01 08:36:09 +02:00
|
|
|
#include "libavutil/error.h"
|
|
|
|
#include "libavutil/macros.h"
|
2013-05-11 21:41:46 +03:00
|
|
|
#include "libavutil/mem.h"
|
2017-07-12 02:16:33 +02:00
|
|
|
#include "libavutil/slicethread.h"
|
2013-05-11 21:41:46 +03:00
|
|
|
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "thread.h"
|
|
|
|
|
|
|
|
typedef struct ThreadContext {
|
|
|
|
AVFilterGraph *graph;
|
2017-07-12 02:16:33 +02:00
|
|
|
AVSliceThread *thread;
|
2013-08-08 17:28:41 +03:00
|
|
|
avfilter_action_func *func;
|
2013-05-11 21:41:46 +03:00
|
|
|
|
2016-01-24 15:41:00 +02:00
|
|
|
/* per-execute parameters */
|
2013-05-11 21:41:46 +03:00
|
|
|
AVFilterContext *ctx;
|
|
|
|
void *arg;
|
|
|
|
int *rets;
|
|
|
|
} ThreadContext;
|
|
|
|
|
2017-07-12 02:16:33 +02:00
|
|
|
static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
|
2013-05-11 21:41:46 +03:00
|
|
|
{
|
2017-07-12 02:16:33 +02:00
|
|
|
ThreadContext *c = priv;
|
|
|
|
int ret = c->func(c->ctx, c->arg, jobnr, nb_jobs);
|
|
|
|
if (c->rets)
|
|
|
|
c->rets[jobnr] = ret;
|
2013-05-11 21:41:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void slice_thread_uninit(ThreadContext *c)
|
|
|
|
{
|
2017-07-12 02:16:33 +02:00
|
|
|
avpriv_slicethread_free(&c->thread);
|
2013-05-11 21:41:46 +03:00
|
|
|
}
|
|
|
|
|
2013-08-08 17:28:41 +03:00
|
|
|
static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func,
|
2013-05-11 21:41:46 +03:00
|
|
|
void *arg, int *ret, int nb_jobs)
|
|
|
|
{
|
|
|
|
ThreadContext *c = ctx->graph->internal->thread;
|
|
|
|
|
|
|
|
if (nb_jobs <= 0)
|
|
|
|
return 0;
|
|
|
|
c->ctx = ctx;
|
|
|
|
c->arg = arg;
|
|
|
|
c->func = func;
|
2017-03-28 18:00:02 +02:00
|
|
|
c->rets = ret;
|
2013-05-11 21:41:46 +03:00
|
|
|
|
2017-07-12 02:16:33 +02:00
|
|
|
avpriv_slicethread_execute(c->thread, nb_jobs, 0);
|
2013-05-11 21:41:46 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-16 18:31:23 +03:00
|
|
|
static int thread_init_internal(ThreadContext *c, int nb_threads)
|
2013-05-11 21:41:46 +03:00
|
|
|
{
|
2017-07-12 02:16:33 +02:00
|
|
|
nb_threads = avpriv_slicethread_create(&c->thread, c, worker_func, NULL, nb_threads);
|
2013-05-11 21:41:46 +03:00
|
|
|
if (nb_threads <= 1)
|
2017-07-12 02:16:33 +02:00
|
|
|
avpriv_slicethread_free(&c->thread);
|
|
|
|
return FFMAX(nb_threads, 1);
|
2013-05-11 21:41:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int ff_graph_thread_init(AVFilterGraph *graph)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (graph->nb_threads == 1) {
|
|
|
|
graph->thread_type = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
graph->internal->thread = av_mallocz(sizeof(ThreadContext));
|
|
|
|
if (!graph->internal->thread)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2013-07-16 18:31:23 +03:00
|
|
|
ret = thread_init_internal(graph->internal->thread, graph->nb_threads);
|
2013-05-11 21:41:46 +03:00
|
|
|
if (ret <= 1) {
|
|
|
|
av_freep(&graph->internal->thread);
|
|
|
|
graph->thread_type = 0;
|
|
|
|
graph->nb_threads = 1;
|
|
|
|
return (ret < 0) ? ret : 0;
|
|
|
|
}
|
|
|
|
graph->nb_threads = ret;
|
|
|
|
|
|
|
|
graph->internal->thread_execute = thread_execute;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ff_graph_thread_free(AVFilterGraph *graph)
|
|
|
|
{
|
|
|
|
if (graph->internal->thread)
|
|
|
|
slice_thread_uninit(graph->internal->thread);
|
|
|
|
av_freep(&graph->internal->thread);
|
|
|
|
}
|