You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 lines
8.5KB

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