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.

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