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.

489 lines
30KB

  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. #include "libavutil/avstring.h"
  21. #include "libavutil/eval.h"
  22. #include "libavutil/imgutils.h"
  23. #include "libavutil/intreadwrite.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. typedef struct ChromaShiftContext {
  31. const AVClass *class;
  32. int cbh, cbv;
  33. int crh, crv;
  34. int rh, rv;
  35. int gh, gv;
  36. int bh, bv;
  37. int ah, av;
  38. int edge;
  39. int nb_planes;
  40. int depth;
  41. int height[4];
  42. int width[4];
  43. int linesize[4];
  44. AVFrame *in;
  45. int is_rgbashift;
  46. int (*filter_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  47. } ChromaShiftContext;
  48. static int query_formats(AVFilterContext *ctx)
  49. {
  50. static const enum AVPixelFormat yuv_pix_fmts[] = {
  51. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
  52. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
  53. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  54. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  55. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV440P10,
  56. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  57. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  58. AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
  59. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
  60. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  61. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  62. AV_PIX_FMT_NONE
  63. };
  64. static const enum AVPixelFormat rgb_pix_fmts[] = {
  65. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP9,
  66. AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
  67. AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  68. AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  69. AV_PIX_FMT_NONE
  70. };
  71. const enum AVPixelFormat *pix_fmts;
  72. AVFilterFormats *fmts_list;
  73. if (!strcmp(ctx->filter->name, "rgbashift"))
  74. pix_fmts = rgb_pix_fmts;
  75. else
  76. pix_fmts = yuv_pix_fmts;
  77. fmts_list = ff_make_format_list(pix_fmts);
  78. if (!fmts_list)
  79. return AVERROR(ENOMEM);
  80. return ff_set_common_formats(ctx, fmts_list);
  81. }
  82. #define DEFINE_SMEAR(depth, type, div) \
  83. static int smear_slice ## depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
  84. { \
  85. ChromaShiftContext *s = ctx->priv; \
  86. AVFrame *in = s->in; \
  87. AVFrame *out = arg; \
  88. const int sulinesize = in->linesize[1] / div; \
  89. const int svlinesize = in->linesize[2] / div; \
  90. const int ulinesize = out->linesize[1] / div; \
  91. const int vlinesize = out->linesize[2] / div; \
  92. const int cbh = s->cbh; \
  93. const int cbv = s->cbv; \
  94. const int crh = s->crh; \
  95. const int crv = s->crv; \
  96. const int h = s->height[1]; \
  97. const int w = s->width[1]; \
  98. const int slice_start = (h * jobnr) / nb_jobs; \
  99. const int slice_end = (h * (jobnr+1)) / nb_jobs; \
  100. const type *su = (const type *)in->data[1]; \
  101. const type *sv = (const type *)in->data[2]; \
  102. type *du = (type *)out->data[1] + slice_start * ulinesize; \
  103. type *dv = (type *)out->data[2] + slice_start * vlinesize; \
  104. \
  105. for (int y = slice_start; y < slice_end; y++) { \
  106. const int duy = av_clip(y - cbv, 0, h-1) * sulinesize; \
  107. const int dvy = av_clip(y - crv, 0, h-1) * svlinesize; \
  108. \
  109. for (int x = 0; x < w; x++) { \
  110. du[x] = su[av_clip(x - cbh, 0, w - 1) + duy]; \
  111. dv[x] = sv[av_clip(x - crh, 0, w - 1) + dvy]; \
  112. } \
  113. \
  114. du += ulinesize; \
  115. dv += vlinesize; \
  116. } \
  117. \
  118. return 0; \
  119. }
  120. DEFINE_SMEAR(8, uint8_t, 1)
  121. DEFINE_SMEAR(16, uint16_t, 2)
  122. #define DEFINE_WRAP(depth, type, div) \
  123. static int wrap_slice ## depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
  124. { \
  125. ChromaShiftContext *s = ctx->priv; \
  126. AVFrame *in = s->in; \
  127. AVFrame *out = arg; \
  128. const int sulinesize = in->linesize[1] / div; \
  129. const int svlinesize = in->linesize[2] / div; \
  130. const int ulinesize = out->linesize[1] / div; \
  131. const int vlinesize = out->linesize[2] / div; \
  132. const int cbh = s->cbh; \
  133. const int cbv = s->cbv; \
  134. const int crh = s->crh; \
  135. const int crv = s->crv; \
  136. const int h = s->height[1]; \
  137. const int w = s->width[1]; \
  138. const int slice_start = (h * jobnr) / nb_jobs; \
  139. const int slice_end = (h * (jobnr+1)) / nb_jobs; \
  140. const type *su = (const type *)in->data[1]; \
  141. const type *sv = (const type *)in->data[2]; \
  142. type *du = (type *)out->data[1] + slice_start * ulinesize; \
  143. type *dv = (type *)out->data[2] + slice_start * vlinesize; \
  144. \
  145. for (int y = slice_start; y < slice_end; y++) { \
  146. int uy = (y - cbv) % h; \
  147. int vy = (y - crv) % h; \
  148. \
  149. if (uy < 0) \
  150. uy += h; \
  151. if (vy < 0) \
  152. vy += h; \
  153. \
  154. for (int x = 0; x < w; x++) { \
  155. int ux = (x - cbh) % w; \
  156. int vx = (x - crh) % w; \
  157. \
  158. if (ux < 0) \
  159. ux += w; \
  160. if (vx < 0) \
  161. vx += w; \
  162. \
  163. du[x] = su[ux + uy * sulinesize]; \
  164. dv[x] = sv[vx + vy * svlinesize]; \
  165. } \
  166. \
  167. du += ulinesize; \
  168. dv += vlinesize; \
  169. } \
  170. \
  171. return 0; \
  172. }
  173. DEFINE_WRAP(8, uint8_t, 1)
  174. DEFINE_WRAP(16, uint16_t, 2)
  175. #define DEFINE_RGBASMEAR(depth, type, div) \
  176. static int rgbasmear_slice ## depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
  177. { \
  178. ChromaShiftContext *s = ctx->priv; \
  179. AVFrame *in = s->in; \
  180. AVFrame *out = arg; \
  181. const int srlinesize = in->linesize[2] / div; \
  182. const int sglinesize = in->linesize[0] / div; \
  183. const int sblinesize = in->linesize[1] / div; \
  184. const int salinesize = in->linesize[3] / div; \
  185. const int rlinesize = out->linesize[2] / div; \
  186. const int glinesize = out->linesize[0] / div; \
  187. const int blinesize = out->linesize[1] / div; \
  188. const int alinesize = out->linesize[3] / div; \
  189. const int rh = s->rh; \
  190. const int rv = s->rv; \
  191. const int gh = s->gh; \
  192. const int gv = s->gv; \
  193. const int bh = s->bh; \
  194. const int bv = s->bv; \
  195. const int ah = s->ah; \
  196. const int av = s->av; \
  197. const int h = s->height[1]; \
  198. const int w = s->width[1]; \
  199. const int slice_start = (h * jobnr) / nb_jobs; \
  200. const int slice_end = (h * (jobnr+1)) / nb_jobs; \
  201. const type *sr = (const type *)in->data[2]; \
  202. const type *sg = (const type *)in->data[0]; \
  203. const type *sb = (const type *)in->data[1]; \
  204. const type *sa = (const type *)in->data[3]; \
  205. type *dr = (type *)out->data[2] + slice_start * rlinesize; \
  206. type *dg = (type *)out->data[0] + slice_start * glinesize; \
  207. type *db = (type *)out->data[1] + slice_start * blinesize; \
  208. type *da = (type *)out->data[3] + slice_start * alinesize; \
  209. \
  210. for (int y = slice_start; y < slice_end; y++) { \
  211. const int ry = av_clip(y - rv, 0, h-1) * srlinesize; \
  212. const int gy = av_clip(y - gv, 0, h-1) * sglinesize; \
  213. const int by = av_clip(y - bv, 0, h-1) * sblinesize; \
  214. int ay; \
  215. \
  216. for (int x = 0; x < w; x++) { \
  217. dr[x] = sr[av_clip(x - rh, 0, w - 1) + ry]; \
  218. dg[x] = sg[av_clip(x - gh, 0, w - 1) + gy]; \
  219. db[x] = sb[av_clip(x - bh, 0, w - 1) + by]; \
  220. } \
  221. \
  222. dr += rlinesize; \
  223. dg += glinesize; \
  224. db += blinesize; \
  225. \
  226. if (s->nb_planes < 4) \
  227. continue; \
  228. ay = av_clip(y - av, 0, h-1) * salinesize; \
  229. for (int x = 0; x < w; x++) { \
  230. da[x] = sa[av_clip(x - ah, 0, w - 1) + ay]; \
  231. } \
  232. \
  233. da += alinesize; \
  234. } \
  235. \
  236. return 0; \
  237. }
  238. DEFINE_RGBASMEAR(8, uint8_t, 1)
  239. DEFINE_RGBASMEAR(16, uint16_t, 2)
  240. #define DEFINE_RGBAWRAP(depth, type, div) \
  241. static int rgbawrap_slice ## depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
  242. { \
  243. ChromaShiftContext *s = ctx->priv; \
  244. AVFrame *in = s->in; \
  245. AVFrame *out = arg; \
  246. const int srlinesize = in->linesize[2] / div; \
  247. const int sglinesize = in->linesize[0] / div; \
  248. const int sblinesize = in->linesize[1] / div; \
  249. const int salinesize = in->linesize[3] / div; \
  250. const int rlinesize = out->linesize[2] / div; \
  251. const int glinesize = out->linesize[0] / div; \
  252. const int blinesize = out->linesize[1] / div; \
  253. const int alinesize = out->linesize[3] / div; \
  254. const int rh = s->rh; \
  255. const int rv = s->rv; \
  256. const int gh = s->gh; \
  257. const int gv = s->gv; \
  258. const int bh = s->bh; \
  259. const int bv = s->bv; \
  260. const int ah = s->ah; \
  261. const int av = s->av; \
  262. const int h = s->height[1]; \
  263. const int w = s->width[1]; \
  264. const int slice_start = (h * jobnr) / nb_jobs; \
  265. const int slice_end = (h * (jobnr+1)) / nb_jobs; \
  266. const type *sr = (const type *)in->data[2]; \
  267. const type *sg = (const type *)in->data[0]; \
  268. const type *sb = (const type *)in->data[1]; \
  269. const type *sa = (const type *)in->data[3]; \
  270. type *dr = (type *)out->data[2] + slice_start * rlinesize; \
  271. type *dg = (type *)out->data[0] + slice_start * glinesize; \
  272. type *db = (type *)out->data[1] + slice_start * blinesize; \
  273. type *da = (type *)out->data[3] + slice_start * alinesize; \
  274. \
  275. for (int y = slice_start; y < slice_end; y++) { \
  276. int ry = (y - rv) % h; \
  277. int gy = (y - gv) % h; \
  278. int by = (y - bv) % h; \
  279. \
  280. if (ry < 0) \
  281. ry += h; \
  282. if (gy < 0) \
  283. gy += h; \
  284. if (by < 0) \
  285. by += h; \
  286. \
  287. for (int x = 0; x < w; x++) { \
  288. int rx = (x - rh) % w; \
  289. int gx = (x - gh) % w; \
  290. int bx = (x - bh) % w; \
  291. \
  292. if (rx < 0) \
  293. rx += w; \
  294. if (gx < 0) \
  295. gx += w; \
  296. if (bx < 0) \
  297. bx += w; \
  298. dr[x] = sr[rx + ry * srlinesize]; \
  299. dg[x] = sg[gx + gy * sglinesize]; \
  300. db[x] = sb[bx + by * sblinesize]; \
  301. } \
  302. \
  303. dr += rlinesize; \
  304. dg += glinesize; \
  305. db += blinesize; \
  306. \
  307. if (s->nb_planes < 4) \
  308. continue; \
  309. for (int x = 0; x < w; x++) { \
  310. int ax = (x - ah) % w; \
  311. int ay = (x - av) % h; \
  312. \
  313. if (ax < 0) \
  314. ax += w; \
  315. if (ay < 0) \
  316. ay += h; \
  317. da[x] = sa[ax + ay * salinesize]; \
  318. } \
  319. \
  320. da += alinesize; \
  321. } \
  322. \
  323. return 0; \
  324. }
  325. DEFINE_RGBAWRAP(8, uint8_t, 1)
  326. DEFINE_RGBAWRAP(16, uint16_t, 2)
  327. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  328. {
  329. AVFilterContext *ctx = inlink->dst;
  330. AVFilterLink *outlink = ctx->outputs[0];
  331. ChromaShiftContext *s = ctx->priv;
  332. AVFrame *out;
  333. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  334. if (!out) {
  335. av_frame_free(&in);
  336. return AVERROR(ENOMEM);
  337. }
  338. av_frame_copy_props(out, in);
  339. s->in = in;
  340. if (!s->is_rgbashift) {
  341. av_image_copy_plane(out->data[0],
  342. out->linesize[0],
  343. in->data[0], in->linesize[0],
  344. s->linesize[0], s->height[0]);
  345. }
  346. ctx->internal->execute(ctx, s->filter_slice, out, NULL,
  347. FFMIN3(s->height[1],
  348. s->height[2],
  349. ff_filter_get_nb_threads(ctx)));
  350. s->in = NULL;
  351. av_frame_free(&in);
  352. return ff_filter_frame(outlink, out);
  353. }
  354. static int config_input(AVFilterLink *inlink)
  355. {
  356. AVFilterContext *ctx = inlink->dst;
  357. ChromaShiftContext *s = ctx->priv;
  358. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  359. s->is_rgbashift = !strcmp(ctx->filter->name, "rgbashift");
  360. s->depth = desc->comp[0].depth;
  361. s->nb_planes = desc->nb_components;
  362. if (s->is_rgbashift) {
  363. if (s->edge)
  364. s->filter_slice = s->depth > 8 ? rgbawrap_slice16 : rgbawrap_slice8;
  365. else
  366. s->filter_slice = s->depth > 8 ? rgbasmear_slice16 : rgbasmear_slice8;
  367. } else {
  368. if (s->edge)
  369. s->filter_slice = s->depth > 8 ? wrap_slice16 : wrap_slice8;
  370. else
  371. s->filter_slice = s->depth > 8 ? smear_slice16 : smear_slice8;
  372. }
  373. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  374. s->height[0] = s->height[3] = inlink->h;
  375. s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  376. s->width[0] = s->width[3] = inlink->w;
  377. return av_image_fill_linesizes(s->linesize, inlink->format, inlink->w);
  378. }
  379. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  380. char *res, int res_len, int flags)
  381. {
  382. int ret;
  383. ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
  384. if (ret < 0)
  385. return ret;
  386. return config_input(ctx->inputs[0]);
  387. }
  388. #define OFFSET(x) offsetof(ChromaShiftContext, x)
  389. #define VFR AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
  390. static const AVOption chromashift_options[] = {
  391. { "cbh", "shift chroma-blue horizontally", OFFSET(cbh), AV_OPT_TYPE_INT, {.i64=0}, -255, 255, .flags = VFR },
  392. { "cbv", "shift chroma-blue vertically", OFFSET(cbv), AV_OPT_TYPE_INT, {.i64=0}, -255, 255, .flags = VFR },
  393. { "crh", "shift chroma-red horizontally", OFFSET(crh), AV_OPT_TYPE_INT, {.i64=0}, -255, 255, .flags = VFR },
  394. { "crv", "shift chroma-red vertically", OFFSET(crv), AV_OPT_TYPE_INT, {.i64=0}, -255, 255, .flags = VFR },
  395. { "edge", "set edge operation", OFFSET(edge), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, .flags = VFR, "edge" },
  396. { "smear", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, .flags = VFR, "edge" },
  397. { "wrap", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, .flags = VFR, "edge" },
  398. { NULL },
  399. };
  400. static const AVFilterPad inputs[] = {
  401. {
  402. .name = "default",
  403. .type = AVMEDIA_TYPE_VIDEO,
  404. .filter_frame = filter_frame,
  405. .config_props = config_input,
  406. },
  407. { NULL }
  408. };
  409. static const AVFilterPad outputs[] = {
  410. {
  411. .name = "default",
  412. .type = AVMEDIA_TYPE_VIDEO,
  413. },
  414. { NULL }
  415. };
  416. AVFILTER_DEFINE_CLASS(chromashift);
  417. AVFilter ff_vf_chromashift = {
  418. .name = "chromashift",
  419. .description = NULL_IF_CONFIG_SMALL("Shift chroma."),
  420. .priv_size = sizeof(ChromaShiftContext),
  421. .priv_class = &chromashift_class,
  422. .query_formats = query_formats,
  423. .outputs = outputs,
  424. .inputs = inputs,
  425. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  426. .process_command = process_command,
  427. };
  428. static const AVOption rgbashift_options[] = {
  429. { "rh", "shift red horizontally", OFFSET(rh), AV_OPT_TYPE_INT, {.i64=0}, -255, 255, .flags = VFR },
  430. { "rv", "shift red vertically", OFFSET(rv), AV_OPT_TYPE_INT, {.i64=0}, -255, 255, .flags = VFR },
  431. { "gh", "shift green horizontally", OFFSET(gh), AV_OPT_TYPE_INT, {.i64=0}, -255, 255, .flags = VFR },
  432. { "gv", "shift green vertically", OFFSET(gv), AV_OPT_TYPE_INT, {.i64=0}, -255, 255, .flags = VFR },
  433. { "bh", "shift blue horizontally", OFFSET(bh), AV_OPT_TYPE_INT, {.i64=0}, -255, 255, .flags = VFR },
  434. { "bv", "shift blue vertically", OFFSET(bv), AV_OPT_TYPE_INT, {.i64=0}, -255, 255, .flags = VFR },
  435. { "ah", "shift alpha horizontally", OFFSET(ah), AV_OPT_TYPE_INT, {.i64=0}, -255, 255, .flags = VFR },
  436. { "av", "shift alpha vertically", OFFSET(av), AV_OPT_TYPE_INT, {.i64=0}, -255, 255, .flags = VFR },
  437. { "edge", "set edge operation", OFFSET(edge), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, .flags = VFR, "edge" },
  438. { "smear", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, .flags = VFR, "edge" },
  439. { "wrap", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, .flags = VFR, "edge" },
  440. { NULL },
  441. };
  442. AVFILTER_DEFINE_CLASS(rgbashift);
  443. AVFilter ff_vf_rgbashift = {
  444. .name = "rgbashift",
  445. .description = NULL_IF_CONFIG_SMALL("Shift RGBA."),
  446. .priv_size = sizeof(ChromaShiftContext),
  447. .priv_class = &rgbashift_class,
  448. .query_formats = query_formats,
  449. .outputs = outputs,
  450. .inputs = inputs,
  451. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  452. .process_command = process_command,
  453. };