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.

167 lines
5.9KB

  1. /*
  2. * Copyright (c) 2007 Benoit Fouet
  3. * Copyright (c) 2010 Stefano Sabatini
  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. * horizontal flip filter
  24. */
  25. #include <string.h>
  26. #include "avfilter.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/internal.h"
  32. #include "libavutil/intreadwrite.h"
  33. #include "libavutil/imgutils.h"
  34. typedef struct {
  35. int max_step[4]; ///< max pixel step for each plane, expressed as a number of bytes
  36. int hsub, vsub; ///< chroma subsampling
  37. } FlipContext;
  38. static int query_formats(AVFilterContext *ctx)
  39. {
  40. static const enum AVPixelFormat pix_fmts[] = {
  41. AV_PIX_FMT_RGB48BE, AV_PIX_FMT_RGB48LE,
  42. AV_PIX_FMT_BGR48BE, AV_PIX_FMT_BGR48LE,
  43. AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA,
  44. AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
  45. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  46. AV_PIX_FMT_RGB565BE, AV_PIX_FMT_RGB565LE,
  47. AV_PIX_FMT_RGB555BE, AV_PIX_FMT_RGB555LE,
  48. AV_PIX_FMT_BGR565BE, AV_PIX_FMT_BGR565LE,
  49. AV_PIX_FMT_BGR555BE, AV_PIX_FMT_BGR555LE,
  50. AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE,
  51. AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUV420P16BE,
  52. AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUV422P16BE,
  53. AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUV444P16BE,
  54. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  55. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  56. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  57. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
  58. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ440P,
  59. AV_PIX_FMT_YUVA420P,
  60. AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8,
  61. AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
  62. AV_PIX_FMT_PAL8, AV_PIX_FMT_GRAY8,
  63. AV_PIX_FMT_NONE
  64. };
  65. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  66. return 0;
  67. }
  68. static int config_props(AVFilterLink *inlink)
  69. {
  70. FlipContext *flip = inlink->dst->priv;
  71. const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
  72. av_image_fill_max_pixsteps(flip->max_step, NULL, pix_desc);
  73. flip->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
  74. flip->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
  75. return 0;
  76. }
  77. static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  78. {
  79. FlipContext *flip = inlink->dst->priv;
  80. AVFilterBufferRef *inpic = inlink->cur_buf;
  81. AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
  82. uint8_t *inrow, *outrow;
  83. int i, j, plane, step, hsub, vsub;
  84. for (plane = 0; plane < 4 && inpic->data[plane]; plane++) {
  85. step = flip->max_step[plane];
  86. hsub = (plane == 1 || plane == 2) ? flip->hsub : 0;
  87. vsub = (plane == 1 || plane == 2) ? flip->vsub : 0;
  88. outrow = outpic->data[plane] + (y>>vsub) * outpic->linesize[plane];
  89. inrow = inpic ->data[plane] + (y>>vsub) * inpic ->linesize[plane] + ((inlink->w >> hsub) - 1) * step;
  90. for (i = 0; i < h>>vsub; i++) {
  91. switch (step) {
  92. case 1:
  93. for (j = 0; j < (inlink->w >> hsub); j++)
  94. outrow[j] = inrow[-j];
  95. break;
  96. case 2:
  97. {
  98. uint16_t *outrow16 = (uint16_t *)outrow;
  99. uint16_t * inrow16 = (uint16_t *) inrow;
  100. for (j = 0; j < (inlink->w >> hsub); j++)
  101. outrow16[j] = inrow16[-j];
  102. }
  103. break;
  104. case 3:
  105. {
  106. uint8_t *in = inrow;
  107. uint8_t *out = outrow;
  108. for (j = 0; j < (inlink->w >> hsub); j++, out += 3, in -= 3) {
  109. int32_t v = AV_RB24(in);
  110. AV_WB24(out, v);
  111. }
  112. }
  113. break;
  114. case 4:
  115. {
  116. uint32_t *outrow32 = (uint32_t *)outrow;
  117. uint32_t * inrow32 = (uint32_t *) inrow;
  118. for (j = 0; j < (inlink->w >> hsub); j++)
  119. outrow32[j] = inrow32[-j];
  120. }
  121. break;
  122. default:
  123. for (j = 0; j < (inlink->w >> hsub); j++)
  124. memcpy(outrow + j*step, inrow - j*step, step);
  125. }
  126. inrow += inpic ->linesize[plane];
  127. outrow += outpic->linesize[plane];
  128. }
  129. }
  130. return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
  131. }
  132. AVFilter avfilter_vf_hflip = {
  133. .name = "hflip",
  134. .description = NULL_IF_CONFIG_SMALL("Horizontally flip the input video."),
  135. .priv_size = sizeof(FlipContext),
  136. .query_formats = query_formats,
  137. .inputs = (const AVFilterPad[]) {{ .name = "default",
  138. .type = AVMEDIA_TYPE_VIDEO,
  139. .draw_slice = draw_slice,
  140. .config_props = config_props,
  141. .min_perms = AV_PERM_READ, },
  142. { .name = NULL}},
  143. .outputs = (const AVFilterPad[]) {{ .name = "default",
  144. .type = AVMEDIA_TYPE_VIDEO, },
  145. { .name = NULL}},
  146. };