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 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
|
/*
unicap
Copyright (C) 2004 Arne Caspari
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 "config.h"
#include <unicap.h>
#include <unicap_status.h>
#include <unicap_cpi.h>
#include <queue.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <dirent.h>
#include <semaphore.h>
#include <pthread.h>
#include <errno.h>
#include <sys/time.h>
#include <time.h>
#if V4L_DEBUG
#define DEBUG
#endif
#include <debug.h>
#include <libv4l1-videodev.h>
#include <linux/videodev2.h> // for v4l2 checks
#include "v4l.h"
unsigned int v4l_palette_array[] =
{
VIDEO_PALETTE_GREY,
VIDEO_PALETTE_HI240,
VIDEO_PALETTE_RGB565,
VIDEO_PALETTE_RGB555,
VIDEO_PALETTE_RGB24,
VIDEO_PALETTE_RGB32,
VIDEO_PALETTE_YUV422,
VIDEO_PALETTE_YUYV,
VIDEO_PALETTE_UYVY,
VIDEO_PALETTE_YUV420,
VIDEO_PALETTE_YUV411,
VIDEO_PALETTE_RAW,
VIDEO_PALETTE_YUV422P,
VIDEO_PALETTE_YUV411P,
};
static unicap_status_t v4l_enumerate_devices( unicap_device_t *device, int index );
static unicap_status_t v4l_open( void **cpi_data, unicap_device_t *device );
static unicap_status_t v4l_close( void *cpi_data );
static unicap_status_t v4l_reenumerate_formats( void *cpi_data, int *count );
static unicap_status_t v4l_enumerate_formats( void *cpi_data, unicap_format_t *format, int index );
static unicap_status_t v4l_set_format( void *cpi_data, unicap_format_t *format );
static unicap_status_t v4l_get_format( void *cpi_data, unicap_format_t *format );
static unicap_status_t v4l_reenumerate_properties( void *cpi_data, int *count );
static unicap_status_t v4l_enumerate_properties( void *cpi_data, unicap_property_t *property, int index );
static unicap_status_t v4l_set_property( void *cpi_data, unicap_property_t *property );
static unicap_status_t v4l_get_property( void *cpi_data, unicap_property_t *property );
static unicap_status_t v4l_capture_start( void *cpi_data );
static unicap_status_t v4l_capture_stop( void *cpi_data );
static unicap_status_t v4l_queue_buffer( void *cpi_data, unicap_data_buffer_t *buffer );
static unicap_status_t v4l_dequeue_buffer( void *cpi_data, unicap_data_buffer_t **buffer );
static unicap_status_t v4l_wait_buffer( void *cpi_data, unicap_data_buffer_t **buffer );
static unicap_status_t v4l_poll_buffer( void *cpi_data, int *count );
static unicap_status_t v4l_set_event_notify( void *cpi_data,
unicap_event_callback_t func,
unicap_handle_t unicap_handle );
static void v4l_capture_thread( v4l_handle_t handle );
static int queue_buffer( v4l_handle_t v4lhandle, int b );
static struct _unicap_cpi cpi_s =
{
cpi_version: 1<<16,
cpi_capabilities: 0x3ffff,
cpi_enumerate_devices: v4l_enumerate_devices,
cpi_open: v4l_open,
cpi_close: v4l_close,
cpi_reenumerate_formats: v4l_reenumerate_formats,
cpi_enumerate_formats: v4l_enumerate_formats,
cpi_set_format: v4l_set_format,
cpi_get_format: v4l_get_format,
cpi_reenumerate_properties: v4l_reenumerate_properties,
cpi_enumerate_properties: v4l_enumerate_properties,
cpi_set_property: v4l_set_property,
cpi_get_property: v4l_get_property,
cpi_capture_start: v4l_capture_start,
cpi_capture_stop: v4l_capture_stop,
cpi_queue_buffer: v4l_queue_buffer,
cpi_dequeue_buffer: v4l_dequeue_buffer,
cpi_wait_buffer: v4l_wait_buffer,
cpi_poll_buffer: v4l_poll_buffer,
cpi_set_event_notify: v4l_set_event_notify,
};
#if ENABLE_STATIC_CPI
void unicap_v4l_register_static_cpi( struct _unicap_cpi **cpi )
{
*cpi = &cpi_s;
}
#else
unicap_status_t cpi_register( struct _unicap_cpi *reg_data )
{
memcpy( reg_data, &cpi_s, sizeof( struct _unicap_cpi ) );
/* reg_data->cpi_version = 1<<16; */
/* reg_data->cpi_capabilities = 0x3ffff; */
/* reg_data->cpi_enumerate_devices = v4l_enumerate_devices; */
/* reg_data->cpi_open = v4l_open; */
/* reg_data->cpi_close = v4l_close; */
/* reg_data->cpi_reenumerate_formats = v4l_reenumerate_formats; */
/* reg_data->cpi_enumerate_formats = v4l_enumerate_formats; */
/* reg_data->cpi_set_format = v4l_set_format; */
/* reg_data->cpi_get_format = v4l_get_format; */
/* reg_data->cpi_reenumerate_properties = v4l_reenumerate_properties; */
/* reg_data->cpi_enumerate_properties = v4l_enumerate_properties; */
/* reg_data->cpi_set_property = v4l_set_property; */
/* reg_data->cpi_get_property = v4l_get_property; */
/* reg_data->cpi_capture_start = v4l_capture_start; */
/* reg_data->cpi_capture_stop = v4l_capture_stop; */
/* reg_data->cpi_queue_buffer = v4l_queue_buffer; */
/* reg_data->cpi_dequeue_buffer = v4l_dequeue_buffer; */
/* reg_data->cpi_wait_buffer = v4l_wait_buffer; */
/* reg_data->cpi_poll_buffer = v4l_poll_buffer; */
/* reg_data->cpi_set_event_notify = v4l_set_event_notify; */
return STATUS_SUCCESS;
}
#endif//ENABLE_STATIC_CPI
static int file_filter( const struct dirent *a )
{
int match = 0;
// match: 'videoXY' where X = {0..9} and Y = {0..9}
if( !strncmp( a->d_name, "video", 5 ) )
{
if( strlen( a->d_name ) > 5 )
{
if( ( a->d_name[5] >= '0' ) && ( a->d_name[5] <= '9' ) ) // match
// the 'X'
{
match = 1;
}
if( strlen( a->d_name ) > 6 )
{
match = 0;
if( ( a->d_name[6] >= '0' ) && ( a->d_name[6] <= '9' ) )
{
match = 1;
}
}
if( strlen( a->d_name ) > 7 )
{
match = 0;
}
}
}
return match;
}
static unicap_status_t v4l_enumerate_devices( unicap_device_t *device, int index )
{
int fd;
char devname[256];
struct dirent **namelist;
int n;
int found = -1;
struct video_capability v4lcap;
struct v4l2_capability v4l2caps;
TRACE( "*** v4l enumerate devices\n" );
n = scandir( "/dev", &namelist, file_filter, alphasort );
if( n < 0 )
{
TRACE( "Failed to scan directory '/dev' \n" );
return STATUS_NO_DEVICE;
}
while( ( found != index ) && n-- )
{
sprintf( devname, "/dev/%s", namelist[n]->d_name );
/* free( namelist[n]->d_name ); */
TRACE( "v4l: open %s\n", devname );
if( ( fd = open( devname, O_RDONLY | O_NONBLOCK ) ) == -1 )
{
TRACE( "v4l_cpi: open(%s): %s\n", devname, strerror( errno ) );
continue;
}
if( ioctl( fd, VIDIOC_QUERYCAP, &v4l2caps ) == 0 && (v4l2caps.capabilities & V4L2_CAP_VIDEO_CAPTURE))
{
TRACE( "v4l2 ioctls succeeded\n" );
close( fd );
continue;
}
/* if( (v4l2caps.capabilities & V4L2_CAP_VIDEO_CAPTURE) ) */
/* { */
/* // v4l version two device */
/* close( fd ); */
/* continue; */
/* } */
if( ioctl( fd, VIDIOCGCAP, &v4lcap ) < 0 )
{
TRACE( "VIDIOCGCAP ioctl failed!\n" );
close( fd );
continue;
}
if( !(v4lcap.type & VID_TYPE_CAPTURE ) )
{
// no capture to memory capability
close( fd );
continue;
}
found++;
close( fd );
}
/* free( namelist ); */
if( found != index )
{
return STATUS_NO_DEVICE;
}
sprintf( device->identifier, "%s [%d]", v4lcap.name, index );
strcpy( device->device, devname );
strcpy( device->model_name, v4lcap.name );
strcpy( device->vendor_name, "v4l" );
device->model_id = 1;
device->vendor_id = 0xffff0000;
device->flags = UNICAP_CPI_SERIALIZED;
return STATUS_SUCCESS;
}
static unicap_status_t v4l_open( void **cpi_data, unicap_device_t *device )
{
v4l_handle_t v4lhandle;
*cpi_data = malloc( sizeof( struct _v4l_handle ) );
if( !*cpi_data )
{
TRACE( "malloc failed\n" );
return STATUS_FAILURE;
}
v4lhandle = *cpi_data;
memset( v4lhandle, 0x0, sizeof( struct _v4l_handle ) );
if( sem_init( &v4lhandle->sema, 0, 1 ) )
{
TRACE( "sem_init failed\n" );
free( v4lhandle );
return STATUS_FAILURE;
}
if( sem_init( &v4lhandle->out_sema, 0, 0 ) )
{
TRACE( "sem_init failed!\n" );
sem_destroy( &v4lhandle->sema );
free( v4lhandle );
return STATUS_FAILURE;
}
v4lhandle->fd = open( device->device, O_RDWR );
if( v4lhandle->fd == -1 )
{
TRACE( "open failed, device = %s\n", device->device );
TRACE( "error was: %s\n", strerror( errno ) );
return STATUS_FAILURE;
}
if( ioctl( v4lhandle->fd, VIDIOCGCAP, &v4lhandle->v4lcap ) )
{
// failed v4l ioctl, look for next device
close( v4lhandle->fd );
return STATUS_FAILURE;
}
v4l_reenumerate_formats( v4lhandle, NULL );
v4l_reenumerate_properties( v4lhandle, NULL );
v4lhandle->in_queue = ucutil_queue_new();
v4lhandle->out_queue = ucutil_queue_new();
return STATUS_SUCCESS;
}
static unicap_status_t v4l_close( void *cpi_data )
{
v4l_handle_t v4lhandle = ( v4l_handle_t ) cpi_data;
close( v4lhandle->fd );
sem_destroy( &v4lhandle->sema );
sem_destroy( &v4lhandle->out_sema );
if( v4lhandle->unicap_handle )
{
// handle is a copy!!
free( v4lhandle->unicap_handle );
}
free( v4lhandle );
return STATUS_SUCCESS;
}
static unicap_status_t v4l_reenumerate_formats( void *cpi_data, int *_pcount )
{
v4l_handle_t v4lhandle = ( v4l_handle_t ) cpi_data;
struct video_picture v4lpict;
int count = 0;
int p_count = sizeof( v4l_palette_array ) / sizeof( unsigned int );
int i;
if( ioctl( v4lhandle->fd, VIDIOCGPICT, &v4lhandle->v4lpict ) )
{
TRACE( "ioctl failed\n" );
return STATUS_FAILURE;
}
memset( &v4lhandle->palette, 0x0, 32 * sizeof( unsigned int ) );
memcpy( &v4lpict, &v4lhandle->v4lpict, sizeof( struct video_picture ) );
for( i = 0; i < p_count; i++ )
{
v4lpict.palette = v4l_palette_array[i];
TRACE( "try palette: %d\n", v4lpict.palette );
if( !ioctl( v4lhandle->fd, VIDIOCSPICT, &v4lpict ) )
{
TRACE( "succeeded palette: %d\n", v4lpict.palette );
v4lhandle->palette[i] = v4lpict.palette;
count++;
}
}
if( _pcount )
{
*_pcount = count;
}
return STATUS_SUCCESS;
}
static unicap_status_t v4l_enumerate_formats( void *cpi_data, unicap_format_t *format, int index )
{
v4l_handle_t v4lhandle = ( v4l_handle_t ) cpi_data;
unsigned int p = 0xffffffff;
int p_count = sizeof( v4l_palette_array ) / sizeof( unsigned int );
int i, j = -1;
int tmp;
if( v4lhandle->v4lpict.palette == 0 )
{
v4l_reenumerate_formats( cpi_data, &tmp );
}
for( i = 0; i < p_count; i++ )
{
if( v4lhandle->palette[i] )
{
j++;
}
if( j == index )
{
p = v4lhandle->palette[i];
TRACE( "p: %d\n", p );
break;
}
}
if( p == 0xffffffff )
{
return STATUS_NO_MATCH;
}
switch( p )
{
case VIDEO_PALETTE_GREY:
strcpy( format->identifier, "Grey ( Mono 8pp )" );
format->fourcc = FOURCC( 'G', 'R', 'E', 'Y' );
format->bpp = 8;
break;
case VIDEO_PALETTE_HI240:
strcpy( format->identifier, "HI240 Bt848 8Bit color cube" );
format->fourcc = FOURCC( 'H', '2', '4', '0' );
format->bpp = 8;
break;
case VIDEO_PALETTE_RGB565:
strcpy( format->identifier, "RGB 16bpp" );
format->fourcc = FOURCC( 'R', 'G', 'B', '6' );
format->bpp = 16;
break;
case VIDEO_PALETTE_RGB555:
strcpy( format->identifier, "RGB 15bpp" );
format->fourcc = FOURCC( 'R', 'G', 'B', '5' );
format->bpp = 15;
break;
case VIDEO_PALETTE_RGB24:
strcpy( format->identifier, "BGR 24bpp" );
format->fourcc = FOURCC( 'B', 'G', 'R', '3' );
format->bpp = 24;
break;
case VIDEO_PALETTE_RGB32:
strcpy( format->identifier, "RGB 32bpp" );
format->fourcc = FOURCC( 'R', 'G', 'B', '4' );
format->bpp = 32;
break;
case VIDEO_PALETTE_YUV422:
strcpy( format->identifier, "YUV 4:2:2" );
format->fourcc = FOURCC( 'Y', '4', '2', '2' );
format->bpp = 16;
break;
case VIDEO_PALETTE_YUYV:
strcpy( format->identifier, "YUYV" );
format->fourcc = FOURCC( 'Y', 'U', 'Y', 'V' );
format->bpp = 16;
break;
case VIDEO_PALETTE_UYVY:
strcpy( format->identifier, "UYVY" );
format->fourcc = FOURCC( 'U', 'Y', 'V', 'Y' );
format->bpp = 16;
break;
case VIDEO_PALETTE_YUV420:
strcpy( format->identifier, "Y 4:2:0" );
format->fourcc = FOURCC( 'Y', '4', '2', '0' );
format->bpp = 16;
break;
case VIDEO_PALETTE_YUV411:
strcpy( format->identifier, "Y 4:1:1" );
format->fourcc = FOURCC( 'Y', '4', '1', '1' );
format->bpp = 12;
break;
case VIDEO_PALETTE_RAW:
strcpy( format->identifier, "Bt848 raw format" );
format->fourcc = FOURCC( 'R', 'A', 'W', ' ' );
format->bpp = 8;
break;
case VIDEO_PALETTE_YUV422P:
strcpy( format->identifier, "Y 4:2:2 planar" );
format->fourcc = FOURCC( 'Y', '4', '2', 'P' );
format->bpp = 16;
break;
case VIDEO_PALETTE_YUV411P:
strcpy( format->identifier, "Y 4:1:1 planar" );
format->fourcc = FOURCC( '4', '1', '1', 'P' );
format->bpp = 12;
break;
default:
TRACE( "unknown format / wrong index\n" );
return STATUS_FAILURE;
}
format->size.width = v4lhandle->v4lcap.maxwidth;
format->size.height = v4lhandle->v4lcap.maxheight;
format->min_size.width = v4lhandle->v4lcap.minwidth;
format->max_size.width = v4lhandle->v4lcap.maxwidth;
format->min_size.height = v4lhandle->v4lcap.minheight;
format->max_size.height = v4lhandle->v4lcap.maxheight;
format->buffer_size = format->size.width * format->size.height * format->bpp / 8;
format->sizes = 0;
format->size_count = 0;
format->h_stepping = 0;
format->v_stepping = 0;
#ifdef DEBUG
TRACE( "bsize: %d, wxhxb: %dx%dx%d\n", format->buffer_size, format->size.width, format->size.height, format->bpp );
{
char str[1024];
int s = 1024;
unicap_describe_format( format, str, &s );
printf( str );
}
#endif
return STATUS_SUCCESS;
}
static unicap_status_t v4l_set_format( void *cpi_data, unicap_format_t *format )
{
v4l_handle_t v4lhandle = ( v4l_handle_t ) cpi_data;
unsigned int p;
switch( format->fourcc )
{
case FOURCC( 'G', 'R', 'E', 'Y' ):
p = VIDEO_PALETTE_GREY;
break;
case FOURCC( 'H', '2', '4', '0' ):
p = VIDEO_PALETTE_HI240;
break;
case FOURCC( 'R', 'G', 'B', '6' ):
p = VIDEO_PALETTE_RGB565;
break;
case FOURCC( 'R', 'G', 'B', '5' ):
p = VIDEO_PALETTE_RGB555;
break;
case FOURCC( 'B', 'G', 'R', '3' ):
p = VIDEO_PALETTE_RGB24;
break;
case FOURCC( 'R', 'G', 'B', '4' ):
p = VIDEO_PALETTE_RGB32;
break;
case FOURCC( 'Y', '4', '2', '2' ):
p = VIDEO_PALETTE_YUV422;
break;
case FOURCC( 'Y', 'U', 'Y', 'V' ):
p = VIDEO_PALETTE_YUYV;
break;
case FOURCC( 'U', 'Y', 'V', 'Y' ):
p = VIDEO_PALETTE_UYVY;
break;
case FOURCC( 'Y', '4', '2', '0' ):
p = VIDEO_PALETTE_YUV420;
break;
case FOURCC( 'Y', '4', '1', '1' ):
p = VIDEO_PALETTE_YUV411;
break;
case FOURCC( 'R', 'A', 'W', ' ' ):
p = VIDEO_PALETTE_RAW;
break;
case FOURCC( 'Y', '4', '2', 'P' ):
p = VIDEO_PALETTE_YUV422P;
break;
case FOURCC( '4', '1', '1', 'P' ):
p = VIDEO_PALETTE_YUV411P;
break;
default:
TRACE( "set format with unknown fourcc\n" );
return STATUS_FAILURE;
break;
}
// get current settings for brightness etc.
if( ioctl( v4lhandle->fd, VIDIOCGPICT, &v4lhandle->v4lpict ) )
{
TRACE( "ioctl failed: %s\n", strerror( errno ) );
return STATUS_FAILURE;
}
v4lhandle->v4lpict.palette = p;
v4lhandle->v4lpict.depth = format->bpp;
TRACE( "ioctl VIDIOCSPICT\n" );
if( ioctl( v4lhandle->fd, VIDIOCSPICT, &v4lhandle->v4lpict ) )
{
TRACE( "ioctl failed: %s\n", strerror( errno ) );
TRACE( "p: %d\n", v4lhandle->v4lpict.palette );
return STATUS_FAILURE;
}
if( ioctl( v4lhandle->fd, VIDIOCGPICT, &v4lhandle->v4lpict ) )
{
TRACE( "ioctl failed: %s\n", strerror( errno ) );
TRACE( "p: %d\n", v4lhandle->v4lpict.palette );
return STATUS_FAILURE;
}
TRACE( "result: depth: %d palette: %d\n", v4lhandle->v4lpict.depth, v4lhandle->v4lpict.palette );
memset( &v4lhandle->v4lwindow, 0x0, sizeof( struct video_window ) );
v4lhandle->v4lwindow.width = format->size.width;
v4lhandle->v4lwindow.height = format->size.height;
TRACE( "ioctl VIDIOCSWIN size: %dx%d\n", v4lhandle->v4lwindow.width, v4lhandle->v4lwindow.height );
if( ioctl( v4lhandle->fd, VIDIOCSWIN, &v4lhandle->v4lwindow ) )
{
TRACE( "ioctl failed: %s\n", strerror( errno ) );
return STATUS_FAILURE;
}
if( ioctl( v4lhandle->fd, VIDIOCGWIN, &v4lhandle->v4lwindow ) )
{
TRACE( "ioctl failed: %s\n", strerror( errno ) );
return STATUS_FAILURE;
}
TRACE( "succeeded: size: %dx%d \n", v4lhandle->v4lwindow.x, v4lhandle->v4lwindow.y );
unicap_copy_format( &v4lhandle->current_format, format );
return STATUS_SUCCESS;
}
static unicap_status_t v4l_get_format( void *cpi_data, unicap_format_t *format )
{
v4l_handle_t v4lhandle = ( v4l_handle_t ) cpi_data;
memcpy( format, &v4lhandle->current_format, sizeof( unicap_format_t ) );
return STATUS_SUCCESS;
}
static unicap_status_t v4l_reenumerate_properties( void *cpi_data, int *_pcount )
{
v4l_handle_t v4lhandle = ( v4l_handle_t ) cpi_data;
if( ioctl( v4lhandle->fd, VIDIOCGPICT, &v4lhandle->v4lpict_default ) )
{
TRACE( "ioctl failed: %s\n", strerror( errno ) );
return STATUS_FAILURE;
}
if( _pcount )
{
*_pcount = 5;
}
return STATUS_SUCCESS;
}
static unicap_status_t v4l_enumerate_properties( void *cpi_data, unicap_property_t *property, int index )
{
v4l_handle_t v4lhandle = ( v4l_handle_t ) cpi_data;
memset( property, 0x0, sizeof( unicap_property_t ) );
switch( index )
{
case 0:
strcpy( property->identifier, "brightness" );
property->value = v4lhandle->v4lpict_default.brightness;
break;
case 1:
strcpy( property->identifier, "hue" );
property->value = v4lhandle->v4lpict_default.hue;
break;
case 2:
strcpy( property->identifier, "colour" );
property->value = v4lhandle->v4lpict_default.colour;
break;
case 3:
strcpy( property->identifier, "contrast" );
property->value = v4lhandle->v4lpict_default.contrast;
break;
case 4:
strcpy( property->identifier, "whiteness" );
property->value = v4lhandle->v4lpict_default.whiteness;
break;
default:
return STATUS_NO_MATCH;
}
property->range.min = 0.0f;
property->range.max = 1.0f;
property->value = property->value / 65535.0f;
property->flags = UNICAP_FLAGS_MANUAL;
property->flags_mask = UNICAP_FLAGS_MANUAL;
property->stepping = 1.0f/256.0f;
strcpy( property->category, "video" );
return STATUS_SUCCESS;
}
static unicap_status_t v4l_set_property( void *cpi_data, unicap_property_t *property )
{
v4l_handle_t v4lhandle = ( v4l_handle_t ) cpi_data;
int value = property->value * 65535;
if( !strcmp( property->identifier, "brightness" ) )
{
v4lhandle->v4lpict.brightness = value;
}
else if( !strcmp( property->identifier, "hue" ) )
{
v4lhandle->v4lpict.hue = value;
}
else if( !strcmp( property->identifier, "colour" ) )
{
v4lhandle->v4lpict.colour = value;
}
else if( !strcmp( property->identifier, "contrast" ) )
{
v4lhandle->v4lpict.contrast = value;
}
else if( !strcmp( property->identifier, "whiteness" ) )
{
v4lhandle->v4lpict.whiteness = value;
}
else
{
return STATUS_FAILURE;
}
if( ioctl( v4lhandle->fd, VIDIOCSPICT, &v4lhandle->v4lpict ) )
{
TRACE( "ioctl failed: %s\n", strerror( errno ) );
return STATUS_FAILURE;
}
return STATUS_SUCCESS;
}
static unicap_status_t v4l_get_property( void *cpi_data, unicap_property_t *property )
{
v4l_handle_t v4lhandle = ( v4l_handle_t ) cpi_data;
double value;
if( ioctl( v4lhandle->fd, VIDIOCGPICT, &v4lhandle->v4lpict ) )
{
TRACE( "ioctl failed: %s\n", strerror( errno ) );
return STATUS_FAILURE;
}
if( !strcmp( property->identifier, "brightness" ) )
{
value = v4lhandle->v4lpict.brightness;
}
else if( !strcmp( property->identifier, "hue" ) )
{
value = v4lhandle->v4lpict.hue;
}
else if( !strcmp( property->identifier, "colour" ) )
{
value = v4lhandle->v4lpict.colour;
}
else if( !strcmp( property->identifier, "contrast" ) )
{
value = v4lhandle->v4lpict.contrast;
}
else if( !strcmp( property->identifier, "whiteness" ) )
{
value = v4lhandle->v4lpict.whiteness;
}
else
{
return STATUS_FAILURE;
}
property->range.min = 0.0f;
property->range.max = 1.0f;
property->value = value / 65535.0f;
property->flags = UNICAP_FLAGS_MANUAL;
property->flags_mask = UNICAP_FLAGS_MANUAL;
property->stepping = 1.0f/256.0f;
strcpy( property->category, "video" );
return STATUS_SUCCESS;
}
static unicap_status_t v4l_capture_start( void *cpi_data )
{
v4l_handle_t v4lhandle = ( v4l_handle_t ) cpi_data;
int i;
if( ioctl( v4lhandle->fd, VIDIOCGMBUF, &v4lhandle->v4lmbuf ) == -1 )
{
TRACE( "ioctl failed: %s\n", strerror( errno ) );
return STATUS_FAILURE;
}
TRACE( "mbuf: size: %d, frames: %d\n", v4lhandle->v4lmbuf.size, v4lhandle->v4lmbuf.frames );
for( i = 0; i < v4lhandle->v4lmbuf.frames; i++ )
{
TRACE( "offset %d: %x\n", i, v4lhandle->v4lmbuf.offsets[i] );
}
v4lhandle->map = mmap( 0, v4lhandle->v4lmbuf.size, PROT_WRITE | PROT_READ, MAP_SHARED, v4lhandle->fd, 0 );
if( v4lhandle->map == MAP_FAILED )
{
/* ERROR( "mmap failed: %s\nCapture can not start!\n", strerror( errno ) ); */
return STATUS_FAILURE;
}
TRACE( "map: %p\n", v4lhandle->map );
v4lhandle->ready_buffer = -1;
for( i = 0; i < v4lhandle->v4lmbuf.frames; i++ )
{
queue_buffer( v4lhandle, i );
}
v4lhandle->quit_capture_thread = 0;
pthread_create( &v4lhandle->capture_thread, NULL, (void*(*)(void*))v4l_capture_thread, v4lhandle );
v4lhandle->capture_running = 1;
return STATUS_SUCCESS;
}
static unicap_status_t v4l_capture_stop( void *cpi_data )
{
v4l_handle_t v4lhandle = ( v4l_handle_t ) cpi_data;
if( v4lhandle->capture_running )
{
v4lhandle->quit_capture_thread = 1;
sem_post( &v4lhandle->out_sema );
pthread_join( v4lhandle->capture_thread, NULL );
if( v4lhandle->map )
{
munmap( v4lhandle->map, v4lhandle->v4lmbuf.size );
v4lhandle->map = 0;
}
v4lhandle->capture_running = 0;
}
return STATUS_SUCCESS;
}
static unicap_status_t v4l_queue_buffer( void *cpi_data, unicap_data_buffer_t *buffer )
{
v4l_handle_t v4lhandle = ( v4l_handle_t ) cpi_data;
struct _unicap_queue *entry;
entry = malloc( sizeof( unicap_queue_t ) );
entry->data = buffer;
ucutil_insert_back_queue( v4lhandle->in_queue, entry );
return STATUS_SUCCESS;
}
static unicap_status_t v4l_dequeue_buffer( void *cpi_data, unicap_data_buffer_t **buffer )
{
v4l_handle_t handle = ( v4l_handle_t ) cpi_data;
TRACE( "dequeue buffer\n" );
if( handle->capture_running )
{
TRACE( "dequeue while capturing\n" );
return STATUS_FAILURE;
}
return STATUS_NOT_IMPLEMENTED;
}
static unicap_status_t v4l_wait_buffer( void *cpi_data, unicap_data_buffer_t **buffer )
{
v4l_handle_t handle = ( v4l_handle_t ) cpi_data;
unicap_status_t status = STATUS_FAILURE;
*buffer = NULL;
if( !handle->out_queue->next &&! handle->capture_running )
{
TRACE( "Wait->capture stopped" );
return STATUS_IS_STOPPED;
}
sem_wait( &handle->out_sema );
if( handle->out_queue->next )
{
unicap_data_buffer_t *returned_buffer;
struct _unicap_queue *entry;
entry = ucutil_get_front_queue( handle->out_queue );
returned_buffer = ( unicap_data_buffer_t * ) entry->data;
free( entry );
*buffer = returned_buffer;
status = STATUS_SUCCESS;
}
return status;
}
static unicap_status_t v4l_poll_buffer( void *data, int *count )
{
TRACE( "poll buffer\n" );
return STATUS_NOT_IMPLEMENTED;
}
static unicap_status_t v4l_set_event_notify( void *cpi_data, unicap_event_callback_t func, unicap_handle_t unicap_handle )
{
v4l_handle_t handle = ( v4l_handle_t )cpi_data;
handle->event_callback = func;
handle->unicap_handle = unicap_handle;
return STATUS_SUCCESS;
}
static int queue_buffer( v4l_handle_t v4lhandle, int buffer )
{
struct video_mmap v4lmmap;
int frame;
frame = buffer;
TRACE( "queue buffer, frame: %d\n", frame );
v4lmmap.frame = frame;
v4lmmap.width = v4lhandle->current_format.size.width;
v4lmmap.height = v4lhandle->current_format.size.height;
v4lmmap.format = v4lhandle->v4lpict.palette;
if( ioctl( v4lhandle->fd, VIDIOCMCAPTURE, &v4lmmap ) == -1 )
{
TRACE( "ioctl failed: %s\n", strerror( errno ) );
return 0;
}
return 1;
}
static int wait_buffer( v4l_handle_t v4lhandle, unicap_data_buffer_t *data_buffer )
{
int frame;
v4lhandle->ready_buffer = ( v4lhandle->ready_buffer + 1 ) % v4lhandle->v4lmbuf.frames;
frame = v4lhandle->ready_buffer;
TRACE( "wait buffer, frame: %d\n", frame );
if( ioctl( v4lhandle->fd, VIDIOCSYNC, &frame ) == -1 )
{
TRACE( "ioctl failed: %s\n", strerror( errno ) );
return 0;
}
data_buffer->data = v4lhandle->map + v4lhandle->v4lmbuf.offsets[frame];
gettimeofday( &data_buffer->fill_time, NULL );
return 1;
}
static void v4l_capture_thread( v4l_handle_t handle )
{
unicap_data_buffer_t new_frame_buffer;
unicap_copy_format( &new_frame_buffer.format, &handle->current_format );
new_frame_buffer.buffer_size = handle->current_format.buffer_size;
new_frame_buffer.type = UNICAP_BUFFER_TYPE_SYSTEM;
while( !handle->quit_capture_thread )
{
unicap_queue_t *entry;
if( sem_wait( &handle->sema ) )
{
TRACE( "SEM_WAIT_FAILED" );
}
if( !wait_buffer( handle, &new_frame_buffer ) )
{
sem_post( &handle->sema );
continue;
}
sem_post( &handle->sema );
if( handle->event_callback )
{
handle->event_callback( handle->unicap_handle, UNICAP_EVENT_NEW_FRAME, &new_frame_buffer );
}
entry = ucutil_get_front_queue( handle->in_queue );
if( entry )
{
unicap_data_buffer_t *data_buffer = ( unicap_data_buffer_t * ) entry->data;
struct _unicap_queue *outentry = malloc( sizeof( unicap_queue_t ) );
free( entry );
data_buffer->buffer_size = new_frame_buffer.buffer_size;
unicap_copy_format( &data_buffer->format, &new_frame_buffer.format );
if( data_buffer->type == UNICAP_BUFFER_TYPE_SYSTEM )
{
data_buffer->data = new_frame_buffer.data;
}
else
{
memcpy( data_buffer->data, new_frame_buffer.data, new_frame_buffer.buffer_size );
}
outentry->data = data_buffer;
memcpy( &data_buffer->fill_time, &new_frame_buffer.fill_time, sizeof( struct timeval ) );
ucutil_insert_back_queue( handle->out_queue, outentry );
sem_post( &handle->out_sema );
}
if( sem_wait( &handle->sema ) )
{
TRACE( "SEM_WAIT_FAILED\n" );
}
queue_buffer( handle, handle->ready_buffer );
sem_post( &handle->sema );
}
}
|