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.

177 lines
5.2KB

  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 "avfilter.h"
  30. #include "internal.h"
  31. #include "video.h"
  32. static const char *const var_names[] = {
  33. "E", ///< Euler number
  34. "INTERLACED", ///< tell if the current frame is interlaced
  35. "N", ///< frame number (starting at zero)
  36. "PHI", ///< golden ratio
  37. "PI", ///< greek pi
  38. "POS", ///< original position in the file of the frame
  39. "PREV_INPTS", ///< previous input PTS
  40. "PREV_OUTPTS", ///< previous output PTS
  41. "PTS", ///< original pts in the file of the frame
  42. "STARTPTS", ///< PTS at start of movie
  43. "TB", ///< timebase
  44. NULL
  45. };
  46. enum var_name {
  47. VAR_E,
  48. VAR_INTERLACED,
  49. VAR_N,
  50. VAR_PHI,
  51. VAR_PI,
  52. VAR_POS,
  53. VAR_PREV_INPTS,
  54. VAR_PREV_OUTPTS,
  55. VAR_PTS,
  56. VAR_STARTPTS,
  57. VAR_TB,
  58. VAR_VARS_NB
  59. };
  60. typedef struct {
  61. AVExpr *expr;
  62. double var_values[VAR_VARS_NB];
  63. } SetPTSContext;
  64. static av_cold int init(AVFilterContext *ctx, const char *args)
  65. {
  66. SetPTSContext *setpts = ctx->priv;
  67. int ret;
  68. if ((ret = av_expr_parse(&setpts->expr, args ? args : "PTS",
  69. var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
  70. av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
  71. return ret;
  72. }
  73. setpts->var_values[VAR_E ] = M_E;
  74. setpts->var_values[VAR_N ] = 0.0;
  75. setpts->var_values[VAR_PHI ] = M_PHI;
  76. setpts->var_values[VAR_PI ] = M_PI;
  77. setpts->var_values[VAR_PREV_INPTS ] = NAN;
  78. setpts->var_values[VAR_PREV_OUTPTS] = NAN;
  79. setpts->var_values[VAR_STARTPTS ] = NAN;
  80. return 0;
  81. }
  82. static int config_input(AVFilterLink *inlink)
  83. {
  84. SetPTSContext *setpts = inlink->dst->priv;
  85. setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
  86. av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f\n", setpts->var_values[VAR_TB]);
  87. return 0;
  88. }
  89. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  90. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  91. static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
  92. {
  93. SetPTSContext *setpts = inlink->dst->priv;
  94. double d;
  95. AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
  96. if (!outpicref)
  97. return AVERROR(ENOMEM);
  98. if (isnan(setpts->var_values[VAR_STARTPTS]))
  99. setpts->var_values[VAR_STARTPTS] = TS2D(inpicref->pts);
  100. setpts->var_values[VAR_INTERLACED] = inpicref->video->interlaced;
  101. setpts->var_values[VAR_PTS ] = TS2D(inpicref->pts);
  102. setpts->var_values[VAR_POS ] = inpicref->pos == -1 ? NAN : inpicref->pos;
  103. d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
  104. outpicref->pts = D2TS(d);
  105. #ifdef DEBUG
  106. av_log(inlink->dst, AV_LOG_DEBUG,
  107. "n:%"PRId64" interlaced:%d pos:%"PRId64" pts:%"PRId64" t:%f -> pts:%"PRId64" t:%f\n",
  108. (int64_t)setpts->var_values[VAR_N],
  109. (int)setpts->var_values[VAR_INTERLACED],
  110. inpicref ->pos,
  111. inpicref ->pts, inpicref ->pts * av_q2d(inlink->time_base),
  112. outpicref->pts, outpicref->pts * av_q2d(inlink->time_base));
  113. #endif
  114. setpts->var_values[VAR_N] += 1.0;
  115. setpts->var_values[VAR_PREV_INPTS ] = TS2D(inpicref ->pts);
  116. setpts->var_values[VAR_PREV_OUTPTS] = TS2D(outpicref->pts);
  117. return ff_start_frame(inlink->dst->outputs[0], outpicref);
  118. }
  119. static av_cold void uninit(AVFilterContext *ctx)
  120. {
  121. SetPTSContext *setpts = ctx->priv;
  122. av_expr_free(setpts->expr);
  123. setpts->expr = NULL;
  124. }
  125. static const AVFilterPad avfilter_vf_setpts_inputs[] = {
  126. {
  127. .name = "default",
  128. .type = AVMEDIA_TYPE_VIDEO,
  129. .get_video_buffer = ff_null_get_video_buffer,
  130. .config_props = config_input,
  131. .start_frame = start_frame,
  132. },
  133. { NULL }
  134. };
  135. static const AVFilterPad avfilter_vf_setpts_outputs[] = {
  136. {
  137. .name = "default",
  138. .type = AVMEDIA_TYPE_VIDEO,
  139. },
  140. { NULL }
  141. };
  142. AVFilter avfilter_vf_setpts = {
  143. .name = "setpts",
  144. .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
  145. .init = init,
  146. .uninit = uninit,
  147. .priv_size = sizeof(SetPTSContext),
  148. .inputs = avfilter_vf_setpts_inputs,
  149. .outputs = avfilter_vf_setpts_outputs,
  150. };