vf_transpose.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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/avassert.h"
  28. #include "libavutil/imgutils.h"
  29. #include "libavutil/internal.h"
  30. #include "libavutil/intreadwrite.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/pixdesc.h"
  33. #include "avfilter.h"
  34. #include "formats.h"
  35. #include "internal.h"
  36. #include "video.h"
  37. typedef enum {
  38. TRANSPOSE_PT_TYPE_NONE,
  39. TRANSPOSE_PT_TYPE_LANDSCAPE,
  40. TRANSPOSE_PT_TYPE_PORTRAIT,
  41. } PassthroughType;
  42. enum TransposeDir {
  43. TRANSPOSE_CCLOCK_FLIP,
  44. TRANSPOSE_CLOCK,
  45. TRANSPOSE_CCLOCK,
  46. TRANSPOSE_CLOCK_FLIP,
  47. };
  48. typedef struct TransContext {
  49. const AVClass *class;
  50. int hsub, vsub;
  51. int planes;
  52. int pixsteps[4];
  53. int passthrough; ///< PassthroughType, landscape passthrough mode enabled
  54. int dir; ///< TransposeDir
  55. } TransContext;
  56. static int query_formats(AVFilterContext *ctx)
  57. {
  58. AVFilterFormats *pix_fmts = NULL;
  59. int fmt, ret;
  60. for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
  61. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  62. if (!(desc->flags & AV_PIX_FMT_FLAG_PAL ||
  63. desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
  64. desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ||
  65. desc->log2_chroma_w != desc->log2_chroma_h) &&
  66. (ret = ff_add_format(&pix_fmts, fmt)) < 0)
  67. return ret;
  68. }
  69. return ff_set_common_formats(ctx, pix_fmts);
  70. }
  71. static int config_props_output(AVFilterLink *outlink)
  72. {
  73. AVFilterContext *ctx = outlink->src;
  74. TransContext *s = ctx->priv;
  75. AVFilterLink *inlink = ctx->inputs[0];
  76. const AVPixFmtDescriptor *desc_out = av_pix_fmt_desc_get(outlink->format);
  77. const AVPixFmtDescriptor *desc_in = av_pix_fmt_desc_get(inlink->format);
  78. if (s->dir&4) {
  79. av_log(ctx, AV_LOG_WARNING,
  80. "dir values greater than 3 are deprecated, use the passthrough option instead\n");
  81. s->dir &= 3;
  82. s->passthrough = TRANSPOSE_PT_TYPE_LANDSCAPE;
  83. }
  84. if ((inlink->w >= inlink->h && s->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
  85. (inlink->w <= inlink->h && s->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
  86. av_log(ctx, AV_LOG_VERBOSE,
  87. "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
  88. inlink->w, inlink->h, inlink->w, inlink->h);
  89. return 0;
  90. } else {
  91. s->passthrough = TRANSPOSE_PT_TYPE_NONE;
  92. }
  93. s->hsub = desc_in->log2_chroma_w;
  94. s->vsub = desc_in->log2_chroma_h;
  95. s->planes = av_pix_fmt_count_planes(outlink->format);
  96. av_assert0(desc_in->nb_components == desc_out->nb_components);
  97. av_image_fill_max_pixsteps(s->pixsteps, NULL, desc_out);
  98. outlink->w = inlink->h;
  99. outlink->h = inlink->w;
  100. if (inlink->sample_aspect_ratio.num)
  101. outlink->sample_aspect_ratio = av_div_q((AVRational) { 1, 1 },
  102. inlink->sample_aspect_ratio);
  103. else
  104. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  105. av_log(ctx, AV_LOG_VERBOSE,
  106. "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
  107. inlink->w, inlink->h, s->dir, outlink->w, outlink->h,
  108. s->dir == 1 || s->dir == 3 ? "clockwise" : "counterclockwise",
  109. s->dir == 0 || s->dir == 3);
  110. return 0;
  111. }
  112. static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
  113. {
  114. TransContext *s = inlink->dst->priv;
  115. return s->passthrough ?
  116. ff_null_get_video_buffer (inlink, w, h) :
  117. ff_default_get_video_buffer(inlink, w, h);
  118. }
  119. typedef struct ThreadData {
  120. AVFrame *in, *out;
  121. } ThreadData;
  122. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr,
  123. int nb_jobs)
  124. {
  125. TransContext *s = ctx->priv;
  126. ThreadData *td = arg;
  127. AVFrame *out = td->out;
  128. AVFrame *in = td->in;
  129. int plane;
  130. for (plane = 0; plane < s->planes; plane++) {
  131. int hsub = plane == 1 || plane == 2 ? s->hsub : 0;
  132. int vsub = plane == 1 || plane == 2 ? s->vsub : 0;
  133. int pixstep = s->pixsteps[plane];
  134. int inh = AV_CEIL_RSHIFT(in->height, vsub);
  135. int outw = AV_CEIL_RSHIFT(out->width, hsub);
  136. int outh = AV_CEIL_RSHIFT(out->height, vsub);
  137. int start = (outh * jobnr ) / nb_jobs;
  138. int end = (outh * (jobnr+1)) / nb_jobs;
  139. uint8_t *dst, *src;
  140. int dstlinesize, srclinesize;
  141. int x, y;
  142. dstlinesize = out->linesize[plane];
  143. dst = out->data[plane] + start * dstlinesize;
  144. src = in->data[plane];
  145. srclinesize = in->linesize[plane];
  146. if (s->dir & 1) {
  147. src += in->linesize[plane] * (inh - 1);
  148. srclinesize *= -1;
  149. }
  150. if (s->dir & 2) {
  151. dst = out->data[plane] + dstlinesize * (outh - start - 1);
  152. dstlinesize *= -1;
  153. }
  154. switch (pixstep) {
  155. case 1:
  156. for (y = start; y < end; y++, dst += dstlinesize)
  157. for (x = 0; x < outw; x++)
  158. dst[x] = src[x * srclinesize + y];
  159. break;
  160. case 2:
  161. for (y = start; y < end; y++, dst += dstlinesize) {
  162. for (x = 0; x < outw; x++)
  163. *((uint16_t *)(dst + 2 * x)) =
  164. *((uint16_t *)(src + x * srclinesize + y * 2));
  165. }
  166. break;
  167. case 3:
  168. for (y = start; y < end; y++, dst += dstlinesize) {
  169. for (x = 0; x < outw; x++) {
  170. int32_t v = AV_RB24(src + x * srclinesize + y * 3);
  171. AV_WB24(dst + 3 * x, v);
  172. }
  173. }
  174. break;
  175. case 4:
  176. for (y = start; y < end; y++, dst += dstlinesize) {
  177. for (x = 0; x < outw; x++)
  178. *((uint32_t *)(dst + 4 * x)) =
  179. *((uint32_t *)(src + x * srclinesize + y * 4));
  180. }
  181. break;
  182. case 6:
  183. for (y = start; y < end; y++, dst += dstlinesize) {
  184. for (x = 0; x < outw; x++) {
  185. int64_t v = AV_RB48(src + x * srclinesize + y*6);
  186. AV_WB48(dst + 6*x, v);
  187. }
  188. }
  189. break;
  190. case 8:
  191. for (y = start; y < end; y++, dst += dstlinesize) {
  192. for (x = 0; x < outw; x++)
  193. *((uint64_t *)(dst + 8*x)) = *((uint64_t *)(src + x * srclinesize + y*8));
  194. }
  195. break;
  196. }
  197. }
  198. return 0;
  199. }
  200. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  201. {
  202. AVFilterContext *ctx = inlink->dst;
  203. TransContext *s = ctx->priv;
  204. AVFilterLink *outlink = ctx->outputs[0];
  205. ThreadData td;
  206. AVFrame *out;
  207. if (s->passthrough)
  208. return ff_filter_frame(outlink, in);
  209. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  210. if (!out) {
  211. av_frame_free(&in);
  212. return AVERROR(ENOMEM);
  213. }
  214. av_frame_copy_props(out, in);
  215. if (in->sample_aspect_ratio.num == 0) {
  216. out->sample_aspect_ratio = in->sample_aspect_ratio;
  217. } else {
  218. out->sample_aspect_ratio.num = in->sample_aspect_ratio.den;
  219. out->sample_aspect_ratio.den = in->sample_aspect_ratio.num;
  220. }
  221. td.in = in, td.out = out;
  222. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ctx->graph->nb_threads));
  223. av_frame_free(&in);
  224. return ff_filter_frame(outlink, out);
  225. }
  226. #define OFFSET(x) offsetof(TransContext, x)
  227. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  228. static const AVOption transpose_options[] = {
  229. { "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP }, 0, 7, FLAGS, "dir" },
  230. { "cclock_flip", "rotate counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .unit = "dir" },
  231. { "clock", "rotate clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK }, .unit = "dir" },
  232. { "cclock", "rotate counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK }, .unit = "dir" },
  233. { "clock_flip", "rotate clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP }, .unit = "dir" },
  234. { "passthrough", "do not apply transposition if the input matches the specified geometry",
  235. OFFSET(passthrough), AV_OPT_TYPE_INT, {.i64=TRANSPOSE_PT_TYPE_NONE}, 0, INT_MAX, FLAGS, "passthrough" },
  236. { "none", "always apply transposition", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_NONE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
  237. { "portrait", "preserve portrait geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_PORTRAIT}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
  238. { "landscape", "preserve landscape geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_LANDSCAPE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
  239. { NULL }
  240. };
  241. AVFILTER_DEFINE_CLASS(transpose);
  242. static const AVFilterPad avfilter_vf_transpose_inputs[] = {
  243. {
  244. .name = "default",
  245. .type = AVMEDIA_TYPE_VIDEO,
  246. .get_video_buffer = get_video_buffer,
  247. .filter_frame = filter_frame,
  248. },
  249. { NULL }
  250. };
  251. static const AVFilterPad avfilter_vf_transpose_outputs[] = {
  252. {
  253. .name = "default",
  254. .config_props = config_props_output,
  255. .type = AVMEDIA_TYPE_VIDEO,
  256. },
  257. { NULL }
  258. };
  259. AVFilter ff_vf_transpose = {
  260. .name = "transpose",
  261. .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
  262. .priv_size = sizeof(TransContext),
  263. .priv_class = &transpose_class,
  264. .query_formats = query_formats,
  265. .inputs = avfilter_vf_transpose_inputs,
  266. .outputs = avfilter_vf_transpose_outputs,
  267. .flags = AVFILTER_FLAG_SLICE_THREADS,
  268. };