Internal node links are now cached in a per-node list, instead of being generated...
[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 "DNA_sequence_types.h"
43
44 #include "BKE_colortools.h"
45 #include "BKE_sequencer.h"
46
47 #include "IMB_imbuf.h"
48 #include "IMB_imbuf_types.h"
49
50 static SequenceModifierTypeInfo *modifiersTypes[NUM_SEQUENCE_MODIFIER_TYPES];
51 static int modifierTypesInit = FALSE;
52
53 /*********************** Modifiers *************************/
54
55 typedef void (*modifier_apply_threaded_cb) (int width, int height, unsigned char *rect, float *rect_float,
56                                             unsigned char *mask_rect, float *mask_rect_float, void *data_v);
57
58 typedef struct ModifierInitData {
59         ImBuf *ibuf;
60         ImBuf *mask;
61         void *user_data;
62
63         modifier_apply_threaded_cb apply_callback;
64 } ModifierInitData;
65
66 typedef struct ModifierThread {
67         int width, height;
68
69         unsigned char *rect, *mask_rect;
70         float *rect_float, *mask_rect_float;
71
72         void *user_data;
73
74         modifier_apply_threaded_cb apply_callback;
75 } ModifierThread;
76
77
78 static ImBuf *modifier_mask_get(SequenceModifierData *smd, SeqRenderData context, int cfra, int make_float)
79 {
80         return BKE_sequencer_render_mask_input(context, smd->mask_input_type, smd->mask_sequence, smd->mask_id, cfra, make_float);
81 }
82
83 static void modifier_init_handle(void *handle_v, int start_line, int tot_line, void *init_data_v)
84 {
85         ModifierThread *handle = (ModifierThread *) handle_v;
86         ModifierInitData *init_data = (ModifierInitData *) init_data_v;
87         ImBuf *ibuf = init_data->ibuf;
88         ImBuf *mask = init_data->mask;
89
90         int offset = 4 * start_line * ibuf->x;
91
92         memset(handle, 0, sizeof(ModifierThread));
93
94         handle->width = ibuf->x;
95         handle->height = tot_line;
96         handle->apply_callback = init_data->apply_callback;
97         handle->user_data = init_data->user_data;
98
99         if (ibuf->rect)
100                 handle->rect = (unsigned char *) ibuf->rect + offset;
101
102         if (ibuf->rect_float)
103                 handle->rect_float = ibuf->rect_float + offset;
104
105         if (mask) {
106                 if (mask->rect)
107                         handle->mask_rect = (unsigned char *) mask->rect + offset;
108
109                 if (mask->rect_float)
110                         handle->mask_rect_float = mask->rect_float + offset;
111         }
112         else {
113                 handle->mask_rect = NULL;
114                 handle->mask_rect_float = NULL;
115         }
116 }
117
118 static void *modifier_do_thread(void *thread_data_v)
119 {
120         ModifierThread *td = (ModifierThread *) thread_data_v;
121
122         td->apply_callback(td->width, td->height, td->rect, td->rect_float, td->mask_rect, td->mask_rect_float, td->user_data);
123
124         return NULL;
125 }
126
127 static void modifier_apply_threaded(ImBuf *ibuf, ImBuf *mask, modifier_apply_threaded_cb apply_callback, void *user_data)
128 {
129         ModifierInitData init_data;
130
131         init_data.ibuf = ibuf;
132         init_data.mask = mask;
133         init_data.user_data = user_data;
134
135         init_data.apply_callback = apply_callback;
136
137         IMB_processor_apply_threaded(ibuf->y, sizeof(ModifierThread), &init_data,
138                                  modifier_init_handle, modifier_do_thread);
139 }
140
141 /* **** Color Balance Modifier **** */
142
143 static void colorBalance_init_data(SequenceModifierData *smd)
144 {
145         ColorBalanceModifierData *cbmd = (ColorBalanceModifierData *) smd;
146         int c;
147
148         cbmd->color_multiply = 1.0f;
149
150         for (c = 0; c < 3; c++) {
151                 cbmd->color_balance.lift[c] = 1.0f;
152                 cbmd->color_balance.gamma[c] = 1.0f;
153                 cbmd->color_balance.gain[c] = 1.0f;
154         }
155 }
156
157 static void colorBalance_apply(SequenceModifierData *smd, ImBuf *ibuf, ImBuf *mask)
158 {
159         ColorBalanceModifierData *cbmd = (ColorBalanceModifierData *) smd;
160
161         BKE_sequencer_color_balance_apply(&cbmd->color_balance, ibuf, cbmd->color_multiply, FALSE, mask);
162 }
163
164 static SequenceModifierTypeInfo seqModifier_ColorBalance = {
165         "Color Balance",                   /* name */
166         "ColorBalanceModifierData",        /* struct_name */
167         sizeof(ColorBalanceModifierData),  /* struct_size */
168         colorBalance_init_data,            /* init_data */
169         NULL,                              /* free_data */
170         NULL,                              /* copy_data */
171         colorBalance_apply                 /* apply */
172 };
173
174 /* **** Curves Modifier **** */
175
176 static void curves_init_data(SequenceModifierData *smd)
177 {
178         CurvesModifierData *cmd = (CurvesModifierData *) smd;
179
180         curvemapping_set_defaults(&cmd->curve_mapping, 4, 0.0f, 0.0f, 1.0f, 1.0f);
181 }
182
183 static void curves_free_data(SequenceModifierData *smd)
184 {
185         CurvesModifierData *cmd = (CurvesModifierData *) smd;
186
187         curvemapping_free_data(&cmd->curve_mapping);
188 }
189
190 static void curves_copy_data(SequenceModifierData *target, SequenceModifierData *smd)
191 {
192         CurvesModifierData *cmd = (CurvesModifierData *) smd;
193         CurvesModifierData *cmd_target = (CurvesModifierData *) target;
194
195         curvemapping_copy_data(&cmd_target->curve_mapping, &cmd->curve_mapping);
196 }
197
198 static void curves_apply_threaded(int width, int height, unsigned char *rect, float *rect_float,
199                                   unsigned char *mask_rect, float *mask_rect_float, void *data_v)
200 {
201         CurveMapping *curve_mapping = (CurveMapping *) data_v;
202         int x, y;
203
204         for (y = 0; y < height; y++) {
205                 for (x = 0; x < width; x++) {
206                         int pixel_index = (y * width + x) * 4;
207
208                         if (rect_float) {
209                                 float *pixel = rect_float + pixel_index;
210                                 float result[3];
211
212                                 curvemapping_evaluate_premulRGBF(curve_mapping, result, pixel);
213
214                                 if (mask_rect_float) {
215                                         float *m = mask_rect_float + pixel_index;
216
217                                         pixel[0] = pixel[0] * (1.0f - m[0]) + result[0] * m[0];
218                                         pixel[1] = pixel[1] * (1.0f - m[1]) + result[1] * m[1];
219                                         pixel[2] = pixel[2] * (1.0f - m[2]) + result[2] * m[2];
220                                 }
221                                 else {
222                                         pixel[0] = result[0];
223                                         pixel[1] = result[1];
224                                         pixel[2] = result[2];
225                                 }
226                         }
227                         if (rect) {
228                                 unsigned char *pixel = rect + pixel_index;
229                                 unsigned char result[3];
230
231                                 curvemapping_evaluate_premulRGB(curve_mapping, result, pixel);
232
233                                 if (mask_rect) {
234                                         float t[3];
235
236                                         rgb_uchar_to_float(t, mask_rect + pixel_index);
237
238                                         pixel[0] = pixel[0] * (1.0f - t[0]) + result[0] * t[0];
239                                         pixel[1] = pixel[1] * (1.0f - t[1]) + result[1] * t[1];
240                                         pixel[2] = pixel[2] * (1.0f - t[2]) + result[2] * t[2];
241                                 }
242                                 else {
243                                         pixel[0] = result[0];
244                                         pixel[1] = result[1];
245                                         pixel[2] = result[2];
246                                 }
247                         }
248                 }
249         }
250 }
251
252 static void curves_apply(struct SequenceModifierData *smd, ImBuf *ibuf, ImBuf *mask)
253 {
254         CurvesModifierData *cmd = (CurvesModifierData *) smd;
255
256         float black[3] = {0.0f, 0.0f, 0.0f};
257         float white[3] = {1.0f, 1.0f, 1.0f};
258
259         curvemapping_initialize(&cmd->curve_mapping);
260
261         curvemapping_premultiply(&cmd->curve_mapping, 0);
262         curvemapping_set_black_white(&cmd->curve_mapping, black, white);
263
264         modifier_apply_threaded(ibuf, mask, curves_apply_threaded, &cmd->curve_mapping);
265
266         curvemapping_premultiply(&cmd->curve_mapping, 1);
267 }
268
269 static SequenceModifierTypeInfo seqModifier_Curves = {
270         "Curves",                    /* name */
271         "CurvesModifierData",        /* struct_name */
272         sizeof(CurvesModifierData),  /* struct_size */
273         curves_init_data,            /* init_data */
274         curves_free_data,            /* free_data */
275         curves_copy_data,            /* copy_data */
276         curves_apply                 /* apply */
277 };
278
279 /* **** Hue Correct Modifier **** */
280
281 static void hue_correct_init_data(SequenceModifierData *smd)
282 {
283         HueCorrectModifierData *hcmd = (HueCorrectModifierData *) smd;
284         int c;
285
286         curvemapping_set_defaults(&hcmd->curve_mapping, 1, 0.0f, 0.0f, 1.0f, 1.0f);
287         hcmd->curve_mapping.preset = CURVE_PRESET_MID9;
288
289         for (c = 0; c < 3; c++) {
290                 CurveMap *cuma = &hcmd->curve_mapping.cm[c];
291
292                 curvemap_reset(cuma, &hcmd->curve_mapping.clipr, hcmd->curve_mapping.preset, CURVEMAP_SLOPE_POSITIVE);
293         }
294
295         /* default to showing Saturation */
296         hcmd->curve_mapping.cur = 1;
297 }
298
299 static void hue_correct_free_data(SequenceModifierData *smd)
300 {
301         HueCorrectModifierData *hcmd = (HueCorrectModifierData *) smd;
302
303         curvemapping_free_data(&hcmd->curve_mapping);
304 }
305
306 static void hue_correct_copy_data(SequenceModifierData *target, SequenceModifierData *smd)
307 {
308         HueCorrectModifierData *hcmd = (HueCorrectModifierData *) smd;
309         HueCorrectModifierData *hcmd_target = (HueCorrectModifierData *) target;
310
311         curvemapping_copy_data(&hcmd_target->curve_mapping, &hcmd->curve_mapping);
312 }
313
314 static void hue_correct_apply_threaded(int width, int height, unsigned char *rect, float *rect_float,
315                                 unsigned char *mask_rect, float *mask_rect_float, void *data_v)
316 {
317         CurveMapping *curve_mapping = (CurveMapping *) data_v;
318         int x, y;
319
320         for (y = 0; y < height; y++) {
321                 for (x = 0; x < width; x++) {
322                         int pixel_index = (y * width + x) * 4;
323                         float pixel[3], result[3], mask[3] = {1.0f, 1.0f, 1.0f};
324                         float hsv[3], f;
325
326                         if (rect_float)
327                                 copy_v3_v3(pixel, rect_float + pixel_index);
328                         else
329                                 rgb_uchar_to_float(pixel, rect + pixel_index);
330
331                         rgb_to_hsv(pixel[0], pixel[1], pixel[2], hsv, hsv + 1, hsv + 2);
332
333                         /* adjust hue, scaling returned default 0.5 up to 1 */
334                         f = curvemapping_evaluateF(curve_mapping, 0, hsv[0]);
335                         hsv[0] += f - 0.5f;
336
337                         /* adjust saturation, scaling returned default 0.5 up to 1 */
338                         f = curvemapping_evaluateF(curve_mapping, 1, hsv[0]);
339                         hsv[1] *= (f * 2.0f);
340
341                         /* adjust value, scaling returned default 0.5 up to 1 */
342                         f = curvemapping_evaluateF(curve_mapping, 2, hsv[0]);
343                         hsv[2] *= (f * 2.f);
344
345                         hsv[0] = hsv[0] - floorf(hsv[0]); /* mod 1.0 */
346                         CLAMP(hsv[1], 0.0f, 1.0f);
347
348                         /* convert back to rgb */
349                         hsv_to_rgb(hsv[0], hsv[1], hsv[2], result, result + 1, result + 2);
350
351                         if (mask_rect_float)
352                                         copy_v3_v3(mask, mask_rect_float + pixel_index);
353                         else if (mask_rect)
354                                         rgb_uchar_to_float(mask, mask_rect + pixel_index);
355
356                         result[0] = pixel[0] * (1.0f - mask[0]) + result[0] * mask[0];
357                         result[1] = pixel[1] * (1.0f - mask[1]) + result[1] * mask[1];
358                         result[2] = pixel[2] * (1.0f - mask[2]) + result[2] * mask[2];
359
360                         if (rect_float)
361                                 copy_v3_v3(rect_float + pixel_index, result);
362                         else
363                                 rgb_float_to_uchar(rect + pixel_index, result);
364                 }
365         }
366 }
367
368 static void hue_correct_apply(struct SequenceModifierData *smd, ImBuf *ibuf, ImBuf *mask)
369 {
370         HueCorrectModifierData *hcmd = (HueCorrectModifierData *) smd;
371
372         curvemapping_initialize(&hcmd->curve_mapping);
373
374         modifier_apply_threaded(ibuf, mask, hue_correct_apply_threaded, &hcmd->curve_mapping);
375 }
376
377 static SequenceModifierTypeInfo seqModifier_HueCorrect = {
378         "Hue Correct",                    /* name */
379         "HueCorrectModifierData",         /* struct_name */
380         sizeof(HueCorrectModifierData),   /* struct_size */
381         hue_correct_init_data,            /* init_data */
382         hue_correct_free_data,            /* free_data */
383         hue_correct_copy_data,            /* copy_data */
384         hue_correct_apply                 /* apply */
385 };
386
387 /* **** Bright/Contrast Modifier **** */
388
389 typedef struct BrightContrastThreadData {
390         float bright;
391         float contrast;
392 } BrightContrastThreadData;
393
394 static void brightcontrast_apply_threaded(int width, int height, unsigned char *rect, float *rect_float,
395                                           unsigned char *mask_rect, float *mask_rect_float, void *data_v)
396 {
397         BrightContrastThreadData *data = (BrightContrastThreadData *) data_v;
398         int x, y;
399
400         float i;
401         int c;
402         float a, b, v;
403         float brightness = data->bright / 100.0f;
404         float contrast = data->contrast;
405         float delta = contrast / 200.0f;
406
407         a = 1.0f - delta * 2.0f;
408         /*
409          * The algorithm is by Werner D. Streidt
410          * (http://visca.com/ffactory/archives/5-99/msg00021.html)
411          * Extracted of OpenCV demhist.c
412          */
413         if (contrast > 0) {
414                 a = 1.0f / a;
415                 b = a * (brightness - delta);
416         }
417         else {
418                 delta *= -1;
419                 b = a * (brightness + delta);
420         }
421
422         for (y = 0; y < height; y++) {
423                 for (x = 0; x < width; x++) {
424                         int pixel_index = (y * width + x) * 4;
425
426                         if (rect) {
427                                 unsigned char *pixel = rect + pixel_index;
428
429                                 for (c = 0; c < 3; c++) {
430                                         i = pixel[c];
431                                         v = a * i + b;
432
433                                         if (mask_rect) {
434                                                 unsigned char *m = mask_rect + pixel_index;
435                                                 float t = (float) m[c] / 255.0f;
436
437                                                 pixel[c] = pixel[c] * (1.0f - t) + v * t;
438                                         }
439                                         else
440                                                 pixel[c] = v;
441                                 }
442                         }
443                         else if (rect_float) {
444                                 float *pixel = rect_float + pixel_index;
445
446                                 for (c = 0; c < 3; c++) {
447                                         i = pixel[c];
448                                         v = a * i + b;
449
450                                         if (mask_rect_float) {
451                                                 float *m = mask_rect_float + pixel_index;
452
453                                                 pixel[c] = pixel[c] * (1.0f - m[c]) + v * m[c];
454                                         }
455                                         else
456                                                 pixel[c] = v;
457                                 }
458                         }
459                 }
460         }
461 }
462
463 static void brightcontrast_apply(struct SequenceModifierData *smd, ImBuf *ibuf, ImBuf *mask)
464 {
465         BrightContrastModifierData *bcmd = (BrightContrastModifierData *) smd;
466         BrightContrastThreadData data;
467
468         data.bright = bcmd->bright;
469         data.contrast = bcmd->contrast;
470
471         modifier_apply_threaded(ibuf, mask, brightcontrast_apply_threaded, &data);
472 }
473
474 static SequenceModifierTypeInfo seqModifier_BrightContrast = {
475         "Bright/Contrast",                   /* name */
476         "BrightContrastModifierData",        /* struct_name */
477         sizeof(BrightContrastModifierData),  /* struct_size */
478         NULL,                                /* init_data */
479         NULL,                                /* free_data */
480         NULL,                                /* copy_data */
481         brightcontrast_apply                 /* apply */
482 };
483
484 /*********************** Modifier functions *************************/
485
486 static void sequence_modifier_type_info_init(void)
487 {
488 #define INIT_TYPE(typeName) (modifiersTypes[seqModifierType_##typeName] = &seqModifier_##typeName)
489
490         INIT_TYPE(ColorBalance);
491         INIT_TYPE(Curves);
492         INIT_TYPE(HueCorrect);
493         INIT_TYPE(BrightContrast);
494
495 #undef INIT_TYPE
496 }
497
498 SequenceModifierTypeInfo *BKE_sequence_modifier_type_info_get(int type)
499 {
500         if (!modifierTypesInit) {
501                 sequence_modifier_type_info_init();
502                 modifierTypesInit = TRUE;
503         }
504
505         return modifiersTypes[type];
506 }
507
508 SequenceModifierData *BKE_sequence_modifier_new(Sequence *seq, const char *name, int type)
509 {
510         SequenceModifierData *smd;
511         SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(type);
512
513         smd = MEM_callocN(smti->struct_size, "sequence modifier");
514
515         smd->type = type;
516         smd->flag |= SEQUENCE_MODIFIER_EXPANDED;
517
518         if (!name || !name[0])
519                 BLI_strncpy(smd->name, smti->name, sizeof(smd->name));
520         else
521                 BLI_strncpy(smd->name, name, sizeof(smd->name));
522
523         BLI_addtail(&seq->modifiers, smd);
524
525         BKE_sequence_modifier_unique_name(seq, smd);
526
527         if (smti->init_data)
528                 smti->init_data(smd);
529
530         return smd;
531 }
532
533 int BKE_sequence_modifier_remove(Sequence *seq, SequenceModifierData *smd)
534 {
535         if (BLI_findindex(&seq->modifiers, smd) == -1)
536                 return FALSE;
537
538         BLI_remlink(&seq->modifiers, smd);
539         BKE_sequence_modifier_free(smd);
540
541         return TRUE;
542 }
543
544 void BKE_sequence_modifier_clear(Sequence *seq)
545 {
546         SequenceModifierData *smd, *smd_next;
547
548         for (smd = seq->modifiers.first; smd; smd = smd_next) {
549                 smd_next = smd->next;
550                 BKE_sequence_modifier_free(smd);
551         }
552
553         seq->modifiers.first = seq->modifiers.last = NULL;
554 }
555
556 void BKE_sequence_modifier_free(SequenceModifierData *smd)
557 {
558         SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(smd->type);
559
560         if (smti && smti->free_data) {
561                 smti->free_data(smd);
562         }
563
564         MEM_freeN(smd);
565 }
566
567 void BKE_sequence_modifier_unique_name(Sequence *seq, SequenceModifierData *smd)
568 {
569         SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(smd->type);
570
571         BLI_uniquename(&seq->modifiers, smd, smti->name, '.', offsetof(SequenceModifierData, name), sizeof(smd->name));
572 }
573
574 SequenceModifierData *BKE_sequence_modifier_find_by_name(Sequence *seq, char *name)
575 {
576         return BLI_findstring(&(seq->modifiers), name, offsetof(SequenceModifierData, name));
577 }
578
579 ImBuf *BKE_sequence_modifier_apply_stack(SeqRenderData context, Sequence *seq, ImBuf *ibuf, int cfra)
580 {
581         SequenceModifierData *smd;
582         ImBuf *processed_ibuf = ibuf;
583
584         if (seq->modifiers.first && (seq->flag & SEQ_USE_LINEAR_MODIFIERS)) {
585                 processed_ibuf = IMB_dupImBuf(ibuf);
586                 BKE_sequencer_imbuf_from_sequencer_space(context.scene, processed_ibuf);
587         }
588
589         for (smd = seq->modifiers.first; smd; smd = smd->next) {
590                 SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(smd->type);
591
592                 /* could happen if modifier is being removed or not exists in current version of blender */
593                 if (!smti)
594                         continue;
595
596                 /* modifier is muted, do nothing */
597                 if (smd->flag & SEQUENCE_MODIFIER_MUTE)
598                         continue;
599
600                 if (smti->apply) {
601                         ImBuf *mask = modifier_mask_get(smd, context, cfra, ibuf->rect_float != NULL);
602
603                         if (processed_ibuf == ibuf)
604                                 processed_ibuf = IMB_dupImBuf(ibuf);
605
606                         smti->apply(smd, processed_ibuf, mask);
607
608                         if (mask)
609                                 IMB_freeImBuf(mask);
610                 }
611         }
612
613         if (seq->modifiers.first && (seq->flag & SEQ_USE_LINEAR_MODIFIERS)) {
614                 BKE_sequencer_imbuf_to_sequencer_space(context.scene, processed_ibuf, FALSE);
615         }
616
617         return processed_ibuf;
618 }
619
620 void BKE_sequence_modifier_list_copy(Sequence *seqn, Sequence *seq)
621 {
622         SequenceModifierData *smd;
623
624         for (smd = seq->modifiers.first; smd; smd = smd->next) {
625                 SequenceModifierData *smdn;
626                 SequenceModifierTypeInfo *smti = BKE_sequence_modifier_type_info_get(smd->type);
627
628                 smdn = MEM_dupallocN(smd);
629
630                 if (smti && smti->copy_data)
631                         smti->copy_data(smdn, smd);
632
633                 smdn->next = smdn->prev = NULL;
634                 BLI_addtail(&seqn->modifiers, smdn);
635         }
636 }
637
638 int BKE_sequence_supports_modifiers(Sequence *seq)
639 {
640         return !ELEM(seq->type, SEQ_TYPE_SOUND_RAM, SEQ_TYPE_SOUND_HD);
641 }