vf_zoompan.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (c) 2013 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/eval.h"
  21. #include "libavutil/opt.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "avfilter.h"
  24. #include "formats.h"
  25. #include "internal.h"
  26. #include "video.h"
  27. #include "libswscale/swscale.h"
  28. static const char *const var_names[] = {
  29. "in_w", "iw",
  30. "in_h", "ih",
  31. "out_w", "ow",
  32. "out_h", "oh",
  33. "in",
  34. "on",
  35. "duration",
  36. "pduration",
  37. "time",
  38. "frame",
  39. "zoom",
  40. "pzoom",
  41. "x", "px",
  42. "y", "py",
  43. "a",
  44. "sar",
  45. "dar",
  46. "hsub",
  47. "vsub",
  48. NULL
  49. };
  50. enum var_name {
  51. VAR_IN_W, VAR_IW,
  52. VAR_IN_H, VAR_IH,
  53. VAR_OUT_W, VAR_OW,
  54. VAR_OUT_H, VAR_OH,
  55. VAR_IN,
  56. VAR_ON,
  57. VAR_DURATION,
  58. VAR_PDURATION,
  59. VAR_TIME,
  60. VAR_FRAME,
  61. VAR_ZOOM,
  62. VAR_PZOOM,
  63. VAR_X, VAR_PX,
  64. VAR_Y, VAR_PY,
  65. VAR_A,
  66. VAR_SAR,
  67. VAR_DAR,
  68. VAR_HSUB,
  69. VAR_VSUB,
  70. VARS_NB
  71. };
  72. typedef struct ZPcontext {
  73. const AVClass *class;
  74. char *zoom_expr_str;
  75. char *x_expr_str;
  76. char *y_expr_str;
  77. char *duration_expr_str;
  78. int w, h;
  79. double x, y;
  80. double prev_zoom;
  81. int prev_nb_frames;
  82. struct SwsContext *sws;
  83. int64_t frame_count;
  84. } ZPContext;
  85. #define OFFSET(x) offsetof(ZPContext, x)
  86. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  87. static const AVOption zoompan_options[] = {
  88. { "zoom", "set the zoom expression", OFFSET(zoom_expr_str), AV_OPT_TYPE_STRING, {.str = "1" }, .flags = FLAGS },
  89. { "z", "set the zoom expression", OFFSET(zoom_expr_str), AV_OPT_TYPE_STRING, {.str = "1" }, .flags = FLAGS },
  90. { "x", "set the x expression", OFFSET(x_expr_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags = FLAGS },
  91. { "y", "set the y expression", OFFSET(y_expr_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags = FLAGS },
  92. { "d", "set the duration expression", OFFSET(duration_expr_str), AV_OPT_TYPE_STRING, {.str="90"}, .flags = FLAGS },
  93. { "s", "set the output image size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, .flags = FLAGS },
  94. { NULL }
  95. };
  96. AVFILTER_DEFINE_CLASS(zoompan);
  97. static av_cold int init(AVFilterContext *ctx)
  98. {
  99. ZPContext *s = ctx->priv;
  100. s->prev_zoom = 1;
  101. return 0;
  102. }
  103. static int config_output(AVFilterLink *outlink)
  104. {
  105. AVFilterContext *ctx = outlink->src;
  106. ZPContext *s = ctx->priv;
  107. outlink->w = s->w;
  108. outlink->h = s->h;
  109. return 0;
  110. }
  111. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  112. {
  113. AVFilterContext *ctx = inlink->dst;
  114. AVFilterLink *outlink = ctx->outputs[0];
  115. ZPContext *s = ctx->priv;
  116. double var_values[VARS_NB], nb_frames, zoom, dx, dy;
  117. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(in->format);
  118. AVFrame *out;
  119. int i, k, x, y, w, h, ret = 0;
  120. var_values[VAR_IN_W] = var_values[VAR_IW] = in->width;
  121. var_values[VAR_IN_H] = var_values[VAR_IH] = in->height;
  122. var_values[VAR_OUT_W] = var_values[VAR_OW] = s->w;
  123. var_values[VAR_OUT_H] = var_values[VAR_OH] = s->h;
  124. var_values[VAR_IN] = inlink->frame_count + 1;
  125. var_values[VAR_ON] = outlink->frame_count + 1;
  126. var_values[VAR_PX] = s->x;
  127. var_values[VAR_PY] = s->y;
  128. var_values[VAR_X] = 0;
  129. var_values[VAR_Y] = 0;
  130. var_values[VAR_PZOOM] = s->prev_zoom;
  131. var_values[VAR_ZOOM] = 1;
  132. var_values[VAR_PDURATION] = s->prev_nb_frames;
  133. var_values[VAR_A] = (double) in->width / in->height;
  134. var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
  135. (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
  136. var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
  137. var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
  138. var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
  139. if ((ret = av_expr_parse_and_eval(&nb_frames, s->duration_expr_str,
  140. var_names, var_values,
  141. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  142. goto fail;
  143. var_values[VAR_DURATION] = nb_frames;
  144. for (i = 0; i < nb_frames; i++) {
  145. int px[4];
  146. int py[4];
  147. uint8_t *input[4];
  148. int64_t pts = av_rescale_q(in->pts, inlink->time_base,
  149. outlink->time_base) + s->frame_count;
  150. var_values[VAR_TIME] = pts * av_q2d(outlink->time_base);
  151. var_values[VAR_FRAME] = i;
  152. var_values[VAR_ON] = outlink->frame_count + 1;
  153. if ((ret = av_expr_parse_and_eval(&zoom, s->zoom_expr_str,
  154. var_names, var_values,
  155. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  156. goto fail;
  157. zoom = av_clipd(zoom, 1, 10);
  158. var_values[VAR_ZOOM] = zoom;
  159. w = in->width * (1.0 / zoom);
  160. h = in->height * (1.0 / zoom);
  161. if ((ret = av_expr_parse_and_eval(&dx, s->x_expr_str,
  162. var_names, var_values,
  163. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  164. goto fail;
  165. x = dx = av_clipd(dx, 0, FFMAX(in->width - w, 0));
  166. var_values[VAR_X] = dx;
  167. x &= ~((1 << desc->log2_chroma_w) - 1);
  168. if ((ret = av_expr_parse_and_eval(&dy, s->y_expr_str,
  169. var_names, var_values,
  170. NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
  171. goto fail;
  172. y = dy = av_clipd(dy, 0, FFMAX(in->height - h, 0));
  173. var_values[VAR_Y] = dy;
  174. y &= ~((1 << desc->log2_chroma_h) - 1);
  175. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  176. if (!out) {
  177. ret = AVERROR(ENOMEM);
  178. goto fail;
  179. }
  180. px[1] = px[2] = FF_CEIL_RSHIFT(x, desc->log2_chroma_w);
  181. px[0] = px[3] = x;
  182. py[1] = py[2] = FF_CEIL_RSHIFT(y, desc->log2_chroma_h);
  183. py[0] = py[3] = y;
  184. s->sws = sws_alloc_context();
  185. if (!s->sws) {
  186. ret = AVERROR(ENOMEM);
  187. goto fail;
  188. }
  189. for (k = 0; in->data[k]; k++)
  190. input[k] = in->data[k] + py[k] * in->linesize[k] + px[k];
  191. av_opt_set_int(s->sws, "srcw", w, 0);
  192. av_opt_set_int(s->sws, "srch", h, 0);
  193. av_opt_set_int(s->sws, "src_format", in->format, 0);
  194. av_opt_set_int(s->sws, "dstw", outlink->w, 0);
  195. av_opt_set_int(s->sws, "dsth", outlink->h, 0);
  196. av_opt_set_int(s->sws, "dst_format", outlink->format, 0);
  197. av_opt_set_int(s->sws, "sws_flags", SWS_BICUBIC, 0);
  198. if ((ret = sws_init_context(s->sws, NULL, NULL)) < 0)
  199. goto fail;
  200. sws_scale(s->sws, (const uint8_t *const *)&input, in->linesize, 0, h, out->data, out->linesize);
  201. out->pts = pts;
  202. s->frame_count++;
  203. ret = ff_filter_frame(outlink, out);
  204. if (ret < 0)
  205. break;
  206. sws_freeContext(s->sws);
  207. s->sws = NULL;
  208. }
  209. s->x = dx;
  210. s->y = dy;
  211. s->prev_zoom = zoom;
  212. s->prev_nb_frames = nb_frames;
  213. fail:
  214. sws_freeContext(s->sws);
  215. s->sws = NULL;
  216. av_frame_free(&in);
  217. return ret;
  218. }
  219. static int query_formats(AVFilterContext *ctx)
  220. {
  221. static const enum AVPixelFormat pix_fmts[] = {
  222. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  223. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  224. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  225. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P,
  226. AV_PIX_FMT_YUVA420P,
  227. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  228. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  229. AV_PIX_FMT_YUVJ411P,
  230. AV_PIX_FMT_GRAY8,
  231. AV_PIX_FMT_NONE
  232. };
  233. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  234. return 0;
  235. }
  236. static av_cold void uninit(AVFilterContext *ctx)
  237. {
  238. ZPContext *s = ctx->priv;
  239. sws_freeContext(s->sws);
  240. s->sws = NULL;
  241. }
  242. static const AVFilterPad inputs[] = {
  243. {
  244. .name = "default",
  245. .type = AVMEDIA_TYPE_VIDEO,
  246. .filter_frame = filter_frame,
  247. },
  248. { NULL }
  249. };
  250. static const AVFilterPad outputs[] = {
  251. {
  252. .name = "default",
  253. .type = AVMEDIA_TYPE_VIDEO,
  254. .config_props = config_output,
  255. },
  256. { NULL }
  257. };
  258. AVFilter ff_vf_zoompan = {
  259. .name = "zoompan",
  260. .description = NULL_IF_CONFIG_SMALL("Apply Zoom & Pan effect."),
  261. .priv_size = sizeof(ZPContext),
  262. .priv_class = &zoompan_class,
  263. .init = init,
  264. .uninit = uninit,
  265. .query_formats = query_formats,
  266. .inputs = inputs,
  267. .outputs = outputs,
  268. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  269. };