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.

410 lines
13KB

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