Skip to content

Commit 03567ed

Browse files
committed
avfilter/avfiltergraph: Avoid allocation for AVFilterGraphInternal
To do this, allocate AVFilterGraphInternal jointly with AVFilterGraph and rename it to FFFilterGraph in the process (similarly to AVStream/FFStream). The AVFilterGraphInternal* will be removed on the next major version bump. Signed-off-by: Andreas Rheinhardt <[email protected]>
1 parent a1aec77 commit 03567ed

File tree

4 files changed

+43
-35
lines changed

4 files changed

+43
-35
lines changed

libavfilter/avfilter.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ int avfilter_link(AVFilterContext *src, unsigned srcpad,
187187
av_assert0(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
188188
link->format = -1;
189189
link->colorspace = AVCOL_SPC_UNSPECIFIED;
190-
ff_framequeue_init(&li->fifo, &src->graph->internal->frame_queues);
190+
ff_framequeue_init(&li->fifo, &fffiltergraph(src->graph)->frame_queues);
191191

192192
return 0;
193193
}
@@ -904,9 +904,9 @@ int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
904904

905905
if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS &&
906906
ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
907-
ctx->graph->internal->thread_execute) {
907+
fffiltergraph(ctx->graph)->thread_execute) {
908908
ctx->thread_type = AVFILTER_THREAD_SLICE;
909-
ctxi->execute = ctx->graph->internal->thread_execute;
909+
ctxi->execute = fffiltergraph(ctx->graph)->thread_execute;
910910
} else {
911911
ctx->thread_type = 0;
912912
}

libavfilter/avfilter_internal.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,20 @@ typedef struct AVFilterCommand {
7878
struct AVFilterCommand *next;
7979
} AVFilterCommand;
8080

81-
struct AVFilterGraphInternal {
81+
typedef struct FFFilterGraph {
82+
/**
83+
* The public AVFilterGraph. See avfilter.h for it.
84+
*/
85+
AVFilterGraph p;
8286
void *thread;
8387
avfilter_execute_func *thread_execute;
8488
FFFrameQueueGlobal frame_queues;
85-
};
89+
} FFFilterGraph;
90+
91+
static inline FFFilterGraph *fffiltergraph(AVFilterGraph *graph)
92+
{
93+
return (FFFilterGraph*)graph;
94+
}
8695

8796
/**
8897
* Update the position of a link in the age heap.
@@ -119,8 +128,8 @@ int ff_filter_activate(AVFilterContext *filter);
119128
int ff_filter_opt_parse(void *logctx, const AVClass *priv_class,
120129
AVDictionary **options, const char *args);
121130

122-
int ff_graph_thread_init(AVFilterGraph *graph);
131+
int ff_graph_thread_init(FFFilterGraph *graph);
123132

124-
void ff_graph_thread_free(AVFilterGraph *graph);
133+
void ff_graph_thread_free(FFFilterGraph *graph);
125134

126135
#endif /* AVFILTER_AVFILTER_INTERNAL_H */

libavfilter/avfiltergraph.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,33 +67,30 @@ static const AVClass filtergraph_class = {
6767
};
6868

6969
#if !HAVE_THREADS
70-
void ff_graph_thread_free(AVFilterGraph *graph)
70+
void ff_graph_thread_free(FFFilterGraph *graph)
7171
{
7272
}
7373

74-
int ff_graph_thread_init(AVFilterGraph *graph)
74+
int ff_graph_thread_init(FFFilterGraph *graph)
7575
{
76-
graph->thread_type = 0;
77-
graph->nb_threads = 1;
76+
graph->p.thread_type = 0;
77+
graph->p.nb_threads = 1;
7878
return 0;
7979
}
8080
#endif
8181

