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.

231 lines
7.9KB

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