vf_transpose.c 7.9 KB

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