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.

260 lines
7.3KB

  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. #include "libavutil/eval.h"
  26. #include "libavutil/internal.h"
  27. #include "libavutil/mathematics.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/time.h"
  30. #include "audio.h"
  31. #include "avfilter.h"
  32. #include "internal.h"
  33. #include "video.h"
  34. #include "config.h"
  35. static const char *const var_names[] = {
  36. "E", ///< Euler number
  37. "INTERLACED", ///< tell if the current frame is interlaced
  38. "N", ///< frame / sample number (starting at zero)
  39. "PHI", ///< golden ratio
  40. "PI", ///< greek pi
  41. "PREV_INPTS", ///< previous input PTS
  42. "PREV_OUTPTS", ///< previous output PTS
  43. "PTS", ///< original pts in the file of the frame
  44. "STARTPTS", ///< PTS at start of movie
  45. "TB", ///< timebase
  46. "RTCTIME", ///< wallclock (RTC) time in micro seconds
  47. "RTCSTART", ///< wallclock (RTC) time at the start of the movie in micro seconds
  48. "S", // Number of samples in the current frame
  49. "SR", // Audio sample rate
  50. NULL
  51. };
  52. enum var_name {
  53. VAR_E,
  54. VAR_INTERLACED,
  55. VAR_N,
  56. VAR_PHI,
  57. VAR_PI,
  58. VAR_PREV_INPTS,
  59. VAR_PREV_OUTPTS,
  60. VAR_PTS,
  61. VAR_STARTPTS,
  62. VAR_TB,
  63. VAR_RTCTIME,
  64. VAR_RTCSTART,
  65. VAR_S,
  66. VAR_SR,
  67. VAR_VARS_NB
  68. };
  69. typedef struct {
  70. const AVClass *class;
  71. char *expr_str;
  72. AVExpr *expr;
  73. double var_values[VAR_VARS_NB];
  74. } SetPTSContext;
  75. static av_cold int init(AVFilterContext *ctx)
  76. {
  77. SetPTSContext *setpts = ctx->priv;
  78. int ret;
  79. if ((ret = av_expr_parse(&setpts->expr, setpts->expr_str,
  80. var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
  81. av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", setpts->expr_str);
  82. return ret;
  83. }
  84. setpts->var_values[VAR_E] = M_E;
  85. setpts->var_values[VAR_N] = 0.0;
  86. setpts->var_values[VAR_S] = 0.0;
  87. setpts->var_values[VAR_PHI] = M_PHI;
  88. setpts->var_values[VAR_PI] = M_PI;
  89. setpts->var_values[VAR_PREV_INPTS] = NAN;
  90. setpts->var_values[VAR_PREV_OUTPTS] = NAN;
  91. setpts->var_values[VAR_STARTPTS] = NAN;
  92. return 0;
  93. }
  94. static int config_input(AVFilterLink *inlink)
  95. {
  96. SetPTSContext *setpts = inlink->dst->priv;
  97. setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
  98. setpts->var_values[VAR_RTCSTART] = av_gettime();
  99. if (inlink->type == AVMEDIA_TYPE_AUDIO) {
  100. setpts->var_values[VAR_SR] = inlink->sample_rate;
  101. }
  102. av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f\n", setpts->var_values[VAR_TB]);
  103. return 0;
  104. }
  105. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  106. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  107. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  108. {
  109. SetPTSContext *setpts = inlink->dst->priv;
  110. int64_t in_pts = frame->pts;
  111. double d;
  112. if (isnan(setpts->var_values[VAR_STARTPTS]))
  113. setpts->var_values[VAR_STARTPTS] = TS2D(frame->pts);
  114. setpts->var_values[VAR_PTS ] = TS2D(frame->pts);
  115. setpts->var_values[VAR_RTCTIME ] = av_gettime();
  116. if (inlink->type == AVMEDIA_TYPE_VIDEO) {
  117. setpts->var_values[VAR_INTERLACED] = frame->interlaced_frame;
  118. } else {
  119. setpts->var_values[VAR_S] = frame->nb_samples;
  120. }
  121. d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
  122. frame->pts = D2TS(d);
  123. #ifdef DEBUG
  124. av_log(inlink->dst, AV_LOG_DEBUG,
  125. "n:%"PRId64" interlaced:%d pts:%"PRId64" t:%f -> pts:%"PRId64" t:%f\n",
  126. (int64_t)setpts->var_values[VAR_N],
  127. (int)setpts->var_values[VAR_INTERLACED],
  128. in_pts, in_pts * av_q2d(inlink->time_base),
  129. frame->pts, frame->pts * av_q2d(inlink->time_base));
  130. #endif
  131. if (inlink->type == AVMEDIA_TYPE_VIDEO) {
  132. setpts->var_values[VAR_N] += 1.0;
  133. } else {
  134. setpts->var_values[VAR_N] += frame->nb_samples;
  135. }
  136. setpts->var_values[VAR_PREV_INPTS ] = TS2D(in_pts);
  137. setpts->var_values[VAR_PREV_OUTPTS] = TS2D(frame->pts);
  138. return ff_filter_frame(inlink->dst->outputs[0], frame);
  139. }
  140. static av_cold void uninit(AVFilterContext *ctx)
  141. {
  142. SetPTSContext *setpts = ctx->priv;
  143. av_expr_free(setpts->expr);
  144. setpts->expr = NULL;
  145. }
  146. #define OFFSET(x) offsetof(SetPTSContext, x)
  147. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM
  148. static const AVOption options[] = {
  149. { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = FLAGS },
  150. { NULL },
  151. };
  152. #if CONFIG_SETPTS_FILTER
  153. static const AVClass setpts_class = {
  154. .class_name = "setpts",
  155. .item_name = av_default_item_name,
  156. .option = options,
  157. .version = LIBAVUTIL_VERSION_INT,
  158. };
  159. static const AVFilterPad avfilter_vf_setpts_inputs[] = {
  160. {
  161. .name = "default",
  162. .type = AVMEDIA_TYPE_VIDEO,
  163. .get_video_buffer = ff_null_get_video_buffer,
  164. .config_props = config_input,
  165. .filter_frame = filter_frame,
  166. },
  167. { NULL }
  168. };
  169. static const AVFilterPad avfilter_vf_setpts_outputs[] = {
  170. {
  171. .name = "default",
  172. .type = AVMEDIA_TYPE_VIDEO,
  173. },
  174. { NULL }
  175. };
  176. AVFilter ff_vf_setpts = {
  177. .name = "setpts",
  178. .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
  179. .init = init,
  180. .uninit = uninit,
  181. .priv_size = sizeof(SetPTSContext),
  182. .priv_class = &setpts_class,
  183. .inputs = avfilter_vf_setpts_inputs,
  184. .outputs = avfilter_vf_setpts_outputs,
  185. };
  186. #endif
  187. #if CONFIG_ASETPTS_FILTER
  188. static const AVClass asetpts_class = {
  189. .class_name = "asetpts",
  190. .item_name = av_default_item_name,
  191. .option = options,
  192. .version = LIBAVUTIL_VERSION_INT,
  193. };
  194. static const AVFilterPad asetpts_inputs[] = {
  195. {
  196. .name = "default",
  197. .type = AVMEDIA_TYPE_AUDIO,
  198. .get_audio_buffer = ff_null_get_audio_buffer,
  199. .config_props = config_input,
  200. .filter_frame = filter_frame,
  201. },
  202. { NULL }
  203. };
  204. static const AVFilterPad asetpts_outputs[] = {
  205. {
  206. .name = "default",
  207. .type = AVMEDIA_TYPE_AUDIO,
  208. },
  209. { NULL }
  210. };
  211. AVFilter ff_af_asetpts = {
  212. .name = "asetpts",
  213. .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
  214. .init = init,
  215. .uninit = uninit,
  216. .priv_size = sizeof(SetPTSContext),
  217. .priv_class = &asetpts_class,
  218. .inputs = asetpts_inputs,
  219. .outputs = asetpts_outputs,
  220. };
  221. #endif