FFmpeg/libavfilter/vf_tile.c
Michael Niedermayer a05a44e205 Merge commit '7e350379f87e7f74420b4813170fe808e2313911'
* commit '7e350379f87e7f74420b4813170fe808e2313911':
  lavfi: switch to AVFrame.

Conflicts:
	doc/filters.texi
	libavfilter/af_ashowinfo.c
	libavfilter/audio.c
	libavfilter/avfilter.c
	libavfilter/avfilter.h
	libavfilter/buffersink.c
	libavfilter/buffersrc.c
	libavfilter/buffersrc.h
	libavfilter/f_select.c
	libavfilter/f_setpts.c
	libavfilter/fifo.c
	libavfilter/split.c
	libavfilter/src_movie.c
	libavfilter/version.h
	libavfilter/vf_aspect.c
	libavfilter/vf_bbox.c
	libavfilter/vf_blackframe.c
	libavfilter/vf_delogo.c
	libavfilter/vf_drawbox.c
	libavfilter/vf_drawtext.c
	libavfilter/vf_fade.c
	libavfilter/vf_fieldorder.c
	libavfilter/vf_fps.c
	libavfilter/vf_frei0r.c
	libavfilter/vf_gradfun.c
	libavfilter/vf_hqdn3d.c
	libavfilter/vf_lut.c
	libavfilter/vf_overlay.c
	libavfilter/vf_pad.c
	libavfilter/vf_scale.c
	libavfilter/vf_showinfo.c
	libavfilter/vf_transpose.c
	libavfilter/vf_vflip.c
	libavfilter/vf_yadif.c
	libavfilter/video.c
	libavfilter/vsrc_testsrc.c
	libavfilter/yadif.h

Following are notes about the merge authorship and various technical details.

Michael Niedermayer:
  * Main merge operation, notably avfilter.c and video.c
  * Switch to AVFrame:
    - afade
    - anullsrc
    - apad
    - aresample
    - blackframe
    - deshake
    - idet
    - il
    - mandelbrot
    - mptestsrc
    - noise
    - setfield
    - smartblur
    - tinterlace
  * various merge changes and fixes in:
    - ashowinfo
    - blackdetect
    - field
    - fps
    - select
    - testsrc
    - yadif

Nicolas George:
  * Switch to AVFrame:
    - make rawdec work with refcounted frames. Adapted from commit
      759001c534 by Anton Khirnov.
      Also, fix the use of || instead of | in a flags check.
    - make buffer sink and src, audio and video work all together

Clément Bœsch:
  * Switch to AVFrame:
    - aevalsrc
    - alphaextract
    - blend
    - cellauto
    - colormatrix
    - concat
    - earwax
    - ebur128
    - edgedetect
    - geq
    - histeq
    - histogram
    - hue
    - kerndeint
    - life
    - movie
    - mp (with the help of Michael)
    - overlay
    - pad
    - pan
    - pp
    - pp
    - removelogo
    - sendcmd
    - showspectrum
    - showwaves
    - silencedetect
    - stereo3d
    - subtitles
    - super2xsai
    - swapuv
    - thumbnail
    - tile

Hendrik Leppkes:
  * Switch to AVFrame:
    - aconvert
    - amerge
    - asetnsamples
    - atempo
    - biquads

Matthieu Bouron:
  * Switch to AVFrame
    - alphamerge
    - decimate
    - volumedetect

Stefano Sabatini:
  * Switch to AVFrame:
    - astreamsync
    - flite
    - framestep

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
Signed-off-by: Nicolas George <nicolas.george@normalesup.org>
Signed-off-by: Clément Bœsch <ubitux@gmail.com>
Signed-off-by: Hendrik Leppkes <h.leppkes@gmail.com>
Signed-off-by: Matthieu Bouron <matthieu.bouron@gmail.com>
Signed-off-by: Stefano Sabatini <stefasab@gmail.com>

Merged-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-10 01:40:35 +01:00

256 lines
7.9 KiB
C

