vf_fps.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright 2007 Bobby Bingham
  3. * Copyright 2012 Robert Nagy <ronag89 gmail com>
  4. * Copyright 2012 Anton Khirnov <anton khirnov net>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * a filter enforcing given constant framerate
  25. */
  26. #include <float.h>
  27. #include <stdint.h>
  28. #include "libavutil/common.h"
  29. #include "libavutil/fifo.h"
  30. #include "libavutil/mathematics.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/parseutils.h"
  33. #include "avfilter.h"
  34. #include "internal.h"
  35. #include "video.h"
  36. typedef struct FPSContext {
  37. const AVClass *class;
  38. AVFifoBuffer *fifo; ///< store frames until we get two successive timestamps
  39. /* timestamps in input timebase */
  40. int64_t first_pts; ///< pts of the first frame that arrived on this filter
  41. double start_time; ///< pts, in seconds, of the expected first frame
  42. AVRational framerate; ///< target framerate
  43. int rounding; ///< AVRounding method for timestamps
  44. /* statistics */
  45. int frames_in; ///< number of frames on input
  46. int frames_out; ///< number of frames on output
  47. int dup; ///< number of frames duplicated
  48. int drop; ///< number of framed dropped
  49. } FPSContext;
  50. #define OFFSET(x) offsetof(FPSContext, x)
  51. #define V AV_OPT_FLAG_VIDEO_PARAM
  52. #define F AV_OPT_FLAG_FILTERING_PARAM
  53. static const AVOption fps_options[] = {
  54. { "fps", "A string describing desired output framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, .flags = V|F },
  55. { "start_time", "Assume the first PTS should be this value.", OFFSET(start_time), AV_OPT_TYPE_DOUBLE, { .dbl = DBL_MAX}, -DBL_MAX, DBL_MAX, V },
  56. { "round", "set rounding method for timestamps", OFFSET(rounding), AV_OPT_TYPE_INT, { .i64 = AV_ROUND_NEAR_INF }, 0, 5, V|F, "round" },
  57. { "zero", "round towards 0", OFFSET(rounding), AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_ZERO }, 0, 5, V|F, "round" },
  58. { "inf", "round away from 0", OFFSET(rounding), AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_INF }, 0, 5, V|F, "round" },
  59. { "down", "round towards -infty", OFFSET(rounding), AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_DOWN }, 0, 5, V|F, "round" },
  60. { "up", "round towards +infty", OFFSET(rounding), AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_UP }, 0, 5, V|F, "round" },
  61. { "near", "round to nearest", OFFSET(rounding), AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_NEAR_INF }, 0, 5, V|F, "round" },
  62. { NULL }
  63. };
  64. AVFILTER_DEFINE_CLASS(fps);
  65. static av_cold int init(AVFilterContext *ctx)
  66. {
  67. FPSContext *s = ctx->priv;
  68. if (!(s->fifo = av_fifo_alloc_array(2, sizeof(AVFrame*))))
  69. return AVERROR(ENOMEM);
  70. s->first_pts = AV_NOPTS_VALUE;
  71. av_log(ctx, AV_LOG_VERBOSE, "fps=%d/%d\n", s->framerate.num, s->framerate.den);
  72. return 0;
  73. }
  74. static void flush_fifo(AVFifoBuffer *fifo)
  75. {
  76. while (av_fifo_size(fifo)) {
  77. AVFrame *tmp;
  78. av_fifo_generic_read(fifo, &tmp, sizeof(tmp), NULL);
  79. av_frame_free(&tmp);
  80. }
  81. }
  82. static av_cold void uninit(AVFilterContext *ctx)
  83. {
  84. FPSContext *s = ctx->priv;
  85. if (s->fifo) {
  86. s->drop += av_fifo_size(s->fifo) / sizeof(AVFrame*);
  87. flush_fifo(s->fifo);
  88. av_fifo_freep(&s->fifo);
  89. }
  90. av_log(ctx, AV_LOG_VERBOSE, "%d frames in, %d frames out; %d frames dropped, "
  91. "%d frames duplicated.\n", s->frames_in, s->frames_out, s->drop, s->dup);
  92. }
  93. static int config_props(AVFilterLink* link)
  94. {
  95. FPSContext *s = link->src->priv;
  96. link->time_base = av_inv_q(s->framerate);
  97. link->frame_rate= s->framerate;
  98. link->w = link->src->inputs[0]->w;
  99. link->h = link->src->inputs[0]->h;
  100. return 0;
  101. }
  102. static int request_frame(AVFilterLink *outlink)
  103. {
  104. AVFilterContext *ctx = outlink->src;
  105. FPSContext *s = ctx->priv;
  106. int frames_out = s->frames_out;
  107. int ret = 0;
  108. while (ret >= 0 && s->frames_out == frames_out)
  109. ret = ff_request_frame(ctx->inputs[0]);
  110. /* flush the fifo */
  111. if (ret == AVERROR_EOF && av_fifo_size(s->fifo)) {
  112. int i;
  113. for (i = 0; av_fifo_size(s->fifo); i++) {
  114. AVFrame *buf;
  115. av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
  116. buf->pts = av_rescale_q(s->first_pts, ctx->inputs[0]->time_base,
  117. outlink->time_base) + s->frames_out;
  118. if ((ret = ff_filter_frame(outlink, buf)) < 0)
  119. return ret;
  120. s->frames_out++;
  121. }
  122. return 0;
  123. }
  124. return ret;
  125. }
  126. static int write_to_fifo(AVFifoBuffer *fifo, AVFrame *buf)
  127. {
  128. int ret;
  129. if (!av_fifo_space(fifo) &&
  130. (ret = av_fifo_realloc2(fifo, 2*av_fifo_size(fifo)))) {
  131. av_frame_free(&buf);
  132. return ret;
  133. }
  134. av_fifo_generic_write(fifo, &buf, sizeof(buf), NULL);
  135. return 0;
  136. }
  137. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  138. {
  139. AVFilterContext *ctx = inlink->dst;
  140. FPSContext *s = ctx->priv;
  141. AVFilterLink *outlink = ctx->outputs[0];
  142. int64_t delta;
  143. int i, ret;
  144. s->frames_in++;
  145. /* discard frames until we get the first timestamp */
  146. if (s->first_pts == AV_NOPTS_VALUE) {
  147. if (buf->pts != AV_NOPTS_VALUE) {
  148. ret = write_to_fifo(s->fifo, buf);
  149. if (ret < 0)
  150. return ret;
  151. if (s->start_time != DBL_MAX && s->start_time != AV_NOPTS_VALUE) {
  152. double first_pts = s->start_time * AV_TIME_BASE;
  153. first_pts = FFMIN(FFMAX(first_pts, INT64_MIN), INT64_MAX);
  154. s->first_pts = av_rescale_q(first_pts, AV_TIME_BASE_Q,
  155. inlink->time_base);
  156. av_log(ctx, AV_LOG_VERBOSE, "Set first pts to (in:%"PRId64" out:%"PRId64")\n",
  157. s->first_pts, av_rescale_q(first_pts, AV_TIME_BASE_Q,
  158. outlink->time_base));
  159. } else {
  160. s->first_pts = buf->pts;
  161. }
  162. } else {
  163. av_log(ctx, AV_LOG_WARNING, "Discarding initial frame(s) with no "
  164. "timestamp.\n");
  165. av_frame_free(&buf);
  166. s->drop++;
  167. }
  168. return 0;
  169. }
  170. /* now wait for the next timestamp */
  171. if (buf->pts == AV_NOPTS_VALUE || av_fifo_size(s->fifo) <= 0) {
  172. return write_to_fifo(s->fifo, buf);
  173. }
  174. /* number of output frames */
  175. delta = av_rescale_q_rnd(buf->pts - s->first_pts, inlink->time_base,
  176. outlink->time_base, s->rounding) - s->frames_out ;
  177. if (delta < 1) {
  178. /* drop everything buffered except the last */
  179. int drop = av_fifo_size(s->fifo)/sizeof(AVFrame*);
  180. av_log(ctx, AV_LOG_DEBUG, "Dropping %d frame(s).\n", drop);
  181. s->drop += drop;
  182. flush_fifo(s->fifo);
  183. ret = write_to_fifo(s->fifo, buf);
  184. return ret;
  185. }
  186. /* can output >= 1 frames */
  187. for (i = 0; i < delta; i++) {
  188. AVFrame *buf_out;
  189. av_fifo_generic_read(s->fifo, &buf_out, sizeof(buf_out), NULL);
  190. /* duplicate the frame if needed */
  191. if (!av_fifo_size(s->fifo) && i < delta - 1) {
  192. AVFrame *dup = av_frame_clone(buf_out);
  193. av_log(ctx, AV_LOG_DEBUG, "Duplicating frame.\n");
  194. if (dup)
  195. ret = write_to_fifo(s->fifo, dup);
  196. else
  197. ret = AVERROR(ENOMEM);
  198. if (ret < 0) {
  199. av_frame_free(&buf_out);
  200. av_frame_free(&buf);
  201. return ret;
  202. }
  203. s->dup++;
  204. }
  205. buf_out->pts = av_rescale_q(s->first_pts, inlink->time_base,
  206. outlink->time_base) + s->frames_out;
  207. if ((ret = ff_filter_frame(outlink, buf_out)) < 0) {
  208. av_frame_free(&buf);
  209. return ret;
  210. }
  211. s->frames_out++;
  212. }
  213. flush_fifo(s->fifo);
  214. ret = write_to_fifo(s->fifo, buf);
  215. return ret;
  216. }
  217. static const AVFilterPad avfilter_vf_fps_inputs[] = {
  218. {
  219. .name = "default",
  220. .type = AVMEDIA_TYPE_VIDEO,
  221. .filter_frame = filter_frame,
  222. },
  223. { NULL }
  224. };
  225. static const AVFilterPad avfilter_vf_fps_outputs[] = {
  226. {
  227. .name = "default",
  228. .type = AVMEDIA_TYPE_VIDEO,
  229. .request_frame = request_frame,
  230. .config_props = config_props
  231. },
  232. { NULL }
  233. };
  234. AVFilter ff_vf_fps = {
  235. .name = "fps",
  236. .description = NULL_IF_CONFIG_SMALL("Force constant framerate."),
  237. .init = init,
  238. .uninit = uninit,
  239. .priv_size = sizeof(FPSContext),
  240. .priv_class = &fps_class,
  241. .inputs = avfilter_vf_fps_inputs,
  242. .outputs = avfilter_vf_fps_outputs,
  243. };