no longer need ffmpeg patch0 which was for Termux
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / bcbitmap.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcbitmap.h"
23 #include "bcipc.h"
24 #include "bcresources.h"
25 #include "bcsignals.h"
26 #include "bcwindow.h"
27 #include "bccmodels.h"
28 #include "vframe.h"
29
30 #include <string.h>
31 #include <unistd.h>
32
33 #ifdef HAVE_XV
34 #include <X11/extensions/Xvlib.h>
35 #endif
36
37 int BC_Bitmap::max_active_buffers = 0;
38 int BC_Bitmap::zombies = 0;
39
40
41 BC_BitmapImage::BC_BitmapImage(BC_Bitmap *bitmap, int index)
42 {
43         this->index = index;
44         this->bitmap = bitmap;
45         this->top_level = bitmap->top_level;
46         this->drawable = 0;
47         this->data = 0;
48         this->row_data = 0;
49         this->dataSize = 0;
50         this->bytesPerLine = 0;
51         this->bitsPerPixel = 0;
52 }
53
54 BC_BitmapImage::~BC_BitmapImage()
55 {
56         delete [] data;
57         delete [] row_data;
58 }
59
60 bool BC_BitmapImage::is_avail()
61 {
62         return list == &bitmap->avail;
63 }
64
65 BC_BitmapImage *BC_Bitmap::cur_bfr()
66 {
67         if( !active_bfr ) {
68                 avail_lock->lock("BC_Bitmap::cur_bfr 1");
69                 if( (!use_shm || top_level->is_running()) &&
70                      (active_bfr=avail.first) != 0 )
71                         avail.remove_pointer(active_bfr);
72                 else {
73                         update_buffers(buffer_count+1, 0);
74                         if( (active_bfr=avail.first) != 0 )
75                                 avail.remove_pointer(active_bfr);
76                         else
77                                 active_bfr = new_buffer(type, -1);
78                 }
79                 avail_lock->unlock();
80         }
81         return active_bfr;
82 }
83
84 int BC_BitmapImage::
85 read_frame_rgb(VFrame *frame)
86 {
87         int w = bitmap->w, h = bitmap->h;
88         if( frame->get_w() != w || frame->get_h() != h ||
89             frame->get_color_model() != BC_RGB888 )
90                 frame->reallocate(0, 0, 0, 0, 0, w, h, BC_RGB888, -1);
91         uint8_t *dp = frame->get_data();
92         int rmask = ximage->red_mask;
93         int gmask = ximage->green_mask;
94         int bmask = ximage->blue_mask;
95 //due to a bug in the xserver, the mask is being cleared by XShm.C:402
96 //  when a faulty data in a XShmGetImage reply returns a zero visual
97 //workaround, use the original visual for the mask data
98         if ( !rmask ) rmask = bitmap->top_level->vis->red_mask;
99         if ( !gmask ) gmask = bitmap->top_level->vis->green_mask;
100         if ( !bmask ) bmask = bitmap->top_level->vis->blue_mask;
101         double fr = 255.0 / rmask;
102         double fg = 255.0 / gmask;
103         double fb = 255.0 / bmask;
104         int nbyte = (ximage->bits_per_pixel+7)/8;
105         int b0 = ximage->byte_order == MSBFirst ? 0 : nbyte-1;
106         int bi = ximage->byte_order == MSBFirst ? 1 : -1;
107         uint8_t *data = (uint8_t *)ximage->data;
108
109         for( int y=0;  y<h; ++y ) {
110                 uint8_t *cp = data + y*ximage->bytes_per_line + b0;
111                 for( int x=0; x<w; ++x, cp+=nbyte ) {
112                         uint8_t *bp = cp;  uint32_t n = *bp;
113                         for( int i=nbyte; --i>0; n=(n<<8)|*bp ) bp += bi;
114                         *dp++ = (int)((n&rmask)*fr + 0.5);
115                         *dp++ = (int)((n&gmask)*fg + 0.5);
116                         *dp++ = (int)((n&bmask)*fb + 0.5);
117                 }
118         }
119         return 0;
120 }
121
122 void BC_Bitmap::reque(BC_BitmapImage *bfr)
123 {
124         avail_lock->lock("BC_Bitmap::reque");
125         --active_buffers;
126         avail.append(bfr);
127         avail_lock->unlock();
128 }
129
130
131 #ifdef HAVE_XV
132 BC_XvShmImage::BC_XvShmImage(BC_Bitmap *bitmap, int index,
133         int w, int h, int color_model)
134  : BC_BitmapImage(bitmap, index)
135 {
136         Display *display = top_level->get_display();
137         int id = BC_CModels::bc_to_x(color_model);
138 // Create the XvImage
139         xv_image = XvShmCreateImage(display, bitmap->xv_portid, id,
140                         0, w, h, &shm_info);
141         dataSize = xv_image->data_size;
142 // Create the shared memory
143         shm_info.shmid = shmget(IPC_PRIVATE,
144                 dataSize + 8, IPC_CREAT | 0777);
145         if(shm_info.shmid < 0)
146                 perror("BC_XvShmImage::BC_XvShmImage shmget");
147         data = (unsigned char *)shmat(shm_info.shmid, NULL, 0);
148 // This causes it to automatically delete when the program exits.
149         shmctl(shm_info.shmid, IPC_RMID, 0);
150 // setting ximage->data stops BadValue
151         xv_image->data = shm_info.shmaddr = (char*)data;
152         shm_info.readOnly = 0;
153 // Get the real parameters
154         w = xv_image->width;
155         h = xv_image->height;
156         if(!XShmAttach(top_level->display, &shm_info))
157                 perror("BC_XvShmImage::BC_XvShmImage XShmAttach");
158         if( color_model == BC_YUV422 || color_model == BC_UVY422 ) {
159                 bytesPerLine = w*2;
160                 bitsPerPixel = 12;
161                 row_data = new unsigned char*[h];
162                 for( int i=0; i<h; ++i )
163                         row_data[i] = &data[i*bytesPerLine];
164         }
165 }
166
167 BC_XvShmImage::~BC_XvShmImage()
168 {
169         data = 0;
170         XFree(xv_image);
171         XShmDetach(top_level->display, &shm_info);
172         shmdt(shm_info.shmaddr);
173 }
174 #endif
175
176
177 BC_XShmImage::BC_XShmImage(BC_Bitmap *bitmap, int index,
178         int w, int h, int color_model)
179  : BC_BitmapImage(bitmap, index)
180 {
181         Display *display = top_level->display;
182         Visual *visual = top_level->vis;
183         int default_depth = bitmap->get_default_depth();
184         ximage = XShmCreateImage(display, visual,
185                 default_depth, default_depth == 1 ? XYBitmap : ZPixmap,
186                 (char*)NULL, &shm_info, w, h);
187 // Create shared memory
188         bytesPerLine = ximage->bytes_per_line;
189         bitsPerPixel = ximage->bits_per_pixel;
190         dataSize = h * bytesPerLine;
191         shm_info.shmid = shmget(IPC_PRIVATE,
192                 dataSize + 8, IPC_CREAT | 0777);
193         if(shm_info.shmid < 0)
194                 perror("BC_XShmImage::BC_XShmImage shmget");
195         data = (unsigned char *)shmat(shm_info.shmid, NULL, 0);
196 // This causes it to automatically delete when the program exits.
197         shmctl(shm_info.shmid, IPC_RMID, 0);
198         ximage->data = shm_info.shmaddr = (char*)data;
199         shm_info.readOnly = 0;
200 // Get the real parameters
201         if(!XShmAttach(top_level->display, &shm_info))
202                 perror("BC_XShmImage::BC_XShmImage XShmAttach");
203         row_data = new unsigned char*[h];
204         for( int i=0; i<h; ++i )
205                 row_data[i] = &data[i*bytesPerLine];
206 }
207
208 BC_XShmImage::~BC_XShmImage()
209 {
210         data = 0;
211         ximage->data = 0;
212         XDestroyImage(ximage);
213         XShmDetach(top_level->display, &shm_info);
214         XFlush(top_level->display);
215         shmdt(shm_info.shmaddr);
216 }
217
218
219
220 #ifdef HAVE_XV
221 BC_XvImage::BC_XvImage(BC_Bitmap *bitmap, int index,
222         int w, int h, int color_model)
223  : BC_BitmapImage(bitmap, index)
224 {
225         Display *display = top_level->display;
226         int id = BC_CModels::bc_to_x(color_model);
227         xv_image = XvCreateImage(display, bitmap->xv_portid, id, 0, w, h);
228         dataSize = xv_image->data_size;
229         long data_sz = dataSize + 8;
230         data = new unsigned char[data_sz];
231         memset(data, 0, data_sz);
232         xv_image->data = (char *) data;
233         w = xv_image->width;
234         h = xv_image->height;
235         if( color_model == BC_YUV422 || color_model == BC_UVY422 ) {
236                 int bytesPerLine = w*2;
237                 row_data = new unsigned char*[h];
238                 for( int i=0; i<h; ++i )
239                         row_data[i] = &data[i*bytesPerLine];
240         }
241 }
242
243 BC_XvImage::~BC_XvImage()
244 {
245         XFree(xv_image);
246 }
247 #endif
248
249
250 BC_XImage::BC_XImage(BC_Bitmap *bitmap, int index,
251         int w, int h, int color_model)
252  : BC_BitmapImage(bitmap, index)
253 {
254         Display *display = top_level->display;
255         Visual *visual = top_level->vis;
256         int default_depth = bitmap->get_default_depth();
257         ximage = XCreateImage(display, visual, default_depth,
258                 default_depth == 1 ? XYBitmap : ZPixmap,
259                 0, (char*)data, w, h, 8, 0);
260         bytesPerLine = ximage->bytes_per_line;
261         bitsPerPixel = ximage->bits_per_pixel;
262         dataSize = h * bytesPerLine;
263         long data_sz = dataSize + 8;
264         data = new unsigned char[data_sz];
265         memset(data, 0, data_sz);
266         ximage->data = (char*) data;
267         row_data = new unsigned char*[h];
268         for( int i=0; i<h; ++i )
269                 row_data[i] = &data[i*bytesPerLine];
270 }
271
272 BC_XImage::~BC_XImage()
273 {
274         delete [] data;
275         data = 0;
276         ximage->data = 0;
277         XDestroyImage(ximage);
278 }
279
280
281 BC_Bitmap::BC_Bitmap(BC_WindowBase *parent_window, unsigned char *png_data, double scale)
282 {
283 // Decompress data into a temporary vframe
284         VFramePng frame(png_data, scale);
285
286         avail_lock = 0;
287 // Initialize the bitmap
288         initialize(parent_window,
289                 frame.get_w(),
290                 frame.get_h(),
291                 parent_window->get_color_model(),
292                 0);
293
294 // Copy the vframe to the bitmap
295         read_frame(&frame, 0, 0, w, h);
296 }
297
298 BC_Bitmap::BC_Bitmap(BC_WindowBase *parent_window, VFrame *frame)
299 {
300         avail_lock = 0;
301 // Initialize the bitmap
302         initialize(parent_window,
303                 frame->get_w(),
304                 frame->get_h(),
305                 parent_window->get_color_model(),
306                 0);
307
308 // Copy the vframe to the bitmap
309         read_frame(frame, 0, 0, w, h);
310 }
311
312 BC_Bitmap::BC_Bitmap(BC_WindowBase *parent_window,
313         int w, int h, int color_model, int use_shm)
314 {
315         avail_lock = 0;
316         initialize(parent_window, w, h, color_model, use_shm);
317 }
318
319 BC_Bitmap::~BC_Bitmap()
320 {
321         delete_data();
322         delete avail_lock;
323 }
324
325 int BC_Bitmap::initialize(BC_WindowBase *parent_window,
326         int w, int h, int color_model, int use_shm)
327 {
328         BC_Resources *resources = parent_window->get_resources();
329         this->parent_window = parent_window;
330         this->top_level = parent_window->top_level;
331         this->xv_portid = resources->use_xvideo ? top_level->xvideo_port_id : -1;
332         if( w < 1 ) w = 1;
333         if( h < 1 ) h = 1;
334         this->w = w;
335         this->h = h;
336         this->color_model = color_model;
337         this->use_shm = !use_shm ? 0 : need_shm();
338         this->shm_reply = this->use_shm && resources->shm_reply ? 1 : 0;
339         // dont use shm for less than one page
340         if( !this->avail_lock )
341                 this->avail_lock = new Mutex("BC_Bitmap::avail_lock");
342         else
343                 this->avail_lock->reset();
344         this->buffers = 0;
345
346         this->active_bfr = 0;
347         this->buffer_count = 0;
348         this->max_buffer_count = 0;
349         this->active_buffers = 0;
350         allocate_data();
351         return 0;
352 }
353
354 int BC_Bitmap::match_params(int w, int h, int color_model, int use_shm)
355 {
356         if( use_shm ) use_shm = need_shm();
357         if(this->w /* != */ < w || this->h /* != */ < h ||
358            this->color_model != color_model || this->use_shm != use_shm) {
359                 delete_data();
360                 initialize(parent_window, w, h, color_model, use_shm);
361         }
362
363         return 0;
364 }
365
366 int BC_Bitmap::params_match(int w, int h, int color_model, int use_shm)
367 {
368         int result = 0;
369         if( use_shm ) use_shm = need_shm();
370         if(this->w == w && this->h == h && this->color_model == color_model) {
371                 if(use_shm == this->use_shm || use_shm == BC_INFINITY)
372                         result = 1;
373         }
374         return result;
375 }
376
377 BC_BitmapImage *BC_Bitmap::new_buffer(int type, int idx)
378 {
379         BC_BitmapImage *buffer = 0;
380         if( idx < 0 ) {
381                 if( type == bmXShmImage ) type = bmXImage;
382 #ifdef HAVE_XV
383                 else if( type ==  bmXvShmImage ) type = bmXvImage;
384 #endif
385         }
386         switch( type ) {
387         default:
388         case bmXImage:
389                 buffer = new BC_XImage(this, idx, w, h, color_model);
390                 break;
391 #ifdef HAVE_XV
392         case bmXvImage:
393                 buffer = new BC_XvImage(this, idx, w, h, color_model);
394                 break;
395         case bmXShmImage:
396                 buffer = new BC_XShmImage(this, idx, w, h, color_model);
397                 break;
398         case bmXvShmImage:
399                 buffer = new BC_XvShmImage(this, idx, w, h, color_model);
400                 break;
401 #endif
402         }
403         if( buffer->is_zombie() ) ++zombies;
404         return buffer;
405 }
406
407 void BC_Bitmap::
408 update_buffers(int count, int lock_avail)
409 {
410         int i;
411         // can deadlock in XReply (libxcb bug) without this lock
412         //top_level->lock_window("BC_Bitmap::update_buffers");
413         if( lock_avail ) avail_lock->lock("BC_Bitmap::update_buffers");
414         if( count > max_buffer_count ) count = max_buffer_count;
415         BC_BitmapImage **new_buffers = !count ? 0 : new BC_BitmapImage *[count];
416         if( buffer_count < count ) {
417                 for( i=0; i<buffer_count; ++i )
418                         new_buffers[i] = buffers[i];
419                 while( i < count ) {
420                         BC_BitmapImage *buffer = new_buffer(type, i);
421                         new_buffers[i] = buffer;
422                         avail.append(buffer);
423                         ++i;
424                 }
425         }
426         else {
427                 for( i=0; i<count; ++i )
428                         new_buffers[i] = buffers[i];
429                 while( i < buffer_count ) {
430                         BC_BitmapImage *buffer = buffers[i];
431                         if( buffer == active_bfr ) active_bfr = 0;
432                         if( buffer->is_avail() ) {
433                                 delete buffer;
434                         }
435                         else {
436                                 ++zombies;
437                                 buffer->index = -1;
438                         }
439                         ++i;
440                 }
441         }
442         delete [] buffers;
443         buffers = new_buffers;
444         buffer_count = count;
445         XFlush(top_level->display);
446         if( lock_avail ) avail_lock->unlock();
447         //top_level->unlock_window();
448 }
449
450 int BC_Bitmap::allocate_data()
451 {
452         int count = 1;
453         max_buffer_count = MAX_BITMAP_BUFFERS;
454         if(use_shm) { // Use shared memory.
455                 int bsz = best_buffer_size();
456                 if( bsz >= 0x800000 ) max_buffer_count = 2;
457                 else if( bsz >= 0x400000 ) max_buffer_count /= 8;
458                 else if( bsz >= 0x100000 ) max_buffer_count /= 4;
459                 else if( bsz >= 0x10000 ) max_buffer_count /= 2;
460                 type =
461 #ifdef HAVE_XV
462                         hardware_scaling() ? bmXvShmImage :
463 #endif
464                         bmXShmImage;
465                 count = MIN_BITMAP_BUFFERS;
466         }
467         else // use unshared memory.
468                 type =
469 #ifdef HAVE_XV
470                         hardware_scaling() ? bmXvImage :
471 #endif
472                         bmXImage;
473         update_buffers(count);
474         return 0;
475 }
476
477 int BC_Bitmap::delete_data()
478 {
479 //printf("BC_Bitmap::delete_data 1\n");
480         update_buffers(0);
481         active_bfr = 0;
482         buffer_count = 0;
483         max_buffer_count = 0;
484         return 0;
485 }
486
487 int BC_Bitmap::get_default_depth()
488 {
489         return color_model == BC_TRANSPARENCY ? 1 : top_level->default_depth;
490 }
491
492 long BC_Bitmap::best_buffer_size()
493 {
494         long pixelsize = BC_CModels::calculate_pixelsize(get_color_model());
495         return pixelsize * w * h;
496 }
497
498 int BC_Bitmap::need_shm()
499 {
500 // dont use shm for less than one page
501         return best_buffer_size() < 0x1000 ? 0 :
502                 parent_window->get_resources()->use_shm > 0 ? 1 : 0;
503 }
504
505 int BC_Bitmap::invert()
506 {
507         for( int j=0; j<buffer_count; ++j ) {
508                 unsigned char **rows = buffers[j]->get_row_data();
509                 if( !rows ) continue;
510                 long bytesPerLine = buffers[j]->bytes_per_line();
511                 for( int k=0; k<h; ++k ) {
512                         unsigned char *bp = rows[k];
513                         for( int i=0; i<bytesPerLine; ++i )
514                                 *bp++ ^= 0xff;
515                 }
516         }
517         return 0;
518 }
519
520 int BC_XShmImage::get_shm_size()
521 {
522         return bytesPerLine * bitmap->h;
523 }
524
525 int BC_Bitmap::write_drawable(Drawable &pixmap, GC &gc,
526         int dest_x, int dest_y, int source_x, int source_y,
527         int dest_w, int dest_h, int dont_wait)
528 {
529         return write_drawable(pixmap, gc,
530                 source_x, source_y, get_w() - source_x, get_h() - source_y,
531                 dest_x, dest_y, dest_w, dest_h, dont_wait);
532 }
533
534 int BC_XImage::write_drawable(Drawable &pixmap, GC &gc,
535         int source_x, int source_y, int source_w, int source_h,
536         int dest_x, int dest_y, int dest_w, int dest_h)
537 {
538         XPutImage(top_level->display, pixmap, gc,
539                 ximage, source_x, source_y,
540                 dest_x, dest_y, dest_w, dest_h);
541         return 0;
542 }
543
544 int BC_XShmImage::write_drawable(Drawable &pixmap, GC &gc,
545         int source_x, int source_y, int source_w, int source_h,
546         int dest_x, int dest_y, int dest_w, int dest_h)
547 {
548         XShmPutImage(top_level->display, pixmap, gc,
549                 ximage, source_x, source_y,
550                 dest_x, dest_y, dest_w, dest_h, bitmap->shm_reply);
551         return 0;
552 }
553
554 #ifdef HAVE_XV
555 int BC_XvImage::write_drawable(Drawable &pixmap, GC &gc,
556         int source_x, int source_y, int source_w, int source_h,
557         int dest_x, int dest_y, int dest_w, int dest_h)
558 {
559         XvPutImage(top_level->display,
560                 bitmap->xv_portid, pixmap, gc, xv_image,
561                 source_x, source_y, source_w, source_h,
562                 dest_x, dest_y, dest_w, dest_h);
563         return 0;
564 }
565
566 int BC_XvShmImage::write_drawable(Drawable &pixmap, GC &gc,
567         int source_x, int source_y, int source_w, int source_h,
568         int dest_x, int dest_y, int dest_w, int dest_h)
569 {
570         XvShmPutImage(top_level->display,
571                 bitmap->xv_portid, pixmap, gc, xv_image,
572                 source_x, source_y, source_w, source_h,
573                 dest_x, dest_y, dest_w, dest_h, bitmap->shm_reply);
574         return 0;
575 }
576 #endif
577
578 int BC_Bitmap::write_drawable(Drawable &pixmap, GC &gc,
579                 int source_x, int source_y, int source_w, int source_h,
580                 int dest_x, int dest_y, int dest_w, int dest_h,
581                 int dont_wait)
582 {
583 //printf("BC_Bitmap::write_drawable 1 %p %d\n", this, current_ringbuffer); fflush(stdout);
584         //if( dont_wait ) XSync(top_level->display, False);
585         BC_BitmapImage *bfr = cur_bfr();
586         if( !bfr->is_zombie() && is_shared() && shm_reply ) {
587 //printf("activate %p %08lx\n",bfr,bfr->get_shmseg());
588                 top_level->active_bitmaps.insert(bfr, pixmap);
589                 if( ++active_buffers > max_active_buffers )
590                         max_active_buffers = active_buffers;
591         }
592         bfr->write_drawable(pixmap, gc,
593                 source_x, source_y, source_w, source_h,
594                 dest_x, dest_y, dest_w, dest_h);
595         XFlush(top_level->display);
596         avail_lock->lock(" BC_Bitmap::write_drawable");
597         if( bfr->is_zombie() ) {
598                 delete bfr;
599                 --zombies;
600         }
601         else if( is_unshared() || !shm_reply )
602                 avail.append(bfr);
603         active_bfr = 0;
604         avail_lock->unlock();
605         if( !dont_wait && !shm_reply )
606                 XSync(top_level->display, False);
607         return 0;
608 }
609
610
611 int BC_XImage::read_drawable(Drawable &pixmap, int source_x, int source_y)
612 {
613         XGetSubImage(top_level->display, pixmap,
614                 source_x, source_y, bitmap->w, bitmap->h,
615                 0xffffffff, ZPixmap, ximage, 0, 0);
616         return 0;
617 }
618
619 int BC_XShmImage::read_drawable(Drawable &pixmap, int source_x, int source_y)
620 {
621         XShmGetImage(top_level->display, pixmap,
622                 ximage, source_x, source_y, 0xffffffff);
623         return 0;
624 }
625
626 int BC_Bitmap::read_drawable(Drawable &pixmap, int source_x, int source_y, VFrame *frame)
627 {
628         BC_BitmapImage *bfr = cur_bfr();
629         int result = bfr->read_drawable(pixmap, source_x, source_y);
630         if( !result && frame ) result = bfr->read_frame_rgb(frame);
631         return result;
632 }
633
634
635 // ============================ Decoding VFrames
636
637 int BC_Bitmap::read_frame(VFrame *frame, int x1, int y1, int x2, int y2)
638 {
639         return read_frame(frame, x1, y1, x2, y2, parent_window->get_bg_color());
640 }
641
642 int BC_Bitmap::read_frame(VFrame *frame, int x1, int y1, int x2, int y2, int bg_color)
643 {
644         return read_frame(frame,
645                 0, 0, frame->get_w(), frame->get_h(),
646                 x1, y1, x2 - x1, y2 - y1, bg_color);
647 }
648
649
650 int BC_Bitmap::read_frame(VFrame *frame,
651         int in_x, int in_y, int in_w, int in_h,
652         int out_x, int out_y, int out_w, int out_h, int bg_color)
653 {
654         BC_BitmapImage *bfr = cur_bfr();
655         if( hardware_scaling() && frame->get_color_model() == color_model ) {
656 // Hardware accelerated bitmap
657                 switch(color_model) {
658                 case BC_YUV420P:
659                         memcpy(bfr->get_y_data(), frame->get_y(), w * h);
660                         memcpy(bfr->get_u_data(), frame->get_u(), w * h / 4);
661                         memcpy(bfr->get_v_data(), frame->get_v(), w * h / 4);
662                         break;
663                 case BC_YUV422P:
664                         memcpy(bfr->get_y_data(), frame->get_y(), w * h);
665                         memcpy(bfr->get_u_data(), frame->get_u(), w * h / 2);
666                         memcpy(bfr->get_v_data(), frame->get_v(), w * h / 2);
667                         break;
668                 default:
669                 case BC_YUV422:
670                 case BC_UVY422:
671                         memcpy(get_data(), frame->get_data(), w * h + w * h);
672                         break;
673                 }
674         }
675         else {
676 // Software only
677
678 //printf("BC_Bitmap::read_frame %d -> %d %d %d %d %d -> %d %d %d %d\n",
679 //  frame->get_color_model(), color_model,
680 //  in_x, in_y, in_w, in_h, out_x, out_y, out_w, out_h);
681 //if(color_model == 6 && frame->get_color_model() == 19)
682 //printf("BC_Bitmap::read_frame 1 %d %d %d %d\n", frame->get_w(), frame->get_h(), get_w(), get_h());
683                 BC_CModels::transfer(
684                         bfr->get_row_data(), frame->get_rows(),
685                         bfr->get_y_data(), bfr->get_u_data(), bfr->get_v_data(),
686                         frame->get_y(), frame->get_u(), frame->get_v(),
687                         in_x, in_y, in_w, in_h,
688                         out_x, out_y, out_w, out_h,
689                         frame->get_color_model(), color_model,
690                         bg_color, frame->get_w(), w);
691
692                 if( color_model == BC_TRANSPARENCY && !top_level->server_byte_order )
693                         transparency_bitswap(frame->get_data(), w, h);
694 //if(color_model == 6 && frame->get_color_model() == 19)
695 //printf("BC_Bitmap::read_frame 2\n");
696         }
697
698         return 0;
699 }
700
701 uint8_t BC_Bitmap::bitswap[256]={
702   0x00,0x80,0x40,0xc0,0x20,0xa0,0x60,0xe0,0x10,0x90,0x50,0xd0,0x30,0xb0,0x70,0xf0,
703   0x08,0x88,0x48,0xc8,0x28,0xa8,0x68,0xe8,0x18,0x98,0x58,0xd8,0x38,0xb8,0x78,0xf8,
704   0x04,0x84,0x44,0xc4,0x24,0xa4,0x64,0xe4,0x14,0x94,0x54,0xd4,0x34,0xb4,0x74,0xf4,
705   0x0c,0x8c,0x4c,0xcc,0x2c,0xac,0x6c,0xec,0x1c,0x9c,0x5c,0xdc,0x3c,0xbc,0x7c,0xfc,
706   0x02,0x82,0x42,0xc2,0x22,0xa2,0x62,0xe2,0x12,0x92,0x52,0xd2,0x32,0xb2,0x72,0xf2,
707   0x0a,0x8a,0x4a,0xca,0x2a,0xaa,0x6a,0xea,0x1a,0x9a,0x5a,0xda,0x3a,0xba,0x7a,0xfa,
708   0x06,0x86,0x46,0xc6,0x26,0xa6,0x66,0xe6,0x16,0x96,0x56,0xd6,0x36,0xb6,0x76,0xf6,
709   0x0e,0x8e,0x4e,0xce,0x2e,0xae,0x6e,0xee,0x1e,0x9e,0x5e,0xde,0x3e,0xbe,0x7e,0xfe,
710   0x01,0x81,0x41,0xc1,0x21,0xa1,0x61,0xe1,0x11,0x91,0x51,0xd1,0x31,0xb1,0x71,0xf1,
711   0x09,0x89,0x49,0xc9,0x29,0xa9,0x69,0xe9,0x19,0x99,0x59,0xd9,0x39,0xb9,0x79,0xf9,
712   0x05,0x85,0x45,0xc5,0x25,0xa5,0x65,0xe5,0x15,0x95,0x55,0xd5,0x35,0xb5,0x75,0xf5,
713   0x0d,0x8d,0x4d,0xcd,0x2d,0xad,0x6d,0xed,0x1d,0x9d,0x5d,0xdd,0x3d,0xbd,0x7d,0xfd,
714   0x03,0x83,0x43,0xc3,0x23,0xa3,0x63,0xe3,0x13,0x93,0x53,0xd3,0x33,0xb3,0x73,0xf3,
715   0x0b,0x8b,0x4b,0xcb,0x2b,0xab,0x6b,0xeb,0x1b,0x9b,0x5b,0xdb,0x3b,0xbb,0x7b,0xfb,
716   0x07,0x87,0x47,0xc7,0x27,0xa7,0x67,0xe7,0x17,0x97,0x57,0xd7,0x37,0xb7,0x77,0xf7,
717   0x0f,0x8f,0x4f,0xcf,0x2f,0xaf,0x6f,0xef,0x1f,0x9f,0x5f,0xdf,0x3f,0xbf,0x7f,0xff,
718 };
719
720 void BC_Bitmap::transparency_bitswap(uint8_t *buf, int w, int h)
721 {
722         w = ((w-1) | 7) + 1;
723         int len = w * h / 8;
724         int i;
725         for(i=0 ; i+8<=len ; i+=8){
726                 buf[i+0] = bitswap[buf[i+0]];
727                 buf[i+1] = bitswap[buf[i+1]];
728                 buf[i+2] = bitswap[buf[i+2]];
729                 buf[i+3] = bitswap[buf[i+3]];
730                 buf[i+4] = bitswap[buf[i+4]];
731                 buf[i+5] = bitswap[buf[i+5]];
732                 buf[i+6] = bitswap[buf[i+6]];
733                 buf[i+7] = bitswap[buf[i+7]];
734         }
735         for( ; i<len ; i++)
736                 buf[i+0] = bitswap[buf[i+0]];
737 }
738