2010-10-19 00:29:37 +03:00
|
|
|
/*
|
2010-11-28 12:22:58 +02:00
|
|
|
* Copyright (c) 2010 Stefano Sabatini
|
|
|
|
* Copyright (c) 2008 Vitor Sessak
|
2010-10-19 00:29:37 +03:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* transposition filter
|
|
|
|
* Based on MPlayer libmpcodecs/vf_rotate.c.
|
|
|
|
*/
|
|
|
|
|
2012-08-06 16:49:32 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2011-02-07 15:37:08 +02:00
|
|
|
#include "libavutil/imgutils.h"
|
2012-08-06 16:49:32 +03:00
|
|
|
#include "libavutil/internal.h"
|
2014-03-25 10:39:24 +03:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2013-02-25 23:21:29 +03:00
|
|
|
#include "libavutil/opt.h"
|
2014-03-25 10:39:24 +03:00
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
|
2010-10-19 00:29:37 +03:00
|
|
|
#include "avfilter.h"
|
2012-05-30 11:12:55 +03:00
|
|
|
#include "formats.h"
|
2012-06-12 21:12:42 +03:00
|
|
|
#include "internal.h"
|
2012-05-30 12:20:32 +03:00
|
|
|
#include "video.h"
|
2010-10-19 00:29:37 +03:00
|
|
|
|
2012-09-30 14:46:01 +03:00
|
|
|
typedef enum {
|
|
|
|
TRANSPOSE_PT_TYPE_NONE,
|
|
|
|
TRANSPOSE_PT_TYPE_LANDSCAPE,
|
|
|
|
TRANSPOSE_PT_TYPE_PORTRAIT,
|
|
|
|
} PassthroughType;
|
|
|
|
|
2013-02-25 23:21:29 +03:00
|
|
|
enum TransposeDir {
|
|
|
|
TRANSPOSE_CCLOCK_FLIP,
|
|
|
|
TRANSPOSE_CLOCK,
|
|
|
|
TRANSPOSE_CCLOCK,
|
|
|
|
TRANSPOSE_CLOCK_FLIP,
|
|
|
|
};
|
|
|
|
|
2014-04-11 12:54:15 +03:00
|
|
|
typedef struct TransContext {
|
2012-09-30 14:03:08 +03:00
|
|
|
const AVClass *class;
|
2010-10-19 00:29:37 +03:00
|
|
|
int hsub, vsub;
|
|
|
|
int pixsteps[4];
|
|
|
|
|
2015-03-23 20:31:17 +02:00
|
|
|
int passthrough; ///< PassthroughType, landscape passthrough mode enabled
|
|
|
|
int dir; ///< TransposeDir
|
2010-10-19 00:29:37 +03:00
|
|
|
} TransContext;
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
2013-02-12 16:45:23 +03:00
|
|
|
AVFilterFormats *pix_fmts = NULL;
|
|
|
|
int fmt;
|
|
|
|
|
2014-05-27 18:43:03 +03:00
|
|
|
for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
|
2013-02-12 16:45:23 +03:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
|
2013-05-15 12:23:14 +03:00
|
|
|
if (!(desc->flags & AV_PIX_FMT_FLAG_PAL ||
|
|
|
|
desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
|
|
|
|
desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ||
|
2013-02-12 16:45:23 +03:00
|
|
|
desc->log2_chroma_w != desc->log2_chroma_h))
|
|
|
|
ff_add_format(&pix_fmts, fmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-03 19:55:18 +02:00
|
|
|
return ff_set_common_formats(ctx, pix_fmts);
|
2010-10-19 00:29:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int config_props_output(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
2015-09-03 17:18:59 +02:00
|
|
|
TransContext *s = ctx->priv;
|
2010-10-19 00:29:37 +03:00
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
2012-10-06 14:29:37 +03:00
|
|
|
const AVPixFmtDescriptor *desc_out = av_pix_fmt_desc_get(outlink->format);
|
|
|
|
const AVPixFmtDescriptor *desc_in = av_pix_fmt_desc_get(inlink->format);
|
2010-10-19 00:29:37 +03:00
|
|
|
|
2015-09-03 17:18:59 +02:00
|
|
|
if (s->dir&4) {
|
2012-09-30 14:46:01 +03:00
|
|
|
av_log(ctx, AV_LOG_WARNING,
|
|
|
|
"dir values greater than 3 are deprecated, use the passthrough option instead\n");
|
2015-09-03 17:18:59 +02:00
|
|
|
s->dir &= 3;
|
|
|
|
s->passthrough = TRANSPOSE_PT_TYPE_LANDSCAPE;
|
2012-09-30 14:46:01 +03:00
|
|
|
}
|
|
|
|
|
2015-09-03 17:18:59 +02:00
|
|
|
if ((inlink->w >= inlink->h && s->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
|
|
|
|
(inlink->w <= inlink->h && s->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
|
2012-09-30 14:46:01 +03:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE,
|
|
|
|
"w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
|
|
|
|
inlink->w, inlink->h, inlink->w, inlink->h);
|
|
|
|
return 0;
|
|
|
|
} else {
|
2015-09-03 17:18:59 +02:00
|
|
|
s->passthrough = TRANSPOSE_PT_TYPE_NONE;
|
2012-09-01 12:14:27 +03:00
|
|
|
}
|
|
|
|
|
2015-09-03 17:18:59 +02:00
|
|
|
s->hsub = desc_in->log2_chroma_w;
|
|
|
|
s->vsub = desc_in->log2_chroma_h;
|
2010-10-19 00:29:37 +03:00
|
|
|
|
2015-09-03 17:18:59 +02:00
|
|
|
av_image_fill_max_pixsteps(s->pixsteps, NULL, desc_out);
|
2010-10-19 00:29:37 +03:00
|
|
|
|
|
|
|
outlink->w = inlink->h;
|
|
|
|
outlink->h = inlink->w;
|
|
|
|
|
2014-03-25 10:39:24 +03:00
|
|
|
if (inlink->sample_aspect_ratio.num)
|
|
|
|
outlink->sample_aspect_ratio = av_div_q((AVRational) { 1, 1 },
|
|
|
|
inlink->sample_aspect_ratio);
|
|
|
|
else
|
2011-01-31 19:51:47 +02:00
|
|
|
outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
|
|
|
|
|
2014-03-25 10:39:24 +03:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE,
|
|
|
|
"w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
|
2015-09-03 17:18:59 +02:00
|
|
|
inlink->w, inlink->h, s->dir, outlink->w, outlink->h,
|
|
|
|
s->dir == 1 || s->dir == 3 ? "clockwise" : "counterclockwise",
|
|
|
|
s->dir == 0 || s->dir == 3);
|
2010-10-19 00:29:37 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-10 03:30:30 +03:00
|
|
|
static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
|
2012-09-01 12:14:27 +03:00
|
|
|
{
|
2015-09-03 17:18:59 +02:00
|
|
|
TransContext *s = inlink->dst->priv;
|
2012-09-01 12:14:27 +03:00
|
|
|
|
2015-09-03 17:18:59 +02:00
|
|
|
return s->passthrough ?
|
2013-03-10 03:30:30 +03:00
|
|
|
ff_null_get_video_buffer (inlink, w, h) :
|
|
|
|
ff_default_get_video_buffer(inlink, w, h);
|
2012-09-01 12:14:27 +03:00
|
|
|
}
|
|
|
|
|
2013-08-14 22:01:06 +03:00
|
|
|
typedef struct ThreadData {
|
|
|
|
AVFrame *in, *out;
|
|
|
|
} ThreadData;
|
|
|
|
|
|
|
|
static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr,
|
|
|
|
int nb_jobs)
|
2010-10-19 00:29:37 +03:00
|
|
|
{
|
2015-09-03 17:18:59 +02:00
|
|
|
TransContext *s = ctx->priv;
|
2013-08-14 22:01:06 +03:00
|
|
|
ThreadData *td = arg;
|
|
|
|
AVFrame *out = td->out;
|
|
|
|
AVFrame *in = td->in;
|
2012-11-28 23:46:23 +03:00
|
|
|
int plane;
|
2010-10-19 00:29:37 +03:00
|
|
|
|
2012-11-28 23:46:23 +03:00
|
|
|
for (plane = 0; out->data[plane]; plane++) {
|
2015-09-03 17:18:59 +02:00
|
|
|
int hsub = plane == 1 || plane == 2 ? s->hsub : 0;
|
|
|
|
int vsub = plane == 1 || plane == 2 ? s->vsub : 0;
|
|
|
|
int pixstep = s->pixsteps[plane];
|
2015-06-30 16:01:15 +02:00
|
|
|
int inh = FF_CEIL_RSHIFT(in->height, vsub);
|
2014-03-25 21:17:38 +03:00
|
|
|
int outw = FF_CEIL_RSHIFT(out->width, hsub);
|
|
|
|
int outh = FF_CEIL_RSHIFT(out->height, vsub);
|
|
|
|
int start = (outh * jobnr ) / nb_jobs;
|
|
|
|
int end = (outh * (jobnr+1)) / nb_jobs;
|
2012-11-28 23:46:23 +03:00
|
|
|
uint8_t *dst, *src;
|
|
|
|
int dstlinesize, srclinesize;
|
2010-10-19 00:29:37 +03:00
|
|
|
int x, y;
|
|
|
|
|
2012-11-28 23:46:23 +03:00
|
|
|
dstlinesize = out->linesize[plane];
|
2014-03-25 21:17:38 +03:00
|
|
|
dst = out->data[plane] + start * dstlinesize;
|
2014-03-25 10:39:24 +03:00
|
|
|
src = in->data[plane];
|
2012-11-28 23:46:23 +03:00
|
|
|
srclinesize = in->linesize[plane];
|
2010-10-19 00:29:37 +03:00
|
|
|
|
2015-09-03 17:18:59 +02:00
|
|
|
if (s->dir & 1) {
|
2014-03-25 10:39:24 +03:00
|
|
|
src += in->linesize[plane] * (inh - 1);
|
2012-11-28 23:46:23 +03:00
|
|
|
srclinesize *= -1;
|
2010-10-19 00:29:37 +03:00
|
|
|
}
|
|
|
|
|
2015-09-03 17:18:59 +02:00
|
|
|
if (s->dir & 2) {
|
2014-03-25 21:17:38 +03:00
|
|
|
dst = out->data[plane] + dstlinesize * (outh - start - 1);
|
2012-11-28 23:46:23 +03:00
|
|
|
dstlinesize *= -1;
|
2010-10-19 00:29:37 +03:00
|
|
|
}
|
|
|
|
|
2013-09-11 17:56:46 +03:00
|
|
|
switch (pixstep) {
|
|
|
|
case 1:
|
|
|
|
for (y = start; y < end; y++, dst += dstlinesize)
|
2010-10-19 00:29:37 +03:00
|
|
|
for (x = 0; x < outw; x++)
|
2014-03-25 10:39:24 +03:00
|
|
|
dst[x] = src[x * srclinesize + y];
|
2013-09-11 17:56:46 +03:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
for (y = start; y < end; y++, dst += dstlinesize) {
|
2010-10-19 00:29:37 +03:00
|
|
|
for (x = 0; x < outw; x++)
|
2014-03-25 10:39:24 +03:00
|
|
|
*((uint16_t *)(dst + 2 * x)) =
|
|
|
|
*((uint16_t *)(src + x * srclinesize + y * 2));
|
2013-09-11 17:56:46 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
for (y = start; y < end; y++, dst += dstlinesize) {
|
2010-10-19 00:29:37 +03:00
|
|
|
for (x = 0; x < outw; x++) {
|
2014-03-25 10:39:24 +03:00
|
|
|
int32_t v = AV_RB24(src + x * srclinesize + y * 3);
|
|
|
|
AV_WB24(dst + 3 * x, v);
|
2010-10-19 00:29:37 +03:00
|
|
|
}
|
2013-09-11 17:56:46 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
for (y = start; y < end; y++, dst += dstlinesize) {
|
2010-10-19 00:29:37 +03:00
|
|
|
for (x = 0; x < outw; x++)
|
2014-03-25 10:39:24 +03:00
|
|
|
*((uint32_t *)(dst + 4 * x)) =
|
|
|
|
*((uint32_t *)(src + x * srclinesize + y * 4));
|
2013-09-11 17:56:46 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
for (y = start; y < end; y++, dst += dstlinesize) {
|
2013-02-12 16:45:23 +03:00
|
|
|
for (x = 0; x < outw; x++) {
|
2014-03-25 21:17:38 +03:00
|
|
|
int64_t v = AV_RB48(src + x * srclinesize + y*6);
|
2013-02-12 16:45:23 +03:00
|
|
|
AV_WB48(dst + 6*x, v);
|
|
|
|
}
|
2013-09-11 17:56:46 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
for (y = start; y < end; y++, dst += dstlinesize) {
|
2013-02-12 16:45:23 +03:00
|
|
|
for (x = 0; x < outw; x++)
|
2014-03-25 21:17:38 +03:00
|
|
|
*((uint64_t *)(dst + 8*x)) = *((uint64_t *)(src + x * srclinesize + y*8));
|
2010-10-19 00:29:37 +03:00
|
|
|
}
|
2013-09-11 17:56:46 +03:00
|
|
|
break;
|
2010-10-19 00:29:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-14 22:01:06 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
2015-09-03 17:18:59 +02:00
|
|
|
TransContext *s = ctx->priv;
|
2013-08-14 22:01:06 +03:00
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
ThreadData td;
|
|
|
|
AVFrame *out;
|
|
|
|
|
2015-09-03 17:18:59 +02:00
|
|
|
if (s->passthrough)
|
2013-08-14 22:01:06 +03:00
|
|
|
return ff_filter_frame(outlink, in);
|
|
|
|
|
|
|
|
out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
|
|
|
|
if (!out) {
|
|
|
|
av_frame_free(&in);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2013-08-15 00:34:27 +03:00
|
|
|
av_frame_copy_props(out, in);
|
2013-08-14 22:01:06 +03:00
|
|
|
|
|
|
|
if (in->sample_aspect_ratio.num == 0) {
|
|
|
|
out->sample_aspect_ratio = in->sample_aspect_ratio;
|
|
|
|
} else {
|
|
|
|
out->sample_aspect_ratio.num = in->sample_aspect_ratio.den;
|
|
|
|
out->sample_aspect_ratio.den = in->sample_aspect_ratio.num;
|
|
|
|
}
|
|
|
|
|
|
|
|
td.in = in, td.out = out;
|
|
|
|
ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ctx->graph->nb_threads));
|
2012-11-28 10:41:07 +03:00
|
|
|
av_frame_free(&in);
|
2012-11-28 23:46:23 +03:00
|
|
|
return ff_filter_frame(outlink, out);
|
2012-09-01 12:14:27 +03:00
|
|
|
}
|
2011-07-29 21:19:04 +03:00
|
|
|
|
2013-02-25 23:21:29 +03:00
|
|
|
#define OFFSET(x) offsetof(TransContext, x)
|
2013-04-11 01:32:50 +03:00
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
|
|
|
|
|
|
|
|
static const AVOption transpose_options[] = {
|
2013-04-27 21:38:32 +03:00
|
|
|
{ "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP }, 0, 7, FLAGS, "dir" },
|
|
|
|
{ "cclock_flip", "rotate counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .unit = "dir" },
|
|
|
|
{ "clock", "rotate clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK }, .unit = "dir" },
|
|
|
|
{ "cclock", "rotate counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK }, .unit = "dir" },
|
|
|
|
{ "clock_flip", "rotate clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP }, .unit = "dir" },
|
2013-04-11 01:32:50 +03:00
|
|
|
|
|
|
|
{ "passthrough", "do not apply transposition if the input matches the specified geometry",
|
|
|
|
OFFSET(passthrough), AV_OPT_TYPE_INT, {.i64=TRANSPOSE_PT_TYPE_NONE}, 0, INT_MAX, FLAGS, "passthrough" },
|
2013-04-27 20:30:19 +03:00
|
|
|
{ "none", "always apply transposition", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_NONE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
|
|
|
|
{ "portrait", "preserve portrait geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_PORTRAIT}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
|
|
|
|
{ "landscape", "preserve landscape geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_LANDSCAPE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
|
2013-04-11 01:32:50 +03:00
|
|
|
|
2013-09-07 15:13:50 +03:00
|
|
|
{ NULL }
|
2013-02-25 23:21:29 +03:00
|
|
|
};
|
|
|
|
|
2013-04-11 01:32:50 +03:00
|
|
|
AVFILTER_DEFINE_CLASS(transpose);
|
2013-02-25 23:21:29 +03:00
|
|
|
|
2012-07-24 16:14:01 +03:00
|
|
|
static const AVFilterPad avfilter_vf_transpose_inputs[] = {
|
|
|
|
{
|
2014-03-25 10:39:24 +03:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2013-09-07 15:13:50 +03:00
|
|
|
.get_video_buffer = get_video_buffer,
|
2012-11-27 09:49:45 +03:00
|
|
|
.filter_frame = filter_frame,
|
2012-07-24 16:14:01 +03:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_vf_transpose_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.config_props = config_props_output,
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-10-28 09:44:24 +03:00
|
|
|
AVFilter ff_vf_transpose = {
|
2013-09-07 15:13:50 +03:00
|
|
|
.name = "transpose",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Transpose input video."),
|
|
|
|
.priv_size = sizeof(TransContext),
|
|
|
|
.priv_class = &transpose_class,
|
2010-10-19 00:29:37 +03:00
|
|
|
.query_formats = query_formats,
|
2013-09-07 15:13:50 +03:00
|
|
|
.inputs = avfilter_vf_transpose_inputs,
|
|
|
|
.outputs = avfilter_vf_transpose_outputs,
|
|
|
|
.flags = AVFILTER_FLAG_SLICE_THREADS,
|
2010-10-19 00:29:37 +03:00
|
|
|
};
|