File: thread_iso.c

package info (click to toggle)
coriander 1.0.1-3.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,376 kB
  • ctags: 943
  • sloc: ansic: 10,305; sh: 3,030; makefile: 131
file content (610 lines) | stat: -rw-r--r-- 20,888 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
 * Copyright (C) 2000-2004 Damien Douxchamps  <ddouxchamps@users.sf.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "coriander.h"

gint IsoStartThread(camera_t* cam)
{
  int maxspeed, port;
  //int channel, speed;
  chain_t* iso_service=NULL;
  isothread_info_t *info=NULL;

  iso_service=GetService(cam, SERVICE_ISO);

  if (iso_service==NULL) { // if no ISO service running...
    iso_service=(chain_t*)malloc(sizeof(chain_t));
    iso_service->current_buffer=NULL;
    iso_service->next_buffer=NULL;
    iso_service->data=(void*)malloc(sizeof(isothread_info_t));
    pthread_mutex_init(&iso_service->mutex_struct, NULL);
    pthread_mutex_init(&iso_service->mutex_data, NULL);
    
    info=(isothread_info_t*)iso_service->data;
    
    /* currently FORMAT_STILL_IMAGE is not supported*/
    if (cam->misc_info.format == FORMAT_STILL_IMAGE) {
      FreeChain(iso_service);
      return(-1);
    }
    
    info->handle = NULL;
     
    // the iso receive handler gets its own raw1394 handle to free the controls
    port=dc1394_get_camera_port(cam->camera_info.handle);
    //fprintf(stderr,"port for this camera is %d\n",port);
    if ( (info->handle = dc1394_create_handle(port)) < 0) {
      FreeChain(iso_service);
      return(-1);
    }
    
    switch (cam->selfid.packetZero.phySpeed) {
    case 1: maxspeed=SPEED_200;break;
    case 2: maxspeed=SPEED_400;break;
    default: maxspeed=SPEED_100;break;
    }

    // copy params if we are the current camera
    if (cam==camera) {
      info->receive_method=cam->prefs.receive_method;
      strcpy(info->video1394_device, cam->prefs.video1394_device);
      info->capture.dma_device_file=info->video1394_device;
      info->video1394_dropframes=cam->prefs.video1394_dropframes;
      info->dma_buffer_size=cam->prefs.dma_buffer_size;
    }

    switch(info->receive_method) {
    case RECEIVE_METHOD_VIDEO1394:
      if (cam->misc_info.format!=FORMAT_SCALABLE_IMAGE_SIZE)
	if (dc1394_dma_setup_capture(cam->camera_info.handle, cam->camera_info.id, cam->misc_info.iso_channel, 
				     cam->misc_info.format, cam->misc_info.mode, maxspeed,
				     cam->misc_info.framerate, info->dma_buffer_size,
				     info->video1394_dropframes, 
				     info->capture.dma_device_file, &info->capture)
	    == DC1394_SUCCESS) {
	  info->receive_method=RECEIVE_METHOD_VIDEO1394;
	}
	else {
	  MainError("Failed to setup DMA capture with VIDEO1394");
	  dc1394_destroy_handle(info->handle);
	  FreeChain(iso_service);
	  return(-1);
	}
      else {
	if (dc1394_dma_setup_format7_capture(cam->camera_info.handle, cam->camera_info.id, cam->misc_info.iso_channel, 
					     cam->misc_info.mode, maxspeed, QUERY_FROM_CAMERA,
					     QUERY_FROM_CAMERA, QUERY_FROM_CAMERA,
					     QUERY_FROM_CAMERA, QUERY_FROM_CAMERA, 
					     info->dma_buffer_size,
					     info->video1394_dropframes, 
					     info->capture.dma_device_file, &info->capture)
	    == DC1394_SUCCESS) {
	  info->receive_method=RECEIVE_METHOD_VIDEO1394;
	}
	else {
	  MainError("Failed to setup Format_7 DMA capture with VIDEO1394");
	  dc1394_destroy_handle(info->handle);
	  FreeChain(iso_service);
	  return(-1);
	}
      }
      break;
    case RECEIVE_METHOD_RAW1394:
      if (cam->misc_info.format!=FORMAT_SCALABLE_IMAGE_SIZE)
	if (dc1394_setup_capture(cam->camera_info.handle, cam->camera_info.id, cam->misc_info.iso_channel, 
				 cam->misc_info.format, cam->misc_info.mode, maxspeed,
				 cam->misc_info.framerate, &info->capture)
	    == DC1394_SUCCESS) {
	  info->receive_method=RECEIVE_METHOD_RAW1394;
	}
	else {
	  MainError("Failed to setup capture with RAW1394");
	  dc1394_destroy_handle(info->handle);
	  FreeChain(iso_service);
	  return(-1);
	}
      else {
	if (dc1394_setup_format7_capture(cam->camera_info.handle, cam->camera_info.id, cam->misc_info.iso_channel, 
					 cam->misc_info.mode, maxspeed, QUERY_FROM_CAMERA,
					 QUERY_FROM_CAMERA, QUERY_FROM_CAMERA,
					 QUERY_FROM_CAMERA, QUERY_FROM_CAMERA,
					 &info->capture)
	    == DC1394_SUCCESS) {
	  info->receive_method=RECEIVE_METHOD_RAW1394;
	}
	else {
	  MainError("Failed to setup Format_7 capture with RAW1394");
	  dc1394_destroy_handle(info->handle);
	  FreeChain(iso_service);
		return(-1);
	}
      }
      break;
    }
    
    pthread_mutex_lock(&iso_service->mutex_data);
    CommonChainSetup(cam, iso_service, SERVICE_ISO);
    // init image buffers structs
    info->temp=NULL;
    info->temp_size=0;
    info->temp_allocated=0;

    pthread_mutex_lock(&iso_service->mutex_struct);
    InsertChain(cam,iso_service);

    //iso_service->timeout_func_id=-1;
    if (pthread_create(&iso_service->thread, NULL, IsoThread,(void*) iso_service)) {
      RemoveChain(cam, iso_service);
      pthread_mutex_unlock(&iso_service->mutex_struct);
      pthread_mutex_unlock(&iso_service->mutex_data);
      FreeChain(iso_service);
      return(-1);
    }
    else {
      pthread_mutex_unlock(&iso_service->mutex_struct);
      pthread_mutex_unlock(&iso_service->mutex_data);
    }
  }

  return (1);
}

