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.

416 lines
22KB

  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_YUVA422P12, AV_PIX_FMT_YUVA444P12,
  72. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  73. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  74. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  75. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  76. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  77. AV_PIX_FMT_NONE
  78. };
  79. AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
  80. if (!formats)
  81. return AVERROR(ENOMEM);
  82. return ff_set_common_formats(ctx, formats);
  83. }
  84. #define WEAK_HFILTER(name, type, ldiv) \
  85. static void deblockh##name##_weak(uint8_t *dstp, ptrdiff_t dst_linesize, int block, \
  86. int ath, int bth, int gth, int dth, int max) \
  87. { \
  88. type *dst; \
  89. int x; \
  90. \
  91. dst = (type *)dstp; \
  92. dst_linesize /= ldiv; \
  93. \
  94. for (x = 0; x < block; x++) { \
  95. int delta = dst[x] - dst[x - dst_linesize]; \
  96. int A, B, C, D, a, b, c, d; \
  97. \
  98. if (FFABS(delta) >= ath || \
  99. FFABS(dst[x - 1 * dst_linesize] - dst[x - 2 * dst_linesize]) >= bth || \
  100. FFABS(dst[x + 0 * dst_linesize] - dst[x + 1 * dst_linesize]) >= gth) \
  101. continue; \
  102. \
  103. A = dst[x - 2 * dst_linesize]; \
  104. B = dst[x - 1 * dst_linesize]; \
  105. C = dst[x + 0 * dst_linesize]; \
  106. D = dst[x + 1 * dst_linesize]; \
  107. \
  108. a = A + delta / 8; \
  109. b = B + delta / 2; \
  110. c = C - delta / 2; \
  111. d = D - delta / 8; \
  112. \
  113. dst[x - 2 * dst_linesize] = av_clip(a, 0, max); \
  114. dst[x - 1 * dst_linesize] = av_clip(b, 0, max); \
  115. dst[x + 0 * dst_linesize] = av_clip(c, 0, max); \
  116. dst[x + 1 * dst_linesize] = av_clip(d, 0, max); \
  117. } \
  118. }
  119. WEAK_HFILTER(8, uint8_t, 1)
  120. WEAK_HFILTER(16, uint16_t, 2)
  121. #define WEAK_VFILTER(name, type, ldiv) \
  122. static void deblockv##name##_weak(uint8_t *dstp, ptrdiff_t dst_linesize, int block, \
  123. int ath, int bth, int gth, int dth, int max) \
  124. { \
  125. type *dst; \
  126. int y; \
  127. \
  128. dst = (type *)dstp; \
  129. dst_linesize /= ldiv; \
  130. \
  131. for (y = 0; y < block; y++) { \
  132. int delta = dst[0] - dst[-1]; \
  133. int A, B, C, D, a, b, c, d; \
  134. \
  135. if (FFABS(delta) >= ath || \
  136. FFABS(dst[-1] - dst[-2]) >= bth || \
  137. FFABS(dst[0] - dst[1]) >= gth) \
  138. continue; \
  139. \
  140. A = dst[-2]; \
  141. B = dst[-1]; \
  142. C = dst[+0]; \
  143. D = dst[+1]; \
  144. \
  145. a = A + delta / 8; \
  146. b = B + delta / 2; \
  147. c = C - delta / 2; \
  148. d = D - delta / 8; \
  149. \
  150. dst[-2] = av_clip(a, 0, max); \
  151. dst[-1] = av_clip(b, 0, max); \
  152. dst[+0] = av_clip(c, 0, max); \
  153. dst[+1] = av_clip(d, 0, max); \
  154. \
  155. dst += dst_linesize; \
  156. } \
  157. }
  158. WEAK_VFILTER(8, uint8_t, 1)
  159. WEAK_VFILTER(16, uint16_t, 2)
  160. #define STRONG_HFILTER(name, type, ldiv) \
  161. static void deblockh##name##_strong(uint8_t *dstp, ptrdiff_t dst_linesize, int block,\
  162. int ath, int bth, int gth, int dth, int max) \
  163. { \
  164. type *dst; \
  165. int x; \
  166. \
  167. dst = (type *)dstp; \
  168. dst_linesize /= ldiv; \
  169. \
  170. for (x = 0; x < block; x++) { \
  171. int A, B, C, D, E, F, a, b, c, d, e, f; \
  172. int delta = dst[x] - dst[x - dst_linesize]; \
  173. \
  174. if (FFABS(delta) >= ath || \
  175. FFABS(dst[x - 1 * dst_linesize] - dst[x - 2 * dst_linesize]) >= bth || \
  176. FFABS(dst[x + 1 * dst_linesize] - dst[x + 2 * dst_linesize]) >= gth || \
  177. FFABS(dst[x + 0 * dst_linesize] - dst[x + 1 * dst_linesize]) >= dth) \
  178. continue; \
  179. \
  180. A = dst[x - 3 * dst_linesize]; \
  181. B = dst[x - 2 * dst_linesize]; \
  182. C = dst[x - 1 * dst_linesize]; \
  183. D = dst[x + 0 * dst_linesize]; \
  184. E = dst[x + 1 * dst_linesize]; \
  185. F = dst[x + 2 * dst_linesize]; \
  186. \
  187. a = A + delta / 8; \
  188. b = B + delta / 4; \
  189. c = C + delta / 2; \
  190. d = D - delta / 2; \
  191. e = E - delta / 4; \
  192. f = F - delta / 8; \
  193. \
  194. dst[x - 3 * dst_linesize] = av_clip(a, 0, max); \
  195. dst[x - 2 * dst_linesize] = av_clip(b, 0, max); \
  196. dst[x - 1 * dst_linesize] = av_clip(c, 0, max); \
  197. dst[x + 0 * dst_linesize] = av_clip(d, 0, max); \
  198. dst[x + 1 * dst_linesize] = av_clip(e, 0, max); \
  199. dst[x + 2 * dst_linesize] = av_clip(f, 0, max); \
  200. } \
  201. }
  202. STRONG_HFILTER(8, uint8_t, 1)
  203. STRONG_HFILTER(16, uint16_t, 2)
  204. #define STRONG_VFILTER(name, type, ldiv) \
  205. static void deblockv##name##_strong(uint8_t *dstp, ptrdiff_t dst_linesize, int block,\
  206. int ath, int bth, int gth, int dth, int max) \
  207. { \
  208. type *dst; \
  209. int y; \
  210. \
  211. dst = (type *)dstp; \
  212. dst_linesize /= ldiv; \
  213. \
  214. for (y = 0; y < block; y++) { \
  215. int A, B, C, D, E, F, a, b, c, d, e, f; \
  216. int delta = dst[0] - dst[-1]; \
  217. \
  218. if (FFABS(delta) >= ath || \
  219. FFABS(dst[-1] - dst[-2]) >= bth || \
  220. FFABS(dst[+1] - dst[+2]) >= gth || \
  221. FFABS(dst[+0] - dst[+1]) >= dth) \
  222. continue; \
  223. \
  224. A = dst[-3]; \
  225. B = dst[-2]; \
  226. C = dst[-1]; \
  227. D = dst[+0]; \
  228. E = dst[+1]; \
  229. F = dst[+2]; \
  230. \
  231. a = A + delta / 8; \
  232. b = B + delta / 4; \
  233. c = C + delta / 2; \
  234. d = D - delta / 2; \
  235. e = E - delta / 4; \
  236. f = F - delta / 8; \
  237. \
  238. dst[-3] = av_clip(a, 0, max); \
  239. dst[-2] = av_clip(b, 0, max); \
  240. dst[-1] = av_clip(c, 0, max); \
  241. dst[+0] = av_clip(d, 0, max); \
  242. dst[+1] = av_clip(e, 0, max); \
  243. dst[+2] = av_clip(f, 0, max); \
  244. \
  245. dst += dst_linesize; \
  246. } \
  247. }
  248. STRONG_VFILTER(8, uint8_t, 1)
  249. STRONG_VFILTER(16, uint16_t, 2)
  250. static int config_output(AVFilterLink *outlink)
  251. {
  252. AVFilterContext *ctx = outlink->src;
  253. DeblockContext *s = ctx->priv;
  254. AVFilterLink *inlink = ctx->inputs[0];
  255. s->desc = av_pix_fmt_desc_get(outlink->format);
  256. if (!s->desc)
  257. return AVERROR_BUG;
  258. s->nb_planes = av_pix_fmt_count_planes(outlink->format);
  259. s->depth = s->desc->comp[0].depth;
  260. s->bpc = (s->depth + 7) / 8;
  261. s->max = (1 << s->depth) - 1;
  262. s->ath = s->alpha * s->max;
  263. s->bth = s->beta * s->max;
  264. s->gth = s->gamma * s->max;
  265. s->dth = s->delta * s->max;
  266. if (s->depth <= 8 && s->filter == WEAK) {
  267. s->deblockh = deblockh8_weak;
  268. s->deblockv = deblockv8_weak;
  269. } else if (s->depth > 8 && s->filter == WEAK) {
  270. s->deblockh = deblockh16_weak;
  271. s->deblockv = deblockv16_weak;
  272. }
  273. if (s->depth <= 8 && s->filter == STRONG) {
  274. s->deblockh = deblockh8_strong;
  275. s->deblockv = deblockv8_strong;
  276. } else if (s->depth > 8 && s->filter == STRONG) {
  277. s->deblockh = deblockh16_strong;
  278. s->deblockv = deblockv16_strong;
  279. }
  280. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
  281. s->planewidth[0] = s->planewidth[3] = inlink->w;
  282. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
  283. s->planeheight[0] = s->planeheight[3] = inlink->h;
  284. return 0;
  285. }
  286. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  287. {
  288. AVFilterContext *ctx = inlink->dst;
  289. AVFilterLink *outlink = ctx->outputs[0];
  290. DeblockContext *s = ctx->priv;
  291. const int block = s->block;
  292. AVFrame *out;
  293. int plane, x, y;
  294. if (av_frame_is_writable(in)) {
  295. out = in;
  296. } else {
  297. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  298. if (!out) {
  299. av_frame_free(&in);
  300. return AVERROR(ENOMEM);
  301. }
  302. av_frame_copy_props(out, in);
  303. }
  304. for (plane = 0; plane < s->nb_planes; plane++) {
  305. const int width = s->planewidth[plane];
  306. const int height = s->planeheight[plane];
  307. const uint8_t *src = (const uint8_t *)in->data[plane];
  308. uint8_t *dst = (uint8_t *)out->data[plane];
  309. if (in != out)
  310. av_image_copy_plane(dst, out->linesize[plane],
  311. src, in->linesize[plane],
  312. width * s->bpc, height);
  313. if (!((1 << plane) & s->planes))
  314. continue;
  315. for (x = block; x < width; x += block)
  316. s->deblockv(dst + x * s->bpc, out->linesize[plane],
  317. FFMIN(block, height), s->ath, s->bth, s->gth, s->dth, s->max);
  318. for (y = block; y < height; y += block) {
  319. dst += out->linesize[plane] * block;
  320. s->deblockh(dst, out->linesize[plane],
  321. FFMIN(block, width),
  322. s->ath, s->bth, s->gth, s->dth, s->max);
  323. for (x = block; x < width; x += block) {
  324. s->deblockh(dst + x * s->bpc, out->linesize[plane],
  325. FFMIN(block, width - x),
  326. s->ath, s->bth, s->gth, s->dth, s->max);
  327. s->deblockv(dst + x * s->bpc, out->linesize[plane],
  328. FFMIN(block, height - y),
  329. s->ath, s->bth, s->gth, s->dth, s->max);
  330. }
  331. }
  332. }
  333. if (in != out)
  334. av_frame_free(&in);
  335. return ff_filter_frame(outlink, out);
  336. }
  337. #define OFFSET(x) offsetof(DeblockContext, x)
  338. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  339. static const AVOption deblock_options[] = {
  340. { "filter", "set type of filter", OFFSET(filter), AV_OPT_TYPE_INT, {.i64=STRONG},0, 1, FLAGS, "filter" },
  341. { "weak", 0, 0, AV_OPT_TYPE_CONST, {.i64=WEAK}, 0, 0, FLAGS, "filter" },
  342. { "strong", 0, 0, AV_OPT_TYPE_CONST, {.i64=STRONG},0, 0, FLAGS, "filter" },
  343. { "block", "set size of block", OFFSET(block), AV_OPT_TYPE_INT, {.i64=8}, 4, 512, FLAGS },
  344. { "alpha", "set 1st detection threshold", OFFSET(alpha), AV_OPT_TYPE_FLOAT, {.dbl=.098}, 0, 1, FLAGS },
  345. { "beta", "set 2nd detection threshold", OFFSET(beta), AV_OPT_TYPE_FLOAT, {.dbl=.05}, 0, 1, FLAGS },
  346. { "gamma", "set 3rd detection threshold", OFFSET(gamma), AV_OPT_TYPE_FLOAT, {.dbl=.05}, 0, 1, FLAGS },
  347. { "delta", "set 4th detection threshold", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=.05}, 0, 1, FLAGS },
  348. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS },
  349. { NULL },
  350. };
  351. static const AVFilterPad inputs[] = {
  352. {
  353. .name = "default",
  354. .type = AVMEDIA_TYPE_VIDEO,
  355. .filter_frame = filter_frame,
  356. },
  357. { NULL }
  358. };
  359. static const AVFilterPad outputs[] = {
  360. {
  361. .name = "default",
  362. .type = AVMEDIA_TYPE_VIDEO,
  363. .config_props = config_output,
  364. },
  365. { NULL }
  366. };
  367. AVFILTER_DEFINE_CLASS(deblock);
  368. AVFilter ff_vf_deblock = {
  369. .name = "deblock",
  370. .description = NULL_IF_CONFIG_SMALL("Deblock video."),
  371. .priv_size = sizeof(DeblockContext),
  372. .priv_class = &deblock_class,
  373. .query_formats = query_formats,
  374. .inputs = inputs,
  375. .outputs = outputs,
  376. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  377. };