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.

201 lines
6.0KB

  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;
  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. ff_add_format(&pix_fmts, fmt);
  50. }
  51. return ff_set_common_formats(ctx, pix_fmts);
  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. const int hsub = pix_desc->log2_chroma_w;
  58. const int vsub = pix_desc->log2_chroma_h;
  59. av_image_fill_max_pixsteps(s->max_step, NULL, pix_desc);
  60. s->planewidth[0] = s->planewidth[3] = inlink->w;
  61. s->planewidth[1] = s->planewidth[2] = FF_CEIL_RSHIFT(inlink->w, hsub);
  62. s->planeheight[0] = s->planeheight[3] = inlink->h;
  63. s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, vsub);
  64. return 0;
  65. }
  66. typedef struct ThreadData {
  67. AVFrame *in, *out;
  68. } ThreadData;
  69. static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
  70. {
  71. FlipContext *s = ctx->priv;
  72. ThreadData *td = arg;
  73. AVFrame *in = td->in;
  74. AVFrame *out = td->out;
  75. uint8_t *inrow, *outrow;
  76. int i, j, plane, step;
  77. for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
  78. const int width = s->planewidth[plane];
  79. const int height = s->planeheight[plane];
  80. const int start = (height * job ) / nb_jobs;
  81. const int end = (height * (job+1)) / nb_jobs;
  82. step = s->max_step[plane];
  83. outrow = out->data[plane] + start * out->linesize[plane];
  84. inrow = in ->data[plane] + start * in->linesize[plane] + (width - 1) * step;
  85. for (i = start; i < end; 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. return 0;
  126. }
  127. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  128. {
  129. AVFilterContext *ctx = inlink->dst;
  130. AVFilterLink *outlink = ctx->outputs[0];
  131. ThreadData td;
  132. AVFrame *out;
  133. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  134. if (!out) {
  135. av_frame_free(&in);
  136. return AVERROR(ENOMEM);
  137. }
  138. av_frame_copy_props(out, in);
  139. /* copy palette if required */
  140. if (av_pix_fmt_desc_get(inlink->format)->flags & AV_PIX_FMT_FLAG_PAL)
  141. memcpy(out->data[1], in->data[1], AVPALETTE_SIZE);
  142. td.in = in, td.out = out;
  143. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ctx->graph->nb_threads));
  144. av_frame_free(&in);
  145. return ff_filter_frame(outlink, out);
  146. }
  147. static const AVFilterPad avfilter_vf_hflip_inputs[] = {
  148. {
  149. .name = "default",
  150. .type = AVMEDIA_TYPE_VIDEO,
  151. .filter_frame = filter_frame,
  152. .config_props = config_props,
  153. },
  154. { NULL }
  155. };
  156. static const AVFilterPad avfilter_vf_hflip_outputs[] = {
  157. {
  158. .name = "default",
  159. .type = AVMEDIA_TYPE_VIDEO,
  160. },
  161. { NULL }
  162. };
  163. AVFilter ff_vf_hflip = {
  164. .name = "hflip",
  165. .description = NULL_IF_CONFIG_SMALL("Horizontally flip the input video."),
  166. .priv_size = sizeof(FlipContext),
  167. .query_formats = query_formats,
  168. .inputs = avfilter_vf_hflip_inputs,
  169. .outputs = avfilter_vf_hflip_outputs,
  170. .flags = AVFILTER_FLAG_SLICE_THREADS,
  171. };