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.

272 lines
8.3KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * a filter enforcing given constant framerate
  21. */
  22. #include "libavutil/fifo.h"
  23. #include "libavutil/mathematics.h"
  24. #include "libavutil/opt.h"
  25. #include "libavutil/parseutils.h"
  26. #include "avfilter.h"
  27. typedef struct FPSContext {
  28. const AVClass *class;
  29. AVFifoBuffer *fifo; ///< store frames until we get two successive timestamps
  30. /* timestamps in input timebase */
  31. int64_t first_pts; ///< pts of the first frame that arrived on this filter
  32. int64_t pts; ///< pts of the first frame currently in the fifo
  33. AVRational framerate; ///< target framerate
  34. char *fps; ///< a string describing target framerate
  35. /* statistics */
  36. int frames_in; ///< number of frames on input
  37. int frames_out; ///< number of frames on output
  38. int dup; ///< number of frames duplicated
  39. int drop; ///< number of framed dropped
  40. } FPSContext;
  41. #define OFFSET(x) offsetof(FPSContext, x)
  42. #define V AV_OPT_FLAG_VIDEO_PARAM
  43. static const AVOption options[] = {
  44. { "fps", "A string describing desired output framerate", OFFSET(fps), AV_OPT_TYPE_STRING, { .str = "25" }, .flags = V },
  45. { NULL },
  46. };
  47. static const AVClass class = {
  48. .class_name = "FPS filter",
  49. .item_name = av_default_item_name,
  50. .option = options,
  51. .version = LIBAVUTIL_VERSION_INT,
  52. };
  53. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  54. {
  55. FPSContext *s = ctx->priv;
  56. int ret;
  57. s->class = &class;
  58. av_opt_set_defaults(s);
  59. if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
  60. av_log(ctx, AV_LOG_ERROR, "Error parsing the options string %s.\n",
  61. args);
  62. return ret;
  63. }
  64. if ((ret = av_parse_video_rate(&s->framerate, s->fps)) < 0) {
  65. av_log(ctx, AV_LOG_ERROR, "Error parsing framerate %s.\n", s->fps);
  66. return ret;
  67. }
  68. av_opt_free(s);
  69. if (!(s->fifo = av_fifo_alloc(2*sizeof(AVFilterBufferRef*))))
  70. return AVERROR(ENOMEM);
  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. AVFilterBufferRef *tmp;
  78. av_fifo_generic_read(fifo, &tmp, sizeof(tmp), NULL);
  79. avfilter_unref_buffer(tmp);
  80. }
  81. }
  82. static av_cold void uninit(AVFilterContext *ctx)
  83. {
  84. FPSContext *s = ctx->priv;
  85. if (s->fifo) {
  86. flush_fifo(s->fifo);
  87. av_fifo_free(s->fifo);
  88. }
  89. av_log(ctx, AV_LOG_VERBOSE, "%d frames in, %d frames out; %d frames dropped, "
  90. "%d frames duplicated.\n", s->frames_in, s->frames_out, s->drop, s->dup);
  91. }
  92. static int config_props(AVFilterLink* link)
  93. {
  94. FPSContext *s = link->src->priv;
  95. link->time_base = (AVRational){ s->framerate.den, s->framerate.num };
  96. link->w = link->src->inputs[0]->w;
  97. link->h = link->src->inputs[0]->h;
  98. s->pts = AV_NOPTS_VALUE;
  99. return 0;
  100. }
  101. static int request_frame(AVFilterLink *outlink)
  102. {
  103. AVFilterContext *ctx = outlink->src;
  104. FPSContext *s = ctx->priv;
  105. int frames_out = s->frames_out;
  106. int ret = 0;
  107. while (ret >= 0 && s->frames_out == frames_out)
  108. ret = avfilter_request_frame(ctx->inputs[0]);
  109. /* flush the fifo */
  110. if (ret == AVERROR_EOF && av_fifo_size(s->fifo)) {
  111. int i;
  112. for (i = 0; av_fifo_size(s->fifo); i++) {
  113. AVFilterBufferRef *buf;
  114. av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
  115. buf->pts = av_rescale_q(s->first_pts, ctx->inputs[0]->time_base,
  116. outlink->time_base) + s->frames_out;
  117. avfilter_start_frame(outlink, buf);
  118. avfilter_draw_slice(outlink, 0, outlink->h, 1);
  119. avfilter_end_frame(outlink);
  120. s->frames_out++;
  121. }
  122. return 0;
  123. }
  124. return ret;
  125. }
  126. static int write_to_fifo(AVFifoBuffer *fifo, AVFilterBufferRef *buf)
  127. {
  128. int ret;
  129. if (!av_fifo_space(fifo) &&
  130. (ret = av_fifo_realloc2(fifo, 2*av_fifo_size(fifo))))
  131. return ret;
  132. av_fifo_generic_write(fifo, &buf, sizeof(buf), NULL);
  133. return 0;
  134. }
  135. static void end_frame(AVFilterLink *inlink)
  136. {
  137. AVFilterContext *ctx = inlink->dst;
  138. FPSContext *s = ctx->priv;
  139. AVFilterLink *outlink = ctx->outputs[0];
  140. AVFilterBufferRef *buf = inlink->cur_buf;
  141. int64_t delta;
  142. int i;
  143. s->frames_in++;
  144. /* discard frames until we get the first timestamp */
  145. if (s->pts == AV_NOPTS_VALUE) {
  146. if (buf->pts != AV_NOPTS_VALUE) {
  147. write_to_fifo(s->fifo, buf);
  148. s->first_pts = s->pts = buf->pts;
  149. } else {
  150. av_log(ctx, AV_LOG_WARNING, "Discarding initial frame(s) with no "
  151. "timestamp.\n");
  152. avfilter_unref_buffer(buf);
  153. s->drop++;
  154. }
  155. return;
  156. }
  157. /* now wait for the next timestamp */
  158. if (buf->pts == AV_NOPTS_VALUE) {
  159. write_to_fifo(s->fifo, buf);
  160. return;
  161. }
  162. /* number of output frames */
  163. delta = av_rescale_q(buf->pts - s->pts, inlink->time_base,
  164. outlink->time_base);
  165. if (delta < 1) {
  166. /* drop the frame and everything buffered except the first */
  167. AVFilterBufferRef *tmp;
  168. int drop = av_fifo_size(s->fifo)/sizeof(AVFilterBufferRef*);
  169. av_log(ctx, AV_LOG_DEBUG, "Dropping %d frame(s).\n", drop);
  170. s->drop += drop;
  171. av_fifo_generic_read(s->fifo, &tmp, sizeof(tmp), NULL);
  172. flush_fifo(s->fifo);
  173. write_to_fifo(s->fifo, tmp);
  174. avfilter_unref_buffer(buf);
  175. return;
  176. }
  177. /* can output >= 1 frames */
  178. for (i = 0; i < delta; i++) {
  179. AVFilterBufferRef *buf_out;
  180. av_fifo_generic_read(s->fifo, &buf_out, sizeof(buf_out), NULL);
  181. /* duplicate the frame if needed */
  182. if (!av_fifo_size(s->fifo) && i < delta - 1) {
  183. av_log(ctx, AV_LOG_DEBUG, "Duplicating frame.\n");
  184. write_to_fifo(s->fifo, avfilter_ref_buffer(buf_out, AV_PERM_READ));
  185. s->dup++;
  186. }
  187. buf_out->pts = av_rescale_q(s->first_pts, inlink->time_base,
  188. outlink->time_base) + s->frames_out;
  189. avfilter_start_frame(outlink, buf_out);
  190. avfilter_draw_slice(outlink, 0, outlink->h, 1);
  191. avfilter_end_frame(outlink);
  192. s->frames_out++;
  193. }
  194. flush_fifo(s->fifo);
  195. write_to_fifo(s->fifo, buf);
  196. s->pts = s->first_pts + av_rescale_q(s->frames_out, outlink->time_base, inlink->time_base);
  197. }
  198. static void null_start_frame(AVFilterLink *link, AVFilterBufferRef *buf)
  199. {
  200. }
  201. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  202. {
  203. }
  204. AVFilter avfilter_vf_fps = {
  205. .name = "fps",
  206. .description = NULL_IF_CONFIG_SMALL("Force constant framerate"),
  207. .init = init,
  208. .uninit = uninit,
  209. .priv_size = sizeof(FPSContext),
  210. .inputs = (AVFilterPad[]) {{ .name = "default",
  211. .type = AVMEDIA_TYPE_VIDEO,
  212. .start_frame = null_start_frame,
  213. .draw_slice = null_draw_slice,
  214. .end_frame = end_frame, },
  215. { .name = NULL}},
  216. .outputs = (AVFilterPad[]) {{ .name = "default",
  217. .type = AVMEDIA_TYPE_VIDEO,
  218. .request_frame = request_frame,
  219. .config_props = config_props},
  220. { .name = NULL}},
  221. };