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.

305 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 AVPixelFormat pix_fmts[] = {
  75. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  76. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  77. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  78. AV_PIX_FMT_RGB565BE, AV_PIX_FMT_RGB565LE,
  79. AV_PIX_FMT_RGB555BE, AV_PIX_FMT_RGB555LE,
  80. AV_PIX_FMT_BGR565BE, AV_PIX_FMT_BGR565LE,
  81. AV_PIX_FMT_BGR555BE, AV_PIX_FMT_BGR555LE,
  82. AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE,
  83. AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUV420P16BE,
  84. AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUV444P16BE,
  85. AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
  86. AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8,
  87. AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
  88. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  89. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
  90. AV_PIX_FMT_YUV410P,
  91. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_GRAY8,
  92. AV_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 *desc_out = av_pix_fmt_desc_get(outlink->format);
  103. const AVPixFmtDescriptor *desc_in = av_pix_fmt_desc_get(inlink->format);
  104. if (trans->dir&4) {
  105. av_log(ctx, AV_LOG_WARNING,
  106. "dir values greater than 3 are deprecated, use the passthrough option instead\n");
  107. trans->dir &= 3;
  108. trans->passthrough = TRANSPOSE_PT_TYPE_LANDSCAPE;
  109. }
  110. if ((inlink->w >= inlink->h && trans->passthrough == TRANSPOSE_PT_TYPE_LANDSCAPE) ||
  111. (inlink->w <= inlink->h && trans->passthrough == TRANSPOSE_PT_TYPE_PORTRAIT)) {
  112. av_log(ctx, AV_LOG_VERBOSE,
  113. "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
  114. inlink->w, inlink->h, inlink->w, inlink->h);
  115. return 0;
  116. } else {
  117. trans->passthrough = TRANSPOSE_PT_TYPE_NONE;
  118. }
  119. trans->hsub = desc_in->log2_chroma_w;
  120. trans->vsub = desc_in->log2_chroma_h;
  121. av_image_fill_max_pixsteps(trans->pixsteps, NULL, desc_out);
  122. outlink->w = inlink->h;
  123. outlink->h = inlink->w;
  124. if (inlink->sample_aspect_ratio.num){
  125. outlink->sample_aspect_ratio = av_div_q((AVRational){1,1}, inlink->sample_aspect_ratio);
  126. } else
  127. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  128. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
  129. inlink->w, inlink->h, trans->dir, outlink->w, outlink->h,
  130. trans->dir == 1 || trans->dir == 3 ? "clockwise" : "counterclockwise",
  131. trans->dir == 0 || trans->dir == 3);
  132. return 0;
  133. }
  134. static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
  135. {
  136. TransContext *trans = inlink->dst->priv;
  137. return trans->passthrough ?
  138. ff_null_get_video_buffer (inlink, perms, w, h) :
  139. ff_default_get_video_buffer(inlink, perms, w, h);
  140. }
  141. static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  142. {
  143. TransContext *trans = inlink->dst->priv;
  144. AVFilterLink *outlink = inlink->dst->outputs[0];
  145. AVFilterBufferRef *buf_out;
  146. if (trans->passthrough)
  147. return ff_null_start_frame(inlink, picref);
  148. outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE,
  149. outlink->w, outlink->h);
  150. if (!outlink->out_buf)
  151. return AVERROR(ENOMEM);
  152. outlink->out_buf->pts = picref->pts;
  153. if (picref->video->sample_aspect_ratio.num == 0) {
  154. outlink->out_buf->video->sample_aspect_ratio = picref->video->sample_aspect_ratio;
  155. } else {
  156. outlink->out_buf->video->sample_aspect_ratio.num = picref->video->sample_aspect_ratio.den;
  157. outlink->out_buf->video->sample_aspect_ratio.den = picref->video->sample_aspect_ratio.num;
  158. }
  159. buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
  160. if (!buf_out)
  161. return AVERROR(ENOMEM);
  162. return ff_start_frame(outlink, buf_out);
  163. }
  164. static int end_frame(AVFilterLink *inlink)
  165. {
  166. TransContext *trans = inlink->dst->priv;
  167. AVFilterBufferRef *inpic = inlink->cur_buf;
  168. AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
  169. AVFilterLink *outlink = inlink->dst->outputs[0];
  170. int plane, ret;
  171. if (trans->passthrough)
  172. return ff_null_end_frame(inlink);
  173. for (plane = 0; outpic->data[plane]; plane++) {
  174. int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
  175. int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
  176. int pixstep = trans->pixsteps[plane];
  177. int inh = inpic->video->h>>vsub;
  178. int outw = outpic->video->w>>hsub;
  179. int outh = outpic->video->h>>vsub;
  180. uint8_t *out, *in;
  181. int outlinesize, inlinesize;
  182. int x, y;
  183. out = outpic->data[plane]; outlinesize = outpic->linesize[plane];
  184. in = inpic ->data[plane]; inlinesize = inpic ->linesize[plane];
  185. if (trans->dir&1) {
  186. in += inpic->linesize[plane] * (inh-1);
  187. inlinesize *= -1;
  188. }
  189. if (trans->dir&2) {
  190. out += outpic->linesize[plane] * (outh-1);
  191. outlinesize *= -1;
  192. }
  193. for (y = 0; y < outh; y++) {
  194. switch (pixstep) {
  195. case 1:
  196. for (x = 0; x < outw; x++)
  197. out[x] = in[x*inlinesize + y];
  198. break;
  199. case 2:
  200. for (x = 0; x < outw; x++)
  201. *((uint16_t *)(out + 2*x)) = *((uint16_t *)(in + x*inlinesize + y*2));
  202. break;
  203. case 3:
  204. for (x = 0; x < outw; x++) {
  205. int32_t v = AV_RB24(in + x*inlinesize + y*3);
  206. AV_WB24(out + 3*x, v);
  207. }
  208. break;
  209. case 4:
  210. for (x = 0; x < outw; x++)
  211. *((uint32_t *)(out + 4*x)) = *((uint32_t *)(in + x*inlinesize + y*4));
  212. break;
  213. }
  214. out += outlinesize;
  215. }
  216. }
  217. if ((ret = ff_draw_slice(outlink, 0, outpic->video->h, 1)) < 0 ||
  218. (ret = ff_end_frame(outlink)) < 0)
  219. return ret;
  220. return 0;
  221. }
  222. static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  223. {
  224. TransContext *trans = inlink->dst->priv;
  225. return trans->passthrough ? ff_null_draw_slice(inlink, y, h, slice_dir) : 0;
  226. }
  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. .start_frame = start_frame,
  233. .draw_slice = draw_slice,
  234. .end_frame = end_frame,
  235. .min_perms = AV_PERM_READ,
  236. },
  237. { NULL }
  238. };
  239. static const AVFilterPad avfilter_vf_transpose_outputs[] = {
  240. {
  241. .name = "default",
  242. .config_props = config_props_output,
  243. .type = AVMEDIA_TYPE_VIDEO,
  244. },
  245. { NULL }
  246. };
  247. AVFilter avfilter_vf_transpose = {
  248. .name = "transpose",
  249. .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
  250. .init = init,
  251. .priv_size = sizeof(TransContext),
  252. .query_formats = query_formats,
  253. .inputs = avfilter_vf_transpose_inputs,
  254. .outputs = avfilter_vf_transpose_outputs,
  255. .priv_class = &transpose_class,
  256. };