[FFmpeg-devel] [PATCH] memcpy-less expand
Michael Tison
blackspike
Thu Sep 17 23:54:27 CEST 2009
Here's an attempt at the memcpy-less expand. I implemented a
get_video_buffer for the input pad that'll hand a correctly sized
buffer to the previous filter.
I tried to do it without changing the api too much but still changed
one line in avfilter.c so that when srcpic is copied it is copied into
a correctly sized buffer.
Thanks!
Michael
-------------- next part --------------
Index: allfilters.c
===================================================================
--- allfilters.c (revision 5387)
+++ allfilters.c (working copy)
@@ -36,6 +36,7 @@ void avfilter_register_all(void)
REGISTER_FILTER(CROP,crop,vf);
REGISTER_FILTER(DRAWBOX,drawbox,vf);
+ REGISTER_FILTER(EXPAND,expand,vf);
REGISTER_FILTER(FIFO,fifo,vf);
REGISTER_FILTER(FORMAT,format,vf);
REGISTER_FILTER(FPS,fps,vf);
Index: vf_expand.c
===================================================================
--- vf_expand.c (revision 0)
+++ vf_expand.c (revision 0)
@@ -0,0 +1,216 @@
+/*
+ * Expand Video Filter
+ * Copyright (c) 2009 Michael Tison
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavcodec/imgconvert.h"
+#include "avfilter.h"
+
+typedef struct {
+ int w0, h0; //< original width and height (w,h)
+ int dw, dh; //< difference between original w,h and new w,h
+ int x, y; //< top left corner of image in padding
+ int hsub, vsub; //< chroma subsampling
+} ExpContext;
+
+static av_cold int init(AVFilterContext *c, const char *args, void *opaque) {
+ ExpContext *exp = c->priv;
+
+ exp->dh = 0;
+ exp->dw = 0;
+ exp->x = -1;
+ exp->y = -1;
+
+ if(args)
+ sscanf(args, "%d:%d:%d:%d", &exp->dw, &exp->dh, &exp->x, &exp->y);
+
+ return 0;
+}
+
+static int config_props_input(AVFilterLink *link) {
+ ExpContext *exp = link->dst->priv;
+
+ exp->h0 = link->h;
+ exp->w0 = link->w;
+ if(exp->x < 0) exp->x = exp->dw / 2;
+ if(exp->y < 0) exp->y = exp->dh / 2;
+
+ avcodec_get_chroma_sub_sample(link->format, &exp->hsub, &exp->vsub);
+ av_log(NULL, AV_LOG_INFO, "in %s"
+ " and exp->h0=%d, exp->w0=%d\n", __FUNCTION__, exp->h0, exp->w0);
+ return 0;
+}
+
+static void free_video_buffer(AVFilterPic *pic) {
+ av_free(pic->data[0]);
+ av_free(pic);
+}
+
+static AVFilterPicRef *get_video_buffer(AVFilterLink *link, int perms) {
+ ExpContext *exp = link->dst->priv;
+ AVFilterPic *pic = av_mallocz(sizeof(AVFilterPic));
+ AVFilterPicRef *ref = av_mallocz(sizeof(AVFilterPicRef));
+ int i, tempsize;
+ uint8_t *buf;
+
+ ref->pic = pic;
+ ref->w = link->w;
+ ref->h = link->h;
+
+ ref->perms = perms | AV_PERM_READ | AV_PERM_WRITE;
+
+ pic->refcount = 1;
+ pic->format = link->format;
+ pic->free = free_video_buffer;
+ ff_fill_linesize((AVPicture *)pic, pic->format, ref->w + exp->dw);
+
+ for (i=0; i<4;i++)
+ pic->linesize[i] = FFALIGN(pic->linesize[i], 16);
+
+ tempsize = ff_fill_pointer((AVPicture *)pic, NULL, pic->format, ref->h + exp->dh);
+ buf = av_mallocz(tempsize);
+ ff_fill_pointer((AVPicture *)pic, buf, pic->format, ref->h + exp->dh);
+
+ memcpy(ref->data, pic->data, sizeof(pic->data));
+ memcpy(ref->linesize, pic->linesize, sizeof(pic->linesize));
+
+ return ref;
+}
+
+static int query_formats(AVFilterContext *ctx) {
+ avfilter_set_common_formats(ctx,
+ avfilter_make_format_list(10,
+ PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P,
+ PIX_FMT_YUV411P, PIX_FMT_YUV410P,
+ PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P,
+ PIX_FMT_YUV440P, PIX_FMT_YUVJ440P));
+ return 0;
+}
+
+static int config_props_output(AVFilterLink *link) {
+ ExpContext *exp = link->src->priv;
+
+ link->w = link->src->inputs[0]->w + exp->dw;
+ link->h = link->src->inputs[0]->h + exp->dh;
+
+ return 0;
+}
+
+static void start_frame(AVFilterLink *link, AVFilterPicRef *picref) {
+ ExpContext *exp = link->dst->priv;
+ AVFilterPicRef *ref2 = avfilter_ref_pic(picref, ~0);
+ AVFilterPicRef *tmp_ref;
+ int i;
+
+ ref2->w = exp->w0 + exp->dw;
+ ref2->h = exp->h0 + exp->dh;
+
+ ff_fill_linesize((AVPicture*)ref2->pic, ref2->pic->format, ref2->w);
+ for (i=0; i<4;i++)
+ ref2->pic->linesize[i] = FFALIGN(ref2->pic->linesize[i], 16);
+ ff_fill_pointer((AVPicture*)ref2->pic, ref2->pic->data[0], ref2->pic->format, ref2->h);
+
+ memcpy(ref2->linesize, ref2->pic->linesize, sizeof(ref2->pic->linesize));
+ memcpy(ref2->data, ref2->pic->data, sizeof(ref2->pic->data));
+
+ avfilter_start_frame(link->dst->outputs[0], ref2);
+}
+
+static inline int in_region(int in_x, int in_y, int hsub, int vsub, ExpContext *exp) {
+ if(in_x >> hsub >= exp->x >> hsub
+ && in_x >> hsub < (exp->x + exp->w0) >> hsub
+ && in_y >> vsub > exp->y >> vsub
+ && in_y >> vsub < (exp->y + exp->h0) >> vsub) {
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+static void draw_expanded_pic(AVFilterPicRef *pic, ExpContext *exp) {
+ uint8_t *row;
+ int i, j, plane, vsub, hsub, max_h, max_w, c;
+ int xoff, yoff; //< (x,y)offset
+
+ xoff = exp->x;
+ yoff = exp->y;
+
+ for(i=0; i<4; i++)
+ pic->data[i] = pic->pic->data[i];
+ for(i=0; i<4; i++)
+ pic->linesize[i] = pic->pic->linesize[i];
+
+ for(plane = 0; plane < 3; plane++) {
+ c = (plane == 0) ? 0 : 128;
+ hsub = (plane == 0) ? 0 : exp->hsub;
+ vsub = (plane == 0) ? 0 : exp->vsub;
+ max_h = exp->h0 + exp->dh - 1;
+ max_w = exp->w0 + exp->dw;
+
+ row = pic->data[plane] + (max_h >> vsub)*pic->linesize[plane];
+ for(j = max_h >> vsub; j >= 0; j--) {
+ for(i = max_w >> hsub; i >= 0; i--) {
+ if(in_region(i<<hsub, j<<vsub, hsub, vsub, exp))
+ row[i] = *(pic->data[plane] + i + j*pic->linesize[plane]
+ - (xoff >> hsub) - (yoff >> vsub)*pic->linesize[plane]);
+ else
+ row[i] = c;
+ }
+
+ row -= pic->linesize[plane];
+ }
+ }
+}
+
+static void end_frame(AVFilterLink *link) {
+ ExpContext *exp = link->dst->priv;
+ AVFilterLink *output = link->dst->outputs[0];
+ AVFilterPicRef *pic = link->cur_pic;
+
+ draw_expanded_pic(pic, exp);
+
+ avfilter_draw_slice(output, 0, pic->h);
+ avfilter_end_frame(output);
+ avfilter_unref_pic(link->cur_pic);
+ link->cur_pic = NULL;
+}
+
+AVFilter avfilter_vf_expand =
+{
+ .name = "expand",
+ .priv_size = sizeof(ExpContext),
+ .query_formats = query_formats,
+ .init = init,
+ .inputs = (AVFilterPad[]) {{ .name = "default",
+ .type = CODEC_TYPE_VIDEO,
+ .start_frame = start_frame,
+ .get_video_buffer= get_video_buffer,
+ .end_frame = end_frame,
+ .config_props = config_props_input,
+ .min_perms = AV_PERM_READ |
+ AV_PERM_WRITE,
+ .rej_perms = AV_PERM_WRITE | // Force the previous filter to ask for a new buffer...
+ AV_PERM_REUSE |
+ AV_PERM_REUSE2},
+ { .name = NULL}},
+ .outputs = (AVFilterPad[]) {{ .name = "default",
+ .config_props = config_props_output,
+ .type = CODEC_TYPE_VIDEO, },
+ { .name = NULL}},
+};
Index: Makefile
===================================================================
--- Makefile (revision 5387)
+++ Makefile (working copy)
@@ -16,6 +16,7 @@ OBJS = allfilters.o \
OBJS-$(CONFIG_CROP_FILTER) += vf_crop.o
OBJS-$(CONFIG_DRAWBOX_FILTER) += vf_drawbox.o
+OBJS-$(CONFIG_EXPAND_FILTER) += vf_expand.o
OBJS-$(CONFIG_FPS_FILTER) += vf_fps.o
OBJS-$(CONFIG_HFLIP_FILTER) += vf_hflip.o
OBJS-$(CONFIG_NEGATE_FILTER) += vf_negate.o
-------------- next part --------------
Index: libavfilter/avfilter.c
===================================================================
--- libavfilter/avfilter.c (revision 19868)
+++ libavfilter/avfilter.c (working copy)
@@ -218,7 +218,7 @@ void avfilter_start_frame(AVFilterLink *link, AVFi
link_dpad(link).min_perms, link_dpad(link).rej_perms);
*/
- link->cur_pic = avfilter_default_get_video_buffer(link, dst->min_perms);
+ link->cur_pic = avfilter_get_video_buffer(link, dst->min_perms);
link->srcpic = picref;
link->cur_pic->pts = link->srcpic->pts;
link->cur_pic->pixel_aspect = link->srcpic->pixel_aspect;
More information about the ffmpeg-devel
mailing list