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.

297 lines
9.4KB

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