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.

244 lines
7.8KB

  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 <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 AVPixelFormat pix_fmts[] = {
  60. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  61. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  62. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  63. AV_PIX_FMT_RGB565BE, AV_PIX_FMT_RGB565LE,
  64. AV_PIX_FMT_RGB555BE, AV_PIX_FMT_RGB555LE,
  65. AV_PIX_FMT_BGR565BE, AV_PIX_FMT_BGR565LE,
  66. AV_PIX_FMT_BGR555BE, AV_PIX_FMT_BGR555LE,
  67. AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE,
  68. AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUV420P16BE,
  69. AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUV422P16BE,
  70. AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUV444P16BE,
  71. AV_PIX_FMT_NV12, AV_PIX_FMT_NV21,
  72. AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8,
  73. AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
  74. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  75. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
  76. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  77. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
  78. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
  79. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_GRAY8,
  80. AV_PIX_FMT_NONE
  81. };
  82. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  83. return 0;
  84. }
  85. static int config_props_output(AVFilterLink *outlink)
  86. {
  87. AVFilterContext *ctx = outlink->src;
  88. TransContext *trans = ctx->priv;
  89. AVFilterLink *inlink = ctx->inputs[0];
  90. const AVPixFmtDescriptor *desc_out = av_pix_fmt_desc_get(outlink->format);
  91. const AVPixFmtDescriptor *desc_in = av_pix_fmt_desc_get(inlink->format);
  92. trans->hsub = desc_in->log2_chroma_w;
  93. trans->vsub = desc_in->log2_chroma_h;
  94. av_image_fill_max_pixsteps(trans->pixsteps, NULL, desc_out);
  95. outlink->w = inlink->h;
  96. outlink->h = inlink->w;
  97. if (inlink->sample_aspect_ratio.num){
  98. outlink->sample_aspect_ratio = av_div_q((AVRational){1,1}, inlink->sample_aspect_ratio);
  99. } else
  100. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  101. av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d dir:%d -> w:%d h:%d rotation:%s vflip:%d\n",
  102. inlink->w, inlink->h, trans->dir, outlink->w, outlink->h,
  103. trans->dir == 1 || trans->dir == 3 ? "clockwise" : "counterclockwise",
  104. trans->dir == 0 || trans->dir == 3);
  105. return 0;
  106. }
  107. static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  108. {
  109. AVFilterLink *outlink = inlink->dst->outputs[0];
  110. AVFilterBufferRef *buf_out;
  111. outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE,
  112. outlink->w, outlink->h);
  113. if (!outlink->out_buf)
  114. return AVERROR(ENOMEM);
  115. outlink->out_buf->pts = picref->pts;
  116. if (picref->video->pixel_aspect.num == 0) {
  117. outlink->out_buf->video->pixel_aspect = picref->video->pixel_aspect;
  118. } else {
  119. outlink->out_buf->video->pixel_aspect.num = picref->video->pixel_aspect.den;
  120. outlink->out_buf->video->pixel_aspect.den = picref->video->pixel_aspect.num;
  121. }
  122. buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
  123. if (!buf_out)
  124. return AVERROR(ENOMEM);
  125. return ff_start_frame(outlink, buf_out);
  126. }
  127. static int end_frame(AVFilterLink *inlink)
  128. {
  129. TransContext *trans = inlink->dst->priv;
  130. AVFilterBufferRef *inpic = inlink->cur_buf;
  131. AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
  132. AVFilterLink *outlink = inlink->dst->outputs[0];
  133. int plane, ret;
  134. for (plane = 0; outpic->data[plane]; plane++) {
  135. int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
  136. int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
  137. int pixstep = trans->pixsteps[plane];
  138. int inh = inpic->video->h>>vsub;
  139. int outw = outpic->video->w>>hsub;
  140. int outh = outpic->video->h>>vsub;
  141. uint8_t *out, *in;
  142. int outlinesize, inlinesize;
  143. int x, y;
  144. out = outpic->data[plane]; outlinesize = outpic->linesize[plane];
  145. in = inpic ->data[plane]; inlinesize = inpic ->linesize[plane];
  146. if (trans->dir&1) {
  147. in += inpic->linesize[plane] * (inh-1);
  148. inlinesize *= -1;
  149. }
  150. if (trans->dir&2) {
  151. out += outpic->linesize[plane] * (outh-1);
  152. outlinesize *= -1;
  153. }
  154. for (y = 0; y < outh; y++) {
  155. switch (pixstep) {
  156. case 1:
  157. for (x = 0; x < outw; x++)
  158. out[x] = in[x*inlinesize + y];
  159. break;
  160. case 2:
  161. for (x = 0; x < outw; x++)
  162. *((uint16_t *)(out + 2*x)) = *((uint16_t *)(in + x*inlinesize + y*2));
  163. break;
  164. case 3:
  165. for (x = 0; x < outw; x++) {
  166. int32_t v = AV_RB24(in + x*inlinesize + y*3);
  167. AV_WB24(out + 3*x, v);
  168. }
  169. break;
  170. case 4:
  171. for (x = 0; x < outw; x++)
  172. *((uint32_t *)(out + 4*x)) = *((uint32_t *)(in + x*inlinesize + y*4));
  173. break;
  174. }
  175. out += outlinesize;
  176. }
  177. }
  178. if ((ret = ff_draw_slice(outlink, 0, outpic->video->h, 1)) < 0 ||
  179. (ret = ff_end_frame(outlink)) < 0)
  180. return ret;
  181. return 0;
  182. }
  183. static const AVFilterPad avfilter_vf_transpose_inputs[] = {
  184. {
  185. .name = "default",
  186. .type = AVMEDIA_TYPE_VIDEO,
  187. .start_frame = start_frame,
  188. .end_frame = end_frame,
  189. .min_perms = AV_PERM_READ,
  190. },
  191. { NULL }
  192. };
  193. static const AVFilterPad avfilter_vf_transpose_outputs[] = {
  194. {
  195. .name = "default",
  196. .config_props = config_props_output,
  197. .type = AVMEDIA_TYPE_VIDEO,
  198. },
  199. { NULL }
  200. };
  201. AVFilter avfilter_vf_transpose = {
  202. .name = "transpose",
  203. .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
  204. .init = init,
  205. .priv_size = sizeof(TransContext),
  206. .query_formats = query_formats,
  207. .inputs = avfilter_vf_transpose_inputs,
  208. .outputs = avfilter_vf_transpose_outputs,
  209. };