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.

455 lines
15KB

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