├── .gitignore ├── COPYING ├── ChangeLog ├── Makefile ├── README ├── TODO ├── uvccapture.c ├── v4l2uvc.c └── v4l2uvc.h /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.jpg 3 | uvccapture 4 | 5 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Library General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License 307 | along with this program; if not, write to the Free Software 308 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 309 | 310 | 311 | Also add information on how to contact you by electronic and paper mail. 312 | 313 | If the program is interactive, make it output a short notice like this 314 | when it starts in an interactive mode: 315 | 316 | Gnomovision version 69, Copyright (C) year name of author 317 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 318 | This is free software, and you are welcome to redistribute it 319 | under certain conditions; type `show c' for details. 320 | 321 | The hypothetical commands `show w' and `show c' should show the appropriate 322 | parts of the General Public License. Of course, the commands you use may 323 | be called something other than `show w' and `show c'; they could even be 324 | mouse-clicks or menu items--whatever suits your program. 325 | 326 | You should also get your employer (if you work as a programmer) or your 327 | school, if any, to sign a "copyright disclaimer" for the program, if 328 | necessary. Here is a sample; alter the names: 329 | 330 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 331 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 332 | 333 | , 1 April 1989 334 | Ty Coon, President of Vice 335 | 336 | This General Public License does not permit incorporating your program into 337 | proprietary programs. If your program is a subroutine library, you may 338 | consider it more useful to permit linking proprietary applications with the 339 | library. If this is what you want to do, use the GNU Library General 340 | Public License instead of this License. 341 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | -- uvccapture 0.4 21.03.2006 -- 2 | Lots of changes: 3 | * Added install target in the makefile to install into /usr/local/bin 4 | * Added signal capturing (CTRL-C, etc) to gracefully exit when in wait loop 5 | * Added the ability to specify a command to run after each image capture, 6 | command is run with the output picture as an argument. command can be run 7 | blocking, or non-blocking. 8 | * Added support for multiple verbisity levels (add more -v's) 9 | * Minor output spelling corrections 10 | * Added output of current bright/contrast/saturation/gain on startup. 11 | All these thanks to Salvador Gonzalez 12 | * YVYU Capture Mode - now requires jpeglib, activated for images sizes >960x720 13 | allows adjustment of JPEG compression quality (-q) or manually activated (-m) 14 | YUYV -> JPEG code thanks to Philip Heron 15 | 16 | -- uvccapture 0.3 19.02.2006 -- 17 | Removed the JPEG decoding files (util.*) and the calls to convert-frame during 18 | the capture of images. This decoding was only important for SDL display, which 19 | is already gone. Peak CPU usage has gone from ~30% to 0.6%. 20 | 21 | Thanks to Philip Heron for the suggestion. -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CPP=g++ 3 | APP_BINARY=uvccapture 4 | VERSION = 0.5-ac1 5 | PREFIX=/usr/local/bin 6 | 7 | WARNINGS = -Wall 8 | 9 | 10 | CFLAGS = -std=gnu99 -O2 -DLINUX -DVERSION=\"$(VERSION)\" $(WARNINGS) 11 | CPPFLAGS = $(CFLAGS) 12 | 13 | OBJECTS= uvccapture.o v4l2uvc.o 14 | 15 | 16 | all: uvccapture 17 | 18 | clean: 19 | @echo "Cleaning up directory." 20 | rm -f *.a *.o $(APP_BINARY) core *~ log errlog 21 | 22 | install: 23 | install $(APP_BINARY) $(PREFIX) 24 | 25 | # Applications: 26 | uvccapture: $(OBJECTS) 27 | $(CC) $(OBJECTS) $(XPM_LIB) $(MATH_LIB) -ljpeg -o $(APP_BINARY) 28 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Uvccapture is a simple application for capturing images from an USB webcam on 2 | Linux. This repository contains my fork and hacks on uvccapture. 3 | 4 | My modifications include: 5 | - Sequential numbering of files (img001.jpg, img002.jpg, ...). 6 | - Command line option for number of images to capture. 7 | - Command line option to skip initial frames prior to capturing 8 | - Allow capturing JPG format at any resolution supported by the camera. The 9 | original uvccapture switches to YUYV mode at resolution 960x720 and higher, 10 | while many cameras support MJPG at full resolution. 11 | - Allow capture rates higher that 1 frame per second. 12 | 13 | The contents of the original README are included below. 14 | The original code that I forked is avaialble under the v0.5 tag. 15 | 16 | ------------------------------------------------------------------------------- 17 | uvccapture: USB UVC Video Class Snapshot Software 18 | Newest versions always available at http://staticwave.ca/source/uvccapture 19 | 20 | This software is based of the package luvcview: 21 | Copyright (C) 2005 2006 Laurent Pinchart && Michel Xhaard 22 | 23 | Modifications and updates: 24 | Copyright (C) 2006 Gabriel A. Devenyi 25 | 26 | This program communicates via the USB UVC Video Class driver for V4L2, available 27 | at http://linux-uvc.berlios.de/. 28 | 29 | The purpose of this software is to capture an image from a USB webcam at a 30 | specified interval, and save it to a JPEG file, no other formats are supported, 31 | imagemagick can handle anything else you might need. 32 | 33 | Right now this software is really a hack, since still image support is not yet 34 | available in the UVC driver. The program continually polls the UVC driver in 35 | MJPEG mode, and at a specified interval writes a JPEG header and a single frame 36 | to file, creating a JPEG image. Because of this continuous polling, this program 37 | has high CPU usage. Input checking is pretty trusting, so call it with the right 38 | parameters. 39 | 40 | In order for this software to work, whatever device you pass to -d you will need 41 | to have read/write rights to, please see appropriate /dev documentation for how 42 | to do this. 43 | 44 | This software depends on jpeglib(present on almost all systems) 45 | 46 | For usage information, run uvccapture -h 47 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | * Better management and error checking on command line options 2 | * Check whether camera supports jpeg at requested resolution. 3 | * Add support for manual focus 4 | * Add support for PAN and TILT 5 | -------------------------------------------------------------------------------- /uvccapture.c: -------------------------------------------------------------------------------- 1 | 2 | /******************************************************************************* 3 | # uvccapture: USB UVC Video Class Snapshot Software # 4 | #This package work with the Logitech UVC based webcams with the mjpeg feature # 5 | #. # 6 | # Orginally Copyright (C) 2005 2006 Laurent Pinchart && Michel Xhaard # 7 | # Modifications Copyright (C) 2006 Gabriel A. Devenyi # 8 | # (C) 2010 Alexandru Csete # 9 | # # 10 | # This program is free software; you can redistribute it and/or modify # 11 | # it under the terms of the GNU General Public License as published by # 12 | # the Free Software Foundation; either version 2 of the License, or # 13 | # (at your option) any later version. # 14 | # # 15 | # This program is distributed in the hope that it will be useful, # 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # 18 | # GNU General Public License for more details. # 19 | # # 20 | # You should have received a copy of the GNU General Public License # 21 | # along with this program; if not, write to the Free Software # 22 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # 23 | # # 24 | *******************************************************************************/ 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | #include "v4l2uvc.h" 38 | 39 | static const char version[] = VERSION; 40 | int run = 1; 41 | 42 | void sigcatch (int sig) 43 | { 44 | fprintf (stderr, "Exiting...\n"); 45 | run = 0; 46 | } 47 | 48 | void usage (void) 49 | { 50 | fprintf (stderr, "uvccapture version %s\n", version); 51 | fprintf (stderr, "Usage is: uvccapture [options]\n"); 52 | fprintf (stderr, "Options:\n"); 53 | fprintf (stderr, "-v\t\tVerbose (add more v's to be more verbose)\n"); 54 | fprintf (stderr, "-o\tOutput filename (default: snap.jpg). Use img%%05d.jpg for sequential files.\n"); 55 | fprintf (stderr, "-d\tV4L2 Device (default: /dev/video0)\n"); 56 | fprintf (stderr, 57 | "-x\tImage Width (must be supported by device)\n"); 58 | fprintf (stderr, 59 | "-y\tImage Height (must be supported by device)\n"); 60 | fprintf (stderr, 61 | "-c\tCommand to run after each image capture(executed as )\n"); 62 | fprintf (stderr, 63 | "-j\tSkip frames before first capture\n"); 64 | fprintf (stderr, 65 | "-t\tTake continuous shots with seconds between them (0 for single shot)\n"); 66 | fprintf (stderr, 67 | "-n\tTake shots then exit. Only applicable when delay is non-zero\n"); 68 | fprintf (stderr, 69 | "-q\tJPEG Quality Compression Level (activates YUYV capture)\n"); 70 | fprintf (stderr, "-r\t\tUse read instead of mmap for image capture\n"); 71 | fprintf (stderr, 72 | "-w\t\tWait for capture command to finish before starting next capture\n"); 73 | fprintf (stderr, "-m\t\tToggles capture mode to YUYV capture\n"); 74 | fprintf (stderr, "Camera Settings:\n"); 75 | fprintf (stderr, "-B\tBrightness\n"); 76 | fprintf (stderr, "-C\tContrast\n"); 77 | fprintf (stderr, "-S\tSaturation\n"); 78 | fprintf (stderr, "-G\tGain\n"); 79 | exit (8); 80 | } 81 | 82 | 83 | /* This is used to execute the postprocessing command. */ 84 | int spawn (char *argv[], int wait, int verbose) 85 | { 86 | pid_t pid; 87 | int rv; 88 | 89 | switch (pid = fork ()) { 90 | case -1: 91 | return -1; 92 | case 0: 93 | // CHILD 94 | execvp (argv[0], argv); 95 | fprintf (stderr, "Error executing command '%s'\n", argv[0]); 96 | exit (1); 97 | default: 98 | // PARENT 99 | if (wait == 1) { 100 | if (verbose >= 1) 101 | fprintf (stderr, "Waiting for command to finish..."); 102 | waitpid (pid, &rv, 0); 103 | if (verbose >= 1) 104 | fprintf (stderr, "\n"); 105 | } else { 106 | // Clean zombies 107 | waitpid (-1, &rv, WNOHANG); 108 | rv = 0; 109 | } 110 | break; 111 | } 112 | 113 | return rv; 114 | } 115 | 116 | int compress_yuyv_to_jpeg (struct vdIn *vd, FILE * file, int quality) 117 | { 118 | struct jpeg_compress_struct cinfo; 119 | struct jpeg_error_mgr jerr; 120 | JSAMPROW row_pointer[1]; 121 | unsigned char *line_buffer, *yuyv; 122 | int z; 123 | 124 | fprintf (stderr, "Compressing YUYV frame to JPEG image.\n"); 125 | 126 | line_buffer = calloc (vd->width * 3, 1); 127 | yuyv = vd->framebuffer; 128 | 129 | cinfo.err = jpeg_std_error (&jerr); 130 | jpeg_create_compress (&cinfo); 131 | jpeg_stdio_dest (&cinfo, file); 132 | 133 | cinfo.image_width = vd->width; 134 | cinfo.image_height = vd->height; 135 | cinfo.input_components = 3; 136 | cinfo.in_color_space = JCS_RGB; 137 | 138 | jpeg_set_defaults (&cinfo); 139 | jpeg_set_quality (&cinfo, quality, TRUE); 140 | 141 | jpeg_start_compress (&cinfo, TRUE); 142 | 143 | z = 0; 144 | while (cinfo.next_scanline < cinfo.image_height) { 145 | int x; 146 | unsigned char *ptr = line_buffer; 147 | 148 | for (x = 0; x < vd->width; x++) { 149 | int r, g, b; 150 | int y, u, v; 151 | 152 | if (!z) 153 | y = yuyv[0] << 8; 154 | else 155 | y = yuyv[2] << 8; 156 | u = yuyv[1] - 128; 157 | v = yuyv[3] - 128; 158 | 159 | r = (y + (359 * v)) >> 8; 160 | g = (y - (88 * u) - (183 * v)) >> 8; 161 | b = (y + (454 * u)) >> 8; 162 | 163 | *(ptr++) = (r > 255) ? 255 : ((r < 0) ? 0 : r); 164 | *(ptr++) = (g > 255) ? 255 : ((g < 0) ? 0 : g); 165 | *(ptr++) = (b > 255) ? 255 : ((b < 0) ? 0 : b); 166 | 167 | if (z++) { 168 | z = 0; 169 | yuyv += 4; 170 | } 171 | } 172 | 173 | row_pointer[0] = line_buffer; 174 | jpeg_write_scanlines (&cinfo, row_pointer, 1); 175 | } 176 | 177 | jpeg_finish_compress (&cinfo); 178 | jpeg_destroy_compress (&cinfo); 179 | 180 | free (line_buffer); 181 | 182 | return (0); 183 | } 184 | 185 | int main (int argc, char *argv[]) 186 | { 187 | char *videodevice = "/dev/video0"; 188 | char *outputfile = "snap.jpg"; 189 | char thisfile[200]; /* used as filename buffer in multi-file seq. */ 190 | char *post_capture_command[3]; 191 | int format = V4L2_PIX_FMT_MJPEG; 192 | int grabmethod = 1; 193 | int width = 320; 194 | int height = 240; 195 | int brightness = 0, contrast = 0, saturation = 0, gain = 0; 196 | int num = -1; /* number of images to capture */ 197 | int verbose = 0; 198 | int delay = 0; 199 | int skip = 0; 200 | int quality = 95; 201 | int post_capture_command_wait = 0; 202 | int multifile = 0; /* flag indicating that we save to a multi-file sequence */ 203 | int i = 0; 204 | 205 | time_t ref_time; 206 | struct vdIn *videoIn; 207 | FILE *file; 208 | 209 | (void) signal (SIGINT, sigcatch); 210 | (void) signal (SIGQUIT, sigcatch); 211 | (void) signal (SIGKILL, sigcatch); 212 | (void) signal (SIGTERM, sigcatch); 213 | (void) signal (SIGABRT, sigcatch); 214 | (void) signal (SIGTRAP, sigcatch); 215 | 216 | // set post_capture_command to default values 217 | post_capture_command[0] = NULL; 218 | post_capture_command[1] = NULL; 219 | post_capture_command[2] = NULL; 220 | 221 | //Options Parsing (FIXME) 222 | while ((argc > 1) && (argv[1][0] == '-')) { 223 | switch (argv[1][1]) { 224 | case 'v': 225 | verbose++; 226 | break; 227 | 228 | case 'o': 229 | outputfile = &argv[1][2]; 230 | /* A % sign indicates format string for multi-file sequence */ 231 | if (strchr(outputfile, '%')) 232 | multifile = 1; 233 | break; 234 | 235 | case 'd': 236 | videodevice = &argv[1][2]; 237 | break; 238 | 239 | case 'x': 240 | width = atoi (&argv[1][2]); 241 | break; 242 | 243 | case 'y': 244 | height = atoi (&argv[1][2]); 245 | break; 246 | 247 | case 'r': 248 | grabmethod = 0; 249 | break; 250 | 251 | case 'm': 252 | format = V4L2_PIX_FMT_YUYV; 253 | break; 254 | 255 | case 'n': 256 | num = atoi (&argv[1][2]); 257 | break; 258 | 259 | case 'j': 260 | skip = atoi (&argv[1][2]); 261 | break; 262 | 263 | case 't': 264 | delay = atoi (&argv[1][2]); 265 | break; 266 | 267 | case 'c': 268 | post_capture_command[0] = &argv[1][2]; 269 | break; 270 | 271 | case 'w': 272 | post_capture_command_wait = 1; 273 | break; 274 | 275 | case 'B': 276 | brightness = atoi (&argv[1][2]); 277 | break; 278 | 279 | case 'C': 280 | contrast = atoi (&argv[1][2]); 281 | break; 282 | 283 | case 'S': 284 | saturation = atoi (&argv[1][2]); 285 | break; 286 | 287 | case 'G': 288 | gain = atoi (&argv[1][2]); 289 | break; 290 | 291 | case 'q': 292 | quality = atoi (&argv[1][2]); 293 | break; 294 | 295 | case 'h': 296 | usage (); 297 | break; 298 | 299 | default: 300 | fprintf (stderr, "Unknown option %s \n", argv[1]); 301 | usage (); 302 | } 303 | ++argv; 304 | --argc; 305 | } 306 | 307 | 308 | /* user requrested quality activates YUYV mode */ 309 | if (quality != 95) 310 | format = V4L2_PIX_FMT_YUYV; 311 | 312 | if (post_capture_command[0]) 313 | post_capture_command[1] = outputfile; 314 | 315 | if (verbose >= 1) { 316 | fprintf (stderr, "Using videodevice: %s\n", videodevice); 317 | fprintf (stderr, "Saving images to: %s\n", outputfile); 318 | fprintf (stderr, "Image size: %dx%d\n", width, height); 319 | fprintf (stderr, "Taking snapshot every %d seconds\n", delay); 320 | if (grabmethod == 1) 321 | fprintf (stderr, "Taking images using mmap\n"); 322 | else 323 | fprintf (stderr, "Taking images using read\n"); 324 | if (post_capture_command[0]) 325 | fprintf (stderr, "Executing '%s' after each image capture\n", 326 | post_capture_command[0]); 327 | } 328 | videoIn = (struct vdIn *) calloc (1, sizeof (struct vdIn)); 329 | if (init_videoIn 330 | (videoIn, (char *) videodevice, width, height, format, grabmethod) < 0) 331 | exit (1); 332 | 333 | //Reset all camera controls 334 | if (verbose >= 1) 335 | fprintf (stderr, "Resetting camera settings\n"); 336 | v4l2ResetControl (videoIn, V4L2_CID_BRIGHTNESS); 337 | v4l2ResetControl (videoIn, V4L2_CID_CONTRAST); 338 | v4l2ResetControl (videoIn, V4L2_CID_SATURATION); 339 | v4l2ResetControl (videoIn, V4L2_CID_GAIN); 340 | 341 | //Setup Camera Parameters 342 | if (brightness != 0) { 343 | if (verbose >= 1) 344 | fprintf (stderr, "Setting camera brightness to %d\n", brightness); 345 | v4l2SetControl (videoIn, V4L2_CID_BRIGHTNESS, brightness); 346 | } else if (verbose >= 1) { 347 | fprintf (stderr, "Camera brightness level is %d\n", 348 | v4l2GetControl (videoIn, V4L2_CID_BRIGHTNESS)); 349 | } 350 | if (contrast != 0) { 351 | if (verbose >= 1) 352 | fprintf (stderr, "Setting camera contrast to %d\n", contrast); 353 | v4l2SetControl (videoIn, V4L2_CID_CONTRAST, contrast); 354 | } else if (verbose >= 1) { 355 | fprintf (stderr, "Camera contrast level is %d\n", 356 | v4l2GetControl (videoIn, V4L2_CID_CONTRAST)); 357 | } 358 | if (saturation != 0) { 359 | if (verbose >= 1) 360 | fprintf (stderr, "Setting camera saturation to %d\n", saturation); 361 | v4l2SetControl (videoIn, V4L2_CID_SATURATION, saturation); 362 | } else if (verbose >= 1) { 363 | fprintf (stderr, "Camera saturation level is %d\n", 364 | v4l2GetControl (videoIn, V4L2_CID_SATURATION)); 365 | } 366 | if (gain != 0) { 367 | if (verbose >= 1) 368 | fprintf (stderr, "Setting camera gain to %d\n", gain); 369 | v4l2SetControl (videoIn, V4L2_CID_GAIN, gain); 370 | } else if (verbose >= 1) { 371 | fprintf (stderr, "Camera gain level is %d\n", 372 | v4l2GetControl (videoIn, V4L2_CID_GAIN)); 373 | } 374 | ref_time = time (NULL); 375 | 376 | while (run) { 377 | if (verbose >= 2) 378 | fprintf (stderr, "Grabbing frame\n"); 379 | if (uvcGrab (videoIn) < 0) { 380 | fprintf (stderr, "Error grabbing\n"); 381 | close_v4l2 (videoIn); 382 | free (videoIn); 383 | exit (1); 384 | } 385 | 386 | if (skip > 0) { skip--; continue; } 387 | 388 | if ((difftime (time (NULL), ref_time) > delay) || delay == 0) { 389 | if (multifile == 1) { 390 | sprintf (thisfile, outputfile, i); 391 | i++; 392 | if (verbose >= 1) 393 | fprintf (stderr, "Saving image to: %s\n", thisfile); 394 | file = fopen (thisfile, "wb"); 395 | } 396 | else { 397 | if (verbose >= 1) 398 | fprintf (stderr, "Saving image to: %s\n", outputfile); 399 | file = fopen (outputfile, "wb"); 400 | } 401 | 402 | if (file != NULL) { 403 | switch (videoIn->formatIn) { 404 | case V4L2_PIX_FMT_YUYV: 405 | compress_yuyv_to_jpeg (videoIn, file, quality); 406 | break; 407 | default: 408 | fwrite (videoIn->tmpbuffer, videoIn->buf.bytesused + DHT_SIZE, 1, 409 | file); 410 | break; 411 | } 412 | fclose (file); 413 | videoIn->getPict = 0; 414 | } 415 | if (post_capture_command[0]) { 416 | if (verbose >= 1) 417 | fprintf (stderr, "Executing '%s %s'\n", post_capture_command[0], 418 | post_capture_command[1]); 419 | if (spawn (post_capture_command, post_capture_command_wait, verbose)) { 420 | fprintf (stderr, "Command exited with error\n"); 421 | close_v4l2 (videoIn); 422 | free (videoIn); 423 | exit (1); 424 | } 425 | } 426 | 427 | ref_time = time (NULL); 428 | } 429 | if ((delay == 0) || (num == i)) 430 | break; 431 | } 432 | close_v4l2 (videoIn); 433 | free (videoIn); 434 | 435 | return 0; 436 | } 437 | -------------------------------------------------------------------------------- /v4l2uvc.c: -------------------------------------------------------------------------------- 1 | 2 | /******************************************************************************* 3 | # uvccapture: USB UVC Video Class Snapshot Software # 4 | #This package work with the Logitech UVC based webcams with the mjpeg feature # 5 | #. # 6 | # Orginally Copyright (C) 2005 2006 Laurent Pinchart && Michel Xhaard # 7 | # Modifications Copyright (C) 2006 Gabriel A. Devenyi # 8 | # # 9 | # This program is free software; you can redistribute it and/or modify # 10 | # it under the terms of the GNU General Public License as published by # 11 | # the Free Software Foundation; either version 2 of the License, or # 12 | # (at your option) any later version. # 13 | # # 14 | # This program is distributed in the hope that it will be useful, # 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # 17 | # GNU General Public License for more details. # 18 | # # 19 | # You should have received a copy of the GNU General Public License # 20 | # along with this program; if not, write to the Free Software # 21 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # 22 | # # 23 | *******************************************************************************/ 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include "v4l2uvc.h" 35 | 36 | static int debug = 0; 37 | 38 | static unsigned char dht_data[DHT_SIZE] = { 39 | 0xff, 0xc4, 0x01, 0xa2, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 40 | 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 41 | 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x01, 0x00, 0x03, 42 | 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 43 | 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 44 | 0x0a, 0x0b, 0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 45 | 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d, 0x01, 0x02, 0x03, 0x00, 0x04, 46 | 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 47 | 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15, 48 | 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 49 | 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 50 | 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 51 | 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 52 | 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 53 | 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 54 | 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 55 | 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 56 | 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 57 | 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 58 | 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 59 | 0xfa, 0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 60 | 0x04, 0x04, 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 61 | 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 62 | 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 63 | 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 64 | 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 65 | 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 66 | 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 67 | 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 68 | 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 69 | 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 70 | 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 71 | 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 72 | 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 73 | 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa 74 | }; 75 | 76 | static int init_v4l2 (struct vdIn *vd); 77 | 78 | int init_videoIn (struct vdIn *vd, char *device, int width, int height, 79 | int format, int grabmethod) 80 | { 81 | 82 | if (vd == NULL || device == NULL) 83 | return -1; 84 | if (width == 0 || height == 0) 85 | return -1; 86 | if (grabmethod < 0 || grabmethod > 1) 87 | grabmethod = 1; //mmap by default; 88 | vd->videodevice = NULL; 89 | vd->status = NULL; 90 | vd->pictName = NULL; 91 | vd->videodevice = (char *) calloc (1, 16 * sizeof (char)); 92 | vd->status = (char *) calloc (1, 100 * sizeof (char)); 93 | vd->pictName = (char *) calloc (1, 80 * sizeof (char)); 94 | snprintf (vd->videodevice, 12, "%s", device); 95 | vd->toggleAvi = 0; 96 | vd->getPict = 0; 97 | vd->signalquit = 1; 98 | vd->width = width; 99 | vd->height = height; 100 | vd->formatIn = format; 101 | vd->grabmethod = grabmethod; 102 | if (init_v4l2 (vd) < 0) { 103 | fprintf (stderr, " Init v4L2 failed !! exit fatal \n"); 104 | goto error;; 105 | } 106 | /* alloc a temp buffer to reconstruct the pict */ 107 | vd->framesizeIn = (vd->width * vd->height << 1); 108 | switch (vd->formatIn) { 109 | case V4L2_PIX_FMT_MJPEG: 110 | vd->tmpbuffer = (unsigned char *) calloc (1, (size_t) vd->framesizeIn); 111 | if (!vd->tmpbuffer) 112 | goto error; 113 | vd->framebuffer = 114 | (unsigned char *) calloc (1, (size_t) vd->width * (vd->height + 8) * 2); 115 | break; 116 | case V4L2_PIX_FMT_YUYV: 117 | vd->framebuffer = (unsigned char *) calloc (1, (size_t) vd->framesizeIn); 118 | break; 119 | default: 120 | fprintf (stderr, " should never arrive exit fatal !!\n"); 121 | goto error; 122 | break; 123 | } 124 | if (!vd->framebuffer) 125 | goto error; 126 | return 0; 127 | error: 128 | free (vd->videodevice); 129 | free (vd->status); 130 | free (vd->pictName); 131 | close (vd->fd); 132 | return -1; 133 | } 134 | 135 | static int init_v4l2 (struct vdIn *vd) 136 | { 137 | int i; 138 | int ret = 0; 139 | 140 | if ((vd->fd = open (vd->videodevice, O_RDWR)) == -1) { 141 | perror ("ERROR opening V4L interface \n"); 142 | exit (1); 143 | } 144 | memset (&vd->cap, 0, sizeof (struct v4l2_capability)); 145 | ret = ioctl (vd->fd, VIDIOC_QUERYCAP, &vd->cap); 146 | if (ret < 0) { 147 | fprintf (stderr, "Error opening device %s: unable to query device.\n", 148 | vd->videodevice); 149 | goto fatal; 150 | } 151 | 152 | if ((vd->cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) { 153 | fprintf (stderr, 154 | "Error opening device %s: video capture not supported.\n", 155 | vd->videodevice); 156 | goto fatal;; 157 | } 158 | if (vd->grabmethod) { 159 | if (!(vd->cap.capabilities & V4L2_CAP_STREAMING)) { 160 | fprintf (stderr, "%s does not support streaming i/o\n", 161 | vd->videodevice); 162 | goto fatal; 163 | } 164 | } else { 165 | if (!(vd->cap.capabilities & V4L2_CAP_READWRITE)) { 166 | fprintf (stderr, "%s does not support read i/o\n", vd->videodevice); 167 | goto fatal; 168 | } 169 | } 170 | /* set format in */ 171 | memset (&vd->fmt, 0, sizeof (struct v4l2_format)); 172 | vd->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 173 | vd->fmt.fmt.pix.width = vd->width; 174 | vd->fmt.fmt.pix.height = vd->height; 175 | vd->fmt.fmt.pix.pixelformat = vd->formatIn; 176 | vd->fmt.fmt.pix.field = V4L2_FIELD_ANY; 177 | ret = ioctl (vd->fd, VIDIOC_S_FMT, &vd->fmt); 178 | if (ret < 0) { 179 | fprintf (stderr, "Unable to set format: %d.\n", errno); 180 | goto fatal; 181 | } 182 | if ((vd->fmt.fmt.pix.width != vd->width) || 183 | (vd->fmt.fmt.pix.height != vd->height)) { 184 | fprintf (stderr, " format asked unavailable get width %d height %d \n", 185 | vd->fmt.fmt.pix.width, vd->fmt.fmt.pix.height); 186 | vd->width = vd->fmt.fmt.pix.width; 187 | vd->height = vd->fmt.fmt.pix.height; 188 | /* look the format is not part of the deal ??? */ 189 | //vd->formatIn = vd->fmt.fmt.pix.pixelformat; 190 | } 191 | /* request buffers */ 192 | memset (&vd->rb, 0, sizeof (struct v4l2_requestbuffers)); 193 | vd->rb.count = NB_BUFFER; 194 | vd->rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 195 | vd->rb.memory = V4L2_MEMORY_MMAP; 196 | 197 | ret = ioctl (vd->fd, VIDIOC_REQBUFS, &vd->rb); 198 | if (ret < 0) { 199 | fprintf (stderr, "Unable to allocate buffers: %d.\n", errno); 200 | goto fatal; 201 | } 202 | /* map the buffers */ 203 | for (i = 0; i < NB_BUFFER; i++) { 204 | memset (&vd->buf, 0, sizeof (struct v4l2_buffer)); 205 | vd->buf.index = i; 206 | vd->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 207 | vd->buf.memory = V4L2_MEMORY_MMAP; 208 | ret = ioctl (vd->fd, VIDIOC_QUERYBUF, &vd->buf); 209 | if (ret < 0) { 210 | fprintf (stderr, "Unable to query buffer (%d).\n", errno); 211 | goto fatal; 212 | } 213 | if (debug) 214 | fprintf (stderr, "length: %u offset: %u\n", vd->buf.length, 215 | vd->buf.m.offset); 216 | vd->mem[i] = mmap (0 /* start anywhere */ , 217 | vd->buf.length, PROT_READ, MAP_SHARED, vd->fd, 218 | vd->buf.m.offset); 219 | if (vd->mem[i] == MAP_FAILED) { 220 | fprintf (stderr, "Unable to map buffer (%d)\n", errno); 221 | goto fatal; 222 | } 223 | if (debug) 224 | fprintf (stderr, "Buffer mapped at address %p.\n", vd->mem[i]); 225 | } 226 | /* Queue the buffers. */ 227 | for (i = 0; i < NB_BUFFER; ++i) { 228 | memset (&vd->buf, 0, sizeof (struct v4l2_buffer)); 229 | vd->buf.index = i; 230 | vd->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 231 | vd->buf.memory = V4L2_MEMORY_MMAP; 232 | ret = ioctl (vd->fd, VIDIOC_QBUF, &vd->buf); 233 | if (ret < 0) { 234 | fprintf (stderr, "Unable to queue buffer (%d).\n", errno); 235 | goto fatal;; 236 | } 237 | } 238 | return 0; 239 | 240 | fatal: 241 | return -1; 242 | 243 | } 244 | 245 | static int video_enable (struct vdIn *vd) 246 | { 247 | int type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 248 | int ret; 249 | 250 | ret = ioctl (vd->fd, VIDIOC_STREAMON, &type); 251 | if (ret < 0) { 252 | fprintf (stderr, "Unable to %s capture: %d.\n", "start", errno); 253 | return ret; 254 | } 255 | vd->isstreaming = 1; 256 | return 0; 257 | } 258 | 259 | static int video_disable (struct vdIn *vd) 260 | { 261 | int type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 262 | int ret; 263 | 264 | ret = ioctl (vd->fd, VIDIOC_STREAMOFF, &type); 265 | if (ret < 0) { 266 | fprintf (stderr, "Unable to %s capture: %d.\n", "stop", errno); 267 | return ret; 268 | } 269 | vd->isstreaming = 0; 270 | return 0; 271 | } 272 | 273 | int uvcGrab (struct vdIn *vd) 274 | { 275 | #define HEADERFRAME1 0xaf 276 | int ret; 277 | 278 | if (!vd->isstreaming) 279 | if (video_enable (vd)) 280 | goto err; 281 | memset (&vd->buf, 0, sizeof (struct v4l2_buffer)); 282 | vd->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 283 | vd->buf.memory = V4L2_MEMORY_MMAP; 284 | ret = ioctl (vd->fd, VIDIOC_DQBUF, &vd->buf); 285 | if (ret < 0) { 286 | fprintf (stderr, "Unable to dequeue buffer (%d).\n", errno); 287 | goto err; 288 | } 289 | switch (vd->formatIn) { 290 | case V4L2_PIX_FMT_MJPEG: 291 | 292 | memcpy (vd->tmpbuffer, vd->mem[vd->buf.index], HEADERFRAME1); 293 | memcpy (vd->tmpbuffer + HEADERFRAME1, dht_data, DHT_SIZE); 294 | memcpy (vd->tmpbuffer + HEADERFRAME1 + DHT_SIZE, 295 | vd->mem[vd->buf.index] + HEADERFRAME1, 296 | (vd->buf.bytesused - HEADERFRAME1)); 297 | if (debug) 298 | fprintf (stderr, "bytes in used %d \n", vd->buf.bytesused); 299 | break; 300 | case V4L2_PIX_FMT_YUYV: 301 | if (vd->buf.bytesused > vd->framesizeIn) 302 | memcpy (vd->framebuffer, vd->mem[vd->buf.index], 303 | (size_t) vd->framesizeIn); 304 | else 305 | memcpy (vd->framebuffer, vd->mem[vd->buf.index], 306 | (size_t) vd->buf.bytesused); 307 | break; 308 | default: 309 | goto err; 310 | break; 311 | } 312 | ret = ioctl (vd->fd, VIDIOC_QBUF, &vd->buf); 313 | if (ret < 0) { 314 | fprintf (stderr, "Unable to requeue buffer (%d).\n", errno); 315 | goto err; 316 | } 317 | 318 | return 0; 319 | err: 320 | vd->signalquit = 0; 321 | return -1; 322 | } 323 | 324 | int close_v4l2 (struct vdIn *vd) 325 | { 326 | int i; 327 | 328 | if (vd->isstreaming) 329 | video_disable (vd); 330 | 331 | /* If the memory maps are not released the device will remain opened even 332 | after a call to close(); */ 333 | for (i = 0; i < NB_BUFFER; i++) { 334 | munmap (vd->mem[i], vd->buf.length); 335 | } 336 | 337 | if (vd->tmpbuffer) 338 | free (vd->tmpbuffer); 339 | vd->tmpbuffer = NULL; 340 | free (vd->framebuffer); 341 | vd->framebuffer = NULL; 342 | free (vd->videodevice); 343 | free (vd->status); 344 | free (vd->pictName); 345 | vd->videodevice = NULL; 346 | vd->status = NULL; 347 | vd->pictName = NULL; 348 | close (vd->fd); 349 | return 0; 350 | } 351 | 352 | /* return >= 0 ok otherwhise -1 */ 353 | static int isv4l2Control (struct vdIn *vd, int control, struct v4l2_queryctrl *queryctrl) 354 | { 355 | int err = 0; 356 | 357 | queryctrl->id = control; 358 | if ((err = ioctl (vd->fd, VIDIOC_QUERYCTRL, queryctrl)) < 0) { 359 | fprintf (stderr, "ioctl querycontrol error %d \n", errno); 360 | } else if (queryctrl->flags & V4L2_CTRL_FLAG_DISABLED) { 361 | fprintf (stderr, "control %s disabled \n", (char *) queryctrl->name); 362 | } else if (queryctrl->flags & V4L2_CTRL_TYPE_BOOLEAN) { 363 | return 1; 364 | } else if (queryctrl->type & V4L2_CTRL_TYPE_INTEGER) { 365 | return 0; 366 | } else { 367 | fprintf (stderr, "contol %s unsupported \n", (char *) queryctrl->name); 368 | } 369 | return -1; 370 | } 371 | 372 | int v4l2GetControl (struct vdIn *vd, int control) 373 | { 374 | struct v4l2_queryctrl queryctrl; 375 | struct v4l2_control control_s; 376 | int err; 377 | 378 | if (isv4l2Control (vd, control, &queryctrl) < 0) 379 | return -1; 380 | control_s.id = control; 381 | if ((err = ioctl (vd->fd, VIDIOC_G_CTRL, &control_s)) < 0) { 382 | fprintf (stderr, "ioctl get control error\n"); 383 | return -1; 384 | } 385 | return control_s.value; 386 | } 387 | 388 | int v4l2SetControl (struct vdIn *vd, int control, int value) 389 | { 390 | struct v4l2_control control_s; 391 | struct v4l2_queryctrl queryctrl; 392 | int min, max, step, val_def; 393 | int err; 394 | 395 | if (isv4l2Control (vd, control, &queryctrl) < 0) 396 | return -1; 397 | min = queryctrl.minimum; 398 | max = queryctrl.maximum; 399 | step = queryctrl.step; 400 | val_def = queryctrl.default_value; 401 | if ((value >= min) && (value <= max)) { 402 | control_s.id = control; 403 | control_s.value = value; 404 | if ((err = ioctl (vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) { 405 | fprintf (stderr, "ioctl set control error\n"); 406 | return -1; 407 | } 408 | } 409 | return 0; 410 | } 411 | 412 | int v4l2UpControl (struct vdIn *vd, int control) 413 | { 414 | struct v4l2_control control_s; 415 | struct v4l2_queryctrl queryctrl; 416 | int min, max, current, step, val_def; 417 | int err; 418 | 419 | if (isv4l2Control (vd, control, &queryctrl) < 0) 420 | return -1; 421 | min = queryctrl.minimum; 422 | max = queryctrl.maximum; 423 | step = queryctrl.step; 424 | val_def = queryctrl.default_value; 425 | current = v4l2GetControl (vd, control); 426 | current += step; 427 | if (current <= max) { 428 | control_s.id = control; 429 | control_s.value = current; 430 | if ((err = ioctl (vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) { 431 | fprintf (stderr, "ioctl set control error\n"); 432 | return -1; 433 | } 434 | } 435 | return control_s.value; 436 | } 437 | 438 | int v4l2DownControl (struct vdIn *vd, int control) 439 | { 440 | struct v4l2_control control_s; 441 | struct v4l2_queryctrl queryctrl; 442 | int min, max, current, step, val_def; 443 | int err; 444 | 445 | if (isv4l2Control (vd, control, &queryctrl) < 0) 446 | return -1; 447 | min = queryctrl.minimum; 448 | max = queryctrl.maximum; 449 | step = queryctrl.step; 450 | val_def = queryctrl.default_value; 451 | current = v4l2GetControl (vd, control); 452 | current -= step; 453 | if (current >= min) { 454 | control_s.id = control; 455 | control_s.value = current; 456 | if ((err = ioctl (vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) { 457 | fprintf (stderr, "ioctl set control error\n"); 458 | return -1; 459 | } 460 | } 461 | return control_s.value; 462 | } 463 | 464 | int v4l2ToggleControl (struct vdIn *vd, int control) 465 | { 466 | struct v4l2_control control_s; 467 | struct v4l2_queryctrl queryctrl; 468 | int current; 469 | int err; 470 | 471 | if (isv4l2Control (vd, control, &queryctrl) != 1) 472 | return -1; 473 | current = v4l2GetControl (vd, control); 474 | control_s.id = control; 475 | control_s.value = !current; 476 | if ((err = ioctl (vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) { 477 | fprintf (stderr, "ioctl toggle control error\n"); 478 | return -1; 479 | } 480 | return control_s.value; 481 | } 482 | 483 | int v4l2ResetControl (struct vdIn *vd, int control) 484 | { 485 | struct v4l2_control control_s; 486 | struct v4l2_queryctrl queryctrl; 487 | int val_def; 488 | int err; 489 | 490 | if (isv4l2Control (vd, control, &queryctrl) < 0) 491 | return -1; 492 | val_def = queryctrl.default_value; 493 | control_s.id = control; 494 | control_s.value = val_def; 495 | if ((err = ioctl (vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) { 496 | fprintf (stderr, "ioctl reset control error\n"); 497 | return -1; 498 | } 499 | 500 | return 0; 501 | } 502 | 503 | int v4l2ResetPanTilt (struct vdIn *vd, int pantilt) 504 | { 505 | int control = V4L2_CID_PANTILT_RESET; 506 | struct v4l2_control control_s; 507 | struct v4l2_queryctrl queryctrl; 508 | unsigned char val; 509 | int err; 510 | 511 | if (isv4l2Control (vd, control, &queryctrl) < 0) 512 | return -1; 513 | val = (unsigned char) pantilt; 514 | control_s.id = control; 515 | control_s.value = val; 516 | if ((err = ioctl (vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) { 517 | fprintf (stderr, "ioctl reset Pan control error\n"); 518 | return -1; 519 | } 520 | 521 | return 0; 522 | } 523 | 524 | union pantilt { 525 | struct { 526 | short pan; 527 | short tilt; 528 | } s16; 529 | int value; 530 | } pantilt; 531 | 532 | int v4L2UpDownPan (struct vdIn *vd, short inc) 533 | { 534 | int control = V4L2_CID_PANTILT_RELATIVE; 535 | struct v4l2_control control_s; 536 | struct v4l2_queryctrl queryctrl; 537 | int err; 538 | 539 | union pantilt pan; 540 | 541 | control_s.id = control; 542 | if (isv4l2Control (vd, control, &queryctrl) < 0) 543 | return -1; 544 | 545 | pan.s16.pan = inc; 546 | pan.s16.tilt = 0; 547 | 548 | control_s.value = pan.value; 549 | if ((err = ioctl (vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) { 550 | fprintf (stderr, "ioctl pan updown control error\n"); 551 | return -1; 552 | } 553 | return 0; 554 | } 555 | 556 | int v4L2UpDownTilt (struct vdIn *vd, short inc) 557 | { 558 | int control = V4L2_CID_PANTILT_RELATIVE; 559 | struct v4l2_control control_s; 560 | struct v4l2_queryctrl queryctrl; 561 | int err; 562 | union pantilt pan; 563 | 564 | control_s.id = control; 565 | if (isv4l2Control (vd, control, &queryctrl) < 0) 566 | return -1; 567 | 568 | pan.s16.pan = 0; 569 | pan.s16.tilt = inc; 570 | 571 | control_s.value = pan.value; 572 | if ((err = ioctl (vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) { 573 | fprintf (stderr, "ioctl tiltupdown control error\n"); 574 | return -1; 575 | } 576 | return 0; 577 | } 578 | -------------------------------------------------------------------------------- /v4l2uvc.h: -------------------------------------------------------------------------------- 1 | 2 | /******************************************************************************* 3 | # uvccapture: USB UVC Video Class Snapshot Software # 4 | #This package work with the Logitech UVC based webcams with the mjpeg feature # 5 | # # 6 | # Orginally Copyright (C) 2005 2006 Laurent Pinchart && Michel Xhaard # 7 | # Modifications Copyright (C) 2006 Gabriel A. Devenyi # 8 | # # 9 | # This program is free software; you can redistribute it and/or modify # 10 | # it under the terms of the GNU General Public License as published by # 11 | # the Free Software Foundation; either version 2 of the License, or # 12 | # (at your option) any later version. # 13 | # # 14 | # This program is distributed in the hope that it will be useful, # 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # 17 | # GNU General Public License for more details. # 18 | # # 19 | # You should have received a copy of the GNU General Public License # 20 | # along with this program; if not, write to the Free Software # 21 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # 22 | # # 23 | *******************************************************************************/ 24 | 25 | #define NB_BUFFER 16 26 | #define DHT_SIZE 420 27 | 28 | #define V4L2_CID_BACKLIGHT_COMPENSATION (V4L2_CID_PRIVATE_BASE+0) 29 | #define V4L2_CID_POWER_LINE_FREQUENCY (V4L2_CID_PRIVATE_BASE+1) 30 | #define V4L2_CID_SHARPNESS (V4L2_CID_PRIVATE_BASE+2) 31 | #define V4L2_CID_HUE_AUTO (V4L2_CID_PRIVATE_BASE+3) 32 | #define V4L2_CID_FOCUS_AUTO (V4L2_CID_PRIVATE_BASE+4) 33 | #define V4L2_CID_FOCUS_ABSOLUTE (V4L2_CID_PRIVATE_BASE+5) 34 | #define V4L2_CID_FOCUS_RELATIVE (V4L2_CID_PRIVATE_BASE+6) 35 | 36 | #define V4L2_CID_PANTILT_RELATIVE (V4L2_CID_PRIVATE_BASE+7) 37 | #define V4L2_CID_PANTILT_RESET (V4L2_CID_PRIVATE_BASE+8) 38 | 39 | struct vdIn { 40 | int fd; 41 | char *videodevice; 42 | char *status; 43 | char *pictName; 44 | struct v4l2_capability cap; 45 | struct v4l2_format fmt; 46 | struct v4l2_buffer buf; 47 | struct v4l2_requestbuffers rb; 48 | void *mem[NB_BUFFER]; 49 | unsigned char *tmpbuffer; 50 | unsigned char *framebuffer; 51 | int isstreaming; 52 | int grabmethod; 53 | int width; 54 | int height; 55 | int formatIn; 56 | int formatOut; 57 | int framesizeIn; 58 | int signalquit; 59 | int toggleAvi; 60 | int getPict; 61 | 62 | }; 63 | 64 | int init_videoIn (struct vdIn *vd, char *device, int width, int height, 65 | int format, int grabmethod); 66 | int uvcGrab (struct vdIn *vd); 67 | int close_v4l2 (struct vdIn *vd); 68 | 69 | int v4l2GetControl (struct vdIn *vd, int control); 70 | int v4l2SetControl (struct vdIn *vd, int control, int value); 71 | int v4l2UpControl (struct vdIn *vd, int control); 72 | int v4l2DownControl (struct vdIn *vd, int control); 73 | int v4l2ToggleControl (struct vdIn *vd, int control); 74 | int v4l2ResetControl (struct vdIn *vd, int control); 75 | int v4l2ResetPanTilt (struct vdIn *vd, int pantilt); 76 | int v4L2UpDownPan (struct vdIn *vd, short inc); 77 | int v4L2UpDownTilt (struct vdIn *vd, short inc); 78 | --------------------------------------------------------------------------------