vf_transpose.c 7.7 KB

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