vf_transpose.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (c) 2010 Stefano Sabatini
  3. * Copyright (c) 2008 Vitor Sessak
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/intreadwrite.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/imgutils.h"
  31. #include "libavutil/internal.h"
  32. #include "avfilter.h"
  33. #include "formats.h"
  34. #include "internal.h"
  35. #include "video.h"
  36. typedef enum {
  37. TRANSPOSE_PT_TYPE_NONE,
  38. TRANSPOSE_PT_TYPE_LANDSCAPE,
  39. TRANSPOSE_PT_TYPE_PORTRAIT,
  40. } PassthroughType;
  41. typedef struct {
  42. const AVClass *class;
  43. int hsub, vsub;
  44. int pixsteps[4];
  45. /* 0 Rotate by 90 degrees counterclockwise and vflip. */
  46. /* 1 Rotate by 90 degrees clockwise. */
  47. /* 2 Rotate by 90 degrees counterclockwise. */
  48. /* 3 Rotate by 90 degrees clockwise and vflip. */
  49. int dir;
  50. PassthroughType passthrough; ///< landscape passthrough mode enabled
  51. } TransContext;
  52. #define OFFSET(x) offsetof(TransContext, x)
  53. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  54. static const AVOption transpose_options[] = {
  55. { "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, {.i64=0}, 0, 7, FLAGS },
  56. { "passthrough", "do not apply transposition if the input matches the specified geometry",
  57. OFFSET(passthrough), AV_OPT_TYPE_INT, {.i64=TRANSPOSE_PT_TYPE_NONE}, 0, INT_MAX, FLAGS, "passthrough" },
  58. { "none", "always apply transposition", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_NONE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
  59. { "portrait", "preserve portrait geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_PORTRAIT}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
  60. { "landscape", "preserve landscape geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_LANDSCAPE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
  61. { NULL },
  62. };
  63. AVFILTER_DEFINE_CLASS(transpose);
  64. static av_cold int init(AVFilterContext *ctx, const char *args)
  65. {
  66. TransContext *trans = ctx->priv;
  67. const char *shorthand[] = { "dir", "passthrough", NULL };
  68. trans->class = &transpose_class;
  69. av_opt_set_defaults(trans);
  70. return av_opt_set_from_string(trans, args, shorthand, "=", ":");
  71. }
  72. static int query_formats(AVFilterContext *ctx)
  73. {
  74. static const enum AVPixelFormat pix_fmts[] = {
  75. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  76. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  77. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  78. AV_PIX_FMT_RGB565BE, AV_PIX_FMT_RGB565LE,
  79. AV_PIX_FMT_RGB555BE, AV_PIX_FMT_RGB555LE,
  80. AV_PIX_FMT_BGR565BE, AV_PIX_FMT_BGR565LE,
  81. AV_PIX_FMT_BGR555BE, AV_PIX_FMT_BGR555LE,
  82. AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE,
  83. AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUV420P16BE,
  84. AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUV444P16BE,
  85. AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
  86. AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8,
  87. AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
  88. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  89. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
  90. AV_PIX_FMT_YUV410P,
  91. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_GRAY8,
  92. AV_PIX_FMT_NONE
  93. };
  94. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  95. return 0;
  96. }
  97. static int config_props_output(AVFilterLink *outlink)
  98. {
  99. AVFilterContext *ctx = outlink->src;
  100. TransContext *trans = ctx->priv;
  101. AVFilterLink *inlink = ctx->inputs[0];
  102. const AVPixFmtDescriptor *desc_out = av_pix_fmt_desc_get(outlink->format);
  103. const AVPixFmtDescriptor *desc_in = av_pix_fmt_desc_get(inlink->format);
  104. if (trans->dir&4) {
  105. av_log(ctx, AV_LOG_WARNING,
  106. "dir values greater than 3 are deprecated, use the passthrough option instead\n");
  107. trans->dir &= 3;
  108. trans->passthrough = TRANSPOSE_PT_TYPE_LANDSCAPE;
  109. }
  110. if ((inlink->w >= inlink->h && trans->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
  111. (inlink->w <= inlink->h && trans->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
  112. av_log(ctx, AV_LOG_VERBOSE,
  113. "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
  114. inlink->w, inlink->h, inlink->w, inlink->h);
  115. return 0;
  116. } else {
  117. trans->passthrough = TRANSPOSE_PT_TYPE_NONE;
  118. }
  119. trans->hsub = desc_in->log2_chroma_w;
  120. trans->vsub = desc_in->log2_chroma_h;
  121. av_image_fill_max_pixsteps(trans->pixsteps, NULL, desc_out);
  122. outlink->w = inlink->h;
  123. outlink->h = inlink->w;
  124. if (inlink->sample_aspect_ratio.num){
  125. outlink->sample_aspect_ratio = av_div_q((AVRational){1,1}, inlink->sample_aspect_ratio);
  126. } else
  127. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  128. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
  129. inlink->w, inlink->h, trans->dir, outlink->w, outlink->h,
  130. trans->dir == 1 || trans->dir == 3 ? "clockwise" : "counterclockwise",
  131. trans->dir == 0 || trans->dir == 3);
  132. return 0;
  133. }
  134. static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
  135. {
  136. TransContext *trans = inlink->dst->priv;
  137. return trans->passthrough ?
  138. ff_null_get_video_buffer (inlink, perms, w, h) :
  139. ff_default_get_video_buffer(inlink, perms, w, h);
  140. }
  141. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
  142. {
  143. TransContext *trans = inlink->dst->priv;
  144. AVFilterLink *outlink = inlink->dst->outputs[0];
  145. AVFilterBufferRef *out;
  146. int plane;
  147. if (trans->passthrough)
  148. return ff_filter_frame(outlink, in);
  149. out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  150. if (!out) {
  151. avfilter_unref_bufferp(&in);
  152. return AVERROR(ENOMEM);
  153. }
  154. out->pts = in->pts;
  155. if (in->video->sample_aspect_ratio.num == 0) {
  156. out->video->sample_aspect_ratio = in->video->sample_aspect_ratio;
  157. } else {
  158. out->video->sample_aspect_ratio.num = in->video->sample_aspect_ratio.den;
  159. out->video->sample_aspect_ratio.den = in->video->sample_aspect_ratio.num;
  160. }
  161. for (plane = 0; out->data[plane]; plane++) {
  162. int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
  163. int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
  164. int pixstep = trans->pixsteps[plane];
  165. int inh = in->video->h>>vsub;
  166. int outw = out->video->w>>hsub;
  167. int outh = out->video->h>>vsub;
  168. uint8_t *dst, *src;
  169. int dstlinesize, srclinesize;
  170. int x, y;
  171. dst = out->data[plane];
  172. dstlinesize = out->linesize[plane];
  173. src = in->data[plane];
  174. srclinesize = in->linesize[plane];
  175. if (trans->dir&1) {
  176. src += in->linesize[plane] * (inh-1);
  177. srclinesize *= -1;
  178. }
  179. if (trans->dir&2) {
  180. dst += out->linesize[plane] * (outh-1);
  181. dstlinesize *= -1;
  182. }
  183. for (y = 0; y < outh; y++) {
  184. switch (pixstep) {
  185. case 1:
  186. for (x = 0; x < outw; x++)
  187. dst[x] = src[x*srclinesize + y];
  188. break;
  189. case 2:
  190. for (x = 0; x < outw; x++)
  191. *((uint16_t *)(dst + 2*x)) = *((uint16_t *)(src + x*srclinesize + y*2));
  192. break;
  193. case 3:
  194. for (x = 0; x < outw; x++) {
  195. int32_t v = AV_RB24(src + x*srclinesize + y*3);
  196. AV_WB24(dst + 3*x, v);
  197. }
  198. break;
  199. case 4:
  200. for (x = 0; x < outw; x++)
  201. *((uint32_t *)(dst + 4*x)) = *((uint32_t *)(src + x*srclinesize + y*4));
  202. break;
  203. }
  204. dst += dstlinesize;
  205. }
  206. }
  207. avfilter_unref_bufferp(&in);
  208. return ff_filter_frame(outlink, out);
  209. }
  210. static const AVFilterPad avfilter_vf_transpose_inputs[] = {
  211. {
  212. .name = "default",
  213. .type = AVMEDIA_TYPE_VIDEO,
  214. .get_video_buffer= get_video_buffer,
  215. .filter_frame = filter_frame,
  216. .min_perms = AV_PERM_READ,
  217. },
  218. { NULL }
  219. };
  220. static const AVFilterPad avfilter_vf_transpose_outputs[] = {
  221. {
  222. .name = "default",
  223. .config_props = config_props_output,
  224. .type = AVMEDIA_TYPE_VIDEO,
  225. },
  226. { NULL }
  227. };
  228. AVFilter avfilter_vf_transpose = {
  229. .name = "transpose",
  230. .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
  231. .init = init,
  232. .priv_size = sizeof(TransContext),
  233. .query_formats = query_formats,
  234. .inputs = avfilter_vf_transpose_inputs,
  235. .outputs = avfilter_vf_transpose_outputs,
  236. .priv_class = &transpose_class,
  237. };