You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 lines
9.2KB

  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. AVFilterFormats *pix_fmts = NULL;
  75. int fmt;
  76. for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
  77. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  78. if (!(desc->flags & PIX_FMT_PAL ||
  79. desc->flags & PIX_FMT_HWACCEL ||
  80. desc->flags & PIX_FMT_BITSTREAM ||
  81. desc->log2_chroma_w != desc->log2_chroma_h))
  82. ff_add_format(&pix_fmts, fmt);
  83. }
  84. ff_set_common_formats(ctx, pix_fmts);
  85. return 0;
  86. }
  87. static int config_props_output(AVFilterLink *outlink)
  88. {
  89. AVFilterContext *ctx = outlink->src;
  90. TransContext *trans = ctx->priv;
  91. AVFilterLink *inlink = ctx->inputs[0];
  92. const AVPixFmtDescriptor *desc_out = av_pix_fmt_desc_get(outlink->format);
  93. const AVPixFmtDescriptor *desc_in = av_pix_fmt_desc_get(inlink->format);
  94. if (trans->dir&4) {
  95. av_log(ctx, AV_LOG_WARNING,
  96. "dir values greater than 3 are deprecated, use the passthrough option instead\n");
  97. trans->dir &= 3;
  98. trans->passthrough = TRANSPOSE_PT_TYPE_LANDSCAPE;
  99. }
  100. if ((inlink->w >= inlink->h && trans->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
  101. (inlink->w <= inlink->h && trans->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
  102. av_log(ctx, AV_LOG_VERBOSE,
  103. "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
  104. inlink->w, inlink->h, inlink->w, inlink->h);
  105. return 0;
  106. } else {
  107. trans->passthrough = TRANSPOSE_PT_TYPE_NONE;
  108. }
  109. trans->hsub = desc_in->log2_chroma_w;
  110. trans->vsub = desc_in->log2_chroma_h;
  111. av_image_fill_max_pixsteps(trans->pixsteps, NULL, desc_out);
  112. outlink->w = inlink->h;
  113. outlink->h = inlink->w;
  114. if (inlink->sample_aspect_ratio.num){
  115. outlink->sample_aspect_ratio = av_div_q((AVRational){1,1}, inlink->sample_aspect_ratio);
  116. } else
  117. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  118. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
  119. inlink->w, inlink->h, trans->dir, outlink->w, outlink->h,
  120. trans->dir == 1 || trans->dir == 3 ? "clockwise" : "counterclockwise",
  121. trans->dir == 0 || trans->dir == 3);
  122. return 0;
  123. }
  124. static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
  125. {
  126. TransContext *trans = inlink->dst->priv;
  127. return trans->passthrough ?
  128. ff_null_get_video_buffer (inlink, perms, w, h) :
  129. ff_default_get_video_buffer(inlink, perms, w, h);
  130. }
  131. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
  132. {
  133. TransContext *trans = inlink->dst->priv;
  134. AVFilterLink *outlink = inlink->dst->outputs[0];
  135. AVFilterBufferRef *out;
  136. int plane;
  137. if (trans->passthrough)
  138. return ff_filter_frame(outlink, in);
  139. out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  140. if (!out) {
  141. avfilter_unref_bufferp(&in);
  142. return AVERROR(ENOMEM);
  143. }
  144. out->pts = in->pts;
  145. if (in->video->sample_aspect_ratio.num == 0) {
  146. out->video->sample_aspect_ratio = in->video->sample_aspect_ratio;
  147. } else {
  148. out->video->sample_aspect_ratio.num = in->video->sample_aspect_ratio.den;
  149. out->video->sample_aspect_ratio.den = in->video->sample_aspect_ratio.num;
  150. }
  151. for (plane = 0; out->data[plane]; plane++) {
  152. int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
  153. int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
  154. int pixstep = trans->pixsteps[plane];
  155. int inh = in->video->h>>vsub;
  156. int outw = out->video->w>>hsub;
  157. int outh = out->video->h>>vsub;
  158. uint8_t *dst, *src;
  159. int dstlinesize, srclinesize;
  160. int x, y;
  161. dst = out->data[plane];
  162. dstlinesize = out->linesize[plane];
  163. src = in->data[plane];
  164. srclinesize = in->linesize[plane];
  165. if (trans->dir&1) {
  166. src += in->linesize[plane] * (inh-1);
  167. srclinesize *= -1;
  168. }
  169. if (trans->dir&2) {
  170. dst += out->linesize[plane] * (outh-1);
  171. dstlinesize *= -1;
  172. }
  173. for (y = 0; y < outh; y++) {
  174. switch (pixstep) {
  175. case 1:
  176. for (x = 0; x < outw; x++)
  177. dst[x] = src[x*srclinesize + y];
  178. break;
  179. case 2:
  180. for (x = 0; x < outw; x++)
  181. *((uint16_t *)(dst + 2*x)) = *((uint16_t *)(src + x*srclinesize + y*2));
  182. break;
  183. case 3:
  184. for (x = 0; x < outw; x++) {
  185. int32_t v = AV_RB24(src + x*srclinesize + y*3);
  186. AV_WB24(dst + 3*x, v);
  187. }
  188. break;
  189. case 4:
  190. for (x = 0; x < outw; x++)
  191. *((uint32_t *)(dst + 4*x)) = *((uint32_t *)(src + x*srclinesize + y*4));
  192. break;
  193. case 6:
  194. for (x = 0; x < outw; x++) {
  195. int64_t v = AV_RB48(src + x*srclinesize + y*6);
  196. AV_WB48(dst + 6*x, v);
  197. }
  198. break;
  199. case 8:
  200. for (x = 0; x < outw; x++)
  201. *((uint64_t *)(dst + 8*x)) = *((uint64_t *)(src + x*srclinesize + y*8));
  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. };