vf_hflip.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 FlipContext {
  35. int max_step[4]; ///< max pixel step for each plane, expressed as a number of bytes
  36. int planewidth[4]; ///< width of each plane
  37. int planeheight[4]; ///< height of each plane
  38. } FlipContext;
  39. static int query_formats(AVFilterContext *ctx)
  40. {
  41. AVFilterFormats *pix_fmts = NULL;
  42. int fmt, ret;
  43. for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
  44. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  45. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
  46. desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ||
  47. (desc->log2_chroma_w != desc->log2_chroma_h &&
  48. desc->comp[0].plane == desc->comp[1].plane)) &&
  49. (ret = ff_add_format(&pix_fmts, fmt)) < 0)
  50. return ret;
  51. }
  52. return ff_set_common_formats(ctx, pix_fmts);
  53. }
  54. static int config_props(AVFilterLink *inlink)
  55. {
  56. FlipContext *s = inlink->dst->priv;
  57. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  58. const int hsub = pix_desc->log2_chroma_w;
  59. const int vsub = pix_desc->log2_chroma_h;
  60. av_image_fill_max_pixsteps(s->max_step, NULL, pix_desc);
  61. s->planewidth[0] = s->planewidth[3] = inlink->w;
  62. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  63. s->planeheight[0] = s->planeheight[3] = inlink->h;
  64. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  65. return 0;
  66. }
  67. typedef struct ThreadData {
  68. AVFrame *in, *out;
  69. } ThreadData;
  70. static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
  71. {
  72. FlipContext *s = ctx->priv;
  73. ThreadData *td = arg;
  74. AVFrame *in = td->in;
  75. AVFrame *out = td->out;
  76. uint8_t *inrow, *outrow;
  77. int i, j, plane, step;
  78. for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
  79. const int width = s->planewidth[plane];
  80. const int height = s->planeheight[plane];
  81. const int start = (height * job ) / nb_jobs;
  82. const int end = (height * (job+1)) / nb_jobs;
  83. step = s->max_step[plane];
  84. outrow = out->data[plane] + start * out->linesize[plane];
  85. inrow = in ->data[plane] + start * in->linesize[plane] + (width - 1) * step;
  86. for (i = start; i < end; i++) {
  87. switch (step) {
  88. case 1:
  89. for (j = 0; j < width; j++)
  90. outrow[j] = inrow[-j];
  91. break;
  92. case 2:
  93. {
  94. uint16_t *outrow16 = (uint16_t *)outrow;
  95. uint16_t * inrow16 = (uint16_t *) inrow;
  96. for (j = 0; j < width; j++)
  97. outrow16[j] = inrow16[-j];
  98. }
  99. break;
  100. case 3:
  101. {
  102. uint8_t *in = inrow;
  103. uint8_t *out = outrow;
  104. for (j = 0; j < width; j++, out += 3, in -= 3) {
  105. int32_t v = AV_RB24(in);
  106. AV_WB24(out, v);
  107. }
  108. }
  109. break;
  110. case 4:
  111. {
  112. uint32_t *outrow32 = (uint32_t *)outrow;
  113. uint32_t * inrow32 = (uint32_t *) inrow;
  114. for (j = 0; j < width; j++)
  115. outrow32[j] = inrow32[-j];
  116. }
  117. break;
  118. default:
  119. for (j = 0; j < width; j++)
  120. memcpy(outrow + j*step, inrow - j*step, step);
  121. }
  122. inrow += in ->linesize[plane];
  123. outrow += out->linesize[plane];
  124. }
  125. }
  126. return 0;
  127. }
  128. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  129. {
  130. AVFilterContext *ctx = inlink->dst;
  131. AVFilterLink *outlink = ctx->outputs[0];
  132. ThreadData td;
  133. AVFrame *out;
  134. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  135. if (!out) {
  136. av_frame_free(&in);
  137. return AVERROR(ENOMEM);
  138. }
  139. av_frame_copy_props(out, in);
  140. /* copy palette if required */
  141. if (av_pix_fmt_desc_get(inlink->format)->flags & AV_PIX_FMT_FLAG_PAL)
  142. memcpy(out->data[1], in->data[1], AVPALETTE_SIZE);
  143. td.in = in, td.out = out;
  144. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ctx->graph->nb_threads));
  145. av_frame_free(&in);
  146. return ff_filter_frame(outlink, out);
  147. }
  148. static const AVFilterPad avfilter_vf_hflip_inputs[] = {
  149. {
  150. .name = "default",
  151. .type = AVMEDIA_TYPE_VIDEO,
  152. .filter_frame = filter_frame,
  153. .config_props = config_props,
  154. },
  155. { NULL }
  156. };
  157. static const AVFilterPad avfilter_vf_hflip_outputs[] = {
  158. {
  159. .name = "default",
  160. .type = AVMEDIA_TYPE_VIDEO,
  161. },
  162. { NULL }
  163. };
  164. AVFilter ff_vf_hflip = {
  165. .name = "hflip",
  166. .description = NULL_IF_CONFIG_SMALL("Horizontally flip the input video."),
  167. .priv_size = sizeof(FlipContext),
  168. .query_formats = query_formats,
  169. .inputs = avfilter_vf_hflip_inputs,
  170. .outputs = avfilter_vf_hflip_outputs,
  171. .flags = AVFILTER_FLAG_SLICE_THREADS,
  172. };