vf_framerate.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. int (*blend_frames)(AVFilterContext *ctx, float interpolate,
  64. AVFrame *copy_src1, AVFrame *copy_src2);
  65. int max;
  66. int bitdepth;
  67. AVFrame *work;
  68. } FrameRateContext;
  69. #define OFFSET(x) offsetof(FrameRateContext, x)
  70. #define V AV_OPT_FLAG_VIDEO_PARAM
  71. #define F AV_OPT_FLAG_FILTERING_PARAM
  72. #define FRAMERATE_FLAG_SCD 01
  73. static const AVOption framerate_options[] = {
  74. {"fps", "required output frames per second rate", OFFSET(dest_frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="50"}, 0, INT_MAX, V|F },
  75. {"interp_start", "point to start linear interpolation", OFFSET(interp_start), AV_OPT_TYPE_INT, {.i64=15}, 0, 255, V|F },
  76. {"interp_end", "point to end linear interpolation", OFFSET(interp_end), AV_OPT_TYPE_INT, {.i64=240}, 0, 255, V|F },
  77. {"scene", "scene change level", OFFSET(scene_score), AV_OPT_TYPE_DOUBLE, {.dbl=7.0}, 0, INT_MAX, V|F },
  78. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64=1}, 0, INT_MAX, V|F, "flags" },
  79. {"scene_change_detect", "enable scene change detection", 0, AV_OPT_TYPE_CONST, {.i64=FRAMERATE_FLAG_SCD}, INT_MIN, INT_MAX, V|F, "flags" },
  80. {"scd", "enable scene change detection", 0, AV_OPT_TYPE_CONST, {.i64=FRAMERATE_FLAG_SCD}, INT_MIN, INT_MAX, V|F, "flags" },
  81. {NULL}
  82. };
  83. AVFILTER_DEFINE_CLASS(framerate);
  84. static void next_source(AVFilterContext *ctx)
  85. {
  86. FrameRateContext *s = ctx->priv;
  87. int i;
  88. ff_dlog(ctx, "next_source()\n");
  89. if (s->srce[s->last] && s->srce[s->last] != s->srce[s->last-1]) {
  90. ff_dlog(ctx, "next_source() unlink %d\n", s->last);
  91. av_frame_free(&s->srce[s->last]);
  92. }
  93. for (i = s->last; i > s->frst; i--) {
  94. ff_dlog(ctx, "next_source() copy %d to %d\n", i - 1, i);
  95. s->srce[i] = s->srce[i - 1];
  96. }
  97. ff_dlog(ctx, "next_source() make %d null\n", s->frst);
  98. s->srce[s->frst] = NULL;
  99. }
  100. static av_always_inline int64_t sad_8x8_16(const uint16_t *src1, ptrdiff_t stride1,
  101. const uint16_t *src2, ptrdiff_t stride2)
  102. {
  103. int sum = 0;
  104. int x, y;
  105. for (y = 0; y < 8; y++) {
  106. for (x = 0; x < 8; x++)
  107. sum += FFABS(src1[x] - src2[x]);
  108. src1 += stride1;
  109. src2 += stride2;
  110. }
  111. return sum;
  112. }
  113. static double get_scene_score16(AVFilterContext *ctx, AVFrame *crnt, AVFrame *next)
  114. {
  115. FrameRateContext *s = ctx->priv;
  116. double ret = 0;
  117. ff_dlog(ctx, "get_scene_score16()\n");
  118. if (crnt &&
  119. crnt->height == next->height &&
  120. crnt->width == next->width) {
  121. int x, y;
  122. int64_t sad;
  123. double mafd, diff;
  124. const uint16_t *p1 = (const uint16_t *)crnt->data[0];
  125. const uint16_t *p2 = (const uint16_t *)next->data[0];
  126. const int p1_linesize = crnt->linesize[0] / 2;
  127. const int p2_linesize = next->linesize[0] / 2;
  128. ff_dlog(ctx, "get_scene_score16() process\n");
  129. for (sad = y = 0; y < crnt->height; y += 8) {
  130. for (x = 0; x < p1_linesize; x += 8) {
  131. sad += sad_8x8_16(p1 + y * p1_linesize + x,
  132. p1_linesize,
  133. p2 + y * p2_linesize + x,
  134. p2_linesize);
  135. }
  136. }
  137. mafd = sad / (crnt->height * crnt->width * 3);
  138. diff = fabs(mafd - s->prev_mafd);
  139. ret = av_clipf(FFMIN(mafd, diff), 0, 100.0);
  140. s->prev_mafd = mafd;
  141. }
  142. ff_dlog(ctx, "get_scene_score16() result is:%f\n", ret);
  143. return ret;
  144. }
  145. static double get_scene_score(AVFilterContext *ctx, AVFrame *crnt, AVFrame *next)
  146. {
  147. FrameRateContext *s = ctx->priv;
  148. double ret = 0;
  149. ff_dlog(ctx, "get_scene_score()\n");
  150. if (crnt &&
  151. crnt->height == next->height &&
  152. crnt->width == next->width) {
  153. int x, y;
  154. int64_t sad;
  155. double mafd, diff;
  156. uint8_t *p1 = crnt->data[0];
  157. uint8_t *p2 = next->data[0];
  158. const int p1_linesize = crnt->linesize[0];
  159. const int p2_linesize = next->linesize[0];
  160. ff_dlog(ctx, "get_scene_score() process\n");
  161. for (sad = y = 0; y < crnt->height; y += 8) {
  162. for (x = 0; x < p1_linesize; x += 8) {
  163. sad += s->sad(p1 + y * p1_linesize + x,
  164. p1_linesize,
  165. p2 + y * p2_linesize + x,
  166. p2_linesize);
  167. }
  168. }
  169. emms_c();
  170. mafd = sad / (crnt->height * crnt->width * 3);
  171. diff = fabs(mafd - s->prev_mafd);
  172. ret = av_clipf(FFMIN(mafd, diff), 0, 100.0);
  173. s->prev_mafd = mafd;
  174. }
  175. ff_dlog(ctx, "get_scene_score() result is:%f\n", ret);
  176. return ret;
  177. }
  178. static int blend_frames16(AVFilterContext *ctx, float interpolate,
  179. AVFrame *copy_src1, AVFrame *copy_src2)
  180. {
  181. FrameRateContext *s = ctx->priv;
  182. AVFilterLink *outlink = ctx->outputs[0];
  183. double interpolate_scene_score = 0;
  184. if ((s->flags & FRAMERATE_FLAG_SCD) && copy_src2) {
  185. interpolate_scene_score = get_scene_score16(ctx, copy_src1, copy_src2);
  186. ff_dlog(ctx, "blend_frames16() interpolate scene score:%f\n", interpolate_scene_score);
  187. }
  188. // decide if the shot-change detection allows us to blend two frames
  189. if (interpolate_scene_score < s->scene_score && copy_src2) {
  190. uint16_t src2_factor = fabsf(interpolate) * (1 << (s->bitdepth - 8));
  191. uint16_t src1_factor = s->max - src2_factor;
  192. const int half = s->max / 2;
  193. const int uv = (s->max + 1) * half;
  194. const int shift = s->bitdepth;
  195. int plane, line, pixel;
  196. // get work-space for output frame
  197. s->work = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  198. if (!s->work)
  199. return AVERROR(ENOMEM);
  200. av_frame_copy_props(s->work, s->srce[s->crnt]);
  201. ff_dlog(ctx, "blend_frames16() INTERPOLATE to create work frame\n");
  202. for (plane = 0; plane < 4 && copy_src1->data[plane] && copy_src2->data[plane]; plane++) {
  203. int cpy_line_width = s->line_size[plane];
  204. const uint16_t *cpy_src1_data = (const uint16_t *)copy_src1->data[plane];
  205. int cpy_src1_line_size = copy_src1->linesize[plane] / 2;
  206. const uint16_t *cpy_src2_data = (const uint16_t *)copy_src2->data[plane];
  207. int cpy_src2_line_size = copy_src2->linesize[plane] / 2;
  208. int cpy_src_h = (plane > 0 && plane < 3) ? (copy_src1->height >> s->vsub) : (copy_src1->height);
  209. uint16_t *cpy_dst_data = (uint16_t *)s->work->data[plane];
  210. int cpy_dst_line_size = s->work->linesize[plane] / 2;
  211. if (plane <1 || plane >2) {
  212. // luma or alpha
  213. for (line = 0; line < cpy_src_h; line++) {
  214. for (pixel = 0; pixel < cpy_line_width; pixel++)
  215. cpy_dst_data[pixel] = ((cpy_src1_data[pixel] * src1_factor) + (cpy_src2_data[pixel] * src2_factor) + half) >> shift;
  216. cpy_src1_data += cpy_src1_line_size;
  217. cpy_src2_data += cpy_src2_line_size;
  218. cpy_dst_data += cpy_dst_line_size;
  219. }
  220. } else {
  221. // chroma
  222. for (line = 0; line < cpy_src_h; line++) {
  223. for (pixel = 0; pixel < cpy_line_width; pixel++) {
  224. cpy_dst_data[pixel] = (((cpy_src1_data[pixel] - half) * src1_factor) + ((cpy_src2_data[pixel] - half) * src2_factor) + uv) >> shift;
  225. }
  226. cpy_src1_data += cpy_src1_line_size;
  227. cpy_src2_data += cpy_src2_line_size;
  228. cpy_dst_data += cpy_dst_line_size;
  229. }
  230. }
  231. }
  232. return 1;
  233. }
  234. return 0;
  235. }
  236. static int blend_frames8(AVFilterContext *ctx, float interpolate,
  237. AVFrame *copy_src1, AVFrame *copy_src2)
  238. {
  239. FrameRateContext *s = ctx->priv;
  240. AVFilterLink *outlink = ctx->outputs[0];
  241. double interpolate_scene_score = 0;
  242. if ((s->flags & FRAMERATE_FLAG_SCD) && copy_src2) {
  243. interpolate_scene_score = get_scene_score(ctx, copy_src1, copy_src2);
  244. ff_dlog(ctx, "blend_frames8() interpolate scene score:%f\n", interpolate_scene_score);
  245. }
  246. // decide if the shot-change detection allows us to blend two frames
  247. if (interpolate_scene_score < s->scene_score && copy_src2) {
  248. uint16_t src2_factor = fabsf(interpolate);
  249. uint16_t src1_factor = 256 - src2_factor;
  250. int plane, line, pixel;
  251. // get work-space for output frame
  252. s->work = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  253. if (!s->work)
  254. return AVERROR(ENOMEM);
  255. av_frame_copy_props(s->work, s->srce[s->crnt]);
  256. ff_dlog(ctx, "blend_frames8() INTERPOLATE to create work frame\n");
  257. for (plane = 0; plane < 4 && copy_src1->data[plane] && copy_src2->data[plane]; plane++) {
  258. int cpy_line_width = s->line_size[plane];
  259. uint8_t *cpy_src1_data = copy_src1->data[plane];
  260. int cpy_src1_line_size = copy_src1->linesize[plane];
  261. uint8_t *cpy_src2_data = copy_src2->data[plane];
  262. int cpy_src2_line_size = copy_src2->linesize[plane];
  263. int cpy_src_h = (plane > 0 && plane < 3) ? (copy_src1->height >> s->vsub) : (copy_src1->height);
  264. uint8_t *cpy_dst_data = s->work->data[plane];
  265. int cpy_dst_line_size = s->work->linesize[plane];
  266. if (plane <1 || plane >2) {
  267. // luma or alpha
  268. for (line = 0; line < cpy_src_h; line++) {
  269. for (pixel = 0; pixel < cpy_line_width; pixel++) {
  270. // integer version of (src1 * src1_factor) + (src2 + src2_factor) + 0.5
  271. // 0.5 is for rounding
  272. // 128 is the integer representation of 0.5 << 8
  273. cpy_dst_data[pixel] = ((cpy_src1_data[pixel] * src1_factor) + (cpy_src2_data[pixel] * src2_factor) + 128) >> 8;
  274. }
  275. cpy_src1_data += cpy_src1_line_size;
  276. cpy_src2_data += cpy_src2_line_size;
  277. cpy_dst_data += cpy_dst_line_size;
  278. }
  279. } else {
  280. // chroma
  281. for (line = 0; line < cpy_src_h; line++) {
  282. for (pixel = 0; pixel < cpy_line_width; pixel++) {
  283. // as above
  284. // because U and V are based around 128 we have to subtract 128 from the components.
  285. // 32896 is the integer representation of 128.5 << 8
  286. cpy_dst_data[pixel] = (((cpy_src1_data[pixel] - 128) * src1_factor) + ((cpy_src2_data[pixel] - 128) * src2_factor) + 32896) >> 8;
  287. }
  288. cpy_src1_data += cpy_src1_line_size;
  289. cpy_src2_data += cpy_src2_line_size;
  290. cpy_dst_data += cpy_dst_line_size;
  291. }
  292. }
  293. }
  294. return 1;
  295. }
  296. return 0;
  297. }
  298. static int process_work_frame(AVFilterContext *ctx, int stop)
  299. {
  300. FrameRateContext *s = ctx->priv;
  301. int64_t work_next_pts;
  302. AVFrame *copy_src1;
  303. float interpolate;
  304. ff_dlog(ctx, "process_work_frame()\n");
  305. ff_dlog(ctx, "process_work_frame() pending_input_frames %d\n", s->pending_srce_frames);
  306. if (s->srce[s->prev]) ff_dlog(ctx, "process_work_frame() srce prev pts:%"PRId64"\n", s->srce[s->prev]->pts);
  307. if (s->srce[s->crnt]) ff_dlog(ctx, "process_work_frame() srce crnt pts:%"PRId64"\n", s->srce[s->crnt]->pts);
  308. if (s->srce[s->next]) ff_dlog(ctx, "process_work_frame() srce next pts:%"PRId64"\n", s->srce[s->next]->pts);
  309. if (!s->srce[s->crnt]) {
  310. // the filter cannot do anything
  311. ff_dlog(ctx, "process_work_frame() no current frame cached: move on to next frame, do not output a frame\n");
  312. next_source(ctx);
  313. return 0;
  314. }
  315. work_next_pts = s->pts + s->average_dest_pts_delta;
  316. ff_dlog(ctx, "process_work_frame() work crnt pts:%"PRId64"\n", s->pts);
  317. ff_dlog(ctx, "process_work_frame() work next pts:%"PRId64"\n", work_next_pts);
  318. if (s->srce[s->prev])
  319. ff_dlog(ctx, "process_work_frame() srce prev pts:%"PRId64" at dest time base:%u/%u\n",
  320. s->srce_pts_dest[s->prev], s->dest_time_base.num, s->dest_time_base.den);
  321. if (s->srce[s->crnt])
  322. ff_dlog(ctx, "process_work_frame() srce crnt pts:%"PRId64" at dest time base:%u/%u\n",
  323. s->srce_pts_dest[s->crnt], s->dest_time_base.num, s->dest_time_base.den);
  324. if (s->srce[s->next])
  325. ff_dlog(ctx, "process_work_frame() srce next pts:%"PRId64" at dest time base:%u/%u\n",
  326. s->srce_pts_dest[s->next], s->dest_time_base.num, s->dest_time_base.den);
  327. av_assert0(s->srce[s->next]);
  328. // should filter be skipping input frame (output frame rate is lower than input frame rate)
  329. if (!s->flush && s->pts >= s->srce_pts_dest[s->next]) {
  330. 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");
  331. next_source(ctx);
  332. s->pending_srce_frames--;
  333. return 0;
  334. }
  335. // calculate interpolation
  336. interpolate = ((s->pts - s->srce_pts_dest[s->crnt]) * 256.0 / s->average_srce_pts_dest_delta);
  337. ff_dlog(ctx, "process_work_frame() interpolate:%f/256\n", interpolate);
  338. copy_src1 = s->srce[s->crnt];
  339. if (interpolate > s->interp_end) {
  340. ff_dlog(ctx, "process_work_frame() source is:NEXT\n");
  341. copy_src1 = s->srce[s->next];
  342. }
  343. if (s->srce[s->prev] && interpolate < -s->interp_end) {
  344. ff_dlog(ctx, "process_work_frame() source is:PREV\n");
  345. copy_src1 = s->srce[s->prev];
  346. }
  347. // decide whether to blend two frames
  348. if ((interpolate >= s->interp_start && interpolate <= s->interp_end) || (interpolate <= -s->interp_start && interpolate >= -s->interp_end)) {
  349. AVFrame *copy_src2;
  350. if (interpolate > 0) {
  351. ff_dlog(ctx, "process_work_frame() interpolate source is:NEXT\n");
  352. copy_src2 = s->srce[s->next];
  353. } else {
  354. ff_dlog(ctx, "process_work_frame() interpolate source is:PREV\n");
  355. copy_src2 = s->srce[s->prev];
  356. }
  357. if (s->blend_frames(ctx, interpolate, copy_src1, copy_src2))
  358. goto copy_done;
  359. else
  360. ff_dlog(ctx, "process_work_frame() CUT - DON'T INTERPOLATE\n");
  361. }
  362. ff_dlog(ctx, "process_work_frame() COPY to the work frame\n");
  363. // copy the frame we decided is our base source
  364. s->work = av_frame_clone(copy_src1);
  365. if (!s->work)
  366. return AVERROR(ENOMEM);
  367. copy_done:
  368. s->work->pts = s->pts;
  369. // should filter be re-using input frame (output frame rate is higher than input frame rate)
  370. if (!s->flush && (work_next_pts + s->average_dest_pts_delta) < (s->srce_pts_dest[s->crnt] + s->average_srce_pts_dest_delta)) {
  371. ff_dlog(ctx, "process_work_frame() REPEAT FRAME\n");
  372. } else {
  373. ff_dlog(ctx, "process_work_frame() CONSUME FRAME, move to next frame\n");
  374. s->pending_srce_frames--;
  375. next_source(ctx);
  376. }
  377. ff_dlog(ctx, "process_work_frame() output a frame\n");
  378. s->dest_frame_num++;
  379. if (stop)
  380. s->pending_end_frame = 0;
  381. s->last_dest_frame_pts = s->work->pts;
  382. return ff_filter_frame(ctx->outputs[0], s->work);
  383. }
  384. static void set_srce_frame_dest_pts(AVFilterContext *ctx)
  385. {
  386. FrameRateContext *s = ctx->priv;
  387. ff_dlog(ctx, "set_srce_frame_output_pts()\n");
  388. // scale the input pts from the timebase difference between input and output
  389. if (s->srce[s->prev])
  390. s->srce_pts_dest[s->prev] = av_rescale_q(s->srce[s->prev]->pts, s->srce_time_base, s->dest_time_base);
  391. if (s->srce[s->crnt])
  392. s->srce_pts_dest[s->crnt] = av_rescale_q(s->srce[s->crnt]->pts, s->srce_time_base, s->dest_time_base);
  393. if (s->srce[s->next])
  394. s->srce_pts_dest[s->next] = av_rescale_q(s->srce[s->next]->pts, s->srce_time_base, s->dest_time_base);
  395. }
  396. static void set_work_frame_pts(AVFilterContext *ctx)
  397. {
  398. FrameRateContext *s = ctx->priv;
  399. int64_t pts, average_srce_pts_delta = 0;
  400. ff_dlog(ctx, "set_work_frame_pts()\n");
  401. av_assert0(s->srce[s->next]);
  402. av_assert0(s->srce[s->crnt]);
  403. ff_dlog(ctx, "set_work_frame_pts() srce crnt pts:%"PRId64"\n", s->srce[s->crnt]->pts);
  404. ff_dlog(ctx, "set_work_frame_pts() srce next pts:%"PRId64"\n", s->srce[s->next]->pts);
  405. if (s->srce[s->prev])
  406. ff_dlog(ctx, "set_work_frame_pts() srce prev pts:%"PRId64"\n", s->srce[s->prev]->pts);
  407. average_srce_pts_delta = s->average_srce_pts_dest_delta;
  408. ff_dlog(ctx, "set_work_frame_pts() initial average srce pts:%"PRId64"\n", average_srce_pts_delta);
  409. set_srce_frame_dest_pts(ctx);
  410. // calculate the PTS delta
  411. if ((pts = (s->srce_pts_dest[s->next] - s->srce_pts_dest[s->crnt]))) {
  412. average_srce_pts_delta = average_srce_pts_delta?((average_srce_pts_delta+pts)>>1):pts;
  413. } else if (s->srce[s->prev] && (pts = (s->srce_pts_dest[s->crnt] - s->srce_pts_dest[s->prev]))) {
  414. average_srce_pts_delta = average_srce_pts_delta?((average_srce_pts_delta+pts)>>1):pts;
  415. }
  416. s->average_srce_pts_dest_delta = average_srce_pts_delta;
  417. ff_dlog(ctx, "set_work_frame_pts() average srce pts:%"PRId64"\n", average_srce_pts_delta);
  418. ff_dlog(ctx, "set_work_frame_pts() average srce pts:%"PRId64" at dest time base:%u/%u\n",
  419. s->average_srce_pts_dest_delta, s->dest_time_base.num, s->dest_time_base.den);
  420. if (ctx->inputs[0] && !s->average_dest_pts_delta) {
  421. int64_t d = av_q2d(av_inv_q(av_mul_q(s->dest_time_base, s->dest_frame_rate)));
  422. s->average_dest_pts_delta = d;
  423. ff_dlog(ctx, "set_work_frame_pts() average dest pts delta:%"PRId64"\n", s->average_dest_pts_delta);
  424. }
  425. if (!s->dest_frame_num) {
  426. s->pts = s->last_dest_frame_pts = s->srce_pts_dest[s->crnt];
  427. } else {
  428. s->pts = s->last_dest_frame_pts + s->average_dest_pts_delta;
  429. }
  430. ff_dlog(ctx, "set_work_frame_pts() calculated pts:%"PRId64" at dest time base:%u/%u\n",
  431. s->pts, s->dest_time_base.num, s->dest_time_base.den);
  432. }
  433. static av_cold int init(AVFilterContext *ctx)
  434. {
  435. FrameRateContext *s = ctx->priv;
  436. s->dest_frame_num = 0;
  437. s->crnt = (N_SRCE)>>1;
  438. s->last = N_SRCE - 1;
  439. s->next = s->crnt - 1;
  440. s->prev = s->crnt + 1;
  441. return 0;
  442. }
  443. static av_cold void uninit(AVFilterContext *ctx)
  444. {
  445. FrameRateContext *s = ctx->priv;
  446. int i;
  447. for (i = s->frst + 1; i < s->last; i++) {
  448. if (s->srce[i] && (s->srce[i] != s->srce[i + 1]))
  449. av_frame_free(&s->srce[i]);
  450. }
  451. av_frame_free(&s->srce[s->last]);
  452. }
  453. static int query_formats(AVFilterContext *ctx)
  454. {
  455. static const enum AVPixelFormat pix_fmts[] = {
  456. AV_PIX_FMT_YUV410P,
  457. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUVJ411P,
  458. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
  459. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
  460. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
  461. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  462. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
  463. AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
  464. AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
  465. AV_PIX_FMT_NONE
  466. };
  467. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  468. if (!fmts_list)
  469. return AVERROR(ENOMEM);
  470. return ff_set_common_formats(ctx, fmts_list);
  471. }
  472. static int config_input(AVFilterLink *inlink)
  473. {
  474. AVFilterContext *ctx = inlink->dst;
  475. FrameRateContext *s = ctx->priv;
  476. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  477. int plane;
  478. for (plane = 0; plane < 4; plane++) {
  479. s->line_size[plane] = av_image_get_linesize(inlink->format, inlink->w,
  480. plane);
  481. }
  482. s->bitdepth = pix_desc->comp[0].depth;
  483. s->vsub = pix_desc->log2_chroma_h;
  484. s->sad = av_pixelutils_get_sad_fn(3, 3, 2, s); // 8x8 both sources aligned
  485. if (!s->sad)
  486. return AVERROR(EINVAL);
  487. s->srce_time_base = inlink->time_base;
  488. if (s->bitdepth == 8)
  489. s->blend_frames = blend_frames8;
  490. else
  491. s->blend_frames = blend_frames16;
  492. s->max = 1 << (s->bitdepth);
  493. return 0;
  494. }
  495. static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
  496. {
  497. AVFilterContext *ctx = inlink->dst;
  498. FrameRateContext *s = ctx->priv;
  499. // we have one new frame
  500. s->pending_srce_frames++;
  501. if (inpicref->interlaced_frame)
  502. av_log(ctx, AV_LOG_WARNING, "Interlaced frame found - the output will not be correct.\n");
  503. // store the pointer to the new frame
  504. av_frame_free(&s->srce[s->frst]);
  505. s->srce[s->frst] = inpicref;
  506. if (!s->pending_end_frame && s->srce[s->crnt]) {
  507. set_work_frame_pts(ctx);
  508. s->pending_end_frame = 1;
  509. } else {
  510. set_srce_frame_dest_pts(ctx);
  511. }
  512. return process_work_frame(ctx, 1);
  513. }
  514. static int config_output(AVFilterLink *outlink)
  515. {
  516. AVFilterContext *ctx = outlink->src;
  517. FrameRateContext *s = ctx->priv;
  518. int exact;
  519. ff_dlog(ctx, "config_output()\n");
  520. ff_dlog(ctx,
  521. "config_output() input time base:%u/%u (%f)\n",
  522. ctx->inputs[0]->time_base.num,ctx->inputs[0]->time_base.den,
  523. av_q2d(ctx->inputs[0]->time_base));
  524. // make sure timebase is small enough to hold the framerate
  525. exact = av_reduce(&s->dest_time_base.num, &s->dest_time_base.den,
  526. av_gcd((int64_t)s->srce_time_base.num * s->dest_frame_rate.num,
  527. (int64_t)s->srce_time_base.den * s->dest_frame_rate.den ),
  528. (int64_t)s->srce_time_base.den * s->dest_frame_rate.num, INT_MAX);
  529. av_log(ctx, AV_LOG_INFO,
  530. "time base:%u/%u -> %u/%u exact:%d\n",
  531. s->srce_time_base.num, s->srce_time_base.den,
  532. s->dest_time_base.num, s->dest_time_base.den, exact);
  533. if (!exact) {
  534. av_log(ctx, AV_LOG_WARNING, "Timebase conversion is not exact\n");
  535. }
  536. outlink->frame_rate = s->dest_frame_rate;
  537. outlink->time_base = s->dest_time_base;
  538. ff_dlog(ctx,
  539. "config_output() output time base:%u/%u (%f) w:%d h:%d\n",
  540. outlink->time_base.num, outlink->time_base.den,
  541. av_q2d(outlink->time_base),
  542. outlink->w, outlink->h);
  543. av_log(ctx, AV_LOG_INFO, "fps -> fps:%u/%u scene score:%f interpolate start:%d end:%d\n",
  544. s->dest_frame_rate.num, s->dest_frame_rate.den,
  545. s->scene_score, s->interp_start, s->interp_end);
  546. return 0;
  547. }
  548. static int request_frame(AVFilterLink *outlink)
  549. {
  550. AVFilterContext *ctx = outlink->src;
  551. FrameRateContext *s = ctx->priv;
  552. int val, i;
  553. ff_dlog(ctx, "request_frame()\n");
  554. // if there is no "next" frame AND we are not in flush then get one from our input filter
  555. if (!s->srce[s->frst] && !s->flush) {
  556. ff_dlog(ctx, "request_frame() call source's request_frame()\n");
  557. val = ff_request_frame(outlink->src->inputs[0]);
  558. if (val < 0 && (val != AVERROR_EOF)) {
  559. ff_dlog(ctx, "request_frame() source's request_frame() returned error:%d\n", val);
  560. return val;
  561. } else if (val == AVERROR_EOF) {
  562. s->flush = 1;
  563. }
  564. ff_dlog(ctx, "request_frame() source's request_frame() returned:%d\n", val);
  565. return 0;
  566. }
  567. ff_dlog(ctx, "request_frame() REPEAT or FLUSH\n");
  568. if (s->pending_srce_frames <= 0) {
  569. ff_dlog(ctx, "request_frame() nothing else to do, return:EOF\n");
  570. return AVERROR_EOF;
  571. }
  572. // otherwise, make brand-new frame and pass to our output filter
  573. ff_dlog(ctx, "request_frame() FLUSH\n");
  574. // back fill at end of file when source has no more frames
  575. for (i = s->last; i > s->frst; i--) {
  576. if (!s->srce[i - 1] && s->srce[i]) {
  577. ff_dlog(ctx, "request_frame() copy:%d to:%d\n", i, i - 1);
  578. s->srce[i - 1] = s->srce[i];
  579. }
  580. }
  581. set_work_frame_pts(ctx);
  582. return process_work_frame(ctx, 0);
  583. }
  584. static const AVFilterPad framerate_inputs[] = {
  585. {
  586. .name = "default",
  587. .type = AVMEDIA_TYPE_VIDEO,
  588. .config_props = config_input,
  589. .filter_frame = filter_frame,
  590. },
  591. { NULL }
  592. };
  593. static const AVFilterPad framerate_outputs[] = {
  594. {
  595. .name = "default",
  596. .type = AVMEDIA_TYPE_VIDEO,
  597. .request_frame = request_frame,
  598. .config_props = config_output,
  599. },
  600. { NULL }
  601. };
  602. AVFilter ff_vf_framerate = {
  603. .name = "framerate",
  604. .description = NULL_IF_CONFIG_SMALL("Upsamples or downsamples progressive source between specified frame rates."),
  605. .priv_size = sizeof(FrameRateContext),
  606. .priv_class = &framerate_class,
  607. .init = init,
  608. .uninit = uninit,
  609. .query_formats = query_formats,
  610. .inputs = framerate_inputs,
  611. .outputs = framerate_outputs,
  612. };