8282
AVFilterGraph *avfilter_graph_alloc(void)
8383
{
84-
AVFilterGraph *ret = av_mallocz(sizeof(*ret));
85-
if (!ret)
86-
return NULL;
84+
FFFilterGraph *graph = av_mallocz(sizeof(*graph));
85+
AVFilterGraph *ret;
8786

88-
ret->internal = av_mallocz(sizeof(*ret->internal));
89-
if (!ret->internal) {
90-
av_freep(&ret);
87+
if (!graph)
9188
return NULL;
92-
}
9389

90+
ret = &graph->p;
9491
ret->av_class = &filtergraph_class;
9592
av_opt_set_defaults(ret);
96-
ff_framequeue_global_init(&ret->internal->frame_queues);
93+
ff_framequeue_global_init(&graph->frame_queues);
9794

9895
return ret;
9996
}
@@ -119,21 +116,21 @@ void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter
119116
void avfilter_graph_free(AVFilterGraph **graphp)
120117
{
121118
AVFilterGraph *graph = *graphp;
119+
FFFilterGraph *graphi = fffiltergraph(graph);
122120

123121
if (!graph)
124122
return;
125123

126124
while (graph->nb_filters)
127125
avfilter_free(graph->filters[0]);
128126

129-
ff_graph_thread_free(graph);
127+
ff_graph_thread_free(graphi);
130128

131129
av_freep(&graph->sink_links);
132130

133131
av_opt_free(graph);
134132

135133
av_freep(&graph->filters);
136-
av_freep(&graph->internal);
137134
av_freep(graphp);
138135
}
139136

@@ -169,12 +166,13 @@ AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
169166
const char *name)
170167
{
171168
AVFilterContext **filters, *s;
169+
FFFilterGraph *graphi = fffiltergraph(graph);
172170

173-
if (graph->thread_type && !graph->internal->thread_execute) {
171+
if (graph->thread_type && !graphi->thread_execute) {
174172
if (graph->execute) {
175-
graph->internal->thread_execute = graph->execute;
173+
graphi->thread_execute = graph->execute;
176174
} else {
177-
int ret = ff_graph_thread_init(graph);
175+
int ret = ff_graph_thread_init(graphi);
178176
if (ret < 0) {
179177
av_log(graph, AV_LOG_ERROR, "Error initializing threading: %s.\n", av_err2str(ret));
180178
return NULL;

libavfilter/pthread.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static void slice_thread_uninit(ThreadContext *c)
5858
static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func,
5959
void *arg, int *ret, int nb_jobs)
6060
{
61-
ThreadContext *c = ctx->graph->internal->thread;
61+
ThreadContext *c = fffiltergraph(ctx->graph)->thread;
6262

6363
if (nb_jobs <= 0)
6464
return 0;
@@ -79,36 +79,37 @@ static int thread_init_internal(ThreadContext *c, int nb_threads)
7979
return FFMAX(nb_threads, 1);
8080
}
8181

82-
int ff_graph_thread_init(AVFilterGraph *graph)
82+
int ff_graph_thread_init(FFFilterGraph *graphi)
8383
{
84+
AVFilterGraph *graph = &graphi->p;
8485
int ret;
8586

8687
if (graph->nb_threads == 1) {
8788
graph->thread_type = 0;
8889
return 0;
8990
}
9091

91-
graph->internal->thread = av_mallocz(sizeof(ThreadContext));
92-
if (!graph->internal->thread)
92+
graphi->thread = av_mallocz(sizeof(ThreadContext));
93+
if (!graphi->thread)
9394
return AVERROR(ENOMEM);
9495

95-
ret = thread_init_internal(graph->internal->thread, graph->nb_threads);
96+
ret = thread_init_internal(graphi->thread, graph->nb_threads);
9697
if (ret <= 1) {
97-
av_freep(&graph->internal->thread);
98+
av_freep(&graphi->thread);
9899
graph->thread_type = 0;
99100
graph->nb_threads = 1;
100101
return (ret < 0) ? ret : 0;
101102
}
102103
graph->nb_threads = ret;
103104

104-
graph->internal->thread_execute = thread_execute;
105+
graphi->thread_execute = thread_execute;
105106

106107
return 0;
107108
}
108109

109-
void ff_graph_thread_free(AVFilterGraph *graph)
110+
void ff_graph_thread_free(FFFilterGraph *graph)
110111
{
111-
if (graph->internal->thread)
112-
slice_thread_uninit(graph->internal->thread);
113-
av_freep(&graph->internal->thread);
112+
if (graph->thread)
113+
slice_thread_uninit(graph->thread);
114+
av_freep(&graph->thread);
114115
}

0 commit comments

Comments
 (0)