void*
IsoCleanupThread(void* arg) 
{
  chain_t* iso_service;
  isothread_info_t *info;

  iso_service=(chain_t*)arg;
  info=(isothread_info_t*)iso_service->data;

  if ((info->receive_method == RECEIVE_METHOD_VIDEO1394)) {
    //dc1394_dma_done_with_buffer(&info->capture);
    // this should be done at the condition it has not been executed before or
    // else we get an ioctl error from libdc1394. How to do this, I don't know...
    //fprintf(stderr,"dma done with buffer\n");
  }
  
  // clear timing info on GUI...
  pthread_mutex_unlock(&iso_service->mutex_data);
  
  return(NULL);

}
 
void*
IsoThread(void* arg)
{
  chain_t *iso_service;
  isothread_info_t *info;
  int dma_ok=DC1394_FAILURE;
  float tmp;
  // we should only use mutex_data in this function

  iso_service=(chain_t*)arg;

  pthread_mutex_lock(&iso_service->mutex_data);
  info=(isothread_info_t*)iso_service->data;
  pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
  pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
  pthread_mutex_unlock(&iso_service->mutex_data);

  // time inits:
  iso_service->prev_time = times(&iso_service->tms_buf);
  iso_service->fps_frames=0;
  iso_service->processed_frames=0;

  while (1) {
    pthread_testcancel();
    pthread_cleanup_push((void*)IsoCleanupThread, (void*)iso_service);
    
    if (info->receive_method == RECEIVE_METHOD_RAW1394)
      dc1394_single_capture(info->handle, &info->capture);
    else
      dma_ok=dc1394_dma_single_capture(&info->capture);
    
    gettimeofday(&info->rawtime, NULL);
    localtime_r(&info->rawtime.tv_sec, &(iso_service->current_buffer->captime));
    iso_service->current_buffer->captime_usec=info->rawtime.tv_usec;
    sprintf(iso_service->current_buffer->captime_string,"%04d%02d%02d-%02d%02d%02d-%03d",
	    iso_service->current_buffer->captime.tm_year+1900,
	    iso_service->current_buffer->captime.tm_mon+1,
	    iso_service->current_buffer->captime.tm_mday,
	    iso_service->current_buffer->captime.tm_hour,
	    iso_service->current_buffer->captime.tm_min,
	    iso_service->current_buffer->captime.tm_sec,
	    iso_service->current_buffer->captime_usec/1000);

    pthread_mutex_lock(&iso_service->mutex_data);
    
    // check current buffer status
    IsoThreadCheckParams(iso_service);
    
    // Stereo decoding
    switch (iso_service->current_buffer->stereo_decoding) {
    case STEREO_DECODING_INTERLACED:
      StereoDecode((unsigned char *)info->capture.capture_buffer,info->temp,
		   info->orig_sizex*info->orig_sizey*2);
      break;
    case STEREO_DECODING_FIELD:
      memcpy(info->temp,(unsigned char *)info->capture.capture_buffer,info->orig_sizex*info->orig_sizey*2);
      break;
    case NO_STEREO_DECODING:
      if ((iso_service->current_buffer->bayer!=NO_BAYER_DECODING)&&(info->cond16bit!=0)) {
	y162y((unsigned char *)info->capture.capture_buffer,info->temp,
	      info->orig_sizex*info->orig_sizey, iso_service->current_buffer->bpp);
      }
      else {
	// it is necessary to put this here and not in the thread init or IsoThreadCheckParams function because
	// the buffer might change at every capture (typically when capture is too slow and buffering is performed)
	info->temp=(unsigned char*)info->capture.capture_buffer;
      }
      break;
    }

    // Bayer decoding
    switch (iso_service->current_buffer->bayer) {
    case BAYER_DECODING_NEAREST:
      BayerNearestNeighbor(info->temp, iso_service->current_buffer->image,
			   iso_service->current_buffer->width, iso_service->current_buffer->height, iso_service->current_buffer->bayer_pattern);
      break;
    case BAYER_DECODING_EDGE_SENSE:
      BayerEdgeSense(info->temp, iso_service->current_buffer->image,
		     iso_service->current_buffer->width, iso_service->current_buffer->height, iso_service->current_buffer->bayer_pattern);
      break;
    case BAYER_DECODING_SIMPLE:
      BayerSimple(info->temp, iso_service->current_buffer->image,
		  iso_service->current_buffer->width, iso_service->current_buffer->height, iso_service->current_buffer->bayer_pattern);
      break;
    case BAYER_DECODING_DOWNSAMPLE:
      BayerDownsample(info->temp, iso_service->current_buffer->image,
		      iso_service->current_buffer->width, iso_service->current_buffer->height, iso_service->current_buffer->bayer_pattern);
      break;
    case NO_BAYER_DECODING:
      // this is only necessary if no stereo was performed
      if (iso_service->current_buffer->stereo_decoding==NO_STEREO_DECODING) {
	memcpy(iso_service->current_buffer->image, info->temp,
	       iso_service->current_buffer->bytes_per_frame);
      }
      break;
    }
    
    // FPS computation:
    iso_service->current_time=times(&iso_service->tms_buf);
    iso_service->fps_frames++;
    iso_service->processed_frames++;
    
    tmp=(float)(iso_service->current_time-iso_service->prev_time)/sysconf(_SC_CLK_TCK);
    if (tmp==0)
      iso_service->fps=fabs(0.0);
    else
      iso_service->fps=fabs((float)iso_service->fps_frames/tmp);

    if ((info->receive_method == RECEIVE_METHOD_VIDEO1394)&&(dma_ok==DC1394_SUCCESS))
      dc1394_dma_done_with_buffer(&info->capture);
    
    pthread_mutex_unlock(&iso_service->mutex_data);
    
    pthread_mutex_lock(&iso_service->mutex_data);
    RollBuffers(iso_service);
    pthread_mutex_unlock(&iso_service->mutex_data);
    pthread_cleanup_pop(0);
  }

}


