00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/avassert.h"
00023 #include "libavutil/avstring.h"
00024 #include "libavutil/channel_layout.h"
00025 #include "libavutil/common.h"
00026 #include "libavutil/imgutils.h"
00027 #include "libavutil/pixdesc.h"
00028 #include "libavutil/rational.h"
00029 #include "libavutil/samplefmt.h"
00030
00031 #include "audio.h"
00032 #include "avfilter.h"
00033 #include "formats.h"
00034 #include "internal.h"
00035 #include "audio.h"
00036
00037 char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
00038 {
00039 snprintf(buf, buf_size, "%s%s%s%s%s%s",
00040 perms & AV_PERM_READ ? "r" : "",
00041 perms & AV_PERM_WRITE ? "w" : "",
00042 perms & AV_PERM_PRESERVE ? "p" : "",
00043 perms & AV_PERM_REUSE ? "u" : "",
00044 perms & AV_PERM_REUSE2 ? "U" : "",
00045 perms & AV_PERM_NEG_LINESIZES ? "n" : "");
00046 return buf;
00047 }
00048
00049 void ff_tlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
00050 {
00051 av_unused char buf[16];
00052 ff_tlog(ctx,
00053 "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
00054 ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
00055 ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
00056 ref->pts, ref->pos);
00057
00058 if (ref->video) {
00059 ff_tlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
00060 ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
00061 ref->video->w, ref->video->h,
00062 !ref->video->interlaced ? 'P' :
00063 ref->video->top_field_first ? 'T' : 'B',
00064 ref->video->key_frame,
00065 av_get_picture_type_char(ref->video->pict_type));
00066 }
00067 if (ref->audio) {
00068 ff_tlog(ctx, " cl:%"PRId64"d n:%d r:%d",
00069 ref->audio->channel_layout,
00070 ref->audio->nb_samples,
00071 ref->audio->sample_rate);
00072 }
00073
00074 ff_tlog(ctx, "]%s", end ? "\n" : "");
00075 }
00076
00077 unsigned avfilter_version(void) {
00078 av_assert0(LIBAVFILTER_VERSION_MICRO >= 100);
00079 return LIBAVFILTER_VERSION_INT;
00080 }
00081
00082 const char *avfilter_configuration(void)
00083 {
00084 return FFMPEG_CONFIGURATION;
00085 }
00086
00087 const char *avfilter_license(void)
00088 {
00089 #define LICENSE_PREFIX "libavfilter license: "
00090 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
00091 }
00092
00093 void ff_command_queue_pop(AVFilterContext *filter)
00094 {
00095 AVFilterCommand *c= filter->command_queue;
00096 av_freep(&c->arg);
00097 av_freep(&c->command);
00098 filter->command_queue= c->next;
00099 av_free(c);
00100 }
00101
00102 void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
00103 AVFilterPad **pads, AVFilterLink ***links,
00104 AVFilterPad *newpad)
00105 {
00106 unsigned i;
00107
00108 idx = FFMIN(idx, *count);
00109
00110 *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
00111 *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
00112 memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
00113 memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
00114 memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
00115 (*links)[idx] = NULL;
00116
00117 (*count)++;
00118 for (i = idx+1; i < *count; i++)
00119 if (*links[i])
00120 (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
00121 }
00122
00123 int avfilter_link(AVFilterContext *src, unsigned srcpad,
00124 AVFilterContext *dst, unsigned dstpad)
00125 {
00126 AVFilterLink *link;
00127
00128 if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
00129 src->outputs[srcpad] || dst->inputs[dstpad])
00130 return -1;
00131
00132 if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
00133 av_log(src, AV_LOG_ERROR,
00134 "Media type mismatch between the '%s' filter output pad %d (%s) and the '%s' filter input pad %d (%s)\n",
00135 src->name, srcpad, (char *)av_x_if_null(av_get_media_type_string(src->output_pads[srcpad].type), "?"),
00136 dst->name, dstpad, (char *)av_x_if_null(av_get_media_type_string(dst-> input_pads[dstpad].type), "?"));
00137 return AVERROR(EINVAL);
00138 }
00139
00140 src->outputs[srcpad] =
00141 dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
00142
00143 link->src = src;
00144 link->dst = dst;
00145 link->srcpad = &src->output_pads[srcpad];
00146 link->dstpad = &dst->input_pads[dstpad];
00147 link->type = src->output_pads[srcpad].type;
00148 av_assert0(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
00149 link->format = -1;
00150
00151 return 0;
00152 }
00153
00154 void avfilter_link_free(AVFilterLink **link)
00155 {
00156 if (!*link)
00157 return;
00158
00159 if ((*link)->pool)
00160 ff_free_pool((*link)->pool);
00161
00162 avfilter_unref_bufferp(&(*link)->partial_buf);
00163
00164 av_freep(link);
00165 }
00166
00167 void avfilter_link_set_closed(AVFilterLink *link, int closed)
00168 {
00169 link->closed = closed;
00170 }
00171
00172 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
00173 unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
00174 {
00175 int ret;
00176 unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
00177
00178 av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
00179 "between the filter '%s' and the filter '%s'\n",
00180 filt->name, link->src->name, link->dst->name);
00181
00182 link->dst->inputs[dstpad_idx] = NULL;
00183 if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
00184
00185 link->dst->inputs[dstpad_idx] = link;
00186 return ret;
00187 }
00188
00189
00190 link->dst = filt;
00191 link->dstpad = &filt->input_pads[filt_srcpad_idx];
00192 filt->inputs[filt_srcpad_idx] = link;
00193
00194
00195
00196 if (link->out_formats)
00197 ff_formats_changeref(&link->out_formats,
00198 &filt->outputs[filt_dstpad_idx]->out_formats);
00199
00200 if (link->out_samplerates)
00201 ff_formats_changeref(&link->out_samplerates,
00202 &filt->outputs[filt_dstpad_idx]->out_samplerates);
00203 if (link->out_channel_layouts)
00204 ff_channel_layouts_changeref(&link->out_channel_layouts,
00205 &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
00206
00207 return 0;
00208 }
00209
00210 int avfilter_config_links(AVFilterContext *filter)
00211 {
00212 int (*config_link)(AVFilterLink *);
00213 unsigned i;
00214 int ret;
00215
00216 for (i = 0; i < filter->nb_inputs; i ++) {
00217 AVFilterLink *link = filter->inputs[i];
00218 AVFilterLink *inlink = link->src->nb_inputs ?
00219 link->src->inputs[0] : NULL;
00220
00221 if (!link) continue;
00222
00223 link->current_pts = AV_NOPTS_VALUE;
00224
00225 switch (link->init_state) {
00226 case AVLINK_INIT:
00227 continue;
00228 case AVLINK_STARTINIT:
00229 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
00230 return 0;
00231 case AVLINK_UNINIT:
00232 link->init_state = AVLINK_STARTINIT;
00233
00234 if ((ret = avfilter_config_links(link->src)) < 0)
00235 return ret;
00236
00237 if (!(config_link = link->srcpad->config_props)) {
00238 if (link->src->nb_inputs != 1) {
00239 av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
00240 "with more than one input "
00241 "must set config_props() "
00242 "callbacks on all outputs\n");
00243 return AVERROR(EINVAL);
00244 }
00245 } else if ((ret = config_link(link)) < 0) {
00246 av_log(link->src, AV_LOG_ERROR,
00247 "Failed to configure output pad on %s\n",
00248 link->src->name);
00249 return ret;
00250 }
00251
00252 switch (link->type) {
00253 case AVMEDIA_TYPE_VIDEO:
00254 if (!link->time_base.num && !link->time_base.den)
00255 link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
00256
00257 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
00258 link->sample_aspect_ratio = inlink ?
00259 inlink->sample_aspect_ratio : (AVRational){1,1};
00260
00261 if (inlink && !link->frame_rate.num && !link->frame_rate.den)
00262 link->frame_rate = inlink->frame_rate;
00263
00264 if (inlink) {
00265 if (!link->w)
00266 link->w = inlink->w;
00267 if (!link->h)
00268 link->h = inlink->h;
00269 } else if (!link->w || !link->h) {
00270 av_log(link->src, AV_LOG_ERROR,
00271 "Video source filters must set their output link's "
00272 "width and height\n");
00273 return AVERROR(EINVAL);
00274 }
00275 break;
00276
00277 case AVMEDIA_TYPE_AUDIO:
00278 if (inlink) {
00279 if (!link->sample_rate)
00280 link->sample_rate = inlink->sample_rate;
00281 if (!link->time_base.num && !link->time_base.den)
00282 link->time_base = inlink->time_base;
00283 if (!link->channel_layout)
00284 link->channel_layout = inlink->channel_layout;
00285 } else if (!link->sample_rate) {
00286 av_log(link->src, AV_LOG_ERROR,
00287 "Audio source filters must set their output link's "
00288 "sample_rate\n");
00289 return AVERROR(EINVAL);
00290 }
00291
00292 if (!link->time_base.num && !link->time_base.den)
00293 link->time_base = (AVRational) {1, link->sample_rate};
00294 }
00295
00296 if ((config_link = link->dstpad->config_props))
00297 if ((ret = config_link(link)) < 0) {
00298 av_log(link->src, AV_LOG_ERROR,
00299 "Failed to configure input pad on %s\n",
00300 link->dst->name);
00301 return ret;
00302 }
00303
00304 link->init_state = AVLINK_INIT;
00305 }
00306 }
00307
00308 return 0;
00309 }
00310
00311 void ff_tlog_link(void *ctx, AVFilterLink *link, int end)
00312 {
00313 if (link->type == AVMEDIA_TYPE_VIDEO) {
00314 ff_tlog(ctx,
00315 "link[%p s:%dx%d fmt:%s %s->%s]%s",
00316 link, link->w, link->h,
00317 av_get_pix_fmt_name(link->format),
00318 link->src ? link->src->filter->name : "",
00319 link->dst ? link->dst->filter->name : "",
00320 end ? "\n" : "");
00321 } else {
00322 char buf[128];
00323 av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
00324
00325 ff_tlog(ctx,
00326 "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
00327 link, (int)link->sample_rate, buf,
00328 av_get_sample_fmt_name(link->format),
00329 link->src ? link->src->filter->name : "",
00330 link->dst ? link->dst->filter->name : "",
00331 end ? "\n" : "");
00332 }
00333 }
00334
00335 int ff_request_frame(AVFilterLink *link)
00336 {
00337 int ret = -1;
00338 FF_TPRINTF_START(NULL, request_frame); ff_tlog_link(NULL, link, 1);
00339
00340 if (link->closed)
00341 return AVERROR_EOF;
00342 if (link->srcpad->request_frame)
00343 ret = link->srcpad->request_frame(link);
00344 else if (link->src->inputs[0])
00345 ret = ff_request_frame(link->src->inputs[0]);
00346 if (ret == AVERROR_EOF && link->partial_buf) {
00347 AVFilterBufferRef *pbuf = link->partial_buf;
00348 link->partial_buf = NULL;
00349 ff_filter_samples_framed(link, pbuf);
00350 return 0;
00351 }
00352 if (ret == AVERROR_EOF)
00353 link->closed = 1;
00354 return ret;
00355 }
00356
00357 int ff_poll_frame(AVFilterLink *link)
00358 {
00359 int i, min = INT_MAX;
00360
00361 if (link->srcpad->poll_frame)
00362 return link->srcpad->poll_frame(link);
00363
00364 for (i = 0; i < link->src->nb_inputs; i++) {
00365 int val;
00366 if (!link->src->inputs[i])
00367 return -1;
00368 val = ff_poll_frame(link->src->inputs[i]);
00369 min = FFMIN(min, val);
00370 }
00371
00372 return min;
00373 }
00374
00375 void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
00376 {
00377 if (pts == AV_NOPTS_VALUE)
00378 return;
00379 link->current_pts = av_rescale_q(pts, link->time_base, AV_TIME_BASE_Q);
00380
00381 if (link->graph && link->age_index >= 0)
00382 ff_avfilter_graph_update_heap(link->graph, link);
00383 }
00384
00385 int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
00386 {
00387 if(!strcmp(cmd, "ping")){
00388 av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
00389 return 0;
00390 }else if(filter->filter->process_command) {
00391 return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
00392 }
00393 return AVERROR(ENOSYS);
00394 }
00395
00396 #define MAX_REGISTERED_AVFILTERS_NB 128
00397
00398 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
00399
00400 static int next_registered_avfilter_idx = 0;
00401
00402 AVFilter *avfilter_get_by_name(const char *name)
00403 {
00404 int i;
00405
00406 for (i = 0; registered_avfilters[i]; i++)
00407 if (!strcmp(registered_avfilters[i]->name, name))
00408 return registered_avfilters[i];
00409
00410 return NULL;
00411 }
00412
00413 int avfilter_register(AVFilter *filter)
00414 {
00415 int i;
00416
00417 if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
00418 av_log(NULL, AV_LOG_ERROR,
00419 "Maximum number of registered filters %d reached, "
00420 "impossible to register filter with name '%s'\n",
00421 MAX_REGISTERED_AVFILTERS_NB, filter->name);
00422 return AVERROR(ENOMEM);
00423 }
00424
00425 for(i=0; filter->inputs && filter->inputs[i].name; i++) {
00426 const AVFilterPad *input = &filter->inputs[i];
00427 av_assert0( !input->filter_frame
00428 || (!input->start_frame && !input->end_frame && !input->draw_slice));
00429 }
00430
00431 registered_avfilters[next_registered_avfilter_idx++] = filter;
00432 return 0;
00433 }
00434
00435 AVFilter **av_filter_next(AVFilter **filter)
00436 {
00437 return filter ? ++filter : ®istered_avfilters[0];
00438 }
00439
00440 void avfilter_uninit(void)
00441 {
00442 memset(registered_avfilters, 0, sizeof(registered_avfilters));
00443 next_registered_avfilter_idx = 0;
00444 }
00445
00446 static int pad_count(const AVFilterPad *pads)
00447 {
00448 int count;
00449
00450 if (!pads)
00451 return 0;
00452
00453 for(count = 0; pads->name; count ++) pads ++;
00454 return count;
00455 }
00456
00457 static const char *default_filter_name(void *filter_ctx)
00458 {
00459 AVFilterContext *ctx = filter_ctx;
00460 return ctx->name ? ctx->name : ctx->filter->name;
00461 }
00462
00463 static void *filter_child_next(void *obj, void *prev)
00464 {
00465 AVFilterContext *ctx = obj;
00466 if (!prev && ctx->filter && ctx->filter->priv_class)
00467 return ctx->priv;
00468 return NULL;
00469 }
00470
00471 static const AVClass *filter_child_class_next(const AVClass *prev)
00472 {
00473 AVFilter **filter_ptr = NULL;
00474
00475
00476 while (prev && *(filter_ptr = av_filter_next(filter_ptr)))
00477 if ((*filter_ptr)->priv_class == prev)
00478 break;
00479
00480
00481 if (prev && !(*filter_ptr))
00482 return NULL;
00483
00484
00485 while (*(filter_ptr = av_filter_next(filter_ptr)))
00486 if ((*filter_ptr)->priv_class)
00487 return (*filter_ptr)->priv_class;
00488 return NULL;
00489 }
00490
00491 static const AVClass avfilter_class = {
00492 .class_name = "AVFilter",
00493 .item_name = default_filter_name,
00494 .version = LIBAVUTIL_VERSION_INT,
00495 .category = AV_CLASS_CATEGORY_FILTER,
00496 .child_next = filter_child_next,
00497 .child_class_next = filter_child_class_next,
00498 };
00499
00500 const AVClass *avfilter_get_class(void)
00501 {
00502 return &avfilter_class;
00503 }
00504
00505 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
00506 {
00507 AVFilterContext *ret;
00508 *filter_ctx = NULL;
00509
00510 if (!filter)
00511 return AVERROR(EINVAL);
00512
00513 ret = av_mallocz(sizeof(AVFilterContext));
00514 if (!ret)
00515 return AVERROR(ENOMEM);
00516
00517 ret->av_class = &avfilter_class;
00518 ret->filter = filter;
00519 ret->name = inst_name ? av_strdup(inst_name) : NULL;
00520 if (filter->priv_size) {
00521 ret->priv = av_mallocz(filter->priv_size);
00522 if (!ret->priv)
00523 goto err;
00524 }
00525
00526 ret->nb_inputs = pad_count(filter->inputs);
00527 if (ret->nb_inputs ) {
00528 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
00529 if (!ret->input_pads)
00530 goto err;
00531 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
00532 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
00533 if (!ret->inputs)
00534 goto err;
00535 }
00536
00537 ret->nb_outputs = pad_count(filter->outputs);
00538 if (ret->nb_outputs) {
00539 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
00540 if (!ret->output_pads)
00541 goto err;
00542 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
00543 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
00544 if (!ret->outputs)
00545 goto err;
00546 }
00547 #if FF_API_FOO_COUNT
00548 ret->output_count = ret->nb_outputs;
00549 ret->input_count = ret->nb_inputs;
00550 #endif
00551
00552 *filter_ctx = ret;
00553 return 0;
00554
00555 err:
00556 av_freep(&ret->inputs);
00557 av_freep(&ret->input_pads);
00558 ret->nb_inputs = 0;
00559 av_freep(&ret->outputs);
00560 av_freep(&ret->output_pads);
00561 ret->nb_outputs = 0;
00562 av_freep(&ret->priv);
00563 av_free(ret);
00564 return AVERROR(ENOMEM);
00565 }
00566
00567 void avfilter_free(AVFilterContext *filter)
00568 {
00569 int i;
00570 AVFilterLink *link;
00571
00572 if (!filter)
00573 return;
00574
00575 if (filter->filter->uninit)
00576 filter->filter->uninit(filter);
00577
00578 for (i = 0; i < filter->nb_inputs; i++) {
00579 if ((link = filter->inputs[i])) {
00580 if (link->src)
00581 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
00582 ff_formats_unref(&link->in_formats);
00583 ff_formats_unref(&link->out_formats);
00584 ff_formats_unref(&link->in_samplerates);
00585 ff_formats_unref(&link->out_samplerates);
00586 ff_channel_layouts_unref(&link->in_channel_layouts);
00587 ff_channel_layouts_unref(&link->out_channel_layouts);
00588 }
00589 avfilter_link_free(&link);
00590 }
00591 for (i = 0; i < filter->nb_outputs; i++) {
00592 if ((link = filter->outputs[i])) {
00593 if (link->dst)
00594 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
00595 ff_formats_unref(&link->in_formats);
00596 ff_formats_unref(&link->out_formats);
00597 ff_formats_unref(&link->in_samplerates);
00598 ff_formats_unref(&link->out_samplerates);
00599 ff_channel_layouts_unref(&link->in_channel_layouts);
00600 ff_channel_layouts_unref(&link->out_channel_layouts);
00601 }
00602 avfilter_link_free(&link);
00603 }
00604
00605 av_freep(&filter->name);
00606 av_freep(&filter->input_pads);
00607 av_freep(&filter->output_pads);
00608 av_freep(&filter->inputs);
00609 av_freep(&filter->outputs);
00610 av_freep(&filter->priv);
00611 while(filter->command_queue){
00612 ff_command_queue_pop(filter);
00613 }
00614 av_free(filter);
00615 }
00616
00617 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
00618 {
00619 int ret=0;
00620
00621 if (filter->filter->init_opaque)
00622 ret = filter->filter->init_opaque(filter, args, opaque);
00623 else if (filter->filter->init)
00624 ret = filter->filter->init(filter, args);
00625 return ret;
00626 }
00627
00628 const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx)
00629 {
00630 return pads[pad_idx].name;
00631 }
00632
00633 enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx)
00634 {
00635 return pads[pad_idx].type;
00636 }
00637
00638 int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
00639 {
00640 int ret;
00641 FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); ff_tlog_ref(NULL, frame, 1);
00642
00643 switch (link->type) {
00644 case AVMEDIA_TYPE_VIDEO:
00645 if((ret = ff_start_frame(link, frame)) < 0)
00646 return ret;
00647 if((ret = ff_draw_slice(link, 0, frame->video->h, 1)) < 0)
00648 return ret;
00649 if((ret = ff_end_frame(link)) < 0)
00650 return ret;
00651 return ret;
00652 case AVMEDIA_TYPE_AUDIO:
00653 return ff_filter_samples(link, frame);
00654 default: return AVERROR(EINVAL);
00655 }
00656 }