vf_hflip.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "libavutil/opt.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "hflip.h"
  30. #include "internal.h"
  31. #include "video.h"
  32. #include "libavutil/pixdesc.h"
  33. #include "libavutil/internal.h"
  34. #include "libavutil/intreadwrite.h"
  35. #include "libavutil/imgutils.h"
  36. static const AVOption hflip_options[] = {
  37. { NULL }
  38. };
  39. AVFILTER_DEFINE_CLASS(hflip);
  40. static int query_formats(AVFilterContext *ctx)
  41. {
  42. AVFilterFormats *pix_fmts = NULL;
  43. int fmt, ret;
  44. for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
  45. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  46. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
  47. desc->flags & AV_PIX_FMT_FLAG_BITSTREAM ||
  48. (desc->log2_chroma_w != desc->log2_chroma_h &&
  49. desc->comp[0].plane == desc->comp[1].plane)) &&
  50. (ret = ff_add_format(&pix_fmts, fmt)) < 0)
  51. return ret;
  52. }
  53. return ff_set_common_formats(ctx, pix_fmts);
  54. }
  55. static void hflip_byte_c(const uint8_t *src, uint8_t *dst, int w)
  56. {
  57. int j;
  58. for (j = 0; j < w; j++)
  59. dst[j] = src[-j];
  60. }
  61. static void hflip_short_c(const uint8_t *ssrc, uint8_t *ddst, int w)
  62. {
  63. const uint16_t *src = (const uint16_t *)ssrc;
  64. uint16_t *dst = (uint16_t *)ddst;
  65. int j;
  66. for (j = 0; j < w; j++)
  67. dst[j] = src[-j];
  68. }
  69. static void hflip_dword_c(const uint8_t *ssrc, uint8_t *ddst, int w)
  70. {
  71. const uint32_t *src = (const uint32_t *)ssrc;
  72. uint32_t *dst = (uint32_t *)ddst;
  73. int j;
  74. for (j = 0; j < w; j++)
  75. dst[j] = src[-j];
  76. }
  77. static void hflip_b24_c(const uint8_t *src, uint8_t *dst, int w)
  78. {
  79. const uint8_t *in = src;
  80. uint8_t *out = dst;
  81. int j;
  82. for (j = 0; j < w; j++, out += 3, in -= 3) {
  83. int32_t v = AV_RB24(in);
  84. AV_WB24(out, v);
  85. }
  86. }
  87. static void hflip_b48_c(const uint8_t *src, uint8_t *dst, int w)
  88. {
  89. const uint8_t *in = src;
  90. uint8_t *out = dst;
  91. int j;
  92. for (j = 0; j < w; j++, out += 6, in -= 6) {
  93. int64_t v = AV_RB48(in);
  94. AV_WB48(out, v);
  95. }
  96. }
  97. static void hflip_qword_c(const uint8_t *ssrc, uint8_t *ddst, int w)
  98. {
  99. const uint64_t *src = (const uint64_t *)ssrc;
  100. uint64_t *dst = (uint64_t *)ddst;
  101. int j;
  102. for (j = 0; j < w; j++)
  103. dst[j] = src[-j];
  104. }
  105. static int config_props(AVFilterLink *inlink)
  106. {
  107. FlipContext *s = inlink->dst->priv;
  108. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  109. const int hsub = pix_desc->log2_chroma_w;
  110. const int vsub = pix_desc->log2_chroma_h;
  111. int nb_planes;
  112. av_image_fill_max_pixsteps(s->max_step, NULL, pix_desc);
  113. s->planewidth[0] = s->planewidth[3] = inlink->w;
  114. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  115. s->planeheight[0] = s->planeheight[3] = inlink->h;
  116. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  117. nb_planes = av_pix_fmt_count_planes(inlink->format);
  118. return ff_hflip_init(s, s->max_step, nb_planes);
  119. }
  120. int ff_hflip_init(FlipContext *s, int step[4], int nb_planes)
  121. {
  122. int i;
  123. for (i = 0; i < nb_planes; i++) {
  124. switch (step[i]) {
  125. case 1: s->flip_line[i] = hflip_byte_c; break;
  126. case 2: s->flip_line[i] = hflip_short_c; break;
  127. case 3: s->flip_line[i] = hflip_b24_c; break;
  128. case 4: s->flip_line[i] = hflip_dword_c; break;
  129. case 6: s->flip_line[i] = hflip_b48_c; break;
  130. case 8: s->flip_line[i] = hflip_qword_c; break;
  131. default:
  132. return AVERROR_BUG;
  133. }
  134. }
  135. if (ARCH_X86)
  136. ff_hflip_init_x86(s, step, nb_planes);
  137. return 0;
  138. }
  139. typedef struct ThreadData {
  140. AVFrame *in, *out;
  141. } ThreadData;
  142. static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
  143. {
  144. FlipContext *s = ctx->priv;
  145. ThreadData *td = arg;
  146. AVFrame *in = td->in;
  147. AVFrame *out = td->out;
  148. uint8_t *inrow, *outrow;
  149. int i, plane, step;
  150. for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
  151. const int width = s->planewidth[plane];
  152. const int height = s->planeheight[plane];
  153. const int start = (height * job ) / nb_jobs;
  154. const int end = (height * (job+1)) / nb_jobs;
  155. step = s->max_step[plane];
  156. outrow = out->data[plane] + start * out->linesize[plane];
  157. inrow = in ->data[plane] + start * in->linesize[plane] + (width - 1) * step;
  158. for (i = start; i < end; i++) {
  159. s->flip_line[plane](inrow, outrow, width);
  160. inrow += in ->linesize[plane];
  161. outrow += out->linesize[plane];
  162. }
  163. }
  164. return 0;
  165. }
  166. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  167. {
  168. AVFilterContext *ctx = inlink->dst;
  169. AVFilterLink *outlink = ctx->outputs[0];
  170. ThreadData td;
  171. AVFrame *out;
  172. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  173. if (!out) {
  174. av_frame_free(&in);
  175. return AVERROR(ENOMEM);
  176. }
  177. av_frame_copy_props(out, in);
  178. /* copy palette if required */
  179. if (av_pix_fmt_desc_get(inlink->format)->flags & AV_PIX_FMT_FLAG_PAL)
  180. memcpy(out->data[1], in->data[1], AVPALETTE_SIZE);
  181. td.in = in, td.out = out;
  182. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
  183. av_frame_free(&in);
  184. return ff_filter_frame(outlink, out);
  185. }
  186. static const AVFilterPad avfilter_vf_hflip_inputs[] = {
  187. {
  188. .name = "default",
  189. .type = AVMEDIA_TYPE_VIDEO,
  190. .filter_frame = filter_frame,
  191. .config_props = config_props,
  192. },
  193. { NULL }
  194. };
  195. static const AVFilterPad avfilter_vf_hflip_outputs[] = {
  196. {
  197. .name = "default",
  198. .type = AVMEDIA_TYPE_VIDEO,
  199. },
  200. { NULL }
  201. };
  202. AVFilter ff_vf_hflip = {
  203. .name = "hflip",
  204. .description = NULL_IF_CONFIG_SMALL("Horizontally flip the input video."),
  205. .priv_size = sizeof(FlipContext),
  206. .priv_class = &hflip_class,
  207. .query_formats = query_formats,
  208. .inputs = avfilter_vf_hflip_inputs,
  209. .outputs = avfilter_vf_hflip_outputs,
  210. .flags = AVFILTER_FLAG_SLICE_THREADS | AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  211. };