2 * Copyright (c) 2016 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/imgutils.h"
22 #include "libavutil/pixdesc.h"
23 #include "libavutil/opt.h"
26 #include "framesync.h"
30 typedef struct PreMultiplyContext {
32 int width[4], height[4];
35 int half, depth, offset;
38 void (*premultiply[4])(const uint8_t *msrc, const uint8_t *asrc,
40 ptrdiff_t mlinesize, ptrdiff_t alinesize,
43 int half, int shift, int offset);
46 #define OFFSET(x) offsetof(PreMultiplyContext, x)
47 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
49 static const AVOption premultiply_options[] = {
53 AVFILTER_DEFINE_CLASS(premultiply);
55 static int query_formats(AVFilterContext *ctx)
57 static const enum AVPixelFormat pix_fmts[] = {
58 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
59 AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10,
60 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14,
62 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
63 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
64 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
68 return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
71 static void premultiply8(const uint8_t *msrc, const uint8_t *asrc,
73 ptrdiff_t mlinesize, ptrdiff_t alinesize,
76 int half, int shift, int offset)
80 for (y = 0; y < h; y++) {
81 for (x = 0; x < w; x++) {
82 dst[x] = ((msrc[x] * (((asrc[x] >> 1) & 1) + asrc[x])) + 128) >> 8;
91 static void premultiply8yuv(const uint8_t *msrc, const uint8_t *asrc,
93 ptrdiff_t mlinesize, ptrdiff_t alinesize,
96 int half, int shift, int offset)
100 for (y = 0; y < h; y++) {
101 for (x = 0; x < w; x++) {
102 dst[x] = ((((msrc[x] - 128) * (((asrc[x] >> 1) & 1) + asrc[x]))) >> 8) + 128;
111 static void premultiply8offset(const uint8_t *msrc, const uint8_t *asrc,
113 ptrdiff_t mlinesize, ptrdiff_t alinesize,
116 int half, int shift, int offset)
120 for (y = 0; y < h; y++) {
121 for (x = 0; x < w; x++) {
122 dst[x] = ((((msrc[x] - offset) * (((asrc[x] >> 1) & 1) + asrc[x])) + 128) >> 8) + offset;
131 static void premultiply16(const uint8_t *mmsrc, const uint8_t *aasrc,
133 ptrdiff_t mlinesize, ptrdiff_t alinesize,
136 int half, int shift, int offset)
138 const uint16_t *msrc = (const uint16_t *)mmsrc;
139 const uint16_t *asrc = (const uint16_t *)aasrc;
140 uint16_t *dst = (uint16_t *)ddst;
143 for (y = 0; y < h; y++) {
144 for (x = 0; x < w; x++) {
145 dst[x] = ((msrc[x] * (((asrc[x] >> 1) & 1) + asrc[x])) + half) >> shift;
148 dst += dlinesize / 2;
149 msrc += mlinesize / 2;
150 asrc += alinesize / 2;
154 static void premultiply16yuv(const uint8_t *mmsrc, const uint8_t *aasrc,
156 ptrdiff_t mlinesize, ptrdiff_t alinesize,
159 int half, int shift, int offset)
161 const uint16_t *msrc = (const uint16_t *)mmsrc;
162 const uint16_t *asrc = (const uint16_t *)aasrc;
163 uint16_t *dst = (uint16_t *)ddst;
166 for (y = 0; y < h; y++) {
167 for (x = 0; x < w; x++) {
168 dst[x] = ((((msrc[x] - half) * (((asrc[x] >> 1) & 1) + asrc[x]))) >> shift) + half;
171 dst += dlinesize / 2;
172 msrc += mlinesize / 2;
173 asrc += alinesize / 2;
177 static void premultiply16offset(const uint8_t *mmsrc, const uint8_t *aasrc,
179 ptrdiff_t mlinesize, ptrdiff_t alinesize,
182 int half, int shift, int offset)
184 const uint16_t *msrc = (const uint16_t *)mmsrc;
185 const uint16_t *asrc = (const uint16_t *)aasrc;
186 uint16_t *dst = (uint16_t *)ddst;
189 for (y = 0; y < h; y++) {
190 for (x = 0; x < w; x++) {
191 dst[x] = ((((msrc[x] - offset) * (((asrc[x] >> 1) & 1) + asrc[x])) + half) >> shift) + offset;
194 dst += dlinesize / 2;
195 msrc += mlinesize / 2;
196 asrc += alinesize / 2;
200 static int process_frame(FFFrameSync *fs)
202 AVFilterContext *ctx = fs->parent;
203 PreMultiplyContext *s = fs->opaque;
204 AVFilterLink *outlink = ctx->outputs[0];
205 AVFrame *out, *base, *alpha;
208 if ((ret = ff_framesync_get_frame(&s->fs, 0, &base, 0)) < 0 ||
209 (ret = ff_framesync_get_frame(&s->fs, 1, &alpha, 0)) < 0)
212 if (ctx->is_disabled) {
213 out = av_frame_clone(base);
215 return AVERROR(ENOMEM);
217 int p, full, limited;
219 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
221 return AVERROR(ENOMEM);
222 av_frame_copy_props(out, base);
224 full = base->color_range == AVCOL_RANGE_JPEG;
225 limited = base->color_range == AVCOL_RANGE_MPEG;
227 switch (outlink->format) {
228 case AV_PIX_FMT_YUV444P:
229 s->premultiply[0] = full ? premultiply8 : premultiply8offset;
230 s->premultiply[1] = premultiply8yuv;
231 s->premultiply[2] = premultiply8yuv;
233 case AV_PIX_FMT_YUVJ444P:
234 s->premultiply[0] = premultiply8;
235 s->premultiply[1] = premultiply8yuv;
236 s->premultiply[2] = premultiply8yuv;
238 case AV_PIX_FMT_GBRP:
239 s->premultiply[0] = limited ? premultiply8offset : premultiply8;
240 s->premultiply[1] = limited ? premultiply8offset : premultiply8;
241 s->premultiply[2] = limited ? premultiply8offset : premultiply8;
243 case AV_PIX_FMT_YUV444P9:
244 case AV_PIX_FMT_YUV444P10:
245 case AV_PIX_FMT_YUV444P12:
246 case AV_PIX_FMT_YUV444P14:
247 case AV_PIX_FMT_YUV444P16:
248 s->premultiply[0] = full ? premultiply16 : premultiply16offset;
249 s->premultiply[1] = premultiply16yuv;
250 s->premultiply[2] = premultiply16yuv;
252 case AV_PIX_FMT_GBRP9:
253 case AV_PIX_FMT_GBRP10:
254 case AV_PIX_FMT_GBRP12:
255 case AV_PIX_FMT_GBRP14:
256 case AV_PIX_FMT_GBRP16:
257 s->premultiply[0] = limited ? premultiply16offset : premultiply16;
258 s->premultiply[1] = limited ? premultiply16offset : premultiply16;
259 s->premultiply[2] = limited ? premultiply16offset : premultiply16;
261 case AV_PIX_FMT_GRAY8:
262 s->premultiply[0] = limited ? premultiply8offset : premultiply8;
264 case AV_PIX_FMT_GRAY10:
265 case AV_PIX_FMT_GRAY12:
266 case AV_PIX_FMT_GRAY16:
267 s->premultiply[0] = limited ? premultiply16offset : premultiply16;
271 for (p = 0; p < s->nb_planes; p++) {
272 s->premultiply[p](base->data[p], alpha->data[0],
274 base->linesize[p], alpha->linesize[0],
276 s->width[p], s->height[p],
277 s->half, s->depth, s->offset);
280 out->pts = av_rescale_q(base->pts, s->fs.time_base, outlink->time_base);
282 return ff_filter_frame(outlink, out);
285 static int config_input(AVFilterLink *inlink)
287 AVFilterContext *ctx = inlink->dst;
288 PreMultiplyContext *s = ctx->priv;
289 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
292 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
294 hsub = desc->log2_chroma_w;
295 vsub = desc->log2_chroma_h;
296 s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
297 s->height[0] = s->height[3] = inlink->h;
298 s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
299 s->width[0] = s->width[3] = inlink->w;
301 s->depth = desc->comp[0].depth;
302 s->half = (1 << s->depth) / 2;
303 s->offset = 16 << (s->depth - 8);
308 static int config_output(AVFilterLink *outlink)
310 AVFilterContext *ctx = outlink->src;
311 PreMultiplyContext *s = ctx->priv;
312 AVFilterLink *base = ctx->inputs[0];
313 AVFilterLink *alpha = ctx->inputs[1];
317 if (base->format != alpha->format) {
318 av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
319 return AVERROR(EINVAL);
321 if (base->w != alpha->w ||
322 base->h != alpha->h) {
323 av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
324 "(size %dx%d) do not match the corresponding "
325 "second input link %s parameters (%dx%d) ",
326 ctx->input_pads[0].name, base->w, base->h,
327 ctx->input_pads[1].name, alpha->w, alpha->h);
328 return AVERROR(EINVAL);
331 outlink->w = base->w;
332 outlink->h = base->h;
333 outlink->time_base = base->time_base;
334 outlink->sample_aspect_ratio = base->sample_aspect_ratio;
335 outlink->frame_rate = base->frame_rate;
337 if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
341 in[0].time_base = base->time_base;
342 in[1].time_base = alpha->time_base;
344 in[0].before = EXT_STOP;
345 in[0].after = EXT_INFINITY;
347 in[1].before = EXT_STOP;
348 in[1].after = EXT_INFINITY;
350 s->fs.on_event = process_frame;
352 return ff_framesync_configure(&s->fs);
355 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
357 PreMultiplyContext *s = inlink->dst->priv;
358 return ff_framesync_filter_frame(&s->fs, inlink, buf);
361 static int request_frame(AVFilterLink *outlink)
363 PreMultiplyContext *s = outlink->src->priv;
364 return ff_framesync_request_frame(&s->fs, outlink);
367 static av_cold void uninit(AVFilterContext *ctx)
369 PreMultiplyContext *s = ctx->priv;
371 ff_framesync_uninit(&s->fs);
374 static const AVFilterPad premultiply_inputs[] = {
377 .type = AVMEDIA_TYPE_VIDEO,
378 .filter_frame = filter_frame,
379 .config_props = config_input,
383 .type = AVMEDIA_TYPE_VIDEO,
384 .filter_frame = filter_frame,
389 static const AVFilterPad premultiply_outputs[] = {
392 .type = AVMEDIA_TYPE_VIDEO,
393 .config_props = config_output,
394 .request_frame = request_frame,
399 AVFilter ff_vf_premultiply = {
400 .name = "premultiply",
401 .description = NULL_IF_CONFIG_SMALL("PreMultiply first stream with first plane of second stream."),
402 .priv_size = sizeof(PreMultiplyContext),
404 .query_formats = query_formats,
405 .inputs = premultiply_inputs,
406 .outputs = premultiply_outputs,
407 .priv_class = &premultiply_class,
408 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,