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.

181 lines
5.4KB

  1. /*
  2. * Copyright (c) 2010 Stefano Sabatini
  3. * Copyright (c) 2008 Victor Paesa
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * video presentation timestamp (PTS) modification filter
  24. */
  25. /* #define DEBUG */
  26. #include "libavutil/eval.h"
  27. #include "libavutil/internal.h"
  28. #include "libavutil/mathematics.h"
  29. #include "libavutil/time.h"
  30. #include "avfilter.h"
  31. #include "internal.h"
  32. #include "video.h"
  33. static const char *const var_names[] = {
  34. "E", ///< Euler number
  35. "INTERLACED", ///< tell if the current frame is interlaced
  36. "N", ///< frame number (starting at zero)
  37. "PHI", ///< golden ratio
  38. "PI", ///< greek pi
  39. "POS", ///< original position in the file of the frame
  40. "PREV_INPTS", ///< previous input PTS
  41. "PREV_OUTPTS", ///< previous output PTS
  42. "PTS", ///< original pts in the file of the frame
  43. "STARTPTS", ///< PTS at start of movie
  44. "TB", ///< timebase
  45. "RTCTIME", ///< wallclock (RTC) time in micro seconds
  46. "RTCSTART", ///< wallclock (RTC) time at the start of the movie in micro seconds
  47. NULL
  48. };
  49. enum var_name {
  50. VAR_E,
  51. VAR_INTERLACED,
  52. VAR_N,
  53. VAR_PHI,
  54. VAR_PI,
  55. VAR_POS,
  56. VAR_PREV_INPTS,
  57. VAR_PREV_OUTPTS,
  58. VAR_PTS,
  59. VAR_STARTPTS,
  60. VAR_TB,
  61. VAR_RTCTIME,
  62. VAR_RTCSTART,
  63. VAR_VARS_NB
  64. };
  65. typedef struct {
  66. AVExpr *expr;
  67. double var_values[VAR_VARS_NB];
  68. } SetPTSContext;
  69. static av_cold int init(AVFilterContext *ctx, const char *args)
  70. {
  71. SetPTSContext *setpts = ctx->priv;
  72. int ret;
  73. if ((ret = av_expr_parse(&setpts->expr, args ? args : "PTS",
  74. var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
  75. av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
  76. return ret;
  77. }
  78. setpts->var_values[VAR_E ] = M_E;
  79. setpts->var_values[VAR_N ] = 0.0;
  80. setpts->var_values[VAR_PHI ] = M_PHI;
  81. setpts->var_values[VAR_PI ] = M_PI;
  82. setpts->var_values[VAR_PREV_INPTS ] = NAN;
  83. setpts->var_values[VAR_PREV_OUTPTS] = NAN;
  84. setpts->var_values[VAR_STARTPTS ] = NAN;
  85. return 0;
  86. }
  87. static int config_input(AVFilterLink *inlink)
  88. {
  89. SetPTSContext *setpts = inlink->dst->priv;
  90. setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
  91. setpts->var_values[VAR_RTCSTART] = av_gettime();
  92. av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f\n", setpts->var_values[VAR_TB]);
  93. return 0;
  94. }
  95. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  96. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  97. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
  98. {
  99. SetPTSContext *setpts = inlink->dst->priv;
  100. int64_t in_pts = frame->pts;
  101. double d;
  102. if (isnan(setpts->var_values[VAR_STARTPTS]))
  103. setpts->var_values[VAR_STARTPTS] = TS2D(frame->pts);
  104. setpts->var_values[VAR_INTERLACED] = frame->video->interlaced;
  105. setpts->var_values[VAR_PTS ] = TS2D(frame->pts);
  106. setpts->var_values[VAR_POS ] = frame->pos == -1 ? NAN : frame->pos;
  107. setpts->var_values[VAR_RTCTIME ] = av_gettime();
  108. d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
  109. frame->pts = D2TS(d);
  110. #ifdef DEBUG
  111. av_log(inlink->dst, AV_LOG_DEBUG,
  112. "n:%"PRId64" interlaced:%d pos:%"PRId64" pts:%"PRId64" t:%f -> pts:%"PRId64" t:%f\n",
  113. (int64_t)setpts->var_values[VAR_N],
  114. (int)setpts->var_values[VAR_INTERLACED],
  115. frame->pos, in_pts, in_pts * av_q2d(inlink->time_base),
  116. frame->pts, frame->pts * av_q2d(inlink->time_base));
  117. #endif
  118. setpts->var_values[VAR_N] += 1.0;
  119. setpts->var_values[VAR_PREV_INPTS ] = TS2D(in_pts);
  120. setpts->var_values[VAR_PREV_OUTPTS] = TS2D(frame->pts);
  121. return ff_filter_frame(inlink->dst->outputs[0], frame);
  122. }
  123. static av_cold void uninit(AVFilterContext *ctx)
  124. {
  125. SetPTSContext *setpts = ctx->priv;
  126. av_expr_free(setpts->expr);
  127. setpts->expr = NULL;
  128. }
  129. static const AVFilterPad avfilter_vf_setpts_inputs[] = {
  130. {
  131. .name = "default",
  132. .type = AVMEDIA_TYPE_VIDEO,
  133. .get_video_buffer = ff_null_get_video_buffer,
  134. .config_props = config_input,
  135. .filter_frame = filter_frame,
  136. },
  137. { NULL }
  138. };
  139. static const AVFilterPad avfilter_vf_setpts_outputs[] = {
  140. {
  141. .name = "default",
  142. .type = AVMEDIA_TYPE_VIDEO,
  143. },
  144. { NULL }
  145. };
  146. AVFilter avfilter_vf_setpts = {
  147. .name = "setpts",
  148. .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
  149. .init = init,
  150. .uninit = uninit,
  151. .priv_size = sizeof(SetPTSContext),
  152. .inputs = avfilter_vf_setpts_inputs,
  153. .outputs = avfilter_vf_setpts_outputs,
  154. };