* commit '129bb238430ec45a3b5f8f1d384df590ddf7b62f':
lavfi: add a slice threading infrastructure
Conflicts:
Changelog
cmdutils.c
doc/APIchanges
libavfilter/Makefile
libavfilter/avfilter.c
libavfilter/avfilter.h
libavfilter/avfiltergraph.c
libavfilter/internal.h
libavfilter/version.h
Merged-by: Michael Niedermayer <michaelni@gmx.at>
... | ... |
@@ -1660,6 +1660,10 @@ static void show_help_filter(const char *name) |
1660 | 1660 |
printf("Filter %s\n", f->name); |
1661 | 1661 |
if (f->description) |
1662 | 1662 |
printf(" %s\n", f->description); |
1663 |
+ |
|
1664 |
+ if (f->flags & AVFILTER_FLAG_SLICE_THREADS) |
|
1665 |
+ printf(" slice threading supported\n"); |
|
1666 |
+ |
|
1663 | 1667 |
printf(" Inputs:\n"); |
1664 | 1668 |
count = avfilter_pad_count(f->inputs); |
1665 | 1669 |
for (i = 0; i < count; i++) { |
... | ... |
@@ -16,6 +16,13 @@ libavutil: 2012-10-22 |
16 | 16 |
API changes, most recent first: |
17 | 17 |
|
18 | 18 |
|
19 |
+2013-05-24 - xxxxxxx - lavfi 3.70.100 - avfilter.h |
|
20 |
+ Add support for slice multithreading to lavfi. Filters supporting threading |
|
21 |
+ are marked with AVFILTER_FLAG_SLICE_THREADS. |
|
22 |
+ New fields AVFilterContext.thread_type, AVFilterGraph.thread_type and |
|
23 |
+ AVFilterGraph.nb_threads (accessible directly or through AVOptions) may be |
|
24 |
+ used to configure multithreading. |
|
25 |
+ |
|
19 | 26 |
2013-05-24 - xxxxxxx - lavu 52.34.100 - cpu.h |
20 | 27 |
Add av_cpu_count() function for getting the number of logical CPUs. |
21 | 28 |
|
... | ... |
@@ -240,6 +240,8 @@ OBJS-$(CONFIG_MOVIE_FILTER) += src_movie.o |
240 | 240 |
SKIPHEADERS-$(CONFIG_LIBVIDSTAB) += vidstabutils.h |
241 | 241 |
SKIPHEADERS-$(CONFIG_OPENCL) += opencl_internal.h deshake_opencl_kernel.h unsharp_opencl_kernel.h |
242 | 242 |
|
243 |
+OBJS-$(HAVE_THREADS) += pthread.o |
|
244 |
+ |
|
243 | 245 |
TOOLS = graph2dot |
244 | 246 |
TESTPROGS = drawutils filtfmts formats |
245 | 247 |
|
... | ... |
@@ -538,9 +538,12 @@ static const AVClass *filter_child_class_next(const AVClass *prev) |
538 | 538 |
|
539 | 539 |
#define OFFSET(x) offsetof(AVFilterContext, x) |
540 | 540 |
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM |
541 |
-static const AVOption filters_common_options[] = { |
|
541 |
+static const AVOption options[] = { |
|
542 |
+ { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS, |
|
543 |
+ { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" }, |
|
544 |
+ { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .unit = "thread_type" }, |
|
542 | 545 |
{ "enable", "set enable expression", OFFSET(enable_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS }, |
543 |
- { NULL } |
|
546 |
+ { NULL }, |
|
544 | 547 |
}; |
545 | 548 |
|
546 | 549 |
static const AVClass avfilter_class = { |
... | ... |
@@ -549,10 +552,23 @@ static const AVClass avfilter_class = { |
549 | 549 |
.version = LIBAVUTIL_VERSION_INT, |
550 | 550 |
.category = AV_CLASS_CATEGORY_FILTER, |
551 | 551 |
.child_next = filter_child_next, |
552 |
- .option = filters_common_options, |
|
553 | 552 |
.child_class_next = filter_child_class_next, |
553 |
+ .option = options, |
|
554 | 554 |
}; |
555 | 555 |
|
556 |
+static int default_execute(AVFilterContext *ctx, action_func *func, void *arg, |
|
557 |
+ int *ret, int nb_jobs) |
|
558 |
+{ |
|
559 |
+ int i; |
|
560 |
+ |
|
561 |
+ for (i = 0; i < nb_jobs; i++) { |
|
562 |
+ int r = func(ctx, arg, i, nb_jobs); |
|
563 |
+ if (ret) |
|
564 |
+ ret[i] = r; |
|
565 |
+ } |
|
566 |
+ return 0; |
|
567 |
+} |
|
568 |
+ |
|
556 | 569 |
AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name) |
557 | 570 |
{ |
558 | 571 |
AVFilterContext *ret; |
... | ... |
@@ -573,11 +589,17 @@ AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name) |
573 | 573 |
goto err; |
574 | 574 |
} |
575 | 575 |
|
576 |
+ av_opt_set_defaults(ret); |
|
576 | 577 |
if (filter->priv_class) { |
577 | 578 |
*(const AVClass**)ret->priv = filter->priv_class; |
578 | 579 |
av_opt_set_defaults(ret->priv); |
579 | 580 |
} |
580 | 581 |
|
582 |
+ ret->internal = av_mallocz(sizeof(*ret->internal)); |
|
583 |
+ if (!ret->internal) |
|
584 |
+ goto err; |
|
585 |
+ ret->internal->execute = default_execute; |
|
586 |
+ |
|
581 | 587 |
ret->nb_inputs = avfilter_pad_count(filter->inputs); |
582 | 588 |
if (ret->nb_inputs ) { |
583 | 589 |
ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs); |
... | ... |
@@ -614,6 +636,7 @@ err: |
614 | 614 |
av_freep(&ret->output_pads); |
615 | 615 |
ret->nb_outputs = 0; |
616 | 616 |
av_freep(&ret->priv); |
617 |
+ av_freep(&ret->internal); |
|
617 | 618 |
av_free(ret); |
618 | 619 |
return NULL; |
619 | 620 |
} |
... | ... |
@@ -681,6 +704,7 @@ void avfilter_free(AVFilterContext *filter) |
681 | 681 |
av_expr_free(filter->enable); |
682 | 682 |
filter->enable = NULL; |
683 | 683 |
av_freep(&filter->var_values); |
684 |
+ av_freep(&filter->internal); |
|
684 | 685 |
av_free(filter); |
685 | 686 |
} |
686 | 687 |
|
... | ... |
@@ -772,6 +796,21 @@ int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options) |
772 | 772 |
{ |
773 | 773 |
int ret = 0; |
774 | 774 |
|
775 |
+ ret = av_opt_set_dict(ctx, options); |
|
776 |
+ if (ret < 0) { |
|
777 |
+ av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n"); |
|
778 |
+ return ret; |
|
779 |
+ } |
|
780 |
+ |
|
781 |
+ if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS && |
|
782 |
+ ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE && |
|
783 |
+ ctx->graph->internal->thread_execute) { |
|
784 |
+ ctx->thread_type = AVFILTER_THREAD_SLICE; |
|
785 |
+ ctx->internal->execute = ctx->graph->internal->thread_execute; |
|
786 |
+ } else { |
|
787 |
+ ctx->thread_type = 0; |
|
788 |
+ } |
|
789 |
+ |
|
775 | 790 |
if (ctx->filter->priv_class) { |
776 | 791 |
ret = av_opt_set_dict(ctx->priv, options); |
777 | 792 |
if (ret < 0) { |
... | ... |
@@ -429,6 +429,11 @@ enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx); |
429 | 429 |
*/ |
430 | 430 |
#define AVFILTER_FLAG_DYNAMIC_OUTPUTS (1 << 1) |
431 | 431 |
/** |
432 |
+ * The filter supports multithreading by splitting frames into multiple parts |
|
433 |
+ * and processing them concurrently. |
|
434 |
+ */ |
|
435 |
+#define AVFILTER_FLAG_SLICE_THREADS (1 << 2) |
|
436 |
+/** |
|
432 | 437 |
* Some filters support a generic "enable" expression option that can be used |
433 | 438 |
* to enable or disable a filter in the timeline. Filters supporting this |
434 | 439 |
* option have this flag set. When the enable expression is false, the default |
... | ... |
@@ -542,6 +547,13 @@ typedef struct AVFilter { |
542 | 542 |
int (*init_opaque)(AVFilterContext *ctx, void *opaque); |
543 | 543 |
} AVFilter; |
544 | 544 |
|
545 |
+/** |
|
546 |
+ * Process multiple parts of the frame concurrently. |
|
547 |
+ */ |
|
548 |
+#define AVFILTER_THREAD_SLICE (1 << 0) |
|
549 |
+ |
|
550 |
+typedef struct AVFilterInternal AVFilterInternal; |
|
551 |
+ |
|
545 | 552 |
/** An instance of a filter */ |
546 | 553 |
struct AVFilterContext { |
547 | 554 |
const AVClass *av_class; ///< needed for av_log() and filters common options |
... | ... |
@@ -568,6 +580,29 @@ struct AVFilterContext { |
568 | 568 |
|
569 | 569 |
struct AVFilterGraph *graph; ///< filtergraph this filter belongs to |
570 | 570 |
|
571 |
+ /** |
|
572 |
+ * Type of multithreading being allowed/used. A combination of |
|
573 |
+ * AVFILTER_THREAD_* flags. |
|
574 |
+ * |
|
575 |
+ * May be set by the caller before initializing the filter to forbid some |
|
576 |
+ * or all kinds of multithreading for this filter. The default is allowing |
|
577 |
+ * everything. |
|
578 |
+ * |
|
579 |
+ * When the filter is initialized, this field is combined using bit AND with |
|
580 |
+ * AVFilterGraph.thread_type to get the final mask used for determining |
|
581 |
+ * allowed threading types. I.e. a threading type needs to be set in both |
|
582 |
+ * to be allowed. |
|
583 |
+ * |
|
584 |
+ * After the filter is initialzed, libavfilter sets this field to the |
|
585 |
+ * threading type that is actually used (0 for no multithreading). |
|
586 |
+ */ |
|
587 |
+ int thread_type; |
|
588 |
+ |
|
589 |
+ /** |
|
590 |
+ * An opaque struct for libavfilter internal use. |
|
591 |
+ */ |
|
592 |
+ AVFilterInternal *internal; |
|
593 |
+ |
|
571 | 594 |
struct AVFilterCommand *command_queue; |
572 | 595 |
|
573 | 596 |
char *enable_str; ///< enable expression string |
... | ... |
@@ -1020,6 +1055,8 @@ int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src); |
1020 | 1020 |
*/ |
1021 | 1021 |
const AVClass *avfilter_get_class(void); |
1022 | 1022 |
|
1023 |
+typedef struct AVFilterGraphInternal AVFilterGraphInternal; |
|
1024 |
+ |
|
1023 | 1025 |
typedef struct AVFilterGraph { |
1024 | 1026 |
const AVClass *av_class; |
1025 | 1027 |
#if FF_API_FOO_COUNT |
... | ... |
@@ -1036,6 +1073,33 @@ typedef struct AVFilterGraph { |
1036 | 1036 |
#if FF_API_FOO_COUNT |
1037 | 1037 |
unsigned nb_filters; |
1038 | 1038 |
#endif |
1039 |
+ |
|
1040 |
+ /** |
|
1041 |
+ * Type of multithreading allowed for filters in this graph. A combination |
|
1042 |
+ * of AVFILTER_THREAD_* flags. |
|
1043 |
+ * |
|
1044 |
+ * May be set by the caller at any point, the setting will apply to all |
|
1045 |
+ * filters initialized after that. The default is allowing everything. |
|
1046 |
+ * |
|
1047 |
+ * When a filter in this graph is initialized, this field is combined using |
|
1048 |
+ * bit AND with AVFilterContext.thread_type to get the final mask used for |
|
1049 |
+ * determining allowed threading types. I.e. a threading type needs to be |
|
1050 |
+ * set in both to be allowed. |
|
1051 |
+ */ |
|
1052 |
+ int thread_type; |
|
1053 |
+ |
|
1054 |
+ /** |
|
1055 |
+ * Maximum number of threads used by filters in this graph. May be set by |
|
1056 |
+ * the caller before adding any filters to the filtergraph. Zero (the |
|
1057 |
+ * default) means that the number of threads is determined automatically. |
|
1058 |
+ */ |
|
1059 |
+ int nb_threads; |
|
1060 |
+ |
|
1061 |
+ /** |
|
1062 |
+ * Opaque object for libavfilter internal use. |
|
1063 |
+ */ |
|
1064 |
+ AVFilterGraphInternal *internal; |
|
1065 |
+ |
|
1039 | 1066 |
char *aresample_swr_opts; ///< swr options to use for the auto-inserted aresample filters, Access ONLY through AVOptions |
1040 | 1067 |
|
1041 | 1068 |
/** |
... | ... |
@@ -20,6 +20,8 @@ |
20 | 20 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | 21 |
*/ |
22 | 22 |
|
23 |
+#include "config.h" |
|
24 |
+ |
|
23 | 25 |
#include <string.h> |
24 | 26 |
|
25 | 27 |
#include "libavutil/avassert.h" |
... | ... |
@@ -29,33 +31,63 @@ |
29 | 29 |
#include "libavutil/opt.h" |
30 | 30 |
#include "libavutil/pixdesc.h" |
31 | 31 |
#include "libavcodec/avcodec.h" // avcodec_find_best_pix_fmt_of_2() |
32 |
+ |
|
32 | 33 |
#include "avfilter.h" |
33 | 34 |
#include "formats.h" |
34 | 35 |
#include "internal.h" |
35 |
- |
|
36 |
-#define OFFSET(x) offsetof(AVFilterGraph,x) |
|
37 |
- |
|
38 |
-static const AVOption options[]={ |
|
39 |
-{"scale_sws_opts" , "default scale filter options" , OFFSET(scale_sws_opts) , AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, 0 }, |
|
40 |
-{"aresample_swr_opts" , "default aresample filter options" , OFFSET(aresample_swr_opts) , AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, 0 }, |
|
41 |
-{0} |
|
36 |
+#include "thread.h" |
|
37 |
+ |
|
38 |
+#define OFFSET(x) offsetof(AVFilterGraph, x) |
|
39 |
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
|
40 |
+static const AVOption filtergraph_options[] = { |
|
41 |
+ { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS, |
|
42 |
+ { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" }, |
|
43 |
+ { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .flags = FLAGS, .unit = "thread_type" }, |
|
44 |
+ { "threads", "Maximum number of threads", OFFSET(nb_threads), |
|
45 |
+ AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS }, |
|
46 |
+ {"scale_sws_opts" , "default scale filter options" , OFFSET(scale_sws_opts) , |
|
47 |
+ AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, |
|
48 |
+ {"aresample_swr_opts" , "default aresample filter options" , OFFSET(aresample_swr_opts) , |
|
49 |
+ AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, |
|
50 |
+ { NULL }, |
|
42 | 51 |
}; |
43 | 52 |
|
44 |
- |
|
45 | 53 |
static const AVClass filtergraph_class = { |
46 | 54 |
.class_name = "AVFilterGraph", |
47 | 55 |
.item_name = av_default_item_name, |
48 |
- .option = options, |
|
49 | 56 |
.version = LIBAVUTIL_VERSION_INT, |
57 |
+ .option = filtergraph_options, |
|
50 | 58 |
.category = AV_CLASS_CATEGORY_FILTER, |
51 | 59 |
}; |
52 | 60 |
|
61 |
+#if !HAVE_THREADS |
|
62 |
+void ff_graph_thread_free(AVFilterGraph *graph) |
|
63 |
+{ |
|
64 |
+} |
|
65 |
+ |
|
66 |
+int ff_graph_thread_init(AVFilterGraph *graph) |
|
67 |
+{ |
|
68 |
+ graph->thread_type = 0; |
|
69 |
+ graph->nb_threads = 1; |
|
70 |
+ return 0; |
|
71 |
+} |
|
72 |
+#endif |
|
73 |
+ |
|
53 | 74 |
AVFilterGraph *avfilter_graph_alloc(void) |
54 | 75 |
{ |
55 | 76 |
AVFilterGraph *ret = av_mallocz(sizeof(*ret)); |
56 | 77 |
if (!ret) |
57 | 78 |
return NULL; |
79 |
+ |
|
80 |
+ ret->internal = av_mallocz(sizeof(*ret->internal)); |
|
81 |
+ if (!ret->internal) { |
|
82 |
+ av_freep(&ret); |
|
83 |
+ return NULL; |
|
84 |
+ } |
|
85 |
+ |
|
58 | 86 |
ret->av_class = &filtergraph_class; |
87 |
+ av_opt_set_defaults(ret); |
|
88 |
+ |
|
59 | 89 |
return ret; |
60 | 90 |
} |
61 | 91 |
|
... | ... |
@@ -80,11 +112,15 @@ void avfilter_graph_free(AVFilterGraph **graph) |
80 | 80 |
while ((*graph)->nb_filters) |
81 | 81 |
avfilter_free((*graph)->filters[0]); |
82 | 82 |
|
83 |
+ ff_graph_thread_free(*graph); |
|
84 |
+ |
|
83 | 85 |
av_freep(&(*graph)->sink_links); |
86 |
+ |
|
84 | 87 |
av_freep(&(*graph)->scale_sws_opts); |
85 | 88 |
av_freep(&(*graph)->aresample_swr_opts); |
86 | 89 |
av_freep(&(*graph)->resample_lavr_opts); |
87 | 90 |
av_freep(&(*graph)->filters); |
91 |
+ av_freep(&(*graph)->internal); |
|
88 | 92 |
av_freep(graph); |
89 | 93 |
} |
90 | 94 |
|
... | ... |
@@ -143,6 +179,14 @@ AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph, |
143 | 143 |
{ |
144 | 144 |
AVFilterContext **filters, *s; |
145 | 145 |
|
146 |
+ if (graph->thread_type && !graph->internal->thread) { |
|
147 |
+ int ret = ff_graph_thread_init(graph); |
|
148 |
+ if (ret < 0) { |
|
149 |
+ av_log(graph, AV_LOG_ERROR, "Error initializing threading.\n"); |
|
150 |
+ return NULL; |
|
151 |
+ } |
|
152 |
+ } |
|
153 |
+ |
|
146 | 154 |
s = ff_filter_alloc(filter, name); |
147 | 155 |
if (!s) |
148 | 156 |
return NULL; |
... | ... |
@@ -27,6 +27,7 @@ |
27 | 27 |
#include "avfilter.h" |
28 | 28 |
#include "avfiltergraph.h" |
29 | 29 |
#include "formats.h" |
30 |
+#include "thread.h" |
|
30 | 31 |
#include "video.h" |
31 | 32 |
|
32 | 33 |
#define POOL_SIZE 32 |
... | ... |
@@ -141,6 +142,17 @@ struct AVFilterPad { |
141 | 141 |
}; |
142 | 142 |
#endif |
143 | 143 |
|
144 |
+struct AVFilterGraphInternal { |
|
145 |
+ void *thread; |
|
146 |
+ int (*thread_execute)(AVFilterContext *ctx, action_func *func, void *arg, |
|
147 |
+ int *ret, int nb_jobs); |
|
148 |
+}; |
|
149 |
+ |
|
150 |
+struct AVFilterInternal { |
|
151 |
+ int (*execute)(AVFilterContext *ctx, action_func *func, void *arg, |
|
152 |
+ int *ret, int nb_jobs); |
|
153 |
+}; |
|
154 |
+ |
|
144 | 155 |
/** default handler for freeing audio/video buffer when there are no references left */ |
145 | 156 |
void ff_avfilter_default_free_buffer(AVFilterBuffer *buf); |
146 | 157 |
|
147 | 158 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,229 @@ |
0 |
+/* |
|
1 |
+ * |
|
2 |
+ * This file is part of FFmpeg. |
|
3 |
+ * |
|
4 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
5 |
+ * modify it under the terms of the GNU Lesser General Public |
|
6 |
+ * License as published by the Free Software Foundation; either |
|
7 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
8 |
+ * |
|
9 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
10 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
12 |
+ * Lesser General Public License for more details. |
|
13 |
+ * |
|
14 |
+ * You should have received a copy of the GNU Lesser General Public |
|
15 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
16 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
17 |
+ */ |
|
18 |
+ |
|
19 |
+/** |
|
20 |
+ * @file |
|
21 |
+ * Libavfilter multithreading support |
|
22 |
+ */ |
|
23 |
+ |
|
24 |
+#include "config.h" |
|
25 |
+ |
|
26 |
+#include "libavutil/common.h" |
|
27 |
+#include "libavutil/cpu.h" |
|
28 |
+#include "libavutil/mem.h" |
|
29 |
+ |
|
30 |
+#include "avfilter.h" |
|
31 |
+#include "internal.h" |
|
32 |
+#include "thread.h" |
|
33 |
+ |
|
34 |
+#if HAVE_PTHREADS |
|
35 |
+#include <pthread.h> |
|
36 |
+#elif HAVE_W32THREADS |
|
37 |
+#include "compat/w32pthreads.h" |
|
38 |
+#endif |
|
39 |
+ |
|
40 |
+typedef struct ThreadContext { |
|
41 |
+ AVFilterGraph *graph; |
|
42 |
+ |
|
43 |
+ int nb_threads; |
|
44 |
+ pthread_t *workers; |
|
45 |
+ action_func *func; |
|
46 |
+ |
|
47 |
+ /* per-execute perameters */ |
|
48 |
+ AVFilterContext *ctx; |
|
49 |
+ void *arg; |
|
50 |
+ int *rets; |
|
51 |
+ int nb_rets; |
|
52 |
+ int nb_jobs; |
|
53 |
+ |
|
54 |
+ pthread_cond_t last_job_cond; |
|
55 |
+ pthread_cond_t current_job_cond; |
|
56 |
+ pthread_mutex_t current_job_lock; |
|
57 |
+ int current_job; |
|
58 |
+ int done; |
|
59 |
+} ThreadContext; |
|
60 |
+ |
|
61 |
+static void* attribute_align_arg worker(void *v) |
|
62 |
+{ |
|
63 |
+ ThreadContext *c = v; |
|
64 |
+ int our_job = c->nb_jobs; |
|
65 |
+ int nb_threads = c->nb_threads; |
|
66 |
+ int self_id; |
|
67 |
+ |
|
68 |
+ pthread_mutex_lock(&c->current_job_lock); |
|
69 |
+ self_id = c->current_job++; |
|
70 |
+ for (;;) { |
|
71 |
+ while (our_job >= c->nb_jobs) { |
|
72 |
+ if (c->current_job == nb_threads + c->nb_jobs) |
|
73 |
+ pthread_cond_signal(&c->last_job_cond); |
|
74 |
+ |
|
75 |
+ pthread_cond_wait(&c->current_job_cond, &c->current_job_lock); |
|
76 |
+ our_job = self_id; |
|
77 |
+ |
|
78 |
+ if (c->done) { |
|
79 |
+ pthread_mutex_unlock(&c->current_job_lock); |
|
80 |
+ return NULL; |
|
81 |
+ } |
|
82 |
+ } |
|
83 |
+ pthread_mutex_unlock(&c->current_job_lock); |
|
84 |
+ |
|
85 |
+ c->rets[our_job % c->nb_rets] = c->func(c->ctx, c->arg, our_job, c->nb_jobs); |
|
86 |
+ |
|
87 |
+ pthread_mutex_lock(&c->current_job_lock); |
|
88 |
+ our_job = c->current_job++; |
|
89 |
+ } |
|
90 |
+} |
|
91 |
+ |
|
92 |
+static void slice_thread_uninit(ThreadContext *c) |
|
93 |
+{ |
|
94 |
+ int i; |
|
95 |
+ |
|
96 |
+ pthread_mutex_lock(&c->current_job_lock); |
|
97 |
+ c->done = 1; |
|
98 |
+ pthread_cond_broadcast(&c->current_job_cond); |
|
99 |
+ pthread_mutex_unlock(&c->current_job_lock); |
|
100 |
+ |
|
101 |
+ for (i = 0; i < c->nb_threads; i++) |
|
102 |
+ pthread_join(c->workers[i], NULL); |
|
103 |
+ |
|
104 |
+ pthread_mutex_destroy(&c->current_job_lock); |
|
105 |
+ pthread_cond_destroy(&c->current_job_cond); |
|
106 |
+ pthread_cond_destroy(&c->last_job_cond); |
|
107 |
+ av_freep(&c->workers); |
|
108 |
+} |
|
109 |
+ |
|
110 |
+static void slice_thread_park_workers(ThreadContext *c) |
|
111 |
+{ |
|
112 |
+ pthread_cond_wait(&c->last_job_cond, &c->current_job_lock); |
|
113 |
+ pthread_mutex_unlock(&c->current_job_lock); |
|
114 |
+} |
|
115 |
+ |
|
116 |
+static int thread_execute(AVFilterContext *ctx, action_func *func, |
|
117 |
+ void *arg, int *ret, int nb_jobs) |
|
118 |
+{ |
|
119 |
+ ThreadContext *c = ctx->graph->internal->thread; |
|
120 |
+ int dummy_ret; |
|
121 |
+ |
|
122 |
+ if (nb_jobs <= 0) |
|
123 |
+ return 0; |
|
124 |
+ |
|
125 |
+ pthread_mutex_lock(&c->current_job_lock); |
|
126 |
+ |
|
127 |
+ c->current_job = c->nb_threads; |
|
128 |
+ c->nb_jobs = nb_jobs; |
|
129 |
+ c->ctx = ctx; |
|
130 |
+ c->arg = arg; |
|
131 |
+ c->func = func; |
|
132 |
+ if (ret) { |
|
133 |
+ c->rets = ret; |
|
134 |
+ c->nb_rets = nb_jobs; |
|
135 |
+ } else { |
|
136 |
+ c->rets = &dummy_ret; |
|
137 |
+ c->nb_rets = 1; |
|
138 |
+ } |
|
139 |
+ pthread_cond_broadcast(&c->current_job_cond); |
|
140 |
+ |
|
141 |
+ slice_thread_park_workers(c); |
|
142 |
+ |
|
143 |
+ return 0; |
|
144 |
+} |
|
145 |
+ |
|
146 |
+static int thread_init(ThreadContext *c, int nb_threads) |
|
147 |
+{ |
|
148 |
+ int i, ret; |
|
149 |
+ |
|
150 |
+ if (!nb_threads) { |
|
151 |
+ int nb_cpus = av_cpu_count(); |
|
152 |
+ av_log(c->graph, AV_LOG_DEBUG, "Detected %d logical cores.\n", nb_cpus); |
|
153 |
+ // use number of cores + 1 as thread count if there is more than one |
|
154 |
+ if (nb_cpus > 1) |
|
155 |
+ nb_threads = nb_cpus + 1; |
|
156 |
+ else |
|
157 |
+ nb_threads = 1; |
|
158 |
+ } |
|
159 |
+ |
|
160 |
+ if (nb_threads <= 1) |
|
161 |
+ return 1; |
|
162 |
+ |
|
163 |
+ c->nb_threads = nb_threads; |
|
164 |
+ c->workers = av_mallocz(sizeof(*c->workers) * nb_threads); |
|
165 |
+ if (!c->workers) |
|
166 |
+ return AVERROR(ENOMEM); |
|
167 |
+ |
|
168 |
+ c->current_job = 0; |
|
169 |
+ c->nb_jobs = 0; |
|
170 |
+ c->done = 0; |
|
171 |
+ |
|
172 |
+ pthread_cond_init(&c->current_job_cond, NULL); |
|
173 |
+ pthread_cond_init(&c->last_job_cond, NULL); |
|
174 |
+ |
|
175 |
+ pthread_mutex_init(&c->current_job_lock, NULL); |
|
176 |
+ pthread_mutex_lock(&c->current_job_lock); |
|
177 |
+ for (i = 0; i < nb_threads; i++) { |
|
178 |
+ ret = pthread_create(&c->workers[i], NULL, worker, c); |
|
179 |
+ if (ret) { |
|
180 |
+ pthread_mutex_unlock(&c->current_job_lock); |
|
181 |
+ c->nb_threads = i; |
|
182 |
+ slice_thread_uninit(c); |
|
183 |
+ return AVERROR(ret); |
|
184 |
+ } |
|
185 |
+ } |
|
186 |
+ |
|
187 |
+ slice_thread_park_workers(c); |
|
188 |
+ |
|
189 |
+ return c->nb_threads; |
|
190 |
+} |
|
191 |
+ |
|
192 |
+int ff_graph_thread_init(AVFilterGraph *graph) |
|
193 |
+{ |
|
194 |
+ int ret; |
|
195 |
+ |
|
196 |
+#if HAVE_W32THREADS |
|
197 |
+ w32thread_init(); |
|
198 |
+#endif |
|
199 |
+ |
|
200 |
+ if (graph->nb_threads == 1) { |
|
201 |
+ graph->thread_type = 0; |
|
202 |
+ return 0; |
|
203 |
+ } |
|
204 |
+ |
|
205 |
+ graph->internal->thread = av_mallocz(sizeof(ThreadContext)); |
|
206 |
+ if (!graph->internal->thread) |
|
207 |
+ return AVERROR(ENOMEM); |
|
208 |
+ |
|
209 |
+ ret = thread_init(graph->internal->thread, graph->nb_threads); |
|
210 |
+ if (ret <= 1) { |
|
211 |
+ av_freep(&graph->internal->thread); |
|
212 |
+ graph->thread_type = 0; |
|
213 |
+ graph->nb_threads = 1; |
|
214 |
+ return (ret < 0) ? ret : 0; |
|
215 |
+ } |
|
216 |
+ graph->nb_threads = ret; |
|
217 |
+ |
|
218 |
+ graph->internal->thread_execute = thread_execute; |
|
219 |
+ |
|
220 |
+ return 0; |
|
221 |
+} |
|
222 |
+ |
|
223 |
+void ff_graph_thread_free(AVFilterGraph *graph) |
|
224 |
+{ |
|
225 |
+ if (graph->internal->thread) |
|
226 |
+ slice_thread_uninit(graph->internal->thread); |
|
227 |
+ av_freep(&graph->internal->thread); |
|
228 |
+} |
0 | 229 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,31 @@ |
0 |
+/* |
|
1 |
+ * |
|
2 |
+ * This file is part of FFmpeg. |
|
3 |
+ * |
|
4 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
5 |
+ * modify it under the terms of the GNU Lesser General Public |
|
6 |
+ * License as published by the Free Software Foundation; either |
|
7 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
8 |
+ * |
|
9 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
10 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
12 |
+ * Lesser General Public License for more details. |
|
13 |
+ * |
|
14 |
+ * You should have received a copy of the GNU Lesser General Public |
|
15 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
16 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
17 |
+ */ |
|
18 |
+ |
|
19 |
+#ifndef AVFILTER_THREAD_H |
|
20 |
+#define AVFILTER_THREAD_H |
|
21 |
+ |
|
22 |
+#include "avfilter.h" |
|
23 |
+ |
|
24 |
+typedef int (action_func)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); |
|
25 |
+ |
|
26 |
+int ff_graph_thread_init(AVFilterGraph *graph); |
|
27 |
+ |
|
28 |
+void ff_graph_thread_free(AVFilterGraph *graph); |
|
29 |
+ |
|
30 |
+#endif /* AVFILTER_THREAD_H */ |
... | ... |
@@ -30,8 +30,8 @@ |
30 | 30 |
#include "libavutil/avutil.h" |
31 | 31 |
|
32 | 32 |
#define LIBAVFILTER_VERSION_MAJOR 3 |
33 |
-#define LIBAVFILTER_VERSION_MINOR 69 |
|
34 |
-#define LIBAVFILTER_VERSION_MICRO 101 |
|
33 |
+#define LIBAVFILTER_VERSION_MINOR 70 |
|
34 |
+#define LIBAVFILTER_VERSION_MICRO 100 |
|
35 | 35 |
|
36 | 36 |
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ |
37 | 37 |
LIBAVFILTER_VERSION_MINOR, \ |