vf_swaprect.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) 2015 Paul B. Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/avstring.h"
  21. #include "libavutil/eval.h"
  22. #include "libavutil/imgutils.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "formats.h"
  27. #include "internal.h"
  28. #include "video.h"
  29. typedef struct SwapRectContext {
  30. const AVClass *class;
  31. char *w, *h;
  32. char *x1, *y1;
  33. char *x2, *y2;
  34. int nb_planes;
  35. int pixsteps[4];
  36. const AVPixFmtDescriptor *desc;
  37. uint8_t *temp;
  38. } SwapRectContext;
  39. #define OFFSET(x) offsetof(SwapRectContext, x)
  40. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  41. static const AVOption swaprect_options[] = {
  42. { "w", "set rect width", OFFSET(w), AV_OPT_TYPE_STRING, {.str="w/2"}, 0, 0, .flags = FLAGS },
  43. { "h", "set rect height", OFFSET(h), AV_OPT_TYPE_STRING, {.str="h/2"}, 0, 0, .flags = FLAGS },
  44. { "x1", "set 1st rect x top left coordinate", OFFSET(x1), AV_OPT_TYPE_STRING, {.str="w/2"}, 0, 0, .flags = FLAGS },
  45. { "y1", "set 1st rect y top left coordinate", OFFSET(y1), AV_OPT_TYPE_STRING, {.str="h/2"}, 0, 0, .flags = FLAGS },
  46. { "x2", "set 2nd rect x top left coordinate", OFFSET(x2), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, .flags = FLAGS },
  47. { "y2", "set 2nd rect y top left coordinate", OFFSET(y2), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, .flags = FLAGS },
  48. { NULL },
  49. };
  50. AVFILTER_DEFINE_CLASS(swaprect);
  51. static int query_formats(AVFilterContext *ctx)
  52. {
  53. AVFilterFormats *pix_fmts = NULL;
  54. int fmt, ret;
  55. for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
  56. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  57. if (!(desc->flags & AV_PIX_FMT_FLAG_PAL ||
  58. desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
  59. desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) &&
  60. (ret = ff_add_format(&pix_fmts, fmt)) < 0)
  61. return ret;
  62. }
  63. return ff_set_common_formats(ctx, pix_fmts);
  64. }
  65. static const char *const var_names[] = { "w", "h", "a", "n", "t", "pos", "sar", "dar", NULL };
  66. enum { VAR_W, VAR_H, VAR_A, VAR_N, VAR_T, VAR_POS, VAR_SAR, VAR_DAR, VAR_VARS_NB };
  67. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  68. {
  69. AVFilterContext *ctx = inlink->dst;
  70. AVFilterLink *outlink = ctx->outputs[0];
  71. SwapRectContext *s = ctx->priv;
  72. double var_values[VAR_VARS_NB];
  73. int x1[4], y1[4];
  74. int x2[4], y2[4];
  75. int aw[4], ah[4];
  76. int lw[4], lh[4];
  77. int pw[4], ph[4];
  78. double dw, dh;
  79. double dx1, dy1;
  80. double dx2, dy2;
  81. int y, p, w, h, ret;
  82. var_values[VAR_W] = inlink->w;
  83. var_values[VAR_H] = inlink->h;
  84. var_values[VAR_A] = (float) inlink->w / inlink->h;
  85. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ? av_q2d(inlink->sample_aspect_ratio) : 1;
  86. var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
  87. var_values[VAR_N] = inlink->frame_count_out;
  88. var_values[VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base);
  89. var_values[VAR_POS] = in->pkt_pos ? NAN : in->pkt_pos;
  90. ret = av_expr_parse_and_eval(&dw, s->w,
  91. var_names, &var_values[0],
  92. NULL, NULL, NULL, NULL,
  93. 0, 0, ctx);
  94. if (ret < 0)
  95. return ret;
  96. ret = av_expr_parse_and_eval(&dh, s->h,
  97. var_names, &var_values[0],
  98. NULL, NULL, NULL, NULL,
  99. 0, 0, ctx);
  100. if (ret < 0)
  101. return ret;
  102. ret = av_expr_parse_and_eval(&dx1, s->x1,
  103. var_names, &var_values[0],
  104. NULL, NULL, NULL, NULL,
  105. 0, 0, ctx);
  106. if (ret < 0)
  107. return ret;
  108. ret = av_expr_parse_and_eval(&dy1, s->y1,
  109. var_names, &var_values[0],
  110. NULL, NULL, NULL, NULL,
  111. 0, 0, ctx);
  112. if (ret < 0)
  113. return ret;
  114. ret = av_expr_parse_and_eval(&dx2, s->x2,
  115. var_names, &var_values[0],
  116. NULL, NULL, NULL, NULL,
  117. 0, 0, ctx);
  118. if (ret < 0)
  119. return ret;
  120. ret = av_expr_parse_and_eval(&dy2, s->y2,
  121. var_names, &var_values[0],
  122. NULL, NULL, NULL, NULL,
  123. 0, 0, ctx);
  124. if (ret < 0)
  125. return ret;
  126. w = dw; h = dh; x1[0] = dx1; y1[0] = dy1; x2[0] = dx2; y2[0] = dy2;
  127. x1[0] = av_clip(x1[0], 0, inlink->w - 1);
  128. y1[0] = av_clip(y1[0], 0, inlink->w - 1);
  129. x2[0] = av_clip(x2[0], 0, inlink->w - 1);
  130. y2[0] = av_clip(y2[0], 0, inlink->w - 1);
  131. ah[1] = ah[2] = AV_CEIL_RSHIFT(h, s->desc->log2_chroma_h);
  132. ah[0] = ah[3] = h;
  133. aw[1] = aw[2] = AV_CEIL_RSHIFT(w, s->desc->log2_chroma_w);
  134. aw[0] = aw[3] = w;
  135. w = FFMIN3(w, inlink->w - x1[0], inlink->w - x2[0]);
  136. h = FFMIN3(h, inlink->h - y1[0], inlink->h - y2[0]);
  137. ph[1] = ph[2] = AV_CEIL_RSHIFT(h, s->desc->log2_chroma_h);
  138. ph[0] = ph[3] = h;
  139. pw[1] = pw[2] = AV_CEIL_RSHIFT(w, s->desc->log2_chroma_w);
  140. pw[0] = pw[3] = w;
  141. lh[1] = lh[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
  142. lh[0] = lh[3] = inlink->h;
  143. lw[1] = lw[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
  144. lw[0] = lw[3] = inlink->w;
  145. x1[1] = x1[2] = AV_CEIL_RSHIFT(x1[0], s->desc->log2_chroma_w);
  146. x1[0] = x1[3] = x1[0];
  147. y1[1] = y1[2] = AV_CEIL_RSHIFT(y1[0], s->desc->log2_chroma_h);
  148. y1[0] = y1[3] = y1[0];
  149. x2[1] = x2[2] = AV_CEIL_RSHIFT(x2[0], s->desc->log2_chroma_w);
  150. x2[0] = x2[3] = x2[0];
  151. y2[1] = y2[2] = AV_CEIL_RSHIFT(y2[0], s->desc->log2_chroma_h);
  152. y2[0] = y2[3] = y2[0];
  153. for (p = 0; p < s->nb_planes; p++) {
  154. if (ph[p] == ah[p] && pw[p] == aw[p]) {
  155. uint8_t *src = in->data[p] + y1[p] * in->linesize[p] + x1[p] * s->pixsteps[p];
  156. uint8_t *dst = in->data[p] + y2[p] * in->linesize[p] + x2[p] * s->pixsteps[p];
  157. for (y = 0; y < ph[p]; y++) {
  158. memcpy(s->temp, src, pw[p] * s->pixsteps[p]);
  159. memmove(src, dst, pw[p] * s->pixsteps[p]);
  160. memcpy(dst, s->temp, pw[p] * s->pixsteps[p]);
  161. src += in->linesize[p];
  162. dst += in->linesize[p];
  163. }
  164. }
  165. }
  166. return ff_filter_frame(outlink, in);
  167. }
  168. static int config_input(AVFilterLink *inlink)
  169. {
  170. AVFilterContext *ctx = inlink->dst;
  171. SwapRectContext *s = ctx->priv;
  172. if (!s->w || !s->h ||
  173. !s->x1 || !s->y1 ||
  174. !s->x2 || !s->y2)
  175. return AVERROR(EINVAL);
  176. s->desc = av_pix_fmt_desc_get(inlink->format);
  177. av_image_fill_max_pixsteps(s->pixsteps, NULL, s->desc);
  178. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  179. s->temp = av_malloc_array(inlink->w, s->pixsteps[0]);
  180. if (!s->temp)
  181. return AVERROR(ENOMEM);
  182. return 0;
  183. }
  184. static av_cold void uninit(AVFilterContext *ctx)
  185. {
  186. SwapRectContext *s = ctx->priv;
  187. av_freep(&s->temp);
  188. }
  189. static const AVFilterPad inputs[] = {
  190. {
  191. .name = "default",
  192. .type = AVMEDIA_TYPE_VIDEO,
  193. .filter_frame = filter_frame,
  194. .config_props = config_input,
  195. .needs_writable = 1,
  196. },
  197. { NULL }
  198. };
  199. static const AVFilterPad outputs[] = {
  200. {
  201. .name = "default",
  202. .type = AVMEDIA_TYPE_VIDEO,
  203. },
  204. { NULL }
  205. };
  206. AVFilter ff_vf_swaprect = {
  207. .name = "swaprect",
  208. .description = NULL_IF_CONFIG_SMALL("Swap 2 rectangular objects in video."),
  209. .priv_size = sizeof(SwapRectContext),
  210. .priv_class = &swaprect_class,
  211. .query_formats = query_formats,
  212. .uninit = uninit,
  213. .inputs = inputs,
  214. .outputs = outputs,
  215. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  216. };