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.

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