vf_transpose.c 10 KB

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