f_settb.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) 2010 Stefano Sabatini
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Set timebase for the output link.
  23. */
  24. #include <inttypes.h>
  25. #include <stdio.h>
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/eval.h"
  28. #include "libavutil/internal.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/rational.h"
  31. #include "avfilter.h"
  32. #include "internal.h"
  33. #include "audio.h"
  34. #include "video.h"
  35. static const char *const var_names[] = {
  36. "AVTB", /* default timebase 1/AV_TIME_BASE */
  37. "intb", /* input timebase */
  38. "sr", /* sample rate */
  39. NULL
  40. };
  41. enum var_name {
  42. VAR_AVTB,
  43. VAR_INTB,
  44. VAR_SR,
  45. VAR_VARS_NB
  46. };
  47. typedef struct {
  48. char tb_expr[256];
  49. double var_values[VAR_VARS_NB];
  50. } SetTBContext;
  51. static av_cold int init(AVFilterContext *ctx, const char *args)
  52. {
  53. SetTBContext *settb = ctx->priv;
  54. av_strlcpy(settb->tb_expr, "intb", sizeof(settb->tb_expr));
  55. if (args)
  56. sscanf(args, "%255[^:]", settb->tb_expr);
  57. return 0;
  58. }
  59. static int config_output_props(AVFilterLink *outlink)
  60. {
  61. AVFilterContext *ctx = outlink->src;
  62. SetTBContext *settb = ctx->priv;
  63. AVFilterLink *inlink = ctx->inputs[0];
  64. AVRational time_base;
  65. int ret;
  66. double res;
  67. settb->var_values[VAR_AVTB] = av_q2d(AV_TIME_BASE_Q);
  68. settb->var_values[VAR_INTB] = av_q2d(inlink->time_base);
  69. settb->var_values[VAR_SR] = inlink->sample_rate;
  70. outlink->w = inlink->w;
  71. outlink->h = inlink->h;
  72. if ((ret = av_expr_parse_and_eval(&res, settb->tb_expr, var_names, settb->var_values,
  73. NULL, NULL, NULL, NULL, NULL, 0, NULL)) < 0) {
  74. av_log(ctx, AV_LOG_ERROR, "Invalid expression '%s' for timebase.\n", settb->tb_expr);
  75. return ret;
  76. }
  77. time_base = av_d2q(res, INT_MAX);
  78. if (time_base.num <= 0 || time_base.den <= 0) {
  79. av_log(ctx, AV_LOG_ERROR,
  80. "Invalid non-positive values for the timebase num:%d or den:%d.\n",
  81. time_base.num, time_base.den);
  82. return AVERROR(EINVAL);
  83. }
  84. outlink->time_base = time_base;
  85. av_log(outlink->src, AV_LOG_VERBOSE, "tb:%d/%d -> tb:%d/%d\n",
  86. inlink ->time_base.num, inlink ->time_base.den,
  87. outlink->time_base.num, outlink->time_base.den);
  88. return 0;
  89. }
  90. static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
  91. {
  92. AVFilterContext *ctx = inlink->dst;
  93. AVFilterLink *outlink = ctx->outputs[0];
  94. if (av_cmp_q(inlink->time_base, outlink->time_base)) {
  95. int64_t orig_pts = picref->pts;
  96. picref->pts = av_rescale_q(picref->pts, inlink->time_base, outlink->time_base);
  97. av_log(ctx, AV_LOG_DEBUG, "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
  98. inlink ->time_base.num, inlink ->time_base.den, orig_pts,
  99. outlink->time_base.num, outlink->time_base.den, picref->pts);
  100. }
  101. inlink->cur_buf = NULL;
  102. return ff_start_frame(outlink, picref);
  103. }
  104. static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
  105. {
  106. AVFilterContext *ctx = inlink->dst;
  107. AVFilterLink *outlink = ctx->outputs[0];
  108. if (av_cmp_q(inlink->time_base, outlink->time_base)) {
  109. int64_t orig_pts = samplesref->pts;
  110. samplesref->pts = av_rescale_q(samplesref->pts, inlink->time_base, outlink->time_base);
  111. av_log(ctx, AV_LOG_DEBUG, "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
  112. inlink ->time_base.num, inlink ->time_base.den, orig_pts,
  113. outlink->time_base.num, outlink->time_base.den, samplesref->pts);
  114. }
  115. return ff_filter_samples(outlink, samplesref);
  116. }
  117. #if CONFIG_SETTB_FILTER
  118. AVFilter avfilter_vf_settb = {
  119. .name = "settb",
  120. .description = NULL_IF_CONFIG_SMALL("Set timebase for the video output link."),
  121. .init = init,
  122. .priv_size = sizeof(SetTBContext),
  123. .inputs = (const AVFilterPad[]) {
  124. { .name = "default",
  125. .type = AVMEDIA_TYPE_VIDEO,
  126. .get_video_buffer = ff_null_get_video_buffer,
  127. .start_frame = start_frame,
  128. .end_frame = ff_null_end_frame },
  129. { .name = NULL }
  130. },
  131. .outputs = (const AVFilterPad[]) {
  132. { .name = "default",
  133. .type = AVMEDIA_TYPE_VIDEO,
  134. .config_props = config_output_props, },
  135. { .name = NULL}
  136. },
  137. };
  138. #endif
  139. #if CONFIG_ASETTB_FILTER
  140. AVFilter avfilter_af_asettb = {
  141. .name = "asettb",
  142. .description = NULL_IF_CONFIG_SMALL("Set timebase for the audio output link."),
  143. .init = init,
  144. .priv_size = sizeof(SetTBContext),
  145. .inputs = (const AVFilterPad[]) {
  146. { .name = "default",
  147. .type = AVMEDIA_TYPE_AUDIO,
  148. .get_audio_buffer = ff_null_get_audio_buffer,
  149. .filter_samples = filter_samples, },
  150. { .name = NULL }
  151. },
  152. .outputs = (const AVFilterPad[]) {
  153. { .name = "default",
  154. .type = AVMEDIA_TYPE_AUDIO,
  155. .config_props = config_output_props, },
  156. { .name = NULL}
  157. },
  158. };
  159. #endif