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.

177 lines
5.1KB

  1. /*
  2. * Copyright (c) 2007 Benoit Fouet
  3. * Copyright (c) 2010 Stefano Sabatini
  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. * 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. AVFilterFormats *pix_fmts = NULL;
  41. int fmt;
  42. for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
  43. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  44. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
  45. desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ||
  46. (desc->log2_chroma_w != desc->log2_chroma_h &&
  47. desc->comp[0].plane == desc->comp[1].plane)))
  48. ff_add_format(&pix_fmts, fmt);
  49. }
  50. ff_set_common_formats(ctx, pix_fmts);
  51. return 0;
  52. }
  53. static int config_props(AVFilterLink *inlink)
  54. {
  55. FlipContext *s = inlink->dst->priv;
  56. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  57. av_image_fill_max_pixsteps(s->max_step, NULL, pix_desc);
  58. s->hsub = pix_desc->log2_chroma_w;
  59. s->vsub = pix_desc->log2_chroma_h;
  60. return 0;
  61. }
  62. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  63. {
  64. AVFilterContext *ctx = inlink->dst;
  65. FlipContext *s = ctx->priv;
  66. AVFilterLink *outlink = ctx->outputs[0];
  67. AVFrame *out;
  68. uint8_t *inrow, *outrow;
  69. int i, j, plane, step;
  70. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  71. if (!out) {
  72. av_frame_free(&in);
  73. return AVERROR(ENOMEM);
  74. }
  75. av_frame_copy_props(out, in);
  76. /* copy palette if required */
  77. if (av_pix_fmt_desc_get(inlink->format)->flags & AV_PIX_FMT_FLAG_PAL)
  78. memcpy(out->data[1], in->data[1], AVPALETTE_SIZE);
  79. for (plane = 0; plane < 4 && in->data[plane]; plane++) {
  80. const int width = (plane == 1 || plane == 2) ? FF_CEIL_RSHIFT(inlink->w, s->hsub) : inlink->w;
  81. const int height = (plane == 1 || plane == 2) ? FF_CEIL_RSHIFT(inlink->h, s->vsub) : inlink->h;
  82. step = s->max_step[plane];
  83. outrow = out->data[plane];
  84. inrow = in ->data[plane] + (width - 1) * step;
  85. for (i = 0; i < height; i++) {
  86. switch (step) {
  87. case 1:
  88. for (j = 0; j < width; j++)
  89. outrow[j] = inrow[-j];
  90. break;
  91. case 2:
  92. {
  93. uint16_t *outrow16 = (uint16_t *)outrow;
  94. uint16_t * inrow16 = (uint16_t *) inrow;
  95. for (j = 0; j < width; j++)
  96. outrow16[j] = inrow16[-j];
  97. }
  98. break;
  99. case 3:
  100. {
  101. uint8_t *in = inrow;
  102. uint8_t *out = outrow;
  103. for (j = 0; j < width; j++, out += 3, in -= 3) {
  104. int32_t v = AV_RB24(in);
  105. AV_WB24(out, v);
  106. }
  107. }
  108. break;
  109. case 4:
  110. {
  111. uint32_t *outrow32 = (uint32_t *)outrow;
  112. uint32_t * inrow32 = (uint32_t *) inrow;
  113. for (j = 0; j < width; j++)
  114. outrow32[j] = inrow32[-j];
  115. }
  116. break;
  117. default:
  118. for (j = 0; j < width; j++)
  119. memcpy(outrow + j*step, inrow - j*step, step);
  120. }
  121. inrow += in ->linesize[plane];
  122. outrow += out->linesize[plane];
  123. }
  124. }
  125. av_frame_free(&in);
  126. return ff_filter_frame(outlink, out);
  127. }
  128. static const AVFilterPad avfilter_vf_hflip_inputs[] = {
  129. {
  130. .name = "default",
  131. .type = AVMEDIA_TYPE_VIDEO,
  132. .filter_frame = filter_frame,
  133. .config_props = config_props,
  134. },
  135. { NULL }
  136. };
  137. static const AVFilterPad avfilter_vf_hflip_outputs[] = {
  138. {
  139. .name = "default",
  140. .type = AVMEDIA_TYPE_VIDEO,
  141. },
  142. { NULL }
  143. };
  144. AVFilter avfilter_vf_hflip = {
  145. .name = "hflip",
  146. .description = NULL_IF_CONFIG_SMALL("Horizontally flip the input video."),
  147. .priv_size = sizeof(FlipContext),
  148. .query_formats = query_formats,
  149. .inputs = avfilter_vf_hflip_inputs,
  150. .outputs = avfilter_vf_hflip_outputs,
  151. };