vf_transpose.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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/pixdesc.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/internal.h"
  31. #include "avfilter.h"
  32. #include "formats.h"
  33. #include "internal.h"
  34. #include "video.h"
  35. typedef struct {
  36. int hsub, vsub;
  37. int pixsteps[4];
  38. /* 0 Rotate by 90 degrees counterclockwise and vflip. */
  39. /* 1 Rotate by 90 degrees clockwise. */
  40. /* 2 Rotate by 90 degrees counterclockwise. */
  41. /* 3 Rotate by 90 degrees clockwise and vflip. */
  42. int dir;
  43. int passthrough; ///< landscape passthrough mode enabled
  44. } TransContext;
  45. static av_cold int init(AVFilterContext *ctx, const char *args)
  46. {
  47. TransContext *trans = ctx->priv;
  48. trans->dir = 0;
  49. if (args)
  50. sscanf(args, "%d", &trans->dir);
  51. if (trans->dir < 0 || trans->dir > 7) {
  52. av_log(ctx, AV_LOG_ERROR, "Invalid value %d not between 0 and 7.\n",
  53. trans->dir);
  54. return AVERROR(EINVAL);
  55. }
  56. return 0;
  57. }
  58. static int query_formats(AVFilterContext *ctx)
  59. {
  60. enum PixelFormat pix_fmts[] = {
  61. PIX_FMT_ARGB, PIX_FMT_RGBA,
  62. PIX_FMT_ABGR, PIX_FMT_BGRA,
  63. PIX_FMT_RGB24, PIX_FMT_BGR24,
  64. PIX_FMT_RGB565BE, PIX_FMT_RGB565LE,
  65. PIX_FMT_RGB555BE, PIX_FMT_RGB555LE,
  66. PIX_FMT_BGR565BE, PIX_FMT_BGR565LE,
  67. PIX_FMT_BGR555BE, PIX_FMT_BGR555LE,
  68. PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE,
  69. PIX_FMT_YUV420P16LE, PIX_FMT_YUV420P16BE,
  70. PIX_FMT_YUV444P16LE, PIX_FMT_YUV444P16BE,
  71. PIX_FMT_NV12, PIX_FMT_NV21,
  72. PIX_FMT_RGB8, PIX_FMT_BGR8,
  73. PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE,
  74. PIX_FMT_YUV444P, PIX_FMT_YUVJ444P,
  75. PIX_FMT_YUV420P, PIX_FMT_YUVJ420P,
  76. PIX_FMT_YUV410P,
  77. PIX_FMT_YUVA420P, PIX_FMT_GRAY8,
  78. PIX_FMT_NONE
  79. };
  80. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  81. return 0;
  82. }
  83. static int config_props_output(AVFilterLink *outlink)
  84. {
  85. AVFilterContext *ctx = outlink->src;
  86. TransContext *trans = ctx->priv;
  87. AVFilterLink *inlink = ctx->inputs[0];
  88. const AVPixFmtDescriptor *pixdesc = &av_pix_fmt_descriptors[outlink->format];
  89. if (trans->dir&4) {
  90. trans->dir &= 3;
  91. if (inlink->w >= inlink->h) {
  92. trans->passthrough = 1;
  93. av_log(ctx, AV_LOG_VERBOSE,
  94. "w:%d h:%d -> w:%d h:%d (landscape passthrough mode)\n",
  95. inlink->w, inlink->h, outlink->w, outlink->h);
  96. return 0;
  97. }
  98. }
  99. trans->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
  100. trans->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
  101. av_image_fill_max_pixsteps(trans->pixsteps, NULL, pixdesc);
  102. outlink->w = inlink->h;
  103. outlink->h = inlink->w;
  104. if (inlink->sample_aspect_ratio.num){
  105. outlink->sample_aspect_ratio = av_div_q((AVRational){1,1}, inlink->sample_aspect_ratio);
  106. } else
  107. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  108. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
  109. inlink->w, inlink->h, trans->dir, outlink->w, outlink->h,
  110. trans->dir == 1 || trans->dir == 3 ? "clockwise" : "counterclockwise",
  111. trans->dir == 0 || trans->dir == 3);
  112. return 0;
  113. }
  114. static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
  115. {
  116. TransContext *trans = inlink->dst->priv;
  117. return trans->passthrough ?
  118. ff_null_get_video_buffer (inlink, perms, w, h) :
  119. ff_default_get_video_buffer(inlink, perms, w, h);
  120. }
  121. static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  122. {
  123. TransContext *trans = inlink->dst->priv;
  124. AVFilterLink *outlink = inlink->dst->outputs[0];
  125. AVFilterBufferRef *buf_out;
  126. if (trans->passthrough)
  127. return ff_null_start_frame(inlink, picref);
  128. outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE,
  129. outlink->w, outlink->h);
  130. if (!outlink->out_buf)
  131. return AVERROR(ENOMEM);
  132. outlink->out_buf->pts = picref->pts;
  133. if (picref->video->sample_aspect_ratio.num == 0) {
  134. outlink->out_buf->video->sample_aspect_ratio = picref->video->sample_aspect_ratio;
  135. } else {
  136. outlink->out_buf->video->sample_aspect_ratio.num = picref->video->sample_aspect_ratio.den;
  137. outlink->out_buf->video->sample_aspect_ratio.den = picref->video->sample_aspect_ratio.num;
  138. }
  139. buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
  140. if (!buf_out)
  141. return AVERROR(ENOMEM);
  142. return ff_start_frame(outlink, buf_out);
  143. }
  144. static int end_frame(AVFilterLink *inlink)
  145. {
  146. TransContext *trans = inlink->dst->priv;
  147. AVFilterBufferRef *inpic = inlink->cur_buf;
  148. AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
  149. AVFilterLink *outlink = inlink->dst->outputs[0];
  150. int plane, ret;
  151. if (trans->passthrough)
  152. return ff_null_end_frame(inlink);
  153. for (plane = 0; outpic->data[plane]; plane++) {
  154. int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
  155. int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
  156. int pixstep = trans->pixsteps[plane];
  157. int inh = inpic->video->h>>vsub;
  158. int outw = outpic->video->w>>hsub;
  159. int outh = outpic->video->h>>vsub;
  160. uint8_t *out, *in;
  161. int outlinesize, inlinesize;
  162. int x, y;
  163. out = outpic->data[plane]; outlinesize = outpic->linesize[plane];
  164. in = inpic ->data[plane]; inlinesize = inpic ->linesize[plane];
  165. if (trans->dir&1) {
  166. in += inpic->linesize[plane] * (inh-1);
  167. inlinesize *= -1;
  168. }
  169. if (trans->dir&2) {
  170. out += outpic->linesize[plane] * (outh-1);
  171. outlinesize *= -1;
  172. }
  173. for (y = 0; y < outh; y++) {
  174. switch (pixstep) {
  175. case 1:
  176. for (x = 0; x < outw; x++)
  177. out[x] = in[x*inlinesize + y];
  178. break;
  179. case 2:
  180. for (x = 0; x < outw; x++)
  181. *((uint16_t *)(out + 2*x)) = *((uint16_t *)(in + x*inlinesize + y*2));
  182. break;
  183. case 3:
  184. for (x = 0; x < outw; x++) {
  185. int32_t v = AV_RB24(in + x*inlinesize + y*3);
  186. AV_WB24(out + 3*x, v);
  187. }
  188. break;
  189. case 4:
  190. for (x = 0; x < outw; x++)
  191. *((uint32_t *)(out + 4*x)) = *((uint32_t *)(in + x*inlinesize + y*4));
  192. break;
  193. }
  194. out += outlinesize;
  195. }
  196. }
  197. if ((ret = ff_draw_slice(outlink, 0, outpic->video->h, 1)) < 0 ||
  198. (ret = ff_end_frame(outlink)) < 0)
  199. return ret;
  200. return 0;
  201. }
  202. static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  203. {
  204. TransContext *trans = inlink->dst->priv;
  205. return trans->passthrough ? ff_null_draw_slice(inlink, y, h, slice_dir) : 0;
  206. }
  207. AVFilter avfilter_vf_transpose = {
  208. .name = "transpose",
  209. .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
  210. .init = init,
  211. .priv_size = sizeof(TransContext),
  212. .query_formats = query_formats,
  213. .inputs = (const AVFilterPad[]) {{ .name = "default",
  214. .type = AVMEDIA_TYPE_VIDEO,
  215. .get_video_buffer= get_video_buffer,
  216. .start_frame = start_frame,
  217. .draw_slice = draw_slice,
  218. .end_frame = end_frame,
  219. .min_perms = AV_PERM_READ, },
  220. { .name = NULL}},
  221. .outputs = (const AVFilterPad[]) {{ .name = "default",
  222. .config_props = config_props_output,
  223. .type = AVMEDIA_TYPE_VIDEO, },
  224. { .name = NULL}},
  225. };