Project

General

Profile

BitmapToolkit Scol plugin
private_impl.cpp
Go to the documentation of this file.
1/**********************************************************
2 Software developed by AVA ( Ava Group of the University of Cordoba, ava at uco dot es)
3 Main author Rafael Munoz Salinas (rmsalinas at uco dot es)
4 This software is released under BSD license as expressed below
5-------------------------------------------------------------------
6Copyright (c) 2013, AVA ( Ava Group University of Cordoba, ava at uco dot es)
7All rights reserved.
8
9Redistribution and use in source and binary forms, with or without
10modification, are permitted provided that the following conditions
11are met:
121. Redistributions of source code must retain the above copyright
13 notice, this list of conditions and the following disclaimer.
142. Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in the
16 documentation and/or other materials provided with the distribution.
173. All advertising materials mentioning features or use of this software
18 must display the following acknowledgement:
19
20 This product includes software developed by the Ava group of the University of Cordoba.
21
224. Neither the name of the University nor the names of its contributors
23 may be used to endorse or promote products derived from this software
24 without specific prior written permission.
25
26THIS SOFTWARE IS PROVIDED BY AVA ''AS IS'' AND ANY
27EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29DISCLAIMED. IN NO EVENT SHALL AVA BE LIABLE FOR ANY
30DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
33ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36****************************************************************/
37
39#include <iostream>
40#include <cstdio>
41#include "mmal/util/mmal_util.h"
42#include "mmal/util/mmal_util_params.h"
43#include "mmal/util/mmal_default_components.h"
44using namespace std;
45namespace raspicam {
46 namespace _private{
47#define MMAL_CAMERA_VIDEO_PORT 1
48#define MMAL_CAMERA_CAPTURE_PORT 2
49#define VIDEO_FRAME_RATE_DEN 1
50#define VIDEO_OUTPUT_BUFFERS_NUM 3
51
52
54 camera_video_port = NULL;
55// camera_still_port = NULL;
56 _isOpened=false;
57 _isCapturing=false;
58 //set default state params
59 setDefaultStateParams();
60 }
61
66
67 void Private_Impl::setDefaultStateParams() {
68
69 // Default everything to zero
70 memset ( &State, 0, sizeof ( RASPIVID_STATE ) );
71 State.framerate = 30;
72 State.width = 1280; // use a multiple of 320 (640, 1280)
73 State.height = 960; // use a multiple of 240 (480, 960)
74 State.sharpness = 0;
75 State.contrast = 0;
76 State.brightness = 50;
77 State.saturation = 0;
78 State.ISO = 400;
79 State.videoStabilisation = false;
80 State.exposureCompensation = 0;
86 State.colourEffects.enable = 0;
87 State.colourEffects.u = 128;
88 State.colourEffects.v = 128;
89 State.rotation = 0;
90 State.hflip = State.vflip = 0;
91 State.roi.x = State.roi.y = 0.0;
92 State.roi.w = State.roi.h = 1.0;
93 State.shutterSpeed=0;//auto
94 State.awbg_red=1.0;
95 State.awbg_blue=1.0;
96
97 }
98 bool Private_Impl::open ( bool StartCapture ) {
99 if ( _isOpened ) return false; //already opened
100// create camera
101 if ( ! create_camera_component ( &State ) ) {
102 cerr<<__func__<<" Failed to create camera component"<<__FILE__<<" "<<__LINE__<<endl;
103 return false;
104 }
105 commitParameters();
106 camera_video_port = State.camera_component->output[MMAL_CAMERA_VIDEO_PORT];
107 callback_data.pstate = &State;
108 // assign data to use for callback
109 camera_video_port->userdata = ( struct MMAL_PORT_USERDATA_T * ) &callback_data;
110
111 _isOpened=true;
112 if ( StartCapture ) return startCapture();
113 else return true;
114 }
118 if ( !_isOpened ) {
119 cerr<<__FILE__<<":"<<__LINE__<<":"<<__func__<<" not opened."<<endl;
120 return false; //already opened
121 }
122
123 // start capture
124 if ( mmal_port_parameter_set_boolean ( camera_video_port, MMAL_PARAMETER_CAPTURE, 1 ) != MMAL_SUCCESS ) {
125 release();
126 return false;
127 }
128 // Send all the buffers to the video port
129
130 int num = mmal_queue_length ( State.video_pool->queue );
131 int q;
132 for ( q=0; q<num; q++ ) {
133 MMAL_BUFFER_HEADER_T *buffer = mmal_queue_get ( State.video_pool->queue );
134
135 if ( !buffer )
136 cerr<<"Unable to get a required buffer"<<q<<" from pool queue"<<endl;
137
138 if ( mmal_port_send_buffer ( camera_video_port, buffer ) != MMAL_SUCCESS )
139 cerr<<"Unable to send a buffer to encoder output port "<< q<<endl;
140 }
141 _isCapturing=true;
142 return true;
143 }
144
146 if ( !_isOpened ) return;
147
148 // Disable camera_video_port
149 if ( camera_video_port && camera_video_port->is_enabled ) {
150 mmal_port_disable ( camera_video_port );
151 camera_video_port = NULL;
152 }
154 // Disable all our ports that are not handled by connections
155 if ( State.camera_component )
156 mmal_component_disable ( State.camera_component );
157
158
159 destroy_camera_component ( &State );
160
161 _isOpened=false;
162 _isCapturing=false;
163
164 }
169 if ( !isCapturing() ) return false;
170 callback_data.waitForFrame();
171 return true;
172 }
176 void Private_Impl::retrieve ( unsigned char *data,RASPICAM_FORMAT type ) {
177 if ( callback_data._buffData.size==0 ) return;
178 if ( type!=RASPICAM_FORMAT_IGNORE ) {
179 cerr<<__FILE__<<":"<<__LINE__<<" :Private_Impl::retrieve type is not RASPICAM_FORMAT_IGNORE as it should be"<<endl;
180 }
182 auto imagePtr=callback_data._buffData.data;
183 for(int i=0;i<State.height+State.height/2;i++) {
184 memcpy ( data,imagePtr,State.width);
185 data+=State.width;
186 imagePtr+=format->es->video.width;//line stride
187 }
188 }
189 else if( State.captureFtm ==RASPICAM_FORMAT_GRAY){
190 auto imagePtr=callback_data._buffData.data;
191 for(int i=0;i<State.height;i++) {
192 memcpy ( data,imagePtr,State.width);
193 data+=State.width;
194 imagePtr+=format->es->video.width;//line stride
195 }
196 }
198 auto imagePtr=callback_data._buffData.data;
199 for(int i=0;i<State.height;i++) {
200 memcpy ( data,imagePtr,State.width*3);
201 data+=State.width*3;
202 imagePtr+=format->es->video.width*3;//line stride
203 }
204 }
205
206 }
207
208
209 unsigned char *Private_Impl::getImageBufferData() const{
210 return callback_data._buffData.data;
211 }
212
214 return getImageTypeSize ( getFormat() );
215 }
216
217
224 switch ( type ) {
226 return getWidth() *getHeight() + 2* ( ( getWidth() /2 *getHeight() /2 ) );
227 break;
229 return getWidth() *getHeight();
230 break;
233 return 3*getWidth() *getHeight();
234 break;
235 default:
236 return 0;
237 };
238 }
239
240
241
248 void Private_Impl::destroy_camera_component ( RASPIVID_STATE *state ) {
249 if ( state->video_pool )
250 mmal_port_pool_destroy ( state->camera_component->output[MMAL_CAMERA_VIDEO_PORT], state->video_pool );
251 if ( state->camera_component ) {
252 mmal_component_destroy ( state->camera_component );
253 state->camera_component = NULL;
254 }
255 }
256 MMAL_COMPONENT_T *Private_Impl::create_camera_component ( RASPIVID_STATE *state ) {
257 MMAL_COMPONENT_T *camera = 0;
258 MMAL_PORT_T *video_port = NULL;
259
260 MMAL_STATUS_T status;
261 /* Create the component */
262 status = mmal_component_create ( MMAL_COMPONENT_DEFAULT_CAMERA, &camera );
263
264 if ( status != MMAL_SUCCESS ) {
265 cerr<< ( "Failed to create camera component" );
266 return 0;
267 }
268
269 if ( !camera->output_num ) {
270 cerr<< ( "Camera doesn't have output ports" );
271 mmal_component_destroy ( camera );
272 return 0;
273 }
274
275 video_port = camera->output[MMAL_CAMERA_VIDEO_PORT];
276
277 // set up the camera configuration
278
279 MMAL_PARAMETER_CAMERA_CONFIG_T cam_config;
280 cam_config.hdr.id=MMAL_PARAMETER_CAMERA_CONFIG;
281 cam_config.hdr.size=sizeof ( cam_config );
282 cam_config.max_stills_w = state->width;
283 cam_config.max_stills_h = state->height;
284 cam_config.stills_yuv422 = 0;
285 cam_config.one_shot_stills = 0;
286 cam_config.max_preview_video_w = state->width;
287 cam_config.max_preview_video_h = state->height;
288 cam_config.num_preview_video_frames = 3;
289 cam_config.stills_capture_circular_buffer_height = 0;
290 cam_config.fast_preview_resume = 0;
291 cam_config.use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RESET_STC;
292 mmal_port_parameter_set ( camera->control, &cam_config.hdr );
293
294 // Set the encode format on the video port
295
296 format = video_port->format;
297 format->encoding_variant = convertFormat ( State.captureFtm );
298 format->encoding = convertFormat ( State.captureFtm );
299 format->es->video.width = VCOS_ALIGN_UP(state->width, 32);
300 format->es->video.height = VCOS_ALIGN_UP(state->height, 16);
301 format->es->video.crop.x = 0;
302 format->es->video.crop.y = 0;
303 format->es->video.crop.width = state->width;
304 format->es->video.crop.height = state->height;
305 format->es->video.frame_rate.num = state->framerate;
306 format->es->video.frame_rate.den = VIDEO_FRAME_RATE_DEN;
307
308 status = mmal_port_format_commit ( video_port );
309 if ( status ) {
310 cerr<< ( "camera video format couldn't be set" );
311 mmal_component_destroy ( camera );
312 return 0;
313 }
314
315 // PR : plug the callback to the video port
316 status = mmal_port_enable ( video_port,video_buffer_callback );
317 if ( status ) {
318 cerr<< ( "camera video callback2 error" );
319 mmal_component_destroy ( camera );
320 return 0;
321 }
322
323 // Ensure there are enough buffers to avoid dropping frames
324 if ( video_port->buffer_num < VIDEO_OUTPUT_BUFFERS_NUM )
325 video_port->buffer_num = VIDEO_OUTPUT_BUFFERS_NUM;
326
327
328
329 //PR : create pool of message on video port
330 MMAL_POOL_T *pool;
331 video_port->buffer_size = video_port->buffer_size_recommended;
332 video_port->buffer_num = video_port->buffer_num_recommended;
333 pool = mmal_port_pool_create ( video_port, video_port->buffer_num, video_port->buffer_size );
334 if ( !pool ) {
335 cerr<< ( "Failed to create buffer header pool for video output port" );
336 }
337 state->video_pool = pool;
338
339
340 /* Enable component */
341 status = mmal_component_enable ( camera );
342
343 if ( status ) {
344 cerr<< ( "camera component couldn't be enabled" );
345 mmal_component_destroy ( camera );
346 return 0;
347 }
348
349 state->camera_component = camera;//this needs to be before set_all_parameters
350
351 return camera;
352 }
353
354
355 void Private_Impl::commitBrightness() {
356 mmal_port_parameter_set_rational ( State.camera_component->control, MMAL_PARAMETER_BRIGHTNESS, ( MMAL_RATIONAL_T ) {
357 State.brightness, 100
358 } );
359 }
360
361
362 void Private_Impl::commitRotation() {
363 int rotation = int ( State.rotation / 90 ) * 90;
364 mmal_port_parameter_set_int32 ( State.camera_component->output[0], MMAL_PARAMETER_ROTATION,rotation );
365 mmal_port_parameter_set_int32 ( State.camera_component->output[1], MMAL_PARAMETER_ROTATION,rotation );
366 mmal_port_parameter_set_int32 ( State.camera_component->output[2], MMAL_PARAMETER_ROTATION, rotation );
367 }
368
369 void Private_Impl::commitISO() {
370 if ( mmal_port_parameter_set_uint32 ( State.camera_component->control, MMAL_PARAMETER_ISO, State.ISO ) != MMAL_SUCCESS )
371 cout << __func__ << ": Failed to set ISO parameter.\n";
372 }
373
374 void Private_Impl::commitSharpness() {
375 if ( mmal_port_parameter_set_rational ( State.camera_component->control, MMAL_PARAMETER_SHARPNESS, ( MMAL_RATIONAL_T ) {
376 State.sharpness, 100
377 } ) != MMAL_SUCCESS )
378 cout << __func__ << ": Failed to set sharpness parameter.\n";
379 }
380
381 void Private_Impl::commitShutterSpeed() {
382 if ( mmal_port_parameter_set_uint32 ( State.camera_component->control, MMAL_PARAMETER_SHUTTER_SPEED, State.shutterSpeed ) != MMAL_SUCCESS )
383 cout << __func__ << ": Failed to set shutter parameter.\n";
384 }
385
386
387
388 void Private_Impl::commitContrast() {
389 if ( mmal_port_parameter_set_rational ( State.camera_component->control, MMAL_PARAMETER_CONTRAST, ( MMAL_RATIONAL_T ) {
390 State.contrast, 100
391 } ) != MMAL_SUCCESS )
392 cout << __func__ << ": Failed to set contrast parameter.\n";
393 }
394
395 void Private_Impl::commitSaturation() {
396 if ( mmal_port_parameter_set_rational ( State.camera_component->control, MMAL_PARAMETER_SATURATION, ( MMAL_RATIONAL_T ) {
397 State.saturation, 100
398 } ) != MMAL_SUCCESS )
399 cout << __func__ << ": Failed to set saturation parameter.\n";
400 }
401
402 void Private_Impl::commitExposure() {
403 MMAL_PARAMETER_EXPOSUREMODE_T exp_mode = {{MMAL_PARAMETER_EXPOSURE_MODE,sizeof ( exp_mode ) }, convertExposure ( State.rpc_exposureMode ) };
404 if ( mmal_port_parameter_set ( State.camera_component->control, &exp_mode.hdr ) != MMAL_SUCCESS )
405 cout << __func__ << ": Failed to set exposure parameter.\n";
406 }
413 void Private_Impl::commitExposureCompensation() {
414 if ( mmal_port_parameter_set_int32 ( State.camera_component->control, MMAL_PARAMETER_EXPOSURE_COMP , State.exposureCompensation ) !=MMAL_SUCCESS )
415 cout << __func__ << ": Failed to set Exposure Compensation parameter.\n";
416
417 }
418
419
420 void Private_Impl::commitAWB() {
421 MMAL_PARAMETER_AWBMODE_T param = {{MMAL_PARAMETER_AWB_MODE,sizeof ( param ) }, convertAWB ( State.rpc_awbMode ) };
422 if ( mmal_port_parameter_set ( State.camera_component->control, &param.hdr ) != MMAL_SUCCESS )
423 cout << __func__ << ": Failed to set AWB parameter.\n";
424 }
425
426 void Private_Impl::commitImageEffect() {
427 MMAL_PARAMETER_IMAGEFX_T imgFX = {{MMAL_PARAMETER_IMAGE_EFFECT,sizeof ( imgFX ) }, convertImageEffect ( State.rpc_imageEffect ) };
428 if ( mmal_port_parameter_set ( State.camera_component->control, &imgFX.hdr ) != MMAL_SUCCESS )
429 cout << __func__ << ": Failed to set image effect parameter.\n";
430 }
431
432 void Private_Impl::commitMetering() {
433 MMAL_PARAMETER_EXPOSUREMETERINGMODE_T meter_mode = {{MMAL_PARAMETER_EXP_METERING_MODE, sizeof ( meter_mode ) }, convertMetering ( State.rpc_exposureMeterMode ) };
434 if ( mmal_port_parameter_set ( State.camera_component->control, &meter_mode.hdr ) != MMAL_SUCCESS )
435 cout << __func__ << ": Failed to set metering parameter.\n";
436 }
437
438 void Private_Impl::commitFlips() {
439 MMAL_PARAMETER_MIRROR_T mirror = {{MMAL_PARAMETER_MIRROR, sizeof ( MMAL_PARAMETER_MIRROR_T ) }, MMAL_PARAM_MIRROR_NONE};
440 if ( State.hflip && State.vflip )
441 mirror.value = MMAL_PARAM_MIRROR_BOTH;
442 else if ( State.hflip )
443 mirror.value = MMAL_PARAM_MIRROR_HORIZONTAL;
444 else if ( State.vflip )
445 mirror.value = MMAL_PARAM_MIRROR_VERTICAL;
446 if ( mmal_port_parameter_set ( State.camera_component->output[0], &mirror.hdr ) != MMAL_SUCCESS ||
447 mmal_port_parameter_set ( State.camera_component->output[1], &mirror.hdr ) != MMAL_SUCCESS ||
448 mmal_port_parameter_set ( State.camera_component->output[2], &mirror.hdr ) )
449 cout << __func__ << ": Failed to set horizontal/vertical flip parameter.\n";
450 }
451
452
459 void Private_Impl::commitParameters ( ) {
460 assert ( State.camera_component!=0 );
461 commitSaturation();
462 commitSharpness();
463 commitContrast();
464 commitBrightness();
465 commitISO();
466 if ( State.shutterSpeed!=0 ) {
467 commitShutterSpeed();
469 commitExposure();
470 } else
471 commitExposure();
472 commitExposureCompensation();
473 commitMetering();
474 commitImageEffect();
475 commitRotation();
476 commitFlips();
477 commitVideoStabilization();
478 commitAWB();
479 commitAWB_RB();
480
481 }
482 void Private_Impl::commitVideoStabilization() {
483 // Set Video Stabilization
484 if ( mmal_port_parameter_set_boolean ( State.camera_component->control, MMAL_PARAMETER_VIDEO_STABILISATION, State.videoStabilisation ) != MMAL_SUCCESS )
485 cout << __func__ << ": Failed to set video stabilization parameter.\n";
486 }
487
488
489
490 void Private_Impl::video_buffer_callback ( MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer ) {
491 MMAL_BUFFER_HEADER_T *new_buffer;
492 PORT_USERDATA *pData = ( PORT_USERDATA * ) port->userdata;
493
494 bool hasGrabbed=false;
495// pData->_mutex.lock();
496 std::unique_lock<std::mutex> lck ( pData->_mutex );
497 if ( pData ) {
498 if ( pData->wantToGrab && buffer->length ) {
499 mmal_buffer_header_mem_lock ( buffer );
500 pData->_buffData.resize ( buffer->length );
501 memcpy ( pData->_buffData.data,buffer->data,buffer->length );
502 pData->wantToGrab =false;
503 hasGrabbed=true;
504 mmal_buffer_header_mem_unlock ( buffer );
505 }
506 }
507 //pData->_mutex.unlock();
508 // if ( hasGrabbed ) pData->Thcond.BroadCast(); //wake up waiting client
509 // release buffer back to the pool
510 mmal_buffer_header_release ( buffer );
511 // and send one back to the port (if still open)
512 if ( port->is_enabled ) {
513 MMAL_STATUS_T status;
514
515 new_buffer = mmal_queue_get ( pData->pstate->video_pool->queue );
516
517 if ( new_buffer )
518 status = mmal_port_send_buffer ( port, new_buffer );
519
520 if ( !new_buffer || status != MMAL_SUCCESS )
521 printf ( "Unable to return a buffer to the encoder port" );
522 }
523
524 if ( pData->pstate->shutterSpeed!=0 )
525 mmal_port_parameter_set_uint32 ( pData->pstate->camera_component->control, MMAL_PARAMETER_SHUTTER_SPEED, pData->pstate->shutterSpeed ) ;
526 if ( hasGrabbed ) pData->Thcond.BroadCast(); //wake up waiting client
527
528 }
529
530
531
532 void Private_Impl::setWidth ( unsigned int width ) {
533 State.width = width - (width % 320);
534 }
535
536 void Private_Impl::setHeight ( unsigned int height ) {
537 State.height = height - (height % 240);
538 }
540 if ( isOpened() ) {
541 cerr<<__FILE__<<":"<<__LINE__<<":"<<__func__<<": can not change format with camera already opened"<<endl;
542 return;
543 }
544 State.captureFtm = fmt;
545 }
546
547 void Private_Impl::setCaptureSize ( unsigned int width, unsigned int height ) {
548 setWidth ( width );
549 setHeight ( height );
550 }
551
553 State.videoStabilisation=v;
554 if ( isOpened() ) commitVideoStabilization();
555 }
556
557 void Private_Impl::setBrightness ( unsigned int brightness ) {
558 if ( brightness > 100 ) brightness = 100 ;
559 State.brightness = brightness;
560 if ( isOpened() ) commitBrightness();
561 }
562 void Private_Impl::setShutterSpeed ( unsigned int shutter ) {
563 if ( shutter > 330000 )
564 shutter = 330000;
565 State.shutterSpeed= shutter;
566 if ( isOpened() ) commitShutterSpeed();
567 }
568
569
570
571
572 void Private_Impl::setRotation ( int rotation ) {
573 while ( rotation < 0 )
574 rotation += 360;
575 if ( rotation >= 360 )
576 rotation = rotation % 360;
577 State.rotation = rotation;
578 if ( isOpened() ) commitRotation();
579 }
580
581 void Private_Impl::setISO ( int iso ) {
582 State.ISO = iso;
583 if ( isOpened() ) commitISO();
584 }
585
586 void Private_Impl::setSharpness ( int sharpness ) {
587 if ( sharpness < -100 ) sharpness = -100;
588 if ( sharpness > 100 ) sharpness = 100;
589 State.sharpness = sharpness;
590 if ( isOpened() ) commitSharpness();
591 }
592
593 void Private_Impl::setContrast ( int contrast ) {
594 if ( contrast < -100 ) contrast = -100;
595 if ( contrast > 100 ) contrast = 100;
596 State.contrast = contrast;
597 if ( isOpened() ) commitContrast();
598 }
599
600 void Private_Impl::setSaturation ( int saturation ) {
601 if ( saturation < -100 ) saturation = -100;
602 if ( saturation > 100 ) saturation = 100;
603 State.saturation = saturation;
604 if ( isOpened() ) commitSaturation();
605 }
606
607
608 void Private_Impl::setAWB_RB ( float red_g, float blue_g ) {
609 State.awbg_blue = blue_g;
610 State.awbg_red = red_g;
611 if ( isOpened() ) commitAWB_RB();
612 }
614 State.rpc_exposureMode = exposure;
615 if ( isOpened() ) commitExposure();
616 }
617
619 State.rpc_awbMode = awb;
620 if ( isOpened() ) commitAWB();
621 }
622
624 State.rpc_imageEffect = imageEffect;
625 if ( isOpened() ) commitImageEffect();
626 }
627
629 State.rpc_exposureMeterMode = metering;
630 if ( isOpened() ) commitMetering();
631 }
633 if ( val < -10 ) val= -10;
634 if ( val > 10 ) val = 10;
635 State.exposureCompensation=val;
636 if ( isOpened() ) commitExposureCompensation();
637 }
638
640 State.hflip = hFlip;
641 if ( isOpened() ) commitFlips();
642 }
643
644 void Private_Impl::setVerticalFlip ( bool vFlip ) {
645 State.vflip = vFlip;
646 if ( isOpened() ) commitFlips();
647 }
648
649
650 MMAL_PARAM_EXPOSUREMETERINGMODE_T Private_Impl::convertMetering ( RASPICAM_METERING metering ) {
651 switch ( metering ) {
653 return MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE;
655 return MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT;
657 return MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT;
659 return MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX;
660 default:
661 return MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE;
662 }
663 }
664 MMAL_PARAM_EXPOSUREMODE_T Private_Impl::convertExposure ( RASPICAM_EXPOSURE exposure ) {
665
666 switch ( exposure ) {
668 return MMAL_PARAM_EXPOSUREMODE_OFF;
670 return MMAL_PARAM_EXPOSUREMODE_AUTO;
672 return MMAL_PARAM_EXPOSUREMODE_NIGHT;
674 return MMAL_PARAM_EXPOSUREMODE_NIGHTPREVIEW;
676 return MMAL_PARAM_EXPOSUREMODE_BACKLIGHT;
678 return MMAL_PARAM_EXPOSUREMODE_SPOTLIGHT;
680 return MMAL_PARAM_EXPOSUREMODE_SPORTS;
682 return MMAL_PARAM_EXPOSUREMODE_SNOW;
684 return MMAL_PARAM_EXPOSUREMODE_BEACH;
686 return MMAL_PARAM_EXPOSUREMODE_VERYLONG;
688 return MMAL_PARAM_EXPOSUREMODE_FIXEDFPS;
690 return MMAL_PARAM_EXPOSUREMODE_ANTISHAKE;
692 return MMAL_PARAM_EXPOSUREMODE_FIREWORKS;
693 default:
694 return MMAL_PARAM_EXPOSUREMODE_AUTO;
695 }
696 }
697
698 MMAL_PARAM_AWBMODE_T Private_Impl::convertAWB ( RASPICAM_AWB awb ) {
699 switch ( awb ) {
700 case RASPICAM_AWB_OFF:
701 return MMAL_PARAM_AWBMODE_OFF;
703 return MMAL_PARAM_AWBMODE_AUTO;
705 return MMAL_PARAM_AWBMODE_SUNLIGHT;
707 return MMAL_PARAM_AWBMODE_CLOUDY;
709 return MMAL_PARAM_AWBMODE_SHADE;
711 return MMAL_PARAM_AWBMODE_TUNGSTEN;
713 return MMAL_PARAM_AWBMODE_FLUORESCENT;
715 return MMAL_PARAM_AWBMODE_INCANDESCENT;
717 return MMAL_PARAM_AWBMODE_FLASH;
719 return MMAL_PARAM_AWBMODE_HORIZON;
720 default:
721 return MMAL_PARAM_AWBMODE_AUTO;
722 }
723 }
724
725 MMAL_PARAM_IMAGEFX_T Private_Impl::convertImageEffect ( RASPICAM_IMAGE_EFFECT imageEffect ) {
726 switch ( imageEffect ) {
728 return MMAL_PARAM_IMAGEFX_NONE;
730 return MMAL_PARAM_IMAGEFX_NEGATIVE;
732 return MMAL_PARAM_IMAGEFX_SOLARIZE;
734 return MMAL_PARAM_IMAGEFX_SKETCH;
736 return MMAL_PARAM_IMAGEFX_DENOISE;
738 return MMAL_PARAM_IMAGEFX_EMBOSS;
740 return MMAL_PARAM_IMAGEFX_OILPAINT;
742 return MMAL_PARAM_IMAGEFX_HATCH;
744 return MMAL_PARAM_IMAGEFX_GPEN;
746 return MMAL_PARAM_IMAGEFX_PASTEL;
748 return MMAL_PARAM_IMAGEFX_WATERCOLOUR;
750 return MMAL_PARAM_IMAGEFX_FILM;
752 return MMAL_PARAM_IMAGEFX_BLUR;
754 return MMAL_PARAM_IMAGEFX_SATURATION;
756 return MMAL_PARAM_IMAGEFX_COLOURSWAP;
758 return MMAL_PARAM_IMAGEFX_WASHEDOUT;
760 return MMAL_PARAM_IMAGEFX_POSTERISE;
762 return MMAL_PARAM_IMAGEFX_COLOURPOINT;
764 return MMAL_PARAM_IMAGEFX_COLOURBALANCE;
766 return MMAL_PARAM_IMAGEFX_CARTOON;
767 default:
768 return MMAL_PARAM_IMAGEFX_NONE;
769 }
770 }
771
772 void Private_Impl::setFrameRate ( unsigned int frame_rate ) {
773 State.framerate = frame_rate;
774 }
775
776 int Private_Impl::convertFormat ( RASPICAM_FORMAT fmt ) {
777 switch ( fmt ) {
779 return MMAL_ENCODING_RGB24;
781 return MMAL_ENCODING_BGR24;
783 return MMAL_ENCODING_I420;
785 return MMAL_ENCODING_I420;
786 default:
787 return MMAL_ENCODING_I420;
788 }
789 }
790
791
792 //Returns an id of the camera. We assume the camera id is the one of the raspberry
793 //the id is obtained using raspberry serial number obtained in /proc/cpuinfo
794 string Private_Impl::getId() const{
795 char serial[1024];
796 serial[0]='\0';
797 ifstream file ( "/proc/cpuinfo" );
798 if ( !file ) {
799 cerr<<__FILE__<<" "<<__LINE__<<":"<<__func__<<"Could not read /proc/cpuinfo"<<endl;
800 return serial;
801 }
802 //read lines until find serial
803 bool found=false;
804 while ( !file.eof() && !found ) {
805 char line[1024];
806 file.getline ( line,1024 );
807 string str ( line );
808 char aux[100];
809
810 if ( str.find ( "Serial" ) !=string::npos ) {
811 if ( sscanf ( line,"%s : %s",aux,serial ) !=2 ) {
812 cerr<<__FILE__<<" "<<__LINE__<<":"<<__func__<<"Error parsing /proc/cpuinfo"<<endl;
813 } else found=true;
814 }
815 };
816 return serial;
817 }
818
819 void Private_Impl::convertBGR2RGB ( unsigned char * in_bgr,unsigned char * out_rgb,int size ) {
820 unsigned char *end=in_bgr+size;
821 unsigned char *in_ptr=in_bgr;
822 while ( in_ptr<end ) {
823 swap ( in_ptr[2],in_ptr[0] );
824 in_ptr+=3;
825 }
826 mempcpy ( out_rgb,in_bgr,size );
827
828
829 }
830 void Private_Impl::commitAWB_RB() {
831 MMAL_PARAMETER_AWB_GAINS_T param = {{MMAL_PARAMETER_CUSTOM_AWB_GAINS,sizeof(param)}, {0,0}, {0,0}};
832 param.r_gain.num = (unsigned int)(State.awbg_red * 65536);
833 param.b_gain.num = (unsigned int)(State.awbg_blue * 65536);
834 param.r_gain.den = param.b_gain.den = 65536;
835 if ( mmal_port_parameter_set(State.camera_component->control, &param.hdr) != MMAL_SUCCESS )
836 cout << __func__ << ": Failed to set AWBG gains parameter.\n";
837 }
838 };
839};
840
unsigned int getWidth() const
void setHeight(unsigned int height)
void setFormat(RASPICAM_FORMAT fmt)
void setWidth(unsigned int width)
size_t getImageTypeSize(RASPICAM_FORMAT type) const
unsigned int getHeight() const
void setImageEffect(RASPICAM_IMAGE_EFFECT imageEffect)
void setBrightness(unsigned int brightness)
void setAWB_RB(float red, float blue)
RASPICAM_FORMAT getFormat() const
void setExposure(RASPICAM_EXPOSURE exposure)
void setShutterSpeed(unsigned int shutter)
bool open(bool StartCapture=true)
void setAWB(RASPICAM_AWB awb)
void setFrameRate(unsigned int frame_rate)
unsigned char * getImageBufferData() const
void setSaturation(int saturation)
void setMetering(RASPICAM_METERING metering)
void setCaptureSize(unsigned int width, unsigned int height)
void retrieve(unsigned char *data, RASPICAM_FORMAT type=RASPICAM_FORMAT_IGNORE)
@ RASPICAM_AWB_INCANDESCENT
@ RASPICAM_AWB_SUNLIGHT
@ RASPICAM_AWB_CLOUDY
@ RASPICAM_AWB_FLUORESCENT
@ RASPICAM_AWB_FLASH
@ RASPICAM_AWB_TUNGSTEN
@ RASPICAM_AWB_AUTO
@ RASPICAM_AWB_SHADE
@ RASPICAM_AWB_HORIZON
@ RASPICAM_METERING_SPOT
@ RASPICAM_METERING_AVERAGE
@ RASPICAM_METERING_MATRIX
@ RASPICAM_METERING_BACKLIT
@ RASPICAM_EXPOSURE_SPOTLIGHT
@ RASPICAM_EXPOSURE_OFF
@ RASPICAM_EXPOSURE_BEACH
@ RASPICAM_EXPOSURE_NIGHTPREVIEW
@ RASPICAM_EXPOSURE_AUTO
@ RASPICAM_EXPOSURE_BACKLIGHT
@ RASPICAM_EXPOSURE_ANTISHAKE
@ RASPICAM_EXPOSURE_FIREWORKS
@ RASPICAM_EXPOSURE_NIGHT
@ RASPICAM_EXPOSURE_SNOW
@ RASPICAM_EXPOSURE_SPORTS
@ RASPICAM_EXPOSURE_FIXEDFPS
@ RASPICAM_EXPOSURE_VERYLONG
@ RASPICAM_FORMAT_BGR
@ RASPICAM_FORMAT_GRAY
@ RASPICAM_FORMAT_YUV420
@ RASPICAM_FORMAT_RGB
@ RASPICAM_FORMAT_IGNORE
@ RASPICAM_IMAGE_EFFECT_WATERCOLOR
@ RASPICAM_IMAGE_EFFECT_DENOISE
@ RASPICAM_IMAGE_EFFECT_GPEN
@ RASPICAM_IMAGE_EFFECT_CARTOON
@ RASPICAM_IMAGE_EFFECT_COLORSWAP
@ RASPICAM_IMAGE_EFFECT_COLORPOINT
@ RASPICAM_IMAGE_EFFECT_WASHEDOUT
@ RASPICAM_IMAGE_EFFECT_SKETCH
@ RASPICAM_IMAGE_EFFECT_EMBOSS
@ RASPICAM_IMAGE_EFFECT_HATCH
@ RASPICAM_IMAGE_EFFECT_OILPAINT
@ RASPICAM_IMAGE_EFFECT_SOLARIZE
@ RASPICAM_IMAGE_EFFECT_POSTERISE
@ RASPICAM_IMAGE_EFFECT_FILM
@ RASPICAM_IMAGE_EFFECT_NEGATIVE
@ RASPICAM_IMAGE_EFFECT_NONE
@ RASPICAM_IMAGE_EFFECT_BLUR
@ RASPICAM_IMAGE_EFFECT_SATURATION
@ RASPICAM_IMAGE_EFFECT_PASTEL
@ RASPICAM_IMAGE_EFFECT_COLORBALANCE
#define VIDEO_FRAME_RATE_DEN
#define VIDEO_OUTPUT_BUFFERS_NUM
#define MMAL_CAMERA_VIDEO_PORT
int height
Requested width of image.
float awbg_red
region of interest to use on the sensor. Normalised [0,1] values in the rect
RASPICAM_METERING rpc_exposureMeterMode
MMAL_POOL_T * video_pool
Pointer to the camera component.
PARAM_FLOAT_RECT_T roi
0 or 1
RASPICAM_IMAGE_EFFECT rpc_imageEffect
MMAL_PARAM_COLOURFX_T colourEffects
int exposureCompensation
0 or 1 (false or true)
int framerate
requested height of image
bool videoStabilisation
TODO : what range?
MMAL_COMPONENT_T * camera_component
the camera output or the encoder output (with compression artifacts)
int sharpness
Pointer to the pool of buffers used by encoder output port.