From c9601d407a7ca943e53fa49a5a5f69b868c6ed11 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Tue, 18 Nov 2014 22:52:06 +0200 Subject: Xilinx video library - Initial import Signed-off-by: Laurent Pinchart --- .gitignore | 4 + COPYING.LGPL | 502 +++++++++++++++++++++++++++++++ Makefile | 23 ++ doc/Doxyfile | 9 + doc/Makefile | 7 + include/xlnx-events.h | 43 +++ include/xlnx-list.h | 99 +++++++ include/xlnx-plugin.h | 93 ++++++ include/xlnx-tools.h | 70 +++++ include/xlnx-video.h | 129 ++++++++ lib/Makefile | 26 ++ lib/xlnx-buffers.c | 80 +++++ lib/xlnx-buffers.h | 83 ++++++ lib/xlnx-events.c | 269 +++++++++++++++++ lib/xlnx-plugin.c | 220 ++++++++++++++ lib/xlnx-priv.h | 107 +++++++ lib/xlnx-video.c | 388 ++++++++++++++++++++++++ plugins/Makefile | 12 + plugins/dummy/Makefile | 21 ++ plugins/dummy/xlnx-dummy.c | 42 +++ plugins/v4l2/Makefile | 22 ++ plugins/v4l2/v4l2.c | 721 +++++++++++++++++++++++++++++++++++++++++++++ plugins/v4l2/v4l2.h | 289 ++++++++++++++++++ plugins/v4l2/xlnx-v4l2.c | 417 ++++++++++++++++++++++++++ src/Makefile | 22 ++ src/main.c | 213 +++++++++++++ 26 files changed, 3911 insertions(+) create mode 100644 .gitignore create mode 100644 COPYING.LGPL create mode 100644 Makefile create mode 100644 doc/Doxyfile create mode 100644 doc/Makefile create mode 100644 include/xlnx-events.h create mode 100644 include/xlnx-list.h create mode 100644 include/xlnx-plugin.h create mode 100644 include/xlnx-tools.h create mode 100644 include/xlnx-video.h create mode 100644 lib/Makefile create mode 100644 lib/xlnx-buffers.c create mode 100644 lib/xlnx-buffers.h create mode 100644 lib/xlnx-events.c create mode 100644 lib/xlnx-plugin.c create mode 100644 lib/xlnx-priv.h create mode 100644 lib/xlnx-video.c create mode 100644 plugins/Makefile create mode 100644 plugins/dummy/Makefile create mode 100644 plugins/dummy/xlnx-dummy.c create mode 100644 plugins/v4l2/Makefile create mode 100644 plugins/v4l2/v4l2.c create mode 100644 plugins/v4l2/v4l2.h create mode 100644 plugins/v4l2/xlnx-v4l2.c create mode 100644 src/Makefile create mode 100644 src/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ed8b1e --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.o +*.so +doc/html +src/xlnx-video diff --git a/COPYING.LGPL b/COPYING.LGPL new file mode 100644 index 0000000..4362b49 --- /dev/null +++ b/COPYING.LGPL @@ -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/Makefile b/Makefile new file mode 100644 index 0000000..2b1058b --- /dev/null +++ b/Makefile @@ -0,0 +1,23 @@ +SUBDIRS := 3rdparty doc lib plugins src + +export topdir := $(PWD) + +all: all-recursive + +clean: clean-recursive + +doc: + $(MAKE) -C doc $@ + +install: install-recursive + +.PHONY: doc + +recursive=all-recursive clean-recursive install-recursive + +$(recursive): + @target=`echo $@ | sed s/-recursive//` ; \ + for subdir in $(SUBDIRS); do \ + echo "Making $$target in $$subdir" ; \ + $(MAKE) -C $$subdir $$target; \ + done diff --git a/doc/Doxyfile b/doc/Doxyfile new file mode 100644 index 0000000..370364b --- /dev/null +++ b/doc/Doxyfile @@ -0,0 +1,9 @@ +PROJECT_NAME = "Xilinx Video Library" +OUTPUT_DIRECTORY = ./ +OPTIMIZE_OUTPUT_FOR_C = YES +EXTRACT_LOCAL_CLASSES = NO +INPUT = ../include ../lib +EXCLUDE_PATTERNS = */include/xlnx-list.h */include/xlnx-tools.h +EXCLUDE_PATTERNS += */lib/xlnx-list.c */lib/xlnx-tools.c +EXCLUDE_PATTERNS += */lib/*.h +GENERATE_LATEX = NO diff --git a/doc/Makefile b/doc/Makefile new file mode 100644 index 0000000..27f225b --- /dev/null +++ b/doc/Makefile @@ -0,0 +1,7 @@ +all: + +doc: + doxygen Doxyfile + +clean: + rm -rf html diff --git a/include/xlnx-events.h b/include/xlnx-events.h new file mode 100644 index 0000000..ebf575d --- /dev/null +++ b/include/xlnx-events.h @@ -0,0 +1,43 @@ +/* + * Xilinx Video Library - select()-based generic event loop + * + * Copyright (C) 2014-2016 Ideas on board Oy + * + * Contact: Laurent Pinchart + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __XLNX_EVENTS_H__ +#define __XLNX_EVENTS_H__ + +/** + * \file + * \brief select()-based event loop implementation + * + * This event loop is a sample event handling operations implementation. It + * registers event handler operations with the library context and implements an + * infinite event loop based on the select() function to wait for events on + * file descriptors. + */ + +struct xlnx_video; + +int xlnx_events_init(struct xlnx_video *xv); +void xlnx_events_fini(struct xlnx_video *xv); +bool xlnx_events_loop(struct xlnx_video *xv); +void xlnx_events_stop(struct xlnx_video *xv); + +#endif /* __XLNX_EVENTS_H__ */ diff --git a/include/xlnx-list.h b/include/xlnx-list.h new file mode 100644 index 0000000..f23ab24 --- /dev/null +++ b/include/xlnx-list.h @@ -0,0 +1,99 @@ +/* + * Xilinx Video Library - Double linked lists + * + * Copyright (C) 2014-2016 Ideas on board Oy + * + * 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. + */ + +#ifndef __XLNX_LIST_H__ +#define __XLNX_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 int list_empty(struct list_entry *list) +{ + return list->next == 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_insert_after(struct list_entry *entry, struct list_entry *after) +{ + list_prepend(entry, after); +} + +static inline void list_insert_before(struct list_entry *entry, struct list_entry *before) +{ + list_append(entry, before); +} + +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 /* __XLNX_LIST_H__ */ diff --git a/include/xlnx-plugin.h b/include/xlnx-plugin.h new file mode 100644 index 0000000..ff39fed --- /dev/null +++ b/include/xlnx-plugin.h @@ -0,0 +1,93 @@ +/* + * Xilinx Video Library - Plugin API + * + * Copyright (C) 2014-2016 Ideas on board Oy + * + * 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. + */ + +#ifndef __XLNX_PLUGIN_H__ +#define __XLNX_PLUGIN_H__ + +/** + * \file + * \brief Library plugin support + * + * To be discovered by the Xilinx video library, plugins must export a plugin + * operations structure with a well-defined symbol name. Plugins should use the + * EXPORT_XLNX_VIDEO_PLUGIN() macro to declare the strucure and initialize its + * members with function pointers. + * + * \code{.c} + * EXPORT_XLNX_VIDEO_PLUGIN(myplugin) = { + * .info = myplugin_info, + * .scan = myplugin_scan, + * } + * \endcode + */ + +#include + +struct media_device; +struct xlnx_video_compoment; + +/** + * \brief Type of video component supported by the plugin + */ +enum xlnx_video_plugin_type { + /** + * Hardware video component, handled automatically when the plugin is + * loaded or the component discovered. + */ + XLNX_VIDEO_PLUGIN_TYPE_HARDWARE, + /** + * Software video component, instantiated manually. + */ + XLNX_VIDEO_PLUGIN_TYPE_SOFTWARE, +}; + +/** + * \brief Plugin information + */ +struct xlnx_video_plugin_info { + /** Plugin name */ + const char *name; + /** Plugin type, one of XLNX_VIDEO_PLUGIN_TYPE_* */ + enum xlnx_video_plugin_type type; +}; + +/** + * \brief Xilinx video plugin operations + */ +struct xlnx_video_plugin_ops { + /** + * Query plugin information. The returned structure must be valid for + * the lifetime of the plugin. + */ + const struct xlnx_video_plugin_info *(*info)(void); + struct xlnx_video_component *(*scan)(struct xlnx_video *xv, + struct media_device *mdev); + struct xlnx_video_component *(*create_component)(struct xlnx_video *xv); + void (*destroy_component)(struct xlnx_video *xv, + struct xlnx_video_compoment *xvcomp); + /** Reserved fields for ABI compatibility, must be set to NULL. */ + void (*reserved[10])(void); +}; + +/** \cond IGNORE */ +#define XLNX_VIDEO_PLUGIN_SYMBOL __xlnx_video_plugin +#define EXPORT_XLNX_VIDEO_PLUGIN(name) \ +const struct xlnx_video_plugin_ops XLNX_VIDEO_PLUGIN_SYMBOL +/** \endcond */ + +#endif /* __XLNX_PLUGIN_H__ */ diff --git a/include/xlnx-tools.h b/include/xlnx-tools.h new file mode 100644 index 0000000..799d915 --- /dev/null +++ b/include/xlnx-tools.h @@ -0,0 +1,70 @@ +/* + * Xilinx Video Library - Tools + * + * Copyright (C) 2014-2016 Ideas on board Oy + * + * 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. + */ + +#ifndef __XLNX_TOOLS_H__ +#define __XLNX_TOOLS_H__ + +#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0])) +#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) + +#define min(a, b) ({ \ + typeof(a) __a = (a); \ + typeof(b) __b = (b); \ + __a < __b ? __a : __b; \ +}) + +#define min_t(type, a, b) ({ \ + type __a = (a); \ + type __b = (b); \ + __a < __b ? __a : __b; \ +}) + +#define max(a, b) ({ \ + typeof(a) __a = (a); \ + typeof(b) __b = (b); \ + __a > __b ? __a : __b; \ +}) + +#define max_t(type, a, b) ({ \ + type __a = (a); \ + type __b = (b); \ + __a > __b ? __a : __b; \ +}) + +#define clamp(val, min, max) ({ \ + typeof(val) __val = (val); \ + typeof(min) __min = (min); \ + typeof(max) __max = (max); \ + __val = __val < __min ? __min : __val; \ + __val > __max ? __max : __val; \ +}) + +#define clamp_t(type, val, min, max) ({ \ + type __val = (val); \ + type __min = (min); \ + type __max = (max); \ + __val = __val < __min ? __min : __val; \ + __val > __max ? __max : __val; \ +}) + +#define div_round_up(num, denom) (((num) + (denom) - 1) / (denom)) + +#define container_of(ptr, type, member) \ + (type *)((char *)(ptr) - offsetof(type, member)) + +#endif /* __XLNX_TOOLS_H__ */ diff --git a/include/xlnx-video.h b/include/xlnx-video.h new file mode 100644 index 0000000..eaebb80 --- /dev/null +++ b/include/xlnx-video.h @@ -0,0 +1,129 @@ +/* + * Xilinx Video Library + * + * Copyright (C) 2014-2016 Ideas on board Oy + * + * 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. + */ + +#ifndef __XLNX_VIDEO_H__ +#define __XLNX_VIDEO_H__ + +/** + * \file + * \brief Xilinx Video Library + */ + +struct xlnx_video; +struct xlnx_video_component; +struct xlnx_video_pipeline; + +struct xlnx_video *xlnx_video_create(void); +int xlnx_video_setup(struct xlnx_video *xv); +void xlnx_video_destroy(struct xlnx_video *xv); + +/** + * \brief Library log level + * + * Only messages with a log level lower than or equal to the library context + * log level are logged, all other messages are dropped silently. + * + * \sa xlnx_log_set_level() + */ +enum xlnx_log_level { + /** Don't log any message */ + XLNX_LOG_NONE = 0, + /** Log error messages */ + XLNX_LOG_ERROR = 1, + /** Log warning messages */ + XLNX_LOG_WARNING = 2, + /** Log informational messages */ + XLNX_LOG_INFO = 3, + /** Log debug messages */ + XLNX_LOG_DEBUG = 4, +}; + +void xlnx_video_set_log_level(struct xlnx_video *xv, enum xlnx_log_level level); + +/** + * \brief Event handler operations + * + * The library requires a set of event handling operations whose implementation + * can vary between applications depending on their architecture. To avoid + * restricting applications to a particular programming model, those operations + * are not implemented by the library directly but must be provided by + * applications in the form of callback functions. + * + * The xlnx_video_event_operations structure contains callback function + * pointers for all event handling operations. Applications must supply an + * implementation for each operation. + * + * All event handling operations are called by the library with the handler + * parameter set to the event handler passed to the + * xlnx_video_set_event_handler() function. + */ +struct xlnx_video_event_operations { + /** + * \param handler event handler private data + * \param fd the file descriptor number + * \param events the types of events to watch for + * \param callback callback event handler + * \param priv private token passed to the callback event handler + * + * Start watching the file descriptor \a fd for events of type + * \a events, and call the \a callback function with the priv parameter + * set to \a priv when such an event is detected. + */ + void (*watch)(void *handler, int fd, int events, + void(*callback)(void *priv), void *priv); + /** + * \param handler event handler private data + * \param fd the file descriptor number + * + * Stop watching the file descriptor \a fd for events. + */ + void (*unwatch)(void *handler, int fd); +}; + +void xlnx_video_set_event_handler(struct xlnx_video *xv, + const struct xlnx_video_event_operations *ops, + void *handler); +void *xlnx_video_get_event_handler_data(struct xlnx_video *xv); + +int xlnx_video_plugin_load_directory(struct xlnx_video *xv, const char *path); + +/** + * \brief Video component type + */ +enum xlnx_video_component_type { + /** Video capture component (input) */ + XLNX_VIDEO_COMPONENT_CAPTURE = 1 << 0, + /** Video processing component (memory to memory) */ + XLNX_VIDEO_COMPONENT_PROCESS = 1 << 1, + /** Video display component (output) */ + XLNX_VIDEO_COMPONENT_DISPLAY = 1 << 2, + /** Any component type */ + XLNX_VIDEO_COMPONENT_ANY = XLNX_VIDEO_COMPONENT_CAPTURE + | XLNX_VIDEO_COMPONENT_PROCESS + | XLNX_VIDEO_COMPONENT_DISPLAY, +}; + +int xlnx_video_component_enumerate(struct xlnx_video *xv); +int xlnx_video_component_list(struct xlnx_video *xv, + enum xlnx_video_component_type type, + struct xlnx_video_component ***components); +const char *xlnx_video_component_name(const struct xlnx_video_component *xvcomp); +enum xlnx_video_component_type +xlnx_video_component_type(const struct xlnx_video_component *xvcomp); + +#endif diff --git a/lib/Makefile b/lib/Makefile new file mode 100644 index 0000000..cb91e0e --- /dev/null +++ b/lib/Makefile @@ -0,0 +1,26 @@ +AR := $(CROSS_COMPILE)ar +CC := $(CROSS_COMPILE)gcc +CFLAGS := -O2 -W -Wall -Wno-unused-parameter -I$(KDIR)/usr/include -I$(topdir)/3rdparty/media-ctl/include -I$(topdir)/3rdparty/media-enum/include -I$(topdir)/include -I. -fPIC +LDFLAGS := -L$(topdir)/3rdparty/media-ctl -L$(topdir)/3rdparty/media-enum +LIBS := -lmediaenum -lmediactl -ldl + +OBJECTS := xlnx-buffers.o \ + xlnx-events.o \ + xlnx-plugin.o \ + xlnx-video.o + +TARGET := libxlnxvideo.so + +all: $(TARGET) + +$(TARGET): $(OBJECTS) + $(CC) -o $@ -shared $^ $(LDFLAGS) $(LIBS) + +%.o : %.c + $(CC) $(CFLAGS) -c -o $@ $< + +clean: + -$(RM) *.o + -$(RM) $(TARGET) + +install: diff --git a/lib/xlnx-buffers.c b/lib/xlnx-buffers.c new file mode 100644 index 0000000..34f873a --- /dev/null +++ b/lib/xlnx-buffers.c @@ -0,0 +1,80 @@ +/* + * Xilinx Video Library - Buffers Pool + * + * Copyright (C) 2014-2016 Ideas on board Oy + * + * 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. + */ + +#include +#include + +#include "xlnx-buffers.h" + +struct xlnx_video_buffers_pool *xlnx_video_buffers_pool_new(unsigned int nbufs) +{ + struct xlnx_video_buffer *buffers; + struct xlnx_video_buffers_pool *pool; + unsigned int i; + + 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); + + for (i = 0; i < nbufs; ++i) + buffers[i].index = i; + + pool->nbufs = nbufs; + pool->buffers = buffers; + + return pool; +} + +void xlnx_video_buffers_pool_delete(struct xlnx_video_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 xlnx_video_buffers_pool_alloc(struct xlnx_video_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/lib/xlnx-buffers.h b/lib/xlnx-buffers.h new file mode 100644 index 0000000..2170150 --- /dev/null +++ b/lib/xlnx-buffers.h @@ -0,0 +1,83 @@ +/* + * Xilinx Video Library - Buffers Pool + * + * Copyright (C) 2014-2016 Ideas on board Oy + * + * 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. + */ +#ifndef __XLNX_VIDEO_BUFFERS_POOL_H__ +#define __XLNX_VIDEO_BUFFERS_POOL_H__ + +#include +#include + +/* + * struct xlnx_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 xlnx_video_buffer +{ + unsigned int index; + unsigned int size; + unsigned int bytesused; + struct timeval timestamp; + bool error; + bool allocated; + void *mem; +}; + +struct xlnx_video_buffers_pool +{ + unsigned int nbufs; + struct xlnx_video_buffer *buffers; +}; + +/* + * xlnx_video_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 xlnx_video_buffers_pool *xlnx_video_buffers_pool_new(unsigned int nbufs); + +/* + * xlnx_video_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 xlnx_video_buffers_pool_delete(struct xlnx_video_buffers_pool *pool); + +/* + * xlnx_video_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 xlnx_video_buffers_pool_alloc(struct xlnx_video_buffers_pool *pool, + size_t size, size_t align); + +#endif /* __XLNX_VIDEO_BUFFERS_POOL_H__ */ diff --git a/lib/xlnx-events.c b/lib/xlnx-events.c new file mode 100644 index 0000000..933f27a --- /dev/null +++ b/lib/xlnx-events.c @@ -0,0 +1,269 @@ +/* + * Generic Event Loop + * + * Copyright (C) 2014-2016 Ideas on board Oy + * + * Contact: Laurent Pinchart + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#define _DEFAULT_SOURCE +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +#include "xlnx-priv.h" + +#define SELECT_TIMEOUT 2000 /* in milliseconds */ + +struct xlnx_events { + struct list_entry events; + bool done; + + int maxfd; + fd_set rfds; + fd_set wfds; + fd_set efds; +}; + +struct xlnx_event_fd { + struct list_entry list; + + int fd; + int type; + void (*callback)(void *priv); + void *priv; +}; + +static void xlnx_events_watch_fd(void *handler, int fd, int event_type, + void(*callback)(void *), void *priv) +{ + struct xlnx_events *events = handler; + struct xlnx_event_fd *event; + + event = malloc(sizeof *event); + if (event == NULL) + return; + + event->fd = fd; + event->type = event_type; + event->callback = callback; + event->priv = priv; + + switch (event->type) { + case POLLIN: + FD_SET(fd, &events->rfds); + break; + case POLLOUT: + FD_SET(fd, &events->wfds); + break; + case POLLERR: + FD_SET(fd, &events->efds); + break; + } + + events->maxfd = max(events->maxfd, fd); + + list_append(&event->list, &events->events); +} + +static void xlnx_events_unwatch_fd(void *handler, int fd) +{ + struct xlnx_events *events = handler; + struct xlnx_event_fd *event = NULL; + struct xlnx_event_fd *entry; + int maxfd = 0; + + list_for_each_entry(entry, &events->events, list) { + if (entry->fd == fd) + event = entry; + else + maxfd = max(maxfd, entry->fd); + } + + if (event == NULL) + return; + + switch (event->type) { + case POLLIN: + FD_CLR(fd, &events->rfds); + break; + case POLLOUT: + FD_CLR(fd, &events->wfds); + break; + case POLLERR: + FD_CLR(fd, &events->efds); + break; + } + + events->maxfd = maxfd; + + list_remove(&event->list); + free(event); +} + +static void xlnx_events_dispatch(struct xlnx_events *events, const fd_set *rfds, + const fd_set *wfds, const fd_set *efds) +{ + struct xlnx_event_fd *event; + struct xlnx_event_fd *next; + + list_for_each_entry_safe(event, next, &events->events, list) { + if (event->type == POLLIN && FD_ISSET(event->fd, rfds)) + event->callback(event->priv); + else if (event->type == POLLOUT && FD_ISSET(event->fd, wfds)) + event->callback(event->priv); + else if (event->type == POLLERR && FD_ISSET(event->fd, efds)) + event->callback(event->priv); + + /* If the callback stopped events processing, we're done. */ + if (events->done) + break; + } +} + +/** + * \brief Start and run the event loop + * \param xv the library context + * + * This function starts and runs the event loop synchronously, waiting for + * events and dispatching them as they arrive. It doesn't return until the loop + * is requested to stop by a call to xlnx_events_stop() or a fatal error occurs. + * + * \return false if the loop exited due to a stop request, or true if it exited + * due to a fatal error. + */ +bool xlnx_events_loop(struct xlnx_video *xv) +{ + struct xlnx_events *events = xlnx_video_get_event_handler_data(xv); + + events->done = false; + + while (!events->done) { + struct timeval timeout; + fd_set rfds; + fd_set wfds; + fd_set efds; + int ret; + + timeout.tv_sec = SELECT_TIMEOUT / 1000; + timeout.tv_usec = (SELECT_TIMEOUT % 1000) * 1000; + rfds = events->rfds; + wfds = events->wfds; + efds = events->efds; + + ret = select(events->maxfd + 1, &rfds, &wfds, &efds, &timeout); + if (ret < 0) { + /* EINTR means that a signal has been received, continue + * to the next iteration in that case. + */ + if (errno == EINTR) + continue; + + printf("error: select failed with %d\n", errno); + break; + } + if (ret == 0) { + /* select() should never time out as the library is + * supposed to process images continuously. A timeout is + * thus considered as a fatal error. + */ + printf("error: select timeout\n"); + break; + } + + xlnx_events_dispatch(events, &rfds, &wfds, &efds); + } + + return !events->done; +} + +/** + * \brief Stop the event loop + * \param xv the library context + * + * Stop the running event loop started with xlnx_events_loop(). The loop is + * stopped asynchronously after processing the next event. This function doesn't + * wait for the loop to exit before returning. + */ +void xlnx_events_stop(struct xlnx_video *xv) +{ + struct xlnx_events *events = xlnx_video_get_event_handler_data(xv); + + events->done = true; +} + +static const struct xlnx_video_event_operations xlnx_events_ops = { + .watch = xlnx_events_watch_fd, + .unwatch = xlnx_events_unwatch_fd, +}; + +/** + * \brief Initialize event handling + * \param xv the library context + * + * This function registers event handling operations with the library context + * \a xv. The event handling implementation is based on a select() loop that is + * controlled through the xlnx_events_loop() and xlnx_events_stop() functions. + * + * Before destroying the library context event handling must be cleaned up with + * a call to xlnx_events_fini(). + * + * \return 0 on success or a negative error code on failure. + */ +int xlnx_events_init(struct xlnx_video *xv) +{ + struct xlnx_events *events; + + events = calloc(1, sizeof(*events)); + if (!events) + return -ENOMEM; + + FD_ZERO(&events->rfds); + FD_ZERO(&events->wfds); + FD_ZERO(&events->efds); + events->maxfd = 0; + list_init(&events->events); + + xlnx_video_set_event_handler(xv, &xlnx_events_ops, events); + return 0; +} + +/** + * \brief Clean up event handling + * \param xv the library context + * + * This function cleans up event handling. It must be called before destroying + * any library context previously initialized by a call to xlnx_events_init(). + */ +void xlnx_events_fini(struct xlnx_video *xv) +{ + struct xlnx_events *events = xlnx_video_get_event_handler_data(xv); + + if (events) { + xlnx_video_set_event_handler(xv, NULL, NULL); + free(events); + } +} diff --git a/lib/xlnx-plugin.c b/lib/xlnx-plugin.c new file mode 100644 index 0000000..bd4553d --- /dev/null +++ b/lib/xlnx-plugin.c @@ -0,0 +1,220 @@ +/* + * Xilinx Video Library - Plugin API + * + * Copyright (C) 2014-2016 Ideas on board Oy + * + * 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. + */ + +#include +#include +#include +#include +#include + +#include + +#include +#include + +#include "xlnx-priv.h" + +#define __stringify(n) #n +#define _stringify(n) __stringify(n) + +#define XLNX_VIDEO_PLUGIN_DIR "/usr/lib/xilinx/video/plugins" +#define XLNX_VIDEO_PLUGIN_SYMBOL_NAME _stringify(XLNX_VIDEO_PLUGIN_SYMBOL) + +struct xlnx_video_plugin { + struct list_entry list; + void *handle; + const struct xlnx_video_plugin_ops *ops; + const struct xlnx_video_plugin_info *info; +}; + +struct xlnx_video_component *xlnx_video_plugin_scan(struct xlnx_video *xv, + struct media_device *mdev) +{ + const struct media_device_info *mdev_info = media_get_info(mdev); + struct xlnx_video_component *xvcomp; + struct xlnx_video_plugin *plugin; + + xlnx_dbg(xv, "Searching plugin for media device %s (%s)\n", + mdev_info->model, mdev_info->bus_info); + + list_for_each_entry(plugin, &xv->plugins, list) { + xlnx_dbg(xv, "Trying plugin `%s'\n", plugin->info->name); + xvcomp = plugin->ops->scan(xv, mdev); + if (xvcomp) { + xlnx_dbg(xv, "Plugin supports media device\n"); + return xvcomp; + } + } + + xlnx_dbg(xv, "No matching plugin found\n"); + return NULL; +} + +static int xlnx_video_plugin_load_file(struct xlnx_video *xv, const char *filename) +{ + const struct xlnx_video_plugin_ops *plugin_ops; + struct xlnx_video_plugin *plugin; + void *plugin_so; + int ret; + + xlnx_dbg(xv, "Loading plugin from file %s\n", filename); + + plugin_so = dlopen(filename, RTLD_LAZY); + if (!plugin_so) { + xlnx_dbg(xv, "Failed to open shared library (%s)\n", dlerror()); + return -EINVAL; + } + + plugin_ops = dlsym(plugin_so, XLNX_VIDEO_PLUGIN_SYMBOL_NAME); + if (!plugin_ops) { + xlnx_dbg(xv, "Plugin symbol not found (%s)\n", dlerror()); + ret = -EINVAL; + goto error; + } + + plugin = calloc(1, sizeof(*plugin)); + if (!plugin) { + ret = -ENOMEM; + goto error; + } + + plugin->handle = plugin_so; + plugin->ops = plugin_ops; + plugin->info = plugin_ops->info(); + + list_append(&plugin->list, &xv->plugins); + + xlnx_dbg(xv, "Plugin loaded successfully\n"); + return 0; + +error: + dlclose(plugin_so); + return ret; +} + +/** + * \brief Load plugins from a given directory + * \param xv the library context + * \param path the full path name to the directory containing plugins + * + * This function attempts to load all plugins from the \a path directory. A + * plugin is a shared object whose name ends with '.so' and contains a symbol + * named '__xlnx_video_plugin' pointing to a struct xlnx_video_plugin_ops + * instance. + * + * When a valid plugin is found and loaded, call the init function supplied in + * the plugin operations structure. + * + * Shared objects that fail to load, don't export the plugin operations + * structure, don't implement the init function or return an error from their + * init function are ignored and not considered as a fatal error. + * + * \return Return the number of plugins successfully loaded, or a negative error + * code if a fatal error occurs. + */ +int xlnx_video_plugin_load_directory(struct xlnx_video *xv, const char *path) +{ + struct dirent *entry; + unsigned int count = 0; + size_t pathlen; + DIR *dir; + int ret = 0; + + xlnx_dbg(xv, "Loading plugins from directory %s\n", path); + + dir = opendir(path); + if (!dir) { + xlnx_dbg(xv, "Failed to open directory\n"); + return -errno; + } + + pathlen = strlen(path); + + while ((entry = readdir(dir))) { + char *filename; + size_t len; + + if (entry->d_type != DT_REG && entry->d_type != DT_LNK && + entry->d_type != DT_UNKNOWN) + continue; + + len = strlen(entry->d_name); + if (strcmp(entry->d_name + len - 3, ".so")) + continue; + + filename = malloc(pathlen + 1 + len + 1); + if (!filename) + break; + + strcpy(filename, path); + filename[pathlen] = '/'; + strcpy(filename + pathlen + 1, entry->d_name); + + ret = xlnx_video_plugin_load_file(xv, filename); + free(filename); + + if (ret < 0) + continue; + + count++; + } + + closedir(dir); + + return ret < 0 ? ret : (int)count; +} + +/** \cond INTERNAL_API */ + +/** + * \brief Initialize plugin support + * \param xv the library context + * + * Initialize plugin support and load plugins from the system default plugin + * directory ('/usr/lib/xilinx/video/plugins' by default). + * + * \return 0 on success or a negative error code if an error occurs. + */ +int xlnx_video_plugin_init(struct xlnx_video *xv) +{ + xlnx_video_plugin_load_directory(xv, XLNX_VIDEO_PLUGIN_DIR); + + return 0; +} + +/** + * \brief Initialize plugin support + * \param xv the library context + * + * Initialize plugin support and load plugins from the system default plugin + * directory ('/usr/lib/xilinx/video/plugins' by default). + * + * \return 0 on success or a negative error code if an error occurs. + */ +void xlnx_video_plugin_fini(struct xlnx_video *xv) +{ + struct xlnx_video_plugin *plugin, *next; + + list_for_each_entry_safe(plugin, next, &xv->plugins, list) { + list_remove(&plugin->list); + dlclose(plugin->handle); + free(plugin); + } +} + +/** \endcond */ diff --git a/lib/xlnx-priv.h b/lib/xlnx-priv.h new file mode 100644 index 0000000..ca83425 --- /dev/null +++ b/lib/xlnx-priv.h @@ -0,0 +1,107 @@ +/* + * Xilinx Video Library - Internal API + * + * Copyright (C) 2014-2016 Ideas on board Oy + * + * 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. + */ +#ifndef __XLNX_PRIV_H__ +#define __XLNX_PRIV_H__ +/** \cond INTERNAL_API */ + +#include +#include + +#include +#include + +struct media_device; +struct media_entity; +struct media_entity_desc; + +/** + * \brief Xilinx video entity in a component + */ +struct xlnx_video_entity { + /** Entities list */ + struct list_entry list; + /** Media entity */ + struct media_entity *entity; + /** Media entity information */ + const struct media_entity_desc *info; + /** Indicates whether this is the last entity in the component */ + bool last; +}; + +/** + * \brief Xilinx video component + */ +struct xlnx_video_component { + /** The library context */ + struct xlnx_video *xv; + /** List entry in the context's components list */ + struct list_entry list; + /** List of video entities in the component */ + struct list_entry entities; + + /** Component type */ + enum xlnx_video_component_type type; + /** Component name */ + const char *name; + + /** + * Entity at the input of the component (NULL if the component has no + * input entity). + */ + struct xlnx_video_entity *input; + /** + * Entity at the output of the component (NULL if the component has no + * output entity). + */ + struct xlnx_video_entity *output; +}; + +/** + * \brief Xilinx video library context + */ +struct xlnx_video { + /** List of video components in the system */ + struct list_entry components; + /** Number of components in the components list */ + unsigned int num_components; + + /** List of loaded plugins */ + struct list_entry plugins; + + /** Event handler operations */ + const struct xlnx_video_event_operations *event_ops; + /** Event handler private data */ + void *event_handler; + + /** Log level for library messages */ + enum xlnx_log_level log_level; +}; + +int xlnx_video_plugin_init(struct xlnx_video *xv); +void xlnx_video_plugin_fini(struct xlnx_video *xv); +struct xlnx_video_component *xlnx_video_plugin_scan(struct xlnx_video *xv, + struct media_device *mdev); + +void xlnx_video_log_print(struct xlnx_video *xv, unsigned int level, + const char *func, const char *fmt, ...); + +#define xlnx_dbg(xv, fmt...) \ + xlnx_video_log_print(xv, XLNX_LOG_DEBUG, __func__, fmt) + +/** \endcond INTERNAL_API */ +#endif /* __XLNX_PRIV_H__ */ diff --git a/lib/xlnx-video.c b/lib/xlnx-video.c new file mode 100644 index 0000000..61c9f7c --- /dev/null +++ b/lib/xlnx-video.c @@ -0,0 +1,388 @@ +/* + * Xilinx Video Library + * + * Copyright (C) 2014-2016 Ideas on board Oy + * + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include "xlnx-priv.h" + +/** + * \mainpage + * + * The Xilinx video library offers a high-level API to enumerate video-related + * components in the system, assemble them in pipelines and stream video in and + * out of them. + * + * To use the library, an application must create a library context with a call + * to xlnx_video_create(). The function returns a context pointer that will be + * passed to all other library functions. Once a context isn't needed anymore it + * should be destroyed by a call to xlnx_video_destroy(). + * + * Multiple contexts can be created and managed separately. However, as contexts + * store information about all media devices present in the system, accessing + * the same media devices from multiple contexts isn't supported and leads to + * undefined behaviour. + * + * Right after creation contexts are not ready for use. They must be initialized + * by registering event handling operations with xlnx_video_set_event_handler(). + * Optionally logging can be initialized if desired with + * xlnx_video_set_log_level(). Finally, to complete initialization applications + * must call xlnx_video_setup() on the context. From that point onward the + * context can be used with other library functions. + */ + +/* ----------------------------------------------------------------------------- + * Components Discovery and Management + */ + +/** + * \brief Get a list of components in the system + * \param xv the library context + * \param type the desired component type + * \param [out] components array of returned components + * + * Store a list of components matching the requested \a type in the + * \a components array. + * + * The \a type parameter is a bitmask of components type. Components matching + * one of the types in the bitmask will be included in the list, all other + * components will be ignored. + * + * The \a components parameter points to an array of components pointer. + * The function allocates an array of component pointers, fills it with + * pointers to all components matching the requested \a type, and stores it in + * the \a components pointer. The components array is allocated using the C + * memory allocator and must be freed by the caller with a call to free(). + * + * The \a components array pointer can be NULL, in which case the function will + * not allocate any memory and will only return the number of matching + * components. + * + * If no component matches the requested \a type this function will not allocate + * any memory and will not touch the \a components parameter. + * + * Components must first be enumerated by a call to + * xlnx_video_component_enumerate() before calling this function. + * + * \return the total number of components matching the requested \a type found + * in the system and supported by the library (including the loaded plugins) on + * success, or a negative error code on failure. + */ +int xlnx_video_component_list(struct xlnx_video *xv, + enum xlnx_video_component_type type, + struct xlnx_video_component ***components) +{ + struct xlnx_video_component **comps; + struct xlnx_video_component *xvcomp; + unsigned int count; + + if (!xv->num_components) + return 0; + + count = 0; + list_for_each_entry(xvcomp, &xv->components, list) { + if (!(xvcomp->type & type)) + continue; + + count++; + } + + if (!count || !components) + return count; + + comps = calloc(count, sizeof(*comps)); + if (comps == NULL) + return -ENOMEM; + + count = 0; + list_for_each_entry(xvcomp, &xv->components, list) { + if (!(xvcomp->type & type)) + continue; + + comps[count++] = xvcomp; + } + + *components = comps; + return count; +} + +/** + * \brief Get the component name + * \param xvcomp the component + * + * \return the name of the component \a xvcomp. The name string is valid for the + * lifetime of the component. + */ +const char *xlnx_video_component_name(const struct xlnx_video_component *xvcomp) +{ + return xvcomp->name; +} + +/** + * \brief Get the component type + * \param xvcomp the component + * + * \return the type of the component \a xvcomp. + */ +enum xlnx_video_component_type +xlnx_video_component_type(const struct xlnx_video_component *xvcomp) +{ + return xvcomp->type; +} + +/* ----------------------------------------------------------------------------- + * Components Discovery + */ + +static int xlnx_video_component_scan_media(struct xlnx_video *xv, + struct media_device *media) +{ + struct xlnx_video_component *xvcomp; + + xvcomp = xlnx_video_plugin_scan(xv, media); + if (xvcomp) { + list_append(&xv->components, &xvcomp->list); + xv->num_components++; + } + + return 0; +} + +static int xlnx_video_component_scan(struct xlnx_video *xv) +{ + struct media_enumerate *media_enum; + struct media_device **devices; + unsigned int num_devices; + unsigned int i; + int ret; + + /* Start by scanning media controller devices. */ + media_enum = media_enumerate_new(); + if (!media_enum) + return -ENOMEM; + + ret = media_enumerate_scan(media_enum); + if (ret < 0) + goto done; + + num_devices = media_enumerate_get_devices_count(media_enum); + if (num_devices == 0) { + ret = -ENXIO; + goto done; + } + + devices = media_enumerate_get_devices(media_enum); + + for (i = 0; i < num_devices; ++i) { + ret = xlnx_video_component_scan_media(xv, devices[i]); + if (ret < 0) + goto done; + } + +done: + media_enumerate_unref(media_enum); + return ret; +} + +/** + * \brief Enumerate components in the system + * \param xv the library context + * + * This function enumerates all supported components in the system and cache + * them in the library context \a xv. It must be called once after initializing + * the context and before calling any other of the component-related functions. + * + * \return 0 on success or a negative error code otherwise. + */ +int xlnx_video_component_enumerate(struct xlnx_video *xv) +{ + int ret; + + ret = xlnx_video_component_scan(xv); + if (ret < 0) + return ret; + + return 0; +} + +/* ----------------------------------------------------------------------------- + * Initialization + */ + +/** + * \brief Create a working context for the library + * + * The Xilinx video library uses context objects to store internal data. + * Applications must call this function to create and initialize a context + * that can then be passed to other library functions. + * + * Multiple contexts can be created and managed separately. However, as contexts + * store information about all media devices present in the system, accessing + * the same media devices from multiple contexts isn't supported and leads to + * undefined behaviour. + * + * When a context isn't needed anymore applications must call the + * xlnx_video_destroy() function to destroy of it. + * + * \return a context pointer on success or NULL if an error occurs. + */ +struct xlnx_video *xlnx_video_create(void) +{ + struct xlnx_video *xv; + + xv = calloc(1, sizeof *xv); + if (!xv) + return NULL; + + list_init(&xv->components); + list_init(&xv->plugins); + + xv->log_level = XLNX_LOG_NONE; + + return xv; +} + +/** + * \brief Setup a library context to make it ready for use + * \param xv the library context + * + * After creating and initializing a library context \a xv, applications must + * call this function to finish context setup and make it ready for use. + * + * \return 0 on success or a negative error code otherwise. + */ +int xlnx_video_setup(struct xlnx_video *xv) +{ + int ret; + + ret = xlnx_video_plugin_init(xv); + if (ret < 0) + return ret; + + return 0; +} + +/** + * \brief Destroy a library context + * \param xv the library context + * + * Applications must call this function to destroy library contexts created + * by xlnx_video_create() when they're not needed anymore, and at the latest + * when the application exits. The context pointer \a xv becomes invalid when + * this function is called. + */ +void xlnx_video_destroy(struct xlnx_video *xv) +{ + xlnx_video_plugin_fini(xv); + free(xv); +} + +/** + * \brief Register event handler operations + * \param xv the library context + * \param ops the event handling operations + * \param handler the event handler private data + * + * The library requires the application to provide an implementation of a set of + * operations to handle event notifications on file descriptors. + * + * This function sets the event handler operations \a ops and private data + * pointer \a handler for the library context. It must be called after creating + * a context with xlnx_video_create() and before initializing it with + * xlnx_video_init(). + */ +void xlnx_video_set_event_handler(struct xlnx_video *xv, + const struct xlnx_video_event_operations *ops, + void *handler) +{ + xv->event_ops = ops; + xv->event_handler = handler; +} + +/** + * \brief Retrieve the event handler private data registered with the context + * \param xv the library context + * + * \return the event handler private data pointer registered with the library + * context by a previous call to xlnx_video_set_event_handler(). + */ +void *xlnx_video_get_event_handler_data(struct xlnx_video *xv) +{ + return xv->event_handler; +} + +/** + * \brief Log a library message + * \param xv the library context + * \param level the message log level + * \param fmt the message format + * \param ... the message arguments + * + * Log the message specified by \a format and \a ... arguments using the library + * logging infrastructure. The message will be logged only if its \a level is + * lower than or equal to the \a xv library context current log level, as set by + * xlnx_log_set_level(). Otherwise the message will be dropped silently. + */ +void xlnx_video_log_print(struct xlnx_video *xv, unsigned int level, + const char *func, const char *fmt, ...) +{ + struct timespec ts; + va_list ap; + + if (level == XLNX_LOG_NONE || level > xv->log_level) + return; + + clock_gettime(CLOCK_MONOTONIC, &ts); + + printf("[%lu.%06lu] %s: ", ts.tv_sec, ts.tv_nsec / 1000, func); + + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); +} + +/** + * \brief Set the library context log level + * \param xv the library context + * \param level the desired log level + * + * The Xilinx video library can print error, informational and debug messages + * to help debugging applications and library internals. Only messages whose log + * level is lower than or equal to the library context log level are printed, + * other messages are dropped silently + * + * This function sets the active log level for the context \a xv to \a level. + * The default log level is XLNX_LOG_NONE, which drops all log messages. + */ +void xlnx_video_set_log_level(struct xlnx_video *xv, enum xlnx_log_level level) +{ + xv->log_level = level; +} diff --git a/plugins/Makefile b/plugins/Makefile new file mode 100644 index 0000000..472d4d1 --- /dev/null +++ b/plugins/Makefile @@ -0,0 +1,12 @@ +SUBDIRS=dummy + +recursive=all clean install + +all: + +$(recursive): + @target=$@ ; \ + for subdir in $(SUBDIRS); do \ + echo "Making $$target in $$subdir" ; \ + $(MAKE) -C $$subdir $$target; \ + done diff --git a/plugins/dummy/Makefile b/plugins/dummy/Makefile new file mode 100644 index 0000000..0fb053b --- /dev/null +++ b/plugins/dummy/Makefile @@ -0,0 +1,21 @@ +AR := $(CROSS_COMPILE)ar +CC := $(CROSS_COMPILE)gcc +CFLAGS := -O2 -W -Wall -Wno-unused-parameter -I$(KDIR)/usr/include -I$(topdir)/include -I. -fPIC +LDFLAGS := + +OBJECTS := xlnx-dummy.o +TARGET := xlnx-dummy.so + +%.o : %.c + $(CC) $(CFLAGS) -c -o $@ $< + +all: $(TARGET) + +$(TARGET): $(OBJECTS) + $(CC) -o $@ -shared $^ + +clean: + -$(RM) *.o + -$(RM) $(TARGET) + +install: diff --git a/plugins/dummy/xlnx-dummy.c b/plugins/dummy/xlnx-dummy.c new file mode 100644 index 0000000..a43fc25 --- /dev/null +++ b/plugins/dummy/xlnx-dummy.c @@ -0,0 +1,42 @@ +/* + * Xilinx Video Library - Dummy plugin that supports no device + * + * Copyright (C) 2016 Ideas on board Oy + * + * 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. + */ + +#include + +#include + +static const struct xlnx_video_plugin_info dummy_plugin_info = { + .name = "Dummy plugin", + .type = XLNX_VIDEO_PLUGIN_TYPE_SOFTWARE, +}; + +static const struct xlnx_video_plugin_info *dummy_info(void) +{ + return &dummy_plugin_info; +} + +static struct xlnx_video_component *dummy_scan(struct xlnx_video *xv, + struct media_device *mdev) +{ + return NULL; +} + +EXPORT_XLNX_VIDEO_PLUGIN(dummy) = { + .info = dummy_info, + .scan = dummy_scan, +}; diff --git a/plugins/v4l2/Makefile b/plugins/v4l2/Makefile new file mode 100644 index 0000000..fcce631 --- /dev/null +++ b/plugins/v4l2/Makefile @@ -0,0 +1,22 @@ +AR := $(CROSS_COMPILE)ar +CC := $(CROSS_COMPILE)gcc +CFLAGS := -O2 -W -Wall -Wno-unused-parameter -I$(KDIR)/usr/include -I$(topdir)/3rdparty/media-ctl/include -I$(topdir)/include -I. -fPIC +LDFLAGS := -L$(topdir)/3rdparty/media-ctl +LIBS := -lmediactl + +OBJECTS := xlnx-v4l2.o +TARGET := xlnx-v4l2.so + +%.o : %.c + $(CC) $(CFLAGS) -c -o $@ $< + +all: $(TARGET) + +$(TARGET): $(OBJECTS) + $(CC) -o $@ -shared $^ + +clean: + -$(RM) *.o + -$(RM) $(TARGET) + +install: diff --git a/plugins/v4l2/v4l2.c b/plugins/v4l2/v4l2.c new file mode 100644 index 0000000..c0fdd54 --- /dev/null +++ b/plugins/v4l2/v4l2.c @@ -0,0 +1,721 @@ +/* + * Xilinx Video Library - Generic V4L2 support + * + * Copyright (C) 2014-2016 Ideas on board Oy + * + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +#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; + dev->name = strdup(devname); + 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); + } + + free(dev->name); + 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("%s: unable to get control (%d).\n", dev->name, 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("%s: unable to set control (%d).\n", dev->name, 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("%s: unable to get multiple controls (%d).\n", dev->name, + 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("%s: unable to set multiple controls (%d).\n", dev->name, + 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("%s: unable to get crop rectangle (%d).\n", dev->name, + 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("%s: unable to set crop rectangle (%d).\n", dev->name, + 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("%s: unable to get format (%d).\n", dev->name, 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("%s: unable to set format (%d).\n", dev->name, 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 xlnx_video_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("%s: unable to request buffers (%d).\n", dev->name, + errno); + ret = -errno; + goto done; + } + + if (rb.count > pool->nbufs) { + printf("%s: driver needs more buffers (%u) than available (%u).\n", + dev->name, rb.count, pool->nbufs); + ret = -E2BIG; + goto done; + } + + dev->nbufs = rb.count; + + printf("%s: %u buffers requested.\n", dev->name, 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("%s: unable to query buffer %u (%d).\n", + dev->name, 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("%s: unable to map buffer %u (%d)\n", + dev->name, i, errno); + ret = -errno; + goto done; + } + pool->buffers[i].size = buf.length; + printf("%s: buffer %u mapped at address %p.\n", + dev->name, i, pool->buffers[i].mem); + break; + + case V4L2_MEMORY_USERPTR: + if (pool->buffers[i].size < buf.length) { + printf("%s: buffer %u too small (%u bytes required, " + "%u bytes available.\n", dev->name, i, + buf.length, pool->buffers[i].size); + ret = -EINVAL; + goto done; + } + + printf("%s: buffer %u valid.\n", dev->name, 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 xlnx_video_buffer *buffer = &dev->pool->buffers[i]; + + if (buffer->mem == NULL) + continue; + + ret = munmap(buffer->mem, buffer->size); + if (ret < 0) { + printf("%s: unable to unmap buffer %u (%d)\n", + dev->name, 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("%s: unable to release buffers (%d)\n", dev->name, + errno); + return -errno; + } + + dev->nbufs = 0; + dev->pool = NULL; + + return 0; +} + +int v4l2_dequeue_buffer(struct v4l2_device *dev, struct xlnx_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("%s: unable to dequeue buffer index %u/%u (%d)\n", + dev->name, buf.index, dev->nbufs, errno); + return -errno; + } + + *buffer = dev->pool->buffers[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 xlnx_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("%s: unable to queue buffer index %u/%u (%d)\n", + dev->name, buf.index, dev->nbufs, 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/plugins/v4l2/v4l2.h b/plugins/v4l2/v4l2.h new file mode 100644 index 0000000..c2b683a --- /dev/null +++ b/plugins/v4l2/v4l2.h @@ -0,0 +1,289 @@ +/* + * Xilinx Video Library - Generic V4L2 support + * + * Copyright (C) 2014-2016 Ideas on board Oy + * + * 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. + */ +#ifndef __V4L2_H +#define __V4L2_H + +#include +#include +#include + +#include + +struct xlnx_video_buffers_pool; +struct xlnx_video_buffer; + +struct v4l2_device +{ + int fd; + char *name; + + 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 xlnx_video_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 xlnx_video_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 xlnx_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 xlnx_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 diff --git a/plugins/v4l2/xlnx-v4l2.c b/plugins/v4l2/xlnx-v4l2.c new file mode 100644 index 0000000..c307041 --- /dev/null +++ b/plugins/v4l2/xlnx-v4l2.c @@ -0,0 +1,417 @@ +/* + * Xilinx Video Library - V4L2 plugin + * + * Copyright (C) 2014-2016 Ideas on board Oy + * + * 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. + */ + +#if 0 +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +/*static inline struct xlnx_video_node * +to_xlnx_video_node(struct xlnx_video_entity *e) { + return container_of(e, struct xlnx_video_node, entity); +}*/ + +/* ----------------------------------------------------------------------------- + * Helper functions + */ + +/* + * struct xlnx_video_format_info - Media bus format information + * @code: V4L2 media bus format code + * @pixelformat: V4L2 pixel format FCC identifier + */ +struct xlnx_video_format_info { + enum v4l2_mbus_pixelcode code; + __u32 pixelformat; +}; + +static const struct xlnx_video_format_info formats[] = { + { MEDIA_BUS_FMT_RGB888_1X32_PADHI, V4L2_PIX_FMT_BGR32, }, + { MEDIA_BUS_FMT_UYVY8_1X16, V4L2_PIX_FMT_YUYV, }, + { MEDIA_BUS_FMT_VUY8_1X24, V4L2_PIX_FMT_YUV444, }, + { MEDIA_BUS_FMT_SRGGB8_1X8, V4L2_PIX_FMT_SGRBG8, }, + { MEDIA_BUS_FMT_SGRBG8_1X8, V4L2_PIX_FMT_SGRBG8, }, + { MEDIA_BUS_FMT_SGBRG8_1X8, V4L2_PIX_FMT_SGBRG8, }, + { MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_PIX_FMT_SBGGR8, }, +}; + +static __u32 mbus_to_pix(enum v4l2_mbus_pixelcode code) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(formats); ++i) { + if (formats[i].code == code) + return formats[i].pixelformat; + } + + return 0; +} + +static enum v4l2_mbus_pixelcode pix_to_mbus(__u32 pixelformat) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(formats); ++i) { + if (formats[i].pixelformat == pixelformat) + return formats[i].code; + } + + return 0; +} + +/* ----------------------------------------------------------------------------- + * Video streaming + */ + +int xlnx_video_component_set_format(struct xlnx_video_component *xvcomp, + struct v4l2_mbus_framefmt *fmt) +{ + struct v4l2_mbus_framefmt format; + struct v4l2_pix_format v4l2_fmt; + struct xlnx_video_entity *source = NULL; + struct xlnx_video_entity *sink; + struct xlnx_video_node *video; + struct media_entity_pad *pad; + int ret; + + /* Configure formats. Start from the input and propagate the format + * through the component. + */ + format = *initial_fmt; + + list_for_each_entry(sink, &xvcomp->entities, list) { + if (source == NULL) { + /* Configure the first entity in the component with the + * initial format. + */ + pad = sink->source.link->source; + + if ((sink->info->type & MEDIA_ENT_TYPE_MASK) == MEDIA_ENT_T_V4L2_SUBDEV) { + ret = v4l2_subdev_set_format(sink->entity, &format, 0, + V4L2_SUBDEV_FORMAT_ACTIVE); + if (ret < 0) { + printf("error: set format on component input failed.\n"); + return ret; + } + } else if (sink->info->type == MEDIA_ENT_T_DEVNODE_V4L) { + video = to_xlnx_video_node(sink); + + memset(&v4l2_fmt, 0, sizeof v4l2_fmt); + v4l2_fmt.pixelformat = mbus_to_pix(format.code); + v4l2_fmt.width = format.width; + v4l2_fmt.height = format.height; + + ret = v4l2_set_format(video->video, &v4l2_fmt); + if (ret < 0) { + printf("error: set format failed on %s.\n", + sink->info->name); + return ret; + } + + format.code = pix_to_mbus(v4l2_fmt.pixelformat); + format.width = v4l2_fmt.width; + format.height = v4l2_fmt.height; + + video->format = format; + } + + source = sink; + continue; + } + + if ((source->info->type & MEDIA_ENT_TYPE_MASK) == MEDIA_ENT_T_V4L2_SUBDEV) { + pad = source->source.link->source; + + /* Try to force the output format code onto the source pad. */ + ret = v4l2_subdev_get_format(pad->entity, &format, pad->index, + V4L2_SUBDEV_FORMAT_ACTIVE); + if (ret < 0) { + printf("error: set format failed on %s:%u.\n", + pad->entity->info->name, pad->index); + return ret; + } + + if (sink->last) + format = *fmt; + else + format.code = fmt->code; + + ret = v4l2_subdev_set_format(pad->entity, &format, pad->index, + V4L2_SUBDEV_FORMAT_ACTIVE); + if (ret < 0) { + printf("error: set format failed on %s:%u.\n", + pad->entity->info.name, pad->index); + return ret; + } + + if (sink->last) + *fmt = format; + } + + source->source.format = format; + + if ((sink->info->type & MEDIA_ENT_TYPE_MASK) == MEDIA_ENT_T_V4L2_SUBDEV) { + /* Propagate the format to the link target. */ + pad = sink->sink.link->sink; + + ret = v4l2_subdev_set_format(pad->entity, &format, pad->index, + V4L2_SUBDEV_FORMAT_ACTIVE); + if (ret < 0) { + printf("error: set format failed on %s:%u.\n", + pad->entity->info.name, pad->index); + return ret; + } + } else if (sink->info->type == MEDIA_ENT_T_DEVNODE_V4L) { + video = to_xlnx_video_node(sink); + + /* Set the capture format on the capture or output video + * node. If the connected source is a pool, v4l2_fmt + * already contains the format that has been setup on + * the associated capture video node. + */ + memset(&v4l2_fmt, 0, sizeof v4l2_fmt); + v4l2_fmt.pixelformat = mbus_to_pix(format.code); + v4l2_fmt.width = format.width; + v4l2_fmt.height = format.height; + + ret = v4l2_set_format(video->video, &v4l2_fmt); + if (ret < 0) { + printf("error: set format failed on %s.\n", + sink->info->name); + return ret; + } + + format.code = pix_to_mbus(v4l2_fmt.pixelformat); + format.width = v4l2_fmt.width; + format.height = v4l2_fmt.height; + + video->format = format; + } + + sink->sink.format = format; + + source = sink; + } + + return 0; +} + +static int xlnx_video_validate_entity(struct media_entity *entity, + const struct media_link **linkp) +{ + const struct media_link *link_source = NULL; + const struct media_link *link_sink = NULL; + const struct media_entity_desc *info; + unsigned int num_links; + unsigned int i; + + info = media_entity_get_info(entity); + + if (info->type == MEDIA_ENT_T_DEVNODE_V4L) { + const struct media_link *link; + + /* Video nodes must have a single pad and a single link. */ + if (info->pads != 1) + return -EINVAL; + + if (media_entity_get_links_count(entity) != 1) + return -EINVAL; + + link = media_entity_get_link(entity, 0); + *linkp = link->sink->entity == entity ? link : NULL; + return 0; + } + + if ((info->type & MEDIA_ENT_TYPE_MASK) != MEDIA_ENT_T_V4L2_SUBDEV) + return -EINVAL; + + /* + * Only components with linear internal pipelines are supported, the + * entity must have a single connected source and a single connected + * sink. + */ + num_links = media_entity_get_links_count(entity); + + for (i = 0; i < num_links; ++i) { + const struct media_link *link = media_entity_get_link(entity, i); + + /* + * Components with configurable internal pipelines are not + * supported. + */ + if (!(link->flags & MEDIA_LNK_FL_IMMUTABLE)) + return -EINVAL; + + if (!(link->flags & MEDIA_LNK_FL_ENABLED)) + continue; + + if (link->source->entity == entity) { + if (link_source && link_source != link) + return -EINVAL; + link_source = link; + } else { + if (link_sink && link_sink != link) + return -EINVAL; + link_sink = link; + } + } + + *linkp = link_sink; + return 0; +} + +static int xlnx_video_scan_component(struct xlnx_video_component *xvcomp) +{ + struct media_entity *entity; + + entity = xvcomp->output->entity; + + while (1) { + const struct media_link *link; + int ret; + + ret = xlnx_video_validate_entity(entity, &link); + if (ret < 0) + return ret; + + if (link == NULL) { + const struct media_entity_desc *info; + + info = media_entity_get_info(entity); + if (info->type == MEDIA_ENT_T_DEVNODE_V4L) + xvcomp->input.entity = entity; + break; + } + + entity = link->source->entity; + } + + return 0; +} + +static int xlnx_video_scan_media(struct xlnx_video *xv, struct media_device *media) +{ +} +#endif + +#include +#include + +#include +#include +#include + +#include + +#include + +static const struct xlnx_video_plugin_info v4l2_plugin_info = { + .name = "V4L2 plugin", + .type = XLNX_VIDEO_PLUGIN_TYPE_HARDWARE, +}; + +static const struct xlnx_video_plugin_info *v4l2_info(void) +{ + return &v4l2_plugin_info; +} + +/* + * \brief Xilinx V4L2 video node + */ +struct v4l2_video_node { + /* V4L2 device */ + struct v4l2_device *video; +#if 0 + /* Current video format */ + struct v4l2_mbus_framefmt format; + struct xlnx_video_buffers_pool *pool; + /* Number of buffers queued to the driver */ + unsigned int queued; + /* Whether video capture is running on the device */ + bool running; +#endif +}; + +struct v4l2_component { + struct xlnx_video_component *xvcomp; + + struct v4l2_video_node *input; + struct v4l2_video_node *output; +}; + +static struct xlnx_video_component *v4l2_scan(struct xlnx_video *xv, + struct media_device *mdev) +{ + struct media_entity *entity; + unsigned int num_entities; + unsigned int i; + + num_entities = media_get_entities_count(mdev); + if (num_entities == 0) + return 0; + + for (i = 0; i < num_entities; ++i) { + const struct media_entity_desc *info; + const struct media_pad *pad; + struct v4l2_component *xvcomp; + int ret; + + entity = media_get_entity(mdev, i); + info = media_entity_get_info(entity); + + if (info->type != MEDIA_ENT_T_DEVNODE_V4L || + info->pads != 1) + continue; + + pad = media_entity_get_pad(entity, 0); + if (!(pad->flags & MEDIA_PAD_FL_SINK)) + continue; + + xvcomp = calloc(1, sizeof *xvcomp); + if (!xvcomp) + return -ENOMEM; + + list_init(&xvcomp->entities); + xvcomp->xv = xv; + xvcomp->output.entity = entity; + + ret = xlnx_video_scan_component(xvcomp); + if (ret < 0) { + free(xvcomp); + return ret; + } + + list_append(&xv->components, &xvcomp->list); + xv->num_components++; + } + + return 0; +} + +EXPORT_XLNX_VIDEO_PLUGIN(v4l2) = { + .info = v4l2_info, + .scan = v4l2_scan, +}; diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..1bea07c --- /dev/null +++ b/src/Makefile @@ -0,0 +1,22 @@ +CC := $(CROSS_COMPILE)gcc +CFLAGS := -O2 -W -Wall -Wno-unused-parameter -I$(KDIR)/usr/include -I$(topdir)/include -I. -fPIC +LDFLAGS := -L$(topdir)/lib +LIBS := -lxlnxvideo + +OBJECTS := main.o + +TARGET := xlnx-video + +all: $(TARGET) + +$(TARGET): $(OBJECTS) + $(CC) -o $@ $^ $(LDFLAGS) $(LIBS) + +%.o : %.c + $(CC) $(CFLAGS) -c -o $@ $< + +clean: + -$(RM) *.o + -$(RM) $(TARGET) + +install: diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..133c178 --- /dev/null +++ b/src/main.c @@ -0,0 +1,213 @@ +/* + * Xilinx Video Library - Command line sample application + * + * Copyright (C) 2016 Ideas on board Oy + * + * Contact: Laurent Pinchart + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY [LICENSOR] "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include + +#include +#include +#include + +static const char *component_type_name(enum xlnx_video_component_type type) +{ + static const struct { + enum xlnx_video_component_type type; + const char *name; + } types[] = { + { XLNX_VIDEO_COMPONENT_CAPTURE, "capture" }, + { XLNX_VIDEO_COMPONENT_PROCESS, "process" }, + { XLNX_VIDEO_COMPONENT_DISPLAY, "display" }, + }; + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(types); ++i) { + if (types[i].type == type) + return types[i].name; + } + + return "unknown"; +} + +static void list_components(struct xlnx_video *xv) +{ + struct xlnx_video_component **components; + int num_components; + int i; + + num_components = xlnx_video_component_list(xv, XLNX_VIDEO_COMPONENT_ANY, + &components); + if (num_components < 0) { + printf("Failed to list components\n"); + return; + } else if (!num_components) { + printf("No components found.\n"); + return; + } + + printf("%u component%s found\n", num_components, + num_components > 1 ? "s" : ""); + + for (i = 0; i < num_components; ++i) { + const struct xlnx_video_component *component = components[i]; + enum xlnx_video_component_type type; + const char *name; + + type = xlnx_video_component_type(component); + name = xlnx_video_component_name(component); + + printf("- Component %u: type %s name %s\n", i, + component_type_name(type), name); + } + + free(components); +} + +/* ----------------------------------------------------------------------------- + * Command Line Parsing + */ + +static unsigned int opt_debug; +static bool opt_list_components; +static const char *opt_plugins_directory; + +static void usage(const char *argv0) +{ + printf("%s [options]\n", argv0); + printf("-c, --components List available components\n"); + printf("-d, --debug level Set the debug level (0-4, default: 0)\n"); + printf("-h, --help Show this help message and exit\n"); + printf("-p, --plugins dir Load plugins from given directory\n"); +} + +static struct option opts[] = { + {"components", 0, 0, 'c'}, + {"debug", 1, 0, 'd'}, + {"help", 0, 0, 'h'}, + {"plugins", 1, 0, 'p'}, +}; + +static int parse_cmdline(int argc, char **argv) +{ + char *endp; + int opt; + + /* Parse options */ + while ((opt = getopt_long(argc, argv, "cd:hp:", opts, NULL)) != -1) { + switch (opt) { + case 'c': + opt_list_components = true; + break; + + case 'd': + opt_debug = strtoul(optarg, &endp, 10); + if (*endp != '\0') { + fprintf(stderr, "Invalid debug level %s\n", optarg); + usage(argv[0]); + return 1; + } + break; + + case 'h': + usage(argv[0]); + return 1; + + case 'p': + opt_plugins_directory = optarg; + break; + } + } + + return 0; +} + +/* ----------------------------------------------------------------------------- + * Main + */ + +int main(int argc, char *argv[]) +{ + struct xlnx_video *xv; + int ret; + + ret = parse_cmdline(argc, argv); + if (ret) + return ret; + + xv = xlnx_video_create(); + if (!xv) { + printf("Failed to create a library context\n"); + return 1; + } + + if (opt_debug) + xlnx_video_set_log_level(xv, opt_debug); + + ret = xlnx_events_init(xv); + if (ret < 0) { + printf("Failed to initialize events handling\n"); + goto done; + } + + ret = xlnx_video_setup(xv); + if (ret < 0) { + printf("Failed to setup the library context\n"); + goto done; + } + + if (opt_plugins_directory) { + ret = xlnx_video_plugin_load_directory(xv, opt_plugins_directory); + if (ret < 0) { + printf("Failed to load plugins from directory `%s'\n", + opt_plugins_directory); + goto done; + } + + printf("Loaded %u plugin%s from directory `%s'\n", ret, + ret != 1 ? "s" : "", opt_plugins_directory); + } + + ret = xlnx_video_component_enumerate(xv); + if (ret < 0) { + printf("Failed to enumerate components\n"); + goto done; + } + + if (opt_list_components) + list_components(xv); + +done: + xlnx_events_fini(xv); + xlnx_video_destroy(xv); + return ret; +} -- cgit v1.2.3