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
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* This file is part of Libav.
|
2010-10-19 00:29:37 +03:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2010-10-19 00:29:37 +03:00
|
|
|
* 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.
|
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2010-10-19 00:29:37 +03:00
|
|
|
* 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
|
2011-03-18 19:35:10 +02:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2010-10-19 00:29:37 +03:00
|
|
|
* 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>
|
|
|
|
|
2010-10-19 00:29:37 +03:00
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "libavutil/pixdesc.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"
|
2013-02-25 23:21:29 +03:00
|
|
|
#include "libavutil/opt.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
|
|
|
|
2013-02-25 23:21:29 +03:00
|
|
|
enum TransposeDir {
|
|
|
|
TRANSPOSE_CCLOCK_FLIP,
|
|
|
|
TRANSPOSE_CLOCK,
|
|
|
|
TRANSPOSE_CCLOCK,
|
|
|
|
TRANSPOSE_CLOCK_FLIP,
|
|
|
|
};
|
|
|
|
|
2010-10-19 00:29:37 +03:00
|
|
|
typedef struct {
|
2013-02-25 23:21:29 +03:00
|
|
|
const AVClass *class;
|
2010-10-19 00:29:37 +03:00
|
|
|
int hsub, vsub;
|
|
|
|
int pixsteps[4];
|
|
|
|
|
2013-02-25 23:21:29 +03:00
|
|
|
enum TransposeDir dir;
|
2010-10-19 00:29:37 +03:00
|
|
|
} TransContext;
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
2012-10-06 13:10:34 +03:00
|
|
|
enum AVPixelFormat pix_fmts[] = {
|
|
|
|
AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
|
|
|
|
AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
|
|
|
|
AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
|
|
|
|
AV_PIX_FMT_RGB565BE, AV_PIX_FMT_RGB565LE,
|
|
|
|
AV_PIX_FMT_RGB555BE, AV_PIX_FMT_RGB555LE,
|
|
|
|
AV_PIX_FMT_BGR565BE, AV_PIX_FMT_BGR565LE,
|
|
|
|
AV_PIX_FMT_BGR555BE, AV_PIX_FMT_BGR555LE,
|
|
|
|
AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE,
|
|
|
|
AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUV420P16BE,
|
|
|
|
AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUV422P16BE,
|
|
|
|
AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUV444P16BE,
|
|
|
|
AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
|
|
|
|
AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8,
|
|
|
|
AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
|
|
|
|
AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
|
|
|
|
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
|
|
|
|
AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
|
|
|
|
AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
|
|
|
|
AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
|
|
|
|
AV_PIX_FMT_YUVA420P, AV_PIX_FMT_GRAY8,
|
|
|
|
AV_PIX_FMT_NONE
|
2010-10-19 00:29:37 +03:00
|
|
|
};
|
|
|
|
|
2012-05-30 11:12:55 +03:00
|
|
|
ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
|
2010-10-19 00:29:37 +03: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 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
|
|
|
|
2012-10-06 14:29:37 +03:00
|
|
|
trans->hsub = desc_in->log2_chroma_w;
|
|
|
|
trans->vsub = desc_in->log2_chroma_h;
|
2010-10-19 00:29:37 +03:00
|
|
|
|
2012-10-06 14:29:37 +03:00
|
|
|
av_image_fill_max_pixsteps(trans->pixsteps, NULL, desc_out);
|
2010-10-19 00:29:37 +03:00
|
|
|
|
|
|
|
outlink->w = inlink->h;
|
|
|
|
outlink->h = inlink->w;
|
|
|
|
|
2011-02-02 21:39:56 +02:00
|
|
|
if (inlink->sample_aspect_ratio.num){
|
|
|
|
outlink->sample_aspect_ratio = av_div_q((AVRational){1,1}, inlink->sample_aspect_ratio);
|
|
|
|
} else
|
|
|
|
outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
|
|
|
|
|
2012-06-25 07:31:38 +03:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
|
2010-10-19 00:29:37 +03: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;
|
|
|
|
}
|
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
2010-10-19 00:29:37 +03:00
|
|
|
{
|
|
|
|
AVFilterLink *outlink = inlink->dst->outputs[0];
|
2012-11-27 09:49:45 +03:00
|
|
|
TransContext *trans = inlink->dst->priv;
|
2012-11-28 10:41:07 +03:00
|
|
|
AVFrame *out;
|
2012-11-27 09:49:45 +03:00
|
|
|
int plane;
|
2010-10-19 00:29:37 +03:00
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
|
2012-11-27 09:49:45 +03:00
|
|
|
if (!out) {
|
2012-11-28 10:41:07 +03:00
|
|
|
av_frame_free(&in);
|
2012-07-08 18:29:42 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2012-11-27 09:49:45 +03:00
|
|
|
}
|
2012-07-08 18:29:42 +03:00
|
|
|
|
2012-11-27 09:49:45 +03:00
|
|
|
out->pts = in->pts;
|
2010-10-19 00:29:37 +03:00
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
if (in->sample_aspect_ratio.num == 0) {
|
|
|
|
out->sample_aspect_ratio = in->sample_aspect_ratio;
|
2010-10-19 00:29:37 +03:00
|
|
|
} else {
|
2012-11-28 10:41:07 +03:00
|
|
|
out->sample_aspect_ratio.num = in->sample_aspect_ratio.den;
|
|
|
|
out->sample_aspect_ratio.den = in->sample_aspect_ratio.num;
|
2010-10-19 00:29:37 +03:00
|
|
|
}
|
|
|
|
|
2012-11-27 09:49:45 +03:00
|
|
|
for (plane = 0; out->data[plane]; plane++) {
|
2010-10-19 00:29:37 +03:00
|
|
|
int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
|
|
|
|
int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
|
|
|
|
int pixstep = trans->pixsteps[plane];
|
2012-11-28 10:41:07 +03:00
|
|
|
int inh = in->height >> vsub;
|
|
|
|
int outw = out->width >> hsub;
|
|
|
|
int outh = out->height >> vsub;
|
2012-11-27 09:49:45 +03:00
|
|
|
uint8_t *dst, *src;
|
|
|
|
int dstlinesize, srclinesize;
|
2010-10-19 00:29:37 +03:00
|
|
|
int x, y;
|
|
|
|
|
2012-11-27 09:49:45 +03:00
|
|
|
dst = out->data[plane];
|
|
|
|
dstlinesize = out->linesize[plane];
|
|
|
|
src = in->data[plane];
|
|
|
|
srclinesize = in->linesize[plane];
|
2010-10-19 00:29:37 +03:00
|
|
|
|
|
|
|
if (trans->dir&1) {
|
2012-11-27 09:49:45 +03:00
|
|
|
src += in->linesize[plane] * (inh-1);
|
|
|
|
srclinesize *= -1;
|
2010-10-19 00:29:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (trans->dir&2) {
|
2012-11-27 09:49:45 +03:00
|
|
|
dst += out->linesize[plane] * (outh-1);
|
|
|
|
dstlinesize *= -1;
|
2010-10-19 00:29:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (y = 0; y < outh; y++) {
|
|
|
|
switch (pixstep) {
|
|
|
|
case 1:
|
|
|
|
for (x = 0; x < outw; x++)
|
2012-11-27 09:49:45 +03:00
|
|
|
dst[x] = src[x*srclinesize + y];
|
2010-10-19 00:29:37 +03:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
for (x = 0; x < outw; x++)
|
2012-11-27 09:49:45 +03:00
|
|
|
*((uint16_t *)(dst + 2*x)) = *((uint16_t *)(src + x*srclinesize + y*2));
|
2010-10-19 00:29:37 +03:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
for (x = 0; x < outw; x++) {
|
2012-11-27 09:49:45 +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
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
for (x = 0; x < outw; x++)
|
2012-11-27 09:49:45 +03:00
|
|
|
*((uint32_t *)(dst + 4*x)) = *((uint32_t *)(src + x*srclinesize + y*4));
|
2010-10-19 00:29:37 +03:00
|
|
|
break;
|
|
|
|
}
|
2012-11-27 09:49:45 +03:00
|
|
|
dst += dstlinesize;
|
2010-10-19 00:29:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
av_frame_free(&in);
|
2012-11-27 09:49:45 +03:00
|
|
|
return ff_filter_frame(outlink, out);
|
2010-10-19 00:29:37 +03:00
|
|
|
}
|
|
|
|
|
2013-02-25 23:21:29 +03:00
|
|
|
#define OFFSET(x) offsetof(TransContext, x)
|
|
|
|
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
|
|
|
|
static const AVOption options[] = {
|
|
|
|
{ "dir", "Transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP },
|
|
|
|
TRANSPOSE_CCLOCK_FLIP, TRANSPOSE_CLOCK_FLIP, FLAGS, "dir" },
|
|
|
|
{ "cclock_flip", "counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .unit = "dir" },
|
|
|
|
{ "clock", "clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK }, .unit = "dir" },
|
|
|
|
{ "cclock", "counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK }, .unit = "dir" },
|
|
|
|
{ "clock_flip", "clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP }, .unit = "dir" },
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass transpose_class = {
|
|
|
|
.class_name = "transpose",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2012-07-24 16:14:01 +03:00
|
|
|
static const AVFilterPad avfilter_vf_transpose_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
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 }
|
|
|
|
};
|
|
|
|
|
2010-10-19 00:29:37 +03:00
|
|
|
AVFilter avfilter_vf_transpose = {
|
|
|
|
.name = "transpose",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Transpose input video."),
|
|
|
|
|
|
|
|
.priv_size = sizeof(TransContext),
|
2013-02-25 23:21:29 +03:00
|
|
|
.priv_class = &transpose_class,
|
2010-10-19 00:29:37 +03:00
|
|
|
|
|
|
|
.query_formats = query_formats,
|
|
|
|
|
2012-07-24 16:14:01 +03:00
|
|
|
.inputs = avfilter_vf_transpose_inputs,
|
|
|
|
.outputs = avfilter_vf_transpose_outputs,
|
2010-10-19 00:29:37 +03:00
|
|
|
};
|