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.

724 lines
23KB

  1. /*
  2. * Copyright (c) 2016 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/imgutils.h"
  21. #include "libavutil/pixdesc.h"
  22. #include "libavutil/opt.h"
  23. #include "avfilter.h"
  24. #include "filters.h"
  25. #include "formats.h"
  26. #include "framesync.h"
  27. #include "internal.h"
  28. #include "video.h"
  29. typedef struct PreMultiplyContext {
  30. const AVClass *class;
  31. int width[4], height[4];
  32. int linesize[4];
  33. int nb_planes;
  34. int planes;
  35. int inverse;
  36. int inplace;
  37. int half, depth, offset, max;
  38. FFFrameSync fs;
  39. void (*premultiply[4])(const uint8_t *msrc, const uint8_t *asrc,
  40. uint8_t *dst,
  41. ptrdiff_t mlinesize, ptrdiff_t alinesize,
  42. ptrdiff_t dlinesize,
  43. int w, int h,
  44. int half, int shift, int offset);
  45. } PreMultiplyContext;
  46. #define OFFSET(x) offsetof(PreMultiplyContext, x)
  47. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  48. static const AVOption options[] = {
  49. { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
  50. { "inplace","enable inplace mode", OFFSET(inplace), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
  51. { NULL }
  52. };
  53. #define premultiply_options options
  54. AVFILTER_DEFINE_CLASS(premultiply);
  55. static int query_formats(AVFilterContext *ctx)
  56. {
  57. PreMultiplyContext *s = ctx->priv;
  58. static const enum AVPixelFormat no_alpha_pix_fmts[] = {
  59. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  60. AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10,
  61. AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14,
  62. AV_PIX_FMT_YUV444P16,
  63. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  64. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  65. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
  66. AV_PIX_FMT_NONE
  67. };
  68. static const enum AVPixelFormat alpha_pix_fmts[] = {
  69. AV_PIX_FMT_YUVA444P,
  70. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P16,
  71. AV_PIX_FMT_GBRAP,
  72. AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  73. AV_PIX_FMT_NONE
  74. };
  75. return ff_set_common_formats(ctx, ff_make_format_list(s->inplace ? alpha_pix_fmts : no_alpha_pix_fmts));
  76. }
  77. static void premultiply8(const uint8_t *msrc, const uint8_t *asrc,
  78. uint8_t *dst,
  79. ptrdiff_t mlinesize, ptrdiff_t alinesize,
  80. ptrdiff_t dlinesize,
  81. int w, int h,
  82. int half, int shift, int offset)
  83. {
  84. int x, y;
  85. for (y = 0; y < h; y++) {
  86. for (x = 0; x < w; x++) {
  87. dst[x] = ((msrc[x] * (((asrc[x] >> 1) & 1) + asrc[x])) + 128) >> 8;
  88. }
  89. dst += dlinesize;
  90. msrc += mlinesize;
  91. asrc += alinesize;
  92. }
  93. }
  94. static void premultiply8yuv(const uint8_t *msrc, const uint8_t *asrc,
  95. uint8_t *dst,
  96. ptrdiff_t mlinesize, ptrdiff_t alinesize,
  97. ptrdiff_t dlinesize,
  98. int w, int h,
  99. int half, int shift, int offset)
  100. {
  101. int x, y;
  102. for (y = 0; y < h; y++) {
  103. for (x = 0; x < w; x++) {
  104. dst[x] = ((((msrc[x] - 128) * (((asrc[x] >> 1) & 1) + asrc[x]))) >> 8) + 128;
  105. }
  106. dst += dlinesize;
  107. msrc += mlinesize;
  108. asrc += alinesize;
  109. }
  110. }
  111. static void premultiply8offset(const uint8_t *msrc, const uint8_t *asrc,
  112. uint8_t *dst,
  113. ptrdiff_t mlinesize, ptrdiff_t alinesize,
  114. ptrdiff_t dlinesize,
  115. int w, int h,
  116. int half, int shift, int offset)
  117. {
  118. int x, y;
  119. for (y = 0; y < h; y++) {
  120. for (x = 0; x < w; x++) {
  121. dst[x] = ((((msrc[x] - offset) * (((asrc[x] >> 1) & 1) + asrc[x])) + 128) >> 8) + offset;
  122. }
  123. dst += dlinesize;
  124. msrc += mlinesize;
  125. asrc += alinesize;
  126. }
  127. }
  128. static void premultiply16(const uint8_t *mmsrc, const uint8_t *aasrc,
  129. uint8_t *ddst,
  130. ptrdiff_t mlinesize, ptrdiff_t alinesize,
  131. ptrdiff_t dlinesize,
  132. int w, int h,
  133. int half, int shift, int offset)
  134. {
  135. const uint16_t *msrc = (const uint16_t *)mmsrc;
  136. const uint16_t *asrc = (const uint16_t *)aasrc;
  137. uint16_t *dst = (uint16_t *)ddst;
  138. int x, y;
  139. for (y = 0; y < h; y++) {
  140. for (x = 0; x < w; x++) {
  141. dst[x] = ((msrc[x] * (((asrc[x] >> 1) & 1) + asrc[x])) + half) >> shift;
  142. }
  143. dst += dlinesize / 2;
  144. msrc += mlinesize / 2;
  145. asrc += alinesize / 2;
  146. }
  147. }
  148. static void premultiply16yuv(const uint8_t *mmsrc, const uint8_t *aasrc,
  149. uint8_t *ddst,
  150. ptrdiff_t mlinesize, ptrdiff_t alinesize,
  151. ptrdiff_t dlinesize,
  152. int w, int h,
  153. int half, int shift, int offset)
  154. {
  155. const uint16_t *msrc = (const uint16_t *)mmsrc;
  156. const uint16_t *asrc = (const uint16_t *)aasrc;
  157. uint16_t *dst = (uint16_t *)ddst;
  158. int x, y;
  159. for (y = 0; y < h; y++) {
  160. for (x = 0; x < w; x++) {
  161. dst[x] = ((((msrc[x] - half) * (((asrc[x] >> 1) & 1) + asrc[x]))) >> shift) + half;
  162. }
  163. dst += dlinesize / 2;
  164. msrc += mlinesize / 2;
  165. asrc += alinesize / 2;
  166. }
  167. }
  168. static void premultiply16offset(const uint8_t *mmsrc, const uint8_t *aasrc,
  169. uint8_t *ddst,
  170. ptrdiff_t mlinesize, ptrdiff_t alinesize,
  171. ptrdiff_t dlinesize,
  172. int w, int h,
  173. int half, int shift, int offset)
  174. {
  175. const uint16_t *msrc = (const uint16_t *)mmsrc;
  176. const uint16_t *asrc = (const uint16_t *)aasrc;
  177. uint16_t *dst = (uint16_t *)ddst;
  178. int x, y;
  179. for (y = 0; y < h; y++) {
  180. for (x = 0; x < w; x++) {
  181. dst[x] = ((((msrc[x] - offset) * (((asrc[x] >> 1) & 1) + asrc[x])) + half) >> shift) + offset;
  182. }
  183. dst += dlinesize / 2;
  184. msrc += mlinesize / 2;
  185. asrc += alinesize / 2;
  186. }
  187. }
  188. static void unpremultiply8(const uint8_t *msrc, const uint8_t *asrc,
  189. uint8_t *dst,
  190. ptrdiff_t mlinesize, ptrdiff_t alinesize,
  191. ptrdiff_t dlinesize,
  192. int w, int h,
  193. int half, int max, int offset)
  194. {
  195. int x, y;
  196. for (y = 0; y < h; y++) {
  197. for (x = 0; x < w; x++) {
  198. if (asrc[x] > 0 && asrc[x] < 255)
  199. dst[x] = FFMIN(msrc[x] * 255 / asrc[x], 255);
  200. else
  201. dst[x] = msrc[x];
  202. }
  203. dst += dlinesize;
  204. msrc += mlinesize;
  205. asrc += alinesize;
  206. }
  207. }
  208. static void unpremultiply8yuv(const uint8_t *msrc, const uint8_t *asrc,
  209. uint8_t *dst,
  210. ptrdiff_t mlinesize, ptrdiff_t alinesize,
  211. ptrdiff_t dlinesize,
  212. int w, int h,
  213. int half, int max, int offset)
  214. {
  215. int x, y;
  216. for (y = 0; y < h; y++) {
  217. for (x = 0; x < w; x++) {
  218. if (asrc[x] > 0 && asrc[x] < 255)
  219. dst[x] = FFMIN((msrc[x] - 128) * 255 / asrc[x] + 128, 255);
  220. else
  221. dst[x] = msrc[x];
  222. }
  223. dst += dlinesize;
  224. msrc += mlinesize;
  225. asrc += alinesize;
  226. }
  227. }
  228. static void unpremultiply8offset(const uint8_t *msrc, const uint8_t *asrc,
  229. uint8_t *dst,
  230. ptrdiff_t mlinesize, ptrdiff_t alinesize,
  231. ptrdiff_t dlinesize,
  232. int w, int h,
  233. int half, int max, int offset)
  234. {
  235. int x, y;
  236. for (y = 0; y < h; y++) {
  237. for (x = 0; x < w; x++) {
  238. if (asrc[x] > 0 && asrc[x] < 255)
  239. dst[x] = FFMIN(FFMAX(msrc[x] - offset, 0) * 255 / asrc[x] + offset, 255);
  240. else
  241. dst[x] = msrc[x];
  242. }
  243. dst += dlinesize;
  244. msrc += mlinesize;
  245. asrc += alinesize;
  246. }
  247. }
  248. static void unpremultiply16(const uint8_t *mmsrc, const uint8_t *aasrc,
  249. uint8_t *ddst,
  250. ptrdiff_t mlinesize, ptrdiff_t alinesize,
  251. ptrdiff_t dlinesize,
  252. int w, int h,
  253. int half, int max, int offset)
  254. {
  255. const uint16_t *msrc = (const uint16_t *)mmsrc;
  256. const uint16_t *asrc = (const uint16_t *)aasrc;
  257. uint16_t *dst = (uint16_t *)ddst;
  258. int x, y;
  259. for (y = 0; y < h; y++) {
  260. for (x = 0; x < w; x++) {
  261. if (asrc[x] > 0 && asrc[x] < max)
  262. dst[x] = FFMIN(msrc[x] * (unsigned)max / asrc[x], max);
  263. else
  264. dst[x] = msrc[x];
  265. }
  266. dst += dlinesize / 2;
  267. msrc += mlinesize / 2;
  268. asrc += alinesize / 2;
  269. }
  270. }
  271. static void unpremultiply16yuv(const uint8_t *mmsrc, const uint8_t *aasrc,
  272. uint8_t *ddst,
  273. ptrdiff_t mlinesize, ptrdiff_t alinesize,
  274. ptrdiff_t dlinesize,
  275. int w, int h,
  276. int half, int max, int offset)
  277. {
  278. const uint16_t *msrc = (const uint16_t *)mmsrc;
  279. const uint16_t *asrc = (const uint16_t *)aasrc;
  280. uint16_t *dst = (uint16_t *)ddst;
  281. int x, y;
  282. for (y = 0; y < h; y++) {
  283. for (x = 0; x < w; x++) {
  284. if (asrc[x] > 0 && asrc[x] < max)
  285. dst[x] = FFMAX(FFMIN((msrc[x] - half) * max / asrc[x], half - 1), -half) + half;
  286. else
  287. dst[x] = msrc[x];
  288. }
  289. dst += dlinesize / 2;
  290. msrc += mlinesize / 2;
  291. asrc += alinesize / 2;
  292. }
  293. }
  294. static void unpremultiply16offset(const uint8_t *mmsrc, const uint8_t *aasrc,
  295. uint8_t *ddst,
  296. ptrdiff_t mlinesize, ptrdiff_t alinesize,
  297. ptrdiff_t dlinesize,
  298. int w, int h,
  299. int half, int max, int offset)
  300. {
  301. const uint16_t *msrc = (const uint16_t *)mmsrc;
  302. const uint16_t *asrc = (const uint16_t *)aasrc;
  303. uint16_t *dst = (uint16_t *)ddst;
  304. int x, y;
  305. for (y = 0; y < h; y++) {
  306. for (x = 0; x < w; x++) {
  307. if (asrc[x] > 0 && asrc[x] < max)
  308. dst[x] = FFMAX(FFMIN(FFMAX(msrc[x] - offset, 0) * (unsigned)max / asrc[x] + offset, max), 0);
  309. else
  310. dst[x] = msrc[x];
  311. }
  312. dst += dlinesize / 2;
  313. msrc += mlinesize / 2;
  314. asrc += alinesize / 2;
  315. }
  316. }
  317. static int filter_frame(AVFilterContext *ctx,
  318. AVFrame **out, AVFrame *base, AVFrame *alpha)
  319. {
  320. PreMultiplyContext *s = ctx->priv;
  321. AVFilterLink *outlink = ctx->outputs[0];
  322. if (ctx->is_disabled) {
  323. *out = av_frame_clone(base);
  324. if (!*out)
  325. return AVERROR(ENOMEM);
  326. } else {
  327. int p, full, limited;
  328. *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  329. if (!*out)
  330. return AVERROR(ENOMEM);
  331. av_frame_copy_props(*out, base);
  332. full = base->color_range == AVCOL_RANGE_JPEG;
  333. limited = base->color_range == AVCOL_RANGE_MPEG;
  334. if (s->inverse) {
  335. switch (outlink->format) {
  336. case AV_PIX_FMT_YUV444P:
  337. case AV_PIX_FMT_YUVA444P:
  338. s->premultiply[0] = full ? unpremultiply8 : unpremultiply8offset;
  339. s->premultiply[1] = s->premultiply[2] = unpremultiply8yuv;
  340. break;
  341. case AV_PIX_FMT_YUVJ444P:
  342. s->premultiply[0] = unpremultiply8;
  343. s->premultiply[1] = s->premultiply[2] = unpremultiply8yuv;
  344. break;
  345. case AV_PIX_FMT_GBRP:
  346. case AV_PIX_FMT_GBRAP:
  347. s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? unpremultiply8offset : unpremultiply8;
  348. break;
  349. case AV_PIX_FMT_YUV444P9:
  350. case AV_PIX_FMT_YUVA444P9:
  351. case AV_PIX_FMT_YUV444P10:
  352. case AV_PIX_FMT_YUVA444P10:
  353. case AV_PIX_FMT_YUV444P12:
  354. case AV_PIX_FMT_YUV444P14:
  355. case AV_PIX_FMT_YUV444P16:
  356. case AV_PIX_FMT_YUVA444P16:
  357. s->premultiply[0] = full ? unpremultiply16 : unpremultiply16offset;
  358. s->premultiply[1] = s->premultiply[2] = unpremultiply16yuv;
  359. break;
  360. case AV_PIX_FMT_GBRP9:
  361. case AV_PIX_FMT_GBRP10:
  362. case AV_PIX_FMT_GBRAP10:
  363. case AV_PIX_FMT_GBRP12:
  364. case AV_PIX_FMT_GBRAP12:
  365. case AV_PIX_FMT_GBRP14:
  366. case AV_PIX_FMT_GBRP16:
  367. case AV_PIX_FMT_GBRAP16:
  368. s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? unpremultiply16offset : unpremultiply16;
  369. break;
  370. case AV_PIX_FMT_GRAY8:
  371. s->premultiply[0] = limited ? unpremultiply8offset : unpremultiply8;
  372. break;
  373. case AV_PIX_FMT_GRAY9:
  374. case AV_PIX_FMT_GRAY10:
  375. case AV_PIX_FMT_GRAY12:
  376. case AV_PIX_FMT_GRAY16:
  377. s->premultiply[0] = limited ? unpremultiply16offset : unpremultiply16;
  378. break;
  379. }
  380. } else {
  381. switch (outlink->format) {
  382. case AV_PIX_FMT_YUV444P:
  383. case AV_PIX_FMT_YUVA444P:
  384. s->premultiply[0] = full ? premultiply8 : premultiply8offset;
  385. s->premultiply[1] = s->premultiply[2] = premultiply8yuv;
  386. break;
  387. case AV_PIX_FMT_YUVJ444P:
  388. s->premultiply[0] = premultiply8;
  389. s->premultiply[1] = s->premultiply[2] = premultiply8yuv;
  390. break;
  391. case AV_PIX_FMT_GBRP:
  392. case AV_PIX_FMT_GBRAP:
  393. s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? premultiply8offset : premultiply8;
  394. break;
  395. case AV_PIX_FMT_YUV444P9:
  396. case AV_PIX_FMT_YUVA444P9:
  397. case AV_PIX_FMT_YUV444P10:
  398. case AV_PIX_FMT_YUVA444P10:
  399. case AV_PIX_FMT_YUV444P12:
  400. case AV_PIX_FMT_YUV444P14:
  401. case AV_PIX_FMT_YUV444P16:
  402. case AV_PIX_FMT_YUVA444P16:
  403. s->premultiply[0] = full ? premultiply16 : premultiply16offset;
  404. s->premultiply[1] = s->premultiply[2] = premultiply16yuv;
  405. break;
  406. case AV_PIX_FMT_GBRP9:
  407. case AV_PIX_FMT_GBRP10:
  408. case AV_PIX_FMT_GBRAP10:
  409. case AV_PIX_FMT_GBRP12:
  410. case AV_PIX_FMT_GBRAP12:
  411. case AV_PIX_FMT_GBRP14:
  412. case AV_PIX_FMT_GBRP16:
  413. case AV_PIX_FMT_GBRAP16:
  414. s->premultiply[0] = s->premultiply[1] = s->premultiply[2] = limited ? premultiply16offset : premultiply16;
  415. break;
  416. case AV_PIX_FMT_GRAY8:
  417. s->premultiply[0] = limited ? premultiply8offset : premultiply8;
  418. break;
  419. case AV_PIX_FMT_GRAY9:
  420. case AV_PIX_FMT_GRAY10:
  421. case AV_PIX_FMT_GRAY12:
  422. case AV_PIX_FMT_GRAY16:
  423. s->premultiply[0] = limited ? premultiply16offset : premultiply16;
  424. break;
  425. }
  426. }
  427. for (p = 0; p < s->nb_planes; p++) {
  428. if (!((1 << p) & s->planes) || p == 3) {
  429. av_image_copy_plane((*out)->data[p], (*out)->linesize[p], base->data[p], base->linesize[p],
  430. s->linesize[p], s->height[p]);
  431. continue;
  432. }
  433. s->premultiply[p](base->data[p], s->inplace ? alpha->data[3] : alpha->data[0],
  434. (*out)->data[p],
  435. base->linesize[p], s->inplace ? alpha->linesize[3] : alpha->linesize[0],
  436. (*out)->linesize[p],
  437. s->width[p], s->height[p],
  438. s->half, s->inverse ? s->max : s->depth, s->offset);
  439. }
  440. }
  441. return 0;
  442. }
  443. static int process_frame(FFFrameSync *fs)
  444. {
  445. AVFilterContext *ctx = fs->parent;
  446. PreMultiplyContext *s = fs->opaque;
  447. AVFilterLink *outlink = ctx->outputs[0];
  448. AVFrame *out = NULL, *base, *alpha;
  449. int ret;
  450. if ((ret = ff_framesync_get_frame(&s->fs, 0, &base, 0)) < 0 ||
  451. (ret = ff_framesync_get_frame(&s->fs, 1, &alpha, 0)) < 0)
  452. return ret;
  453. if ((ret = filter_frame(ctx, &out, base, alpha)) < 0)
  454. return ret;
  455. out->pts = av_rescale_q(base->pts, s->fs.time_base, outlink->time_base);
  456. return ff_filter_frame(outlink, out);
  457. }
  458. static int config_input(AVFilterLink *inlink)
  459. {
  460. AVFilterContext *ctx = inlink->dst;
  461. PreMultiplyContext *s = ctx->priv;
  462. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  463. int vsub, hsub, ret;
  464. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  465. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  466. return ret;
  467. hsub = desc->log2_chroma_w;
  468. vsub = desc->log2_chroma_h;
  469. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  470. s->height[0] = s->height[3] = inlink->h;
  471. s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  472. s->width[0] = s->width[3] = inlink->w;
  473. s->depth = desc->comp[0].depth;
  474. s->max = (1 << s->depth) - 1;
  475. s->half = (1 << s->depth) / 2;
  476. s->offset = 16 << (s->depth - 8);
  477. return 0;
  478. }
  479. static int config_output(AVFilterLink *outlink)
  480. {
  481. AVFilterContext *ctx = outlink->src;
  482. PreMultiplyContext *s = ctx->priv;
  483. AVFilterLink *base = ctx->inputs[0];
  484. AVFilterLink *alpha;
  485. FFFrameSyncIn *in;
  486. int ret;
  487. if (!s->inplace) {
  488. alpha = ctx->inputs[1];
  489. if (base->format != alpha->format) {
  490. av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
  491. return AVERROR(EINVAL);
  492. }
  493. if (base->w != alpha->w ||
  494. base->h != alpha->h) {
  495. av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
  496. "(size %dx%d) do not match the corresponding "
  497. "second input link %s parameters (%dx%d) ",
  498. ctx->input_pads[0].name, base->w, base->h,
  499. ctx->input_pads[1].name, alpha->w, alpha->h);
  500. return AVERROR(EINVAL);
  501. }
  502. }
  503. outlink->w = base->w;
  504. outlink->h = base->h;
  505. outlink->time_base = base->time_base;
  506. outlink->sample_aspect_ratio = base->sample_aspect_ratio;
  507. outlink->frame_rate = base->frame_rate;
  508. if (s->inplace)
  509. return 0;
  510. if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
  511. return ret;
  512. in = s->fs.in;
  513. in[0].time_base = base->time_base;
  514. in[1].time_base = alpha->time_base;
  515. in[0].sync = 1;
  516. in[0].before = EXT_STOP;
  517. in[0].after = EXT_INFINITY;
  518. in[1].sync = 1;
  519. in[1].before = EXT_STOP;
  520. in[1].after = EXT_INFINITY;
  521. s->fs.opaque = s;
  522. s->fs.on_event = process_frame;
  523. return ff_framesync_configure(&s->fs);
  524. }
  525. static int activate(AVFilterContext *ctx)
  526. {
  527. PreMultiplyContext *s = ctx->priv;
  528. if (s->inplace) {
  529. AVFrame *frame = NULL;
  530. AVFrame *out = NULL;
  531. int ret, status;
  532. int64_t pts;
  533. if ((ret = ff_inlink_consume_frame(ctx->inputs[0], &frame)) > 0) {
  534. ret = filter_frame(ctx, &out, frame, frame);
  535. av_frame_free(&frame);
  536. if (ret < 0)
  537. return ret;
  538. ret = ff_filter_frame(ctx->outputs[0], out);
  539. }
  540. if (ret < 0) {
  541. return ret;
  542. } else if (ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) {
  543. ff_outlink_set_status(ctx->outputs[0], status, pts);
  544. return 0;
  545. } else {
  546. if (ff_outlink_frame_wanted(ctx->outputs[0]))
  547. ff_inlink_request_frame(ctx->inputs[0]);
  548. return 0;
  549. }
  550. } else {
  551. return ff_framesync_activate(&s->fs);
  552. }
  553. }
  554. static av_cold int init(AVFilterContext *ctx)
  555. {
  556. PreMultiplyContext *s = ctx->priv;
  557. AVFilterPad pad = { 0 };
  558. int ret;
  559. if (!strcmp(ctx->filter->name, "unpremultiply"))
  560. s->inverse = 1;
  561. pad.type = AVMEDIA_TYPE_VIDEO;
  562. pad.name = av_strdup("main");
  563. pad.config_props = config_input;
  564. if (!pad.name)
  565. return AVERROR(ENOMEM);
  566. if ((ret = ff_insert_inpad(ctx, 0, &pad)) < 0) {
  567. av_freep(&pad.name);
  568. return ret;
  569. }
  570. if (!s->inplace) {
  571. pad.type = AVMEDIA_TYPE_VIDEO;
  572. pad.name = av_strdup("alpha");
  573. pad.config_props = NULL;
  574. if (!pad.name)
  575. return AVERROR(ENOMEM);
  576. if ((ret = ff_insert_inpad(ctx, 1, &pad)) < 0) {
  577. av_freep(&pad.name);
  578. return ret;
  579. }
  580. }
  581. return 0;
  582. }
  583. static av_cold void uninit(AVFilterContext *ctx)
  584. {
  585. PreMultiplyContext *s = ctx->priv;
  586. if (!s->inplace)
  587. ff_framesync_uninit(&s->fs);
  588. }
  589. static const AVFilterPad premultiply_outputs[] = {
  590. {
  591. .name = "default",
  592. .type = AVMEDIA_TYPE_VIDEO,
  593. .config_props = config_output,
  594. },
  595. { NULL }
  596. };
  597. #if CONFIG_PREMULTIPLY_FILTER
  598. AVFilter ff_vf_premultiply = {
  599. .name = "premultiply",
  600. .description = NULL_IF_CONFIG_SMALL("PreMultiply first stream with first plane of second stream."),
  601. .priv_size = sizeof(PreMultiplyContext),
  602. .init = init,
  603. .uninit = uninit,
  604. .query_formats = query_formats,
  605. .activate = activate,
  606. .inputs = NULL,
  607. .outputs = premultiply_outputs,
  608. .priv_class = &premultiply_class,
  609. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
  610. AVFILTER_FLAG_DYNAMIC_INPUTS,
  611. };
  612. #endif /* CONFIG_PREMULTIPLY_FILTER */
  613. #if CONFIG_UNPREMULTIPLY_FILTER
  614. #define unpremultiply_options options
  615. AVFILTER_DEFINE_CLASS(unpremultiply);
  616. AVFilter ff_vf_unpremultiply = {
  617. .name = "unpremultiply",
  618. .description = NULL_IF_CONFIG_SMALL("UnPreMultiply first stream with first plane of second stream."),
  619. .priv_size = sizeof(PreMultiplyContext),
  620. .init = init,
  621. .uninit = uninit,
  622. .query_formats = query_formats,
  623. .activate = activate,
  624. .inputs = NULL,
  625. .outputs = premultiply_outputs,
  626. .priv_class = &unpremultiply_class,
  627. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
  628. AVFILTER_FLAG_DYNAMIC_INPUTS,
  629. };
  630. #endif /* CONFIG_UNPREMULTIPLY_FILTER */