gint IsoStopThread(camera_t* cam)
{
  isothread_info_t *info;
  chain_t *iso_service;
  iso_service=GetService(cam,SERVICE_ISO);  

  if (iso_service!=NULL) { // if ISO service running...
    info=(isothread_info_t*)iso_service->data;
    pthread_cancel(iso_service->thread);
    pthread_join(iso_service->thread, NULL);
    pthread_mutex_lock(&iso_service->mutex_data);
    pthread_mutex_lock(&iso_service->mutex_struct);

    RemoveChain(cam,iso_service);
    
    if ((info->temp!=NULL)&&(info->temp_allocated>0)) {
      free(info->temp);
      info->temp=NULL;
      info->temp_allocated=0;
      info->temp_size=0;
    }
    
    if (info->receive_method == RECEIVE_METHOD_VIDEO1394) {
      dc1394_dma_unlisten(cam->camera_info.handle, &info->capture);
      dc1394_dma_release_camera(info->handle, &info->capture);
    }
    else 
      dc1394_release_camera(cam->camera_info.handle, &info->capture);
    
    dc1394_destroy_handle(info->handle);
    info->handle = NULL;
    
    pthread_mutex_unlock(&iso_service->mutex_struct);
    pthread_mutex_unlock(&iso_service->mutex_data);
    
    FreeChain(iso_service);
    
  }
  
  return (1);
}


