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.

421 lines
14KB

  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 "formats.h"
  25. #include "framesync.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. typedef struct PreMultiplyContext {
  29. const AVClass *class;
  30. int width[4], height[4];
  31. int linesize[4];
  32. int nb_planes;
  33. int planes;
  34. int half, depth, offset;
  35. FFFrameSync fs;
  36. void (*premultiply[4])(const uint8_t *msrc, const uint8_t *asrc,
  37. uint8_t *dst,
  38. ptrdiff_t mlinesize, ptrdiff_t alinesize,
  39. ptrdiff_t dlinesize,
  40. int w, int h,
  41. int half, int shift, int offset);
  42. } PreMultiplyContext;
  43. #define OFFSET(x) offsetof(PreMultiplyContext, x)
  44. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  45. static const AVOption premultiply_options[] = {
  46. { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
  47. { NULL }
  48. };
  49. AVFILTER_DEFINE_CLASS(premultiply);
  50. static int query_formats(AVFilterContext *ctx)
  51. {
  52. static const enum AVPixelFormat pix_fmts[] = {
  53. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  54. AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10,
  55. AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14,
  56. AV_PIX_FMT_YUV444P16,
  57. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  58. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  59. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
  60. AV_PIX_FMT_NONE
  61. };
  62. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  63. }
  64. static void premultiply8(const uint8_t *msrc, const uint8_t *asrc,
  65. uint8_t *dst,
  66. ptrdiff_t mlinesize, ptrdiff_t alinesize,
  67. ptrdiff_t dlinesize,
  68. int w, int h,
  69. int half, int shift, int offset)
  70. {
  71. int x, y;
  72. for (y = 0; y < h; y++) {
  73. for (x = 0; x < w; x++) {
  74. dst[x] = ((msrc[x] * (((asrc[x] >> 1) & 1) + asrc[x])) + 128) >> 8;
  75. }
  76. dst += dlinesize;
  77. msrc += mlinesize;
  78. asrc += alinesize;
  79. }
  80. }
  81. static void premultiply8yuv(const uint8_t *msrc, const uint8_t *asrc,
  82. uint8_t *dst,
  83. ptrdiff_t mlinesize, ptrdiff_t alinesize,
  84. ptrdiff_t dlinesize,
  85. int w, int h,
  86. int half, int shift, int offset)
  87. {
  88. int x, y;
  89. for (y = 0; y < h; y++) {
  90. for (x = 0; x < w; x++) {
  91. dst[x] = ((((msrc[x] - 128) * (((asrc[x] >> 1) & 1) + asrc[x]))) >> 8) + 128;
  92. }
  93. dst += dlinesize;
  94. msrc += mlinesize;
  95. asrc += alinesize;
  96. }
  97. }
  98. static void premultiply8offset(const uint8_t *msrc, const uint8_t *asrc,
  99. uint8_t *dst,
  100. ptrdiff_t mlinesize, ptrdiff_t alinesize,
  101. ptrdiff_t dlinesize,
  102. int w, int h,
  103. int half, int shift, int offset)
  104. {
  105. int x, y;
  106. for (y = 0; y < h; y++) {
  107. for (x = 0; x < w; x++) {
  108. dst[x] = ((((msrc[x] - offset) * (((asrc[x] >> 1) & 1) + asrc[x])) + 128) >> 8) + offset;
  109. }
  110. dst += dlinesize;
  111. msrc += mlinesize;
  112. asrc += alinesize;
  113. }
  114. }
  115. static void premultiply16(const uint8_t *mmsrc, const uint8_t *aasrc,
  116. uint8_t *ddst,
  117. ptrdiff_t mlinesize, ptrdiff_t alinesize,
  118. ptrdiff_t dlinesize,
  119. int w, int h,
  120. int half, int shift, int offset)
  121. {
  122. const uint16_t *msrc = (const uint16_t *)mmsrc;
  123. const uint16_t *asrc = (const uint16_t *)aasrc;
  124. uint16_t *dst = (uint16_t *)ddst;
  125. int x, y;
  126. for (y = 0; y < h; y++) {
  127. for (x = 0; x < w; x++) {
  128. dst[x] = ((msrc[x] * (((asrc[x] >> 1) & 1) + asrc[x])) + half) >> shift;
  129. }
  130. dst += dlinesize / 2;
  131. msrc += mlinesize / 2;
  132. asrc += alinesize / 2;
  133. }
  134. }
  135. static void premultiply16yuv(const uint8_t *mmsrc, const uint8_t *aasrc,
  136. uint8_t *ddst,
  137. ptrdiff_t mlinesize, ptrdiff_t alinesize,
  138. ptrdiff_t dlinesize,
  139. int w, int h,
  140. int half, int shift, int offset)
  141. {
  142. const uint16_t *msrc = (const uint16_t *)mmsrc;
  143. const uint16_t *asrc = (const uint16_t *)aasrc;
  144. uint16_t *dst = (uint16_t *)ddst;
  145. int x, y;
  146. for (y = 0; y < h; y++) {
  147. for (x = 0; x < w; x++) {
  148. dst[x] = ((((msrc[x] - half) * (((asrc[x] >> 1) & 1) + asrc[x]))) >> shift) + half;
  149. }
  150. dst += dlinesize / 2;
  151. msrc += mlinesize / 2;
  152. asrc += alinesize / 2;
  153. }
  154. }
  155. static void premultiply16offset(const uint8_t *mmsrc, const uint8_t *aasrc,
  156. uint8_t *ddst,
  157. ptrdiff_t mlinesize, ptrdiff_t alinesize,
  158. ptrdiff_t dlinesize,
  159. int w, int h,
  160. int half, int shift, int offset)
  161. {
  162. const uint16_t *msrc = (const uint16_t *)mmsrc;
  163. const uint16_t *asrc = (const uint16_t *)aasrc;
  164. uint16_t *dst = (uint16_t *)ddst;
  165. int x, y;
  166. for (y = 0; y < h; y++) {
  167. for (x = 0; x < w; x++) {
  168. dst[x] = ((((msrc[x] - offset) * (((asrc[x] >> 1) & 1) + asrc[x])) + half) >> shift) + offset;
  169. }
  170. dst += dlinesize / 2;
  171. msrc += mlinesize / 2;
  172. asrc += alinesize / 2;
  173. }
  174. }
  175. static int process_frame(FFFrameSync *fs)
  176. {
  177. AVFilterContext *ctx = fs->parent;
  178. PreMultiplyContext *s = fs->opaque;
  179. AVFilterLink *outlink = ctx->outputs[0];
  180. AVFrame *out, *base, *alpha;
  181. int ret;
  182. if ((ret = ff_framesync_get_frame(&s->fs, 0, &base, 0)) < 0 ||
  183. (ret = ff_framesync_get_frame(&s->fs, 1, &alpha, 0)) < 0)
  184. return ret;
  185. if (ctx->is_disabled) {
  186. out = av_frame_clone(base);
  187. if (!out)
  188. return AVERROR(ENOMEM);
  189. } else {
  190. int p, full, limited;
  191. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  192. if (!out)
  193. return AVERROR(ENOMEM);
  194. av_frame_copy_props(out, base);
  195. full = base->color_range == AVCOL_RANGE_JPEG;
  196. limited = base->color_range == AVCOL_RANGE_MPEG;
  197. switch (outlink->format) {
  198. case AV_PIX_FMT_YUV444P:
  199. s->premultiply[0] = full ? premultiply8 : premultiply8offset;
  200. s->premultiply[1] = premultiply8yuv;
  201. s->premultiply[2] = premultiply8yuv;
  202. break;
  203. case AV_PIX_FMT_YUVJ444P:
  204. s->premultiply[0] = premultiply8;
  205. s->premultiply[1] = premultiply8yuv;
  206. s->premultiply[2] = premultiply8yuv;
  207. break;
  208. case AV_PIX_FMT_GBRP:
  209. s->premultiply[0] = limited ? premultiply8offset : premultiply8;
  210. s->premultiply[1] = limited ? premultiply8offset : premultiply8;
  211. s->premultiply[2] = limited ? premultiply8offset : premultiply8;
  212. break;
  213. case AV_PIX_FMT_YUV444P9:
  214. case AV_PIX_FMT_YUV444P10:
  215. case AV_PIX_FMT_YUV444P12:
  216. case AV_PIX_FMT_YUV444P14:
  217. case AV_PIX_FMT_YUV444P16:
  218. s->premultiply[0] = full ? premultiply16 : premultiply16offset;
  219. s->premultiply[1] = premultiply16yuv;
  220. s->premultiply[2] = premultiply16yuv;
  221. break;
  222. case AV_PIX_FMT_GBRP9:
  223. case AV_PIX_FMT_GBRP10:
  224. case AV_PIX_FMT_GBRP12:
  225. case AV_PIX_FMT_GBRP14:
  226. case AV_PIX_FMT_GBRP16:
  227. s->premultiply[0] = limited ? premultiply16offset : premultiply16;
  228. s->premultiply[1] = limited ? premultiply16offset : premultiply16;
  229. s->premultiply[2] = limited ? premultiply16offset : premultiply16;
  230. break;
  231. case AV_PIX_FMT_GRAY8:
  232. s->premultiply[0] = limited ? premultiply8offset : premultiply8;
  233. break;
  234. case AV_PIX_FMT_GRAY10:
  235. case AV_PIX_FMT_GRAY12:
  236. case AV_PIX_FMT_GRAY16:
  237. s->premultiply[0] = limited ? premultiply16offset : premultiply16;
  238. break;
  239. }
  240. for (p = 0; p < s->nb_planes; p++) {
  241. if (!((1 << p) & s->planes)) {
  242. av_image_copy_plane(out->data[p], out->linesize[p], base->data[p], base->linesize[p],
  243. s->linesize[p], s->height[p]);
  244. continue;
  245. }
  246. s->premultiply[p](base->data[p], alpha->data[0],
  247. out->data[p],
  248. base->linesize[p], alpha->linesize[0],
  249. out->linesize[p],
  250. s->width[p], s->height[p],
  251. s->half, s->depth, s->offset);
  252. }
  253. }
  254. out->pts = av_rescale_q(base->pts, s->fs.time_base, outlink->time_base);
  255. return ff_filter_frame(outlink, out);
  256. }
  257. static int config_input(AVFilterLink *inlink)
  258. {
  259. AVFilterContext *ctx = inlink->dst;
  260. PreMultiplyContext *s = ctx->priv;
  261. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  262. int vsub, hsub, ret;
  263. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  264. if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
  265. return ret;
  266. hsub = desc->log2_chroma_w;
  267. vsub = desc->log2_chroma_h;
  268. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  269. s->height[0] = s->height[3] = inlink->h;
  270. s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  271. s->width[0] = s->width[3] = inlink->w;
  272. s->depth = desc->comp[0].depth;
  273. s->half = (1 << s->depth) / 2;
  274. s->offset = 16 << (s->depth - 8);
  275. return 0;
  276. }
  277. static int config_output(AVFilterLink *outlink)
  278. {
  279. AVFilterContext *ctx = outlink->src;
  280. PreMultiplyContext *s = ctx->priv;
  281. AVFilterLink *base = ctx->inputs[0];
  282. AVFilterLink *alpha = ctx->inputs[1];
  283. FFFrameSyncIn *in;
  284. int ret;
  285. if (base->format != alpha->format) {
  286. av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
  287. return AVERROR(EINVAL);
  288. }
  289. if (base->w != alpha->w ||
  290. base->h != alpha->h) {
  291. av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
  292. "(size %dx%d) do not match the corresponding "
  293. "second input link %s parameters (%dx%d) ",
  294. ctx->input_pads[0].name, base->w, base->h,
  295. ctx->input_pads[1].name, alpha->w, alpha->h);
  296. return AVERROR(EINVAL);
  297. }
  298. outlink->w = base->w;
  299. outlink->h = base->h;
  300. outlink->time_base = base->time_base;
  301. outlink->sample_aspect_ratio = base->sample_aspect_ratio;
  302. outlink->frame_rate = base->frame_rate;
  303. if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
  304. return ret;
  305. in = s->fs.in;
  306. in[0].time_base = base->time_base;
  307. in[1].time_base = alpha->time_base;
  308. in[0].sync = 1;
  309. in[0].before = EXT_STOP;
  310. in[0].after = EXT_INFINITY;
  311. in[1].sync = 1;
  312. in[1].before = EXT_STOP;
  313. in[1].after = EXT_INFINITY;
  314. s->fs.opaque = s;
  315. s->fs.on_event = process_frame;
  316. return ff_framesync_configure(&s->fs);
  317. }
  318. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  319. {
  320. PreMultiplyContext *s = inlink->dst->priv;
  321. return ff_framesync_filter_frame(&s->fs, inlink, buf);
  322. }
  323. static int request_frame(AVFilterLink *outlink)
  324. {
  325. PreMultiplyContext *s = outlink->src->priv;
  326. return ff_framesync_request_frame(&s->fs, outlink);
  327. }
  328. static av_cold void uninit(AVFilterContext *ctx)
  329. {
  330. PreMultiplyContext *s = ctx->priv;
  331. ff_framesync_uninit(&s->fs);
  332. }
  333. static const AVFilterPad premultiply_inputs[] = {
  334. {
  335. .name = "main",
  336. .type = AVMEDIA_TYPE_VIDEO,
  337. .filter_frame = filter_frame,
  338. .config_props = config_input,
  339. },
  340. {
  341. .name = "alpha",
  342. .type = AVMEDIA_TYPE_VIDEO,
  343. .filter_frame = filter_frame,
  344. },
  345. { NULL }
  346. };
  347. static const AVFilterPad premultiply_outputs[] = {
  348. {
  349. .name = "default",
  350. .type = AVMEDIA_TYPE_VIDEO,
  351. .config_props = config_output,
  352. .request_frame = request_frame,
  353. },
  354. { NULL }
  355. };
  356. AVFilter ff_vf_premultiply = {
  357. .name = "premultiply",
  358. .description = NULL_IF_CONFIG_SMALL("PreMultiply first stream with first plane of second stream."),
  359. .priv_size = sizeof(PreMultiplyContext),
  360. .uninit = uninit,
  361. .query_formats = query_formats,
  362. .inputs = premultiply_inputs,
  363. .outputs = premultiply_outputs,
  364. .priv_class = &premultiply_class,
  365. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  366. };