e4ac81feeca8ff1ff8a2b9ca4bc4e8d48e2f4df4
[goodguy/history.git] / cinelerra-5.1 / guicast / xfer.C
1
2 void BC_Xfer::init(
3         uint8_t **output_rows, int out_colormodel, int out_x, int out_y, int out_w, int out_h,
4                 uint8_t *out_yp, uint8_t *out_up, uint8_t *out_vp, uint8_t *out_ap, int out_rowspan,
5         uint8_t **input_rows, int in_colormodel, int in_x, int in_y, int in_w, int in_h,
6                 uint8_t *in_yp, uint8_t *in_up, uint8_t *in_vp, uint8_t *in_ap, int in_rowspan,
7         int bg_color)
8 {
9         // prevent bounds errors on poorly dimensioned macro pixel formats
10         switch( in_colormodel ) {
11         case BC_UVY422:
12         case BC_YUV422:   in_w &= ~1;               break;  // 2x1
13         case BC_YUV420P:
14         case BC_YUV420PI: in_w &= ~1;  in_h &= ~1;  break;  // 2x2
15         case BC_YUV422P:  in_w &= ~1;               break;  // 2x1
16         case BC_YUV410P:  in_w &= ~3;  in_h &= ~3;  break;  // 4x4
17         case BC_YUV411P:  in_w &= ~3;               break;  // 4x1
18         }
19         switch( out_colormodel ) {
20         case BC_UVY422:
21         case BC_YUV422:  out_w &= ~1;               break;
22         case BC_YUV420P:
23         case BC_YUV420PI: out_w &= ~1; out_h &= ~1; break;
24         case BC_YUV422P: out_w &= ~1;               break;
25         case BC_YUV410P: out_w &= ~3; out_h &= ~3;  break;
26         case BC_YUV411P: out_w &= ~3;               break;
27         }
28         this->output_rows = output_rows;
29         this->input_rows = input_rows;
30         this->out_yp = out_yp;
31         this->out_up = out_up;
32         this->out_vp = out_vp;
33         this->out_ap = out_ap;
34         this->in_yp = in_yp;
35         this->in_up = in_up;
36         this->in_vp = in_vp;
37         this->in_ap = in_ap;
38         this->in_x = in_x;
39         this->in_y = in_y;
40         this->in_w = in_w;
41         this->in_h = in_h;
42         this->out_x = out_x;
43         this->out_y = out_y;
44         this->out_w = out_w;
45         this->out_h = out_h;
46         this->in_colormodel = in_colormodel;
47         switch( in_colormodel ) {
48         case BC_RGB_FLOATP:
49         case BC_RGBA_FLOATP:
50                 if( !BC_CModels::has_alpha(out_colormodel) )
51                         this->in_colormodel = BC_RGB_FLOATP;
52                 in_rowspan = in_w * sizeof(float);
53                 break;
54         }
55         this->total_in_w = in_rowspan;
56         this->out_colormodel = out_colormodel;
57         switch( out_colormodel ) {
58         case BC_RGB_FLOATP:
59         case BC_RGBA_FLOATP:
60                 out_rowspan = out_w * sizeof(float);
61                 break;
62         }
63         this->total_out_w = out_rowspan;
64         this->bg_color = bg_color;
65         this->bg_r = (bg_color>>16) & 0xff;
66         this->bg_g = (bg_color>>8) & 0xff;
67         this->bg_b = (bg_color>>0) & 0xff;
68
69         this->in_pixelsize = BC_CModels::calculate_pixelsize(in_colormodel);
70         this->out_pixelsize = BC_CModels::calculate_pixelsize(out_colormodel);
71         this->scale = (out_w != in_w) || (in_x != 0);
72
73 /* + 1 so we don't overflow when calculating in advance */
74         column_table = new int[out_w+1];
75         double hscale = (double)in_w/out_w;
76         for( int i=0; i<out_w; ++i ) {
77                         column_table[i] = ((int)(hscale * i) + in_x) * in_pixelsize;
78                         if( in_colormodel == BC_YUV422 || in_colormodel == BC_UVY422 )
79                                 column_table[i] &= ~3;
80         }
81         double vscale = (double)in_h/out_h;
82         row_table = new int[out_h];
83         for( int i=0; i<out_h; ++i )
84                 row_table[i] = (int)(vscale * i) + in_y;
85 }
86
87 BC_Xfer::BC_Xfer(uint8_t **output_rows, uint8_t **input_rows,
88         uint8_t *out_yp, uint8_t *out_up, uint8_t *out_vp,
89         uint8_t *in_yp, uint8_t *in_up, uint8_t *in_vp,
90         int in_x, int in_y, int in_w, int in_h, int out_x, int out_y, int out_w, int out_h,
91         int in_colormodel, int out_colormodel, int bg_color, int in_rowspan, int out_rowspan)
92 {
93         init(output_rows, out_colormodel, out_x, out_y, out_w, out_h,
94                 out_yp, out_up, out_vp, 0, out_rowspan,
95              input_rows, in_colormodel, in_x, in_y, in_w, in_h,
96                 in_yp, in_up, in_vp, 0, in_rowspan,  bg_color);
97 }
98
99 BC_Xfer::BC_Xfer(
100         uint8_t **output_ptrs, int out_colormodel,
101                 int out_x, int out_y, int out_w, int out_h, int out_rowspan,
102         uint8_t **input_ptrs, int in_colormodel,
103                 int in_x, int in_y, int in_w, int in_h, int in_rowspan,
104         int bg_color)
105 {
106         uint8_t *out_yp = 0, *out_up = 0, *out_vp = 0, *out_ap = 0;
107         uint8_t *in_yp = 0,  *in_up = 0,  *in_vp = 0,  *in_ap = 0;
108         if( BC_CModels::is_planar(in_colormodel) ) {
109                 in_yp = input_ptrs[0]; in_up = input_ptrs[1]; in_vp = input_ptrs[2];
110                 if( BC_CModels::has_alpha(in_colormodel) ) in_ap = input_ptrs[3];
111         }
112         if( BC_CModels::is_planar(out_colormodel) ) {
113                 out_yp = output_ptrs[0]; out_up = output_ptrs[1]; out_vp = output_ptrs[2];
114                 if( BC_CModels::has_alpha(out_colormodel) ) out_ap = output_ptrs[3];
115         }
116         init(output_ptrs, out_colormodel, out_x, out_y, out_w, out_h,
117                  out_yp, out_up, out_vp, out_ap, out_rowspan,
118              input_ptrs, in_colormodel, in_x, in_y, in_w, in_h,
119                  in_yp, in_up, in_vp, in_ap, in_rowspan,  bg_color);
120 }
121
122 BC_Xfer::~BC_Xfer()
123 {
124         delete [] column_table;
125         delete [] row_table;
126 }
127
128 void BC_CModels::transfer(unsigned char **output_rows, unsigned char **input_rows,
129         unsigned char *out_yp, unsigned char *out_up, unsigned char *out_vp,
130         unsigned char *in_yp, unsigned char *in_up, unsigned char *in_vp,
131         int in_x, int in_y, int in_w, int in_h, int out_x, int out_y, int out_w, int out_h,
132         int in_colormodel, int out_colormodel, int bg_color, int in_rowspan, int out_rowspan)
133 {
134         BC_Xfer xfer(output_rows, input_rows,
135                 out_yp, out_up, out_vp, in_yp, in_up, in_vp,
136                 in_x, in_y, in_w, in_h, out_x, out_y, out_w, out_h,
137                 in_colormodel, out_colormodel, bg_color, in_rowspan, out_rowspan);
138         xfer.xfer();
139 }
140
141 void BC_CModels::transfer(
142         uint8_t **output_ptrs, int out_colormodel,
143                 int out_x, int out_y, int out_w, int out_h, int out_rowspan,
144         uint8_t **input_ptrs, int in_colormodel,
145                 int in_x, int in_y, int in_w, int in_h, int in_rowspan,  int bg_color)
146 {
147         BC_Xfer xfer(
148                 output_ptrs, out_colormodel, out_x, out_y, out_w, out_h, out_rowspan,
149                 input_ptrs, in_colormodel, in_x, in_y, in_w, in_h, in_rowspan,  bg_color);
150         xfer.xfer();
151 }
152
153 // specialized functions
154
155 //  in bccmdl.py:  specialize("bc_rgba8888", "bc_transparency", "XFER_rgba8888_to_transparency")
156 void BC_Xfer::XFER_rgba8888_to_transparency(unsigned y0, unsigned y1)
157 {
158   for( unsigned i=y0; i<y1; ++i ) {
159     uint8_t *outp = output_rows[i + out_y] + out_x * out_pixelsize;
160     uint8_t *inp_row = input_rows[row_table[i]];
161     int bit_no = 0, bit_vec = 0;
162     for( unsigned j=0; j<out_w; ) {
163       uint8_t *inp = inp_row + column_table[j];
164       if( inp[3] < 127 ) bit_vec |= 0x01 << bit_no;
165       bit_no = ++j & 7;
166       if( !bit_no ) { *outp++ = bit_vec;  bit_vec = 0; }
167     }
168     if( bit_no ) *outp = bit_vec;
169   }
170 }
171
172 BC_Xfer::SlicerList BC_Xfer::slicers;
173
174 BC_Xfer::SlicerList::SlicerList()
175 {
176   count = 0;
177 }
178
179 BC_Xfer::SlicerList::~SlicerList()
180 {
181   reset();
182 }
183
184 void BC_Xfer::SlicerList::reset()
185 {
186   lock("BC_Xfer::SlicerList::reset");
187   while( last ) remove(last);
188   count = 0;
189   unlock();
190 }
191
192 BC_Xfer::Slicer *BC_Xfer::SlicerList::get_slicer(BC_Xfer *xp)
193 {
194   Slicer *slicer = first;
195   if( !slicer ) {
196     if( count < BC_Resources::machine_cpus ) {
197       slicer = new Slicer(xp);
198       ++count;
199     }
200   }
201   else
202     remove_pointer(slicer);
203   return slicer;
204 }
205
206 void BC_Xfer::xfer_slices(int slices)
207 {
208   if( !xfn ) return;
209   int max_slices = BC_Resources::machine_cpus/2;
210   if( slices > max_slices ) slices = max_slices;
211   if( slices < 1 ) slices = 1;
212   Slicer *active[slices];
213   unsigned y0 = 0, y1 = out_h;
214   int slices1 = slices-1;
215   if( slices1 > 0 ) {
216     slicers.lock("BC_Xfer::xfer_slices");
217     for( int i=0; i<slices1; y0=y1 ) {
218       Slicer *slicer = slicers.get_slicer(this);
219       if( !slicer ) { slices1 = i;  break; }
220       active[i] = slicer;
221       y1 = out_h * ++i / slices;
222       slicer->slice(this, y0, y1);
223     }
224     slicers.unlock();
225   }
226   (this->*xfn)(y0, out_h);
227   if( slices1 > 0 ) {
228     for( int i=0; i<slices1; ++i )
229       active[i]->complete->lock("BC_Xfer::xfer_slices");
230     slicers.lock("BC_Xfer::xfer_slices");
231     for( int i=0; i<slices1; ++i )
232       slicers.append(active[i]);
233     slicers.unlock();
234   }
235 }
236
237 BC_Xfer::Slicer::Slicer(BC_Xfer *xp)
238 {
239   this->xp = xp;
240   init = new Condition(0, "BC_Xfer::Slicer::init", 0);
241   complete = new Condition(0, "BC_Xfer::Slicer::complete", 0);
242   done = 0;
243   start();
244 }
245 BC_Xfer::Slicer::~Slicer()
246 {
247   done = 1;
248   init->unlock();
249   join();
250 }
251
252 void BC_Xfer::Slicer::slice(BC_Xfer *xp, unsigned y0, unsigned y1)
253 {
254   this->xp = xp;
255   this->y0 = y0;
256   this->y1 = y1;
257   init->unlock();
258 }
259
260 void BC_Xfer::Slicer::run()
261 {
262   while( !done ) {
263     init->lock("Slicer::run");
264     if( done ) break;
265     xfer_fn xfn = xp->xfn;
266     (xp->*xfn)(y0, y1);
267     complete->unlock();
268   }
269 }
270
271 void BC_CModels::bcxfer_stop_slicers()
272 {
273   BC_Xfer::slicers.reset();
274 }
275