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.

270 lines
8.6KB

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