Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright (c) 2008 vmrsss
  3.  * Copyright (c) 2009 Stefano Sabatini
  4.  *
  5.  * This file is part of FFmpeg.
  6.  *
  7.  * FFmpeg is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * FFmpeg is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with FFmpeg; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20.  */
  21.  
  22. /**
  23.  * @file
  24.  * video padding filter
  25.  */
  26.  
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "internal.h"
  30. #include "video.h"
  31. #include "libavutil/avstring.h"
  32. #include "libavutil/common.h"
  33. #include "libavutil/eval.h"
  34. #include "libavutil/pixdesc.h"
  35. #include "libavutil/colorspace.h"
  36. #include "libavutil/avassert.h"
  37. #include "libavutil/imgutils.h"
  38. #include "libavutil/parseutils.h"
  39. #include "libavutil/mathematics.h"
  40. #include "libavutil/opt.h"
  41.  
  42. #include "drawutils.h"
  43.  
  44. static const char *const var_names[] = {
  45.     "in_w",   "iw",
  46.     "in_h",   "ih",
  47.     "out_w",  "ow",
  48.     "out_h",  "oh",
  49.     "x",
  50.     "y",
  51.     "a",
  52.     "sar",
  53.     "dar",
  54.     "hsub",
  55.     "vsub",
  56.     NULL
  57. };
  58.  
  59. enum var_name {
  60.     VAR_IN_W,   VAR_IW,
  61.     VAR_IN_H,   VAR_IH,
  62.     VAR_OUT_W,  VAR_OW,
  63.     VAR_OUT_H,  VAR_OH,
  64.     VAR_X,
  65.     VAR_Y,
  66.     VAR_A,
  67.     VAR_SAR,
  68.     VAR_DAR,
  69.     VAR_HSUB,
  70.     VAR_VSUB,
  71.     VARS_NB
  72. };
  73.  
  74. static int query_formats(AVFilterContext *ctx)
  75. {
  76.     return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  77. }
  78.  
  79. typedef struct PadContext {
  80.     const AVClass *class;
  81.     int w, h;               ///< output dimensions, a value of 0 will result in the input size
  82.     int x, y;               ///< offsets of the input area with respect to the padded area
  83.     int in_w, in_h;         ///< width and height for the padded input video, which has to be aligned to the chroma values in order to avoid chroma issues
  84.  
  85.     char *w_expr;           ///< width  expression string
  86.     char *h_expr;           ///< height expression string
  87.     char *x_expr;           ///< width  expression string
  88.     char *y_expr;           ///< height expression string
  89.     uint8_t rgba_color[4];  ///< color for the padding area
  90.     FFDrawContext draw;
  91.     FFDrawColor color;
  92. } PadContext;
  93.  
  94. static int config_input(AVFilterLink *inlink)
  95. {
  96.     AVFilterContext *ctx = inlink->dst;
  97.     PadContext *s = ctx->priv;
  98.     int ret;
  99.     double var_values[VARS_NB], res;
  100.     char *expr;
  101.  
  102.     ff_draw_init(&s->draw, inlink->format, 0);
  103.     ff_draw_color(&s->draw, &s->color, s->rgba_color);
  104.  
  105.     var_values[VAR_IN_W]  = var_values[VAR_IW] = inlink->w;
  106.     var_values[VAR_IN_H]  = var_values[VAR_IH] = inlink->h;
  107.     var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
  108.     var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
  109.     var_values[VAR_A]     = (double) inlink->w / inlink->h;
  110.     var_values[VAR_SAR]   = inlink->sample_aspect_ratio.num ?
  111.         (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  112.     var_values[VAR_DAR]   = var_values[VAR_A] * var_values[VAR_SAR];
  113.     var_values[VAR_HSUB]  = 1 << s->draw.hsub_max;
  114.     var_values[VAR_VSUB]  = 1 << s->draw.vsub_max;
  115.  
  116.     /* evaluate width and height */
  117.     av_expr_parse_and_eval(&res, (expr = s->w_expr),
  118.                            var_names, var_values,
  119.                            NULL, NULL, NULL, NULL, NULL, 0, ctx);
  120.     s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  121.     if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr),
  122.                                       var_names, var_values,
  123.                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  124.         goto eval_fail;
  125.     s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
  126.     /* evaluate the width again, as it may depend on the evaluated output height */
  127.     if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
  128.                                       var_names, var_values,
  129.                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  130.         goto eval_fail;
  131.     s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
  132.  
  133.     /* evaluate x and y */
  134.     av_expr_parse_and_eval(&res, (expr = s->x_expr),
  135.                            var_names, var_values,
  136.                            NULL, NULL, NULL, NULL, NULL, 0, ctx);
  137.     s->x = var_values[VAR_X] = res;
  138.     if ((ret = av_expr_parse_and_eval(&res, (expr = s->y_expr),
  139.                                       var_names, var_values,
  140.                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  141.         goto eval_fail;
  142.     s->y = var_values[VAR_Y] = res;
  143.     /* evaluate x again, as it may depend on the evaluated y value */
  144.     if ((ret = av_expr_parse_and_eval(&res, (expr = s->x_expr),
  145.                                       var_names, var_values,
  146.                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  147.         goto eval_fail;
  148.     s->x = var_values[VAR_X] = res;
  149.  
  150.     /* sanity check params */
  151.     if (s->w < 0 || s->h < 0 || s->x < 0 || s->y < 0) {
  152.         av_log(ctx, AV_LOG_ERROR, "Negative values are not acceptable.\n");
  153.         return AVERROR(EINVAL);
  154.     }
  155.  
  156.     if (!s->w)
  157.         s->w = inlink->w;
  158.     if (!s->h)
  159.         s->h = inlink->h;
  160.  
  161.     s->w    = ff_draw_round_to_sub(&s->draw, 0, -1, s->w);
  162.     s->h    = ff_draw_round_to_sub(&s->draw, 1, -1, s->h);
  163.     s->x    = ff_draw_round_to_sub(&s->draw, 0, -1, s->x);
  164.     s->y    = ff_draw_round_to_sub(&s->draw, 1, -1, s->y);
  165.     s->in_w = ff_draw_round_to_sub(&s->draw, 0, -1, inlink->w);
  166.     s->in_h = ff_draw_round_to_sub(&s->draw, 1, -1, inlink->h);
  167.  
  168.     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",
  169.            inlink->w, inlink->h, s->w, s->h, s->x, s->y,
  170.            s->rgba_color[0], s->rgba_color[1], s->rgba_color[2], s->rgba_color[3]);
  171.  
  172.     if (s->x <  0 || s->y <  0                      ||
  173.         s->w <= 0 || s->h <= 0                      ||
  174.         (unsigned)s->x + (unsigned)inlink->w > s->w ||
  175.         (unsigned)s->y + (unsigned)inlink->h > s->h) {
  176.         av_log(ctx, AV_LOG_ERROR,
  177.                "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
  178.                s->x, s->y, s->x + inlink->w, s->y + inlink->h, s->w, s->h);
  179.         return AVERROR(EINVAL);
  180.     }
  181.  
  182.     return 0;
  183.  
  184. eval_fail:
  185.     av_log(NULL, AV_LOG_ERROR,
  186.            "Error when evaluating the expression '%s'\n", expr);
  187.     return ret;
  188.  
  189. }
  190.  
  191. static int config_output(AVFilterLink *outlink)
  192. {
  193.     PadContext *s = outlink->src->priv;
  194.  
  195.     outlink->w = s->w;
  196.     outlink->h = s->h;
  197.     return 0;
  198. }
  199.  
  200. static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
  201. {
  202.     PadContext *s = inlink->dst->priv;
  203.  
  204.     AVFrame *frame = ff_get_video_buffer(inlink->dst->outputs[0],
  205.                                          w + (s->w - s->in_w),
  206.                                          h + (s->h - s->in_h));
  207.     int plane;
  208.  
  209.     if (!frame)
  210.         return NULL;
  211.  
  212.     frame->width  = w;
  213.     frame->height = h;
  214.  
  215.     for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) {
  216.         int hsub = s->draw.hsub[plane];
  217.         int vsub = s->draw.vsub[plane];
  218.         frame->data[plane] += (s->x >> hsub) * s->draw.pixelstep[plane] +
  219.                               (s->y >> vsub) * frame->linesize[plane];
  220.     }
  221.  
  222.     return frame;
  223. }
  224.  
  225. /* check whether each plane in this buffer can be padded without copying */
  226. static int buffer_needs_copy(PadContext *s, AVFrame *frame, AVBufferRef *buf)
  227. {
  228.     int planes[4] = { -1, -1, -1, -1}, *p = planes;
  229.     int i, j;
  230.  
  231.     /* get all planes in this buffer */
  232.     for (i = 0; i < FF_ARRAY_ELEMS(planes) && frame->data[i]; i++) {
  233.         if (av_frame_get_plane_buffer(frame, i) == buf)
  234.             *p++ = i;
  235.     }
  236.  
  237.     /* for each plane in this buffer, check that it can be padded without
  238.      * going over buffer bounds or other planes */
  239.     for (i = 0; i < FF_ARRAY_ELEMS(planes) && planes[i] >= 0; i++) {
  240.         int hsub = s->draw.hsub[planes[i]];
  241.         int vsub = s->draw.vsub[planes[i]];
  242.  
  243.         uint8_t *start = frame->data[planes[i]];
  244.         uint8_t *end   = start + (frame->height >> vsub) *
  245.                                  frame->linesize[planes[i]];
  246.  
  247.         /* amount of free space needed before the start and after the end
  248.          * of the plane */
  249.         ptrdiff_t req_start = (s->x >> hsub) * s->draw.pixelstep[planes[i]] +
  250.                               (s->y >> vsub) * frame->linesize[planes[i]];
  251.         ptrdiff_t req_end   = ((s->w - s->x - frame->width) >> hsub) *
  252.                               s->draw.pixelstep[planes[i]] +
  253.                               ((s->h - s->y - frame->height) >> vsub) * frame->linesize[planes[i]];
  254.  
  255.         if (frame->linesize[planes[i]] < (s->w >> hsub) * s->draw.pixelstep[planes[i]])
  256.             return 1;
  257.         if (start - buf->data < req_start ||
  258.             (buf->data + buf->size) - end < req_end)
  259.             return 1;
  260.  
  261.         for (j = 0; j < FF_ARRAY_ELEMS(planes) && planes[j] >= 0; j++) {
  262.             int vsub1 = s->draw.vsub[planes[j]];
  263.             uint8_t *start1 = frame->data[planes[j]];
  264.             uint8_t *end1   = start1 + (frame->height >> vsub1) *
  265.                                        frame->linesize[planes[j]];
  266.             if (i == j)
  267.                 continue;
  268.  
  269.             if (FFSIGN(start - end1) != FFSIGN(start - end1 - req_start) ||
  270.                 FFSIGN(end - start1) != FFSIGN(end - start1 + req_end))
  271.                 return 1;
  272.         }
  273.     }
  274.  
  275.     return 0;
  276. }
  277.  
  278. static int frame_needs_copy(PadContext *s, AVFrame *frame)
  279. {
  280.     int i;
  281.  
  282.     if (!av_frame_is_writable(frame))
  283.         return 1;
  284.  
  285.     for (i = 0; i < 4 && frame->buf[i]; i++)
  286.         if (buffer_needs_copy(s, frame, frame->buf[i]))
  287.             return 1;
  288.     return 0;
  289. }
  290.  
  291. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  292. {
  293.     PadContext *s = inlink->dst->priv;
  294.     AVFrame *out;
  295.     int needs_copy = frame_needs_copy(s, in);
  296.  
  297.     if (needs_copy) {
  298.         av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
  299.         out = ff_get_video_buffer(inlink->dst->outputs[0],
  300.                                   FFMAX(inlink->w, s->w),
  301.                                   FFMAX(inlink->h, s->h));
  302.         if (!out) {
  303.             av_frame_free(&in);
  304.             return AVERROR(ENOMEM);
  305.         }
  306.  
  307.         av_frame_copy_props(out, in);
  308.     } else {
  309.         int i;
  310.  
  311.         out = in;
  312.         for (i = 0; i < 4 && out->data[i] && out->linesize[i]; i++) {
  313.             int hsub = s->draw.hsub[i];
  314.             int vsub = s->draw.vsub[i];
  315.             out->data[i] -= (s->x >> hsub) * s->draw.pixelstep[i] +
  316.                             (s->y >> vsub) * out->linesize[i];
  317.         }
  318.     }
  319.  
  320.     /* top bar */
  321.     if (s->y) {
  322.         ff_fill_rectangle(&s->draw, &s->color,
  323.                           out->data, out->linesize,
  324.                           0, 0, s->w, s->y);
  325.     }
  326.  
  327.     /* bottom bar */
  328.     if (s->h > s->y + s->in_h) {
  329.         ff_fill_rectangle(&s->draw, &s->color,
  330.                           out->data, out->linesize,
  331.                           0, s->y + s->in_h, s->w, s->h - s->y - s->in_h);
  332.     }
  333.  
  334.     /* left border */
  335.     ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize,
  336.                       0, s->y, s->x, in->height);
  337.  
  338.     if (needs_copy) {
  339.         ff_copy_rectangle2(&s->draw,
  340.                           out->data, out->linesize, in->data, in->linesize,
  341.                           s->x, s->y, 0, 0, in->width, in->height);
  342.     }
  343.  
  344.     /* right border */
  345.     ff_fill_rectangle(&s->draw, &s->color, out->data, out->linesize,
  346.                       s->x + s->in_w, s->y, s->w - s->x - s->in_w,
  347.                       in->height);
  348.  
  349.     out->width  = s->w;
  350.     out->height = s->h;
  351.  
  352.     if (in != out)
  353.         av_frame_free(&in);
  354.     return ff_filter_frame(inlink->dst->outputs[0], out);
  355. }
  356.  
  357. #define OFFSET(x) offsetof(PadContext, x)
  358. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  359.  
  360. static const AVOption pad_options[] = {
  361.     { "width",  "set the pad area width expression",       OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
  362.     { "w",      "set the pad area width expression",       OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
  363.     { "height", "set the pad area height expression",      OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
  364.     { "h",      "set the pad area height expression",      OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
  365.     { "x",      "set the x offset expression for the input image position", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  366.     { "y",      "set the y offset expression for the input image position", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
  367.     { "color",  "set the color of the padded area border", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS },
  368.     { NULL }
  369. };
  370.  
  371. AVFILTER_DEFINE_CLASS(pad);
  372.  
  373. static const AVFilterPad avfilter_vf_pad_inputs[] = {
  374.     {
  375.         .name             = "default",
  376.         .type             = AVMEDIA_TYPE_VIDEO,
  377.         .config_props     = config_input,
  378.         .get_video_buffer = get_video_buffer,
  379.         .filter_frame     = filter_frame,
  380.     },
  381.     { NULL }
  382. };
  383.  
  384. static const AVFilterPad avfilter_vf_pad_outputs[] = {
  385.     {
  386.         .name         = "default",
  387.         .type         = AVMEDIA_TYPE_VIDEO,
  388.         .config_props = config_output,
  389.     },
  390.     { NULL }
  391. };
  392.  
  393. AVFilter ff_vf_pad = {
  394.     .name          = "pad",
  395.     .description   = NULL_IF_CONFIG_SMALL("Pad the input video."),
  396.     .priv_size     = sizeof(PadContext),
  397.     .priv_class    = &pad_class,
  398.     .query_formats = query_formats,
  399.     .inputs        = avfilter_vf_pad_inputs,
  400.     .outputs       = avfilter_vf_pad_outputs,
  401. };
  402.