vf_fps.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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/fifo.h"
  27. #include "libavutil/mathematics.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/parseutils.h"
  30. #include "avfilter.h"
  31. typedef struct FPSContext {
  32. const AVClass *class;
  33. AVFifoBuffer *fifo; ///< store frames until we get two successive timestamps
  34. /* timestamps in input timebase */
  35. int64_t first_pts; ///< pts of the first frame that arrived on this filter
  36. int64_t pts; ///< pts of the first frame currently in the fifo
  37. AVRational framerate; ///< target framerate
  38. char *fps; ///< a string describing target framerate
  39. /* statistics */
  40. int frames_in; ///< number of frames on input
  41. int frames_out; ///< number of frames on output
  42. int dup; ///< number of frames duplicated
  43. int drop; ///< number of framed dropped
  44. } FPSContext;
  45. #define OFFSET(x) offsetof(FPSContext, x)
  46. #define V AV_OPT_FLAG_VIDEO_PARAM
  47. static const AVOption options[] = {
  48. { "fps", "A string describing desired output framerate", OFFSET(fps), AV_OPT_TYPE_STRING, { .str = "25" }, .flags = V },
  49. { NULL },
  50. };
  51. static const AVClass class = {
  52. .class_name = "FPS filter",
  53. .item_name = av_default_item_name,
  54. .option = options,
  55. .version = LIBAVUTIL_VERSION_INT,
  56. };
  57. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  58. {
  59. FPSContext *s = ctx->priv;
  60. int ret;
  61. s->class = &class;
  62. av_opt_set_defaults(s);
  63. if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
  64. av_log(ctx, AV_LOG_ERROR, "Error parsing the options string %s.\n",
  65. args);
  66. return ret;
  67. }
  68. if ((ret = av_parse_video_rate(&s->framerate, s->fps)) < 0) {
  69. av_log(ctx, AV_LOG_ERROR, "Error parsing framerate %s.\n", s->fps);
  70. return ret;
  71. }
  72. av_opt_free(s);
  73. if (!(s->fifo = av_fifo_alloc(2*sizeof(AVFilterBufferRef*))))
  74. return AVERROR(ENOMEM);
  75. av_log(ctx, AV_LOG_VERBOSE, "fps=%d/%d\n", s->framerate.num, s->framerate.den);
  76. return 0;
  77. }
  78. static void flush_fifo(AVFifoBuffer *fifo)
  79. {
  80. while (av_fifo_size(fifo)) {
  81. AVFilterBufferRef *tmp;
  82. av_fifo_generic_read(fifo, &tmp, sizeof(tmp), NULL);
  83. avfilter_unref_buffer(tmp);
  84. }
  85. }
  86. static av_cold void uninit(AVFilterContext *ctx)
  87. {
  88. FPSContext *s = ctx->priv;
  89. if (s->fifo) {
  90. flush_fifo(s->fifo);
  91. av_fifo_free(s->fifo);
  92. }
  93. av_log(ctx, AV_LOG_VERBOSE, "%d frames in, %d frames out; %d frames dropped, "
  94. "%d frames duplicated.\n", s->frames_in, s->frames_out, s->drop, s->dup);
  95. }
  96. static int config_props(AVFilterLink* link)
  97. {
  98. FPSContext *s = link->src->priv;
  99. link->time_base = (AVRational){ s->framerate.den, s->framerate.num };
  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 = avfilter_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. avfilter_start_frame(outlink, buf);
  122. avfilter_draw_slice(outlink, 0, outlink->h, 1);
  123. avfilter_end_frame(outlink);
  124. s->frames_out++;
  125. }
  126. return 0;
  127. }
  128. return ret;
  129. }
  130. static int write_to_fifo(AVFifoBuffer *fifo, AVFilterBufferRef *buf)
  131. {
  132. int ret;
  133. if (!av_fifo_space(fifo) &&
  134. (ret = av_fifo_realloc2(fifo, 2*av_fifo_size(fifo))))
  135. return ret;
  136. av_fifo_generic_write(fifo, &buf, sizeof(buf), NULL);
  137. return 0;
  138. }
  139. static void end_frame(AVFilterLink *inlink)
  140. {
  141. AVFilterContext *ctx = inlink->dst;
  142. FPSContext *s = ctx->priv;
  143. AVFilterLink *outlink = ctx->outputs[0];
  144. AVFilterBufferRef *buf = inlink->cur_buf;
  145. int64_t delta;
  146. int i;
  147. s->frames_in++;
  148. /* discard frames until we get the first timestamp */
  149. if (s->pts == AV_NOPTS_VALUE) {
  150. if (buf->pts != AV_NOPTS_VALUE) {
  151. write_to_fifo(s->fifo, buf);
  152. s->first_pts = s->pts = buf->pts;
  153. } else {
  154. av_log(ctx, AV_LOG_WARNING, "Discarding initial frame(s) with no "
  155. "timestamp.\n");
  156. avfilter_unref_buffer(buf);
  157. s->drop++;
  158. }
  159. return;
  160. }
  161. /* now wait for the next timestamp */
  162. if (buf->pts == AV_NOPTS_VALUE || av_fifo_size(s->fifo) <= 0) {
  163. write_to_fifo(s->fifo, buf);
  164. return;
  165. }
  166. /* number of output frames */
  167. delta = av_rescale_q(buf->pts - s->pts, inlink->time_base,
  168. outlink->time_base);
  169. if (delta < 1) {
  170. /* drop the frame and everything buffered except the first */
  171. AVFilterBufferRef *tmp;
  172. int drop = av_fifo_size(s->fifo)/sizeof(AVFilterBufferRef*);
  173. av_log(ctx, AV_LOG_DEBUG, "Dropping %d frame(s).\n", drop);
  174. s->drop += drop;
  175. av_fifo_generic_read(s->fifo, &tmp, sizeof(tmp), NULL);
  176. flush_fifo(s->fifo);
  177. write_to_fifo(s->fifo, tmp);
  178. avfilter_unref_buffer(buf);
  179. return;
  180. }
  181. /* can output >= 1 frames */
  182. for (i = 0; i < delta; i++) {
  183. AVFilterBufferRef *buf_out;
  184. av_fifo_generic_read(s->fifo, &buf_out, sizeof(buf_out), NULL);
  185. /* duplicate the frame if needed */
  186. if (!av_fifo_size(s->fifo) && i < delta - 1) {
  187. av_log(ctx, AV_LOG_DEBUG, "Duplicating frame.\n");
  188. write_to_fifo(s->fifo, avfilter_ref_buffer(buf_out, AV_PERM_READ));
  189. s->dup++;
  190. }
  191. buf_out->pts = av_rescale_q(s->first_pts, inlink->time_base,
  192. outlink->time_base) + s->frames_out;
  193. avfilter_start_frame(outlink, buf_out);
  194. avfilter_draw_slice(outlink, 0, outlink->h, 1);
  195. avfilter_end_frame(outlink);
  196. s->frames_out++;
  197. }
  198. flush_fifo(s->fifo);
  199. write_to_fifo(s->fifo, buf);
  200. s->pts = s->first_pts + av_rescale_q(s->frames_out, outlink->time_base, inlink->time_base);
  201. }
  202. static void null_start_frame(AVFilterLink *link, AVFilterBufferRef *buf)
  203. {
  204. }
  205. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  206. {
  207. }
  208. AVFilter avfilter_vf_fps = {
  209. .name = "fps",
  210. .description = NULL_IF_CONFIG_SMALL("Force constant framerate"),
  211. .init = init,
  212. .uninit = uninit,
  213. .priv_size = sizeof(FPSContext),
  214. .inputs = (AVFilterPad[]) {{ .name = "default",
  215. .type = AVMEDIA_TYPE_VIDEO,
  216. .start_frame = null_start_frame,
  217. .draw_slice = null_draw_slice,
  218. .end_frame = end_frame, },
  219. { .name = NULL}},
  220. .outputs = (AVFilterPad[]) {{ .name = "default",
  221. .type = AVMEDIA_TYPE_VIDEO,
  222. .request_frame = request_frame,
  223. .config_props = config_props},
  224. { .name = NULL}},
  225. };