From febcb53ca85d911619456c09c4be49fd73c4964b Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Sat, 30 Jul 2011 14:33:37 +0200 Subject: omap3-isp-live: Initial commit Signed-off-by: Laurent Pinchart --- isp/LICENSE | 502 +++++++++++++++++++++++++++++++++++ isp/Makefile | 24 ++ isp/controls.c | 259 ++++++++++++++++++ isp/controls.h | 29 ++ isp/list.h | 88 ++++++ isp/media.c | 390 +++++++++++++++++++++++++++ isp/media.h | 156 +++++++++++ isp/omap3isp-priv.h | 68 +++++ isp/omap3isp.c | 750 ++++++++++++++++++++++++++++++++++++++++++++++++++++ isp/omap3isp.h | 80 ++++++ isp/subdev.c | 287 ++++++++++++++++++++ isp/subdev.h | 201 ++++++++++++++ isp/tools.h | 34 +++ isp/v4l2-pool.c | 80 ++++++ isp/v4l2-pool.h | 87 ++++++ isp/v4l2.c | 717 +++++++++++++++++++++++++++++++++++++++++++++++++ isp/v4l2.h | 289 ++++++++++++++++++++ 17 files changed, 4041 insertions(+) create mode 100644 isp/LICENSE create mode 100644 isp/Makefile create mode 100644 isp/controls.c create mode 100644 isp/controls.h create mode 100644 isp/list.h create mode 100644 isp/media.c create mode 100644 isp/media.h create mode 100644 isp/omap3isp-priv.h create mode 100644 isp/omap3isp.c create mode 100644 isp/omap3isp.h create mode 100644 isp/subdev.c create mode 100644 isp/subdev.h create mode 100644 isp/tools.h create mode 100644 isp/v4l2-pool.c create mode 100644 isp/v4l2-pool.h create mode 100644 isp/v4l2.c create mode 100644 isp/v4l2.h (limited to 'isp') diff --git a/isp/LICENSE b/isp/LICENSE new file mode 100644 index 0000000..4362b49 --- /dev/null +++ b/isp/LICENSE @@ -0,0 +1,502 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! diff --git a/isp/Makefile b/isp/Makefile new file mode 100644 index 0000000..4b47ee6 --- /dev/null +++ b/isp/Makefile @@ -0,0 +1,24 @@ +AR := $(CROSS_COMPILE)ar +CC := $(CROSS_COMPILE)gcc +CFLAGS := -O2 -W -Wall -I$(KDIR)/usr/include -fPIC +LDFLAGS := + +OBJECTS := controls.o \ + media.o \ + omap3isp.o \ + subdev.o \ + v4l2.o \ + v4l2-pool.o + +%.o : %.c + $(CC) $(CFLAGS) -c -o $@ $< + +all: libomap3isp.so + +libomap3isp.so: $(OBJECTS) + $(CC) -o $@ -shared $^ + +clean: + -$(RM) *.o + -$(RM) libomap3isp.so + diff --git a/isp/controls.c b/isp/controls.c new file mode 100644 index 0000000..1c46039 --- /dev/null +++ b/isp/controls.c @@ -0,0 +1,259 @@ +/* + * OMAP3 ISP library - OMAP3 ISP controls + * + * Copyright (C) 2010-2011 Ideas on board SPRL + * + * Contact: Laurent Pinchart + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at + * your option) any later version. + * + * This library 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 Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include + +#include +#include +#include +#include + +#include "controls.h" +#include "omap3isp.h" +#include "omap3isp-priv.h" +#include "subdev.h" + +/* ----------------------------------------------------------------------------- + * Matrix algebra helper functions + */ + +struct matrix { + float coeff[3][3]; +}; + +static void matrix_zero(struct matrix *m) +{ + unsigned int i, j; + + for (i = 0; i < 3; ++i) { + for (j = 0; j < 3; ++j) + m->coeff[i][j] = 0.0; + } +} + +static void matrix_invert(struct matrix *m) +{ + /* Invert the matrix using the transpose of the matrix of cofactors. The + * Gauss-Jordan elimination would be faster in the general case, but we + * know that the matrix is 3x3. + */ + const float eps = 1e-6; + struct matrix out; + unsigned int i, j; + float det; + + out.coeff[0][0] = m->coeff[1][1] * m->coeff[2][2] + - m->coeff[1][2] * m->coeff[2][1]; + out.coeff[0][1] = m->coeff[0][2] * m->coeff[2][1] + - m->coeff[0][1] * m->coeff[2][2]; + out.coeff[0][2] = m->coeff[0][1] * m->coeff[1][2] + - m->coeff[0][2] * m->coeff[1][1]; + out.coeff[1][0] = m->coeff[1][2] * m->coeff[2][0] + - m->coeff[1][0] * m->coeff[2][2]; + out.coeff[1][1] = m->coeff[0][0] * m->coeff[2][2] + - m->coeff[0][2] * m->coeff[2][0]; + out.coeff[1][2] = m->coeff[0][2] * m->coeff[1][0] + - m->coeff[0][0] * m->coeff[1][2]; + out.coeff[2][0] = m->coeff[1][0] * m->coeff[2][1] + - m->coeff[1][1] * m->coeff[2][0]; + out.coeff[2][1] = m->coeff[0][1] * m->coeff[2][0] + - m->coeff[0][0] * m->coeff[2][1]; + out.coeff[2][2] = m->coeff[0][0] * m->coeff[1][1] + - m->coeff[1][0] * m->coeff[0][1]; + + det = m->coeff[0][0] * out.coeff[0][0] + + m->coeff[0][1] * out.coeff[1][0] + + m->coeff[0][2] * out.coeff[2][0]; + + if (det < eps) + return; + + det = 1/det; + + for (i = 0; i < 3; ++i) + for (j = 0; j < 3; ++j) + m->coeff[i][j] = out.coeff[i][j] * det; +} + +static void matrix_multiply(struct matrix *a, const struct matrix *b) +{ + struct matrix out; + + /* Compute a * b and return the result in a. */ + out.coeff[0][0] = a->coeff[0][0] * b->coeff[0][0] + + a->coeff[0][1] * b->coeff[1][0] + + a->coeff[0][2] * b->coeff[2][0]; + out.coeff[0][1] = a->coeff[0][0] * b->coeff[0][1] + + a->coeff[0][1] * b->coeff[1][1] + + a->coeff[0][2] * b->coeff[2][1]; + out.coeff[0][2] = a->coeff[0][0] * b->coeff[0][2] + + a->coeff[0][1] * b->coeff[1][2] + + a->coeff[0][2] * b->coeff[2][2]; + out.coeff[1][0] = a->coeff[1][0] * b->coeff[0][0] + + a->coeff[1][1] * b->coeff[1][0] + + a->coeff[1][2] * b->coeff[2][0]; + out.coeff[1][1] = a->coeff[1][0] * b->coeff[0][1] + + a->coeff[1][1] * b->coeff[1][1] + + a->coeff[1][2] * b->coeff[2][1]; + out.coeff[1][2] = a->coeff[1][0] * b->coeff[0][2] + + a->coeff[1][1] * b->coeff[1][2] + + a->coeff[1][2] * b->coeff[2][2]; + out.coeff[2][0] = a->coeff[2][0] * b->coeff[0][0] + + a->coeff[2][1] * b->coeff[1][0] + + a->coeff[2][2] * b->coeff[2][0]; + out.coeff[2][1] = a->coeff[2][0] * b->coeff[0][1] + + a->coeff[2][1] * b->coeff[1][1] + + a->coeff[2][2] * b->coeff[2][1]; + out.coeff[2][2] = a->coeff[2][0] * b->coeff[0][2] + + a->coeff[2][1] * b->coeff[1][2] + + a->coeff[2][2] * b->coeff[2][2]; + + *a = out; +} + +static void matrix_float_to_s10q8(__u16 out[3][3], const struct matrix *in) +{ + unsigned int i, j; + + for (i = 0; i < 3; ++i) { + for (j = 0; j < 3; ++j) + out[i][j] = (__u16)((__s16)(in->coeff[i][j] * 256) & 0x3ff); + } +} + +static void matrix_float_to_s12q8(__u16 out[3][3], const struct matrix *in) +{ + unsigned int i, j; + + for (i = 0; i < 3; ++i) { + for (j = 0; j < 3; ++j) + out[i][j] = (__u16)((__s16)(in->coeff[i][j] * 256) & 0xfff); + } +} + +/* ----------------------------------------------------------------------------- + * Preview engine parameters configuration + */ + +static const struct matrix omap3isp_preview_csc = { + .coeff = { + /* Default values. */ + { 0.2968750, 0.5937500, 0.1093750 }, + { -0.1718750, -0.3281250, 0.5000000 }, + { 0.5000000, -0.3828125, -0.0781250 }, +#if 0 + /* Default values for fluorescent light. */ + { 0.25781250, 0.50390625, 0.09765625 }, + { -0.14843750, -0.29296875, 0.43750000 }, + { 0.43750000, -0.36718750, -0.07031250 }, +#endif + }, +}; + +static const struct matrix omap3isp_preview_rgb2rgb = { + .coeff = { + /* Default values. */ + { 1.0, 0.0, 0.0 }, + { 0.0, 1.0, 0.0 }, + { 0.0, 0.0, 1.0 }, +#if 0 + /* Default values for fluorescent light. */ + { 1.88281250, -0.812500, -0.07031250 }, + { -0.39453125, 1.671875, -0.27734375 }, + { -0.12500000, -1.250000, 2.37500000 }, +#endif + }, +}; + +int omap3_isp_preview_setup(struct omap3_isp_device *isp) +{ + struct omap3isp_prev_update_config config; + struct omap3isp_prev_rgbtorgb rgb2rgb; + struct omap3isp_prev_csc csc; + int ret; + + memset(&config, 0, sizeof config); + config.update = OMAP3ISP_PREV_RGB2RGB | OMAP3ISP_PREV_COLOR_CONV; + config.flag = OMAP3ISP_PREV_RGB2RGB | OMAP3ISP_PREV_COLOR_CONV; + config.rgb2rgb = &rgb2rgb; + config.csc = &csc; + + memset(&rgb2rgb, 0, sizeof rgb2rgb); + matrix_float_to_s12q8(rgb2rgb.matrix, &omap3isp_preview_rgb2rgb); + + memset(&csc, 0, sizeof csc); + matrix_float_to_s10q8(csc.matrix, &omap3isp_preview_csc); + + v4l2_subdev_open(isp->preview); + + ret = ioctl(isp->preview->fd, VIDIOC_OMAP3ISP_PRV_CFG, &config); + if (ret < 0) + return -errno; + + return ret; +} + +int omap3_isp_preview_set_contrast(struct omap3_isp_device *isp, unsigned int value) +{ + int contrast = value; + int ret; + + ret = v4l2_subdev_set_control(isp->preview, V4L2_CID_CONTRAST, &contrast); + if (ret < 0) + return -errno; + + return ret; +} +int omap3_isp_preview_set_saturation(struct omap3_isp_device *isp, float value) +{ + struct omap3isp_prev_update_config config; + struct omap3isp_prev_rgbtorgb rgb2rgb; + struct matrix saturation; + struct matrix gain; + int ret; + + matrix_zero(&gain); + gain.coeff[0][0] = 1.0; + gain.coeff[1][1] = value; + gain.coeff[2][2] = value; + + saturation = omap3isp_preview_csc; + matrix_invert(&saturation); + matrix_multiply(&saturation, &gain); + matrix_multiply(&saturation, &omap3isp_preview_csc); + + memset(&config, 0, sizeof config); + config.update = OMAP3ISP_PREV_RGB2RGB; + config.flag = OMAP3ISP_PREV_RGB2RGB; + config.rgb2rgb = &rgb2rgb; + + memset(&rgb2rgb, 0, sizeof rgb2rgb); + matrix_float_to_s12q8(rgb2rgb.matrix, &saturation); + + ret = ioctl(isp->preview->fd, VIDIOC_OMAP3ISP_PRV_CFG, &config); + if (ret < 0) + return -errno; + + return ret; +} diff --git a/isp/controls.h b/isp/controls.h new file mode 100644 index 0000000..6c7f74e --- /dev/null +++ b/isp/controls.h @@ -0,0 +1,29 @@ +/* + * OMAP3 ISP library - OMAP3 ISP controls + * + * Copyright (C) 2010-2011 Ideas on board SPRL + * + * Contact: Laurent Pinchart + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at + * your option) any later version. + * + * This library 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 Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef __OMAP3ISP_CONTROLS_H +#define __OMAP3ISP_CONTROLS_H + +struct omap3_isp_device; + +int omap3_isp_preview_setup(struct omap3_isp_device *isp); + +#endif /* __OMAP3ISP_CONTROLS_H */ diff --git a/isp/list.h b/isp/list.h new file mode 100644 index 0000000..081e2c4 --- /dev/null +++ b/isp/list.h @@ -0,0 +1,88 @@ +/* + * OMAP3 ISP library - Double linked lists + * + * Copyright (C) 2010-2011 Ideas on board SPRL + * + * Contact: Laurent Pinchart + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at + * your option) any later version. + * + * This library 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 Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __LIST_H +#define __LIST_H + +#include + +struct list_entry { + struct list_entry *prev; + struct list_entry *next; +}; + +static inline void list_init(struct list_entry *list) +{ + list->next = list; + list->prev = list; +} + +static inline void list_append(struct list_entry *entry, struct list_entry *list) +{ + entry->next = list; + entry->prev = list->prev; + list->prev->next = entry; + list->prev = entry; +} + +static inline void list_prepend(struct list_entry *entry, struct list_entry *list) +{ + entry->next = list->next; + entry->prev = list; + list->next->prev = entry; + list->next = entry; +} + +static inline void list_remove(struct list_entry *entry) +{ + entry->prev->next = entry->next; + entry->next->prev = entry->prev; +} + +#define list_entry(entry, type, member) \ + (type *)((char *)(entry) - offsetof(type, member)) + +#define list_first_entry(list, type, member) \ + list_entry(list->next, type, member) + +#define list_last_entry(list, type, member) \ + list_entry(list->prev, type, member) + +#define list_for_each(entry, list) \ + for (entry = (list)->next; entry != (list); entry = entry->next) + +#define list_for_each_entry(entry, list, member) \ + for (entry = list_entry((list)->next, typeof(*entry), member); \ + &entry->member != (list); \ + entry = list_entry(entry->member.next, typeof(*entry), member)) + +#define list_for_each_safe(entry, __next, list) \ + for (entry = (list)->next, __next = entry->next; entry != (list); \ + entry = __next, __next = entry->next) + +#define list_for_each_entry_safe(entry, __next, list, member) \ + for (entry = list_entry((list)->next, typeof(*entry), member), \ + __next = list_entry(entry->member.next, typeof(*entry), member); \ + &entry->member != (list); \ + entry = __next, __next = list_entry(entry->member.next, typeof(*entry), member)) + +#endif /* __LIST_H */ diff --git a/isp/media.c b/isp/media.c new file mode 100644 index 0000000..d2082f0 --- /dev/null +++ b/isp/media.c @@ -0,0 +1,390 @@ +/* + * OMAP3 ISP library - Media controller + * + * Copyright (C) 2010-2011 Ideas on board SPRL + * + * Contact: Laurent Pinchart + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at + * your option) any later version. + * + * This library 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 Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "media.h" +#include "tools.h" + +struct media_entity_pad *media_entity_remote_source(struct media_entity_pad *pad) +{ + unsigned int i; + + for (i = 0; i < pad->entity->num_links; ++i) { + struct media_entity_link *link = &pad->entity->links[i]; + + if (!(link->flags & MEDIA_LNK_FL_ENABLED)) + continue; + + if (link->source == pad) + return link->sink; + + if (link->sink == pad) + return link->source; + } + + return NULL; +} + +struct media_entity *media_get_entity_by_name(struct media_device *media, + const char *name) +{ + unsigned int i; + + for (i = 0; i < media->entities_count; ++i) { + struct media_entity *entity = &media->entities[i]; + + if (strcmp(entity->info.name, name) == 0) + return entity; + } + + return NULL; +} + +struct media_entity *media_get_entity_by_id(struct media_device *media, + __u32 id) +{ + unsigned int i; + + for (i = 0; i < media->entities_count; ++i) { + struct media_entity *entity = &media->entities[i]; + + if (entity->info.id == id) + return entity; + } + + return NULL; +} + +int media_setup_link(struct media_device *media, + struct media_entity_pad *source, + struct media_entity_pad *sink, + __u32 flags) +{ + struct media_entity_link *link; + struct media_link_desc ulink; + unsigned int i; + int ret; + + for (i = 0; i < source->entity->num_links; i++) { + link = &source->entity->links[i]; + + if (link->source->entity == source->entity && + link->source->index == source->index && + link->sink->entity == sink->entity && + link->sink->index == sink->index) + break; + } + + if (i == source->entity->num_links) { + printf("%s: Link not found\n", __func__); + return -EINVAL; + } + + /* source pad */ + ulink.source.entity = source->entity->info.id; + ulink.source.index = source->index; + ulink.source.flags = MEDIA_PAD_FL_SOURCE; + + /* sink pad */ + ulink.sink.entity = sink->entity->info.id; + ulink.sink.index = sink->index; + ulink.sink.flags = MEDIA_PAD_FL_SINK; + + ulink.flags = flags | (link->flags & MEDIA_LNK_FL_IMMUTABLE); + + ret = ioctl(media->fd, MEDIA_IOC_SETUP_LINK, &ulink); + if (ret < 0) { + printf("%s: Unable to setup link (%s)\n", __func__, + strerror(errno)); + return ret; + } + + link->flags = flags; + return 0; +} + +int media_reset_links(struct media_device *media) +{ + unsigned int i, j; + int ret; + + for (i = 0; i < media->entities_count; ++i) { + struct media_entity *entity = &media->entities[i]; + + for (j = 0; j < entity->num_links; j++) { + struct media_entity_link *link = &entity->links[j]; + + if (link->flags & MEDIA_LNK_FL_IMMUTABLE || + link->source->entity != entity) + continue; + + ret = media_setup_link(media, link->source, link->sink, + link->flags & ~MEDIA_LNK_FL_ENABLED); + if (ret < 0) + return ret; + } + } + + return 0; +} + +static struct media_entity_link *media_entity_add_link(struct media_entity *entity) +{ + if (entity->num_links >= entity->max_links) { + struct media_entity_link *links = entity->links; + unsigned int max_links = entity->max_links * 2; + unsigned int i; + + links = realloc(links, max_links * sizeof *links); + if (links == NULL) + return NULL; + + for (i = 0; i < entity->num_links; ++i) + links[i].reverse->reverse = &links[i]; + + entity->max_links = max_links; + entity->links = links; + } + + return &entity->links[entity->num_links++]; +} + +static int media_enum_links(struct media_device *media) +{ + __u32 id; + int ret = 0; + + for (id = 1; id <= media->entities_count; id++) { + struct media_entity *entity = &media->entities[id - 1]; + struct media_links_enum links; + unsigned int i; + + links.entity = entity->info.id; + links.pads = malloc(entity->info.pads * sizeof(struct media_pad_desc)); + links.links = malloc(entity->info.links * sizeof(struct media_link_desc)); + + if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) { + printf("%s: Unable to enumerate pads and links (%s).\n", + __func__, strerror(errno)); + free(links.pads); + free(links.links); + return -errno; + } + + for (i = 0; i < entity->info.pads; ++i) { + entity->pads[i].entity = entity; + entity->pads[i].index = links.pads[i].index; + entity->pads[i].flags = links.pads[i].flags; + } + + for (i = 0; i < entity->info.links; ++i) { + struct media_link_desc *link = &links.links[i]; + struct media_entity_link *fwdlink; + struct media_entity_link *backlink; + struct media_entity *source; + struct media_entity *sink; + + source = media_get_entity_by_id(media, link->source.entity); + sink = media_get_entity_by_id(media, link->sink.entity); + + if (source == NULL || sink == NULL) { + printf("WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n", + id, i, link->source.entity, link->source.index, + link->sink.entity, link->sink.index); + ret = -EINVAL; + } + + fwdlink = media_entity_add_link(source); + fwdlink->source = &source->pads[link->source.index]; + fwdlink->sink = &sink->pads[link->sink.index]; + fwdlink->flags = links.links[i].flags; + + backlink = media_entity_add_link(sink); + backlink->source = &source->pads[link->source.index]; + backlink->sink = &sink->pads[link->sink.index]; + backlink->flags = links.links[i].flags; + + fwdlink->reverse = backlink; + backlink->reverse = fwdlink; + } + + free(links.pads); + free(links.links); + } + + return ret; +} + +static int media_enum_entities(struct media_device *media) +{ + struct media_entity *entity; + struct stat devstat; + unsigned int size; + char devname[32]; + char sysname[32]; + char target[1024]; + char *p; + __u32 id; + int ret; + + for (id = 0; ; id = entity->info.id) { + size = (media->entities_count + 1) * sizeof(*media->entities); + media->entities = realloc(media->entities, size); + + entity = &media->entities[media->entities_count]; + memset(entity, 0, sizeof(*entity)); + entity->fd = -1; + entity->info.id = id | MEDIA_ENT_ID_FLAG_NEXT; + + ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info); + if (ret < 0) { + if (errno == EINVAL) + break; + return -errno; + } + + /* Number of links (for outbound links) plus number of pads (for + * inbound links) is a good safe initial estimate of the total + * number of links. + */ + entity->max_links = entity->info.pads + entity->info.links; + + entity->pads = malloc(entity->info.pads * sizeof(*entity->pads)); + entity->links = malloc(entity->max_links * sizeof(*entity->links)); + if (entity->pads == NULL || entity->links == NULL) + return -ENOMEM; + + media->entities_count++; + + /* Find the corresponding device name. */ + if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE && + media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV) + continue; + + sprintf(sysname, "/sys/dev/char/%u:%u", entity->info.v4l.major, + entity->info.v4l.minor); + ret = readlink(sysname, target, sizeof(target)); + if (ret < 0) + continue; + + target[ret] = '\0'; + p = strrchr(target, '/'); + if (p == NULL) + continue; + + sprintf(devname, "/dev/%s", p + 1); + ret = stat(devname, &devstat); + if (ret < 0) + continue; + + /* Sanity check: udev might have reordered the device nodes. + * Make sure the major/minor match. We should really use + * libudev. + */ + if (major(devstat.st_rdev) == entity->info.v4l.major && + minor(devstat.st_rdev) == entity->info.v4l.minor) + strcpy(entity->devname, devname); + } + + return 0; +} + +struct media_device *media_open(const char *name, int verbose) +{ + struct media_device *media; + int ret; + + media = malloc(sizeof(*media)); + if (media == NULL) { + printf("%s: unable to allocate memory\n", __func__); + return NULL; + } + memset(media, 0, sizeof(*media)); + + if (verbose) + printf("Opening media device %s\n", name); + media->fd = open(name, O_RDWR); + if (media->fd < 0) { + media_close(media); + printf("%s: Can't open media device %s\n", __func__, name); + return NULL; + } + + if (verbose) + printf("Enumerating entities\n"); + + ret = media_enum_entities(media); + if (ret < 0) { + printf("%s: Unable to enumerate entities for device %s (%s)\n", + __func__, name, strerror(-ret)); + media_close(media); + return NULL; + } + + if (verbose) { + printf("Found %u entities\n", media->entities_count); + printf("Enumerating pads and links\n"); + } + + ret = media_enum_links(media); + if (ret < 0) { + printf("%s: Unable to enumerate pads and linksfor device %s\n", + __func__, name); + media_close(media); + return NULL; + } + + return media; +} + +void media_close(struct media_device *media) +{ + unsigned int i; + + if (media->fd != -1) + close(media->fd); + + for (i = 0; i < media->entities_count; ++i) { + struct media_entity *entity = &media->entities[i]; + + free(entity->pads); + free(entity->links); + if (entity->fd != -1) + close(entity->fd); + } + + free(media->entities); + free(media); +} diff --git a/isp/media.h b/isp/media.h new file mode 100644 index 0000000..7940915 --- /dev/null +++ b/isp/media.h @@ -0,0 +1,156 @@ +/* + * OMAP3 ISP library - Media controller + * + * Copyright (C) 2010-2011 Ideas on board SPRL + * + * Contact: Laurent Pinchart + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at + * your option) any later version. + * + * This library 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 Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __MEDIA_H__ +#define __MEDIA_H__ + +#include + +struct media_entity_link { + struct media_entity_pad *source; + struct media_entity_pad *sink; + struct media_entity_link *reverse; + __u32 flags; +}; + +struct media_entity_pad { + struct media_entity *entity; + __u32 index; + __u32 flags; +}; + +struct media_entity { + struct media_entity_desc info; + struct media_entity_pad *pads; + struct media_entity_link *links; + unsigned int max_links; + unsigned int num_links; + + char devname[32]; + int fd; +}; + +struct media_device { + int fd; + struct media_entity *entities; + unsigned int entities_count; +}; + +/* + * media_open - Open a media device + * @name: Name (including path) of the device node + * @verbose: Whether to print verbose information on the standard output + * + * Open the media device referenced by @name and enumerate entities, pads and + * links. + * + * Return a pointer to a newly allocated media_device structure instance on + * success and NULL on failure. The returned pointer must be freed with + * media_close when the device isn't needed anymore. + */ +struct media_device *media_open(const char *name, int verbose); + +/* + * media_close - Close a media device + * @media: Device instance + * + * Close the @media device instance and free allocated resources. Access to the + * device instance is forbidden after this function returns. + */ +void media_close(struct media_device *media); + +/* + * media_entity_remote_source - Locate the pad at the other end of a link + * @pad: Sink pad at one end of the link + * + * Locate the source pad connected to @pad through an enabled link. As only one + * link connected to a sink pad can be enabled at a time, the connected source + * pad is guaranteed to be unique. + * + * Return a pointer to the connected source pad, or NULL if all links connected + * to @pad are disabled. + */ +struct media_entity_pad *media_entity_remote_source(struct media_entity_pad *pad); + +/* + * media_entity_type - Get the type of an entity + * @entity: The entity + * + * Return the type of @entity. + */ +static inline unsigned int media_entity_type(struct media_entity *entity) +{ + return entity->info.type & MEDIA_ENT_TYPE_MASK; +} + +/* + * media_get_entity_by_name - Find an entity by its name + * @media: Media device + * @name: Entity name + * + * Search for an entity with a name equal to @name. Return a pointer to the + * entity if found, or NULL otherwise. + */ +struct media_entity *media_get_entity_by_name(struct media_device *media, + const char *name); + +/* + * media_get_entity_by_id - Find an entity by its ID + * @media: Media device + * @id: Entity ID + * + * Search for an entity with an ID equal to @id. Return a pointer to the entity + * if found, or NULL otherwise. + */ +struct media_entity *media_get_entity_by_id(struct media_device *media, + __u32 id); + +/* + * media_setup_link - Configure a link + * @media: Media device + * @source: Source pad at the link origin + * @sink: Sink pad at the link target + * @flags: Configuration flags + * + * Locate the link between @source and @sink, and configure it by applying the + * new @flags. + * + * Only the MEDIA_LINK_FLAG_ENABLED flag is writable. + * + * Return 0 on success, or a negative error code on failure. + */ +int media_setup_link(struct media_device *media, + struct media_entity_pad *source, struct media_entity_pad *sink, + __u32 flags); + +/* + * media_reset_links - Reset all links to the disabled state. + * @media: Media device + * + * Disable all links in the media device. This function is usually used after + * opening a media device to reset all links to a known state. + * + * Return 0 on success, or a negative error code on failure. + */ +int media_reset_links(struct media_device *media); + +#endif diff --git a/isp/omap3isp-priv.h b/isp/omap3isp-priv.h new file mode 100644 index 0000000..fe03d96 --- /dev/null +++ b/isp/omap3isp-priv.h @@ -0,0 +1,68 @@ +/* + * OMAP3 ISP library - OMAP3 ISP private header + * + * Copyright (C) 2010-2011 Ideas on board SPRL + * + * Contact: Laurent Pinchart + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at + * your option) any later version. + * + * This library 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 Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __OMAP3ISP_PRIV_H +#define __OMAP3ISP_PRIV_H + +#include "omap3isp.h" + +/* + * struct omap3_isp_video - OMAP3 video device + * @subdev: Sub-device from which video is captured for this device + * @node: Output video node entity + * @video: V4L2 capture device + * @format: Current video format + * @pool: Buffers pool + * @scaler: Whether scaling should be performed on the ISP or the sensor + * @dequeued: Bitmask of dequeued buffers that belong to the application + * @queued: Number of buffers queued to the driver + * @running: Whether video capture is running on the device + */ +struct omap3_isp_video { + struct media_entity *subdev; + struct media_entity *node; + struct v4l2_device *video; + struct v4l2_mbus_framefmt format; + struct v4l2_buffers_pool *pool; + enum omap3_isp_scaler scaler; + unsigned int dequeued; + unsigned int queued; + bool running; +}; + +struct omap3_isp_device { + struct media_device *mdev; + + struct media_entity *ccdc; + struct media_entity *preview; + struct media_entity *resizer; + struct media_entity *sensor; + + struct v4l2_mbus_framefmt sensor_format; + + struct omap3_isp_video viewfinder; + struct omap3_isp_video snapshot; + + const struct omap3_isp_operations *ops; +}; + +#endif diff --git a/isp/omap3isp.c b/isp/omap3isp.c new file mode 100644 index 0000000..19671c7 --- /dev/null +++ b/isp/omap3isp.c @@ -0,0 +1,750 @@ +/* + * OMAP3 ISP library - OMAP3 ISP + * + * Copyright (C) 2010-2011 Ideas on board SPRL + * + * Contact: Laurent Pinchart + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at + * your option) any later version. + * + * This library 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 Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include + +#include "controls.h" +#include "omap3isp.h" +#include "omap3isp-priv.h" +#include "subdev.h" + +#define ENTITY_CCDC "OMAP3 ISP CCDC" +#define ENTITY_PREVIEW "OMAP3 ISP preview" +#define ENTITY_RESIZER "OMAP3 ISP resizer" + +/* ----------------------------------------------------------------------------- + * Helper functions + */ + +static int setup_link(struct omap3_isp_device *isp, struct media_entity *source, + struct media_entity *sink, __u32 flags) +{ + unsigned int i; + int ret; + + for (i = 0; i < source->num_links; ++i) { + if (source->links[i].source->entity == source && + source->links[i].sink->entity == sink) + break; + } + + if (i == source->num_links) + return -ENOENT; + + ret = media_setup_link(isp->mdev, source->links[i].source, + source->links[i].sink, flags); + if (ret < 0) + printf("error: unable to %s %s -> %s link.\n", + flags & MEDIA_LNK_FL_ENABLED ? "enable" : "disable", + source->info.name, sink->info.name); + + return ret; +} + +static struct media_entity_link *entity_output_link(struct media_entity *entity) +{ + unsigned int i; + + for (i = 0; i < entity->num_links; ++i) { + if (entity->links[i].source->entity == entity && + entity->links[i].flags & MEDIA_LNK_FL_ENABLED) + return &entity->links[i]; + } + + return NULL; +} + +static struct media_entity *entity_output_node(struct media_entity *entity) +{ + struct media_entity *node; + unsigned int i; + + for (i = 0; i < entity->num_links; ++i) { + node = entity->links[i].sink->entity; + if (media_entity_type(node) == MEDIA_ENT_T_DEVNODE) + break; + } + + if (i == entity->num_links) { + printf("error: unable to locate %s output video node.\n", + entity->info.name); + return NULL; + } + + return node; +} + +static __u32 mbus_to_pix(enum v4l2_mbus_pixelcode code) +{ + switch (code) { + case V4L2_MBUS_FMT_SBGGR10_1X10: + return V4L2_PIX_FMT_SBGGR10; + case V4L2_MBUS_FMT_SGBRG10_1X10: + return V4L2_PIX_FMT_SGBRG10; + case V4L2_MBUS_FMT_SGRBG10_1X10: + return V4L2_PIX_FMT_SGRBG10; + case V4L2_MBUS_FMT_SRGGB10_1X10: + return V4L2_PIX_FMT_SRGGB10; + case V4L2_MBUS_FMT_UYVY8_1X16: + return V4L2_PIX_FMT_UYVY; + case V4L2_MBUS_FMT_YUYV8_1X16: + return V4L2_PIX_FMT_YUYV; + default: + return 0; + } +} + +/* ----------------------------------------------------------------------------- + * Open/close + */ + +struct omap3_isp_device *omap3_isp_open(const char *devname, + const struct omap3_isp_operations *ops) +{ + struct omap3_isp_device *isp; + struct media_entity *entity; + unsigned int i; + int ret; + + isp = malloc(sizeof *isp); + if (isp == NULL) + return NULL; + + memset(isp, 0, sizeof *isp); + isp->ops = ops; + + /* Open the media device and reset all links to make sure we're in a + * consistent, known state. + */ + isp->mdev = media_open(devname, 0); + if (isp->mdev == NULL) { + printf("error: unable to open media device %s\n", devname); + goto error; + } + + ret = media_reset_links(isp->mdev); + if (ret < 0) { + printf("error: unable to reset links.\n"); + goto error; + } + + /* Locate the entities that will be used in the pipelines. OMAP3 ISP + * modules are looked up by name. + */ + isp->ccdc = media_get_entity_by_name(isp->mdev, ENTITY_CCDC); + isp->preview = media_get_entity_by_name(isp->mdev, ENTITY_PREVIEW); + isp->resizer = media_get_entity_by_name(isp->mdev, ENTITY_RESIZER); + + if (isp->ccdc == NULL || isp->preview == NULL || isp->resizer == NULL) { + printf("error: unable to locate one or more ISP entities.\n"); + goto error; + } + + /* The sensor and video nodes are located by following links. */ + for (i = 0; i < isp->ccdc->num_links; ++i) { + entity = isp->ccdc->links[i].source->entity; + if (media_entity_type(entity) == MEDIA_ENT_T_V4L2_SUBDEV && + entity->info.pads == 1) + break; + } + + if (i == isp->ccdc->num_links) { + printf("error: unable to locate sensor.\n"); + goto error; + } + + isp->sensor = entity; + + isp->viewfinder.scaler = OMAP3_ISP_SCALER_ISP; + isp->viewfinder.subdev = isp->resizer; + isp->viewfinder.node = entity_output_node(isp->resizer); + if (isp->viewfinder.node == NULL) + goto error; + + /* Retrieve the sensor default format. */ + ret = v4l2_subdev_get_format(isp->sensor, &isp->sensor_format, 0, + V4L2_SUBDEV_FORMAT_TRY); + if (ret < 0) { + printf("error: unable to get sensor default format.\n"); + goto error; + } + + /* Setup the viewfinder pipeline. */ + ret = setup_link(isp, isp->sensor, isp->ccdc, MEDIA_LNK_FL_ENABLED); + if (ret < 0) + goto error; + + ret = setup_link(isp, isp->ccdc, isp->preview, MEDIA_LNK_FL_ENABLED); + if (ret < 0) + goto error; + + ret = setup_link(isp, isp->preview, isp->resizer, MEDIA_LNK_FL_ENABLED); + if (ret < 0) + goto error; + + ret = setup_link(isp, isp->resizer, isp->viewfinder.node, + MEDIA_LNK_FL_ENABLED); + if (ret < 0) + goto error; + + ret = omap3_isp_preview_setup(isp); + if (ret < 0) { + printf("error: unable to setup preview engine.\n"); + goto error; + } + + return isp; + +error: + omap3_isp_close(isp); + return NULL; +} + +void omap3_isp_close(struct omap3_isp_device *isp) +{ + if (isp == NULL) + return; + + media_close(isp->mdev); + free(isp); +} + +/* ----------------------------------------------------------------------------- + * Pipeline management + */ + +static int omap3_isp_pipeline_set_format(struct omap3_isp_device *isp, + struct v4l2_mbus_framefmt *ofmt, + enum omap3_isp_scaler scaler, + enum v4l2_subdev_format_whence which) +{ + struct v4l2_mbus_framefmt format; + struct media_entity_link *link; + struct media_entity_pad *pad; + struct media_entity *entity; + int ret; + + /* Configure formats. Start from the sensor output and propagate the + * format through the pipeline. + */ + + /* When scaling on the ISP, select the sensor default output format. + * Otherwise scale as much as possible on the sensor. + */ + if (scaler == OMAP3_ISP_SCALER_ISP) + format = isp->sensor_format; + else + format = *ofmt; + + ret = v4l2_subdev_set_format(isp->sensor, &format, 0, which); + if (ret < 0) { + printf("error: get format on sensor output failed.\n"); + return ret; + } + + for (entity = isp->sensor; ; ) { + + link = entity_output_link(entity); + if (link == NULL) + break; + + entity = link->sink->entity; + if (media_entity_type(entity) == MEDIA_ENT_T_DEVNODE) + break; + + pad = link->source; + ret = v4l2_subdev_get_format(pad->entity, &format, pad->index, which); + if (ret < 0) { + printf("error: get format failed on %s:%u.\n", + pad->entity->info.name, pad->index); + return ret; + } + + /* Try to force the output format code onto the output pad. */ + format.code = ofmt->code; + ret = v4l2_subdev_set_format(pad->entity, &format, pad->index, which); + if (ret < 0) { + printf("error: set format failed on %s:%u.\n", + pad->entity->info.name, pad->index); + return ret; + } + + pad = link->sink; + ret = v4l2_subdev_set_format(pad->entity, &format, pad->index, which); + if (ret < 0) { + printf("error: set format failed on %s:%u.\n", + pad->entity->info.name, pad->index); + return ret; + } + } + + if (ofmt == NULL) + return 0; + + pad = link->source; + ret = v4l2_subdev_set_format(pad->entity, ofmt, pad->index, which); + if (ret < 0) { + printf("error: set format failed on %s:%u.\n", + pad->entity->info.name, pad->index); + return ret; + } + + return 0; +} + +/* ----------------------------------------------------------------------------- + * Viewfinder + */ + +static int omap3_isp_viewfinder_setup_pipeline(struct omap3_isp_device *isp, + struct v4l2_mbus_framefmt *ofmt) +{ + int ret; + + /* Configure the formats on the pipeline. */ + ret = omap3_isp_pipeline_set_format(isp, ofmt, isp->viewfinder.scaler, + V4L2_SUBDEV_FORMAT_ACTIVE); + if (ret < 0) { + printf("error: unable to configure formats on pipeline.\n"); + return ret; + } + + return 0; +} + +int omap3_isp_viewfinder_setup(struct omap3_isp_device *isp, + struct v4l2_mbus_framefmt *ofmt) +{ + struct v4l2_pix_format format; + int ret; + + /* Setup the pipeline. */ + ret = omap3_isp_viewfinder_setup_pipeline(isp, ofmt); + if (ret < 0) + return ret; + + /* Open the V4L2 device. */ + isp->viewfinder.video = v4l2_open(isp->viewfinder.node->devname); + if (isp->viewfinder.video == NULL) { + printf("error: unable to open video capture device %s\n", + isp->viewfinder.node->devname); + return -ENXIO; + } + + /* Set the capture format on the output video node. */ + memset(&format, 0, sizeof format); + format.pixelformat = mbus_to_pix(ofmt->code); + format.width = ofmt->width; + format.height = ofmt->height; + + ret = v4l2_set_format(isp->viewfinder.video, &format); + if (ret < 0) + return ret; + + isp->viewfinder.format = *ofmt; + + return 0; +} + +int omap3_isp_viewfinder_set_pool(struct omap3_isp_device *isp, + struct v4l2_buffers_pool *pool) +{ + int ret; + + /* Allocate video buffers. */ + ret = v4l2_alloc_buffers(isp->viewfinder.video, pool, V4L2_MEMORY_USERPTR); + if (ret < 0) { + printf("error: unable to allocate buffers for viewfinder.\n"); + return ret; + } + + isp->viewfinder.dequeued = 0; + isp->viewfinder.queued = 0; + return 0; +} + +int omap3_isp_viewfinder_set_scaler(struct omap3_isp_device *isp, + enum omap3_isp_scaler scaler) +{ + struct v4l2_mbus_framefmt format; + int ret; + + if (isp->viewfinder.scaler == scaler) + return 0; + + isp->viewfinder.scaler = scaler; + + /* If omap3_isp_viewfinder_setup() hasn't been called yet retur now. */ + if (isp->viewfinder.format.width == 0 || + isp->viewfinder.format.height == 0) + return 0; + + format = isp->viewfinder.format; + ret = omap3_isp_viewfinder_setup_pipeline(isp, &format); + if (ret < 0) + return ret; + + return 0; +} + +static void omap3_isp_viewfinder_event(void *priv) +{ + struct omap3_isp_device *isp = priv; + struct v4l2_video_buffer buffer; + int ret; + + /* Dequeue the buffer */ + ret = v4l2_dequeue_buffer(isp->viewfinder.video, &buffer); + if (ret < 0) { + printf("error: unable to dequeue buffer: %s (%d)\n", + strerror(-ret), ret); + return; + } + + isp->viewfinder.dequeued |= 1 << buffer.index; + isp->viewfinder.queued--; + + if (isp->viewfinder.queued == 0) + isp->ops->unwatch_fd(isp->viewfinder.video->fd); + + isp->ops->viewfinder_ready(isp, &buffer); +} + +int omap3_isp_viewfinder_start(struct omap3_isp_device *isp) +{ + struct v4l2_video_buffer buffer; + unsigned int i; + int ret; + + /* Queue all buffers for video capture. */ + for (i = 0; i < isp->viewfinder.video->nbufs; ++i) { + if (isp->viewfinder.dequeued & (1 << i)) + continue; + + buffer.index = i; + + ret = v4l2_queue_buffer(isp->viewfinder.video, &buffer); + if (ret < 0) { + printf("error: unable to queue buffer %u (%d)\n", i, ret); + return -errno; + } + + isp->viewfinder.queued++; + } + + if (isp->ops->prepare_streamon) + isp->ops->prepare_streamon(isp); + + /* Watch the viewfinder file descriptor. */ + isp->ops->watch_fd(isp->viewfinder.video->fd, OMAP3_ISP_EVENT_READ, + omap3_isp_viewfinder_event, isp); + + ret = v4l2_stream_on(isp->viewfinder.video); + if (ret < 0) { + printf("error: streamon failed for viewfinder\n"); + return ret; + } + + isp->viewfinder.running = true; + return 0; +} + + +int omap3_isp_viewfinder_stop(struct omap3_isp_device *isp) +{ + int ret; + + isp->ops->unwatch_fd(isp->viewfinder.video->fd); + + ret = v4l2_stream_off(isp->viewfinder.video); + if (ret < 0) { + printf("error: streamoff failed for viewfinder\n"); + return ret; + } + + isp->viewfinder.queued = 0; + isp->viewfinder.running = false; + return 0; +} + +int omap3_isp_viewfinder_put_buffer(struct omap3_isp_device *isp, + struct v4l2_video_buffer *buffer) +{ + isp->viewfinder.dequeued &= ~(1 << buffer->index); + + if (!isp->viewfinder.running) + return 0; + + if (isp->viewfinder.queued == 0) + isp->ops->watch_fd(isp->viewfinder.video->fd, + OMAP3_ISP_EVENT_READ, + omap3_isp_viewfinder_event, isp); + + isp->viewfinder.queued++; + return v4l2_queue_buffer(isp->viewfinder.video, buffer); +} + +/* ----------------------------------------------------------------------------- + * Snapshot + * + * Snapshot capture is optional. Applications using snapshot capture must call + * omap3_isp_snapshot_setup() before starting the viewfinder. + */ + +static int omap3_isp_snapshot_restore_pipeline(struct omap3_isp_device *isp) +{ + int ret; + + ret = setup_link(isp, isp->snapshot.subdev, isp->snapshot.node, 0); + if (ret < 0) + return ret; + + if (isp->snapshot.format.code == V4L2_MBUS_FMT_UYVY8_1X16 || + isp->snapshot.format.code == V4L2_MBUS_FMT_YUYV8_1X16) + ret = setup_link(isp, isp->viewfinder.subdev, isp->viewfinder.node, + MEDIA_LNK_FL_ENABLED); + else + ret = setup_link(isp, isp->ccdc, isp->preview, MEDIA_LNK_FL_ENABLED); + if (ret < 0) + return ret; + + return 0; +} + +static int omap3_isp_snapshot_setup_pipeline(struct omap3_isp_device *isp, + struct v4l2_mbus_framefmt *ofmt, + enum v4l2_subdev_format_whence which) +{ + int ret; + + /* Setup the links. */ + if (ofmt->code == V4L2_MBUS_FMT_UYVY8_1X16 || + ofmt->code == V4L2_MBUS_FMT_YUYV8_1X16) + ret = setup_link(isp, isp->viewfinder.subdev, isp->viewfinder.node, 0); + else + ret = setup_link(isp, isp->ccdc, isp->preview, 0); + if (ret < 0) + return ret; + + ret = setup_link(isp, isp->snapshot.subdev, isp->snapshot.node, + MEDIA_LNK_FL_ENABLED); + if (ret < 0) + return ret; + + /* Configure the formats on the pipeline. */ + ret = omap3_isp_pipeline_set_format(isp, ofmt, isp->snapshot.scaler, which); + if (ret < 0) { + printf("error: unable to configure formats on pipeline.\n"); + return ret; + } + + return 0; +} + +int omap3_isp_snapshot_setup(struct omap3_isp_device *isp, + struct v4l2_mbus_framefmt *ofmt) +{ + struct v4l2_video_buffer buffer; + struct v4l2_pix_format format; + struct media_entity *entity; + unsigned int i; + int ret; + + /* Locate the entity at the end of the pipeline. Use the CCDC for raw + * capture and the resizer for YUV capture. + */ + switch (ofmt->code) { + case V4L2_MBUS_FMT_UYVY8_1X16: + case V4L2_MBUS_FMT_YUYV8_1X16: + entity = isp->resizer; + break; + default: + entity = isp->ccdc; + break; + } + + isp->snapshot.scaler = OMAP3_ISP_SCALER_ISP; + isp->snapshot.subdev = entity; + isp->snapshot.node = entity_output_node(entity); + if (isp->snapshot.node == NULL) + return -ENOENT; + + /* Try the format. */ + ret = omap3_isp_snapshot_setup_pipeline(isp, ofmt, V4L2_SUBDEV_FORMAT_TRY); + if (ret < 0) + return ret; + + isp->snapshot.format = *ofmt; + + ret = omap3_isp_snapshot_restore_pipeline(isp); + if (ret < 0) + return ret; + + /* Open the V4L2 device. */ + isp->snapshot.video = v4l2_open(isp->snapshot.node->devname); + if (isp->snapshot.video == NULL) { + printf("error: unable to open snaphost capture device %s\n", + isp->snapshot.node->devname); + return -ENODEV; + } + + /* Set the capture format on the output video node. */ + memset(&format, 0, sizeof format); + format.pixelformat = mbus_to_pix(ofmt->code); + format.width = ofmt->width; + format.height = ofmt->height; + + ret = v4l2_set_format(isp->snapshot.video, &format); + if (ret < 0) + return ret; + + /* Pre-allocate capture buffers. */ + isp->snapshot.pool = v4l2_buffers_pool_new(2); + if (isp->snapshot.pool == NULL) { + printf("error: unable to allocate buffers pool for snapshot.\n"); + return -ENOMEM; + } + + ret = v4l2_alloc_buffers(isp->snapshot.video, isp->snapshot.pool, + V4L2_MEMORY_MMAP); + if (ret < 0) { + printf("error: unable to allocate buffers for snapshot.\n"); + return ret; + } + + /* Queue all buffers. */ + for (i = 0; i < isp->snapshot.video->nbufs; ++i) { + buffer.index = i; + + ret = v4l2_queue_buffer(isp->snapshot.video, &buffer); + if (ret < 0) { + printf("error: unable to queue buffer %u\n", i); + return -errno; + } + } + + return 0; +} + +static void omap3_isp_snapshot_event(void *priv) +{ + struct omap3_isp_device *isp = priv; + struct v4l2_mbus_framefmt format; + struct v4l2_video_buffer buffer; + unsigned int i; + int ret; + + /* Dequeue a buffer, stop the stream and fire the snapshot event. */ + ret = v4l2_dequeue_buffer(isp->snapshot.video, &buffer); + if (ret < 0) { + printf("error: unable to dequeue snapshot buffer.\n"); + return; + } + + isp->ops->unwatch_fd(isp->snapshot.video->fd); + + ret = v4l2_stream_off(isp->snapshot.video); + if (ret < 0) { + printf("error: streamoff failed for snapshot\n"); + return; + } + + isp->snapshot.running = false; + + isp->ops->snapshot_ready(isp, &buffer); + + /* Resume the viewfinder. */ + ret = omap3_isp_snapshot_restore_pipeline(isp); + if (ret < 0) + return; + + format = isp->viewfinder.format; + ret = omap3_isp_viewfinder_setup_pipeline(isp, &format); + if (ret < 0) + return; + + ret = omap3_isp_viewfinder_start(isp); + if (ret < 0) { + printf("error: unable to resume viewfinder.\n"); + return; + } + + /* Queue all buffers for the next snapshot. */ + for (i = 0; i < isp->snapshot.video->nbufs; ++i) { + buffer.index = i; + + ret = v4l2_queue_buffer(isp->snapshot.video, &buffer); + if (ret < 0) { + printf("error: unable to queue buffer %u\n", i); + return; + } + } +} + +int omap3_isp_snapshot_capture(struct omap3_isp_device *isp) +{ + struct v4l2_mbus_framefmt format; + int ret; + + /* Suspend the viewfinder. */ + ret = omap3_isp_viewfinder_stop(isp); + if (ret < 0) { + printf("error: unable to suspend viewfinder.\n"); + return ret; + } + + /* Configure the pipeline. */ + format = isp->snapshot.format; + ret = omap3_isp_snapshot_setup_pipeline(isp, &format, V4L2_SUBDEV_FORMAT_ACTIVE); + if (ret < 0) { + printf("error: unable to setup snapshot pipeline.\n"); + return ret; + } + + if (isp->ops->prepare_streamon) + isp->ops->prepare_streamon(isp); + + /* Watch the snapshot file descriptor. */ + isp->ops->watch_fd(isp->snapshot.video->fd, OMAP3_ISP_EVENT_READ, + omap3_isp_snapshot_event, isp); + + ret = v4l2_stream_on(isp->snapshot.video); + if (ret < 0) { + printf("error: streamon failed for snapshot\n"); + return ret; + } + + isp->snapshot.running = true; + return 0; +} + +int omap3_isp_snapshot_put_buffer(struct omap3_isp_device *isp __attribute__((__unused__)), + struct v4l2_video_buffer *buffer __attribute__((__unused__))) +{ + /* No-op, the stream is already stopped so we don't need to requeue the + * buffer. + */ + return 0; +} diff --git a/isp/omap3isp.h b/isp/omap3isp.h new file mode 100644 index 0000000..6427efc --- /dev/null +++ b/isp/omap3isp.h @@ -0,0 +1,80 @@ +/* + * OMAP3 ISP library - OMAP3 ISP + * + * Copyright (C) 2010-2011 Ideas on board SPRL + * + * Contact: Laurent Pinchart + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at + * your option) any later version. + * + * This library 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 Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __OMAP3ISP_H +#define __OMAP3ISP_H + +#include +#include +#include + +#include "media.h" +#include "v4l2.h" + +struct omap3_isp_device; + +enum omap3_isp_scaler { + OMAP3_ISP_SCALER_ISP = 0, + OMAP3_ISP_SCALER_SENSOR = 1, +}; + +enum omap3_isp_event_type { + OMAP3_ISP_EVENT_READ = 1, + OMAP3_ISP_EVENT_WRITE = 2, +}; + +struct omap3_isp_operations { + void (*prepare_streamon)(struct omap3_isp_device *isp); + void (*viewfinder_ready)(struct omap3_isp_device *isp, + struct v4l2_video_buffer *buffer); + void (*snapshot_ready)(struct omap3_isp_device *isp, + struct v4l2_video_buffer *buffer); + void (*watch_fd)(int fd, enum omap3_isp_event_type type, + void(*callback)(void *priv), void *priv); + void (*unwatch_fd)(int fd); +}; + +struct omap3_isp_device *omap3_isp_open(const char *devname, + const struct omap3_isp_operations *ops); +void omap3_isp_close(struct omap3_isp_device *isp); + +int omap3_isp_viewfinder_setup(struct omap3_isp_device *isp, + struct v4l2_mbus_framefmt *ofmt); +int omap3_isp_viewfinder_set_pool(struct omap3_isp_device *isp, + struct v4l2_buffers_pool *pool); +int omap3_isp_viewfinder_set_scaler(struct omap3_isp_device *isp, + enum omap3_isp_scaler scaler); +int omap3_isp_viewfinder_start(struct omap3_isp_device *isp); +int omap3_isp_viewfinder_stop(struct omap3_isp_device *isp); +int omap3_isp_viewfinder_put_buffer(struct omap3_isp_device *isp, + struct v4l2_video_buffer *buffer); + +int omap3_isp_snapshot_setup(struct omap3_isp_device *isp, + struct v4l2_mbus_framefmt *ofmt); +int omap3_isp_snapshot_capture(struct omap3_isp_device *isp); +int omap3_isp_snapshot_put_buffer(struct omap3_isp_device *isp, + struct v4l2_video_buffer *buffer); + +int omap3_isp_preview_set_contrast(struct omap3_isp_device *isp, unsigned int value); +int omap3_isp_preview_set_saturation(struct omap3_isp_device *isp, float value); + +#endif diff --git a/isp/subdev.c b/isp/subdev.c new file mode 100644 index 0000000..b4894a2 --- /dev/null +++ b/isp/subdev.c @@ -0,0 +1,287 @@ +/* + * OMAP3 ISP library - V4L2 sub-devices + * + * Copyright (C) 2010-2011 Ideas on board SPRL + * + * Contact: Laurent Pinchart + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at + * your option) any later version. + * + * This library 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 Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include "media.h" +#include "subdev.h" +#include "tools.h" + +static struct { + const char *name; + enum v4l2_mbus_pixelcode code; +} mbus_formats[] = { + { "YUYV", V4L2_MBUS_FMT_YUYV8_1X16 }, + { "UYVY", V4L2_MBUS_FMT_UYVY8_1X16 }, + { "SGRBG10", V4L2_MBUS_FMT_SGRBG10_1X10 }, + { "SGRBG10_DPCM8", V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8 }, +}; + +static const char *pixelcode_to_string(enum v4l2_mbus_pixelcode code) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) { + if (mbus_formats[i].code == code) + return mbus_formats[i].name; + } + + return "unknown"; +} + +int v4l2_subdev_open(struct media_entity *entity) +{ + if (entity->fd != -1) + return 0; + + entity->fd = open(entity->devname, O_RDWR); + if (entity->fd == -1) { + printf("%s: Failed to open subdev device node %s\n", __func__, + entity->devname); + return -errno; + } + + return 0; +} + +void v4l2_subdev_close(struct media_entity *entity) +{ + close(entity->fd); +} + +int v4l2_subdev_get_format(struct media_entity *entity, + struct v4l2_mbus_framefmt *format, unsigned int pad, + enum v4l2_subdev_format_whence which) +{ + struct v4l2_subdev_format fmt; + int ret; + + ret = v4l2_subdev_open(entity); + if (ret < 0) + return ret; + + memset(&fmt, 0, sizeof(fmt)); + fmt.pad = pad; + fmt.which = which; + + ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FMT, &fmt); + if (ret < 0) + return -errno; + + *format = fmt.format; + return 0; +} + +int v4l2_subdev_set_format(struct media_entity *entity, + struct v4l2_mbus_framefmt *format, unsigned int pad, + enum v4l2_subdev_format_whence which) +{ + struct v4l2_subdev_format fmt; + int ret; + + ret = v4l2_subdev_open(entity); + if (ret < 0) + return ret; + + memset(&fmt, 0, sizeof(fmt)); + fmt.pad = pad; + fmt.which = which; + fmt.format = *format; + + ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FMT, &fmt); + if (ret < 0) + return -errno; + + *format = fmt.format; + return 0; +} + +int v4l2_subdev_get_crop(struct media_entity *entity, struct v4l2_rect *rect, + unsigned int pad, enum v4l2_subdev_format_whence which) +{ + struct v4l2_subdev_crop crop; + int ret; + + ret = v4l2_subdev_open(entity); + if (ret < 0) + return ret; + + memset(&crop, 0, sizeof(crop)); + crop.pad = pad; + crop.which = which; + + ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_CROP, &crop); + if (ret < 0) + return -errno; + + *rect = crop.rect; + return 0; +} + +int v4l2_subdev_set_crop(struct media_entity *entity, struct v4l2_rect *rect, + unsigned int pad, enum v4l2_subdev_format_whence which) +{ + struct v4l2_subdev_crop crop; + int ret; + + ret = v4l2_subdev_open(entity); + if (ret < 0) + return ret; + + memset(&crop, 0, sizeof(crop)); + crop.pad = pad; + crop.which = which; + crop.rect = *rect; + + ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_CROP, &crop); + if (ret < 0) + return -errno; + + *rect = crop.rect; + return 0; +} + +int v4l2_subdev_set_frame_interval(struct media_entity *entity, + struct v4l2_fract *interval) +{ + struct v4l2_subdev_frame_interval ival; + int ret; + + ret = v4l2_subdev_open(entity); + if (ret < 0) + return ret; + + memset(&ival, 0, sizeof(ival)); + ival.interval = *interval; + + ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FRAME_INTERVAL, &ival); + if (ret < 0) + return -errno; + + *interval = ival.interval; + return 0; +} + +/* ----------------------------------------------------------------------------- + * Controls + */ + +int v4l2_subdev_get_control(struct media_entity *entity, unsigned int id, + int32_t *value) +{ + struct v4l2_control ctrl; + int ret; + + ret = v4l2_subdev_open(entity); + if (ret < 0) + return ret; + + ctrl.id = id; + + ret = ioctl(entity->fd, VIDIOC_G_CTRL, &ctrl); + if (ret < 0) { + printf("unable to get control: %s (%d).\n", + strerror(errno), errno); + return -errno; + } + + *value = ctrl.value; + return 0; +} + +int v4l2_subdev_set_control(struct media_entity *entity, unsigned int id, + int32_t *value) +{ + struct v4l2_control ctrl; + int ret; + + ret = v4l2_subdev_open(entity); + if (ret < 0) + return ret; + + ctrl.id = id; + ctrl.value = *value; + + ret = ioctl(entity->fd, VIDIOC_S_CTRL, &ctrl); + if (ret < 0) { + printf("unable to set control: %s (%d).\n", + strerror(errno), errno); + return -errno; + } + + *value = ctrl.value; + return 0; +} + +int v4l2_subdev_get_controls(struct media_entity *entity, unsigned int count, + struct v4l2_ext_control *ctrls) +{ + struct v4l2_ext_controls controls; + int ret; + + ret = v4l2_subdev_open(entity); + if (ret < 0) + return ret; + + memset(&controls, 0, sizeof controls); + controls.count = count; + controls.controls = ctrls; + + ret = ioctl(entity->fd, VIDIOC_G_EXT_CTRLS, &controls); + if (ret < 0) + printf("unable to get multiple controls: %s (%d).\n", + strerror(errno), errno); + + return ret; +} + +int v4l2_subdev_set_controls(struct media_entity *entity, unsigned int count, + struct v4l2_ext_control *ctrls) +{ + struct v4l2_ext_controls controls; + int ret; + + ret = v4l2_subdev_open(entity); + if (ret < 0) + return ret; + + memset(&controls, 0, sizeof controls); + controls.count = count; + controls.controls = ctrls; + + ret = ioctl(entity->fd, VIDIOC_S_EXT_CTRLS, &controls); + if (ret < 0) + printf("unable to set multiple controls: %s (%d).\n", + strerror(errno), errno); + + return ret; +} diff --git a/isp/subdev.h b/isp/subdev.h new file mode 100644 index 0000000..5bbbe84 --- /dev/null +++ b/isp/subdev.h @@ -0,0 +1,201 @@ +/* + * OMAP3 ISP library - V4L2 sub-devices + * + * Copyright (C) 2010-2011 Ideas on board SPRL + * + * Contact: Laurent Pinchart + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at + * your option) any later version. + * + * This library 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 Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __SUBDEV_H__ +#define __SUBDEV_H__ + +#include + +struct media_entity; + +/* + * v4l2_subdev_open - Open a sub-device + * @entity: Sub-device media entity + * + * Open the V4L2 subdev device node associated with @entity. The file descriptor + * is stored in the media_entity structure. + * + * Return 0 on success, or a negative error code on failure. + */ +int v4l2_subdev_open(struct media_entity *entity); + +/* + * v4l2_subdev_close - Close a sub-device + * @entity: Sub-device media entity + * + * Close the V4L2 subdev device node associated with the @entity and opened by + * a previous call to v4l2_subdev_open() (either explicit or implicit). + */ +void v4l2_subdev_close(struct media_entity *entity); + +/* + * v4l2_subdev_get_format - Retrieve the format on a pad + * @entity: Subdev-device media entity + * @format: Format to be filled + * @pad: Pad number + * @which: Identifier of the format to get + * + * Retrieve the current format on the @entity @pad and store it in the @format + * structure. + * + * @which is set to V4L2_SUBDEV_FORMAT_TRY to retrieve the try format stored in + * the file handle, of V4L2_SUBDEV_FORMAT_ACTIVE to retrieve the current active + * format. + * + * Return 0 on success, or a negative error code on failure. + */ +int v4l2_subdev_get_format(struct media_entity *entity, + struct v4l2_mbus_framefmt *format, unsigned int pad, + enum v4l2_subdev_format_whence which); + +/* + * v4l2_subdev_set_format - Set the format on a pad + * @entity: Subdev-device media entity + * @format: Format + * @pad: Pad number + * @which: Identifier of the format to set + * + * Set the format on the @entity @pad to @format. The driver is allowed to + * modify the requested format, in which case @format is updated with the + * modifications. + * + * @which is set to V4L2_SUBDEV_FORMAT_TRY to set the try format stored in the + * file handle, of V4L2_SUBDEV_FORMAT_ACTIVE to configure the device with an + * active format. + * + * Return 0 on success, or a negative error code on failure. + */ +int v4l2_subdev_set_format(struct media_entity *entity, + struct v4l2_mbus_framefmt *format, unsigned int pad, + enum v4l2_subdev_format_whence which); + +/* + * v4l2_subdev_get_crop - Retrieve the crop rectangle on a pad + * @entity: Subdev-device media entity + * @rect: Crop rectangle to be filled + * @pad: Pad number + * @which: Identifier of the format to get + * + * Retrieve the current crop rectangleon the @entity @pad and store it in the + * @rect structure. + * + * @which is set to V4L2_SUBDEV_FORMAT_TRY to retrieve the try crop rectangle + * stored in the file handle, of V4L2_SUBDEV_FORMAT_ACTIVE to retrieve the + * current active crop rectangle. + * + * Return 0 on success, or a negative error code on failure. + */ +int v4l2_subdev_get_crop(struct media_entity *entity, struct v4l2_rect *rect, + unsigned int pad, enum v4l2_subdev_format_whence which); + +/* + * v4l2_subdev_set_crop - Set the crop rectangle on a pad + * @entity: Subdev-device media entity + * @crop: Crop rectangle + * @pad: Pad number + * @which: Identifier of the format to set + * + * Set the crop rectangle on the @entity @pad to @rect. The driver is allowed to + * modify the requested rectangle, in which case @rect is updated with the + * modifications. + * + * @which is set to V4L2_SUBDEV_FORMAT_TRY to set the try crop rectangle stored + * in the file handle, of V4L2_SUBDEV_FORMAT_ACTIVE to configure the device with + * an active crop rectangle. + * + * Return 0 on success, or a negative error code on failure. + */ +int v4l2_subdev_set_crop(struct media_entity *entity, struct v4l2_rect *rect, + unsigned int pad, enum v4l2_subdev_format_whence which); + +/* + * v4l2_subdev_set_frame_interval - Set the frame interval on a sub-device + * @entity: Subdev-device media entity + * @interval: Frame interval + * + * Set the frame interval on subdev @entity to @interval. The driver is allowed + * to modify the requested frame interval, in which case @interval is updated + * with the modifications. + * + * Frame interval setting is usually supported only on devices at the beginning + * of video pipelines, such as sensors. + * + * Return 0 on success, or a negative error code on failure. + */ +int v4l2_subdev_set_frame_interval(struct media_entity *entity, + struct v4l2_fract *interval); + +/* + * v4l2_subdev_get_control - Read the value of a control + * @entity: Subdev-device media entity + * @id: Control ID + * @value: Control value to be filled + * + * Retrieve the current value of control @id and store it in @value. + * + * Return 0 on success or a negative error code on failure. + */ +int v4l2_subdev_get_control(struct media_entity *entity, unsigned int id, + int32_t *value); + +/* + * v4l2_subdev_set_control - Write the value of a control + * @entity: Subdev-device media entity + * @id: Control ID + * @value: Control value + * + * Set control @id to @value. The device is allowed to modify the requested + * value, in which case @value is updated to the modified value. + * + * Return 0 on success or a negative error code on failure. + */ +int v4l2_subdev_set_control(struct media_entity *entity, unsigned int id, + int32_t *value); + +/* + * v4l2_subdev_get_controls - Read the value of multiple controls + * @entity: Subdev-device media entity + * @count: Number of controls + * @ctrls: Controls to be read + * + * Retrieve the current value of controls identified by @ctrls. + * + * Return 0 on success or a negative error code on failure. + */ +int v4l2_subdev_get_controls(struct media_entity *entity, unsigned int count, + struct v4l2_ext_control *ctrls); + +/* + * v4l2_subdev_set_controls - Write the value of multiple controls + * @entity: Subdev-device media entity + * @count: Number of controls + * @ctrls: Controls to be written + * + * Set controls identified by @ctrls. The device is allowed to modify the + * requested values, in which case @ctrls is updated to the modified value. + * + * Return 0 on success or a negative error code on failure. + */ +int v4l2_subdev_set_controls(struct media_entity *entity, unsigned int count, + struct v4l2_ext_control *ctrls); + +#endif diff --git a/isp/tools.h b/isp/tools.h new file mode 100644 index 0000000..b60cc2e --- /dev/null +++ b/isp/tools.h @@ -0,0 +1,34 @@ +/* + * OMAP3 ISP library - Tools + * + * Copyright (C) 2010-2011 Ideas on board SPRL + * + * Contact: Laurent Pinchart + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at + * your option) any later version. + * + * This library 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 Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __TOOLS_H__ +#define __TOOLS_H__ + +#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0])) + +#define min(a, b) ((a) < (b) ? (a) : (b)) +#define max(a, b) ((a) > (b) ? (a) : (b)) + +#define container_of(ptr, type, member) \ + (type *)((char *)(ptr) - offsetof(type, member)) + +#endif /* __TOOLS_H__ */ diff --git a/isp/v4l2-pool.c b/isp/v4l2-pool.c new file mode 100644 index 0000000..b0bf3b5 --- /dev/null +++ b/isp/v4l2-pool.c @@ -0,0 +1,80 @@ +/* + * OMAP3 ISP library - V4L2 buffers pool + * + * Copyright (C) 2010-2011 Ideas on board SPRL + * + * Contact: Laurent Pinchart + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at + * your option) any later version. + * + * This library 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 Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +#include "v4l2-pool.h" + +struct v4l2_buffers_pool *v4l2_buffers_pool_new(unsigned int nbufs) +{ + struct v4l2_video_buffer *buffers; + struct v4l2_buffers_pool *pool; + + pool = malloc(sizeof *pool); + if (pool == NULL) + return NULL; + + buffers = malloc(sizeof *buffers * nbufs); + if (buffers == NULL) { + free(pool); + return NULL; + } + + memset(buffers, 0, sizeof *buffers * nbufs); + + pool->nbufs = nbufs; + pool->buffers = buffers; + + return pool; +} + +void v4l2_buffers_pool_delete(struct v4l2_buffers_pool *pool) +{ + unsigned int i; + + for (i = 0; i < pool->nbufs; ++i) { + if (pool->buffers[i].allocated) + free(pool->buffers[i].mem); + } + + free(pool->buffers); + free(pool); +} + +int v4l2_buffers_pool_alloc(struct v4l2_buffers_pool *pool, size_t size, + size_t align) +{ + unsigned int i; + int ret; + + for (i = 0; i < pool->nbufs; ++i) { + ret = posix_memalign(&pool->buffers[i].mem, align, size); + if (ret != 0) + return -ret; + + pool->buffers[i].size = size; + pool->buffers[i].allocated = true; + } + + return 0; +} diff --git a/isp/v4l2-pool.h b/isp/v4l2-pool.h new file mode 100644 index 0000000..32f3b3b --- /dev/null +++ b/isp/v4l2-pool.h @@ -0,0 +1,87 @@ +/* + * OMAP3 ISP library - V4L2 buffers pool + * + * Copyright (C) 2010-2011 Ideas on board SPRL + * + * Contact: Laurent Pinchart + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at + * your option) any later version. + * + * This library 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 Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef __V4L2_POOL_H +#define __V4L2_POOL_H + +#include +#include + +/* + * struct v4l2_video_buffer - Video buffer information + * @index: Zero-based buffer index, limited to the number of buffers minus one + * @size: Size of the video memory, in bytes + * @bytesused: Number of bytes used by video data, smaller or equal to @size + * @timestamp: Time stamp at which the buffer has been captured + * @error: True if an error occured while capturing video data for the buffer + * @allocated: True if memory for the buffer has been allocated by the pool + * @mem: Video data memory + */ +struct v4l2_video_buffer +{ + unsigned int index; + unsigned int size; + unsigned int bytesused; + struct timeval timestamp; + bool error; + bool allocated; + void *mem; +}; + +struct v4l2_buffers_pool +{ + unsigned int nbufs; + struct v4l2_video_buffer *buffers; +}; + +/* + * v4l2_buffers_pool_new - Create a new buffers pool + * @nbufs: Number of buffers in the pool + * + * Allocate a new buffers pool with space for @nbufs buffers. Memory for the + * buffers is not allocated. + */ +struct v4l2_buffers_pool *v4l2_buffers_pool_new(unsigned int nbufs); + +/* + * v4l2_buffers_pool_delete - Delete a buffers pool + * @pool: Buffers pool + * + * Delete a buffers pool and free buffers memory if it has been allocated by + * the pool. + */ +void v4l2_buffers_pool_delete(struct v4l2_buffers_pool *pool); + +/* + * v4l2_buffers_pool_alloc - Allocate memory for buffers in a pool + * @pool: Buffers pool + * @size: Buffer size + * @align: Buffer memory alignment in bytes + * + * Allocate @size bytes of memory for each buffer aligned to a multiple of + * @align bytes. The alignment must be a power of 2. + * + * Return 0 on success or a negative error code on failure. + */ +int v4l2_buffers_pool_alloc(struct v4l2_buffers_pool *pool, size_t size, + size_t align); + +#endif diff --git a/isp/v4l2.c b/isp/v4l2.c new file mode 100644 index 0000000..75e90b1 --- /dev/null +++ b/isp/v4l2.c @@ -0,0 +1,717 @@ +/* + * OMAP3 ISP library - V4L2 devices + * + * Copyright (C) 2010-2011 Ideas on board SPRL + * + * Contact: Laurent Pinchart + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at + * your option) any later version. + * + * This library 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 Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "list.h" +#include "tools.h" +#include "v4l2.h" + +#ifndef V4L2_BUF_FLAG_ERROR +#define V4L2_BUF_FLAG_ERROR 0x0040 +#endif + +struct v4l2_ival_desc { + struct v4l2_fract min; + struct v4l2_fract max; + struct v4l2_fract step; + + struct list_entry list; +}; + +struct v4l2_frame_desc { + unsigned int min_width; + unsigned int min_height; + unsigned int max_width; + unsigned int max_height; + unsigned int step_width; + unsigned int step_height; + + struct list_entry list; + struct list_entry ivals; +}; + +struct v4l2_format_desc { + unsigned int pixelformat; + + struct list_entry list; + struct list_entry frames; +}; + +/* ----------------------------------------------------------------------------- + * Formats enumeration + */ + +static int +v4l2_enum_frame_intervals(struct v4l2_device *dev, struct v4l2_format_desc *format, + struct v4l2_frame_desc *frame) +{ + struct v4l2_ival_desc *ival; + unsigned int i; + int ret; + + for (i = 0; ; ++i) { + struct v4l2_frmivalenum ivalenum; + + memset(&ivalenum, 0, sizeof ivalenum); + ivalenum.index = i; + ivalenum.pixel_format = format->pixelformat; + ivalenum.width = frame->min_width; + ivalenum.height = frame->min_height; + ret = ioctl(dev->fd, VIDIOC_ENUM_FRAMEINTERVALS, &ivalenum); + if (ret < 0) + break; + + if (i != ivalenum.index) + printf("Warning: driver returned wrong ival index " + "%u.\n", ivalenum.index); + if (format->pixelformat != ivalenum.pixel_format) + printf("Warning: driver returned wrong ival pixel " + "format %08x.\n", ivalenum.pixel_format); + if (frame->min_width != ivalenum.width) + printf("Warning: driver returned wrong ival width " + "%u.\n", ivalenum.width); + if (frame->min_height != ivalenum.height) + printf("Warning: driver returned wrong ival height " + "%u.\n", ivalenum.height); + + ival = malloc(sizeof *ival); + if (ival == NULL) + return -ENOMEM; + + memset(ival, 0, sizeof *ival); + + switch (ivalenum.type) { + case V4L2_FRMIVAL_TYPE_DISCRETE: + ival->min = ivalenum.discrete; + ival->max = ivalenum.discrete; + ival->step.numerator = 1; + ival->step.denominator = 1; + break; + + case V4L2_FRMIVAL_TYPE_STEPWISE: + ival->min = ivalenum.stepwise.min; + ival->max = ivalenum.stepwise.max; + ival->step = ivalenum.stepwise.step; + break; + + default: + printf("Error: driver returned invalid frame ival " + "type %u\n", ivalenum.type); + return -EINVAL; + } + + list_append(&ival->list, &frame->ivals); + } + + return 0; +} + +static int +v4l2_enum_frame_sizes(struct v4l2_device *dev, struct v4l2_format_desc *format) +{ + struct v4l2_frame_desc *frame; + unsigned int i; + int ret; + + for (i = 0; ; ++i) { + struct v4l2_frmsizeenum frmenum; + + memset(&frmenum, 0, sizeof frmenum); + frmenum.index = i; + frmenum.pixel_format = format->pixelformat; + + ret = ioctl(dev->fd, VIDIOC_ENUM_FRAMESIZES, &frmenum); + if (ret < 0) + break; + + if (i != frmenum.index) + printf("Warning: driver returned wrong frame index " + "%u.\n", frmenum.index); + if (format->pixelformat != frmenum.pixel_format) + printf("Warning: driver returned wrong frame pixel " + "format %08x.\n", frmenum.pixel_format); + + frame = malloc(sizeof *frame); + if (frame == NULL) + return -ENOMEM; + + memset(frame, 0, sizeof *frame); + + list_init(&frame->ivals); + frame->step_width = 1; + frame->step_height = 1; + + switch (frmenum.type) { + case V4L2_FRMSIZE_TYPE_DISCRETE: + frame->min_width = frmenum.discrete.width; + frame->min_height = frmenum.discrete.height; + frame->max_width = frmenum.discrete.width; + frame->max_height = frmenum.discrete.height; + break; + + case V4L2_FRMSIZE_TYPE_STEPWISE: + frame->step_width = frmenum.stepwise.step_width; + frame->step_height = frmenum.stepwise.step_height; + case V4L2_FRMSIZE_TYPE_CONTINUOUS: + frame->min_width = frmenum.stepwise.min_width; + frame->min_height = frmenum.stepwise.min_height; + frame->max_width = frmenum.stepwise.max_width; + frame->max_height = frmenum.stepwise.max_height; + break; + + default: + printf("Error: driver returned invalid frame size " + "type %u\n", frmenum.type); + return -EINVAL; + } + + list_append(&frame->list, &format->frames); + + ret = v4l2_enum_frame_intervals(dev, format, frame); + if (ret < 0) + return ret; + } + + return 0; +} +static int v4l2_enum_formats(struct v4l2_device *dev) +{ + struct v4l2_format_desc *format; + unsigned int i; + int ret; + + for (i = 0; ; ++i) { + struct v4l2_fmtdesc fmtenum; + + memset(&fmtenum, 0, sizeof fmtenum); + fmtenum.index = i; + fmtenum.type = dev->type; + + ret = ioctl(dev->fd, VIDIOC_ENUM_FMT, &fmtenum); + if (ret < 0) + break; + + if (i != fmtenum.index) + printf("Warning: driver returned wrong format index " + "%u.\n", fmtenum.index); + if (dev->type != fmtenum.type) + printf("Warning: driver returned wrong format type " + "%u.\n", fmtenum.type); + + format = malloc(sizeof *format); + if (format == NULL) + return -ENOMEM; + + memset(format, 0, sizeof *format); + + list_init(&format->frames); + format->pixelformat = fmtenum.pixelformat; + + list_append(&format->list, &dev->formats); + + ret = v4l2_enum_frame_sizes(dev, format); + if (ret < 0) + return ret; + } + + return 0; +} + +/* ----------------------------------------------------------------------------- + * Open/close + */ + +struct v4l2_device *v4l2_open(const char *devname) +{ + struct v4l2_device *dev; + struct v4l2_capability cap; + int ret; + + dev = malloc(sizeof *dev); + if (dev == NULL) + return NULL; + + memset(dev, 0, sizeof *dev); + dev->fd = -1; + list_init(&dev->formats); + + dev->fd = open(devname, O_RDWR | O_NONBLOCK); + if (dev->fd < 0) { + printf("Error opening device %s: %d.\n", devname, errno); + v4l2_close(dev); + return NULL; + } + + memset(&cap, 0, sizeof cap); + ret = ioctl(dev->fd, VIDIOC_QUERYCAP, &cap); + if (ret < 0) { + printf("Error opening device %s: unable to query " + "device.\n", devname); + v4l2_close(dev); + return NULL; + } + + if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) + dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + else if (cap.capabilities & V4L2_CAP_VIDEO_OUTPUT) + dev->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; + else { + printf("Error opening device %s: neither video capture " + "nor video output supported.\n", devname); + v4l2_close(dev); + return NULL; + } + + ret = v4l2_enum_formats(dev); + if (ret < 0) { + printf("Error opening device %s: unable to enumerate " + "formats.\n", devname); + v4l2_close(dev); + return NULL; + } + + printf("Device %s opened: %s (%s).\n", devname, cap.card, cap.bus_info); + + return dev; +} + +void v4l2_close(struct v4l2_device *dev) +{ + struct v4l2_format_desc *format, *next_fmt; + struct v4l2_frame_desc *frame, *next_frm; + struct v4l2_ival_desc *ival, *next_ival; + + if (dev == NULL) + return; + + list_for_each_entry_safe(format, next_fmt, &dev->formats, list) { + list_for_each_entry_safe(frame, next_frm, &format->frames, list) { + list_for_each_entry_safe(ival, next_ival, &frame->ivals, list) { + free(ival); + } + free(frame); + } + free(format); + } + + close(dev->fd); +} + +/* ----------------------------------------------------------------------------- + * Controls + */ + +int v4l2_get_control(struct v4l2_device *dev, unsigned int id, int32_t *value) +{ + struct v4l2_control ctrl; + int ret; + + ctrl.id = id; + + ret = ioctl(dev->fd, VIDIOC_G_CTRL, &ctrl); + if (ret < 0) { + printf("unable to get control: %s (%d).\n", + strerror(errno), errno); + return -errno; + } + + *value = ctrl.value; + return 0; +} + +int v4l2_set_control(struct v4l2_device *dev, unsigned int id, int32_t *value) +{ + struct v4l2_control ctrl; + int ret; + + ctrl.id = id; + ctrl.value = *value; + + ret = ioctl(dev->fd, VIDIOC_S_CTRL, &ctrl); + if (ret < 0) { + printf("unable to set control: %s (%d).\n", + strerror(errno), errno); + return -errno; + } + + *value = ctrl.value; + return 0; +} + +int v4l2_get_controls(struct v4l2_device *dev, unsigned int count, + struct v4l2_ext_control *ctrls) +{ + struct v4l2_ext_controls controls; + int ret; + + memset(&controls, 0, sizeof controls); + controls.count = count; + controls.controls = ctrls; + + ret = ioctl(dev->fd, VIDIOC_G_EXT_CTRLS, &controls); + if (ret < 0) + printf("unable to get multiple controls: %s (%d).\n", + strerror(errno), errno); + + return ret; +} + +int v4l2_set_controls(struct v4l2_device *dev, unsigned int count, + struct v4l2_ext_control *ctrls) +{ + struct v4l2_ext_controls controls; + int ret; + + memset(&controls, 0, sizeof controls); + controls.count = count; + controls.controls = ctrls; + + ret = ioctl(dev->fd, VIDIOC_S_EXT_CTRLS, &controls); + if (ret < 0) + printf("unable to set multiple controls: %s (%d).\n", + strerror(errno), errno); + + return ret; +} + +/* ----------------------------------------------------------------------------- + * Formats and frame rates + */ + +int v4l2_get_crop(struct v4l2_device *dev, struct v4l2_rect *rect) +{ + struct v4l2_crop crop; + int ret; + + memset(&crop, 0, sizeof crop); + crop.type = dev->type; + + ret = ioctl(dev->fd, VIDIOC_G_CROP, &crop); + if (ret < 0) { + printf("Unable to get crop rectangle: %s (%d).\n", + strerror(errno), errno); + return -errno; + } + + dev->crop = crop.c; + *rect = crop.c; + + return 0; +} + +int v4l2_set_crop(struct v4l2_device *dev, struct v4l2_rect *rect) +{ + struct v4l2_crop crop; + int ret; + + memset(&crop, 0, sizeof crop); + crop.type = dev->type; + crop.c = *rect; + + ret = ioctl(dev->fd, VIDIOC_S_CROP, &crop); + if (ret < 0) { + printf("Unable to set crop rectangle: %s (%d).\n", + strerror(errno), errno); + return -errno; + } + + dev->crop = crop.c; + *rect = crop.c; + + return 0; +} + +int v4l2_get_format(struct v4l2_device *dev, struct v4l2_pix_format *format) +{ + struct v4l2_format fmt; + int ret; + + memset(&fmt, 0, sizeof fmt); + fmt.type = dev->type; + + ret = ioctl(dev->fd, VIDIOC_G_FMT, &fmt); + if (ret < 0) { + printf("Unable to get format: %s (%d).\n", strerror(errno), + errno); + return -errno; + } + + dev->format = fmt.fmt.pix; + *format = fmt.fmt.pix; + + return 0; +} + +int v4l2_set_format(struct v4l2_device *dev, struct v4l2_pix_format *format) +{ + struct v4l2_format fmt; + int ret; + + memset(&fmt, 0, sizeof fmt); + fmt.type = dev->type; + fmt.fmt.pix.width = format->width; + fmt.fmt.pix.height = format->height; + fmt.fmt.pix.pixelformat = format->pixelformat; + fmt.fmt.pix.field = V4L2_FIELD_ANY; + + ret = ioctl(dev->fd, VIDIOC_S_FMT, &fmt); + if (ret < 0) { + printf("Unable to set format: %s (%d).\n", strerror(errno), + errno); + return -errno; + } + + dev->format = fmt.fmt.pix; + *format = fmt.fmt.pix; + + return 0; +} + +/* ----------------------------------------------------------------------------- + * Buffers management + */ + +int v4l2_alloc_buffers(struct v4l2_device *dev, struct v4l2_buffers_pool *pool, + enum v4l2_memory memtype) +{ + struct v4l2_requestbuffers rb; + struct v4l2_buffer buf; + unsigned int i; + int ret; + + dev->memtype = memtype; + dev->pool = pool; + + memset(&rb, 0, sizeof rb); + rb.count = pool->nbufs; + rb.type = dev->type; + rb.memory = memtype; + + ret = ioctl(dev->fd, VIDIOC_REQBUFS, &rb); + if (ret < 0) { + printf("Unable to request buffers: %d.\n", errno); + ret = -errno; + goto done; + } + + if (rb.count > pool->nbufs) { + printf("Driver needs more buffers (%u) than available (%u).\n", + rb.count, pool->nbufs); + ret = -E2BIG; + goto done; + } + + dev->nbufs = rb.count; + + printf("%u buffers requested.\n", rb.count); + + /* Map the buffers. */ + for (i = 0; i < rb.count; ++i) { + memset(&buf, 0, sizeof buf); + buf.index = i; + buf.type = dev->type; + buf.memory = memtype; + ret = ioctl(dev->fd, VIDIOC_QUERYBUF, &buf); + if (ret < 0) { + printf("Unable to query buffer %u (%d).\n", i, errno); + ret = -errno; + goto done; + } + + switch (memtype) { + case V4L2_MEMORY_MMAP: + pool->buffers[i].mem = mmap(0, buf.length, PROT_READ | PROT_WRITE, + MAP_SHARED, dev->fd, buf.m.offset); + if (pool->buffers[i].mem == MAP_FAILED) { + printf("Unable to map buffer %u (%d)\n", i, errno); + ret = -errno; + goto done; + } + pool->buffers[i].size = buf.length; + printf("Buffer %u mapped at address %p.\n", i, pool->buffers[i].mem); + break; + + case V4L2_MEMORY_USERPTR: + if (pool->buffers[i].size < buf.length) { + printf("Buffer %u too small (%u bytes required, " + "%u bytes available.\n", i, buf.length, + pool->buffers[i].size); + ret = -EINVAL; + goto done; + } + + printf("Buffer %u valid.\n", i); + break; + + default: + break; + } + } + + ret = 0; + +done: + if (ret < 0) + v4l2_free_buffers(dev); + + return ret; +} + +int v4l2_free_buffers(struct v4l2_device *dev) +{ + struct v4l2_requestbuffers rb; + unsigned int i; + int ret; + + if (dev->pool == NULL) + return 0; + + if (dev->memtype == V4L2_MEMORY_MMAP) { + for (i = 0; i < dev->nbufs; ++i) { + struct v4l2_video_buffer *buffer = &dev->pool->buffers[i]; + + if (buffer->mem == NULL) + continue; + + ret = munmap(buffer->mem, buffer->size); + if (ret < 0) { + printf("Unable to unmap buffer %u (%d)\n", i, errno); + return -errno; + } + + buffer->mem = NULL; + buffer->size = 0; + } + } + + memset(&rb, 0, sizeof rb); + rb.count = 0; + rb.type = dev->type; + rb.memory = dev->memtype; + + ret = ioctl(dev->fd, VIDIOC_REQBUFS, &rb); + if (ret < 0) { + printf("Unable to release buffers: %d.\n", errno); + return -errno; + } + + dev->nbufs = 0; + dev->pool = NULL; + + return 0; +} + +int v4l2_dequeue_buffer(struct v4l2_device *dev, struct v4l2_video_buffer *buffer) +{ + struct v4l2_buffer buf; + int ret; + + memset(&buf, 0, sizeof buf); + buf.type = dev->type; + buf.memory = dev->memtype; + + ret = ioctl(dev->fd, VIDIOC_DQBUF, &buf); + if (ret < 0) { + printf("Unable to dequeue buffer (%d).\n", errno); + return -errno; + } + + *buffer = dev->pool->buffers[buf.index]; + buffer->index = buf.index; + buffer->bytesused = buf.bytesused; + buffer->timestamp = buf.timestamp; + buffer->error = !!(buf.flags & V4L2_BUF_FLAG_ERROR); + + return 0; +} + +int v4l2_queue_buffer(struct v4l2_device *dev, struct v4l2_video_buffer *buffer) +{ + struct v4l2_buffer buf; + int ret; + + if (buffer->index >= dev->nbufs) + return -EINVAL; + + memset(&buf, 0, sizeof buf); + buf.index = buffer->index; + buf.type = dev->type; + buf.memory = dev->memtype; + + if (dev->memtype == V4L2_MEMORY_USERPTR) + buf.m.userptr = (unsigned long)dev->pool->buffers[buffer->index].mem; + + buf.length = dev->pool->buffers[buffer->index].size; + + if (dev->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) + buf.bytesused = buffer->bytesused; + + ret = ioctl(dev->fd, VIDIOC_QBUF, &buf); + if (ret < 0) { + printf("Unable to queue buffer (%d).\n", errno); + return -errno; + } + + return 0; +} + +/* ----------------------------------------------------------------------------- + * Stream management + */ + +int v4l2_stream_on(struct v4l2_device *dev) +{ + int type = dev->type; + int ret; + + ret = ioctl(dev->fd, VIDIOC_STREAMON, &type); + if (ret < 0) + return -errno; + + return 0; +} + +int v4l2_stream_off(struct v4l2_device *dev) +{ + int type = dev->type; + int ret; + + ret = ioctl(dev->fd, VIDIOC_STREAMOFF, &type); + if (ret < 0) + return -errno; + + return 0; +} diff --git a/isp/v4l2.h b/isp/v4l2.h new file mode 100644 index 0000000..7d28d67 --- /dev/null +++ b/isp/v4l2.h @@ -0,0 +1,289 @@ +/* + * OMAP3 ISP library - V4L2 devices + * + * Copyright (C) 2010-2011 Ideas on board SPRL + * + * Contact: Laurent Pinchart + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at + * your option) any later version. + * + * This library 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 Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef __V4L2_H +#define __V4L2_H + +#include +#include +#include + +#include "list.h" +#include "v4l2-pool.h" + +struct v4l2_device +{ + int fd; + + enum v4l2_buf_type type; + enum v4l2_memory memtype; + + struct list_entry formats; + struct v4l2_pix_format format; + struct v4l2_rect crop; + + unsigned int nbufs; + struct v4l2_buffers_pool *pool; +}; + +/* + * v4l2_open - Open a V4L2 device + * @devname: Name (including path) of the device node + * + * Open the V4L2 device referenced by @devname for video capture or display in + * non-blocking mode. + * + * If the device can be opened, query its capabilities and enumerates frame + * formats, sizes and intervals. + * + * Return a pointer to a newly allocated v4l2_device structure instance on + * success and NULL on failure. The returned pointer must be freed with + * v4l2_close when the device isn't needed anymore. + */ +struct v4l2_device *v4l2_open(const char *devname); + +/* + * v4l2_close - Close a V4L2 device + * @dev: Device instance + * + * Close the device instance given as argument and free allocated resources. + * Access to the device instance is forbidden after this function returns. + */ +void v4l2_close(struct v4l2_device *dev); + +/* + * v4l2_get_format - Retrieve the current pixel format + * @dev: Device instance + * @format: Pixel format structure to be filled + * + * Query the device to retrieve the current pixel format and frame size and fill + * the @format structure. + * + * Return 0 on success or a negative error code on failure. + */ +int v4l2_get_format(struct v4l2_device *dev, struct v4l2_pix_format *format); + +/* + * v4l2_set_format - Set the pixel format + * @dev: Device instance + * @format: Pixel format structure to be set + * + * Set the pixel format and frame size stored in @format. The device can modify + * the requested format and size, in which case the @format structure will be + * updated to reflect the modified settings. + * + * Return 0 on success or a negative error code on failure. + */ +int v4l2_set_format(struct v4l2_device *dev, struct v4l2_pix_format *format); + +/* + * v4l2_get_crop - Retrieve the current crop rectangle + * @dev: Device instance + * @rect: Crop rectangle structure to be filled + * + * Query the device to retrieve the current crop rectangle and fill the @rect + * structure. + * + * Return 0 on success or a negative error code on failure. + */ +int v4l2_get_crop(struct v4l2_device *dev, struct v4l2_rect *rect); + +/* + * v4l2_set_crop - Set the crop rectangle + * @dev: Device instance + * @rect: Crop rectangle structure to be set + * + * Set the crop rectangle stored in @rect. The device can modify the requested + * rectangle, in which case the @rect structure will be updated to reflect the + * modified settings. + * + * Return 0 on success or a negative error code on failure. + */ +int v4l2_set_crop(struct v4l2_device *dev, struct v4l2_rect *rect); + +/* + * v4l2_alloc_buffers - Allocate buffers for video frames + * @dev: Device instance + * @pool: Buffers pool + * @memtype: Type of buffers + * + * Request the driver to allocate the number of buffers in the pool. The driver + * can modify the number of buffers depending on its needs. The number of + * allocated buffers will be stored in the @dev::nbufs field. + * + * When @memtype is set to V4L2_MEMORY_MMAP the buffers are allocated by the + * driver and mapped to userspace. When @memtype is set to V4L2_MEMORY_USERPTR + * the driver only allocates buffer objects and relies on the application to + * provide memory storage for video frames. + * + * Upon successful allocation the @dev::pool field points to the buffers pool. + * + * Return 0 on success or a negative error code on failure. + */ +int v4l2_alloc_buffers(struct v4l2_device *dev, struct v4l2_buffers_pool *pool, + enum v4l2_memory memtype); + +/* + * v4l2_free_buffers - Free buffers + * @dev: Device instance + * + * Free buffers previously allocated by v4l2_alloc_buffers(). If the buffers + * have been allocated with the V4L2_MEMORY_USERPTR memory type only the buffer + * objects are freed, and the caller is responsible for freeing the video frames + * memory if required. + * + * When successful this function sets the @dev::nbufs field to zero and the + * @dev::pool field to NULL. + * + * Return 0 on success or a negative error code on failure. + */ +int v4l2_free_buffers(struct v4l2_device *dev); + +/* + * v4l2_queue_buffer - Queue a buffer for video capture/output + * @dev: Device instance + * @buffer: Buffer to be queued + * + * Queue the buffer identified by @buffer for video capture or output, depending + * on the device type. + * + * The caller must initialize the @buffer::index field with the index of the + * buffer to be queued. The index is zero-based and must be lower than the + * number of allocated buffers. + * + * For V4L2_MEMORY_USERPTR buffers, the caller must initialize the @buffer::mem + * field with the address of the video frame memory, and the @buffer:length + * field with its size in bytes. For optimal performances the address and length + * should be identical between v4l2_queue_buffer() calls for a given buffer + * index. + * + * For video output, the caller must initialize the @buffer::bytesused field + * with the size of video data. The value should differ from the buffer length + * for variable-size video formats only. + * + * Upon successful return the buffer ownership is transferred to the driver. The + * caller must not touch video memory for that buffer before calling + * v4l2_dequeue_buffer(). Attempting to queue an already queued buffer will + * fail. + * + * Return 0 on success or a negative error code on failure. + */ +int v4l2_queue_buffer(struct v4l2_device *dev, struct v4l2_video_buffer *buffer); + +/* + * v4l2_dequeue_buffer - Dequeue the next buffer + * @dev: Device instance + * @buffer: Dequeued buffer data to be filled + * + * Dequeue the next buffer processed by the driver and fill all fields in + * @buffer. + * + * This function does not block. If no buffer is ready it will return + * immediately with -EAGAIN. + * + * If an error occured during video capture or display, the @buffer::error field + * is set to true. Depending on the device the video data can be partly + * corrupted or complete garbage. + * + * Once dequeued the buffer ownership is transferred to the caller. Video memory + * for that buffer can be safely read from and written to. + * + * Return 0 on success or a negative error code on failure. An error that + * results in @buffer:error being set is not considered as a failure condition + * for the purpose of the return value. + */ +int v4l2_dequeue_buffer(struct v4l2_device *dev, struct v4l2_video_buffer *buffer); + +/* + * v4l2_stream_on - Start video streaming + * @dev: Device instance + * + * Start video capture or output on the device. For video output devices at + * least one buffer must be queued before starting the stream. + * + * Return 0 on success or a negative error code on failure. + */ +int v4l2_stream_on(struct v4l2_device *dev); + +/* + * v4l2_stream_off - Stop video streaming + * @dev: Device instance + * + * Stop video capture or output on the device. Upon successful return ownership + * of all buffers is returned to the caller. + * + * Return 0 on success or a negative error code on failure. + */ +int v4l2_stream_off(struct v4l2_device *dev); + +/* + * v4l2_get_control - Read the value of a control + * @dev: Device instance + * @id: Control ID + * @value: Control value to be filled + * + * Retrieve the current value of control @id and store it in @value. + * + * Return 0 on success or a negative error code on failure. + */ +int v4l2_get_control(struct v4l2_device *dev, unsigned int id, int32_t *value); + +/* + * v4l2_set_control - Write the value of a control + * @dev: Device instance + * @id: Control ID + * @value: Control value + * + * Set control @id to @value. The device is allowed to modify the requested + * value, in which case @value is updated to the modified value. + * + * Return 0 on success or a negative error code on failure. + */ +int v4l2_set_control(struct v4l2_device *dev, unsigned int id, int32_t *value); + +/* + * v4l2_get_controls - Read the value of multiple controls + * @dev: Device instance + * @count: Number of controls + * @ctrls: Controls to be read + * + * Retrieve the current value of controls identified by @ctrls. + * + * Return 0 on success or a negative error code on failure. + */ +int v4l2_get_controls(struct v4l2_device *dev, unsigned int count, + struct v4l2_ext_control *ctrls); + +/* + * v4l2_set_controls - Write the value of multiple controls + * @dev: Device instance + * @count: Number of controls + * @ctrls: Controls to be written + * + * Set controls identified by @ctrls. The device is allowed to modify the + * requested values, in which case @ctrls is updated to the modified value. + * + * Return 0 on success or a negative error code on failure. + */ +int v4l2_set_controls(struct v4l2_device *dev, unsigned int count, + struct v4l2_ext_control *ctrls); + +#endif -- cgit v1.2.3