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.

593 lines
23KB

  1. /*
  2. * Copyright (C) 2012 Mark Himsley
  3. *
  4. * get_scene_score() Copyright (c) 2011 Stefano Sabatini
  5. * taken from libavfilter/vf_select.c
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * filter for upsampling or downsampling a progressive source
  26. */
  27. #define DEBUG
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/internal.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/pixdesc.h"
  33. #include "libavutil/pixelutils.h"
  34. #include "avfilter.h"
  35. #include "internal.h"
  36. #include "video.h"
  37. #define N_SRCE 3
  38. typedef struct FrameRateContext {
  39. const AVClass *class;
  40. // parameters
  41. AVRational dest_frame_rate; ///< output frames per second
  42. int flags; ///< flags affecting frame rate conversion algorithm
  43. double scene_score; ///< score that denotes a scene change has happened
  44. int interp_start; ///< start of range to apply linear interpolation
  45. int interp_end; ///< end of range to apply linear interpolation
  46. int line_size[4]; ///< bytes of pixel data per line for each plane
  47. int vsub;
  48. int frst, next, prev, crnt, last;
  49. int pending_srce_frames; ///< how many input frames are still waiting to be processed
  50. int flush; ///< are we flushing final frames
  51. int pending_end_frame; ///< flag indicating we are waiting to call filter_frame()
  52. AVRational srce_time_base; ///< timebase of source
  53. AVRational dest_time_base; ///< timebase of destination
  54. int32_t dest_frame_num;
  55. int64_t last_dest_frame_pts; ///< pts of the last frame output
  56. int64_t average_srce_pts_dest_delta;///< average input pts delta converted from input rate to output rate
  57. int64_t average_dest_pts_delta; ///< calculated average output pts delta
  58. av_pixelutils_sad_fn sad; ///< Sum of the absolute difference function (scene detect only)
  59. double prev_mafd; ///< previous MAFD (scene detect only)
  60. AVFrame *srce[N_SRCE]; ///< buffered source frames
  61. int64_t srce_pts_dest[N_SRCE]; ///< pts for source frames scaled to output timebase
  62. int64_t pts; ///< pts of frame we are working on
  63. } FrameRateContext;
  64. #define OFFSET(x) offsetof(FrameRateContext, x)
  65. #define V AV_OPT_FLAG_VIDEO_PARAM
  66. #define F AV_OPT_FLAG_FILTERING_PARAM
  67. #define FRAMERATE_FLAG_SCD 01
  68. static const AVOption framerate_options[] = {
  69. {"fps", "required output frames per second rate", OFFSET(dest_frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="50"}, 0, INT_MAX, V|F },
  70. {"interp_start", "point to start linear interpolation", OFFSET(interp_start), AV_OPT_TYPE_INT, {.i64=15}, 0, 255, V|F },
  71. {"interp_end", "point to end linear interpolation", OFFSET(interp_end), AV_OPT_TYPE_INT, {.i64=240}, 0, 255, V|F },
  72. {"scene", "scene change level", OFFSET(scene_score), AV_OPT_TYPE_DOUBLE, {.dbl=7.0}, 0, INT_MAX, V|F },
  73. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64=1}, 0, INT_MAX, V|F, "flags" },
  74. {"scene_change_detect", "enable scene change detection", 0, AV_OPT_TYPE_CONST, {.i64=FRAMERATE_FLAG_SCD}, INT_MIN, INT_MAX, V|F, "flags" },
  75. {"scd", "enable scene change detection", 0, AV_OPT_TYPE_CONST, {.i64=FRAMERATE_FLAG_SCD}, INT_MIN, INT_MAX, V|F, "flags" },
  76. {NULL}
  77. };
  78. AVFILTER_DEFINE_CLASS(framerate);
  79. static void next_source(AVFilterContext *ctx)
  80. {
  81. FrameRateContext *s = ctx->priv;
  82. int i;
  83. ff_dlog(ctx, "next_source()\n");
  84. if (s->srce[s->last] && s->srce[s->last] != s->srce[s->last-1]) {
  85. ff_dlog(ctx, "next_source() unlink %d\n", s->last);
  86. av_frame_free(&s->srce[s->last]);
  87. }
  88. for (i = s->last; i > s->frst; i--) {
  89. ff_dlog(ctx, "next_source() copy %d to %d\n", i - 1, i);
  90. s->srce[i] = s->srce[i - 1];
  91. }
  92. ff_dlog(ctx, "next_source() make %d null\n", s->frst);
  93. s->srce[s->frst] = NULL;
  94. }
  95. static double get_scene_score(AVFilterContext *ctx, AVFrame *crnt, AVFrame *next)
  96. {
  97. FrameRateContext *s = ctx->priv;
  98. double ret = 0;
  99. ff_dlog(ctx, "get_scene_score()\n");
  100. if (crnt &&
  101. crnt->height == next->height &&
  102. crnt->width == next->width) {
  103. int x, y;
  104. int64_t sad;
  105. double mafd, diff;
  106. uint8_t *p1 = crnt->data[0];
  107. uint8_t *p2 = next->data[0];
  108. const int p1_linesize = crnt->linesize[0];
  109. const int p2_linesize = next->linesize[0];
  110. ff_dlog(ctx, "get_scene_score() process\n");
  111. for (sad = y = 0; y < crnt->height; y += 8) {
  112. for (x = 0; x < p1_linesize; x += 8) {
  113. sad += s->sad(p1 + y * p1_linesize + x,
  114. p1_linesize,
  115. p2 + y * p2_linesize + x,
  116. p2_linesize);
  117. }
  118. }
  119. emms_c();
  120. mafd = sad / (crnt->height * crnt->width * 3);
  121. diff = fabs(mafd - s->prev_mafd);
  122. ret = av_clipf(FFMIN(mafd, diff), 0, 100.0);
  123. s->prev_mafd = mafd;
  124. }
  125. ff_dlog(ctx, "get_scene_score() result is:%f\n", ret);
  126. return ret;
  127. }
  128. static int process_work_frame(AVFilterContext *ctx, int stop)
  129. {
  130. FrameRateContext *s = ctx->priv;
  131. AVFilterLink *outlink = ctx->outputs[0];
  132. int64_t work_next_pts;
  133. AVFrame *copy_src1, *copy_src2, *work;
  134. int interpolate;
  135. ff_dlog(ctx, "process_work_frame()\n");
  136. ff_dlog(ctx, "process_work_frame() pending_input_frames %d\n", s->pending_srce_frames);
  137. if (s->srce[s->prev]) ff_dlog(ctx, "process_work_frame() srce prev pts:%"PRId64"\n", s->srce[s->prev]->pts);
  138. if (s->srce[s->crnt]) ff_dlog(ctx, "process_work_frame() srce crnt pts:%"PRId64"\n", s->srce[s->crnt]->pts);
  139. if (s->srce[s->next]) ff_dlog(ctx, "process_work_frame() srce next pts:%"PRId64"\n", s->srce[s->next]->pts);
  140. if (!s->srce[s->crnt]) {
  141. // the filter cannot do anything
  142. ff_dlog(ctx, "process_work_frame() no current frame cached: move on to next frame, do not output a frame\n");
  143. next_source(ctx);
  144. return 0;
  145. }
  146. work_next_pts = s->pts + s->average_dest_pts_delta;
  147. ff_dlog(ctx, "process_work_frame() work crnt pts:%"PRId64"\n", s->pts);
  148. ff_dlog(ctx, "process_work_frame() work next pts:%"PRId64"\n", work_next_pts);
  149. if (s->srce[s->prev])
  150. ff_dlog(ctx, "process_work_frame() srce prev pts:%"PRId64" at dest time base:%u/%u\n",
  151. s->srce_pts_dest[s->prev], s->dest_time_base.num, s->dest_time_base.den);
  152. if (s->srce[s->crnt])
  153. ff_dlog(ctx, "process_work_frame() srce crnt pts:%"PRId64" at dest time base:%u/%u\n",
  154. s->srce_pts_dest[s->crnt], s->dest_time_base.num, s->dest_time_base.den);
  155. if (s->srce[s->next])
  156. ff_dlog(ctx, "process_work_frame() srce next pts:%"PRId64" at dest time base:%u/%u\n",
  157. s->srce_pts_dest[s->next], s->dest_time_base.num, s->dest_time_base.den);
  158. av_assert0(s->srce[s->next]);
  159. // should filter be skipping input frame (output frame rate is lower than input frame rate)
  160. if (!s->flush && s->pts >= s->srce_pts_dest[s->next]) {
  161. ff_dlog(ctx, "process_work_frame() work crnt pts >= srce next pts: SKIP FRAME, move on to next frame, do not output a frame\n");
  162. next_source(ctx);
  163. s->pending_srce_frames--;
  164. return 0;
  165. }
  166. // calculate interpolation
  167. interpolate = (int) ((s->pts - s->srce_pts_dest[s->crnt]) * 256.0 / s->average_srce_pts_dest_delta);
  168. ff_dlog(ctx, "process_work_frame() interpolate:%d/256\n", interpolate);
  169. copy_src1 = s->srce[s->crnt];
  170. if (interpolate > s->interp_end) {
  171. ff_dlog(ctx, "process_work_frame() source is:NEXT\n");
  172. copy_src1 = s->srce[s->next];
  173. }
  174. if (s->srce[s->prev] && interpolate < -s->interp_end) {
  175. ff_dlog(ctx, "process_work_frame() source is:PREV\n");
  176. copy_src1 = s->srce[s->prev];
  177. }
  178. // decide whether to blend two frames
  179. if ((interpolate >= s->interp_start && interpolate <= s->interp_end) || (interpolate <= -s->interp_start && interpolate >= -s->interp_end)) {
  180. double interpolate_scene_score = 0;
  181. if (interpolate > 0) {
  182. ff_dlog(ctx, "process_work_frame() interpolate source is:NEXT\n");
  183. copy_src2 = s->srce[s->next];
  184. } else {
  185. ff_dlog(ctx, "process_work_frame() interpolate source is:PREV\n");
  186. copy_src2 = s->srce[s->prev];
  187. }
  188. if ((s->flags & FRAMERATE_FLAG_SCD) && copy_src2) {
  189. interpolate_scene_score = get_scene_score(ctx, copy_src1, copy_src2);
  190. ff_dlog(ctx, "process_work_frame() interpolate scene score:%f\n", interpolate_scene_score);
  191. }
  192. // decide if the shot-change detection allows us to blend two frames
  193. if (interpolate_scene_score < s->scene_score && copy_src2) {
  194. uint16_t src2_factor = abs(interpolate);
  195. uint16_t src1_factor = 256 - src2_factor;
  196. int plane, line, pixel;
  197. // get work-space for output frame
  198. work = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  199. if (!work)
  200. return AVERROR(ENOMEM);
  201. av_frame_copy_props(work, s->srce[s->crnt]);
  202. ff_dlog(ctx, "process_work_frame() INTERPOLATE to create work frame\n");
  203. for (plane = 0; plane < 4 && copy_src1->data[plane] && copy_src2->data[plane]; plane++) {
  204. int cpy_line_width = s->line_size[plane];
  205. uint8_t *cpy_src1_data = copy_src1->data[plane];
  206. int cpy_src1_line_size = copy_src1->linesize[plane];
  207. uint8_t *cpy_src2_data = copy_src2->data[plane];
  208. int cpy_src2_line_size = copy_src2->linesize[plane];
  209. int cpy_src_h = (plane > 0 && plane < 3) ? (copy_src1->height >> s->vsub) : (copy_src1->height);
  210. uint8_t *cpy_dst_data = work->data[plane];
  211. int cpy_dst_line_size = work->linesize[plane];
  212. if (plane <1 || plane >2) {
  213. // luma or alpha
  214. for (line = 0; line < cpy_src_h; line++) {
  215. for (pixel = 0; pixel < cpy_line_width; pixel++) {
  216. // integer version of (src1 * src1_factor) + (src2 + src2_factor) + 0.5
  217. // 0.5 is for rounding
  218. // 128 is the integer representation of 0.5 << 8
  219. cpy_dst_data[pixel] = ((cpy_src1_data[pixel] * src1_factor) + (cpy_src2_data[pixel] * src2_factor) + 128) >> 8;
  220. }
  221. cpy_src1_data += cpy_src1_line_size;
  222. cpy_src2_data += cpy_src2_line_size;
  223. cpy_dst_data += cpy_dst_line_size;
  224. }
  225. } else {
  226. // chroma
  227. for (line = 0; line < cpy_src_h; line++) {
  228. for (pixel = 0; pixel < cpy_line_width; pixel++) {
  229. // as above
  230. // because U and V are based around 128 we have to subtract 128 from the components.
  231. // 32896 is the integer representation of 128.5 << 8
  232. cpy_dst_data[pixel] = (((cpy_src1_data[pixel] - 128) * src1_factor) + ((cpy_src2_data[pixel] - 128) * src2_factor) + 32896) >> 8;
  233. }
  234. cpy_src1_data += cpy_src1_line_size;
  235. cpy_src2_data += cpy_src2_line_size;
  236. cpy_dst_data += cpy_dst_line_size;
  237. }
  238. }
  239. }
  240. goto copy_done;
  241. }
  242. else {
  243. ff_dlog(ctx, "process_work_frame() CUT - DON'T INTERPOLATE\n");
  244. }
  245. }
  246. ff_dlog(ctx, "process_work_frame() COPY to the work frame\n");
  247. // copy the frame we decided is our base source
  248. work = av_frame_clone(copy_src1);
  249. if (!work)
  250. return AVERROR(ENOMEM);
  251. copy_done:
  252. work->pts = s->pts;
  253. // should filter be re-using input frame (output frame rate is higher than input frame rate)
  254. if (!s->flush && (work_next_pts + s->average_dest_pts_delta) < (s->srce_pts_dest[s->crnt] + s->average_srce_pts_dest_delta)) {
  255. ff_dlog(ctx, "process_work_frame() REPEAT FRAME\n");
  256. } else {
  257. ff_dlog(ctx, "process_work_frame() CONSUME FRAME, move to next frame\n");
  258. s->pending_srce_frames--;
  259. next_source(ctx);
  260. }
  261. ff_dlog(ctx, "process_work_frame() output a frame\n");
  262. s->dest_frame_num++;
  263. if (stop)
  264. s->pending_end_frame = 0;
  265. s->last_dest_frame_pts = work->pts;
  266. return ff_filter_frame(ctx->outputs[0], work);
  267. }
  268. static void set_srce_frame_dest_pts(AVFilterContext *ctx)
  269. {
  270. FrameRateContext *s = ctx->priv;
  271. ff_dlog(ctx, "set_srce_frame_output_pts()\n");
  272. // scale the input pts from the timebase difference between input and output
  273. if (s->srce[s->prev])
  274. s->srce_pts_dest[s->prev] = av_rescale_q(s->srce[s->prev]->pts, s->srce_time_base, s->dest_time_base);
  275. if (s->srce[s->crnt])
  276. s->srce_pts_dest[s->crnt] = av_rescale_q(s->srce[s->crnt]->pts, s->srce_time_base, s->dest_time_base);
  277. if (s->srce[s->next])
  278. s->srce_pts_dest[s->next] = av_rescale_q(s->srce[s->next]->pts, s->srce_time_base, s->dest_time_base);
  279. }
  280. static void set_work_frame_pts(AVFilterContext *ctx)
  281. {
  282. FrameRateContext *s = ctx->priv;
  283. int64_t pts, average_srce_pts_delta = 0;
  284. ff_dlog(ctx, "set_work_frame_pts()\n");
  285. av_assert0(s->srce[s->next]);
  286. av_assert0(s->srce[s->crnt]);
  287. ff_dlog(ctx, "set_work_frame_pts() srce crnt pts:%"PRId64"\n", s->srce[s->crnt]->pts);
  288. ff_dlog(ctx, "set_work_frame_pts() srce next pts:%"PRId64"\n", s->srce[s->next]->pts);
  289. if (s->srce[s->prev])
  290. ff_dlog(ctx, "set_work_frame_pts() srce prev pts:%"PRId64"\n", s->srce[s->prev]->pts);
  291. average_srce_pts_delta = s->average_srce_pts_dest_delta;
  292. ff_dlog(ctx, "set_work_frame_pts() initial average srce pts:%"PRId64"\n", average_srce_pts_delta);
  293. // calculate the PTS delta
  294. if ((pts = (s->srce[s->next]->pts - s->srce[s->crnt]->pts))) {
  295. average_srce_pts_delta = average_srce_pts_delta?((average_srce_pts_delta+pts)>>1):pts;
  296. } else if (s->srce[s->prev] && (pts = (s->srce[s->crnt]->pts - s->srce[s->prev]->pts))) {
  297. average_srce_pts_delta = average_srce_pts_delta?((average_srce_pts_delta+pts)>>1):pts;
  298. }
  299. s->average_srce_pts_dest_delta = av_rescale_q(average_srce_pts_delta, s->srce_time_base, s->dest_time_base);
  300. ff_dlog(ctx, "set_work_frame_pts() average srce pts:%"PRId64"\n", average_srce_pts_delta);
  301. ff_dlog(ctx, "set_work_frame_pts() average srce pts:%"PRId64" at dest time base:%u/%u\n",
  302. s->average_srce_pts_dest_delta, s->dest_time_base.num, s->dest_time_base.den);
  303. set_srce_frame_dest_pts(ctx);
  304. if (ctx->inputs[0] && !s->average_dest_pts_delta) {
  305. int64_t d = av_q2d(av_inv_q(av_mul_q(s->srce_time_base, s->dest_frame_rate)));
  306. if (d == 0) { // FIXME
  307. av_log(ctx, AV_LOG_WARNING, "Buggy path reached, use settb filter before this filter!\n");
  308. d = av_q2d(av_mul_q(ctx->inputs[0]->time_base, s->dest_frame_rate));
  309. }
  310. s->average_dest_pts_delta = av_rescale_q(d, s->srce_time_base, s->dest_time_base);
  311. ff_dlog(ctx, "set_frame_pts() average output pts from input timebase\n");
  312. ff_dlog(ctx, "set_work_frame_pts() average dest pts delta:%"PRId64"\n", s->average_dest_pts_delta);
  313. }
  314. if (!s->dest_frame_num) {
  315. s->pts = s->last_dest_frame_pts = s->srce_pts_dest[s->crnt];
  316. } else {
  317. s->pts = s->last_dest_frame_pts + s->average_dest_pts_delta;
  318. }
  319. ff_dlog(ctx, "set_work_frame_pts() calculated pts:%"PRId64" at dest time base:%u/%u\n",
  320. s->pts, s->dest_time_base.num, s->dest_time_base.den);
  321. }
  322. static av_cold int init(AVFilterContext *ctx)
  323. {
  324. FrameRateContext *s = ctx->priv;
  325. s->dest_frame_num = 0;
  326. s->crnt = (N_SRCE)>>1;
  327. s->last = N_SRCE - 1;
  328. s->next = s->crnt - 1;
  329. s->prev = s->crnt + 1;
  330. return 0;
  331. }
  332. static av_cold void uninit(AVFilterContext *ctx)
  333. {
  334. FrameRateContext *s = ctx->priv;
  335. int i;
  336. for (i = s->frst + 1; i > s->last; i++) {
  337. if (s->srce[i] && (s->srce[i] != s->srce[i + 1]))
  338. av_frame_free(&s->srce[i]);
  339. }
  340. av_frame_free(&s->srce[s->last]);
  341. }
  342. static int query_formats(AVFilterContext *ctx)
  343. {
  344. static const enum AVPixelFormat pix_fmts[] = {
  345. AV_PIX_FMT_YUV410P,
  346. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUVJ411P,
  347. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
  348. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
  349. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
  350. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  351. AV_PIX_FMT_NONE
  352. };
  353. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  354. if (!fmts_list)
  355. return AVERROR(ENOMEM);
  356. return ff_set_common_formats(ctx, fmts_list);
  357. }
  358. static int config_input(AVFilterLink *inlink)
  359. {
  360. AVFilterContext *ctx = inlink->dst;
  361. FrameRateContext *s = ctx->priv;
  362. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  363. int plane;
  364. for (plane = 0; plane < 4; plane++) {
  365. s->line_size[plane] = av_image_get_linesize(inlink->format, inlink->w,
  366. plane);
  367. }
  368. s->vsub = pix_desc->log2_chroma_h;
  369. s->sad = av_pixelutils_get_sad_fn(3, 3, 2, s); // 8x8 both sources aligned
  370. if (!s->sad)
  371. return AVERROR(EINVAL);
  372. s->srce_time_base = inlink->time_base;
  373. return 0;
  374. }
  375. static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
  376. {
  377. AVFilterContext *ctx = inlink->dst;
  378. FrameRateContext *s = ctx->priv;
  379. // we have one new frame
  380. s->pending_srce_frames++;
  381. if (inpicref->interlaced_frame)
  382. av_log(ctx, AV_LOG_WARNING, "Interlaced frame found - the output will not be correct.\n");
  383. // store the pointer to the new frame
  384. av_frame_free(&s->srce[s->frst]);
  385. s->srce[s->frst] = inpicref;
  386. if (!s->pending_end_frame && s->srce[s->crnt]) {
  387. set_work_frame_pts(ctx);
  388. s->pending_end_frame = 1;
  389. } else {
  390. set_srce_frame_dest_pts(ctx);
  391. }
  392. return process_work_frame(ctx, 1);
  393. }
  394. static int config_output(AVFilterLink *outlink)
  395. {
  396. AVFilterContext *ctx = outlink->src;
  397. FrameRateContext *s = ctx->priv;
  398. int exact;
  399. ff_dlog(ctx, "config_output()\n");
  400. ff_dlog(ctx,
  401. "config_output() input time base:%u/%u (%f)\n",
  402. ctx->inputs[0]->time_base.num,ctx->inputs[0]->time_base.den,
  403. av_q2d(ctx->inputs[0]->time_base));
  404. // make sure timebase is small enough to hold the framerate
  405. exact = av_reduce(&s->dest_time_base.num, &s->dest_time_base.den,
  406. av_gcd((int64_t)s->srce_time_base.num * s->dest_frame_rate.num,
  407. (int64_t)s->srce_time_base.den * s->dest_frame_rate.den ),
  408. (int64_t)s->srce_time_base.den * s->dest_frame_rate.num, INT_MAX);
  409. av_log(ctx, AV_LOG_INFO,
  410. "time base:%u/%u -> %u/%u exact:%d\n",
  411. s->srce_time_base.num, s->srce_time_base.den,
  412. s->dest_time_base.num, s->dest_time_base.den, exact);
  413. if (!exact) {
  414. av_log(ctx, AV_LOG_WARNING, "Timebase conversion is not exact\n");
  415. }
  416. outlink->frame_rate = s->dest_frame_rate;
  417. outlink->time_base = s->dest_time_base;
  418. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  419. ff_dlog(ctx,
  420. "config_output() output time base:%u/%u (%f) w:%d h:%d\n",
  421. outlink->time_base.num, outlink->time_base.den,
  422. av_q2d(outlink->time_base),
  423. outlink->w, outlink->h);
  424. av_log(ctx, AV_LOG_INFO, "fps -> fps:%u/%u scene score:%f interpolate start:%d end:%d\n",
  425. s->dest_frame_rate.num, s->dest_frame_rate.den,
  426. s->scene_score, s->interp_start, s->interp_end);
  427. return 0;
  428. }
  429. static int request_frame(AVFilterLink *outlink)
  430. {
  431. AVFilterContext *ctx = outlink->src;
  432. FrameRateContext *s = ctx->priv;
  433. int val, i;
  434. ff_dlog(ctx, "request_frame()\n");
  435. // if there is no "next" frame AND we are not in flush then get one from our input filter
  436. if (!s->srce[s->frst] && !s->flush) {
  437. ff_dlog(ctx, "request_frame() call source's request_frame()\n");
  438. if ((val = ff_request_frame(outlink->src->inputs[0])) < 0) {
  439. ff_dlog(ctx, "request_frame() source's request_frame() returned error:%d\n", val);
  440. return val;
  441. }
  442. ff_dlog(ctx, "request_frame() source's request_frame() returned:%d\n", val);
  443. return 0;
  444. }
  445. ff_dlog(ctx, "request_frame() REPEAT or FLUSH\n");
  446. if (s->pending_srce_frames <= 0) {
  447. ff_dlog(ctx, "request_frame() nothing else to do, return:EOF\n");
  448. return AVERROR_EOF;
  449. }
  450. // otherwise, make brand-new frame and pass to our output filter
  451. ff_dlog(ctx, "request_frame() FLUSH\n");
  452. // back fill at end of file when source has no more frames
  453. for (i = s->last; i > s->frst; i--) {
  454. if (!s->srce[i - 1] && s->srce[i]) {
  455. ff_dlog(ctx, "request_frame() copy:%d to:%d\n", i, i - 1);
  456. s->srce[i - 1] = s->srce[i];
  457. }
  458. }
  459. set_work_frame_pts(ctx);
  460. return process_work_frame(ctx, 0);
  461. }
  462. static const AVFilterPad framerate_inputs[] = {
  463. {
  464. .name = "default",
  465. .type = AVMEDIA_TYPE_VIDEO,
  466. .config_props = config_input,
  467. .filter_frame = filter_frame,
  468. },
  469. { NULL }
  470. };
  471. static const AVFilterPad framerate_outputs[] = {
  472. {
  473. .name = "default",
  474. .type = AVMEDIA_TYPE_VIDEO,
  475. .request_frame = request_frame,
  476. .config_props = config_output,
  477. },
  478. { NULL }
  479. };
  480. AVFilter ff_vf_framerate = {
  481. .name = "framerate",
  482. .description = NULL_IF_CONFIG_SMALL("Upsamples or downsamples progressive source between specified frame rates."),
  483. .priv_size = sizeof(FrameRateContext),
  484. .priv_class = &framerate_class,
  485. .init = init,
  486. .uninit = uninit,
  487. .query_formats = query_formats,
  488. .inputs = framerate_inputs,
  489. .outputs = framerate_outputs,
  490. };