Mostly based on libavcodec's
Anton Khirnov authored on 2013/05/12 03:41:46... | ... |
@@ -1284,6 +1284,9 @@ static void show_help_filter(const char *name) |
1284 | 1284 |
|
1285 | 1285 |
printf("Filter %s [%s]:\n", f->name, f->description); |
1286 | 1286 |
|
1287 |
+ if (f->flags & AVFILTER_FLAG_SLICE_THREADS) |
|
1288 |
+ printf(" slice threading supported\n"); |
|
1289 |
+ |
|
1287 | 1290 |
printf(" Inputs:\n"); |
1288 | 1291 |
count = avfilter_pad_count(f->inputs); |
1289 | 1292 |
for (i = 0; i < count; i++) { |
... | ... |
@@ -13,6 +13,13 @@ libavutil: 2012-10-22 |
13 | 13 |
|
14 | 14 |
API changes, most recent first: |
15 | 15 |
|
16 |
+2013-05-xx - xxxxxxx - lavfi 3.10.0 - avfilter.h |
|
17 |
+ Add support for slice multithreading to lavfi. Filters supporting threading |
|
18 |
+ are marked with AVFILTER_FLAG_SLICE_THREADS. |
|
19 |
+ New fields AVFilterContext.thread_type, AVFilterGraph.thread_type and |
|
20 |
+ AVFilterGraph.nb_threads (accessible directly or through AVOptions) may be |
|
21 |
+ used to configure multithreading. |
|
22 |
+ |
|
16 | 23 |
2013-xx-xx - xxxxxxx - lavu 52.12.0 - cpu.h |
17 | 24 |
Add av_cpu_count() function for getting the number of logical CPUs. |
18 | 25 |
|
... | ... |
@@ -352,14 +352,37 @@ static const AVClass *filter_child_class_next(const AVClass *prev) |
352 | 352 |
return NULL; |
353 | 353 |
} |
354 | 354 |
|
355 |
+#define OFFSET(x) offsetof(AVFilterContext, x) |
|
356 |
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM |
|
357 |
+static const AVOption options[] = { |
|
358 |
+ { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS, |
|
359 |
+ { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" }, |
|
360 |
+ { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .unit = "thread_type" }, |
|
361 |
+ { NULL }, |
|
362 |
+}; |
|
363 |
+ |
|
355 | 364 |
static const AVClass avfilter_class = { |
356 | 365 |
.class_name = "AVFilter", |
357 | 366 |
.item_name = filter_name, |
358 | 367 |
.version = LIBAVUTIL_VERSION_INT, |
359 | 368 |
.child_next = filter_child_next, |
360 | 369 |
.child_class_next = filter_child_class_next, |
370 |
+ .option = options, |
|
361 | 371 |
}; |
362 | 372 |
|
373 |
+static int default_execute(AVFilterContext *ctx, action_func *func, void *arg, |
|
374 |
+ int *ret, int nb_jobs) |
|
375 |
+{ |
|
376 |
+ int i; |
|
377 |
+ |
|
378 |
+ for (i = 0; i < nb_jobs; i++) { |
|
379 |
+ int r = func(ctx, arg, i, nb_jobs); |
|
380 |
+ if (ret) |
|
381 |
+ ret[i] = r; |
|
382 |
+ } |
|
383 |
+ return 0; |
|
384 |
+} |
|
385 |
+ |
|
363 | 386 |
AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name) |
364 | 387 |
{ |
365 | 388 |
AVFilterContext *ret; |
... | ... |
@@ -380,11 +403,17 @@ AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name) |
380 | 380 |
goto err; |
381 | 381 |
} |
382 | 382 |
|
383 |
+ av_opt_set_defaults(ret); |
|
383 | 384 |
if (filter->priv_class) { |
384 | 385 |
*(const AVClass**)ret->priv = filter->priv_class; |
385 | 386 |
av_opt_set_defaults(ret->priv); |
386 | 387 |
} |
387 | 388 |
|
389 |
+ ret->internal = av_mallocz(sizeof(*ret->internal)); |
|
390 |
+ if (!ret->internal) |
|
391 |
+ goto err; |
|
392 |
+ ret->internal->execute = default_execute; |
|
393 |
+ |
|
388 | 394 |
ret->nb_inputs = avfilter_pad_count(filter->inputs); |
389 | 395 |
if (ret->nb_inputs ) { |
390 | 396 |
ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs); |
... | ... |
@@ -421,6 +450,7 @@ err: |
421 | 421 |
av_freep(&ret->output_pads); |
422 | 422 |
ret->nb_outputs = 0; |
423 | 423 |
av_freep(&ret->priv); |
424 |
+ av_freep(&ret->internal); |
|
424 | 425 |
av_free(ret); |
425 | 426 |
return NULL; |
426 | 427 |
} |
... | ... |
@@ -478,6 +508,7 @@ void avfilter_free(AVFilterContext *filter) |
478 | 478 |
av_freep(&filter->inputs); |
479 | 479 |
av_freep(&filter->outputs); |
480 | 480 |
av_freep(&filter->priv); |
481 |
+ av_freep(&filter->internal); |
|
481 | 482 |
av_free(filter); |
482 | 483 |
} |
483 | 484 |
|
... | ... |
@@ -525,6 +556,21 @@ int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options) |
525 | 525 |
{ |
526 | 526 |
int ret = 0; |
527 | 527 |
|
528 |
+ ret = av_opt_set_dict(ctx, options); |
|
529 |
+ if (ret < 0) { |
|
530 |
+ av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n"); |
|
531 |
+ return ret; |
|
532 |
+ } |
|
533 |
+ |
|
534 |
+ if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS && |
|
535 |
+ ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE && |
|
536 |
+ ctx->graph->internal->thread_execute) { |
|
537 |
+ ctx->thread_type = AVFILTER_THREAD_SLICE; |
|
538 |
+ ctx->internal->execute = ctx->graph->internal->thread_execute; |
|
539 |
+ } else { |
|
540 |
+ ctx->thread_type = 0; |
|
541 |
+ } |
|
542 |
+ |
|
528 | 543 |
if (ctx->filter->priv_class) { |
529 | 544 |
ret = av_opt_set_dict(ctx->priv, options); |
530 | 545 |
if (ret < 0) { |
... | ... |
@@ -401,6 +401,11 @@ enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx); |
401 | 401 |
* the options supplied to it. |
402 | 402 |
*/ |
403 | 403 |
#define AVFILTER_FLAG_DYNAMIC_OUTPUTS (1 << 1) |
404 |
+/** |
|
405 |
+ * The filter supports multithreading by splitting frames into multiple parts |
|
406 |
+ * and processing them concurrently. |
|
407 |
+ */ |
|
408 |
+#define AVFILTER_FLAG_SLICE_THREADS (1 << 2) |
|
404 | 409 |
|
405 | 410 |
/** |
406 | 411 |
* Filter definition. This defines the pads a filter contains, and all the |
... | ... |
@@ -472,6 +477,13 @@ typedef struct AVFilter { |
472 | 472 |
struct AVFilter *next; |
473 | 473 |
} AVFilter; |
474 | 474 |
|
475 |
+/** |
|
476 |
+ * Process multiple parts of the frame concurrently. |
|
477 |
+ */ |
|
478 |
+#define AVFILTER_THREAD_SLICE (1 << 0) |
|
479 |
+ |
|
480 |
+typedef struct AVFilterInternal AVFilterInternal; |
|
481 |
+ |
|
475 | 482 |
/** An instance of a filter */ |
476 | 483 |
struct AVFilterContext { |
477 | 484 |
const AVClass *av_class; ///< needed for av_log() |
... | ... |
@@ -497,6 +509,29 @@ struct AVFilterContext { |
497 | 497 |
void *priv; ///< private data for use by the filter |
498 | 498 |
|
499 | 499 |
struct AVFilterGraph *graph; ///< filtergraph this filter belongs to |
500 |
+ |
|
501 |
+ /** |
|
502 |
+ * Type of multithreading being allowed/used. A combination of |
|
503 |
+ * AVFILTER_THREAD_* flags. |
|
504 |
+ * |
|
505 |
+ * May be set by the caller before initializing the filter to forbid some |
|
506 |
+ * or all kinds of multithreading for this filter. The default is allowing |
|
507 |
+ * everything. |
|
508 |
+ * |
|
509 |
+ * When the filter is initialized, this field is combined using bit AND with |
|
510 |
+ * AVFilterGraph.thread_type to get the final mask used for determining |
|
511 |
+ * allowed threading types. I.e. a threading type needs to be set in both |
|
512 |
+ * to be allowed. |
|
513 |
+ * |
|
514 |
+ * After the filter is initialzed, libavfilter sets this field to the |
|
515 |
+ * threading type that is actually used (0 for no multithreading). |
|
516 |
+ */ |
|
517 |
+ int thread_type; |
|
518 |
+ |
|
519 |
+ /** |
|
520 |
+ * An opaque struct for libavfilter internal use. |
|
521 |
+ */ |
|
522 |
+ AVFilterInternal *internal; |
|
500 | 523 |
}; |
501 | 524 |
|
502 | 525 |
/** |
... | ... |
@@ -793,6 +828,8 @@ int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src); |
793 | 793 |
*/ |
794 | 794 |
const AVClass *avfilter_get_class(void); |
795 | 795 |
|
796 |
+typedef struct AVFilterGraphInternal AVFilterGraphInternal; |
|
797 |
+ |
|
796 | 798 |
typedef struct AVFilterGraph { |
797 | 799 |
const AVClass *av_class; |
798 | 800 |
#if FF_API_FOO_COUNT |
... | ... |
@@ -809,6 +846,32 @@ typedef struct AVFilterGraph { |
809 | 809 |
#if FF_API_FOO_COUNT |
810 | 810 |
unsigned nb_filters; |
811 | 811 |
#endif |
812 |
+ |
|
813 |
+ /** |
|
814 |
+ * Type of multithreading allowed for filters in this graph. A combination |
|
815 |
+ * of AVFILTER_THREAD_* flags. |
|
816 |
+ * |
|
817 |
+ * May be set by the caller at any point, the setting will apply to all |
|
818 |
+ * filters initialized after that. The default is allowing everything. |
|
819 |
+ * |
|
820 |
+ * When a filter in this graph is initialized, this field is combined using |
|
821 |
+ * bit AND with AVFilterContext.thread_type to get the final mask used for |
|
822 |
+ * determining allowed threading types. I.e. a threading type needs to be |
|
823 |
+ * set in both to be allowed. |
|
824 |
+ */ |
|
825 |
+ int thread_type; |
|
826 |
+ |
|
827 |
+ /** |
|
828 |
+ * Maximum number of threads used by filters in this graph. May be set by |
|
829 |
+ * the caller before adding any filters to the filtergraph. Zero (the |
|
830 |
+ * default) means that the number of threads is determined automatically. |
|
831 |
+ */ |
|
832 |
+ int nb_threads; |
|
833 |
+ |
|
834 |
+ /** |
|
835 |
+ * Opaque object for libavfilter internal use. |
|
836 |
+ */ |
|
837 |
+ AVFilterGraphInternal *internal; |
|
812 | 838 |
} AVFilterGraph; |
813 | 839 |
|
814 | 840 |
/** |
... | ... |
@@ -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" |
... | ... |
@@ -27,22 +29,59 @@ |
27 | 27 |
#include "libavutil/channel_layout.h" |
28 | 28 |
#include "libavutil/common.h" |
29 | 29 |
#include "libavutil/log.h" |
30 |
+#include "libavutil/opt.h" |
|
31 |
+ |
|
30 | 32 |
#include "avfilter.h" |
31 | 33 |
#include "formats.h" |
32 | 34 |
#include "internal.h" |
35 |
+#include "thread.h" |
|
36 |
+ |
|
37 |
+#define OFFSET(x) offsetof(AVFilterGraph, x) |
|
38 |
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM |
|
39 |
+static const AVOption filtergraph_options[] = { |
|
40 |
+ { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS, |
|
41 |
+ { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" }, |
|
42 |
+ { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .flags = FLAGS, .unit = "thread_type" }, |
|
43 |
+ { "threads", "Maximum number of threads", OFFSET(nb_threads), |
|
44 |
+ AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS }, |
|
45 |
+ { NULL }, |
|
46 |
+}; |
|
33 | 47 |
|
34 | 48 |
static const AVClass filtergraph_class = { |
35 | 49 |
.class_name = "AVFilterGraph", |
36 | 50 |
.item_name = av_default_item_name, |
37 | 51 |
.version = LIBAVUTIL_VERSION_INT, |
52 |
+ .option = filtergraph_options, |
|
38 | 53 |
}; |
39 | 54 |
|
55 |
+#if !HAVE_THREADS |
|
56 |
+void ff_graph_thread_free(AVFilterGraph *graph) |
|
57 |
+{ |
|
58 |
+} |
|
59 |
+ |
|
60 |
+int ff_graph_thread_init(AVFilterGraph *graph) |
|
61 |
+{ |
|
62 |
+ graph->thread_type = 0; |
|
63 |
+ graph->nb_threads = 1; |
|
64 |
+ return 0; |
|
65 |
+} |
|
66 |
+#endif |
|
67 |
+ |
|
40 | 68 |
AVFilterGraph *avfilter_graph_alloc(void) |
41 | 69 |
{ |
42 | 70 |
AVFilterGraph *ret = av_mallocz(sizeof(*ret)); |
43 | 71 |
if (!ret) |
44 | 72 |
return NULL; |
73 |
+ |
|
74 |
+ ret->internal = av_mallocz(sizeof(*ret->internal)); |
|
75 |
+ if (!ret->internal) { |
|
76 |
+ av_freep(&ret); |
|
77 |
+ return NULL; |
|
78 |
+ } |
|
79 |
+ |
|
45 | 80 |
ret->av_class = &filtergraph_class; |
81 |
+ av_opt_set_defaults(ret); |
|
82 |
+ |
|
46 | 83 |
return ret; |
47 | 84 |
} |
48 | 85 |
|
... | ... |
@@ -67,9 +106,12 @@ void avfilter_graph_free(AVFilterGraph **graph) |
67 | 67 |
while ((*graph)->nb_filters) |
68 | 68 |
avfilter_free((*graph)->filters[0]); |
69 | 69 |
|
70 |
+ ff_graph_thread_free(*graph); |
|
71 |
+ |
|
70 | 72 |
av_freep(&(*graph)->scale_sws_opts); |
71 | 73 |
av_freep(&(*graph)->resample_lavr_opts); |
72 | 74 |
av_freep(&(*graph)->filters); |
75 |
+ av_freep(&(*graph)->internal); |
|
73 | 76 |
av_freep(graph); |
74 | 77 |
} |
75 | 78 |
|
... | ... |
@@ -123,6 +165,14 @@ AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph, |
123 | 123 |
{ |
124 | 124 |
AVFilterContext **filters, *s; |
125 | 125 |
|
126 |
+ if (graph->thread_type && !graph->internal->thread) { |
|
127 |
+ int ret = ff_graph_thread_init(graph); |
|
128 |
+ if (ret < 0) { |
|
129 |
+ av_log(graph, AV_LOG_ERROR, "Error initializing threading.\n"); |
|
130 |
+ return NULL; |
|
131 |
+ } |
|
132 |
+ } |
|
133 |
+ |
|
126 | 134 |
s = ff_filter_alloc(filter, name); |
127 | 135 |
if (!s) |
128 | 136 |
return NULL; |
... | ... |
@@ -25,6 +25,7 @@ |
25 | 25 |
*/ |
26 | 26 |
|
27 | 27 |
#include "avfilter.h" |
28 |
+#include "thread.h" |
|
28 | 29 |
|
29 | 30 |
#if !FF_API_AVFILTERPAD_PUBLIC |
30 | 31 |
/** |
... | ... |
@@ -117,6 +118,17 @@ struct AVFilterPad { |
117 | 117 |
}; |
118 | 118 |
#endif |
119 | 119 |
|
120 |
+struct AVFilterGraphInternal { |
|
121 |
+ void *thread; |
|
122 |
+ int (*thread_execute)(AVFilterContext *ctx, action_func *func, void *arg, |
|
123 |
+ int *ret, int nb_jobs); |
|
124 |
+}; |
|
125 |
+ |
|
126 |
+struct AVFilterInternal { |
|
127 |
+ int (*execute)(AVFilterContext *ctx, action_func *func, void *arg, |
|
128 |
+ int *ret, int nb_jobs); |
|
129 |
+}; |
|
130 |
+ |
|
120 | 131 |
/** default handler for freeing audio/video buffer when there are no references left */ |
121 | 132 |
void ff_avfilter_default_free_buffer(AVFilterBuffer *buf); |
122 | 133 |
|
123 | 134 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,229 @@ |
0 |
+/* |
|
1 |
+ * |
|
2 |
+ * This file is part of Libav. |
|
3 |
+ * |
|
4 |
+ * Libav 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 |
+ * Libav 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 Libav; 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 Libav. |
|
3 |
+ * |
|
4 |
+ * Libav 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 |
+ * Libav 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 Libav; 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,7 +30,7 @@ |
30 | 30 |
#include "libavutil/avutil.h" |
31 | 31 |
|
32 | 32 |
#define LIBAVFILTER_VERSION_MAJOR 3 |
33 |
-#define LIBAVFILTER_VERSION_MINOR 9 |
|
33 |
+#define LIBAVFILTER_VERSION_MINOR 10 |
|
34 | 34 |
#define LIBAVFILTER_VERSION_MICRO 0 |
35 | 35 |
|
36 | 36 |
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ |