From b74fd506af7104a94e0755f668b0727f50228525 Mon Sep 17 00:00:00 2001 From: cainmagi Date: Mon, 19 Jul 2021 04:17:27 -0500 Subject: [PATCH 01/15] 3.1.0 Upload the Linux building script. --- .gitattributes | 2 + .gitignore | 172 +++ CHANGELOG.md | 125 ++ LICENSE | 674 ++++++++++ MpegCoder/MpegBase.h | 107 ++ MpegCoder/MpegCoder.cpp | 1957 ++++++++++++++++++++++++++++ MpegCoder/MpegCoder.h | 138 ++ MpegCoder/MpegPyd.h | 1616 +++++++++++++++++++++++ MpegCoder/MpegStreamer.cpp | 2189 ++++++++++++++++++++++++++++++++ MpegCoder/MpegStreamer.h | 179 +++ MpegCoder/dllmain.cpp | 48 + MpegCoder/snprintf.cpp | 75 ++ README.md | 106 ++ find_libpython/__init__.py | 360 ++++++ find_libpython/__main__.py | 5 + find_libpython/_version.py | 5 + mpegCoder-linux.code-workspace | 19 + setup.py | 102 ++ webtools.py | 304 +++++ 19 files changed, 8183 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 LICENSE create mode 100644 MpegCoder/MpegBase.h create mode 100644 MpegCoder/MpegCoder.cpp create mode 100644 MpegCoder/MpegCoder.h create mode 100644 MpegCoder/MpegPyd.h create mode 100644 MpegCoder/MpegStreamer.cpp create mode 100644 MpegCoder/MpegStreamer.h create mode 100644 MpegCoder/dllmain.cpp create mode 100644 MpegCoder/snprintf.cpp create mode 100644 README.md create mode 100644 find_libpython/__init__.py create mode 100644 find_libpython/__main__.py create mode 100644 find_libpython/_version.py create mode 100644 mpegCoder-linux.code-workspace create mode 100644 setup.py create mode 100644 webtools.py diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1c2bcbb --- /dev/null +++ b/.gitignore @@ -0,0 +1,172 @@ +# Others +include/* +lib/* +.vs/* +.vscode/* +*.pdb +*.pyd +*.ipdb +*.iobj +*.exp +*.log +*.tlog +*.lastbuildstate +unsuccessfulbuild +/MpegCoder/x64/ + +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..35630f0 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,125 @@ +# FFmpeg-Encoder-Decoder-for-Python + +## Update Report + +### V3.1.0 update report: + +1. Support `str()` type for all string arguments. + +2. Support `http`, `ftp`, `sftp` streams for `MpegServer`. + +3. Support `nthread` option for `MpegDecoder`, `MpegEncoder`, `MpegClient` and `MpegServer`. + +4. Fix typos in docstrings. + +### V3.0.0 update report: + +1. Fix a severe memory leaking bugs when using `AVPacket`. + +2. Fix a bug caused by using `MpegClient.terminate()` when a video is closed by the server. + +3. Support the `MpegServer`. This class is used for serving the online video streams. + +4. Refactor the implementation of the loggings. + +5. Add `getParameter()` and `setParameter(configDict)` APIs to `MpegEncoder` and `MpegServer`. + +6. Move `FFMpeg` depedencies and the `OutputStream` class to the `cmpc` space. + +7. Fix dependency issues and cpp standard issues. + +8. Upgrade to `FFMpeg 4.4` Version. + +9. Add a quick script for fetching the `FFMpeg` dependencies. + +### V2.05 update report: + +1. Fix a severe bug that causes the memory leak when using `MpegClient`.This bug also exists in `MpegDecoder`, but it seems that the bug would not cause memory leak in that case. (Although we have also fixed it now.) + +2. Upgrade to `FFMpeg 4.0` Version. + +### V2.01 update report: + +1. Fix a bug that occurs when the first received frame may has a PTS larger than zero. + +2. Enable the project produce the newest `FFMpeg 3.4.2` version and use `Python 3.6.4`, `numpy 1.14`. + +### V2.0 update report: + +1. Revise the bug of the encoder which may cause the stream duration is shorter than the real duration of the video in some not advanced media players. + +2. Improve the structure of the code and remove some unnecessary codes. + +3. Provide a complete version of client, which could demux the video stream from a server in any network protocol. + +### V1.8 update report: + +1. Provide options `(widthDst, heightDst)` to let `MpegDecoder` could control the output size manually. To ensure the option is valid, we must use the method `setParameter` before `FFmpegSetup`. Now you could use this options to get a rescaled output directly: + + ```python + d = mpegCoder.MpegDecoder() # initialize + d.setParameter(widthDst=400, heightDst=300) # noted that these options must be set before 'FFmpegSetup'! + d.FFmpegSetup(b'i.avi') # the original video size would not influence the output + print(d) # examine the parameters. You could also get the original video size by 'getParameter' + d.ExtractFrame(0, 100) # get 100 frames with 400x300 + ``` + + In another example, the set optional parameters could be inherited by encoder, too: + + ```python + d.setParameter(widthDst=400, heightDst=300) # set optional parameters + ... + e.setParameter(decoder=d) # the width/height would inherit from widthDst/heightDst rather than original width/height of the decoder. + ``` + + Noted that we do not provide `widthDst`/`heightDst` in `getParameter`, because these 2 options are all set by users. There is no need to get them from the video metadata. + +2. Optimize some realization of Decoder so that its efficiency could be improved. + +### V1.7-linux update report: + +Thanks to God, we succeed in this work! + +A new version is avaliable for Linux. To implement this tool, you need to install some libraries firstly: + +* python3.5 + +* numpy 1.13 + +If you want, you could install `ffmpeg` on Linux: Here are some instructions + +1. Check every pack which ffmpeg needs here: [Dependency of FFmpeg](https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu "Dependency of FFmpeg") + +2. Use these steps to install ffmpeg instead of provided commands on the above site. + +```Bash + $ git clone https://git.ffmpeg.org/ffmpeg.git + $ cd ffmpeg + $ ./configure --prefix=host --enable-gpl --enable-libx264 --enable-libx265 --enable-shared --disable-static --disable-doc + $ make + $ make install +``` + +### V1.7 update report: + +1. Realize the encoder totally. + +2. Provide a global option `dumpLevel` to control the log shown in the screen. + +3. Fix bugs in initialize functions. + +### V1.5 update report: + +1. Provide an incomplete version of encoder, which could encode frames as a video stream that could not be played by player. + +### V1.4 update report: + +1. Fix a severe bug of the decoder, which causes the memory collapsed if decoding a lot of frames. + +### V1.2 update report: + +1. Use numpy array to replace the native pyList, which improves the speed significantly. + +### V1.0 update report: + +1. Provide the decoder which could decode videos in arbitrary formats and arbitrary coding. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..94a9ed0 --- /dev/null +++ b/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, 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 +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If 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 convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU 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 +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "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 PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM 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 PROGRAM (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 PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state 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 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 3 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, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/MpegCoder/MpegBase.h b/MpegCoder/MpegBase.h new file mode 100644 index 0000000..5becc29 --- /dev/null +++ b/MpegCoder/MpegBase.h @@ -0,0 +1,107 @@ +#ifndef MPEGBASE_H_INCLUDED +#define MPEGBASE_H_INCLUDED + +#define MPEGCODER_EXPORTS +#ifdef MPEGCODER_EXPORTS + #define MPEGCODER_API __declspec(dllexport) +#else + #define MPEGCODER_API __declspec(dllimport) +#endif + +#define FFMPG3_4 +#define FFMPG4_0 +#define FFMPG4_4 + +#define MPEGCODER_CURRENT_VERSION "3.1.0" + +#define STREAM_PIX_FMT AVPixelFormat::AV_PIX_FMT_YUV420P /* default pix_fmt */ + +#define SCALE_FLAGS SWS_BICUBIC +//SWS_BILINEAR + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using std::string; +using std::cerr; +using std::cout; +using std::endl; +using std::ostream; + +namespace cmpc { + extern "C" + { + #include "libavcodec/avcodec.h" + #include "libavformat/avformat.h" + #include "libswscale/swscale.h" + #include "libavutil/imgutils.h" + #include "libavutil/samplefmt.h" + #include "libavutil/timestamp.h" + #include "libavutil/opt.h" + #include "libavutil/avassert.h" + #include "libavutil/channel_layout.h" + #include "libavutil/mathematics.h" + #include "libavutil/time.h" + #include "libswresample/swresample.h" + } +} + +#ifdef __cplusplus +namespace cmpc { + static const string av_make_error_string2(int errnum) { + char errbuf[AV_ERROR_MAX_STRING_SIZE]; + av_strerror(errnum, errbuf, AV_ERROR_MAX_STRING_SIZE); + string strerrbuf = errbuf; + return strerrbuf; + } + #undef av_err2str + #define av_err2str(errnum) av_make_error_string2(errnum).c_str() + static const string av_ts_make_string_cpp(int64_t ts) { + char tsstrbuf[AV_TS_MAX_STRING_SIZE]; + av_ts_make_string(tsstrbuf, ts); + string strtsstrbuf = tsstrbuf; + return strtsstrbuf; + } + #undef av_ts2str + #define av_ts2str(ts) av_ts_make_string_cpp(ts).c_str() + static const string av_ts_make_time_string_cpp(int64_t ts, AVRational* tb) { + char tsstrbuf[AV_TS_MAX_STRING_SIZE]; + av_ts_make_time_string(tsstrbuf, ts, tb); + string strtsstrbuf = tsstrbuf; + return strtsstrbuf; + } + #undef av_ts2timestr + #define av_ts2timestr(ts, tb) av_ts_make_time_string_cpp(ts, tb).c_str() +} +#endif // __cplusplus + +namespace cmpc { + // a wrapper around a single output AVStream + typedef struct _OutputStream { + AVStream* st; + AVCodecContext* enc; + + /* pts of the next frame that will be generated */ + int64_t next_frame; + + AVFrame* frame; + AVFrame* tmp_frame; + + struct SwsContext* sws_ctx; + } OutputStream; +} + +// compatibility with newer API +#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1) + #define av_frame_alloc avcodec_alloc_frame + #define av_frame_free avcodec_free_frame +#endif + +#endif \ No newline at end of file diff --git a/MpegCoder/MpegCoder.cpp b/MpegCoder/MpegCoder.cpp new file mode 100644 index 0000000..79ef157 --- /dev/null +++ b/MpegCoder/MpegCoder.cpp @@ -0,0 +1,1957 @@ +// MpegCoder.cpp: Define the exporated methods of the DLL files. + +#define NO_IMPORT_ARRAY +#define PY_ARRAY_UNIQUE_SYMBOL MPEGARRAY_API +#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#include +#include "MpegCoder.h" +#include "MpegStreamer.h" + +int8_t cmpc::__dumpControl = 1; + +// Constructors. +cmpc::CMpegDecoder::CMpegDecoder(void) + : PFormatCtx(nullptr), PCodecCtx(nullptr), width(0), height(0), PVideoStream(nullptr), \ + PVideoStreamIDX(-1), PVideoFrameCount(0), _duration(0), _predictFrameNum(0), RGBbuffer(nullptr), \ + PswsCtx(nullptr), currentGOPTSM(0), nthread(0), EndofGOP(false), widthDst(0), heightDst(0), \ + PPixelFormat(STREAM_PIX_FMT) { + videoPath.clear(); + _str_codec.clear(); + + /* Enable or disable frame reference counting. You are not supposed to support + * both paths in your application but pick the one most appropriate to your + * needs. Look for the use of refcount in this example to see what are the + * differences of API usage between them. */ + refcount = 1; +} + +void cmpc::CMpegDecoder::meta_protected_clear(void) { + auto protectWidth = widthDst; + auto protectHeight = heightDst; + auto protectNthread = nthread; + clear(); + widthDst = protectWidth; + heightDst = protectHeight; + nthread = protectNthread; +} + +void cmpc::CMpegDecoder::clear(void) { + width = height = 0; + widthDst = heightDst = 0; + PVideoStreamIDX = -1; + PVideoFrameCount = 0; + nthread = 0; + _duration = 0; + _predictFrameNum = 0; + currentGOPTSM = 0; + EndofGOP = false; + PPixelFormat = STREAM_PIX_FMT; + _str_codec.clear(); + //videoPath.clear(); + + PVideoStream = nullptr; + if (PswsCtx) { + sws_freeContext(PswsCtx); + PswsCtx = nullptr; + } + if (RGBbuffer) { + av_free(RGBbuffer); + RGBbuffer = nullptr; + } + if (PCodecCtx) { + avcodec_free_context(&PCodecCtx); + PCodecCtx = nullptr; + } + if (PFormatCtx) { + avformat_close_input(&PFormatCtx); + PFormatCtx = nullptr; + } + refcount = 1; +} + +cmpc::CMpegDecoder::~CMpegDecoder() { + clear(); +} + +cmpc::CMpegDecoder::CMpegDecoder(const CMpegDecoder &ref) { + videoPath.assign(ref.videoPath); + if (!FFmpegSetup()) { + clear(); + } +} + +cmpc::CMpegDecoder& cmpc::CMpegDecoder::operator=(const CMpegDecoder &ref) { + videoPath.assign(ref.videoPath); + if (!FFmpegSetup()) { + clear(); + } + return *this; +} + +cmpc::CMpegDecoder::CMpegDecoder(CMpegDecoder &&ref) noexcept + : PFormatCtx(ref.PFormatCtx), PCodecCtx(ref.PCodecCtx), width(ref.width), height(ref.height), \ + PVideoStream(ref.PVideoStream), PVideoStreamIDX(ref.PVideoStreamIDX), refcount(ref.refcount), \ + PVideoFrameCount(ref.PVideoFrameCount), _str_codec(ref._str_codec), _duration(ref._duration), \ + _predictFrameNum(ref._predictFrameNum), RGBbuffer(ref.RGBbuffer), PPixelFormat(ref.PPixelFormat) \ + , PswsCtx(ref.PswsCtx), currentGOPTSM(ref.currentGOPTSM), EndofGOP(ref.EndofGOP), \ + widthDst(ref.widthDst), heightDst(ref.heightDst), nthread(ref.nthread) { + ref.PFormatCtx = nullptr; + ref.PCodecCtx = nullptr; + ref.PVideoStream = nullptr; + ref.PswsCtx = nullptr; +} + +cmpc::CMpegDecoder& cmpc::CMpegDecoder::operator=(CMpegDecoder &&ref) noexcept { + if (this != &ref) { + clear(); + width = ref.width; + height = ref.height; + widthDst = ref.widthDst; + heightDst = ref.heightDst; + PVideoStreamIDX = ref.PVideoStreamIDX; + videoPath.assign(ref.videoPath); + _str_codec.assign(ref._str_codec); + _duration = ref._duration; + _predictFrameNum = ref._predictFrameNum; + PFormatCtx = ref.PFormatCtx; + PCodecCtx = ref.PCodecCtx; + PVideoStream = ref.PVideoStream; + PVideoFrameCount = ref.PVideoFrameCount; + currentGOPTSM = ref.currentGOPTSM; + EndofGOP = ref.EndofGOP; + RGBbuffer = ref.RGBbuffer; + PPixelFormat = ref.PPixelFormat; + PswsCtx = ref.PswsCtx; + nthread = ref.nthread; + ref.PFormatCtx = nullptr; + ref.PCodecCtx = nullptr; + ref.PVideoStream = nullptr; + ref.RGBbuffer = nullptr; + ref.PswsCtx = nullptr; + refcount = ref.refcount; + } + return *this; +} + +void cmpc::CMpegDecoder::resetPath(string inVideoPath) { + videoPath.assign(inVideoPath); +} + +//以下是封装FFmpeg的C接口 +void cmpc::CMpegDecoder::setGOPPosition(int64_t inpos) { + currentGOPTSM = __FrameToPts(inpos); + EndofGOP = false; +} + +void cmpc::CMpegDecoder::setGOPPosition(double inpos) { + currentGOPTSM = __TimeToPts(inpos); + EndofGOP = false; +} + +int cmpc::CMpegDecoder::_open_codec_context(int &stream_idx, AVCodecContext *&dec_ctx, \ + AVFormatContext *PFormatCtx, enum AVMediaType type) { // Search the correct decoder, and make the configurations. + auto ret = av_find_best_stream(PFormatCtx, type, -1, -1, nullptr, 0); + if (ret < 0) { + cerr << "Could not find "<< av_get_media_type_string(type) << \ + " stream in input file '" << videoPath << "'" << endl; + return ret; + } + else { + auto stream_index = ret; + auto st = PFormatCtx->streams[stream_index]; // The AVStream object. + + /* find decoder for the stream */ + auto dec = avcodec_find_decoder(st->codecpar->codec_id); // Decoder (AVCodec). + if (!dec) { + cerr << "Failed to find " << av_get_media_type_string(type) << " codec" << endl; + return AVERROR(EINVAL); + } + _str_codec.assign(dec->name); + + /* Allocate a codec context for the decoder */ + auto dec_ctx_ = avcodec_alloc_context3(dec); // Decoder context (AVCodecContext). + if (!dec_ctx_) { + cerr << "Failed to allocate the " << av_get_media_type_string(type) << " codec context" << endl; + return AVERROR(ENOMEM); + } + + if (nthread > 0) { + dec_ctx_->thread_count = nthread; + } + + /* Copy codec parameters from input stream to output codec context */ + if ((ret = avcodec_parameters_to_context(dec_ctx_, st->codecpar)) < 0) { + cerr << "Failed to copy " << av_get_media_type_string(type) << \ + " codec parameters to decoder context" << endl; + return ret; + } + + /* Init the decoders, with or without reference counting */ + AVDictionary *opts = nullptr; // The uninitialized argument dictionary. + av_dict_set(&opts, "refcounted_frames", refcount ? "1" : "0", 0); + if ((ret = avcodec_open2(dec_ctx_, dec, &opts)) < 0) { + cerr << "Failed to open " << av_get_media_type_string(type) << " codec" << endl; + return ret; + } + dec_ctx = dec_ctx_; + stream_idx = stream_index; + } + return 0; +} + +bool cmpc::CMpegDecoder::FFmpegSetup(string inVideoPath) { + resetPath(inVideoPath); + return FFmpegSetup(); +} + +bool cmpc::CMpegDecoder::FFmpegSetup() { // Open the video file, and search the correct codec. + meta_protected_clear(); + int ret = 0; + + /* open input file, and allocate format context */ + if (avformat_open_input(&PFormatCtx, videoPath.c_str(), nullptr, nullptr) < 0) { + cerr << "Could not open source file " << videoPath << endl; + return false; + } + + /* retrieve stream information */ + if (avformat_find_stream_info(PFormatCtx, nullptr) < 0) { + cerr << "Could not find stream information" << endl; + return false; + } + + if (_open_codec_context(PVideoStreamIDX, PCodecCtx, PFormatCtx, AVMediaType::AVMEDIA_TYPE_VIDEO) >= 0) { + PVideoStream = PFormatCtx->streams[PVideoStreamIDX]; + auto time_base = PVideoStream->time_base; + auto frame_base = PVideoStream->avg_frame_rate; + + /* allocate image where the decoded image will be put */ + width = PCodecCtx->width; + height = PCodecCtx->height; + PPixelFormat = PCodecCtx->pix_fmt; + _duration = static_cast(PVideoStream->duration) / static_cast(time_base.den) * static_cast(time_base.num); + _predictFrameNum = av_rescale(static_cast(_duration*0xFFFF), frame_base.num, frame_base.den)/0xFFFF; + } + + /* dump input information to stderr */ + auto dump_level = av_log_get_level(); + if (dump_level >= AV_LOG_INFO) { + av_dump_format(PFormatCtx, 0, videoPath.c_str(), 0); + } + + if (!PVideoStream) { // Check whether the video stream is opened correctly. + cerr << "Could not find audio or video stream in the input, aborting" << endl; + clear(); + return false; + } + + // Initialize SWS context for software scaling. + if (widthDst > 0 && heightDst > 0) { + PswsCtx = sws_getContext(width, height, PPixelFormat, widthDst, heightDst, AVPixelFormat::AV_PIX_FMT_RGB24, SCALE_FLAGS, nullptr, nullptr, nullptr); + auto numBytes = av_image_get_buffer_size(AVPixelFormat::AV_PIX_FMT_RGB24, widthDst, heightDst, 1); + RGBbuffer = (uint8_t *)av_malloc(numBytes * sizeof(uint8_t)); + } + else { + PswsCtx = sws_getContext(width, height, PPixelFormat, width, height, AVPixelFormat::AV_PIX_FMT_RGB24, SCALE_FLAGS, nullptr, nullptr, nullptr); + auto numBytes = av_image_get_buffer_size(AVPixelFormat::AV_PIX_FMT_RGB24, width, height, 1); + RGBbuffer = (uint8_t *)av_malloc(numBytes * sizeof(uint8_t)); + } + return true; +} + +void cmpc::CMpegDecoder::dumpFormat() { + if ((!videoPath.empty()) && PFormatCtx) { + av_dump_format(PFormatCtx, 0, videoPath.c_str(), 0); + } + else { + cerr << "Still need to FFmpegSetup()" << endl; + } +} + +void cmpc::CMpegDecoder::setParameter(string keyword, void *ptr) { + if (keyword.compare("widthDst") == 0) { + auto ref = reinterpret_cast(ptr); + widthDst = *ref; + } + else if (keyword.compare("heightDst") == 0) { + auto ref = reinterpret_cast(ptr); + heightDst = *ref; + } + else if (keyword.compare("nthread") == 0) { + auto ref = reinterpret_cast(ptr); + if (PCodecCtx) { + PCodecCtx->thread_count = *ref; + } + nthread = *ref; + } +} + +PyObject* cmpc::CMpegDecoder::getParameter() { + auto res = PyDict_New(); + string key; + PyObject* val = nullptr; + // Fill the values. + key.assign("videoPath"); + val = Py_BuildValue("y", videoPath.c_str()); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("codecName"); + val = Py_BuildValue("y", _str_codec.c_str()); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + if (PCodecCtx) { + key.assign("bitRate"); + val = Py_BuildValue("L", PCodecCtx->bit_rate); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("GOPSize"); + val = Py_BuildValue("i", PCodecCtx->gop_size); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("maxBframe"); + val = Py_BuildValue("i", PCodecCtx->max_b_frames); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("nthread"); + val = Py_BuildValue("i", PCodecCtx->thread_count); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + } + else { + key.assign("nthread"); + val = Py_BuildValue("i", nthread); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + } + if (widthDst > 0) { + key.assign("widthDst"); + val = Py_BuildValue("i", widthDst); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + } + if (heightDst > 0) { + key.assign("heightDst"); + val = Py_BuildValue("i", heightDst); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + } + key.assign("width"); + val = Py_BuildValue("i", width); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("height"); + val = Py_BuildValue("i", height); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + if (PVideoStream) { + key.assign("frameRate"); + auto &frame_rate = PVideoStream->avg_frame_rate; + val = Py_BuildValue("(ii)", frame_rate.num, frame_rate.den); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + } + return res; +} + +PyObject * cmpc::CMpegDecoder::getParameter(string keyword) { + if (keyword.compare("videoPath") == 0) { + return PyUnicode_DecodeFSDefaultAndSize(videoPath.c_str(), static_cast(videoPath.size())); + } + else if (keyword.compare("width") == 0) { + return Py_BuildValue("i", width); + } + else if (keyword.compare("height") == 0) { + return Py_BuildValue("i", height); + } + else if (keyword.compare("frameCount") == 0) { + return Py_BuildValue("i", PVideoFrameCount); + } + else if (keyword.compare("coderName") == 0) { + return PyUnicode_DecodeFSDefaultAndSize(_str_codec.c_str(), static_cast(_str_codec.size())); + } + else if (keyword.compare("duration") == 0) { + return Py_BuildValue("d", _duration); + } + else if (keyword.compare("estFrameNum") == 0) { + return Py_BuildValue("L", _predictFrameNum); + } + else if (keyword.compare("avgFrameRate") == 0) { + auto frame_base = PVideoStream->avg_frame_rate; + double frameRate = static_cast(frame_base.num) / static_cast(frame_base.den); + return Py_BuildValue("d", frameRate); + } + else if (keyword.compare("nthread") == 0) { + if (PCodecCtx) { + return Py_BuildValue("i", PCodecCtx->thread_count); + } + else { + return Py_BuildValue("i", nthread); + } + } + else { + Py_RETURN_NONE; + } +} + +// The flush packet is a non-NULL packet with size 0 and data NULL +int cmpc::CMpegDecoder::__avcodec_decode_video2(AVCodecContext *avctx, AVFrame *frame, bool &got_frame, AVPacket *pkt) { + int ret; + + got_frame = false; + + if (pkt) { + ret = avcodec_send_packet(avctx, pkt); + // In particular, we don't expect AVERROR(EAGAIN), because we read all + // decoded frames with avcodec_receive_frame() until done. + if (ret < 0) { + //cout << ret << ", " << AVERROR(EAGAIN) << ", " << AVERROR_EOF << endl; + return ret == AVERROR_EOF ? 0 : ret; + } + } + + ret = avcodec_receive_frame(avctx, frame); + if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) + return ret; + if (ret >= 0) + got_frame = true; + + //cout << ret << ", " << AVERROR(EAGAIN) << ", " << AVERROR_EOF << endl; + + return 0; +} + +int cmpc::CMpegDecoder::_SaveFrame(PyObject *PyFrameList, AVFrame *&frame, AVFrame *&frameRGB, AVPacket *&pkt, bool &got_frame, int64_t minPTS, bool &processed, int cached) { + int ret = 0; + int decoded = pkt->size; + PyObject *OneFrame = nullptr; + + got_frame = false; + + if (pkt->stream_index == PVideoStreamIDX) { + /* decode video frame */ + ret = __avcodec_decode_video2(PCodecCtx, frame, got_frame, pkt); + if (ret < 0) { + cout << "Error decoding video frame (" << av_err2str(ret) << ")" << endl; + return ret; + } + + if (got_frame) { + + if (frame->pts < minPTS) { + //cout << frame->pts << " < " << minPTS << endl; + processed = false; + return decoded; + } + + if (frame->width != width || frame->height != height || + frame->format != PPixelFormat) { + /* To handle this change, one could call av_image_alloc again and + * decode the following frames into another rawvideo file. */ + cout << "Error: Width, height and pixel format have to be " + "constant in a rawvideo file, but the width, height or " + "pixel format of the input video changed:\n" + "old: width = " << width << ", height = " << height << ", format = " + << av_get_pix_fmt_name(PPixelFormat) << endl << + "new: width = " << frame->width << ", height = " << frame->height << ", format = " + << av_get_pix_fmt_name(static_cast(frame->format)) << endl; + return -1; + } + + + PVideoFrameCount++; + if (__dumpControl > 0) { + std::ostringstream str_data; + str_data << "video_frame" << (cached ? "(cached)" : "") << " n:" << PVideoFrameCount << + " coded_n:" << frame->coded_picture_number << endl; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_INFO, "%s", str_data_s.c_str()); + } + + /* copy decoded frame to destination buffer: + * this is required since rawvideo expects non aligned data */ + + sws_scale(PswsCtx, frame->data, frame->linesize, 0, height, frameRGB->data, frameRGB->linesize); + + /* write to rawvideo file */ + if (widthDst>0 && heightDst>0) + OneFrame = _SaveFrame_castToPyFrameArray(frameRGB->data, widthDst, heightDst); + else + OneFrame = _SaveFrame_castToPyFrameArray(frameRGB->data, width, height); + PyList_Append(PyFrameList, OneFrame); + processed = true; + } + } + + /* If we use frame reference counting, we own the data and need + * to de-reference it when we don't use it anymore */ + + if (got_frame && refcount) + av_frame_unref(frame); + + return decoded; +} + +int cmpc::CMpegDecoder::_SaveFrameForGOP(PyObject *PyFrameList, AVFrame *&frame, AVFrame *&frameRGB, AVPacket *&pkt, bool &got_frame, int &GOPstate, bool &processed, int cached) { + int ret = 0; + int decoded = pkt->size; + PyObject *OneFrame = nullptr; + + got_frame = false; + + if (pkt->stream_index == PVideoStreamIDX) { + /* decode video frame */ + ret = __avcodec_decode_video2(PCodecCtx, frame, got_frame, pkt); + if (ret < 0) { + cout << "Error decoding video frame (" << av_err2str(ret) << ")" << endl; + return ret; + } + + if (got_frame) { + + currentGOPTSM = frame->pts+1; + + switch (GOPstate) { + case 0: + if (frame->key_frame) { + GOPstate = 1; + } + else { + processed = false; + return decoded; + } + break; + case 1: + if (frame->key_frame) { + GOPstate = 2; + processed = false; + return decoded; + } + break; + default: + break; + } + + if (frame->width != width || frame->height != height || + frame->format != PPixelFormat) { + /* To handle this change, one could call av_image_alloc again and + * decode the following frames into another rawvideo file. */ + cout << "Error: Width, height and pixel format have to be " + "constant in a rawvideo file, but the width, height or " + "pixel format of the input video changed:\n" + "old: width = " << width << ", height = " << height << ", format = " + << av_get_pix_fmt_name(PPixelFormat) << endl << + "new: width = " << frame->width << ", height = " << frame->height << ", format = " + << av_get_pix_fmt_name(static_cast(frame->format)) << endl; + return -1; + } + + PVideoFrameCount++; + if (__dumpControl > 0) { + std::ostringstream str_data; + str_data << "video_frame" << (cached ? "(cached)" : "") << " n:" << PVideoFrameCount << + " coded_n:" << frame->coded_picture_number << endl; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_INFO, "%s", str_data_s.c_str()); + } + + /* copy decoded frame to destination buffer: + * this is required since rawvideo expects non aligned data */ + /*av_image_copy(video_dst_data, video_dst_linesize, + (const uint8_t **)frame->data, frame->linesize, + PPixelFormat, width, height);*/ + + sws_scale(PswsCtx, frame->data, frame->linesize, 0, height, frameRGB->data, frameRGB->linesize); + + /* write to rawvideo file */ + if (widthDst>0 && heightDst>0) + OneFrame = _SaveFrame_castToPyFrameArray(frameRGB->data, widthDst, heightDst); + else + OneFrame = _SaveFrame_castToPyFrameArray(frameRGB->data, width, height); + PyList_Append(PyFrameList, OneFrame); + //cout << "[" << width << "-" << height << ", " << width*height << ", " << video_dst_bufsize << "]" << endl; + //cout << "PTS = " << frameRGB->pts << ", coded Fnum = " << frameRGB->coded_picture_number << endl; + processed = true; + } + } + + /* If we use frame reference counting, we own the data and need + * to de-reference it when we don't use it anymore */ + + if (got_frame && refcount) + av_frame_unref(frame); + + return decoded; +} + +PyObject *cmpc::CMpegDecoder::_SaveFrame_castToPyFrameArray(uint8_t *data[], int fWidth, int fHeight) { + npy_intp dims[] = { fHeight, fWidth, 3 }; + auto newdata = new uint8_t[static_cast(fHeight) * static_cast(fWidth) * 3]; + memcpy(newdata, data[0], static_cast(fHeight) * static_cast(fWidth) * 3); + PyObject *PyFrame = PyArray_SimpleNewFromData(3, dims, NPY_UINT8, reinterpret_cast(newdata)); + PyArray_ENABLEFLAGS((PyArrayObject*)PyFrame, NPY_ARRAY_OWNDATA); + return PyFrame; +} + +attribute_deprecated +PyObject *cmpc::CMpegDecoder::_SaveFrame_castToPyFrameArrayOld(uint8_t *data[], int fWidth, int fHeight) { + npy_intp dims[] = { static_cast(fHeight) * static_cast(fWidth) * 3 }; + PyObject *PyFrame = PyArray_SimpleNew(1, dims, NPY_UINT8); + if (PyFrame == NULL) { + Py_RETURN_NONE; + } + auto out_iter = NpyIter_New((PyArrayObject *)PyFrame, NPY_ITER_READWRITE, + NPY_CORDER, NPY_NO_CASTING, NULL); + if (out_iter == NULL) { + Py_DECREF(PyFrame); + Py_RETURN_NONE; + } + /* + * The iternext function gets stored in a local variable + * so it can be called repeatedly in an efficient manner. + */ + auto iternext = NpyIter_GetIterNext(out_iter, NULL); + if (iternext == NULL) { + NpyIter_Deallocate(out_iter); + Py_DECREF(PyFrame); + Py_RETURN_NONE; + } + /* The location of the data pointer which the iterator may update */ + auto dataptr = NpyIter_GetDataPtrArray(out_iter); + //auto out_iter = (PyArrayIterObject *)PyArray_IterNew(PyFrame); + uint8_t * pdata = data[0]; + for (auto i = 0; i < fHeight; i++) { + for (auto j = 0; j < fWidth; j++) { + for (auto k = 0; k < 3; k++, pdata++ ) { + uint8_t * out_dataptr = (uint8_t *) (*dataptr); + *out_dataptr = *pdata; + iternext(out_iter); + } + } + } + PyObject *pyshape = Py_BuildValue("[iii]", fHeight, fWidth, 3); + PyFrame = PyArray_Reshape((PyArrayObject*)PyFrame, pyshape); + Py_DECREF(pyshape); + NpyIter_Deallocate(out_iter); + PyGC_Collect(); + //Py_INCREF(PyFrame); + return PyFrame; +} + +int64_t cmpc::CMpegDecoder::__FrameToPts(int64_t seekFrame) const{ + auto time_base = PVideoStream->time_base; + auto frame_base = PVideoStream->avg_frame_rate; + //cout << "Frame_Base: den=" << frame_base.den << ", num=" << frame_base.num << endl; + auto seekTimeStamp = PVideoStream->start_time + av_rescale( av_rescale(seekFrame, time_base.den, time_base.num), frame_base.den, frame_base.num ); + return seekTimeStamp; +} + +int64_t cmpc::CMpegDecoder::__TimeToPts(double seekTime) const { + auto time_base = PVideoStream->time_base; + auto seekTimeStamp = PVideoStream->start_time + av_rescale(static_cast(seekTime*1000), time_base.den, time_base.num)/1000; + return seekTimeStamp; +} + +bool cmpc::CMpegDecoder::ExtractGOP(PyObject* PyFrameList) { + int ret; + bool got_frame; + + if (EndofGOP) + return false; + + AVFrame *frame = av_frame_alloc(); + auto pkt = av_packet_alloc(); + if (!frame) { + cerr << "Could not allocate frame" << endl; + ret = AVERROR(ENOMEM); + return false; + } + AVFrame *frameRGB = av_frame_alloc(); + if (!frameRGB) { + cerr << "Could not allocate frameRGB" << endl; + return false; + } + /* initialize packet, set data to NULL, let the demuxer fill it */ + if (PVideoStream && (__dumpControl > 0)) { + std::ostringstream str_data; + str_data << "Demuxing video from file '" << videoPath << "' into Python-List" << endl; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_INFO, "%s", str_data_s.c_str()); + } + + /* Reset the contex to remove the flushed state. */ + avcodec_flush_buffers(PCodecCtx); + + /* read frames from the file */ + bool frameProcessed = false; + PVideoFrameCount = 0; + + //cout << framePos_TimeBase << endl; + if (av_seek_frame(PFormatCtx, PVideoStreamIDX, currentGOPTSM, AVSEEK_FLAG_BACKWARD) < 0) { + cerr << "AV seek frame fail!" << endl; + av_seek_frame(PFormatCtx, -1, 0, AVSEEK_FLAG_BACKWARD); + } + + // Assign appropriate parts of buffer to image planes in pFrameRGB Note that pFrameRGB is an AVFrame, but AVFrame is a superset of AVPicture + if (widthDst > 0 && heightDst > 0) { + av_image_fill_arrays(frameRGB->data, frameRGB->linesize, RGBbuffer, AVPixelFormat::AV_PIX_FMT_RGB24, widthDst, heightDst, 1); + } + else { + av_image_fill_arrays(frameRGB->data, frameRGB->linesize, RGBbuffer, AVPixelFormat::AV_PIX_FMT_RGB24, width, height, 1); + } + + int GOPstate = 0; // 0: Have not meed key frame; 1: During GOP; 2: End of GOP + int count = 0; + + auto temp_pkt = av_packet_alloc(); + while (av_read_frame(PFormatCtx, pkt) >= 0) { + //cout << "[Test - " << pkt.size << " ]" << endl; + av_packet_ref(temp_pkt, pkt); + frameProcessed = false; + do { + ret = _SaveFrameForGOP(PyFrameList, frame, frameRGB, temp_pkt, got_frame, GOPstate, frameProcessed, 0); + if (ret < 0) + break; + temp_pkt->data += ret; + temp_pkt->size -= ret; + } while (temp_pkt->size > 0); + /* flush cached frames */ + av_packet_unref(temp_pkt); + av_packet_unref(pkt); + if (frameProcessed) + count++; + if (GOPstate == 2) + break; + } + av_packet_free(&temp_pkt); + + if (GOPstate == 1) { //If the end of reading is not raised by I frame, it indicates that the video reaches the end. + EndofGOP = true; + } + + do { + _SaveFrameForGOP(PyFrameList, frame, frameRGB, pkt, got_frame, GOPstate, frameProcessed, 1); + } while (got_frame); + + //cout << "Demuxing succeeded." << endl; + + if (PVideoStream && (__dumpControl > 0)) { + std::ostringstream str_data; + str_data << "Succeed in convert GOP into Python_List, got " << count << " frames." << endl; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_INFO, "%s", str_data_s.c_str()); + } + + //av_free(RGBbuffer); + //RGBbuffer = nullptr; + //cout << "Free Buffer" << endl; + //sws_freeContext(PswsCtx); + //cout << "Free ctx" << endl; + //PswsCtx = nullptr; + if (frameRGB) { + av_frame_free(&frameRGB); + } + if (frame) { + av_frame_free(&frame); + } + if (pkt) { + av_packet_free(&pkt); + } + + //cout << "End Process" << endl; + + return true; +} + +bool cmpc::CMpegDecoder::ExtractFrame(PyObject* PyFrameList, int64_t framePos, int64_t frameNum, double timePos, int mode) { + int ret; + bool got_frame; + auto frame = av_frame_alloc(); + if (!frame) { + cerr << "Could not allocate frame" << endl; + ret = AVERROR(ENOMEM); + return false; + } + auto pkt = av_packet_alloc(); + if (!pkt) { + cerr << "Could not allocate packet" << endl; + ret = AVERROR(ENOMEM); + return false; + } + auto frameRGB = av_frame_alloc(); + if (!frameRGB) { + cerr << "Could not allocate frameRGB" << endl; + return false; + } + /* initialize packet, set data to NULL, let the demuxer fill it */ + if (PVideoStream && (__dumpControl > 0)) { + std::ostringstream str_data; + str_data << "Demuxing video from file '" << videoPath << "' into Python-List" << endl; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_INFO, "%s", str_data_s.c_str()); + } + + /* Reset the contex to remove the flushed state. */ + avcodec_flush_buffers(PCodecCtx); + + /* read frames from the file */ + int64_t count = 0; + bool frameProcessed = false; + PVideoFrameCount = 0; + + int64_t framePos_TimeBase; + if (mode && 0x1) { + framePos_TimeBase = __TimeToPts(timePos); + } + else { + framePos_TimeBase = __FrameToPts(framePos); + } + if (av_seek_frame(PFormatCtx, PVideoStreamIDX, framePos_TimeBase, AVSEEK_FLAG_BACKWARD) < 0) { + cerr << "AV seek frame fail!" << endl; + av_seek_frame(PFormatCtx, -1, 0, AVSEEK_FLAG_BACKWARD); + } + + // Assign appropriate parts of buffer to image planes in pFrameRGB Note that pFrameRGB is an AVFrame, but AVFrame is a superset of AVPicture + if (widthDst > 0 && heightDst > 0) { + av_image_fill_arrays(frameRGB->data, frameRGB->linesize, RGBbuffer, AVPixelFormat::AV_PIX_FMT_RGB24, widthDst, heightDst, 1); + } + else { + av_image_fill_arrays(frameRGB->data, frameRGB->linesize, RGBbuffer, AVPixelFormat::AV_PIX_FMT_RGB24, width, height, 1); + } + + auto temp_pkt = av_packet_alloc(); + while (av_read_frame(PFormatCtx, pkt) >= 0) { + av_packet_ref(temp_pkt, pkt); + frameProcessed = false; + do { + ret = _SaveFrame(PyFrameList, frame, frameRGB, temp_pkt, got_frame, framePos_TimeBase, frameProcessed, 0); + if (ret < 0) + break; + temp_pkt->data += ret; + temp_pkt->size -= ret; + } while (temp_pkt->size > 0); + /* flush cached frames */ + av_packet_unref(temp_pkt); + av_packet_unref(pkt); + if (frameProcessed) + count++; + if (count >= frameNum) + break; + } + av_packet_free(&temp_pkt); + + do { + _SaveFrame(PyFrameList, frame, frameRGB, pkt, got_frame, framePos_TimeBase, frameProcessed, 1); + } while (got_frame); + + if (PVideoStream && count > 0 && (__dumpControl > 0)) { + std::ostringstream str_data; + str_data << "Succeed in convert frames into Python_List" << endl; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_INFO, "%s", str_data_s.c_str()); + } + + if (frameRGB) { + av_frame_free(&frameRGB); + } + if (frame) { + av_frame_free(&frame); + } + if (pkt) { + av_packet_free(&pkt); + } + + return true; +} + +ostream & cmpc::operator<<(ostream & out, cmpc::CMpegDecoder & self_class) { + out << std::setw(1) << "/"; + out << std::setfill('*') << std::setw(44) << "" << std::setfill(' ') << endl; + out << std::setw(1) << " * Packed FFmpeg Decoder - Y. Jin V" << MPEGCODER_CURRENT_VERSION << endl; + out << " " << std::setfill('*') << std::setw(44) << "" << std::setfill(' ') << endl; + out << std::setiosflags(std::ios::left) << std::setw(25) << " * VideoPath: " \ + << self_class.videoPath << endl; + out << std::setiosflags(std::ios::left) << std::setw(25) << " * (Width, Height): " \ + << self_class.width << ", " << self_class.height << endl; + if (self_class.widthDst > 0 && self_class.heightDst > 0) { + out << std::setiosflags(std::ios::left) << std::setw(25) << " * (WidthDst, HeightDst): " \ + << self_class.widthDst << ", " << self_class.heightDst << endl; + } + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Deccoder: " \ + << self_class._str_codec << endl; + if (self_class.PCodecCtx) { + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Thread number: " \ + << self_class.PCodecCtx->thread_count << endl; + } + else { + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Thread number (P): " \ + << self_class.nthread << endl; + } + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Duration: " \ + << self_class._duration << " [s]" << endl; + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Predicted FrameNum: " \ + << self_class._predictFrameNum << endl; + out << std::setw(1) << " */"; + return out; +} + + +/** + * Related with the encoder. + */ + + // Constructors following 3-5 law. +cmpc::CMpegEncoder::CMpegEncoder(void): + bitRate(1024), width(100), height(100), timeBase(_setAVRational(1, 25)), frameRate(_setAVRational(25, 1)),\ + GOPSize(10), MaxBFrame(1), PStreamContex({ 0 }), PFormatCtx(nullptr), PswsCtx(nullptr), RGBbuffer(nullptr), \ + Ppacket(nullptr), __have_video(false), __enable_header(false), widthSrc(0), heightSrc(0), __frameRGB(nullptr), \ + nthread(0) { + videoPath.clear(); + codecName.clear(); +} + +void cmpc::CMpegEncoder::clear(void) { + FFmpegClose(); + videoPath.clear(); + codecName.clear(); + bitRate = 1024; + width = 100; + height = 100; + heightSrc = 0; + widthSrc = 0; + timeBase = _setAVRational(1, 25); + frameRate = _setAVRational(25, 1); + GOPSize = 10; + MaxBFrame = 1; + nthread = 0; + PStreamContex = { 0 }; + __have_video = false; + __enable_header = false; +} + +void cmpc::CMpegEncoder::__copyMetaData(const CMpegEncoder &ref) { + videoPath.assign(ref.videoPath); + codecName.assign(ref.codecName); + bitRate = ref.bitRate; + width = ref.width; + height = ref.height; + widthSrc = ref.widthSrc; + heightSrc = ref.heightSrc; + timeBase = ref.timeBase; + frameRate = ref.frameRate; + GOPSize = ref.GOPSize; + MaxBFrame = ref.MaxBFrame; + nthread = ref.nthread; +} + +cmpc::CMpegEncoder::~CMpegEncoder(void) { + clear(); +} + + +cmpc::CMpegEncoder::CMpegEncoder(const CMpegEncoder &ref) { + __copyMetaData(ref); + if (!FFmpegSetup()) { + clear(); + } +} + +cmpc::CMpegEncoder& cmpc::CMpegEncoder::operator=(const CMpegEncoder &ref) { + __copyMetaData(ref); + if (!FFmpegSetup()) { + clear(); + } + return *this; +} + +cmpc::CMpegEncoder::CMpegEncoder(CMpegEncoder &&ref) noexcept: + bitRate(ref.bitRate), width(ref.width), height(ref.height), timeBase(ref.timeBase), frameRate(ref.frameRate), \ + GOPSize(ref.GOPSize), MaxBFrame(ref.MaxBFrame), PStreamContex(ref.PStreamContex), PswsCtx(ref.PswsCtx), \ + RGBbuffer(ref.RGBbuffer), Ppacket(ref.Ppacket), PFormatCtx(ref.PFormatCtx), __have_video(ref.__have_video), \ + __enable_header(ref.__enable_header), widthSrc(ref.widthSrc), heightSrc(ref.heightSrc), __frameRGB(ref.__frameRGB), \ + nthread(ref.nthread) { + videoPath.assign(ref.videoPath); + codecName.assign(ref.codecName); +} + +cmpc::CMpegEncoder& cmpc::CMpegEncoder::operator=(CMpegEncoder &&ref) noexcept { + videoPath.assign(ref.videoPath); + codecName.assign(ref.codecName); + bitRate = ref.bitRate; + width = ref.width; + height = ref.height; + widthSrc = ref.widthSrc; + heightSrc = ref.heightSrc; + timeBase = ref.timeBase; + frameRate = ref.frameRate; + GOPSize = ref.GOPSize; + nthread = ref.nthread; + MaxBFrame = ref.MaxBFrame; + PFormatCtx = ref.PFormatCtx; + PStreamContex = ref.PStreamContex; + PswsCtx = ref.PswsCtx; + RGBbuffer = ref.RGBbuffer; + Ppacket = ref.Ppacket; + __frameRGB = ref.__frameRGB; + __have_video = ref.__have_video; + __enable_header = ref.__enable_header; + ref.PFormatCtx = nullptr; + ref.PStreamContex = { 0 }; + ref.PswsCtx = nullptr; + ref.RGBbuffer = nullptr; + ref.Ppacket = nullptr; + ref.__frameRGB = nullptr; + return *this; +} + +void cmpc::CMpegEncoder::resetPath(string inVideoPath) { + videoPath.assign(inVideoPath); +} + +bool cmpc::CMpegEncoder::FFmpegSetup(string inVideoPath) { + resetPath(inVideoPath); + return FFmpegSetup(); +} + +cmpc::AVRational cmpc::CMpegEncoder::_setAVRational(int num, int den) { + AVRational res; + res.num = num; res.den = den; + return res; +} + +int64_t cmpc::CMpegEncoder::__FrameToPts(int64_t seekFrame) const { + return av_rescale(av_rescale(seekFrame, timeBase.den, timeBase.num), frameRate.den, frameRate.num); +} + +int64_t cmpc::CMpegEncoder::__TimeToPts(double seekTime) const { + return av_rescale(static_cast(seekTime * 1000), timeBase.den, timeBase.num) / 1000; +} + +void cmpc::CMpegEncoder::__log_packet(){ + AVRational *time_base = &PFormatCtx->streams[Ppacket->stream_index]->time_base; + std::ostringstream str_data; + str_data << "pts:" << av_ts2str(Ppacket->pts) << " pts_time:" << av_ts2timestr(Ppacket->pts, time_base) + << " dts:" << av_ts2str(Ppacket->dts) << " dts_time:" << av_ts2timestr(Ppacket->dts, time_base) << endl; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_INFO, "%s", str_data_s.c_str()); +} + +int cmpc::CMpegEncoder::__write_frame(){ + /* rescale output packet timestamp values from codec to stream timebase */ + av_packet_rescale_ts(Ppacket, PStreamContex.enc->time_base, PStreamContex.st->time_base); + Ppacket->stream_index = PStreamContex.st->index; + + /* Write the compressed frame to the media file. */ + if (__dumpControl > 0) + __log_packet(); + return av_interleaved_write_frame(PFormatCtx, Ppacket); +} + +/* Add an output stream. */ +bool cmpc::CMpegEncoder::__add_stream(AVCodec **codec){ + /* find the encoder */ + AVCodecID codec_id; + auto srcwidth = widthSrc > 0 ? widthSrc : width; + auto srcheight = heightSrc > 0 ? heightSrc : height; + *codec = avcodec_find_encoder_by_name(codecName.c_str()); + if (!(*codec)) { + codec_id = PFormatCtx->oformat->video_codec; + cerr << "Could not find encoder "<< codecName <<", use " << avcodec_get_name(codec_id) << " as an alternative." << endl; + *codec = avcodec_find_encoder(codec_id); + } + else { + codec_id = (*codec)->id; + PFormatCtx->oformat->video_codec = codec_id; + } + if (!(*codec)) { + cerr << "Could not find encoder for '" << avcodec_get_name(codec_id) << "'" << endl; + return false; + } + + PStreamContex.st = avformat_new_stream(PFormatCtx, nullptr); + if (!PStreamContex.st) { + cerr << "Could not allocate stream" << endl; + return false; + } + PStreamContex.st->id = PFormatCtx->nb_streams - 1; + auto c = avcodec_alloc_context3(*codec); + if (!c) { + cerr << "Could not alloc an encoding context" << endl; + return false; + } + if (nthread > 0) { + c->thread_count = nthread; + } + PStreamContex.enc = c; + + switch ((*codec)->type) { + case AVMediaType::AVMEDIA_TYPE_VIDEO: + c->codec_id = codec_id; + + c->bit_rate = bitRate; + /* Resolution must be a multiple of two. */ + c->width = width; + c->height = height; + /* timebase: This is the fundamental unit of time (in seconds) in terms + * of which frame timestamps are represented. For fixed-fps content, + * timebase should be 1/framerate and timestamp increments should be + * identical to 1. */ + PStreamContex.st->time_base.den = 0; + PStreamContex.st->time_base.num = 0; + //av_stream_set_r_frame_rate(PStreamContex.st, frameRate); + //cout << "(" << frameRate.num << ", " << frameRate.den << ")" << endl; + //PStreamContex.st->r_frame_rate + c->time_base = timeBase; + + //PStreamContex.st->frame + c->framerate = frameRate; + + c->gop_size = GOPSize; /* emit one intra frame every twelve frames at most */ + c->max_b_frames = MaxBFrame; + c->pix_fmt = STREAM_PIX_FMT; + if (c->codec_id == AVCodecID::AV_CODEC_ID_FLV1) { + /* just for testing, we also add B-frames */ + c->max_b_frames = 0; + } + if (c->codec_id == AVCodecID::AV_CODEC_ID_MPEG2VIDEO) { + /* just for testing, we also add B-frames */ + c->max_b_frames = 2; + } + if (c->codec_id == AVCodecID::AV_CODEC_ID_MPEG1VIDEO) { + /* Needed to avoid using macroblocks in which some coeffs overflow. + * This does not happen with normal video, it just happens here as + * the motion of the chroma plane does not match the luma plane. */ + c->mb_decision = 2; + } + if (c->pix_fmt != STREAM_PIX_FMT) { + /* as we only generate a YUV420P picture, we must convert it + * to the codec pixel format if needed */ + if (!PStreamContex.sws_ctx) { + PStreamContex.sws_ctx = sws_getContext(c->width, c->height, + STREAM_PIX_FMT, + c->width, c->height, + c->pix_fmt, + SCALE_FLAGS, nullptr, nullptr, nullptr); + if (!PStreamContex.sws_ctx) { + cerr << "Could not initialize the conversion context" << endl; + return false; + } + } + } + if (!PswsCtx) { + PswsCtx = sws_getContext(srcwidth, srcheight, + AVPixelFormat::AV_PIX_FMT_RGB24, + c->width, c->height, + c->pix_fmt, + SCALE_FLAGS, nullptr, nullptr, nullptr); + if (!PswsCtx) { + cerr << "Could not initialize the conversion context" << endl; + return false; + } + } + if (!RGBbuffer) { + auto numBytes = av_image_get_buffer_size(AVPixelFormat::AV_PIX_FMT_RGB24, srcwidth, srcheight, 1); + RGBbuffer = (uint8_t *)av_malloc(numBytes * sizeof(uint8_t)); + } + break; + + default: + break; + } + + /* Some formats want stream headers to be separate. */ + if (PFormatCtx->oformat->flags & AVFMT_GLOBALHEADER) + c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; + return true; +} + +/* video output */ +cmpc::AVFrame* cmpc::CMpegEncoder::__alloc_picture(enum AVPixelFormat pix_fmt, int width, int height) { + auto picture = av_frame_alloc(); + if (!picture) + return nullptr; + picture->format = pix_fmt; + picture->width = width; + picture->height = height; + /* allocate the buffers for the frame data */ + auto ret = av_frame_get_buffer(picture, 32); + if (ret < 0) { + cerr << "Could not allocate frame data." << endl; + return nullptr; + } + return picture; +} + +bool cmpc::CMpegEncoder::__open_video(AVCodec *codec, AVDictionary *opt_arg){ + int ret; + auto c = PStreamContex.enc; + AVDictionary *opt = nullptr; + + av_dict_copy(&opt, opt_arg, 0); + /* open the codec */ + ret = avcodec_open2(c, codec, &opt); + av_dict_free(&opt); + if (ret < 0) { + cerr << "Could not open video codec: " << av_err2str(ret) << endl; + return false; + } + /* allocate and init a re-usable frame */ + PStreamContex.frame = __alloc_picture(c->pix_fmt, c->width, c->height); + if (!PStreamContex.frame) { + cerr << "Could not allocate video frame" << endl; + return false; + } + /* If the output format is not YUV420P, then a temporary YUV420P + * picture is needed too. It is then converted to the required + * output format. */ + PStreamContex.tmp_frame = nullptr; + if (c->pix_fmt != STREAM_PIX_FMT) { + PStreamContex.tmp_frame = __alloc_picture(STREAM_PIX_FMT, c->width, c->height); + if (!PStreamContex.tmp_frame) { + cerr << "Could not allocate temporary picture" << endl; + return false; + } + } + /* copy the stream parameters to the muxer */ + ret = avcodec_parameters_from_context(PStreamContex.st->codecpar, c); + if (ret < 0) { + cerr << "Could not copy the stream parameters" << endl; + return false; + } + return true; +} + +cmpc::AVFrame *cmpc::CMpegEncoder::__get_video_frame(PyArrayObject* PyFrame) { + auto c = PStreamContex.enc; + + /* check if we want to generate more frames */ + //if (av_compare_ts(PStreamContex.next_pts, c->time_base, STREAM_DURATION, { 1, 1 }) >= 0) + // return nullptr; + /* when we pass a frame to the encoder, it may keep a reference to it + * internally; make sure we do not overwrite it here */ + if (av_frame_make_writable(PStreamContex.frame) < 0) + return nullptr; + if (c->pix_fmt != STREAM_PIX_FMT) { + /* as we only generate a YUV420P picture, we must convert it + * to the codec pixel format if needed */ + if (!PStreamContex.sws_ctx) { + PStreamContex.sws_ctx = sws_getContext(c->width, c->height, + STREAM_PIX_FMT, + c->width, c->height, + c->pix_fmt, + SCALE_FLAGS, nullptr, nullptr, nullptr); + if (!PStreamContex.sws_ctx) { + cerr << "Could not initialize the conversion context" << endl; + return nullptr; + } + } + if (!_LoadFrame_castFromPyFrameArray(PStreamContex.tmp_frame, PyFrame)) { + return nullptr; + } + sws_scale(PStreamContex.sws_ctx, + (const uint8_t * const *)PStreamContex.tmp_frame->data, PStreamContex.tmp_frame->linesize, + 0, c->height, PStreamContex.frame->data, PStreamContex.frame->linesize); + } + else { + if (!_LoadFrame_castFromPyFrameArray(PStreamContex.frame, PyFrame)) { + return nullptr; + } + } + + PStreamContex.frame->pts = PStreamContex.next_frame; + PStreamContex.next_frame++; + return PStreamContex.frame; +} + +bool cmpc::CMpegEncoder::_LoadFrame_castFromPyFrameArray(AVFrame *frame, PyArrayObject* PyFrame) { + /* make sure the frame data is writable */ + if (!__frameRGB) { + cerr << "Could not allocate frameRGB" << endl; + return false; + } + auto out_dataptr = reinterpret_cast(PyArray_DATA(PyFrame)); + auto srcwidth = widthSrc > 0 ? widthSrc : width; + auto srcheight = heightSrc > 0 ? heightSrc : height; + memcpy(RGBbuffer, out_dataptr, static_cast(srcwidth) * static_cast(srcheight) * 3 * sizeof(uint8_t)); + // Assign appropriate parts of buffer to image planes in pFrameRGB Note that pFrameRGB is an AVFrame, but AVFrame is a superset of AVPicture + av_image_fill_arrays(__frameRGB->data, __frameRGB->linesize, RGBbuffer, AVPixelFormat::AV_PIX_FMT_RGB24, srcwidth, srcheight, 1); + sws_scale(PswsCtx, __frameRGB->data, __frameRGB->linesize, 0, srcheight, frame->data, frame->linesize); + //cout << "Free 1" << endl; + //delete frameRGB; + //cout << "Free 2" << endl; + return true; +} + +/* +* encode one video frame and send it to the muxer +* return 1 when encoding is finished, 0 otherwise +*/ +int cmpc::CMpegEncoder::__avcodec_encode_video2(AVCodecContext *enc_ctx, AVPacket *pkt, AVFrame *frame){ + int ret; + int wfret = 0; + + if (frame) { + if (__dumpControl > 1) { + std::ostringstream str_data; + str_data << "Send frame " << frame->pts << endl; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_DEBUG, "%s", str_data_s.c_str()); + } + } + else{ + return AVERROR(EAGAIN); + } + + ret = avcodec_send_frame(enc_ctx, frame); + // In particular, we don't expect AVERROR(EAGAIN), because we read all + // decoded frames with avcodec_receive_frame() until done. + if (ret < 0) { + return ret == AVERROR_EOF ? 0 : ret; + } + + ret = avcodec_receive_packet(enc_ctx, pkt); + if (ret == AVERROR(EAGAIN)) + return 0; + + if (__dumpControl > 0) { + std::ostringstream str_data; + str_data << "Write packet " << pkt->pts << " (size=" << pkt->size << "), "; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_INFO, "%s", str_data_s.c_str()); + } + + if (!ret) { + wfret = __write_frame(); + av_packet_unref(Ppacket); + if (wfret < 0) { + cerr << "Error while writing video frame: " << av_err2str(ret) << endl; + return wfret; + } + } + return ret; +} + +int cmpc::CMpegEncoder::__avcodec_encode_video2_flush(AVCodecContext *enc_ctx, AVPacket *pkt) { + int ret; + int wfret = 0; + if (__dumpControl > 1) { + std::ostringstream str_data; + str_data << "Flush all packets" << endl; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_DEBUG, "%s", str_data_s.c_str()); + } + + ret = avcodec_send_frame(enc_ctx, nullptr); + // In particular, we don't expect AVERROR(EAGAIN), because we read all + // decoded frames with avcodec_receive_frame() until done. + if (ret < 0) { + return ret == AVERROR_EOF ? 0 : ret; + } + + while (ret >= 0) { + ret = avcodec_receive_packet(enc_ctx, pkt); + if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) { + return 0; + } + if (__dumpControl > 0) { + std::ostringstream str_data; + str_data << "Write packet " << pkt->pts << " (size=" << pkt->size << "), "; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_INFO, "%s", str_data_s.c_str()); + } + if (!ret) { + wfret = __write_frame(); + av_packet_unref(pkt); + } + else { + wfret = 0; + } + if (wfret < 0) { + cerr << "Error while writing video frame: " << av_err2str(ret) << endl; + return wfret; + } + } + return ret; +} + +int cmpc::CMpegEncoder::EncodeFrame(PyArrayObject* PyFrame) { + int ret; + auto c = PStreamContex.enc; + AVFrame *frame = nullptr; + if ((!__have_video) || (!__enable_header)) + cerr << "Not allowed to use this method before FFmpegSetup()" << endl; + if (PyFrame) { + frame = __get_video_frame(PyFrame); + ret = __avcodec_encode_video2(c, Ppacket, frame); + } + else { + frame = nullptr; + ret = __avcodec_encode_video2_flush(c, Ppacket); + } + + if (ret < 0) { + cerr << "Error encoding video frame: " << av_err2str(ret) << endl; + return ret; + } + return frame ? 0 : 1; +} + +void cmpc::CMpegEncoder::setParameter(string keyword, void *ptr) { + if (keyword.compare("decoder") == 0) { + CMpegDecoder *ref = reinterpret_cast(ptr); + resetPath(ref->videoPath); + codecName.assign(ref->_str_codec); + if (ref->PCodecCtx) { + bitRate = ref->PCodecCtx->bit_rate; + GOPSize = ref->PCodecCtx->gop_size; + MaxBFrame = ref->PCodecCtx->max_b_frames; + if (PStreamContex.enc) { + PStreamContex.enc->thread_count = ref->PCodecCtx->thread_count; + } + nthread = ref->PCodecCtx->thread_count; + } + else { + if (PStreamContex.enc) { + PStreamContex.enc->thread_count = ref->nthread; + } + nthread = ref->nthread; + } + if (ref->widthDst > 0 && ref->heightDst > 0) { + width = ref->widthDst; + height = ref->heightDst; + } + else { + width = ref->width; + height = ref->height; + } + widthSrc = width; + heightSrc = height; + if (ref->PVideoStream) { + //timeBase = ref->PVideoStream->time_base; + frameRate = ref->PVideoStream->avg_frame_rate; + timeBase = _setAVRational(frameRate.den, frameRate.num); + } + } + else if (keyword.compare("client") == 0) { + CMpegClient* ref = reinterpret_cast(ptr); + resetPath(ref->videoPath); + codecName.assign(ref->_str_codec); + if (ref->PCodecCtx) { + bitRate = ref->PCodecCtx->bit_rate; + GOPSize = ref->PCodecCtx->gop_size; + MaxBFrame = ref->PCodecCtx->max_b_frames; + if (PStreamContex.enc) { + PStreamContex.enc->thread_count = ref->PCodecCtx->thread_count; + } + nthread = ref->PCodecCtx->thread_count; + } + else { + if (PStreamContex.enc) { + PStreamContex.enc->thread_count = ref->nthread; + } + nthread = ref->nthread; + } + if (ref->widthDst > 0 && ref->heightDst > 0) { + width = ref->widthDst; + height = ref->heightDst; + } + else { + width = ref->width; + height = ref->height; + } + widthSrc = width; + heightSrc = height; + if (ref->PVideoStream) { + //timeBase = ref->PVideoStream->time_base; + frameRate = ref->PVideoStream->avg_frame_rate; + timeBase = _setAVRational(frameRate.den, frameRate.num); + } + } + else if (keyword.compare("configDict") == 0) { + PyObject* ref = reinterpret_cast(ptr); + if (PyDict_Check(ref)) { + string key; + PyObject* val; + // Set parameters. + key.assign("videoPath"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyBytes_Check(val)) { + auto val_str = string(PyBytes_AsString(val)); + resetPath(val_str); + } + } + else { + key.assign("videoAddress"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyBytes_Check(val)) { + auto val_str = string(PyBytes_AsString(val)); + resetPath(val_str); + } + } + } + key.assign("codecName"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyBytes_Check(val)) { + auto val_str = string(PyBytes_AsString(val)); + codecName.assign(val_str); + } + } + key.assign("bitRate"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyLong_Check(val)) { + auto val_num = static_cast(PyLong_AsLongLong(val)); + bitRate = val_num; + } + } + key.assign("GOPSize"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyLong_Check(val)) { + auto val_num = static_cast(PyLong_AsLong(val)); + GOPSize = val_num; + } + } + key.assign("maxBframe"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyLong_Check(val)) { + auto val_num = static_cast(PyLong_AsLong(val)); + MaxBFrame = val_num; + } + } + key.assign("width"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyLong_Check(val)) { + auto val_num = static_cast(PyLong_AsLong(val)); + width = val_num; + widthSrc = val_num; + } + } + key.assign("height"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyLong_Check(val)) { + auto val_num = static_cast(PyLong_AsLong(val)); + height = val_num; + heightSrc = val_num; + } + } + key.assign("widthSrc"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyLong_Check(val)) { + auto val_num_1 = static_cast(PyLong_AsLong(val)); + key.assign("heightSrc"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyLong_Check(val)) { + auto val_num_2 = static_cast(PyLong_AsLong(val)); + widthSrc = val_num_1; + heightSrc = val_num_2; + } + } + } + } + key.assign("widthDst"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyLong_Check(val)) { + auto val_num_1 = static_cast(PyLong_AsLong(val)); + key.assign("heightDst"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyLong_Check(val)) { + auto val_num_2 = static_cast(PyLong_AsLong(val)); + width = val_num_1; + height = val_num_2; + } + } + } + } + key.assign("frameRate"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyTuple_Check(val)) { + auto valObj = PyTuple_GetItem(val, 0); + int num = static_cast(PyLong_AsLong(valObj)); + valObj = PyTuple_GetItem(val, 1); + int den = static_cast(PyLong_AsLong(valObj)); + frameRate = _setAVRational(num, den); + timeBase = _setAVRational(den, num); + } + } + key.assign("nthread"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyLong_Check(val)) { + auto val_num = static_cast(PyLong_AsLong(val)); + if (PStreamContex.enc) { + PStreamContex.enc->thread_count = val_num; + } + nthread = val_num; + } + } + } + } + else if (keyword.compare("videoPath") == 0) { + string *ref = reinterpret_cast(ptr); + resetPath(*ref); + } + else if (keyword.compare("codecName") == 0) { + string *ref = reinterpret_cast(ptr); + codecName.assign(*ref); + } + else if (keyword.compare("bitRate") == 0) { + double *ref = reinterpret_cast(ptr); + auto bit_rate = static_cast((*ref) * 1024); + bitRate = bit_rate; + } + else if (keyword.compare("width") == 0) { + int *ref = reinterpret_cast(ptr); + width = *ref; + } + else if (keyword.compare("height") == 0) { + int *ref = reinterpret_cast(ptr); + height = *ref; + } + else if (keyword.compare("widthSrc") == 0) { + int *ref = reinterpret_cast(ptr); + widthSrc = *ref; + } + else if (keyword.compare("heightSrc") == 0) { + int *ref = reinterpret_cast(ptr); + heightSrc = *ref; + } + else if (keyword.compare("GOPSize") == 0) { + int *ref = reinterpret_cast(ptr); + GOPSize = *ref; + } + else if (keyword.compare("maxBframe") == 0) { + int *ref = reinterpret_cast(ptr); + MaxBFrame = *ref; + } + else if (keyword.compare("frameRate") == 0) { + PyObject *ref = reinterpret_cast(ptr); + auto refObj = PyTuple_GetItem(ref, 0); + int num = static_cast(PyLong_AsLong(refObj)); + refObj = PyTuple_GetItem(ref, 1); + int den = static_cast(PyLong_AsLong(refObj)); + frameRate = _setAVRational(num, den); + timeBase = _setAVRational(den, num); + } + else if (keyword.compare("nthread") == 0) { + auto ref = reinterpret_cast(ptr); + if (PStreamContex.enc) { + PStreamContex.enc->thread_count = *ref; + } + nthread = *ref; + } +} + +PyObject* cmpc::CMpegEncoder::getParameter(string keyword) { + if (keyword.compare("videoPath") == 0) { + return PyUnicode_DecodeFSDefaultAndSize(videoPath.c_str(), static_cast(videoPath.size())); + } + else if (keyword.compare("codecName") == 0) { + return PyUnicode_DecodeFSDefaultAndSize(codecName.c_str(), static_cast(codecName.size())); + } + else if (keyword.compare("bitRate") == 0) { + auto bit_rate = static_cast(bitRate) / 1024; + return Py_BuildValue("d", bit_rate); + } + else if (keyword.compare("width") == 0) { + return Py_BuildValue("i", width); + } + else if (keyword.compare("height") == 0) { + return Py_BuildValue("i", height); + } + else if (keyword.compare("widthSrc") == 0) { + return Py_BuildValue("i", widthSrc); + } + else if (keyword.compare("heightSrc") == 0) { + return Py_BuildValue("i", heightSrc); + } + else if (keyword.compare("GOPSize") == 0) { + return Py_BuildValue("i", GOPSize); + } + else if (keyword.compare("maxBframe") == 0) { + return Py_BuildValue("i", MaxBFrame); + } + else if (keyword.compare("frameRate") == 0) { + auto frame_base = frameRate; + double frameRate = static_cast(frame_base.num) / static_cast(frame_base.den); + return Py_BuildValue("d", frameRate); + } + else if (keyword.compare("nthread") == 0) { + if (PStreamContex.enc) { + return Py_BuildValue("i", PStreamContex.enc->thread_count); + } + else { + return Py_BuildValue("i", nthread); + } + } + else { + Py_RETURN_NONE; + } +} + +PyObject* cmpc::CMpegEncoder::getParameter() { + auto res = PyDict_New(); + string key; + PyObject* val = nullptr; + // Fill the values. + key.assign("videoPath"); + val = Py_BuildValue("y", videoPath.c_str()); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("codecName"); + val = Py_BuildValue("y", codecName.c_str()); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("bitRate"); + val = Py_BuildValue("L", bitRate); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("GOPSize"); + val = Py_BuildValue("i", GOPSize); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("maxBframe"); + val = Py_BuildValue("i", MaxBFrame); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + if (widthSrc > 0) { + key.assign("widthSrc"); + val = Py_BuildValue("i", widthSrc); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + } + if (heightSrc > 0) { + key.assign("heightSrc"); + val = Py_BuildValue("i", heightSrc); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + } + key.assign("width"); + val = Py_BuildValue("i", width); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("height"); + val = Py_BuildValue("i", height); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("frameRate"); + val = Py_BuildValue("(ii)", frameRate.num, frameRate.den); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + if (PStreamContex.enc) { + key.assign("nthread"); + val = Py_BuildValue("i", PStreamContex.enc->thread_count); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + } + else { + key.assign("nthread"); + val = Py_BuildValue("i", nthread); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + } + return res; +} + +bool cmpc::CMpegEncoder::FFmpegSetup() { + AVCodec *video_codec = nullptr; + int ret; + + if (Ppacket) + av_packet_free(&Ppacket); + Ppacket = av_packet_alloc(); + if (!Ppacket) + return false; + + AVDictionary *opt = nullptr; + //av_dict_set(&opt, "vcodec", codecName.c_str(), 0); + //av_dict_set(&opt, "fflags", "", 0); + + /* allocate the output media context */ + //auto getFormat = av_guess_format(codecName.c_str(), nullptr, nullptr); + avformat_alloc_output_context2(&PFormatCtx, nullptr, nullptr, videoPath.c_str()); + PFormatCtx->avoid_negative_ts = AVFMT_AVOID_NEG_TS_AUTO; + if (!PFormatCtx) { + cout << "Could not select the encoder automatically: using MPEG." << endl; + //cout << "Could not deduce output format from file extension: using MPEG." << endl; + avformat_alloc_output_context2(&PFormatCtx, nullptr, "mpeg", videoPath.c_str()); + } + if (!PFormatCtx) + return false; + + auto fmt = PFormatCtx->oformat; + + /* Add the audio and video streams using the default format codecs + * and initialize the codecs. */ + if (fmt->video_codec != AVCodecID::AV_CODEC_ID_NONE) { + if (!__add_stream(&video_codec)) { + FFmpegClose(); + return false; + } + else + __have_video = true; + } + + /* Now that all the parameters are set, we can open the audio and + * video codecs and allocate the necessary encode buffers. */ + if (__have_video) { + if (!__open_video(video_codec, opt)) { + FFmpegClose(); + return false; + } + else + __have_video = true; + } + + if (__dumpControl > 1) { + av_dump_format(PFormatCtx, 0, videoPath.c_str(), 1); + } + + /* open the output file, if needed */ + if (!(fmt->flags & AVFMT_NOFILE)) { + ret = avio_open2(&PFormatCtx->pb, videoPath.c_str(), AVIO_FLAG_WRITE, nullptr, nullptr); + if (ret < 0) { + cerr << "Could not open '" << videoPath << "': " << av_err2str(ret) << endl; + FFmpegClose(); + return false; + } + } + + if (!(__frameRGB = av_frame_alloc())) { + cerr << "Could Allocate Temp Frame" << endl; + FFmpegClose(); + return false; + } + + /* Write the stream header, if any. */ + ret = avformat_write_header(PFormatCtx, &opt); + if (ret < 0) { + cerr << "Error occurred when opening output file: " << av_err2str(ret) << endl; + FFmpegClose(); + return false; + } + else{ + __enable_header = true; + } + return true; +} + +void cmpc::CMpegEncoder::FFmpegClose(){ + if (__enable_header && __have_video) { + //cout << "Flush Video" << endl; + int x; + if ((x = EncodeFrame(nullptr)) == 0) { + // cout << "Ret: " << x << endl; + } + if (__dumpControl > 0) { + std::ostringstream str_data; + str_data << "All frames are flushed from cache, the video would be closed." << endl; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_INFO, "%s", str_data_s.c_str()); + } + } + if (PFormatCtx) { + if (__enable_header) { + av_write_trailer(PFormatCtx); + __enable_header = false; + } + /* Close each codec. */ + if (__have_video) { + /* free the stream */ + //avformat_free_context(PFormatCtx); + if (PStreamContex.enc) + avcodec_free_context(&PStreamContex.enc); + if (PStreamContex.frame) + av_frame_free(&PStreamContex.frame); + if (PStreamContex.tmp_frame) + av_frame_free(&PStreamContex.tmp_frame); + if (PStreamContex.sws_ctx) { + sws_freeContext(PStreamContex.sws_ctx); + PStreamContex.sws_ctx = nullptr; + } + if (PswsCtx) { + sws_freeContext(PswsCtx); + PswsCtx = nullptr; + } + if (RGBbuffer) { + av_free(RGBbuffer); + RGBbuffer = nullptr; + } + __have_video = false; + } + auto fmt = PFormatCtx->oformat; + if (!(fmt->flags & AVFMT_NOFILE)) + /* Close the output file. */ + avio_closep(&PFormatCtx->pb); + /* free the stream */ + avformat_free_context(PFormatCtx); + PFormatCtx = nullptr; + } + if (Ppacket) { + av_packet_free(&Ppacket); + Ppacket = nullptr; + } + if (__frameRGB) { + av_frame_free(&__frameRGB); + } +} + +void cmpc::CMpegEncoder::dumpFormat() { + if (PFormatCtx) + av_dump_format(PFormatCtx, 0, videoPath.c_str(), 1); + else + cerr << "Not loaded video format context now. dumpFormat() is not avaliable." << endl; +} + +ostream & cmpc::operator<<(ostream & out, cmpc::CMpegEncoder & self_class) { + out << std::setw(1) << "/"; + out << std::setfill('*') << std::setw(44) << "" << std::setfill(' ') << endl; + out << std::setw(1) << " * Packed FFmpeg Encoder - Y. Jin V" << MPEGCODER_CURRENT_VERSION << endl; + out << " " << std::setfill('*') << std::setw(44) << "" << std::setfill(' ') << endl; + out << std::setiosflags(std::ios::left) << std::setw(25) << " * VideoPath: " \ + << self_class.videoPath << endl; + out << std::setiosflags(std::ios::left) << std::setw(25) << " * (Width, Height): " \ + << self_class.width << ", " << self_class.height << endl; + if (self_class.widthSrc > 0 && self_class.heightSrc > 0) { + out << std::setiosflags(std::ios::left) << std::setw(25) << " * (WidthSrc, HeightSrc): " \ + << self_class.widthSrc << ", " << self_class.heightSrc << endl; + } + else if(self_class.widthSrc > 0) { + out << std::setiosflags(std::ios::left) << std::setw(25) << " * WidthSrc: " \ + << self_class.widthSrc << endl; + } + else if (self_class.heightSrc > 0) { + out << std::setiosflags(std::ios::left) << std::setw(25) << " * HeightSrc: " \ + << self_class.heightSrc << endl; + } + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Enccoder: " \ + << self_class.codecName << endl; + if (self_class.PStreamContex.enc) { + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Thread number: " \ + << self_class.PStreamContex.enc->thread_count << endl; + } + else { + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Thread number (P): " \ + << self_class.nthread << endl; + } + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Bit Rate: " \ + << (self_class.bitRate >> 10) << " [Kbit/s]" << endl; + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Frame Rate: " \ + << static_cast(self_class.frameRate.num) / static_cast(self_class.frameRate.den) << " [FPS]" << endl; + out << std::setiosflags(std::ios::left) << std::setw(25) << " * GOP Size: " \ + << self_class.GOPSize << endl; + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Maxmal Bframe Density: " \ + << self_class.MaxBFrame << " [/GOP]" << endl; + out << std::setw(1) << " */"; + return out; +} diff --git a/MpegCoder/MpegCoder.h b/MpegCoder/MpegCoder.h new file mode 100644 index 0000000..7a1d35b --- /dev/null +++ b/MpegCoder/MpegCoder.h @@ -0,0 +1,138 @@ +// 下列 ifdef 块是创建使从 DLL 导出更简单的 +// 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 MPEGCODER_EXPORT +// 符号编译的。在使用此 DLL 的 +// 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将 +// MPEGCODER_API 函数视为自 DLL 导入,而此 DLL 则将用此宏定义的 +// 符号视为是被导出的。 +#ifndef MPEGCODER_H_INCLUDED +#define MPEGCODER_H_INCLUDED + +#include "MpegBase.h" + +#define MPEGCODER_DEBUG + +// The following classes are exported to mpegCoder.so +namespace cmpc { + + extern int8_t __dumpControl; + class CMpegClient; + class CMpegServer; + + class CMpegDecoder { + public: + CMpegDecoder(void); // Constructor. + ~CMpegDecoder(void); // 3-5 law. Destructor. + CMpegDecoder(const CMpegDecoder &ref); // Copy constructor. + CMpegDecoder& operator=(const CMpegDecoder &ref); // Copy assignment operator. + CMpegDecoder(CMpegDecoder &&ref) noexcept; // Move constructor. + CMpegDecoder& operator=(CMpegDecoder &&ref) noexcept; // Move assignment operator. + friend class CMpegEncoder; // Let the encoder be able to access the member of this class. + friend class CMpegServer; // Let the server be able to access the member of this class. + friend ostream & operator<<(ostream & out, CMpegDecoder & self_class); // Show the results. + void clear(void); // Clear all configurations and resources. + void meta_protected_clear(void); // Clear the resources, but the configurations are remained. + void dumpFormat(); // Show the av_format results. + void setParameter(string keyword, void *ptr); // Set arguments. + PyObject* getParameter(string keyword); // Get the current arguments. + PyObject* getParameter(); // Get all key arguments. + void resetPath(string inVideoPath); // Reset the path (encoded) of the online video stream. + bool FFmpegSetup(); // Configure the decoder, and extract the basic meta-data. This method is also equipped in the constructor. + bool FFmpegSetup(string inVideoPath); // Configure the decoder with extra arguments. + bool ExtractFrame(PyObject* PyFrameList, int64_t framePos, int64_t frameNum, double timePos, int mode); // Extract n frames as PyFrame, where n is given by frameNum, and the starting postion is given by framePos. + bool ExtractGOP(PyObject* PyFrameList); // Extract a GOP as PyFrames. + void setGOPPosition(int64_t inpos); // Set the current GOP poistion by the index of frames. + void setGOPPosition(double inpos); // Set the cuurent GOP position by the time. + private: + string videoPath; // The path of video stream to be decoded. + AVFormatContext *PFormatCtx; // Format context of the video. + AVCodecContext *PCodecCtx; // Codec context of the video. + int width, height; // Width, height of the video. + int widthDst, heightDst; // Target width, height of ExtractFrame(). + enum AVPixelFormat PPixelFormat; // Enum object of the pixel format. + AVStream *PVideoStream; // Video stream. + + int PVideoStreamIDX; // The index of the video stream. + int PVideoFrameCount; // The counter of the decoded frames. + uint8_t *RGBbuffer; // The buffer of the RGB formatted images. + struct SwsContext *PswsCtx; // The context of the scale transformator. + + string _str_codec; // Show the name of the current codec. + double _duration; // Show the time of the video play. + int64_t _predictFrameNum; // The prediction of the total number of frames. + + int64_t currentGOPTSM; // The timestamp where the GOP cursor is pointinng to. + bool EndofGOP; // A flag of reading GOP. This value need to be reset to be false by the reset methods. + int nthread; // The number of threads; + + /* Enable or disable frame reference counting. You are not supposed to support + * both paths in your application but pick the one most appropriate to your + * needs. Look for the use of refcount in this example to see what are the + * differences of API usage between them. */ + int refcount; // Reference count of the video frame. + int _open_codec_context(int &stream_idx, AVCodecContext *&dec_ctx, AVFormatContext *PFormatCtx, enum AVMediaType type); + int _SaveFrame(PyObject *PyFrameList, AVFrame *&frame, AVFrame *&frameRGB, AVPacket *&pkt, bool &got_frame, int64_t minPTS, bool &processed, int cached); + int _SaveFrameForGOP(PyObject *PyFrameList, AVFrame *&frame, AVFrame *&frameRGB, AVPacket *&pkt, bool &got_frame, int &GOPstate, bool &processed, int cached); + PyObject *_SaveFrame_castToPyFrameArray(uint8_t *data[], int fWidth, int fHeight); + PyObject *_SaveFrame_castToPyFrameArrayOld(uint8_t *data[], int fWidth, int fHeight); + int __avcodec_decode_video2(AVCodecContext *avctx, AVFrame *frame, bool &got_frame, AVPacket *pkt); + int64_t __FrameToPts(int64_t seekFrame) const; + int64_t __TimeToPts(double seekTime) const; + }; + + class CMpegEncoder { + public: + CMpegEncoder(void); // Constructor. + ~CMpegEncoder(void); // 3-5 law. Destructor. + CMpegEncoder(const CMpegEncoder &ref); // Copy constructor. + CMpegEncoder& operator=(const CMpegEncoder &ref); // Copy assignment operator. + CMpegEncoder(CMpegEncoder &&ref) noexcept; // Move constructor. + CMpegEncoder& operator=(CMpegEncoder &&ref) noexcept; // Move assignment operator. + friend ostream & operator<<(ostream & out, CMpegEncoder & self_class); // Show the results. + void clear(void); // Clear all configurations and resources. + void resetPath(string inVideoPath); // Reset the path of the output video stream. + void dumpFormat(); // Show the av_format results. + bool FFmpegSetup(); // Configure the encoder, and create the file handle. This method is also equipped in the constructor. + bool FFmpegSetup(string inVideoPath); // Configure the encoder with extra arguments. + void FFmpegClose(); // Close the encoder, and finalize the written of the encoded video. + int EncodeFrame(PyArrayObject* PyFrame); // Encode one frame. + void setParameter(string keyword, void *ptr); // Set arguments. + PyObject* getParameter(string keyword); // Get the current arguments. + PyObject* getParameter(); // Get all key arguments. + private: + string videoPath; // The path of the output video stream. + string codecName; // The name of the codec + int64_t bitRate; // The bit rate of the output video. + int width, height; // The size of the frames in the output video. + int widthSrc, heightSrc; // The size of the input data (frames). + AVRational timeBase, frameRate; // The time base and the frame rate. + int GOPSize, MaxBFrame; // The size of GOPs, and the maximal number of B frames. + OutputStream PStreamContex; // The context of the current video parser. + AVFormatContext *PFormatCtx; // Format context of the video. + AVPacket *Ppacket; // AV Packet used for writing frames. + struct SwsContext *PswsCtx; // The context of the scale transformator. + AVFrame *__frameRGB; // A temp AV frame object. Used for converting the data format. + uint8_t *RGBbuffer; // Data buffer. + bool __have_video, __enable_header; + + int nthread; // The number of threads; + + AVRational _setAVRational(int num, int den); + int64_t __FrameToPts(int64_t seekFrame) const; + int64_t __TimeToPts(double seekTime) const; + bool _LoadFrame_castFromPyFrameArray(AVFrame *frame, PyArrayObject* PyFrame); + void __log_packet(); + int __write_frame(); + bool __add_stream(AVCodec **codec); + AVFrame* __alloc_picture(enum AVPixelFormat pix_fmt, int width, int height); + bool __open_video(AVCodec *codec, AVDictionary *opt_arg); + AVFrame *__get_video_frame(PyArrayObject* PyFrame); + int __avcodec_encode_video2(AVCodecContext *enc_ctx, AVPacket *pkt, AVFrame *frame); + int __avcodec_encode_video2_flush(AVCodecContext *enc_ctx, AVPacket *pkt); + void __copyMetaData(const CMpegEncoder &ref); + }; + + ostream& operator<<(ostream& out, CMpegDecoder& self_class); + ostream& operator<<(ostream& out, CMpegEncoder& self_class); +} + +#endif diff --git a/MpegCoder/MpegPyd.h b/MpegCoder/MpegPyd.h new file mode 100644 index 0000000..56840b7 --- /dev/null +++ b/MpegCoder/MpegPyd.h @@ -0,0 +1,1616 @@ +#ifndef MPEGPYD_H_INCLUDED +#define MPEGPYD_H_INCLUDED + +#define PY_ARRAY_UNIQUE_SYMBOL MPEGARRAY_API +#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION + +#include +#include +#include +#include +#include +#include +#include +#include "MpegCoder.h" +#include "MpegStreamer.h" +using std::string; +using std::ostringstream; + +PyObject *str2PyStr(string Str) { + // Convert the output string to the widechar unicode string. + int wlen = mbstowcs(nullptr, Str.c_str(), 0); + wchar_t* wszString = new wchar_t[wlen + 1]; + mbstowcs(wszString, Str.c_str(), wlen); + wszString[wlen] = 0; + PyObject* res = PyUnicode_FromUnicode((const Py_UNICODE*)wszString, wlen); + delete[] wszString; + return res; +} + +bool PyStr2str(PyObject* py_str, string &s_str) { // Convert a python str to std::string. + if (!py_str) { + return false; + } + if (PyUnicode_Check(py_str)) { + auto py_bytes = PyUnicode_EncodeFSDefault(py_str); + if (!py_bytes) { + PyErr_SetString(PyExc_TypeError, "Error.PyStr2str: fail to encode the unicode str.'"); + return false; + } + auto c_str = PyBytes_AsString(py_bytes); + if (!c_str) { + PyErr_SetString(PyExc_TypeError, "Error.PyStr2str: fail to parse data from the encoded str.'"); + return false; + } + s_str.assign(c_str); + Py_DECREF(py_bytes); + } + else { + if (PyBytes_Check(py_str)) { + auto c_str = PyBytes_AsString(py_str); + if (!c_str) { + PyErr_SetString(PyExc_TypeError, "Error.PyStr2str: fail to parse data from the bytes object.'"); + return false; + } + s_str.assign(c_str); + } + else { + PyErr_SetString(PyExc_TypeError, "Error.PyStr2str: fail to convert the object to string, maybe the object is not str or bytes.'"); + return false; + } + } + return true; +} + +/***************************************************************************** +* C style definition of Python classes. +* Each class would ref the C implemented class directly. +* No extra python data member is added to these classes, +* because the data members have been already packed as private members of the +* C classes. +*****************************************************************************/ +typedef struct _C_MpegDecoder +{ + PyObject_HEAD // == PyObject ob_base; Define the PyObject header. + cmpc::CMpegDecoder *_in_Handle; // Define the implementation of the C Object. +} C_MpegDecoder; + +typedef struct _C_MpegEncoder +{ + PyObject_HEAD // == PyObject ob_base; Define the PyObject header. + cmpc::CMpegEncoder *_in_Handle; // Define the implementation of the C Object. +} C_MpegEncoder; + +typedef struct _C_MpegClient +{ + PyObject_HEAD // == PyObject ob_base; Define the PyObject header. + cmpc::CMpegClient *_in_Handle; // Define the implementation of the C Object. +} C_MpegClient; + +typedef struct _C_MpegServer +{ + PyObject_HEAD // == PyObject ob_base; Define the PyObject header. + cmpc::CMpegServer* _in_Handle; // Define the implementation of the C Object. +} C_MpegServer; + +static PyMemberDef C_MPDC_DataMembers[] = // Register the members of the python class. +{ // Do not register any data, because all data of this class is private. + //{"m_dEnglish", T_FLOAT, offsetof(CScore, m_dEnglish), 0, "The English score of instance."}, + { "hAddress", T_ULONGLONG, offsetof(C_MpegDecoder, _in_Handle), READONLY, "The address of the handle in memory." }, + { nullptr, 0, 0, 0, nullptr } +}; + +static PyMemberDef C_MPEC_DataMembers[] = // Register the members of the python class. +{ // Do not register any data, because all data of this class is private. + //{"m_dEnglish", T_FLOAT, offsetof(CScore, m_dEnglish), 0, "The English score of instance."}, + { "hAddress", T_ULONGLONG, offsetof(C_MpegEncoder, _in_Handle), READONLY, "The address of the handle in memory." }, + { nullptr, 0, 0, 0, nullptr } +}; + +static PyMemberDef C_MPCT_DataMembers[] = // Register the members of the python class. +{ // Do not register any data, because all data of this class is private. + //{"m_dEnglish", T_FLOAT, offsetof(CScore, m_dEnglish), 0, "The English score of instance."}, + { "hAddress", T_ULONGLONG, offsetof(C_MpegClient, _in_Handle), READONLY, "The address of the handle in memory." }, + { nullptr, 0, 0, 0, nullptr } +}; + +static PyMemberDef C_MPSV_DataMembers[] = // Register the members of the python class. +{ // Do not register any data, because all data of this class is private. + //{"m_dEnglish", T_FLOAT, offsetof(CScore, m_dEnglish), 0, "The English score of instance."}, + { "hAddress", T_ULONGLONG, offsetof(C_MpegServer, _in_Handle), READONLY, "The address of the handle in memory." }, + { nullptr, 0, 0, 0, nullptr } +}; + +/***************************************************************************** +* Delearaction of all methods and functions. +* Prepare the function objects for the registeration of the classes and +* functions. +*****************************************************************************/ +/*static void Example(ClassName* Self, PyObject* pArgs); +PyMODINIT_FUNC PyFunc_Example(void);*/ + +static PyObject* C_MPC_Global(PyObject* Self, PyObject *args, PyObject *kwargs) { + char dumpLevel = -1; + static char *kwlist[] = { "dumpLevel", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|B", kwlist, &dumpLevel)) { + PyErr_SetString(PyExc_TypeError, "Error.GlobalSettings: invalid keyword'"); + return nullptr; + } + if (dumpLevel != -1) { + cmpc::__dumpControl = static_cast(dumpLevel); + switch (dumpLevel) { + case 0: + cmpc::av_log_set_level(AV_LOG_ERROR); + break; + case 1: + cmpc::av_log_set_level(AV_LOG_INFO); + break; + case 2: + default: + cmpc::av_log_set_level(AV_LOG_DEBUG); + break; + } + } + Py_RETURN_NONE; +} + +static PyObject* C_MPC_Help(PyObject* Self) { + cout << R"(================================================================================ + __ _ _ _ _ ,___ + ( / / / o ( / ) ) / / / + (__/ , , _, /_ _ _ _' ( / / / ,_ _ _, / __ __/ _ _ + _/_(_/_(__/ /_(/_/ / /_/_)_ / / (__/|_)_(/_(_)_(___/(_)(_/_(/_/ (_ + // /| /| + (/ (/ (/ +================================================================================ +Yuchen's Mpeg Coder - Readme + This is a mpegcoder adapted from FFmpeg & Python-c-api.Using it you could + get access to processing video easily. Just use it as a common module in + python like this. + >>> import mpegCoder + Noted that this API need you to install numpy. + An example of decoding a video in an arbitrary format: + >>> d = mpegCoder.MpegDecoder() + >>> d.FFmpegSetup(b'inputVideo.mp4') + >>> p = d.ExtractGOP(10) # Get a gop of current video by setting the + start position of 10th frame. + >>> p = d.ExtractGOP() # Get a gop of current video, using the current + position after the last ExtractGOP. + >>> d.ExtractFrame(100, 100) # Extract 100 frames from the begining of + 100th frame. + An example of transfer the coding of a video with an assigned codec: + >>> d = mpegCoder.MpegDecoder() + >>> d.FFmpegSetup(b'i.avi') + >>> e = mpegCoder.MpegEncoder() + >>> e.setParameter(decoder=d, codecName=b'libx264', videoPath=b'o.mp4') + # inherit most of parameters from the decoder. + >>> opened = e.FFmpegSetup() # Load the encoder. + >>> if opened: # If encoder is not loaded successfully, do not continue. + ... p = True + ... while p: + ... p = d.ExtractGOP() # Extract current GOP. + ... if p is not None: + ... for i in p: # Select every frame. + ... e.EncodeFrame(i) # Encode current frame. + ... e.FFmpegClose() # End encoding, and flush all frames in cache. + >>> d.clear() # Close the input video. + An example of demuxing the video streamer from a server: + >>> d = mpegCoder.MpegClient() # create the handle + >>> d.setParameter(dstFrameRate=(5,1), readSize=5, cacheSize=12) + # normalize the frame rate to 5 FPS, and use a cache which size is + # 12 frames. Read 5 frames each time. + >>> success = d.FFmpegSetup(b'rtsp://localhost:8554/video') + >>> if not success: # exit if fail to connect with the server + ... exit() + >>> d.start() # start the sub-thread for demuxing the stream. + >>> for i in range(10): # processing loop + ... time.sleep(5) + ... p = d.ExtractFrame() # every 5 seconds, read 5 frames (1 sec.) + ... # do some processing + >>> d.terminate() # shut down the current thread. You could call start() + # and let it restart. + >>> d.clear() # Disconnect with the stream. + For more instructions, you could tap help(mpegCoder). +================================================================================ +V3.1.0 update report: + 1. Support str() type for all string arguments. + 2. Support http, ftp, sftp streams for MpegServer. + 3. Support "nthread" option for MpegDecoder, MpegEncoder, MpegClient and + MpegServer. + 4. Fix typos in docstrings. +V3.0.0 update report: + 1. Fix a severe memory leaking bugs when using AVPacket. + 2. Fix a bug caused by using `MpegClient.terminate()` when a video is closed + by the server. + 3. Support the MpegServer. This class is used for serving the online video + streams. + 4. Refactor the implementation of the loggings. + 5. Add getParameter() and setParameter(configDict) APIs to MpegEncoder and + MpegServer. + 6. Move FFMpeg depedencies and the OutputStream class to the cmpc space. + 7. Fix dependency issues and cpp standard issues. + 8. Upgrade to `FFMpeg 4.4` Version. + 9. Add a quick script for fetching the `FFMpeg` dependencies. +V2.05 update report: + 1. Fix a severe bug that causes the memory leak when using MpegClient. + This bug also exists in MpegDecoder, but it seems that the bug would not cause + memory leak in that case. (Although we have also fixed it now.) + 2. Upgrade to FFMpeg 4.0 Version. +V2.01 update report: + Fix a bug that occurs when the first received frame may has a PTS larger than + zero. +V2.0 update report: + 1. Revise the bug of the encoder which may cause the stream duration is shorter + than the real duration of the video in some not advanced media players. + 2. Improve the structure of the code and remove some unnecessary codes. + 3. Provide a complete version of client, which could demux the video stream + from a server in any network protocol. +V1.8 update report: + 1. Provide options (widthDst, heightDst) to let MpegDecoder could control the + output size manually. To ensure the option is valid, we must use the method + 'setParameter' before 'FFmpegSetup'. + 2. Optimize some realization of Decoder so that its efficiency could be + improved. +V1.7 update report: + 1. Realize the encoder totally. + 2. Provide a global option 'dumpLevel' to control the log shown in the screen. + 3. Fix bugs in initalize functions. +V1.5 update report: + 1. Provide an incomplete version of encoder, which could encode frames as a + video stream that could not be played by player. +V1.4 update report: + 1. Fix a severe bug of the decoder, which causes the memory collapsed if + decoding a lot of frames. +V1.2 update report: + 1. Use numpy array to replace the native pyList, which improves the speed + significantlly. +V1.0 update report: + 1. Provide the decoder which could decode videos in arbitrary formats and + arbitrary coding. +)"; + Py_RETURN_NONE; +} + +/***************************************************************************** +* Declare the core methods of the classes. +*****************************************************************************/ +static int C_MPDC_init(C_MpegDecoder* Self, PyObject* args, PyObject *kwargs) { // Construct + PyObject* vpath = nullptr; + static char *kwlist[] = { "videoPath", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) { + PyErr_SetString(PyExc_TypeError, "Error.Initialize: need 'videoPath(str)'" ); + return -1; + } + string in_vpath; + if (!vpath) { + in_vpath.clear(); + } + else if (!PyStr2str(vpath, in_vpath)) { + return -1; + } + Self->_in_Handle = new cmpc::CMpegDecoder; + if (!in_vpath.empty()) { + Self->_in_Handle->FFmpegSetup(in_vpath); + } + + in_vpath.clear(); + //cout << sizeof(Self->_in_Handle) << " - " << sizeof(unsigned long long) << endl; + return 0; +} + +static int C_MPEC_init(C_MpegEncoder* Self) { // Construct + Self->_in_Handle = new cmpc::CMpegEncoder; + return 0; +} + +static int C_MPCT_init(C_MpegClient* Self) { // Construct + Self->_in_Handle = new cmpc::CMpegClient; + return 0; +} + +static int C_MPSV_init(C_MpegServer* Self) { // Construct + Self->_in_Handle = new cmpc::CMpegServer; + return 0; +} + +static void C_MPDC_Destruct(C_MpegDecoder* Self) { // Destructor + delete Self->_in_Handle; // Delete the allocated class implementation. + /* If there are still other members, also need to deallocate them, + * for example, Py_XDECREF(Self->Member); */ + Py_TYPE(Self)->tp_free((PyObject*)Self); // Destruct the PyObject. +} + +static void C_MPEC_Destruct(C_MpegEncoder* Self) { // Destructor + delete Self->_in_Handle; // Delete the allocated class implementation. + /* If there are still other members, also need to deallocate them, + * for example, Py_XDECREF(Self->Member); */ + Py_TYPE(Self)->tp_free((PyObject*)Self); // Destruct the PyObject. +} + +static void C_MPCT_Destruct(C_MpegClient* Self) { // Destructor + delete Self->_in_Handle; // Delete the allocated class implementation. + /* If there are still other members, also need to deallocate them, + * for example, Py_XDECREF(Self->Member); */ + Py_TYPE(Self)->tp_free((PyObject*)Self); // Destruct the PyObject. +} + +static void C_MPSV_Destruct(C_MpegServer* Self) { // Destructor + delete Self->_in_Handle; // Delete the allocated class implementation. + /* If there are still other members, also need to deallocate them, + * for example, Py_XDECREF(Self->Member); */ + Py_TYPE(Self)->tp_free((PyObject*)Self); // Destruct the PyObject. +} + +static PyObject* C_MPDC_Str(C_MpegDecoder* Self) { // The __str__ (print) operator. + ostringstream OStr; + OStr << *(Self->_in_Handle); + string Str = OStr.str(); + return str2PyStr(Str); // Convert the string to unicode wide char. +} + +static PyObject* C_MPEC_Str(C_MpegEncoder* Self) { // The __str__ (print) operator. + ostringstream OStr; + OStr << *(Self->_in_Handle); + string Str = OStr.str(); + return str2PyStr(Str); // Convert the string to unicode wide char. +} + +static PyObject* C_MPCT_Str(C_MpegClient* Self) { // The __str__ (print) operator. + ostringstream OStr; + OStr << *(Self->_in_Handle); + string Str = OStr.str(); + return str2PyStr(Str); // Convert the string to unicode wide char. +} + +static PyObject* C_MPSV_Str(C_MpegServer* Self) { // The __str__ (print) operator. + ostringstream OStr; + OStr << *(Self->_in_Handle); + string Str = OStr.str(); + return str2PyStr(Str); // Convert the string to unicode wide char. +} + +static PyObject* C_MPDC_Repr(C_MpegDecoder* Self) { // The __repr__ operator. + return C_MPDC_Str(Self); +} + +static PyObject* C_MPEC_Repr(C_MpegEncoder* Self) { // The __repr__ operator. + return C_MPEC_Str(Self); +} + +static PyObject* C_MPCT_Repr(C_MpegClient* Self) { // The __repr__ operator. + return C_MPCT_Str(Self); +} + +static PyObject* C_MPSV_Repr(C_MpegServer* Self) { // The __repr__ operator. + return C_MPSV_Str(Self); +} + +/***************************************************************************** +* Define the Python-C-APIs for . +* C_MPDC_Setup: Configure the decoder by the video. +* C_MPDC_ExtractFrame Extract serveral frames. +*****************************************************************************/ +static PyObject* C_MPDC_Setup(C_MpegDecoder* Self, PyObject *args, PyObject *kwargs) { + /* Wrapped (bool)C_MPDC_Setup method, the inputs are: + * videoPath [str/bytes->str]: the video path to be decoded. + */ + PyObject* vpath = nullptr; + static char *kwlist[] = { "videoPath", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) { + PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoPath(str)'"); + return nullptr; + } + string in_vpath; + if (!vpath) { + in_vpath.clear(); + } + else if (!PyStr2str(vpath, in_vpath)) { + return nullptr; + } + bool res; + if (!in_vpath.empty()) + res = Self->_in_Handle->FFmpegSetup(in_vpath); + else + res = Self->_in_Handle->FFmpegSetup(); + + in_vpath.clear(); + if (res) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; +} + +static PyObject* C_MPEC_Setup(C_MpegEncoder* Self, PyObject *args, PyObject *kwargs) { + /* Wrapped (bool)C_MPEC_Setup method, the inputs are: + * videoPath [str/bytes->str]: the video path to be encoded. + */ + PyObject* vpath = nullptr; + static char *kwlist[] = { "videoPath", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) { + PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoPath(str)'"); + return nullptr; + } + string in_vpath; + if (!vpath) { + in_vpath.clear(); + } + else if (!PyStr2str(vpath, in_vpath)) { + return nullptr; + } + bool res; + if (!in_vpath.empty()) + res = Self->_in_Handle->FFmpegSetup(in_vpath); + else + res = Self->_in_Handle->FFmpegSetup(); + + in_vpath.clear(); + if (res) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; +} + +static PyObject* C_MPCT_Setup(C_MpegClient* Self, PyObject *args, PyObject *kwargs) { + /* Wrapped (bool)C_MPCT_Setup method, the inputs are: + * videoAddress [str/bytes->str]: the video path to be demuxed. + */ + PyObject* vpath = nullptr; + static char *kwlist[] = { "videoAddress", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) { + PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoAddress(str)'"); + return nullptr; + } + string in_vpath; + if (!vpath) { + in_vpath.clear(); + } + else if (!PyStr2str(vpath, in_vpath)) { + return nullptr; + } + bool res; + if (!in_vpath.empty()) + res = Self->_in_Handle->FFmpegSetup(in_vpath); + else + res = Self->_in_Handle->FFmpegSetup(); + + in_vpath.clear(); + if (res) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; +} + +static PyObject* C_MPSV_Setup(C_MpegServer* Self, PyObject* args, PyObject* kwargs) { + /* Wrapped (bool)C_MPSV_Setup method, the inputs are: + * videoAddress [str/bytes->str]: the video address to be served. + */ + PyObject* vpath = nullptr; + static char* kwlist[] = { "videoAddress", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) { + PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoAddress(str)'"); + return nullptr; + } + string in_vpath; + if (!vpath) { + in_vpath.clear(); + } + else if (!PyStr2str(vpath, in_vpath)) { + return nullptr; + } + bool res; + if (!in_vpath.empty()) + res = Self->_in_Handle->FFmpegSetup(in_vpath); + else + res = Self->_in_Handle->FFmpegSetup(); + + in_vpath.clear(); + if (res) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; +} + +static PyObject* C_MPDC_resetPath(C_MpegDecoder* Self, PyObject *args, PyObject *kwargs) { + /* Wrapped (bool)C_MPDC_resetPath method, the inputs are: + * videoPath [str/bytes->str]: the video path to be decoded. + */ + PyObject* vpath = nullptr; + static char *kwlist[] = { "videoPath", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) { + PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoPath(str)'"); + return nullptr; + } + string in_vpath; + if (!PyStr2str(vpath, in_vpath)) { + return nullptr; + } + Self->_in_Handle->resetPath(in_vpath); + + in_vpath.clear(); + Py_RETURN_NONE; +} + +static PyObject* C_MPEC_resetPath(C_MpegEncoder* Self, PyObject* args, PyObject* kwargs) { + /* Wrapped (bool)C_MPEC_resetPath method, the inputs are: + * videoPath [str/bytes->str]: the video path to be encoded. + */ + PyObject* vpath = nullptr; + static char* kwlist[] = { "videoPath", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) { + PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoPath(str)'"); + return nullptr; + } + string in_vpath; + if (!PyStr2str(vpath, in_vpath)) { + return nullptr; + } + Self->_in_Handle->resetPath(in_vpath); + + in_vpath.clear(); + Py_RETURN_NONE; +} + +static PyObject* C_MPCT_resetPath(C_MpegClient* Self, PyObject *args, PyObject *kwargs) { + /* Wrapped (bool)C_MPCT_resetPath method, the inputs are: + * videoAddress [str/bytes->str]: the video path to be demuxed. + */ + PyObject* vpath = nullptr; + static char *kwlist[] = { "videoAddress", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) { + PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoAddress(str)'"); + return nullptr; + } + string in_vpath; + if (!PyStr2str(vpath, in_vpath)) { + return nullptr; + } + Self->_in_Handle->resetPath(in_vpath); + + in_vpath.clear(); + Py_RETURN_NONE; +} + +static PyObject* C_MPSV_resetPath(C_MpegServer* Self, PyObject* args, PyObject* kwargs) { + /* Wrapped (bool)C_MPSV_resetPath method, the inputs are: + * videoAddress [str/bytes->str]: the video address to be served. + */ + PyObject* vpath = nullptr; + static char* kwlist[] = { "videoAddress", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) { + PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoAddress(str)'"); + return nullptr; + } + string in_vpath; + if (!PyStr2str(vpath, in_vpath)) { + return nullptr; + } + Self->_in_Handle->resetPath(in_vpath); + + in_vpath.clear(); + Py_RETURN_NONE; +} + +static PyObject* C_MPCT_Start(C_MpegClient* Self) { + /* Wrapped (void)Start method, the input is required to be empty. */ + auto success = Self->_in_Handle->start(); + if (!success) { + PyErr_SetString(PyExc_ConnectionError, "Error.Start: before call this method, need to call FFmpegSetup() successfully, and also you should not call it when the decoding thread is running.'"); + return nullptr; + } + Py_RETURN_NONE; +} + +static PyObject* C_MPCT_Terminate(C_MpegClient* Self) { + /* Wrapped (void)Terminate method, the input is required to be empty. */ + Self->_in_Handle->terminate(); + Py_RETURN_NONE; +} + +/* Pay attention to the following two methods : + * Why do we remove the Py_IN/DECREF? + * Because no temp variables are created, so we do not need to manage them, + * but just use None as the returned value. */ +static PyObject* FreePyArray(PyArrayObject *PyArray) { + uint8_t * out_dataptr = (uint8_t *)PyArray_DATA(PyArray); + delete [] out_dataptr; + return nullptr; +} +void FreePyList(PyObject *PyList) { + Py_ssize_t getlen = PyList_Size(PyList); + for (Py_ssize_t i = 0; i < getlen; i++) { + PyObject *Item = PyList_GetItem(PyList, i); + FreePyArray((PyArrayObject*)Item); + } + Py_DECREF(PyList); + PyGC_Collect(); +} + +static PyObject* C_MPDC_ExtractFrame(C_MpegDecoder* Self, PyObject *args, PyObject *kwargs) { + /* Wrapped (int)ExtractFrame method, the inputs are: + * framePos [int->int64_t]: the start position of the extracted frames. + * frameNum [int->int64_t]: the number of extracted frames. + */ + int64_t framePos = 0, frameNum = 1; + static char *kwlist[] = { "framePos", "frameNum", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|LL", kwlist, &framePos, &frameNum)) { + PyErr_SetString(PyExc_TypeError, "Error.ExtractFrame: need 'framePos(int)/frameNum(int)'"); + return nullptr; + } + PyObject *PyFrameList = PyList_New(static_cast(0)); + //cout << framePos << " - " << frameNum << endl; + bool res = Self->_in_Handle->ExtractFrame(PyFrameList, framePos, frameNum, 0, 0); + Py_ssize_t getlen = PyList_Size(PyFrameList); + res = res && (getlen > 0); + if (res) { + PyObject *PyFrameArray = PyArray_FromObject(PyFrameList, NPY_UINT8, 4, 4); + FreePyList(PyFrameList); + return PyFrameArray; + } + else { + Py_DECREF(PyFrameList); + PyGC_Collect(); + Py_RETURN_NONE; + } +} + +static PyObject* C_MPDC_ExtractFrame_Time(C_MpegDecoder* Self, PyObject *args, PyObject *kwargs) { + /* Wrapped (int)ExtractFrame method, the inputs are: + * timePos [float->double]: the start position (time unit) of the extracted frames. + * frameNum [int->int64_t]: the number of extracted frames. + */ + double timePos = 0; + int64_t frameNum = 1; + static char *kwlist[] = { "timePos", "frameNum", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|dL", kwlist, &timePos, &frameNum)) { + PyErr_SetString(PyExc_TypeError, "Error.ExtractFrame_Time: need 'timePos(float)/frameNum(int)'"); + return nullptr; + } + PyObject *PyFrameList = PyList_New(static_cast(0)); + //cout << framePos << " - " << frameNum << endl; + bool res = Self->_in_Handle->ExtractFrame(PyFrameList, 0, frameNum, timePos, 1); + Py_ssize_t getlen = PyList_Size(PyFrameList); + res = res && (getlen > 0); + if (res) { + PyObject *PyFrameArray = PyArray_FromObject(PyFrameList, NPY_UINT8, 4, 4); + FreePyList(PyFrameList); + return PyFrameArray; + } + else { + Py_DECREF(PyFrameList); + PyGC_Collect(); + Py_RETURN_NONE; + } +} + +static PyObject* C_MPEC_EncodeFrame(C_MpegEncoder* Self, PyObject *args, PyObject *kwargs) { + /* Wrapped (bool)EncodeFrame method, the inputs are: + * PyArrayFrame [ndarray->PyArrayObject]: the frame to be encoded. + */ + PyObject *PyArrayFrame = nullptr; + static char *kwlist[] = { "PyArrayFrame", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &PyArrayFrame)) { + PyErr_SetString(PyExc_TypeError, "Error.EncodeFrame: need 'PyArrayFrame(ndarray)'"); + return nullptr; + } + int res = Self->_in_Handle->EncodeFrame(reinterpret_cast(PyArrayFrame)); + if (res>=0) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; +} + +static PyObject* C_MPSV_ServeFrame(C_MpegServer* Self, PyObject* args, PyObject* kwargs) { + /* Wrapped (bool)ServeFrame method, the inputs are: + * PyArrayFrame [ndarray->PyArrayObject]: the frame to be encoded and served. + */ + PyObject* PyArrayFrame = nullptr; + static char* kwlist[] = { "PyArrayFrame", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &PyArrayFrame)) { + PyErr_SetString(PyExc_TypeError, "Error.EncodeFrame: need 'PyArrayFrame(ndarray)'"); + return nullptr; + } + int res = Self->_in_Handle->ServeFrame(reinterpret_cast(PyArrayFrame)); + if (res >= 0) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; +} + +static PyObject* C_MPSV_ServeFrameBlock(C_MpegServer* Self, PyObject* args, PyObject* kwargs) { + /* Wrapped (bool)ServeFrameBlock method, the inputs are: + * PyArrayFrame [ndarray->PyArrayObject]: the frame to be encoded and served. + */ + PyObject* PyArrayFrame = nullptr; + static char* kwlist[] = { "PyArrayFrame", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &PyArrayFrame)) { + PyErr_SetString(PyExc_TypeError, "Error.EncodeFrame: need 'PyArrayFrame(ndarray)'"); + return nullptr; + } + int res = Self->_in_Handle->ServeFrameBlock(reinterpret_cast(PyArrayFrame)); + if (res >= 0) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; +} + +static PyObject* C_MPCT_ExtractFrame(C_MpegClient* Self, PyObject *args, PyObject *kwargs) { + /* Wrapped (int)ExtractFrame method, the inputs are: + * readSize [int->int64_t]: the number of frames to be readed. This value could not + * exceeded the size of the frame buffer. + */ + int64_t readSize = 0; + static char *kwlist[] = { "readSize", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|L", kwlist, &readSize)) { + PyErr_SetString(PyExc_TypeError, "Error.ExtractFrame: need 'readSize(int)'"); + return nullptr; + } + PyObject *res = nullptr; + if (readSize>0) + res = Self->_in_Handle->ExtractFrame(readSize); + else + res = Self->_in_Handle->ExtractFrame(); + if (res) { + return res; + } + else { + Py_RETURN_NONE; + } +} + +static PyObject* C_MPDC_ExtractGOP(C_MpegDecoder* Self, PyObject *args, PyObject *kwargs) { + /* Wrapped (int)ExtractGOP method, the inputs are: + * framePos [int->int64_t]: the start position of the GOP to be extracted. + */ + int64_t framePos = -1; + static char *kwlist[] = { "framePos", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|L", kwlist, &framePos)) { + PyErr_SetString(PyExc_TypeError, "Error.ExtractGOP: need 'framePos(int)'"); + return nullptr; + } + PyObject *PyFrameList = PyList_New(static_cast(0)); + //cout << framePos << " - " << frameNum << endl; + if (!(framePos < 0)) + Self->_in_Handle->setGOPPosition(framePos); + bool res = Self->_in_Handle->ExtractGOP(PyFrameList); + Py_ssize_t getlen = PyList_Size(PyFrameList); + res = res && (getlen > 0); + if (res) { + PyObject *PyFrameArray = PyArray_FromObject(PyFrameList, NPY_UINT8, 4, 4); + FreePyList(PyFrameList); + return PyFrameArray; + } + else { + Py_DECREF(PyFrameList); + PyGC_Collect(); + Py_RETURN_NONE; + } +} + +static PyObject* C_MPDC_ExtractGOP_Time(C_MpegDecoder* Self, PyObject *args, PyObject *kwargs) { + /* Wrapped (int)ExtractGOP_Time method, the inputs are: + * timePos [float->double]: the start position (time unit) of the GOP to be extracted. + */ + double timePos = -1; + static char *kwlist[] = { "timePos", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|d", kwlist, &timePos)) { + PyErr_SetString(PyExc_TypeError, "Error.ExtractGOP_Time: need 'timePos(float)'"); + return nullptr; + } + PyObject *PyFrameList = PyList_New(static_cast(0)); + //cout << framePos << " - " << frameNum << endl; + if (!(timePos < 0)) + Self->_in_Handle->setGOPPosition(timePos); + bool res = Self->_in_Handle->ExtractGOP(PyFrameList); + Py_ssize_t getlen = PyList_Size(PyFrameList); + res = res && (getlen > 0); + if (res) { + PyObject *PyFrameArray = PyArray_FromObject(PyFrameList, NPY_UINT8, 4, 4); + FreePyList(PyFrameList); + return PyFrameArray; + } + else { + Py_DECREF(PyFrameList); + PyGC_Collect(); + Py_RETURN_NONE; + } +} + +static PyObject* C_MPDC_setGOPPosition(C_MpegDecoder* Self, PyObject *args, PyObject *kwargs) { + /* Wrapped (void)setGOPPosition method, the inputs are: + * framePos [int->int64_t]: the start position of the GOP to be extracted. + * timePos [float->double]: the start position (time unit) of the GOP to be extracted. + */ + int64_t framePos = -1; + double timePos = -1; + static char *kwlist[] = { "framePos", "timePos", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ld", kwlist, &framePos, &timePos)) { + PyErr_SetString(PyExc_TypeError, "Error.setGOPPosition: need 'framePos(int)'/'timePos(float)'"); + return nullptr; + } + if (!(framePos < 0)) + Self->_in_Handle->setGOPPosition(framePos); + else if (!(timePos < 0)) + Self->_in_Handle->setGOPPosition(timePos); + Py_RETURN_NONE; +} + +static PyObject* C_MPDC_getParam(C_MpegDecoder* Self, PyObject *args, PyObject *kwargs) { + /* Wrapped (bool)C_MPDC_getParam function, the inputs are: + * paramName [str/bytes->str]: The name of the parameter to be gotten, could be. + * videoPath: [str] Path of the current video. + * width/height: [int] The width / height of the frame. + * frameCount: [int] The count of frames of the current decoding work. + * coderName: [str] The name of the decoder. + * nthread: [int] The number of decoder threads. + * duration: [float] The duration of the video. + * estFrameNum: [int] The estimated total frame number. + * avgFrameRate [float] The average frame rate. + */ + PyObject* param = nullptr; + static char *kwlist[] = { "paramName", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, ¶m)) { + PyErr_SetString(PyExc_TypeError, "Error.getParameter: need 'paramName(str)'" ); + return nullptr; + } + string in_param; + if (!param) { + in_param.clear(); + } + else if (!PyStr2str(param, in_param)) { + return nullptr; + } + PyObject* res = nullptr; + if (in_param.empty()) { + res = Self->_in_Handle->getParameter(); + } + else { + res = Self->_in_Handle->getParameter(in_param); + } + in_param.clear(); + return res; +} + +static PyObject* C_MPEC_getParam(C_MpegEncoder* Self, PyObject* args, PyObject* kwargs) { + /* Wrapped (bool)C_MPEC_getParam function, the inputs are: + * paramName [str/bytes->str]: The name of the parameter to be gotten, could be. + * videoPath: [str] Path of the current video. + * codecName: [str] The name of the codec. + * nthread: [int] The number of encoder threads. + * bitRate: [int] The target bit rate. + * width/height: [int] The width / height of the encoded frame. + * widthSrc/heightSrc: [int] The width / height of the input frame. + * GOPSize: [int] The size of one GOP. + * maxBframe: [int] The maximal number of continuous B frames. + * frameRate: [float] The target frame rate. + */ + PyObject* param = nullptr; + static char* kwlist[] = { "paramName", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, ¶m)) { + PyErr_SetString(PyExc_TypeError, "Error.getParameter: need 'paramName(str)'"); + return nullptr; + } + string in_param; + if (!param) { + in_param.clear(); + } + else if (!PyStr2str(param, in_param)) { + return nullptr; + } + PyObject* res = nullptr; + if (in_param.empty()) { + res = Self->_in_Handle->getParameter(); + } + else { + res = Self->_in_Handle->getParameter(in_param); + } + in_param.clear(); + return res; +} + +static PyObject* C_MPCT_getParam(C_MpegClient* Self, PyObject *args, PyObject *kwargs) { + /* Wrapped (bool)C_MPCT_getParam method, the inputs are: + * parameter [str/bytes->str]: The name of the parameter to be gotten, could be. + * videoAddress: [str] The address of the current video. + * width/height: [int] The width / height of the received frame. + * frameCount: [int] The count of frames of the current decoding work. + * coderName: [str] The name of the decoder. + * nthread: [int] The number of decoder threads. + * duration: [float] The duration of the video. + * estFrameNum: [int] The estimated total frame number. + * avgFrameRate [float] The average frame rate. + */ + PyObject* param = nullptr; + static char *kwlist[] = { "paramName", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, ¶m)) { + PyErr_SetString(PyExc_TypeError, "Error.getParameter: need 'paramName(str)'"); + return nullptr; + } + string in_param; + if (!param) { + in_param.clear(); + } + else if (!PyStr2str(param, in_param)) { + return nullptr; + } + PyObject* res = nullptr; + if (in_param.empty()) { + res = Self->_in_Handle->getParameter(); + } + else { + res = Self->_in_Handle->getParameter(in_param); + } + in_param.clear(); + return res; +} + +static PyObject* C_MPSV_getParam(C_MpegServer* Self, PyObject* args, PyObject* kwargs) { + /* Wrapped (bool)C_MPSV_getParam function, the inputs are: + * paramName [str/bytes->str]: The name of the parameter to be gotten, could be. + * videoAddress: [str] The address of the current video. + * codecName: [str] The name of the codec. + * formatName: [str] The name of the stream format. + * nthread: [int] The number of encoder threads. + * bitRate: [int] The target bit rate. + * width/height: [int] The width / height of the encoded frame. + * widthSrc/heightSrc: [int] The width / height of the input frame. + * GOPSize: [int] The size of one GOP. + * maxBframe: [int] The maximal number of continuous B frames. + * frameRate: [float] The target frame rate. + * waitRef [float] The reference used for sync. waiting. + * ptsAhead [int] The ahead time duration in the uit of time stamp. + */ + PyObject* param = nullptr; + static char* kwlist[] = { "paramName", nullptr }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, ¶m)) { + PyErr_SetString(PyExc_TypeError, "Error.getParameter: need 'paramName(str)'"); + return nullptr; + } + string in_param; + if (!param) { + in_param.clear(); + } + else if (!PyStr2str(param, in_param)) { + return nullptr; + } + PyObject* res = nullptr; + if (in_param.empty()) { + res = Self->_in_Handle->getParameter(); + } + else { + res = Self->_in_Handle->getParameter(in_param); + } + in_param.clear(); + return res; +} + +static PyObject* C_MPDC_setParam(C_MpegDecoder* Self, PyObject *args, PyObject *kwargs) { + /* Wrapped (void)C_MPDC_setParam method, the inputs are: + * widthDst/heightDst: [int] The width / height of the decoded frames. + * nthread: [int] The number of decoder threads. + */ + int widthDst = 0; + int heightDst = 0; + int nthread = 0; + static char *kwlist[] = { "widthDst", "heightDst", "nthread", nullptr}; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii", kwlist, &widthDst, &heightDst, &nthread)) { + PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'params'" ); + return nullptr; + } + if (widthDst > 0) { + Self->_in_Handle->setParameter("widthDst", &widthDst); + } + if (heightDst > 0) { + Self->_in_Handle->setParameter("heightDst", &heightDst); + } + if (nthread > 0) { + Self->_in_Handle->setParameter("nthread", &nthread); + } + Py_RETURN_NONE; +} + +static PyObject* C_MPEC_setParam(C_MpegEncoder* Self, PyObject *args, PyObject *kwargs) { + /* Wrapped (bool)C_MPEC_setParam method, the inputs are: + * decoder: [MpegDecoder / MpegClient]: The parameters to be configured. + * configDict: [dict] A collection of key params. + * videoPath: [str/bytes] Path of the current video. + * codecName: [str/bytes] The name of the codec. + * nthread: [int] The number of encoder threads. + * bitRate: [double] The target bit rate. + * width/height: [int] The width / height of the encoded frame. + * widthSrc/heightSrc: [int] The width / height of the input frame. + * GOPSize: [int] The size of one GOP. + * maxBframe: [int] The maximal number of continuous B frames. + * frameRate: [tuple] The target frame rate. + */ + PyObject* decoder = nullptr; + PyObject* configDict = nullptr; + PyObject* videoPath = nullptr; + PyObject* codecName = nullptr; + double bitRate = -1; + int nthread = 0; + int width = 0; + int height = 0; + int widthSrc = 0; + int heightSrc = 0; + int GOPSize = 0; + int MaxBframe = -1; + PyObject *frameRate = nullptr; + static char *kwlist[] = { "decoder", "configDict", "videoPath", "codecName", "nthread", "bitRate", "width", "height", "widthSrc", "heightSrc", "GOPSize", "maxBframe", "frameRate", nullptr}; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOOOidiiiiiiO", kwlist, &decoder, &configDict, &videoPath, &codecName, &nthread, &bitRate, &width, &height, &widthSrc, &heightSrc, &GOPSize, &MaxBframe, &frameRate)) { + PyErr_SetString(PyExc_TypeError, "Error.setParameter: need 'params'"); + return nullptr; + } + string temp_str; + if (decoder) { + temp_str.assign(decoder->ob_type->tp_name); + if (temp_str.compare("mpegCoder.MpegDecoder") == 0) { + auto decoderPtr = reinterpret_cast(decoder); + Self->_in_Handle->setParameter("decoder", decoderPtr->_in_Handle); + } else if (temp_str.compare("mpegCoder.MpegClient") == 0) { + auto decoderPtr = reinterpret_cast(decoder); + Self->_in_Handle->setParameter("client", decoderPtr->_in_Handle); + } + else { + cerr << "Warning.setParameter: Not intended decoder type, no valid update in this step." << endl; + } + } + else if (configDict) { + if (PyDict_Check(configDict)) { + Self->_in_Handle->setParameter("configDict", configDict); + } + else { + cerr << "Warning.setParameter: Not intended configDict type (require to be a dict), no valid update in this step." << endl; + } + } + if (videoPath) { + if (PyStr2str(videoPath, temp_str)) { + Self->_in_Handle->setParameter("videoPath", &temp_str); + } + else { + return nullptr; + } + } + if (codecName) { + if (PyStr2str(codecName, temp_str)) { + Self->_in_Handle->setParameter("codecName", &temp_str); + } + else { + return nullptr; + } + } + if (nthread > 0) { + Self->_in_Handle->setParameter("nthread", &nthread); + } + if (bitRate>0) { + Self->_in_Handle->setParameter("bitRate", &bitRate); + } + if (width>0) { + Self->_in_Handle->setParameter("width", &width); + } + if (height>0) { + Self->_in_Handle->setParameter("height", &height); + } + if (widthSrc>0) { + Self->_in_Handle->setParameter("widthSrc", &widthSrc); + } + if (heightSrc>0) { + Self->_in_Handle->setParameter("heightSrc", &heightSrc); + } + if (GOPSize>0) { + Self->_in_Handle->setParameter("GOPSize", &GOPSize); + } + if (MaxBframe>=0) { + Self->_in_Handle->setParameter("maxBframe", &MaxBframe); + } + if (frameRate) { + if (PyTuple_Check(frameRate) && PyTuple_Size(frameRate) == 2) { + Self->_in_Handle->setParameter("frameRate", frameRate); + } + else { + cerr << "Warning.setParameter: {frameRate} must be a 2-dim tuple, so there is no valid update in this step." << endl; + } + } + temp_str.clear(); + Py_RETURN_NONE; +} + +static PyObject* C_MPCT_setParam(C_MpegClient* Self, PyObject *args, PyObject *kwargs) { + /* Wrapped (void)C_MPCT_setParam method, the inputs are: + * widthDst/heightDst: [int] The width / height of the decoded frames. + * cacheSize/readSize: [int] The size of the cache, and the reading size. + * dstFrameRate: [tuple] The target frame rate of the client. + * nthread: [int] The number of decoder threads. + */ + int widthDst = 0; + int heightDst = 0; + int nthread = 0; + int64_t cacheSize = 0; + int64_t readSize = 0; + PyObject *frameRate = nullptr; + static char *kwlist[] = { "widthDst", "heightDst", "cacheSize", "readSize", "dstFrameRate", "nthread", nullptr}; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiLLOi", kwlist, &widthDst, &heightDst, &cacheSize, &readSize, &frameRate, &nthread)) { + PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'params'"); + return nullptr; + } + if (widthDst>0) { + Self->_in_Handle->setParameter("widthDst", &widthDst); + } + if (heightDst>0) { + Self->_in_Handle->setParameter("heightDst", &heightDst); + } + if (cacheSize>0) { + Self->_in_Handle->setParameter("cacheSize", &cacheSize); + } + if (readSize>0) { + Self->_in_Handle->setParameter("readSize", &readSize); + } + if (frameRate) { + if (PyTuple_Check(frameRate) && PyTuple_Size(frameRate) == 2) { + Self->_in_Handle->setParameter("dstFrameRate", frameRate); + } + else { + cerr << "Warning.setParameter: {dstFrameRate} must be a 2-dim tuple, so there is no valid update in this step." << endl; + } + } + if (nthread > 0) { + Self->_in_Handle->setParameter("nthread", &nthread); + } + Py_RETURN_NONE; +} + +static PyObject* C_MPSV_setParam(C_MpegServer* Self, PyObject* args, PyObject* kwargs) { + /* Wrapped (bool)C_MPSV_setParam method, the inputs are: + * decoder [MpegDecoder / MpegClient]: The parameters to be configured. + * videoAddress: [str/bytes] The address of the current video. + * codecName: [str/bytes] The name of the codec. + * nthread: [int] The number of encoder threads. + * bitRate: [double] The target bit rate. + * width/height: [int] The width / height of the encoded frame. + * widthSrc/heightSrc: [int] The width / height of the input frame. + * GOPSize: [int] The size of one GOP. + * maxBframe: [int] The maximal number of continuous B frames. + * frameRate: [tuple] The target frame rate. + * frameAhead [int] The number of ahead frames. This value is suggested + * to be larger than the GOPSize. + */ + PyObject* decoder = nullptr; + PyObject* configDict = nullptr; + PyObject* videoAddress = nullptr; + PyObject* codecName = nullptr; + double bitRate = -1; + int nthread = 0; + int width = 0; + int height = 0; + int widthSrc = 0; + int heightSrc = 0; + int GOPSize = 0; + int MaxBframe = -1; + int frameAhead = 0; + PyObject* frameRate = nullptr; + static char* kwlist[] = { "decoder", "configDict", "videoAddress", "codecName", "nthread", "bitRate", "width", "height", "widthSrc", "heightSrc", "GOPSize", "maxBframe", "frameRate", "frameAhead", nullptr}; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOOOidiiiiiiOi", kwlist, &decoder, &configDict, &videoAddress, &codecName, &nthread, &bitRate, &width, &height, &widthSrc, &heightSrc, &GOPSize, &MaxBframe, &frameRate, &frameAhead)) { + PyErr_SetString(PyExc_TypeError, "Error.setParameter: need 'params'"); + return nullptr; + } + string temp_str; + if (decoder) { + temp_str.assign(decoder->ob_type->tp_name); + if (temp_str.compare("mpegCoder.MpegDecoder") == 0) { + auto decoderPtr = reinterpret_cast(decoder); + Self->_in_Handle->setParameter("decoder", decoderPtr->_in_Handle); + } + else if (temp_str.compare("mpegCoder.MpegClient") == 0) { + auto decoderPtr = reinterpret_cast(decoder); + Self->_in_Handle->setParameter("client", decoderPtr->_in_Handle); + } + else { + cerr << "Warning.setParameter: Not intended decoder type, no valid update in this step." << endl; + } + } + else if (configDict) { + if (PyDict_Check(configDict)) { + Self->_in_Handle->setParameter("configDict", configDict); + } + else { + cerr << "Warning.setParameter: Not intended configDict type (require to be a dict), no valid update in this step." << endl; + } + } + if (videoAddress) { + if (PyStr2str(videoAddress, temp_str)) { + Self->_in_Handle->setParameter("videoAddress", &temp_str); + } + else { + return nullptr; + } + } + if (codecName) { + if (PyStr2str(codecName, temp_str)) { + Self->_in_Handle->setParameter("codecName", &temp_str); + } + else { + return nullptr; + } + } + if (nthread > 0) { + Self->_in_Handle->setParameter("nthread", &nthread); + } + if (bitRate > 0) { + Self->_in_Handle->setParameter("bitRate", &bitRate); + } + if (width > 0) { + Self->_in_Handle->setParameter("width", &width); + } + if (height > 0) { + Self->_in_Handle->setParameter("height", &height); + } + if (widthSrc > 0) { + Self->_in_Handle->setParameter("widthSrc", &widthSrc); + } + if (heightSrc > 0) { + Self->_in_Handle->setParameter("heightSrc", &heightSrc); + } + if (GOPSize > 0) { + Self->_in_Handle->setParameter("GOPSize", &GOPSize); + } + if (MaxBframe >= 0) { + Self->_in_Handle->setParameter("maxBframe", &MaxBframe); + } + if (frameRate) { + if (PyTuple_Check(frameRate) && PyTuple_Size(frameRate) == 2) { + Self->_in_Handle->setParameter("frameRate", frameRate); + } + else { + cerr << "Warning.setParameter: {frameRate} must be a 2-dim tuple, so there is no valid update in this step." << endl; + } + } + if (frameAhead > 0) { + Self->_in_Handle->setParameter("frameAhead", &frameAhead); + } + temp_str.clear(); + Py_RETURN_NONE; +} + +static PyObject* C_MPDC_DumpFile(C_MpegDecoder* Self) { + /* Wrapped (void)dumpFormat method, the input is required to be empty. */ + Self->_in_Handle->dumpFormat(); + Py_RETURN_NONE; +} + +static PyObject* C_MPEC_DumpFile(C_MpegEncoder* Self) { + /* Wrapped (void)dumpFormat method, the input is required to be empty. */ + Self->_in_Handle->dumpFormat(); + Py_RETURN_NONE; +} + +static PyObject* C_MPCT_DumpFile(C_MpegClient* Self) { + /* Wrapped (void)dumpFormat method, the input is required to be empty. */ + Self->_in_Handle->dumpFormat(); + Py_RETURN_NONE; +} + +static PyObject* C_MPSV_DumpFile(C_MpegServer* Self) { + /* Wrapped (void)dumpFormat method, the input is required to be empty. */ + Self->_in_Handle->dumpFormat(); + Py_RETURN_NONE; +} + +static PyObject* C_MPDC_Clear(C_MpegDecoder* Self) { + /* Wrapped (void)clear method, the input is required to be empty. */ + Self->_in_Handle->clear(); + Py_RETURN_NONE; +} + +static PyObject* C_MPEC_Clear(C_MpegEncoder* Self) { + /* Wrapped (void)clear method, the input is required to be empty. */ + Self->_in_Handle->clear(); + Py_RETURN_NONE; +} + +static PyObject* C_MPCT_Clear(C_MpegClient* Self) { + /* Wrapped (void)clear method, the input is required to be empty. */ + Self->_in_Handle->clear(); + Py_RETURN_NONE; +} + +static PyObject* C_MPSV_Clear(C_MpegServer* Self) { + /* Wrapped (void)clear method, the input is required to be empty. */ + Self->_in_Handle->clear(); + Py_RETURN_NONE; +} + +static PyObject* C_MPEC_Close(C_MpegEncoder* Self) { + /* Wrapped (void)close method, the input is required to be empty. */ + Self->_in_Handle->FFmpegClose(); + Py_RETURN_NONE; +} + +static PyObject* C_MPSV_Close(C_MpegServer* Self) { + /* Wrapped (void)close method, the input is required to be empty. */ + Self->_in_Handle->FFmpegClose(); + Py_RETURN_NONE; +} + +/***************************************************************************** +* Register the methods of each class. +*****************************************************************************/ +static PyMethodDef C_MPC_MethodMembers[] = // Register the global method list. +{ + { "setGlobal", (PyCFunction)C_MPC_Global, METH_VARARGS | METH_KEYWORDS, \ + "Set global setting parameters.\n - dumpLevel: [int] the level of dumped log.\n -|- 0: silent executing.\n -|- 1: [default] dump basic informations.\n -|- 2: dump all informations." }, + { "readme", (PyCFunction)C_MPC_Help, METH_NOARGS, \ + "Use it to see readme and some useful instructions." }, + { nullptr, nullptr, 0, nullptr } +}; + +static PyMethodDef C_MPDC_MethodMembers[] = // Register the member methods of Decoder. +{ // This step add the methods to the C-API of the class. + { "FFmpegSetup", (PyCFunction)C_MPDC_Setup, METH_VARARGS | METH_KEYWORDS, \ + "Reset the decoder and the video format.\n - videoPath: [str/bytes] the path of decoded video file." }, + { "resetPath", (PyCFunction)C_MPDC_resetPath, METH_VARARGS | METH_KEYWORDS, \ + "Reset the path of decoded video.\n - videoPath: [str/bytes] the path of decoded video file." }, + { "ExtractFrame", (PyCFunction)C_MPDC_ExtractFrame, METH_VARARGS | METH_KEYWORDS, \ + "Extract a series of continius frames at the specific position.\n - framePos: [int] the start position of the decoder.\n - frameNum: [int] the expected number of extracted frames." }, + { "ExtractFrameByTime", (PyCFunction)C_MPDC_ExtractFrame_Time, METH_VARARGS | METH_KEYWORDS, \ + "Extract a series of continius frames at the specific position (time based).\n - timePos: [double] the start position (second) of the decoder.\n - frameNum: [int] the expected number of extracted frames." }, + { "ExtractGOP", (PyCFunction)C_MPDC_ExtractGOP, METH_VARARGS | METH_KEYWORDS, \ + "Extract a series of continius frames as a GOP at the specific position.\n - framePos: [int] the start position of the decoder." }, + { "ExtractGOPByTime", (PyCFunction)C_MPDC_ExtractGOP_Time, METH_VARARGS | METH_KEYWORDS, \ + "Extract a series of continius frames as a GOP at the specific position (time based).\n - timePos: [double] the start position (second) of the decoder." }, + { "ResetGOPPosition", (PyCFunction)C_MPDC_setGOPPosition, METH_VARARGS | METH_KEYWORDS, \ + "Reset the start position of GOP flow.\n - framePos: [int] the start position of the decoder.\n - timePos: [double] the start position (second) of the decoder." }, + { "clear", (PyCFunction)C_MPDC_Clear, METH_NOARGS, \ + "Clear all states (except the videoPath)." }, + { "dumpFile", (PyCFunction)C_MPDC_DumpFile, METH_NOARGS, \ + "Show current state of formatContex." }, + { "setParameter", (PyCFunction)C_MPDC_setParam, METH_VARARGS | METH_KEYWORDS, \ + "Set the optional parameters of 'Setup' & 'Extract' functions via different methods.\n - widthDst: [int] the width of destination (frame), if <=0 (default), it would take no effect.\n - heightDst: [int] the height of destination (frame), if <=0 (default), it would take no effect.\n - nthread: [int] number of decoder threads." }, + { "getParameter", (PyCFunction)C_MPDC_getParam, METH_VARARGS | METH_KEYWORDS, \ + "Input a parameter's name to get it.\n - paramName: [str/bytes] the name of needed parameter. If set empty, would return all key params.\n -|- videoPath: [str] the current path of the read video.\n -|- width/height: [int] the size of one frame.\n -|- frameCount: [int] the number of returned frames in the last ExtractFrame().\n -|- coderName: [str] the name of the decoder.\n -|- nthread: [int] number of decoder threads.\n -|- duration: [double] the total seconds of this video.\n -|- estFrameNum: [int] the estimated total frame number(may be not accurate).\n -|- avgFrameRate: [double] the average of FPS." }, + { nullptr, nullptr, 0, nullptr } +}; + +static PyMethodDef C_MPEC_MethodMembers[] = // Register the member methods of Encoder. +{ // This step add the methods to the C-API of the class. + { "FFmpegSetup", (PyCFunction)C_MPEC_Setup, METH_VARARGS | METH_KEYWORDS, \ + "Open the encoded video and reset the encoder.\n - videoPath: [str/bytes] the path of encoded(written) video file." }, + { "resetPath", (PyCFunction)C_MPEC_resetPath, METH_VARARGS | METH_KEYWORDS, \ + "Reset the output path of encoded video.\n - videoPath: [str/bytes] the path of encoded video file." }, + { "EncodeFrame", (PyCFunction)C_MPEC_EncodeFrame, METH_VARARGS | METH_KEYWORDS, \ + "Encode one frame.\n - PyArrayFrame: [ndarray] the frame that needs to be encoded." }, + { "setParameter", (PyCFunction)C_MPEC_setParam, METH_VARARGS | METH_KEYWORDS, \ + "Set the necessary parameters of 'Setup' & 'Encode' functions via different methods.\n - decoder: [MpegDecoder / MpegClient] copy metadata from a known decoder.\n - configDict: [dict] a config dict returned by getParameter().\n - videoPath: [str/bytes] the current path of the encoded video.\n - codecName: [str/bytes] the name of the encoder.\n - nthread: [int] number of encoder threads.\n - bitRate: [float] the indended bit rate (Kb/s).\n - width/height: [int] the size of one encoded (scaled) frame.\n - widthSrc/heightSrc: [int] the size of one input frame, if set <=0, these parameters would not be enabled.\n - GOPSize: [int] the number of frames in a GOP.\n - maxBframe: [int] the maximal number of B frames in a GOP.\n - frameRate: [tuple] a 2-dim tuple indicating the FPS(num, den) of the stream." }, + { "getParameter", (PyCFunction)C_MPEC_getParam, METH_VARARGS | METH_KEYWORDS, \ + "Input a parameter's name to get it.\n - paramName: [str/bytes] the name of needed parameter. If set empty, would return all key params.\n -|- videoPath: [str] the current path of the encoded video.\n -|- codecName: [str] the name of the encoder.\n -|- nthread: [int] number of encoder threads.\n -|- bitRate: [float] the indended bit rate (Kb/s).\n -|- width/height: [int] the size of one encoded (scaled) frame.\n -|- widthSrc/heightSrc: [int] the size of one input frame, if set <=0, these parameters would not be enabled.\n -|- GOPSize: [int] the number of frames in a GOP.\n -|- maxBframe: [int] the maximal number of B frames in a GOP.\n -|- frameRate: [tuple] a 2-dim tuple indicating the FPS(num, den) of the stream." }, + { "clear", (PyCFunction)C_MPEC_Clear, METH_NOARGS, \ + "Clear all states." }, + { "dumpFile", (PyCFunction)C_MPEC_DumpFile, METH_NOARGS, \ + "Show current state of formatContex." }, + { "FFmpegClose", (PyCFunction)C_MPEC_Close, METH_NOARGS, \ + "Close currently encoded video and write the end code of a MPEG file." }, + { nullptr, nullptr, 0, nullptr } +}; + +static PyMethodDef C_MPCT_MethodMembers[] = // Register the member methods of Encoder. +{ // This step add the methods to the C-API of the class. + { "FFmpegSetup", (PyCFunction)C_MPCT_Setup, METH_VARARGS | METH_KEYWORDS, \ + "Reset the decoder and the video format.\n - videoAddress: [str/bytes] the path of decoded video file." }, + { "resetPath", (PyCFunction)C_MPCT_resetPath, METH_VARARGS | METH_KEYWORDS, \ + "Reset the address of decoded video.\n - videoAddress: [str/bytes] the path of decoded video file." }, + { "start", (PyCFunction)C_MPCT_Start, METH_NOARGS, \ + "Start the demuxing thread, must be called after FFmpegSetup()." }, + { "terminate", (PyCFunction)C_MPCT_Terminate, METH_NOARGS, \ + "Terminate all current demuxing threads, usually used when there is only one thread." }, + { "ExtractFrame", (PyCFunction)C_MPCT_ExtractFrame, METH_VARARGS | METH_KEYWORDS, \ + "Extract frames from the current buffer.\n - readSize: [int] the number of extracted frames, should not be larger than cache number. \nIf not set, will be used as the default value." }, + { "clear", (PyCFunction)C_MPCT_Clear, METH_NOARGS, \ + "Clear all states (except the videoAddress)." }, + { "dumpFile", (PyCFunction)C_MPCT_DumpFile, METH_NOARGS, \ + "Show current state of formatContex." }, + { "setParameter", (PyCFunction)C_MPCT_setParam, METH_VARARGS | METH_KEYWORDS, \ + "Set the optional parameters of 'Setup' & 'Extract' functions and the demuxing thread via different methods.\n - widthDst: [int] the width of destination (frame), if <=0 (default), it would take no effect.\n - heightDst: [int] the height of destination (frame), if <=0 (default), it would take no effect.\n - cacheSize: [int] the number of allocated avaliable frames in the cache.\n - readSize: [int] the default value of ExtractFrame().\n - dstFrameRate: [tuple] a 2-dim tuple indicating the destination FPS(num, den) of the stream.\n - nthread: [int] number of decoder threads." }, + { "getParameter", (PyCFunction)C_MPCT_getParam, METH_VARARGS | METH_KEYWORDS, \ + "Input a parameter's name to get it.\n - paramName: [str/bytes] the name of needed parameter. If set empty, would return all key params.\n -|- videoAddress: [str] the current path of the read video.\n -|- width/height: [int] the size of one frame.\n -|- frameCount: [int] the number of returned frames in the last ExtractFrame().\n -|- coderName: [str] the name of the decoder.\n -|- nthread: [int] number of decoder threads.\n -|- duration: [double] the total seconds of this video.\n -|- estFrameNum: [int] the estimated total frame number(may be not accurate).\n -|- srcFrameRate: [double] the average of FPS of the source video." }, + { nullptr, nullptr, 0, nullptr } +}; + +static PyMethodDef C_MPSV_MethodMembers[] = // Register the member methods of Server. +{ // This step add the methods to the C-API of the class. + { "FFmpegSetup", (PyCFunction)C_MPSV_Setup, METH_VARARGS | METH_KEYWORDS, \ + "Open the encoded video and reset the encoder.\n - videoAddress: [str/bytes] the path of encoded(written) video file." }, + { "resetPath", (PyCFunction)C_MPSV_resetPath, METH_VARARGS | METH_KEYWORDS, \ + "Reset the output path of encoded video.\n - videoAddress: [str/bytes] the path of encoded video file." }, + { "ServeFrame", (PyCFunction)C_MPSV_ServeFrame, METH_VARARGS | METH_KEYWORDS, \ + "Encode one frame and send the frame non-blockly.\n - PyArrayFrame: [ndarray] the frame that needs to be encoded." }, + { "ServeFrameBlock", (PyCFunction)C_MPSV_ServeFrameBlock, METH_VARARGS | METH_KEYWORDS, \ + "Encode one frame and send the frame blockly. This method is suggested to be used in sub-processes.\n - PyArrayFrame: [ndarray] the frame that needs to be encoded." }, + { "setParameter", (PyCFunction)C_MPSV_setParam, METH_VARARGS | METH_KEYWORDS, \ + "Set the necessary parameters of 'Setup' & 'Serve' functions via different methods.\n - decoder: [MpegDecoder / MpegClient] copy metadata from a known decoder.\n - configDict: [dict] a config dict returned by getParameter().\n - videoAddress: [str/bytes] the current path of the encoded video.\n - codecName: [str/bytes] the name of the encoder.\n - nthread: [int] number of encoder threads.\n - bitRate: [float] the indended bit rate (Kb/s).\n - width/height: [int] the size of one encoded (scaled) frame.\n - widthSrc/heightSrc: [int] the size of one input frame, if set <=0, these parameters would not be enabled.\n - GOPSize: [int] the number of frames in a GOP.\n - maxBframe: [int] the maximal number of B frames in a GOP.\n - frameRate: [tuple] a 2-dim tuple indicating the FPS(num, den) of the stream.\n - frameAhead: [int] The number of ahead frames. This value is suggested to be larger than the GOPSize.." }, + { "getParameter", (PyCFunction)C_MPSV_getParam, METH_VARARGS | METH_KEYWORDS, \ + "Input a parameter's name to get it.\n - paramName: [str/bytes] the name of needed parameter. If set empty, would return all key params.\n -|- videoAddress: [str] the current path of the encoded video.\n -|- codecName: [str] the name of the encoder.\n -|- formatName: [str] the format name of the stream.\n -|- nthread: [int] number of encoder threads.\n -|- bitRate: [float] the indended bit rate (Kb/s).\n -|- width/height: [int] the size of one encoded (scaled) frame.\n -|- widthSrc/heightSrc: [int] the size of one input frame, if set <=0, these parameters would not be enabled.\n -|- GOPSize: [int] the number of frames in a GOP.\n -|- maxBframe: [int] the maximal number of B frames in a GOP.\n -|- frameRate: [tuple] a 2-dim tuple indicating the FPS(num, den) of the stream.\n -|- waitRef: [float] The reference used for sync. waiting.\n -|- ptsAhead: [int] The ahead time duration in the uit of time stamp." }, + { "clear", (PyCFunction)C_MPSV_Clear, METH_NOARGS, \ + "Clear all states." }, + { "dumpFile", (PyCFunction)C_MPSV_DumpFile, METH_NOARGS, \ + "Show current state of formatContex." }, + { "FFmpegClose", (PyCFunction)C_MPSV_Close, METH_NOARGS, \ + "Close currently encoded video and write the end code of a MPEG file." }, + { nullptr, nullptr, 0, nullptr } +}; + +/***************************************************************************** +* Declaration of the class, including the name, information and the members. +* This is the top-level packing of the class APIs. +*****************************************************************************/ +static PyTypeObject C_MPDC_ClassInfo = +{ + PyVarObject_HEAD_INIT(nullptr, 0)"mpegCoder.MpegDecoder", // The implementation of the __class__.__name__. + sizeof(C_MpegDecoder), // The memory length of the class. This value is required for PyObject_New. + 0, + (destructor)C_MPDC_Destruct, // Destructor. + 0, + 0, + 0, + 0, + (reprfunc)C_MPDC_Repr, // __repr__ method. + 0, + 0, + 0, + 0, + 0, + (reprfunc)C_MPDC_Str, // __str__ method. + 0, + 0, + 0, + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // If no methods are provided, this value is Py_TPFLAGS_DEFAULE. + "This class has wrapped the C-API of FFmpeg decoder so that users could call its methods\n to decode the frame data in python quickly.", // __doc__, the docstring of the class. + 0, + 0, + 0, + 0, + 0, + 0, + C_MPDC_MethodMembers, // The collection of all method members. + C_MPDC_DataMembers, // THe collection of all data members. + 0, + 0, + 0, + 0, + 0, + 0, + (initproc)C_MPDC_init, // Constructor. + 0, +}; + +static PyTypeObject C_MPEC_ClassInfo = +{ + PyVarObject_HEAD_INIT(nullptr, 0)"mpegCoder.MpegEncoder", // The implementation of the __class__.__name__. + sizeof(C_MpegEncoder), // The memory length of the class. This value is required for PyObject_New. + 0, + (destructor)C_MPEC_Destruct, // Destructor. + 0, + 0, + 0, + 0, + (reprfunc)C_MPEC_Repr, // __repr__ method. + 0, + 0, + 0, + 0, + 0, + (reprfunc)C_MPEC_Str, // __str__ method. + 0, + 0, + 0, + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // If no methods are provided, this value is Py_TPFLAGS_DEFAULE. + "This class has wrapped the C-API of FFmpeg encoder so that users could call its methods\n to encode frames by using numpy-data quickly.", // __doc__, the docstring of the class. + 0, + 0, + 0, + 0, + 0, + 0, + C_MPEC_MethodMembers, // The collection of all method members. + C_MPEC_DataMembers, // THe collection of all data members. + 0, + 0, + 0, + 0, + 0, + 0, + (initproc)C_MPEC_init, // Constructor. + 0, +}; + +static PyTypeObject C_MPCT_ClassInfo = +{ + PyVarObject_HEAD_INIT(nullptr, 0)"mpegCoder.MpegClient", // The implementation of the __class__.__name__. + sizeof(C_MpegClient), // The memory length of the class. This value is required for PyObject_New. + 0, + (destructor)C_MPCT_Destruct, // Destructor. + 0, + 0, + 0, + 0, + (reprfunc)C_MPCT_Repr, // __repr__ method. + 0, + 0, + 0, + 0, + 0, + (reprfunc)C_MPCT_Str, // __str__ method. + 0, + 0, + 0, + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // If no methods are provided, this value is Py_TPFLAGS_DEFAULE. + "This class has wrapped the C-API of FFmpeg demuxer so that users could call its methods\n to demux the network stream in python quickly.", // __doc__, the docstring of the class. + 0, + 0, + 0, + 0, + 0, + 0, + C_MPCT_MethodMembers, // The collection of all method members. + C_MPCT_DataMembers, // THe collection of all data members. + 0, + 0, + 0, + 0, + 0, + 0, + (initproc)C_MPCT_init, // Constructor. + 0, +}; + +static PyTypeObject C_MPSV_ClassInfo = +{ + PyVarObject_HEAD_INIT(nullptr, 0)"mpegCoder.MpegServer", // The implementation of the __class__.__name__. + sizeof(C_MpegServer), // The memory length of the class. This value is required for PyObject_New. + 0, + (destructor)C_MPSV_Destruct, // Destructor. + 0, + 0, + 0, + 0, + (reprfunc)C_MPSV_Repr, // __repr__ method. + 0, + 0, + 0, + 0, + 0, + (reprfunc)C_MPSV_Str, // __str__ method. + 0, + 0, + 0, + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // If no methods are provided, this value is Py_TPFLAGS_DEFAULE. + "This class has wrapped the C-API of FFmpeg stream server so that users could call its methods\n to server streamed frames by using numpy-data quickly.", // __doc__, the docstring of the class. + 0, + 0, + 0, + 0, + 0, + 0, + C_MPSV_MethodMembers, // The collection of all method members. + C_MPSV_DataMembers, // THe collection of all data members. + 0, + 0, + 0, + 0, + 0, + 0, + (initproc)C_MPSV_init, // Constructor. + 0, +}; + +/***************************************************************************** +* Decleartion of the module. +* This is the top-level packing of the module APIs. +*****************************************************************************/ +static PyModuleDef ModuleInfo = +{ + PyModuleDef_HEAD_INIT, + "mpegCoder", // The __name__ of the module. + "A FFmpeg module which could provide a class for encode/decode a video in any format.", // __doc__; The docstring of the module. + -1, + nullptr, nullptr, nullptr, nullptr, nullptr +}; + +#endif diff --git a/MpegCoder/MpegStreamer.cpp b/MpegCoder/MpegStreamer.cpp new file mode 100644 index 0000000..a78468c --- /dev/null +++ b/MpegCoder/MpegStreamer.cpp @@ -0,0 +1,2189 @@ +#define NO_IMPORT_ARRAY +#define PY_ARRAY_UNIQUE_SYMBOL MPEGARRAY_API +#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION +#include +#include "MpegCoder.h" +#include "MpegStreamer.h" + +cmpc::CMpegClient::CMpegClient(void) : + width(0), height(0), widthDst(0), heightDst(0), PPixelFormat(AVPixelFormat::AV_PIX_FMT_NONE), PVideoStreamIDX(0), + PVideoFrameCount(0), cache_size(0), read_size(0), frameRate({ 0,0 }), reading(false), + _duration(0), _predictFrameNum(0), refcount(0), PFormatCtx(nullptr), PCodecCtx(nullptr), + PVideoStream(nullptr), frame(nullptr), PswsCtx(nullptr), buffer(), read_handle(), read_check(), + info_lock(), nthread(0) { +} +cmpc::CMpegClient::~CMpegClient(void) { + clear(); +} +cmpc::CMpegClient::CMpegClient(CMpegClient &&ref) noexcept: + width(ref.width), height(ref.height), widthDst(ref.widthDst), heightDst(ref.heightDst), + PPixelFormat(ref.PPixelFormat), PVideoStreamIDX(ref.PVideoStreamIDX), PVideoFrameCount(ref.PVideoFrameCount), + cache_size(ref.cache_size), read_size(ref.read_size), frameRate(ref.frameRate), reading(ref.reading), + _duration(ref._duration), _predictFrameNum(ref._predictFrameNum), refcount(ref.refcount), + PFormatCtx(ref.PFormatCtx), PCodecCtx(ref.PCodecCtx), PVideoStream(ref.PVideoStream), frame(ref.frame), + PswsCtx(ref.PswsCtx), buffer(std::move(ref.buffer)), nthread(ref.nthread), read_check(), + info_lock(){ + read_handle = std::move(ref.read_handle); + ref.PFormatCtx = nullptr; + ref.PCodecCtx = nullptr; + ref.PVideoStream = nullptr; + ref.frame = nullptr; + ref.PswsCtx = nullptr; +} +cmpc::CMpegClient& cmpc::CMpegClient::operator=(CMpegClient &&ref) noexcept { + if (this != &ref) { + width = ref.width; + height = ref.height; + widthDst = ref.widthDst; + heightDst = ref.heightDst; + PPixelFormat = ref.PPixelFormat; + PVideoStreamIDX = ref.PVideoStreamIDX; + PVideoFrameCount = ref.PVideoFrameCount; + cache_size = ref.cache_size; + read_size = ref.read_size; + frameRate = ref.frameRate; + reading = ref.reading; + _duration = ref._duration; + _predictFrameNum = ref._predictFrameNum; + refcount = ref.refcount; + PFormatCtx = ref.PFormatCtx; + PCodecCtx = ref.PCodecCtx; + PVideoStream = ref.PVideoStream; + frame = ref.frame; + PswsCtx = ref.PswsCtx; + buffer = std::move(ref.buffer); + read_handle = std::move(ref.read_handle); + nthread = ref.nthread; + ref.PFormatCtx = nullptr; + ref.PCodecCtx = nullptr; + ref.PVideoStream = nullptr; + ref.frame = nullptr; + ref.PswsCtx = nullptr; + } +} + +void cmpc::CMpegClient::meta_protected_clear(void) { + auto protectWidth = widthDst; + auto protectHeight = heightDst; + auto protectCacheSize = cache_size; + auto protectReadSize = read_size; + auto protectFrameRate = frameRate; + auto protectNthread = nthread; + clear(); + widthDst = protectWidth; + heightDst = protectHeight; + cache_size = protectCacheSize; + read_size = protectReadSize; + frameRate = protectFrameRate; + nthread = protectNthread; +} + +void cmpc::CMpegClient::clear(void) { + if (read_handle.joinable()) { + read_check.lock(); + reading = false; + read_check.unlock(); + read_handle.join(); + //std::terminate(); + read_handle = std::move(std::thread()); + } + else { + read_handle = std::move(std::thread()); + } + width = height = 0; + widthDst = heightDst = 0; + PPixelFormat = AVPixelFormat::AV_PIX_FMT_NONE; + PVideoStreamIDX = -1; + PVideoFrameCount = 0; + _duration = 0; + _predictFrameNum = 0; + _str_codec.clear(); + //videoPath.clear(); + buffer.clear(); + cache_size = 0; + read_size = 0; + frameRate = _setAVRational(0, 0); + read_check.lock(); + read_check.unlock(); + info_lock.lock(); + info_lock.unlock(); + nthread = 0; + PVideoStream = nullptr; + if (frame) { + av_frame_free(&frame); + frame = nullptr; + } + if (PswsCtx) { + sws_freeContext(PswsCtx); + PswsCtx = nullptr; + } + if (PCodecCtx) { + avcodec_free_context(&PCodecCtx); + PCodecCtx = nullptr; + } + if (PFormatCtx) { + avformat_close_input(&PFormatCtx); + PFormatCtx = nullptr; + } + refcount = 1; +} + +int cmpc::CMpegClient::_open_codec_context(int &stream_idx, AVCodecContext *&dec_ctx, \ + AVFormatContext *PFormatCtx, enum cmpc::AVMediaType type) { // Search the correct decoder, and make the configurations. + int ret; + + //search video stream + ret = av_find_best_stream(PFormatCtx, type, -1, -1, nullptr, 0); + if (ret < 0) { + cerr << "Could not find " << av_get_media_type_string(type) << \ + " stream in input address: '" << videoPath << "'" << endl; + return ret; + } + else { + auto stream_index = ret; + auto st = PFormatCtx->streams[stream_index]; // The AVStream object. + + /* find decoder for the stream */ + auto dec = avcodec_find_decoder(st->codecpar->codec_id); // Decoder (AVCodec). + if (!dec) { + cerr << "Failed to find " << av_get_media_type_string(type) << " codec" << endl; + return AVERROR(EINVAL); + } + _str_codec.assign(dec->name); + + /* Allocate a codec context for the decoder / Add this to allocate the context by codec */ + auto dec_ctx_ = avcodec_alloc_context3(dec); // Decoder context (AVCodecContext). + if (!dec_ctx_) { + cerr << "Failed to allocate the " << av_get_media_type_string(type) << " codec context" << endl; + return AVERROR(ENOMEM); + } + + if (nthread > 0) { + dec_ctx_->thread_count = nthread; + } + + /* Copy codec parameters from input stream to output codec context */ + if ((ret = avcodec_parameters_to_context(dec_ctx_, st->codecpar)) < 0) { + cerr << "Failed to copy " << av_get_media_type_string(type) << \ + " codec parameters to decoder context" << endl; + return ret; + } + + /* Init the decoders, with or without reference counting */ + AVDictionary* opts = nullptr; // The uninitialized argument dictionary. + av_dict_set(&opts, "refcounted_frames", refcount ? "1" : "0", 0); + if ((ret = avcodec_open2(dec_ctx_, dec, &opts)) < 0) { + cerr << "Failed to open " << av_get_media_type_string(type) << " codec" << endl; + return ret; + } + dec_ctx = dec_ctx_; + stream_idx = stream_index; + } + return 0; +} + +bool cmpc::CMpegClient::__setup_check() const { + if (cache_size > 0 && read_size > 0 && frameRate.den > 0 && frameRate.num > 0 && (!read_handle.joinable())) { + return true; + } + else { + return false; + } +} + +bool cmpc::CMpegClient::FFmpegSetup(string inVideoPath) { + videoPath.assign(inVideoPath); + return FFmpegSetup(); +} + +bool cmpc::CMpegClient::FFmpegSetup() { + if (!__setup_check()) { + cerr << "Have not get necessary and correct configurations, so FFmpegSetup() should not be called." << endl; + return false; + } + meta_protected_clear(); + + /* open Stream: register all formats and codecs */ + if (avformat_open_input(&PFormatCtx, videoPath.c_str(), nullptr, nullptr) < 0) { + cerr << "Could not open source address " << videoPath << endl; + clear(); + return false; + } // For example, "rtsp://localhost:8554/h264.3gp" + + /* retrieve stream information */ + if (avformat_find_stream_info(PFormatCtx, nullptr) < 0) { + cerr << "Could not find stream information" << endl; + clear(); + return false; + } + AVRational time_base, frame_base; + if (_open_codec_context(PVideoStreamIDX, PCodecCtx, PFormatCtx, AVMEDIA_TYPE_VIDEO) >= 0) { + PVideoStream = PFormatCtx->streams[PVideoStreamIDX]; + time_base = PVideoStream->time_base; + frame_base = PVideoStream->avg_frame_rate; + + /* allocate image where the decoded image will be put */ + width = PCodecCtx->width; + height = PCodecCtx->height; + if (widthDst <= 0) { + widthDst = width; + } + if (heightDst <= 0) { + heightDst = height; + } + PPixelFormat = PCodecCtx->pix_fmt; + _duration = static_cast(PVideoStream->duration) / static_cast(time_base.den) * static_cast(time_base.num); + _predictFrameNum = av_rescale(static_cast(_duration * 0xFFFF), frame_base.num, frame_base.den) / 0xFFFF; + } + else { + cerr << "Could not get codec context from the stream, aborting" << endl; + clear(); + return false; + } + + /* dump input information to stderr */ + if (__dumpControl > 1) { + av_dump_format(PFormatCtx, 0, videoPath.c_str(), 0); + } + + if (!PVideoStream) { // Check whether the video stream is correctly opened. + cerr << "Could not find audio or video stream in the network, aborting" << endl; + clear(); + return false; + } + + if (width == 0 || height == 0) { + cerr << "Could not get enough meta-data in the network, aborting" << endl; + clear(); + return false; + } + + PswsCtx = sws_getContext(width, height, PCodecCtx->pix_fmt, widthDst, heightDst, AV_PIX_FMT_RGB24, + SCALE_FLAGS, nullptr, nullptr, nullptr); + + buffer.set(cache_size, width, height, widthDst, heightDst); + buffer.set_timer(frameRate, time_base); + if (!buffer.reset_memory()) { // Check whether the buffer is allocated correctly. + cerr << "Could not allocate the memory of frame buffer list." << endl; + clear(); + return false; + } + + read_check.lock(); + reading = true; + read_check.unlock(); + return true; +} + +void cmpc::CMpegClient::dumpFormat() { + if ((!videoPath.empty()) && PFormatCtx) { + av_dump_format(PFormatCtx, 0, videoPath.c_str(), 0); + } + else { + cerr << "Still need to FFmpegSetup()" << endl; + } +} + +void cmpc::CMpegClient::resetPath(string inVideoPath) { + videoPath.assign(inVideoPath); +} + +cmpc::AVRational cmpc::CMpegClient::_setAVRational(int num, int den) { + AVRational res; + res.num = num; res.den = den; + return res; +} + +int cmpc::CMpegClient::__save_frame(AVFrame *&frame, AVPacket *&pkt, bool &got_frame, int cached) { + int ret = 0; + int decoded = pkt->size; + + got_frame = false; + + if (pkt->stream_index == PVideoStreamIDX) { + /* decode video frame */ + ret = __avcodec_decode_video2(PCodecCtx, frame, got_frame, pkt); + if (ret < 0) { + cout << "Error decoding video frame (" << av_err2str(ret) << ")" << endl; + return ret; + } + + if (got_frame) { + + if (frame->width != width || frame->height != height || + frame->format != PPixelFormat) { + /* To handle this change, one could call av_image_alloc again and + * decode the following frames into another rawvideo file. */ + cout << "Error: Width, height and pixel format have to be " + "constant in a rawvideo file, but the width, height or " + "pixel format of the input video changed:\n" + "old: width = " << width << ", height = " << height << ", format = " + << av_get_pix_fmt_name(PPixelFormat) << endl << + "new: width = " << frame->width << ", height = " << frame->height << ", format = " + << av_get_pix_fmt_name(static_cast(frame->format)) << endl; + return -1; + } + + info_lock.lock(); + PVideoFrameCount++; + info_lock.unlock(); + if (__dumpControl > 0) { + std::ostringstream str_data; + str_data << "video_frame" << (cached ? "(cached)" : "") << " n:" << PVideoFrameCount << + " coded_n:" << frame->coded_picture_number << endl; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_INFO, "%s", str_data_s.c_str()); + } + + /* copy decoded frame to destination buffer: + * this is required since rawvideo expects non aligned data */ + + buffer.write(PswsCtx, frame); + } + } + + /* If we use frame reference counting, we own the data and need + * to de-reference it when we don't use it anymore */ + + if (got_frame && refcount) + av_frame_unref(frame); + + return decoded; +} + +void cmpc::CMpegClient::__client_holder() { + int ret; + bool got_frame; + if (frame) { + cerr << "Current frame is occupied, could not start a new client." << endl; + return; + } + frame = av_frame_alloc(); + auto pkt = av_packet_alloc(); + if (!frame) { + cerr << "Could not allocate frame" << endl; + ret = AVERROR(ENOMEM); + return; + } + /* initialize packet, set data to NULL, let the demuxer fill it */ + if (PVideoStream && (__dumpControl > 0)) { + std::ostringstream str_data; + str_data << "Demuxing video from address '" << videoPath << "' into Python-List" << endl; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_INFO, "%s", str_data_s.c_str()); + } + + /* Reset the contex to remove the flushed state. */ + avcodec_flush_buffers(PCodecCtx); + + /* read frames from the file */ + info_lock.lock(); + PVideoFrameCount = 0; + info_lock.unlock(); + + //start reading packets from stream and write them to file + av_read_play(PFormatCtx); //play RTSP + + auto temp_pkt = av_packet_alloc(); + while (av_read_frame(PFormatCtx, pkt) >= 0) { + //cout << "[Test - " << pkt.size << " ]" << endl; + av_packet_ref(temp_pkt, pkt); + do { + ret = __save_frame(frame, temp_pkt, got_frame, 0); + if (ret < 0) + break; + temp_pkt->data += ret; + temp_pkt->size -= ret; + } while (temp_pkt->size > 0); + /* flush cached frames */ + av_packet_unref(pkt); + av_packet_unref(temp_pkt); + read_check.lock(); + if (!reading) { + read_check.unlock(); + break; + } + else { + read_check.unlock(); + } + } + av_packet_free(&temp_pkt); + + do { + __save_frame(frame, pkt, got_frame, 1); + } while (got_frame); + + //cout << "Demuxing succeeded." << endl; + + if (PVideoStream && (__dumpControl > 0)) { + std::ostringstream str_data; + str_data << "End of stream client." << endl; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_INFO, "%s", str_data_s.c_str()); + } + + if (frame) { + av_frame_free(&frame); + frame = nullptr; + } + if (pkt) { + av_packet_free(&pkt); + } + + read_check.lock(); + reading = false; + read_check.unlock(); +} + +int cmpc::CMpegClient::__avcodec_decode_video2(AVCodecContext *avctx, AVFrame *frame, bool &got_frame, AVPacket *pkt) { + int ret; + + got_frame = false; + + if (pkt) { + ret = avcodec_send_packet(avctx, pkt); + // In particular, we don't expect AVERROR(EAGAIN), because we read all + // decoded frames with avcodec_receive_frame() until done. + if (ret < 0) { + //cout << ret << ", " << AVERROR(EAGAIN) << ", " << AVERROR_EOF << endl; + return ret == AVERROR_EOF ? 0 : ret; + } + } + + ret = avcodec_receive_frame(avctx, frame); + if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) + return ret; + if (ret >= 0) + got_frame = true; + + //cout << ret << ", " << AVERROR(EAGAIN) << ", " << AVERROR_EOF << endl; + + return 0; +} + +PyObject* cmpc::CMpegClient::ExtractFrame() { + return ExtractFrame(read_size); +} + +PyObject* cmpc::CMpegClient::ExtractFrame(int64_t readsize) { + if (readsize==0 || readsize > cache_size) { + cerr << "Read size of frames is out of range." << endl; + return nullptr; + } + else if (frame == nullptr) { + cerr << "Current frame object is empty, maybe the client has not been started." << endl; + return nullptr; + } + buffer.freeze_write(readsize); + auto res = buffer.read(); + if (res == nullptr) { + cerr << "Unable to get frames from current buffer." << endl; + } + return res; +} + +void cmpc::CMpegClient::setParameter(string keyword, void *ptr) { + if (keyword.compare("widthDst") == 0) { + auto ref = reinterpret_cast(ptr); + widthDst = *ref; + } + else if (keyword.compare("heightDst") == 0) { + auto ref = reinterpret_cast(ptr); + heightDst = *ref; + } + else if (keyword.compare("cacheSize") == 0) { + auto ref = reinterpret_cast(ptr); + cache_size = *ref; + } + else if (keyword.compare("readSize") == 0) { + auto ref = reinterpret_cast(ptr); + read_size = *ref; + } + else if (keyword.compare("dstFrameRate") == 0) { + PyObject *ref = reinterpret_cast(ptr); + auto refObj = PyTuple_GetItem(ref, 0); + int num = static_cast(PyLong_AsLong(refObj)); + refObj = PyTuple_GetItem(ref, 1); + int den = static_cast(PyLong_AsLong(refObj)); + frameRate = _setAVRational(num, den); + } + else if (keyword.compare("nthread") == 0) { + auto ref = reinterpret_cast(ptr); + if (PCodecCtx) { + PCodecCtx->thread_count = *ref; + } + nthread = *ref; + } +} + +PyObject * cmpc::CMpegClient::getParameter(string keyword) { + if (keyword.compare("videoAddress") == 0) { + return PyUnicode_DecodeFSDefaultAndSize(videoPath.c_str(), static_cast(videoPath.size())); + } + else if (keyword.compare("width") == 0) { + return Py_BuildValue("i", width); + } + else if (keyword.compare("height") == 0) { + return Py_BuildValue("i", height); + } + else if (keyword.compare("frameCount") == 0) { + info_lock.lock(); + auto value = Py_BuildValue("i", PVideoFrameCount); + info_lock.unlock(); + return value; + } + else if (keyword.compare("coderName") == 0) { + return PyUnicode_DecodeFSDefaultAndSize(_str_codec.c_str(), static_cast(_str_codec.size())); + } + else if (keyword.compare("duration") == 0) { + return Py_BuildValue("d", _duration); + } + else if (keyword.compare("estFrameNum") == 0) { + return Py_BuildValue("L", _predictFrameNum); + } + else if (keyword.compare("srcFrameRate") == 0) { + if (!PVideoStream) { + return Py_BuildValue("d", 0.0); + } + auto frame_base = PVideoStream->avg_frame_rate; + double srcFrameRate = static_cast(frame_base.num) / static_cast(frame_base.den); + return Py_BuildValue("d", srcFrameRate); + } + else if (keyword.compare("nthread") == 0) { + if (PCodecCtx) { + return Py_BuildValue("i", PCodecCtx->thread_count); + } + else { + return Py_BuildValue("i", nthread); + } + } + else { + Py_RETURN_NONE; + } +} + +PyObject* cmpc::CMpegClient::getParameter() { + auto res = PyDict_New(); + string key; + PyObject* val = nullptr; + // Fill the values. + key.assign("videoAddress"); + val = Py_BuildValue("y", videoPath.c_str()); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("codecName"); + val = Py_BuildValue("y", _str_codec.c_str()); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + if (PCodecCtx) { + key.assign("bitRate"); + val = Py_BuildValue("L", PCodecCtx->bit_rate); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("GOPSize"); + val = Py_BuildValue("i", PCodecCtx->gop_size); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("maxBframe"); + val = Py_BuildValue("i", PCodecCtx->max_b_frames); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("nthread"); + val = Py_BuildValue("i", PCodecCtx->thread_count); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + } + else { + key.assign("nthread"); + val = Py_BuildValue("i", nthread); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + } + if (widthDst > 0) { + key.assign("widthDst"); + val = Py_BuildValue("i", widthDst); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + } + if (heightDst > 0) { + key.assign("heightDst"); + val = Py_BuildValue("i", heightDst); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + } + key.assign("width"); + val = Py_BuildValue("i", width); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("height"); + val = Py_BuildValue("i", height); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + if (PVideoStream) { + key.assign("frameRate"); + auto& frame_rate = PVideoStream->avg_frame_rate; + val = Py_BuildValue("(ii)", frame_rate.num, frame_rate.den); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + } + return res; +} + +bool cmpc::CMpegClient::start() { + if (reading && (frame == nullptr)) { + read_handle = std::move(std::thread(std::mem_fn(&CMpegClient::__client_holder), std::ref(*this))); + return true; + } + return false; +} +void cmpc::CMpegClient::terminate() { + read_check.lock(); + auto protectReading = reading; + read_check.unlock(); + if (read_handle.joinable()) { + read_check.lock(); + reading = false; + read_check.unlock(); + read_handle.join(); + //std::terminate(); + read_handle = std::move(std::thread()); + } + else { + read_handle = std::move(std::thread()); + } + info_lock.lock(); + info_lock.unlock(); + read_check.lock(); + reading = protectReading; + read_check.unlock(); + if (frame) { + av_frame_free(&frame); + } +} +ostream & cmpc::operator<<(ostream & out, cmpc::CMpegClient & self_class) { + double dstFrameRate; + out << std::setw(1) << "/"; + out << std::setfill('*') << std::setw(44) << "" << std::setfill(' ') << endl; + out << std::setw(1) << " * Packed FFmpeg Client - Y. Jin V" << MPEGCODER_CURRENT_VERSION << endl; + out << " " << std::setfill('*') << std::setw(44) << "" << std::setfill(' ') << endl; + out << std::setiosflags(std::ios::left) << std::setw(25) << " * VideoAddress: " \ + << self_class.videoPath << endl; + out << std::setiosflags(std::ios::left) << std::setw(25) << " * (Width, Height): " \ + << self_class.width << ", " << self_class.height << endl; + if (self_class.widthDst > 0 && self_class.heightDst > 0) { + out << std::setiosflags(std::ios::left) << std::setw(25) << " * (WidthDst, HeightDst): " \ + << self_class.widthDst << ", " << self_class.heightDst << endl; + } + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Deccoder: " \ + << self_class._str_codec << endl; + if (self_class.PCodecCtx) { + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Thread number: " \ + << self_class.PCodecCtx->thread_count << endl; + } + else { + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Thread number (P): " \ + << self_class.nthread << endl; + } + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Duration: " \ + << self_class._duration << " [s]" << endl; + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Predicted FrameNum: " \ + << self_class._predictFrameNum << endl; + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Read/Cache size: " \ + << self_class.read_size << "/" << self_class.cache_size << endl; + if (self_class.PVideoStream) { + auto frame_base = self_class.PVideoStream->avg_frame_rate; + double srcFrameRate = static_cast(frame_base.num) / static_cast(frame_base.den); + if (self_class.frameRate.den) { + dstFrameRate = static_cast(self_class.frameRate.num) / static_cast(self_class.frameRate.den); + } + else { + dstFrameRate = 0; + } + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Dst./Src. frame rate: " \ + << std::setprecision(3) << dstFrameRate << "/" << srcFrameRate << std::setprecision(6) << endl; + } + else { + if (self_class.frameRate.den) { + dstFrameRate = static_cast(self_class.frameRate.num) / static_cast(self_class.frameRate.den); + } + else { + dstFrameRate = 0; + } + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Src. frame rate: " \ + << std::setprecision(3) << dstFrameRate << std::setprecision(6) << endl; + } + out << std::setw(1) << " */"; + return out; +} + +cmpc::BufferList::BufferList(void): + _Buffer_pos(0), _Buffer_rpos(-1), _Buffer_size(0), dst_width(0), dst_height(0), _Buffer_capacity(0), + _Buffer_List(nullptr), __Read_size(0), next_pts(0), interval_pts(0), src_width(0), src_height(0), + frameRGB(nullptr){ +} +cmpc::BufferList::~BufferList(void) { + if (_Buffer_List) { + for (auto i = 0; i < _Buffer_size; i++) { + if (_Buffer_List[i]) { + av_free(_Buffer_List[i]); + _Buffer_List[i] = nullptr; + } + } + delete[]_Buffer_List; + _Buffer_List = nullptr; + } + if (frameRGB) { + av_frame_free(&frameRGB); + } +} +cmpc::BufferList::BufferList(const BufferList &ref): + _Buffer_pos(ref._Buffer_pos), _Buffer_rpos(ref._Buffer_rpos), _Buffer_size(ref._Buffer_size), + dst_width(ref.dst_width), dst_height(ref.dst_height), _Buffer_capacity(ref._Buffer_capacity), + __Read_size(ref.__Read_size), next_pts(ref.next_pts), interval_pts(ref.interval_pts), + src_width(ref.src_width), src_height(ref.src_height), frameRGB(ref.frameRGB), _Buffer_List(nullptr){ + if (!(frameRGB = av_frame_alloc())) { + cerr << "Could Allocate Temp Frame (RGB)" << endl; + return; + } + _Buffer_List = new uint8_t*[_Buffer_size]; + memset(_Buffer_List, 0, _Buffer_size * sizeof(uint8_t*)); + if (_Buffer_capacity > 0) { + for (auto i = 0; i < _Buffer_size; i++) { + if (ref._Buffer_List[i] != nullptr) { + _Buffer_List[i] = (uint8_t *)av_malloc(_Buffer_capacity * sizeof(uint8_t)); + memcpy(_Buffer_List[i], ref._Buffer_List[i], _Buffer_capacity * sizeof(uint8_t)); + } + } + } +} +cmpc::BufferList& cmpc::BufferList::operator=(const BufferList &ref) { + if (this != &ref) { + _Buffer_pos = ref._Buffer_pos; + _Buffer_rpos = ref._Buffer_rpos; + _Buffer_size = ref._Buffer_size; + __Read_size = ref.__Read_size; + next_pts = ref.next_pts; + interval_pts = ref.interval_pts; + dst_width = ref.dst_width; + dst_height = ref.dst_height; + src_width = ref.src_width; + src_height = ref.src_height; + _Buffer_capacity = ref._Buffer_capacity; + if (!(frameRGB = av_frame_alloc())) { + cerr << "Could Allocate Temp Frame (RGB)" << endl; + return *this; + } + _Buffer_List = new uint8_t*[_Buffer_size]; + memset(_Buffer_List, 0, _Buffer_size * sizeof(uint8_t*)); + if (_Buffer_capacity > 0) { + for (auto i = 0; i < _Buffer_size; i++) { + if (ref._Buffer_List[i] != nullptr) { + _Buffer_List[i] = (uint8_t *)av_malloc(_Buffer_capacity * sizeof(uint8_t)); + memcpy(_Buffer_List[i], ref._Buffer_List[i], _Buffer_capacity * sizeof(uint8_t)); + } + } + } + } + return *this; +} +cmpc::BufferList::BufferList(BufferList &&ref) noexcept: + _Buffer_pos(ref._Buffer_pos), _Buffer_rpos(ref._Buffer_rpos), _Buffer_size(ref._Buffer_size), + dst_width(ref.dst_width), dst_height(ref.dst_height), _Buffer_capacity(ref._Buffer_capacity), + _Buffer_List(ref._Buffer_List), __Read_size(ref.__Read_size), next_pts(ref.next_pts), + interval_pts(ref.interval_pts), src_width(ref.src_width), src_height(ref.src_height), + frameRGB(ref.frameRGB) { + ref._Buffer_List = nullptr; + ref.frameRGB = nullptr; +} +cmpc::BufferList& cmpc::BufferList::operator=(BufferList &&ref) noexcept { + if (this != &ref) { + _Buffer_pos = ref._Buffer_pos; + _Buffer_rpos = ref._Buffer_rpos; + _Buffer_size = ref._Buffer_size; + __Read_size = ref.__Read_size; + interval_pts = ref.interval_pts; + next_pts = ref.next_pts; + dst_width = ref.dst_width; + dst_height = ref.dst_height; + src_width = ref.src_width; + src_height = ref.src_height; + _Buffer_capacity = ref._Buffer_capacity; + _Buffer_List = ref._Buffer_List; + frameRGB = ref.frameRGB; + ref._Buffer_List = nullptr; + ref.frameRGB = nullptr; + } + return *this; +} +void cmpc::BufferList::clear(void) { + if (_Buffer_List) { + for (auto i = 0; i < _Buffer_size; i++) { + if (_Buffer_List[i]) { + av_free(_Buffer_List[i]); + _Buffer_List[i] = nullptr; + } + } + delete[]_Buffer_List; + _Buffer_List = nullptr; + } + _Buffer_pos = 0; + _Buffer_rpos = -1; + _Buffer_size = 0; + __Read_size = 0; + next_pts = 0; + interval_pts = 0; + src_width = 0; + src_height = 0; + dst_width = 0; + dst_height = 0; + if (frameRGB) { + av_frame_free(&frameRGB); + } +} +const int64_t cmpc::BufferList::size() const { + return _Buffer_size; +} +void cmpc::BufferList::set(int64_t set_size, int width, int height, int widthDst, int heightDst) { + _Buffer_size = set_size; + if (widthDst != 0) { + dst_width = widthDst; + } + else { + dst_width = width; + } + if (heightDst != 0) { + dst_height = heightDst; + } + else { + dst_height = height; + } + src_width = width; + src_height = height; + _Buffer_capacity = av_image_get_buffer_size(AV_PIX_FMT_RGB24, dst_width, dst_height, 1); +} +void cmpc::BufferList::set_timer(AVRational targetFrameRate, AVRational timeBase) { + auto interval_pts = av_rescale(av_rescale(1, timeBase.den, timeBase.num), targetFrameRate.den, targetFrameRate.num); +} +bool cmpc::BufferList::reset_memory() { + if (!frameRGB) { + if (!(frameRGB = av_frame_alloc())) { + cerr << "Could Allocate Temp Frame (RGB)" << endl; + return false; + } + } + if (!_Buffer_List) { + _Buffer_List = new uint8_t*[_Buffer_size]; + memset(_Buffer_List, 0, _Buffer_size * sizeof(uint8_t*)); + } + for (auto i = 0; i < _Buffer_size; i++) { + if (!_Buffer_List[i]) { + _Buffer_List[i] = (uint8_t *)av_malloc(_Buffer_capacity * sizeof(uint8_t)); + } + memset(_Buffer_List[i], 0, _Buffer_capacity * sizeof(uint8_t)); + } + return true; +} +void cmpc::BufferList::freeze_write(int64_t read_size) { + auto read_pos = _Buffer_pos - read_size; + if (read_pos < 0) { + read_pos += _Buffer_size; + } + _Buffer_rpos = read_pos; + __Read_size = read_size; +} +bool cmpc::BufferList::write(SwsContext *PswsCtx, AVFrame *frame) { + if (frame->pts < next_pts) { + if (frame->pts > (next_pts - 2 * interval_pts)) { + return false; + } + else { + next_pts = frame->pts + interval_pts; + } + } + else { + if (next_pts > 0) + next_pts += interval_pts; + else + next_pts = frame->pts; + } + if (_Buffer_pos == _Buffer_rpos) { + return false; + } + av_image_fill_arrays(frameRGB->data, frameRGB->linesize, _Buffer_List[_Buffer_pos], AV_PIX_FMT_RGB24, dst_width, dst_height, 1); + sws_scale(PswsCtx, frame->data, frame->linesize, 0, src_height, frameRGB->data, frameRGB->linesize); + _Buffer_pos++; + if (_Buffer_pos >= _Buffer_size) + _Buffer_pos -= _Buffer_size; + return true; +} +PyObject * cmpc::BufferList::read() { + if (_Buffer_rpos < 0) { + return nullptr; + } + auto _Buffer_rend = (_Buffer_rpos + __Read_size) % _Buffer_size; + npy_intp dims[] = { __Read_size, dst_height, dst_width, 3 }; + auto newdata = new uint8_t[__Read_size * _Buffer_capacity]; + auto p = newdata; + for (auto i = _Buffer_rpos; i != _Buffer_rend; i = (i + 1) % _Buffer_size) { + memcpy(p, _Buffer_List[i], _Buffer_capacity * sizeof(uint8_t)); + p += _Buffer_capacity; + } + PyObject *PyFrame = PyArray_SimpleNewFromData(4, dims, NPY_UINT8, reinterpret_cast(newdata)); + PyArray_ENABLEFLAGS((PyArrayObject*)PyFrame, NPY_ARRAY_OWNDATA); + _Buffer_rpos = -1; + __Read_size = 0; + return PyArray_Return((PyArrayObject*)PyFrame); + //Py_RETURN_NONE; +} + +/** + * Related with the encoder. + */ + + // Constructors following 3-5 law. +cmpc::CMpegServer::CMpegServer(void) : + bitRate(1024), width(100), height(100), timeBase(_setAVRational(1, 25)), frameRate(_setAVRational(25, 1)), \ + GOPSize(10), MaxBFrame(1), PStreamContex({ 0 }), PFormatCtx(nullptr), PswsCtx(nullptr), RGBbuffer(nullptr), \ + Ppacket(nullptr), __have_video(false), __enable_header(false), widthSrc(0), heightSrc(0), __frameRGB(nullptr), \ + __start_time(0), __cur_time(0), time_base_q(_setAVRational(1, AV_TIME_BASE)), nthread(nthread) { + videoPath.clear(); + __formatName.clear(); + codecName.clear(); + __pts_ahead = av_rescale(av_rescale(20, timeBase.den, timeBase.num), frameRate.den, frameRate.num); +} + +void cmpc::CMpegServer::meta_protected_clear(void) { + auto protectWidth = width; + auto protectHeight = height; + auto protectWidthSrc = widthSrc; + auto protectHeightSrc = heightSrc; + auto protectBitRate = bitRate; + auto protectGOPSize = GOPSize; + auto protectMaxBFrame = MaxBFrame; + auto protectPTSAhead = __pts_ahead; + auto protectVideoPath(videoPath); + auto protectFormatName(__formatName); + auto protectCodecName(codecName); + auto protectTimeBase(timeBase); + auto protectFrameRate(frameRate); + auto protectNthread = nthread; + clear(); + width = protectWidth; + height = protectHeight; + widthSrc = protectWidthSrc; + heightSrc = protectHeightSrc; + bitRate = protectBitRate; + GOPSize = protectGOPSize; + timeBase = protectTimeBase; + frameRate = protectFrameRate; + __pts_ahead = protectPTSAhead; + videoPath.assign(protectVideoPath); + __formatName.assign(protectFormatName); + codecName.assign(protectCodecName); + nthread = protectNthread; +} + +void cmpc::CMpegServer::clear(void) { + FFmpegClose(); + videoPath.clear(); + __formatName.clear(); + codecName.clear(); + bitRate = 1024; + width = 100; + height = 100; + heightSrc = 0; + widthSrc = 0; + timeBase = _setAVRational(1, 25); + frameRate = _setAVRational(25, 1); + GOPSize = 10; + MaxBFrame = 1; + nthread = 0; + PStreamContex = { 0 }; + __have_video = false; + __enable_header = false; + __pts_ahead = av_rescale(av_rescale(20, timeBase.den, timeBase.num), frameRate.den, frameRate.num); + __start_time = 0; + __cur_time = 0; +} + +void cmpc::CMpegServer::__copyMetaData(const CMpegServer& ref) { + videoPath.assign(ref.videoPath); + __formatName.assign(ref.__formatName); + codecName.assign(ref.codecName); + bitRate = ref.bitRate; + width = ref.width; + height = ref.height; + widthSrc = ref.widthSrc; + heightSrc = ref.heightSrc; + timeBase = ref.timeBase; + frameRate = ref.frameRate; + GOPSize = ref.GOPSize; + MaxBFrame = ref.MaxBFrame; + __pts_ahead = ref.__pts_ahead; + nthread = ref.nthread; + __start_time = 0; + __cur_time = 0; + time_base_q = _setAVRational(1, AV_TIME_BASE); +} + +cmpc::CMpegServer::~CMpegServer(void) { + clear(); +} + + +cmpc::CMpegServer::CMpegServer(const CMpegServer& ref) { + __copyMetaData(ref); + if (!FFmpegSetup()) { + clear(); + } +} + +cmpc::CMpegServer& cmpc::CMpegServer::operator=(const CMpegServer& ref) { + __copyMetaData(ref); + if (!FFmpegSetup()) { + clear(); + } + return *this; +} + +cmpc::CMpegServer::CMpegServer(CMpegServer&& ref) noexcept : + bitRate(ref.bitRate), width(ref.width), height(ref.height), timeBase(ref.timeBase), frameRate(ref.frameRate), \ + GOPSize(ref.GOPSize), MaxBFrame(ref.MaxBFrame), PStreamContex(ref.PStreamContex), PswsCtx(ref.PswsCtx), \ + RGBbuffer(ref.RGBbuffer), Ppacket(ref.Ppacket), PFormatCtx(ref.PFormatCtx), __have_video(ref.__have_video), \ + __enable_header(ref.__enable_header), widthSrc(ref.widthSrc), heightSrc(ref.heightSrc), __frameRGB(ref.__frameRGB), \ + __pts_ahead(ref.__pts_ahead), __start_time(ref.__start_time), nthread(ref.nthread), __cur_time(ref.__cur_time), \ + time_base_q(ref.time_base_q) { + videoPath.assign(std::move(ref.videoPath)); + codecName.assign(std::move(ref.codecName)); + __formatName.assign(std::move(ref.__formatName)); +} + +cmpc::CMpegServer& cmpc::CMpegServer::operator=(CMpegServer&& ref) noexcept { + videoPath.assign(std::move(ref.videoPath)); + __formatName.assign(std::move(ref.__formatName)); + codecName.assign(std::move(ref.codecName)); + bitRate = ref.bitRate; + width = ref.width; + height = ref.height; + widthSrc = ref.widthSrc; + heightSrc = ref.heightSrc; + timeBase = ref.timeBase; + frameRate = ref.frameRate; + time_base_q = ref.time_base_q; + GOPSize = ref.GOPSize; + MaxBFrame = ref.MaxBFrame; + __pts_ahead = ref.__pts_ahead; + __start_time = ref.__start_time; + __cur_time = ref.__cur_time; + PFormatCtx = ref.PFormatCtx; + PStreamContex = ref.PStreamContex; + PswsCtx = ref.PswsCtx; + RGBbuffer = ref.RGBbuffer; + Ppacket = ref.Ppacket; + nthread = ref.nthread; + __frameRGB = ref.__frameRGB; + __have_video = ref.__have_video; + __enable_header = ref.__enable_header; + ref.PFormatCtx = nullptr; + ref.PStreamContex = { 0 }; + ref.PswsCtx = nullptr; + ref.RGBbuffer = nullptr; + ref.Ppacket = nullptr; + ref.__frameRGB = nullptr; + return *this; +} + +void cmpc::CMpegServer::resetPath(string inVideoPath) { + videoPath.assign(inVideoPath); + if (videoPath.compare(0, 7, "rtsp://") == 0) { + __formatName.assign("rtsp"); + } + else if (videoPath.compare(0, 7, "rtmp://") == 0) { + __formatName.assign("rtmp"); + } + else if (videoPath.compare(0, 7, "http://") == 0) { + __formatName.assign("http"); + } + else if (videoPath.compare(0, 6, "ftp://") == 0) { + __formatName.assign("ftp"); + } + else if (videoPath.compare(0, 7, "sftp://") == 0) { + __formatName.assign("sftp"); + } + else { + __formatName.clear(); + } +} + +bool cmpc::CMpegServer::FFmpegSetup(string inVideoPath) { + resetPath(inVideoPath); + return FFmpegSetup(); +} + +cmpc::AVRational cmpc::CMpegServer::_setAVRational(int num, int den) { + AVRational res; + res.num = num; res.den = den; + return res; +} + +int64_t cmpc::CMpegServer::__FrameToPts(int64_t seekFrame) const { + return av_rescale(av_rescale(seekFrame, timeBase.den, timeBase.num), frameRate.den, frameRate.num); +} + +int64_t cmpc::CMpegServer::__TimeToPts(double seekTime) const { + return av_rescale(static_cast(seekTime * 1000), timeBase.den, timeBase.num) / 1000; +} + +bool cmpc::CMpegServer::__setup_check() const { + if ((! videoPath.empty()) && (! __formatName.empty()) && frameRate.den > 0 && frameRate.num > 0) { + return true; + } + else { + return false; + } +} + +void cmpc::CMpegServer::__log_packet() { + AVRational* time_base = &PFormatCtx->streams[Ppacket->stream_index]->time_base; + std::ostringstream str_data; + str_data << "pts:" << av_ts2str(Ppacket->pts) << " pts_time:" << av_ts2timestr(Ppacket->pts, time_base) + << " dts:" << av_ts2str(Ppacket->dts) << " dts_time:" << av_ts2timestr(Ppacket->dts, time_base) << endl; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_INFO, "%s", str_data_s.c_str()); +} + +int cmpc::CMpegServer::__write_frame() { + /* rescale output packet timestamp values from codec to stream timebase */ + av_packet_rescale_ts(Ppacket, PStreamContex.enc->time_base, PStreamContex.st->time_base); + Ppacket->stream_index = PStreamContex.st->index; + + // Update the time cursor according to the packet index. + AVRational &time_base = PFormatCtx->streams[Ppacket->stream_index]->time_base; + + auto cur_time = av_rescale_q(Ppacket->pts, time_base, time_base_q); + if (cur_time > __cur_time) { + __cur_time = cur_time; + } + + /* Write the compressed frame to the media file. */ + if (__dumpControl > 0) + __log_packet(); + return av_interleaved_write_frame(PFormatCtx, Ppacket); +} + +/* Add an output stream. */ +bool cmpc::CMpegServer::__add_stream(AVCodec** codec) { + /* find the encoder */ + AVCodecID codec_id; + auto srcwidth = widthSrc > 0 ? widthSrc : width; + auto srcheight = heightSrc > 0 ? heightSrc : height; + *codec = avcodec_find_encoder_by_name(codecName.c_str()); + if (!(*codec)) { + codec_id = PFormatCtx->oformat->video_codec; + cerr << "Could not find encoder " << codecName << ", use " << avcodec_get_name(codec_id) << " as an alternative." << endl; + *codec = avcodec_find_encoder(codec_id); + } + else { + codec_id = (*codec)->id; + PFormatCtx->oformat->video_codec = codec_id; + } + if (!(*codec)) { + cerr << "Could not find encoder for '" << avcodec_get_name(codec_id) << "'" << endl; + return false; + } + + PStreamContex.st = avformat_new_stream(PFormatCtx, nullptr); + if (!PStreamContex.st) { + cerr << "Could not allocate stream" << endl; + return false; + } + PStreamContex.st->id = PFormatCtx->nb_streams - 1; + auto c = avcodec_alloc_context3(*codec); + if (!c) { + cerr << "Could not alloc an encoding context" << endl; + return false; + } + if (nthread > 0) { + c->thread_count = nthread; + } + PStreamContex.enc = c; + + switch ((*codec)->type) { + case AVMediaType::AVMEDIA_TYPE_VIDEO: + c->codec_id = codec_id; + + c->bit_rate = bitRate; + /* Resolution must be a multiple of two. */ + c->width = width; + c->height = height; + /* timebase: This is the fundamental unit of time (in seconds) in terms + * of which frame timestamps are represented. For fixed-fps content, + * timebase should be 1/framerate and timestamp increments should be + * identical to 1. */ + PStreamContex.st->time_base.den = 0; + PStreamContex.st->time_base.num = 0; + //av_stream_set_r_frame_rate(PStreamContex.st, frameRate); + //cout << "(" << frameRate.num << ", " << frameRate.den << ")" << endl; + //PStreamContex.st->r_frame_rate + c->time_base = timeBase; + + //PStreamContex.st->frame + c->framerate = frameRate; + + c->gop_size = GOPSize; /* emit one intra frame every twelve frames at most */ + c->max_b_frames = MaxBFrame; + c->pix_fmt = STREAM_PIX_FMT; + if (c->codec_id == AVCodecID::AV_CODEC_ID_FLV1) { + /* just for testing, we also add B-frames */ + c->max_b_frames = 0; + } + if (c->codec_id == AVCodecID::AV_CODEC_ID_MPEG2VIDEO) { + /* just for testing, we also add B-frames */ + c->max_b_frames = 2; + } + if (c->codec_id == AVCodecID::AV_CODEC_ID_MPEG1VIDEO) { + /* Needed to avoid using macroblocks in which some coeffs overflow. + * This does not happen with normal video, it just happens here as + * the motion of the chroma plane does not match the luma plane. */ + c->mb_decision = 2; + } + if (c->pix_fmt != STREAM_PIX_FMT) { + /* as we only generate a YUV420P picture, we must convert it + * to the codec pixel format if needed */ + if (!PStreamContex.sws_ctx) { + PStreamContex.sws_ctx = sws_getContext(c->width, c->height, + STREAM_PIX_FMT, + c->width, c->height, + c->pix_fmt, + SCALE_FLAGS, nullptr, nullptr, nullptr); + if (!PStreamContex.sws_ctx) { + cerr << "Could not initialize the conversion context" << endl; + return false; + } + } + } + if (!PswsCtx) { + PswsCtx = sws_getContext(srcwidth, srcheight, + AVPixelFormat::AV_PIX_FMT_RGB24, + c->width, c->height, + c->pix_fmt, + SCALE_FLAGS, nullptr, nullptr, nullptr); + if (!PswsCtx) { + cerr << "Could not initialize the conversion context" << endl; + return false; + } + } + if (!RGBbuffer) { + auto numBytes = av_image_get_buffer_size(AVPixelFormat::AV_PIX_FMT_RGB24, srcwidth, srcheight, 1); + RGBbuffer = (uint8_t*)av_malloc(numBytes * sizeof(uint8_t)); + } + break; + + default: + break; + } + + /* Some formats want stream headers to be separate. */ + if (PFormatCtx->oformat->flags & AVFMT_GLOBALHEADER) + c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; + return true; +} + +/* video output */ +cmpc::AVFrame* cmpc::CMpegServer::__alloc_picture(enum AVPixelFormat pix_fmt, int width, int height) { + auto picture = av_frame_alloc(); + if (!picture) + return nullptr; + picture->format = pix_fmt; + picture->width = width; + picture->height = height; + /* allocate the buffers for the frame data */ + auto ret = av_frame_get_buffer(picture, 32); + if (ret < 0) { + cerr << "Could not allocate frame data." << endl; + return nullptr; + } + return picture; +} + +bool cmpc::CMpegServer::__open_video(AVCodec* codec, AVDictionary* opt_arg) { + int ret; + auto c = PStreamContex.enc; + AVDictionary* opt = nullptr; + + av_dict_copy(&opt, opt_arg, 0); + /* open the codec */ + ret = avcodec_open2(c, codec, &opt); + av_dict_free(&opt); + if (ret < 0) { + cerr << "Could not open video codec: " << av_err2str(ret) << endl; + return false; + } + /* allocate and init a re-usable frame */ + PStreamContex.frame = __alloc_picture(c->pix_fmt, c->width, c->height); + if (!PStreamContex.frame) { + cerr << "Could not allocate video frame" << endl; + return false; + } + /* If the output format is not YUV420P, then a temporary YUV420P + * picture is needed too. It is then converted to the required + * output format. */ + PStreamContex.tmp_frame = nullptr; + if (c->pix_fmt != STREAM_PIX_FMT) { + PStreamContex.tmp_frame = __alloc_picture(STREAM_PIX_FMT, c->width, c->height); + if (!PStreamContex.tmp_frame) { + cerr << "Could not allocate temporary picture" << endl; + return false; + } + } + /* copy the stream parameters to the muxer */ + ret = avcodec_parameters_from_context(PStreamContex.st->codecpar, c); + if (ret < 0) { + cerr << "Could not copy the stream parameters" << endl; + return false; + } + return true; +} + +cmpc::AVFrame* cmpc::CMpegServer::__get_video_frame(PyArrayObject* PyFrame) { + auto c = PStreamContex.enc; + + /* check if we want to generate more frames */ + //if (av_compare_ts(PStreamContex.next_pts, c->time_base, STREAM_DURATION, { 1, 1 }) >= 0) + // return nullptr; + /* when we pass a frame to the encoder, it may keep a reference to it + * internally; make sure we do not overwrite it here */ + if (av_frame_make_writable(PStreamContex.frame) < 0) + return nullptr; + if (c->pix_fmt != STREAM_PIX_FMT) { + /* as we only generate a YUV420P picture, we must convert it + * to the codec pixel format if needed */ + if (!PStreamContex.sws_ctx) { + PStreamContex.sws_ctx = sws_getContext(c->width, c->height, + STREAM_PIX_FMT, + c->width, c->height, + c->pix_fmt, + SCALE_FLAGS, nullptr, nullptr, nullptr); + if (!PStreamContex.sws_ctx) { + cerr << "Could not initialize the conversion context" << endl; + return nullptr; + } + } + if (!_LoadFrame_castFromPyFrameArray(PStreamContex.tmp_frame, PyFrame)) { + return nullptr; + } + sws_scale(PStreamContex.sws_ctx, + (const uint8_t* const*)PStreamContex.tmp_frame->data, PStreamContex.tmp_frame->linesize, + 0, c->height, PStreamContex.frame->data, PStreamContex.frame->linesize); + } + else { + if (!_LoadFrame_castFromPyFrameArray(PStreamContex.frame, PyFrame)) { + return nullptr; + } + } + + PStreamContex.frame->pts = PStreamContex.next_frame; + PStreamContex.next_frame++; + return PStreamContex.frame; +} + +bool cmpc::CMpegServer::_LoadFrame_castFromPyFrameArray(AVFrame* frame, PyArrayObject* PyFrame) { + /* make sure the frame data is writable */ + if (!__frameRGB) { + cerr << "Could not allocate frameRGB" << endl; + return false; + } + auto out_dataptr = reinterpret_cast(PyArray_DATA(PyFrame)); + auto srcwidth = widthSrc > 0 ? widthSrc : width; + auto srcheight = heightSrc > 0 ? heightSrc : height; + memcpy(RGBbuffer, out_dataptr, static_cast(srcwidth) * static_cast(srcheight) * 3 * sizeof(uint8_t)); + // Assign appropriate parts of buffer to image planes in pFrameRGB Note that pFrameRGB is an AVFrame, but AVFrame is a superset of AVPicture + av_image_fill_arrays(__frameRGB->data, __frameRGB->linesize, RGBbuffer, AVPixelFormat::AV_PIX_FMT_RGB24, srcwidth, srcheight, 1); + sws_scale(PswsCtx, __frameRGB->data, __frameRGB->linesize, 0, srcheight, frame->data, frame->linesize); + //cout << "Free 1" << endl; + //delete frameRGB; + //cout << "Free 2" << endl; + return true; +} + +/* +* encode one video frame and send it to the muxer +* return 1 when encoding is finished, 0 otherwise +*/ +int cmpc::CMpegServer::__avcodec_encode_video2(AVCodecContext* enc_ctx, AVPacket* pkt, AVFrame* frame) { + int ret; + int wfret = 0; + + if (frame) { + if (__dumpControl > 1) { + std::ostringstream str_data; + str_data << "Send frame " << frame->pts << endl; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_DEBUG, "%s", str_data_s.c_str()); + } + } + else { + return AVERROR(EAGAIN); + } + + ret = avcodec_send_frame(enc_ctx, frame); + // In particular, we don't expect AVERROR(EAGAIN), because we read all + // decoded frames with avcodec_receive_frame() until done. + if (ret < 0) { + return ret == AVERROR_EOF ? 0 : ret; + } + + ret = avcodec_receive_packet(enc_ctx, pkt); + if (ret == AVERROR(EAGAIN)) + return 0; + + if (__dumpControl > 0) { + std::ostringstream str_data; + str_data << "Write packet " << pkt->pts << " (size=" << pkt->size << "), "; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_INFO, "%s", str_data_s.c_str()); + } + + if (!ret) { + wfret = __write_frame(); + av_packet_unref(Ppacket); + if (wfret < 0) { + cerr << "Error while writing video frame: " << av_err2str(ret) << endl; + return wfret; + } + } + return ret; +} + +int cmpc::CMpegServer::__avcodec_encode_video2_flush(AVCodecContext* enc_ctx, AVPacket* pkt) { + int ret; + int wfret = 0; + if (__dumpControl > 1) { + std::ostringstream str_data; + str_data << "Flush all packets" << endl; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_DEBUG, "%s", str_data_s.c_str()); + } + + ret = avcodec_send_frame(enc_ctx, nullptr); + // In particular, we don't expect AVERROR(EAGAIN), because we read all + // decoded frames with avcodec_receive_frame() until done. + if (ret < 0) { + return ret == AVERROR_EOF ? 0 : ret; + } + + while (ret >= 0) { + ret = avcodec_receive_packet(enc_ctx, pkt); + if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) { + return 0; + } + if (__dumpControl > 0) { + std::ostringstream str_data; + str_data << "Write packet " << pkt->pts << " (size=" << pkt->size << "), "; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_INFO, "%s", str_data_s.c_str()); + } + if (!ret) { + wfret = __write_frame(); + av_packet_unref(pkt); + } + else { + wfret = 0; + } + if (wfret < 0) { + cerr << "Error while writing video frame: " << av_err2str(ret) << endl; + return wfret; + } + } + return ret; +} + +int cmpc::CMpegServer::ServeFrameBlock(PyArrayObject* PyFrame) { + if (__start_time > 0) { + auto cur_time = static_cast(av_gettime() - __start_time); + if (cur_time < __cur_time) { + av_usleep(static_cast((__cur_time - cur_time) / 2)); + } + ServeFrame(PyFrame); + return 0; + } + else { + return -1; + } +} + +int cmpc::CMpegServer::ServeFrame(PyArrayObject* PyFrame) { + int ret; + auto c = PStreamContex.enc; + AVFrame* frame = nullptr; + + if ((!__have_video) || (!__enable_header)) + cerr << "Not allowed to use this method before FFmpegSetup()" << endl; + if (PyFrame) { + frame = __get_video_frame(PyFrame); + ret = __avcodec_encode_video2(c, Ppacket, frame); + } + else { + frame = nullptr; + ret = __avcodec_encode_video2_flush(c, Ppacket); + } + + if (ret < 0) { + cerr << "Error encoding video frame: " << av_err2str(ret) << endl; + return ret; + } + return frame ? 0 : 1; +} + +void cmpc::CMpegServer::setParameter(string keyword, void* ptr) { + if (keyword.compare("decoder") == 0) { + CMpegDecoder* ref = reinterpret_cast(ptr); + resetPath(ref->videoPath); + codecName.assign(ref->_str_codec); + if (ref->PCodecCtx) { + bitRate = ref->PCodecCtx->bit_rate; + GOPSize = ref->PCodecCtx->gop_size; + MaxBFrame = ref->PCodecCtx->max_b_frames; + if (PStreamContex.enc) { + PStreamContex.enc->thread_count = ref->PCodecCtx->thread_count; + } + nthread = ref->PCodecCtx->thread_count; + } + else { + if (PStreamContex.enc) { + PStreamContex.enc->thread_count = ref->nthread; + } + nthread = ref->nthread; + } + if (ref->widthDst > 0 && ref->heightDst > 0) { + width = ref->widthDst; + height = ref->heightDst; + } + else { + width = ref->width; + height = ref->height; + } + widthSrc = width; + heightSrc = height; + if (ref->PVideoStream) { + //timeBase = ref->PVideoStream->time_base; + frameRate = ref->PVideoStream->avg_frame_rate; + timeBase = _setAVRational(frameRate.den, frameRate.num); + } + if (GOPSize > 0) { + auto frame_ahead = 2 * GOPSize; + __pts_ahead = __FrameToPts(static_cast(frame_ahead)); + } + } + else if (keyword.compare("client") == 0) { + CMpegClient* ref = reinterpret_cast(ptr); + resetPath(ref->videoPath); + codecName.assign(ref->_str_codec); + if (ref->PCodecCtx) { + bitRate = ref->PCodecCtx->bit_rate; + GOPSize = ref->PCodecCtx->gop_size; + MaxBFrame = ref->PCodecCtx->max_b_frames; + if (PStreamContex.enc) { + PStreamContex.enc->thread_count = ref->PCodecCtx->thread_count; + } + nthread = ref->PCodecCtx->thread_count; + } + else { + if (PStreamContex.enc) { + PStreamContex.enc->thread_count = ref->nthread; + } + nthread = ref->nthread; + } + if (ref->widthDst > 0 && ref->heightDst > 0) { + width = ref->widthDst; + height = ref->heightDst; + } + else { + width = ref->width; + height = ref->height; + } + widthSrc = width; + heightSrc = height; + if (ref->PVideoStream) { + //timeBase = ref->PVideoStream->time_base; + frameRate = ref->PVideoStream->avg_frame_rate; + timeBase = _setAVRational(frameRate.den, frameRate.num); + } + if (GOPSize > 0) { + auto frame_ahead = 2 * GOPSize; + __pts_ahead = __FrameToPts(static_cast(frame_ahead)); + } + } + else if (keyword.compare("configDict") == 0) { + PyObject* ref = reinterpret_cast(ptr); + if (PyDict_Check(ref)) { + string key; + PyObject* val; + // Set parameters. + key.assign("videoPath"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyBytes_Check(val)) { + auto val_str = string(PyBytes_AsString(val)); + resetPath(val_str); + } + } + else { + key.assign("videoAddress"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyBytes_Check(val)) { + auto val_str = string(PyBytes_AsString(val)); + resetPath(val_str); + } + } + } + key.assign("codecName"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyBytes_Check(val)) { + auto val_str = string(PyBytes_AsString(val)); + codecName.assign(val_str); + } + } + key.assign("bitRate"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyLong_Check(val)) { + auto val_num = static_cast(PyLong_AsLongLong(val)); + bitRate = val_num; + } + } + key.assign("GOPSize"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyLong_Check(val)) { + auto val_num = static_cast(PyLong_AsLong(val)); + GOPSize = val_num; + } + } + key.assign("maxBframe"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyLong_Check(val)) { + auto val_num = static_cast(PyLong_AsLong(val)); + MaxBFrame = val_num; + } + } + key.assign("width"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyLong_Check(val)) { + auto val_num = static_cast(PyLong_AsLong(val)); + width = val_num; + widthSrc = val_num; + } + } + key.assign("height"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyLong_Check(val)) { + auto val_num = static_cast(PyLong_AsLong(val)); + height = val_num; + heightSrc = val_num; + } + } + key.assign("widthSrc"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyLong_Check(val)) { + auto val_num_1 = static_cast(PyLong_AsLong(val)); + key.assign("heightSrc"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyLong_Check(val)) { + auto val_num_2 = static_cast(PyLong_AsLong(val)); + widthSrc = val_num_1; + heightSrc = val_num_2; + } + } + } + } + key.assign("widthDst"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyLong_Check(val)) { + auto val_num_1 = static_cast(PyLong_AsLong(val)); + key.assign("heightDst"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyLong_Check(val)) { + auto val_num_2 = static_cast(PyLong_AsLong(val)); + width = val_num_1; + height = val_num_2; + } + } + } + } + key.assign("frameRate"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyTuple_Check(val)) { + auto valObj = PyTuple_GetItem(val, 0); + int num = static_cast(PyLong_AsLong(valObj)); + valObj = PyTuple_GetItem(val, 1); + int den = static_cast(PyLong_AsLong(valObj)); + frameRate = _setAVRational(num, den); + timeBase = _setAVRational(den, num); + if (GOPSize > 0) { + auto frame_ahead = 2 * GOPSize; + __pts_ahead = __FrameToPts(static_cast(frame_ahead)); + } + } + } + key.assign("nthread"); + val = PyDict_GetItemString(ref, key.c_str()); + if (val) { + if (PyLong_Check(val)) { + auto val_num = static_cast(PyLong_AsLong(val)); + if (PStreamContex.enc) { + PStreamContex.enc->thread_count = val_num; + } + nthread = val_num; + } + } + } + } + else if (keyword.compare("videoAddress") == 0) { + string* ref = reinterpret_cast(ptr); + resetPath(*ref); + } + else if (keyword.compare("codecName") == 0) { + string* ref = reinterpret_cast(ptr); + codecName.assign(*ref); + } + else if (keyword.compare("bitRate") == 0) { + double* ref = reinterpret_cast(ptr); + auto bit_rate = static_cast((*ref) * 1024); + bitRate = bit_rate; + } + else if (keyword.compare("width") == 0) { + int* ref = reinterpret_cast(ptr); + width = *ref; + } + else if (keyword.compare("height") == 0) { + int* ref = reinterpret_cast(ptr); + height = *ref; + } + else if (keyword.compare("widthSrc") == 0) { + int* ref = reinterpret_cast(ptr); + widthSrc = *ref; + } + else if (keyword.compare("heightSrc") == 0) { + int* ref = reinterpret_cast(ptr); + heightSrc = *ref; + } + else if (keyword.compare("GOPSize") == 0) { + int* ref = reinterpret_cast(ptr); + GOPSize = *ref; + } + else if (keyword.compare("frameAhead") == 0) { + int* ref = reinterpret_cast(ptr); + auto frame_ahead = *ref; + __pts_ahead = __FrameToPts(static_cast(frame_ahead)); + } + else if (keyword.compare("maxBframe") == 0) { + int* ref = reinterpret_cast(ptr); + MaxBFrame = *ref; + } + else if (keyword.compare("frameRate") == 0) { + PyObject* ref = reinterpret_cast(ptr); + auto refObj = PyTuple_GetItem(ref, 0); + int num = static_cast(PyLong_AsLong(refObj)); + refObj = PyTuple_GetItem(ref, 1); + int den = static_cast(PyLong_AsLong(refObj)); + frameRate = _setAVRational(num, den); + timeBase = _setAVRational(den, num); + if (GOPSize > 0) { + auto frame_ahead = 2 * GOPSize; + __pts_ahead = __FrameToPts(static_cast(frame_ahead)); + } + } + else if (keyword.compare("nthread") == 0) { + auto ref = reinterpret_cast(ptr); + if (PStreamContex.enc) { + PStreamContex.enc->thread_count = *ref; + } + nthread = *ref; + } +} + +PyObject* cmpc::CMpegServer::getParameter(string keyword) { + if (keyword.compare("videoAddress") == 0) { + return PyUnicode_DecodeFSDefaultAndSize(videoPath.c_str(), static_cast(videoPath.size())); + } + else if (keyword.compare("codecName") == 0) { + return PyUnicode_DecodeFSDefaultAndSize(codecName.c_str(), static_cast(codecName.size())); + } + else if (keyword.compare("formatName") == 0) { + return PyUnicode_DecodeFSDefaultAndSize(__formatName.c_str(), static_cast(__formatName.size())); + } + else if (keyword.compare("bitRate") == 0) { + auto bit_rate = static_cast(bitRate) / 1024; + return Py_BuildValue("d", bit_rate); + } + else if (keyword.compare("width") == 0) { + return Py_BuildValue("i", width); + } + else if (keyword.compare("height") == 0) { + return Py_BuildValue("i", height); + } + else if (keyword.compare("widthSrc") == 0) { + return Py_BuildValue("i", widthSrc); + } + else if (keyword.compare("heightSrc") == 0) { + return Py_BuildValue("i", heightSrc); + } + else if (keyword.compare("GOPSize") == 0) { + return Py_BuildValue("i", GOPSize); + } + else if (keyword.compare("maxBframe") == 0) { + return Py_BuildValue("i", MaxBFrame); + } + else if (keyword.compare("ptsAhead") == 0) { + return Py_BuildValue("L", __pts_ahead); + } + else if (keyword.compare("waitRef") == 0) { + int64_t cur_time = 0; + if (__start_time > 0) { + cur_time = av_gettime() - __start_time; + if (cur_time < __cur_time) { + return Py_BuildValue("d", static_cast(__cur_time - cur_time) * av_q2d(time_base_q) / 2); + } + else { + return Py_BuildValue("d", 0.0); + } + } + else { + return Py_BuildValue("d", 0.0); + } + } + else if (keyword.compare("frameRate") == 0) { + auto frame_base = frameRate; + auto frame_rate = static_cast(frame_base.num) / static_cast(frame_base.den); + return Py_BuildValue("d", frame_rate); + } + else if (keyword.compare("nthread") == 0) { + if (PStreamContex.enc) { + return Py_BuildValue("i", PStreamContex.enc->thread_count); + } + else { + return Py_BuildValue("i", nthread); + } + } + else { + Py_RETURN_NONE; + } +} + +PyObject* cmpc::CMpegServer::getParameter() { + auto res = PyDict_New(); + string key; + PyObject* val = nullptr; + // Fill the values. + key.assign("videoAddress"); + val = Py_BuildValue("y", videoPath.c_str()); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("codecName"); + val = Py_BuildValue("y", codecName.c_str()); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("formatName"); + val = Py_BuildValue("y", __formatName.c_str()); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("bitRate"); + val = Py_BuildValue("L", bitRate); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("GOPSize"); + val = Py_BuildValue("i", GOPSize); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("maxBframe"); + val = Py_BuildValue("i", MaxBFrame); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("ptsAhead"); + val = Py_BuildValue("L", __pts_ahead); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + if (widthSrc > 0) { + key.assign("widthSrc"); + val = Py_BuildValue("i", widthSrc); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + } + if (heightSrc > 0) { + key.assign("heightSrc"); + val = Py_BuildValue("i", heightSrc); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + } + key.assign("width"); + val = Py_BuildValue("i", width); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("height"); + val = Py_BuildValue("i", height); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + key.assign("frameRate"); + val = Py_BuildValue("(ii)", frameRate.num, frameRate.den); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + if (PStreamContex.enc) { + key.assign("nthread"); + val = Py_BuildValue("i", PStreamContex.enc->thread_count); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + } + else { + key.assign("nthread"); + val = Py_BuildValue("i", nthread); + PyDict_SetItemString(res, key.c_str(), val); + Py_DECREF(val); + } + return res; +} + +bool cmpc::CMpegServer::FFmpegSetup() { + if (!__setup_check()) { + cerr << "Have not get necessary and correct configurations, so FFmpegSetup() should not be called." << endl; + return false; + } + AVCodec* video_codec = nullptr; + int ret; + + if (Ppacket) + av_packet_free(&Ppacket); + Ppacket = av_packet_alloc(); + if (!Ppacket) + return false; + + AVDictionary* opt = nullptr; + //av_dict_set(&opt, "vcodec", codecName.c_str(), 0); + //av_dict_set(&opt, "fflags", "", 0); + + /* allocate the output media context */ + //auto getFormat = av_guess_format(codecName.c_str(), nullptr, nullptr); + string format_name; + if (__formatName.compare("rtsp") == 0) { + format_name.assign("rtsp"); + } + else if(__formatName.compare("rtmp") == 0) { + format_name.assign("flv"); + } + else if (__formatName.compare("http") == 0) { + format_name.assign("flv"); + } + else if (__formatName.compare("ftp") == 0) { + format_name.assign("flv"); + } + else if (__formatName.compare("sftp") == 0) { + format_name.assign("flv"); + } + else { + cout << "The format name " << __formatName << " is not supported. Now we only support \"rtsp\", \"rtmp\", \"http\"." << endl; + return false; + } + avformat_alloc_output_context2(&PFormatCtx, nullptr, format_name.c_str(), videoPath.c_str()); + PFormatCtx->avoid_negative_ts = AVFMT_AVOID_NEG_TS_AUTO; + if (!PFormatCtx) { + cout << "Could not select the encoder. The allocation is failed." << endl; + return false; + } + + auto fmt = PFormatCtx->oformat; + + /* Add the audio and video streams using the default format codecs + * and initialize the codecs. */ + if (fmt->video_codec != AVCodecID::AV_CODEC_ID_NONE) { + if (!__add_stream(&video_codec)) { + FFmpegClose(); + return false; + } + else + __have_video = true; + } + + /* Now that all the parameters are set, we can open the audio and + * video codecs and allocate the necessary encode buffers. */ + if (__have_video) { + if (!__open_video(video_codec, opt)) { + FFmpegClose(); + return false; + } + else + __have_video = true; + } + + if (__dumpControl > 1) { + av_dump_format(PFormatCtx, 0, videoPath.c_str(), 1); + } + + /* open the output file, if needed */ + if (!(fmt->flags & AVFMT_NOFILE)) { + AVDictionary* opt_io = nullptr; + /*if (__formatName.compare("http") == 0) { + ret = av_dict_set(&opt_io, "listen", "1", 0); + if (ret < 0) { + cerr << "Could not set the options for the file: " << av_err2str(ret) << endl; + FFmpegClose(); + return false; + } + }*/ + ret = avio_open2(&PFormatCtx->pb, videoPath.c_str(), AVIO_FLAG_WRITE, nullptr, &opt_io); + if (ret < 0) { + cerr << "Could not open '" << videoPath << "': " << av_err2str(ret) << endl; + FFmpegClose(); + return false; + } + if (opt_io) { + av_dict_free(&opt_io); + } + } + + if (!(__frameRGB = av_frame_alloc())) { + cerr << "Could Allocate Temp Frame" << endl; + FFmpegClose(); + return false; + } + + /* Write the stream header, if any. */ + ret = avformat_write_header(PFormatCtx, &opt); + if (ret < 0) { + cerr << "Error occurred when opening output file: " << av_err2str(ret) << endl; + FFmpegClose(); + return false; + } + else { + __enable_header = true; + } + + // Register the start time. + __start_time = av_gettime(); + return true; +} + +void cmpc::CMpegServer::FFmpegClose() { + if (__enable_header && __have_video) { + //cout << "Flush Video" << endl; + int x; + if ((x = ServeFrame(nullptr)) == 0) { + // cout << "Ret: " << x << endl; + } + if (__dumpControl > 0) { + std::ostringstream str_data; + str_data << "All frames are flushed from cache, the video would be closed." << endl; + auto str_data_s = str_data.str(); + av_log(nullptr, AV_LOG_INFO, "%s", str_data_s.c_str()); + } + } + __start_time = 0; + __cur_time = 0; + if (PFormatCtx) { + if (__enable_header) { + av_write_trailer(PFormatCtx); + __enable_header = false; + } + /* Close each codec. */ + if (__have_video) { + /* free the stream */ + //avformat_free_context(PFormatCtx); + if (PStreamContex.enc) + avcodec_free_context(&PStreamContex.enc); + if (PStreamContex.frame) + av_frame_free(&PStreamContex.frame); + if (PStreamContex.tmp_frame) + av_frame_free(&PStreamContex.tmp_frame); + if (PStreamContex.sws_ctx) { + sws_freeContext(PStreamContex.sws_ctx); + PStreamContex.sws_ctx = nullptr; + } + if (PswsCtx) { + sws_freeContext(PswsCtx); + PswsCtx = nullptr; + } + if (RGBbuffer) { + av_free(RGBbuffer); + RGBbuffer = nullptr; + } + __have_video = false; + } + auto fmt = PFormatCtx->oformat; + if (!(fmt->flags & AVFMT_NOFILE)) + /* Close the output file. */ + avio_closep(&PFormatCtx->pb); + /* free the stream */ + avformat_free_context(PFormatCtx); + PFormatCtx = nullptr; + } + if (Ppacket) { + av_packet_free(&Ppacket); + Ppacket = nullptr; + } + if (__frameRGB) { + av_frame_free(&__frameRGB); + } +} + +void cmpc::CMpegServer::dumpFormat() { + if (PFormatCtx) + av_dump_format(PFormatCtx, 0, videoPath.c_str(), 1); + else + cerr << "Not loaded video format context now. dumpFormat() is not avaliable." << endl; +} + +ostream& cmpc::operator<<(ostream& out, cmpc::CMpegServer& self_class) { + out << std::setw(1) << "/"; + out << std::setfill('*') << std::setw(44) << "" << std::setfill(' ') << endl; + out << std::setw(1) << " * Packed FFmpeg Server - Y. Jin V" << MPEGCODER_CURRENT_VERSION << endl; + out << " " << std::setfill('*') << std::setw(44) << "" << std::setfill(' ') << endl; + out << std::setiosflags(std::ios::left) << std::setw(25) << " * VideoAddress: " \ + << self_class.videoPath << endl; + out << std::setiosflags(std::ios::left) << std::setw(25) << " * (Width, Height): " \ + << self_class.width << ", " << self_class.height << endl; + if (self_class.widthSrc > 0 && self_class.heightSrc > 0) { + out << std::setiosflags(std::ios::left) << std::setw(25) << " * (WidthSrc, HeightSrc): " \ + << self_class.widthSrc << ", " << self_class.heightSrc << endl; + } + else if (self_class.widthSrc > 0) { + out << std::setiosflags(std::ios::left) << std::setw(25) << " * WidthSrc: " \ + << self_class.widthSrc << endl; + } + else if (self_class.heightSrc > 0) { + out << std::setiosflags(std::ios::left) << std::setw(25) << " * HeightSrc: " \ + << self_class.heightSrc << endl; + } + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Enccoder: " \ + << self_class.codecName << endl; + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Stream format: " \ + << self_class.__formatName << endl; + if (self_class.PStreamContex.enc) { + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Thread number: " \ + << self_class.PStreamContex.enc->thread_count << endl; + } + else { + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Thread number (P): " \ + << self_class.nthread << endl; + } + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Bit Rate: " \ + << (self_class.bitRate >> 10) << " [Kbit/s]" << endl; + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Frame Rate: " \ + << static_cast(self_class.frameRate.num) / static_cast(self_class.frameRate.den) << " [FPS]" << endl; + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Ahead PTS: " \ + << self_class.__pts_ahead << endl; + out << std::setiosflags(std::ios::left) << std::setw(25) << " * GOP Size: " \ + << self_class.GOPSize << endl; + out << std::setiosflags(std::ios::left) << std::setw(25) << " * Maxmal Bframe Density: " \ + << self_class.MaxBFrame << " [/GOP]" << endl; + out << std::setw(1) << " */"; + return out; +} diff --git a/MpegCoder/MpegStreamer.h b/MpegCoder/MpegStreamer.h new file mode 100644 index 0000000..c09afac --- /dev/null +++ b/MpegCoder/MpegStreamer.h @@ -0,0 +1,179 @@ +// 下列 ifdef 块是创建使从 DLL 导出更简单的 +// 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 MPEGCODER_EXPORT +// 符号编译的。在使用此 DLL 的 +// 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将 +// MPEGCODER_API 函数视为自 DLL 导入,而此 DLL 则将用此宏定义的 +// 符号视为是被导出的。 +#ifndef MPEGSTREAMER_H_INCLUDED +#define MPEGSTREAMER_H_INCLUDED + +#include "MpegBase.h" + +// Exported from MpegCoder.dll +namespace cmpc { + + extern int8_t __dumpControl; + class CMpegDecoder; + class CMpegEncoder; + + class BufferList { // A buffer holder of several frames + public: + BufferList(void); + ~BufferList(void); + BufferList(const BufferList &ref); + BufferList& operator=(const BufferList &ref); + BufferList(BufferList &&ref) noexcept; + BufferList& operator=(BufferList &&ref) noexcept; + void clear(void); + const int64_t size() const; + void set(int64_t set_size, int width, int height, int widthDst=0, int heightDst=0); + void set_timer(AVRational targetFrameRate, AVRational timeBase); + bool reset_memory(); + void freeze_write(int64_t read_size); + bool write(SwsContext *PswsCtx, AVFrame *frame); + PyObject *read(); + private: + int64_t _Buffer_pos; // Writring cursor of the source buffer,pointing to the index of the currently written frame. + int64_t _Buffer_rpos; // Reading cursor of the source buffer,pointing to the index of the currently read frame. + int64_t _Buffer_size; // Size of the source buffer, it should be determined by the numeber of required frames. + int64_t __Read_size; // A temporary variable used for showing the size of the data to be read. + int64_t next_pts; + int64_t interval_pts; + int dst_width, dst_height; + int src_width, src_height; + int _Buffer_capacity; + AVFrame *frameRGB; + uint8_t **_Buffer_List; // Source buffer, the size of this buffer is determined by the number of required frames. + }; + + class CMpegClient { + public: + CMpegClient(void); // Constructor. + ~CMpegClient(void); // 3-5 law. Destructor. + CMpegClient(const CMpegClient &ref) = delete; // Delete the copy constructor. + CMpegClient& operator=(const CMpegClient &ref) = delete; // Delete the copy assignment operator. + CMpegClient(CMpegClient &&ref) noexcept; // Move constructor. + CMpegClient& operator=(CMpegClient &&ref) noexcept; // Move assignment operator. + friend class CMpegEncoder; // Let the encoder be able to access the member of this class. + friend class CMpegServer; // Let the server be able to access the member of this class. + friend ostream & operator<<(ostream & out, CMpegClient & self_class); // Show the results. + void clear(void); // Clear all configurations and resources. + void meta_protected_clear(void); // Clear the resources, but the configurations are remained. + void dumpFormat(); // Show the av_format results. + void setParameter(string keyword, void *ptr); // Set arguments. + PyObject* getParameter(string keyword); // Get the current arguments. + PyObject* getParameter(); // Get all key arguments. + void resetPath(string inVideoPath); // Reset the path (URL) of the online video stream. + bool FFmpegSetup(); // Configure the decoder, and extract the basic meta-data. This method is also equipped in the constructor. + bool FFmpegSetup(string inVideoPath); // Configure the decoder with extra arguments. + bool start(); // Start the listening to the online stream. + void terminate(); // Terminate the listener. + PyObject * ExtractFrame(int64_t readsize); // Extract frames with the given number. + PyObject * ExtractFrame(); // Extract frames. The number is configured in the class properties. + private: + string videoPath; // The path (URL) of the online video stream. + AVFormatContext *PFormatCtx; // Format context of the video. + AVCodecContext *PCodecCtx; // Codec context of the video. + int width, height; // Width, height of the video. + int widthDst, heightDst; // Target width, height of ExtractFrame(). + enum AVPixelFormat PPixelFormat; // Enum object of the pixel format. + AVStream *PVideoStream; // Video stream. + + AVFrame *frame; + + int PVideoStreamIDX; // The index of the video stream. + int PVideoFrameCount; // The counter of the decoded frames. + BufferList buffer; // The buffer of the RGB formatted images. + struct SwsContext *PswsCtx; // The context of the scale transformator. + int64_t cache_size, read_size; + AVRational frameRate; + + std::thread read_handle; // The thread of the circular frame reader. + std::mutex read_check; // Lock for reading the status. + std::mutex info_lock; // Lock for reading the info. + bool reading; + + string _str_codec; // The name of the current codec. + double _duration; // The duration of the current video. + int64_t _predictFrameNum; // The prediction of the total number of frames. + int nthread; // The number of threads; + + /* Enable or disable frame reference counting. You are not supposed to support + * both paths in your application but pick the one most appropriate to your + * needs. Look for the use of refcount in this example to see what are the + * differences of API usage between them. */ + int refcount; // Reference count of the video frame. + bool __setup_check() const; + int _open_codec_context(int &stream_idx, AVCodecContext *&dec_ctx, AVFormatContext *PFormatCtx, enum AVMediaType type); + void __client_holder(); + AVRational _setAVRational(int num, int den); + int __save_frame(AVFrame *&frame, AVPacket *&pkt, bool &got_frame, int cached); + int __avcodec_decode_video2(AVCodecContext *avctx, AVFrame *frame, bool &got_frame, AVPacket *pkt); + }; + + class CMpegServer { + public: + CMpegServer(void); // Constructor. + ~CMpegServer(void); // 3-5 law. Destructor. + CMpegServer(const CMpegServer& ref); // Delete the copy constructor. + CMpegServer& operator=(const CMpegServer& ref); // Delete the copy assignment operator. + CMpegServer(CMpegServer&& ref) noexcept; // Move constructor. + CMpegServer& operator=(CMpegServer&& ref) noexcept; // Move assignment operator. + //friend class CMpegEncoder; // Let the server be able to access the member of this class. + friend ostream& operator<<(ostream& out, CMpegServer& self_class); // Show the results. + void clear(void); // Clear all configurations and resources. + void meta_protected_clear(void); // Clear the resources, but the configurations are remained. + void resetPath(string inVideoPath); // Reset the path of the output video stream. + void dumpFormat(); // Show the av_format results. + bool FFmpegSetup(); // Configure the encoder, and create the file handle. This method is also equipped in the constructor. + bool FFmpegSetup(string inVideoPath); // Configure the encoder with extra arguments. + void FFmpegClose(); // Close the encoder, and finalize the written of the encoded video. + void setParameter(string keyword, void* ptr); // Set arguments. + PyObject* getParameter(string keyword); // Get the current arguments. + PyObject* getParameter(); // Get all key arguments. + int ServeFrameBlock(PyArrayObject* PyFrame); // Encode the frame into the output stream (block mode). + int ServeFrame(PyArrayObject* PyFrame); // Encode the frame into the output stream. + private: + string videoPath; // The path of the output video stream. + string __formatName; // The format name of the stream. Could be "rtsp" or "rtmp". This value is detected from the videoPath. + string codecName; // The name of the codec + int64_t bitRate; // The bit rate of the output video. + int64_t __pts_ahead; // The ahead pts. + int64_t __start_time; // The start time stamp. This value is used for controlling the writing of the frames. + int64_t __cur_time; // The current time stamp. This value is restricted by __pts_ahead. + int width, height; // The size of the frames in the output video. + int widthSrc, heightSrc; // The size of the input data (frames). + AVRational timeBase, frameRate; // The time base and the frame rate. + AVRational time_base_q; // The time base used for calculating the absolute time. + int GOPSize, MaxBFrame; // The size of GOPs, and the maximal number of B frames. + OutputStream PStreamContex; // The context of the current video parser. + AVFormatContext* PFormatCtx; // Format context of the video. + AVPacket* Ppacket; // AV Packet used for writing frames. + struct SwsContext* PswsCtx; // The context of the scale transformator. + AVFrame* __frameRGB; // A temp AV frame object. Used for converting the data format. + uint8_t* RGBbuffer; // Data buffer. + bool __have_video, __enable_header; + + int nthread; // The number of threads; + + AVRational _setAVRational(int num, int den); + int64_t __FrameToPts(int64_t seekFrame) const; + int64_t __TimeToPts(double seekTime) const; + bool __setup_check() const; + bool _LoadFrame_castFromPyFrameArray(AVFrame* frame, PyArrayObject* PyFrame); + void __log_packet(); + int __write_frame(); + bool __add_stream(AVCodec** codec); + AVFrame* __alloc_picture(enum AVPixelFormat pix_fmt, int width, int height); + bool __open_video(AVCodec* codec, AVDictionary* opt_arg); + AVFrame* __get_video_frame(PyArrayObject* PyFrame); + int __avcodec_encode_video2(AVCodecContext* enc_ctx, AVPacket* pkt, AVFrame* frame); + int __avcodec_encode_video2_flush(AVCodecContext* enc_ctx, AVPacket* pkt); + void __copyMetaData(const CMpegServer& ref); + }; + + ostream& operator<<(ostream& out, CMpegClient& self_class); + ostream& operator<<(ostream& out, CMpegServer& self_class); +} + +#endif \ No newline at end of file diff --git a/MpegCoder/dllmain.cpp b/MpegCoder/dllmain.cpp new file mode 100644 index 0000000..adafbd8 --- /dev/null +++ b/MpegCoder/dllmain.cpp @@ -0,0 +1,48 @@ +// dllmain.cpp : The entry of the dll program. +#include "MpegPyd.h" + +/***************************************************************************** +* The initialization of the module. Would be invoked when using import. +*****************************************************************************/ +PyMODINIT_FUNC // == __decslpec(dllexport) PyObject*, Define the exported main function. +PyInit_mpegCoder(void) { // The external module name is: --CppClass + import_array(); + /* Initialize libavcodec, and register all codecs and formats. */ + // Register everything + #ifndef FFMPG3_4 + av_register_all(); + #endif + #ifndef FFMPG4_0 + avformat_network_init(); + #endif + + PyObject* pReturn = 0; + // Configure the __new__ method as the default method. This method is used for building the instances. + C_MPDC_ClassInfo.tp_new = PyType_GenericNew; + C_MPEC_ClassInfo.tp_new = PyType_GenericNew; + C_MPCT_ClassInfo.tp_new = PyType_GenericNew; + C_MPSV_ClassInfo.tp_new = PyType_GenericNew; + + /* Finish the initialization, including the derivations. + * When success, return 0; Otherwise, return -1 and throw errors. */ + if (PyType_Ready(&C_MPDC_ClassInfo) < 0) + return nullptr; + if (PyType_Ready(&C_MPEC_ClassInfo) < 0) + return nullptr; + if (PyType_Ready(&C_MPCT_ClassInfo) < 0) + return nullptr; + if (PyType_Ready(&C_MPSV_ClassInfo) < 0) + return nullptr; + + pReturn = PyModule_Create(&ModuleInfo); // Create the module according to the module info. + if (pReturn == 0) + return nullptr; + + Py_INCREF(&ModuleInfo); // Because the module is not registered to the python counter, Py_INCREF is required to be invoked. + PyModule_AddFunctions(pReturn, C_MPC_MethodMembers); // Add the global method members. + PyModule_AddObject(pReturn, "MpegDecoder", (PyObject*)&C_MPDC_ClassInfo); // Add the class as one module member. + PyModule_AddObject(pReturn, "MpegEncoder", (PyObject*)&C_MPEC_ClassInfo); + PyModule_AddObject(pReturn, "MpegClient", (PyObject*)&C_MPCT_ClassInfo); + PyModule_AddObject(pReturn, "MpegServer", (PyObject*)&C_MPSV_ClassInfo); + return pReturn; +} diff --git a/MpegCoder/snprintf.cpp b/MpegCoder/snprintf.cpp new file mode 100644 index 0000000..6e53ecf --- /dev/null +++ b/MpegCoder/snprintf.cpp @@ -0,0 +1,75 @@ +/* + * C99-compatible snprintf() and vsnprintf() implementations + * Copyright (c) 2012 Ronald S. Bultje + * + * This file is part of FFmpeg. + * + * FFmpeg 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. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +extern "C" +{ + #include + #include + #include + #include +} + +#include "compat/va_copy.h" +#include "libavutil/error.h" +#include "compat/msvcrt/snprintf.h" + +#if defined(__MINGW32__) +#define EOVERFLOW EFBIG +#endif + +extern "C" +{ + int avpriv_snprintf(char *s, size_t n, const char *fmt, ...) { + va_list ap; + int ret; + + va_start(ap, fmt); + ret = avpriv_vsnprintf(s, n, fmt, ap); + va_end(ap); + + return ret; + } + + int avpriv_vsnprintf(char *s, size_t n, const char *fmt, va_list ap) { + int ret; + va_list ap_copy; + + if (n == 0) + return _vscprintf(fmt, ap); + else if (n > INT_MAX) + return AVERROR(EOVERFLOW); + + /* we use n - 1 here because if the buffer is not big enough, the MS + * runtime libraries don't add a terminating zero at the end. MSDN + * recommends to provide _snprintf/_vsnprintf() a buffer size that + * is one less than the actual buffer, and zero it before calling + * _snprintf/_vsnprintf() to workaround this problem. + * See http://msdn.microsoft.com/en-us/library/1kt27hek(v=vs.80).aspx */ + memset(s, 0, n); + va_copy(ap_copy, ap); + ret = _vsnprintf_s(s, n - 1, INT_MAX, fmt, ap_copy); + va_end(ap_copy); + if (ret == -1) + ret = _vscprintf(fmt, ap); + + return ret; + } +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..512ca11 --- /dev/null +++ b/README.md @@ -0,0 +1,106 @@ +# FFmpeg-Encoder-Decoder-for-Python + +This is a mpegCoder adapted from FFmpeg & Python-c-api. Using it you could get access to processing video easily. Just use it as a common module in python like this. + +```python +import mpegCoder +``` + +| Branch | Description | +| :-------------: | :-----------: | +| [`master` :link:][git-master] | The source project of `mpegCoder`, Windows version. | +| `master-linux` :link: | The source project of `mpegCoder`, Linux version. | +| [`example-client-check` :link:][exp1] | A testing project of the online video stream demuxing. | +| [`example-client-player` :link:][exp2] | A testing project of the simple online video stream player. | + +## Source project of `mpegCoder` (Linux) + +The following instructions are used for building the project on Windows with Visual Studio 2019. + +1. Clone the `master` branch which only contains the codes of `mpegCoder`: + + ```bash + git clone --single-branch -b master-linux https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python.git + ``` + +2. Get the shared version of the Linux FFMpeg. We support three different approaches for this step: + * Build the shared libs of FFMpeg from the source codes by yourself. We provide [a compiling script :page_with_curl:](https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/blob/deps/install-ffmpeg-4_4.sh) in the deps branch. You could download and build it by the following commands: + + ```shell + mkdir -p /apps + chmod +rwx /apps + curl -O https://raw.githubusercontent.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/deps/install-ffmpeg-4_4.sh + chmod +rwx install-ffmpeg-4_4.sh + ./install-ffmpeg-4_4.sh + ``` + + After running this script, the FFMpeg with most of the dependencies would be complied along with the shared libraries. Then you could replace the FFMpeg path in the `setup.py` by + + ```python + FFMPEG_DIR = '/apps/build/ffmpeg-4.4' + ``` + + * Download the pre-built dependencies. These dependencies are built by myself. You could download the archive [here :package:](https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/releases/download/deps-3.0.0/dep-linux-ffmpeg_4_4.tar.xz). The files need to be extracted to `./dependencies`: + + ```shell + cd FFmpeg-Encoder-Decoder-for-Python + mkdir -p dependencies + wget -O- https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/releases/download/deps-3.0.0/dep-linux-ffmpeg_4_4.tar.xz | tar xJ -C "./dependencies" + ``` + + * The dependencies could be also downloaded by the automatic script, you just need to run + + ```shell + python webtools.py + ``` + + This script requires users to install `urllib3`. The `tqdm` is also recommended to be installed. + +3. Building `mpegCoder` requires `GLIBC>=2.29`. This requirement is not satisfied in some cases. However, if you have built FFMpeg by our script, the requirement would be fulfilled (i.e. you could skip this step). If users are using our pre-built dependencies, users may need to solve this problem by + + ```shell + cd FFmpeg-Encoder-Decoder-for-Python + ln -sf ./dependencies/lib-fix/libm-2.31.so /lib/x86_64-linux-gnu/libm.so.6 + ``` + +4. Build `mpegCoder` by running the following script. The built file would be stored in `./build`. If you has not fetched the dependencies in the step 2, running the `setup.py` may trigger an event for fetching the online dependencies. + + ```shell + python setup.py build + ``` + +5. Rename the built module as `mpegCoder.so`, then you could import it in the same directory. If you have built FFMpeg by our script, you do not need any other dependencies when importing the libs. However, if not, you may need to download [the lib dependencies :package:](https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/releases/download/deps-3.0.0/so-linux-ffmpeg_4_4.tar.xz) and add the `lib` folder to your `LD_LIBRARY_PATH`: + + ```shell + mkdir -p /apps/ffmpeg-4.4 + cd /apps/ffmpeg-4.4 + wget -O- https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/releases/download/deps-3.0.0/so-linux-ffmpeg_4_4.tar.xz | tar xJ -C "." + echo "export LD_LIBRARY_PATH=/apps/ffmpeg-4.4/lib:\$LD_LIBRARY_PATH" >> ~/.bashrc + export LD_LIBRARY_PATH=/apps/ffmpeg-4.4/lib:$LD_LIBRARY_PATH + ``` + + If you want to run the `mpegCoder` with a different environment (which means you have compiled `mpegCoder` in another environment), you still need to setup the dependencies by this step. Running FFMpeg still requries `GLIBC>=2.29`, so you may need to add the dependency by + + ```shell + ln -sf /apps/ffmpeg-4.4/lib-fix/libm-2.31.so /lib/x86_64-linux-gnu/libm.so.6 + ``` + +## Update reports + +Has been moved to [:bookmark_tabs: CHANGELOG.md](./CHANGELOG.md) + +## Version of currently used FFmpeg library + +Current FFMpeg version is `4.4`. + +| Dependency | Version | +| :-------------: | :------------: | +| `libavcodec` | `58.134.100.0` | +| `libavformat` | `58.76.100.0` | +| `libavutil` | `56.70.100.0` | +| `libswresample` | `3.9.100.0` | +| `libswscale` | `5.9.100.0` | + +[git-master]:https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python "master (windows)" +[exp1]:https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/tree/example-client-check "check the client" +[exp2]:https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/tree/example-client-player "client with player" diff --git a/find_libpython/__init__.py b/find_libpython/__init__.py new file mode 100644 index 0000000..5143094 --- /dev/null +++ b/find_libpython/__init__.py @@ -0,0 +1,360 @@ +""" +Locate libpython associated with this Python executable. +""" + +# License +# +# Copyright 2018, Takafumi Arakaki +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +from __future__ import print_function, absolute_import + +from logging import getLogger +import ctypes.util +import functools +import os +import sys +import sysconfig + +from find_libpython._version import version as __version__ # noqa: F401 + +logger = getLogger("find_libpython") + +is_windows = os.name == "nt" +is_apple = sys.platform == "darwin" +is_msys = sysconfig.get_platform().startswith("msys") +is_mingw = sysconfig.get_platform().startswith("mingw") + +SHLIB_SUFFIX = sysconfig.get_config_var("SHLIB_SUFFIX") +if SHLIB_SUFFIX is None: + if is_windows: + SHLIB_SUFFIX = ".dll" + else: + SHLIB_SUFFIX = ".so" +if is_apple: + # sysconfig.get_config_var("SHLIB_SUFFIX") can be ".so" in macOS. + # Let's not use the value from sysconfig. + SHLIB_SUFFIX = ".dylib" + + +def library_name(name, suffix=SHLIB_SUFFIX, is_windows=is_windows): + """ + Convert a file basename `name` to a library name (no "lib" and ".so" etc.) + + >>> library_name("libpython3.7m.so") # doctest: +SKIP + 'python3.7m' + >>> library_name("libpython3.7m.so", suffix=".so", is_windows=False) + 'python3.7m' + >>> library_name("libpython3.7m.dylib", suffix=".dylib", is_windows=False) + 'python3.7m' + >>> library_name("python37.dll", suffix=".dll", is_windows=True) + 'python37' + """ + if not is_windows and name.startswith("lib"): + name = name[len("lib") :] + if suffix and name.endswith(suffix): + name = name[: -len(suffix)] + return name + + +def append_truthy(list, item): + if item: + list.append(item) + + +def uniquifying(items): + """ + Yield items while excluding the duplicates and preserving the order. + + >>> list(uniquifying([1, 2, 1, 2, 3])) + [1, 2, 3] + """ + seen = set() + for x in items: + if x not in seen: + yield x + seen.add(x) + + +def uniquified(func): + """ Wrap iterator returned from `func` by `uniquifying`. """ + + @functools.wraps(func) + def wrapper(*args, **kwds): + return uniquifying(func(*args, **kwds)) + + return wrapper + + +@uniquified +def candidate_names(suffix=SHLIB_SUFFIX): + """ + Iterate over candidate file names of libpython. + + Yields + ------ + name : str + Candidate name libpython. + """ + LDLIBRARY = sysconfig.get_config_var("LDLIBRARY") + if LDLIBRARY and os.path.splitext(LDLIBRARY)[1] == suffix: + yield LDLIBRARY + + LIBRARY = sysconfig.get_config_var("LIBRARY") + if LIBRARY and os.path.splitext(LIBRARY)[1] == suffix: + yield LIBRARY + + DLLLIBRARY = sysconfig.get_config_var("DLLLIBRARY") + if DLLLIBRARY: + yield DLLLIBRARY + + if is_mingw: + dlprefix = "lib" + elif is_windows or is_msys: + dlprefix = "" + else: + dlprefix = "lib" + + sysdata = dict( + v=sys.version_info, + # VERSION is X.Y in Linux/macOS and XY in Windows: + VERSION=( + sysconfig.get_config_var("VERSION") + or "{v.major}.{v.minor}".format(v=sys.version_info) + ), + ABIFLAGS=( + sysconfig.get_config_var("ABIFLAGS") + or sysconfig.get_config_var("abiflags") + or "" + ), + ) + + for stem in [ + "python{VERSION}{ABIFLAGS}".format(**sysdata), + "python{VERSION}".format(**sysdata), + "python{v.major}".format(**sysdata), + "python", + ]: + yield dlprefix + stem + suffix + + +@uniquified +def candidate_paths(suffix=SHLIB_SUFFIX): + """ + Iterate over candidate paths of libpython. + + Yields + ------ + path : str or None + Candidate path to libpython. The path may not be a fullpath + and may not exist. + """ + + # List candidates for directories in which libpython may exist + lib_dirs = [] + append_truthy(lib_dirs, sysconfig.get_config_var("LIBPL")) + append_truthy(lib_dirs, sysconfig.get_config_var("srcdir")) + append_truthy(lib_dirs, sysconfig.get_config_var("LIBDIR")) + + # LIBPL seems to be the right config_var to use. It is the one + # used in python-config when shared library is not enabled: + # https://github.com/python/cpython/blob/v3.7.0/Misc/python-config.in#L55-L57 + # + # But we try other places just in case. + + if is_windows or is_msys or is_mingw: + lib_dirs.append(os.path.join(os.path.dirname(sys.executable))) + else: + lib_dirs.append( + os.path.join(os.path.dirname(os.path.dirname(sys.executable)), "lib") + ) + + # For macOS: + append_truthy(lib_dirs, sysconfig.get_config_var("PYTHONFRAMEWORKPREFIX")) + + lib_dirs.append(sys.exec_prefix) + lib_dirs.append(os.path.join(sys.exec_prefix, "lib")) + + lib_basenames = list(candidate_names(suffix=suffix)) + + for directory in lib_dirs: + for basename in lib_basenames: + yield os.path.join(directory, basename) + + # In macOS and Windows, ctypes.util.find_library returns a full path: + for basename in lib_basenames: + yield ctypes.util.find_library(library_name(basename)) + + +# Possibly useful links: +# * https://packages.ubuntu.com/bionic/amd64/libpython3.6/filelist +# * https://github.com/Valloric/ycmd/issues/518 +# * https://github.com/Valloric/ycmd/pull/519 + + +def normalize_path(path, suffix=SHLIB_SUFFIX, is_apple=is_apple): + """ + Normalize shared library `path` to a real path. + + If `path` is not a full path, `None` is returned. If `path` does + not exists, append `SHLIB_SUFFIX` and check if it exists. + Finally, the path is canonicalized by following the symlinks. + + Parameters + ---------- + path : str ot None + A candidate path to a shared library. + """ + if not path: + return None + if not os.path.isabs(path): + return None + if os.path.exists(path): + return os.path.realpath(path) + if os.path.exists(path + suffix): + return os.path.realpath(path + suffix) + if is_apple: + return normalize_path(_remove_suffix_apple(path), suffix=".so", is_apple=False) + return None + + +def _remove_suffix_apple(path): + """ + Strip off .so or .dylib. + + >>> _remove_suffix_apple("libpython.so") + 'libpython' + >>> _remove_suffix_apple("libpython.dylib") + 'libpython' + >>> _remove_suffix_apple("libpython3.7") + 'libpython3.7' + """ + if path.endswith(".dylib"): + return path[: -len(".dylib")] + if path.endswith(".so"): + return path[: -len(".so")] + return path + + +@uniquified +def finding_libpython(): + """ + Iterate over existing libpython paths. + + The first item is likely to be the best one. + + Yields + ------ + path : str + Existing path to a libpython. + """ + logger.debug("is_windows = %s", is_windows) + logger.debug("is_apple = %s", is_apple) + logger.debug("is_mingw = %s", is_mingw) + logger.debug("is_msys = %s", is_msys) + for path in candidate_paths(): + logger.debug("Candidate: %s", path) + normalized = normalize_path(path) + if normalized: + logger.debug("Found: %s", normalized) + yield normalized + else: + logger.debug("Not found.") + + +def find_libpython(): + """ + Return a path (`str`) to libpython or `None` if not found. + + Parameters + ---------- + path : str or None + Existing path to the (supposedly) correct libpython. + """ + for path in finding_libpython(): + return os.path.realpath(path) + + +def print_all(items): + for x in items: + print(x) + + +def cli_find_libpython(cli_op, verbose): + import logging + + # Importing `logging` module here so that using `logging.debug` + # instead of `logger.debug` outside of this function becomes an + # error. + + if verbose: + logging.basicConfig(format="%(levelname)s %(message)s", level=logging.DEBUG) + + if cli_op == "list-all": + print_all(finding_libpython()) + elif cli_op == "candidate-names": + print_all(candidate_names()) + elif cli_op == "candidate-paths": + print_all(p for p in candidate_paths() if p and os.path.isabs(p)) + else: + path = find_libpython() + if path is None: + return 1 + print(path, end="") + + +def main(args=None): + import argparse + + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + "--verbose", "-v", action="store_true", help="Print debugging information." + ) + + parser.add_argument( + "--version", action="version", version="find_libpython {}".format(__version__) + ) + + group = parser.add_mutually_exclusive_group() + group.add_argument( + "--list-all", + action="store_const", + dest="cli_op", + const="list-all", + help="Print list of all paths found.", + ) + group.add_argument( + "--candidate-names", + action="store_const", + dest="cli_op", + const="candidate-names", + help="Print list of candidate names of libpython.", + ) + group.add_argument( + "--candidate-paths", + action="store_const", + dest="cli_op", + const="candidate-paths", + help="Print list of candidate paths of libpython.", + ) + + ns = parser.parse_args(args) + parser.exit(cli_find_libpython(**vars(ns))) diff --git a/find_libpython/__main__.py b/find_libpython/__main__.py new file mode 100644 index 0000000..bd24103 --- /dev/null +++ b/find_libpython/__main__.py @@ -0,0 +1,5 @@ +from find_libpython import main + + +if __name__ == "__main__": + main() diff --git a/find_libpython/_version.py b/find_libpython/_version.py new file mode 100644 index 0000000..9ade197 --- /dev/null +++ b/find_libpython/_version.py @@ -0,0 +1,5 @@ +# coding: utf-8 +# file generated by setuptools_scm +# don't change, don't track in version control +version = '0.2.0' +version_tuple = (0, 2, 0) diff --git a/mpegCoder-linux.code-workspace b/mpegCoder-linux.code-workspace new file mode 100644 index 0000000..a947773 --- /dev/null +++ b/mpegCoder-linux.code-workspace @@ -0,0 +1,19 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings": { + "python.linting.flake8Args": [ + "--ignore", "E501" + ], + "python.linting.pycodestyleArgs": [ + "--ignore", "E501" + ], + "python.linting.pylintArgs": [ + "-d", "C0301" + ], + "cSpell.enabled": true + } +} \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..6fbd19e --- /dev/null +++ b/setup.py @@ -0,0 +1,102 @@ +#!python +# -*- coding: UTF-8 -*- +''' +################################################################ +# Compiliation file for mpegCoder +# @ FFMpeg encoder and decoder. +# Yuchen Jin @ cainmagi@gmail.com +# Requirements: (Pay attention to version) +# python 3.3+ +# urllib3 1.26.2+ +# This script is used for compiling the core module of the +# mpegCoder. +################################################################ +''' + +import os +import re +import sysconfig +try: + from setuptools import setup, Extension +except ImportError: + from distutils.core import setup, Extension + +import find_libpython +import numpy as np + +try: + import webtools + HAS_WEBTOOLS=True +except ImportError: + HAS_WEBTOOLS=False + +BASE_SRC_DIR = 'MpegCoder' +FFMPEG_DIR = './dependencies' +PYTHON_INC_DIR = sysconfig.get_path('include') # /usr/include/python3.x +NUMPY_DIR = os.path.join(os.path.dirname(np.__file__), 'core') # '/usr/local/lib/python3.x/dist-packages/numpy/core' + +PYTHON_LIB_PATH = find_libpython.find_libpython() +PYTHON_LIB_DIR = os.path.dirname(PYTHON_LIB_PATH) # '/usr/lib/python3.x/config-3.xm-x86_64-linux-gnu' +PYTHON_LIB_NAME = re.search(R'(?:lib|)(python3\.(?:.+?))\..*?', os.path.basename(PYTHON_LIB_PATH)).groups(1)[0] +TARGET='mpegCoder' + +with open('README.md', 'r') as fh: + LONG_DESCRIPTION = fh.read() + +if (not os.path.isdir(os.path.join(FFMPEG_DIR, 'include'))) or (not os.path.isdir(os.path.join(FFMPEG_DIR, 'lib'))): + if HAS_WEBTOOLS: + print('The FFMpeg dependencies are not found. Fetch the files online...') + webtools.download_tarball('cainmagi', 'FFmpeg-Encoder-Decoder-for-Python', 'deps-3.0.0', 'dep-linux-ffmpeg_4_4.tar.xz', path=os.path.join('.', 'dependencies'), mode='auto', verbose=True, token='') + else: + raise FileNotFoundError('The required dependencies ("include" and "lib" directories) are not found in FFMPEG_DIR path ({0})'.format(FFMPEG_DIR)) + +module_mpegCoder = Extension( + name = TARGET, + language = 'c++', + define_macros = [('MAJOR_VERSION', '3'), + ('MINOR_VERSION', '1'), + ('BUILD_VERSION', '0')], + extra_compile_args = ['-std=c++11','-pthread'], + include_dirs = [PYTHON_INC_DIR, np.get_include(), '{0}/include'.format(FFMPEG_DIR), BASE_SRC_DIR], + #libraries = [PYTHON_LIB_NAME, 'avcodec', 'avdevice', 'avfilter', 'avformat', 'avutil', 'postproc', 'swresample', 'swscale', 'npymath'], + libraries = [PYTHON_LIB_NAME, 'avcodec', 'avformat', 'avutil', 'swresample', 'swscale', 'npymath'], + library_dirs = [PYTHON_LIB_DIR, '{0}/lib'.format(NUMPY_DIR), '{0}/lib'.format(FFMPEG_DIR)], + sources = [ + '{0}/MpegCoder.cpp'.format(BASE_SRC_DIR), + '{0}/MpegStreamer.cpp'.format(BASE_SRC_DIR), + '{0}/dllmain.cpp'.format(BASE_SRC_DIR) + ] +) + +setup( + name = 'mpegCoder', + version = '3.1.0', + description = 'A FFmpeg module which could provide a class for encoding, decoding, or streaming a video in any format.', + author = 'Yuchen Jin', + author_email = 'cainmagi@gmail.com', + url = 'https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python', + project_urls={ + 'Tracker': 'https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/issues', + }, + long_description=LONG_DESCRIPTION, + long_description_content_type='text/markdown', + classifiers=[ + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3 :: Only', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', + 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', + 'Operating System :: POSIX :: Linux', + 'Topic :: Software Development :: Libraries :: Python Modules' + ], + keywords=[ + 'python', 'h264', 'video', 'rtsp', 'ffmpeg', 'rtmp', 'encoder', 'numpy', 'python3', 'python3-library', 'ffmpeg-wrapper', 'video-stream', 'python-c-api', 'rtsp-push', 'rtmp-push', 'rtsp-player', 'rtmp-player', 'ffmpeg-encoder' + ], + python_requires='>=3.5', + license='GPLv3', + ext_modules = [module_mpegCoder] +) + diff --git a/webtools.py b/webtools.py new file mode 100644 index 0000000..f00e173 --- /dev/null +++ b/webtools.py @@ -0,0 +1,304 @@ +#!python +# -*- coding: UTF-8 -*- +''' +################################################################ +# WebTools +# @ FFMpeg encoder and decoder. +# Yuchen Jin @ cainmagi@gmail.com +# Requirements: (Pay attention to version) +# python 3.3+ +# urllib3 1.26.2+ +# Tools used for checking and downloading datasets. +# Inspired by: +# https://gist.github.com/devhero/8ae2229d9ea1a59003ced4587c9cb236 +# and https://gist.github.com/maxim/6e15aa45ba010ab030c4 +# This tool is picked from the other project, MDNC, see: +# https://github.com/cainmagi/MDNC +################################################################ +''' + +import os +import json +import tarfile +import urllib3 + +try: + from tqdm.tqdm import wrapattr +except ImportError: + import contextlib + + @contextlib.contextmanager + def wrapattr(req, mode=None, total=0, desc=None): + return req + +__all__ = [ + 'get_token', + 'download_tarball_link', 'download_tarball_public', 'download_tarball_private', 'download_tarball' +] + + +class _SafePoolManager(urllib3.PoolManager): + '''A wrapped urllib3.PoolManager with context supported. + This is a private class. Should not be used by users. + ''' + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, exc_traceback): + self.clear() + + +def get_token(token='', silent=False): + '''Automatically get the token, if the token is missing. + Arguments: + token: the given OAuth token. Only when this argument is unset, + the program will try to find a token from env. + silent: a flag. If set true, this tool would not ask for a token + when the token could not be found. + ''' + if not token: + token = os.environ.get('GITTOKEN', None) + if token is None: + token = os.environ.get('GITHUB_API_TOKEN', None) + if isinstance(token, str) and token != '': + token = token.split(':')[-1] + else: + if not silent: + print('data.webtools: A Github OAuth token is required for downloading the data in private repository. Please provide your OAuth token:') + token = input('Token:') + if not token: + print('data.webtools: Provide blank token. Try to download the tarball without token.') + print('data.webtools: Tips: specify the environment variable $GITTOKEN or $GITHUB_API_TOKEN could help you skip this step.') + else: + return '' + return token + + +def __get_tarball_mode(name, mode='auto'): + '''Detect the tarball compression mode by file name. + Arguments: + name: the file name with a file name extension. + mode: the mode name, should be '', 'gz, ''bz2', or 'xz'. If specified, + the compression mode would not be detected by file name. + ''' + name = os.path.split(name)[-1] + pos = name.find('?') + if pos > 0: + name = name[:name.find('?')] # Remove the HTML args. + if mode == 'auto': + if name.endswith('tar'): + mode = '' + elif name.endswith('tar.gz') or name.endswith('tar.gzip'): + mode = 'gz' + elif name.endswith('tar.bz2') or name.endswith('tar.bzip2'): + mode = 'bz2' + elif name.endswith('tar.xz'): + mode = 'xz' + if mode not in ('', 'gz', 'bz2', 'xz'): + raise TypeError('data.webtools: The file name to be downloaded should end with supported format. Now we supports: tar, tar.gz/tar.gzip, tar.bz2/tar.bzip2, tar.xz.') + return mode + + +def download_tarball_link(link, path='.', mode='auto', verbose=False): + '''Download an online tarball and extract it automatically. + The tarball is directed by the link. This tool would not work on + private github repository. + The tarball would be sent to pipeline and not get stored. + Now supports gz, bz2 or xz format. + Arguments: + link: the web link. + path: the extracted data root path. Should be a folder path. + mode: the mode of extraction. Could be 'gz', 'bz2', 'xz' or + 'auto'. + verbose: a flag, whether to show the downloaded size during + the web request. + ''' + mode = __get_tarball_mode(name=link, mode=mode) + os.makedirs(path, exist_ok=True) + # Initialize urllib3 + with _SafePoolManager(retries=urllib3.util.Retry(connect=5, read=2, redirect=5), + timeout=urllib3.util.Timeout(connect=5.0)) as http: + # Get the data. + git_header = { + 'User-Agent': 'cainmagi/webtools' + } + req = http.request(url=link, headers=git_header, method='GET', preload_content=False) + if req.status < 400: + if verbose: + file_name = os.path.split(link)[-1] + with wrapattr(req, 'read', total=0, desc='Get {0}'.format(file_name)) as req: + with tarfile.open(fileobj=req, mode='r|{0}'.format(mode)) as tar: + tar.extractall(path) + else: + with tarfile.open(fileobj=req, mode='r|{0}'.format(mode)) as tar: + tar.extractall(path) + else: + raise FileNotFoundError('data.webtools: Fail to get access to the tarball. Maybe the repo or the tag is not correct, or the repo is private, or the network is not available. The error message is: {0}'.format(req.read().decode('utf-8'))) + req.release_conn() + + +def __download_tarball_from_repo(user, repo, tag, asset, path='.', mode='auto', token=None, verbose=False): + '''Download an online tarball and extract it automatically. + A base tool. Should not used by users. Please use + download_tarball, or + download_tarball_public, or + download_tarball_private + for instead. + ''' + # Initialize the urllib3 + with _SafePoolManager(retries=urllib3.util.Retry(connect=5, read=2, redirect=5), + timeout=urllib3.util.Timeout(connect=5.0)) as http: + # Get the release info. + link_full = 'https://api.github.com/repos/{user}/{repo}/releases/tags/{tag}'.format(user=user, repo=repo, tag=tag) + git_header = { + 'Accept': 'application/vnd.github.v3+json', + 'User-Agent': 'cainmagi/webtools' + } + if token: + git_header['Authorization'] = 'token {token}'.format(token=token) + req = http.request(url=link_full, headers=git_header, method='GET', preload_content=False) + if req.status < 400: + info = json.loads(req.read().decode()) + link_assets = info['assets_url'] + else: + raise FileNotFoundError('data.webtools: Fail to get access to the release. Maybe the repo or the tag is not correct, or the authentication fails, or the network is not available. The error message is: {0}'.format(req.read().decode('utf-8'))) + req.release_conn() + # Get the assets info. + req = http.request(url=link_assets, headers=git_header, method='GET', preload_content=False) + if req.status < 400: + info = json.loads(req.read().decode()) + asset_info = next(filter(lambda aitem: aitem['name'] == asset, info), None) + if asset_info is None: + raise FileNotFoundError('data.webtools: Fail to locate the asset "{asset}" in the given release.'.format(asset=asset)) + link_asset = asset_info['url'] + else: + raise FileNotFoundError('data.webtools: Fail to get access to the release. Maybe the asset address is not correct. The error message is: {0}'.format(req.read().decode('utf-8'))) + req.release_conn() + # Download the data. + git_header = { + 'Accept': 'application/octet-stream', + 'User-Agent': 'cainmagi/webtools' + } + if token: + git_header['Authorization'] = 'token {token}'.format(token=token) + # req = http.request(method='GET', url=link_asset, headers=git_header) + req = http.request(url=link_asset, headers=git_header, method='GET', preload_content=False) + if req.status < 400: + if verbose: + with wrapattr(req, 'read', total=0, desc='Get {0}'.format(asset)) as req: + with tarfile.open(fileobj=req, mode='r|{0}'.format(mode)) as tar: + tar.extractall(path) + else: + with tarfile.open(fileobj=req, mode='r|{0}'.format(mode)) as tar: + tar.extractall(path) + else: + raise FileNotFoundError('data.webtools: Fail to get access to the asset. The error message is: {0}'.format(req.read().decode('utf-8'))) + req.release_conn() + + +def download_tarball_public(user, repo, tag, asset, path='.', mode='auto', verbose=False): + '''Download an online tarball and extract it automatically + (public). + This tool only supports public github repositories. This method + could be replaced by download_tarball_link(), but we do not + recommend to do that. + The tarball would be sent to pipeline and not get stored. + Now supports gz or xz format. + Arguments: + user: the github user name. + repo: the github repository name. + tag: the github release tag. + asset: the github asset (tarball) to be downloaded. + path: the extracted data root path. Should be a folder path. + mode: the mode of extraction. Could be 'gz', 'bz2', 'xz' or + 'auto'. + verbose: a flag, whether to show the downloaded size during + the web request. + ''' + mode = __get_tarball_mode(name=asset, mode=mode) + os.makedirs(path, exist_ok=True) + __download_tarball_from_repo(user=user, repo=repo, tag=tag, asset=asset, + path=path, mode=mode, token=None, verbose=verbose) + + +def download_tarball_private(user, repo, tag, asset, path='.', mode='auto', token=None, verbose=False): + '''Download an online tarball and extract it automatically + (private). + This tool should only be used for downloading assets from + private repositories. Although it could be also used for + public repositories, we do not recommend to use it in those + cases, because it would still require a token. + The tarball would be sent to pipeline and not get stored. + Now supports gz or xz format. + Arguments: + user: the github user name. + repo: the github repository name. + tag: the github release tag. + asset: the github asset (tarball) to be downloaded. + path: the extracted data root path. Should be a folder path. + mode: the mode of extraction. Could be 'gz', 'bz2', 'xz' or + 'auto'. + token: the token required for downloading the private asset. + verbose: a flag, whether to show the downloaded size during + the web request. + ''' + mode = __get_tarball_mode(name=asset, mode=mode) + os.makedirs(path, exist_ok=True) + token = get_token(token) + __download_tarball_from_repo(user=user, repo=repo, tag=tag, asset=asset, + path=path, mode=mode, token=token, verbose=verbose) + + +def download_tarball(user, repo, tag, asset, path='.', mode='auto', token=None, verbose=False): + '''Download an online tarball and extract it automatically. + This tool is used for downloading the assets from github + repositories. It would try to detect the data info in public + mode, and switch to private downloading mode when the Github + repository could not be accessed. + The tarball would be sent to pipeline and not get stored. + Now supports gz or xz format. + Arguments: + user: the github user name. + repo: the github repository name. + tag: the github release tag. + asset: the github asset (tarball) to be downloaded. + path: the extracted data root path. Should be a folder path. + mode: the mode of extraction. Could be 'gz', 'bz2', 'xz' or + 'auto'. + token: the token required for downloading the private asset, + when downloading public asses, this value would not + be used. + verbose: a flag, whether to show the downloaded size during + the web request. + ''' + mode = __get_tarball_mode(name=asset, mode=mode) + os.makedirs(path, exist_ok=True) + # Detect the repository infomation first. + is_public_mode = True + with _SafePoolManager(retries=urllib3.util.Retry(connect=5, read=2, redirect=5), + timeout=urllib3.util.Timeout(connect=5.0)) as http: + link_full = 'https://api.github.com/repos/{user}/{repo}/releases/tags/{tag}'.format(user=user, repo=repo, tag=tag) + git_header = { + 'Accept': 'application/vnd.github.v3+json', + 'User-Agent': 'cainmagi/webtools' + } + req = http.request(url=link_full, headers=git_header, method='GET', preload_content=False) + if req.status < 400: + is_public_mode = is_public_mode + else: + is_public_mode = False + req.release_conn() + if is_public_mode: + __download_tarball_from_repo(user=user, repo=repo, tag=tag, asset=asset, + path=path, mode=mode, token=None, verbose=verbose) + else: + token = get_token(token) + __download_tarball_from_repo(user=user, repo=repo, tag=tag, asset=asset, + path=path, mode=mode, token=token, verbose=verbose) + + +if __name__ == '__main__': + + # token = get_token(token='') + print('Get ffmpeg dependencies...') + download_tarball('cainmagi', 'FFmpeg-Encoder-Decoder-for-Python', 'deps-3.0.0', 'dep-linux-ffmpeg_4_4.tar.xz', path=os.path.join('.', 'dependencies'), mode='auto', verbose=True, token='') From 4aaefd6cf143689f8489846fe9bffa8531816755 Mon Sep 17 00:00:00 2001 From: cainmagi Date: Mon, 19 Jul 2021 07:51:53 -0500 Subject: [PATCH 02/15] 3.1.0 1. Add more features for `setup.py`. 2. Remove `snprintf.cpp`. --- MpegCoder/snprintf.cpp | 75 ------------------------------------------ README.md | 1 + requirements.txt | 3 ++ 3 files changed, 4 insertions(+), 75 deletions(-) delete mode 100644 MpegCoder/snprintf.cpp create mode 100644 requirements.txt diff --git a/MpegCoder/snprintf.cpp b/MpegCoder/snprintf.cpp deleted file mode 100644 index 6e53ecf..0000000 --- a/MpegCoder/snprintf.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/* - * C99-compatible snprintf() and vsnprintf() implementations - * Copyright (c) 2012 Ronald S. Bultje - * - * This file is part of FFmpeg. - * - * FFmpeg 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. - * - * FFmpeg 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 FFmpeg; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -extern "C" -{ - #include - #include - #include - #include -} - -#include "compat/va_copy.h" -#include "libavutil/error.h" -#include "compat/msvcrt/snprintf.h" - -#if defined(__MINGW32__) -#define EOVERFLOW EFBIG -#endif - -extern "C" -{ - int avpriv_snprintf(char *s, size_t n, const char *fmt, ...) { - va_list ap; - int ret; - - va_start(ap, fmt); - ret = avpriv_vsnprintf(s, n, fmt, ap); - va_end(ap); - - return ret; - } - - int avpriv_vsnprintf(char *s, size_t n, const char *fmt, va_list ap) { - int ret; - va_list ap_copy; - - if (n == 0) - return _vscprintf(fmt, ap); - else if (n > INT_MAX) - return AVERROR(EOVERFLOW); - - /* we use n - 1 here because if the buffer is not big enough, the MS - * runtime libraries don't add a terminating zero at the end. MSDN - * recommends to provide _snprintf/_vsnprintf() a buffer size that - * is one less than the actual buffer, and zero it before calling - * _snprintf/_vsnprintf() to workaround this problem. - * See http://msdn.microsoft.com/en-us/library/1kt27hek(v=vs.80).aspx */ - memset(s, 0, n); - va_copy(ap_copy, ap); - ret = _vsnprintf_s(s, n - 1, INT_MAX, fmt, ap_copy); - va_end(ap_copy); - if (ret == -1) - ret = _vscprintf(fmt, ap); - - return ret; - } -} \ No newline at end of file diff --git a/README.md b/README.md index 512ca11..b375fbf 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ The following instructions are used for building the project on Windows with Vis * The dependencies could be also downloaded by the automatic script, you just need to run ```shell + python -m pip install -r requirements.txt python webtools.py ``` diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..34b1a75 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +urllib3>=1.26.6 +tqdm>=4.50.0 +setuptools>=57.4.0 From 767b49a7eee20918ac71f5abede408162479cd36 Mon Sep 17 00:00:00 2001 From: cainmagi Date: Mon, 19 Jul 2021 07:59:58 -0500 Subject: [PATCH 03/15] 3.1.0 1. Add a script for using apt. 2. Modify the readme file. --- README.md | 22 +++++++++++++++++----- apt-requirements.sh | 29 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 apt-requirements.sh diff --git a/README.md b/README.md index b375fbf..13a3edf 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,20 @@ The following instructions are used for building the project on Windows with Vis git clone --single-branch -b master-linux https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python.git ``` -2. Get the shared version of the Linux FFMpeg. We support three different approaches for this step: +2. Install the pre-requisite packages. The following script could be used by ubuntu or debian. If users are using other Linux distribution, they should install `gcc` and `g++` by themselves. + + ```shell + chmod +rwx ./apt-requirements.sh + ./apt-requirements.sh + ``` + + We strongly suggest that users should also install the python dependencies (optional): + + ```shell + python -m pip install -r requirements.txt + ``` + +3. Get the shared version of the Linux FFMpeg. We support three different approaches for this step: * Build the shared libs of FFMpeg from the source codes by yourself. We provide [a compiling script :page_with_curl:](https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/blob/deps/install-ffmpeg-4_4.sh) in the deps branch. You could download and build it by the following commands: ```shell @@ -51,26 +64,25 @@ The following instructions are used for building the project on Windows with Vis * The dependencies could be also downloaded by the automatic script, you just need to run ```shell - python -m pip install -r requirements.txt python webtools.py ``` This script requires users to install `urllib3`. The `tqdm` is also recommended to be installed. -3. Building `mpegCoder` requires `GLIBC>=2.29`. This requirement is not satisfied in some cases. However, if you have built FFMpeg by our script, the requirement would be fulfilled (i.e. you could skip this step). If users are using our pre-built dependencies, users may need to solve this problem by +4. Building `mpegCoder` requires `GLIBC>=2.29`. This requirement is not satisfied in some cases. However, if you have built FFMpeg by our script, the requirement would be fulfilled (i.e. you could skip this step). If users are using our pre-built dependencies, users may need to solve this problem by ```shell cd FFmpeg-Encoder-Decoder-for-Python ln -sf ./dependencies/lib-fix/libm-2.31.so /lib/x86_64-linux-gnu/libm.so.6 ``` -4. Build `mpegCoder` by running the following script. The built file would be stored in `./build`. If you has not fetched the dependencies in the step 2, running the `setup.py` may trigger an event for fetching the online dependencies. +5. Build `mpegCoder` by running the following script. The built file would be stored in `./build`. If you has not fetched the dependencies in the step 2, running the `setup.py` may trigger an event for fetching the online dependencies. ```shell python setup.py build ``` -5. Rename the built module as `mpegCoder.so`, then you could import it in the same directory. If you have built FFMpeg by our script, you do not need any other dependencies when importing the libs. However, if not, you may need to download [the lib dependencies :package:](https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/releases/download/deps-3.0.0/so-linux-ffmpeg_4_4.tar.xz) and add the `lib` folder to your `LD_LIBRARY_PATH`: +6. Rename the built module as `mpegCoder.so`, then you could import it in the same directory. If you have built FFMpeg by our script, you do not need any other dependencies when importing the libs. However, if not, you may need to download [the lib dependencies :package:](https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/releases/download/deps-3.0.0/so-linux-ffmpeg_4_4.tar.xz) and add the `lib` folder to your `LD_LIBRARY_PATH`: ```shell mkdir -p /apps/ffmpeg-4.4 diff --git a/apt-requirements.sh b/apt-requirements.sh new file mode 100644 index 0000000..8e8c5c4 --- /dev/null +++ b/apt-requirements.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +# Make bashline configurations. +set -e +RESET='\033[0m' +COLOR='\033[1;32m' +COLOR_ERR='\033[1;31m' + +function msg { + echo -e "${COLOR}$(date): $1${RESET}" +} + +function msg_err { + echo -e "${COLOR}$(date): $1${RESET}" +} + +function fail { + msg_err "Error : $?" + exit 1 +} + +function mcd { + mkdir -p "$1" || fail + cd "$1" || fail +} + +msg "Install gcc and g++." +apt-get update --qq || fail +apt-get -y install gcc g++ || fail From 3267b221a5fe2bc9a65d8daa3f0095cf469f3a23 Mon Sep 17 00:00:00 2001 From: cainmagi Date: Mon, 19 Jul 2021 08:02:27 -0500 Subject: [PATCH 04/15] 3.1.0 Fix a bug of the apt script. --- apt-requirements.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apt-requirements.sh b/apt-requirements.sh index 8e8c5c4..d78500e 100644 --- a/apt-requirements.sh +++ b/apt-requirements.sh @@ -25,5 +25,5 @@ function mcd { } msg "Install gcc and g++." -apt-get update --qq || fail +apt-get update -qq || fail apt-get -y install gcc g++ || fail From db30d6e2ae071405a3ffa0fe11652059f58f5459 Mon Sep 17 00:00:00 2001 From: cainmagi Date: Mon, 19 Jul 2021 08:13:35 -0500 Subject: [PATCH 05/15] 3.1.0 Fix a bug of the webtools script. --- webtools.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/webtools.py b/webtools.py index f00e173..5952091 100644 --- a/webtools.py +++ b/webtools.py @@ -23,13 +23,14 @@ import urllib3 try: - from tqdm.tqdm import wrapattr + from tqdm import tqdm + wrapattr=tqdm.wrapattr except ImportError: import contextlib @contextlib.contextmanager def wrapattr(req, mode=None, total=0, desc=None): - return req + yield req __all__ = [ 'get_token', From f0340ce34da08e48eee2f9a682e58bc12b76cb13 Mon Sep 17 00:00:00 2001 From: cainmagi Date: Mon, 19 Jul 2021 08:30:57 -0500 Subject: [PATCH 06/15] 3.1.0 1. Update the installation scripts. 2. Update the readme file. --- README.md | 13 +++---------- apt-requirements.sh | 2 +- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 13a3edf..b004f6c 100644 --- a/README.md +++ b/README.md @@ -69,20 +69,13 @@ The following instructions are used for building the project on Windows with Vis This script requires users to install `urllib3`. The `tqdm` is also recommended to be installed. -4. Building `mpegCoder` requires `GLIBC>=2.29`. This requirement is not satisfied in some cases. However, if you have built FFMpeg by our script, the requirement would be fulfilled (i.e. you could skip this step). If users are using our pre-built dependencies, users may need to solve this problem by - - ```shell - cd FFmpeg-Encoder-Decoder-for-Python - ln -sf ./dependencies/lib-fix/libm-2.31.so /lib/x86_64-linux-gnu/libm.so.6 - ``` - -5. Build `mpegCoder` by running the following script. The built file would be stored in `./build`. If you has not fetched the dependencies in the step 2, running the `setup.py` may trigger an event for fetching the online dependencies. +4. Build `mpegCoder` by running the following script. The built file would be stored in `./build`. If you has not fetched the dependencies in the step 2, running the `setup.py` may trigger an event for fetching the online dependencies. ```shell python setup.py build ``` -6. Rename the built module as `mpegCoder.so`, then you could import it in the same directory. If you have built FFMpeg by our script, you do not need any other dependencies when importing the libs. However, if not, you may need to download [the lib dependencies :package:](https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/releases/download/deps-3.0.0/so-linux-ffmpeg_4_4.tar.xz) and add the `lib` folder to your `LD_LIBRARY_PATH`: +5. Rename the built module as `mpegCoder.so`, then you could import it in the same directory. If you have built FFMpeg by our script, you do not need any other dependencies when importing the libs. However, if not, you may need to download [the lib dependencies :package:](https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/releases/download/deps-3.0.0/so-linux-ffmpeg_4_4.tar.xz) and add the `lib` folder to your `LD_LIBRARY_PATH`: ```shell mkdir -p /apps/ffmpeg-4.4 @@ -92,7 +85,7 @@ The following instructions are used for building the project on Windows with Vis export LD_LIBRARY_PATH=/apps/ffmpeg-4.4/lib:$LD_LIBRARY_PATH ``` - If you want to run the `mpegCoder` with a different environment (which means you have compiled `mpegCoder` in another environment), you still need to setup the dependencies by this step. Running FFMpeg still requries `GLIBC>=2.29`, so you may need to add the dependency by +6. Running `mpegCoder` requires `GLIBC>=2.29`. This requirement is not satisfied in some cases. However, if you have built FFMpeg by our script, the requirement would be fulfilled (i.e. you could skip this step). If users are using our pre-built dependencies, users may need to solve this problem by ```shell ln -sf /apps/ffmpeg-4.4/lib-fix/libm-2.31.so /lib/x86_64-linux-gnu/libm.so.6 diff --git a/apt-requirements.sh b/apt-requirements.sh index d78500e..bca3de8 100644 --- a/apt-requirements.sh +++ b/apt-requirements.sh @@ -26,4 +26,4 @@ function mcd { msg "Install gcc and g++." apt-get update -qq || fail -apt-get -y install gcc g++ || fail +apt-get -y install gcc g++ wget tar xz-utils || fail From fffcc94d7f851cd6950ffa9f1c17fb70acf46fae Mon Sep 17 00:00:00 2001 From: cainmagi Date: Mon, 19 Jul 2021 08:32:33 -0500 Subject: [PATCH 07/15] 3.1.0 Update the installation script. --- apt-requirements.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apt-requirements.sh b/apt-requirements.sh index bca3de8..16f0e71 100644 --- a/apt-requirements.sh +++ b/apt-requirements.sh @@ -24,6 +24,6 @@ function mcd { cd "$1" || fail } -msg "Install gcc and g++." +msg "Install gcc, g++ and other dependencies." apt-get update -qq || fail apt-get -y install gcc g++ wget tar xz-utils || fail From 5dc9b8d9e74cf73c9606ab9f9b457c0d005bb8d0 Mon Sep 17 00:00:00 2001 From: cainmagi Date: Mon, 19 Jul 2021 08:36:38 -0500 Subject: [PATCH 08/15] 3.1.0 Update the pip dependencies. --- requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 34b1a75..cb726c7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ urllib3>=1.26.6 tqdm>=4.50.0 -setuptools>=57.4.0 +setuptools>=50.3.2 +numpy>=1.18.5 From 9e01f28baa59cc1ebdc69949d3e081db6d98ec14 Mon Sep 17 00:00:00 2001 From: cainmagi Date: Tue, 20 Jul 2021 17:06:41 -0500 Subject: [PATCH 09/15] 3.1.0 Fix a bug caused by the constructor `MpegServer()`. --- CHANGELOG.md | 4 +++- MpegCoder/MpegPyd.h | 5 +++-- MpegCoder/MpegStreamer.cpp | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35630f0..7a6e06c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,9 @@ 3. Support `nthread` option for `MpegDecoder`, `MpegEncoder`, `MpegClient` and `MpegServer`. -4. Fix typos in docstrings. +4. Fix a bug caused by the constructor `MpegServer()`. + +5. Fix typos in docstrings. ### V3.0.0 update report: diff --git a/MpegCoder/MpegPyd.h b/MpegCoder/MpegPyd.h index 56840b7..30a4336 100644 --- a/MpegCoder/MpegPyd.h +++ b/MpegCoder/MpegPyd.h @@ -217,10 +217,11 @@ V3.1.0 update report: 2. Support http, ftp, sftp streams for MpegServer. 3. Support "nthread" option for MpegDecoder, MpegEncoder, MpegClient and MpegServer. - 4. Fix typos in docstrings. + 4. Fix a bug caused by the constructor MpegServer(). + 5. Fix typos in docstrings. V3.0.0 update report: 1. Fix a severe memory leaking bugs when using AVPacket. - 2. Fix a bug caused by using `MpegClient.terminate()` when a video is closed + 2. Fix a bug caused by using MpegClient.terminate() when a video is closed by the server. 3. Support the MpegServer. This class is used for serving the online video streams. diff --git a/MpegCoder/MpegStreamer.cpp b/MpegCoder/MpegStreamer.cpp index a78468c..a7844f9 100644 --- a/MpegCoder/MpegStreamer.cpp +++ b/MpegCoder/MpegStreamer.cpp @@ -945,7 +945,7 @@ cmpc::CMpegServer::CMpegServer(void) : bitRate(1024), width(100), height(100), timeBase(_setAVRational(1, 25)), frameRate(_setAVRational(25, 1)), \ GOPSize(10), MaxBFrame(1), PStreamContex({ 0 }), PFormatCtx(nullptr), PswsCtx(nullptr), RGBbuffer(nullptr), \ Ppacket(nullptr), __have_video(false), __enable_header(false), widthSrc(0), heightSrc(0), __frameRGB(nullptr), \ - __start_time(0), __cur_time(0), time_base_q(_setAVRational(1, AV_TIME_BASE)), nthread(nthread) { + __start_time(0), __cur_time(0), time_base_q(_setAVRational(1, AV_TIME_BASE)), nthread(0) { videoPath.clear(); __formatName.clear(); codecName.clear(); From db3115794c06ef20934927c911c85e1ea92ffb06 Mon Sep 17 00:00:00 2001 From: cainmagi Date: Wed, 21 Jul 2021 04:49:14 +0000 Subject: [PATCH 10/15] 3.1.0 Clean up all `gcc` warnings of the source codes. --- .gitignore | 1 + CHANGELOG.md | 4 +- MpegCoder/MpegBase.cpp | 99 +++++++++++++++ MpegCoder/MpegBase.h | 52 ++++---- MpegCoder/MpegCoder.cpp | 216 +++++++++++++++++++------------- MpegCoder/MpegCoder.h | 5 +- MpegCoder/MpegPyd.h | 117 ++++++++++++----- MpegCoder/MpegStreamer.cpp | 221 +++++++++++++++++++-------------- MpegCoder/MpegStreamer.h | 7 +- apt-requirements.sh | 0 mpegCoder-linux.code-workspace | 34 ++--- setup.py | 1 + 12 files changed, 495 insertions(+), 262 deletions(-) create mode 100644 MpegCoder/MpegBase.cpp mode change 100644 => 100755 apt-requirements.sh diff --git a/.gitignore b/.gitignore index 1c2bcbb..952eb75 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Others include/* lib/* +/dependencies/* .vs/* .vscode/* *.pdb diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a6e06c..ebcacbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,9 @@ 4. Fix a bug caused by the constructor `MpegServer()`. -5. Fix typos in docstrings. +5. Clean up all `gcc` warnings of the source codes. + +6. Fix typos in docstrings. ### V3.0.0 update report: diff --git a/MpegCoder/MpegBase.cpp b/MpegCoder/MpegBase.cpp new file mode 100644 index 0000000..c2e44bc --- /dev/null +++ b/MpegCoder/MpegBase.cpp @@ -0,0 +1,99 @@ +#include "MpegBase.h" + +// Global functions. +const string cmpc::av_make_error_string2_cpp(int errnum) { + char errbuf[AV_ERROR_MAX_STRING_SIZE]; + av_strerror(errnum, errbuf, AV_ERROR_MAX_STRING_SIZE); + string strerrbuf = errbuf; + return strerrbuf; +} + +const string cmpc::av_ts_make_string_cpp(int64_t ts) { + char tsstrbuf[AV_TS_MAX_STRING_SIZE]; + av_ts_make_string(tsstrbuf, ts); + string strtsstrbuf = tsstrbuf; + return strtsstrbuf; +} + +const string cmpc::av_ts_make_time_string_cpp(int64_t ts, AVRational* tb) { + char tsstrbuf[AV_TS_MAX_STRING_SIZE]; + av_ts_make_time_string(tsstrbuf, ts, tb); + string strtsstrbuf = tsstrbuf; + return strtsstrbuf; +} + +// CharList implementation. +cmpc::CharList::CharList(void) : data() { +} + +cmpc::CharList::CharList(const std::vector& args) : data() { + set(args); +} + +cmpc::CharList::CharList(const std::vector&& args) noexcept : + data(args) { +} + +cmpc::CharList::~CharList(void) { + clear(); +} + +cmpc::CharList::CharList(const CharList& ref) : data() { + set(ref.data); +} + +cmpc::CharList& cmpc::CharList::operator=(const CharList& ref) { + if (this != &ref) { + set(ref.data); + } + return *this; +} + +cmpc::CharList::CharList(CharList&& ref) noexcept : + data(std::move(ref.data)) { +} + +cmpc::CharList& cmpc::CharList::operator=(CharList&& ref) noexcept { + if (this != &ref) { + set(std::move(ref.data)); + } + return *this; +} + +cmpc::CharList& cmpc::CharList::operator=(const std::vector& args) { + set(args); + return *this; +} + +cmpc::CharList& cmpc::CharList::operator=(std::vector&& args) noexcept { + set(args); + return *this; +} + +void cmpc::CharList::set(const std::vector& args) { + data.clear(); + for (auto it = args.begin(); it != args.end(); ++it) { + string new_str(*it); + data.push_back(new_str); + } +} + +void cmpc::CharList::set(std::vector&& args) noexcept { + data = args; +} + +void cmpc::CharList::clear() { + data.clear(); +} + +std::shared_ptr cmpc::CharList::c_str() { + std::shared_ptr pointer(new const char* [data.size() + 1], std::default_delete()); + auto p_cur = pointer.get(); + for (auto it = data.begin(); it != data.end(); ++it) { + *p_cur = it->c_str(); + p_cur++; + } + *p_cur = nullptr; + return pointer; +} + diff --git a/MpegCoder/MpegBase.h b/MpegCoder/MpegBase.h index 5becc29..70e82ef 100644 --- a/MpegCoder/MpegBase.h +++ b/MpegCoder/MpegBase.h @@ -26,6 +26,8 @@ #include #include #include +#include +#include #include #include #include @@ -55,30 +57,15 @@ namespace cmpc { #ifdef __cplusplus namespace cmpc { - static const string av_make_error_string2(int errnum) { - char errbuf[AV_ERROR_MAX_STRING_SIZE]; - av_strerror(errnum, errbuf, AV_ERROR_MAX_STRING_SIZE); - string strerrbuf = errbuf; - return strerrbuf; - } + const string av_make_error_string2_cpp(int errnum); #undef av_err2str - #define av_err2str(errnum) av_make_error_string2(errnum).c_str() - static const string av_ts_make_string_cpp(int64_t ts) { - char tsstrbuf[AV_TS_MAX_STRING_SIZE]; - av_ts_make_string(tsstrbuf, ts); - string strtsstrbuf = tsstrbuf; - return strtsstrbuf; - } + #define av_err2str(errnum) av_make_error_string2_cpp(errnum) + const string av_ts_make_string_cpp(int64_t ts); #undef av_ts2str - #define av_ts2str(ts) av_ts_make_string_cpp(ts).c_str() - static const string av_ts_make_time_string_cpp(int64_t ts, AVRational* tb) { - char tsstrbuf[AV_TS_MAX_STRING_SIZE]; - av_ts_make_time_string(tsstrbuf, ts, tb); - string strtsstrbuf = tsstrbuf; - return strtsstrbuf; - } + #define av_ts2str(ts) av_ts_make_string_cpp(ts) + const string av_ts_make_time_string_cpp(int64_t ts, AVRational* tb); #undef av_ts2timestr - #define av_ts2timestr(ts, tb) av_ts_make_time_string_cpp(ts, tb).c_str() + #define av_ts2timestr(ts, tb) av_ts_make_time_string_cpp(ts, tb) } #endif // __cplusplus @@ -96,6 +83,27 @@ namespace cmpc { struct SwsContext* sws_ctx; } OutputStream; + + // A wrapper of the char *[] + class CharList { + public: + CharList(void); // Constructor. + CharList(const std::vector& args); // Copy constructor (string ver). + CharList(const std::vector&& args) noexcept; // Move constructor (string ver). + ~CharList(void); // 3-5 law. Destructor. + CharList(const CharList& ref); // Copy constructor. + CharList& operator=(const CharList& ref); // Copy assignment operator. + CharList(CharList&& ref) noexcept; // Move constructor. + CharList& operator=(CharList&& ref) noexcept; // Move assignment operator. + CharList& operator=(const std::vector& args); // Copy assignment operator (string ver). + CharList& operator=(std::vector&& args) noexcept; // Move assignment operator (string ver). + void set(const std::vector& args); // Set strings as data. + void set(std::vector&& args) noexcept; // Set strings as data (move). + void clear(); // clear all data. + std::shared_ptr c_str(); // Equivalent conversion for char ** + private: + std::vector data; + }; } // compatibility with newer API @@ -104,4 +112,4 @@ namespace cmpc { #define av_frame_free avcodec_free_frame #endif -#endif \ No newline at end of file +#endif diff --git a/MpegCoder/MpegCoder.cpp b/MpegCoder/MpegCoder.cpp index 79ef157..7e3b673 100644 --- a/MpegCoder/MpegCoder.cpp +++ b/MpegCoder/MpegCoder.cpp @@ -10,19 +10,17 @@ int8_t cmpc::__dumpControl = 1; // Constructors. -cmpc::CMpegDecoder::CMpegDecoder(void) - : PFormatCtx(nullptr), PCodecCtx(nullptr), width(0), height(0), PVideoStream(nullptr), \ - PVideoStreamIDX(-1), PVideoFrameCount(0), _duration(0), _predictFrameNum(0), RGBbuffer(nullptr), \ - PswsCtx(nullptr), currentGOPTSM(0), nthread(0), EndofGOP(false), widthDst(0), heightDst(0), \ - PPixelFormat(STREAM_PIX_FMT) { - videoPath.clear(); - _str_codec.clear(); - +cmpc::CMpegDecoder::CMpegDecoder(void): + videoPath(), width(0), height(0), widthDst(0), heightDst(0), + PPixelFormat(STREAM_PIX_FMT), PFormatCtx(nullptr), PCodecCtx(nullptr), PVideoStream(nullptr), + PVideoStreamIDX(-1), PVideoFrameCount(0), RGBbuffer(nullptr), PswsCtx(nullptr), + _str_codec(), _duration(0), _predictFrameNum(0), currentGOPTSM(0), EndofGOP(false), + nthread(0), refcount(1) { /* Enable or disable frame reference counting. You are not supposed to support * both paths in your application but pick the one most appropriate to your * needs. Look for the use of refcount in this example to see what are the * differences of API usage between them. */ - refcount = 1; + // refcount = 1; } void cmpc::CMpegDecoder::meta_protected_clear(void) { @@ -73,28 +71,55 @@ cmpc::CMpegDecoder::~CMpegDecoder() { clear(); } -cmpc::CMpegDecoder::CMpegDecoder(const CMpegDecoder &ref) { - videoPath.assign(ref.videoPath); +cmpc::CMpegDecoder::CMpegDecoder(const CMpegDecoder &ref): + videoPath(ref.videoPath), width(0), height(0), widthDst(ref.widthDst), heightDst(ref.heightDst), + PPixelFormat(ref.PPixelFormat), PFormatCtx(nullptr), PCodecCtx(nullptr), PVideoStream(nullptr), + PVideoStreamIDX(-1), PVideoFrameCount(0), RGBbuffer(nullptr), PswsCtx(nullptr), + _str_codec(), _duration(0), _predictFrameNum(0), currentGOPTSM(0), EndofGOP(false), + nthread(ref.nthread), refcount(ref.refcount) { if (!FFmpegSetup()) { clear(); } } cmpc::CMpegDecoder& cmpc::CMpegDecoder::operator=(const CMpegDecoder &ref) { - videoPath.assign(ref.videoPath); - if (!FFmpegSetup()) { - clear(); + if (this != &ref) { + videoPath.assign(ref.videoPath); + width = 0; + height = 0; + widthDst = ref.widthDst; + heightDst = ref.heightDst; + PPixelFormat = ref.PPixelFormat; + PFormatCtx = nullptr; + PCodecCtx = nullptr; + PVideoStream = nullptr; + PVideoStreamIDX = -1; + PVideoFrameCount = 0; + RGBbuffer = nullptr; + PswsCtx = nullptr; + _str_codec.clear(); + _duration = 0.0; + _predictFrameNum = 0; + currentGOPTSM = 0; + EndofGOP = false; + nthread = ref.nthread; + refcount = ref.refcount; + if (!FFmpegSetup()) { + clear(); + } } return *this; } -cmpc::CMpegDecoder::CMpegDecoder(CMpegDecoder &&ref) noexcept - : PFormatCtx(ref.PFormatCtx), PCodecCtx(ref.PCodecCtx), width(ref.width), height(ref.height), \ - PVideoStream(ref.PVideoStream), PVideoStreamIDX(ref.PVideoStreamIDX), refcount(ref.refcount), \ - PVideoFrameCount(ref.PVideoFrameCount), _str_codec(ref._str_codec), _duration(ref._duration), \ - _predictFrameNum(ref._predictFrameNum), RGBbuffer(ref.RGBbuffer), PPixelFormat(ref.PPixelFormat) \ - , PswsCtx(ref.PswsCtx), currentGOPTSM(ref.currentGOPTSM), EndofGOP(ref.EndofGOP), \ - widthDst(ref.widthDst), heightDst(ref.heightDst), nthread(ref.nthread) { +cmpc::CMpegDecoder::CMpegDecoder(CMpegDecoder &&ref) noexcept: + videoPath(std::move(ref.videoPath)), width(ref.width), height(ref.height), + widthDst(ref.widthDst), heightDst(ref.heightDst), PPixelFormat(ref.PPixelFormat), + PFormatCtx(ref.PFormatCtx), PCodecCtx(ref.PCodecCtx), PVideoStream(ref.PVideoStream), + PVideoStreamIDX(ref.PVideoStreamIDX), PVideoFrameCount(ref.PVideoFrameCount), + RGBbuffer(ref.RGBbuffer), PswsCtx(ref.PswsCtx), _str_codec(std::move(ref._str_codec)), + _duration(ref._duration), _predictFrameNum(ref._predictFrameNum), + currentGOPTSM(ref.currentGOPTSM), EndofGOP(ref.EndofGOP), + nthread(ref.nthread), refcount(ref.refcount) { ref.PFormatCtx = nullptr; ref.PCodecCtx = nullptr; ref.PVideoStream = nullptr; @@ -103,32 +128,31 @@ cmpc::CMpegDecoder::CMpegDecoder(CMpegDecoder &&ref) noexcept cmpc::CMpegDecoder& cmpc::CMpegDecoder::operator=(CMpegDecoder &&ref) noexcept { if (this != &ref) { - clear(); + videoPath.assign(std::move(ref.videoPath)); width = ref.width; height = ref.height; widthDst = ref.widthDst; heightDst = ref.heightDst; - PVideoStreamIDX = ref.PVideoStreamIDX; - videoPath.assign(ref.videoPath); - _str_codec.assign(ref._str_codec); - _duration = ref._duration; - _predictFrameNum = ref._predictFrameNum; + PPixelFormat = ref.PPixelFormat; PFormatCtx = ref.PFormatCtx; PCodecCtx = ref.PCodecCtx; PVideoStream = ref.PVideoStream; + PVideoStreamIDX = ref.PVideoStreamIDX; PVideoFrameCount = ref.PVideoFrameCount; - currentGOPTSM = ref.currentGOPTSM; - EndofGOP = ref.EndofGOP; RGBbuffer = ref.RGBbuffer; - PPixelFormat = ref.PPixelFormat; PswsCtx = ref.PswsCtx; + _str_codec.assign(std::move(ref._str_codec)); + _duration = ref._duration; + _predictFrameNum = ref._predictFrameNum; + currentGOPTSM = ref.currentGOPTSM; + EndofGOP = ref.EndofGOP; nthread = ref.nthread; + refcount = ref.refcount; ref.PFormatCtx = nullptr; ref.PCodecCtx = nullptr; ref.PVideoStream = nullptr; ref.RGBbuffer = nullptr; ref.PswsCtx = nullptr; - refcount = ref.refcount; } return *this; } @@ -137,7 +161,6 @@ void cmpc::CMpegDecoder::resetPath(string inVideoPath) { videoPath.assign(inVideoPath); } -//以下是封装FFmpeg的C接口 void cmpc::CMpegDecoder::setGOPPosition(int64_t inpos) { currentGOPTSM = __FrameToPts(inpos); EndofGOP = false; @@ -206,7 +229,6 @@ bool cmpc::CMpegDecoder::FFmpegSetup(string inVideoPath) { bool cmpc::CMpegDecoder::FFmpegSetup() { // Open the video file, and search the correct codec. meta_protected_clear(); - int ret = 0; /* open input file, and allocate format context */ if (avformat_open_input(&PFormatCtx, videoPath.c_str(), nullptr, nullptr) < 0) { @@ -594,7 +616,7 @@ PyObject *cmpc::CMpegDecoder::_SaveFrame_castToPyFrameArray(uint8_t *data[], int attribute_deprecated PyObject *cmpc::CMpegDecoder::_SaveFrame_castToPyFrameArrayOld(uint8_t *data[], int fWidth, int fHeight) { - npy_intp dims[] = { static_cast(fHeight) * static_cast(fWidth) * 3 }; + npy_intp dims[] = { static_cast(fHeight) * static_cast(fWidth) * 3 }; PyObject *PyFrame = PyArray_SimpleNew(1, dims, NPY_UINT8); if (PyFrame == NULL) { Py_RETURN_NONE; @@ -901,9 +923,10 @@ ostream & cmpc::operator<<(ostream & out, cmpc::CMpegDecoder & self_class) { // Constructors following 3-5 law. cmpc::CMpegEncoder::CMpegEncoder(void): - bitRate(1024), width(100), height(100), timeBase(_setAVRational(1, 25)), frameRate(_setAVRational(25, 1)),\ - GOPSize(10), MaxBFrame(1), PStreamContex({ 0 }), PFormatCtx(nullptr), PswsCtx(nullptr), RGBbuffer(nullptr), \ - Ppacket(nullptr), __have_video(false), __enable_header(false), widthSrc(0), heightSrc(0), __frameRGB(nullptr), \ + videoPath(), codecName(), bitRate(1024), width(100), height(100), widthSrc(0), heightSrc(0), + timeBase(_setAVRational(1, 25)), frameRate(_setAVRational(25, 1)), GOPSize(10), MaxBFrame(1), + PStreamContex({ 0 }), PFormatCtx(nullptr), Ppacket(nullptr), PswsCtx(nullptr), + __frameRGB(nullptr), RGBbuffer(nullptr), __have_video(false), __enable_header(false), nthread(0) { videoPath.clear(); codecName.clear(); @@ -928,78 +951,95 @@ void cmpc::CMpegEncoder::clear(void) { __enable_header = false; } -void cmpc::CMpegEncoder::__copyMetaData(const CMpegEncoder &ref) { - videoPath.assign(ref.videoPath); - codecName.assign(ref.codecName); - bitRate = ref.bitRate; - width = ref.width; - height = ref.height; - widthSrc = ref.widthSrc; - heightSrc = ref.heightSrc; - timeBase = ref.timeBase; - frameRate = ref.frameRate; - GOPSize = ref.GOPSize; - MaxBFrame = ref.MaxBFrame; - nthread = ref.nthread; -} - cmpc::CMpegEncoder::~CMpegEncoder(void) { clear(); } - -cmpc::CMpegEncoder::CMpegEncoder(const CMpegEncoder &ref) { - __copyMetaData(ref); +cmpc::CMpegEncoder::CMpegEncoder(const CMpegEncoder &ref): + videoPath(ref.videoPath), codecName(ref.codecName), bitRate(ref.bitRate), + width(ref.width), height(ref.height), widthSrc(ref.widthSrc), heightSrc(ref.heightSrc), + timeBase(ref.timeBase), frameRate(ref.frameRate), GOPSize(ref.GOPSize), MaxBFrame(ref.MaxBFrame), + PStreamContex({ 0 }), PFormatCtx(nullptr), Ppacket(nullptr), PswsCtx(nullptr), + __frameRGB(nullptr), RGBbuffer(nullptr), __have_video(false), __enable_header(false), + nthread(ref.nthread) { if (!FFmpegSetup()) { clear(); } } cmpc::CMpegEncoder& cmpc::CMpegEncoder::operator=(const CMpegEncoder &ref) { - __copyMetaData(ref); - if (!FFmpegSetup()) { - clear(); + if (this != &ref) { + videoPath.assign(ref.videoPath); + codecName.assign(ref.codecName); + bitRate = ref.bitRate; + width = ref.width; + height = ref.height; + widthSrc = ref.widthSrc; + heightSrc = ref.heightSrc; + timeBase = ref.timeBase; + frameRate = ref.frameRate; + GOPSize = ref.GOPSize; + MaxBFrame = ref.MaxBFrame; + PStreamContex = { 0 }; + PFormatCtx = nullptr; + Ppacket = nullptr; + PswsCtx = nullptr; + __frameRGB = nullptr; + RGBbuffer = nullptr; + __have_video = false; + __enable_header = false; + nthread = ref.nthread; + if (!FFmpegSetup()) { + clear(); + } } return *this; } cmpc::CMpegEncoder::CMpegEncoder(CMpegEncoder &&ref) noexcept: - bitRate(ref.bitRate), width(ref.width), height(ref.height), timeBase(ref.timeBase), frameRate(ref.frameRate), \ - GOPSize(ref.GOPSize), MaxBFrame(ref.MaxBFrame), PStreamContex(ref.PStreamContex), PswsCtx(ref.PswsCtx), \ - RGBbuffer(ref.RGBbuffer), Ppacket(ref.Ppacket), PFormatCtx(ref.PFormatCtx), __have_video(ref.__have_video), \ - __enable_header(ref.__enable_header), widthSrc(ref.widthSrc), heightSrc(ref.heightSrc), __frameRGB(ref.__frameRGB), \ - nthread(ref.nthread) { - videoPath.assign(ref.videoPath); - codecName.assign(ref.codecName); -} - -cmpc::CMpegEncoder& cmpc::CMpegEncoder::operator=(CMpegEncoder &&ref) noexcept { - videoPath.assign(ref.videoPath); - codecName.assign(ref.codecName); - bitRate = ref.bitRate; - width = ref.width; - height = ref.height; - widthSrc = ref.widthSrc; - heightSrc = ref.heightSrc; - timeBase = ref.timeBase; - frameRate = ref.frameRate; - GOPSize = ref.GOPSize; - nthread = ref.nthread; - MaxBFrame = ref.MaxBFrame; - PFormatCtx = ref.PFormatCtx; - PStreamContex = ref.PStreamContex; - PswsCtx = ref.PswsCtx; - RGBbuffer = ref.RGBbuffer; - Ppacket = ref.Ppacket; - __frameRGB = ref.__frameRGB; - __have_video = ref.__have_video; - __enable_header = ref.__enable_header; + videoPath(std::move(ref.videoPath)), codecName(std::move(ref.codecName)), bitRate(ref.bitRate), + width(ref.width), height(ref.height), widthSrc(ref.widthSrc), heightSrc(ref.heightSrc), + timeBase(ref.timeBase), frameRate(ref.frameRate), GOPSize(ref.GOPSize), MaxBFrame(ref.MaxBFrame), + PStreamContex(std::move(ref.PStreamContex)), PFormatCtx(ref.PFormatCtx), Ppacket(ref.Ppacket), + PswsCtx(ref.PswsCtx), __frameRGB(ref.__frameRGB), RGBbuffer(ref.RGBbuffer), + __have_video(ref.__have_video), __enable_header(ref.__enable_header), nthread(ref.nthread) { ref.PFormatCtx = nullptr; ref.PStreamContex = { 0 }; ref.PswsCtx = nullptr; ref.RGBbuffer = nullptr; ref.Ppacket = nullptr; ref.__frameRGB = nullptr; +} + +cmpc::CMpegEncoder& cmpc::CMpegEncoder::operator=(CMpegEncoder &&ref) noexcept { + if (this != &ref) { + videoPath.assign(std::move(ref.videoPath)); + codecName.assign(std::move(ref.codecName)); + bitRate = ref.bitRate; + width = ref.width; + height = ref.height; + widthSrc = ref.widthSrc; + heightSrc = ref.heightSrc; + timeBase = ref.timeBase; + frameRate = ref.frameRate; + GOPSize = ref.GOPSize; + nthread = ref.nthread; + MaxBFrame = ref.MaxBFrame; + PFormatCtx = ref.PFormatCtx; + PStreamContex = std::move(ref.PStreamContex); + PswsCtx = ref.PswsCtx; + RGBbuffer = ref.RGBbuffer; + Ppacket = ref.Ppacket; + __frameRGB = ref.__frameRGB; + __have_video = ref.__have_video; + __enable_header = ref.__enable_header; + ref.PFormatCtx = nullptr; + ref.PStreamContex = { 0 }; + ref.PswsCtx = nullptr; + ref.RGBbuffer = nullptr; + ref.Ppacket = nullptr; + ref.__frameRGB = nullptr; + } return *this; } diff --git a/MpegCoder/MpegCoder.h b/MpegCoder/MpegCoder.h index 7a1d35b..3ff3594 100644 --- a/MpegCoder/MpegCoder.h +++ b/MpegCoder/MpegCoder.h @@ -44,11 +44,11 @@ namespace cmpc { void setGOPPosition(double inpos); // Set the cuurent GOP position by the time. private: string videoPath; // The path of video stream to be decoded. - AVFormatContext *PFormatCtx; // Format context of the video. - AVCodecContext *PCodecCtx; // Codec context of the video. int width, height; // Width, height of the video. int widthDst, heightDst; // Target width, height of ExtractFrame(). enum AVPixelFormat PPixelFormat; // Enum object of the pixel format. + AVFormatContext *PFormatCtx; // Format context of the video. + AVCodecContext *PCodecCtx; // Codec context of the video. AVStream *PVideoStream; // Video stream. int PVideoStreamIDX; // The index of the video stream. @@ -128,7 +128,6 @@ namespace cmpc { AVFrame *__get_video_frame(PyArrayObject* PyFrame); int __avcodec_encode_video2(AVCodecContext *enc_ctx, AVPacket *pkt, AVFrame *frame); int __avcodec_encode_video2_flush(AVCodecContext *enc_ctx, AVPacket *pkt); - void __copyMetaData(const CMpegEncoder &ref); }; ostream& operator<<(ostream& out, CMpegDecoder& self_class); diff --git a/MpegCoder/MpegPyd.h b/MpegCoder/MpegPyd.h index 30a4336..1c6d806 100644 --- a/MpegCoder/MpegPyd.h +++ b/MpegCoder/MpegPyd.h @@ -22,7 +22,7 @@ PyObject *str2PyStr(string Str) { wchar_t* wszString = new wchar_t[wlen + 1]; mbstowcs(wszString, Str.c_str(), wlen); wszString[wlen] = 0; - PyObject* res = PyUnicode_FromUnicode((const Py_UNICODE*)wszString, wlen); + PyObject* res = PyUnicode_FromWideChar(wszString, wlen); delete[] wszString; return res; } @@ -131,7 +131,9 @@ PyMODINIT_FUNC PyFunc_Example(void);*/ static PyObject* C_MPC_Global(PyObject* Self, PyObject *args, PyObject *kwargs) { char dumpLevel = -1; - static char *kwlist[] = { "dumpLevel", nullptr }; + cmpc::CharList kwlist_str({ "dumpLevel" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|B", kwlist, &dumpLevel)) { PyErr_SetString(PyExc_TypeError, "Error.GlobalSettings: invalid keyword'"); return nullptr; @@ -214,11 +216,12 @@ Yuchen's Mpeg Coder - Readme ================================================================================ V3.1.0 update report: 1. Support str() type for all string arguments. - 2. Support http, ftp, sftp streams for MpegServer. + 2. Support http, ftp, sftp streams for MpegServer. 3. Support "nthread" option for MpegDecoder, MpegEncoder, MpegClient and MpegServer. - 4. Fix a bug caused by the constructor MpegServer(). - 5. Fix typos in docstrings. + 4. Fix a bug caused by the constructor MpegServer(). + 5. Clean up all gcc warnings of the source codes. + 6. Fix typos in docstrings. V3.0.0 update report: 1. Fix a severe memory leaking bugs when using AVPacket. 2. Fix a bug caused by using MpegClient.terminate() when a video is closed @@ -277,7 +280,9 @@ V1.0 update report: *****************************************************************************/ static int C_MPDC_init(C_MpegDecoder* Self, PyObject* args, PyObject *kwargs) { // Construct PyObject* vpath = nullptr; - static char *kwlist[] = { "videoPath", nullptr }; + cmpc::CharList kwlist_str({ "videoPath" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) { PyErr_SetString(PyExc_TypeError, "Error.Initialize: need 'videoPath(str)'" ); return -1; @@ -396,7 +401,9 @@ static PyObject* C_MPDC_Setup(C_MpegDecoder* Self, PyObject *args, PyObject *kwa * videoPath [str/bytes->str]: the video path to be decoded. */ PyObject* vpath = nullptr; - static char *kwlist[] = { "videoPath", nullptr }; + cmpc::CharList kwlist_str({ "videoPath" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) { PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoPath(str)'"); return nullptr; @@ -426,7 +433,9 @@ static PyObject* C_MPEC_Setup(C_MpegEncoder* Self, PyObject *args, PyObject *kwa * videoPath [str/bytes->str]: the video path to be encoded. */ PyObject* vpath = nullptr; - static char *kwlist[] = { "videoPath", nullptr }; + cmpc::CharList kwlist_str({ "videoPath" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) { PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoPath(str)'"); return nullptr; @@ -456,7 +465,9 @@ static PyObject* C_MPCT_Setup(C_MpegClient* Self, PyObject *args, PyObject *kwar * videoAddress [str/bytes->str]: the video path to be demuxed. */ PyObject* vpath = nullptr; - static char *kwlist[] = { "videoAddress", nullptr }; + cmpc::CharList kwlist_str({ "videoAddress" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) { PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoAddress(str)'"); return nullptr; @@ -486,7 +497,9 @@ static PyObject* C_MPSV_Setup(C_MpegServer* Self, PyObject* args, PyObject* kwar * videoAddress [str/bytes->str]: the video address to be served. */ PyObject* vpath = nullptr; - static char* kwlist[] = { "videoAddress", nullptr }; + cmpc::CharList kwlist_str({ "videoAddress" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) { PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoAddress(str)'"); return nullptr; @@ -516,7 +529,9 @@ static PyObject* C_MPDC_resetPath(C_MpegDecoder* Self, PyObject *args, PyObject * videoPath [str/bytes->str]: the video path to be decoded. */ PyObject* vpath = nullptr; - static char *kwlist[] = { "videoPath", nullptr }; + cmpc::CharList kwlist_str({ "videoPath" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) { PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoPath(str)'"); return nullptr; @@ -536,7 +551,9 @@ static PyObject* C_MPEC_resetPath(C_MpegEncoder* Self, PyObject* args, PyObject* * videoPath [str/bytes->str]: the video path to be encoded. */ PyObject* vpath = nullptr; - static char* kwlist[] = { "videoPath", nullptr }; + cmpc::CharList kwlist_str({ "videoPath" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) { PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoPath(str)'"); return nullptr; @@ -556,7 +573,9 @@ static PyObject* C_MPCT_resetPath(C_MpegClient* Self, PyObject *args, PyObject * * videoAddress [str/bytes->str]: the video path to be demuxed. */ PyObject* vpath = nullptr; - static char *kwlist[] = { "videoAddress", nullptr }; + cmpc::CharList kwlist_str({ "videoAddress" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) { PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoAddress(str)'"); return nullptr; @@ -576,7 +595,9 @@ static PyObject* C_MPSV_resetPath(C_MpegServer* Self, PyObject* args, PyObject* * videoAddress [str/bytes->str]: the video address to be served. */ PyObject* vpath = nullptr; - static char* kwlist[] = { "videoAddress", nullptr }; + cmpc::CharList kwlist_str({ "videoAddress" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) { PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoAddress(str)'"); return nullptr; @@ -632,7 +653,9 @@ static PyObject* C_MPDC_ExtractFrame(C_MpegDecoder* Self, PyObject *args, PyObje * frameNum [int->int64_t]: the number of extracted frames. */ int64_t framePos = 0, frameNum = 1; - static char *kwlist[] = { "framePos", "frameNum", nullptr }; + cmpc::CharList kwlist_str({ "framePos", "frameNum" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|LL", kwlist, &framePos, &frameNum)) { PyErr_SetString(PyExc_TypeError, "Error.ExtractFrame: need 'framePos(int)/frameNum(int)'"); return nullptr; @@ -661,7 +684,9 @@ static PyObject* C_MPDC_ExtractFrame_Time(C_MpegDecoder* Self, PyObject *args, P */ double timePos = 0; int64_t frameNum = 1; - static char *kwlist[] = { "timePos", "frameNum", nullptr }; + cmpc::CharList kwlist_str({ "timePos", "frameNum" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|dL", kwlist, &timePos, &frameNum)) { PyErr_SetString(PyExc_TypeError, "Error.ExtractFrame_Time: need 'timePos(float)/frameNum(int)'"); return nullptr; @@ -688,7 +713,9 @@ static PyObject* C_MPEC_EncodeFrame(C_MpegEncoder* Self, PyObject *args, PyObjec * PyArrayFrame [ndarray->PyArrayObject]: the frame to be encoded. */ PyObject *PyArrayFrame = nullptr; - static char *kwlist[] = { "PyArrayFrame", nullptr }; + cmpc::CharList kwlist_str({ "PyArrayFrame" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &PyArrayFrame)) { PyErr_SetString(PyExc_TypeError, "Error.EncodeFrame: need 'PyArrayFrame(ndarray)'"); return nullptr; @@ -705,7 +732,9 @@ static PyObject* C_MPSV_ServeFrame(C_MpegServer* Self, PyObject* args, PyObject* * PyArrayFrame [ndarray->PyArrayObject]: the frame to be encoded and served. */ PyObject* PyArrayFrame = nullptr; - static char* kwlist[] = { "PyArrayFrame", nullptr }; + cmpc::CharList kwlist_str({ "PyArrayFrame" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &PyArrayFrame)) { PyErr_SetString(PyExc_TypeError, "Error.EncodeFrame: need 'PyArrayFrame(ndarray)'"); return nullptr; @@ -722,7 +751,9 @@ static PyObject* C_MPSV_ServeFrameBlock(C_MpegServer* Self, PyObject* args, PyOb * PyArrayFrame [ndarray->PyArrayObject]: the frame to be encoded and served. */ PyObject* PyArrayFrame = nullptr; - static char* kwlist[] = { "PyArrayFrame", nullptr }; + cmpc::CharList kwlist_str({ "PyArrayFrame" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &PyArrayFrame)) { PyErr_SetString(PyExc_TypeError, "Error.EncodeFrame: need 'PyArrayFrame(ndarray)'"); return nullptr; @@ -740,7 +771,9 @@ static PyObject* C_MPCT_ExtractFrame(C_MpegClient* Self, PyObject *args, PyObjec * exceeded the size of the frame buffer. */ int64_t readSize = 0; - static char *kwlist[] = { "readSize", nullptr }; + cmpc::CharList kwlist_str({ "readSize" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|L", kwlist, &readSize)) { PyErr_SetString(PyExc_TypeError, "Error.ExtractFrame: need 'readSize(int)'"); return nullptr; @@ -763,7 +796,9 @@ static PyObject* C_MPDC_ExtractGOP(C_MpegDecoder* Self, PyObject *args, PyObject * framePos [int->int64_t]: the start position of the GOP to be extracted. */ int64_t framePos = -1; - static char *kwlist[] = { "framePos", nullptr }; + cmpc::CharList kwlist_str({ "framePos" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|L", kwlist, &framePos)) { PyErr_SetString(PyExc_TypeError, "Error.ExtractGOP: need 'framePos(int)'"); return nullptr; @@ -792,7 +827,9 @@ static PyObject* C_MPDC_ExtractGOP_Time(C_MpegDecoder* Self, PyObject *args, PyO * timePos [float->double]: the start position (time unit) of the GOP to be extracted. */ double timePos = -1; - static char *kwlist[] = { "timePos", nullptr }; + cmpc::CharList kwlist_str({ "timePos" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|d", kwlist, &timePos)) { PyErr_SetString(PyExc_TypeError, "Error.ExtractGOP_Time: need 'timePos(float)'"); return nullptr; @@ -823,7 +860,9 @@ static PyObject* C_MPDC_setGOPPosition(C_MpegDecoder* Self, PyObject *args, PyOb */ int64_t framePos = -1; double timePos = -1; - static char *kwlist[] = { "framePos", "timePos", nullptr }; + cmpc::CharList kwlist_str({ "framePos", "timePos" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ld", kwlist, &framePos, &timePos)) { PyErr_SetString(PyExc_TypeError, "Error.setGOPPosition: need 'framePos(int)'/'timePos(float)'"); return nullptr; @@ -848,7 +887,9 @@ static PyObject* C_MPDC_getParam(C_MpegDecoder* Self, PyObject *args, PyObject * * avgFrameRate [float] The average frame rate. */ PyObject* param = nullptr; - static char *kwlist[] = { "paramName", nullptr }; + cmpc::CharList kwlist_str({ "paramName" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, ¶m)) { PyErr_SetString(PyExc_TypeError, "Error.getParameter: need 'paramName(str)'" ); return nullptr; @@ -885,7 +926,9 @@ static PyObject* C_MPEC_getParam(C_MpegEncoder* Self, PyObject* args, PyObject* * frameRate: [float] The target frame rate. */ PyObject* param = nullptr; - static char* kwlist[] = { "paramName", nullptr }; + cmpc::CharList kwlist_str({ "paramName" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, ¶m)) { PyErr_SetString(PyExc_TypeError, "Error.getParameter: need 'paramName(str)'"); return nullptr; @@ -921,7 +964,9 @@ static PyObject* C_MPCT_getParam(C_MpegClient* Self, PyObject *args, PyObject *k * avgFrameRate [float] The average frame rate. */ PyObject* param = nullptr; - static char *kwlist[] = { "paramName", nullptr }; + cmpc::CharList kwlist_str({ "paramName" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, ¶m)) { PyErr_SetString(PyExc_TypeError, "Error.getParameter: need 'paramName(str)'"); return nullptr; @@ -961,7 +1006,9 @@ static PyObject* C_MPSV_getParam(C_MpegServer* Self, PyObject* args, PyObject* k * ptsAhead [int] The ahead time duration in the uit of time stamp. */ PyObject* param = nullptr; - static char* kwlist[] = { "paramName", nullptr }; + cmpc::CharList kwlist_str({ "paramName" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, ¶m)) { PyErr_SetString(PyExc_TypeError, "Error.getParameter: need 'paramName(str)'"); return nullptr; @@ -992,7 +1039,9 @@ static PyObject* C_MPDC_setParam(C_MpegDecoder* Self, PyObject *args, PyObject * int widthDst = 0; int heightDst = 0; int nthread = 0; - static char *kwlist[] = { "widthDst", "heightDst", "nthread", nullptr}; + cmpc::CharList kwlist_str({ "widthDst", "heightDst", "nthread" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii", kwlist, &widthDst, &heightDst, &nthread)) { PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'params'" ); return nullptr; @@ -1036,7 +1085,9 @@ static PyObject* C_MPEC_setParam(C_MpegEncoder* Self, PyObject *args, PyObject * int GOPSize = 0; int MaxBframe = -1; PyObject *frameRate = nullptr; - static char *kwlist[] = { "decoder", "configDict", "videoPath", "codecName", "nthread", "bitRate", "width", "height", "widthSrc", "heightSrc", "GOPSize", "maxBframe", "frameRate", nullptr}; + cmpc::CharList kwlist_str({ "decoder", "configDict", "videoPath", "codecName", "nthread", "bitRate", "width", "height", "widthSrc", "heightSrc", "GOPSize", "maxBframe", "frameRate" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOOOidiiiiiiO", kwlist, &decoder, &configDict, &videoPath, &codecName, &nthread, &bitRate, &width, &height, &widthSrc, &heightSrc, &GOPSize, &MaxBframe, &frameRate)) { PyErr_SetString(PyExc_TypeError, "Error.setParameter: need 'params'"); return nullptr; @@ -1128,7 +1179,9 @@ static PyObject* C_MPCT_setParam(C_MpegClient* Self, PyObject *args, PyObject *k int64_t cacheSize = 0; int64_t readSize = 0; PyObject *frameRate = nullptr; - static char *kwlist[] = { "widthDst", "heightDst", "cacheSize", "readSize", "dstFrameRate", "nthread", nullptr}; + cmpc::CharList kwlist_str({ "widthDst", "heightDst", "cacheSize", "readSize", "dstFrameRate", "nthread" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiLLOi", kwlist, &widthDst, &heightDst, &cacheSize, &readSize, &frameRate, &nthread)) { PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'params'"); return nullptr; @@ -1188,7 +1241,9 @@ static PyObject* C_MPSV_setParam(C_MpegServer* Self, PyObject* args, PyObject* k int MaxBframe = -1; int frameAhead = 0; PyObject* frameRate = nullptr; - static char* kwlist[] = { "decoder", "configDict", "videoAddress", "codecName", "nthread", "bitRate", "width", "height", "widthSrc", "heightSrc", "GOPSize", "maxBframe", "frameRate", "frameAhead", nullptr}; + cmpc::CharList kwlist_str({ "decoder", "configDict", "videoAddress", "codecName", "nthread", "bitRate", "width", "height", "widthSrc", "heightSrc", "GOPSize", "maxBframe", "frameRate", "frameAhead" }); + auto kwlist_ptr = kwlist_str.c_str(); + auto kwlist = (char **)(kwlist_ptr.get()); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOOOidiiiiiiOi", kwlist, &decoder, &configDict, &videoAddress, &codecName, &nthread, &bitRate, &width, &height, &widthSrc, &heightSrc, &GOPSize, &MaxBframe, &frameRate, &frameAhead)) { PyErr_SetString(PyExc_TypeError, "Error.setParameter: need 'params'"); return nullptr; diff --git a/MpegCoder/MpegStreamer.cpp b/MpegCoder/MpegStreamer.cpp index a7844f9..3e136b7 100644 --- a/MpegCoder/MpegStreamer.cpp +++ b/MpegCoder/MpegStreamer.cpp @@ -5,25 +5,28 @@ #include "MpegCoder.h" #include "MpegStreamer.h" -cmpc::CMpegClient::CMpegClient(void) : - width(0), height(0), widthDst(0), heightDst(0), PPixelFormat(AVPixelFormat::AV_PIX_FMT_NONE), PVideoStreamIDX(0), - PVideoFrameCount(0), cache_size(0), read_size(0), frameRate({ 0,0 }), reading(false), - _duration(0), _predictFrameNum(0), refcount(0), PFormatCtx(nullptr), PCodecCtx(nullptr), - PVideoStream(nullptr), frame(nullptr), PswsCtx(nullptr), buffer(), read_handle(), read_check(), - info_lock(), nthread(0) { +cmpc::CMpegClient::CMpegClient(void): + videoPath(), width(0), height(0), widthDst(0), heightDst(0), + PPixelFormat(AVPixelFormat::AV_PIX_FMT_NONE), PFormatCtx(nullptr), PCodecCtx(nullptr), + PVideoStream(nullptr), frame(nullptr), PVideoStreamIDX(0), PVideoFrameCount(0), + buffer(), PswsCtx(nullptr), cache_size(0), read_size(0), + frameRate({ 0,0 }), read_handle(), read_check(), info_lock(), reading(false), + _str_codec(), _duration(0), _predictFrameNum(0), nthread(0), refcount(1) { } cmpc::CMpegClient::~CMpegClient(void) { clear(); } cmpc::CMpegClient::CMpegClient(CMpegClient &&ref) noexcept: - width(ref.width), height(ref.height), widthDst(ref.widthDst), heightDst(ref.heightDst), - PPixelFormat(ref.PPixelFormat), PVideoStreamIDX(ref.PVideoStreamIDX), PVideoFrameCount(ref.PVideoFrameCount), - cache_size(ref.cache_size), read_size(ref.read_size), frameRate(ref.frameRate), reading(ref.reading), - _duration(ref._duration), _predictFrameNum(ref._predictFrameNum), refcount(ref.refcount), - PFormatCtx(ref.PFormatCtx), PCodecCtx(ref.PCodecCtx), PVideoStream(ref.PVideoStream), frame(ref.frame), - PswsCtx(ref.PswsCtx), buffer(std::move(ref.buffer)), nthread(ref.nthread), read_check(), - info_lock(){ - read_handle = std::move(ref.read_handle); + videoPath(std::move(ref.videoPath)), width(ref.width), height(ref.height), + widthDst(ref.widthDst), heightDst(ref.heightDst), + PPixelFormat(ref.PPixelFormat), PFormatCtx(ref.PFormatCtx), PCodecCtx(ref.PCodecCtx), + PVideoStream(ref.PVideoStream), frame(ref.frame), + PVideoStreamIDX(ref.PVideoStreamIDX), PVideoFrameCount(ref.PVideoFrameCount), + buffer(std::move(ref.buffer)), PswsCtx(ref.PswsCtx), + cache_size(ref.cache_size), read_size(ref.read_size), + frameRate(ref.frameRate), read_handle(std::move(std::thread())), read_check(), info_lock(), + reading(ref.reading), _str_codec(std::move(ref._str_codec)), _duration(ref._duration), + _predictFrameNum(ref._predictFrameNum), nthread(ref.nthread), refcount(ref.refcount) { ref.PFormatCtx = nullptr; ref.PCodecCtx = nullptr; ref.PVideoStream = nullptr; @@ -32,6 +35,7 @@ cmpc::CMpegClient::CMpegClient(CMpegClient &&ref) noexcept: } cmpc::CMpegClient& cmpc::CMpegClient::operator=(CMpegClient &&ref) noexcept { if (this != &ref) { + videoPath = std::move(ref.videoPath); width = ref.width; height = ref.height; widthDst = ref.widthDst; @@ -50,9 +54,9 @@ cmpc::CMpegClient& cmpc::CMpegClient::operator=(CMpegClient &&ref) noexcept { PCodecCtx = ref.PCodecCtx; PVideoStream = ref.PVideoStream; frame = ref.frame; - PswsCtx = ref.PswsCtx; + PswsCtx = ref.PswsCtx; buffer = std::move(ref.buffer); - read_handle = std::move(ref.read_handle); + read_handle = std::move(std::thread()); nthread = ref.nthread; ref.PFormatCtx = nullptr; ref.PCodecCtx = nullptr; @@ -60,6 +64,7 @@ cmpc::CMpegClient& cmpc::CMpegClient::operator=(CMpegClient &&ref) noexcept { ref.frame = nullptr; ref.PswsCtx = nullptr; } + return *this; } void cmpc::CMpegClient::meta_protected_clear(void) { @@ -717,9 +722,10 @@ ostream & cmpc::operator<<(ostream & out, cmpc::CMpegClient & self_class) { } cmpc::BufferList::BufferList(void): - _Buffer_pos(0), _Buffer_rpos(-1), _Buffer_size(0), dst_width(0), dst_height(0), _Buffer_capacity(0), - _Buffer_List(nullptr), __Read_size(0), next_pts(0), interval_pts(0), src_width(0), src_height(0), - frameRGB(nullptr){ + _Buffer_pos(0), _Buffer_rpos(-1), _Buffer_size(0), __Read_size(0), + next_pts(0), interval_pts(0), dst_width(0), dst_height(0), + src_width(0), src_height(0), _Buffer_capacity(0), + frameRGB(nullptr), _Buffer_List(nullptr) { } cmpc::BufferList::~BufferList(void) { if (_Buffer_List) { @@ -737,10 +743,11 @@ cmpc::BufferList::~BufferList(void) { } } cmpc::BufferList::BufferList(const BufferList &ref): - _Buffer_pos(ref._Buffer_pos), _Buffer_rpos(ref._Buffer_rpos), _Buffer_size(ref._Buffer_size), - dst_width(ref.dst_width), dst_height(ref.dst_height), _Buffer_capacity(ref._Buffer_capacity), + _Buffer_pos(ref._Buffer_pos), _Buffer_rpos(ref._Buffer_rpos), _Buffer_size(ref._Buffer_size), __Read_size(ref.__Read_size), next_pts(ref.next_pts), interval_pts(ref.interval_pts), - src_width(ref.src_width), src_height(ref.src_height), frameRGB(ref.frameRGB), _Buffer_List(nullptr){ + dst_width(ref.dst_width), dst_height(ref.dst_height), + src_width(ref.src_width), src_height(ref.src_height), + _Buffer_capacity(ref._Buffer_capacity), frameRGB(ref.frameRGB), _Buffer_List(nullptr) { if (!(frameRGB = av_frame_alloc())) { cerr << "Could Allocate Temp Frame (RGB)" << endl; return; @@ -788,10 +795,10 @@ cmpc::BufferList& cmpc::BufferList::operator=(const BufferList &ref) { } cmpc::BufferList::BufferList(BufferList &&ref) noexcept: _Buffer_pos(ref._Buffer_pos), _Buffer_rpos(ref._Buffer_rpos), _Buffer_size(ref._Buffer_size), - dst_width(ref.dst_width), dst_height(ref.dst_height), _Buffer_capacity(ref._Buffer_capacity), - _Buffer_List(ref._Buffer_List), __Read_size(ref.__Read_size), next_pts(ref.next_pts), - interval_pts(ref.interval_pts), src_width(ref.src_width), src_height(ref.src_height), - frameRGB(ref.frameRGB) { + __Read_size(ref.__Read_size), next_pts(ref.next_pts), interval_pts(ref.interval_pts), + dst_width(ref.dst_width), dst_height(ref.dst_height), + src_width(ref.src_width), src_height(ref.src_height), + _Buffer_capacity(ref._Buffer_capacity), frameRGB(ref.frameRGB), _Buffer_List(ref._Buffer_List) { ref._Buffer_List = nullptr; ref.frameRGB = nullptr; } @@ -862,7 +869,7 @@ void cmpc::BufferList::set(int64_t set_size, int width, int height, int widthDst _Buffer_capacity = av_image_get_buffer_size(AV_PIX_FMT_RGB24, dst_width, dst_height, 1); } void cmpc::BufferList::set_timer(AVRational targetFrameRate, AVRational timeBase) { - auto interval_pts = av_rescale(av_rescale(1, timeBase.den, timeBase.num), targetFrameRate.den, targetFrameRate.num); + interval_pts = av_rescale(av_rescale(1, timeBase.den, timeBase.num), targetFrameRate.den, targetFrameRate.num); } bool cmpc::BufferList::reset_memory() { if (!frameRGB) { @@ -942,13 +949,13 @@ PyObject * cmpc::BufferList::read() { // Constructors following 3-5 law. cmpc::CMpegServer::CMpegServer(void) : - bitRate(1024), width(100), height(100), timeBase(_setAVRational(1, 25)), frameRate(_setAVRational(25, 1)), \ - GOPSize(10), MaxBFrame(1), PStreamContex({ 0 }), PFormatCtx(nullptr), PswsCtx(nullptr), RGBbuffer(nullptr), \ - Ppacket(nullptr), __have_video(false), __enable_header(false), widthSrc(0), heightSrc(0), __frameRGB(nullptr), \ - __start_time(0), __cur_time(0), time_base_q(_setAVRational(1, AV_TIME_BASE)), nthread(0) { - videoPath.clear(); - __formatName.clear(); - codecName.clear(); + videoPath(), __formatName(), codecName(), bitRate(1024), + __start_time(0), __cur_time(0), width(100), height(100), widthSrc(0), heightSrc(0), + timeBase(_setAVRational(1, 25)), frameRate(_setAVRational(25, 1)), + time_base_q(_setAVRational(1, AV_TIME_BASE)), GOPSize(10), MaxBFrame(1), + PStreamContex({ 0 }), PFormatCtx(nullptr), Ppacket(nullptr), PswsCtx(nullptr), + __frameRGB(nullptr), RGBbuffer(nullptr), __have_video(false), __enable_header(false), + nthread(0) { __pts_ahead = av_rescale(av_rescale(20, timeBase.den, timeBase.num), frameRate.den, frameRate.num); } @@ -974,6 +981,7 @@ void cmpc::CMpegServer::meta_protected_clear(void) { heightSrc = protectHeightSrc; bitRate = protectBitRate; GOPSize = protectGOPSize; + MaxBFrame = protectMaxBFrame; timeBase = protectTimeBase; frameRate = protectFrameRate; __pts_ahead = protectPTSAhead; @@ -1006,90 +1014,111 @@ void cmpc::CMpegServer::clear(void) { __cur_time = 0; } -void cmpc::CMpegServer::__copyMetaData(const CMpegServer& ref) { - videoPath.assign(ref.videoPath); - __formatName.assign(ref.__formatName); - codecName.assign(ref.codecName); - bitRate = ref.bitRate; - width = ref.width; - height = ref.height; - widthSrc = ref.widthSrc; - heightSrc = ref.heightSrc; - timeBase = ref.timeBase; - frameRate = ref.frameRate; - GOPSize = ref.GOPSize; - MaxBFrame = ref.MaxBFrame; - __pts_ahead = ref.__pts_ahead; - nthread = ref.nthread; - __start_time = 0; - __cur_time = 0; - time_base_q = _setAVRational(1, AV_TIME_BASE); -} - cmpc::CMpegServer::~CMpegServer(void) { clear(); } -cmpc::CMpegServer::CMpegServer(const CMpegServer& ref) { - __copyMetaData(ref); +cmpc::CMpegServer::CMpegServer(const CMpegServer& ref): + videoPath(ref.videoPath), __formatName(ref.__formatName), codecName(ref.codecName), + bitRate(ref.bitRate), __pts_ahead(ref.__pts_ahead), __start_time(0), __cur_time(0), + width(ref.width), height(ref.height), widthSrc(ref.widthSrc), heightSrc(ref.heightSrc), + timeBase(ref.timeBase), frameRate(ref.frameRate), + time_base_q(_setAVRational(1, AV_TIME_BASE)), GOPSize(ref.GOPSize), MaxBFrame(ref.MaxBFrame), + PStreamContex({ 0 }), PFormatCtx(nullptr), Ppacket(nullptr), PswsCtx(nullptr), + __frameRGB(nullptr), RGBbuffer(nullptr), __have_video(false), __enable_header(false), + nthread(ref.nthread) { if (!FFmpegSetup()) { clear(); } } cmpc::CMpegServer& cmpc::CMpegServer::operator=(const CMpegServer& ref) { - __copyMetaData(ref); - if (!FFmpegSetup()) { - clear(); + if (this != &ref) { + videoPath = ref.videoPath; + __formatName = ref.__formatName; + codecName = ref.codecName; + bitRate = ref.bitRate; + __pts_ahead = ref.__pts_ahead; + __start_time = 0; + __cur_time = 0; + width = ref.width; + height = ref.height; + widthSrc = ref.widthSrc; + heightSrc = ref.heightSrc; + timeBase = ref.timeBase; + frameRate = ref.frameRate; + time_base_q = _setAVRational(1, AV_TIME_BASE); + GOPSize = ref.GOPSize; + MaxBFrame = ref.MaxBFrame; + PStreamContex = { 0 }; + PFormatCtx = nullptr; + Ppacket = nullptr; + PswsCtx = nullptr; + __frameRGB = nullptr; + RGBbuffer = nullptr; + __have_video = false; + __enable_header = false; + nthread = ref.nthread; + if (!FFmpegSetup()) { + clear(); + } } return *this; } cmpc::CMpegServer::CMpegServer(CMpegServer&& ref) noexcept : - bitRate(ref.bitRate), width(ref.width), height(ref.height), timeBase(ref.timeBase), frameRate(ref.frameRate), \ - GOPSize(ref.GOPSize), MaxBFrame(ref.MaxBFrame), PStreamContex(ref.PStreamContex), PswsCtx(ref.PswsCtx), \ - RGBbuffer(ref.RGBbuffer), Ppacket(ref.Ppacket), PFormatCtx(ref.PFormatCtx), __have_video(ref.__have_video), \ - __enable_header(ref.__enable_header), widthSrc(ref.widthSrc), heightSrc(ref.heightSrc), __frameRGB(ref.__frameRGB), \ - __pts_ahead(ref.__pts_ahead), __start_time(ref.__start_time), nthread(ref.nthread), __cur_time(ref.__cur_time), \ - time_base_q(ref.time_base_q) { - videoPath.assign(std::move(ref.videoPath)); - codecName.assign(std::move(ref.codecName)); - __formatName.assign(std::move(ref.__formatName)); -} - -cmpc::CMpegServer& cmpc::CMpegServer::operator=(CMpegServer&& ref) noexcept { - videoPath.assign(std::move(ref.videoPath)); - __formatName.assign(std::move(ref.__formatName)); - codecName.assign(std::move(ref.codecName)); - bitRate = ref.bitRate; - width = ref.width; - height = ref.height; - widthSrc = ref.widthSrc; - heightSrc = ref.heightSrc; - timeBase = ref.timeBase; - frameRate = ref.frameRate; - time_base_q = ref.time_base_q; - GOPSize = ref.GOPSize; - MaxBFrame = ref.MaxBFrame; - __pts_ahead = ref.__pts_ahead; - __start_time = ref.__start_time; - __cur_time = ref.__cur_time; - PFormatCtx = ref.PFormatCtx; - PStreamContex = ref.PStreamContex; - PswsCtx = ref.PswsCtx; - RGBbuffer = ref.RGBbuffer; - Ppacket = ref.Ppacket; - nthread = ref.nthread; - __frameRGB = ref.__frameRGB; - __have_video = ref.__have_video; - __enable_header = ref.__enable_header; + videoPath(std::move(ref.videoPath)), __formatName(std::move(ref.__formatName)), + codecName(std::move(ref.codecName)), bitRate(ref.bitRate), __pts_ahead(ref.__pts_ahead), + __start_time(ref.__start_time), __cur_time(ref.__cur_time), + width(ref.width), height(ref.height), widthSrc(ref.widthSrc), heightSrc(ref.heightSrc), + timeBase(ref.timeBase), frameRate(ref.frameRate), time_base_q(ref.time_base_q), + GOPSize(ref.GOPSize), MaxBFrame(ref.MaxBFrame), PStreamContex(std::move(ref.PStreamContex)), + PFormatCtx(ref.PFormatCtx), Ppacket(ref.Ppacket), PswsCtx(ref.PswsCtx), + __frameRGB(ref.__frameRGB), RGBbuffer(ref.RGBbuffer), + __have_video(ref.__have_video), __enable_header(ref.__enable_header), nthread(ref.nthread) { ref.PFormatCtx = nullptr; ref.PStreamContex = { 0 }; ref.PswsCtx = nullptr; ref.RGBbuffer = nullptr; ref.Ppacket = nullptr; ref.__frameRGB = nullptr; +} + +cmpc::CMpegServer& cmpc::CMpegServer::operator=(CMpegServer&& ref) noexcept { + if (this != &ref) { + videoPath.assign(std::move(ref.videoPath)); + __formatName.assign(std::move(ref.__formatName)); + codecName.assign(std::move(ref.codecName)); + bitRate = ref.bitRate; + width = ref.width; + height = ref.height; + widthSrc = ref.widthSrc; + heightSrc = ref.heightSrc; + timeBase = ref.timeBase; + frameRate = ref.frameRate; + time_base_q = ref.time_base_q; + GOPSize = ref.GOPSize; + MaxBFrame = ref.MaxBFrame; + __pts_ahead = ref.__pts_ahead; + __start_time = ref.__start_time; + __cur_time = ref.__cur_time; + PFormatCtx = ref.PFormatCtx; + PStreamContex = std::move(ref.PStreamContex); + PswsCtx = ref.PswsCtx; + RGBbuffer = ref.RGBbuffer; + Ppacket = ref.Ppacket; + nthread = ref.nthread; + __frameRGB = ref.__frameRGB; + __have_video = ref.__have_video; + __enable_header = ref.__enable_header; + ref.PFormatCtx = nullptr; + ref.PStreamContex = { 0 }; + ref.PswsCtx = nullptr; + ref.RGBbuffer = nullptr; + ref.Ppacket = nullptr; + ref.__frameRGB = nullptr; + } return *this; } diff --git a/MpegCoder/MpegStreamer.h b/MpegCoder/MpegStreamer.h index c09afac..2e6606d 100644 --- a/MpegCoder/MpegStreamer.h +++ b/MpegCoder/MpegStreamer.h @@ -72,11 +72,11 @@ namespace cmpc { PyObject * ExtractFrame(); // Extract frames. The number is configured in the class properties. private: string videoPath; // The path (URL) of the online video stream. - AVFormatContext *PFormatCtx; // Format context of the video. - AVCodecContext *PCodecCtx; // Codec context of the video. int width, height; // Width, height of the video. int widthDst, heightDst; // Target width, height of ExtractFrame(). enum AVPixelFormat PPixelFormat; // Enum object of the pixel format. + AVFormatContext *PFormatCtx; // Format context of the video. + AVCodecContext *PCodecCtx; // Codec context of the video. AVStream *PVideoStream; // Video stream. AVFrame *frame; @@ -169,11 +169,10 @@ namespace cmpc { AVFrame* __get_video_frame(PyArrayObject* PyFrame); int __avcodec_encode_video2(AVCodecContext* enc_ctx, AVPacket* pkt, AVFrame* frame); int __avcodec_encode_video2_flush(AVCodecContext* enc_ctx, AVPacket* pkt); - void __copyMetaData(const CMpegServer& ref); }; ostream& operator<<(ostream& out, CMpegClient& self_class); ostream& operator<<(ostream& out, CMpegServer& self_class); } -#endif \ No newline at end of file +#endif diff --git a/apt-requirements.sh b/apt-requirements.sh old mode 100644 new mode 100755 diff --git a/mpegCoder-linux.code-workspace b/mpegCoder-linux.code-workspace index a947773..3e7001e 100644 --- a/mpegCoder-linux.code-workspace +++ b/mpegCoder-linux.code-workspace @@ -1,19 +1,19 @@ { - "folders": [ - { - "path": "." - } - ], - "settings": { - "python.linting.flake8Args": [ - "--ignore", "E501" - ], - "python.linting.pycodestyleArgs": [ - "--ignore", "E501" - ], - "python.linting.pylintArgs": [ - "-d", "C0301" - ], - "cSpell.enabled": true - } + "folders": [ + { + "path": "." + } + ], + "settings": { + "python.linting.flake8Args": [ + "--ignore", "E501" + ], + "python.linting.pycodestyleArgs": [ + "--ignore", "E501" + ], + "python.linting.pylintArgs": [ + "-d", "C0301" + ], + "cSpell.enabled": true + } } \ No newline at end of file diff --git a/setup.py b/setup.py index 6fbd19e..77dfa51 100644 --- a/setup.py +++ b/setup.py @@ -62,6 +62,7 @@ libraries = [PYTHON_LIB_NAME, 'avcodec', 'avformat', 'avutil', 'swresample', 'swscale', 'npymath'], library_dirs = [PYTHON_LIB_DIR, '{0}/lib'.format(NUMPY_DIR), '{0}/lib'.format(FFMPEG_DIR)], sources = [ + '{0}/MpegBase.cpp'.format(BASE_SRC_DIR), '{0}/MpegCoder.cpp'.format(BASE_SRC_DIR), '{0}/MpegStreamer.cpp'.format(BASE_SRC_DIR), '{0}/dllmain.cpp'.format(BASE_SRC_DIR) From 3760c5f62d6f25f4b9b740dce615ab69f56fbcb1 Mon Sep 17 00:00:00 2001 From: cainmagi Date: Fri, 8 Apr 2022 03:01:39 -0500 Subject: [PATCH 11/15] 3.2.0 1. Upgrade to `FFMpeg 5.0` Version. 2. Fix the const assignment bug caused by the codec configuration method. 3. Upgrade the dependencies of FFMpeg to the newest versions (issue #4). --- .gitignore | 1 + CHANGELOG.md | 8 ++++++++ MpegCoder/MpegBase.h | 3 ++- MpegCoder/MpegCoder.cpp | 42 ++++++++++++++++++++++---------------- MpegCoder/MpegCoder.h | 4 ++-- MpegCoder/MpegPyd.h | 4 ++++ MpegCoder/MpegStreamer.cpp | 38 +++++++++++++++++++--------------- MpegCoder/MpegStreamer.h | 4 ++-- README.md | 40 ++++++++++++++++++------------------ requirements.txt | 2 +- setup.py | 10 ++++----- 11 files changed, 91 insertions(+), 65 deletions(-) diff --git a/.gitignore b/.gitignore index 952eb75..334f6e4 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ lib/* *.lastbuildstate unsuccessfulbuild /MpegCoder/x64/ +/autobuild*.sh # Prerequisites *.d diff --git a/CHANGELOG.md b/CHANGELOG.md index ebcacbb..781b55b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ ## Update Report +### V3.2.0 update report: + +1. Upgrade to `FFMpeg 5.0` Version. + +2. Fix the const assignment bug caused by the codec configuration method. + +3. Upgrade the dependencies of FFMpeg to the newest versions (issue [#4 :exclamation:](https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/issues/4)). + ### V3.1.0 update report: 1. Support `str()` type for all string arguments. diff --git a/MpegCoder/MpegBase.h b/MpegCoder/MpegBase.h index 70e82ef..74e43bc 100644 --- a/MpegCoder/MpegBase.h +++ b/MpegCoder/MpegBase.h @@ -11,8 +11,9 @@ #define FFMPG3_4 #define FFMPG4_0 #define FFMPG4_4 +#define FFMPG5_0 -#define MPEGCODER_CURRENT_VERSION "3.1.0" +#define MPEGCODER_CURRENT_VERSION "3.2.0" #define STREAM_PIX_FMT AVPixelFormat::AV_PIX_FMT_YUV420P /* default pix_fmt */ diff --git a/MpegCoder/MpegCoder.cpp b/MpegCoder/MpegCoder.cpp index 7e3b673..1e89ad9 100644 --- a/MpegCoder/MpegCoder.cpp +++ b/MpegCoder/MpegCoder.cpp @@ -1087,43 +1087,45 @@ int cmpc::CMpegEncoder::__write_frame(){ } /* Add an output stream. */ -bool cmpc::CMpegEncoder::__add_stream(AVCodec **codec){ +const cmpc::AVCodec* cmpc::CMpegEncoder::__add_stream() { /* find the encoder */ AVCodecID codec_id; auto srcwidth = widthSrc > 0 ? widthSrc : width; auto srcheight = heightSrc > 0 ? heightSrc : height; - *codec = avcodec_find_encoder_by_name(codecName.c_str()); - if (!(*codec)) { + auto const_codec = avcodec_find_encoder_by_name(codecName.c_str()); + const AVCodec* codec; + if (!(const_codec)) { codec_id = PFormatCtx->oformat->video_codec; - cerr << "Could not find encoder "<< codecName <<", use " << avcodec_get_name(codec_id) << " as an alternative." << endl; - *codec = avcodec_find_encoder(codec_id); + cerr << "Could not find encoder " << codecName << ", use " << avcodec_get_name(codec_id) << " as an alternative." << endl; + codec = avcodec_find_encoder(codec_id); } else { - codec_id = (*codec)->id; - PFormatCtx->oformat->video_codec = codec_id; + codec = const_codec; + codec_id = codec->id; } - if (!(*codec)) { + + if (!codec) { cerr << "Could not find encoder for '" << avcodec_get_name(codec_id) << "'" << endl; - return false; + return nullptr; } PStreamContex.st = avformat_new_stream(PFormatCtx, nullptr); if (!PStreamContex.st) { cerr << "Could not allocate stream" << endl; - return false; + return nullptr; } PStreamContex.st->id = PFormatCtx->nb_streams - 1; - auto c = avcodec_alloc_context3(*codec); + auto c = avcodec_alloc_context3(codec); if (!c) { cerr << "Could not alloc an encoding context" << endl; - return false; + return nullptr; } if (nthread > 0) { c->thread_count = nthread; } PStreamContex.enc = c; - switch ((*codec)->type) { + switch (codec->type) { case AVMediaType::AVMEDIA_TYPE_VIDEO: c->codec_id = codec_id; @@ -1141,7 +1143,7 @@ bool cmpc::CMpegEncoder::__add_stream(AVCodec **codec){ //cout << "(" << frameRate.num << ", " << frameRate.den << ")" << endl; //PStreamContex.st->r_frame_rate c->time_base = timeBase; - + //PStreamContex.st->frame c->framerate = frameRate; @@ -1201,7 +1203,7 @@ bool cmpc::CMpegEncoder::__add_stream(AVCodec **codec){ /* Some formats want stream headers to be separate. */ if (PFormatCtx->oformat->flags & AVFMT_GLOBALHEADER) c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; - return true; + return codec; } /* video output */ @@ -1221,7 +1223,7 @@ cmpc::AVFrame* cmpc::CMpegEncoder::__alloc_picture(enum AVPixelFormat pix_fmt, i return picture; } -bool cmpc::CMpegEncoder::__open_video(AVCodec *codec, AVDictionary *opt_arg){ +bool cmpc::CMpegEncoder::__open_video(const AVCodec* codec, const AVDictionary* opt_arg) { int ret; auto c = PStreamContex.enc; AVDictionary *opt = nullptr; @@ -1804,7 +1806,7 @@ PyObject* cmpc::CMpegEncoder::getParameter() { } bool cmpc::CMpegEncoder::FFmpegSetup() { - AVCodec *video_codec = nullptr; + const AVCodec* video_codec; int ret; if (Ppacket) @@ -1834,13 +1836,17 @@ bool cmpc::CMpegEncoder::FFmpegSetup() { /* Add the audio and video streams using the default format codecs * and initialize the codecs. */ if (fmt->video_codec != AVCodecID::AV_CODEC_ID_NONE) { - if (!__add_stream(&video_codec)) { + video_codec = __add_stream(); + if (!video_codec) { FFmpegClose(); return false; } else __have_video = true; } + else { + video_codec = nullptr; + } /* Now that all the parameters are set, we can open the audio and * video codecs and allocate the necessary encode buffers. */ diff --git a/MpegCoder/MpegCoder.h b/MpegCoder/MpegCoder.h index 3ff3594..dbba0b9 100644 --- a/MpegCoder/MpegCoder.h +++ b/MpegCoder/MpegCoder.h @@ -122,9 +122,9 @@ namespace cmpc { bool _LoadFrame_castFromPyFrameArray(AVFrame *frame, PyArrayObject* PyFrame); void __log_packet(); int __write_frame(); - bool __add_stream(AVCodec **codec); + const AVCodec* __add_stream(); AVFrame* __alloc_picture(enum AVPixelFormat pix_fmt, int width, int height); - bool __open_video(AVCodec *codec, AVDictionary *opt_arg); + bool __open_video(const AVCodec* codec, const AVDictionary* opt_arg); AVFrame *__get_video_frame(PyArrayObject* PyFrame); int __avcodec_encode_video2(AVCodecContext *enc_ctx, AVPacket *pkt, AVFrame *frame); int __avcodec_encode_video2_flush(AVCodecContext *enc_ctx, AVPacket *pkt); diff --git a/MpegCoder/MpegPyd.h b/MpegCoder/MpegPyd.h index 1c6d806..4aca321 100644 --- a/MpegCoder/MpegPyd.h +++ b/MpegCoder/MpegPyd.h @@ -214,6 +214,10 @@ Yuchen's Mpeg Coder - Readme >>> d.clear() # Disconnect with the stream. For more instructions, you could tap help(mpegCoder). ================================================================================ +V3.2.0 update report: + 1. Upgrade FFMpeg to 5.0. + 2. Fix the const assignment bug caused by the codec configuration method. + 3. Upgrade the dependencies of FFMpeg to the newest versions (issue #4). V3.1.0 update report: 1. Support str() type for all string arguments. 2. Support http, ftp, sftp streams for MpegServer. diff --git a/MpegCoder/MpegStreamer.cpp b/MpegCoder/MpegStreamer.cpp index 3e136b7..40f4d76 100644 --- a/MpegCoder/MpegStreamer.cpp +++ b/MpegCoder/MpegStreamer.cpp @@ -1201,43 +1201,45 @@ int cmpc::CMpegServer::__write_frame() { } /* Add an output stream. */ -bool cmpc::CMpegServer::__add_stream(AVCodec** codec) { +const cmpc::AVCodec* cmpc::CMpegServer::__add_stream() { /* find the encoder */ AVCodecID codec_id; auto srcwidth = widthSrc > 0 ? widthSrc : width; auto srcheight = heightSrc > 0 ? heightSrc : height; - *codec = avcodec_find_encoder_by_name(codecName.c_str()); - if (!(*codec)) { + auto const_codec = avcodec_find_encoder_by_name(codecName.c_str()); + const AVCodec* codec; + if (!(const_codec)) { codec_id = PFormatCtx->oformat->video_codec; cerr << "Could not find encoder " << codecName << ", use " << avcodec_get_name(codec_id) << " as an alternative." << endl; - *codec = avcodec_find_encoder(codec_id); + codec = avcodec_find_encoder(codec_id); } else { - codec_id = (*codec)->id; - PFormatCtx->oformat->video_codec = codec_id; + codec = const_codec; + codec_id = codec->id; } - if (!(*codec)) { + + if (!codec) { cerr << "Could not find encoder for '" << avcodec_get_name(codec_id) << "'" << endl; - return false; + return nullptr; } PStreamContex.st = avformat_new_stream(PFormatCtx, nullptr); if (!PStreamContex.st) { cerr << "Could not allocate stream" << endl; - return false; + return nullptr; } PStreamContex.st->id = PFormatCtx->nb_streams - 1; - auto c = avcodec_alloc_context3(*codec); + auto c = avcodec_alloc_context3(codec); if (!c) { cerr << "Could not alloc an encoding context" << endl; - return false; + return nullptr; } if (nthread > 0) { c->thread_count = nthread; } PStreamContex.enc = c; - switch ((*codec)->type) { + switch (codec->type) { case AVMediaType::AVMEDIA_TYPE_VIDEO: c->codec_id = codec_id; @@ -1315,7 +1317,7 @@ bool cmpc::CMpegServer::__add_stream(AVCodec** codec) { /* Some formats want stream headers to be separate. */ if (PFormatCtx->oformat->flags & AVFMT_GLOBALHEADER) c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; - return true; + return codec; } /* video output */ @@ -1335,7 +1337,7 @@ cmpc::AVFrame* cmpc::CMpegServer::__alloc_picture(enum AVPixelFormat pix_fmt, in return picture; } -bool cmpc::CMpegServer::__open_video(AVCodec* codec, AVDictionary* opt_arg) { +bool cmpc::CMpegServer::__open_video(const AVCodec* codec, const AVDictionary* opt_arg) { int ret; auto c = PStreamContex.enc; AVDictionary* opt = nullptr; @@ -1987,7 +1989,7 @@ bool cmpc::CMpegServer::FFmpegSetup() { cerr << "Have not get necessary and correct configurations, so FFmpegSetup() should not be called." << endl; return false; } - AVCodec* video_codec = nullptr; + const AVCodec* video_codec; int ret; if (Ppacket) @@ -2034,13 +2036,17 @@ bool cmpc::CMpegServer::FFmpegSetup() { /* Add the audio and video streams using the default format codecs * and initialize the codecs. */ if (fmt->video_codec != AVCodecID::AV_CODEC_ID_NONE) { - if (!__add_stream(&video_codec)) { + video_codec = __add_stream(); + if (!video_codec) { FFmpegClose(); return false; } else __have_video = true; } + else { + video_codec = nullptr; + } /* Now that all the parameters are set, we can open the audio and * video codecs and allocate the necessary encode buffers. */ diff --git a/MpegCoder/MpegStreamer.h b/MpegCoder/MpegStreamer.h index 2e6606d..b9a8a32 100644 --- a/MpegCoder/MpegStreamer.h +++ b/MpegCoder/MpegStreamer.h @@ -163,9 +163,9 @@ namespace cmpc { bool _LoadFrame_castFromPyFrameArray(AVFrame* frame, PyArrayObject* PyFrame); void __log_packet(); int __write_frame(); - bool __add_stream(AVCodec** codec); + const AVCodec* __add_stream(); AVFrame* __alloc_picture(enum AVPixelFormat pix_fmt, int width, int height); - bool __open_video(AVCodec* codec, AVDictionary* opt_arg); + bool __open_video(const AVCodec* codec, const AVDictionary* opt_arg); AVFrame* __get_video_frame(PyArrayObject* PyFrame); int __avcodec_encode_video2(AVCodecContext* enc_ctx, AVPacket* pkt, AVFrame* frame); int __avcodec_encode_video2_flush(AVCodecContext* enc_ctx, AVPacket* pkt); diff --git a/README.md b/README.md index b004f6c..b3455e3 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ The following instructions are used for building the project on Windows with Vis We strongly suggest that users should also install the python dependencies (optional): - ```shell + ```shell python -m pip install -r requirements.txt ``` @@ -42,23 +42,23 @@ The following instructions are used for building the project on Windows with Vis ```shell mkdir -p /apps chmod +rwx /apps - curl -O https://raw.githubusercontent.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/deps/install-ffmpeg-4_4.sh - chmod +rwx install-ffmpeg-4_4.sh - ./install-ffmpeg-4_4.sh + curl -O https://raw.githubusercontent.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/deps/install-ffmpeg-5_0.sh + chmod +rwx install-ffmpeg-5_0.sh + ./install-ffmpeg-5_0.sh --all --nvcuda ``` After running this script, the FFMpeg with most of the dependencies would be complied along with the shared libraries. Then you could replace the FFMpeg path in the `setup.py` by ```python - FFMPEG_DIR = '/apps/build/ffmpeg-4.4' + FFMPEG_DIR = '/apps/build/ffmpeg-5.0' ``` - * Download the pre-built dependencies. These dependencies are built by myself. You could download the archive [here :package:](https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/releases/download/deps-3.0.0/dep-linux-ffmpeg_4_4.tar.xz). The files need to be extracted to `./dependencies`: + * Download the pre-built dependencies. These dependencies are built by myself. You could download the archive [here :package:](https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/releases/download/deps-3.2.0/dep-linux-ffmpeg_5_0.tar.xz). The files need to be extracted to `./dependencies`: ```shell cd FFmpeg-Encoder-Decoder-for-Python mkdir -p dependencies - wget -O- https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/releases/download/deps-3.0.0/dep-linux-ffmpeg_4_4.tar.xz | tar xJ -C "./dependencies" + wget -O- https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/releases/download/deps-3.2.0/dep-linux-ffmpeg_5_0.tar.xz | tar xJ -C "./dependencies" ``` * The dependencies could be also downloaded by the automatic script, you just need to run @@ -75,20 +75,20 @@ The following instructions are used for building the project on Windows with Vis python setup.py build ``` -5. Rename the built module as `mpegCoder.so`, then you could import it in the same directory. If you have built FFMpeg by our script, you do not need any other dependencies when importing the libs. However, if not, you may need to download [the lib dependencies :package:](https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/releases/download/deps-3.0.0/so-linux-ffmpeg_4_4.tar.xz) and add the `lib` folder to your `LD_LIBRARY_PATH`: +5. Rename the built module as `mpegCoder.so`, then you could import it in the same directory. If you have built FFMpeg by our script, you do not need any other dependencies when importing the libs. However, if not, you may need to download [the lib dependencies :package:](https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/releases/download/deps-3.2.0/so-linux-ffmpeg_5_0.tar.xz) and add the `lib` folder to your `LD_LIBRARY_PATH`: ```shell - mkdir -p /apps/ffmpeg-4.4 - cd /apps/ffmpeg-4.4 - wget -O- https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/releases/download/deps-3.0.0/so-linux-ffmpeg_4_4.tar.xz | tar xJ -C "." - echo "export LD_LIBRARY_PATH=/apps/ffmpeg-4.4/lib:\$LD_LIBRARY_PATH" >> ~/.bashrc - export LD_LIBRARY_PATH=/apps/ffmpeg-4.4/lib:$LD_LIBRARY_PATH + mkdir -p /apps/ffmpeg-5.0 + cd /apps/ffmpeg-5.0 + wget -O- https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/releases/download/deps-3.2.0/so-linux-ffmpeg_5_0.tar.xz | tar xJ -C "." + echo "export LD_LIBRARY_PATH=/apps/ffmpeg-5.0/lib:\$LD_LIBRARY_PATH" >> ~/.bashrc + export LD_LIBRARY_PATH=/apps/ffmpeg-5.0/lib:$LD_LIBRARY_PATH ``` 6. Running `mpegCoder` requires `GLIBC>=2.29`. This requirement is not satisfied in some cases. However, if you have built FFMpeg by our script, the requirement would be fulfilled (i.e. you could skip this step). If users are using our pre-built dependencies, users may need to solve this problem by ```shell - ln -sf /apps/ffmpeg-4.4/lib-fix/libm-2.31.so /lib/x86_64-linux-gnu/libm.so.6 + ln -sf /apps/ffmpeg-4.4/lib-fix/libm-2.35.so /lib/x86_64-linux-gnu/libm.so.6 ``` ## Update reports @@ -97,15 +97,15 @@ Has been moved to [:bookmark_tabs: CHANGELOG.md](./CHANGELOG.md) ## Version of currently used FFmpeg library -Current FFMpeg version is `4.4`. +Current FFMpeg version is `5.0`. | Dependency | Version | | :-------------: | :------------: | -| `libavcodec` | `58.134.100.0` | -| `libavformat` | `58.76.100.0` | -| `libavutil` | `56.70.100.0` | -| `libswresample` | `3.9.100.0` | -| `libswscale` | `5.9.100.0` | +| `libavcodec` | `59.18.100.0` | +| `libavformat` | `59.16.100.0` | +| `libavutil` | `57.17.100.0` | +| `libswresample` | `4.3.100.0` | +| `libswscale` | `6.4.100.0` | [git-master]:https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python "master (windows)" [exp1]:https://github.com/cainmagi/FFmpeg-Encoder-Decoder-for-Python/tree/example-client-check "check the client" diff --git a/requirements.txt b/requirements.txt index cb726c7..c4a2f8c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ urllib3>=1.26.6 tqdm>=4.50.0 setuptools>=50.3.2 -numpy>=1.18.5 +numpy>=1.19.5 diff --git a/setup.py b/setup.py index 77dfa51..bcaa55d 100644 --- a/setup.py +++ b/setup.py @@ -46,7 +46,7 @@ if (not os.path.isdir(os.path.join(FFMPEG_DIR, 'include'))) or (not os.path.isdir(os.path.join(FFMPEG_DIR, 'lib'))): if HAS_WEBTOOLS: print('The FFMpeg dependencies are not found. Fetch the files online...') - webtools.download_tarball('cainmagi', 'FFmpeg-Encoder-Decoder-for-Python', 'deps-3.0.0', 'dep-linux-ffmpeg_4_4.tar.xz', path=os.path.join('.', 'dependencies'), mode='auto', verbose=True, token='') + webtools.download_tarball('cainmagi', 'FFmpeg-Encoder-Decoder-for-Python', 'deps-3.2.0', 'dep-linux-ffmpeg_5_0.tar.xz', path=os.path.join('.', 'dependencies'), mode='auto', verbose=True, token='') else: raise FileNotFoundError('The required dependencies ("include" and "lib" directories) are not found in FFMPEG_DIR path ({0})'.format(FFMPEG_DIR)) @@ -54,7 +54,7 @@ name = TARGET, language = 'c++', define_macros = [('MAJOR_VERSION', '3'), - ('MINOR_VERSION', '1'), + ('MINOR_VERSION', '2'), ('BUILD_VERSION', '0')], extra_compile_args = ['-std=c++11','-pthread'], include_dirs = [PYTHON_INC_DIR, np.get_include(), '{0}/include'.format(FFMPEG_DIR), BASE_SRC_DIR], @@ -71,7 +71,7 @@ setup( name = 'mpegCoder', - version = '3.1.0', + version = '3.2.0', description = 'A FFmpeg module which could provide a class for encoding, decoding, or streaming a video in any format.', author = 'Yuchen Jin', author_email = 'cainmagi@gmail.com', @@ -84,11 +84,11 @@ classifiers=[ 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3 :: Only', - 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: 3.9', + 'Programming Language :: Python :: 3.10', 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', 'Operating System :: POSIX :: Linux', 'Topic :: Software Development :: Libraries :: Python Modules' @@ -96,7 +96,7 @@ keywords=[ 'python', 'h264', 'video', 'rtsp', 'ffmpeg', 'rtmp', 'encoder', 'numpy', 'python3', 'python3-library', 'ffmpeg-wrapper', 'video-stream', 'python-c-api', 'rtsp-push', 'rtmp-push', 'rtsp-player', 'rtmp-player', 'ffmpeg-encoder' ], - python_requires='>=3.5', + python_requires='>=3.6,<3.11', license='GPLv3', ext_modules = [module_mpegCoder] ) From a8f1c6a4389271dd96e70bc73e1693547cad57d2 Mon Sep 17 00:00:00 2001 From: cainmagi Date: Fri, 8 Apr 2022 03:10:29 -0500 Subject: [PATCH 12/15] 3.2.0 Fix two severe bugs. --- MpegCoder/MpegCoder.cpp | 4 ++-- MpegCoder/MpegStreamer.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/MpegCoder/MpegCoder.cpp b/MpegCoder/MpegCoder.cpp index 1e89ad9..7d558ff 100644 --- a/MpegCoder/MpegCoder.cpp +++ b/MpegCoder/MpegCoder.cpp @@ -1175,7 +1175,7 @@ const cmpc::AVCodec* cmpc::CMpegEncoder::__add_stream() { SCALE_FLAGS, nullptr, nullptr, nullptr); if (!PStreamContex.sws_ctx) { cerr << "Could not initialize the conversion context" << endl; - return false; + return nullptr; } } } @@ -1187,7 +1187,7 @@ const cmpc::AVCodec* cmpc::CMpegEncoder::__add_stream() { SCALE_FLAGS, nullptr, nullptr, nullptr); if (!PswsCtx) { cerr << "Could not initialize the conversion context" << endl; - return false; + return nullptr; } } if (!RGBbuffer) { diff --git a/MpegCoder/MpegStreamer.cpp b/MpegCoder/MpegStreamer.cpp index 40f4d76..deabb5c 100644 --- a/MpegCoder/MpegStreamer.cpp +++ b/MpegCoder/MpegStreamer.cpp @@ -1289,7 +1289,7 @@ const cmpc::AVCodec* cmpc::CMpegServer::__add_stream() { SCALE_FLAGS, nullptr, nullptr, nullptr); if (!PStreamContex.sws_ctx) { cerr << "Could not initialize the conversion context" << endl; - return false; + return nullptr; } } } @@ -1301,7 +1301,7 @@ const cmpc::AVCodec* cmpc::CMpegServer::__add_stream() { SCALE_FLAGS, nullptr, nullptr, nullptr); if (!PswsCtx) { cerr << "Could not initialize the conversion context" << endl; - return false; + return nullptr; } } if (!RGBbuffer) { From 215f971e2c8c2f7b07319c761c5a07de2854cb4a Mon Sep 17 00:00:00 2001 From: cainmagi Date: Fri, 8 Apr 2022 03:39:26 -0500 Subject: [PATCH 13/15] 3.2.0 Update the ignore file. --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 334f6e4..2f05d4c 100644 --- a/.gitignore +++ b/.gitignore @@ -14,7 +14,10 @@ lib/* *.lastbuildstate unsuccessfulbuild /MpegCoder/x64/ -/autobuild*.sh + +# Scripts +*.sh +!/apt-requirements.sh # Prerequisites *.d From 7655d6e41df296f1ecb48e0128be3ac20907b296 Mon Sep 17 00:00:00 2001 From: cainmagi Date: Fri, 8 Apr 2022 03:45:07 -0500 Subject: [PATCH 14/15] 3.2.0 Fix a typo in the ReadMe file. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b3455e3..ccfccd4 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ The following instructions are used for building the project on Windows with Vis 6. Running `mpegCoder` requires `GLIBC>=2.29`. This requirement is not satisfied in some cases. However, if you have built FFMpeg by our script, the requirement would be fulfilled (i.e. you could skip this step). If users are using our pre-built dependencies, users may need to solve this problem by ```shell - ln -sf /apps/ffmpeg-4.4/lib-fix/libm-2.35.so /lib/x86_64-linux-gnu/libm.so.6 + ln -sf /apps/ffmpeg-5.0/lib-fix/libm-2.35.so /lib/x86_64-linux-gnu/libm.so.6 ``` ## Update reports From 305c90e0f2bbb8d24084d3c8f4066ca160ecfe7d Mon Sep 17 00:00:00 2001 From: cainmagi Date: Fri, 8 Apr 2022 14:03:46 -0500 Subject: [PATCH 15/15] 3.2.0 Fix the description in the documentation. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ccfccd4..3366484 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ The following instructions are used for building the project on Windows with Vis 6. Running `mpegCoder` requires `GLIBC>=2.29`. This requirement is not satisfied in some cases. However, if you have built FFMpeg by our script, the requirement would be fulfilled (i.e. you could skip this step). If users are using our pre-built dependencies, users may need to solve this problem by ```shell - ln -sf /apps/ffmpeg-5.0/lib-fix/libm-2.35.so /lib/x86_64-linux-gnu/libm.so.6 + ln -sf /apps/ffmpeg-5.0/lib-fix/libm-2.31.so /lib/x86_64-linux-gnu/libm.so.6 ``` ## Update reports