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