h264_metadata: Add option to delete filler data
[libav.git] / libavfilter / vf_transpose.c
CommitLineData
43945b27 1/*
3fa77bde
SS
2 * Copyright (c) 2010 Stefano Sabatini
3 * Copyright (c) 2008 Vitor Sessak
43945b27 4 *
2912e87a 5 * This file is part of Libav.
43945b27 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
43945b27
SS
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
43945b27
SS
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
43945b27
SS
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
23 * @file
24 * transposition filter
25 * Based on MPlayer libmpcodecs/vf_rotate.c.
26 */
27
1d9c2dc8
MS
28#include <stdio.h>
29
737eb597 30#include "libavutil/imgutils.h"
1d9c2dc8 31#include "libavutil/internal.h"
6ee55c7b 32#include "libavutil/intreadwrite.h"
0c2466de 33#include "libavutil/opt.h"
6ee55c7b
VG
34#include "libavutil/pixdesc.h"
35
43945b27 36#include "avfilter.h"
b74a1da4 37#include "formats.h"
9d0bfc50 38#include "internal.h"
803391f7 39#include "video.h"
43945b27 40
0c2466de
AK
41enum TransposeDir {
42 TRANSPOSE_CCLOCK_FLIP,
43 TRANSPOSE_CLOCK,
44 TRANSPOSE_CCLOCK,
45 TRANSPOSE_CLOCK_FLIP,
46};
47
58400ac1 48typedef struct TransContext {
0c2466de 49 const AVClass *class;
43945b27
SS
50 int hsub, vsub;
51 int pixsteps[4];
52
0c2466de 53 enum TransposeDir dir;
43945b27
SS
54} TransContext;
55
43945b27
SS
56static int query_formats(AVFilterContext *ctx)
57{
716d413c
AK
58 enum AVPixelFormat pix_fmts[] = {
59 AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
60 AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
61 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
62 AV_PIX_FMT_RGB565BE, AV_PIX_FMT_RGB565LE,
63 AV_PIX_FMT_RGB555BE, AV_PIX_FMT_RGB555LE,
64 AV_PIX_FMT_BGR565BE, AV_PIX_FMT_BGR565LE,
65 AV_PIX_FMT_BGR555BE, AV_PIX_FMT_BGR555LE,
66 AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE,
67 AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUV420P16BE,
68 AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUV422P16BE,
69 AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUV444P16BE,
70 AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
71 AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8,
72 AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
73 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
74 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
75 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
76 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
77 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
78 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_GRAY8,
79 AV_PIX_FMT_NONE
43945b27
SS
80 };
81
b74a1da4 82 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
43945b27
SS
83 return 0;
84}
85
86static int config_props_output(AVFilterLink *outlink)
87{
88 AVFilterContext *ctx = outlink->src;
89 TransContext *trans = ctx->priv;
90 AVFilterLink *inlink = ctx->inputs[0];
59ee9f78
AK
91 const AVPixFmtDescriptor *desc_out = av_pix_fmt_desc_get(outlink->format);
92 const AVPixFmtDescriptor *desc_in = av_pix_fmt_desc_get(inlink->format);
43945b27 93
59ee9f78
AK
94 trans->hsub = desc_in->log2_chroma_w;
95 trans->vsub = desc_in->log2_chroma_h;
43945b27 96
59ee9f78 97 av_image_fill_max_pixsteps(trans->pixsteps, NULL, desc_out);
43945b27
SS
98
99 outlink->w = inlink->h;
100 outlink->h = inlink->w;
101
6ee55c7b
VG
102 if (inlink->sample_aspect_ratio.num)
103 outlink->sample_aspect_ratio = av_div_q((AVRational) { 1, 1 },
104 inlink->sample_aspect_ratio);
105 else
910b5b82
MN
106 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
107
6ee55c7b
VG
108 av_log(ctx, AV_LOG_VERBOSE,
109 "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
43945b27
SS
110 inlink->w, inlink->h, trans->dir, outlink->w, outlink->h,
111 trans->dir == 1 || trans->dir == 3 ? "clockwise" : "counterclockwise",
112 trans->dir == 0 || trans->dir == 3);
113 return 0;
114}
115
7e350379 116static int filter_frame(AVFilterLink *inlink, AVFrame *in)
43945b27
SS
117{
118 AVFilterLink *outlink = inlink->dst->outputs[0];
565e4993 119 TransContext *trans = inlink->dst->priv;
7e350379 120 AVFrame *out;
565e4993 121 int plane;
43945b27 122
7e350379 123 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
565e4993 124 if (!out) {
7e350379 125 av_frame_free(&in);
ebc8d974 126 return AVERROR(ENOMEM);
565e4993 127 }
ebc8d974 128
565e4993 129 out->pts = in->pts;
43945b27 130
7e350379
AK
131 if (in->sample_aspect_ratio.num == 0) {
132 out->sample_aspect_ratio = in->sample_aspect_ratio;
43945b27 133 } else {
7e350379
AK
134 out->sample_aspect_ratio.num = in->sample_aspect_ratio.den;
135 out->sample_aspect_ratio.den = in->sample_aspect_ratio.num;
43945b27
SS
136 }
137
565e4993 138 for (plane = 0; out->data[plane]; plane++) {
6ee55c7b
VG
139 int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
140 int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
43945b27 141 int pixstep = trans->pixsteps[plane];
6ee55c7b
VG
142 int inh = in->height >> vsub;
143 int outw = out->width >> hsub;
144 int outh = out->height >> vsub;
565e4993
AK
145 uint8_t *dst, *src;
146 int dstlinesize, srclinesize;
43945b27
SS
147 int x, y;
148
6ee55c7b 149 dst = out->data[plane];
565e4993 150 dstlinesize = out->linesize[plane];
6ee55c7b 151 src = in->data[plane];
565e4993 152 srclinesize = in->linesize[plane];
43945b27 153
6ee55c7b
VG
154 if (trans->dir & 1) {
155 src += in->linesize[plane] * (inh - 1);
565e4993 156 srclinesize *= -1;
43945b27
SS
157 }
158
6ee55c7b
VG
159 if (trans->dir & 2) {
160 dst += out->linesize[plane] * (outh - 1);
565e4993 161 dstlinesize *= -1;
43945b27
SS
162 }
163
164 for (y = 0; y < outh; y++) {
165 switch (pixstep) {
166 case 1:
167 for (x = 0; x < outw; x++)
6ee55c7b 168 dst[x] = src[x * srclinesize + y];
43945b27
SS
169 break;
170 case 2:
171 for (x = 0; x < outw; x++)
6ee55c7b
VG
172 *((uint16_t *)(dst + 2 * x)) =
173 *((uint16_t *)(src + x * srclinesize + y * 2));
43945b27
SS
174 break;
175 case 3:
176 for (x = 0; x < outw; x++) {
6ee55c7b
VG
177 int32_t v = AV_RB24(src + x * srclinesize + y * 3);
178 AV_WB24(dst + 3 * x, v);
43945b27
SS
179 }
180 break;
181 case 4:
182 for (x = 0; x < outw; x++)
6ee55c7b
VG
183 *((uint32_t *)(dst + 4 * x)) =
184 *((uint32_t *)(src + x * srclinesize + y * 4));
43945b27
SS
185 break;
186 }
565e4993 187 dst += dstlinesize;
43945b27
SS
188 }
189 }
190
7e350379 191 av_frame_free(&in);
565e4993 192 return ff_filter_frame(outlink, out);
43945b27
SS
193}
194
0c2466de
AK
195#define OFFSET(x) offsetof(TransContext, x)
196#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
197static const AVOption options[] = {
198 { "dir", "Transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP },
199 TRANSPOSE_CCLOCK_FLIP, TRANSPOSE_CLOCK_FLIP, FLAGS, "dir" },
200 { "cclock_flip", "counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .unit = "dir" },
201 { "clock", "clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK }, .unit = "dir" },
202 { "cclock", "counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK }, .unit = "dir" },
203 { "clock_flip", "clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP }, .unit = "dir" },
204 { NULL },
205};
206
207static const AVClass transpose_class = {
208 .class_name = "transpose",
209 .item_name = av_default_item_name,
210 .option = options,
211 .version = LIBAVUTIL_VERSION_INT,
212};
213
568c70e7
MR
214static const AVFilterPad avfilter_vf_transpose_inputs[] = {
215 {
6ee55c7b
VG
216 .name = "default",
217 .type = AVMEDIA_TYPE_VIDEO,
565e4993 218 .filter_frame = filter_frame,
568c70e7
MR
219 },
220 { NULL }
221};
222
223static const AVFilterPad avfilter_vf_transpose_outputs[] = {
224 {
225 .name = "default",
226 .config_props = config_props_output,
227 .type = AVMEDIA_TYPE_VIDEO,
228 },
229 { NULL }
230};
231
cd43ca04 232AVFilter ff_vf_transpose = {
6ee55c7b
VG
233 .name = "transpose",
234 .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
235 .priv_size = sizeof(TransContext),
236 .priv_class = &transpose_class,
43945b27 237 .query_formats = query_formats,
6ee55c7b
VG
238 .inputs = avfilter_vf_transpose_inputs,
239 .outputs = avfilter_vf_transpose_outputs,
43945b27 240};