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.

299 lines
9.9KB

  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. typedef struct ThreadData {
  115. AVFrame *in, *out;
  116. } ThreadData;
  117. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr,
  118. int nb_jobs)
  119. {
  120. TransContext *trans = ctx->priv;
  121. ThreadData *td = arg;
  122. AVFrame *out = td->out;
  123. AVFrame *in = td->in;
  124. int plane;
  125. for (plane = 0; out->data[plane]; plane++) {
  126. int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
  127. int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
  128. int pixstep = trans->pixsteps[plane];
  129. int inh = in->height >> vsub;
  130. int outw = FF_CEIL_RSHIFT(out->width, hsub);
  131. int outh = FF_CEIL_RSHIFT(out->height, vsub);
  132. int start = (outh * jobnr ) / nb_jobs;
  133. int end = (outh * (jobnr+1)) / nb_jobs;
  134. uint8_t *dst, *src;
  135. int dstlinesize, srclinesize;
  136. int x, y;
  137. dstlinesize = out->linesize[plane];
  138. dst = out->data[plane] + start * dstlinesize;
  139. src = in->data[plane];
  140. srclinesize = in->linesize[plane];
  141. if (trans->dir&1) {
  142. src += in->linesize[plane] * (inh-1);
  143. srclinesize *= -1;
  144. }
  145. if (trans->dir&2) {
  146. dst = out->data[plane] + dstlinesize * (outh-start-1);
  147. dstlinesize *= -1;
  148. }
  149. for (y = start; y < end; y++) {
  150. switch (pixstep) {
  151. case 1:
  152. for (x = 0; x < outw; x++)
  153. dst[x] = src[x*srclinesize + y];
  154. break;
  155. case 2:
  156. for (x = 0; x < outw; x++)
  157. *((uint16_t *)(dst + 2*x)) = *((uint16_t *)(src + x*srclinesize + y*2));
  158. break;
  159. case 3:
  160. for (x = 0; x < outw; x++) {
  161. int32_t v = AV_RB24(src + x*srclinesize + y*3);
  162. AV_WB24(dst + 3*x, v);
  163. }
  164. break;
  165. case 4:
  166. for (x = 0; x < outw; x++)
  167. *((uint32_t *)(dst + 4*x)) = *((uint32_t *)(src + x*srclinesize + y*4));
  168. break;
  169. case 6:
  170. for (x = 0; x < outw; x++) {
  171. int64_t v = AV_RB48(src + x*srclinesize + y*6);
  172. AV_WB48(dst + 6*x, v);
  173. }
  174. break;
  175. case 8:
  176. for (x = 0; x < outw; x++)
  177. *((uint64_t *)(dst + 8*x)) = *((uint64_t *)(src + x*srclinesize + y*8));
  178. break;
  179. }
  180. dst += dstlinesize;
  181. }
  182. }
  183. return 0;
  184. }
  185. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  186. {
  187. AVFilterContext *ctx = inlink->dst;
  188. TransContext *trans = ctx->priv;
  189. AVFilterLink *outlink = ctx->outputs[0];
  190. ThreadData td;
  191. AVFrame *out;
  192. if (trans->passthrough)
  193. return ff_filter_frame(outlink, in);
  194. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  195. if (!out) {
  196. av_frame_free(&in);
  197. return AVERROR(ENOMEM);
  198. }
  199. av_frame_copy_props(out, in);
  200. if (in->sample_aspect_ratio.num == 0) {
  201. out->sample_aspect_ratio = in->sample_aspect_ratio;
  202. } else {
  203. out->sample_aspect_ratio.num = in->sample_aspect_ratio.den;
  204. out->sample_aspect_ratio.den = in->sample_aspect_ratio.num;
  205. }
  206. td.in = in, td.out = out;
  207. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ctx->graph->nb_threads));
  208. av_frame_free(&in);
  209. return ff_filter_frame(outlink, out);
  210. }
  211. #define OFFSET(x) offsetof(TransContext, x)
  212. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  213. static const AVOption transpose_options[] = {
  214. { "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP }, 0, 7, FLAGS, "dir" },
  215. { "cclock_flip", "rotate counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .unit = "dir" },
  216. { "clock", "rotate clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK }, .unit = "dir" },
  217. { "cclock", "rotate counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK }, .unit = "dir" },
  218. { "clock_flip", "rotate clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP }, .unit = "dir" },
  219. { "passthrough", "do not apply transposition if the input matches the specified geometry",
  220. OFFSET(passthrough), AV_OPT_TYPE_INT, {.i64=TRANSPOSE_PT_TYPE_NONE}, 0, INT_MAX, FLAGS, "passthrough" },
  221. { "none", "always apply transposition", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_NONE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
  222. { "portrait", "preserve portrait geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_PORTRAIT}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
  223. { "landscape", "preserve landscape geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_LANDSCAPE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
  224. { NULL },
  225. };
  226. AVFILTER_DEFINE_CLASS(transpose);
  227. static const AVFilterPad avfilter_vf_transpose_inputs[] = {
  228. {
  229. .name = "default",
  230. .type = AVMEDIA_TYPE_VIDEO,
  231. .get_video_buffer= get_video_buffer,
  232. .filter_frame = filter_frame,
  233. },
  234. { NULL }
  235. };
  236. static const AVFilterPad avfilter_vf_transpose_outputs[] = {
  237. {
  238. .name = "default",
  239. .config_props = config_props_output,
  240. .type = AVMEDIA_TYPE_VIDEO,
  241. },
  242. { NULL }
  243. };
  244. AVFilter avfilter_vf_transpose = {
  245. .name = "transpose",
  246. .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
  247. .priv_size = sizeof(TransContext),
  248. .priv_class = &transpose_class,
  249. .query_formats = query_formats,
  250. .inputs = avfilter_vf_transpose_inputs,
  251. .outputs = avfilter_vf_transpose_outputs,
  252. .flags = AVFILTER_FLAG_SLICE_THREADS,
  253. };