setpts.c 7.7 KB

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