void
IsoThreadCheckParams(chain_t *iso_service)
{

  isothread_info_t *info;
  info=(isothread_info_t*)iso_service->data;
  // copy harmless parameters anyway:
  pthread_mutex_lock(&iso_service->camera->uimutex);

  iso_service->current_buffer->bpp=iso_service->camera->bpp;
  iso_service->current_buffer->bayer_pattern=iso_service->camera->bayer_pattern;
  iso_service->current_buffer->width=info->capture.frame_width;
  iso_service->current_buffer->height=info->capture.frame_height;
  iso_service->current_buffer->bytes_per_frame=info->capture.quadlets_per_frame*4;
  iso_service->current_buffer->mode=iso_service->camera->misc_info.mode;
  iso_service->current_buffer->format=iso_service->camera->misc_info.format;
  iso_service->current_buffer->format7_color_mode=iso_service->camera->format7_info.mode[iso_service->camera->misc_info.mode-MODE_FORMAT7_MIN].color_coding_id;
  iso_service->current_buffer->stereo_decoding=iso_service->camera->stereo;
  iso_service->current_buffer->bayer=iso_service->camera->bayer;
  info->orig_sizex=iso_service->current_buffer->width;
  info->orig_sizey=iso_service->current_buffer->height;

  if (iso_service->current_buffer->format!=FORMAT_SCALABLE_IMAGE_SIZE) {
    info->cond16bit=((iso_service->current_buffer->mode==MODE_640x480_MONO16)||
		     (iso_service->current_buffer->mode==MODE_800x600_MONO16)||
		     (iso_service->current_buffer->mode==MODE_1024x768_MONO16)||
		     (iso_service->current_buffer->mode==MODE_1280x960_MONO16)||
		     (iso_service->current_buffer->mode==MODE_1600x1200_MONO16));
  }
  else {
    // warning: little change, might have big effect
    info->cond16bit=(iso_service->camera->format7_info.mode[iso_service->current_buffer->mode-MODE_FORMAT7_MIN].color_coding_id==COLOR_FORMAT7_MONO16);
  }

  // the buffer sizes. If a size is not good, re-allocate.
  switch (iso_service->current_buffer->bayer) {
  case BAYER_DECODING_DOWNSAMPLE:
    switch (iso_service->current_buffer->stereo_decoding) {
    case STEREO_DECODING_FIELD:
    case STEREO_DECODING_INTERLACED:
      // height is the same (/2 downsampling, *2 stereo decoding)
      iso_service->current_buffer->width/=2;
      iso_service->current_buffer->bytes_per_frame=iso_service->current_buffer->width*iso_service->current_buffer->height*3; // RGB buffer -> *3
      AllocImageBuffer(iso_service);
      AllocTempBuffer(info->orig_sizey*info->orig_sizex*2*sizeof(unsigned char),info);
      break;
    case NO_STEREO_DECODING:
      iso_service->current_buffer->width/=2;
      iso_service->current_buffer->height/=2;
      iso_service->current_buffer->bytes_per_frame=iso_service->current_buffer->width*iso_service->current_buffer->height*3; // RGB buffer -> *3
      AllocImageBuffer(iso_service);
      if (info->cond16bit!=0)
	AllocTempBuffer(info->orig_sizey*info->orig_sizex*sizeof(unsigned char),info);
      else {
	AllocTempBuffer(0,info);
      }
      break;
    }
    break;
  case BAYER_DECODING_EDGE_SENSE:
  case BAYER_DECODING_NEAREST:
  case BAYER_DECODING_SIMPLE:
    switch (iso_service->current_buffer->stereo_decoding) {
    case STEREO_DECODING_FIELD:
    case STEREO_DECODING_INTERLACED:
      iso_service->current_buffer->height*=2;
      iso_service->current_buffer->bytes_per_frame=iso_service->current_buffer->width*iso_service->current_buffer->height*3; // RGB buffer -> *3
      AllocImageBuffer(iso_service);
      AllocTempBuffer(info->orig_sizey*info->orig_sizex*2*sizeof(unsigned char),info);
      break;
    case NO_STEREO_DECODING:
      iso_service->current_buffer->bytes_per_frame=iso_service->current_buffer->width*iso_service->current_buffer->height*3; // RGB buffer -> *3
      AllocImageBuffer(iso_service);
      if (info->cond16bit!=0)
	AllocTempBuffer(info->orig_sizey*info->orig_sizex*sizeof(unsigned char),info);
      else {
	AllocTempBuffer(0,info);
      }
      break;
    }
    break;
  case NO_BAYER_DECODING:
    switch (iso_service->current_buffer->stereo_decoding) {
    case STEREO_DECODING_FIELD:
    case STEREO_DECODING_INTERLACED:
      iso_service->current_buffer->height*=2;
      iso_service->current_buffer->bytes_per_frame=iso_service->current_buffer->width*iso_service->current_buffer->height; // GRAY output only (8bpp)
      AllocImageBuffer(iso_service);
      AllocTempBuffer(0,info);
      //fprintf(stderr,"info->temp linked to curent image buffer\n");
      // it is very important to allocate the image buffer before the following
      // assignation because the image allocation might change the buffer pointer! 
      info->temp=(unsigned char *)iso_service->current_buffer->image;
      break;
    case NO_STEREO_DECODING:
      AllocImageBuffer(iso_service);
      AllocTempBuffer(0,info);
      break;
    }
    break;
  }
  SetColorMode(iso_service->current_buffer);
  
  pthread_mutex_unlock(&iso_service->camera->uimutex);

}

