setpts.c 9.6 KB

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