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.

303 lines
8.9KB

  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/common.h"
  23. #include "libavutil/fifo.h"
  24. #include "libavutil/mathematics.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/parseutils.h"
  27. #include "avfilter.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. typedef struct FPSContext {
  31. const AVClass *class;
  32. AVFifoBuffer *fifo; ///< store frames until we get two successive timestamps
  33. /* timestamps in input timebase */
  34. int64_t first_pts; ///< pts of the first frame that arrived on this filter
  35. int64_t pts; ///< pts of the first frame currently in the fifo
  36. double start_time; ///< pts, in seconds, of the expected first frame
  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. { "start_time", "Assume the first PTS should be this value.", OFFSET(start_time), AV_OPT_TYPE_DOUBLE, { .dbl = AV_NOPTS_VALUE}, INT64_MIN, INT64_MAX, V },
  50. { NULL },
  51. };
  52. static const AVClass class = {
  53. .class_name = "FPS filter",
  54. .item_name = av_default_item_name,
  55. .option = options,
  56. .version = LIBAVUTIL_VERSION_INT,
  57. };
  58. static av_cold int init(AVFilterContext *ctx)
  59. {
  60. FPSContext *s = ctx->priv;
  61. int ret;
  62. if ((ret = av_parse_video_rate(&s->framerate, s->fps)) < 0) {
  63. av_log(ctx, AV_LOG_ERROR, "Error parsing framerate %s.\n", s->fps);
  64. return ret;
  65. }
  66. if (!(s->fifo = av_fifo_alloc(2*sizeof(AVFrame*))))
  67. return AVERROR(ENOMEM);
  68. s->pts = AV_NOPTS_VALUE;
  69. s->first_pts = AV_NOPTS_VALUE;
  70. av_log(ctx, AV_LOG_VERBOSE, "fps=%d/%d\n", s->framerate.num, s->framerate.den);
  71. return 0;
  72. }
  73. static void flush_fifo(AVFifoBuffer *fifo)
  74. {
  75. while (av_fifo_size(fifo)) {
  76. AVFrame *tmp;
  77. av_fifo_generic_read(fifo, &tmp, sizeof(tmp), NULL);
  78. av_frame_free(&tmp);
  79. }
  80. }
  81. static av_cold void uninit(AVFilterContext *ctx)
  82. {
  83. FPSContext *s = ctx->priv;
  84. if (s->fifo) {
  85. s->drop += av_fifo_size(s->fifo) / sizeof(AVFilterBufferRef*);
  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. return 0;
  99. }
  100. static int request_frame(AVFilterLink *outlink)
  101. {
  102. AVFilterContext *ctx = outlink->src;
  103. FPSContext *s = ctx->priv;
  104. int frames_out = s->frames_out;
  105. int ret = 0;
  106. while (ret >= 0 && s->frames_out == frames_out)
  107. ret = ff_request_frame(ctx->inputs[0]);
  108. /* flush the fifo */
  109. if (ret == AVERROR_EOF && av_fifo_size(s->fifo)) {
  110. int i;
  111. for (i = 0; av_fifo_size(s->fifo); i++) {
  112. AVFrame *buf;
  113. av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
  114. buf->pts = av_rescale_q(s->first_pts, ctx->inputs[0]->time_base,
  115. outlink->time_base) + s->frames_out;
  116. if ((ret = ff_filter_frame(outlink, buf)) < 0)
  117. return ret;
  118. s->frames_out++;
  119. }
  120. return 0;
  121. }
  122. return ret;
  123. }
  124. static int write_to_fifo(AVFifoBuffer *fifo, AVFrame *buf)
  125. {
  126. int ret;
  127. if (!av_fifo_space(fifo) &&
  128. (ret = av_fifo_realloc2(fifo, 2*av_fifo_size(fifo)))) {
  129. av_frame_free(&buf);
  130. return ret;
  131. }
  132. av_fifo_generic_write(fifo, &buf, sizeof(buf), NULL);
  133. return 0;
  134. }
  135. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  136. {
  137. AVFilterContext *ctx = inlink->dst;
  138. FPSContext *s = ctx->priv;
  139. AVFilterLink *outlink = ctx->outputs[0];
  140. int64_t delta;
  141. int i, ret;
  142. s->frames_in++;
  143. /* discard frames until we get the first timestamp */
  144. if (s->pts == AV_NOPTS_VALUE) {
  145. if (buf->pts != AV_NOPTS_VALUE) {
  146. ret = write_to_fifo(s->fifo, buf);
  147. if (ret < 0)
  148. return ret;
  149. if (s->start_time != AV_NOPTS_VALUE) {
  150. double first_pts = s->start_time * AV_TIME_BASE;
  151. first_pts = FFMIN(FFMAX(first_pts, INT64_MIN), INT64_MAX);
  152. s->first_pts = s->pts = av_rescale_q(first_pts, AV_TIME_BASE_Q,
  153. inlink->time_base);
  154. av_log(ctx, AV_LOG_VERBOSE, "Set first pts to (in:%"PRId64" out:%"PRId64")\n",
  155. s->first_pts, av_rescale_q(first_pts, AV_TIME_BASE_Q,
  156. outlink->time_base));
  157. } else {
  158. s->first_pts = s->pts = buf->pts;
  159. }
  160. } else {
  161. av_log(ctx, AV_LOG_WARNING, "Discarding initial frame(s) with no "
  162. "timestamp.\n");
  163. av_frame_free(&buf);
  164. s->drop++;
  165. }
  166. return 0;
  167. }
  168. /* now wait for the next timestamp */
  169. if (buf->pts == AV_NOPTS_VALUE) {
  170. return write_to_fifo(s->fifo, buf);
  171. }
  172. /* number of output frames */
  173. delta = av_rescale_q(buf->pts - s->pts, inlink->time_base,
  174. outlink->time_base);
  175. if (delta < 1) {
  176. /* drop the frame and everything buffered except the first */
  177. AVFrame *tmp;
  178. int drop = av_fifo_size(s->fifo)/sizeof(AVFrame*);
  179. av_log(ctx, AV_LOG_DEBUG, "Dropping %d frame(s).\n", drop);
  180. s->drop += drop;
  181. av_fifo_generic_read(s->fifo, &tmp, sizeof(tmp), NULL);
  182. flush_fifo(s->fifo);
  183. ret = write_to_fifo(s->fifo, tmp);
  184. av_frame_free(&buf);
  185. return ret;
  186. }
  187. /* can output >= 1 frames */
  188. for (i = 0; i < delta; i++) {
  189. AVFrame *buf_out;
  190. av_fifo_generic_read(s->fifo, &buf_out, sizeof(buf_out), NULL);
  191. /* duplicate the frame if needed */
  192. if (!av_fifo_size(s->fifo) && i < delta - 1) {
  193. AVFrame *dup = av_frame_clone(buf_out);
  194. av_log(ctx, AV_LOG_DEBUG, "Duplicating frame.\n");
  195. if (dup)
  196. ret = write_to_fifo(s->fifo, dup);
  197. else
  198. ret = AVERROR(ENOMEM);
  199. if (ret < 0) {
  200. av_frame_free(&buf_out);
  201. av_frame_free(&buf);
  202. return ret;
  203. }
  204. s->dup++;
  205. }
  206. buf_out->pts = av_rescale_q(s->first_pts, inlink->time_base,
  207. outlink->time_base) + s->frames_out;
  208. if ((ret = ff_filter_frame(outlink, buf_out)) < 0) {
  209. av_frame_free(&buf);
  210. return ret;
  211. }
  212. s->frames_out++;
  213. }
  214. flush_fifo(s->fifo);
  215. ret = write_to_fifo(s->fifo, buf);
  216. s->pts = s->first_pts + av_rescale_q(s->frames_out, outlink->time_base, inlink->time_base);
  217. return ret;
  218. }
  219. static const AVFilterPad avfilter_vf_fps_inputs[] = {
  220. {
  221. .name = "default",
  222. .type = AVMEDIA_TYPE_VIDEO,
  223. .filter_frame = filter_frame,
  224. },
  225. { NULL }
  226. };
  227. static const AVFilterPad avfilter_vf_fps_outputs[] = {
  228. {
  229. .name = "default",
  230. .type = AVMEDIA_TYPE_VIDEO,
  231. .request_frame = request_frame,
  232. .config_props = config_props
  233. },
  234. { NULL }
  235. };
  236. AVFilter avfilter_vf_fps = {
  237. .name = "fps",
  238. .description = NULL_IF_CONFIG_SMALL("Force constant framerate"),
  239. .init = init,
  240. .uninit = uninit,
  241. .priv_size = sizeof(FPSContext),
  242. .priv_class = &class,
  243. .inputs = avfilter_vf_fps_inputs,
  244. .outputs = avfilter_vf_fps_outputs,
  245. };