void
AllocTempBuffer(long long unsigned int requested_size, isothread_info_t* info) 
{
  if (requested_size==0) {
    if (info->temp_allocated>0) {
      free(info->temp);
    }
    info->temp_allocated=0;
    info->temp_size=0;
    }
  else { // some allocated space is required
    if (requested_size!=info->temp_size) { // req and actual size don't not match
      if (info->temp_allocated>0) {
	free(info->temp);
      }
      info->temp=(unsigned char *)malloc(requested_size*sizeof(unsigned char));
      info->temp_allocated=1;
      info->temp_size=requested_size;
    }
  }
}

void
AllocImageBuffer(chain_t* iso_service) 
{
  // ============ Allocate standard buffer ===============
  if (iso_service->current_buffer->buffer_size!=iso_service->current_buffer->bytes_per_frame) {
    // (re)allocation is necessary
    if (iso_service->current_buffer->image!=NULL) {
      free(iso_service->current_buffer->image);
      iso_service->current_buffer->image=NULL;
    }
    iso_service->current_buffer->image=(unsigned char*)malloc(iso_service->current_buffer->bytes_per_frame*sizeof(unsigned char));
    iso_service->current_buffer->buffer_size=iso_service->current_buffer->bytes_per_frame;
  }
}

void
SetColorMode(buffer_t *buffer)
{
  float bpp=-1;

  if (buffer->bayer==NO_BAYER_DECODING) {
    switch(buffer->mode) {
    case MODE_160x120_YUV444:
      buffer->buffer_color_mode=COLOR_FORMAT7_YUV444;
      bpp=3;
      break;
    case MODE_320x240_YUV422:
    case MODE_640x480_YUV422:
    case MODE_800x600_YUV422:
    case MODE_1024x768_YUV422:
    case MODE_1280x960_YUV422:
    case MODE_1600x1200_YUV422:
      if (buffer->stereo_decoding!=NO_STEREO_DECODING) {
	buffer->buffer_color_mode=COLOR_FORMAT7_MONO8;
	bpp=1;
      }
      else {
	buffer->buffer_color_mode=COLOR_FORMAT7_YUV422;
	bpp=2;
      }
      break;
    case MODE_640x480_YUV411:
      buffer->buffer_color_mode=COLOR_FORMAT7_YUV411;
      bpp=1.5;
      break;
    case MODE_640x480_RGB:
    case MODE_800x600_RGB:
    case MODE_1024x768_RGB:
    case MODE_1280x960_RGB:
    case MODE_1600x1200_RGB:
      buffer->buffer_color_mode=COLOR_FORMAT7_RGB8;
      bpp=3;
      break;
    case MODE_640x480_MONO:
    case MODE_800x600_MONO:
    case MODE_1024x768_MONO:
    case MODE_1280x960_MONO:
    case MODE_1600x1200_MONO:
      buffer->buffer_color_mode=COLOR_FORMAT7_MONO8;
      bpp=1;
      break;
    case MODE_640x480_MONO16:
    case MODE_800x600_MONO16:
    case MODE_1024x768_MONO16:
    case MODE_1280x960_MONO16:
    case MODE_1600x1200_MONO16:
      if (buffer->stereo_decoding!=NO_STEREO_DECODING) {
	buffer->buffer_color_mode=COLOR_FORMAT7_MONO8;
	bpp=1;
      }
      else {
	buffer->buffer_color_mode=COLOR_FORMAT7_MONO16;
	bpp=2;
      }
      break;
    case MODE_FORMAT7_0:
    case MODE_FORMAT7_1:
    case MODE_FORMAT7_2:
    case MODE_FORMAT7_3:
    case MODE_FORMAT7_4:
    case MODE_FORMAT7_5:
    case MODE_FORMAT7_6:
    case MODE_FORMAT7_7:
      if ((buffer->format7_color_mode==COLOR_FORMAT7_MONO16)&&(buffer->stereo_decoding!=NO_STEREO_DECODING)) {
	buffer->buffer_color_mode=COLOR_FORMAT7_MONO8;
	bpp=1;
      }
      else {
	buffer->buffer_color_mode=buffer->format7_color_mode;
	switch (buffer->format7_color_mode) {
	case COLOR_FORMAT7_MONO8:
	  bpp=1;
	  break;
	case COLOR_FORMAT7_MONO16:
	  bpp=2;
	  break;
	case COLOR_FORMAT7_RGB8:
	  bpp=3;
	  break;
	case COLOR_FORMAT7_RGB16:
	  bpp=6;
	  break;
	case COLOR_FORMAT7_YUV444:
	  bpp=3;
	  break;
	case COLOR_FORMAT7_YUV422:
	  bpp=2;
	  break;
	case COLOR_FORMAT7_YUV411:
	  bpp=1.5;
	  break;
	}
      }
      break;
    }
  }
  else  { // we force RGB mode
    buffer->buffer_color_mode=COLOR_FORMAT7_RGB8;
    bpp=3;
  }

  if (bpp==-1)
    fprintf(stderr,"ERROR: BPP is -1!!\n");

  buffer->buffer_image_bytes=(int)(buffer->width*buffer->height*bpp);

}