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.

302 lines
9.1KB

  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. AVRational framerate; ///< target framerate
  37. char *fps; ///< a string describing target framerate
  38. /* statistics */
  39. int frames_in; ///< number of frames on input
  40. int frames_out; ///< number of frames on output
  41. int dup; ///< number of frames duplicated
  42. int drop; ///< number of framed dropped
  43. } FPSContext;
  44. #define OFFSET(x) offsetof(FPSContext, x)
  45. #define V AV_OPT_FLAG_VIDEO_PARAM
  46. static const AVOption options[] = {
  47. { "fps", "A string describing desired output framerate", OFFSET(fps), AV_OPT_TYPE_STRING, { .str = "25" }, .flags = V },
  48. { NULL },
  49. };
  50. static const AVClass class = {
  51. .class_name = "FPS filter",
  52. .item_name = av_default_item_name,
  53. .option = options,
  54. .version = LIBAVUTIL_VERSION_INT,
  55. };
  56. static av_cold int init(AVFilterContext *ctx, const char *args)
  57. {
  58. FPSContext *s = ctx->priv;
  59. int ret;
  60. s->class = &class;
  61. av_opt_set_defaults(s);
  62. if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
  63. av_log(ctx, AV_LOG_ERROR, "Error parsing the options string %s.\n",
  64. args);
  65. return ret;
  66. }
  67. if ((ret = av_parse_video_rate(&s->framerate, s->fps)) < 0) {
  68. av_log(ctx, AV_LOG_ERROR, "Error parsing framerate %s.\n", s->fps);
  69. return ret;
  70. }
  71. av_opt_free(s);
  72. if (!(s->fifo = av_fifo_alloc(2*sizeof(AVFilterBufferRef*))))
  73. return AVERROR(ENOMEM);
  74. av_log(ctx, AV_LOG_VERBOSE, "fps=%d/%d\n", s->framerate.num, s->framerate.den);
  75. return 0;
  76. }
  77. static void flush_fifo(AVFifoBuffer *fifo)
  78. {
  79. while (av_fifo_size(fifo)) {
  80. AVFilterBufferRef *tmp;
  81. av_fifo_generic_read(fifo, &tmp, sizeof(tmp), NULL);
  82. avfilter_unref_buffer(tmp);
  83. }
  84. }
  85. static av_cold void uninit(AVFilterContext *ctx)
  86. {
  87. FPSContext *s = ctx->priv;
  88. if (s->fifo) {
  89. flush_fifo(s->fifo);
  90. av_fifo_free(s->fifo);
  91. }
  92. av_log(ctx, AV_LOG_VERBOSE, "%d frames in, %d frames out; %d frames dropped, "
  93. "%d frames duplicated.\n", s->frames_in, s->frames_out, s->drop, s->dup);
  94. }
  95. static int config_props(AVFilterLink* link)
  96. {
  97. FPSContext *s = link->src->priv;
  98. link->time_base = (AVRational){ s->framerate.den, s->framerate.num };
  99. link->w = link->src->inputs[0]->w;
  100. link->h = link->src->inputs[0]->h;
  101. s->pts = AV_NOPTS_VALUE;
  102. return 0;
  103. }
  104. static int request_frame(AVFilterLink *outlink)
  105. {
  106. AVFilterContext *ctx = outlink->src;
  107. FPSContext *s = ctx->priv;
  108. int frames_out = s->frames_out;
  109. int ret = 0;
  110. while (ret >= 0 && s->frames_out == frames_out)
  111. ret = ff_request_frame(ctx->inputs[0]);
  112. /* flush the fifo */
  113. if (ret == AVERROR_EOF && av_fifo_size(s->fifo)) {
  114. int i;
  115. for (i = 0; av_fifo_size(s->fifo); i++) {
  116. AVFilterBufferRef *buf;
  117. av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
  118. buf->pts = av_rescale_q(s->first_pts, ctx->inputs[0]->time_base,
  119. outlink->time_base) + s->frames_out;
  120. if ((ret = ff_start_frame(outlink, buf)) < 0 ||
  121. (ret = ff_draw_slice(outlink, 0, outlink->h, 1)) < 0 ||
  122. (ret = ff_end_frame(outlink)) < 0)
  123. return ret;
  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. avfilter_unref_bufferp(&buf);
  136. return ret;
  137. }
  138. av_fifo_generic_write(fifo, &buf, sizeof(buf), NULL);
  139. return 0;
  140. }
  141. static int end_frame(AVFilterLink *inlink)
  142. {
  143. AVFilterContext *ctx = inlink->dst;
  144. FPSContext *s = ctx->priv;
  145. AVFilterLink *outlink = ctx->outputs[0];
  146. AVFilterBufferRef *buf = inlink->cur_buf;
  147. int64_t delta;
  148. int i, ret;
  149. inlink->cur_buf = NULL;
  150. s->frames_in++;
  151. /* discard frames until we get the first timestamp */
  152. if (s->pts == AV_NOPTS_VALUE) {
  153. if (buf->pts != AV_NOPTS_VALUE) {
  154. ret = write_to_fifo(s->fifo, buf);
  155. if (ret < 0)
  156. return ret;
  157. s->first_pts = s->pts = buf->pts;
  158. } else {
  159. av_log(ctx, AV_LOG_WARNING, "Discarding initial frame(s) with no "
  160. "timestamp.\n");
  161. avfilter_unref_buffer(buf);
  162. s->drop++;
  163. }
  164. return 0;
  165. }
  166. /* now wait for the next timestamp */
  167. if (buf->pts == AV_NOPTS_VALUE) {
  168. return write_to_fifo(s->fifo, buf);
  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. ret = write_to_fifo(s->fifo, tmp);
  182. avfilter_unref_buffer(buf);
  183. return ret;
  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. AVFilterBufferRef *dup = avfilter_ref_buffer(buf_out, AV_PERM_READ);
  192. av_log(ctx, AV_LOG_DEBUG, "Duplicating frame.\n");
  193. if (dup)
  194. ret = write_to_fifo(s->fifo, dup);
  195. else
  196. ret = AVERROR(ENOMEM);
  197. if (ret < 0) {
  198. avfilter_unref_bufferp(&buf_out);
  199. avfilter_unref_bufferp(&buf);
  200. return ret;
  201. }
  202. s->dup++;
  203. }
  204. buf_out->pts = av_rescale_q(s->first_pts, inlink->time_base,
  205. outlink->time_base) + s->frames_out;
  206. if ((ret = ff_start_frame(outlink, buf_out)) < 0 ||
  207. (ret = ff_draw_slice(outlink, 0, outlink->h, 1)) < 0 ||
  208. (ret = ff_end_frame(outlink)) < 0) {
  209. avfilter_unref_bufferp(&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 int null_start_frame(AVFilterLink *link, AVFilterBufferRef *buf)
  220. {
  221. return 0;
  222. }
  223. static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
  224. {
  225. return 0;
  226. }
  227. AVFilter avfilter_vf_fps = {
  228. .name = "fps",
  229. .description = NULL_IF_CONFIG_SMALL("Force constant framerate"),
  230. .init = init,
  231. .uninit = uninit,
  232. .priv_size = sizeof(FPSContext),
  233. .inputs = (const AVFilterPad[]) {{ .name = "default",
  234. .type = AVMEDIA_TYPE_VIDEO,
  235. .start_frame = null_start_frame,
  236. .draw_slice = null_draw_slice,
  237. .end_frame = end_frame, },
  238. { .name = NULL}},
  239. .outputs = (const AVFilterPad[]) {{ .name = "default",
  240. .type = AVMEDIA_TYPE_VIDEO,
  241. .request_frame = request_frame,
  242. .config_props = config_props},
  243. { .name = NULL}},
  244. };