r827: Fix a crash when no audio output device can be opened.
[cinelerra_cv.git] / cinelerra / fadeengine.C
blobc20b411d8a7076cfbe417dee860760d739c724c2
1 #include "fadeengine.h"
2 #include "vframe.h"
4 #include <stdint.h>
14 FadeUnit::FadeUnit(FadeEngine *server)
15  : LoadClient(server)
17         this->engine = server;
20 FadeUnit::~FadeUnit()
25 #define APPLY_FADE(equivalent, \
26         input_rows,  \
27         output_rows,  \
28         max,  \
29         type,  \
30         temp_type,  \
31         chroma_zero,  \
32         components) \
33 { \
34         temp_type opacity = (temp_type)(alpha * max); \
35         temp_type transparency = (temp_type)(max - opacity); \
36         temp_type product = (temp_type) (chroma_zero * transparency); \
37  \
38         for(int i = row1; i < row2; i++) \
39         { \
40                 type *in_row = (type*)input_rows[i]; \
41                 type *out_row = (type*)output_rows[i]; \
42  \
43                 for(int j = 0; j < width; j++, out_row += components, in_row += components) \
44                 { \
45                         if(components == 3) \
46                         { \
47                                 out_row[0] =  \
48                                         (type)((temp_type)in_row[0] * opacity / max); \
49                                 out_row[1] =  \
50                                         (type)(((temp_type)in_row[1] * opacity +  \
51                                                 product) / max); \
52                                 out_row[2] =  \
53                                         (type)(((temp_type)in_row[2] * opacity +  \
54                                                 product) / max); \
55                         } \
56                         else \
57                         { \
58                                 if(!equivalent) \
59                                 { \
60                                         out_row[0] = in_row[0]; \
61                                         out_row[1] = in_row[1]; \
62                                         out_row[2] = in_row[2]; \
63                                 } \
64  \
65                                 if(in_row[3] == max) \
66                                         out_row[3] = opacity; \
67                                 else \
68                                 out_row[3] =  \
69                                         (type)((temp_type)in_row[3] * opacity / max); \
70                         } \
71                 } \
72         } \
77 void FadeUnit::process_package(LoadPackage *package)
79         FadePackage *pkg = (FadePackage*)package;
80         VFrame *output = engine->output;
81         VFrame *input = engine->input;
82         float alpha = engine->alpha;
83         int row1 = pkg->out_row1;
84         int row2 = pkg->out_row2;
85         unsigned char **in_rows = input->get_rows();
86         unsigned char **out_rows = output->get_rows();
87         int width = input->get_w();
89         if(input->get_rows()[0] == output->get_rows()[0])
90         {
91                 switch(input->get_color_model())
92                 {
93                         case BC_RGB888:
94                                 APPLY_FADE(1, out_rows, in_rows, 0xff, unsigned char, uint16_t, 0x0, 3);
95                                 break;
96                         case BC_RGBA8888:
97                                 APPLY_FADE(1, out_rows, in_rows, 0xff, unsigned char, uint16_t, 0x0, 4);
98                                 break;
99                         case BC_RGB_FLOAT:
100                                 APPLY_FADE(1, out_rows, in_rows, 1.0, float, float, 0x0, 3);
101                                 break;
102                         case BC_RGBA_FLOAT:
103                                 APPLY_FADE(1, out_rows, in_rows, 1.0, float, float, 0x0, 4);
104                                 break;
105                         case BC_RGB161616:
106                                 APPLY_FADE(1, out_rows, in_rows, 0xffff, uint16_t, uint32_t, 0x0, 3);
107                                 break;
108                         case BC_RGBA16161616:
109                                 APPLY_FADE(1, out_rows, in_rows, 0xffff, uint16_t, uint32_t, 0x0, 4);
110                                 break;
111                         case BC_YUV888:
112                                 APPLY_FADE(1, out_rows, in_rows, 0xff, unsigned char, uint16_t, 0x80, 3);
113                                 break;
114                         case BC_YUVA8888:
115                                 APPLY_FADE(1, out_rows, in_rows, 0xff, unsigned char, uint16_t, 0x80, 4);
116                                 break;
117                         case BC_YUV161616:
118                                 APPLY_FADE(1, out_rows, in_rows, 0xffff, uint16_t, uint32_t, 0x8000, 3);
119                                 break;
120                         case BC_YUVA16161616:
121                                 APPLY_FADE(1, out_rows, in_rows, 0xffff, uint16_t, uint32_t, 0x8000, 4);
122                                 break;
123                 }
124         }
125         else
126         {
127                 switch(input->get_color_model())
128                 {
129                         case BC_RGB888:
130                                 APPLY_FADE(0, out_rows, in_rows, 0xff, unsigned char, uint16_t, 0x0, 3);
131                                 break;
132                         case BC_RGBA8888:
133                                 APPLY_FADE(0, out_rows, in_rows, 0xff, unsigned char, uint16_t, 0x0, 4);
134                                 break;
135                         case BC_RGB_FLOAT:
136                                 APPLY_FADE(0, out_rows, in_rows, 1.0, float, float, 0x0, 3);
137                                 break;
138                         case BC_RGBA_FLOAT:
139                                 APPLY_FADE(0, out_rows, in_rows, 1.0, float, float, 0x0, 4);
140                                 break;
141                         case BC_RGB161616:
142                                 APPLY_FADE(0, out_rows, in_rows, 0xffff, uint16_t, uint32_t, 0x0, 3);
143                                 break;
144                         case BC_RGBA16161616:
145                                 APPLY_FADE(0, out_rows, in_rows, 0xffff, uint16_t, uint32_t, 0x0, 4);
146                                 break;
147                         case BC_YUV888:
148                                 APPLY_FADE(0, out_rows, in_rows, 0xff, unsigned char, uint16_t, 0x80, 3);
149                                 break;
150                         case BC_YUVA8888:
151                                 APPLY_FADE(0, out_rows, in_rows, 0xff, unsigned char, uint16_t, 0x80, 4);
152                                 break;
153                         case BC_YUV161616:
154                                 APPLY_FADE(0, out_rows, in_rows, 0xffff, uint16_t, uint32_t, 0x8000, 3);
155                                 break;
156                         case BC_YUVA16161616:
157                                 APPLY_FADE(0, out_rows, in_rows, 0xffff, uint16_t, uint32_t, 0x8000, 4);
158                                 break;
159                 }
160         }
171 FadeEngine::FadeEngine(int cpus)
172  : LoadServer(cpus, cpus)
176 FadeEngine::~FadeEngine()
180 void FadeEngine::do_fade(VFrame *output, VFrame *input, float alpha)
182         this->output = output;
183         this->input = input;
184         this->alpha = alpha;
185         
186 // Sanity
187         if(alpha == 1)
188                 output->copy_from(input);
189         else
190                 process_packages();
194 void FadeEngine::init_packages()
196         for(int i = 0; i < total_packages; i++)
197         {
198                 FadePackage *package = (FadePackage*)packages[i];
199                 package->out_row1 = input->get_h() * i / total_packages;
200                 package->out_row2 = input->get_h() * (i + 1) / total_packages;
201         }
204 LoadClient* FadeEngine::new_client()
206         return new FadeUnit(this);
209 LoadPackage* FadeEngine::new_package()
211         return new FadePackage;
215 FadePackage::FadePackage()