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.

345 lines
11KB

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