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.

292 lines
10KB

  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. enum PixelFormat pix_fmts[] = {
  75. PIX_FMT_ARGB, PIX_FMT_RGBA,
  76. PIX_FMT_ABGR, PIX_FMT_BGRA,
  77. PIX_FMT_RGB24, PIX_FMT_BGR24,
  78. PIX_FMT_RGB565BE, PIX_FMT_RGB565LE,
  79. PIX_FMT_RGB555BE, PIX_FMT_RGB555LE,
  80. PIX_FMT_BGR565BE, PIX_FMT_BGR565LE,
  81. PIX_FMT_BGR555BE, PIX_FMT_BGR555LE,
  82. PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE,
  83. PIX_FMT_YUV420P16LE, PIX_FMT_YUV420P16BE,
  84. PIX_FMT_YUV444P16LE, PIX_FMT_YUV444P16BE,
  85. PIX_FMT_NV12, PIX_FMT_NV21,
  86. PIX_FMT_RGB8, PIX_FMT_BGR8,
  87. PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE,
  88. PIX_FMT_YUV444P, PIX_FMT_YUVJ444P,
  89. PIX_FMT_YUV420P, PIX_FMT_YUVJ420P,
  90. PIX_FMT_YUV410P,
  91. PIX_FMT_YUVA420P, PIX_FMT_GRAY8,
  92. PIX_FMT_NONE
  93. };
  94. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  95. return 0;
  96. }
  97. static int config_props_output(AVFilterLink *outlink)
  98. {
  99. AVFilterContext *ctx = outlink->src;
  100. TransContext *trans = ctx->priv;
  101. AVFilterLink *inlink = ctx->inputs[0];
  102. const AVPixFmtDescriptor *pixdesc = &av_pix_fmt_descriptors[outlink->format];
  103. if (trans->dir&4) {
  104. av_log(ctx, AV_LOG_WARNING,
  105. "dir values greater than 3 are deprecated, use the passthrough option instead\n");
  106. trans->dir &= 3;
  107. trans->passthrough = TRANSPOSE_PT_TYPE_LANDSCAPE;
  108. }
  109. if ((inlink->w >= inlink->h && trans->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
  110. (inlink->w <= inlink->h && trans->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
  111. av_log(ctx, AV_LOG_VERBOSE,
  112. "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
  113. inlink->w, inlink->h, inlink->w, inlink->h);
  114. return 0;
  115. } else {
  116. trans->passthrough = TRANSPOSE_PT_TYPE_NONE;
  117. }
  118. trans->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
  119. trans->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
  120. av_image_fill_max_pixsteps(trans->pixsteps, NULL, pixdesc);
  121. outlink->w = inlink->h;
  122. outlink->h = inlink->w;
  123. if (inlink->sample_aspect_ratio.num){
  124. outlink->sample_aspect_ratio = av_div_q((AVRational){1,1}, inlink->sample_aspect_ratio);
  125. } else
  126. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  127. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
  128. inlink->w, inlink->h, trans->dir, outlink->w, outlink->h,
  129. trans->dir == 1 || trans->dir == 3 ? "clockwise" : "counterclockwise",
  130. trans->dir == 0 || trans->dir == 3);
  131. return 0;
  132. }
  133. static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
  134. {
  135. TransContext *trans = inlink->dst->priv;
  136. return trans->passthrough ?
  137. ff_null_get_video_buffer (inlink, perms, w, h) :
  138. ff_default_get_video_buffer(inlink, perms, w, h);
  139. }
  140. static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  141. {
  142. TransContext *trans = inlink->dst->priv;
  143. AVFilterLink *outlink = inlink->dst->outputs[0];
  144. AVFilterBufferRef *buf_out;
  145. if (trans->passthrough)
  146. return ff_null_start_frame(inlink, picref);
  147. outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE,
  148. outlink->w, outlink->h);
  149. if (!outlink->out_buf)
  150. return AVERROR(ENOMEM);
  151. outlink->out_buf->pts = picref->pts;
  152. if (picref->video->sample_aspect_ratio.num == 0) {
  153. outlink->out_buf->video->sample_aspect_ratio = picref->video->sample_aspect_ratio;
  154. } else {
  155. outlink->out_buf->video->sample_aspect_ratio.num = picref->video->sample_aspect_ratio.den;
  156. outlink->out_buf->video->sample_aspect_ratio.den = picref->video->sample_aspect_ratio.num;
  157. }
  158. buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
  159. if (!buf_out)
  160. return AVERROR(ENOMEM);
  161. return ff_start_frame(outlink, buf_out);
  162. }
  163. static int end_frame(AVFilterLink *inlink)
  164. {
  165. TransContext *trans = inlink->dst->priv;
  166. AVFilterBufferRef *inpic = inlink->cur_buf;
  167. AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
  168. AVFilterLink *outlink = inlink->dst->outputs[0];
  169. int plane, ret;
  170. if (trans->passthrough)
  171. return ff_null_end_frame(inlink);
  172. for (plane = 0; outpic->data[plane]; plane++) {
  173. int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
  174. int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
  175. int pixstep = trans->pixsteps[plane];
  176. int inh = inpic->video->h>>vsub;
  177. int outw = outpic->video->w>>hsub;
  178. int outh = outpic->video->h>>vsub;
  179. uint8_t *out, *in;
  180. int outlinesize, inlinesize;
  181. int x, y;
  182. out = outpic->data[plane]; outlinesize = outpic->linesize[plane];
  183. in = inpic ->data[plane]; inlinesize = inpic ->linesize[plane];
  184. if (trans->dir&1) {
  185. in += inpic->linesize[plane] * (inh-1);
  186. inlinesize *= -1;
  187. }
  188. if (trans->dir&2) {
  189. out += outpic->linesize[plane] * (outh-1);
  190. outlinesize *= -1;
  191. }
  192. for (y = 0; y < outh; y++) {
  193. switch (pixstep) {
  194. case 1:
  195. for (x = 0; x < outw; x++)
  196. out[x] = in[x*inlinesize + y];
  197. break;
  198. case 2:
  199. for (x = 0; x < outw; x++)
  200. *((uint16_t *)(out + 2*x)) = *((uint16_t *)(in + x*inlinesize + y*2));
  201. break;
  202. case 3:
  203. for (x = 0; x < outw; x++) {
  204. int32_t v = AV_RB24(in + x*inlinesize + y*3);
  205. AV_WB24(out + 3*x, v);
  206. }
  207. break;
  208. case 4:
  209. for (x = 0; x < outw; x++)
  210. *((uint32_t *)(out + 4*x)) = *((uint32_t *)(in + x*inlinesize + y*4));
  211. break;
  212. }
  213. out += outlinesize;
  214. }
  215. }
  216. if ((ret = ff_draw_slice(outlink, 0, outpic->video->h, 1)) < 0 ||
  217. (ret = ff_end_frame(outlink)) < 0)
  218. return ret;
  219. return 0;
  220. }
  221. static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  222. {
  223. TransContext *trans = inlink->dst->priv;
  224. return trans->passthrough ? ff_null_draw_slice(inlink, y, h, slice_dir) : 0;
  225. }
  226. AVFilter avfilter_vf_transpose = {
  227. .name = "transpose",
  228. .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
  229. .init = init,
  230. .priv_size = sizeof(TransContext),
  231. .query_formats = query_formats,
  232. .inputs = (const AVFilterPad[]) {{ .name = "default",
  233. .type = AVMEDIA_TYPE_VIDEO,
  234. .get_video_buffer= get_video_buffer,
  235. .start_frame = start_frame,
  236. .draw_slice = draw_slice,
  237. .end_frame = end_frame,
  238. .min_perms = AV_PERM_READ, },
  239. { .name = NULL}},
  240. .outputs = (const AVFilterPad[]) {{ .name = "default",
  241. .config_props = config_props_output,
  242. .type = AVMEDIA_TYPE_VIDEO, },
  243. { .name = NULL}},
  244. .priv_class = &transpose_class,
  245. };