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.

273 lines
8.8KB

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