3 * copyright (c) 2007 Bobby Bingham
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavcodec/imgconvert.h"
27 unsigned avfilter_version(void) {
28 return LIBAVFILTER_VERSION_INT
;
31 /** helper macros to get the in/out pad on the dst/src filter */
32 #define link_dpad(link) link->dst-> input_pads[link->dstpad]
33 #define link_spad(link) link->src->output_pads[link->srcpad]
35 AVFilterPicRef
*avfilter_ref_pic(AVFilterPicRef
*ref
, int pmask
)
37 AVFilterPicRef
*ret
= av_malloc(sizeof(AVFilterPicRef
));
40 ret
->pic
->refcount
++;
44 void avfilter_unref_pic(AVFilterPicRef
*ref
)
46 if(!(--ref
->pic
->refcount
))
47 ref
->pic
->free(ref
->pic
);
51 void avfilter_insert_pad(unsigned idx
, unsigned *count
, size_t padidx_off
,
52 AVFilterPad
**pads
, AVFilterLink
***links
,
57 idx
= FFMIN(idx
, *count
);
59 *pads
= av_realloc(*pads
, sizeof(AVFilterPad
) * (*count
+ 1));
60 *links
= av_realloc(*links
, sizeof(AVFilterLink
*) * (*count
+ 1));
61 memmove(*pads
+idx
+1, *pads
+idx
, sizeof(AVFilterPad
) * (*count
-idx
));
62 memmove(*links
+idx
+1, *links
+idx
, sizeof(AVFilterLink
*) * (*count
-idx
));
63 memcpy(*pads
+idx
, newpad
, sizeof(AVFilterPad
));
67 for(i
= idx
+1; i
< *count
; i
++)
69 (*(unsigned *)((uint8_t *) *links
[i
] + padidx_off
)) ++;
72 int avfilter_link(AVFilterContext
*src
, unsigned srcpad
,
73 AVFilterContext
*dst
, unsigned dstpad
)
77 if(src
->output_count
<= srcpad
|| dst
->input_count
<= dstpad
||
78 src
->outputs
[srcpad
] || dst
->inputs
[dstpad
])
81 src
->outputs
[srcpad
] =
82 dst
-> inputs
[dstpad
] = link
= av_mallocz(sizeof(AVFilterLink
));
86 link
->srcpad
= srcpad
;
87 link
->dstpad
= dstpad
;
88 link
->format
= PIX_FMT_NONE
;
93 int avfilter_insert_filter(AVFilterLink
*link
, AVFilterContext
*filt
,
94 unsigned in
, unsigned out
)
96 av_log(link
->dst
, AV_LOG_INFO
, "auto-inserting filter '%s'\n",
99 link
->dst
->inputs
[link
->dstpad
] = NULL
;
100 if(avfilter_link(filt
, out
, link
->dst
, link
->dstpad
)) {
101 /* failed to link output filter to new filter */
102 link
->dst
->inputs
[link
->dstpad
] = link
;
106 /* re-hookup the link to the new destination filter we inserted */
109 filt
->inputs
[in
] = link
;
111 /* if any information on supported colorspaces already exists on the
112 * link, we need to preserve that */
113 if(link
->out_formats
)
114 avfilter_formats_changeref(&link
->out_formats
,
115 &filt
->outputs
[out
]->out_formats
);
120 int avfilter_config_links(AVFilterContext
*filter
)
122 int (*config_link
)(AVFilterLink
*);
125 for(i
= 0; i
< filter
->input_count
; i
++) {
126 AVFilterLink
*link
= filter
->inputs
[i
];
130 switch(link
->init_state
) {
133 case AVLINK_STARTINIT
:
134 av_log(filter
, AV_LOG_INFO
, "circular filter chain detected\n");
137 link
->init_state
= AVLINK_STARTINIT
;
139 if(avfilter_config_links(link
->src
))
142 if(!(config_link
= link_spad(link
).config_props
))
143 config_link
= avfilter_default_config_output_link
;
144 if(config_link(link
))
147 if((config_link
= link_dpad(link
).config_props
))
148 if(config_link(link
))
151 link
->init_state
= AVLINK_INIT
;
158 static void dprintf_picref(void *ctx
, AVFilterPicRef
*picref
, int end
)
161 "picref[%p data[%p, %p, %p, %p] linesize[%d, %d, %d, %d] pts:%"PRId64
" s:%dx%d]%s",
163 picref
->data
[0], picref
->data
[1], picref
->data
[2], picref
->data
[3],
164 picref
->linesize
[0], picref
->linesize
[1], picref
->linesize
[2], picref
->linesize
[3],
165 picref
->pts
, picref
->w
, picref
->h
,
169 static void dprintf_link(void *ctx
, AVFilterLink
*link
, int end
)
172 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
173 link
, link
->w
, link
->h
,
174 avcodec_get_pix_fmt_name(link
->format
),
175 link
->src
? link
->src
->filter
->name
: "",
176 link
->dst
? link
->dst
->filter
->name
: "",
180 #define DPRINTF_START(ctx, func) dprintf(NULL, "%-16s: ", #func)
182 AVFilterPicRef
*avfilter_get_video_buffer(AVFilterLink
*link
, int perms
, int w
, int h
)
184 AVFilterPicRef
*ret
= NULL
;
186 DPRINTF_START(NULL
, get_video_buffer
); dprintf_link(NULL
, link
, 0); dprintf(NULL
, " perms:%d w:%d h:%d\n", perms
, w
, h
);
188 if(link_dpad(link
).get_video_buffer
)
189 ret
= link_dpad(link
).get_video_buffer(link
, perms
, w
, h
);
192 ret
= avfilter_default_get_video_buffer(link
, perms
, w
, h
);
194 DPRINTF_START(NULL
, get_video_buffer
); dprintf_link(NULL
, link
, 0); dprintf(NULL
, " returning "); dprintf_picref(NULL
, ret
, 1);
199 int avfilter_request_frame(AVFilterLink
*link
)
201 DPRINTF_START(NULL
, request_frame
); dprintf_link(NULL
, link
, 1);
203 if(link_spad(link
).request_frame
)
204 return link_spad(link
).request_frame(link
);
205 else if(link
->src
->inputs
[0])
206 return avfilter_request_frame(link
->src
->inputs
[0]);
210 int avfilter_poll_frame(AVFilterLink
*link
)
214 if(link_spad(link
).poll_frame
)
215 return link_spad(link
).poll_frame(link
);
217 for (i
=0; i
<link
->src
->input_count
; i
++) {
219 if(!link
->src
->inputs
[i
])
221 val
= avfilter_poll_frame(link
->src
->inputs
[i
]);
222 min
= FFMIN(min
, val
);
228 /* XXX: should we do the duplicating of the picture ref here, instead of
229 * forcing the source filter to do it? */
230 void avfilter_start_frame(AVFilterLink
*link
, AVFilterPicRef
*picref
)
232 void (*start_frame
)(AVFilterLink
*, AVFilterPicRef
*);
233 AVFilterPad
*dst
= &link_dpad(link
);
235 DPRINTF_START(NULL
, start_frame
); dprintf_link(NULL
, link
, 0); dprintf(NULL
, " "); dprintf_picref(NULL
, picref
, 1);
237 if(!(start_frame
= dst
->start_frame
))
238 start_frame
= avfilter_default_start_frame
;
240 /* prepare to copy the picture if it has insufficient permissions */
241 if((dst
->min_perms
& picref
->perms
) != dst
->min_perms
||
242 dst
->rej_perms
& picref
->perms
) {
244 av_log(link->dst, AV_LOG_INFO,
245 "frame copy needed (have perms %x, need %x, reject %x)\n",
247 link_dpad(link).min_perms, link_dpad(link).rej_perms);
250 link
->cur_pic
= avfilter_default_get_video_buffer(link
, dst
->min_perms
, link
->w
, link
->h
);
251 link
->srcpic
= picref
;
252 link
->cur_pic
->pts
= link
->srcpic
->pts
;
253 link
->cur_pic
->pixel_aspect
= link
->srcpic
->pixel_aspect
;
256 link
->cur_pic
= picref
;
258 start_frame(link
, link
->cur_pic
);
261 void avfilter_end_frame(AVFilterLink
*link
)
263 void (*end_frame
)(AVFilterLink
*);
265 if(!(end_frame
= link_dpad(link
).end_frame
))
266 end_frame
= avfilter_default_end_frame
;
270 /* unreference the source picture if we're feeding the destination filter
271 * a copied version dues to permission issues */
273 avfilter_unref_pic(link
->srcpic
);
279 void avfilter_draw_slice(AVFilterLink
*link
, int y
, int h
)
281 uint8_t *src
[4], *dst
[4];
282 int i
, j
, hsub
, vsub
;
283 void (*draw_slice
)(AVFilterLink
*, int, int);
285 DPRINTF_START(NULL
, draw_slice
); dprintf_link(NULL
, link
, 0); dprintf(NULL
, " y:%d h:%d\n", y
, h
);
287 /* copy the slice if needed for permission reasons */
289 avcodec_get_chroma_sub_sample(link
->format
, &hsub
, &vsub
);
291 for(i
= 0; i
< 4; i
++) {
292 if(link
->srcpic
->data
[i
]) {
293 src
[i
] = link
->srcpic
-> data
[i
] +
294 (y
>> (i
==0 ? 0 : vsub
)) * link
->srcpic
-> linesize
[i
];
295 dst
[i
] = link
->cur_pic
->data
[i
] +
296 (y
>> (i
==0 ? 0 : vsub
)) * link
->cur_pic
->linesize
[i
];
298 src
[i
] = dst
[i
] = NULL
;
301 for(i
= 0; i
< 4; i
++) {
303 ff_get_plane_bytewidth(link
->format
, link
->cur_pic
->w
, i
);
305 if(!src
[i
]) continue;
307 for(j
= 0; j
< h
>> (i
==0 ? 0 : vsub
); j
++) {
308 memcpy(dst
[i
], src
[i
], planew
);
309 src
[i
] += link
->srcpic
->linesize
[i
];
310 dst
[i
] += link
->cur_pic
->linesize
[i
];
315 if(!(draw_slice
= link_dpad(link
).draw_slice
))
316 draw_slice
= avfilter_default_draw_slice
;
317 draw_slice(link
, y
, h
);
320 AVFilter
*first_avfilter
= NULL
;
322 AVFilter
*avfilter_get_by_name(const char *name
)
326 for (filter
= first_avfilter
; filter
; filter
= filter
->next
)
327 if (!strcmp(filter
->name
, name
))
333 void avfilter_register(AVFilter
*filter
)
344 void avfilter_uninit(void)
346 first_avfilter
= NULL
;
349 static int pad_count(const AVFilterPad
*pads
)
353 for(count
= 0; pads
->name
; count
++) pads
++;
357 static const char *filter_name(void *p
)
359 AVFilterContext
*filter
= p
;
360 return filter
->filter
->name
;
363 static const AVClass avfilter_class
= {
368 AVFilterContext
*avfilter_open(AVFilter
*filter
, const char *inst_name
)
370 AVFilterContext
*ret
;
375 ret
= av_mallocz(sizeof(AVFilterContext
));
377 ret
->av_class
= &avfilter_class
;
378 ret
->filter
= filter
;
379 ret
->name
= inst_name
? av_strdup(inst_name
) : NULL
;
380 ret
->priv
= av_mallocz(filter
->priv_size
);
382 ret
->input_count
= pad_count(filter
->inputs
);
383 if (ret
->input_count
) {
384 ret
->input_pads
= av_malloc(sizeof(AVFilterPad
) * ret
->input_count
);
385 memcpy(ret
->input_pads
, filter
->inputs
, sizeof(AVFilterPad
) * ret
->input_count
);
386 ret
->inputs
= av_mallocz(sizeof(AVFilterLink
*) * ret
->input_count
);
389 ret
->output_count
= pad_count(filter
->outputs
);
390 if (ret
->output_count
) {
391 ret
->output_pads
= av_malloc(sizeof(AVFilterPad
) * ret
->output_count
);
392 memcpy(ret
->output_pads
, filter
->outputs
, sizeof(AVFilterPad
) * ret
->output_count
);
393 ret
->outputs
= av_mallocz(sizeof(AVFilterLink
*) * ret
->output_count
);
399 void avfilter_destroy(AVFilterContext
*filter
)
403 if(filter
->filter
->uninit
)
404 filter
->filter
->uninit(filter
);
406 for(i
= 0; i
< filter
->input_count
; i
++) {
407 if(filter
->inputs
[i
])
408 filter
->inputs
[i
]->src
->outputs
[filter
->inputs
[i
]->srcpad
] = NULL
;
409 av_freep(&filter
->inputs
[i
]);
411 for(i
= 0; i
< filter
->output_count
; i
++) {
412 if(filter
->outputs
[i
])
413 filter
->outputs
[i
]->dst
->inputs
[filter
->outputs
[i
]->dstpad
] = NULL
;
414 av_freep(&filter
->outputs
[i
]);
417 av_freep(&filter
->name
);
418 av_freep(&filter
->input_pads
);
419 av_freep(&filter
->output_pads
);
420 av_freep(&filter
->inputs
);
421 av_freep(&filter
->outputs
);
422 av_freep(&filter
->priv
);
426 int avfilter_init_filter(AVFilterContext
*filter
, const char *args
, void *opaque
)
430 if(filter
->filter
->init
)
431 ret
= filter
->filter
->init(filter
, args
, opaque
);