vf_decimate.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Copyright (c) 2012 Fredrik Mellbin
  3. * Copyright (c) 2013 Clément Bœsch
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/opt.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "libavutil/timestamp.h"
  24. #include "avfilter.h"
  25. #include "internal.h"
  26. #define INPUT_MAIN 0
  27. #define INPUT_CLEANSRC 1
  28. struct qitem {
  29. AVFrame *frame;
  30. int64_t maxbdiff;
  31. int64_t totdiff;
  32. };
  33. typedef struct {
  34. const AVClass *class;
  35. struct qitem *queue; ///< window of cycle frames and the associated data diff
  36. int fid; ///< current frame id in the queue
  37. int filled; ///< 1 if the queue is filled, 0 otherwise
  38. AVFrame *last; ///< last frame from the previous queue
  39. AVFrame **clean_src; ///< frame queue for the clean source
  40. int got_frame[2]; ///< frame request flag for each input stream
  41. double ts_unit; ///< timestamp units for the output frames
  42. int64_t start_pts; ///< base for output timestamps
  43. uint32_t eof; ///< bitmask for end of stream
  44. int hsub, vsub; ///< chroma subsampling values
  45. int depth;
  46. int nxblocks, nyblocks;
  47. int bdiffsize;
  48. int64_t *bdiffs;
  49. /* options */
  50. int cycle;
  51. double dupthresh_flt;
  52. double scthresh_flt;
  53. int64_t dupthresh;
  54. int64_t scthresh;
  55. int blockx, blocky;
  56. int ppsrc;
  57. int chroma;
  58. } DecimateContext;
  59. #define OFFSET(x) offsetof(DecimateContext, x)
  60. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  61. static const AVOption decimate_options[] = {
  62. { "cycle", "set the number of frame from which one will be dropped", OFFSET(cycle), AV_OPT_TYPE_INT, {.i64 = 5}, 2, 25, FLAGS },
  63. { "dupthresh", "set duplicate threshold", OFFSET(dupthresh_flt), AV_OPT_TYPE_DOUBLE, {.dbl = 1.1}, 0, 100, FLAGS },
  64. { "scthresh", "set scene change threshold", OFFSET(scthresh_flt), AV_OPT_TYPE_DOUBLE, {.dbl = 15.0}, 0, 100, FLAGS },
  65. { "blockx", "set the size of the x-axis blocks used during metric calculations", OFFSET(blockx), AV_OPT_TYPE_INT, {.i64 = 32}, 4, 1<<9, FLAGS },
  66. { "blocky", "set the size of the y-axis blocks used during metric calculations", OFFSET(blocky), AV_OPT_TYPE_INT, {.i64 = 32}, 4, 1<<9, FLAGS },
  67. { "ppsrc", "mark main input as a pre-processed input and activate clean source input stream", OFFSET(ppsrc), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
  68. { "chroma", "set whether or not chroma is considered in the metric calculations", OFFSET(chroma), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
  69. { NULL }
  70. };
  71. AVFILTER_DEFINE_CLASS(decimate);
  72. static void calc_diffs(const DecimateContext *dm, struct qitem *q,
  73. const AVFrame *f1, const AVFrame *f2)
  74. {
  75. int64_t maxdiff = -1;
  76. int64_t *bdiffs = dm->bdiffs;
  77. int plane, i, j;
  78. memset(bdiffs, 0, dm->bdiffsize * sizeof(*bdiffs));
  79. for (plane = 0; plane < (dm->chroma && f1->data[2] ? 3 : 1); plane++) {
  80. int x, y, xl;
  81. const int linesize1 = f1->linesize[plane];
  82. const int linesize2 = f2->linesize[plane];
  83. const uint8_t *f1p = f1->data[plane];
  84. const uint8_t *f2p = f2->data[plane];
  85. int width = plane ? FF_CEIL_RSHIFT(f1->width, dm->hsub) : f1->width;
  86. int height = plane ? FF_CEIL_RSHIFT(f1->height, dm->vsub) : f1->height;
  87. int hblockx = dm->blockx / 2;
  88. int hblocky = dm->blocky / 2;
  89. if (plane) {
  90. hblockx >>= dm->hsub;
  91. hblocky >>= dm->vsub;
  92. }
  93. for (y = 0; y < height; y++) {
  94. int ydest = y / hblocky;
  95. int xdest = 0;
  96. #define CALC_DIFF(nbits) do { \
  97. for (x = 0; x < width; x += hblockx) { \
  98. int64_t acc = 0; \
  99. int m = FFMIN(width, x + hblockx); \
  100. for (xl = x; xl < m; xl++) \
  101. acc += abs(((const uint##nbits##_t *)f1p)[xl] - \
  102. ((const uint##nbits##_t *)f2p)[xl]); \
  103. bdiffs[ydest * dm->nxblocks + xdest] += acc; \
  104. xdest++; \
  105. } \
  106. } while (0)
  107. if (dm->depth == 8) CALC_DIFF(8);
  108. else CALC_DIFF(16);
  109. f1p += linesize1;
  110. f2p += linesize2;
  111. }
  112. }
  113. for (i = 0; i < dm->nyblocks - 1; i++) {
  114. for (j = 0; j < dm->nxblocks - 1; j++) {
  115. int64_t tmp = bdiffs[ i * dm->nxblocks + j ]
  116. + bdiffs[ i * dm->nxblocks + j + 1]
  117. + bdiffs[(i + 1) * dm->nxblocks + j ]
  118. + bdiffs[(i + 1) * dm->nxblocks + j + 1];
  119. if (tmp > maxdiff)
  120. maxdiff = tmp;
  121. }
  122. }
  123. q->totdiff = 0;
  124. for (i = 0; i < dm->bdiffsize; i++)
  125. q->totdiff += bdiffs[i];
  126. q->maxbdiff = maxdiff;
  127. }
  128. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  129. {
  130. int scpos = -1, duppos = -1;
  131. int drop = INT_MIN, i, lowest = 0, ret;
  132. AVFilterContext *ctx = inlink->dst;
  133. AVFilterLink *outlink = ctx->outputs[0];
  134. DecimateContext *dm = ctx->priv;
  135. AVFrame *prv;
  136. /* update frames queue(s) */
  137. if (FF_INLINK_IDX(inlink) == INPUT_MAIN) {
  138. dm->queue[dm->fid].frame = in;
  139. dm->got_frame[INPUT_MAIN] = 1;
  140. } else {
  141. dm->clean_src[dm->fid] = in;
  142. dm->got_frame[INPUT_CLEANSRC] = 1;
  143. }
  144. if (!dm->got_frame[INPUT_MAIN] || (dm->ppsrc && !dm->got_frame[INPUT_CLEANSRC]))
  145. return 0;
  146. dm->got_frame[INPUT_MAIN] = dm->got_frame[INPUT_CLEANSRC] = 0;
  147. if (in) {
  148. /* update frame metrics */
  149. prv = dm->fid ? dm->queue[dm->fid - 1].frame : dm->last;
  150. if (!prv)
  151. prv = in;
  152. calc_diffs(dm, &dm->queue[dm->fid], prv, in);
  153. if (++dm->fid != dm->cycle)
  154. return 0;
  155. av_frame_free(&dm->last);
  156. dm->last = av_frame_clone(in);
  157. dm->fid = 0;
  158. /* we have a complete cycle, select the frame to drop */
  159. lowest = 0;
  160. for (i = 0; i < dm->cycle; i++) {
  161. if (dm->queue[i].totdiff > dm->scthresh)
  162. scpos = i;
  163. if (dm->queue[i].maxbdiff < dm->queue[lowest].maxbdiff)
  164. lowest = i;
  165. }
  166. if (dm->queue[lowest].maxbdiff < dm->dupthresh)
  167. duppos = lowest;
  168. drop = scpos >= 0 && duppos < 0 ? scpos : lowest;
  169. }
  170. /* metrics debug */
  171. if (av_log_get_level() >= AV_LOG_DEBUG) {
  172. av_log(ctx, AV_LOG_DEBUG, "1/%d frame drop:\n", dm->cycle);
  173. for (i = 0; i < dm->cycle && dm->queue[i].frame; i++) {
  174. av_log(ctx, AV_LOG_DEBUG," #%d: totdiff=%08"PRIx64" maxbdiff=%08"PRIx64"%s%s%s%s\n",
  175. i + 1, dm->queue[i].totdiff, dm->queue[i].maxbdiff,
  176. i == scpos ? " sc" : "",
  177. i == duppos ? " dup" : "",
  178. i == lowest ? " lowest" : "",
  179. i == drop ? " [DROP]" : "");
  180. }
  181. }
  182. /* push all frames except the drop */
  183. ret = 0;
  184. for (i = 0; i < dm->cycle && dm->queue[i].frame; i++) {
  185. if (i == drop) {
  186. if (dm->ppsrc)
  187. av_frame_free(&dm->clean_src[i]);
  188. av_frame_free(&dm->queue[i].frame);
  189. } else {
  190. AVFrame *frame = dm->queue[i].frame;
  191. if (frame->pts != AV_NOPTS_VALUE && dm->start_pts == AV_NOPTS_VALUE)
  192. dm->start_pts = frame->pts;
  193. if (dm->ppsrc) {
  194. av_frame_free(&frame);
  195. frame = dm->clean_src[i];
  196. }
  197. frame->pts = outlink->frame_count * dm->ts_unit +
  198. (dm->start_pts == AV_NOPTS_VALUE ? 0 : dm->start_pts);
  199. ret = ff_filter_frame(outlink, frame);
  200. if (ret < 0)
  201. break;
  202. }
  203. }
  204. return ret;
  205. }
  206. static int config_input(AVFilterLink *inlink)
  207. {
  208. int max_value;
  209. AVFilterContext *ctx = inlink->dst;
  210. DecimateContext *dm = ctx->priv;
  211. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  212. const int w = inlink->w;
  213. const int h = inlink->h;
  214. dm->hsub = pix_desc->log2_chroma_w;
  215. dm->vsub = pix_desc->log2_chroma_h;
  216. dm->depth = pix_desc->comp[0].depth_minus1 + 1;
  217. max_value = (1 << dm->depth) - 1;
  218. dm->scthresh = (int64_t)(((int64_t)max_value * w * h * dm->scthresh_flt) / 100);
  219. dm->dupthresh = (int64_t)(((int64_t)max_value * dm->blockx * dm->blocky * dm->dupthresh_flt) / 100);
  220. dm->nxblocks = (w + dm->blockx/2 - 1) / (dm->blockx/2);
  221. dm->nyblocks = (h + dm->blocky/2 - 1) / (dm->blocky/2);
  222. dm->bdiffsize = dm->nxblocks * dm->nyblocks;
  223. dm->bdiffs = av_malloc(dm->bdiffsize * sizeof(*dm->bdiffs));
  224. dm->queue = av_calloc(dm->cycle, sizeof(*dm->queue));
  225. if (!dm->bdiffs || !dm->queue)
  226. return AVERROR(ENOMEM);
  227. if (dm->ppsrc) {
  228. dm->clean_src = av_calloc(dm->cycle, sizeof(*dm->clean_src));
  229. if (!dm->clean_src)
  230. return AVERROR(ENOMEM);
  231. }
  232. return 0;
  233. }
  234. static av_cold int decimate_init(AVFilterContext *ctx)
  235. {
  236. DecimateContext *dm = ctx->priv;
  237. AVFilterPad pad = {
  238. .name = av_strdup("main"),
  239. .type = AVMEDIA_TYPE_VIDEO,
  240. .filter_frame = filter_frame,
  241. .config_props = config_input,
  242. };
  243. if (!pad.name)
  244. return AVERROR(ENOMEM);
  245. ff_insert_inpad(ctx, INPUT_MAIN, &pad);
  246. if (dm->ppsrc) {
  247. pad.name = av_strdup("clean_src");
  248. pad.config_props = NULL;
  249. if (!pad.name)
  250. return AVERROR(ENOMEM);
  251. ff_insert_inpad(ctx, INPUT_CLEANSRC, &pad);
  252. }
  253. if ((dm->blockx & (dm->blockx - 1)) ||
  254. (dm->blocky & (dm->blocky - 1))) {
  255. av_log(ctx, AV_LOG_ERROR, "blockx and blocky settings must be power of two\n");
  256. return AVERROR(EINVAL);
  257. }
  258. dm->start_pts = AV_NOPTS_VALUE;
  259. return 0;
  260. }
  261. static av_cold void decimate_uninit(AVFilterContext *ctx)
  262. {
  263. int i;
  264. DecimateContext *dm = ctx->priv;
  265. av_frame_free(&dm->last);
  266. av_freep(&dm->bdiffs);
  267. av_freep(&dm->queue);
  268. av_freep(&dm->clean_src);
  269. for (i = 0; i < ctx->nb_inputs; i++)
  270. av_freep(&ctx->input_pads[i].name);
  271. }
  272. static int request_inlink(AVFilterContext *ctx, int lid)
  273. {
  274. int ret = 0;
  275. DecimateContext *dm = ctx->priv;
  276. if (!dm->got_frame[lid]) {
  277. AVFilterLink *inlink = ctx->inputs[lid];
  278. ret = ff_request_frame(inlink);
  279. if (ret == AVERROR_EOF) { // flushing
  280. dm->eof |= 1 << lid;
  281. ret = filter_frame(inlink, NULL);
  282. }
  283. }
  284. return ret;
  285. }
  286. static int request_frame(AVFilterLink *outlink)
  287. {
  288. int ret;
  289. AVFilterContext *ctx = outlink->src;
  290. DecimateContext *dm = ctx->priv;
  291. const uint32_t eof_mask = 1<<INPUT_MAIN | dm->ppsrc<<INPUT_CLEANSRC;
  292. if ((dm->eof & eof_mask) == eof_mask) // flush done?
  293. return AVERROR_EOF;
  294. if ((ret = request_inlink(ctx, INPUT_MAIN)) < 0)
  295. return ret;
  296. if (dm->ppsrc && (ret = request_inlink(ctx, INPUT_CLEANSRC)) < 0)
  297. return ret;
  298. return 0;
  299. }
  300. static int query_formats(AVFilterContext *ctx)
  301. {
  302. static const enum AVPixelFormat pix_fmts[] = {
  303. #define PF_NOALPHA(suf) AV_PIX_FMT_YUV420##suf, AV_PIX_FMT_YUV422##suf, AV_PIX_FMT_YUV444##suf
  304. #define PF_ALPHA(suf) AV_PIX_FMT_YUVA420##suf, AV_PIX_FMT_YUVA422##suf, AV_PIX_FMT_YUVA444##suf
  305. #define PF(suf) PF_NOALPHA(suf), PF_ALPHA(suf)
  306. PF(P), PF(P9), PF(P10), PF_NOALPHA(P12), PF_NOALPHA(P14), PF(P16),
  307. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  308. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
  309. AV_PIX_FMT_NONE
  310. };
  311. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  312. return 0;
  313. }
  314. static int config_output(AVFilterLink *outlink)
  315. {
  316. AVFilterContext *ctx = outlink->src;
  317. DecimateContext *dm = ctx->priv;
  318. const AVFilterLink *inlink =
  319. ctx->inputs[dm->ppsrc ? INPUT_CLEANSRC : INPUT_MAIN];
  320. AVRational fps = inlink->frame_rate;
  321. if (!fps.num || !fps.den) {
  322. av_log(ctx, AV_LOG_ERROR, "The input needs a constant frame rate; "
  323. "current rate of %d/%d is invalid\n", fps.num, fps.den);
  324. return AVERROR(EINVAL);
  325. }
  326. fps = av_mul_q(fps, (AVRational){dm->cycle - 1, dm->cycle});
  327. av_log(ctx, AV_LOG_VERBOSE, "FPS: %d/%d -> %d/%d\n",
  328. inlink->frame_rate.num, inlink->frame_rate.den, fps.num, fps.den);
  329. outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
  330. outlink->time_base = inlink->time_base;
  331. outlink->frame_rate = fps;
  332. outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
  333. outlink->w = inlink->w;
  334. outlink->h = inlink->h;
  335. dm->ts_unit = av_q2d(av_inv_q(av_mul_q(fps, outlink->time_base)));
  336. return 0;
  337. }
  338. static const AVFilterPad decimate_outputs[] = {
  339. {
  340. .name = "default",
  341. .type = AVMEDIA_TYPE_VIDEO,
  342. .request_frame = request_frame,
  343. .config_props = config_output,
  344. },
  345. { NULL }
  346. };
  347. AVFilter ff_vf_decimate = {
  348. .name = "decimate",
  349. .description = NULL_IF_CONFIG_SMALL("Decimate frames (post field matching filter)."),
  350. .init = decimate_init,
  351. .uninit = decimate_uninit,
  352. .priv_size = sizeof(DecimateContext),
  353. .query_formats = query_formats,
  354. .outputs = decimate_outputs,
  355. .priv_class = &decimate_class,
  356. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  357. };