setpts.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright (c) 2010 Stefano Sabatini
  3. * Copyright (c) 2008 Victor Paesa
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. static const char *const var_names[] = {
  36. "FRAME_RATE", ///< defined only for constant frame-rate video
  37. "INTERLACED", ///< tell if the current frame is interlaced
  38. "N", ///< frame / sample number (starting at zero)
  39. "NB_CONSUMED_SAMPLES", ///< number of samples consumed by the filter (only audio)
  40. "NB_SAMPLES", ///< number of samples in the current frame (only audio)
  41. "POS", ///< original position in the file of the frame
  42. "PREV_INPTS", ///< previous input PTS
  43. "PREV_INT", ///< previous input time in seconds
  44. "PREV_OUTPTS", ///< previous output PTS
  45. "PREV_OUTT", ///< previous output time in seconds
  46. "PTS", ///< original pts in the file of the frame
  47. "SAMPLE_RATE", ///< sample rate (only audio)
  48. "STARTPTS", ///< PTS at start of movie
  49. "STARTT", ///< time at start of movie
  50. "T", ///< original time in the file of the frame
  51. "TB", ///< timebase
  52. "RTCTIME", ///< wallclock (RTC) time in micro seconds
  53. "RTCSTART", ///< wallclock (RTC) time at the start of the movie in micro seconds
  54. "S", // Number of samples in the current frame
  55. "SR", // Audio sample rate
  56. NULL
  57. };
  58. enum var_name {
  59. VAR_FRAME_RATE,
  60. VAR_INTERLACED,
  61. VAR_N,
  62. VAR_NB_CONSUMED_SAMPLES,
  63. VAR_NB_SAMPLES,
  64. VAR_POS,
  65. VAR_PREV_INPTS,
  66. VAR_PREV_INT,
  67. VAR_PREV_OUTPTS,
  68. VAR_PREV_OUTT,
  69. VAR_PTS,
  70. VAR_SAMPLE_RATE,
  71. VAR_STARTPTS,
  72. VAR_STARTT,
  73. VAR_T,
  74. VAR_TB,
  75. VAR_RTCTIME,
  76. VAR_RTCSTART,
  77. VAR_S,
  78. VAR_SR,
  79. VAR_VARS_NB
  80. };
  81. typedef struct SetPTSContext {
  82. const AVClass *class;
  83. char *expr_str;
  84. AVExpr *expr;
  85. double var_values[VAR_VARS_NB];
  86. enum AVMediaType type;
  87. } SetPTSContext;
  88. static av_cold int init(AVFilterContext *ctx)
  89. {
  90. SetPTSContext *setpts = ctx->priv;
  91. int ret;
  92. if ((ret = av_expr_parse(&setpts->expr, setpts->expr_str,
  93. var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
  94. av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", setpts->expr_str);
  95. return ret;
  96. }
  97. setpts->var_values[VAR_N] = 0.0;
  98. setpts->var_values[VAR_S] = 0.0;
  99. setpts->var_values[VAR_PREV_INPTS] = NAN;
  100. setpts->var_values[VAR_PREV_INT] = NAN;
  101. setpts->var_values[VAR_PREV_OUTPTS] = NAN;
  102. setpts->var_values[VAR_PREV_OUTT] = NAN;
  103. setpts->var_values[VAR_STARTPTS] = NAN;
  104. setpts->var_values[VAR_STARTT] = NAN;
  105. return 0;
  106. }
  107. static int config_input(AVFilterLink *inlink)
  108. {
  109. AVFilterContext *ctx = inlink->dst;
  110. SetPTSContext *setpts = ctx->priv;
  111. setpts->type = inlink->type;
  112. setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
  113. setpts->var_values[VAR_RTCSTART] = av_gettime();
  114. setpts->var_values[VAR_SR] =
  115. setpts->var_values[VAR_SAMPLE_RATE] =
  116. setpts->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
  117. setpts->var_values[VAR_FRAME_RATE] = inlink->frame_rate.num &&
  118. inlink->frame_rate.den ?
  119. av_q2d(inlink->frame_rate) : NAN;
  120. av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f FRAME_RATE:%f SAMPLE_RATE:%f\n",
  121. setpts->var_values[VAR_TB],
  122. setpts->var_values[VAR_FRAME_RATE],
  123. setpts->var_values[VAR_SAMPLE_RATE]);
  124. return 0;
  125. }
  126. #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
  127. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  128. #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
  129. #define BUF_SIZE 64
  130. static inline char *double2int64str(char *buf, double v)
  131. {
  132. if (isnan(v)) snprintf(buf, BUF_SIZE, "nan");
  133. else snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)v);
  134. return buf;
  135. }
  136. #define d2istr(v) double2int64str((char[BUF_SIZE]){0}, v)
  137. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  138. {
  139. SetPTSContext *setpts = inlink->dst->priv;
  140. int64_t in_pts = frame->pts;
  141. double d;
  142. if (isnan(setpts->var_values[VAR_STARTPTS])) {
  143. setpts->var_values[VAR_STARTPTS] = TS2D(frame->pts);
  144. setpts->var_values[VAR_STARTT ] = TS2T(frame->pts, inlink->time_base);
  145. }
  146. setpts->var_values[VAR_PTS ] = TS2D(frame->pts);
  147. setpts->var_values[VAR_T ] = TS2T(frame->pts, inlink->time_base);
  148. setpts->var_values[VAR_POS ] = av_frame_get_pkt_pos(frame) == -1 ? NAN : av_frame_get_pkt_pos(frame);
  149. setpts->var_values[VAR_RTCTIME ] = av_gettime();
  150. if (inlink->type == AVMEDIA_TYPE_VIDEO) {
  151. setpts->var_values[VAR_INTERLACED] = frame->interlaced_frame;
  152. } else if (inlink->type == AVMEDIA_TYPE_AUDIO) {
  153. setpts->var_values[VAR_S] = frame->nb_samples;
  154. setpts->var_values[VAR_NB_SAMPLES] = frame->nb_samples;
  155. }
  156. d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
  157. frame->pts = D2TS(d);
  158. av_log(inlink->dst, AV_LOG_TRACE,
  159. "N:%"PRId64" PTS:%s T:%f POS:%s",
  160. (int64_t)setpts->var_values[VAR_N],
  161. d2istr(setpts->var_values[VAR_PTS]),
  162. setpts->var_values[VAR_T],
  163. d2istr(setpts->var_values[VAR_POS]));
  164. switch (inlink->type) {
  165. case AVMEDIA_TYPE_VIDEO:
  166. av_log(inlink->dst, AV_LOG_TRACE, " INTERLACED:%"PRId64,
  167. (int64_t)setpts->var_values[VAR_INTERLACED]);
  168. break;
  169. case AVMEDIA_TYPE_AUDIO:
  170. av_log(inlink->dst, AV_LOG_TRACE, " NB_SAMPLES:%"PRId64" NB_CONSUMED_SAMPLES:%"PRId64,
  171. (int64_t)setpts->var_values[VAR_NB_SAMPLES],
  172. (int64_t)setpts->var_values[VAR_NB_CONSUMED_SAMPLES]);
  173. break;
  174. }
  175. av_log(inlink->dst, AV_LOG_TRACE, " -> PTS:%s T:%f\n", d2istr(d), TS2T(d, inlink->time_base));
  176. if (inlink->type == AVMEDIA_TYPE_VIDEO) {
  177. setpts->var_values[VAR_N] += 1.0;
  178. } else {
  179. setpts->var_values[VAR_N] += frame->nb_samples;
  180. }
  181. setpts->var_values[VAR_PREV_INPTS ] = TS2D(in_pts);
  182. setpts->var_values[VAR_PREV_INT ] = TS2T(in_pts, inlink->time_base);
  183. setpts->var_values[VAR_PREV_OUTPTS] = TS2D(frame->pts);
  184. setpts->var_values[VAR_PREV_OUTT] = TS2T(frame->pts, inlink->time_base);
  185. if (setpts->type == AVMEDIA_TYPE_AUDIO) {
  186. setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += frame->nb_samples;
  187. }
  188. return ff_filter_frame(inlink->dst->outputs[0], frame);
  189. }
  190. static av_cold void uninit(AVFilterContext *ctx)
  191. {
  192. SetPTSContext *setpts = ctx->priv;
  193. av_expr_free(setpts->expr);
  194. setpts->expr = NULL;
  195. }
  196. #define OFFSET(x) offsetof(SetPTSContext, x)
  197. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  198. static const AVOption options[] = {
  199. { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = FLAGS },
  200. { NULL }
  201. };
  202. #if CONFIG_SETPTS_FILTER
  203. #define setpts_options options
  204. AVFILTER_DEFINE_CLASS(setpts);
  205. static const AVFilterPad avfilter_vf_setpts_inputs[] = {
  206. {
  207. .name = "default",
  208. .type = AVMEDIA_TYPE_VIDEO,
  209. .config_props = config_input,
  210. .filter_frame = filter_frame,
  211. },
  212. { NULL }
  213. };
  214. static const AVFilterPad avfilter_vf_setpts_outputs[] = {
  215. {
  216. .name = "default",
  217. .type = AVMEDIA_TYPE_VIDEO,
  218. },
  219. { NULL }
  220. };
  221. AVFilter ff_vf_setpts = {
  222. .name = "setpts",
  223. .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
  224. .init = init,
  225. .uninit = uninit,
  226. .priv_size = sizeof(SetPTSContext),
  227. .priv_class = &setpts_class,
  228. .inputs = avfilter_vf_setpts_inputs,
  229. .outputs = avfilter_vf_setpts_outputs,
  230. };
  231. #endif /* CONFIG_SETPTS_FILTER */
  232. #if CONFIG_ASETPTS_FILTER
  233. #define asetpts_options options
  234. AVFILTER_DEFINE_CLASS(asetpts);
  235. static const AVFilterPad asetpts_inputs[] = {
  236. {
  237. .name = "default",
  238. .type = AVMEDIA_TYPE_AUDIO,
  239. .config_props = config_input,
  240. .filter_frame = filter_frame,
  241. },
  242. { NULL }
  243. };
  244. static const AVFilterPad asetpts_outputs[] = {
  245. {
  246. .name = "default",
  247. .type = AVMEDIA_TYPE_AUDIO,
  248. },
  249. { NULL }
  250. };
  251. AVFilter ff_af_asetpts = {
  252. .name = "asetpts",
  253. .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
  254. .init = init,
  255. .uninit = uninit,
  256. .priv_size = sizeof(SetPTSContext),
  257. .priv_class = &asetpts_class,
  258. .inputs = asetpts_inputs,
  259. .outputs = asetpts_outputs,
  260. };
  261. #endif /* CONFIG_ASETPTS_FILTER */