00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include <stdio.h>
00029
00030 #include "libavutil/intreadwrite.h"
00031 #include "libavutil/pixdesc.h"
00032 #include "libavutil/imgutils.h"
00033 #include "libavutil/internal.h"
00034 #include "avfilter.h"
00035 #include "formats.h"
00036 #include "internal.h"
00037 #include "video.h"
00038
00039 typedef struct {
00040 int hsub, vsub;
00041 int pixsteps[4];
00042
00043
00044
00045
00046
00047 int dir;
00048 int passthrough;
00049 } TransContext;
00050
00051 static av_cold int init(AVFilterContext *ctx, const char *args)
00052 {
00053 TransContext *trans = ctx->priv;
00054 trans->dir = 0;
00055
00056 if (args)
00057 sscanf(args, "%d", &trans->dir);
00058
00059 if (trans->dir < 0 || trans->dir > 7) {
00060 av_log(ctx, AV_LOG_ERROR, "Invalid value %d not between 0 and 7.\n",
00061 trans->dir);
00062 return AVERROR(EINVAL);
00063 }
00064 return 0;
00065 }
00066
00067 static int query_formats(AVFilterContext *ctx)
00068 {
00069 enum PixelFormat pix_fmts[] = {
00070 PIX_FMT_ARGB, PIX_FMT_RGBA,
00071 PIX_FMT_ABGR, PIX_FMT_BGRA,
00072 PIX_FMT_RGB24, PIX_FMT_BGR24,
00073 PIX_FMT_RGB565BE, PIX_FMT_RGB565LE,
00074 PIX_FMT_RGB555BE, PIX_FMT_RGB555LE,
00075 PIX_FMT_BGR565BE, PIX_FMT_BGR565LE,
00076 PIX_FMT_BGR555BE, PIX_FMT_BGR555LE,
00077 PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE,
00078 PIX_FMT_YUV420P16LE, PIX_FMT_YUV420P16BE,
00079 PIX_FMT_YUV444P16LE, PIX_FMT_YUV444P16BE,
00080 PIX_FMT_NV12, PIX_FMT_NV21,
00081 PIX_FMT_RGB8, PIX_FMT_BGR8,
00082 PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE,
00083 PIX_FMT_YUV444P, PIX_FMT_YUVJ444P,
00084 PIX_FMT_YUV420P, PIX_FMT_YUVJ420P,
00085 PIX_FMT_YUV410P,
00086 PIX_FMT_YUVA420P, PIX_FMT_GRAY8,
00087 PIX_FMT_NONE
00088 };
00089
00090 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00091 return 0;
00092 }
00093
00094 static int config_props_output(AVFilterLink *outlink)
00095 {
00096 AVFilterContext *ctx = outlink->src;
00097 TransContext *trans = ctx->priv;
00098 AVFilterLink *inlink = ctx->inputs[0];
00099 const AVPixFmtDescriptor *pixdesc = &av_pix_fmt_descriptors[outlink->format];
00100
00101 if (trans->dir&4) {
00102 trans->dir &= 3;
00103 if (inlink->w >= inlink->h) {
00104 trans->passthrough = 1;
00105
00106 av_log(ctx, AV_LOG_VERBOSE,
00107 "w:%d h:%d -> w:%d h:%d (landscape passthrough mode)\n",
00108 inlink->w, inlink->h, outlink->w, outlink->h);
00109 return 0;
00110 }
00111 }
00112
00113 trans->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
00114 trans->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
00115
00116 av_image_fill_max_pixsteps(trans->pixsteps, NULL, pixdesc);
00117
00118 outlink->w = inlink->h;
00119 outlink->h = inlink->w;
00120
00121 if (inlink->sample_aspect_ratio.num){
00122 outlink->sample_aspect_ratio = av_div_q((AVRational){1,1}, inlink->sample_aspect_ratio);
00123 } else
00124 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
00125
00126 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
00127 inlink->w, inlink->h, trans->dir, outlink->w, outlink->h,
00128 trans->dir == 1 || trans->dir == 3 ? "clockwise" : "counterclockwise",
00129 trans->dir == 0 || trans->dir == 3);
00130 return 0;
00131 }
00132
00133 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
00134 {
00135 TransContext *trans = inlink->dst->priv;
00136
00137 return trans->passthrough ?
00138 ff_null_get_video_buffer (inlink, perms, w, h) :
00139 ff_default_get_video_buffer(inlink, perms, w, h);
00140 }
00141
00142 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
00143 {
00144 TransContext *trans = inlink->dst->priv;
00145 AVFilterLink *outlink = inlink->dst->outputs[0];
00146 AVFilterBufferRef *buf_out;
00147
00148 if (trans->passthrough)
00149 return ff_null_start_frame(inlink, picref);
00150
00151 outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE,
00152 outlink->w, outlink->h);
00153 if (!outlink->out_buf)
00154 return AVERROR(ENOMEM);
00155
00156 outlink->out_buf->pts = picref->pts;
00157
00158 if (picref->video->sample_aspect_ratio.num == 0) {
00159 outlink->out_buf->video->sample_aspect_ratio = picref->video->sample_aspect_ratio;
00160 } else {
00161 outlink->out_buf->video->sample_aspect_ratio.num = picref->video->sample_aspect_ratio.den;
00162 outlink->out_buf->video->sample_aspect_ratio.den = picref->video->sample_aspect_ratio.num;
00163 }
00164
00165 buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
00166 if (!buf_out)
00167 return AVERROR(ENOMEM);
00168 return ff_start_frame(outlink, buf_out);
00169 }
00170
00171 static int end_frame(AVFilterLink *inlink)
00172 {
00173 TransContext *trans = inlink->dst->priv;
00174 AVFilterBufferRef *inpic = inlink->cur_buf;
00175 AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
00176 AVFilterLink *outlink = inlink->dst->outputs[0];
00177 int plane, ret;
00178
00179 if (trans->passthrough)
00180 return ff_null_end_frame(inlink);
00181
00182 for (plane = 0; outpic->data[plane]; plane++) {
00183 int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
00184 int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
00185 int pixstep = trans->pixsteps[plane];
00186 int inh = inpic->video->h>>vsub;
00187 int outw = outpic->video->w>>hsub;
00188 int outh = outpic->video->h>>vsub;
00189 uint8_t *out, *in;
00190 int outlinesize, inlinesize;
00191 int x, y;
00192
00193 out = outpic->data[plane]; outlinesize = outpic->linesize[plane];
00194 in = inpic ->data[plane]; inlinesize = inpic ->linesize[plane];
00195
00196 if (trans->dir&1) {
00197 in += inpic->linesize[plane] * (inh-1);
00198 inlinesize *= -1;
00199 }
00200
00201 if (trans->dir&2) {
00202 out += outpic->linesize[plane] * (outh-1);
00203 outlinesize *= -1;
00204 }
00205
00206 for (y = 0; y < outh; y++) {
00207 switch (pixstep) {
00208 case 1:
00209 for (x = 0; x < outw; x++)
00210 out[x] = in[x*inlinesize + y];
00211 break;
00212 case 2:
00213 for (x = 0; x < outw; x++)
00214 *((uint16_t *)(out + 2*x)) = *((uint16_t *)(in + x*inlinesize + y*2));
00215 break;
00216 case 3:
00217 for (x = 0; x < outw; x++) {
00218 int32_t v = AV_RB24(in + x*inlinesize + y*3);
00219 AV_WB24(out + 3*x, v);
00220 }
00221 break;
00222 case 4:
00223 for (x = 0; x < outw; x++)
00224 *((uint32_t *)(out + 4*x)) = *((uint32_t *)(in + x*inlinesize + y*4));
00225 break;
00226 }
00227 out += outlinesize;
00228 }
00229 }
00230
00231 if ((ret = ff_draw_slice(outlink, 0, outpic->video->h, 1)) < 0 ||
00232 (ret = ff_end_frame(outlink)) < 0)
00233 return ret;
00234 return 0;
00235 }
00236
00237 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
00238 {
00239 TransContext *trans = inlink->dst->priv;
00240
00241 return trans->passthrough ? ff_null_draw_slice(inlink, y, h, slice_dir) : 0;
00242 }
00243
00244 AVFilter avfilter_vf_transpose = {
00245 .name = "transpose",
00246 .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
00247
00248 .init = init,
00249 .priv_size = sizeof(TransContext),
00250
00251 .query_formats = query_formats,
00252
00253 .inputs = (const AVFilterPad[]) {{ .name = "default",
00254 .type = AVMEDIA_TYPE_VIDEO,
00255 .get_video_buffer= get_video_buffer,
00256 .start_frame = start_frame,
00257 .draw_slice = draw_slice,
00258 .end_frame = end_frame,
00259 .min_perms = AV_PERM_READ, },
00260 { .name = NULL}},
00261 .outputs = (const AVFilterPad[]) {{ .name = "default",
00262 .config_props = config_props_output,
00263 .type = AVMEDIA_TYPE_VIDEO, },
00264 { .name = NULL}},
00265 };