diff options
Diffstat (limited to 'libavfilter/vf_pad.c')
-rw-r--r-- | libavfilter/vf_pad.c | 167 |
1 files changed, 59 insertions, 108 deletions
diff --git a/libavfilter/vf_pad.c b/libavfilter/vf_pad.c index 2e98aea..31702b7 100644 --- a/libavfilter/vf_pad.c +++ b/libavfilter/vf_pad.c @@ -2,20 +2,20 @@ * Copyright (c) 2008 vmrsss * Copyright (c) 2009 Stefano Sabatini * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -38,9 +38,6 @@ #include "drawutils.h" static const char *const var_names[] = { - "PI", - "PHI", - "E", "in_w", "iw", "in_h", "ih", "out_w", "ow", @@ -48,15 +45,14 @@ static const char *const var_names[] = { "x", "y", "a", + "sar", + "dar", "hsub", "vsub", NULL }; enum var_name { - VAR_PI, - VAR_PHI, - VAR_E, VAR_IN_W, VAR_IW, VAR_IN_H, VAR_IH, VAR_OUT_W, VAR_OW, @@ -64,6 +60,8 @@ enum var_name { VAR_X, VAR_Y, VAR_A, + VAR_SAR, + VAR_DAR, VAR_HSUB, VAR_VSUB, VARS_NB @@ -71,22 +69,7 @@ enum var_name { static int query_formats(AVFilterContext *ctx) { - static const enum PixelFormat pix_fmts[] = { - PIX_FMT_ARGB, PIX_FMT_RGBA, - PIX_FMT_ABGR, PIX_FMT_BGRA, - PIX_FMT_RGB24, PIX_FMT_BGR24, - - PIX_FMT_YUV444P, PIX_FMT_YUV422P, - PIX_FMT_YUV420P, PIX_FMT_YUV411P, - PIX_FMT_YUV410P, PIX_FMT_YUV440P, - PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, - PIX_FMT_YUVJ420P, PIX_FMT_YUVJ440P, - PIX_FMT_YUVA420P, - - PIX_FMT_NONE - }; - - ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); + ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0)); return 0; } @@ -100,10 +83,9 @@ typedef struct { char x_expr[256]; ///< width expression string char y_expr[256]; ///< height expression string - uint8_t color[4]; ///< color expressed either in YUVA or RGBA colorspace for the padding area - uint8_t *line[4]; - int line_step[4]; - int hsub, vsub; ///< chroma subsampling values + uint8_t rgba_color[4]; ///< color for the padding area + FFDrawContext draw; + FFDrawColor color; int needs_copy; } PadContext; @@ -118,49 +100,36 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) av_strlcpy(pad->y_expr, "0" , sizeof(pad->h_expr)); if (args) - sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]:%255s", + sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]:%127s", pad->w_expr, pad->h_expr, pad->x_expr, pad->y_expr, color_string); - if (av_parse_color(pad->color, color_string, -1, ctx) < 0) + if (av_parse_color(pad->rgba_color, color_string, -1, ctx) < 0) return AVERROR(EINVAL); return 0; } -static av_cold void uninit(AVFilterContext *ctx) -{ - PadContext *pad = ctx->priv; - int i; - - for (i = 0; i < 4; i++) { - av_freep(&pad->line[i]); - pad->line_step[i] = 0; - } -} - static int config_input(AVFilterLink *inlink) { AVFilterContext *ctx = inlink->dst; PadContext *pad = ctx->priv; - const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format]; - uint8_t rgba_color[4]; - int ret, is_packed_rgba; + int ret; double var_values[VARS_NB], res; char *expr; - pad->hsub = pix_desc->log2_chroma_w; - pad->vsub = pix_desc->log2_chroma_h; + ff_draw_init(&pad->draw, inlink->format, 0); + ff_draw_color(&pad->draw, &pad->color, pad->rgba_color); - var_values[VAR_PI] = M_PI; - var_values[VAR_PHI] = M_PHI; - var_values[VAR_E] = M_E; var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w; var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h; var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN; var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN; var_values[VAR_A] = (float) inlink->w / inlink->h; - var_values[VAR_HSUB] = 1<<pad->hsub; - var_values[VAR_VSUB] = 1<<pad->vsub; + var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ? + (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1; + var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR]; + var_values[VAR_HSUB] = 1 << pad->draw.hsub_max; + var_values[VAR_VSUB] = 1 << pad->draw.vsub_max; /* evaluate width and height */ av_expr_parse_and_eval(&res, (expr = pad->w_expr), @@ -207,22 +176,16 @@ static int config_input(AVFilterLink *inlink) if (!pad->h) pad->h = inlink->h; - pad->w &= ~((1 << pad->hsub) - 1); - pad->h &= ~((1 << pad->vsub) - 1); - pad->x &= ~((1 << pad->hsub) - 1); - pad->y &= ~((1 << pad->vsub) - 1); - - pad->in_w = inlink->w & ~((1 << pad->hsub) - 1); - pad->in_h = inlink->h & ~((1 << pad->vsub) - 1); + pad->w = ff_draw_round_to_sub(&pad->draw, 0, -1, pad->w); + pad->h = ff_draw_round_to_sub(&pad->draw, 1, -1, pad->h); + pad->x = ff_draw_round_to_sub(&pad->draw, 0, -1, pad->x); + pad->y = ff_draw_round_to_sub(&pad->draw, 1, -1, pad->y); + pad->in_w = ff_draw_round_to_sub(&pad->draw, 0, -1, inlink->w); + pad->in_h = ff_draw_round_to_sub(&pad->draw, 1, -1, inlink->h); - memcpy(rgba_color, pad->color, sizeof(rgba_color)); - ff_fill_line_with_color(pad->line, pad->line_step, pad->w, pad->color, - inlink->format, rgba_color, &is_packed_rgba, NULL); - - av_log(ctx, AV_LOG_INFO, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X[%s]\n", + av_log(ctx, AV_LOG_INFO, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X\n", inlink->w, inlink->h, pad->w, pad->h, pad->x, pad->y, - pad->color[0], pad->color[1], pad->color[2], pad->color[3], - is_packed_rgba ? "rgba" : "yuva"); + pad->rgba_color[0], pad->rgba_color[1], pad->rgba_color[2], pad->rgba_color[3]); if (pad->x < 0 || pad->y < 0 || pad->w <= 0 || pad->h <= 0 || @@ -255,22 +218,19 @@ static int config_output(AVFilterLink *outlink) static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h) { PadContext *pad = inlink->dst->priv; + int align = (perms&AV_PERM_ALIGN) ? AVFILTER_ALIGN : 1; AVFilterBufferRef *picref = avfilter_get_video_buffer(inlink->dst->outputs[0], perms, - w + (pad->w - pad->in_w), + w + (pad->w - pad->in_w) + 4*align, h + (pad->h - pad->in_h)); int plane; picref->video->w = w; picref->video->h = h; - for (plane = 0; plane < 4 && picref->data[plane]; plane++) { - int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0; - int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0; - - picref->data[plane] += (pad->x >> hsub) * pad->line_step[plane] + - (pad->y >> vsub) * picref->linesize[plane]; - } + for (plane = 0; plane < 4 && picref->data[plane]; plane++) + picref->data[plane] += FFALIGN(pad->x >> pad->draw.hsub[plane], align) * pad->draw.pixelstep[plane] + + (pad->y >> pad->draw.vsub[plane]) * picref->linesize[plane]; return picref; } @@ -280,12 +240,12 @@ static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, i int64_t x_in_buf, y_in_buf; x_in_buf = outpicref->data[plane] - outpicref->buf->data[plane] - + (x >> hsub) * pad ->line_step[plane] - + (y >> vsub) * outpicref->linesize [plane]; + + (x >> hsub) * pad->draw.pixelstep[plane] + + (y >> vsub) * outpicref->linesize[plane]; - if(x_in_buf < 0 || x_in_buf % pad->line_step[plane]) + if(x_in_buf < 0 || x_in_buf % pad->draw.pixelstep[plane]) return 1; - x_in_buf /= pad->line_step[plane]; + x_in_buf /= pad->draw.pixelstep[plane]; av_assert0(outpicref->buf->linesize[plane]>0); //while reference can use negative linesize the main buffer should not @@ -304,17 +264,17 @@ static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref) AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0); int plane; - for (plane = 0; plane < 4 && outpicref->data[plane]; plane++) { - int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0; - int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0; + for (plane = 0; plane < 4 && outpicref->data[plane] && pad->draw.pixelstep[plane]; plane++) { + int hsub = pad->draw.hsub[plane]; + int vsub = pad->draw.vsub[plane]; av_assert0(outpicref->buf->w>0 && outpicref->buf->h>0); if(outpicref->format != outpicref->buf->format) //unsupported currently break; - outpicref->data[plane] -= (pad->x >> hsub) * pad ->line_step[plane] - + (pad->y >> vsub) * outpicref->linesize [plane]; + outpicref->data[plane] -= (pad->x >> hsub) * pad->draw.pixelstep[plane] + + (pad->y >> vsub) * outpicref->linesize[plane]; if( does_clip(pad, outpicref, plane, hsub, vsub, 0, 0) || does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1) @@ -338,13 +298,7 @@ static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref) outpicref->video->w = pad->w; outpicref->video->h = pad->h; - ff_start_frame(inlink->dst->outputs[0], outpicref); -} - -static void end_frame(AVFilterLink *link) -{ - ff_end_frame(link->dst->outputs[0]); - avfilter_unref_buffer(link->cur_buf); + ff_start_frame(inlink->dst->outputs[0], avfilter_ref_buffer(outpicref, ~0)); } static void draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice) @@ -363,9 +317,9 @@ static void draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, } if (bar_h) { - ff_draw_rectangle(link->dst->outputs[0]->out_buf->data, + ff_fill_rectangle(&pad->draw, &pad->color, + link->dst->outputs[0]->out_buf->data, link->dst->outputs[0]->out_buf->linesize, - pad->line, pad->line_step, pad->hsub, pad->vsub, 0, bar_y, pad->w, bar_h); ff_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir); } @@ -379,27 +333,26 @@ static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir) y += pad->y; - y &= ~((1 << pad->vsub) - 1); - h &= ~((1 << pad->vsub) - 1); + y = ff_draw_round_to_sub(&pad->draw, 1, -1, y); + h = ff_draw_round_to_sub(&pad->draw, 1, -1, h); if (!h) return; draw_send_bar_slice(link, y, h, slice_dir, 1); /* left border */ - ff_draw_rectangle(outpic->data, outpic->linesize, pad->line, pad->line_step, - pad->hsub, pad->vsub, 0, y, pad->x, h); + ff_fill_rectangle(&pad->draw, &pad->color, outpic->data, outpic->linesize, + 0, y, pad->x, h); if(pad->needs_copy){ - ff_copy_rectangle(outpic->data, outpic->linesize, - inpic->data, inpic->linesize, pad->line_step, - pad->hsub, pad->vsub, - pad->x, y, y-pad->y, inpic->video->w, h); + ff_copy_rectangle2(&pad->draw, + outpic->data, outpic->linesize, + inpic ->data, inpic ->linesize, + pad->x, y, 0, y - pad->y, inpic->video->w, h); } /* right border */ - ff_draw_rectangle(outpic->data, outpic->linesize, - pad->line, pad->line_step, pad->hsub, pad->vsub, + ff_fill_rectangle(&pad->draw, &pad->color, outpic->data, outpic->linesize, pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h); ff_draw_slice(link->dst->outputs[0], y, h, slice_dir); @@ -412,19 +365,17 @@ AVFilter avfilter_vf_pad = { .priv_size = sizeof(PadContext), .init = init, - .uninit = uninit, .query_formats = query_formats, - .inputs = (AVFilterPad[]) {{ .name = "default", + .inputs = (const AVFilterPad[]) {{ .name = "default", .type = AVMEDIA_TYPE_VIDEO, .config_props = config_input, .get_video_buffer = get_video_buffer, .start_frame = start_frame, - .draw_slice = draw_slice, - .end_frame = end_frame, }, + .draw_slice = draw_slice, }, { .name = NULL}}, - .outputs = (AVFilterPad[]) {{ .name = "default", + .outputs = (const AVFilterPad[]) {{ .name = "default", .type = AVMEDIA_TYPE_VIDEO, .config_props = config_output, }, { .name = NULL}}, |