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.

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