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.

221 lines
7.5KB

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