Sequencer: Add option to use absolute mask animation time
[blender.git] / source / blender / blenkernel / intern / seqmodifier.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2012 Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation,
22  *                 Sergey Sharybin
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/blenkernel/intern/seqmodifier.c
28  *  \ingroup bke
29  */
30
31 #include <stddef.h>
32 #include <string.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "BLI_listbase.h"
37 #include "BLI_path_util.h"
38 #include "BLI_string.h"
39 #include "BLI_utildefines.h"
40 #include "BLI_math.h"
41
42 #include "BLT_translation.h"
43
44 #include "DNA_sequence_types.h"
45 #include "DNA_scene_types.h"
46
47 #include "BKE_colortools.h"
48 #include "BKE_sequencer.h"
49
50 #include "IMB_imbuf.h"
51 #include "IMB_imbuf_types.h"
52 #include "IMB_colormanagement.h"
53
54 static SequenceModifierTypeInfo *modifiersTypes[NUM_SEQUENCE_MODIFIER_TYPES];
55 static bool modifierTypesInit = false;
56
57 /*********************** Modifiers *************************/
58
59 typedef void (*modifier_apply_threaded_cb) (int width, int height, unsigned char *rect, float *rect_float,
60                                             unsigned char *mask_rect, float *mask_rect_float, void *data_v);
61
62 typedef struct ModifierInitData {
63         ImBuf *ibuf;
64         ImBuf *mask;
65         void *user_data;
66
67         modifier_apply_threaded_cb apply_callback;
68 } ModifierInitData;
69
70 typedef struct ModifierThread {
71         int width, height;
72
73         unsigned char *rect, *mask_rect;
74         float *rect_float, *mask_rect_float;
75
76         void *user_data;
77
78         modifier_apply_threaded_cb apply_callback;
79 } ModifierThread;
80
81
82 static ImBuf *modifier_mask_get(SequenceModifierData *smd, const SeqRenderData *context, int cfra, int fra_offset, bool make_float)
83 {
84         return BKE_sequencer_render_mask_input(context, smd->mask_input_type, smd->mask_sequence, smd->mask_id, cfra, fra_offset, make_float);
85 }
86
87 static void modifier_init_handle(void *handle_v, int start_line, int tot_line, void *init_data_v)
88 {
89         ModifierThread *handle = (ModifierThread *) handle_v;
90         ModifierInitData *init_data = (ModifierInitData *) init_data_v;
91         ImBuf *ibuf = init_data->ibuf;
92         ImBuf *mask = init_data->mask;
93
94         int offset = 4 * start_line * ibuf->x;
95
96         memset(handle, 0, sizeof(ModifierThread));
97
98         handle->width = ibuf->x;
99         handle->height = tot_line;
100         handle->apply_callback = init_data->apply_callback;
101         handle->user_data = init_data->user_data;
102
103         if (ibuf->rect)
104                 handle->rect = (unsigned char *) ibuf->rect + offset;
105
106         if (ibuf->rect_float)
107                 handle->rect_float = ibuf->rect_float + offset;
108
109         if (mask) {
110                 if (mask->rect)
111                         handle->mask_rect = (unsigned char *) mask->rect + offset;
112
113                 if (mask->rect_float)
114                         handle->mask_rect_float = mask->rect_float + offset;
115         }
116         else {
117                 handle->mask_rect = NULL;
118                 handle->mask_rect_float = NULL;
119         }
120 }
121
122 static void *modifier_do_thread(void *thread_data_v)
123 {
124         ModifierThread *td = (ModifierThread *) thread_data_v;
125
126         td->apply_callback(td->width, td->height, td->rect, td->rect_float, td->mask_rect, td->mask_rect_float, td->user_data);
127
128         return NULL;
129 }
130
131 static void modifier_apply_threaded(ImBuf *ibuf, ImBuf *mask, modifier_apply_threaded_cb apply_callback, void *user_data)
132 {
133         ModifierInitData init_data;
134
135         init_data.ibuf = ibuf;
136         init_data.mask = mask;
137         init_data.user_data = user_data;
138
139         init_data.apply_callback = apply_callback;
140
141         IMB_processor_apply_threaded(ibuf->y, sizeof(ModifierThread), &init_data,
142                                      modifier_init_handle, modifier_do_thread);
143 }
144
145 /* **** Color Balance Modifier **** */
146
147 static void colorBalance_init_data(SequenceModifierData *smd)
148 {
149         ColorBalanceModifierData *cbmd = (ColorBalanceModifierData *) smd;
150         int c;
151
152         cbmd->color_multiply = 1.0f;
153
154         for (c = 0; c < 3; c++) {
155                 cbmd->color_balance.lift[c] = 1.0f;
156                 cbmd->color_balance.gamma[c] = 1.0f;
157                 cbmd->color_balance.gain[c] = 1.0f;
158         }
159 }
160
161 static void colorBalance_apply(SequenceModifierData *smd, ImBuf *ibuf, ImBuf *mask)
162 {
163         ColorBalanceModifierData *cbmd = (ColorBalanceModifierData *) smd;
164
165         BKE_sequencer_color_balance_apply(&cbmd->color_balance, ibuf, cbmd->color_multiply, false, mask);
166 }
167
168 static SequenceModifierTypeInfo seqModifier_ColorBalance = {
169         CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Color Balance"),  /* name */
170         "ColorBalanceModifierData",                            /* struct_name */
171         sizeof(ColorBalanceModifierData),                      /* struct_size */
172         colorBalance_init_data,                                /* init_data */
173         NULL,                                                  /* free_data */
174         NULL,                                                  /* copy_data */
175         colorBalance_apply                                     /* apply */
176 };
177
178 /* **** White Balance Modifier **** */
179
180 static void whiteBalance_init_data(SequenceModifierData *smd)
181 {
182         WhiteBalanceModifierData *cbmd = (WhiteBalanceModifierData *) smd;
183         copy_v3_fl(cbmd->white_value, 1.0f);
184 }
185
186 typedef struct WhiteBalanceThreadData {
187         struct ColorSpace *colorspace;
188         float white[3];
189 } WhiteBalanceThreadData;
190
191 static void whiteBalance_apply_threaded(int width, int height, unsigned char *rect, float *rect_float,
192                                         unsigned char *mask_rect, float *mask_rect_float, void *data_v)
193 {
194         int x, y;
195         float multiplier[3];
196
197         WhiteBalanceThreadData *data = (WhiteBalanceThreadData *) data_v;
198
199         multiplier[0] = 1.0f / data->white[0];
200         multiplier[1] = 1.0f / data->white[1];
201         multiplier[2] = 1.0f / data->white[2];
202
203         for (y = 0; y < height; y++) {
204                 for (x = 0; x < width; x++) {
205                         int pixel_index = (y * width + x) * 4;
206                         float result[4], mask[3] = {1.0f, 1.0f, 1.0f};
207
208                         if (rect_float) {
209                                 copy_v3_v3(result, rect_float + pixel_index);
210                         }
211                         else {
212                                 straight_uchar_to_premul_float(result, rect + pixel_index);
213                                 IMB_colormanagement_colorspace_to_scene_linear_v3(result, data->colorspace);
214                         }
215
216                         mul_v3_v3(result, multiplier);
217
218                         if (mask_rect_float) {
219                                 copy_v3_v3(mask, mask_rect_float + pixel_index);
220                         }
221                         else if (mask_rect) {
222                                 rgb_uchar_to_float(mask, mask_rect + pixel_index);
223                         }
224
225                         result[0] = result[0] * (1.0f - mask[0]) + result[0] * mask[0];
226                         result[1] = result[1] * (1.0f - mask[1]) + result[1] * mask[1];
227                         result[2] = result[2] * (1.0f - mask[2]) + result[2] * mask[2];
228
229                         if (rect_float) {
230                                 copy_v3_v3(rect_float + pixel_index, result);
231                         }
232                         else {
233                                 IMB_colormanagement_scene_linear_to_colorspace_v3(result, data->colorspace);
234                                 premul_float_to_straight_uchar(rect + pixel_index, result);
235                         }
236                 }
237         }
238 }
239
240 static void whiteBalance_apply(SequenceModifierData *smd, ImBuf *ibuf, ImBuf *mask)
241 {
242         WhiteBalanceThreadData data;
243         WhiteBalanceModifierData *wbmd = (WhiteBalanceModifierData *) smd;
244
245         copy_v3_v3(data.white, wbmd->white_value);
246         IMB_colormanagement_display_to_scene_linear_v3(data.white,
247                                                        IMB_colormanagement_display_get_named(wbmd->modifier.scene->display_settings.display_device));
248         data.colorspace = ibuf->rect_colorspace;
249
250         modifier_apply_threaded(ibuf, mask, whiteBalance_apply_threaded, &data);
251 }
252
253 static SequenceModifierTypeInfo seqModifier_WhiteBalance = {
254         CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "White Balance"),  /* name */
255         "WhiteBalanceModifierData",                            /* struct_name */
256         sizeof(WhiteBalanceModifierData),                      /* struct_size */
257         whiteBalance_init_data,                                /* init_data */
258         NULL,                                                  /* free_data */
259         NULL,                                                  /* copy_data */
260         whiteBalance_apply                                     /* apply */
261 };
262
263 /* **** Curves Modifier **** */
264
265 static void curves_init_data(SequenceModifierData *smd)
266 {
267         CurvesModifierData *cmd = (CurvesModifierData *) smd;
268
269         curvemapping_set_defaults(&cmd->curve_mapping, 4, 0.0f, 0.0f, 1.0f, 1.0f);
270 }
271
272 static void curves_free_data(SequenceModifierData *smd)
273 {
274         CurvesModifierData *cmd = (CurvesModifierData *) smd;
275
276         curvemapping_free_data(&cmd->curve_mapping);
277 }
278
279 static void curves_copy_data(SequenceModifierData *target, SequenceModifierData *smd)
280 {
281         CurvesModifierData *cmd = (CurvesModifierData *) smd;
282         CurvesModifierData *cmd_target = (CurvesModifierData *) target;
283
284         curvemapping_copy_data(&cmd_target->curve_mapping, &cmd->curve_mapping);
285 }
286
287 static void curves_apply_threaded(int width, int height, unsigned char *rect, float *rect_float,
288                                   unsigned char *mask_rect, float *mask_rect_float, void *data_v)
289 {
290         CurveMapping *curve_mapping = (CurveMapping *) data_v;
291         int x, y;
292
293         for (y = 0; y < height; y++) {
294                 for (x = 0; x < width; x++) {
295                         int pixel_index = (y * width + x) * 4;
296
297                         if (rect_float) {
298                                 float *pixel = rect_float + pixel_index;
299                                 float result[3];
300
301                                 curvemapping_evaluate_premulRGBF(curve_mapping, result, pixel);
302
303                                 if (mask_rect_float) {
304                                         const float *m = mask_rect_float + pixel_index;
305
306                                         pixel[0] = pixel[0] * (1.0f - m[0]) + result[0] * m[0];
307                                         pixel[1] = pixel[1] * (1.0f - m[1]) + result[1] * m[1];
308                                         pixel[2] = pixel[2] * (1.0f - m[2]) + result[2] * m[2];
309                                 }
310                                 else {
311                                         pixel[0] = result[0];
312                                         pixel[1] = result[1];
313                                         pixel[2] = result[2];
314                                 }
315                         }
316                         if (rect) {
317                                 unsigned char *pixel = rect + pixel_index;
318                                 float result[3], tempc[4];
319
320                                 straight_uchar_to_premul_float(tempc, pixel);
321
322                                 curvemapping_evaluate_premulRGBF(curve_mapping, result, tempc);
323
324                                 if (mask_rect) {
325                                         float t[3];
326
327                                         rgb_uchar_to_float(t, mask_rect + pixel_index);
328
329                                         tempc[0] = tempc[0] * (1.0f - t[0]) + result[0] * t[0];
330                                         tempc[1] = tempc[1] * (1.0f - t[1]) + result[1] * t[1];
331                                         tempc[2] = tempc[2] * (1.0f - t[2]) + result[2] * t[2];
332                                 }
333                                 else {
334                                         tempc[0] = result[0];
335                                         tempc[1] = result[1];
336                                         tempc[2] = result[2];
337                                 }
338
339                                 premul_float_to_straight_uchar(pixel, tempc);
340                         }
341                 }
342         }
343 }
344
345 static void curves_apply(struct SequenceModifierData *smd, ImBuf *ibuf, ImBuf *mask)
346 {
347         CurvesModifierData *cmd = (CurvesModifierData *) smd;
348
349         float black[3] = {0.0f, 0.0f, 0.0f};
350         float white[3] = {1.0f, 1.0f, 1.0f};
351
352         curvemapping_initialize(&cmd->curve_mapping);
353
354         curvemapping_premultiply(&cmd->curve_mapping, 0);
355         curvemapping_set_black_white(&cmd->curve_mapping, black, white);
356
357         modifier_apply_threaded(ibuf, mask, curves_apply_threaded, &cmd->curve_mapping);
358
359         curvemapping_premultiply(&cmd->curve_mapping, 1);
360 }
361
362 static SequenceModifierTypeInfo seqModifier_Curves = {
363         CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Curves"),   /* name */
364         "CurvesModifierData",                            /* struct_name */
365         sizeof(CurvesModifierData),                      /* struct_size */
366         curves_init_data,                                /* init_data */
367         curves_free_data,                                /* free_data */
368         curves_copy_data,                                /* copy_data */
369         curves_apply                                     /* apply */
370 };
371
372 /* **** Hue Correct Modifier **** */
373
374 static void hue_correct_init_data(SequenceModifierData *smd)
375 {
376         HueCorrectModifierData *hcmd = (HueCorrectModifierData *) smd;
377         int c;
378
379         curvemapping_set_defaults(&hcmd->curve_mapping, 1, 0.0f, 0.0f, 1.0f, 1.0f);
380         hcmd->curve_mapping.preset = CURVE_PRESET_MID9;
381
382         for (c = 0; c < 3; c++) {
383                 CurveMap *cuma = &hcmd->curve_mapping.cm[c];
384
385                 curvemap_reset(cuma, &hcmd->curve_mapping.clipr, hcmd->curve_mapping.preset, CURVEMAP_SLOPE_POSITIVE);
386         }
387
388         /* default to showing Saturation */
389         hcmd->curve_mapping.cur = 1;
390 }
391
392 static void hue_correct_free_data(SequenceModifierData *smd)
393 {
394         HueCorrectModifierData *hcmd = (HueCorrectModifierData *) smd;
395
396         curvemapping_free_data(&hcmd->curve_mapping);
397 }
398
399 static void hue_correct_copy_data(SequenceModifierData *target, SequenceModifierData *smd)
400 {
401         HueCorrectModifierData *hcmd = (HueCorrectModifierData *) smd;
402         HueCorrectModifierData *hcmd_target = (HueCorrectModifierData *) target;
403
404         curvemapping_copy_data(&hcmd_target->curve_mapping, &hcmd->curve_mapping);
405 }
406
407 static void hue_correct_apply_threaded(int width, int height, unsigned char *rect, float *rect_float,
408                                 unsigned char *mask_rect, float *mask_rect_float, void *data_v)
409 {
410         CurveMapping *curve_mapping = (CurveMapping *) data_v;
411         int x, y;
412
413         for (y = 0; y < height; y++) {
414                 for (x = 0; x < width; x++) {
415                         int pixel_index = (y * width + x) * 4;
416                         float pixel[3], result[3], mask[3] = {1.0f, 1.0f, 1.0f};
417                         float hsv[3], f;
418
419                         if (rect_float)
420                                 copy_v3_v3(pixel, rect_float + pixel_index);
421                         else
422                                 rgb_uchar_to_float(pixel, rect + pixel_index);
423
424                         rgb_to_hsv(pixel[0], pixel[1], pixel[2], hsv, hsv + 1, hsv + 2);
425
426                         /* adjust hue, scaling returned default 0.5 up to 1 */
427                         f = curvemapping_evaluateF(curve_mapping, 0, hsv[0]);
428                         hsv[0] += f - 0.5f;
429
430                         /* adjust saturation, scaling returned default 0.5 up to 1 */
431                         f = curvemapping_evaluateF(curve_mapping, 1, hsv[0]);
432                         hsv[1] *= (f * 2.0f);
433
434                         /* adjust value, scaling returned default 0.5 up to 1 */
435                         f = curvemapping_evaluateF(curve_mapping, 2, hsv[0]);
436                         hsv[2] *= (f * 2.f);
437
438                         hsv[0] = hsv[0] - floorf(hsv[0]); /* mod 1.0 */
439                         CLAMP(hsv[1], 0.0f, 1.0f);
440
441                         /* convert back to rgb */
442                         hsv_to_rgb(hsv[0], hsv[1], hsv[2], result, result + 1, result + 2);
443
444                         if (mask_rect_float)
445                                 copy_v3_v3(mask, mask_rect_float + pixel_index);
446                         else if (mask_rect)
447                                 rgb_uchar_to_float(mask, mask_rect + pixel_index);
448
449                         result[0] = pixel[0] * (1.0f - mask[0]) + result[0] * mask[0];
450                         result[1] = pixel[1] * (1.0f - mask[1]) + result[1] * mask[1];
451                         result[2] = pixel[2] * (1.0f - mask[2]) + result[2] * mask[2];
452
453                         if (rect_float)
454                                 copy_v3_v3(rect_float + pixel_index, result);
455                         else
456                                 rgb_float_to_uchar(rect + pixel_index, result);
457                 }
458         }
459 }
460
461 static void hue_correct_apply(struct SequenceModifierData *smd, ImBuf *ibuf, ImBuf *mask)
462 {
463         HueCorrectModifierData *hcmd = (HueCorrectModifierData *) smd;
464
465         curvemapping_initialize(&hcmd->curve_mapping);
466
467         modifier_apply_threaded(ibuf, mask, hue_correct_apply_threaded, &hcmd->curve_mapping);
468 }
469
470 static SequenceModifierTypeInfo seqModifier_HueCorrect = {
471         CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Hue Correct"),    /* name */
472         "HueCorrectModifierData",                              /* struct_name */
473         sizeof(HueCorrectModifierData),                        /* struct_size */
474         hue_correct_init_data,                                 /* init_data */
475         hue_correct_free_data,                                 /* free_data */
476         hue_correct_copy_data,                                 /* copy_data */
477         hue_correct_apply                                      /* apply */
478 };
479
480 /* **** Bright/Contrast Modifier **** */
481
482 typedef struct BrightContrastThreadData {
483         float bright;
484         float contrast;
485 } BrightContrastThreadData;
486
487 static void brightcontrast_apply_threaded(int width, int height, unsigned char *rect, float *rect_float,
488                                           unsigned char *mask_rect, float *mask_rect_float, void *data_v)
489 {
490         BrightContrastThreadData *data = (BrightContrastThreadData *) data_v;
491         int x, y;
492
493         float i;
494         int c;
495         float a, b, v;
496         float brightness = data->bright / 100.0f;
497         float contrast = data->contrast;
498         float delta = contrast / 200.0f;
499
500         a = 1.0f - delta * 2.0f;
501         /*
502          * The algorithm is by Werner D. Streidt
503          * (http://visca.com/ffactory/archives/5-99/msg00021.html)
504          * Extracted of OpenCV demhist.c
505          */
506         if (contrast > 0) {
507                 a = 1.0f / a;
508                 b = a * (brightness - delta);
509         }
510         else {
511                 delta *= -1;
512                 b = a * (brightness + delta);
513         }
514
515         for (y = 0; y < height; y++) {
516                 for (x = 0; x < width; x++) {
517                         int pixel_index = (y * width + x) * 4;
518
519                         if (rect) {
520                                 unsigned char *pixel = rect + pixel_index;
521
522                                 for (c = 0; c < 3; c++) {
523                                         i = (float) pixel[c] / 255.0f;
524                                         v = a * i + b;
525
526                                         if (mask_rect) {
527                                                 unsigned char *m = mask_rect + pixel_index;
528                                                 float t = (float) m[c] / 255.0f;
529
530                                                 v = (float) pixel[c] / 255.0f * (1.0f - t) + v * t;
531                                         }
532
533                                         pixel[c] = FTOCHAR(v);
534                                 }
535                         }
536                         else if (rect_float) {
537                                 float *pixel = rect_float + pixel_index;
538
539                                 for (c = 0; c < 3; c++) {
540                                         i = pixel[c];
541                                         v = a * i + b;
542
543                                         if (mask_rect_float) {
544                                                 const float *m = mask_rect_float + pixel_index;
545
546                                                 pixel[c] = pixel[c] * (1.0f - m[c]) + v * m[c];
547                                         }
548                                         else
549                                                 pixel[c] = v;
550                                 }
551                         }
552                 }
553         }
554 }
555
556 static void brightcontrast_apply(struct SequenceModifierData *smd, ImBuf *ibuf, ImBuf *mask)
557 {
558         BrightContrastModifierData *bcmd = (BrightContrastModifierData *) smd;
559         BrightContrastThreadData data;
560
561         data.bright = bcmd->bright;
562         data.contrast = bcmd->contrast;
563
564         modifier_apply_threaded(ibuf, mask, brightcontrast_apply_threaded, &data);
565 }
566
567 static SequenceModifierTypeInfo seqModifier_BrightContrast = {
568         CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Bright/Contrast"),   /* name */
569         "BrightContrastModifierData",                             /* struct_name */
570         sizeof(BrightContrastModifierData),                       /* struct_size */
571         NULL,                                                     /* init_data */
572         NULL,                                                     /* free_data */
573         NULL,                                                     /* copy_data */
574         brightcontrast_apply                                      /* apply */
575 };
576
577 /* **** Mask Modifier **** */
578
579 static void maskmodifier_apply_threaded(int width, int height, unsigned char *rect, float *rect_float,
580                                         unsigned char *mask_rect, float *mask_rect_float, void *UNUSED(data_v))
581 {
582         int x, y;
583
584         if (rect && !mask_rect)
585                 return;
586
587         if (rect_float && !mask_rect_float)
588                 return;
589
590         for (y = 0; y < height; y++) {
591                 for (x = 0; x < width; x++) {
592                         int pixel_index = (y * width + x) * 4;
593
594                         if (rect) {
595                                 unsigned char *pixel = rect + pixel_index;
596                                 unsigned char *mask_pixel = mask_rect + pixel_index;
597                                 unsigned char mask = min_iii(mask_pixel[0], mask_pixel[1], mask_pixel[2]);
598
599                                 /* byte buffer is straight, so only affect on alpha itself,
600                                  * this is the only way to alpha-over byte strip after
601                                  * applying mask modifier.
602                                  */
603                                 pixel[3] = (float)(pixel[3] * mask) / 255.0f;
604                         }
605                         else if (rect_float) {
606                                 int c;
607                                 float *pixel = rect_float + pixel_index;
608                                 const float *mask_pixel = mask_rect_float + pixel_index;
609                                 float mask = min_fff(mask_pixel[0], mask_pixel[1], mask_pixel[2]);
610
611                                 /* float buffers are premultiplied, so need to premul color
612                                  * as well to make it easy to alpha-over masted strip.
613                                  */
614                                 for (c = 0; c < 4; c++)
615                                         pixel[c] = pixel[c] * mask;
616                         }
617                 }
618         }
619 }
620
621 static void maskmodifier_apply(struct SequenceModifierData *UNUSED(smd), ImBuf *ibuf, ImBuf *mask)
622 {
623         // SequencerMaskModifierData *bcmd = (SequencerMaskModifierData *)smd;
624
625         modifier_apply_threaded(ibuf, mask, maskmodifier_apply_threaded, NULL);
626 }
627
628 static SequenceModifierTypeInfo seqModifier_Mask = {
629         CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Mask"), /* name */
630         "SequencerMaskModifierData",                 /* struct_name */
631         sizeof(SequencerMaskModifierData),           /* struct_size */
632         NULL,                                        /* init_data */
633         NULL,                                        /* free_data */
634         NULL,                                        /* copy_data */
635         maskmodifier_apply                           /* apply */
636 };
637
638 /* **** Tonemap Modifier **** */
639
640 typedef struct AvgLogLum {
641         SequencerTonemapModifierData *tmmd;
642         struct ColorSpace *colorspace;
643         float al;
644         float auto_key;
645         float lav;
646         float cav[4];
647         float igm;
648 } AvgLogLum;
649
650 static void tonemapmodifier_init_data(SequenceModifierData *smd)
651 {
652         SequencerTonemapModifierData *tmmd = (SequencerTonemapModifierData *) smd;
653         /* Same as tonemap compositor node. */
654         tmmd->type = SEQ_TONEMAP_RD_PHOTORECEPTOR;
655         tmmd->key = 0.18f;
656         tmmd->offset = 1.0f;
657         tmmd->gamma = 1.0f;
658         tmmd->intensity = 0.0f;
659         tmmd->contrast = 0.0f;
660         tmmd->adaptation = 1.0f;
661         tmmd->correction = 0.0f;
662 }
663
664 static void tonemapmodifier_apply_threaded_simple(int width,
665                                                   int height,
666                                                   unsigned char *rect,
667                                                   float *rect_float,
668                                                   unsigned char *mask_rect,
669                                                   float *mask_rect_float,
670                                                   void *data_v)
671 {
672         AvgLogLum *avg = (AvgLogLum *)data_v;
673         for (int y = 0; y < height; y++) {
674                 for (int x = 0; x < width; x++) {
675                         int pixel_index = (y * width + x) * 4;
676                         float input[4], output[4], mask[3] = {1.0f, 1.0f, 1.0f};
677                         /* Get input value. */
678                         if (rect_float) {
679                                 copy_v4_v4(input, &rect_float[pixel_index]);
680                         }
681                         else {
682                                 straight_uchar_to_premul_float(input, &rect[pixel_index]);
683                         }
684                         IMB_colormanagement_colorspace_to_scene_linear_v3(input, avg->colorspace);
685                         copy_v4_v4(output, input);
686                         /* Get mask value. */
687                         if (mask_rect_float) {
688                                 copy_v3_v3(mask, mask_rect_float + pixel_index);
689                         }
690                         else if (mask_rect) {
691                                 rgb_uchar_to_float(mask, mask_rect + pixel_index);
692                         }
693                         /* Apply correction. */
694                         mul_v3_fl(output, avg->al);
695                         float dr = output[0] + avg->tmmd->offset;
696                         float dg = output[1] + avg->tmmd->offset;
697                         float db = output[2] + avg->tmmd->offset;
698                         output[0] /= ((dr == 0.0f) ? 1.0f : dr);
699                         output[1] /= ((dg == 0.0f) ? 1.0f : dg);
700                         output[2] /= ((db == 0.0f) ? 1.0f : db);
701                         const float igm = avg->igm;
702                         if (igm != 0.0f) {
703                                 output[0] = powf(max_ff(output[0], 0.0f), igm);
704                                 output[1] = powf(max_ff(output[1], 0.0f), igm);
705                                 output[2] = powf(max_ff(output[2], 0.0f), igm);
706                         }
707                         /* Apply mask. */
708                         output[0] = input[0] * (1.0f - mask[0]) + output[0] * mask[0];
709                         output[1] = input[1] * (1.0f - mask[1]) + output[1] * mask[1];
710                         output[2] = input[2] * (1.0f - mask[2]) + output[2] * mask[2];
711                         /* Copy result back. */
712                         IMB_colormanagement_scene_linear_to_colorspace_v3(output, avg->colorspace);
713                         if (rect_float) {
714                                 copy_v4_v4(&rect_float[pixel_index], output);
715                         }
716                         else {
717                                 premul_float_to_straight_uchar(&rect[pixel_index], output);
718                         }
719                 }
720         }
721 }
722
723 static void tonemapmodifier_apply_threaded_photoreceptor(int width,
724                                                          int height,
725                                                          unsigned char *rect,
726                                                          float *rect_float,
727                                                          unsigned char *mask_rect,
728                                                          float *mask_rect_float,
729                                                          void *data_v)
730 {
731         AvgLogLum *avg = (AvgLogLum *)data_v;
732         const float f = expf(-avg->tmmd->intensity);
733         const float m = (avg->tmmd->contrast > 0.0f) ? avg->tmmd->contrast : (0.3f + 0.7f * powf(avg->auto_key, 1.4f));
734         const float ic = 1.0f - avg->tmmd->correction, ia = 1.0f - avg->tmmd->adaptation;
735         for (int y = 0; y < height; y++) {
736                 for (int x = 0; x < width; x++) {
737                         int pixel_index = (y * width + x) * 4;
738                         float input[4], output[4], mask[3] = {1.0f, 1.0f, 1.0f};
739                         /* Get input value. */
740                         if (rect_float) {
741                                 copy_v4_v4(input, &rect_float[pixel_index]);
742                         }
743                         else {
744                                 straight_uchar_to_premul_float(input, &rect[pixel_index]);
745                         }
746                         IMB_colormanagement_colorspace_to_scene_linear_v3(input, avg->colorspace);
747                         copy_v4_v4(output, input);
748                         /* Get mask value. */
749                         if (mask_rect_float) {
750                                 copy_v3_v3(mask, mask_rect_float + pixel_index);
751                         }
752                         else if (mask_rect) {
753                                 rgb_uchar_to_float(mask, mask_rect + pixel_index);
754                         }
755                         /* Apply correction. */
756                         const float L = IMB_colormanagement_get_luminance(output);
757                         float I_l = output[0] + ic * (L - output[0]);
758                         float I_g = avg->cav[0] + ic * (avg->lav - avg->cav[0]);
759                         float I_a = I_l + ia * (I_g - I_l);
760                         output[0] /= (output[0] + powf(f * I_a, m));
761                         I_l = output[1] + ic * (L - output[1]);
762                         I_g = avg->cav[1] + ic * (avg->lav - avg->cav[1]);
763                         I_a = I_l + ia * (I_g - I_l);
764                         output[1] /= (output[1] + powf(f * I_a, m));
765                         I_l = output[2] + ic * (L - output[2]);
766                         I_g = avg->cav[2] + ic * (avg->lav - avg->cav[2]);
767                         I_a = I_l + ia * (I_g - I_l);
768                         output[2] /= (output[2] + powf(f * I_a, m));
769                         /* Apply mask. */
770                         output[0] = input[0] * (1.0f - mask[0]) + output[0] * mask[0];
771                         output[1] = input[1] * (1.0f - mask[1]) + output[1] * mask[1];
772                         output[2] = input[2] * (1.0f - mask[2]) + output[2] * mask[2];
773                         /* Copy result back. */
774                         IMB_colormanagement_scene_linear_to_colorspace_v3(output, avg->colorspace);
775                         if (rect_float) {
776                                 copy_v4_v4(&rect_float[pixel_index], output);
777                         }
778                         else {
779                                 premul_float_to_straight_uchar(&rect[pixel_index], output);
780                         }
781                 }
782         }
783 }
784
785 static void tonemapmodifier_apply(struct SequenceModifierData *smd,
786                                   ImBuf *ibuf,
787                                   ImBuf *mask)
788 {
789         SequencerTonemapModifierData *tmmd = (SequencerTonemapModifierData *) smd;
790         AvgLogLum data;
791         data.tmmd = tmmd;
792         data.colorspace = (ibuf->rect_float != NULL)
793                               ? ibuf->float_colorspace
794                               : ibuf->rect_colorspace;
795         float lsum = 0.0f;
796         int p = ibuf->x * ibuf->y;
797         float *fp = ibuf->rect_float;
798         unsigned char *cp = (unsigned char *)ibuf->rect;
799         float avl, maxl = -FLT_MAX, minl = FLT_MAX;
800         const float sc = 1.0f / p;
801         float Lav = 0.f;
802         float cav[4] = {0.0f, 0.0f, 0.0f, 0.0f};
803         while (p--) {
804                 float pixel[4];
805                 if (fp != NULL) {
806                         copy_v4_v4(pixel, fp);
807                 }
808                 else {
809                         straight_uchar_to_premul_float(pixel, cp);
810                 }
811                 IMB_colormanagement_colorspace_to_scene_linear_v3(pixel, data.colorspace);
812                 float L = IMB_colormanagement_get_luminance(pixel);
813                 Lav += L;
814                 add_v3_v3(cav, pixel);
815                 lsum += logf(max_ff(L, 0.0f) + 1e-5f);
816                 maxl = (L > maxl) ? L : maxl;
817                 minl = (L < minl) ? L : minl;
818                 if (fp != NULL) {
819                         fp += 4;
820                 }
821                 else {
822                         cp += 4;
823                 }
824         }
825         data.lav = Lav * sc;
826         mul_v3_v3fl(data.cav, cav, sc);
827         maxl = logf(maxl + 1e-5f);
828         minl = logf(minl + 1e-5f);
829         avl = lsum * sc;
830         data.auto_key = (maxl > minl) ? ((maxl - avl) / (maxl - minl)) : 1.0f;
831         float al = expf(avl);
832         data.al = (al == 0.0f) ? 0.0f : (tmmd->key / al);
833         data.igm = (tmmd->gamma == 0.0f) ? 1.0f : (1.0f / tmmd->gamma);
834
835         if (tmmd->type == SEQ_TONEMAP_RD_PHOTORECEPTOR) {
836                 modifier_apply_threaded(ibuf,
837                                         mask,
838                                         tonemapmodifier_apply_threaded_photoreceptor,
839                                         &data);
840         }
841         else /* if (tmmd->type == SEQ_TONEMAP_RD_SIMPLE) */ {
842                 modifier_apply_threaded(ibuf,
843                                         mask,
844                                         tonemapmodifier_apply_threaded_simple,
845                                         &data);
846         }
847 }
848
849 static SequenceModifierTypeInfo seqModifier_Tonemap = {
850         CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Tonemap"), /* name */
851         "SequencerTonemapModifierData",                 /* struct_name */
852         sizeof(SequencerTonemapModifierData),           /* struct_size */
853         tonemapmodifier_init_data,                      /* init_data */
854         NULL,                                           /* free_data */
855         NULL,                                           /* copy_data */
856         tonemapmodifier_apply                           /* apply */
857 };
858
859 /*********************** Modifier functions *************************/
860
861 static void sequence_modifier_type_info_init(void)
862 {
863 #define INIT_TYPE(typeName) (modifiersTypes[seqModifierType_##typeName] = &seqModifier_##typeName)
864
865         INIT_TYPE(ColorBalance);
866         INIT_TYPE(Curves);
867         INIT_TYPE(HueCorrect);
868         INIT_TYPE(BrightContrast);
869         INIT_TYPE(Mask);
870         INIT_TYPE(WhiteBalance);
871         INIT_TYPE(Tonemap);
872
873 #undef INIT_TYPE
874 }
875
876 const SequenceModifierTypeInfo *BKE_sequence_modifier_type_info_get(int type)
877 {
878         if (!modifierTypesInit) {
879                 sequence_modifier_type_info_init();
880                 modifierTypesInit = true;
881         }
882
883         return modifiersTypes[type];
884 }
885
886 SequenceModifierData *BKE_sequence_modifier_new(Sequence *seq, const char *name, int type, struct Scene *scene)
887 {
888         SequenceModifierData *smd;
889         const SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(type);
890
891         smd = MEM_callocN(smti->struct_size, "sequence modifier");
892
893         smd->type = type;
894         smd->flag |= SEQUENCE_MODIFIER_EXPANDED;
895         smd->scene = scene;
896
897         if (!name || !name[0])
898                 BLI_strncpy(smd->name, smti->name, sizeof(smd->name));
899         else
900                 BLI_strncpy(smd->name, name, sizeof(smd->name));
901
902         BLI_addtail(&seq->modifiers, smd);
903
904         BKE_sequence_modifier_unique_name(seq, smd);
905
906         if (smti->init_data)
907                 smti->init_data(smd);
908
909         return smd;
910 }
911
912 bool BKE_sequence_modifier_remove(Sequence *seq, SequenceModifierData *smd)
913 {
914         if (BLI_findindex(&seq->modifiers, smd) == -1)
915                 return false;
916
917         BLI_remlink(&seq->modifiers, smd);
918         BKE_sequence_modifier_free(smd);
919
920         return true;
921 }
922
923 void BKE_sequence_modifier_clear(Sequence *seq)
924 {
925         SequenceModifierData *smd, *smd_next;
926
927         for (smd = seq->modifiers.first; smd; smd = smd_next) {
928                 smd_next = smd->next;
929                 BKE_sequence_modifier_free(smd);
930         }
931
932         BLI_listbase_clear(&seq->modifiers);
933 }
934
935 void BKE_sequence_modifier_free(SequenceModifierData *smd)
936 {
937         const SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(smd->type);
938
939         if (smti && smti->free_data) {
940                 smti->free_data(smd);
941         }
942
943         MEM_freeN(smd);
944 }
945
946 void BKE_sequence_modifier_unique_name(Sequence *seq, SequenceModifierData *smd)
947 {
948         const SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(smd->type);
949
950         BLI_uniquename(&seq->modifiers, smd, CTX_DATA_(BLT_I18NCONTEXT_ID_SEQUENCE, smti->name), '.',
951                        offsetof(SequenceModifierData, name), sizeof(smd->name));
952 }
953
954 SequenceModifierData *BKE_sequence_modifier_find_by_name(Sequence *seq, const char *name)
955 {
956         return BLI_findstring(&(seq->modifiers), name, offsetof(SequenceModifierData, name));
957 }
958
959 ImBuf *BKE_sequence_modifier_apply_stack(const SeqRenderData *context, Sequence *seq, ImBuf *ibuf, int cfra)
960 {
961         SequenceModifierData *smd;
962         ImBuf *processed_ibuf = ibuf;
963
964         if (seq->modifiers.first && (seq->flag & SEQ_USE_LINEAR_MODIFIERS)) {
965                 processed_ibuf = IMB_dupImBuf(ibuf);
966                 BKE_sequencer_imbuf_from_sequencer_space(context->scene, processed_ibuf);
967         }
968
969         for (smd = seq->modifiers.first; smd; smd = smd->next) {
970                 const SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(smd->type);
971
972                 /* could happen if modifier is being removed or not exists in current version of blender */
973                 if (!smti)
974                         continue;
975
976                 /* modifier is muted, do nothing */
977                 if (smd->flag & SEQUENCE_MODIFIER_MUTE)
978                         continue;
979
980                 if (smti->apply) {
981                         int frame_offset;
982                         if (smd->mask_time == SEQUENCE_MASK_TIME_RELATIVE) {
983                                 frame_offset = seq->start;
984                         }
985                         else /*if (smd->mask_time == SEQUENCE_MASK_TIME_ABSOLUTE)*/ {
986                                 frame_offset = 0;
987                         }
988
989                         ImBuf *mask = modifier_mask_get(smd,
990                                                         context,
991                                                         cfra,
992                                                         frame_offset,
993                                                         ibuf->rect_float != NULL);
994
995                         if (processed_ibuf == ibuf)
996                                 processed_ibuf = IMB_dupImBuf(ibuf);
997
998                         smti->apply(smd, processed_ibuf, mask);
999
1000                         if (mask)
1001                                 IMB_freeImBuf(mask);
1002                 }
1003         }
1004
1005         if (seq->modifiers.first && (seq->flag & SEQ_USE_LINEAR_MODIFIERS)) {
1006                 BKE_sequencer_imbuf_to_sequencer_space(context->scene, processed_ibuf, false);
1007         }
1008
1009         return processed_ibuf;
1010 }
1011
1012 void BKE_sequence_modifier_list_copy(Sequence *seqn, Sequence *seq)
1013 {
1014         SequenceModifierData *smd;
1015
1016         for (smd = seq->modifiers.first; smd; smd = smd->next) {
1017                 SequenceModifierData *smdn;
1018                 const SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(smd->type);
1019
1020                 smdn = MEM_dupallocN(smd);
1021
1022                 if (smti && smti->copy_data)
1023                         smti->copy_data(smdn, smd);
1024
1025                 smdn->next = smdn->prev = NULL;
1026                 BLI_addtail(&seqn->modifiers, smdn);
1027         }
1028 }
1029
1030 int BKE_sequence_supports_modifiers(Sequence *seq)
1031 {
1032         return !ELEM(seq->type, SEQ_TYPE_SOUND_RAM, SEQ_TYPE_SOUND_HD);
1033 }