vf_deblock.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright (c) 2018 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /*
  21. * Based on paper: A Simple and Efficient Deblocking Algorithm for Low Bit-Rate Video Coding.
  22. */
  23. #include "libavutil/imgutils.h"
  24. #include "libavutil/opt.h"
  25. #include "libavutil/pixdesc.h"
  26. #include "avfilter.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. enum FilterType { WEAK, STRONG, NB_FILTER };
  31. typedef struct DeblockContext {
  32. const AVClass *class;
  33. const AVPixFmtDescriptor *desc;
  34. int filter;
  35. int block;
  36. int planes;
  37. float alpha;
  38. float beta;
  39. float gamma;
  40. float delta;
  41. int ath;
  42. int bth;
  43. int gth;
  44. int dth;
  45. int max;
  46. int depth;
  47. int bpc;
  48. int nb_planes;
  49. int planewidth[4];
  50. int planeheight[4];
  51. void (*deblockh)(uint8_t *dst, ptrdiff_t dst_linesize, int block,
  52. int ath, int bth, int gth, int dth, int max);
  53. void (*deblockv)(uint8_t *dst, ptrdiff_t dst_linesize, int block,
  54. int ath, int bth, int gth, int dth, int max);
  55. } DeblockContext;
  56. static int query_formats(AVFilterContext *ctx)
  57. {
  58. static const enum AVPixelFormat pixel_fmts[] = {
  59. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  60. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  61. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  62. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  63. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  64. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  65. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  66. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  67. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  68. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  69. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  70. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  71. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  72. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  73. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  74. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  75. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  76. AV_PIX_FMT_NONE
  77. };
  78. AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
  79. if (!formats)
  80. return AVERROR(ENOMEM);
  81. return ff_set_common_formats(ctx, formats);
  82. }
  83. #define WEAK_HFILTER(name, type, ldiv) \
  84. static void deblockh##name##_weak(uint8_t *dstp, ptrdiff_t dst_linesize, int block, \
  85. int ath, int bth, int gth, int dth, int max) \
  86. { \
  87. type *dst; \
  88. int x; \
  89. \
  90. dst = (type *)dstp; \
  91. dst_linesize /= ldiv; \
  92. \
  93. for (x = 0; x < block; x++) { \
  94. int delta = dst[x] - dst[x - dst_linesize]; \
  95. int A, B, C, D, a, b, c, d; \
  96. \
  97. if (FFABS(delta) >= ath || \
  98. FFABS(dst[x - 1 * dst_linesize] - dst[x - 2 * dst_linesize]) >= bth || \
  99. FFABS(dst[x + 0 * dst_linesize] - dst[x + 1 * dst_linesize]) >= gth) \
  100. continue; \
  101. \
  102. A = dst[x - 2 * dst_linesize]; \
  103. B = dst[x - 1 * dst_linesize]; \
  104. C = dst[x + 0 * dst_linesize]; \
  105. D = dst[x + 1 * dst_linesize]; \
  106. \
  107. a = A + delta / 8; \
  108. b = B + delta / 2; \
  109. c = C - delta / 2; \
  110. d = D - delta / 8; \
  111. \
  112. dst[x - 2 * dst_linesize] = av_clip(a, 0, max); \
  113. dst[x - 1 * dst_linesize] = av_clip(b, 0, max); \
  114. dst[x + 0 * dst_linesize] = av_clip(c, 0, max); \
  115. dst[x + 1 * dst_linesize] = av_clip(d, 0, max); \
  116. } \
  117. }
  118. WEAK_HFILTER(8, uint8_t, 1)
  119. WEAK_HFILTER(16, uint16_t, 2)
  120. #define WEAK_VFILTER(name, type, ldiv) \
  121. static void deblockv##name##_weak(uint8_t *dstp, ptrdiff_t dst_linesize, int block, \
  122. int ath, int bth, int gth, int dth, int max) \
  123. { \
  124. type *dst; \
  125. int y; \
  126. \
  127. dst = (type *)dstp; \
  128. dst_linesize /= ldiv; \
  129. \
  130. for (y = 0; y < block; y++) { \
  131. int delta = dst[0] - dst[-1]; \
  132. int A, B, C, D, a, b, c, d; \
  133. \
  134. if (FFABS(delta) >= ath || \
  135. FFABS(dst[-1] - dst[-2]) >= bth || \
  136. FFABS(dst[0] - dst[1]) >= gth) \
  137. continue; \
  138. \
  139. A = dst[-2]; \
  140. B = dst[-1]; \
  141. C = dst[+0]; \
  142. D = dst[+1]; \
  143. \
  144. a = A + delta / 8; \
  145. b = B + delta / 2; \
  146. c = C - delta / 2; \
  147. d = D - delta / 8; \
  148. \
  149. dst[-2] = av_clip(a, 0, max); \
  150. dst[-1] = av_clip(b, 0, max); \
  151. dst[+0] = av_clip(c, 0, max); \
  152. dst[+1] = av_clip(d, 0, max); \
  153. \
  154. dst += dst_linesize; \
  155. } \
  156. }
  157. WEAK_VFILTER(8, uint8_t, 1)
  158. WEAK_VFILTER(16, uint16_t, 2)
  159. #define STRONG_HFILTER(name, type, ldiv) \
  160. static void deblockh##name##_strong(uint8_t *dstp, ptrdiff_t dst_linesize, int block,\
  161. int ath, int bth, int gth, int dth, int max) \
  162. { \
  163. type *dst; \
  164. int x; \
  165. \
  166. dst = (type *)dstp; \
  167. dst_linesize /= ldiv; \
  168. \
  169. for (x = 0; x < block; x++) { \
  170. int A, B, C, D, E, F, a, b, c, d, e, f; \
  171. int delta = dst[x] - dst[x - dst_linesize]; \
  172. \
  173. if (FFABS(delta) >= ath || \
  174. FFABS(dst[x - 1 * dst_linesize] - dst[x - 2 * dst_linesize]) >= bth || \
  175. FFABS(dst[x + 1 * dst_linesize] - dst[x + 2 * dst_linesize]) >= gth || \
  176. FFABS(dst[x + 0 * dst_linesize] - dst[x + 1 * dst_linesize]) >= dth) \
  177. continue; \
  178. \
  179. A = dst[x - 3 * dst_linesize]; \
  180. B = dst[x - 2 * dst_linesize]; \
  181. C = dst[x - 1 * dst_linesize]; \
  182. D = dst[x + 0 * dst_linesize]; \
  183. E = dst[x + 1 * dst_linesize]; \
  184. F = dst[x + 2 * dst_linesize]; \
  185. \
  186. a = A + delta / 8; \
  187. b = B + delta / 4; \
  188. c = C + delta / 2; \
  189. d = D - delta / 2; \
  190. e = E - delta / 4; \
  191. f = F - delta / 8; \
  192. \
  193. dst[x - 3 * dst_linesize] = av_clip(a, 0, max); \
  194. dst[x - 2 * dst_linesize] = av_clip(b, 0, max); \
  195. dst[x - 1 * dst_linesize] = av_clip(c, 0, max); \
  196. dst[x + 0 * dst_linesize] = av_clip(d, 0, max); \
  197. dst[x + 1 * dst_linesize] = av_clip(e, 0, max); \
  198. dst[x + 2 * dst_linesize] = av_clip(f, 0, max); \
  199. } \
  200. }
  201. STRONG_HFILTER(8, uint8_t, 1)
  202. STRONG_HFILTER(16, uint16_t, 2)
  203. #define STRONG_VFILTER(name, type, ldiv) \
  204. static void deblockv##name##_strong(uint8_t *dstp, ptrdiff_t dst_linesize, int block,\
  205. int ath, int bth, int gth, int dth, int max) \
  206. { \
  207. type *dst; \
  208. int y; \
  209. \
  210. dst = (type *)dstp; \
  211. dst_linesize /= ldiv; \
  212. \
  213. for (y = 0; y < block; y++) { \
  214. int A, B, C, D, E, F, a, b, c, d, e, f; \
  215. int delta = dst[0] - dst[-1]; \
  216. \
  217. if (FFABS(delta) >= ath || \
  218. FFABS(dst[-1] - dst[-2]) >= bth || \
  219. FFABS(dst[+1] - dst[+2]) >= gth || \
  220. FFABS(dst[+0] - dst[+1]) >= dth) \
  221. continue; \
  222. \
  223. A = dst[-3]; \
  224. B = dst[-2]; \
  225. C = dst[-1]; \
  226. D = dst[+0]; \
  227. E = dst[+1]; \
  228. F = dst[+2]; \
  229. \
  230. a = A + delta / 8; \
  231. b = B + delta / 4; \
  232. c = C + delta / 2; \
  233. d = D - delta / 2; \
  234. e = E - delta / 4; \
  235. f = F - delta / 8; \
  236. \
  237. dst[-3] = av_clip(a, 0, max); \
  238. dst[-2] = av_clip(b, 0, max); \
  239. dst[-1] = av_clip(c, 0, max); \
  240. dst[+0] = av_clip(d, 0, max); \
  241. dst[+1] = av_clip(e, 0, max); \
  242. dst[+2] = av_clip(f, 0, max); \
  243. \
  244. dst += dst_linesize; \
  245. } \
  246. }
  247. STRONG_VFILTER(8, uint8_t, 1)
  248. STRONG_VFILTER(16, uint16_t, 2)
  249. static int config_output(AVFilterLink *outlink)
  250. {
  251. AVFilterContext *ctx = outlink->src;
  252. DeblockContext *s = ctx->priv;
  253. AVFilterLink *inlink = ctx->inputs[0];
  254. s->desc = av_pix_fmt_desc_get(outlink->format);
  255. if (!s->desc)
  256. return AVERROR_BUG;
  257. s->nb_planes = av_pix_fmt_count_planes(outlink->format);
  258. s->depth = s->desc->comp[0].depth;
  259. s->bpc = (s->depth + 7) / 8;
  260. s->max = (1 << s->depth) - 1;
  261. s->ath = s->alpha * s->max;
  262. s->bth = s->beta * s->max;
  263. s->gth = s->gamma * s->max;
  264. s->dth = s->delta * s->max;
  265. if (s->depth <= 8 && s->filter == WEAK) {
  266. s->deblockh = deblockh8_weak;
  267. s->deblockv = deblockv8_weak;
  268. } else if (s->depth >= 8 && s->filter == WEAK) {
  269. s->deblockh = deblockh16_weak;
  270. s->deblockv = deblockv16_weak;
  271. }
  272. if (s->depth <= 8 && s->filter == STRONG) {
  273. s->deblockh = deblockh8_strong;
  274. s->deblockv = deblockv8_strong;
  275. } else if (s->depth >= 8 && s->filter == STRONG) {
  276. s->deblockh = deblockh16_strong;
  277. s->deblockv = deblockv16_strong;
  278. }
  279. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
  280. s->planewidth[0] = s->planewidth[3] = inlink->w;
  281. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
  282. s->planeheight[0] = s->planeheight[3] = inlink->h;
  283. return 0;
  284. }
  285. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  286. {
  287. AVFilterContext *ctx = inlink->dst;
  288. AVFilterLink *outlink = ctx->outputs[0];
  289. DeblockContext *s = ctx->priv;
  290. const int block = s->block;
  291. AVFrame *out;
  292. int plane, x, y;
  293. if (av_frame_is_writable(in)) {
  294. out = in;
  295. } else {
  296. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  297. if (!out) {
  298. av_frame_free(&in);
  299. return AVERROR(ENOMEM);
  300. }
  301. av_frame_copy_props(out, in);
  302. }
  303. for (plane = 0; plane < s->nb_planes; plane++) {
  304. const int width = s->planewidth[plane];
  305. const int height = s->planeheight[plane];
  306. const uint8_t *src = (const uint8_t *)in->data[plane];
  307. uint8_t *dst = (uint8_t *)out->data[plane];
  308. if (in != out)
  309. av_image_copy_plane(dst, out->linesize[plane],
  310. src, in->linesize[plane],
  311. width * s->bpc, height);
  312. if (!((1 << plane) & s->planes))
  313. continue;
  314. for (x = block; x < width; x += block)
  315. s->deblockv(dst + x * s->bpc, out->linesize[plane],
  316. FFMIN(block, height), s->ath, s->bth, s->gth, s->dth, s->max);
  317. for (y = block; y < height; y += block) {
  318. dst += out->linesize[plane] * block;
  319. s->deblockh(dst, out->linesize[plane],
  320. FFMIN(block, width),
  321. s->ath, s->bth, s->gth, s->dth, s->max);
  322. for (x = block; x < width; x += block) {
  323. s->deblockh(dst + x * s->bpc, out->linesize[plane],
  324. FFMIN(block, width - x),
  325. s->ath, s->bth, s->gth, s->dth, s->max);
  326. s->deblockv(dst + x * s->bpc, out->linesize[plane],
  327. FFMIN(block, height - y),
  328. s->ath, s->bth, s->gth, s->dth, s->max);
  329. }
  330. }
  331. }
  332. if (in != out)
  333. av_frame_free(&in);
  334. return ff_filter_frame(outlink, out);
  335. }
  336. #define OFFSET(x) offsetof(DeblockContext, x)
  337. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  338. static const AVOption deblock_options[] = {
  339. { "filter", "set type of filter", OFFSET(filter), AV_OPT_TYPE_INT, {.i64=STRONG},0, 1, FLAGS, "filter" },
  340. { "weak", 0, 0, AV_OPT_TYPE_CONST, {.i64=WEAK}, 0, 0, FLAGS, "filter" },
  341. { "strong", 0, 0, AV_OPT_TYPE_CONST, {.i64=STRONG},0, 0, FLAGS, "filter" },
  342. { "block", "set size of block", OFFSET(block), AV_OPT_TYPE_INT, {.i64=8}, 4, 512, FLAGS },
  343. { "alpha", "set 1st detection threshold", OFFSET(alpha), AV_OPT_TYPE_FLOAT, {.dbl=.098}, 0, 1, FLAGS },
  344. { "beta", "set 2nd detection threshold", OFFSET(beta), AV_OPT_TYPE_FLOAT, {.dbl=.05}, 0, 1, FLAGS },
  345. { "gamma", "set 3rd detection threshold", OFFSET(gamma), AV_OPT_TYPE_FLOAT, {.dbl=.05}, 0, 1, FLAGS },
  346. { "delta", "set 4th detection threshold", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=.05}, 0, 1, FLAGS },
  347. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS },
  348. { NULL },
  349. };
  350. static const AVFilterPad inputs[] = {
  351. {
  352. .name = "default",
  353. .type = AVMEDIA_TYPE_VIDEO,
  354. .filter_frame = filter_frame,
  355. },
  356. { NULL }
  357. };
  358. static const AVFilterPad outputs[] = {
  359. {
  360. .name = "default",
  361. .type = AVMEDIA_TYPE_VIDEO,
  362. .config_props = config_output,
  363. },
  364. { NULL }
  365. };
  366. AVFILTER_DEFINE_CLASS(deblock);
  367. AVFilter ff_vf_deblock = {
  368. .name = "deblock",
  369. .description = NULL_IF_CONFIG_SMALL("Deblock video."),
  370. .priv_size = sizeof(DeblockContext),
  371. .priv_class = &deblock_class,
  372. .query_formats = query_formats,
  373. .inputs = inputs,
  374. .outputs = outputs,
  375. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  376. };