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.

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