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.

743 lines
28KB

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