vf_framerate.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 "avfilter.h"
  34. #include "internal.h"
  35. #include "video.h"
  36. #include "filters.h"
  37. #include "framerate.h"
  38. #include "scene_sad.h"
  39. #define OFFSET(x) offsetof(FrameRateContext, x)
  40. #define V AV_OPT_FLAG_VIDEO_PARAM
  41. #define F AV_OPT_FLAG_FILTERING_PARAM
  42. #define FRAMERATE_FLAG_SCD 01
  43. static const AVOption framerate_options[] = {
  44. {"fps", "required output frames per second rate", OFFSET(dest_frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="50"}, 0, INT_MAX, V|F },
  45. {"interp_start", "point to start linear interpolation", OFFSET(interp_start), AV_OPT_TYPE_INT, {.i64=15}, 0, 255, V|F },
  46. {"interp_end", "point to end linear interpolation", OFFSET(interp_end), AV_OPT_TYPE_INT, {.i64=240}, 0, 255, V|F },
  47. {"scene", "scene change level", OFFSET(scene_score), AV_OPT_TYPE_DOUBLE, {.dbl=8.2}, 0, INT_MAX, V|F },
  48. {"flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64=1}, 0, INT_MAX, V|F, "flags" },
  49. {"scene_change_detect", "enable scene change detection", 0, AV_OPT_TYPE_CONST, {.i64=FRAMERATE_FLAG_SCD}, INT_MIN, INT_MAX, V|F, "flags" },
  50. {"scd", "enable scene change detection", 0, AV_OPT_TYPE_CONST, {.i64=FRAMERATE_FLAG_SCD}, INT_MIN, INT_MAX, V|F, "flags" },
  51. {NULL}
  52. };
  53. AVFILTER_DEFINE_CLASS(framerate);
  54. static double get_scene_score(AVFilterContext *ctx, AVFrame *crnt, AVFrame *next)
  55. {
  56. FrameRateContext *s = ctx->priv;
  57. double ret = 0;
  58. ff_dlog(ctx, "get_scene_score()\n");
  59. if (crnt->height == next->height &&
  60. crnt->width == next->width) {
  61. uint64_t sad;
  62. double mafd, diff;
  63. ff_dlog(ctx, "get_scene_score() process\n");
  64. s->sad(crnt->data[0], crnt->linesize[0], next->data[0], next->linesize[0], crnt->width, crnt->height, &sad);
  65. emms_c();
  66. mafd = (double)sad * 100.0 / (crnt->width * crnt->height) / (1 << s->bitdepth);
  67. diff = fabs(mafd - s->prev_mafd);
  68. ret = av_clipf(FFMIN(mafd, diff), 0, 100.0);
  69. s->prev_mafd = mafd;
  70. }
  71. ff_dlog(ctx, "get_scene_score() result is:%f\n", ret);
  72. return ret;
  73. }
  74. typedef struct ThreadData {
  75. AVFrame *copy_src1, *copy_src2;
  76. uint16_t src1_factor, src2_factor;
  77. } ThreadData;
  78. static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
  79. {
  80. FrameRateContext *s = ctx->priv;
  81. ThreadData *td = arg;
  82. uint16_t src1_factor = td->src1_factor;
  83. uint16_t src2_factor = td->src2_factor;
  84. int plane;
  85. for (plane = 0; plane < 4 && td->copy_src1->data[plane] && td->copy_src2->data[plane]; plane++) {
  86. int cpy_line_width = s->line_size[plane];
  87. uint8_t *cpy_src1_data = td->copy_src1->data[plane];
  88. int cpy_src1_line_size = td->copy_src1->linesize[plane];
  89. uint8_t *cpy_src2_data = td->copy_src2->data[plane];
  90. int cpy_src2_line_size = td->copy_src2->linesize[plane];
  91. int cpy_src_h = (plane > 0 && plane < 3) ? (td->copy_src1->height >> s->vsub) : (td->copy_src1->height);
  92. uint8_t *cpy_dst_data = s->work->data[plane];
  93. int cpy_dst_line_size = s->work->linesize[plane];
  94. const int start = (cpy_src_h * job ) / nb_jobs;
  95. const int end = (cpy_src_h * (job+1)) / nb_jobs;
  96. cpy_src1_data += start * cpy_src1_line_size;
  97. cpy_src2_data += start * cpy_src2_line_size;
  98. cpy_dst_data += start * cpy_dst_line_size;
  99. s->blend(cpy_src1_data, cpy_src1_line_size,
  100. cpy_src2_data, cpy_src2_line_size,
  101. cpy_dst_data, cpy_dst_line_size,
  102. cpy_line_width, end - start,
  103. src1_factor, src2_factor, s->blend_factor_max >> 1);
  104. }
  105. return 0;
  106. }
  107. static int blend_frames(AVFilterContext *ctx, int interpolate)
  108. {
  109. FrameRateContext *s = ctx->priv;
  110. AVFilterLink *outlink = ctx->outputs[0];
  111. double interpolate_scene_score = 0;
  112. if ((s->flags & FRAMERATE_FLAG_SCD)) {
  113. if (s->score >= 0.0)
  114. interpolate_scene_score = s->score;
  115. else
  116. interpolate_scene_score = s->score = get_scene_score(ctx, s->f0, s->f1);
  117. ff_dlog(ctx, "blend_frames() interpolate scene score:%f\n", interpolate_scene_score);
  118. }
  119. // decide if the shot-change detection allows us to blend two frames
  120. if (interpolate_scene_score < s->scene_score) {
  121. ThreadData td;
  122. td.copy_src1 = s->f0;
  123. td.copy_src2 = s->f1;
  124. td.src2_factor = interpolate;
  125. td.src1_factor = s->blend_factor_max - td.src2_factor;
  126. // get work-space for output frame
  127. s->work = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  128. if (!s->work)
  129. return AVERROR(ENOMEM);
  130. av_frame_copy_props(s->work, s->f0);
  131. ff_dlog(ctx, "blend_frames() INTERPOLATE to create work frame\n");
  132. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(FFMAX(1, outlink->h >> 2), ff_filter_get_nb_threads(ctx)));
  133. return 1;
  134. }
  135. return 0;
  136. }
  137. static int process_work_frame(AVFilterContext *ctx)
  138. {
  139. FrameRateContext *s = ctx->priv;
  140. int64_t work_pts;
  141. int64_t interpolate, interpolate8;
  142. int ret;
  143. if (!s->f1)
  144. return 0;
  145. if (!s->f0 && !s->flush)
  146. return 0;
  147. work_pts = s->start_pts + av_rescale_q(s->n, av_inv_q(s->dest_frame_rate), s->dest_time_base);
  148. if (work_pts >= s->pts1 && !s->flush)
  149. return 0;
  150. if (!s->f0) {
  151. s->work = av_frame_clone(s->f1);
  152. } else {
  153. if (work_pts >= s->pts1 + s->delta && s->flush)
  154. return 0;
  155. interpolate = av_rescale(work_pts - s->pts0, s->blend_factor_max, s->delta);
  156. interpolate8 = av_rescale(work_pts - s->pts0, 256, s->delta);
  157. ff_dlog(ctx, "process_work_frame() interpolate: %"PRId64"/256\n", interpolate8);
  158. if (interpolate >= s->blend_factor_max || interpolate8 > s->interp_end) {
  159. s->work = av_frame_clone(s->f1);
  160. } else if (interpolate <= 0 || interpolate8 < s->interp_start) {
  161. s->work = av_frame_clone(s->f0);
  162. } else {
  163. ret = blend_frames(ctx, interpolate);
  164. if (ret < 0)
  165. return ret;
  166. if (ret == 0)
  167. s->work = av_frame_clone(interpolate > (s->blend_factor_max >> 1) ? s->f1 : s->f0);
  168. }
  169. }
  170. if (!s->work)
  171. return AVERROR(ENOMEM);
  172. s->work->pts = work_pts;
  173. s->n++;
  174. return 1;
  175. }
  176. static av_cold int init(AVFilterContext *ctx)
  177. {
  178. FrameRateContext *s = ctx->priv;
  179. s->start_pts = AV_NOPTS_VALUE;
  180. return 0;
  181. }
  182. static av_cold void uninit(AVFilterContext *ctx)
  183. {
  184. FrameRateContext *s = ctx->priv;
  185. av_frame_free(&s->f0);
  186. av_frame_free(&s->f1);
  187. }
  188. static int query_formats(AVFilterContext *ctx)
  189. {
  190. static const enum AVPixelFormat pix_fmts[] = {
  191. AV_PIX_FMT_YUV410P,
  192. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUVJ411P,
  193. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
  194. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
  195. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
  196. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  197. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
  198. AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
  199. AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
  200. AV_PIX_FMT_NONE
  201. };
  202. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  203. if (!fmts_list)
  204. return AVERROR(ENOMEM);
  205. return ff_set_common_formats(ctx, fmts_list);
  206. }
  207. static void blend_frames_c(BLEND_FUNC_PARAMS)
  208. {
  209. int line, pixel;
  210. for (line = 0; line < height; line++) {
  211. for (pixel = 0; pixel < width; pixel++)
  212. dst[pixel] = ((src1[pixel] * factor1) + (src2[pixel] * factor2) + half) >> BLEND_FACTOR_DEPTH8;
  213. src1 += src1_linesize;
  214. src2 += src2_linesize;
  215. dst += dst_linesize;
  216. }
  217. }
  218. static void blend_frames16_c(BLEND_FUNC_PARAMS)
  219. {
  220. int line, pixel;
  221. uint16_t *dstw = (uint16_t *)dst;
  222. uint16_t *src1w = (uint16_t *)src1;
  223. uint16_t *src2w = (uint16_t *)src2;
  224. width /= 2;
  225. src1_linesize /= 2;
  226. src2_linesize /= 2;
  227. dst_linesize /= 2;
  228. for (line = 0; line < height; line++) {
  229. for (pixel = 0; pixel < width; pixel++)
  230. dstw[pixel] = ((src1w[pixel] * factor1) + (src2w[pixel] * factor2) + half) >> BLEND_FACTOR_DEPTH16;
  231. src1w += src1_linesize;
  232. src2w += src2_linesize;
  233. dstw += dst_linesize;
  234. }
  235. }
  236. void ff_framerate_init(FrameRateContext *s)
  237. {
  238. if (s->bitdepth == 8) {
  239. s->blend_factor_max = 1 << BLEND_FACTOR_DEPTH8;
  240. s->blend = blend_frames_c;
  241. } else {
  242. s->blend_factor_max = 1 << BLEND_FACTOR_DEPTH16;
  243. s->blend = blend_frames16_c;
  244. }
  245. if (ARCH_X86)
  246. ff_framerate_init_x86(s);
  247. }
  248. static int config_input(AVFilterLink *inlink)
  249. {
  250. AVFilterContext *ctx = inlink->dst;
  251. FrameRateContext *s = ctx->priv;
  252. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  253. int plane;
  254. for (plane = 0; plane < 4; plane++) {
  255. s->line_size[plane] = av_image_get_linesize(inlink->format, inlink->w,
  256. plane);
  257. }
  258. s->bitdepth = pix_desc->comp[0].depth;
  259. s->vsub = pix_desc->log2_chroma_h;
  260. s->sad = ff_scene_sad_get_fn(s->bitdepth == 8 ? 8 : 16);
  261. if (!s->sad)
  262. return AVERROR(EINVAL);
  263. s->srce_time_base = inlink->time_base;
  264. ff_framerate_init(s);
  265. return 0;
  266. }
  267. static int activate(AVFilterContext *ctx)
  268. {
  269. int ret, status;
  270. AVFilterLink *inlink = ctx->inputs[0];
  271. AVFilterLink *outlink = ctx->outputs[0];
  272. FrameRateContext *s = ctx->priv;
  273. AVFrame *inpicref;
  274. int64_t pts;
  275. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  276. retry:
  277. ret = process_work_frame(ctx);
  278. if (ret < 0)
  279. return ret;
  280. else if (ret == 1)
  281. return ff_filter_frame(outlink, s->work);
  282. ret = ff_inlink_consume_frame(inlink, &inpicref);
  283. if (ret < 0)
  284. return ret;
  285. if (inpicref) {
  286. if (inpicref->interlaced_frame)
  287. av_log(ctx, AV_LOG_WARNING, "Interlaced frame found - the output will not be correct.\n");
  288. if (inpicref->pts == AV_NOPTS_VALUE) {
  289. av_log(ctx, AV_LOG_WARNING, "Ignoring frame without PTS.\n");
  290. av_frame_free(&inpicref);
  291. }
  292. }
  293. if (inpicref) {
  294. pts = av_rescale_q(inpicref->pts, s->srce_time_base, s->dest_time_base);
  295. if (s->f1 && pts == s->pts1) {
  296. av_log(ctx, AV_LOG_WARNING, "Ignoring frame with same PTS.\n");
  297. av_frame_free(&inpicref);
  298. }
  299. }
  300. if (inpicref) {
  301. av_frame_free(&s->f0);
  302. s->f0 = s->f1;
  303. s->pts0 = s->pts1;
  304. s->f1 = inpicref;
  305. s->pts1 = pts;
  306. s->delta = s->pts1 - s->pts0;
  307. s->score = -1.0;
  308. if (s->delta < 0) {
  309. av_log(ctx, AV_LOG_WARNING, "PTS discontinuity.\n");
  310. s->start_pts = s->pts1;
  311. s->n = 0;
  312. av_frame_free(&s->f0);
  313. }
  314. if (s->start_pts == AV_NOPTS_VALUE)
  315. s->start_pts = s->pts1;
  316. goto retry;
  317. }
  318. if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
  319. if (!s->flush) {
  320. s->flush = 1;
  321. goto retry;
  322. }
  323. ff_outlink_set_status(outlink, status, pts);
  324. return 0;
  325. }
  326. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  327. return FFERROR_NOT_READY;
  328. }
  329. static int config_output(AVFilterLink *outlink)
  330. {
  331. AVFilterContext *ctx = outlink->src;
  332. FrameRateContext *s = ctx->priv;
  333. int exact;
  334. ff_dlog(ctx, "config_output()\n");
  335. ff_dlog(ctx,
  336. "config_output() input time base:%u/%u (%f)\n",
  337. ctx->inputs[0]->time_base.num,ctx->inputs[0]->time_base.den,
  338. av_q2d(ctx->inputs[0]->time_base));
  339. // make sure timebase is small enough to hold the framerate
  340. exact = av_reduce(&s->dest_time_base.num, &s->dest_time_base.den,
  341. av_gcd((int64_t)s->srce_time_base.num * s->dest_frame_rate.num,
  342. (int64_t)s->srce_time_base.den * s->dest_frame_rate.den ),
  343. (int64_t)s->srce_time_base.den * s->dest_frame_rate.num, INT_MAX);
  344. av_log(ctx, AV_LOG_INFO,
  345. "time base:%u/%u -> %u/%u exact:%d\n",
  346. s->srce_time_base.num, s->srce_time_base.den,
  347. s->dest_time_base.num, s->dest_time_base.den, exact);
  348. if (!exact) {
  349. av_log(ctx, AV_LOG_WARNING, "Timebase conversion is not exact\n");
  350. }
  351. outlink->frame_rate = s->dest_frame_rate;
  352. outlink->time_base = s->dest_time_base;
  353. ff_dlog(ctx,
  354. "config_output() output time base:%u/%u (%f) w:%d h:%d\n",
  355. outlink->time_base.num, outlink->time_base.den,
  356. av_q2d(outlink->time_base),
  357. outlink->w, outlink->h);
  358. av_log(ctx, AV_LOG_INFO, "fps -> fps:%u/%u scene score:%f interpolate start:%d end:%d\n",
  359. s->dest_frame_rate.num, s->dest_frame_rate.den,
  360. s->scene_score, s->interp_start, s->interp_end);
  361. return 0;
  362. }
  363. static const AVFilterPad framerate_inputs[] = {
  364. {
  365. .name = "default",
  366. .type = AVMEDIA_TYPE_VIDEO,
  367. .config_props = config_input,
  368. },
  369. { NULL }
  370. };
  371. static const AVFilterPad framerate_outputs[] = {
  372. {
  373. .name = "default",
  374. .type = AVMEDIA_TYPE_VIDEO,
  375. .config_props = config_output,
  376. },
  377. { NULL }
  378. };
  379. AVFilter ff_vf_framerate = {
  380. .name = "framerate",
  381. .description = NULL_IF_CONFIG_SMALL("Upsamples or downsamples progressive source between specified frame rates."),
  382. .priv_size = sizeof(FrameRateContext),
  383. .priv_class = &framerate_class,
  384. .init = init,
  385. .uninit = uninit,
  386. .query_formats = query_formats,
  387. .inputs = framerate_inputs,
  388. .outputs = framerate_outputs,
  389. .flags = AVFILTER_FLAG_SLICE_THREADS,
  390. .activate = activate,
  391. };