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