diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c index 50ba8fbcf8..af06a9172f 100644 --- a/libavfilter/avfiltergraph.c +++ b/libavfilter/avfiltergraph.c @@ -28,9 +28,23 @@ #include "avfiltergraph.h" #include "internal.h" +#include "libavutil/log.h" + +static const AVClass filtergraph_class = { + .class_name = "AVFilterGraph", + .item_name = av_default_item_name, + .version = LIBAVUTIL_VERSION_INT, +}; + AVFilterGraph *avfilter_graph_alloc(void) { - return av_mallocz(sizeof(AVFilterGraph)); + AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph)); + if (!ret) + return NULL; +#if FF_API_GRAPH_AVCLASS + ret->av_class = &filtergraph_class; +#endif + return ret; } void avfilter_graph_free(AVFilterGraph **graph) @@ -261,6 +275,49 @@ static void pick_format(AVFilterLink *link) } } +static int reduce_formats_on_filter(AVFilterContext *filter) +{ + int i, j, k, ret = 0; + + for (i = 0; i < filter->input_count; i++) { + AVFilterLink *link = filter->inputs[i]; + int format = link->out_formats->formats[0]; + + if (link->out_formats->format_count != 1) + continue; + + for (j = 0; j < filter->output_count; j++) { + AVFilterLink *out_link = filter->outputs[j]; + AVFilterFormats *fmts = out_link->in_formats; + + if (link->type != out_link->type || + out_link->in_formats->format_count == 1) + continue; + + for (k = 0; k < out_link->in_formats->format_count; k++) + if (fmts->formats[k] == format) { + fmts->formats[0] = format; + fmts->format_count = 1; + ret = 1; + break; + } + } + } + return ret; +} + +static void reduce_formats(AVFilterGraph *graph) +{ + int i, reduced; + + do { + reduced = 0; + + for (i = 0; i < graph->filter_count; i++) + reduced |= reduce_formats_on_filter(graph->filters[i]); + } while (reduced); +} + static void pick_formats(AVFilterGraph *graph) { int i, j; @@ -284,7 +341,10 @@ int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx) return ret; /* Once everything is merged, it's possible that we'll still have - * multiple valid media format choices. We pick the first one. */ + * multiple valid media format choices. We try to minimize the amount + * of format conversion inside filters */ + reduce_formats(graph); + pick_formats(graph); return 0; diff --git a/libavfilter/avfiltergraph.h b/libavfilter/avfiltergraph.h index 375ab8efbc..cca9488f87 100644 --- a/libavfilter/avfiltergraph.h +++ b/libavfilter/avfiltergraph.h @@ -23,8 +23,12 @@ #define AVFILTER_AVFILTERGRAPH_H #include "avfilter.h" +#include "libavutil/log.h" typedef struct AVFilterGraph { +#if FF_API_GRAPH_AVCLASS + const AVClass *av_class; +#endif unsigned filter_count; AVFilterContext **filters; diff --git a/libavfilter/version.h b/libavfilter/version.h index 6269bf88f0..c169ed25fc 100644 --- a/libavfilter/version.h +++ b/libavfilter/version.h @@ -40,4 +40,12 @@ LIBAVFILTER_VERSION_MICRO) #define LIBAVFILTER_BUILD LIBAVFILTER_VERSION_INT +/** + * Those FF_API_* defines are not part of public API. + * They may change, break or disappear at any time. + */ +#ifndef FF_API_GRAPH_AVCLASS +#define FF_API_GRAPH_AVCLASS (LIBAVFILTER_VERSION_MAJOR > 2) +#endif + #endif // AVFILTER_VERSION_H diff --git a/libavfilter/vf_scale.c b/libavfilter/vf_scale.c index 692e2c5a14..deb7009fa1 100644 --- a/libavfilter/vf_scale.c +++ b/libavfilter/vf_scale.c @@ -214,21 +214,26 @@ static int config_props(AVFilterLink *outlink) if (scale->sws) sws_freeContext(scale->sws); - scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format, - outlink->w, outlink->h, outfmt, - scale->flags, NULL, NULL, NULL); - if (scale->isws[0]) - sws_freeContext(scale->isws[0]); - scale->isws[0] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format, - outlink->w, outlink->h/2, outfmt, + if (inlink->w == outlink->w && inlink->h == outlink->h && + inlink->format == outlink->format) + scale->sws = NULL; + else { + scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format, + outlink->w, outlink->h, outfmt, scale->flags, NULL, NULL, NULL); - if (scale->isws[1]) - sws_freeContext(scale->isws[1]); - scale->isws[1] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format, - outlink->w, outlink->h/2, outfmt, - scale->flags, NULL, NULL, NULL); - if (!scale->sws || !scale->isws[0] || !scale->isws[1]) - return AVERROR(EINVAL); + if (scale->isws[0]) + sws_freeContext(scale->isws[0]); + scale->isws[0] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format, + outlink->w, outlink->h/2, outfmt, + scale->flags, NULL, NULL, NULL); + if (scale->isws[1]) + sws_freeContext(scale->isws[1]); + scale->isws[1] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format, + outlink->w, outlink->h/2, outfmt, + scale->flags, NULL, NULL, NULL); + if (!scale->sws || !scale->isws[0] || !scale->isws[1]) + return AVERROR(EINVAL); + } if (inlink->sample_aspect_ratio.num){ outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink->w, outlink->w * inlink->h}, inlink->sample_aspect_ratio); @@ -257,6 +262,11 @@ static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref) AVFilterLink *outlink = link->dst->outputs[0]; AVFilterBufferRef *outpicref; + if (!scale->sws) { + avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0)); + return; + } + scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w; scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h; @@ -307,6 +317,11 @@ static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir) ScaleContext *scale = link->dst->priv; int out_h; + if (!scale->sws) { + avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir); + return; + } + if (scale->slice_y == 0 && slice_dir == -1) scale->slice_y = link->dst->outputs[0]->h; diff --git a/libavfilter/vsrc_buffer.c b/libavfilter/vsrc_buffer.c index dbccbe07fb..61d66468d5 100644 --- a/libavfilter/vsrc_buffer.c +++ b/libavfilter/vsrc_buffer.c @@ -28,11 +28,12 @@ #include "avcodec.h" #include "buffersrc.h" #include "vsrc_buffer.h" +#include "libavutil/fifo.h" #include "libavutil/imgutils.h" typedef struct { - AVFilterBufferRef *picref; - AVFilterContext *scale; + AVFilterContext *scale; + AVFifoBuffer *fifo; int h, w; enum PixelFormat pix_fmt; AVRational time_base; ///< time_base to set in the output link @@ -51,19 +52,18 @@ int av_vsrc_buffer_add_video_buffer_ref(AVFilterContext *buffer_filter, { BufferSourceContext *c = buffer_filter->priv; AVFilterLink *outlink = buffer_filter->outputs[0]; + AVFilterBufferRef *buf; int ret; - if (c->picref) { + if (av_fifo_size(c->fifo)) { if (flags & AV_VSRC_BUF_FLAG_OVERWRITE) { - avfilter_unref_buffer(c->picref); - c->picref = NULL; - } else { - av_log(buffer_filter, AV_LOG_ERROR, - "Buffering several frames is not supported. " - "Please consume all available frames before adding a new one.\n"); - return AVERROR(EINVAL); + //FIXME not implemented } } + if (!av_fifo_space(c->fifo) && + (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) + + sizeof(buf))) < 0) + return ret; if (picref->video->w != c->w || picref->video->h != c->h || picref->format != c->pix_fmt) { AVFilterContext *scale = buffer_filter->outputs[0]->dst; @@ -110,12 +110,17 @@ int av_vsrc_buffer_add_video_buffer_ref(AVFilterContext *buffer_filter, return ret; } - c->picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, - picref->video->w, picref->video->h); - av_image_copy(c->picref->data, c->picref->linesize, + buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, + picref->video->w, picref->video->h); + av_image_copy(buf->data, buf->linesize, (void*)picref->data, picref->linesize, picref->format, picref->video->w, picref->video->h); - avfilter_copy_buffer_ref_props(c->picref, picref); + avfilter_copy_buffer_ref_props(buf, picref); + + if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0) { + avfilter_unref_buffer(buf); + return ret; + } return 0; } @@ -123,18 +128,17 @@ int av_vsrc_buffer_add_video_buffer_ref(AVFilterContext *buffer_filter, int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf) { BufferSourceContext *c = s->priv; + int ret; - if (c->picref) { - av_log(s, AV_LOG_ERROR, - "Buffering several frames is not supported. " - "Please consume all available frames before adding a new one.\n" - ); - return AVERROR(EINVAL); - } + if (!av_fifo_space(c->fifo) && + (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) + + sizeof(buf))) < 0) + return ret; // CHECK_PARAM_CHANGE(s, c, buf->video->w, buf->video->h, buf->format); - c->picref = buf; + if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0) + return ret; return 0; } @@ -176,6 +180,9 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0) return ret; + if (!(c->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*)))) + return AVERROR(ENOMEM); + av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n", c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name, c->time_base.num, c->time_base.den, @@ -186,9 +193,13 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) static av_cold void uninit(AVFilterContext *ctx) { BufferSourceContext *s = ctx->priv; - if (s->picref) - avfilter_unref_buffer(s->picref); - s->picref = NULL; + while (av_fifo_size(s->fifo)) { + AVFilterBufferRef *buf; + av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL); + avfilter_unref_buffer(buf); + } + av_fifo_free(s->fifo); + s->fifo = NULL; avfilter_free(s->scale); s->scale = NULL; } @@ -217,18 +228,19 @@ static int config_props(AVFilterLink *link) static int request_frame(AVFilterLink *link) { BufferSourceContext *c = link->src->priv; + AVFilterBufferRef *buf; - if (!c->picref) { + if (!av_fifo_size(c->fifo)) { av_log(link->src, AV_LOG_WARNING, "request_frame() called with no available frame!\n"); return AVERROR(EINVAL); } + av_fifo_generic_read(c->fifo, &buf, sizeof(buf), NULL); - avfilter_start_frame(link, avfilter_ref_buffer(c->picref, ~0)); + avfilter_start_frame(link, avfilter_ref_buffer(buf, ~0)); avfilter_draw_slice(link, 0, link->h, 1); avfilter_end_frame(link); - avfilter_unref_buffer(c->picref); - c->picref = NULL; + avfilter_unref_buffer(buf); return 0; } @@ -236,7 +248,7 @@ static int request_frame(AVFilterLink *link) static int poll_frame(AVFilterLink *link) { BufferSourceContext *c = link->src->priv; - return !!c->picref; + return !!av_fifo_size(c->fifo); } AVFilter avfilter_vsrc_buffer = { diff --git a/libavformat/id3v2.c b/libavformat/id3v2.c index 16eff7e27d..1352b57b63 100644 --- a/libavformat/id3v2.c +++ b/libavformat/id3v2.c @@ -577,21 +577,21 @@ static void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t unsync = flags & 0x80; - /* Extended header present, just skip over it */ - if (isv34 && flags & 0x40) { - int size = get_size(s->pb, 4); - if (size < 6) { - reason = "extended header too short."; + if (isv34 && flags & 0x40) { /* Extended header present, just skip over it */ + int extlen = get_size(s->pb, 4); + if (version == 4) + extlen -= 4; // in v2.4 the length includes the length field we just read + + if (extlen < 0) { + reason = "invalid extended header length"; goto error; } - len -= size; + avio_skip(s->pb, extlen); + len -= extlen + 4; if (len < 0) { reason = "extended header too long."; goto error; } - /* already seeked past size, skip the reset */ - size -= 4; - avio_skip(s->pb, size); } while (len >= taghdrlen) {