00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "libavutil/pixdesc.h"
00025 #include "libavutil/rational.h"
00026 #include "libavcore/audioconvert.h"
00027 #include "libavcore/imgutils.h"
00028 #include "avfilter.h"
00029 #include "internal.h"
00030
00031 unsigned avfilter_version(void) {
00032 return LIBAVFILTER_VERSION_INT;
00033 }
00034
00035 const char *avfilter_configuration(void)
00036 {
00037 return FFMPEG_CONFIGURATION;
00038 }
00039
00040 const char *avfilter_license(void)
00041 {
00042 #define LICENSE_PREFIX "libavfilter license: "
00043 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
00044 }
00045
00046 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask)
00047 {
00048 AVFilterBufferRef *ret = av_malloc(sizeof(AVFilterBufferRef));
00049 if (!ret)
00050 return NULL;
00051 *ret = *ref;
00052 if (ref->type == AVMEDIA_TYPE_VIDEO) {
00053 ret->video = av_malloc(sizeof(AVFilterBufferRefVideoProps));
00054 if (!ret->video) {
00055 av_free(ret);
00056 return NULL;
00057 }
00058 *ret->video = *ref->video;
00059 } else if (ref->type == AVMEDIA_TYPE_AUDIO) {
00060 ret->audio = av_malloc(sizeof(AVFilterBufferRefAudioProps));
00061 if (!ret->audio) {
00062 av_free(ret);
00063 return NULL;
00064 }
00065 *ret->audio = *ref->audio;
00066 }
00067 ret->perms &= pmask;
00068 ret->buf->refcount ++;
00069 return ret;
00070 }
00071
00072 void avfilter_unref_buffer(AVFilterBufferRef *ref)
00073 {
00074 if (!ref)
00075 return;
00076 if (!(--ref->buf->refcount))
00077 ref->buf->free(ref->buf);
00078 av_free(ref->video);
00079 av_free(ref->audio);
00080 av_free(ref);
00081 }
00082
00083 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
00084 AVFilterPad **pads, AVFilterLink ***links,
00085 AVFilterPad *newpad)
00086 {
00087 unsigned i;
00088
00089 idx = FFMIN(idx, *count);
00090
00091 *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
00092 *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
00093 memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad) * (*count-idx));
00094 memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
00095 memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
00096 (*links)[idx] = NULL;
00097
00098 (*count)++;
00099 for (i = idx+1; i < *count; i++)
00100 if (*links[i])
00101 (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
00102 }
00103
00104 int avfilter_link(AVFilterContext *src, unsigned srcpad,
00105 AVFilterContext *dst, unsigned dstpad)
00106 {
00107 AVFilterLink *link;
00108
00109 if (src->output_count <= srcpad || dst->input_count <= dstpad ||
00110 src->outputs[srcpad] || dst->inputs[dstpad])
00111 return -1;
00112
00113 if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
00114 av_log(src, AV_LOG_ERROR,
00115 "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
00116 src->name, srcpad, dst->name, dstpad);
00117 return AVERROR(EINVAL);
00118 }
00119
00120 src->outputs[srcpad] =
00121 dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
00122
00123 link->src = src;
00124 link->dst = dst;
00125 link->srcpad = &src->output_pads[srcpad];
00126 link->dstpad = &dst->input_pads[dstpad];
00127 link->type = src->output_pads[srcpad].type;
00128 assert(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
00129 link->format = -1;
00130
00131 return 0;
00132 }
00133
00134 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
00135 unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
00136 {
00137 int ret;
00138 unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
00139
00140 av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
00141 "between the filter '%s' and the filter '%s'\n",
00142 filt->name, link->src->name, link->dst->name);
00143
00144 link->dst->inputs[dstpad_idx] = NULL;
00145 if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
00146
00147 link->dst->inputs[dstpad_idx] = link;
00148 return ret;
00149 }
00150
00151
00152 link->dst = filt;
00153 link->dstpad = &filt->input_pads[filt_srcpad_idx];
00154 filt->inputs[filt_srcpad_idx] = link;
00155
00156
00157
00158 if (link->out_formats)
00159 avfilter_formats_changeref(&link->out_formats,
00160 &filt->outputs[filt_dstpad_idx]->out_formats);
00161
00162 return 0;
00163 }
00164
00165 int avfilter_config_links(AVFilterContext *filter)
00166 {
00167 int (*config_link)(AVFilterLink *);
00168 unsigned i;
00169 int ret;
00170
00171 for (i = 0; i < filter->input_count; i ++) {
00172 AVFilterLink *link = filter->inputs[i];
00173
00174 if (!link) continue;
00175
00176 switch (link->init_state) {
00177 case AVLINK_INIT:
00178 continue;
00179 case AVLINK_STARTINIT:
00180 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
00181 return 0;
00182 case AVLINK_UNINIT:
00183 link->init_state = AVLINK_STARTINIT;
00184
00185 if ((ret = avfilter_config_links(link->src)) < 0)
00186 return ret;
00187
00188 if (!(config_link = link->srcpad->config_props))
00189 config_link = avfilter_default_config_output_link;
00190 if ((ret = config_link(link)) < 0)
00191 return ret;
00192
00193 if (link->time_base.num == 0 && link->time_base.den == 0)
00194 link->time_base = link->src && link->src->input_count ?
00195 link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
00196
00197 if ((config_link = link->dstpad->config_props))
00198 if ((ret = config_link(link)) < 0)
00199 return ret;
00200
00201 link->init_state = AVLINK_INIT;
00202 }
00203 }
00204
00205 return 0;
00206 }
00207
00208 char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
00209 {
00210 snprintf(buf, buf_size, "%s%s%s%s%s%s",
00211 perms & AV_PERM_READ ? "r" : "",
00212 perms & AV_PERM_WRITE ? "w" : "",
00213 perms & AV_PERM_PRESERVE ? "p" : "",
00214 perms & AV_PERM_REUSE ? "u" : "",
00215 perms & AV_PERM_REUSE2 ? "U" : "",
00216 perms & AV_PERM_NEG_LINESIZES ? "n" : "");
00217 return buf;
00218 }
00219
00220 void ff_dprintf_ref(void *ctx, AVFilterBufferRef *ref, int end)
00221 {
00222 av_unused char buf[16];
00223 dprintf(ctx,
00224 "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
00225 ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
00226 ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
00227 ref->pts, ref->pos);
00228
00229 if (ref->video) {
00230 dprintf(ctx, " a:%d/%d s:%dx%d i:%c",
00231 ref->video->pixel_aspect.num, ref->video->pixel_aspect.den,
00232 ref->video->w, ref->video->h,
00233 !ref->video->interlaced ? 'P' :
00234 ref->video->top_field_first ? 'T' : 'B');
00235 }
00236 if (ref->audio) {
00237 dprintf(ctx, " cl:%"PRId64"d sn:%d s:%d sr:%d p:%d",
00238 ref->audio->channel_layout,
00239 ref->audio->nb_samples,
00240 ref->audio->size,
00241 ref->audio->sample_rate,
00242 ref->audio->planar);
00243 }
00244
00245 dprintf(ctx, "]%s", end ? "\n" : "");
00246 }
00247
00248 void ff_dprintf_link(void *ctx, AVFilterLink *link, int end)
00249 {
00250 if (link->type == AVMEDIA_TYPE_VIDEO) {
00251 dprintf(ctx,
00252 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
00253 link, link->w, link->h,
00254 av_pix_fmt_descriptors[link->format].name,
00255 link->src ? link->src->filter->name : "",
00256 link->dst ? link->dst->filter->name : "",
00257 end ? "\n" : "");
00258 } else {
00259 char buf[128];
00260 av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
00261
00262 dprintf(ctx,
00263 "link[%p r:%"PRId64" cl:%s fmt:%-16s %-16s->%-16s]%s",
00264 link, link->sample_rate, buf,
00265 av_get_sample_fmt_name(link->format),
00266 link->src ? link->src->filter->name : "",
00267 link->dst ? link->dst->filter->name : "",
00268 end ? "\n" : "");
00269 }
00270 }
00271
00272 AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
00273 {
00274 AVFilterBufferRef *ret = NULL;
00275
00276 av_unused char buf[16];
00277 FF_DPRINTF_START(NULL, get_video_buffer); ff_dprintf_link(NULL, link, 0);
00278 dprintf(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
00279
00280 if (link->dstpad->get_video_buffer)
00281 ret = link->dstpad->get_video_buffer(link, perms, w, h);
00282
00283 if (!ret)
00284 ret = avfilter_default_get_video_buffer(link, perms, w, h);
00285
00286 if (ret)
00287 ret->type = AVMEDIA_TYPE_VIDEO;
00288
00289 FF_DPRINTF_START(NULL, get_video_buffer); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " returning "); ff_dprintf_ref(NULL, ret, 1);
00290
00291 return ret;
00292 }
00293
00294 AVFilterBufferRef *
00295 avfilter_get_video_buffer_ref_from_arrays(uint8_t *data[4], int linesize[4], int perms,
00296 int w, int h, enum PixelFormat format)
00297 {
00298 AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
00299 AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
00300
00301 if (!pic || !picref)
00302 goto fail;
00303
00304 picref->buf = pic;
00305 picref->buf->free = ff_avfilter_default_free_buffer;
00306 if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
00307 goto fail;
00308
00309 pic->w = picref->video->w = w;
00310 pic->h = picref->video->h = h;
00311
00312
00313 picref->perms = perms | AV_PERM_READ;
00314
00315 pic->refcount = 1;
00316 picref->type = AVMEDIA_TYPE_VIDEO;
00317 pic->format = picref->format = format;
00318
00319 memcpy(pic->data, data, sizeof(pic->data));
00320 memcpy(pic->linesize, linesize, sizeof(pic->linesize));
00321 memcpy(picref->data, pic->data, sizeof(picref->data));
00322 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
00323
00324 return picref;
00325
00326 fail:
00327 if (picref && picref->video)
00328 av_free(picref->video);
00329 av_free(picref);
00330 av_free(pic);
00331 return NULL;
00332 }
00333
00334 AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link, int perms,
00335 enum AVSampleFormat sample_fmt, int size,
00336 int64_t channel_layout, int planar)
00337 {
00338 AVFilterBufferRef *ret = NULL;
00339
00340 if (link->dstpad->get_audio_buffer)
00341 ret = link->dstpad->get_audio_buffer(link, perms, sample_fmt, size, channel_layout, planar);
00342
00343 if (!ret)
00344 ret = avfilter_default_get_audio_buffer(link, perms, sample_fmt, size, channel_layout, planar);
00345
00346 if (ret)
00347 ret->type = AVMEDIA_TYPE_AUDIO;
00348
00349 return ret;
00350 }
00351
00352 int avfilter_request_frame(AVFilterLink *link)
00353 {
00354 FF_DPRINTF_START(NULL, request_frame); ff_dprintf_link(NULL, link, 1);
00355
00356 if (link->srcpad->request_frame)
00357 return link->srcpad->request_frame(link);
00358 else if (link->src->inputs[0])
00359 return avfilter_request_frame(link->src->inputs[0]);
00360 else return -1;
00361 }
00362
00363 int avfilter_poll_frame(AVFilterLink *link)
00364 {
00365 int i, min = INT_MAX;
00366
00367 if (link->srcpad->poll_frame)
00368 return link->srcpad->poll_frame(link);
00369
00370 for (i = 0; i < link->src->input_count; i++) {
00371 int val;
00372 if (!link->src->inputs[i])
00373 return -1;
00374 val = avfilter_poll_frame(link->src->inputs[i]);
00375 min = FFMIN(min, val);
00376 }
00377
00378 return min;
00379 }
00380
00381
00382
00383 void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
00384 {
00385 void (*start_frame)(AVFilterLink *, AVFilterBufferRef *);
00386 AVFilterPad *dst = link->dstpad;
00387 int perms = picref->perms;
00388
00389 FF_DPRINTF_START(NULL, start_frame); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " "); ff_dprintf_ref(NULL, picref, 1);
00390
00391 if (!(start_frame = dst->start_frame))
00392 start_frame = avfilter_default_start_frame;
00393
00394 if (picref->linesize[0] < 0)
00395 perms |= AV_PERM_NEG_LINESIZES;
00396
00397 if ((dst->min_perms & perms) != dst->min_perms || dst->rej_perms & perms) {
00398 av_log(link->dst, AV_LOG_DEBUG,
00399 "frame copy needed (have perms %x, need %x, reject %x)\n",
00400 picref->perms,
00401 link->dstpad->min_perms, link->dstpad->rej_perms);
00402
00403 link->cur_buf = avfilter_get_video_buffer(link, dst->min_perms, link->w, link->h);
00404 link->src_buf = picref;
00405 avfilter_copy_buffer_ref_props(link->cur_buf, link->src_buf);
00406 }
00407 else
00408 link->cur_buf = picref;
00409
00410 start_frame(link, link->cur_buf);
00411 }
00412
00413 void avfilter_end_frame(AVFilterLink *link)
00414 {
00415 void (*end_frame)(AVFilterLink *);
00416
00417 if (!(end_frame = link->dstpad->end_frame))
00418 end_frame = avfilter_default_end_frame;
00419
00420 end_frame(link);
00421
00422
00423
00424 if (link->src_buf) {
00425 avfilter_unref_buffer(link->src_buf);
00426 link->src_buf = NULL;
00427 }
00428 }
00429
00430 void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
00431 {
00432 uint8_t *src[4], *dst[4];
00433 int i, j, vsub;
00434 void (*draw_slice)(AVFilterLink *, int, int, int);
00435
00436 FF_DPRINTF_START(NULL, draw_slice); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
00437
00438
00439 if (link->src_buf) {
00440 vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
00441
00442 for (i = 0; i < 4; i++) {
00443 if (link->src_buf->data[i]) {
00444 src[i] = link->src_buf-> data[i] +
00445 (y >> (i==1 || i==2 ? vsub : 0)) * link->src_buf-> linesize[i];
00446 dst[i] = link->cur_buf->data[i] +
00447 (y >> (i==1 || i==2 ? vsub : 0)) * link->cur_buf->linesize[i];
00448 } else
00449 src[i] = dst[i] = NULL;
00450 }
00451
00452 for (i = 0; i < 4; i++) {
00453 int planew =
00454 av_image_get_linesize(link->format, link->cur_buf->video->w, i);
00455
00456 if (!src[i]) continue;
00457
00458 for (j = 0; j < h >> (i==1 || i==2 ? vsub : 0); j++) {
00459 memcpy(dst[i], src[i], planew);
00460 src[i] += link->src_buf->linesize[i];
00461 dst[i] += link->cur_buf->linesize[i];
00462 }
00463 }
00464 }
00465
00466 if (!(draw_slice = link->dstpad->draw_slice))
00467 draw_slice = avfilter_default_draw_slice;
00468 draw_slice(link, y, h, slice_dir);
00469 }
00470
00471 void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
00472 {
00473 void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
00474 AVFilterPad *dst = link->dstpad;
00475
00476 if (!(filter_samples = dst->filter_samples))
00477 filter_samples = avfilter_default_filter_samples;
00478
00479
00480 if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
00481 dst->rej_perms & samplesref->perms) {
00482
00483 av_log(link->dst, AV_LOG_DEBUG,
00484 "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
00485 samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
00486
00487 link->cur_buf = avfilter_default_get_audio_buffer(link, dst->min_perms,
00488 samplesref->format,
00489 samplesref->audio->size,
00490 samplesref->audio->channel_layout,
00491 samplesref->audio->planar);
00492 link->cur_buf->pts = samplesref->pts;
00493 link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
00494
00495
00496 memcpy(link->cur_buf->data[0], samplesref->data[0], samplesref->audio->size);
00497
00498 avfilter_unref_buffer(samplesref);
00499 } else
00500 link->cur_buf = samplesref;
00501
00502 filter_samples(link, link->cur_buf);
00503 }
00504
00505 #define MAX_REGISTERED_AVFILTERS_NB 64
00506
00507 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
00508
00509 static int next_registered_avfilter_idx = 0;
00510
00511 AVFilter *avfilter_get_by_name(const char *name)
00512 {
00513 int i;
00514
00515 for (i = 0; registered_avfilters[i]; i++)
00516 if (!strcmp(registered_avfilters[i]->name, name))
00517 return registered_avfilters[i];
00518
00519 return NULL;
00520 }
00521
00522 int avfilter_register(AVFilter *filter)
00523 {
00524 if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
00525 return -1;
00526
00527 registered_avfilters[next_registered_avfilter_idx++] = filter;
00528 return 0;
00529 }
00530
00531 AVFilter **av_filter_next(AVFilter **filter)
00532 {
00533 return filter ? ++filter : ®istered_avfilters[0];
00534 }
00535
00536 void avfilter_uninit(void)
00537 {
00538 memset(registered_avfilters, 0, sizeof(registered_avfilters));
00539 next_registered_avfilter_idx = 0;
00540 }
00541
00542 static int pad_count(const AVFilterPad *pads)
00543 {
00544 int count;
00545
00546 for(count = 0; pads->name; count ++) pads ++;
00547 return count;
00548 }
00549
00550 static const char *filter_name(void *p)
00551 {
00552 AVFilterContext *filter = p;
00553 return filter->filter->name;
00554 }
00555
00556 static const AVClass avfilter_class = {
00557 "AVFilter",
00558 filter_name,
00559 NULL,
00560 LIBAVUTIL_VERSION_INT,
00561 };
00562
00563 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
00564 {
00565 AVFilterContext *ret;
00566 *filter_ctx = NULL;
00567
00568 if (!filter)
00569 return AVERROR(EINVAL);
00570
00571 ret = av_mallocz(sizeof(AVFilterContext));
00572
00573 ret->av_class = &avfilter_class;
00574 ret->filter = filter;
00575 ret->name = inst_name ? av_strdup(inst_name) : NULL;
00576 ret->priv = av_mallocz(filter->priv_size);
00577
00578 ret->input_count = pad_count(filter->inputs);
00579 if (ret->input_count) {
00580 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->input_count);
00581 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
00582 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
00583 }
00584
00585 ret->output_count = pad_count(filter->outputs);
00586 if (ret->output_count) {
00587 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->output_count);
00588 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
00589 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
00590 }
00591
00592 *filter_ctx = ret;
00593 return 0;
00594 }
00595
00596 void avfilter_free(AVFilterContext *filter)
00597 {
00598 int i;
00599 AVFilterLink *link;
00600
00601 if (filter->filter->uninit)
00602 filter->filter->uninit(filter);
00603
00604 for (i = 0; i < filter->input_count; i++) {
00605 if ((link = filter->inputs[i])) {
00606 if (link->src)
00607 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
00608 avfilter_formats_unref(&link->in_formats);
00609 avfilter_formats_unref(&link->out_formats);
00610 }
00611 av_freep(&link);
00612 }
00613 for (i = 0; i < filter->output_count; i++) {
00614 if ((link = filter->outputs[i])) {
00615 if (link->dst)
00616 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
00617 avfilter_formats_unref(&link->in_formats);
00618 avfilter_formats_unref(&link->out_formats);
00619 }
00620 av_freep(&link);
00621 }
00622
00623 av_freep(&filter->name);
00624 av_freep(&filter->input_pads);
00625 av_freep(&filter->output_pads);
00626 av_freep(&filter->inputs);
00627 av_freep(&filter->outputs);
00628 av_freep(&filter->priv);
00629 av_free(filter);
00630 }
00631
00632 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
00633 {
00634 int ret=0;
00635
00636 if (filter->filter->init)
00637 ret = filter->filter->init(filter, args, opaque);
00638 return ret;
00639 }
00640