vf_fps.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 "libavutil/common.h"
  27. #include "libavutil/fifo.h"
  28. #include "libavutil/mathematics.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/parseutils.h"
  31. #include "avfilter.h"
  32. #include "internal.h"
  33. #include "video.h"
  34. typedef struct FPSContext {
  35. const AVClass *class;
  36. AVFifoBuffer *fifo; ///< store frames until we get two successive timestamps
  37. /* timestamps in input timebase */
  38. int64_t first_pts; ///< pts of the first frame that arrived on this filter
  39. int64_t pts; ///< pts of the first frame currently in the fifo
  40. AVRational framerate; ///< target framerate
  41. char *fps; ///< a string describing target framerate
  42. /* statistics */
  43. int frames_in; ///< number of frames on input
  44. int frames_out; ///< number of frames on output
  45. int dup; ///< number of frames duplicated
  46. int drop; ///< number of framed dropped
  47. } FPSContext;
  48. #define OFFSET(x) offsetof(FPSContext, x)
  49. #define V AV_OPT_FLAG_VIDEO_PARAM
  50. #define F AV_OPT_FLAG_FILTERING_PARAM
  51. static const AVOption fps_options[] = {
  52. { "fps", "A string describing desired output framerate", OFFSET(fps), AV_OPT_TYPE_STRING, { .str = "25" }, .flags = V|F },
  53. { NULL },
  54. };
  55. AVFILTER_DEFINE_CLASS(fps);
  56. static av_cold int init(AVFilterContext *ctx, const char *args)
  57. {
  58. FPSContext *s = ctx->priv;
  59. int ret;
  60. s->class = &fps_class;
  61. av_opt_set_defaults(s);
  62. if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
  63. av_log(ctx, AV_LOG_ERROR, "Error parsing the options string %s.\n",
  64. args);
  65. return ret;
  66. }
  67. if ((ret = av_parse_video_rate(&s->framerate, s->fps)) < 0) {
  68. av_log(ctx, AV_LOG_ERROR, "Error parsing framerate %s.\n", s->fps);
  69. return ret;
  70. }
  71. av_opt_free(s);
  72. if (!(s->fifo = av_fifo_alloc(2*sizeof(AVFilterBufferRef*))))
  73. return AVERROR(ENOMEM);
  74. av_log(ctx, AV_LOG_VERBOSE, "fps=%d/%d\n", s->framerate.num, s->framerate.den);
  75. return 0;
  76. }
  77. static void flush_fifo(AVFifoBuffer *fifo)
  78. {
  79. while (av_fifo_size(fifo)) {
  80. AVFilterBufferRef *tmp;
  81. av_fifo_generic_read(fifo, &tmp, sizeof(tmp), NULL);
  82. avfilter_unref_buffer(tmp);
  83. }
  84. }
  85. static av_cold void uninit(AVFilterContext *ctx)
  86. {
  87. FPSContext *s = ctx->priv;
  88. if (s->fifo) {
  89. flush_fifo(s->fifo);
  90. av_fifo_free(s->fifo);
  91. }
  92. av_log(ctx, AV_LOG_VERBOSE, "%d frames in, %d frames out; %d frames dropped, "
  93. "%d frames duplicated.\n", s->frames_in, s->frames_out, s->drop, s->dup);
  94. }
  95. static int config_props(AVFilterLink* link)
  96. {
  97. FPSContext *s = link->src->priv;
  98. link->time_base = av_inv_q(s->framerate);
  99. link->frame_rate= s->framerate;
  100. link->w = link->src->inputs[0]->w;
  101. link->h = link->src->inputs[0]->h;
  102. s->pts = AV_NOPTS_VALUE;
  103. return 0;
  104. }
  105. static int request_frame(AVFilterLink *outlink)
  106. {
  107. AVFilterContext *ctx = outlink->src;
  108. FPSContext *s = ctx->priv;
  109. int frames_out = s->frames_out;
  110. int ret = 0;
  111. while (ret >= 0 && s->frames_out == frames_out)
  112. ret = ff_request_frame(ctx->inputs[0]);
  113. /* flush the fifo */
  114. if (ret == AVERROR_EOF && av_fifo_size(s->fifo)) {
  115. int i;
  116. for (i = 0; av_fifo_size(s->fifo); i++) {
  117. AVFilterBufferRef *buf;
  118. av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
  119. buf->pts = av_rescale_q(s->first_pts, ctx->inputs[0]->time_base,
  120. outlink->time_base) + s->frames_out;
  121. if ((ret = ff_start_frame(outlink, buf)) < 0 ||
  122. (ret = ff_draw_slice(outlink, 0, outlink->h, 1)) < 0 ||
  123. (ret = ff_end_frame(outlink)) < 0)
  124. return ret;
  125. s->frames_out++;
  126. }
  127. return 0;
  128. }
  129. return ret;
  130. }
  131. static int write_to_fifo(AVFifoBuffer *fifo, AVFilterBufferRef *buf)
  132. {
  133. int ret;
  134. if (!av_fifo_space(fifo) &&
  135. (ret = av_fifo_realloc2(fifo, 2*av_fifo_size(fifo)))) {
  136. avfilter_unref_bufferp(&buf);
  137. return ret;
  138. }
  139. av_fifo_generic_write(fifo, &buf, sizeof(buf), NULL);
  140. return 0;
  141. }
  142. static int end_frame(AVFilterLink *inlink)
  143. {
  144. AVFilterContext *ctx = inlink->dst;
  145. FPSContext *s = ctx->priv;
  146. AVFilterLink *outlink = ctx->outputs[0];
  147. AVFilterBufferRef *buf = inlink->cur_buf;
  148. int64_t delta;
  149. int i, ret;
  150. inlink->cur_buf = NULL;
  151. s->frames_in++;
  152. /* discard frames until we get the first timestamp */
  153. if (s->pts == AV_NOPTS_VALUE) {
  154. if (buf->pts != AV_NOPTS_VALUE) {
  155. ret = write_to_fifo(s->fifo, buf);
  156. if (ret < 0)
  157. return ret;
  158. s->first_pts = s->pts = buf->pts;
  159. } else {
  160. av_log(ctx, AV_LOG_WARNING, "Discarding initial frame(s) with no "
  161. "timestamp.\n");
  162. avfilter_unref_buffer(buf);
  163. s->drop++;
  164. }
  165. return 0;
  166. }
  167. /* now wait for the next timestamp */
  168. if (buf->pts == AV_NOPTS_VALUE || av_fifo_size(s->fifo) <= 0) {
  169. return write_to_fifo(s->fifo, buf);
  170. }
  171. /* number of output frames */
  172. delta = av_rescale_q(buf->pts - s->pts, inlink->time_base,
  173. outlink->time_base);
  174. if (delta < 1) {
  175. /* drop the frame and everything buffered except the first */
  176. AVFilterBufferRef *tmp;
  177. int drop = av_fifo_size(s->fifo)/sizeof(AVFilterBufferRef*);
  178. av_log(ctx, AV_LOG_DEBUG, "Dropping %d frame(s).\n", drop);
  179. s->drop += drop;
  180. av_fifo_generic_read(s->fifo, &tmp, sizeof(tmp), NULL);
  181. flush_fifo(s->fifo);
  182. ret = write_to_fifo(s->fifo, tmp);
  183. avfilter_unref_buffer(buf);
  184. return ret;
  185. }
  186. /* can output >= 1 frames */
  187. for (i = 0; i < delta; i++) {
  188. AVFilterBufferRef *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. AVFilterBufferRef *dup = avfilter_ref_buffer(buf_out, ~0);
  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. avfilter_unref_bufferp(&buf_out);
  200. avfilter_unref_bufferp(&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_start_frame(outlink, buf_out)) < 0 ||
  208. (ret = ff_draw_slice(outlink, 0, outlink->h, 1)) < 0 ||
  209. (ret = ff_end_frame(outlink)) < 0) {
  210. avfilter_unref_bufferp(&buf);
  211. return ret;
  212. }
  213. s->frames_out++;
  214. }
  215. flush_fifo(s->fifo);
  216. ret = write_to_fifo(s->fifo, buf);
  217. s->pts = s->first_pts + av_rescale_q(s->frames_out, outlink->time_base, inlink->time_base);
  218. return ret;
  219. }
  220. static int null_start_frame(AVFilterLink *link, AVFilterBufferRef *buf)
  221. {
  222. return 0;
  223. }
  224. static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  225. {
  226. return 0;
  227. }
  228. AVFilter avfilter_vf_fps = {
  229. .name = "fps",
  230. .description = NULL_IF_CONFIG_SMALL("Force constant framerate"),
  231. .init = init,
  232. .uninit = uninit,
  233. .priv_size = sizeof(FPSContext),
  234. .inputs = (const AVFilterPad[]) {{ .name = "default",
  235. .type = AVMEDIA_TYPE_VIDEO,
  236. .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
  237. .start_frame = null_start_frame,
  238. .draw_slice = null_draw_slice,
  239. .end_frame = end_frame, },
  240. { .name = NULL}},
  241. .outputs = (const AVFilterPad[]) {{ .name = "default",
  242. .type = AVMEDIA_TYPE_VIDEO,
  243. .rej_perms = AV_PERM_WRITE,
  244. .request_frame = request_frame,
  245. .config_props = config_props},
  246. { .name = NULL}},
  247. .priv_class = &fps_class,
  248. };