]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vf_pad.c
Merge commit '7c5012127fb7e18f0616011257bb4248f6a8b608'
[ffmpeg] / libavfilter / vf_pad.c
index 262a41648830c2f59113abc64a076c80570f8665..7d2ea92a7f259e38598cc38b25f43df5becfd987 100644 (file)
@@ -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
  */
 
@@ -29,6 +29,7 @@
 #include "internal.h"
 #include "video.h"
 #include "libavutil/avstring.h"
+#include "libavutil/common.h"
 #include "libavutil/eval.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/colorspace.h"
@@ -39,9 +40,6 @@
 #include "drawutils.h"
 
 static const char *const var_names[] = {
-    "PI",
-    "PHI",
-    "E",
     "in_w",   "iw",
     "in_h",   "ih",
     "out_w",  "ow",
@@ -49,15 +47,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,
@@ -65,6 +62,8 @@ enum var_name {
     VAR_X,
     VAR_Y,
     VAR_A,
+    VAR_SAR,
+    VAR_DAR,
     VAR_HSUB,
     VAR_VSUB,
     VARS_NB
@@ -72,22 +71,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;
 }
 
@@ -101,10 +85,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;
 
@@ -119,49 +102,36 @@ static av_cold int init(AVFilterContext *ctx, const char *args)
     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),
@@ -208,22 +178,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_VERBOSE, "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_VERBOSE, "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                      ||
@@ -256,22 +220,22 @@ 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 = ff_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;
 
+    if (!picref)
+        return NULL;
+
     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;
 }
@@ -281,12 +245,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
 
@@ -309,17 +273,17 @@ static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
     if (!outpicref)
         return AVERROR(ENOMEM);
 
-    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)
@@ -328,7 +292,7 @@ static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
           )
             break;
     }
-    pad->needs_copy= plane < 4 && outpicref->data[plane];
+    pad->needs_copy= plane < 4 && outpicref->data[plane] || !(outpicref->perms & AV_PERM_WRITE);
     if(pad->needs_copy){
         av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
         avfilter_unref_buffer(outpicref);
@@ -362,11 +326,6 @@ fail:
     return ret;
 }
 
-static int end_frame(AVFilterLink *link)
-{
-    return ff_end_frame(link->dst->outputs[0]);
-}
-
 static int draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
 {
     PadContext *pad = link->dst->priv;
@@ -383,9 +342,9 @@ static int 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);
         ret = ff_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
     }
@@ -401,27 +360,26 @@ static int 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 0;
     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);
     ret = ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
     if (ret < 0)
@@ -436,7 +394,6 @@ AVFilter avfilter_vf_pad = {
 
     .priv_size     = sizeof(PadContext),
     .init          = init,
-    .uninit        = uninit,
     .query_formats = query_formats,
 
     .inputs    = (const AVFilterPad[]) {{ .name             = "default",
@@ -444,8 +401,7 @@ AVFilter avfilter_vf_pad = {
                                           .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   = (const AVFilterPad[]) {{ .name             = "default",