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 "libavutil/pixdesc.h"
25 #include "libavutil/rational.h"
26 #include "libavutil/audioconvert.h"
27 #include "libavutil/avassert.h"
28 #include "libavutil/avstring.h"
34 char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
36 snprintf(buf, buf_size, "%s%s%s%s%s%s",
37 perms & AV_PERM_READ ? "r" : "",
38 perms & AV_PERM_WRITE ? "w" : "",
39 perms & AV_PERM_PRESERVE ? "p" : "",
40 perms & AV_PERM_REUSE ? "u" : "",
41 perms & AV_PERM_REUSE2 ? "U" : "",
42 perms & AV_PERM_NEG_LINESIZES ? "n" : "");
46 void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
48 av_unused char buf[16];
50 "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
51 ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
52 ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
56 av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
57 ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
58 ref->video->w, ref->video->h,
59 !ref->video->interlaced ? 'P' : /* Progressive */
60 ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
61 ref->video->key_frame,
62 av_get_picture_type_char(ref->video->pict_type));
65 av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d",
66 ref->audio->channel_layout,
67 ref->audio->nb_samples,
68 ref->audio->sample_rate);
71 av_dlog(ctx, "]%s", end ? "\n" : "");
74 unsigned avfilter_version(void) {
75 av_assert0(LIBAVFILTER_VERSION_MICRO >= 100);
76 return LIBAVFILTER_VERSION_INT;
79 const char *avfilter_configuration(void)
81 return FFMPEG_CONFIGURATION;
84 const char *avfilter_license(void)
86 #define LICENSE_PREFIX "libavfilter license: "
87 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
90 void ff_command_queue_pop(AVFilterContext *filter)
92 AVFilterCommand *c= filter->command_queue;
94 av_freep(&c->command);
95 filter->command_queue= c->next;
99 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
100 AVFilterPad **pads, AVFilterLink ***links,
105 idx = FFMIN(idx, *count);
107 *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
108 *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
109 memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
110 memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
111 memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
112 (*links)[idx] = NULL;
115 for (i = idx+1; i < *count; i++)
117 (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
120 int avfilter_link(AVFilterContext *src, unsigned srcpad,
121 AVFilterContext *dst, unsigned dstpad)
125 if (src->output_count <= srcpad || dst->input_count <= dstpad ||
126 src->outputs[srcpad] || dst->inputs[dstpad])
129 if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
130 av_log(src, AV_LOG_ERROR,
131 "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
132 src->name, srcpad, dst->name, dstpad);
133 return AVERROR(EINVAL);
136 src->outputs[srcpad] =
137 dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
141 link->srcpad = &src->output_pads[srcpad];
142 link->dstpad = &dst->input_pads[dstpad];
143 link->type = src->output_pads[srcpad].type;
144 assert(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
150 void avfilter_link_free(AVFilterLink **link)
156 ff_free_pool((*link)->pool);
161 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
162 unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
165 unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
167 av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
168 "between the filter '%s' and the filter '%s'\n",
169 filt->name, link->src->name, link->dst->name);
171 link->dst->inputs[dstpad_idx] = NULL;
172 if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
173 /* failed to link output filter to new filter */
174 link->dst->inputs[dstpad_idx] = link;
178 /* re-hookup the link to the new destination filter we inserted */
180 link->dstpad = &filt->input_pads[filt_srcpad_idx];
181 filt->inputs[filt_srcpad_idx] = link;
183 /* if any information on supported media formats already exists on the
184 * link, we need to preserve that */
185 if (link->out_formats)
186 avfilter_formats_changeref(&link->out_formats,
187 &filt->outputs[filt_dstpad_idx]->out_formats);
188 if (link->out_channel_layouts)
189 ff_channel_layouts_changeref(&link->out_channel_layouts,
190 &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
191 if (link->out_samplerates)
192 avfilter_formats_changeref(&link->out_samplerates,
193 &filt->outputs[filt_dstpad_idx]->out_samplerates);
198 int avfilter_config_links(AVFilterContext *filter)
200 int (*config_link)(AVFilterLink *);
204 for (i = 0; i < filter->input_count; i ++) {
205 AVFilterLink *link = filter->inputs[i];
206 AVFilterLink *inlink = link->src->input_count ?
207 link->src->inputs[0] : NULL;
211 link->current_pts = AV_NOPTS_VALUE;
213 switch (link->init_state) {
216 case AVLINK_STARTINIT:
217 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
220 link->init_state = AVLINK_STARTINIT;
222 if ((ret = avfilter_config_links(link->src)) < 0)
225 if (!(config_link = link->srcpad->config_props)) {
226 if (link->src->input_count != 1) {
227 av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
228 "with more than one input "
229 "must set config_props() "
230 "callbacks on all outputs\n");
231 return AVERROR(EINVAL);
233 } else if ((ret = config_link(link)) < 0)
236 switch (link->type) {
237 case AVMEDIA_TYPE_VIDEO:
238 if (!link->time_base.num && !link->time_base.den)
239 link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
241 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
242 link->sample_aspect_ratio = inlink ?
243 inlink->sample_aspect_ratio : (AVRational){1,1};
250 } else if (!link->w || !link->h) {
251 av_log(link->src, AV_LOG_ERROR,
252 "Video source filters must set their output link's "
253 "width and height\n");
254 return AVERROR(EINVAL);
258 case AVMEDIA_TYPE_AUDIO:
260 if (!link->sample_rate)
261 link->sample_rate = inlink->sample_rate;
262 if (!link->time_base.num && !link->time_base.den)
263 link->time_base = inlink->time_base;
264 } else if (!link->sample_rate) {
265 av_log(link->src, AV_LOG_ERROR,
266 "Audio source filters must set their output link's "
268 return AVERROR(EINVAL);
271 if (!link->time_base.num && !link->time_base.den)
272 link->time_base = (AVRational) {1, link->sample_rate};
275 if ((config_link = link->dstpad->config_props))
276 if ((ret = config_link(link)) < 0)
279 link->init_state = AVLINK_INIT;
286 void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
288 if (link->type == AVMEDIA_TYPE_VIDEO) {
290 "link[%p s:%dx%d fmt:%s %s->%s]%s",
291 link, link->w, link->h,
292 av_pix_fmt_descriptors[link->format].name,
293 link->src ? link->src->filter->name : "",
294 link->dst ? link->dst->filter->name : "",
298 av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
301 "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
302 link, (int)link->sample_rate, buf,
303 av_get_sample_fmt_name(link->format),
304 link->src ? link->src->filter->name : "",
305 link->dst ? link->dst->filter->name : "",
310 int avfilter_request_frame(AVFilterLink *link)
312 FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
314 if (link->srcpad->request_frame)
315 return link->srcpad->request_frame(link);
316 else if (link->src->inputs[0])
317 return avfilter_request_frame(link->src->inputs[0]);
321 int avfilter_poll_frame(AVFilterLink *link)
323 int i, min = INT_MAX;
325 if (link->srcpad->poll_frame)
326 return link->srcpad->poll_frame(link);
328 for (i = 0; i < link->src->input_count; i++) {
330 if (!link->src->inputs[i])
332 val = avfilter_poll_frame(link->src->inputs[i]);
333 min = FFMIN(min, val);
339 void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
341 if (pts == AV_NOPTS_VALUE)
343 link->current_pts = pts; /* TODO use duration */
344 if (link->graph && link->age_index >= 0)
345 ff_avfilter_graph_update_heap(link->graph, link);
348 int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
350 if(!strcmp(cmd, "ping")){
351 av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
353 }else if(filter->filter->process_command) {
354 return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
356 return AVERROR(ENOSYS);
359 #define MAX_REGISTERED_AVFILTERS_NB 128
361 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
363 static int next_registered_avfilter_idx = 0;
365 AVFilter *avfilter_get_by_name(const char *name)
369 for (i = 0; registered_avfilters[i]; i++)
370 if (!strcmp(registered_avfilters[i]->name, name))
371 return registered_avfilters[i];
376 int avfilter_register(AVFilter *filter)
378 if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
379 av_log(NULL, AV_LOG_ERROR,
380 "Maximum number of registered filters %d reached, "
381 "impossible to register filter with name '%s'\n",
382 MAX_REGISTERED_AVFILTERS_NB, filter->name);
383 return AVERROR(ENOMEM);
386 registered_avfilters[next_registered_avfilter_idx++] = filter;
390 AVFilter **av_filter_next(AVFilter **filter)
392 return filter ? ++filter : ®istered_avfilters[0];
395 void avfilter_uninit(void)
397 memset(registered_avfilters, 0, sizeof(registered_avfilters));
398 next_registered_avfilter_idx = 0;
401 static int pad_count(const AVFilterPad *pads)
405 for(count = 0; pads->name; count ++) pads ++;
409 const char *avfilter_default_filter_name(void *filter_ctx)
411 AVFilterContext *ctx = filter_ctx;
412 return ctx->name ? ctx->name : ctx->filter->name;
415 static const AVClass avfilter_class = {
416 .class_name = "AVFilter",
417 .item_name = avfilter_default_filter_name,
418 .version = LIBAVUTIL_VERSION_INT,
419 .category = AV_CLASS_CATEGORY_FILTER,
422 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
424 AVFilterContext *ret;
428 return AVERROR(EINVAL);
430 ret = av_mallocz(sizeof(AVFilterContext));
432 return AVERROR(ENOMEM);
434 ret->av_class = &avfilter_class;
435 ret->filter = filter;
436 ret->name = inst_name ? av_strdup(inst_name) : NULL;
437 if (filter->priv_size) {
438 ret->priv = av_mallocz(filter->priv_size);
443 ret->input_count = pad_count(filter->inputs);
444 if (ret->input_count) {
445 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
446 if (!ret->input_pads)
448 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
449 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
454 ret->output_count = pad_count(filter->outputs);
455 if (ret->output_count) {
456 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
457 if (!ret->output_pads)
459 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
460 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
469 av_freep(&ret->inputs);
470 av_freep(&ret->input_pads);
471 ret->input_count = 0;
472 av_freep(&ret->outputs);
473 av_freep(&ret->output_pads);
474 ret->output_count = 0;
475 av_freep(&ret->priv);
477 return AVERROR(ENOMEM);
480 void avfilter_free(AVFilterContext *filter)
488 if (filter->filter->uninit)
489 filter->filter->uninit(filter);
491 for (i = 0; i < filter->input_count; i++) {
492 if ((link = filter->inputs[i])) {
494 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
495 avfilter_formats_unref(&link->in_formats);
496 avfilter_formats_unref(&link->out_formats);
497 avfilter_formats_unref(&link->in_samplerates);
498 avfilter_formats_unref(&link->out_samplerates);
499 ff_channel_layouts_unref(&link->in_channel_layouts);
500 ff_channel_layouts_unref(&link->out_channel_layouts);
502 avfilter_link_free(&link);
504 for (i = 0; i < filter->output_count; i++) {
505 if ((link = filter->outputs[i])) {
507 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
508 avfilter_formats_unref(&link->in_formats);
509 avfilter_formats_unref(&link->out_formats);
510 avfilter_formats_unref(&link->in_samplerates);
511 avfilter_formats_unref(&link->out_samplerates);
512 ff_channel_layouts_unref(&link->in_channel_layouts);
513 ff_channel_layouts_unref(&link->out_channel_layouts);
515 avfilter_link_free(&link);
518 av_freep(&filter->name);
519 av_freep(&filter->input_pads);
520 av_freep(&filter->output_pads);
521 av_freep(&filter->inputs);
522 av_freep(&filter->outputs);
523 av_freep(&filter->priv);
524 while(filter->command_queue){
525 ff_command_queue_pop(filter);
530 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
534 if (filter->filter->init)
535 ret = filter->filter->init(filter, args, opaque);