/*
* Copyright (c) 2012 Nicolas George
*
* 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
*/
/**
* @file
* tile video filter
*/
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "avfilter.h"
#include "drawutils.h"
#include "formats.h"
#include "video.h"
#include "internal.h"
typedef struct {
const AVClass *class;
unsigned w, h;
unsigned margin;
unsigned padding;
unsigned current;
unsigned nb_frames;
FFDrawContext draw;
FFDrawColor blank;
AVFrame *out_ref;
} TileContext;
#define REASONABLE_SIZE 1024
#define OFFSET(x) offsetof(TileContext, x)
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
static const AVOption tile_options[] = {
{ "layout", "set grid size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,
{.str = "6x5"}, 0, 0, FLAGS },
{ "margin", "set outer border margin in pixels", OFFSET(margin),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
{ "padding", "set inner border thickness in pixels", OFFSET(padding),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
{ "nb_frames", "set maximum number of frame to render", OFFSET(nb_frames),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
{NULL},
};
AVFILTER_DEFINE_CLASS(tile);
static av_cold int init(AVFilterContext *ctx, const char *args)
{
TileContext *tile = ctx->priv;
static const char *shorthand[] = { "layout", "nb_frames", "margin", "padding", NULL };
int ret;
tile->class = &tile_class;
av_opt_set_defaults(tile);
if ((ret = av_opt_set_from_string(tile, args, shorthand, "=", ":")) < 0)
return ret;
if (tile->w > REASONABLE_SIZE || tile->h > REASONABLE_SIZE) {
av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
tile->w, tile->h);
return AVERROR(EINVAL);
}
if (tile->nb_frames == 0) {
tile->nb_frames = tile->w * tile->h;
} else if (tile->nb_frames > tile->w * tile->h) {
av_log(ctx, AV_LOG_ERROR, "nb_frames must be less than or equal to %dx%d=%d\n",
tile->w, tile->h, tile->w * tile->h);
return AVERROR(EINVAL);
}
return 0;
}
static int query_formats(AVFilterContext *ctx)
{
ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
return 0;
}
static int config_props(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
TileContext *tile = ctx->priv;
AVFilterLink *inlink = ctx->inputs[0];
const unsigned total_margin_w = (tile->w - 1) * tile->padding + 2*tile->margin;
const unsigned total_margin_h = (tile->h - 1) * tile->padding + 2*tile->margin;
if (inlink->w > (INT_MAX - total_margin_w) / tile->w) {
av_log(ctx, AV_LOG_ERROR, "Total width %ux%u is too much.\n",
tile->w, inlink->w);
return AVERROR(EINVAL);
}
if (inlink->h > (INT_MAX - total_margin_h) / tile->h) {
av_log(ctx, AV_LOG_ERROR, "Total height %ux%u is too much.\n",
tile->h, inlink->h);
return AVERROR(EINVAL);
}
outlink->w = tile->w * inlink->w + total_margin_w;
outlink->h = tile->h * inlink->h + total_margin_h;
outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
outlink->frame_rate = av_mul_q(inlink->frame_rate,
(AVRational){ 1, tile->nb_frames });
ff_draw_init(&tile->draw, inlink->format, 0);
/* TODO make the color an option, or find an unified way of choosing it */
ff_draw_color(&tile->draw, &tile->blank, (uint8_t[]){ 0, 0, 0, -1 });
return 0;
}
static void get_current_tile_pos(AVFilterContext *ctx, unsigned *x, unsigned *y)
{
TileContext *tile = ctx->priv;
AVFilterLink *inlink = ctx->inputs[0];
const unsigned tx = tile->current % tile->w;
const unsigned ty = tile->current / tile->w;
*x = tile->margin + (inlink->w + tile->padding) * tx;
*y = tile->margin + (inlink->h + tile->padding) * ty;
}
static void draw_blank_frame(AVFilterContext *ctx, AVFrame *out_buf)
{
TileContext *tile = ctx->priv;
AVFilterLink *inlink = ctx->inputs[0];
unsigned x0, y0;
get_current_tile_pos(ctx, &x0, &y0);
ff_fill_rectangle(&tile->draw, &tile->blank,
out_buf->data, out_buf->linesize,
x0, y0, inlink->w, inlink->h);
tile->current++;
}
static int end_last_frame(AVFilterContext *ctx)
{
TileContext *tile = ctx->priv;
AVFilterLink *outlink = ctx->outputs[0];
AVFrame *out_buf = tile->out_ref;
int ret;
while (tile->current < tile->nb_frames)
draw_blank_frame(ctx, out_buf);
ret = ff_filter_frame(outlink, out_buf);
tile->current = 0;
return ret;
}
/* Note: direct rendering is not possible since there is no guarantee that
* buffers are fed to filter_frame in the order they were obtained from
* get_buffer (think B-frames). */
static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
{
AVFilterContext *ctx = inlink->dst;
TileContext *tile = ctx->priv;
AVFilterLink *outlink = ctx->outputs[0];
unsigned x0, y0;
if (!tile->current) {
tile->out_ref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
if (!tile->out_ref)
return AVERROR(ENOMEM);
av_frame_copy_props(tile->out_ref, picref);
tile->out_ref->width = outlink->w;
tile->out_ref->height = outlink->h;
/* fill surface once for margin/padding */
if (tile->margin || tile->padding)
ff_fill_rectangle(&tile->draw, &tile->blank,
tile->out_ref->data,
tile->out_ref->linesize,
0, 0, outlink->w, outlink->h);
}
get_current_tile_pos(ctx, &x0, &y0);
ff_copy_rectangle2(&tile->draw,
tile->out_ref->data, tile->out_ref->linesize,
picref->data, picref->linesize,
x0, y0, 0, 0, inlink->w, inlink->h);
av_frame_free(&picref);
if (++tile->current == tile->nb_frames)
return end_last_frame(ctx);
return 0;
}
static int request_frame(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
TileContext *tile = ctx->priv;
AVFilterLink *inlink = ctx->inputs[0];
int r;
while (1) {
r = ff_request_frame(inlink);
if (r < 0) {
if (r == AVERROR_EOF && tile->current)
r = end_last_frame(ctx);
break;
}
if (!tile->current) /* done */
break;
}
return r;
}
static const AVFilterPad tile_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.filter_frame = filter_frame,
},
{ NULL }
};
static const AVFilterPad tile_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.config_props = config_props,
.request_frame = request_frame,
},
{ NULL }
};
AVFilter avfilter_vf_tile = {
.name = "tile",
.description = NULL_IF_CONFIG_SMALL("Tile several successive frames together."),
.init = init,
.query_formats = query_formats,
.priv_size = sizeof(TileContext),
.inputs = tile_inputs,
.outputs = tile_outputs,
.priv_class = &tile_class,
};