Cleanup: Remove more #if 0 blocks
[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_string.h"
38 #include "BLI_string_utils.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         float white[3];
188 } WhiteBalanceThreadData;
189
190 static void whiteBalance_apply_threaded(int width, int height, unsigned char *rect, float *rect_float,
191                                         unsigned char *mask_rect, float *mask_rect_float, void *data_v)
192 {
193         int x, y;
194         float multiplier[3];
195
196         WhiteBalanceThreadData *data = (WhiteBalanceThreadData *) data_v;
197
198         multiplier[0] = (data->white[0] != 0.0f) ? 1.0f / data->white[0] : FLT_MAX;
199         multiplier[1] = (data->white[1] != 0.0f) ? 1.0f / data->white[1] : FLT_MAX;
200         multiplier[2] = (data->white[2] != 0.0f) ? 1.0f / data->white[2] : FLT_MAX;
201
202         for (y = 0; y < height; y++) {
203                 for (x = 0; x < width; x++) {
204                         int pixel_index = (y * width + x) * 4;
205                         float rgba[4], result[4], mask[3] = {1.0f, 1.0f, 1.0f};
206
207                         if (rect_float) {
208                                 copy_v3_v3(rgba, rect_float + pixel_index);
209                         }
210                         else {
211                                 straight_uchar_to_premul_float(rgba, rect + pixel_index);
212                         }
213
214                         copy_v4_v4(result, rgba);
215
216                         /* similar to division without the clipping */
217                         for (int i = 0; i < 3; i++) {
218                                 result[i] = 1.0f - powf(1.0f - rgba[i], multiplier[i]);
219                         }
220
221                         if (mask_rect_float) {
222                                 copy_v3_v3(mask, mask_rect_float + pixel_index);
223                         }
224                         else if (mask_rect) {
225                                 rgb_uchar_to_float(mask, mask_rect + pixel_index);
226                         }
227
228                         result[0] = rgba[0] * (1.0f - mask[0]) + result[0] * mask[0];
229                         result[1] = rgba[1] * (1.0f - mask[1]) + result[1] * mask[1];
230                         result[2] = rgba[2] * (1.0f - mask[2]) + result[2] * mask[2];
231
232                         if (rect_float) {
233                                 copy_v3_v3(rect_float + pixel_index, result);
234                         }
235                         else {
236                                 premul_float_to_straight_uchar(rect + pixel_index, result);
237                         }
238                 }
239         }
240 }
241
242 static void whiteBalance_apply(SequenceModifierData *smd, ImBuf *ibuf, ImBuf *mask)
243 {
244         WhiteBalanceThreadData data;
245         WhiteBalanceModifierData *wbmd = (WhiteBalanceModifierData *) smd;
246
247         copy_v3_v3(data.white, wbmd->white_value);
248
249         modifier_apply_threaded(ibuf, mask, whiteBalance_apply_threaded, &data);
250 }
251
252 static SequenceModifierTypeInfo seqModifier_WhiteBalance = {
253         CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "White Balance"),  /* name */
254         "WhiteBalanceModifierData",                            /* struct_name */
255         sizeof(WhiteBalanceModifierData),                      /* struct_size */
256         whiteBalance_init_data,                                /* init_data */
257         NULL,                                                  /* free_data */
258         NULL,                                                  /* copy_data */
259         whiteBalance_apply                                     /* apply */
260 };
261
262 /* **** Curves Modifier **** */
263
264 static void curves_init_data(SequenceModifierData *smd)
265 {
266         CurvesModifierData *cmd = (CurvesModifierData *) smd;
267
268         curvemapping_set_defaults(&cmd->curve_mapping, 4, 0.0f, 0.0f, 1.0f, 1.0f);
269 }
270
271 static void curves_free_data(SequenceModifierData *smd)
272 {
273         CurvesModifierData *cmd = (CurvesModifierData *) smd;
274
275         curvemapping_free_data(&cmd->curve_mapping);
276 }
277
278 static void curves_copy_data(SequenceModifierData *target, SequenceModifierData *smd)
279 {
280         CurvesModifierData *cmd = (CurvesModifierData *) smd;
281         CurvesModifierData *cmd_target = (CurvesModifierData *) target;
282
283         curvemapping_copy_data(&cmd_target->curve_mapping, &cmd->curve_mapping);
284 }
285
286 static void curves_apply_threaded(int width, int height, unsigned char *rect, float *rect_float,
287                                   unsigned char *mask_rect, float *mask_rect_float, void *data_v)
288 {
289         CurveMapping *curve_mapping = (CurveMapping *) data_v;
290         int x, y;
291
292         for (y = 0; y < height; y++) {
293                 for (x = 0; x < width; x++) {
294                         int pixel_index = (y * width + x) * 4;
295
296                         if (rect_float) {
297                                 float *pixel = rect_float + pixel_index;
298                                 float result[3];
299
300                                 curvemapping_evaluate_premulRGBF(curve_mapping, result, pixel);
301
302                                 if (mask_rect_float) {
303                                         const float *m = mask_rect_float + pixel_index;
304
305                                         pixel[0] = pixel[0] * (1.0f - m[0]) + result[0] * m[0];
306                                         pixel[1] = pixel[1] * (1.0f - m[1]) + result[1] * m[1];
307                                         pixel[2] = pixel[2] * (1.0f - m[2]) + result[2] * m[2];
308                                 }
309                                 else {
310                                         pixel[0] = result[0];
311                                         pixel[1] = result[1];
312                                         pixel[2] = result[2];
313                                 }
314                         }
315                         if (rect) {
316                                 unsigned char *pixel = rect + pixel_index;
317                                 float result[3], tempc[4];
318
319                                 straight_uchar_to_premul_float(tempc, pixel);
320
321                                 curvemapping_evaluate_premulRGBF(curve_mapping, result, tempc);
322
323                                 if (mask_rect) {
324                                         float t[3];
325
326                                         rgb_uchar_to_float(t, mask_rect + pixel_index);
327
328                                         tempc[0] = tempc[0] * (1.0f - t[0]) + result[0] * t[0];
329                                         tempc[1] = tempc[1] * (1.0f - t[1]) + result[1] * t[1];
330                                         tempc[2] = tempc[2] * (1.0f - t[2]) + result[2] * t[2];
331                                 }
332                                 else {
333                                         tempc[0] = result[0];
334                                         tempc[1] = result[1];
335                                         tempc[2] = result[2];
336                                 }
337
338                                 premul_float_to_straight_uchar(pixel, tempc);
339                         }
340                 }
341         }
342 }
343
344 static void curves_apply(struct SequenceModifierData *smd, ImBuf *ibuf, ImBuf *mask)
345 {
346         CurvesModifierData *cmd = (CurvesModifierData *) smd;
347
348         float black[3] = {0.0f, 0.0f, 0.0f};
349         float white[3] = {1.0f, 1.0f, 1.0f};
350
351         curvemapping_initialize(&cmd->curve_mapping);
352
353         curvemapping_premultiply(&cmd->curve_mapping, 0);
354         curvemapping_set_black_white(&cmd->curve_mapping, black, white);
355
356         modifier_apply_threaded(ibuf, mask, curves_apply_threaded, &cmd->curve_mapping);
357
358         curvemapping_premultiply(&cmd->curve_mapping, 1);
359 }
360
361 static SequenceModifierTypeInfo seqModifier_Curves = {
362         CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Curves"),   /* name */
363         "CurvesModifierData",                            /* struct_name */
364         sizeof(CurvesModifierData),                      /* struct_size */
365         curves_init_data,                                /* init_data */
366         curves_free_data,                                /* free_data */
367         curves_copy_data,                                /* copy_data */
368         curves_apply                                     /* apply */
369 };
370
371 /* **** Hue Correct Modifier **** */
372
373 static void hue_correct_init_data(SequenceModifierData *smd)
374 {
375         HueCorrectModifierData *hcmd = (HueCorrectModifierData *) smd;
376         int c;
377
378         curvemapping_set_defaults(&hcmd->curve_mapping, 1, 0.0f, 0.0f, 1.0f, 1.0f);
379         hcmd->curve_mapping.preset = CURVE_PRESET_MID9;
380
381         for (c = 0; c < 3; c++) {
382                 CurveMap *cuma = &hcmd->curve_mapping.cm[c];
383
384                 curvemap_reset(cuma, &hcmd->curve_mapping.clipr, hcmd->curve_mapping.preset, CURVEMAP_SLOPE_POSITIVE);
385         }
386
387         /* default to showing Saturation */
388         hcmd->curve_mapping.cur = 1;
389 }
390
391 static void hue_correct_free_data(SequenceModifierData *smd)
392 {
393         HueCorrectModifierData *hcmd = (HueCorrectModifierData *) smd;
394
395         curvemapping_free_data(&hcmd->curve_mapping);
396 }
397
398 static void hue_correct_copy_data(SequenceModifierData *target, SequenceModifierData *smd)
399 {
400         HueCorrectModifierData *hcmd = (HueCorrectModifierData *) smd;
401         HueCorrectModifierData *hcmd_target = (HueCorrectModifierData *) target;
402
403         curvemapping_copy_data(&hcmd_target->curve_mapping, &hcmd->curve_mapping);
404 }
405
406 static void hue_correct_apply_threaded(int width, int height, unsigned char *rect, float *rect_float,
407                                 unsigned char *mask_rect, float *mask_rect_float, void *data_v)
408 {
409         CurveMapping *curve_mapping = (CurveMapping *) data_v;
410         int x, y;
411
412         for (y = 0; y < height; y++) {
413                 for (x = 0; x < width; x++) {
414                         int pixel_index = (y * width + x) * 4;
415                         float pixel[3], result[3], mask[3] = {1.0f, 1.0f, 1.0f};
416                         float hsv[3], f;
417
418                         if (rect_float)
419                                 copy_v3_v3(pixel, rect_float + pixel_index);
420                         else
421                                 rgb_uchar_to_float(pixel, rect + pixel_index);
422
423                         rgb_to_hsv(pixel[0], pixel[1], pixel[2], hsv, hsv + 1, hsv + 2);
424
425                         /* adjust hue, scaling returned default 0.5 up to 1 */
426                         f = curvemapping_evaluateF(curve_mapping, 0, hsv[0]);
427                         hsv[0] += f - 0.5f;
428
429                         /* adjust saturation, scaling returned default 0.5 up to 1 */
430                         f = curvemapping_evaluateF(curve_mapping, 1, hsv[0]);
431                         hsv[1] *= (f * 2.0f);
432
433                         /* adjust value, scaling returned default 0.5 up to 1 */
434                         f = curvemapping_evaluateF(curve_mapping, 2, hsv[0]);
435                         hsv[2] *= (f * 2.f);
436
437                         hsv[0] = hsv[0] - floorf(hsv[0]); /* mod 1.0 */
438                         CLAMP(hsv[1], 0.0f, 1.0f);
439
440                         /* convert back to rgb */
441                         hsv_to_rgb(hsv[0], hsv[1], hsv[2], result, result + 1, result + 2);
442
443                         if (mask_rect_float)
444                                 copy_v3_v3(mask, mask_rect_float + pixel_index);
445                         else if (mask_rect)
446                                 rgb_uchar_to_float(mask, mask_rect + pixel_index);
447
448                         result[0] = pixel[0] * (1.0f - mask[0]) + result[0] * mask[0];
449                         result[1] = pixel[1] * (1.0f - mask[1]) + result[1] * mask[1];
450                         result[2] = pixel[2] * (1.0f - mask[2]) + result[2] * mask[2];
451
452                         if (rect_float)
453                                 copy_v3_v3(rect_float + pixel_index, result);
454                         else
455                                 rgb_float_to_uchar(rect + pixel_index, result);
456                 }
457         }
458 }
459
460 static void hue_correct_apply(struct SequenceModifierData *smd, ImBuf *ibuf, ImBuf *mask)
461 {
462         HueCorrectModifierData *hcmd = (HueCorrectModifierData *) smd;
463
464         curvemapping_initialize(&hcmd->curve_mapping);
465
466         modifier_apply_threaded(ibuf, mask, hue_correct_apply_threaded, &hcmd->curve_mapping);
467 }
468
469 static SequenceModifierTypeInfo seqModifier_HueCorrect = {
470         CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Hue Correct"),    /* name */
471         "HueCorrectModifierData",                              /* struct_name */
472         sizeof(HueCorrectModifierData),                        /* struct_size */
473         hue_correct_init_data,                                 /* init_data */
474         hue_correct_free_data,                                 /* free_data */
475         hue_correct_copy_data,                                 /* copy_data */
476         hue_correct_apply                                      /* apply */
477 };
478
479 /* **** Bright/Contrast Modifier **** */
480
481 typedef struct BrightContrastThreadData {
482         float bright;
483         float contrast;
484 } BrightContrastThreadData;
485
486 static void brightcontrast_apply_threaded(int width, int height, unsigned char *rect, float *rect_float,
487                                           unsigned char *mask_rect, float *mask_rect_float, void *data_v)
488 {
489         BrightContrastThreadData *data = (BrightContrastThreadData *) data_v;
490         int x, y;
491
492         float i;
493         int c;
494         float a, b, v;
495         float brightness = data->bright / 100.0f;
496         float contrast = data->contrast;
497         float delta = contrast / 200.0f;
498
499         a = 1.0f - delta * 2.0f;
500         /*
501          * The algorithm is by Werner D. Streidt
502          * (http://visca.com/ffactory/archives/5-99/msg00021.html)
503          * Extracted of OpenCV demhist.c
504          */
505         if (contrast > 0) {
506                 a = 1.0f / a;
507                 b = a * (brightness - delta);
508         }
509         else {
510                 delta *= -1;
511                 b = a * (brightness + delta);
512         }
513
514         for (y = 0; y < height; y++) {
515                 for (x = 0; x < width; x++) {
516                         int pixel_index = (y * width + x) * 4;
517
518                         if (rect) {
519                                 unsigned char *pixel = rect + pixel_index;
520
521                                 for (c = 0; c < 3; c++) {
522                                         i = (float) pixel[c] / 255.0f;
523                                         v = a * i + b;
524
525                                         if (mask_rect) {
526                                                 unsigned char *m = mask_rect + pixel_index;
527                                                 float t = (float) m[c] / 255.0f;
528
529                                                 v = (float) pixel[c] / 255.0f * (1.0f - t) + v * t;
530                                         }
531
532                                         pixel[c] = unit_float_to_uchar_clamp(v);
533                                 }
534                         }
535                         else if (rect_float) {
536                                 float *pixel = rect_float + pixel_index;
537
538                                 for (c = 0; c < 3; c++) {
539                                         i = pixel[c];
540                                         v = a * i + b;
541
542                                         if (mask_rect_float) {
543                                                 const float *m = mask_rect_float + pixel_index;
544
545                                                 pixel[c] = pixel[c] * (1.0f - m[c]) + v * m[c];
546                                         }
547                                         else
548                                                 pixel[c] = v;
549                                 }
550                         }
551                 }
552         }
553 }
554
555 static void brightcontrast_apply(struct SequenceModifierData *smd, ImBuf *ibuf, ImBuf *mask)
556 {
557         BrightContrastModifierData *bcmd = (BrightContrastModifierData *) smd;
558         BrightContrastThreadData data;
559
560         data.bright = bcmd->bright;
561         data.contrast = bcmd->contrast;
562
563         modifier_apply_threaded(ibuf, mask, brightcontrast_apply_threaded, &data);
564 }
565
566 static SequenceModifierTypeInfo seqModifier_BrightContrast = {
567         CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Bright/Contrast"),   /* name */
568         "BrightContrastModifierData",                             /* struct_name */
569         sizeof(BrightContrastModifierData),                       /* struct_size */
570         NULL,                                                     /* init_data */
571         NULL,                                                     /* free_data */
572         NULL,                                                     /* copy_data */
573         brightcontrast_apply                                      /* apply */
574 };
575
576 /* **** Mask Modifier **** */
577
578 static void maskmodifier_apply_threaded(int width, int height, unsigned char *rect, float *rect_float,
579                                         unsigned char *mask_rect, float *mask_rect_float, void *UNUSED(data_v))
580 {
581         int x, y;
582
583         if (rect && !mask_rect)
584                 return;
585
586         if (rect_float && !mask_rect_float)
587                 return;
588
589         for (y = 0; y < height; y++) {
590                 for (x = 0; x < width; x++) {
591                         int pixel_index = (y * width + x) * 4;
592
593                         if (rect) {
594                                 unsigned char *pixel = rect + pixel_index;
595                                 unsigned char *mask_pixel = mask_rect + pixel_index;
596                                 unsigned char mask = min_iii(mask_pixel[0], mask_pixel[1], mask_pixel[2]);
597
598                                 /* byte buffer is straight, so only affect on alpha itself,
599                                  * this is the only way to alpha-over byte strip after
600                                  * applying mask modifier.
601                                  */
602                                 pixel[3] = (float)(pixel[3] * mask) / 255.0f;
603                         }
604                         else if (rect_float) {
605                                 int c;
606                                 float *pixel = rect_float + pixel_index;
607                                 const float *mask_pixel = mask_rect_float + pixel_index;
608                                 float mask = min_fff(mask_pixel[0], mask_pixel[1], mask_pixel[2]);
609
610                                 /* float buffers are premultiplied, so need to premul color
611                                  * as well to make it easy to alpha-over masted strip.
612                                  */
613                                 for (c = 0; c < 4; c++)
614                                         pixel[c] = pixel[c] * mask;
615                         }
616                 }
617         }
618 }
619
620 static void maskmodifier_apply(struct SequenceModifierData *UNUSED(smd), ImBuf *ibuf, ImBuf *mask)
621 {
622         // SequencerMaskModifierData *bcmd = (SequencerMaskModifierData *)smd;
623
624         modifier_apply_threaded(ibuf, mask, maskmodifier_apply_threaded, NULL);
625 }
626
627 static SequenceModifierTypeInfo seqModifier_Mask = {
628         CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Mask"), /* name */
629         "SequencerMaskModifierData",                 /* struct_name */
630         sizeof(SequencerMaskModifierData),           /* struct_size */
631         NULL,                                        /* init_data */
632         NULL,                                        /* free_data */
633         NULL,                                        /* copy_data */
634         maskmodifier_apply                           /* apply */
635 };
636
637 /* **** Tonemap Modifier **** */
638
639 typedef struct AvgLogLum {
640         SequencerTonemapModifierData *tmmd;
641         struct ColorSpace *colorspace;
642         float al;
643         float auto_key;
644         float lav;
645         float cav[4];
646         float igm;
647 } AvgLogLum;
648
649 static void tonemapmodifier_init_data(SequenceModifierData *smd)
650 {
651         SequencerTonemapModifierData *tmmd = (SequencerTonemapModifierData *) smd;
652         /* Same as tonemap compositor node. */
653         tmmd->type = SEQ_TONEMAP_RD_PHOTORECEPTOR;
654         tmmd->key = 0.18f;
655         tmmd->offset = 1.0f;
656         tmmd->gamma = 1.0f;
657         tmmd->intensity = 0.0f;
658         tmmd->contrast = 0.0f;
659         tmmd->adaptation = 1.0f;
660         tmmd->correction = 0.0f;
661 }
662
663 static void tonemapmodifier_apply_threaded_simple(int width,
664                                                   int height,
665                                                   unsigned char *rect,
666                                                   float *rect_float,
667                                                   unsigned char *mask_rect,
668                                                   float *mask_rect_float,
669                                                   void *data_v)
670 {
671         AvgLogLum *avg = (AvgLogLum *)data_v;
672         for (int y = 0; y < height; y++) {
673                 for (int x = 0; x < width; x++) {
674                         int pixel_index = (y * width + x) * 4;
675                         float input[4], output[4], mask[3] = {1.0f, 1.0f, 1.0f};
676                         /* Get input value. */
677                         if (rect_float) {
678                                 copy_v4_v4(input, &rect_float[pixel_index]);
679                         }
680                         else {
681                                 straight_uchar_to_premul_float(input, &rect[pixel_index]);
682                         }
683                         IMB_colormanagement_colorspace_to_scene_linear_v3(input, avg->colorspace);
684                         copy_v4_v4(output, input);
685                         /* Get mask value. */
686                         if (mask_rect_float) {
687                                 copy_v3_v3(mask, mask_rect_float + pixel_index);
688                         }
689                         else if (mask_rect) {
690                                 rgb_uchar_to_float(mask, mask_rect + pixel_index);
691                         }
692                         /* Apply correction. */
693                         mul_v3_fl(output, avg->al);
694                         float dr = output[0] + avg->tmmd->offset;
695                         float dg = output[1] + avg->tmmd->offset;
696                         float db = output[2] + avg->tmmd->offset;
697                         output[0] /= ((dr == 0.0f) ? 1.0f : dr);
698                         output[1] /= ((dg == 0.0f) ? 1.0f : dg);
699                         output[2] /= ((db == 0.0f) ? 1.0f : db);
700                         const float igm = avg->igm;
701                         if (igm != 0.0f) {
702                                 output[0] = powf(max_ff(output[0], 0.0f), igm);
703                                 output[1] = powf(max_ff(output[1], 0.0f), igm);
704                                 output[2] = powf(max_ff(output[2], 0.0f), igm);
705                         }
706                         /* Apply mask. */
707                         output[0] = input[0] * (1.0f - mask[0]) + output[0] * mask[0];
708                         output[1] = input[1] * (1.0f - mask[1]) + output[1] * mask[1];
709                         output[2] = input[2] * (1.0f - mask[2]) + output[2] * mask[2];
710                         /* Copy result back. */
711                         IMB_colormanagement_scene_linear_to_colorspace_v3(output, avg->colorspace);
712                         if (rect_float) {
713                                 copy_v4_v4(&rect_float[pixel_index], output);
714                         }
715                         else {
716                                 premul_float_to_straight_uchar(&rect[pixel_index], output);
717                         }
718                 }
719         }
720 }
721
722 static void tonemapmodifier_apply_threaded_photoreceptor(int width,
723                                                          int height,
724                                                          unsigned char *rect,
725                                                          float *rect_float,
726                                                          unsigned char *mask_rect,
727                                                          float *mask_rect_float,
728                                                          void *data_v)
729 {
730         AvgLogLum *avg = (AvgLogLum *)data_v;
731         const float f = expf(-avg->tmmd->intensity);
732         const float m = (avg->tmmd->contrast > 0.0f) ? avg->tmmd->contrast : (0.3f + 0.7f * powf(avg->auto_key, 1.4f));
733         const float ic = 1.0f - avg->tmmd->correction, ia = 1.0f - avg->tmmd->adaptation;
734         for (int y = 0; y < height; y++) {
735                 for (int x = 0; x < width; x++) {
736                         int pixel_index = (y * width + x) * 4;
737                         float input[4], output[4], mask[3] = {1.0f, 1.0f, 1.0f};
738                         /* Get input value. */
739                         if (rect_float) {
740                                 copy_v4_v4(input, &rect_float[pixel_index]);
741                         }
742                         else {
743                                 straight_uchar_to_premul_float(input, &rect[pixel_index]);
744                         }
745                         IMB_colormanagement_colorspace_to_scene_linear_v3(input, avg->colorspace);
746                         copy_v4_v4(output, input);
747                         /* Get mask value. */
748                         if (mask_rect_float) {
749                                 copy_v3_v3(mask, mask_rect_float + pixel_index);
750                         }
751                         else if (mask_rect) {
752                                 rgb_uchar_to_float(mask, mask_rect + pixel_index);
753                         }
754                         /* Apply correction. */
755                         const float L = IMB_colormanagement_get_luminance(output);
756                         float I_l = output[0] + ic * (L - output[0]);
757                         float I_g = avg->cav[0] + ic * (avg->lav - avg->cav[0]);
758                         float I_a = I_l + ia * (I_g - I_l);
759                         output[0] /= (output[0] + powf(f * I_a, m));
760                         I_l = output[1] + ic * (L - output[1]);
761                         I_g = avg->cav[1] + ic * (avg->lav - avg->cav[1]);
762                         I_a = I_l + ia * (I_g - I_l);
763                         output[1] /= (output[1] + powf(f * I_a, m));
764                         I_l = output[2] + ic * (L - output[2]);
765                         I_g = avg->cav[2] + ic * (avg->lav - avg->cav[2]);
766                         I_a = I_l + ia * (I_g - I_l);
767                         output[2] /= (output[2] + powf(f * I_a, m));
768                         /* Apply mask. */
769                         output[0] = input[0] * (1.0f - mask[0]) + output[0] * mask[0];
770                         output[1] = input[1] * (1.0f - mask[1]) + output[1] * mask[1];
771                         output[2] = input[2] * (1.0f - mask[2]) + output[2] * mask[2];
772                         /* Copy result back. */
773                         IMB_colormanagement_scene_linear_to_colorspace_v3(output, avg->colorspace);
774                         if (rect_float) {
775                                 copy_v4_v4(&rect_float[pixel_index], output);
776                         }
777                         else {
778                                 premul_float_to_straight_uchar(&rect[pixel_index], output);
779                         }
780                 }
781         }
782 }
783
784 static void tonemapmodifier_apply(struct SequenceModifierData *smd,
785                                   ImBuf *ibuf,
786                                   ImBuf *mask)
787 {
788         SequencerTonemapModifierData *tmmd = (SequencerTonemapModifierData *) smd;
789         AvgLogLum data;
790         data.tmmd = tmmd;
791         data.colorspace = (ibuf->rect_float != NULL)
792                               ? ibuf->float_colorspace
793                               : ibuf->rect_colorspace;
794         float lsum = 0.0f;
795         int p = ibuf->x * ibuf->y;
796         float *fp = ibuf->rect_float;
797         unsigned char *cp = (unsigned char *)ibuf->rect;
798         float avl, maxl = -FLT_MAX, minl = FLT_MAX;
799         const float sc = 1.0f / p;
800         float Lav = 0.f;
801         float cav[4] = {0.0f, 0.0f, 0.0f, 0.0f};
802         while (p--) {
803                 float pixel[4];
804                 if (fp != NULL) {
805                         copy_v4_v4(pixel, fp);
806                 }
807                 else {
808                         straight_uchar_to_premul_float(pixel, cp);
809                 }
810                 IMB_colormanagement_colorspace_to_scene_linear_v3(pixel, data.colorspace);
811                 float L = IMB_colormanagement_get_luminance(pixel);
812                 Lav += L;
813                 add_v3_v3(cav, pixel);
814                 lsum += logf(max_ff(L, 0.0f) + 1e-5f);
815                 maxl = (L > maxl) ? L : maxl;
816                 minl = (L < minl) ? L : minl;
817                 if (fp != NULL) {
818                         fp += 4;
819                 }
820                 else {
821                         cp += 4;
822                 }
823         }
824         data.lav = Lav * sc;
825         mul_v3_v3fl(data.cav, cav, sc);
826         maxl = logf(maxl + 1e-5f);
827         minl = logf(minl + 1e-5f);
828         avl = lsum * sc;
829         data.auto_key = (maxl > minl) ? ((maxl - avl) / (maxl - minl)) : 1.0f;
830         float al = expf(avl);
831         data.al = (al == 0.0f) ? 0.0f : (tmmd->key / al);
832         data.igm = (tmmd->gamma == 0.0f) ? 1.0f : (1.0f / tmmd->gamma);
833
834         if (tmmd->type == SEQ_TONEMAP_RD_PHOTORECEPTOR) {
835                 modifier_apply_threaded(ibuf,
836                                         mask,
837                                         tonemapmodifier_apply_threaded_photoreceptor,
838                                         &data);
839         }
840         else /* if (tmmd->type == SEQ_TONEMAP_RD_SIMPLE) */ {
841                 modifier_apply_threaded(ibuf,
842                                         mask,
843                                         tonemapmodifier_apply_threaded_simple,
844                                         &data);
845         }
846 }
847
848 static SequenceModifierTypeInfo seqModifier_Tonemap = {
849         CTX_N_(BLT_I18NCONTEXT_ID_SEQUENCE, "Tonemap"), /* name */
850         "SequencerTonemapModifierData",                 /* struct_name */
851         sizeof(SequencerTonemapModifierData),           /* struct_size */
852         tonemapmodifier_init_data,                      /* init_data */
853         NULL,                                           /* free_data */
854         NULL,                                           /* copy_data */
855         tonemapmodifier_apply                           /* apply */
856 };
857
858 /*********************** Modifier functions *************************/
859
860 static void sequence_modifier_type_info_init(void)
861 {
862 #define INIT_TYPE(typeName) (modifiersTypes[seqModifierType_##typeName] = &seqModifier_##typeName)
863
864         INIT_TYPE(ColorBalance);
865         INIT_TYPE(Curves);
866         INIT_TYPE(HueCorrect);
867         INIT_TYPE(BrightContrast);
868         INIT_TYPE(Mask);
869         INIT_TYPE(WhiteBalance);
870         INIT_TYPE(Tonemap);
871
872 #undef INIT_TYPE
873 }
874
875 const SequenceModifierTypeInfo *BKE_sequence_modifier_type_info_get(int type)
876 {
877         if (!modifierTypesInit) {
878                 sequence_modifier_type_info_init();
879                 modifierTypesInit = true;
880         }
881
882         return modifiersTypes[type];
883 }
884
885 SequenceModifierData *BKE_sequence_modifier_new(Sequence *seq, const char *name, int type)
886 {
887         SequenceModifierData *smd;
888         const SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(type);
889
890         smd = MEM_callocN(smti->struct_size, "sequence modifier");
891
892         smd->type = type;
893         smd->flag |= SEQUENCE_MODIFIER_EXPANDED;
894
895         if (!name || !name[0])
896                 BLI_strncpy(smd->name, smti->name, sizeof(smd->name));
897         else
898                 BLI_strncpy(smd->name, name, sizeof(smd->name));
899
900         BLI_addtail(&seq->modifiers, smd);
901
902         BKE_sequence_modifier_unique_name(seq, smd);
903
904         if (smti->init_data)
905                 smti->init_data(smd);
906
907         return smd;
908 }
909
910 bool BKE_sequence_modifier_remove(Sequence *seq, SequenceModifierData *smd)
911 {
912         if (BLI_findindex(&seq->modifiers, smd) == -1)
913                 return false;
914
915         BLI_remlink(&seq->modifiers, smd);
916         BKE_sequence_modifier_free(smd);
917
918         return true;
919 }
920
921 void BKE_sequence_modifier_clear(Sequence *seq)
922 {
923         SequenceModifierData *smd, *smd_next;
924
925         for (smd = seq->modifiers.first; smd; smd = smd_next) {
926                 smd_next = smd->next;
927                 BKE_sequence_modifier_free(smd);
928         }
929
930         BLI_listbase_clear(&seq->modifiers);
931 }
932
933 void BKE_sequence_modifier_free(SequenceModifierData *smd)
934 {
935         const SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(smd->type);
936
937         if (smti && smti->free_data) {
938                 smti->free_data(smd);
939         }
940
941         MEM_freeN(smd);
942 }
943
944 void BKE_sequence_modifier_unique_name(Sequence *seq, SequenceModifierData *smd)
945 {
946         const SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(smd->type);
947
948         BLI_uniquename(&seq->modifiers, smd, CTX_DATA_(BLT_I18NCONTEXT_ID_SEQUENCE, smti->name), '.',
949                        offsetof(SequenceModifierData, name), sizeof(smd->name));
950 }
951
952 SequenceModifierData *BKE_sequence_modifier_find_by_name(Sequence *seq, const char *name)
953 {
954         return BLI_findstring(&(seq->modifiers), name, offsetof(SequenceModifierData, name));
955 }
956
957 ImBuf *BKE_sequence_modifier_apply_stack(const SeqRenderData *context, Sequence *seq, ImBuf *ibuf, int cfra)
958 {
959         SequenceModifierData *smd;
960         ImBuf *processed_ibuf = ibuf;
961
962         if (seq->modifiers.first && (seq->flag & SEQ_USE_LINEAR_MODIFIERS)) {
963                 processed_ibuf = IMB_dupImBuf(ibuf);
964                 BKE_sequencer_imbuf_from_sequencer_space(context->scene, processed_ibuf);
965         }
966
967         for (smd = seq->modifiers.first; smd; smd = smd->next) {
968                 const SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(smd->type);
969
970                 /* could happen if modifier is being removed or not exists in current version of blender */
971                 if (!smti)
972                         continue;
973
974                 /* modifier is muted, do nothing */
975                 if (smd->flag & SEQUENCE_MODIFIER_MUTE)
976                         continue;
977
978                 if (smti->apply) {
979                         int frame_offset;
980                         if (smd->mask_time == SEQUENCE_MASK_TIME_RELATIVE) {
981                                 frame_offset = seq->start;
982                         }
983                         else /*if (smd->mask_time == SEQUENCE_MASK_TIME_ABSOLUTE)*/ {
984                                 frame_offset = 0;
985                         }
986
987                         ImBuf *mask = modifier_mask_get(smd,
988                                                         context,
989                                                         cfra,
990                                                         frame_offset,
991                                                         ibuf->rect_float != NULL);
992
993                         if (processed_ibuf == ibuf)
994                                 processed_ibuf = IMB_dupImBuf(ibuf);
995
996                         smti->apply(smd, processed_ibuf, mask);
997
998                         if (mask)
999                                 IMB_freeImBuf(mask);
1000                 }
1001         }
1002
1003         if (seq->modifiers.first && (seq->flag & SEQ_USE_LINEAR_MODIFIERS)) {
1004                 BKE_sequencer_imbuf_to_sequencer_space(context->scene, processed_ibuf, false);
1005         }
1006
1007         return processed_ibuf;
1008 }
1009
1010 void BKE_sequence_modifier_list_copy(Sequence *seqn, Sequence *seq)
1011 {
1012         SequenceModifierData *smd;
1013
1014         for (smd = seq->modifiers.first; smd; smd = smd->next) {
1015                 SequenceModifierData *smdn;
1016                 const SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(smd->type);
1017
1018                 smdn = MEM_dupallocN(smd);
1019
1020                 if (smti && smti->copy_data)
1021                         smti->copy_data(smdn, smd);
1022
1023                 smdn->next = smdn->prev = NULL;
1024                 BLI_addtail(&seqn->modifiers, smdn);
1025         }
1026 }
1027
1028 int BKE_sequence_supports_modifiers(Sequence *seq)
1029 {
1030         return !ELEM(seq->type, SEQ_TYPE_SOUND_RAM, SEQ_TYPE_SOUND_HD);
1031 }